// Code generated for darwin/arm64 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 -I /Users/jnml/src/modernc.org/builder/.exclude/modernc.org/libz/include/darwin/arm64 -I /Users/jnml/src/modernc.org/builder/.exclude/modernc.org/libtcl8.6/include/darwin/arm64 -extended-errors -o sqlite3.go sqlite3.c -DSQLITE_MUTEX_NOOP -DSQLITE_OS_UNIX=1 -eval-all-macros', DO NOT EDIT. //go:build darwin && arm64 // +build darwin,arm64 package sqlite3 import ( "reflect" "unsafe" "modernc.org/libc" ) var ( _ reflect.Type _ unsafe.Pointer ) const ACCESSPERMS = 511 const ACCESSX_MAX_DESCRIPTORS = 100 const ACCESSX_MAX_TABLESIZE = 16384 const ALIGNBYTES = -1 const ALLBITS = -1 const ALLPERMS = 4095 const AQ_BUFSZ = 32767 const AQ_HIWATER = 100 const AQ_LOWATER = 10 const AQ_MAXBUFSZ = 1048576 const AQ_MAXHIGH = 10000 const ARG_MAX = 1048576 const ATTRIBUTION_NAME_MAX = 255 const ATTR_BIT_MAP_COUNT = 5 const ATTR_BULK_REQUIRED = 2147483649 const ATTR_CMNEXT_ATTRIBUTION_TAG = 2048 const ATTR_CMNEXT_CLONEID = 256 const ATTR_CMNEXT_CLONE_REFCNT = 4096 const ATTR_CMNEXT_EXT_FLAGS = 512 const ATTR_CMNEXT_LINKID = 16 const ATTR_CMNEXT_NOFIRMLINKPATH = 32 const ATTR_CMNEXT_PRIVATESIZE = 8 const ATTR_CMNEXT_REALDEVID = 64 const ATTR_CMNEXT_REALFSID = 128 const ATTR_CMNEXT_RECURSIVE_GENCOUNT = 1024 const ATTR_CMNEXT_RELPATH = 4 const ATTR_CMNEXT_SETMASK = 0 const ATTR_CMNEXT_VALIDMASK = 8188 const ATTR_CMN_ACCESSMASK = 131072 const ATTR_CMN_ACCTIME = 4096 const ATTR_CMN_ADDEDTIME = 268435456 const ATTR_CMN_BKUPTIME = 8192 const ATTR_CMN_CHGTIME = 2048 const ATTR_CMN_CRTIME = 512 const ATTR_CMN_DATA_PROTECT_FLAGS = 1073741824 const ATTR_CMN_DEVID = 2 const ATTR_CMN_DOCUMENT_ID = 1048576 const ATTR_CMN_ERROR = 536870912 const ATTR_CMN_EXTENDED_SECURITY = 4194304 const ATTR_CMN_FILEID = 33554432 const ATTR_CMN_FLAGS = 262144 const ATTR_CMN_FNDRINFO = 16384 const ATTR_CMN_FSID = 4 const ATTR_CMN_FULLPATH = 134217728 const ATTR_CMN_GEN_COUNT = 524288 const ATTR_CMN_GRPID = 65536 const ATTR_CMN_GRPUUID = 16777216 const ATTR_CMN_MODTIME = 1024 const ATTR_CMN_NAME = 1 const ATTR_CMN_NAMEDATTRCOUNT = 524288 const ATTR_CMN_NAMEDATTRLIST = 1048576 const ATTR_CMN_OBJID = 32 const ATTR_CMN_OBJPERMANENTID = 64 const ATTR_CMN_OBJTAG = 16 const ATTR_CMN_OBJTYPE = 8 const ATTR_CMN_OWNERID = 32768 const ATTR_CMN_PARENTID = 67108864 const ATTR_CMN_PAROBJID = 128 const ATTR_CMN_RETURNED_ATTRS = 2147483648 const ATTR_CMN_SCRIPT = 256 const ATTR_CMN_SETMASK = 1372061440 const ATTR_CMN_USERACCESS = 2097152 const ATTR_CMN_UUID = 8388608 const ATTR_CMN_VALIDMASK = 4294967295 const ATTR_CMN_VOLSETMASK = 26368 const ATTR_DIR_ALLOCSIZE = 8 const ATTR_DIR_DATALENGTH = 32 const ATTR_DIR_ENTRYCOUNT = 2 const ATTR_DIR_IOBLOCKSIZE = 16 const ATTR_DIR_LINKCOUNT = 1 const ATTR_DIR_MOUNTSTATUS = 4 const ATTR_DIR_SETMASK = 0 const ATTR_DIR_VALIDMASK = 63 const ATTR_FILE_ALLOCSIZE = 4 const ATTR_FILE_CLUMPSIZE = 16 const ATTR_FILE_DATAALLOCSIZE = 1024 const ATTR_FILE_DATAEXTENTS = 2048 const ATTR_FILE_DATALENGTH = 512 const ATTR_FILE_DEVTYPE = 32 const ATTR_FILE_FILETYPE = 64 const ATTR_FILE_FORKCOUNT = 128 const ATTR_FILE_FORKLIST = 256 const ATTR_FILE_IOBLOCKSIZE = 8 const ATTR_FILE_LINKCOUNT = 1 const ATTR_FILE_RSRCALLOCSIZE = 8192 const ATTR_FILE_RSRCEXTENTS = 16384 const ATTR_FILE_RSRCLENGTH = 4096 const ATTR_FILE_SETMASK = 32 const ATTR_FILE_TOTALSIZE = 2 const ATTR_FILE_VALIDMASK = 14335 const ATTR_FORK_ALLOCSIZE = 2 const ATTR_FORK_RESERVED = 4294967295 const ATTR_FORK_SETMASK = 0 const ATTR_FORK_TOTALSIZE = 1 const ATTR_FORK_VALIDMASK = 3 const ATTR_MAX_BUFFER = 8192 const ATTR_VOL_ALLOCATIONCLUMP = 64 const ATTR_VOL_ATTRIBUTES = 1073741824 const ATTR_VOL_CAPABILITIES = 131072 const ATTR_VOL_DIRCOUNT = 1024 const ATTR_VOL_ENCODINGSUSED = 65536 const ATTR_VOL_FILECOUNT = 512 const ATTR_VOL_FSSUBTYPE = 2097152 const ATTR_VOL_FSTYPE = 1 const ATTR_VOL_FSTYPENAME = 1048576 const ATTR_VOL_INFO = 2147483648 const ATTR_VOL_IOBLOCKSIZE = 128 const ATTR_VOL_MAXOBJCOUNT = 2048 const ATTR_VOL_MINALLOCATION = 32 const ATTR_VOL_MOUNTEDDEVICE = 32768 const ATTR_VOL_MOUNTFLAGS = 16384 const ATTR_VOL_MOUNTPOINT = 4096 const ATTR_VOL_NAME = 8192 const ATTR_VOL_OBJCOUNT = 256 const ATTR_VOL_QUOTA_SIZE = 268435456 const ATTR_VOL_RESERVED_SIZE = 536870912 const ATTR_VOL_SETMASK = 2147491840 const ATTR_VOL_SIGNATURE = 2 const ATTR_VOL_SIZE = 4 const ATTR_VOL_SPACEAVAIL = 16 const ATTR_VOL_SPACEFREE = 8 const ATTR_VOL_SPACEUSED = 8388608 const ATTR_VOL_UUID = 262144 const ATTR_VOL_VALIDMASK = 4038590463 const AT_EACCESS = 16 const AT_FDCWD = -2 const AT_FDONLY = 1024 const AT_REALDEV = 512 const AT_REMOVEDIR = 128 const AT_SYMLINK_FOLLOW = 64 const AT_SYMLINK_NOFOLLOW = 32 const AT_SYMLINK_NOFOLLOW_ANY = 2048 const AUC_AUDITING = 1 const AUC_DISABLED = -1 const AUC_NOAUDIT = 2 const AUC_UNSET = 0 const AUDITDEV_FILENAME = "audit" const AUDIT_AHLT = 2 const AUDIT_ARGE = 8 const AUDIT_ARGV = 4 const AUDIT_CNT = 1 const AUDIT_GROUP = 128 const AUDIT_HARD_LIMIT_FREE_BLOCKS = 4 const AUDIT_PATH = 512 const AUDIT_PERZONE = 8192 const AUDIT_PUBLIC = 2048 const AUDIT_RECORD_MAGIC = 2190085915 const AUDIT_SCNT = 1024 const AUDIT_SEQ = 16 const AUDIT_TRAIL = 256 const AUDIT_TRIGGER_CLOSE_AND_DIE = 4 const AUDIT_TRIGGER_EXPIRE_TRAILS = 8 const AUDIT_TRIGGER_INITIALIZE = 7 const AUDIT_TRIGGER_LOW_SPACE = 1 const AUDIT_TRIGGER_MAX = 8 const AUDIT_TRIGGER_MIN = 1 const AUDIT_TRIGGER_NO_SPACE = 5 const AUDIT_TRIGGER_READ_FILE = 3 const AUDIT_TRIGGER_ROTATE_KERNEL = 2 const AUDIT_TRIGGER_ROTATE_USER = 6 const AUDIT_USER = 64 const AUDIT_WINDATA = 32 const AUDIT_ZONENAME = 4096 const AUTH_OPEN_NOAUTHFD = -1 const AU_ASSIGN_ASID = -1 const AU_CLASS_MASK_RESERVED = 268435456 const AU_DEFAUDITSID = 0 const AU_FS_MINFREE = 20 const AU_IPv4 = 4 const AU_IPv6 = 16 const A_GETCAR = 9 const A_GETCLASS = 22 const A_GETCOND = 37 const A_GETCTLMODE = 41 const A_GETCWD = 8 const A_GETEXPAFTER = 43 const A_GETFSIZE = 27 const A_GETKAUDIT = 29 const A_GETKMASK = 4 const A_GETPINFO = 24 const A_GETPINFO_ADDR = 28 const A_GETPOLICY = 33 const A_GETQCTRL = 35 const A_GETSFLAGS = 39 const A_GETSINFO_ADDR = 32 const A_GETSTAT = 12 const A_OLDGETCOND = 20 const A_OLDGETPOLICY = 2 const A_OLDGETQCTRL = 6 const A_OLDSETCOND = 21 const A_OLDSETPOLICY = 3 const A_OLDSETQCTRL = 7 const A_SENDTRIGGER = 31 const A_SETCLASS = 23 const A_SETCOND = 38 const A_SETCTLMODE = 42 const A_SETEXPAFTER = 44 const A_SETFSIZE = 26 const A_SETKAUDIT = 30 const A_SETKMASK = 5 const A_SETPMASK = 25 const A_SETPOLICY = 34 const A_SETQCTRL = 36 const A_SETSFLAGS = 40 const A_SETSMASK = 15 const A_SETSTAT = 13 const A_SETUMASK = 14 const BADSIG = "SIG_ERR" const BC_BASE_MAX = 99 const BC_DIM_MAX = 2048 const BC_SCALE_MAX = 99 const BC_STRING_MAX = 1000 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 BLKDEV_IOSIZE = 2048 const BSD = 199506 const BSD4_3 = 1 const BSD4_4 = 1 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 = 1024 const BUS_ADRALN = 1 const BUS_ADRERR = 2 const BUS_NOOP = 0 const BUS_OBJERR = 3 const BYTE_ORDER = 1234 const CACHE_STALE = 0 const CBLOCK = 64 const CBQSIZE = 8 const CBSIZE = 56 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 CHARCLASS_NAME_MAX = 14 const CHILD_MAX = 266 const CKCNSTRNT_COLUMN = 1 const CKCNSTRNT_ROWID = 2 const CLBYTES = 4096 const CLD_CONTINUED = 6 const CLD_DUMPED = 3 const CLD_EXITED = 1 const CLD_KILLED = 2 const CLD_NOOP = 0 const CLD_STOPPED = 5 const CLD_TRAPPED = 4 const CLOCK_MONOTONIC = 0 const CLOCK_MONOTONIC_RAW = 0 const CLOCK_MONOTONIC_RAW_APPROX = 0 const CLOCK_PROCESS_CPUTIME_ID = 0 const CLOCK_REALTIME = 0 const CLOCK_THREAD_CPUTIME_ID = 0 const CLOCK_UPTIME_RAW = 0 const CLOCK_UPTIME_RAW_APPROX = 0 const CLOFF = 4095 const CLOFSET = 4095 const CLSHIFT = 12 const CLSIZE = 1 const CLSIZELOG2 = 0 const CMASK = 18 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 COLL_WEIGHTS_MAX = 2 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 CPF_IGNORE_MODE = 2 const CPF_MASK = 3 const CPF_OVERWRITE = 1 const CPUMON_MAKE_FATAL = 4096 const CRF_MAC_ENFORCE = 2 const CRF_NOMEMBERD = 1 const CROUND = 63 const CRYPTEX_AUTH_STRUCT_VERSION = 1 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 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 DEV_BSHIFT = 9 const DEV_BSIZE = 512 const DIRECT_MODE = 0 const DIR_MNTSTATUS_MNTPOINT = 1 const DIR_MNTSTATUS_TRIGGER = 2 const DOMAIN = 1 const DOTLOCK_SUFFIX = ".lock" const DST_AUST = 2 const DST_CAN = 6 const DST_EET = 5 const DST_MET = 4 const DST_NONE = 0 const DST_USA = 1 const DST_WET = 3 const DYNAMIC_TARGETS_ENABLED = 0 const E2BIG = 7 const EACCES = 13 const EADDRINUSE = 48 const EADDRNOTAVAIL = 49 const EAFNOSUPPORT = 47 const EAGAIN = 35 const EALREADY = 37 const EAUTH = 80 const EBADARCH = 86 const EBADEXEC = 85 const EBADF = 9 const EBADMACHO = 88 const EBADMSG = 94 const EBADRPC = 72 const EBUSY = 16 const ECANCELED = 89 const ECHILD = 10 const ECONNABORTED = 53 const ECONNREFUSED = 61 const ECONNRESET = 54 const EDEADLK = 11 const EDESTADDRREQ = 39 const EDEVERR = 83 const EDOM = 33 const EDQUOT = 69 const EEXIST = 17 const EFAULT = 14 const EFBIG = 27 const EFTYPE = 79 const EF_IS_PURGEABLE = 8 const EF_IS_SPARSE = 16 const EF_IS_SYNC_ROOT = 4 const EF_IS_SYNTHETIC = 32 const EF_MAY_SHARE_BLOCKS = 1 const EF_NO_XATTRS = 2 const EF_SHARES_ALL_BLOCKS = 64 const EHOSTDOWN = 64 const EHOSTUNREACH = 65 const EIDRM = 90 const EILSEQ = 92 const EINPROGRESS = 36 const EINTR = 4 const EINVAL = 22 const EIO = 5 const EISCONN = 56 const EISDIR = 21 const ELAST = 106 const ELOOP = 62 const EMFILE = 24 const EMLINK = 31 const EMSGSIZE = 40 const EMULTIHOP = 95 const ENAMETOOLONG = 63 const ENAME_NAME = 0 const ENAME_ROWID = 3 const ENAME_SPAN = 1 const ENAME_TAB = 2 const ENEEDAUTH = 81 const ENETDOWN = 50 const ENETRESET = 52 const ENETUNREACH = 51 const ENFILE = 23 const ENOATTR = 93 const ENOBUFS = 55 const ENODATA = 96 const ENODEV = 19 const ENOENT = 2 const ENOEXEC = 8 const ENOLCK = 77 const ENOLINK = 97 const ENOMEM = 12 const ENOMSG = 91 const ENOPOLICY = 103 const ENOPROTOOPT = 42 const ENOSPC = 28 const ENOSR = 98 const ENOSTR = 99 const ENOSYS = 78 const ENOTBLK = 15 const ENOTCONN = 57 const ENOTDIR = 20 const ENOTEMPTY = 66 const ENOTRECOVERABLE = 104 const ENOTSOCK = 38 const ENOTSUP = 45 const ENOTTY = 25 const ENXIO = 6 const EOF = -1 const EOPNOTSUPP = 102 const EOVERFLOW = 84 const EOWNERDEAD = 105 const EPERM = 1 const EPFNOSUPPORT = 46 const EPIPE = 32 const EPROCLIM = 67 const EPROCUNAVAIL = 76 const EPROGMISMATCH = 75 const EPROGUNAVAIL = 74 const EPROTO = 100 const EPROTONOSUPPORT = 43 const EPROTOTYPE = 41 const EPWROFF = 82 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 EQFULL = 106 const EQUIV_CLASS_MAX = 2 const ERANGE = 34 const EREMOTE = 71 const EROFS = 30 const ERPCMISMATCH = 73 const ESHLIBVERS = 87 const ESHUTDOWN = 58 const ESOCKTNOSUPPORT = 44 const ESPIPE = 29 const ESRCH = 3 const ESTALE = 70 const ETIME = 101 const ETIMEDOUT = 60 const ETOOMANYREFS = 59 const ETXTBSY = 26 const EU4_EXPR = 2 const EU4_IDX = 1 const EU4_NONE = 0 const EUSERS = 68 const EWOULDBLOCK = 35 const EXCLUDED_TABLE_NUMBER = 2 const EXCLUSIVE_LOCK = 4 const EXDEV = 18 const EXIT_FAILURE = 1 const EXIT_SUCCESS = 0 const EXPRDUP_REDUCE = 1 const EXPR_FULLSIZE = 0 const EXPR_NEST_MAX = 32 const FALSE = 0 const FAPPEND = 8 const FASYNC = 64 const FCNTL_FS_SPECIFIC_BASE = 65536 const FD_CLOEXEC = 1 const FD_SETSIZE = 1024 const FFDSYNC = 4194304 const FFSYNC = 128 const FILENAME_MAX = 1024 const FILESEC_GUID = 0 const FLAG_SIGNED = 1 const FLAG_STRING = 4 const FNDELAY = 4 const FNONBLOCK = 4 const FOOTPRINT_INTERVAL_RESET = 1 const FOPEN_MAX = 20 const FPE_FLTDIV = 1 const FPE_FLTINV = 5 const FPE_FLTOVF = 2 const FPE_FLTRES = 4 const FPE_FLTSUB = 6 const FPE_FLTUND = 3 const FPE_INTDIV = 7 const FPE_INTOVF = 8 const FPE_NOOP = 0 const FP_FAST_FMA = 1 const FP_FAST_FMAF = 1 const FP_FAST_FMAL = 1 const FP_ILOGB0 = -2147483648 const FP_ILOGBNAN = -2147483648 const FP_INFINITE = 2 const FP_NAN = 1 const FP_NORMAL = 4 const FP_QNAN = 1 const FP_SNAN = 1 const FP_SUBNORMAL = 5 const FP_SUPERNORMAL = 6 const FP_ZERO = 3 const FREAD = 1 const FSCALE = 2048 const FSCRED = -1 const FSHIFT = 11 const FSOPT_ATTR_CMN_EXTENDED = 32 const FSOPT_NOFOLLOW = 1 const FSOPT_NOFOLLOW_ANY = 2048 const FSOPT_NOINMEMUPDATE = 2 const FSOPT_PACK_INVAL_ATTRS = 8 const FSOPT_REPORT_FULLSIZE = 4 const FSOPT_RETURN_REALDEV = 512 const FST_EOF = -1 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 FWRITE = 2 const F_ADDFILESIGS = 61 const F_ADDFILESIGS_FOR_DYLD_SIM = 83 const F_ADDFILESIGS_INFO = 103 const F_ADDFILESIGS_RETURN = 97 const F_ADDFILESUPPL = 104 const F_ADDSIGS = 59 const F_ALLOCATEALL = 4 const F_ALLOCATECONTIG = 2 const F_ALLOCATEPERSIST = 8 const F_ATTRIBUTION_TAG = 111 const F_BARRIERFSYNC = 85 const F_CHECK_LV = 98 const F_CHKCLEAN = 41 const F_CREATE_TAG = 1 const F_DELETE_TAG = 2 const F_DUPFD = 0 const F_DUPFD_CLOEXEC = 67 const F_FINDSIGS = 78 const F_FLUSH_DATA = 40 const F_FREEZE_FS = 53 const F_FULLFSYNC = 51 const F_GETCODEDIR = 72 const F_GETFD = 1 const F_GETFL = 3 const F_GETLEASE = 107 const F_GETLK = 7 const F_GETLKPID = 66 const F_GETNOSIGPIPE = 74 const F_GETOWN = 5 const F_GETPATH = 50 const F_GETPATH_MTMINFO = 71 const F_GETPATH_NOFIRMLINK = 102 const F_GETPROTECTIONCLASS = 63 const F_GETPROTECTIONLEVEL = 77 const F_GETSIGSINFO = 105 const F_GLOBAL_NOCACHE = 55 const F_LOCK = 1 const F_LOG2PHYS = 49 const F_LOG2PHYS_EXT = 65 const F_NOCACHE = 48 const F_NODIRECT = 62 const F_OFD_GETLK = 92 const F_OFD_SETLK = 90 const F_OFD_SETLKW = 91 const F_OFD_SETLKWTIMEOUT = 93 const F_OK = 0 const F_PATHPKG_CHECK = 52 const F_PEOFPOSMODE = 3 const F_PREALLOCATE = 42 const F_PUNCHHOLE = 99 const F_QUERY_TAG = 4 const F_RDADVISE = 44 const F_RDAHEAD = 45 const F_RDLCK = 1 const F_SETBACKINGSTORE = 70 const F_SETFD = 2 const F_SETFL = 4 const F_SETLEASE = 106 const F_SETLK = 8 const F_SETLKW = 9 const F_SETLKWTIMEOUT = 10 const F_SETNOSIGPIPE = 73 const F_SETOWN = 6 const F_SETPROTECTIONCLASS = 64 const F_SETSIZE = 43 const F_SINGLE_WRITER = 76 const F_SPECULATIVE_READ = 101 const F_TEST = 3 const F_THAW_FS = 54 const F_TLOCK = 2 const F_TRANSCODEKEY = 75 const F_TRANSFEREXTENTS = 110 const F_TRIM_ACTIVE_FILE = 100 const F_ULOCK = 0 const F_UNLCK = 2 const F_VOLPOSMODE = 4 const F_WRLCK = 3 const GCC_VERSION = 4002001 const GEOPOLY_PI = 3.141592653589793 const GETSIGSINFO_PLATFORM_BINARY = 1 const GID_MAX = 2147483647 const GRAFTDMG_SECURE_BOOT_CRYPTEX_ARGS_VERSION = 1 const GUARD_TYPE_MACH_PORT = 1 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 = 1 const HAVE_LSTAT = 1 const HAVE_MREMAP = 0 const HAVE_PREAD = 1 const HAVE_PWRITE = 1 const HAVE_READLINK = 1 const HAVE_USLEEP = 1 const HUGE = 0 const HUGE_VAL = 0 const HUGE_VALF = 0 const HUGE_VALL = 0 const ILL_BADSTK = 8 const ILL_COPROC = 7 const ILL_ILLADR = 5 const ILL_ILLOPC = 1 const ILL_ILLOPN = 4 const ILL_ILLTRP = 2 const ILL_NOOP = 0 const ILL_PRVOPC = 3 const ILL_PRVREG = 6 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 INT16_MAX = 32767 const INT16_MIN = -32768 const INT32_MAX = 2147483647 const INT32_MIN = -2147483648 const INT64_MAX = 9223372036854775807 const INT64_MIN = -9223372036854775808 const INT8_MAX = 127 const INT8_MIN = -128 const INTERFACE = 1 const INTMAX_MAX = 9223372036854775807 const INTMAX_MIN = -9223372036854775808 const INTPTR_MAX = 9223372036854775807 const INTPTR_MIN = -9223372036854775808 const INT_FAST16_MAX = 32767 const INT_FAST16_MIN = -32768 const INT_FAST32_MAX = 2147483647 const INT_FAST32_MIN = -2147483648 const INT_FAST64_MAX = 9223372036854775807 const INT_FAST64_MIN = -9223372036854775808 const INT_FAST8_MAX = 127 const INT_FAST8_MIN = -128 const INT_LEAST16_MAX = 32767 const INT_LEAST16_MIN = -32768 const INT_LEAST32_MAX = 2147483647 const INT_LEAST32_MIN = -2147483648 const INT_LEAST64_MAX = 9223372036854775807 const INT_LEAST64_MIN = -9223372036854775808 const INT_LEAST8_MAX = 127 const INT_LEAST8_MIN = -128 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 IOCPARM_MASK = 8191 const IOCPARM_MAX = 8192 const IOPOL_APPLICATION = 5 const IOPOL_ATIME_UPDATES_DEFAULT = 0 const IOPOL_ATIME_UPDATES_OFF = 1 const IOPOL_DEFAULT = 0 const IOPOL_IMPORTANT = 1 const IOPOL_MATERIALIZE_DATALESS_FILES_DEFAULT = 0 const IOPOL_MATERIALIZE_DATALESS_FILES_OFF = 1 const IOPOL_MATERIALIZE_DATALESS_FILES_ON = 2 const IOPOL_NORMAL = 1 const IOPOL_PASSIVE = 2 const IOPOL_SCOPE_DARWIN_BG = 2 const IOPOL_SCOPE_PROCESS = 0 const IOPOL_SCOPE_THREAD = 1 const IOPOL_STANDARD = 5 const IOPOL_THROTTLE = 3 const IOPOL_TYPE_DISK = 0 const IOPOL_TYPE_VFS_ALLOW_LOW_SPACE_WRITES = 9 const IOPOL_TYPE_VFS_ATIME_UPDATES = 2 const IOPOL_TYPE_VFS_DISALLOW_RW_FOR_O_EVTONLY = 10 const IOPOL_TYPE_VFS_IGNORE_CONTENT_PROTECTION = 6 const IOPOL_TYPE_VFS_IGNORE_PERMISSIONS = 7 const IOPOL_TYPE_VFS_MATERIALIZE_DATALESS_FILES = 3 const IOPOL_TYPE_VFS_SKIP_MTIME_UPDATE = 8 const IOPOL_TYPE_VFS_STATFS_NO_DATA_VOLUME = 4 const IOPOL_TYPE_VFS_TRIGGER_RESOLVE = 5 const IOPOL_UTILITY = 4 const IOPOL_VFS_ALLOW_LOW_SPACE_WRITES_OFF = 0 const IOPOL_VFS_ALLOW_LOW_SPACE_WRITES_ON = 1 const IOPOL_VFS_CONTENT_PROTECTION_DEFAULT = 0 const IOPOL_VFS_CONTENT_PROTECTION_IGNORE = 1 const IOPOL_VFS_DISALLOW_RW_FOR_O_EVTONLY_DEFAULT = 0 const IOPOL_VFS_DISALLOW_RW_FOR_O_EVTONLY_ON = 1 const IOPOL_VFS_IGNORE_PERMISSIONS_OFF = 0 const IOPOL_VFS_IGNORE_PERMISSIONS_ON = 1 const IOPOL_VFS_NOCACHE_WRITE_FS_BLKSIZE_DEFAULT = 0 const IOPOL_VFS_NOCACHE_WRITE_FS_BLKSIZE_ON = 1 const IOPOL_VFS_SKIP_MTIME_UPDATE_OFF = 0 const IOPOL_VFS_SKIP_MTIME_UPDATE_ON = 1 const IOPOL_VFS_STATFS_FORCE_NO_DATA_VOLUME = 1 const IOPOL_VFS_STATFS_NO_DATA_VOLUME_DEFAULT = 0 const IOPOL_VFS_TRIGGER_RESOLVE_DEFAULT = 0 const IOPOL_VFS_TRIGGER_RESOLVE_OFF = 1 const ITIMER_PROF = 2 const ITIMER_REAL = 0 const ITIMER_VIRTUAL = 1 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 LINE_MAX = 2048 const LINK_MAX = 32767 const LITTLE_ENDIAN = 1234 const LOCATE_NOERR = 2 const LOCATE_VIEW = 1 const LOCK_EX = 2 const LOCK_NB = 4 const LOCK_SH = 1 const LOCK_UN = 8 const LONGDOUBLE_TYPE = 0 const LOOKASIDE_SMALL = 128 const L_INCR = 1 const L_SET = 0 const L_XTND = 2 const L_ctermid = 1024 const L_tmpnam = 1024 const M10d_Any = 1 const M10d_No = 2 const M10d_Yes = 0 const M16KCLBYTES = 16384 const M16KCLSHIFT = 14 const MACH_MSG_TYPE_INTEGER_T = 0 const MACH_PORT_DENAP_RECEIVER = 6 const MACH_PORT_DNREQUESTS_SIZE = 3 const MACH_PORT_DNREQUESTS_SIZE_COUNT = 1 const MACH_PORT_GUARD_INFO = 8 const MACH_PORT_IMPORTANCE_RECEIVER = 5 const MACH_PORT_INFO_EXT = 7 const MACH_PORT_LIMITS_INFO = 1 const MACH_PORT_NULL = 0 const MACH_PORT_QLIMIT_BASIC = 5 const MACH_PORT_QLIMIT_DEFAULT = 5 const MACH_PORT_QLIMIT_KERNEL = 65534 const MACH_PORT_QLIMIT_LARGE = 1024 const MACH_PORT_QLIMIT_MAX = 1024 const MACH_PORT_QLIMIT_MIN = 0 const MACH_PORT_QLIMIT_SMALL = 16 const MACH_PORT_QLIMIT_ZERO = 0 const MACH_PORT_RECEIVE_STATUS = 2 const MACH_PORT_SERVICE_THROTTLED = 9 const MACH_PORT_SERVICE_THROTTLED_COUNT = 1 const MACH_PORT_SRIGHTS_NONE = 0 const MACH_PORT_SRIGHTS_PRESENT = 1 const MACH_PORT_STATUS_FLAG_GUARDED = 2 const MACH_PORT_STATUS_FLAG_GUARD_IMMOVABLE_RECEIVE = 64 const MACH_PORT_STATUS_FLAG_IMP_DONATION = 8 const MACH_PORT_STATUS_FLAG_NO_GRANT = 128 const MACH_PORT_STATUS_FLAG_REVIVE = 16 const MACH_PORT_STATUS_FLAG_STRICT_GUARD = 4 const MACH_PORT_STATUS_FLAG_TASKPTR = 32 const MACH_PORT_STATUS_FLAG_TEMPOWNER = 1 const MACH_PORT_TEMPOWNER = 4 const MACH_PORT_TYPE_DNREQUEST = 2147483648 const MACH_PORT_TYPE_SPREQUEST = 1073741824 const MACH_PORT_TYPE_SPREQUEST_DELAYED = 536870912 const MACH_SERVICE_PORT_INFO_STRING_NAME_MAX_BUF_LEN = 255 const MAC_OS_VERSION_11_0 = 110000 const MAC_OS_VERSION_11_1 = 110100 const MAC_OS_VERSION_11_3 = 110300 const MAC_OS_VERSION_11_4 = 110400 const MAC_OS_VERSION_11_5 = 110500 const MAC_OS_VERSION_11_6 = 110600 const MAC_OS_VERSION_12_0 = 120000 const MAC_OS_VERSION_12_1 = 120100 const MAC_OS_VERSION_12_2 = 120200 const MAC_OS_VERSION_12_3 = 120300 const MAC_OS_VERSION_12_4 = 120400 const MAC_OS_VERSION_12_5 = 120500 const MAC_OS_VERSION_12_6 = 120600 const MAC_OS_VERSION_12_7 = 120700 const MAC_OS_VERSION_13_0 = 130000 const MAC_OS_VERSION_13_1 = 130100 const MAC_OS_VERSION_13_2 = 130200 const MAC_OS_VERSION_13_3 = 130300 const MAC_OS_VERSION_13_4 = 130400 const MAC_OS_VERSION_13_5 = 130500 const MAC_OS_VERSION_13_6 = 130600 const MAC_OS_VERSION_14_0 = 140000 const MAC_OS_VERSION_14_1 = 140100 const MAC_OS_VERSION_14_2 = 140200 const MAC_OS_VERSION_14_3 = 140300 const MAC_OS_VERSION_14_4 = 140400 const MAC_OS_X_VERSION_10_0 = 1000 const MAC_OS_X_VERSION_10_1 = 1010 const MAC_OS_X_VERSION_10_10 = 101000 const MAC_OS_X_VERSION_10_10_2 = 101002 const MAC_OS_X_VERSION_10_10_3 = 101003 const MAC_OS_X_VERSION_10_11 = 101100 const MAC_OS_X_VERSION_10_11_2 = 101102 const MAC_OS_X_VERSION_10_11_3 = 101103 const MAC_OS_X_VERSION_10_11_4 = 101104 const MAC_OS_X_VERSION_10_12 = 101200 const MAC_OS_X_VERSION_10_12_1 = 101201 const MAC_OS_X_VERSION_10_12_2 = 101202 const MAC_OS_X_VERSION_10_12_4 = 101204 const MAC_OS_X_VERSION_10_13 = 101300 const MAC_OS_X_VERSION_10_13_1 = 101301 const MAC_OS_X_VERSION_10_13_2 = 101302 const MAC_OS_X_VERSION_10_13_4 = 101304 const MAC_OS_X_VERSION_10_14 = 101400 const MAC_OS_X_VERSION_10_14_1 = 101401 const MAC_OS_X_VERSION_10_14_4 = 101404 const MAC_OS_X_VERSION_10_14_5 = 101405 const MAC_OS_X_VERSION_10_14_6 = 101406 const MAC_OS_X_VERSION_10_15 = 101500 const MAC_OS_X_VERSION_10_15_1 = 101501 const MAC_OS_X_VERSION_10_15_4 = 101504 const MAC_OS_X_VERSION_10_16 = 101600 const MAC_OS_X_VERSION_10_2 = 1020 const MAC_OS_X_VERSION_10_3 = 1030 const MAC_OS_X_VERSION_10_4 = 1040 const MAC_OS_X_VERSION_10_5 = 1050 const MAC_OS_X_VERSION_10_6 = 1060 const MAC_OS_X_VERSION_10_7 = 1070 const MAC_OS_X_VERSION_10_8 = 1080 const MAC_OS_X_VERSION_10_9 = 1090 const MADV_CAN_REUSE = 9 const MADV_DONTNEED = 4 const MADV_FREE = 5 const MADV_FREE_REUSABLE = 7 const MADV_FREE_REUSE = 8 const MADV_NORMAL = 0 const MADV_PAGEOUT = 10 const MADV_RANDOM = 1 const MADV_SEQUENTIAL = 2 const MADV_WILLNEED = 3 const MADV_ZERO = 11 const MADV_ZERO_WIRED_PAGES = 6 const MAP_32BIT = 32768 const MAP_ANON = 4096 const MAP_ANONYMOUS = 4096 const MAP_COPY = 2 const MAP_FAILED = -1 const MAP_FILE = 0 const MAP_FIXED = 16 const MAP_HASSEMAPHORE = 512 const MAP_JIT = 2048 const MAP_NOCACHE = 1024 const MAP_NOEXTEND = 256 const MAP_NORESERVE = 64 const MAP_PRIVATE = 2 const MAP_RENAME = 32 const MAP_RESERVED0080 = 128 const MAP_RESILIENT_CODESIGN = 8192 const MAP_RESILIENT_MEDIA = 16384 const MAP_SHARED = 1 const MAP_TPRO = 524288 const MAP_TRANSLATED_ALLOW_EXECUTE = 131072 const MAP_UNIX03 = 262144 const MATH_ERREXCEPT = 2 const MATH_ERRNO = 1 const MAXAUDITDATA = 32767 const MAXBSIZE = 1048576 const MAXCOMLEN = 16 const MAXDOMNAMELEN = 256 const MAXFLOAT = 0 const MAXFRAG = 8 const MAXHOSTNAMELEN = 256 const MAXINTERP = 64 const MAXLOGNAME = 255 const MAXPATHLEN = 1024 const MAXPHYS = 65536 const MAXPHYSIO = 65536 const MAXPHYSIO_WIRED = 16777216 const MAXPRI = 127 const MAXSYMLINKS = 32 const MAXUPRC = 266 const MAX_AUDIT_RECORDS = 20 const MAX_AUDIT_RECORD_SIZE = 32767 const MAX_CANON = 1024 const MAX_FATAL_kGUARD_EXC_CODE = 128 const MAX_GRAFT_ARGS_SIZE = 512 const MAX_INPUT = 1024 const MAX_OPTIONAL_kGUARD_EXC_CODE = 524288 const MAX_PATHNAME = 512 const MAX_SECTOR_SIZE = 65536 const MBIGCLBYTES = 4096 const MBIGCLSHIFT = 12 const MB_CUR_MAX = 0 const MB_LEN_MAX = 1 const MCLBYTES = 2048 const MCLOFSET = 2047 const MCLSHIFT = 11 const MCL_CURRENT = 1 const MCL_FUTURE = 2 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 MFSNAMELEN = 15 const MFSTYPENAMELEN = 16 const MINCORE_ANONYMOUS = 128 const MINCORE_COPIED = 64 const MINCORE_INCORE = 1 const MINCORE_MODIFIED = 4 const MINCORE_MODIFIED_OTHER = 16 const MINCORE_PAGED_OUT = 32 const MINCORE_REFERENCED = 2 const MINCORE_REFERENCED_OTHER = 8 const MINSIGSTKSZ = 32768 const MIN_AUDIT_FILE_SIZE = 524288 const MNAMELEN = 1024 const MNT_ASYNC = 64 const MNT_AUTOMOUNTED = 4194304 const MNT_CMDFLAGS = 983040 const MNT_CPROTECT = 128 const MNT_DEFWRITE = 33554432 const MNT_DONTBROWSE = 1048576 const MNT_DOVOLFS = 32768 const MNT_DWAIT = 4 const MNT_EXPORTED = 256 const MNT_EXT_FSKIT = 2 const MNT_EXT_ROOT_DATA_VOL = 1 const MNT_FORCE = 524288 const MNT_IGNORE_OWNERSHIP = 2097152 const MNT_JOURNALED = 8388608 const MNT_LOCAL = 4096 const MNT_MULTILABEL = 67108864 const MNT_NOATIME = 268435456 const MNT_NOBLOCK = 131072 const MNT_NODEV = 16 const MNT_NOEXEC = 4 const MNT_NOFOLLOW = 134217728 const MNT_NOSUID = 8 const MNT_NOUSERXATTR = 16777216 const MNT_NOWAIT = 2 const MNT_QUARANTINE = 1024 const MNT_QUOTA = 8192 const MNT_RDONLY = 1 const MNT_RELOAD = 262144 const MNT_REMOVABLE = 512 const MNT_ROOTFS = 16384 const MNT_SNAPSHOT = 1073741824 const MNT_STRICTATIME = 2147483648 const MNT_SYNCHRONOUS = 2 const MNT_UNION = 32 const MNT_UNKNOWNPERMISSIONS = 2097152 const MNT_UPDATE = 65536 const MNT_VISFLAGMASK = 3757111295 const MNT_WAIT = 1 const MPG_FLAGS_IMMOVABLE_PINNED = 72057594037927936 const MPG_FLAGS_MOD_REFS_PINNED_COPYIN = 288230376151711744 const MPG_FLAGS_MOD_REFS_PINNED_DEALLOC = 72057594037927936 const MPG_FLAGS_MOD_REFS_PINNED_DESTROY = 144115188075855872 const MPG_FLAGS_NONE = 0 const MPG_FLAGS_STRICT_REPLY_INVALID_REPLY_DISP = 72057594037927936 const MPG_FLAGS_STRICT_REPLY_INVALID_REPLY_PORT = 144115188075855872 const MPG_FLAGS_STRICT_REPLY_INVALID_VOUCHER = 288230376151711744 const MPG_FLAGS_STRICT_REPLY_MASK = 18374686479671623680 const MPG_FLAGS_STRICT_REPLY_MISMATCHED_PERSONA = 1152921504606846976 const MPG_FLAGS_STRICT_REPLY_NO_BANK_ATTR = 576460752303423488 const MPG_IMMOVABLE_RECEIVE = 2 const MPG_STRICT = 1 const MPO_CONNECTION_PORT = 2048 const MPO_CONTEXT_AS_GUARD = 1 const MPO_DENAP_RECEIVER = 64 const MPO_ENFORCE_REPLY_PORT_SEMANTICS = 8192 const MPO_FILTER_MSG = 256 const MPO_IMMOVABLE_RECEIVE = 128 const MPO_IMPORTANCE_RECEIVER = 8 const MPO_INSERT_SEND_RIGHT = 16 const MPO_PROVISIONAL_ID_PROT_OPTOUT = 32768 const MPO_PROVISIONAL_REPLY_PORT = 16384 const MPO_QLIMIT = 2 const MPO_REPLY_PORT = 4096 const MPO_SERVICE_PORT = 1024 const MPO_STRICT = 32 const MPO_TEMPOWNER = 4 const MPO_TG_BLOCK_TRACKING = 512 const MSIZE = 256 const MSIZESHIFT = 8 const MSVC_VERSION = 0 const MS_ASYNC = 1 const MS_DEACTIVATE = 8 const MS_INVALIDATE = 2 const MS_KILLPAGES = 4 const MS_SYNC = 16 const M_1_PI = 0 const M_2_PI = 0 const M_2_SQRTPI = 0 const M_E = 0 const M_LN10 = 0 const M_LN2 = 0 const M_LOG10E = 0 const M_LOG2E = 0 const M_PI = 3.141592653589793 const M_PI_2 = 0 const M_PI_4 = 0 const M_SQRT1_2 = 0 const M_SQRT2 = 0 const NAME_MAX = 255 const NAN = 0 const NB = 3 const NBBY = 8 const NBPG = 4096 const NBPW = 0 const NCARGS = 1048576 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 = 0 const NFSV2_MAX_FH_SIZE = 32 const NFSV3_MAX_FH_SIZE = 64 const NFSV4_MAX_FH_SIZE = 128 const NFS_MAX_FH_SIZE = 128 const NGROUPS = 16 const NGROUPS_MAX = 16 const NMBCLUSTERS = 0 const NN = 1 const NOFILE = 256 const NOGROUP = 65535 const NOT_WITHIN = 0 const NO_LOCK = 0 const NSIG = 32 const NZERO = 20 const N_OR_COST = 3 const N_SORT_BUCKET = 32 const N_STATEMENT = 8 const NeXTBSD = 1995064 const NeXTBSD4_0 = 0 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 OPEN_MAX = 10240 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_JUMP0 = 128 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_IfSizeBetween = 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_ASSUME_PTR_ABI_SINGLE_BEGIN = 0 const OS_ASSUME_PTR_ABI_SINGLE_END = 0 const OS_HEADER_INDEXABLE = 0 const OS_UNSAFE_INDEXABLE = 0 const OS_VXWORKS = 0 const OVERFLOW = 3 const O_ACCMODE = 3 const O_ALERT = 536870912 const O_APPEND = 8 const O_ASYNC = 64 const O_BINARY = 0 const O_CLOEXEC = 16777216 const O_CREAT = 512 const O_DIRECTORY = 1048576 const O_DP_AUTHENTICATE = 4 const O_DP_GETRAWENCRYPTED = 1 const O_DP_GETRAWUNENCRYPTED = 2 const O_DSYNC = 4194304 const O_EVTONLY = 32768 const O_EXCL = 2048 const O_EXEC = 1073741824 const O_EXLOCK = 32 const O_FSYNC = 128 const O_LARGEFILE = 0 const O_NDELAY = 4 const O_NOCTTY = 131072 const O_NOFOLLOW = 256 const O_NOFOLLOW_ANY = 536870912 const O_NONBLOCK = 4 const O_POPUP = 2147483648 const O_RDONLY = 0 const O_RDWR = 2 const O_SEARCH = 1074790400 const O_SHLOCK = 16 const O_SYMLINK = 2097152 const O_SYNC = 128 const O_TRUNC = 1024 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 PATH_MAX = 1024 const PCACHE1_MIGHT_USE_GROUP_MUTEX = 1 const PCACHE_DIRTYLIST_ADD = 2 const PCACHE_DIRTYLIST_FRONT = 3 const PCACHE_DIRTYLIST_REMOVE = 1 const PCATCH = 256 const PDP_ENDIAN = 3412 const PDROP = 1024 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 PGOFSET = 4095 const PGSHIFT = 12 const PINOD = 8 const PIPE_BUF = 512 const PLOCK = 36 const PLOSS = 6 const POLL_ERR = 4 const POLL_HUP = 6 const POLL_IN = 1 const POLL_MSG = 3 const POLL_OUT = 2 const POLL_PRI = 5 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 PPAUSE = 40 const PPPDISC = 5 const PREFERRED_SCHEMA_TABLE = "sqlite_schema" const PREFERRED_TEMP_SCHEMA_TABLE = "sqlite_temp_schema" const PRIBIO = 16 const PRIMASK = 255 const PRIO_DARWIN_BG = 4096 const PRIO_DARWIN_NONUI = 4097 const PRIO_DARWIN_PROCESS = 4 const PRIO_DARWIN_THREAD = 3 const PRIO_MAX = 20 const PRIO_MIN = -20 const PRIO_PGRP = 1 const PRIO_PROCESS = 0 const PRIO_USER = 2 const PROT_EXEC = 4 const PROT_NONE = 0 const PROT_READ = 1 const PROT_WRITE = 2 const PROXY_CONCHVERSION = 2 const PROXY_HEADERLEN = 1 const PROXY_HOSTIDLEN = 16 const PROXY_MAXCONCHLEN = 1041 const PROXY_PATHINDEX = 17 const PSOCK = 24 const PSPIN = 2048 const PSWP = 0 const PTF_INTKEY = 1 const PTF_LEAF = 8 const PTF_LEAFDATA = 4 const PTF_ZERODATA = 2 const PTRDIFF_MAX = 9223372036854775807 const PTRDIFF_MIN = -9223372036854775808 const PTRMAP_BTREE = 5 const PTRMAP_FREEPAGE = 2 const PTRMAP_OVERFLOW1 = 3 const PTRMAP_OVERFLOW2 = 4 const PTRMAP_ROOTPAGE = 1 const PTTYBLOCK = 512 const PUSER = 50 const PVFS = 20 const PVM = 4 const PWAIT = 32 const PZERO = 22 const P_tmpdir = "/var/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_EXCL = 4 const RENAME_NOFOLLOW_ANY = 16 const RENAME_RESERVED1 = 8 const RENAME_SECLUDE = 1 const RENAME_SWAP = 2 const RESERVED_BYTE = 1 const RESERVED_LOCK = 2 const RE_DUP_MAX = 255 const RLIMIT_AS = 5 const RLIMIT_CORE = 4 const RLIMIT_CPU = 0 const RLIMIT_CPU_USAGE_MONITOR = 2 const RLIMIT_DATA = 2 const RLIMIT_FOOTPRINT_INTERVAL = 4 const RLIMIT_FSIZE = 1 const RLIMIT_MEMLOCK = 6 const RLIMIT_NOFILE = 8 const RLIMIT_NPROC = 7 const RLIMIT_RSS = 5 const RLIMIT_STACK = 3 const RLIMIT_THREAD_CPULIMITS = 3 const RLIMIT_WAKEUPS_MONITOR = 1 const RLIM_NLIMITS = 9 const RLIM_SAVED_CUR = "RLIM_INFINITY" const RLIM_SAVED_MAX = "RLIM_INFINITY" 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 RSIZE_MAX = 9223372036854775807 const RTLD_DEFAULT = -2 const RTLD_FIRST = 256 const RTLD_GLOBAL = 8 const RTLD_LAZY = 1 const RTLD_LOCAL = 4 const RTLD_MAIN_ONLY = -5 const RTLD_NEXT = -1 const RTLD_NODELETE = 128 const RTLD_NOLOAD = 16 const RTLD_NOW = 2 const RTLD_SELF = -3 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 RUSAGE_CHILDREN = -1 const RUSAGE_INFO_CURRENT = 6 const RUSAGE_INFO_V0 = 0 const RUSAGE_INFO_V1 = 1 const RUSAGE_INFO_V2 = 2 const RUSAGE_INFO_V3 = 3 const RUSAGE_INFO_V4 = 4 const RUSAGE_INFO_V5 = 5 const RUSAGE_INFO_V6 = 6 const RUSAGE_SELF = 0 const RU_PROC_RUNS_RESLIDE = 1 const R_OK = 4 const SAVEPOINT_BEGIN = 0 const SAVEPOINT_RELEASE = 1 const SAVEPOINT_ROLLBACK = 2 const SA_64REGSET = 512 const SA_NOCLDSTOP = 8 const SA_NOCLDWAIT = 32 const SA_NODEFER = 16 const SA_ONSTACK = 1 const SA_RESETHAND = 4 const SA_RESTART = 2 const SA_SIGINFO = 64 const SA_USERSPACE_MASK = 127 const SA_USERTRAMP = 256 const SBC_ALTERNATE_SHARED_REGION = 2 const SBC_PANIC_ON_AUTHFAIL = 8 const SBC_PRESERVE_GRAFT = 32 const SBC_PRESERVE_MOUNT = 1 const SBC_STRICT_AUTH = 16 const SBC_SYSTEM_CONTENT = 4 const SCHEMA_ROOT = 1 const SEARCHFS_MAX_SEARCHPARMS = 4096 const SEEK_CUR = 1 const SEEK_DATA = 4 const SEEK_END = 2 const SEEK_HOLE = 3 const SEEK_SET = 0 const SEGV_ACCERR = 2 const SEGV_MAPERR = 1 const SEGV_NOOP = 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_APPEND = 262144 const SF_ARCHIVED = 65536 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_Correlated = 536870912 const SF_DATALESS = 1073741824 const SF_Distinct = 1 const SF_Expanded = 64 const SF_FIRMLINK = 8388608 const SF_FixedLimit = 16384 const SF_HasAgg = 16 const SF_HasTypeInfo = 128 const SF_IMMUTABLE = 131072 const SF_IncludeHidden = 131072 const SF_MaybeConvert = 32768 const SF_MinMaxAgg = 4096 const SF_MultiPart = 33554432 const SF_MultiValue = 1024 const SF_NOUNLINK = 1048576 const SF_NestedFrom = 2048 const SF_NoopOrderBy = 4194304 const SF_OrderByReqd = 134217728 const SF_PushDown = 16777216 const SF_RESTRICTED = 524288 const SF_Recursive = 8192 const SF_Resolved = 4 const SF_SETTABLE = 1073676288 const SF_SUPPORTED = 10420224 const SF_SYNTHETIC = 3221225472 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 SIGABRT = 6 const SIGALRM = 14 const SIGBUS = 10 const SIGCHLD = 20 const SIGCONT = 19 const SIGEMT = 7 const SIGEV_NONE = 0 const SIGEV_SIGNAL = 1 const SIGEV_THREAD = 3 const SIGFPE = 8 const SIGHUP = 1 const SIGILL = 4 const SIGINFO = 29 const SIGINT = 2 const SIGIO = 23 const SIGIOT = 6 const SIGKILL = 9 const SIGPIPE = 13 const SIGPROF = 27 const SIGQUIT = 3 const SIGSEGV = 11 const SIGSTKSZ = 131072 const SIGSTOP = 17 const SIGSYS = 12 const SIGTERM = 15 const SIGTRAP = 5 const SIGTSTP = 18 const SIGTTIN = 21 const SIGTTOU = 22 const SIGURG = 16 const SIGUSR1 = 30 const SIGUSR2 = 31 const SIGVTALRM = 26 const SIGWINCH = 28 const SIGXCPU = 24 const SIGXFSZ = 25 const SIG_ATOMIC_MAX = 2147483647 const SIG_ATOMIC_MIN = -2147483648 const SIG_BLOCK = 1 const SIG_SETMASK = 3 const SIG_UNBLOCK = 2 const SING = 2 const SIOCGETVLAN = "SIOCGIFVLAN" const SIOCSETVLAN = "SIOCSIFVLAN" const SIZE_MAX = 18446744073709551615 const SI_ASYNCIO = 65540 const SI_MESGQ = 65541 const SI_QUEUE = 65538 const SI_TIMER = 65539 const SI_USER = 65537 const SLIPDISC = 4 const SLOT_2_0 = 2080895 const SLOT_4_2_0 = 4028612735 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 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 = 0 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_OPTIMIZE_LIMIT = 2000 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_DIGIT_SEPARATOR = 95 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 = 1 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 = 1024 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 = 8 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-05-23 13:25:27 96c92aba00c8375bc32fafcdf12429c58bd8aabfcadab6683e35bbb9cdebf19e" 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.46.0" const SQLITE_VERSION_NUMBER = 3046000 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 SRCHFS_MATCHDIRS = 4 const SRCHFS_MATCHFILES = 8 const SRCHFS_MATCHPARTIALNAMES = 2 const SRCHFS_NEGATEPARAMS = 2147483648 const SRCHFS_SKIPINAPPROPRIATE = 128 const SRCHFS_SKIPINVISIBLE = 32 const SRCHFS_SKIPLINKS = 16 const SRCHFS_SKIPPACKAGES = 64 const SRCHFS_START = 1 const SRCHFS_VALIDOPTIONSMASK = 2147483903 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 SS_DISABLE = 4 const SS_ONSTACK = 1 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 STDERR_FILENO = 2 const STDIN_FILENO = 0 const STDOUT_FILENO = 1 const SV_INTERRUPT = 2 const SV_NOCLDSTOP = 8 const SV_NODEFER = 16 const SV_ONSTACK = 1 const SV_RESETHAND = 4 const SV_SIGINFO = 64 const SYNC_VOLUME_FULLSYNC = 1 const SYNC_VOLUME_WAIT = 2 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_IFWHT = 57344 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_ISTXT = 512 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 TABLDISC = 3 const TABTYP_NORM = 0 const TABTYP_VIEW = 2 const TABTYP_VTAB = 1 const TARGET_ABI_USES_IOS_VALUES = 1 const TARGET_CPU_68K = 0 const TARGET_CPU_ALPHA = 0 const TARGET_CPU_ARM = 0 const TARGET_CPU_ARM64 = 1 const TARGET_CPU_MIPS = 0 const TARGET_CPU_PPC = 0 const TARGET_CPU_PPC64 = 0 const TARGET_CPU_SPARC = 0 const TARGET_CPU_X86 = 0 const TARGET_CPU_X86_64 = 0 const TARGET_IPHONE_SIMULATOR = 0 const TARGET_OS_DRIVERKIT = 0 const TARGET_OS_EMBEDDED = 0 const TARGET_OS_IOS = 0 const TARGET_OS_IPHONE = 0 const TARGET_OS_LINUX = 0 const TARGET_OS_MAC = 1 const TARGET_OS_MACCATALYST = 0 const TARGET_OS_NANO = 0 const TARGET_OS_OSX = 1 const TARGET_OS_RTKIT = 0 const TARGET_OS_SIMULATOR = 0 const TARGET_OS_TV = 0 const TARGET_OS_UIKITFORMAC = 0 const TARGET_OS_UNIX = 0 const TARGET_OS_VISION = 0 const TARGET_OS_WATCH = 0 const TARGET_OS_WIN32 = 0 const TARGET_OS_WINDOWS = 0 const TARGET_RT_64_BIT = 1 const TARGET_RT_BIG_ENDIAN = 0 const TARGET_RT_LITTLE_ENDIAN = 1 const TARGET_RT_MAC_CFM = 0 const TARGET_RT_MAC_MACHO = 1 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_MaybeReanalyze = 256 const TF_NoVisibleRowid = 512 const TF_OOOHidden = 1024 const TF_Readonly = 1 const TF_Shadow = 4096 const TF_Strict = 65536 const TF_WithoutRowid = 128 const TIME_UTC = 1 const TIOCGSIZE = "TIOCGWINSZ" 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 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 TIOCSSIZE = "TIOCSWINSZ" 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 = 185 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_QNUMBER = 183 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 = 184 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 = 174 const TK_UNBOUNDED = 90 const TK_UNION = 134 const TK_UNIQUE = 123 const TK_UPDATE = 129 const TK_UPLUS = 173 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 TLOSS = 5 const TMP_MAX = 308915776 const TOKEN = 0 const TRANS_NONE = 0 const TRANS_READ = 1 const TRANS_WRITE = 2 const TRAP_BRKPT = 1 const TRAP_TRACE = 2 const TREETRACE_ENABLED = 0 const TRIGGER_AFTER = 2 const TRIGGER_BEFORE = 1 const TRUE = 1 const TTYDISC = 0 const UF_APPEND = 4 const UF_COMPRESSED = 32 const UF_DATAVAULT = 128 const UF_HIDDEN = 32768 const UF_IMMUTABLE = 2 const UF_NODUMP = 1 const UF_OPAQUE = 8 const UF_SETTABLE = 65535 const UF_TRACKED = 64 const UID_MAX = 2147483647 const UINT16_MAX = 65535 const UINT32_MAX = 4294967295 const UINT64_MAX = 18446744073709551615 const UINT8_MAX = 255 const UINTMAX_MAX = 18446744073709551615 const UINTPTR_MAX = 18446744073709551615 const UINT_FAST16_MAX = 65535 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 UNDERFLOW = 4 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 USER_FSIGNATURES_CDHASH_LEN = 20 const USE_PREAD = 1 const UTIME_NOW = -1 const UTIME_OMIT = -2 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 VFS_CONF = 2 const VFS_CTL_DISC = 65544 const VFS_CTL_NEWADDR = 65540 const VFS_CTL_NOLOCKS = 65542 const VFS_CTL_NSTATUS = 65546 const VFS_CTL_OSTATFS = 65537 const VFS_CTL_QUERY = 65539 const VFS_CTL_SADDR = 65543 const VFS_CTL_SERVERINFO = 65545 const VFS_CTL_STATFS = 65547 const VFS_CTL_STATFS64 = 65547 const VFS_CTL_TIMEO = 65541 const VFS_CTL_UMOUNT = 65538 const VFS_CTL_VERS1 = 1 const VFS_GENERIC = 0 const VFS_MAXTYPENUM = 1 const VFS_NUMMNTOPS = 1 const VOL_CAPABILITIES_FORMAT = 0 const VOL_CAPABILITIES_INTERFACES = 1 const VOL_CAPABILITIES_RESERVED1 = 2 const VOL_CAPABILITIES_RESERVED2 = 3 const VOL_CAP_FMT_2TB_FILESIZE = 2048 const VOL_CAP_FMT_64BIT_OBJECT_IDS = 131072 const VOL_CAP_FMT_CASE_PRESERVING = 512 const VOL_CAP_FMT_CASE_SENSITIVE = 256 const VOL_CAP_FMT_CLONE_MAPPING = 67108864 const VOL_CAP_FMT_DECMPFS_COMPRESSION = 65536 const VOL_CAP_FMT_DIR_HARDLINKS = 262144 const VOL_CAP_FMT_DOCUMENT_ID = 524288 const VOL_CAP_FMT_FAST_STATFS = 1024 const VOL_CAP_FMT_HARDLINKS = 4 const VOL_CAP_FMT_HIDDEN_FILES = 8192 const VOL_CAP_FMT_JOURNAL = 8 const VOL_CAP_FMT_JOURNAL_ACTIVE = 16 const VOL_CAP_FMT_NO_IMMUTABLE_FILES = 2097152 const VOL_CAP_FMT_NO_PERMISSIONS = 4194304 const VOL_CAP_FMT_NO_ROOT_TIMES = 32 const VOL_CAP_FMT_NO_VOLUME_SIZES = 32768 const VOL_CAP_FMT_OPENDENYMODES = 4096 const VOL_CAP_FMT_PATH_FROM_ID = 16384 const VOL_CAP_FMT_PERSISTENTOBJECTIDS = 1 const VOL_CAP_FMT_SEALED = 33554432 const VOL_CAP_FMT_SHARED_SPACE = 8388608 const VOL_CAP_FMT_SPARSE_FILES = 64 const VOL_CAP_FMT_SYMBOLICLINKS = 2 const VOL_CAP_FMT_VOL_GROUPS = 16777216 const VOL_CAP_FMT_WRITE_GENERATION_COUNT = 1048576 const VOL_CAP_FMT_ZERO_RUNS = 128 const VOL_CAP_INT_ADVLOCK = 256 const VOL_CAP_INT_ALLOCATE = 64 const VOL_CAP_INT_ATTRIBUTION_TAG = 4194304 const VOL_CAP_INT_ATTRLIST = 2 const VOL_CAP_INT_CLONE = 65536 const VOL_CAP_INT_COPYFILE = 32 const VOL_CAP_INT_EXCHANGEDATA = 16 const VOL_CAP_INT_EXTENDED_ATTR = 16384 const VOL_CAP_INT_EXTENDED_SECURITY = 1024 const VOL_CAP_INT_FLOCK = 512 const VOL_CAP_INT_MANLOCK = 4096 const VOL_CAP_INT_NAMEDSTREAMS = 8192 const VOL_CAP_INT_NFSEXPORT = 4 const VOL_CAP_INT_READDIRATTR = 8 const VOL_CAP_INT_RENAME_EXCL = 524288 const VOL_CAP_INT_RENAME_OPENFAIL = 1048576 const VOL_CAP_INT_RENAME_SECLUDE = 2097152 const VOL_CAP_INT_RENAME_SWAP = 262144 const VOL_CAP_INT_SEARCHFS = 1 const VOL_CAP_INT_SNAPSHOT = 131072 const VOL_CAP_INT_USERACCESS = 2048 const VOL_CAP_INT_VOL_RENAME = 128 const VQ_ASSIST = 64 const VQ_DEAD = 32 const VQ_DESIRED_DISK = 16384 const VQ_FLAG10000 = 65536 const VQ_FREE_SPACE_CHANGE = 32768 const VQ_LOWDISK = 4 const VQ_MOUNT = 8 const VQ_NEARLOWDISK = 8192 const VQ_NEEDAUTH = 2 const VQ_NOTRESP = 1 const VQ_NOTRESPLOCK = 128 const VQ_QUOTA = 4096 const VQ_SERVEREVENT = 2048 const VQ_SYNCEVENT = 1024 const VQ_UNMOUNT = 16 const VQ_UPDATE = 256 const VQ_VERYLOWDISK = 512 const ViewCanHaveRowid = 0 const WAIT_ANY = -1 const WAIT_MYPGRP = 0 const WAKEMON_DISABLE = 2 const WAKEMON_ENABLE = 1 const WAKEMON_GET_PARAMS = 4 const WAKEMON_MAKE_FATAL = 16 const WAKEMON_SET_DEFAULTS = 8 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 WCHAR_MAX = 2147483647 const WCHAR_MIN = -2147483648 const WCONTINUED = 16 const WCOREFLAG = 128 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 WINT_MAX = 2147483647 const WINT_MIN = -2147483648 const WNOHANG = 1 const WNOWAIT = 32 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 = 8 const WUNTRACED = 2 const W_OK = 2 const XN_EXPR = -2 const XN_ROWID = -1 const XUCRED_VERSION = 0 const X_OK = 1 const X_TLOSS = 0 const YYDYNSTACK = 1 const YYFALLBACK = 1 const YYFREE = 0 const YYGROWABLESTACK = 1 const YYMALLOCARGTYPE = 0 const YYNOCODE = 322 const YYNOERRORRECOVERY = 1 const YYNRULE = 409 const YYNRULE_WITH_ACTION = 344 const YYNSTATE = 583 const YYNTOKEN = 186 const YYPARSEFREENEVERNULL = 1 const YYREALLOC = 0 const YYSTACKDEPTH = 100 const YYWILDCARD = 101 const YY_ACCEPT_ACTION = 1255 const YY_ACTTAB_COUNT = 2142 const YY_ERROR_ACTION = 1254 const YY_MAX_DSTRCTR = 319 const YY_MAX_REDUCE = 1665 const YY_MAX_SHIFT = 582 const YY_MAX_SHIFTREDUCE = 1253 const YY_MIN_DSTRCTR = 205 const YY_MIN_REDUCE = 1257 const YY_MIN_SHIFTREDUCE = 845 const YY_NO_ACTION = 1256 const YY_REDUCE_COUNT = 411 const YY_REDUCE_MAX = 1798 const YY_REDUCE_MIN = -275 const YY_SHIFT_COUNT = 582 const YY_SHIFT_MAX = 2128 const YY_SHIFT_MIN = 0 const _ACCESS_EXTENDED_MASK = 4193792 const _APPEND_OK = 8192 const _ARM_SIGNAL_ = 1 const _CHOWN_OK = 2097152 const _CS_DARWIN_USER_CACHE_DIR = 65538 const _CS_DARWIN_USER_DIR = 65536 const _CS_DARWIN_USER_TEMP_DIR = 65537 const _CS_PATH = 1 const _CS_POSIX_V6_ILP32_OFF32_CFLAGS = 2 const _CS_POSIX_V6_ILP32_OFF32_LDFLAGS = 3 const _CS_POSIX_V6_ILP32_OFF32_LIBS = 4 const _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS = 5 const _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS = 6 const _CS_POSIX_V6_ILP32_OFFBIG_LIBS = 7 const _CS_POSIX_V6_LP64_OFF64_CFLAGS = 8 const _CS_POSIX_V6_LP64_OFF64_LDFLAGS = 9 const _CS_POSIX_V6_LP64_OFF64_LIBS = 10 const _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS = 11 const _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS = 12 const _CS_POSIX_V6_LPBIG_OFFBIG_LIBS = 13 const _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS = 14 const _CS_XBS5_ILP32_OFF32_CFLAGS = 20 const _CS_XBS5_ILP32_OFF32_LDFLAGS = 21 const _CS_XBS5_ILP32_OFF32_LIBS = 22 const _CS_XBS5_ILP32_OFF32_LINTFLAGS = 23 const _CS_XBS5_ILP32_OFFBIG_CFLAGS = 24 const _CS_XBS5_ILP32_OFFBIG_LDFLAGS = 25 const _CS_XBS5_ILP32_OFFBIG_LIBS = 26 const _CS_XBS5_ILP32_OFFBIG_LINTFLAGS = 27 const _CS_XBS5_LP64_OFF64_CFLAGS = 28 const _CS_XBS5_LP64_OFF64_LDFLAGS = 29 const _CS_XBS5_LP64_OFF64_LIBS = 30 const _CS_XBS5_LP64_OFF64_LINTFLAGS = 31 const _CS_XBS5_LPBIG_OFFBIG_CFLAGS = 32 const _CS_XBS5_LPBIG_OFFBIG_LDFLAGS = 33 const _CS_XBS5_LPBIG_OFFBIG_LIBS = 34 const _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS = 35 const _DARWIN_FEATURE_64_BIT_INODE = 1 const _DARWIN_FEATURE_ONLY_64_BIT_INODE = 1 const _DARWIN_FEATURE_ONLY_UNIX_CONFORMANCE = 1 const _DARWIN_FEATURE_ONLY_VERS_1050 = 1 const _DARWIN_FEATURE_UNIX_CONFORMANCE = 3 const _DELETE_OK = 4096 const _EXECUTE_OK = 2048 const _FILE_OFFSET_BITS = 64 const _FORTIFY_SOURCE = 2 const _IOFBF = 0 const _IOLBF = 1 const _IONBF = 2 const _LARGEFILE_SOURCE = 1 const _LARGE_FILE = 1 const _LP64 = 1 const _MALLOC_TYPE_AVAILABILITY = 0 const _PC_2_SYMLINKS = 15 const _PC_ALLOC_SIZE_MIN = 16 const _PC_ASYNC_IO = 17 const _PC_AUTH_OPAQUE_NP = 14 const _PC_CASE_PRESERVING = 12 const _PC_CASE_SENSITIVE = 11 const _PC_CHOWN_RESTRICTED = 7 const _PC_EXTENDED_SECURITY_NP = 13 const _PC_FILESIZEBITS = 18 const _PC_LINK_MAX = 1 const _PC_MAX_CANON = 2 const _PC_MAX_INPUT = 3 const _PC_MIN_HOLE_SIZE = 27 const _PC_NAME_CHARS_MAX = 10 const _PC_NAME_MAX = 4 const _PC_NO_TRUNC = 8 const _PC_PATH_MAX = 5 const _PC_PIPE_BUF = 6 const _PC_PRIO_IO = 19 const _PC_REC_INCR_XFER_SIZE = 20 const _PC_REC_MAX_XFER_SIZE = 21 const _PC_REC_MIN_XFER_SIZE = 22 const _PC_REC_XFER_ALIGN = 23 const _PC_SYMLINK_MAX = 24 const _PC_SYNC_IO = 25 const _PC_VDISABLE = 9 const _PC_XATTR_SIZE_BITS = 26 const _POSIX2_CHAR_TERM = 200112 const _POSIX2_C_BIND = 200112 const _POSIX2_C_DEV = 200112 const _POSIX2_FORT_DEV = -1 const _POSIX2_FORT_RUN = 200112 const _POSIX2_LOCALEDEF = 200112 const _POSIX2_PBS = -1 const _POSIX2_PBS_ACCOUNTING = -1 const _POSIX2_PBS_CHECKPOINT = -1 const _POSIX2_PBS_LOCATE = -1 const _POSIX2_PBS_MESSAGE = -1 const _POSIX2_PBS_TRACK = -1 const _POSIX2_SW_DEV = 200112 const _POSIX2_UPE = 200112 const _POSIX2_VERSION = 200112 const _POSIX_ADVISORY_INFO = -1 const _POSIX_ASYNCHRONOUS_IO = -1 const _POSIX_BARRIERS = -1 const _POSIX_CHOWN_RESTRICTED = 200112 const _POSIX_CLOCK_SELECTION = -1 const _POSIX_CPUTIME = -1 const _POSIX_FSYNC = 200112 const _POSIX_IPV6 = 200112 const _POSIX_JOB_CONTROL = 200112 const _POSIX_MAPPED_FILES = 200112 const _POSIX_MEMLOCK = -1 const _POSIX_MEMLOCK_RANGE = -1 const _POSIX_MEMORY_PROTECTION = 200112 const _POSIX_MESSAGE_PASSING = -1 const _POSIX_MONOTONIC_CLOCK = -1 const _POSIX_NO_TRUNC = 200112 const _POSIX_PRIORITIZED_IO = -1 const _POSIX_PRIORITY_SCHEDULING = -1 const _POSIX_RAW_SOCKETS = -1 const _POSIX_READER_WRITER_LOCKS = 200112 const _POSIX_REALTIME_SIGNALS = -1 const _POSIX_REGEXP = 200112 const _POSIX_SAVED_IDS = 200112 const _POSIX_SEMAPHORES = -1 const _POSIX_SHARED_MEMORY_OBJECTS = -1 const _POSIX_SHELL = 200112 const _POSIX_SPAWN = 200112 const _POSIX_SPIN_LOCKS = -1 const _POSIX_SPORADIC_SERVER = -1 const _POSIX_SYNCHRONIZED_IO = -1 const _POSIX_THREADS = 200112 const _POSIX_THREAD_ATTR_STACKADDR = 200112 const _POSIX_THREAD_ATTR_STACKSIZE = 200112 const _POSIX_THREAD_CPUTIME = -1 const _POSIX_THREAD_KEYS_MAX = 128 const _POSIX_THREAD_PRIORITY_SCHEDULING = -1 const _POSIX_THREAD_PRIO_INHERIT = -1 const _POSIX_THREAD_PRIO_PROTECT = -1 const _POSIX_THREAD_PROCESS_SHARED = 200112 const _POSIX_THREAD_SAFE_FUNCTIONS = 200112 const _POSIX_THREAD_SPORADIC_SERVER = -1 const _POSIX_TIMEOUTS = -1 const _POSIX_TIMERS = -1 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_VERSION = 200112 const _QUAD_HIGHWORD = 1 const _QUAD_LOWWORD = 0 const _RATTR_OK = 32768 const _READ_OK = 512 const _REXT_OK = 131072 const _RLIMIT_POSIX_FLAG = 4096 const _RMFILE_OK = 16384 const _RPERM_OK = 524288 const _SC_2_CHAR_TERM = 20 const _SC_2_C_BIND = 18 const _SC_2_C_DEV = 19 const _SC_2_FORT_DEV = 21 const _SC_2_FORT_RUN = 22 const _SC_2_LOCALEDEF = 23 const _SC_2_PBS = 59 const _SC_2_PBS_ACCOUNTING = 60 const _SC_2_PBS_CHECKPOINT = 61 const _SC_2_PBS_LOCATE = 62 const _SC_2_PBS_MESSAGE = 63 const _SC_2_PBS_TRACK = 64 const _SC_2_SW_DEV = 24 const _SC_2_UPE = 25 const _SC_2_VERSION = 17 const _SC_ADVISORY_INFO = 65 const _SC_AIO_LISTIO_MAX = 42 const _SC_AIO_MAX = 43 const _SC_AIO_PRIO_DELTA_MAX = 44 const _SC_ARG_MAX = 1 const _SC_ASYNCHRONOUS_IO = 28 const _SC_ATEXIT_MAX = 107 const _SC_BARRIERS = 66 const _SC_BC_BASE_MAX = 9 const _SC_BC_DIM_MAX = 10 const _SC_BC_SCALE_MAX = 11 const _SC_BC_STRING_MAX = 12 const _SC_CHILD_MAX = 2 const _SC_CLK_TCK = 3 const _SC_CLOCK_SELECTION = 67 const _SC_COLL_WEIGHTS_MAX = 13 const _SC_CPUTIME = 68 const _SC_DELAYTIMER_MAX = 45 const _SC_EXPR_NEST_MAX = 14 const _SC_FILE_LOCKING = 69 const _SC_FSYNC = 38 const _SC_GETGR_R_SIZE_MAX = 70 const _SC_GETPW_R_SIZE_MAX = 71 const _SC_HOST_NAME_MAX = 72 const _SC_IOV_MAX = 56 const _SC_IPV6 = 118 const _SC_JOB_CONTROL = 6 const _SC_LINE_MAX = 15 const _SC_LOGIN_NAME_MAX = 73 const _SC_MAPPED_FILES = 47 const _SC_MEMLOCK = 30 const _SC_MEMLOCK_RANGE = 31 const _SC_MEMORY_PROTECTION = 32 const _SC_MESSAGE_PASSING = 33 const _SC_MONOTONIC_CLOCK = 74 const _SC_MQ_OPEN_MAX = 46 const _SC_MQ_PRIO_MAX = 75 const _SC_NGROUPS_MAX = 4 const _SC_NPROCESSORS_CONF = 57 const _SC_NPROCESSORS_ONLN = 58 const _SC_OPEN_MAX = 5 const _SC_PAGESIZE = 29 const _SC_PAGE_SIZE = 29 const _SC_PASS_MAX = 131 const _SC_PHYS_PAGES = 200 const _SC_PRIORITIZED_IO = 34 const _SC_PRIORITY_SCHEDULING = 35 const _SC_RAW_SOCKETS = 119 const _SC_READER_WRITER_LOCKS = 76 const _SC_REALTIME_SIGNALS = 36 const _SC_REGEXP = 77 const _SC_RE_DUP_MAX = 16 const _SC_RTSIG_MAX = 48 const _SC_SAVED_IDS = 7 const _SC_SEMAPHORES = 37 const _SC_SEM_NSEMS_MAX = 49 const _SC_SEM_VALUE_MAX = 50 const _SC_SHARED_MEMORY_OBJECTS = 39 const _SC_SHELL = 78 const _SC_SIGQUEUE_MAX = 51 const _SC_SPAWN = 79 const _SC_SPIN_LOCKS = 80 const _SC_SPORADIC_SERVER = 81 const _SC_SS_REPL_MAX = 126 const _SC_STREAM_MAX = 26 const _SC_SYMLOOP_MAX = 120 const _SC_SYNCHRONIZED_IO = 40 const _SC_THREADS = 96 const _SC_THREAD_ATTR_STACKADDR = 82 const _SC_THREAD_ATTR_STACKSIZE = 83 const _SC_THREAD_CPUTIME = 84 const _SC_THREAD_DESTRUCTOR_ITERATIONS = 85 const _SC_THREAD_KEYS_MAX = 86 const _SC_THREAD_PRIORITY_SCHEDULING = 89 const _SC_THREAD_PRIO_INHERIT = 87 const _SC_THREAD_PRIO_PROTECT = 88 const _SC_THREAD_PROCESS_SHARED = 90 const _SC_THREAD_SAFE_FUNCTIONS = 91 const _SC_THREAD_SPORADIC_SERVER = 92 const _SC_THREAD_STACK_MIN = 93 const _SC_THREAD_THREADS_MAX = 94 const _SC_TIMEOUTS = 95 const _SC_TIMERS = 41 const _SC_TIMER_MAX = 52 const _SC_TRACE = 97 const _SC_TRACE_EVENT_FILTER = 98 const _SC_TRACE_EVENT_NAME_MAX = 127 const _SC_TRACE_INHERIT = 99 const _SC_TRACE_LOG = 100 const _SC_TRACE_NAME_MAX = 128 const _SC_TRACE_SYS_MAX = 129 const _SC_TRACE_USER_EVENT_MAX = 130 const _SC_TTY_NAME_MAX = 101 const _SC_TYPED_MEMORY_OBJECTS = 102 const _SC_TZNAME_MAX = 27 const _SC_V6_ILP32_OFF32 = 103 const _SC_V6_ILP32_OFFBIG = 104 const _SC_V6_LP64_OFF64 = 105 const _SC_V6_LPBIG_OFFBIG = 106 const _SC_VERSION = 8 const _SC_XBS5_ILP32_OFF32 = 122 const _SC_XBS5_ILP32_OFFBIG = 123 const _SC_XBS5_LP64_OFF64 = 124 const _SC_XBS5_LPBIG_OFFBIG = 125 const _SC_XOPEN_CRYPT = 108 const _SC_XOPEN_ENH_I18N = 109 const _SC_XOPEN_LEGACY = 110 const _SC_XOPEN_REALTIME = 111 const _SC_XOPEN_REALTIME_THREADS = 112 const _SC_XOPEN_SHM = 113 const _SC_XOPEN_STREAMS = 114 const _SC_XOPEN_UNIX = 115 const _SC_XOPEN_VERSION = 116 const _SC_XOPEN_XCU_VERSION = 121 const _STRUCT_MCONTEXT = "_STRUCT_MCONTEXT64" const _V6_ILP32_OFF32 = -1 const _V6_ILP32_OFFBIG = -1 const _V6_LP64_OFF64 = 1 const _V6_LPBIG_OFFBIG = 1 const _WATTR_OK = 65536 const _WEXT_OK = 262144 const _WPERM_OK = 1048576 const _WRITE_OK = 1024 const _WSTOPPED = 127 const _XBS5_ILP32_OFF32 = -1 const _XBS5_ILP32_OFFBIG = -1 const _XBS5_LP64_OFF64 = 1 const _XBS5_LPBIG_OFFBIG = 1 const _XOPEN_CRYPT = 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_STREAMS = -1 const _XOPEN_UNIX = 1 const _XOPEN_VERSION = 600 const _XOPEN_XCU_VERSION = 4 const __AARCH64EL__ = 1 const __AARCH64_CMODEL_SMALL__ = 1 const __AARCH64_SIMD__ = 1 const __API_TO_BE_DEPRECATED = 100000 const __API_TO_BE_DEPRECATED_DRIVERKIT = 100000 const __API_TO_BE_DEPRECATED_IOS = 100000 const __API_TO_BE_DEPRECATED_MACCATALYST = 100000 const __API_TO_BE_DEPRECATED_MACOS = 100000 const __API_TO_BE_DEPRECATED_TVOS = 100000 const __API_TO_BE_DEPRECATED_VISIONOS = 100000 const __API_TO_BE_DEPRECATED_WATCHOS = 100000 const __APPLE_CC__ = 6000 const __APPLE__ = 1 const __ARM64_ARCH_8__ = 1 const __ARM_64BIT_STATE = 1 const __ARM_ACLE = 200 const __ARM_ALIGN_MAX_STACK_PWR = 4 const __ARM_ARCH = 8 const __ARM_ARCH_8_3__ = 1 const __ARM_ARCH_8_4__ = 1 const __ARM_ARCH_8_5__ = 1 const __ARM_ARCH_ISA_A64 = 1 const __ARM_ARCH_PROFILE = 65 const __ARM_FEATURE_AES = 1 const __ARM_FEATURE_ATOMICS = 1 const __ARM_FEATURE_CLZ = 1 const __ARM_FEATURE_COMPLEX = 1 const __ARM_FEATURE_CRC32 = 1 const __ARM_FEATURE_CRYPTO = 1 const __ARM_FEATURE_DIRECTED_ROUNDING = 1 const __ARM_FEATURE_DIV = 1 const __ARM_FEATURE_DOTPROD = 1 const __ARM_FEATURE_FMA = 1 const __ARM_FEATURE_FP16_FML = 1 const __ARM_FEATURE_FP16_SCALAR_ARITHMETIC = 1 const __ARM_FEATURE_FP16_VECTOR_ARITHMETIC = 1 const __ARM_FEATURE_FRINT = 1 const __ARM_FEATURE_IDIV = 1 const __ARM_FEATURE_JCVT = 1 const __ARM_FEATURE_LDREX = 15 const __ARM_FEATURE_NUMERIC_MAXMIN = 1 const __ARM_FEATURE_QRDMX = 1 const __ARM_FEATURE_RCPC = 1 const __ARM_FEATURE_SHA2 = 1 const __ARM_FEATURE_SHA3 = 1 const __ARM_FEATURE_SHA512 = 1 const __ARM_FEATURE_SM3 = 1 const __ARM_FEATURE_SM4 = 1 const __ARM_FEATURE_UNALIGNED = 1 const __ARM_FP = 14 const __ARM_FP16_ARGS = 1 const __ARM_FP16_FORMAT_IEEE = 1 const __ARM_NEON = 1 const __ARM_NEON_FP = 14 const __ARM_NEON__ = 1 const __ARM_PCS_AAPCS64 = 1 const __ARM_SIZEOF_MINIMAL_ENUM = 4 const __ARM_SIZEOF_WCHAR_T = 4 const __ASSUME_PTR_ABI_SINGLE_BEGIN = 0 const __ASSUME_PTR_ABI_SINGLE_END = 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 __AUDIT_API_DEPRECATED = 0 const __BIGGEST_ALIGNMENT__ = 8 const __BITINT_MAXWIDTH__ = 128 const __BLOCKS__ = 1 const __BOOL_WIDTH__ = 8 const __BRIDGEOS_2_0 = 20000 const __BRIDGEOS_3_0 = 30000 const __BRIDGEOS_3_1 = 30100 const __BRIDGEOS_3_4 = 30400 const __BRIDGEOS_4_0 = 40000 const __BRIDGEOS_4_1 = 40100 const __BRIDGEOS_5_0 = 50000 const __BRIDGEOS_5_1 = 50100 const __BRIDGEOS_5_3 = 50300 const __BRIDGEOS_6_0 = 60000 const __BRIDGEOS_6_2 = 60200 const __BRIDGEOS_6_4 = 60400 const __BRIDGEOS_6_5 = 60500 const __BRIDGEOS_6_6 = 60600 const __BRIDGEOS_7_0 = 70000 const __BRIDGEOS_7_1 = 70100 const __BRIDGEOS_7_2 = 70200 const __BRIDGEOS_7_3 = 70300 const __BRIDGEOS_7_4 = 70400 const __BRIDGEOS_7_6 = 70600 const __BRIDGEOS_8_0 = 80000 const __BRIDGEOS_8_1 = 80100 const __BRIDGEOS_8_2 = 80200 const __BRIDGEOS_8_3 = 80300 const __BRIDGEOS_8_4 = 80400 const __BYTE_ORDER__ = 1234 const __CCGO__ = 1 const __CHAR_BIT__ = 8 const __CLANG_ATOMIC_BOOL_LOCK_FREE = 2 const __CLANG_ATOMIC_CHAR16_T_LOCK_FREE = 2 const __CLANG_ATOMIC_CHAR32_T_LOCK_FREE = 2 const __CLANG_ATOMIC_CHAR_LOCK_FREE = 2 const __CLANG_ATOMIC_INT_LOCK_FREE = 2 const __CLANG_ATOMIC_LLONG_LOCK_FREE = 2 const __CLANG_ATOMIC_LONG_LOCK_FREE = 2 const __CLANG_ATOMIC_POINTER_LOCK_FREE = 2 const __CLANG_ATOMIC_SHORT_LOCK_FREE = 2 const __CLANG_ATOMIC_WCHAR_T_LOCK_FREE = 2 const __CONSTANT_CFSTRINGS__ = 1 const __DARWIN_64_BIT_INO_T = 1 const __DARWIN_ALIGNBYTES = -1 const __DARWIN_ALIGNBYTES32 = -1 const __DARWIN_BIG_ENDIAN = 4321 const __DARWIN_BYTE_ORDER = 1234 const __DARWIN_C_ANSI = 4096 const __DARWIN_C_FULL = 900000 const __DARWIN_C_LEVEL = 900000 const __DARWIN_FD_SETSIZE = 1024 const __DARWIN_LITTLE_ENDIAN = 1234 const __DARWIN_NBBY = 8 const __DARWIN_NFDBITS = 0 const __DARWIN_NON_CANCELABLE = 0 const __DARWIN_NO_LONG_LONG = 0 const __DARWIN_NSIG = 32 const __DARWIN_ONLY_64_BIT_INO_T = 1 const __DARWIN_ONLY_UNIX_CONFORMANCE = 1 const __DARWIN_ONLY_VERS_1050 = 1 const __DARWIN_OPAQUE_ARM_THREAD_STATE64 = 0 const __DARWIN_PDP_ENDIAN = 3412 const __DARWIN_SUF_EXTSN = "$DARWIN_EXTSN" const __DARWIN_UNIX03 = 1 const __DARWIN_VERS_1050 = 1 const __DARWIN_WCHAR_MAX = 2147483647 const __DARWIN_WCHAR_MIN = -2147483648 const __DARWIN_WEOF = -1 const __DBL_DECIMAL_DIG__ = 17 const __DBL_DENORM_MIN__ = 0 const __DBL_DIG__ = 15 const __DBL_EPSILON__ = 0 const __DBL_HAS_DENORM__ = 1 const __DBL_HAS_INFINITY__ = 1 const __DBL_HAS_QUIET_NAN__ = 1 const __DBL_MANT_DIG__ = 53 const __DBL_MAX_10_EXP__ = 308 const __DBL_MAX_EXP__ = 1024 const __DBL_MAX__ = 0 const __DBL_MIN_10_EXP__ = -307 const __DBL_MIN_EXP__ = -1021 const __DBL_MIN__ = 2.2250738585072014e-308 const __DECIMAL_DIG__ = 17 const __DRIVERKIT_19_0 = 190000 const __DRIVERKIT_20_0 = 200000 const __DRIVERKIT_21_0 = 210000 const __DRIVERKIT_22_0 = 220000 const __DRIVERKIT_22_4 = 220400 const __DRIVERKIT_22_5 = 220500 const __DRIVERKIT_22_6 = 220600 const __DRIVERKIT_23_0 = 230000 const __DRIVERKIT_23_1 = 230100 const __DRIVERKIT_23_2 = 230200 const __DRIVERKIT_23_3 = 230300 const __DRIVERKIT_23_4 = 230400 const __DYLDDL_DRIVERKIT_UNAVAILABLE = 0 const __DYNAMIC__ = 1 const __ENABLE_LEGACY_MAC_AVAILABILITY = 1 const __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ = 140000 const __ENVIRONMENT_OS_VERSION_MIN_REQUIRED__ = 140000 const __FINITE_MATH_ONLY__ = 0 const __FLT16_DECIMAL_DIG__ = 5 const __FLT16_DENORM_MIN__ = 0 const __FLT16_DIG__ = 3 const __FLT16_EPSILON__ = 0 const __FLT16_HAS_DENORM__ = 1 const __FLT16_HAS_INFINITY__ = 1 const __FLT16_HAS_QUIET_NAN__ = 1 const __FLT16_MANT_DIG__ = 11 const __FLT16_MAX_10_EXP__ = 4 const __FLT16_MAX_EXP__ = 16 const __FLT16_MAX__ = 0 const __FLT16_MIN_10_EXP__ = -4 const __FLT16_MIN_EXP__ = -13 const __FLT16_MIN__ = 0 const __FLT_DECIMAL_DIG__ = 9 const __FLT_DENORM_MIN__ = 0 const __FLT_DIG__ = 6 const __FLT_EPSILON__ = 0 const __FLT_HAS_DENORM__ = 1 const __FLT_HAS_INFINITY__ = 1 const __FLT_HAS_QUIET_NAN__ = 1 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__ = 1.17549435e-38 const __FLT_RADIX__ = 2 const __FP_FAST_FMA = 1 const __FP_FAST_FMAF = 1 const __FUNCTION__ = 0 const __GCC_ATOMIC_BOOL_LOCK_FREE = 2 const __GCC_ATOMIC_CHAR16_T_LOCK_FREE = 2 const __GCC_ATOMIC_CHAR32_T_LOCK_FREE = 2 const __GCC_ATOMIC_CHAR_LOCK_FREE = 2 const __GCC_ATOMIC_INT_LOCK_FREE = 2 const __GCC_ATOMIC_LLONG_LOCK_FREE = 2 const __GCC_ATOMIC_LONG_LOCK_FREE = 2 const __GCC_ATOMIC_POINTER_LOCK_FREE = 2 const __GCC_ATOMIC_SHORT_LOCK_FREE = 2 const __GCC_ATOMIC_TEST_AND_SET_TRUEVAL = 1 const __GCC_ATOMIC_WCHAR_T_LOCK_FREE = 2 const __GCC_HAVE_DWARF2_CFI_ASM = 1 const __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 = 1 const __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 = 1 const __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 = 1 const __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 = 1 const __GNUC_MINOR__ = 2 const __GNUC_PATCHLEVEL__ = 1 const __GNUC_STDC_INLINE__ = 1 const __GNUC_VA_LIST = 1 const __GNUC__ = 4 const __GXX_ABI_VERSION = 1002 const __ILP32_OFF32 = -1 const __ILP32_OFFBIG = -1 const __INT16_FMTd__ = "hd" const __INT16_FMTi__ = "hi" const __INT16_MAX__ = 32767 const __INT16_TYPE__ = 0 const __INT32_FMTd__ = "d" const __INT32_FMTi__ = "i" const __INT32_MAX__ = 2147483647 const __INT32_TYPE__ = 0 const __INT64_C_SUFFIX__ = 0 const __INT64_FMTd__ = "lld" const __INT64_FMTi__ = "lli" const __INT64_MAX__ = 9223372036854775807 const __INT8_FMTd__ = "hhd" const __INT8_FMTi__ = "hhi" const __INT8_MAX__ = 127 const __INTMAX_C_SUFFIX__ = 0 const __INTMAX_FMTd__ = "ld" const __INTMAX_FMTi__ = "li" const __INTMAX_MAX__ = 9223372036854775807 const __INTMAX_WIDTH__ = 64 const __INTPTR_FMTd__ = "ld" const __INTPTR_FMTi__ = "li" const __INTPTR_MAX__ = 9223372036854775807 const __INTPTR_WIDTH__ = 64 const __INT_FAST16_FMTd__ = "hd" const __INT_FAST16_FMTi__ = "hi" const __INT_FAST16_MAX__ = 32767 const __INT_FAST16_TYPE__ = 0 const __INT_FAST16_WIDTH__ = 16 const __INT_FAST32_FMTd__ = "d" const __INT_FAST32_FMTi__ = "i" const __INT_FAST32_MAX__ = 2147483647 const __INT_FAST32_TYPE__ = 0 const __INT_FAST32_WIDTH__ = 32 const __INT_FAST64_FMTd__ = "lld" const __INT_FAST64_FMTi__ = "lli" const __INT_FAST64_MAX__ = 9223372036854775807 const __INT_FAST64_WIDTH__ = 64 const __INT_FAST8_FMTd__ = "hhd" const __INT_FAST8_FMTi__ = "hhi" const __INT_FAST8_MAX__ = 127 const __INT_FAST8_WIDTH__ = 8 const __INT_LEAST16_FMTd__ = "hd" const __INT_LEAST16_FMTi__ = "hi" const __INT_LEAST16_MAX__ = 32767 const __INT_LEAST16_TYPE__ = 0 const __INT_LEAST16_WIDTH__ = 16 const __INT_LEAST32_FMTd__ = "d" const __INT_LEAST32_FMTi__ = "i" const __INT_LEAST32_MAX__ = 2147483647 const __INT_LEAST32_TYPE__ = 0 const __INT_LEAST32_WIDTH__ = 32 const __INT_LEAST64_FMTd__ = "lld" const __INT_LEAST64_FMTi__ = "lli" const __INT_LEAST64_MAX = 9223372036854775807 const __INT_LEAST64_MAX__ = 9223372036854775807 const __INT_LEAST64_MIN = -9223372036854775808 const __INT_LEAST64_WIDTH__ = 64 const __INT_LEAST8_FMTd__ = "hhd" const __INT_LEAST8_FMTi__ = "hhi" const __INT_LEAST8_MAX__ = 127 const __INT_LEAST8_WIDTH__ = 8 const __INT_MAX__ = 2147483647 const __INT_WIDTH__ = 32 const __IPHONE_10_0 = 100000 const __IPHONE_10_1 = 100100 const __IPHONE_10_2 = 100200 const __IPHONE_10_3 = 100300 const __IPHONE_11_0 = 110000 const __IPHONE_11_1 = 110100 const __IPHONE_11_2 = 110200 const __IPHONE_11_3 = 110300 const __IPHONE_11_4 = 110400 const __IPHONE_12_0 = 120000 const __IPHONE_12_1 = 120100 const __IPHONE_12_2 = 120200 const __IPHONE_12_3 = 120300 const __IPHONE_12_4 = 120400 const __IPHONE_13_0 = 130000 const __IPHONE_13_1 = 130100 const __IPHONE_13_2 = 130200 const __IPHONE_13_3 = 130300 const __IPHONE_13_4 = 130400 const __IPHONE_13_5 = 130500 const __IPHONE_13_6 = 130600 const __IPHONE_13_7 = 130700 const __IPHONE_14_0 = 140000 const __IPHONE_14_1 = 140100 const __IPHONE_14_2 = 140200 const __IPHONE_14_3 = 140300 const __IPHONE_14_4 = 140400 const __IPHONE_14_5 = 140500 const __IPHONE_14_6 = 140600 const __IPHONE_14_7 = 140700 const __IPHONE_14_8 = 140800 const __IPHONE_15_0 = 150000 const __IPHONE_15_1 = 150100 const __IPHONE_15_2 = 150200 const __IPHONE_15_3 = 150300 const __IPHONE_15_4 = 150400 const __IPHONE_15_5 = 150500 const __IPHONE_15_6 = 150600 const __IPHONE_15_7 = 150700 const __IPHONE_15_8 = 150800 const __IPHONE_16_0 = 160000 const __IPHONE_16_1 = 160100 const __IPHONE_16_2 = 160200 const __IPHONE_16_3 = 160300 const __IPHONE_16_4 = 160400 const __IPHONE_16_5 = 160500 const __IPHONE_16_6 = 160600 const __IPHONE_16_7 = 160700 const __IPHONE_17_0 = 170000 const __IPHONE_17_1 = 170100 const __IPHONE_17_2 = 170200 const __IPHONE_17_3 = 170300 const __IPHONE_17_4 = 170400 const __IPHONE_2_0 = 20000 const __IPHONE_2_1 = 20100 const __IPHONE_2_2 = 20200 const __IPHONE_3_0 = 30000 const __IPHONE_3_1 = 30100 const __IPHONE_3_2 = 30200 const __IPHONE_4_0 = 40000 const __IPHONE_4_1 = 40100 const __IPHONE_4_2 = 40200 const __IPHONE_4_3 = 40300 const __IPHONE_5_0 = 50000 const __IPHONE_5_1 = 50100 const __IPHONE_6_0 = 60000 const __IPHONE_6_1 = 60100 const __IPHONE_7_0 = 70000 const __IPHONE_7_1 = 70100 const __IPHONE_8_0 = 80000 const __IPHONE_8_1 = 80100 const __IPHONE_8_2 = 80200 const __IPHONE_8_3 = 80300 const __IPHONE_8_4 = 80400 const __IPHONE_9_0 = 90000 const __IPHONE_9_1 = 90100 const __IPHONE_9_2 = 90200 const __IPHONE_9_3 = 90300 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_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__ = 2.2250738585072014e-308 const __LITTLE_ENDIAN__ = 1 const __LLONG_WIDTH__ = 64 const __LONG_LONG_MAX__ = 9223372036854775807 const __LONG_MAX__ = 9223372036854775807 const __LONG_WIDTH__ = 64 const __LP64_OFF64 = 1 const __LP64__ = 1 const __LPBIG_OFFBIG = 1 const __MACH__ = 1 const __MAC_10_0 = 1000 const __MAC_10_1 = 1010 const __MAC_10_10 = 101000 const __MAC_10_10_2 = 101002 const __MAC_10_10_3 = 101003 const __MAC_10_11 = 101100 const __MAC_10_11_2 = 101102 const __MAC_10_11_3 = 101103 const __MAC_10_11_4 = 101104 const __MAC_10_12 = 101200 const __MAC_10_12_1 = 101201 const __MAC_10_12_2 = 101202 const __MAC_10_12_4 = 101204 const __MAC_10_13 = 101300 const __MAC_10_13_1 = 101301 const __MAC_10_13_2 = 101302 const __MAC_10_13_4 = 101304 const __MAC_10_14 = 101400 const __MAC_10_14_1 = 101401 const __MAC_10_14_4 = 101404 const __MAC_10_14_5 = 101405 const __MAC_10_14_6 = 101406 const __MAC_10_15 = 101500 const __MAC_10_15_1 = 101501 const __MAC_10_15_4 = 101504 const __MAC_10_16 = 101600 const __MAC_10_2 = 1020 const __MAC_10_3 = 1030 const __MAC_10_4 = 1040 const __MAC_10_5 = 1050 const __MAC_10_6 = 1060 const __MAC_10_7 = 1070 const __MAC_10_8 = 1080 const __MAC_10_9 = 1090 const __MAC_11_0 = 110000 const __MAC_11_1 = 110100 const __MAC_11_3 = 110300 const __MAC_11_4 = 110400 const __MAC_11_5 = 110500 const __MAC_11_6 = 110600 const __MAC_12_0 = 120000 const __MAC_12_1 = 120100 const __MAC_12_2 = 120200 const __MAC_12_3 = 120300 const __MAC_12_4 = 120400 const __MAC_12_5 = 120500 const __MAC_12_6 = 120600 const __MAC_12_7 = 120700 const __MAC_13_0 = 130000 const __MAC_13_1 = 130100 const __MAC_13_2 = 130200 const __MAC_13_3 = 130300 const __MAC_13_4 = 130400 const __MAC_13_5 = 130500 const __MAC_13_6 = 130600 const __MAC_14_0 = 140000 const __MAC_14_1 = 140100 const __MAC_14_2 = 140200 const __MAC_14_3 = 140300 const __MAC_14_4 = 140400 const __MAC_OS_X_VERSION_MAX_ALLOWED = 140400 const __MAC_OS_X_VERSION_MIN_REQUIRED = 140000 const __NO_INLINE__ = 1 const __NO_MATH_ERRNO__ = 1 const __NULLABILITY_COMPLETENESS_POP = 0 const __NULLABILITY_COMPLETENESS_PUSH = 0 const __OBJC_BOOL_IS_BOOL = 1 const __OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES = 3 const __OPENCL_MEMORY_SCOPE_DEVICE = 2 const __OPENCL_MEMORY_SCOPE_SUB_GROUP = 4 const __OPENCL_MEMORY_SCOPE_WORK_GROUP = 1 const __OPENCL_MEMORY_SCOPE_WORK_ITEM = 0 const __ORDER_BIG_ENDIAN__ = 4321 const __ORDER_LITTLE_ENDIAN__ = 1234 const __ORDER_PDP_ENDIAN__ = 3412 const __PIC__ = 2 const __POINTER_WIDTH__ = 64 const __PRAGMA_REDEFINE_EXTNAME = 1 const __PRETTY_FUNCTION__ = 0 const __PTHREAD_ATTR_SIZE__ = 56 const __PTHREAD_CONDATTR_SIZE__ = 8 const __PTHREAD_COND_SIZE__ = 40 const __PTHREAD_MUTEXATTR_SIZE__ = 8 const __PTHREAD_MUTEX_SIZE__ = 56 const __PTHREAD_ONCE_SIZE__ = 8 const __PTHREAD_RWLOCKATTR_SIZE__ = 16 const __PTHREAD_RWLOCK_SIZE__ = 192 const __PTHREAD_SIZE__ = 8176 const __PTRDIFF_FMTd__ = "ld" const __PTRDIFF_FMTi__ = "li" const __PTRDIFF_MAX__ = 9223372036854775807 const __PTRDIFF_WIDTH__ = 64 const __SALC = 16384 const __SAPP = 256 const __SCHAR_MAX__ = 127 const __SEOF = 32 const __SERR = 64 const __SHRT_MAX__ = 32767 const __SHRT_WIDTH__ = 16 const __SIGN = 32768 const __SIG_ATOMIC_MAX__ = 2147483647 const __SIG_ATOMIC_WIDTH__ = 32 const __SIZEOF_DOUBLE__ = 8 const __SIZEOF_FLOAT__ = 4 const __SIZEOF_INT128__ = 16 const __SIZEOF_INT__ = 4 const __SIZEOF_LONG_DOUBLE__ = 8 const __SIZEOF_LONG_LONG__ = 8 const __SIZEOF_LONG__ = 8 const __SIZEOF_POINTER__ = 8 const __SIZEOF_PTRDIFF_T__ = 8 const __SIZEOF_SHORT__ = 2 const __SIZEOF_SIZE_T__ = 8 const __SIZEOF_WCHAR_T__ = 4 const __SIZEOF_WINT_T__ = 4 const __SIZE_FMTX__ = "lX" const __SIZE_FMTo__ = "lo" const __SIZE_FMTu__ = "lu" const __SIZE_FMTx__ = "lx" const __SIZE_MAX__ = 18446744073709551615 const __SIZE_WIDTH__ = 64 const __SLBF = 1 const __SMBF = 128 const __SMOD = 8192 const __SNBF = 2 const __SNPT = 2048 const __SOFF = 4096 const __SOPT = 1024 const __SQLITESESSION_H_ = 1 const __SRD = 4 const __SRW = 16 const __SSP__ = 1 const __SSTR = 512 const __STDC_HOSTED__ = 1 const __STDC_NO_THREADS__ = 1 const __STDC_UTF_16__ = 1 const __STDC_UTF_32__ = 1 const __STDC_VERSION__ = 201710 const __STDC_WANT_LIB_EXT1__ = 1 const __STDC__ = 1 const __SWR = 8 const __TVOS_10_0 = 100000 const __TVOS_10_0_1 = 100001 const __TVOS_10_1 = 100100 const __TVOS_10_2 = 100200 const __TVOS_11_0 = 110000 const __TVOS_11_1 = 110100 const __TVOS_11_2 = 110200 const __TVOS_11_3 = 110300 const __TVOS_11_4 = 110400 const __TVOS_12_0 = 120000 const __TVOS_12_1 = 120100 const __TVOS_12_2 = 120200 const __TVOS_12_3 = 120300 const __TVOS_12_4 = 120400 const __TVOS_13_0 = 130000 const __TVOS_13_2 = 130200 const __TVOS_13_3 = 130300 const __TVOS_13_4 = 130400 const __TVOS_14_0 = 140000 const __TVOS_14_1 = 140100 const __TVOS_14_2 = 140200 const __TVOS_14_3 = 140300 const __TVOS_14_5 = 140500 const __TVOS_14_6 = 140600 const __TVOS_14_7 = 140700 const __TVOS_15_0 = 150000 const __TVOS_15_1 = 150100 const __TVOS_15_2 = 150200 const __TVOS_15_3 = 150300 const __TVOS_15_4 = 150400 const __TVOS_15_5 = 150500 const __TVOS_15_6 = 150600 const __TVOS_16_0 = 160000 const __TVOS_16_1 = 160100 const __TVOS_16_2 = 160200 const __TVOS_16_3 = 160300 const __TVOS_16_4 = 160400 const __TVOS_16_5 = 160500 const __TVOS_16_6 = 160600 const __TVOS_17_0 = 170000 const __TVOS_17_1 = 170100 const __TVOS_17_2 = 170200 const __TVOS_17_3 = 170300 const __TVOS_17_4 = 170400 const __TVOS_9_0 = 90000 const __TVOS_9_1 = 90100 const __TVOS_9_2 = 90200 const __UINT16_FMTX__ = "hX" const __UINT16_FMTo__ = "ho" const __UINT16_FMTu__ = "hu" const __UINT16_FMTx__ = "hx" const __UINT16_MAX__ = 65535 const __UINT32_C_SUFFIX__ = 0 const __UINT32_FMTX__ = "X" const __UINT32_FMTo__ = "o" const __UINT32_FMTu__ = "u" const __UINT32_FMTx__ = "x" const __UINT32_MAX__ = 4294967295 const __UINT64_C_SUFFIX__ = 0 const __UINT64_FMTX__ = "llX" const __UINT64_FMTo__ = "llo" const __UINT64_FMTu__ = "llu" const __UINT64_FMTx__ = "llx" const __UINT64_MAX__ = 18446744073709551615 const __UINT8_FMTX__ = "hhX" const __UINT8_FMTo__ = "hho" const __UINT8_FMTu__ = "hhu" const __UINT8_FMTx__ = "hhx" const __UINT8_MAX__ = 255 const __UINTMAX_C_SUFFIX__ = 0 const __UINTMAX_FMTX__ = "lX" const __UINTMAX_FMTo__ = "lo" const __UINTMAX_FMTu__ = "lu" const __UINTMAX_FMTx__ = "lx" const __UINTMAX_MAX__ = 18446744073709551615 const __UINTMAX_WIDTH__ = 64 const __UINTPTR_FMTX__ = "lX" const __UINTPTR_FMTo__ = "lo" const __UINTPTR_FMTu__ = "lu" const __UINTPTR_FMTx__ = "lx" const __UINTPTR_MAX__ = 18446744073709551615 const __UINTPTR_WIDTH__ = 64 const __UINT_FAST16_FMTX__ = "hX" const __UINT_FAST16_FMTo__ = "ho" const __UINT_FAST16_FMTu__ = "hu" const __UINT_FAST16_FMTx__ = "hx" const __UINT_FAST16_MAX__ = 65535 const __UINT_FAST32_FMTX__ = "X" const __UINT_FAST32_FMTo__ = "o" const __UINT_FAST32_FMTu__ = "u" const __UINT_FAST32_FMTx__ = "x" const __UINT_FAST32_MAX__ = 4294967295 const __UINT_FAST64_FMTX__ = "llX" const __UINT_FAST64_FMTo__ = "llo" const __UINT_FAST64_FMTu__ = "llu" const __UINT_FAST64_FMTx__ = "llx" const __UINT_FAST64_MAX__ = 18446744073709551615 const __UINT_FAST8_FMTX__ = "hhX" const __UINT_FAST8_FMTo__ = "hho" const __UINT_FAST8_FMTu__ = "hhu" const __UINT_FAST8_FMTx__ = "hhx" const __UINT_FAST8_MAX__ = 255 const __UINT_LEAST16_FMTX__ = "hX" const __UINT_LEAST16_FMTo__ = "ho" const __UINT_LEAST16_FMTu__ = "hu" const __UINT_LEAST16_FMTx__ = "hx" const __UINT_LEAST16_MAX__ = 65535 const __UINT_LEAST32_FMTX__ = "X" const __UINT_LEAST32_FMTo__ = "o" const __UINT_LEAST32_FMTu__ = "u" const __UINT_LEAST32_FMTx__ = "x" const __UINT_LEAST32_MAX__ = 4294967295 const __UINT_LEAST64_FMTX__ = "llX" const __UINT_LEAST64_FMTo__ = "llo" const __UINT_LEAST64_FMTu__ = "llu" const __UINT_LEAST64_FMTx__ = "llx" const __UINT_LEAST64_MAX = 18446744073709551615 const __UINT_LEAST64_MAX__ = 18446744073709551615 const __UINT_LEAST8_FMTX__ = "hhX" const __UINT_LEAST8_FMTo__ = "hho" const __UINT_LEAST8_FMTu__ = "hhu" const __UINT_LEAST8_FMTx__ = "hhx" const __UINT_LEAST8_MAX__ = 255 const __USER_LABEL_PREFIX__ = 0 const __VERSION__ = "Apple LLVM 15.0.0 (clang-1500.3.9.4)" const __VISIONOS_1_0 = 10000 const __VISIONOS_1_1 = 10100 const __WATCHOS_10_0 = 100000 const __WATCHOS_10_1 = 100100 const __WATCHOS_10_2 = 100200 const __WATCHOS_10_3 = 100300 const __WATCHOS_10_4 = 100400 const __WATCHOS_1_0 = 10000 const __WATCHOS_2_0 = 20000 const __WATCHOS_2_1 = 20100 const __WATCHOS_2_2 = 20200 const __WATCHOS_3_0 = 30000 const __WATCHOS_3_1 = 30100 const __WATCHOS_3_1_1 = 30101 const __WATCHOS_3_2 = 30200 const __WATCHOS_4_0 = 40000 const __WATCHOS_4_1 = 40100 const __WATCHOS_4_2 = 40200 const __WATCHOS_4_3 = 40300 const __WATCHOS_5_0 = 50000 const __WATCHOS_5_1 = 50100 const __WATCHOS_5_2 = 50200 const __WATCHOS_5_3 = 50300 const __WATCHOS_6_0 = 60000 const __WATCHOS_6_1 = 60100 const __WATCHOS_6_2 = 60200 const __WATCHOS_7_0 = 70000 const __WATCHOS_7_1 = 70100 const __WATCHOS_7_2 = 70200 const __WATCHOS_7_3 = 70300 const __WATCHOS_7_4 = 70400 const __WATCHOS_7_5 = 70500 const __WATCHOS_7_6 = 70600 const __WATCHOS_8_0 = 80000 const __WATCHOS_8_1 = 80100 const __WATCHOS_8_3 = 80300 const __WATCHOS_8_4 = 80400 const __WATCHOS_8_5 = 80500 const __WATCHOS_8_6 = 80600 const __WATCHOS_8_7 = 80700 const __WATCHOS_8_8 = 80800 const __WATCHOS_9_0 = 90000 const __WATCHOS_9_1 = 90100 const __WATCHOS_9_2 = 90200 const __WATCHOS_9_3 = 90300 const __WATCHOS_9_4 = 90400 const __WATCHOS_9_5 = 90500 const __WATCHOS_9_6 = 90600 const __WCHAR_MAX__ = 2147483647 const __WCHAR_TYPE__ = 0 const __WCHAR_WIDTH__ = 32 const __WINT_MAX__ = 2147483647 const __WINT_TYPE__ = 0 const __WINT_WIDTH__ = 32 const __aarch64__ = 1 const __apple_build_version__ = 15000309 const __arm64 = 1 const __arm64__ = 1 const __bool_true_false_are_defined = 1 const __clang__ = 1 const __clang_literal_encoding__ = "UTF-8" const __clang_major__ = 15 const __clang_minor__ = 0 const __clang_patchlevel__ = 0 const __clang_version__ = "15.0.0 (clang-1500.3.9.4)" const __clang_wide_literal_encoding__ = "UTF-32" const __const = 0 const __exported_pop = 0 const __exported_push = 0 const __has_ptrcheck = 0 const __has_safe_buffers = 0 const __header_inline = 0 const __llvm__ = 1 const __nonnull = 0 const __null_unspecified = 0 const __nullable = 0 const __pic__ = 2 const __restrict = 0 const __restrict_arr = 0 const __signed = 0 const __unreachable_ok_pop = 0 const __unreachable_ok_push = 0 const __volatile = 0 const bBatch = 0 const bool1 = 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 false1 = 0 const fdatasync = 0 const first_valueInvFunc = 0 const first_valueValueFunc = 0 const fts5GetVarint = 0 const fts5YYDYNSTACK = 0 const fts5YYFREE = 0 const fts5YYGROWABLESTACK = 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 fts5YYREALLOC = 0 const fts5YYSTACKDEPTH = 100 const fts5YY_ACCEPT_ACTION = 81 const fts5YY_ACTTAB_COUNT = 105 const fts5YY_ERROR_ACTION = 80 const fts5YY_MAX_DSTRCTR = 24 const fts5YY_MAX_REDUCE = 110 const fts5YY_MAX_SHIFT = 34 const fts5YY_MAX_SHIFTREDUCE = 79 const fts5YY_MIN_DSTRCTR = 16 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 math_errhandling = 0 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 ru_first = 0 const ru_last = 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 static_assert = 0 const stderr = 0 const stdin = 0 const stdout = 0 const sv_onstack = 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 true1 = 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 = uint64 type t__predefined_wchar_t = int32 type t__predefined_ptrdiff_t = int64 type Tva_list = uintptr type va_list = Tva_list type t__gnuc_va_list = uintptr // 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 { FpVfs uintptr FpVdbe uintptr FpDfltColl uintptr Fmutex uintptr FaDb uintptr FnDb int32 FmDbFlags Tu32 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 Fu1 struct { 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 } 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 one 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 { 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 { FnConstraint int32 FaConstraint uintptr FnOrderBy int32 FaOrderBy uintptr FaConstraintUsage uintptr FidxNum int32 FidxStr uintptr FneedToFreeIdxStr int32 ForderByConsumed int32 FestimatedCost float64 FestimatedRows Tsqlite3_int64 FidxFlags int32 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 { FnConstraint int32 FaConstraint uintptr FnOrderBy int32 FaOrderBy uintptr FaConstraintUsage uintptr FidxNum int32 FidxStr uintptr FneedToFreeIdxStr int32 ForderByConsumed int32 FestimatedCost float64 FestimatedRows Tsqlite3_int64 FidxFlags int32 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 { 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 } 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 { 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 } 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 { 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 Frec TSessionBuffer 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 pUserData pointer passed to the xCreateFunction() ** API when the extension function was registered. ** ** 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 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__darwin_intptr_t = int64 type t__darwin_natural_t = uint32 type t__darwin_ct_rune_t = int32 type t__mbstate_t = struct { F_mbstateL [0]int64 F__mbstate8 [128]int8 } type t__darwin_mbstate_t = struct { F_mbstateL [0]int64 F__mbstate8 [128]int8 } type t__darwin_ptrdiff_t = int64 type t__darwin_size_t = uint64 type t__darwin_va_list = uintptr type t__darwin_wchar_t = int32 type t__darwin_rune_t = int32 type t__darwin_wint_t = int32 type t__darwin_clock_t = uint64 type t__darwin_socklen_t = uint32 type t__darwin_ssize_t = int64 type t__darwin_time_t = int64 type t__darwin_blkcnt_t = int64 type t__darwin_blksize_t = int32 type t__darwin_dev_t = int32 type t__darwin_fsblkcnt_t = uint32 type t__darwin_fsfilcnt_t = uint32 type t__darwin_gid_t = uint32 type t__darwin_id_t = uint32 type t__darwin_ino64_t = uint64 type t__darwin_ino_t = uint64 type t__darwin_mach_port_name_t = uint32 type t__darwin_mach_port_t = uint32 type t__darwin_mode_t = uint16 type t__darwin_off_t = int64 type t__darwin_pid_t = int32 type t__darwin_sigset_t = uint32 type t__darwin_suseconds_t = int32 type t__darwin_uid_t = uint32 type t__darwin_useconds_t = uint32 type t__darwin_uuid_t = [16]uint8 type t__darwin_uuid_string_t = [37]int8 type t__darwin_pthread_handler_rec = struct { F__routine uintptr F__arg uintptr F__next uintptr } type T_opaque_pthread_attr_t = struct { F__sig int64 F__opaque [56]int8 } type _opaque_pthread_attr_t = T_opaque_pthread_attr_t type T_opaque_pthread_cond_t = struct { F__sig int64 F__opaque [40]int8 } type _opaque_pthread_cond_t = T_opaque_pthread_cond_t type T_opaque_pthread_condattr_t = struct { F__sig int64 F__opaque [8]int8 } type _opaque_pthread_condattr_t = T_opaque_pthread_condattr_t type T_opaque_pthread_mutex_t = struct { F__sig int64 F__opaque [56]int8 } type _opaque_pthread_mutex_t = T_opaque_pthread_mutex_t type T_opaque_pthread_mutexattr_t = struct { F__sig int64 F__opaque [8]int8 } type _opaque_pthread_mutexattr_t = T_opaque_pthread_mutexattr_t type T_opaque_pthread_once_t = struct { F__sig int64 F__opaque [8]int8 } type _opaque_pthread_once_t = T_opaque_pthread_once_t type T_opaque_pthread_rwlock_t = struct { F__sig int64 F__opaque [192]int8 } type _opaque_pthread_rwlock_t = T_opaque_pthread_rwlock_t type T_opaque_pthread_rwlockattr_t = struct { F__sig int64 F__opaque [16]int8 } type _opaque_pthread_rwlockattr_t = T_opaque_pthread_rwlockattr_t type T_opaque_pthread_t = struct { F__sig int64 F__cleanup_stack uintptr F__opaque [8176]int8 } type _opaque_pthread_t = T_opaque_pthread_t type t__darwin_pthread_attr_t = struct { F__sig int64 F__opaque [56]int8 } type t__darwin_pthread_cond_t = struct { F__sig int64 F__opaque [40]int8 } type t__darwin_pthread_condattr_t = struct { F__sig int64 F__opaque [8]int8 } type t__darwin_pthread_key_t = uint64 type t__darwin_pthread_mutex_t = struct { F__sig int64 F__opaque [56]int8 } type t__darwin_pthread_mutexattr_t = struct { F__sig int64 F__opaque [8]int8 } type t__darwin_pthread_once_t = struct { F__sig int64 F__opaque [8]int8 } type t__darwin_pthread_rwlock_t = struct { F__sig int64 F__opaque [192]int8 } type t__darwin_pthread_rwlockattr_t = struct { F__sig int64 F__opaque [16]int8 } type t__darwin_pthread_t = uintptr type t__darwin_nl_item = int32 type t__darwin_wctrans_t = int32 type t__darwin_wctype_t = uint32 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 = int64 type register_t = Tregister_t type Tintptr_t = int64 type intptr_t = Tintptr_t type Tuintptr_t = uint64 type uintptr_t = Tuintptr_t type Tuser_addr_t = uint64 type user_addr_t = Tuser_addr_t type Tuser_size_t = uint64 type user_size_t = Tuser_size_t type Tuser_ssize_t = int64 type user_ssize_t = Tuser_ssize_t type Tuser_long_t = int64 type user_long_t = Tuser_long_t type Tuser_ulong_t = uint64 type user_ulong_t = Tuser_ulong_t type Tuser_time_t = int64 type user_time_t = Tuser_time_t type Tuser_off_t = int64 type user_off_t = Tuser_off_t type Tsyscall_arg_t = uint64 type syscall_arg_t = Tsyscall_arg_t type Tsize_t = uint64 type size_t = Tsize_t type Tfpos_t = int64 type fpos_t = Tfpos_t type t__sbuf = struct { F_base uintptr F_size int32 } type TFILE = struct { F_p uintptr F_r int32 F_w int32 F_flags int16 F_file int16 F_bf t__sbuf F_lbfsize int32 F_cookie uintptr F_close uintptr F_read uintptr F_seek uintptr F_write uintptr F_ub t__sbuf F_extra uintptr F_ur int32 F_ubuf [3]uint8 F_nbuf [1]uint8 F_lb t__sbuf F_blksize int32 F_offset Tfpos_t } type FILE = TFILE type t__sFILE = TFILE type Toff_t = int64 type off_t = Toff_t type Tssize_t = int64 type ssize_t = Tssize_t type Tidtype_t = int32 type idtype_t = Tidtype_t const _P_ALL = 0 const _P_PID = 1 const _P_PGID = 2 type Tpid_t = int32 type pid_t = Tpid_t type Tid_t = uint32 type id_t = Tid_t type Tsig_atomic_t = int32 type sig_atomic_t = Tsig_atomic_t type t__darwin_arm_exception_state = struct { F__exception t__uint32_t F__fsr t__uint32_t F__far t__uint32_t } type t__darwin_arm_exception_state64 = struct { F__far t__uint64_t F__esr t__uint32_t F__exception t__uint32_t } type t__darwin_arm_thread_state = struct { F__r [13]t__uint32_t F__sp t__uint32_t F__lr t__uint32_t F__pc t__uint32_t F__cpsr t__uint32_t } type t__darwin_arm_thread_state64 = struct { F__x [29]t__uint64_t F__fp t__uint64_t F__lr t__uint64_t F__sp t__uint64_t F__pc t__uint64_t F__cpsr t__uint32_t F__pad t__uint32_t } type t__darwin_arm_vfp_state = struct { F__r [64]t__uint32_t F__fpscr t__uint32_t } type t__darwin_arm_neon_state64 = struct { F__ccgo_align [0]uint64 F__v [32][2]uint64 F__fpsr t__uint32_t F__fpcr t__uint32_t F__ccgo_pad3 [8]byte } type t__darwin_arm_neon_state = struct { F__ccgo_align [0]uint64 F__v [16][2]uint64 F__fpsr t__uint32_t F__fpcr t__uint32_t F__ccgo_pad3 [8]byte } type t__arm_pagein_state = struct { F__pagein_error int32 } type t__arm_legacy_debug_state = struct { F__bvr [16]t__uint32_t F__bcr [16]t__uint32_t F__wvr [16]t__uint32_t F__wcr [16]t__uint32_t } type t__darwin_arm_debug_state32 = struct { F__bvr [16]t__uint32_t F__bcr [16]t__uint32_t F__wvr [16]t__uint32_t F__wcr [16]t__uint32_t F__mdscr_el1 t__uint64_t } type t__darwin_arm_debug_state64 = struct { F__bvr [16]t__uint64_t F__bcr [16]t__uint64_t F__wvr [16]t__uint64_t F__wcr [16]t__uint64_t F__mdscr_el1 t__uint64_t } type t__darwin_arm_cpmu_state64 = struct { F__ctrs [16]t__uint64_t } type t__darwin_mcontext32 = struct { F__es t__darwin_arm_exception_state F__ss t__darwin_arm_thread_state F__fs t__darwin_arm_vfp_state } type t__darwin_mcontext64 = struct { F__ccgo_align [0]uint64 F__es t__darwin_arm_exception_state64 F__ss t__darwin_arm_thread_state64 F__ns t__darwin_arm_neon_state64 } type Tmcontext_t = uintptr type mcontext_t = Tmcontext_t type Tpthread_attr_t = struct { F__sig int64 F__opaque [56]int8 } type pthread_attr_t = Tpthread_attr_t type t__darwin_sigaltstack = struct { Fss_sp uintptr Fss_size t__darwin_size_t Fss_flags int32 } type Tstack_t = struct { Fss_sp uintptr Fss_size t__darwin_size_t Fss_flags int32 } type stack_t = Tstack_t type t__darwin_ucontext = struct { Fuc_onstack int32 Fuc_sigmask t__darwin_sigset_t Fuc_stack t__darwin_sigaltstack Fuc_link uintptr Fuc_mcsize t__darwin_size_t Fuc_mcontext uintptr } type Tucontext_t = struct { Fuc_onstack int32 Fuc_sigmask t__darwin_sigset_t Fuc_stack t__darwin_sigaltstack Fuc_link uintptr Fuc_mcsize t__darwin_size_t Fuc_mcontext uintptr } type ucontext_t = Tucontext_t type Tsigset_t = uint32 type sigset_t = Tsigset_t type Tuid_t = uint32 type uid_t = Tuid_t type Tsigval = struct { Fsival_ptr [0]uintptr Fsival_int int32 F__ccgo_pad2 [4]byte } type sigval = Tsigval type Tsigevent = struct { Fsigev_notify int32 Fsigev_signo int32 Fsigev_value Tsigval Fsigev_notify_function uintptr Fsigev_notify_attributes uintptr } type sigevent = Tsigevent type Tsiginfo_t = struct { Fsi_signo int32 Fsi_errno int32 Fsi_code int32 Fsi_pid Tpid_t Fsi_uid Tuid_t Fsi_status int32 Fsi_addr uintptr Fsi_value Tsigval Fsi_band int64 F__pad [7]uint64 } type siginfo_t = Tsiginfo_t type t__siginfo = Tsiginfo_t type t__sigaction_u = struct { F__sa_sigaction [0]uintptr F__sa_handler uintptr } type t__sigaction = struct { F__sigaction_u t__sigaction_u Fsa_tramp uintptr Fsa_mask Tsigset_t Fsa_flags int32 } type Tsigaction = struct { F__sigaction_u t__sigaction_u Fsa_mask Tsigset_t Fsa_flags int32 } type sigaction = Tsigaction type Tsig_t = uintptr type sig_t = Tsig_t type Tsigvec = struct { Fsv_handler uintptr Fsv_mask int32 Fsv_flags int32 } type sigvec = Tsigvec type Tsigstack = struct { Fss_sp uintptr Fss_onstack int32 } type sigstack = Tsigstack type Tuint64_t = uint64 type uint64_t = Tuint64_t type Tint_least64_t = int64 type int_least64_t = Tint_least64_t type Tuint_least64_t = uint64 type uint_least64_t = Tuint_least64_t type Tint_fast64_t = int64 type int_fast64_t = Tint_fast64_t type Tuint_fast64_t = uint64 type uint_fast64_t = Tuint_fast64_t type Tuint32_t = uint32 type uint32_t = Tuint32_t type Tint_least32_t = int32 type int_least32_t = Tint_least32_t type Tuint_least32_t = uint32 type uint_least32_t = Tuint_least32_t type Tint_fast32_t = int32 type int_fast32_t = Tint_fast32_t type Tuint_fast32_t = uint32 type uint_fast32_t = Tuint_fast32_t type Tuint16_t = uint16 type uint16_t = Tuint16_t type Tint_least16_t = int16 type int_least16_t = Tint_least16_t type Tuint_least16_t = uint16 type uint_least16_t = Tuint_least16_t type Tint_fast16_t = int16 type int_fast16_t = Tint_fast16_t type Tuint_fast16_t = uint16 type uint_fast16_t = Tuint_fast16_t type Tuint8_t = uint8 type uint8_t = Tuint8_t type Tint_least8_t = int8 type int_least8_t = Tint_least8_t type Tuint_least8_t = uint8 type uint_least8_t = Tuint_least8_t type Tint_fast8_t = int8 type int_fast8_t = Tint_fast8_t type Tuint_fast8_t = uint8 type uint_fast8_t = Tuint_fast8_t type Tintmax_t = int64 type intmax_t = Tintmax_t type Tuintmax_t = uint64 type uintmax_t = Tuintmax_t type Ttimeval = struct { Ftv_sec t__darwin_time_t Ftv_usec t__darwin_suseconds_t } type timeval = Ttimeval type Trlim_t = uint64 type rlim_t = Trlim_t type Trusage = struct { Fru_utime Ttimeval Fru_stime Ttimeval Fru_maxrss int64 Fru_ixrss int64 Fru_idrss int64 Fru_isrss int64 Fru_minflt int64 Fru_majflt int64 Fru_nswap int64 Fru_inblock int64 Fru_oublock int64 Fru_msgsnd int64 Fru_msgrcv int64 Fru_nsignals int64 Fru_nvcsw int64 Fru_nivcsw int64 } type rusage = Trusage type Trusage_info_t = uintptr type rusage_info_t = Trusage_info_t type Trusage_info_v0 = struct { Fri_uuid [16]Tuint8_t Fri_user_time Tuint64_t Fri_system_time Tuint64_t Fri_pkg_idle_wkups Tuint64_t Fri_interrupt_wkups Tuint64_t Fri_pageins Tuint64_t Fri_wired_size Tuint64_t Fri_resident_size Tuint64_t Fri_phys_footprint Tuint64_t Fri_proc_start_abstime Tuint64_t Fri_proc_exit_abstime Tuint64_t } type rusage_info_v0 = Trusage_info_v0 type Trusage_info_v1 = struct { Fri_uuid [16]Tuint8_t Fri_user_time Tuint64_t Fri_system_time Tuint64_t Fri_pkg_idle_wkups Tuint64_t Fri_interrupt_wkups Tuint64_t Fri_pageins Tuint64_t Fri_wired_size Tuint64_t Fri_resident_size Tuint64_t Fri_phys_footprint Tuint64_t Fri_proc_start_abstime Tuint64_t Fri_proc_exit_abstime Tuint64_t Fri_child_user_time Tuint64_t Fri_child_system_time Tuint64_t Fri_child_pkg_idle_wkups Tuint64_t Fri_child_interrupt_wkups Tuint64_t Fri_child_pageins Tuint64_t Fri_child_elapsed_abstime Tuint64_t } type rusage_info_v1 = Trusage_info_v1 type Trusage_info_v2 = struct { Fri_uuid [16]Tuint8_t Fri_user_time Tuint64_t Fri_system_time Tuint64_t Fri_pkg_idle_wkups Tuint64_t Fri_interrupt_wkups Tuint64_t Fri_pageins Tuint64_t Fri_wired_size Tuint64_t Fri_resident_size Tuint64_t Fri_phys_footprint Tuint64_t Fri_proc_start_abstime Tuint64_t Fri_proc_exit_abstime Tuint64_t Fri_child_user_time Tuint64_t Fri_child_system_time Tuint64_t Fri_child_pkg_idle_wkups Tuint64_t Fri_child_interrupt_wkups Tuint64_t Fri_child_pageins Tuint64_t Fri_child_elapsed_abstime Tuint64_t Fri_diskio_bytesread Tuint64_t Fri_diskio_byteswritten Tuint64_t } type rusage_info_v2 = Trusage_info_v2 type Trusage_info_v3 = struct { Fri_uuid [16]Tuint8_t Fri_user_time Tuint64_t Fri_system_time Tuint64_t Fri_pkg_idle_wkups Tuint64_t Fri_interrupt_wkups Tuint64_t Fri_pageins Tuint64_t Fri_wired_size Tuint64_t Fri_resident_size Tuint64_t Fri_phys_footprint Tuint64_t Fri_proc_start_abstime Tuint64_t Fri_proc_exit_abstime Tuint64_t Fri_child_user_time Tuint64_t Fri_child_system_time Tuint64_t Fri_child_pkg_idle_wkups Tuint64_t Fri_child_interrupt_wkups Tuint64_t Fri_child_pageins Tuint64_t Fri_child_elapsed_abstime Tuint64_t Fri_diskio_bytesread Tuint64_t Fri_diskio_byteswritten Tuint64_t Fri_cpu_time_qos_default Tuint64_t Fri_cpu_time_qos_maintenance Tuint64_t Fri_cpu_time_qos_background Tuint64_t Fri_cpu_time_qos_utility Tuint64_t Fri_cpu_time_qos_legacy Tuint64_t Fri_cpu_time_qos_user_initiated Tuint64_t Fri_cpu_time_qos_user_interactive Tuint64_t Fri_billed_system_time Tuint64_t Fri_serviced_system_time Tuint64_t } type rusage_info_v3 = Trusage_info_v3 type Trusage_info_v4 = struct { Fri_uuid [16]Tuint8_t Fri_user_time Tuint64_t Fri_system_time Tuint64_t Fri_pkg_idle_wkups Tuint64_t Fri_interrupt_wkups Tuint64_t Fri_pageins Tuint64_t Fri_wired_size Tuint64_t Fri_resident_size Tuint64_t Fri_phys_footprint Tuint64_t Fri_proc_start_abstime Tuint64_t Fri_proc_exit_abstime Tuint64_t Fri_child_user_time Tuint64_t Fri_child_system_time Tuint64_t Fri_child_pkg_idle_wkups Tuint64_t Fri_child_interrupt_wkups Tuint64_t Fri_child_pageins Tuint64_t Fri_child_elapsed_abstime Tuint64_t Fri_diskio_bytesread Tuint64_t Fri_diskio_byteswritten Tuint64_t Fri_cpu_time_qos_default Tuint64_t Fri_cpu_time_qos_maintenance Tuint64_t Fri_cpu_time_qos_background Tuint64_t Fri_cpu_time_qos_utility Tuint64_t Fri_cpu_time_qos_legacy Tuint64_t Fri_cpu_time_qos_user_initiated Tuint64_t Fri_cpu_time_qos_user_interactive Tuint64_t Fri_billed_system_time Tuint64_t Fri_serviced_system_time Tuint64_t Fri_logical_writes Tuint64_t Fri_lifetime_max_phys_footprint Tuint64_t Fri_instructions Tuint64_t Fri_cycles Tuint64_t Fri_billed_energy Tuint64_t Fri_serviced_energy Tuint64_t Fri_interval_max_phys_footprint Tuint64_t Fri_runnable_time Tuint64_t } type rusage_info_v4 = Trusage_info_v4 type Trusage_info_v5 = struct { Fri_uuid [16]Tuint8_t Fri_user_time Tuint64_t Fri_system_time Tuint64_t Fri_pkg_idle_wkups Tuint64_t Fri_interrupt_wkups Tuint64_t Fri_pageins Tuint64_t Fri_wired_size Tuint64_t Fri_resident_size Tuint64_t Fri_phys_footprint Tuint64_t Fri_proc_start_abstime Tuint64_t Fri_proc_exit_abstime Tuint64_t Fri_child_user_time Tuint64_t Fri_child_system_time Tuint64_t Fri_child_pkg_idle_wkups Tuint64_t Fri_child_interrupt_wkups Tuint64_t Fri_child_pageins Tuint64_t Fri_child_elapsed_abstime Tuint64_t Fri_diskio_bytesread Tuint64_t Fri_diskio_byteswritten Tuint64_t Fri_cpu_time_qos_default Tuint64_t Fri_cpu_time_qos_maintenance Tuint64_t Fri_cpu_time_qos_background Tuint64_t Fri_cpu_time_qos_utility Tuint64_t Fri_cpu_time_qos_legacy Tuint64_t Fri_cpu_time_qos_user_initiated Tuint64_t Fri_cpu_time_qos_user_interactive Tuint64_t Fri_billed_system_time Tuint64_t Fri_serviced_system_time Tuint64_t Fri_logical_writes Tuint64_t Fri_lifetime_max_phys_footprint Tuint64_t Fri_instructions Tuint64_t Fri_cycles Tuint64_t Fri_billed_energy Tuint64_t Fri_serviced_energy Tuint64_t Fri_interval_max_phys_footprint Tuint64_t Fri_runnable_time Tuint64_t Fri_flags Tuint64_t } type rusage_info_v5 = Trusage_info_v5 type Trusage_info_v6 = struct { Fri_uuid [16]Tuint8_t Fri_user_time Tuint64_t Fri_system_time Tuint64_t Fri_pkg_idle_wkups Tuint64_t Fri_interrupt_wkups Tuint64_t Fri_pageins Tuint64_t Fri_wired_size Tuint64_t Fri_resident_size Tuint64_t Fri_phys_footprint Tuint64_t Fri_proc_start_abstime Tuint64_t Fri_proc_exit_abstime Tuint64_t Fri_child_user_time Tuint64_t Fri_child_system_time Tuint64_t Fri_child_pkg_idle_wkups Tuint64_t Fri_child_interrupt_wkups Tuint64_t Fri_child_pageins Tuint64_t Fri_child_elapsed_abstime Tuint64_t Fri_diskio_bytesread Tuint64_t Fri_diskio_byteswritten Tuint64_t Fri_cpu_time_qos_default Tuint64_t Fri_cpu_time_qos_maintenance Tuint64_t Fri_cpu_time_qos_background Tuint64_t Fri_cpu_time_qos_utility Tuint64_t Fri_cpu_time_qos_legacy Tuint64_t Fri_cpu_time_qos_user_initiated Tuint64_t Fri_cpu_time_qos_user_interactive Tuint64_t Fri_billed_system_time Tuint64_t Fri_serviced_system_time Tuint64_t Fri_logical_writes Tuint64_t Fri_lifetime_max_phys_footprint Tuint64_t Fri_instructions Tuint64_t Fri_cycles Tuint64_t Fri_billed_energy Tuint64_t Fri_serviced_energy Tuint64_t Fri_interval_max_phys_footprint Tuint64_t Fri_runnable_time Tuint64_t Fri_flags Tuint64_t Fri_user_ptime Tuint64_t Fri_system_ptime Tuint64_t Fri_pinstructions Tuint64_t Fri_pcycles Tuint64_t Fri_energy_nj Tuint64_t Fri_penergy_nj Tuint64_t Fri_secure_time_in_system Tuint64_t Fri_secure_ptime_in_system Tuint64_t Fri_reserved [12]Tuint64_t } type rusage_info_v6 = Trusage_info_v6 type Trusage_info_current = struct { Fri_uuid [16]Tuint8_t Fri_user_time Tuint64_t Fri_system_time Tuint64_t Fri_pkg_idle_wkups Tuint64_t Fri_interrupt_wkups Tuint64_t Fri_pageins Tuint64_t Fri_wired_size Tuint64_t Fri_resident_size Tuint64_t Fri_phys_footprint Tuint64_t Fri_proc_start_abstime Tuint64_t Fri_proc_exit_abstime Tuint64_t Fri_child_user_time Tuint64_t Fri_child_system_time Tuint64_t Fri_child_pkg_idle_wkups Tuint64_t Fri_child_interrupt_wkups Tuint64_t Fri_child_pageins Tuint64_t Fri_child_elapsed_abstime Tuint64_t Fri_diskio_bytesread Tuint64_t Fri_diskio_byteswritten Tuint64_t Fri_cpu_time_qos_default Tuint64_t Fri_cpu_time_qos_maintenance Tuint64_t Fri_cpu_time_qos_background Tuint64_t Fri_cpu_time_qos_utility Tuint64_t Fri_cpu_time_qos_legacy Tuint64_t Fri_cpu_time_qos_user_initiated Tuint64_t Fri_cpu_time_qos_user_interactive Tuint64_t Fri_billed_system_time Tuint64_t Fri_serviced_system_time Tuint64_t Fri_logical_writes Tuint64_t Fri_lifetime_max_phys_footprint Tuint64_t Fri_instructions Tuint64_t Fri_cycles Tuint64_t Fri_billed_energy Tuint64_t Fri_serviced_energy Tuint64_t Fri_interval_max_phys_footprint Tuint64_t Fri_runnable_time Tuint64_t Fri_flags Tuint64_t Fri_user_ptime Tuint64_t Fri_system_ptime Tuint64_t Fri_pinstructions Tuint64_t Fri_pcycles Tuint64_t Fri_energy_nj Tuint64_t Fri_penergy_nj Tuint64_t Fri_secure_time_in_system Tuint64_t Fri_secure_ptime_in_system Tuint64_t Fri_reserved [12]Tuint64_t } type rusage_info_current = Trusage_info_current type Trlimit = struct { Frlim_cur Trlim_t Frlim_max Trlim_t } type rlimit = Trlimit type Tproc_rlimit_control_wakeupmon = struct { Fwm_flags Tuint32_t Fwm_rate Tint32_t } type proc_rlimit_control_wakeupmon = Tproc_rlimit_control_wakeupmon type T_OSUnalignedU16 = struct { F__val Tuint16_t } type _OSUnalignedU16 = T_OSUnalignedU16 type T_OSUnalignedU32 = struct { F__val Tuint32_t } type _OSUnalignedU32 = T_OSUnalignedU32 type T_OSUnalignedU64 = struct { F__val Tuint64_t } type _OSUnalignedU64 = T_OSUnalignedU64 type Twait = struct { Fw_T [0]struct { F__ccgo0 uint32 } Fw_S [0]struct { F__ccgo0 uint32 } Fw_status int32 } type wait = Twait type Tct_rune_t = int32 type ct_rune_t = Tct_rune_t type Trune_t = int32 type rune_t = Trune_t type Twchar_t = int32 type wchar_t = Twchar_t type Tdiv_t = struct { Fquot int32 Frem int32 } type div_t = Tdiv_t type Tldiv_t = struct { Fquot int64 Frem int64 } type ldiv_t = Tldiv_t type Tlldiv_t = struct { Fquot int64 Frem int64 } type lldiv_t = Tlldiv_t type Tmalloc_type_id_t = uint64 type malloc_type_id_t = Tmalloc_type_id_t type Tdev_t = int32 type dev_t = Tdev_t type Tmode_t = uint16 type mode_t = Tmode_t type Trsize_t = uint64 type rsize_t = Trsize_t type Terrno_t = int32 type errno_t = Terrno_t type Tptrdiff_t = int64 type ptrdiff_t = Tptrdiff_t type Tmax_align_t = float64 type max_align_t = Tmax_align_t /* Some C libraries expect to see a wint_t here. Others (notably MinGW) will use __WINT_TYPE__ directly; accommodate both by requiring __need_wint_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 = uint64 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 adjustments ** 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][124]Tu32 FapSub [0][62]uintptr FaBitmap [496]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__ccgo8 uint8 Faffinity int8 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]int8 } 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 int8 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 } } 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 int8 FisSpecial int8 Fn int32 FiDP int32 Fz uintptr FzBuf [24]int8 } 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 { 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__ccgo100 uint16 FnSample int32 FmxSample int32 FnSampleCol int32 FaAvgEq uintptr FaSample uintptr FaiRowEst uintptr 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 FbHasWith 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 F__ccgo_pad2 [4]byte } 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 { 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 } type PreUpdate = TPreUpdate type TPrintfArguments = struct { FnArg int32 FnUsed int32 FapArg uintptr } type PrintfArguments = TPrintfArguments type TRCStr = struct { 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]int8 } 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 { FzName uintptr FnDeferredCons Ti64 FnDeferredImmCons Ti64 FpNext uintptr } 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 { 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 } FcolUsed TBitmask Fu1 struct { FpFuncArg [0]uintptr FnRow [0]Tu32 FzIndexedBy uintptr } Fu2 struct { FpCteUse [0]uintptr FpIBIndex uintptr } } type SrcItem = TSrcItem type TSrcList = struct { 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 { FpKeyInfo uintptr FaMem uintptr Fu struct { Fi [0]Ti64 Fz uintptr } Fn int32 FnField Tu16 Fdefault_rc Ti8 FerrCode Tu8 Fr1 Ti8 Fr2 Ti8 FeqSeen Tu8 } 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 { 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__ccgo68 uint8 FnRowOut TLogEst FiTop int32 FiEndWhere int32 FpLoops uintptr FpMemToFree uintptr 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 { 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 FjournalOff Ti64 FjournalHdr Ti64 FpBackup uintptr FaSavepoint uintptr FnSavepoint int32 FiDataVersion Tu32 FdbFileVers [16]int8 FnMmapOut int32 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 { FpPage uintptr FpData uintptr FpExtra uintptr FpCache uintptr FpDirty uintptr FpPager uintptr Fpgno TPgno Fflags Tu16 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 { FeState Tu8 FcurFlags Tu8 FcurPagerFlags Tu8 Fhints Tu8 FskipNext int32 FpBtree uintptr FaOverflow uintptr FpKey uintptr FpBt uintptr FpNext uintptr Finfo TCellInfo FnKey Ti64 FpgnoRoot TPgno FiPage Ti8 FcurIntKey Tu8 Fix Tu16 FaiIdx [19]Tu16 FpKeyInfo uintptr FpPage uintptr FapPage [19]uintptr } 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 { FpKey uintptr FnKey Tsqlite3_int64 FpData uintptr FaMem uintptr FnMem Tu16 FnData int32 FnZero int32 } 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 { FpKey uintptr FnKey Tsqlite3_int64 FpData uintptr FaMem uintptr FnMem Tu16 FnData int32 FnZero int32 } 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 { Fdb uintptr FppVPrev uintptr FpVNext uintptr FpParse uintptr FnVar TynVar FnMem int32 FnCursor int32 FcacheCtr Tu32 Fpc int32 Frc int32 FnChange Ti64 FiStatement int32 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 FstartTime Ti64 FnResColumn Tu16 FnResAlloc Tu16 FerrorAction Tu8 FminWriteFileFormat Tu8 FprepFlags Tu8 FeVdbeState Tu8 F__ccgo200 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 { 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 { FpPage uintptr FpData uintptr FpExtra uintptr FpCache uintptr FpDirty uintptr FpPager uintptr Fpgno TPgno Fflags Tu16 FnRef Ti64 FpDirtyNext uintptr FpDirtyPrev uintptr } type PgHdr = TPgHdr type TPCache = struct { FpDirty uintptr FpDirtyTail uintptr FpSynced uintptr 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 { FpVfs uintptr FpVdbe uintptr FpDfltColl uintptr Fmutex uintptr FaDb uintptr FnDb int32 FmDbFlags Tu32 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 Fu1 struct { 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 } 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 { FzName uintptr FnDeferredCons Ti64 FnDeferredImmCons Ti64 FpNext uintptr } 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__ccgo8 uint8 Faffinity int8 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 { FpKeyInfo uintptr FaMem uintptr Fu struct { Fi [0]Ti64 Fz uintptr } Fn int32 FnField Tu16 Fdefault_rc Ti8 FerrCode Tu8 Fr1 Ti8 Fr2 Ti8 FeqSeen Tu8 } 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 { 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__ccgo100 uint16 FnSample int32 FmxSample int32 FnSampleCol int32 FaAvgEq uintptr FaSample uintptr FaiRowEst uintptr 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 int8 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 } } 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 ** u1.nRow !fg.isTabFunc && !fg.isIndexedBy ** ** u2.pIBIndex fg.isIndexedBy && !fg.isCte ** u2.pCteUse fg.isCte && !fg.isIndexedBy */ type TSrcItem1 = struct { 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 } FcolUsed TBitmask Fu1 struct { FpFuncArg [0]uintptr FnRow [0]Tu32 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 { 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 FbHasWith 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 F__ccgo_pad2 [4]byte } 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]int8 } 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 { 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 { 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 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]int8 } 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 int8 FisSpecial int8 Fn int32 FiDP int32 Fz uintptr FzBuf [24]int8 } 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 + 42, 3: __ccgo_ts + 61, 4: __ccgo_ts + 86, 5: __ccgo_ts + 108, 6: __ccgo_ts + 138, 7: __ccgo_ts + 158, 8: __ccgo_ts + 178, 9: __ccgo_ts + 201, 10: __ccgo_ts + 226, 11: __ccgo_ts + 253, 12: __ccgo_ts + 278, 13: __ccgo_ts + 300, 14: __ccgo_ts + 332, 15: __ccgo_ts + 358, 16: __ccgo_ts + 383, 17: __ccgo_ts + 404, 18: __ccgo_ts + 427, 19: __ccgo_ts + 446, 20: __ccgo_ts + 458, 21: __ccgo_ts + 473, 22: __ccgo_ts + 495, 23: __ccgo_ts + 520, 24: __ccgo_ts + 543, 25: __ccgo_ts + 565, 26: __ccgo_ts + 576, 27: __ccgo_ts + 589, 28: __ccgo_ts + 604, 29: __ccgo_ts + 620, 30: __ccgo_ts + 633, 31: __ccgo_ts + 654, 32: __ccgo_ts + 678, 33: __ccgo_ts + 701, 34: __ccgo_ts + 717, 35: __ccgo_ts + 733, 36: __ccgo_ts + 757, 37: __ccgo_ts + 784, 38: __ccgo_ts + 804, 39: __ccgo_ts + 825, 40: __ccgo_ts + 847, 41: __ccgo_ts + 877, 42: __ccgo_ts + 902, 43: __ccgo_ts + 928, 44: __ccgo_ts + 948, 45: __ccgo_ts + 974, 46: __ccgo_ts + 997, 47: __ccgo_ts + 1023, 48: __ccgo_ts + 1045, 49: __ccgo_ts + 1066, 50: __ccgo_ts + 1077, 51: __ccgo_ts + 1085, 52: __ccgo_ts + 1099, 53: __ccgo_ts + 1112, } func _sqlite3CompileOptions(tls *libc.TLS, pnOpt uintptr) (r uintptr) { *(*int32)(unsafe.Pointer(pnOpt)) = int32(libc.Uint64FromInt64(432) / libc.Uint64FromInt64(8)) 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(0x81), 9: uint8(0x01), 10: uint8(0x01), 11: uint8(0x81), 12: uint8(0x83), 13: uint8(0x83), 14: uint8(0x01), 15: uint8(0x01), 16: uint8(0x03), 17: uint8(0x03), 18: uint8(0x01), 19: uint8(0x12), 20: uint8(0x01), 21: uint8(0xc9), 22: uint8(0xc9), 23: uint8(0xc9), 24: uint8(0xc9), 25: uint8(0x01), 26: uint8(0x49), 27: uint8(0x49), 28: uint8(0x49), 29: uint8(0x49), 30: uint8(0xc9), 31: uint8(0x49), 32: uint8(0xc1), 33: uint8(0x01), 34: uint8(0x41), 35: uint8(0x41), 36: uint8(0xc1), 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(0x81), 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]int8{'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]int8{ 0: int8(SQLITE_AFF_NUMERIC), 1: int8(SQLITE_AFF_BLOB), 2: int8(SQLITE_AFF_INTEGER), 3: int8(SQLITE_AFF_INTEGER), 4: int8(SQLITE_AFF_REAL), 5: int8(SQLITE_AFF_TEXT), } var _sqlite3StdType = [6]uintptr{ 0: __ccgo_ts + 1125, 1: __ccgo_ts + 1129, 2: __ccgo_ts + 1134, 3: __ccgo_ts + 1138, 4: __ccgo_ts + 1146, 5: __ccgo_ts + 1151, } /************** 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 { FmnPmaSize int32 FmxPmaSize int32 FmxKeysize int32 Fpgsz int32 FpReader uintptr FpMerger uintptr Fdb uintptr FpKeyInfo uintptr FpUnpacked uintptr Flist TSorterList FiMemory int32 FnMemory int32 FbUsePMA Tu8 FbUseThreads Tu8 FiPrev Tu8 FnTask Tu8 FtypeMask Tu8 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 { FpCValue uintptr FiOffset Ti64 FiCol int32 FcacheStatus Tu32 FcolCacheCtr Tu32 } 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 { 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 { 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 { FpCValue uintptr FiOffset Ti64 FiCol int32 FcacheStatus Tu32 FcolCacheCtr Tu32 } 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 { Fv uintptr FpParent uintptr FaOp uintptr FaMem uintptr FapCsr uintptr FaOnce uintptr Ftoken uintptr FlastRowid Ti64 FpAuxData uintptr FnCursor int32 Fpc int32 FnOp int32 FnMem int32 FnChildMem int32 FnChildCsr int32 FnChange Ti64 FnDbChange Ti64 } type VdbeFrame = TVdbeFrame type TVdbeFrame1 = struct { Fv uintptr FpParent uintptr FaOp uintptr FaMem uintptr FapCsr uintptr FaOnce uintptr Ftoken uintptr FlastRowid Ti64 FpAuxData uintptr FnCursor int32 Fpc int32 FnOp int32 FnMem int32 FnChildMem int32 FnChildCsr int32 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]int8 } type DblquoteStr = TDblquoteStr type TDblquoteStr1 = struct { FpNextStr uintptr Fz [8]int8 } 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 { Fdb uintptr FppVPrev uintptr FpVNext uintptr FpParse uintptr FnVar TynVar FnMem int32 FnCursor int32 FcacheCtr Tu32 Fpc int32 Frc int32 FnChange Ti64 FiStatement int32 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 FstartTime Ti64 FnResColumn Tu16 FnResAlloc Tu16 FerrorAction Tu8 FminWriteFileFormat Tu8 FprepFlags Tu8 FeVdbeState Tu8 F__ccgo200 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 { 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 } 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 = int64 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]int8{ 1: int8(1), 2: int8(1), 7: int8(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) { return *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*8)) } // 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) { *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*8)) += int64(N) if *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*8)) > *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 80 + uintptr(op)*8)) { *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 80 + uintptr(op)*8)) = *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*8)) } } func _sqlite3StatusDown(tls *libc.TLS, op int32, N int32) { *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*8)) -= int64(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 newValue = int64(X) if newValue > *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 80 + uintptr(op)*8)) { *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 80 + uintptr(op)*8)) = 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.Uint64FromInt64(80)/libc.Uint64FromInt64(8)) { return _sqlite3MisuseError(tls, int32(23961)) } if _statMutex[op] != 0 { v1 = _sqlite3Pcache1Mutex(tls) } else { v1 = _sqlite3MallocMutex(tls) } pMutex = v1 Xsqlite3_mutex_enter(tls, pMutex) *(*Tsqlite3_int64)(unsafe.Pointer(pCurrent)) = *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*8)) *(*Tsqlite3_int64)(unsafe.Pointer(pHighwater)) = *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 80 + uintptr(op)*8)) if resetFlag != 0 { *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + 80 + uintptr(op)*8)) = *(*Tsqlite3StatValueType)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Stat)) + uintptr(op)*8)) } 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): *(*int32)(unsafe.Pointer(pCurrent)) = 0 *(*int32)(unsafe.Pointer(pHighwater)) = int32(*(*Tu32)(unsafe.Pointer(db + 440 + 16 + uintptr(op-int32(SQLITE_DBSTATUS_LOOKASIDE_HIT))*4))) if resetFlag != 0 { *(*Tu32)(unsafe.Pointer(db + 440 + 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)*32))).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 (*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)*32))).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(32)))*((*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 + 56)).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 (*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) i2 = 0 for { if !(i2 < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i2)*32))).FpBt != 0 { pPager1 = _sqlite3BtreePager(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i2)*32))).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 Tclock_t = uint64 type clock_t = Tclock_t type Ttime_t = int64 type time_t = Ttime_t type Ttimespec = struct { Ftv_sec t__darwin_time_t Ftv_nsec int64 } type timespec = Ttimespec 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 int64 Ftm_zone uintptr } type tm = Ttm type Tclockid_t = int32 type clockid_t = Tclockid_t const __CLOCK_REALTIME = 0 const __CLOCK_MONOTONIC = 6 const __CLOCK_MONOTONIC_RAW = 4 const __CLOCK_MONOTONIC_RAW_APPROX = 5 const __CLOCK_UPTIME_RAW = 8 const __CLOCK_UPTIME_RAW_APPROX = 9 const __CLOCK_PROCESS_CPUTIME_ID = 12 const __CLOCK_THREAD_CPUTIME_ID = 16 /* ** 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 { FiJD Tsqlite3_int64 FY int32 FM int32 FD int32 Fh int32 Fm int32 Ftz int32 Fs float64 FvalidJD int8 FvalidYMD int8 FvalidHMS int8 FnFloor int8 F__ccgo44 uint8 } type DateTime = TDateTime type TDateTime1 = struct { FiJD Tsqlite3_int64 FY int32 FM int32 FD int32 Fh int32 Fm int32 Ftz int32 Fs float64 FvalidJD int8 FvalidYMD int8 FvalidHMS int8 FnFloor int8 F__ccgo44 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 int8 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 = int8(int32(*(*int8)(unsafe.Pointer(zFormat))) - int32('0')) min = int8(int32(*(*int8)(unsafe.Pointer(zFormat + 1))) - int32('0')) val = 0 max = _aMx[int32(*(*int8)(unsafe.Pointer(zFormat + 2)))-int32('a')] nextC = *(*int8)(unsafe.Pointer(zFormat + 3)) val = 0 for { v1 = N N-- if !(v1 != 0) { break } if !(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zDate)))])&libc.Int32FromInt32(0x04) != 0) { goto end_getDigits } val = val*int32(10) + int32(*(*int8)(unsafe.Pointer(zDate))) - int32('0') zDate++ } if val < int32(min) || val > int32(max) || int32(nextC) != 0 && int32(nextC) != int32(*(*int8)(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(*(*int8)(unsafe.Pointer(zDate)))])&int32(0x01) != 0 { zDate++ } (*TDateTime)(unsafe.Pointer(p)).Ftz = 0 c = int32(*(*int8)(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++ libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 4, 0x10) libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 3, 0x8) goto zulu_time } else { return libc.BoolInt32(c != 0) } } } zDate++ if _getDigits(tls, zDate, __ccgo_ts+1156, 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(*(*int8)(unsafe.Pointer(zDate)))])&int32(0x01) != 0 { zDate++ } return libc.BoolInt32(int32(*(*int8)(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 _ /* h at bp+0 */ int32 var _ /* m at bp+4 */ int32 var _ /* s at bp+8 */ int32 _, _ = ms, rScale ms = float64(0) if _getDigits(tls, zDate, __ccgo_ts+1164, libc.VaList(bp+24, bp, bp+4)) != int32(2) { return int32(1) } zDate += uintptr(5) if int32(*(*int8)(unsafe.Pointer(zDate))) == int32(':') { zDate++ if _getDigits(tls, zDate, __ccgo_ts+1172, libc.VaList(bp+24, bp+8)) != int32(1) { return int32(1) } zDate += uintptr(2) if int32(*(*int8)(unsafe.Pointer(zDate))) == int32('.') && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zDate + 1)))])&int32(0x04) != 0 { rScale = float64(1) zDate++ for int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zDate)))])&int32(0x04) != 0 { ms = ms*float64(10) + float64(*(*int8)(unsafe.Pointer(zDate))) - libc.Float64FromUint8('0') rScale *= float64(10) zDate++ } ms /= rScale } } else { *(*int32)(unsafe.Pointer(bp + 8)) = 0 } (*TDateTime)(unsafe.Pointer(p)).FvalidJD = 0 libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1) (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = int8(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) } return 0 } // C documentation // // /* // ** Put the DateTime object into its error state. // */ func _datetimeError(tls *libc.TLS, p uintptr) { libc.X__builtin___memset_chk(tls, p, 0, uint64(48), libc.X__builtin_object_size(tls, p, 0)) libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 1, 0x2) } // 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) || int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x1>>0)) != 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 = int8(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)).Ftz != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(p)) -= int64((*TDateTime)(unsafe.Pointer(p)).Ftz * int32(60000)) (*TDateTime)(unsafe.Pointer(p)).FvalidYMD = 0 (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = 0 (*TDateTime)(unsafe.Pointer(p)).Ftz = 0 libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 3, 0x8) libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 4, 0x10) } } } // C documentation // // /* // ** Given the YYYY-MM-DD information current in p, determine if there // ** is day-of-month overflow and set nFloor to the number of days that // ** would need to be subtracted from the date in order to bring the // ** date back to the end of the month. // */ func _computeFloor(tls *libc.TLS, p uintptr) { if (*TDateTime)(unsafe.Pointer(p)).FD <= int32(28) { (*TDateTime)(unsafe.Pointer(p)).FnFloor = 0 } else { if int32(1)<<(*TDateTime)(unsafe.Pointer(p)).FM&int32(0x15aa) != 0 { (*TDateTime)(unsafe.Pointer(p)).FnFloor = 0 } else { if (*TDateTime)(unsafe.Pointer(p)).FM != int32(2) { (*TDateTime)(unsafe.Pointer(p)).FnFloor = libc.BoolInt8((*TDateTime)(unsafe.Pointer(p)).FD == libc.Int32FromInt32(31)) } else { if (*TDateTime)(unsafe.Pointer(p)).FY%int32(4) != 0 || (*TDateTime)(unsafe.Pointer(p)).FY%int32(100) == 0 && (*TDateTime)(unsafe.Pointer(p)).FY%int32(400) != 0 { (*TDateTime)(unsafe.Pointer(p)).FnFloor = int8((*TDateTime)(unsafe.Pointer(p)).FD - int32(28)) } else { (*TDateTime)(unsafe.Pointer(p)).FnFloor = int8((*TDateTime)(unsafe.Pointer(p)).FD - int32(29)) } } } } } // 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(*(*int8)(unsafe.Pointer(zDate))) == int32('-') { zDate++ neg = int32(1) } else { neg = 0 } if _getDigits(tls, zDate, __ccgo_ts+1176, libc.VaList(bp+24, bp, bp+4, bp+8)) != int32(3) { return int32(1) } zDate += uintptr(10) for int32(_sqlite3CtypeMap[uint8(*(*int8)(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(*(*int8)(unsafe.Pointer(zDate))) == 0 { (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = 0 } else { return int32(1) } } (*TDateTime)(unsafe.Pointer(p)).FvalidJD = 0 (*TDateTime)(unsafe.Pointer(p)).FvalidYMD = int8(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)) _computeFloor(tls, p) if (*TDateTime)(unsafe.Pointer(p)).Ftz != 0 { _computeJD(tls, p) } return 0 } /* Forward declaration */ // 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 = int8(1) libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 3, 0x8) libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 4, 0x10) _clearYMD_HMS_TZ(tls, p) 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 libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 0, 0x1) 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 = int8(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+1188) == 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+1192) == 0 || _sqlite3StrICmp(tls, zDate, __ccgo_ts+1199) == 0) && _sqlite3NotPureFunc(tls, context) != 0 { libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 2, 0x4) 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 = int8(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) libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1) (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = int8(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 = 0 (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = 0 (*TDateTime)(unsafe.Pointer(p)).Ftz = 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(112) defer tls.Free(112) var iYearDiff int32 var _ /* sLocal at bp+8 */ Ttm var _ /* t at bp+0 */ Ttime_t var _ /* x at bp+64 */ TDateTime _ = iYearDiff /* Initialize the contents of sLocal to avoid a compiler warning. */ libc.X__builtin___memset_chk(tls, bp+8, 0, uint64(56), libc.X__builtin_object_size(tls, bp+8, 0)) _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 + 64)) = *(*TDateTime)(unsafe.Pointer(p)) _computeYMD_HMS(tls, bp+64) iYearDiff = int32(2000) + (*(*TDateTime)(unsafe.Pointer(bp + 64))).FY%int32(4) - (*(*TDateTime)(unsafe.Pointer(bp + 64))).FY (*(*TDateTime)(unsafe.Pointer(bp + 64))).FY += iYearDiff (*(*TDateTime)(unsafe.Pointer(bp + 64))).FvalidJD = 0 _computeJD(tls, bp+64) *(*Ttime_t)(unsafe.Pointer(bp)) = (*(*TDateTime)(unsafe.Pointer(bp + 64))).FiJD/libc.Int64FromInt32(1000) - libc.Int64FromInt32(21086676)*libc.Int64FromInt32(10000) } else { iYearDiff = 0 *(*Ttime_t)(unsafe.Pointer(bp)) = (*TDateTime)(unsafe.Pointer(p)).FiJD/libc.Int64FromInt32(1000) - libc.Int64FromInt32(21086676)*libc.Int64FromInt32(10000) } if _osLocaltime(tls, bp, bp+8) != 0 { Xsqlite3_result_error(tls, pCtx, __ccgo_ts+1209, -int32(1)) return int32(SQLITE_ERROR) } (*TDateTime)(unsafe.Pointer(p)).FY = (*(*Ttm)(unsafe.Pointer(bp + 8))).Ftm_year + int32(1900) - iYearDiff (*TDateTime)(unsafe.Pointer(p)).FM = (*(*Ttm)(unsafe.Pointer(bp + 8))).Ftm_mon + int32(1) (*TDateTime)(unsafe.Pointer(p)).FD = (*(*Ttm)(unsafe.Pointer(bp + 8))).Ftm_mday (*TDateTime)(unsafe.Pointer(p)).Fh = (*(*Ttm)(unsafe.Pointer(bp + 8))).Ftm_hour (*TDateTime)(unsafe.Pointer(p)).Fm = (*(*Ttm)(unsafe.Pointer(bp + 8))).Ftm_min (*TDateTime)(unsafe.Pointer(p)).Fs = float64((*(*Ttm)(unsafe.Pointer(bp + 8))).Ftm_sec) + float64((*TDateTime)(unsafe.Pointer(p)).FiJD%libc.Int64FromInt32(1000))*float64(0.001) (*TDateTime)(unsafe.Pointer(p)).FvalidYMD = int8(1) (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = int8(1) (*TDateTime)(unsafe.Pointer(p)).FvalidJD = 0 libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1) (*TDateTime)(unsafe.Pointer(p)).Ftz = 0 libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 1, 0x2) 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]int8 FrLimit float32 FrXform float32 }{ 0: { FnName: uint8(6), FzName: [7]int8{'s', 'e', 'c', 'o', 'n', 'd'}, FrLimit: float32(4.6427e+14), FrXform: float32(1), }, 1: { FnName: uint8(6), FzName: [7]int8{'m', 'i', 'n', 'u', 't', 'e'}, FrLimit: float32(7.7379e+12), FrXform: float32(60), }, 2: { FnName: uint8(4), FzName: [7]int8{'h', 'o', 'u', 'r'}, FrLimit: float32(1.2897e+11), FrXform: float32(3600), }, 3: { FnName: uint8(3), FzName: [7]int8{'d', 'a', 'y'}, FrLimit: float32(5.373485e+06), FrXform: float32(86400), }, 4: { FnName: uint8(5), FzName: [7]int8{'m', 'o', 'n', 't', 'h'}, FrLimit: float32(176546), FrXform: float32(libc.Float64FromFloat64(30) * libc.Float64FromFloat64(86400)), }, 5: { FnName: uint8(4), FzName: [7]int8{'y', 'e', 'a', 'r'}, FrLimit: float32(14713), FrXform: float32(libc.Float64FromFloat64(365) * libc.Float64FromFloat64(86400)), }, } // 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 !(int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x1>>0)) != 0) || (*TDateTime)(unsafe.Pointer(p)).FvalidJD != 0 { libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1) } 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 = int8(1) libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1) } } } // 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 // ** +/-YYYY-MM-DD HH:MM:SS.SSS // ** ceiling // ** floor // ** start of month // ** start of year // ** start of week // ** start of day // ** weekday N // ** unixepoch // ** auto // ** localtime // ** utc // ** subsec // ** subsecond // ** // ** 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, v1, v10, v13, v3, v6, v8 int32 var iErr, iGuess, iOrigJD, v2 Ti64 var rRounder, v11 float64 var z0 int8 var z2 uintptr var v4, v7 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, v11, v13, v2, v3, v4, v6, v7, v8 rc = int32(1) switch int32(_sqlite3UpperToLower[uint8(*(*int8)(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+1232) == 0 { if idx > int32(1) { return int32(1) } /* IMP: R-33611-57934 */ _autoAdjustDate(tls, p) rc = 0 } case int32('c'): /* ** ceiling ** ** Resolve day-of-month overflow by rolling forward into the next ** month. As this is the default action, this modifier is really ** a no-op that is only included for symmetry. See "floor". */ if Xsqlite3_stricmp(tls, z, __ccgo_ts+1237) == 0 { _computeJD(tls, p) _clearYMD_HMS_TZ(tls, p) rc = 0 (*TDateTime)(unsafe.Pointer(p)).FnFloor = 0 } case int32('f'): /* ** floor ** ** Resolve day-of-month overflow by rolling back to the end of the ** previous month. */ if Xsqlite3_stricmp(tls, z, __ccgo_ts+1245) == 0 { _computeJD(tls, p) *(*Tsqlite3_int64)(unsafe.Pointer(p)) -= int64(int32((*TDateTime)(unsafe.Pointer(p)).FnFloor) * int32(86400000)) _clearYMD_HMS_TZ(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+1251) == 0 { if idx > int32(1) { return int32(1) } /* IMP: R-31176-64601 */ if (*TDateTime)(unsafe.Pointer(p)).FvalidJD != 0 && int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x1>>0)) != 0 { rc = 0 libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1) } } 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+1261) == 0 && _sqlite3NotPureFunc(tls, pCtx) != 0 { if int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x10>>4)) != 0 { v1 = SQLITE_OK } else { v1 = _toLocaltime(tls, p, pCtx) } rc = v1 libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 3, 0x8) libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 4, 0x10) } 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+1271) == 0 && int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x1>>0)) != 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 = int8(1) libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1) rc = 0 } } else { if Xsqlite3_stricmp(tls, z, __ccgo_ts+1281) == 0 && _sqlite3NotPureFunc(tls, pCtx) != 0 { if int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x8>>3)) == 0 { /* Guess at the corresponding utc time */ cnt = 0 /* Guess is off by this much */ _computeJD(tls, p) v2 = (*TDateTime)(unsafe.Pointer(p)).FiJD iOrigJD = v2 iGuess = v2 iErr = 0 for { libc.X__builtin___memset_chk(tls, bp+8, 0, uint64(48), libc.X__builtin_object_size(tls, bp+8, 0)) iGuess -= iErr (*(*TDateTime)(unsafe.Pointer(bp + 8))).FiJD = iGuess (*(*TDateTime)(unsafe.Pointer(bp + 8))).FvalidJD = int8(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 _5 _5: ; if v4 = iErr != 0; v4 { v3 = cnt cnt++ } if !(v4 && v3 < int32(3)) { break } } libc.X__builtin___memset_chk(tls, p, 0, uint64(48), libc.X__builtin_object_size(tls, p, 0)) (*TDateTime)(unsafe.Pointer(p)).FiJD = iGuess (*TDateTime)(unsafe.Pointer(p)).FvalidJD = int8(1) libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 3, 0x8) libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 4, 0x10) } 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 v7 = Xsqlite3_strnicmp(tls, z, __ccgo_ts+1285, 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); v7 { v6 = int32(*(*float64)(unsafe.Pointer(bp))) n = v6 } if v7 && float64(v6) == *(*float64)(unsafe.Pointer(bp)) { _computeYMD_HMS(tls, p) (*TDateTime)(unsafe.Pointer(p)).Ftz = 0 (*TDateTime)(unsafe.Pointer(p)).FvalidJD = 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+1294, int32(9)) != 0 { if Xsqlite3_stricmp(tls, z, __ccgo_ts+1192) == 0 || Xsqlite3_stricmp(tls, z, __ccgo_ts+1199) == 0 { libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(1), 2, 0x4) 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 = int8(1) v8 = libc.Int32FromInt32(0) (*TDateTime)(unsafe.Pointer(p)).Fm = v8 (*TDateTime)(unsafe.Pointer(p)).Fh = v8 (*TDateTime)(unsafe.Pointer(p)).Fs = float64(0) libc.SetBitFieldPtr8Uint32(p+44, libc.Uint32FromInt32(0), 0, 0x1) (*TDateTime)(unsafe.Pointer(p)).Ftz = 0 (*TDateTime)(unsafe.Pointer(p)).FvalidJD = 0 if Xsqlite3_stricmp(tls, z, __ccgo_ts+1304) == 0 { (*TDateTime)(unsafe.Pointer(p)).FD = int32(1) rc = 0 } else { if Xsqlite3_stricmp(tls, z, __ccgo_ts+1310) == 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+1315) == 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 = *(*int8)(unsafe.Pointer(z)) n = int32(1) for { if !(*(*int8)(unsafe.Pointer(z + uintptr(n))) != 0) { break } if int32(*(*int8)(unsafe.Pointer(z + uintptr(n)))) == int32(':') { break } if int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(z + uintptr(n))))])&int32(0x01) != 0 { break } if int32(*(*int8)(unsafe.Pointer(z + uintptr(n)))) == int32('-') { if n == int32(5) && _getDigits(tls, z+1, __ccgo_ts+1319, libc.VaList(bp+136, bp+56)) == int32(1) { break } if n == int32(6) && _getDigits(tls, z+1, __ccgo_ts+1323, libc.VaList(bp+136, bp+56)) == int32(1) { break } } goto _9 _9: ; n++ } if _sqlite3AtoF(tls, z, bp, n, uint8(SQLITE_UTF8)) <= 0 { break } if int32(*(*int8)(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+1327, libc.VaList(bp+136, bp+56, bp+60, bp+64)) != int32(3) { break } } else { if _getDigits(tls, z+1, __ccgo_ts+1339, 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 = 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 { v10 = ((*TDateTime)(unsafe.Pointer(p)).FM - int32(1)) / int32(12) } else { v10 = ((*TDateTime)(unsafe.Pointer(p)).FM - int32(12)) / int32(12) } x = v10 *(*int32)(unsafe.Pointer(p + 8)) += x *(*int32)(unsafe.Pointer(p + 12)) -= x * int32(12) _computeFloor(tls, p) _computeJD(tls, p) (*TDateTime)(unsafe.Pointer(p)).FvalidHMS = 0 (*TDateTime)(unsafe.Pointer(p)).FvalidYMD = 0 *(*Tsqlite3_int64)(unsafe.Pointer(p)) += int64(*(*int32)(unsafe.Pointer(bp + 64))) * int64(86400000) if int32(*(*int8)(unsafe.Pointer(z + 11))) == 0 { rc = 0 break } if int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(z + 11)))])&int32(0x01) != 0 && _getDigits(tls, z+12, __ccgo_ts+1164, libc.VaList(bp+136, bp+68, bp+72)) == int32(2) { z2 = z + 12 n = int32(2) } else { break } } if int32(*(*int8)(unsafe.Pointer(z2 + uintptr(n)))) == int32(':') { if !(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(z2)))])&libc.Int32FromInt32(0x04) != 0) { z2++ } libc.X__builtin___memset_chk(tls, bp+80, 0, uint64(48), libc.X__builtin_object_size(tls, bp+80, 0)) 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(*(*int8)(unsafe.Pointer(z)))])&int32(0x01) != 0 { z++ } n = _sqlite3Strlen30(tls, z) if n < int32(3) || n > int32(10) { break } if int32(_sqlite3UpperToLower[uint8(*(*int8)(unsafe.Pointer(z + uintptr(n-int32(1)))))]) == int32('s') { n-- } _computeJD(tls, p) if *(*float64)(unsafe.Pointer(bp)) < libc.Float64FromInt32(0) { v11 = -libc.Float64FromFloat64(0.5) } else { v11 = +libc.Float64FromFloat64(0.5) } rRounder = v11 (*TDateTime)(unsafe.Pointer(p)).FnFloor = 0 i = 0 for { if !(i < int32(libc.Uint64FromInt64(96)/libc.Uint64FromInt64(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 */ _computeYMD_HMS(tls, p) *(*int32)(unsafe.Pointer(p + 12)) += int32(*(*float64)(unsafe.Pointer(bp))) if (*TDateTime)(unsafe.Pointer(p)).FM > 0 { v13 = ((*TDateTime)(unsafe.Pointer(p)).FM - int32(1)) / int32(12) } else { v13 = ((*TDateTime)(unsafe.Pointer(p)).FM - int32(12)) / int32(12) } x = v13 *(*int32)(unsafe.Pointer(p + 8)) += x *(*int32)(unsafe.Pointer(p + 12)) -= x * int32(12) _computeFloor(tls, p) (*TDateTime)(unsafe.Pointer(p)).FvalidJD = 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))) _computeYMD_HMS(tls, p) *(*int32)(unsafe.Pointer(p + 8)) += y _computeFloor(tls, p) (*TDateTime)(unsafe.Pointer(p)).FvalidJD = 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 _12 _12: ; 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.X__builtin___memset_chk(tls, p, 0, uint64(48), libc.X__builtin_object_size(tls, p, 0)) 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)*8))) n = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*8))) if z == uintptr(0) || _parseModifier(tls, context, z, n, p, i) != 0 { return int32(1) } goto _2 _2: ; i++ } _computeJD(tls, p) if int32(uint32(*(*uint8)(unsafe.Pointer(p + 44))&0x2>>1)) != 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 */ (*TDateTime)(unsafe.Pointer(p)).FvalidYMD = 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 int32(uint32(*(*uint8)(unsafe.Pointer(bp + 44))&0x4>>2)) != 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]int8 _, _, _ = 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]int8)(unsafe.Pointer(bp + 48)))[int32(1)] = int8(int32('0') + Y/int32(1000)%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(2)] = int8(int32('0') + Y/int32(100)%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(3)] = int8(int32('0') + Y/int32(10)%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(4)] = int8(int32('0') + Y%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(5)] = int8('-') (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(6)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FM/int32(10)%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(7)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FM%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(8)] = int8('-') (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(9)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FD/int32(10)%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(10)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FD%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(11)] = int8(' ') (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(12)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fh/int32(10)%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(13)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fh%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(14)] = int8(':') (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(15)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fm/int32(10)%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(16)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fm%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(17)] = int8(':') if int32(uint32(*(*uint8)(unsafe.Pointer(bp + 44))&0x4>>2)) != 0 { s = int32(libc.Float64FromFloat64(1000)*(*(*TDateTime)(unsafe.Pointer(bp))).Fs + libc.Float64FromFloat64(0.5)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(18)] = int8(int32('0') + s/int32(10000)%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(19)] = int8(int32('0') + s/int32(1000)%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(20)] = int8('.') (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(21)] = int8(int32('0') + s/int32(100)%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(22)] = int8(int32('0') + s/int32(10)%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(23)] = int8(int32('0') + s%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(24)] = 0 n = int32(24) } else { s = int32((*(*TDateTime)(unsafe.Pointer(bp))).Fs) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(18)] = int8(int32('0') + s/int32(10)%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(19)] = int8(int32('0') + s%int32(10)) (*(*[32]int8)(unsafe.Pointer(bp + 48)))[int32(20)] = 0 n = int32(20) } if (*(*TDateTime)(unsafe.Pointer(bp))).FY < 0 { (*(*[32]int8)(unsafe.Pointer(bp + 48)))[0] = int8('-') 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]int8 _, _ = n, s if _isDate(tls, context, argc, argv, bp) == 0 { _computeHMS(tls, bp) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[0] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fh/int32(10)%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(1)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fh%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(2)] = int8(':') (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(3)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fm/int32(10)%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(4)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).Fm%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(5)] = int8(':') if int32(uint32(*(*uint8)(unsafe.Pointer(bp + 44))&0x4>>2)) != 0 { s = int32(libc.Float64FromFloat64(1000)*(*(*TDateTime)(unsafe.Pointer(bp))).Fs + libc.Float64FromFloat64(0.5)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(6)] = int8(int32('0') + s/int32(10000)%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(7)] = int8(int32('0') + s/int32(1000)%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(8)] = int8('.') (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(9)] = int8(int32('0') + s/int32(100)%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(10)] = int8(int32('0') + s/int32(10)%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(11)] = int8(int32('0') + s%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(12)] = 0 n = int32(12) } else { s = int32((*(*TDateTime)(unsafe.Pointer(bp))).Fs) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(6)] = int8(int32('0') + s/int32(10)%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(7)] = int8(int32('0') + s%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(8)] = 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]int8 _ = Y if _isDate(tls, context, argc, argv, bp) == 0 { _computeYMD(tls, bp) Y = (*(*TDateTime)(unsafe.Pointer(bp))).FY if Y < 0 { Y = -Y } (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(1)] = int8(int32('0') + Y/int32(1000)%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(2)] = int8(int32('0') + Y/int32(100)%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(3)] = int8(int32('0') + Y/int32(10)%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(4)] = int8(int32('0') + Y%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(5)] = int8('-') (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(6)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FM/int32(10)%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(7)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FM%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(8)] = int8('-') (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(9)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FD/int32(10)%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(10)] = int8(int32('0') + (*(*TDateTime)(unsafe.Pointer(bp))).FD%int32(10)) (*(*[16]int8)(unsafe.Pointer(bp + 48)))[int32(11)] = 0 if (*(*TDateTime)(unsafe.Pointer(bp))).FY < 0 { (*(*[16]int8)(unsafe.Pointer(bp + 48)))[0] = int8('-') 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 // // /* // ** Compute the number of days after the most recent January 1. // ** // ** In other words, compute the zero-based day number for the // ** current year: // ** // ** Jan01 = 0, Jan02 = 1, ..., Jan31 = 30, Feb01 = 31, ... // ** Dec31 = 364 or 365. // */ func _daysAfterJan01(tls *libc.TLS, pDate uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* jan01 at bp+0 */ TDateTime *(*TDateTime)(unsafe.Pointer(bp)) = *(*TDateTime)(unsafe.Pointer(pDate)) (*(*TDateTime)(unsafe.Pointer(bp))).FvalidJD = 0 (*(*TDateTime)(unsafe.Pointer(bp))).FM = int32(1) (*(*TDateTime)(unsafe.Pointer(bp))).FD = int32(1) _computeJD(tls, bp) return int32(((*TDateTime)(unsafe.Pointer(pDate)).FiJD - (*(*TDateTime)(unsafe.Pointer(bp))).FiJD + libc.Int64FromInt32(43200000)) / libc.Int64FromInt32(86400000)) } // C documentation // // /* // ** Return the number of days after the most recent Monday. // ** // ** In other words, return the day of the week according // ** to this code: // ** // ** 0=Monday, 1=Tuesday, 2=Wednesday, ..., 6=Sunday. // */ func _daysAfterMonday(tls *libc.TLS, pDate uintptr) (r int32) { return int32(((*TDateTime)(unsafe.Pointer(pDate)).FiJD+libc.Int64FromInt32(43200000))/libc.Int64FromInt32(86400000)) % int32(7) } // C documentation // // /* // ** Return the number of days after the most recent Sunday. // ** // ** In other words, return the day of the week according // ** to this code: // ** // ** 0=Sunday, 1=Monday, 2=Tues, ..., 6=Saturday // */ func _daysAfterSunday(tls *libc.TLS, pDate uintptr) (r int32) { return int32(((*TDateTime)(unsafe.Pointer(pDate)).FiJD+libc.Int64FromInt32(129600000))/libc.Int64FromInt32(86400000)) % int32(7) } // C documentation // // /* // ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...) // ** // ** Return a string described by FORMAT. Conversions as follows: // ** // ** %d day of month 01-31 // ** %e day of month 1-31 // ** %f ** fractional seconds SS.SSS // ** %F ISO date. YYYY-MM-DD // ** %G ISO year corresponding to %V 0000-9999. // ** %g 2-digit ISO year corresponding to %V 00-99 // ** %H hour 00-24 // ** %k hour 0-24 (leading zero converted to space) // ** %I hour 01-12 // ** %j day of year 001-366 // ** %J ** julian day number // ** %l hour 1-12 (leading zero converted to space) // ** %m month 01-12 // ** %M minute 00-59 // ** %p "am" or "pm" // ** %P "AM" or "PM" // ** %R time as HH:MM // ** %s seconds since 1970-01-01 // ** %S seconds 00-59 // ** %T time as HH:MM:SS // ** %u day of week 1-7 Monday==1, Sunday==7 // ** %w day of week 0-6 Sunday==0, Monday==1 // ** %U week of year 00-53 (First Sunday is start of week 01) // ** %V week of year 01-53 (First week containing Thursday is week 01) // ** %W week of year 00-53 (First Monday is start of week 01) // ** %Y year 0000-9999 // ** %% % // */ func _strftimeFunc(tls *libc.TLS, context uintptr, argc int32, argv uintptr) { bp := tls.Alloc(208) defer tls.Free(208) var c, cf int8 var db, zFmt, v3, v4, v5, v6, v7 uintptr var h 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+128 */ TDateTime var _ /* y at bp+80 */ TDateTime _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, cf, db, h, i, iS, j, s, 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)*8, bp) != 0 { return } db = Xsqlite3_context_db_handle(tls, context) _sqlite3StrAccumInit(tls, bp+48, uintptr(0), uintptr(0), 0, *(*int32)(unsafe.Pointer(db + 136))) _computeJD(tls, bp) _computeYMD_HMS(tls, bp) v2 = libc.Uint64FromInt32(0) j = v2 i = v2 for { if !(*(*int8)(unsafe.Pointer(zFmt + uintptr(i))) != 0) { break } if int32(*(*int8)(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 + uint64(1) cf = *(*int8)(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 + 1351 } else { v3 = __ccgo_ts + 1356 } Xsqlite3_str_appendf(tls, bp+48, v3, libc.VaList(bp+184, (*(*TDateTime)(unsafe.Pointer(bp))).FD)) case int32('f'): /* Fractional seconds. (Non-standard) */ s = (*(*TDateTime)(unsafe.Pointer(bp))).Fs if s > float64(59.999) { s = float64(59.999) } Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1360, libc.VaList(bp+184, s)) case int32('F'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1367, libc.VaList(bp+184, (*(*TDateTime)(unsafe.Pointer(bp))).FY, (*(*TDateTime)(unsafe.Pointer(bp))).FM, (*(*TDateTime)(unsafe.Pointer(bp))).FD)) case int32('G'): /* Fall thru */ fallthrough case int32('g'): *(*TDateTime)(unsafe.Pointer(bp + 80)) = *(*TDateTime)(unsafe.Pointer(bp)) /* Move y so that it is the Thursday in the same week as x */ (*(*TDateTime)(unsafe.Pointer(bp + 80))).FiJD += int64((int32(3) - _daysAfterMonday(tls, bp)) * int32(86400000)) (*(*TDateTime)(unsafe.Pointer(bp + 80))).FvalidYMD = 0 _computeYMD(tls, bp+80) if int32(cf) == int32('g') { Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1351, libc.VaList(bp+184, (*(*TDateTime)(unsafe.Pointer(bp + 80))).FY%int32(100))) } else { Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1382, libc.VaList(bp+184, (*(*TDateTime)(unsafe.Pointer(bp + 80))).FY)) } case int32('H'): fallthrough case int32('k'): if int32(cf) == int32('H') { v4 = __ccgo_ts + 1351 } else { v4 = __ccgo_ts + 1356 } Xsqlite3_str_appendf(tls, bp+48, v4, libc.VaList(bp+184, (*(*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 + 1351 } else { v5 = __ccgo_ts + 1356 } Xsqlite3_str_appendf(tls, bp+48, v5, libc.VaList(bp+184, h)) case int32('j'): /* Day of year. Jan01==1, Jan02==2, and so forth */ Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1387, libc.VaList(bp+184, _daysAfterJan01(tls, bp)+int32(1))) case int32('J'): /* Julian day number. (Non-standard) */ Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1392, libc.VaList(bp+184, float64((*(*TDateTime)(unsafe.Pointer(bp))).FiJD)/float64(8.64e+07))) case int32('m'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1351, libc.VaList(bp+184, (*(*TDateTime)(unsafe.Pointer(bp))).FM)) case int32('M'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1351, libc.VaList(bp+184, (*(*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 + 1398 } else { v6 = __ccgo_ts + 1401 } Xsqlite3_str_append(tls, bp+48, v6, int32(2)) } else { if int32(cf) == int32('p') { v7 = __ccgo_ts + 1404 } else { v7 = __ccgo_ts + 1407 } Xsqlite3_str_append(tls, bp+48, v7, int32(2)) } case int32('R'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1410, libc.VaList(bp+184, (*(*TDateTime)(unsafe.Pointer(bp))).Fh, (*(*TDateTime)(unsafe.Pointer(bp))).Fm)) case int32('s'): if int32(uint32(*(*uint8)(unsafe.Pointer(bp + 44))&0x4>>2)) != 0 { Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1420, libc.VaList(bp+184, 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+1425, libc.VaList(bp+184, iS)) } case int32('S'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1351, libc.VaList(bp+184, int32((*(*TDateTime)(unsafe.Pointer(bp))).Fs))) case int32('T'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1430, libc.VaList(bp+184, (*(*TDateTime)(unsafe.Pointer(bp))).Fh, (*(*TDateTime)(unsafe.Pointer(bp))).Fm, int32((*(*TDateTime)(unsafe.Pointer(bp))).Fs))) case int32('u'): /* Day of week. 1 to 7. Monday==1, Sunday==7 */ fallthrough case int32('w'): /* Day of week. 0 to 6. Sunday==0, Monday==1 */ c = int8(int32(int8(_daysAfterSunday(tls, bp))) + int32('0')) if int32(c) == int32('0') && int32(cf) == int32('u') { c = int8('7') } Xsqlite3_str_appendchar(tls, bp+48, int32(1), c) case int32('U'): /* Week num. 00-53. First Sun of the year is week 01 */ Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1351, libc.VaList(bp+184, (_daysAfterJan01(tls, bp)-_daysAfterSunday(tls, bp)+int32(7))/int32(7))) case int32('V'): /* Week num. 01-53. First week with a Thur is week 01 */ *(*TDateTime)(unsafe.Pointer(bp + 128)) = *(*TDateTime)(unsafe.Pointer(bp)) /* Adjust y so that is the Thursday in the same week as x */ (*(*TDateTime)(unsafe.Pointer(bp + 128))).FiJD += int64((int32(3) - _daysAfterMonday(tls, bp)) * int32(86400000)) (*(*TDateTime)(unsafe.Pointer(bp + 128))).FvalidYMD = 0 _computeYMD(tls, bp+128) Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1351, libc.VaList(bp+184, _daysAfterJan01(tls, bp+128)/int32(7)+int32(1))) case int32('W'): /* Week num. 00-53. First Mon of the year is week 01 */ Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1351, libc.VaList(bp+184, (_daysAfterJan01(tls, bp)-_daysAfterMonday(tls, bp)+int32(7))/int32(7))) case int32('Y'): Xsqlite3_str_appendf(tls, bp+48, __ccgo_ts+1382, libc.VaList(bp+184, (*(*TDateTime)(unsafe.Pointer(bp))).FY)) case int32('%'): Xsqlite3_str_appendchar(tls, bp+48, int32(1), int8('%')) 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 int8 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*8, 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 = int8('+') 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 = 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 = 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 = 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 = 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)) } _clearYMD_HMS_TZ(tls, bp) _computeYMD_HMS(tls, bp) _sqlite3StrAccumInit(tls, bp+96, uintptr(0), uintptr(0), 0, int32(100)) Xsqlite3_str_appendf(tls, bp+96, __ccgo_ts+1445, libc.VaList(bp+136, 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.Uint64FromInt64(720)/libc.Uint64FromInt64(72))) } 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 + 1251, }, 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 + 1271, }, 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 + 1479, }, 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 + 1484, }, 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 + 1489, }, 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 + 1498, }, 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 + 1507, }, 7: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 1516, }, 8: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 1529, }, 9: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 1547, }, } func init() { p := unsafe.Pointer(&_aDateTimeFuncs) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_juliandayFunc) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_unixepochFunc) *(*uintptr)(unsafe.Add(p, 168)) = __ccgo_fp(_dateFunc) *(*uintptr)(unsafe.Add(p, 240)) = __ccgo_fp(_timeFunc) *(*uintptr)(unsafe.Add(p, 312)) = __ccgo_fp(_datetimeFunc) *(*uintptr)(unsafe.Add(p, 384)) = __ccgo_fp(_strftimeFunc) *(*uintptr)(unsafe.Add(p, 456)) = __ccgo_fp(_timediffFunc) *(*uintptr)(unsafe.Add(p, 528)) = __ccgo_fp(_ctimeFunc) *(*uintptr)(unsafe.Add(p, 600)) = __ccgo_fp(_ctimestampFunc) *(*uintptr)(unsafe.Add(p, 672)) = __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) { 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) { 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. */ 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) return rc } func _sqlite3OsDelete(tls *libc.TLS, pVfs uintptr, zPath uintptr, dirSync int32) (r int32) { var v1 int32 _ = v1 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) { *(*int8)(unsafe.Pointer(zPathOut)) = 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) { /* 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.X__builtin___memset_chk(tls, zBufOut, 0, uint64(nByte), libc.X__builtin_object_size(tls, zBufOut, 0)) if nByte > libc.Int32FromInt64(4) { nByte = int32(4) } libc.X__builtin___memcpy_chk(tls, zBufOut, uintptr(unsafe.Pointer(&_sqlite3Config))+432, uint64(nByte), libc.X__builtin_object_size(tls, zBufOut, 0)) 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) } return rc } func _sqlite3OsCloseFree(tls *libc.TLS, pFile uintptr) { _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 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 } 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 p = libc.Xmalloc(tls, uint64(nByte+int32(8))) if p != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(p)) = int64(nByte) p += 8 } else { Xsqlite3_log(tls, int32(SQLITE_NOMEM), __ccgo_ts+1560, 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 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 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 /* EV: R-46199-30249 */ p -= 8 p = libc.Xrealloc(tls, p, uint64(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+1598, 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, 8)) = __ccgo_fp(_sqlite3MemFree) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_sqlite3MemRealloc) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_sqlite3MemSize) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_sqlite3MemRoundup) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_sqlite3MemInit) *(*uintptr)(unsafe.Add(p, 48)) = __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)) + 96 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 } 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) } 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) } 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 { (*(*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 { (*(*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 { 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 { (*(*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, 8)) = __ccgo_fp(_noopMutexEnd) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_noopMutexAlloc) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_noopMutexFree) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_noopMutexEnter) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_noopMutexTry) *(*uintptr)(unsafe.Add(p, 48)) = __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 { Fmutex uintptr FalarmThreshold Tsqlite3_int64 FhardLimit Tsqlite3_int64 FnearlyFull int32 } 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) *(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(&_mem0)) + 24)) = libc.BoolInt32(n > 0 && n <= nUsed) 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.X__builtin___memset_chk(tls, uintptr(unsafe.Pointer(&_mem0)), 0, uint64(32), libc.X__builtin_object_size(tls, uintptr(unsafe.Pointer(&_mem0)), 0)) } 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 *(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(&_mem0)) + 24)) } // 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.X__builtin___memset_chk(tls, uintptr(unsafe.Pointer(&_mem0)), 0, uint64(32), libc.X__builtin_object_size(tls, uintptr(unsafe.Pointer(&_mem0)), 0)) } // 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 /* 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) { *(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(&_mem0)) + 24)) = int32(1) _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 { *(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(&_mem0)) + 24)) = 0 } } 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)) } } /* 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(uint64(p) >= uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart) && uint64(p) < uint64((*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) { 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) { if db != 0 { if uint64(p) < uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpTrueEnd) { if uint64(p) >= uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle) { return int32(LOOKASIDE_SMALL) } if uint64(p) >= uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart) { 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 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 */ 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 if db != 0 { if uint64(p) < uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd) { if uint64(p) >= uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle) { pBuf = p (*TLookasideSlot)(unsafe.Pointer(pBuf)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree = pBuf return } if uint64(p) >= uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart) { pBuf1 = p (*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 } } Xsqlite3_free(tls, p) } func _sqlite3DbNNFreeNN(tls *libc.TLS, db uintptr, p uintptr) { var pBuf, pBuf1 uintptr _, _ = pBuf, pBuf1 if uint64(p) < uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd) { if uint64(p) >= uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle) { pBuf = p (*TLookasideSlot)(unsafe.Pointer(pBuf)).FpNext = (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpSmallFree = pBuf return } if uint64(p) >= uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpStart) { pBuf1 = p (*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 } Xsqlite3_free(tls, p) } func _sqlite3DbFree(tls *libc.TLS, db uintptr, p uintptr) { 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 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) } } /* 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.X__builtin___memset_chk(tls, p, 0, n, libc.X__builtin_object_size(tls, p, 0)) } 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.X__builtin___memset_chk(tls, p, 0, n, libc.X__builtin_object_size(tls, p, 0)) } 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 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 if n > uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz) { if !((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable != 0) { *(*Tu32)(unsafe.Pointer(db + 440 + 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 + 440 + 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 + 440 + 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 + 440 + 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 + 440 + 16))++ return pBuf } else { *(*Tu32)(unsafe.Pointer(db + 440 + 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) { if p == uintptr(0) { return _sqlite3DbMallocRawNN(tls, db, n) } if uint64(p) < uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpEnd) { if uint64(p) >= uint64((*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FpMiddle) { if n <= uint64(LOOKASIDE_SMALL) { return p } } else { if uint64(p) >= uint64((*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) if int32((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed) == 0 { if _isLookaside(tls, db, p) != 0 { pNew = _sqlite3DbMallocRawNN(tls, db, n) if pNew != 0 { libc.X__builtin___memcpy_chk(tls, pNew, p, uint64(_lookasideMallocSize(tls, db, p)), libc.X__builtin_object_size(tls, pNew, 0)) _sqlite3DbFree(tls, db, p) } } else { 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) + uint64(1) zNew = _sqlite3DbMallocRaw(tls, db, n) if zNew != 0 { libc.X__builtin___memcpy_chk(tls, zNew, z, n, libc.X__builtin_object_size(tls, zNew, 0)) } return zNew } func _sqlite3DbStrNDup(tls *libc.TLS, db uintptr, z uintptr, n Tu64) (r uintptr) { var zNew, v1 uintptr _, _ = zNew, v1 if z != 0 { v1 = _sqlite3DbMallocRawNN(tls, db, n+uint64(1)) } else { v1 = uintptr(0) } zNew = v1 if zNew != 0 { libc.X__builtin___memcpy_chk(tls, zNew, z, n, libc.X__builtin_object_size(tls, zNew, 0)) *(*int8)(unsafe.Pointer(zNew + uintptr(n))) = 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(*(*int8)(unsafe.Pointer(zStart)))])&int32(0x01) != 0 { zStart++ } n = int32(int64(zEnd) - int64(zStart)) for int32(_sqlite3CtypeMap[uint8(*(*int8)(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.AtomicStorePInt32(db+432, libc.Int32FromInt32(1)) } (*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+1634, 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.AtomicStorePInt32(db+432, 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(). */ 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 int8 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]int8{'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]int8{'-', 'x', '0', 0, 'X', '0'} var _fmtinfo = [23]Tet_info{ 0: { Ffmttype: int8('d'), Fbase: uint8(10), Fflags: uint8(1), Ftype1: uint8(etDECIMAL), }, 1: { Ffmttype: int8('s'), Fflags: uint8(4), Ftype1: uint8(etSTRING), }, 2: { Ffmttype: int8('g'), Fflags: uint8(1), Ftype1: uint8(etGENERIC), Fcharset: uint8(30), }, 3: { Ffmttype: int8('z'), Fflags: uint8(4), Ftype1: uint8(etDYNSTRING), }, 4: { Ffmttype: int8('q'), Fflags: uint8(4), Ftype1: uint8(etSQLESCAPE), }, 5: { Ffmttype: int8('Q'), Fflags: uint8(4), Ftype1: uint8(etSQLESCAPE2), }, 6: { Ffmttype: int8('w'), Fflags: uint8(4), Ftype1: uint8(etSQLESCAPE3), }, 7: { Ffmttype: int8('c'), Ftype1: uint8(etCHARX), }, 8: { Ffmttype: int8('o'), Fbase: uint8(8), Fprefix: uint8(2), }, 9: { Ffmttype: int8('u'), Fbase: uint8(10), Ftype1: uint8(etDECIMAL), }, 10: { Ffmttype: int8('x'), Fbase: uint8(16), Fcharset: uint8(16), Fprefix: uint8(1), }, 11: { Ffmttype: int8('X'), Fbase: uint8(16), Fprefix: uint8(4), }, 12: { Ffmttype: int8('f'), Fflags: uint8(1), Ftype1: uint8(etFLOAT), }, 13: { Ffmttype: int8('e'), Fflags: uint8(1), Ftype1: uint8(etEXP), Fcharset: uint8(30), }, 14: { Ffmttype: int8('E'), Fflags: uint8(1), Ftype1: uint8(etEXP), Fcharset: uint8(14), }, 15: { Ffmttype: int8('G'), Fflags: uint8(1), Ftype1: uint8(etGENERIC), Fcharset: uint8(14), }, 16: { Ffmttype: int8('i'), Fbase: uint8(10), Fflags: uint8(1), Ftype1: uint8(etDECIMAL), }, 17: { Ffmttype: int8('n'), Ftype1: uint8(etSIZE), }, 18: { Ffmttype: int8('%'), Ftype1: uint8(etPERCENT), }, 19: { Ffmttype: int8('p'), Fbase: uint8(16), Ftype1: uint8(etPOINTER), Fprefix: uint8(1), }, 20: { Ffmttype: int8('T'), Ftype1: uint8(etTOKEN), }, 21: { Ffmttype: int8('S'), Ftype1: uint8(etSRCITEM), }, 22: { Ffmttype: int8('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) { (*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)*8))) } 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)*8))) } 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)*8))) } // 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(144) defer tls.Free(144) 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 int8 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]int8 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. */ 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(*(*int8)(unsafe.Pointer(fmt))) c = v2 if !(v2 != 0) { break } if c != int32('%') { bufpt = fmt for cond := true; cond; cond = *(*int8)(unsafe.Pointer(fmt)) != 0 && int32(*(*int8)(unsafe.Pointer(fmt))) != int32('%') { fmt++ } Xsqlite3_str_append(tls, pAccum, bufpt, int32(int64(fmt)-int64(bufpt))) if int32(*(*int8)(unsafe.Pointer(fmt))) == 0 { break } } fmt++ v4 = fmt v3 = int32(*(*int8)(unsafe.Pointer(v4))) c = v3 if v3 == 0 { Xsqlite3_str_append(tls, pAccum, __ccgo_ts+1648, 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(*(*int8)(unsafe.Pointer(v14))) if c == int32('l') { fmt++ v15 = fmt c = int32(*(*int8)(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(*(*int8)(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(*(*int8)(unsafe.Pointer(fmt + 1))) c = v19 if v19 != int32('.') && c != int32('l') { fmt++ v20 = fmt c = int32(*(*int8)(unsafe.Pointer(v20))) done = uint8(1) } case int32('.'): fmt++ v21 = fmt c = int32(*(*int8)(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(*(*int8)(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(*(*int8)(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(*(*int8)(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.Uint64FromInt64(138)/libc.Uint64FromInt64(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. */ 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(2) _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 = libc.VaInt64(&ap) } } else { v = int64(libc.VaInt32(&ap)) } } if v < 0 { longvalue = uint64(^v) longvalue++ prefix = int8('-') } else { longvalue = uint64(v) prefix = int8(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 = libc.VaUint64(&ap) } } else { longvalue = uint64(libc.VaUint32(&ap)) } } prefix = 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 *(*int8)(unsafe.Pointer(v46)) = _zOrd[x*int32(2)+int32(1)] bufpt-- v47 = bufpt *(*int8)(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 *(*int8)(unsafe.Pointer(v48)) = *(*int8)(unsafe.Pointer(cset + uintptr(longvalue%uint64(base)))) longvalue = longvalue / uint64(base) } length = int32(t__predefined_ptrdiff_t(zOut+uintptr(nOut-int32(1))) - int64(bufpt)) for precision > length { bufpt-- v49 = bufpt *(*int8)(unsafe.Pointer(v49)) = int8('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 } *(*int8)(unsafe.Pointer(bufpt + uintptr(idx))) = *(*int8)(unsafe.Pointer(bufpt + uintptr(idx+nn))) ix-- if ix == 0 { idx++ v51 = idx *(*int8)(unsafe.Pointer(bufpt + uintptr(v51))) = int8(cThousand) nn-- ix = int32(3) } goto _50 _50: ; idx++ } } if prefix != 0 { bufpt-- v52 = bufpt *(*int8)(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 = *(*int8)(unsafe.Pointer(pre)) x1 = v54 if !(int32(v54) != 0) { break } bufpt-- v55 = bufpt *(*int8)(unsafe.Pointer(v55)) = x1 goto _53 _53: ; pre++ } } length = int32(t__predefined_ptrdiff_t(zOut+uintptr(nOut-int32(1))) - int64(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 + 1650 } else { v57 = __ccgo_ts + 1655 } bufpt = v57 length = _sqlite3Strlen30(tls, bufpt) goto _44 } else { if flag_zeropad != 0 { *(*int8)(unsafe.Pointer((*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fz)) = int8('9') (*(*TFpDecode)(unsafe.Pointer(bp + 72))).FiDP = int32(1000) (*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fn = int32(1) } else { libc.X__builtin___memcpy_chk(tls, bp, __ccgo_ts+1659, uint64(5), libc.X__builtin_object_size(tls, bp, 0)) bufpt = bp if int32((*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fsign) == int32('-') { /* no-op */ } else { if flag_prefix != 0 { (*(*[70]int8)(unsafe.Pointer(bp)))[0] = int8(flag_prefix) } else { bufpt++ } } length = _sqlite3Strlen30(tls, bufpt) goto _44 } } } if int32((*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fsign) == int32('-') { prefix = int8('-') } else { prefix = int8(flag_prefix) } exp = (*(*TFpDecode)(unsafe.Pointer(bp + 72))).FiDP - int32(1) /* ** If the field type is etGENERIC, then convert to either etEXP ** or etFLOAT, as appropriate. */ if int32(xtype) == int32(etGENERIC) { precision-- 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++ *(*int8)(unsafe.Pointer(v61)) = prefix } /* Digits prior to the decimal point */ j = 0 if e2 < 0 { v62 = bufpt bufpt++ *(*int8)(unsafe.Pointer(v62)) = int8('0') } else { for { if !(e2 >= 0) { break } v64 = bufpt bufpt++ if j < (*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fn { v66 = j j++ v65 = int32(*(*int8)(unsafe.Pointer((*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fz + uintptr(v66)))) } else { v65 = int32('0') } *(*int8)(unsafe.Pointer(v64)) = int8(v65) if cThousand != 0 && e2%int32(3) == 0 && e2 > int32(1) { v67 = bufpt bufpt++ *(*int8)(unsafe.Pointer(v67)) = int8(',') } goto _63 _63: ; e2-- } } /* The decimal point */ if flag_dp != 0 { v68 = bufpt bufpt++ *(*int8)(unsafe.Pointer(v68)) = int8('.') } /* "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++ *(*int8)(unsafe.Pointer(v70)) = int8('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(*(*int8)(unsafe.Pointer((*(*TFpDecode)(unsafe.Pointer(bp + 72))).Fz + uintptr(v74)))) } else { v73 = int32('0') } *(*int8)(unsafe.Pointer(v72)) = int8(v73) } /* Remove trailing zeros and the "." if no digits follow the "." */ if flag_rtz != 0 && flag_dp != 0 { for int32(*(*int8)(unsafe.Pointer(bufpt + uintptr(-libc.Int32FromInt32(1))))) == int32('0') { bufpt-- v75 = bufpt *(*int8)(unsafe.Pointer(v75)) = 0 } if int32(*(*int8)(unsafe.Pointer(bufpt + uintptr(-libc.Int32FromInt32(1))))) == int32('.') { if flag_altform2 != 0 { v76 = bufpt bufpt++ *(*int8)(unsafe.Pointer(v76)) = int8('0') } else { bufpt-- v77 = bufpt *(*int8)(unsafe.Pointer(v77)) = 0 } } } /* Add the "eNNN" suffix */ if int32(xtype) == int32(etEXP) { exp = (*(*TFpDecode)(unsafe.Pointer(bp + 72))).FiDP - int32(1) v78 = bufpt bufpt++ *(*int8)(unsafe.Pointer(v78)) = _aDigits[(*Tet_info)(unsafe.Pointer(infop)).Fcharset] if exp < 0 { v79 = bufpt bufpt++ *(*int8)(unsafe.Pointer(v79)) = int8('-') exp = -exp } else { v80 = bufpt bufpt++ *(*int8)(unsafe.Pointer(v80)) = int8('+') } if exp >= int32(100) { v81 = bufpt bufpt++ *(*int8)(unsafe.Pointer(v81)) = int8(exp/libc.Int32FromInt32(100) + libc.Int32FromUint8('0')) /* 100's digit */ exp %= int32(100) } v82 = bufpt bufpt++ *(*int8)(unsafe.Pointer(v82)) = int8(exp/libc.Int32FromInt32(10) + libc.Int32FromUint8('0')) /* 10's digit */ v83 = bufpt bufpt++ *(*int8)(unsafe.Pointer(v83)) = int8(exp%libc.Int32FromInt32(10) + libc.Int32FromUint8('0')) /* 1's digit */ } *(*int8)(unsafe.Pointer(bufpt)) = 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(int64(bufpt) - int64(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 } *(*int8)(unsafe.Pointer(bufpt + uintptr(i))) = *(*int8)(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++ *(*int8)(unsafe.Pointer(bufpt + uintptr(v86))) = int8('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]int8)(unsafe.Pointer(bp)))[0] = int8('%') 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(*(*int8)(unsafe.Pointer(v89))) c = v88 (*(*[70]int8)(unsafe.Pointer(bp)))[0] = int8(v88) if c&int32(0xc0) == int32(0xc0) { for length < int32(4) && int32(*(*int8)(unsafe.Pointer(bufpt)))&int32(0xc0) == int32(0x80) { v90 = length length++ v91 = bufpt bufpt++ (*(*[70]int8)(unsafe.Pointer(bp)))[v90] = *(*int8)(unsafe.Pointer(v91)) } } } else { (*(*[70]int8)(unsafe.Pointer(bp)))[0] = 0 } } else { ch = libc.VaUint32(&ap) if ch < uint32(0x00080) { (*(*[70]int8)(unsafe.Pointer(bp)))[0] = int8(ch & uint32(0xff)) length = int32(1) } else { if ch < uint32(0x00800) { (*(*[70]int8)(unsafe.Pointer(bp)))[0] = int8(int32(0xc0) + int32(uint8(ch>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x1f)))) (*(*[70]int8)(unsafe.Pointer(bp)))[int32(1)] = int8(int32(0x80) + int32(uint8(ch&libc.Uint32FromInt32(0x3f)))) length = int32(2) } else { if ch < uint32(0x10000) { (*(*[70]int8)(unsafe.Pointer(bp)))[0] = int8(int32(0xe0) + int32(uint8(ch>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x0f)))) (*(*[70]int8)(unsafe.Pointer(bp)))[int32(1)] = int8(int32(0x80) + int32(uint8(ch>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3f)))) (*(*[70]int8)(unsafe.Pointer(bp)))[int32(2)] = int8(int32(0x80) + int32(uint8(ch&libc.Uint32FromInt32(0x3f)))) length = int32(3) } else { (*(*[70]int8)(unsafe.Pointer(bp)))[0] = int8(int32(0xf0) + int32(uint8(ch>>libc.Int32FromInt32(18)&libc.Uint32FromInt32(0x07)))) (*(*[70]int8)(unsafe.Pointer(bp)))[int32(1)] = int8(int32(0x80) + int32(uint8(ch>>libc.Int32FromInt32(12)&libc.Uint32FromInt32(0x3f)))) (*(*[70]int8)(unsafe.Pointer(bp)))[int32(2)] = int8(int32(0x80) + int32(uint8(ch>>libc.Int32FromInt32(6)&libc.Uint32FromInt32(0x3f)))) (*(*[70]int8)(unsafe.Pointer(bp)))[int32(3)] = int8(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), int8(' ')) 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 + 1664 } 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. */ (*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 + 29 *(*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(int64(z) - int64(bufpt)) } else { length = 0 for { if !(length < precision && *(*int8)(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(*(*int8)(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 = int8(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 + 1665 } else { v98 = __ccgo_ts + 1670 } 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 = *(*int8)(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(*(*int8)(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++ *(*int8)(unsafe.Pointer(bufpt + uintptr(v104))) = q } k = i1 i1 = 0 for { if !(i1 < k) { break } v106 = j1 j1++ v107 = *(*int8)(unsafe.Pointer(escarg + uintptr(i1))) ch1 = v107 *(*int8)(unsafe.Pointer(bufpt + uintptr(v106))) = v107 if int32(ch1) == int32(q) { v108 = j1 j1++ *(*int8)(unsafe.Pointer(bufpt + uintptr(v108))) = ch1 } goto _105 _105: ; i1++ } if needQuote != 0 { v109 = j1 j1++ *(*int8)(unsafe.Pointer(bufpt + uintptr(v109))) = q } *(*int8)(unsafe.Pointer(bufpt + uintptr(j1))) = 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) 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) 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+1677, 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 /* Because of tag-20240424-1 */ if (*TSelect)(unsafe.Pointer(pSel)).FselFlags&uint32(SF_NestedFrom) != 0 { Xsqlite3_str_appendf(tls, pAccum, __ccgo_ts+1679, libc.VaList(bp+128, (*TSelect)(unsafe.Pointer(pSel)).FselId)) } else { if (*TSelect)(unsafe.Pointer(pSel)).FselFlags&uint32(SF_MultiValue) != 0 { Xsqlite3_str_appendf(tls, pAccum, __ccgo_ts+1689, libc.VaList(bp+128, *(*Tu32)(unsafe.Pointer(&(*TSrcItem)(unsafe.Pointer(pItem)).Fu1)))) } else { Xsqlite3_str_appendf(tls, pAccum, __ccgo_ts+1710, libc.VaList(bp+128, (*TSelect)(unsafe.Pointer(pSel)).FselId)) } } } } } v111 = libc.Int32FromInt32(0) width = v111 length = v111 goto _44 _43: ; 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, int8(' ')) } Xsqlite3_str_append(tls, pAccum, bufpt, length) if flag_leftjustify != 0 { Xsqlite3_str_appendchar(tls, pAccum, width, int8(' ')) } } 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]int8{'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 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 uint64(z) >= uint64(zText) && uint64(z) < uint64(zEnd) { (*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset = int32(int64(z) - int64(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 + 52)) <= 0) { pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft } if pExpr == uintptr(0) { return } (*Tsqlite3)(unsafe.Pointer(db)).FerrByteOffset = *(*int32)(unsafe.Pointer(pExpr + 52)) } // 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 /* 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 { if !(int32((*TStrAccum)(unsafe.Pointer(p)).FprintfFlags)&libc.Int32FromInt32(SQLITE_PRINTF_MALLOCED) != libc.Int32FromInt32(0)) && (*TStrAccum)(unsafe.Pointer(p)).FnChar > uint32(0) { libc.X__builtin___memcpy_chk(tls, zNew, (*TStrAccum)(unsafe.Pointer(p)).FzText, uint64((*TStrAccum)(unsafe.Pointer(p)).FnChar), libc.X__builtin_object_size(tls, zNew, 0)) } (*TStrAccum)(unsafe.Pointer(p)).FzText = zNew (*TStrAccum)(unsafe.Pointer(p)).FnAlloc = uint32(_sqlite3DbMallocSize(tls, (*TStrAccum)(unsafe.Pointer(p)).Fdb, zNew)) p2 = p + 29 *(*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 } } 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 int8) { 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 + 24 v4 = *(*Tu32)(unsafe.Pointer(v5)) *(*Tu32)(unsafe.Pointer(v5))++ *(*int8)(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.X__builtin___memcpy_chk(tls, (*TStrAccum)(unsafe.Pointer(p)).FzText+uintptr((*TStrAccum)(unsafe.Pointer(p)).FnChar), z, uint64(N), libc.X__builtin_object_size(tls, (*TStrAccum)(unsafe.Pointer(p)).FzText+uintptr((*TStrAccum)(unsafe.Pointer(p)).FnChar), 0)) *(*Tu32)(unsafe.Pointer(p + 24)) += 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) { if (*Tsqlite3_str)(unsafe.Pointer(p)).FnChar+uint32(N) >= (*Tsqlite3_str)(unsafe.Pointer(p)).FnAlloc { _enlargeAndAppend(tls, p, z, N) } else { if N != 0 { *(*Tu32)(unsafe.Pointer(p + 24)) += uint32(N) libc.X__builtin___memcpy_chk(tls, (*Tsqlite3_str)(unsafe.Pointer(p)).FzText+uintptr((*Tsqlite3_str)(unsafe.Pointer(p)).FnChar-uint32(N)), z, uint64(N), libc.X__builtin_object_size(tls, (*Tsqlite3_str)(unsafe.Pointer(p)).FzText+uintptr((*Tsqlite3_str)(unsafe.Pointer(p)).FnChar-uint32(N)), 0)) } } } // 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 zText = _sqlite3DbMallocRaw(tls, (*TStrAccum)(unsafe.Pointer(p)).Fdb, uint64((*TStrAccum)(unsafe.Pointer(p)).FnChar+uint32(1))) if zText != 0 { libc.X__builtin___memcpy_chk(tls, zText, (*TStrAccum)(unsafe.Pointer(p)).FzText, uint64((*TStrAccum)(unsafe.Pointer(p)).FnChar+uint32(1)), libc.X__builtin_object_size(tls, zText, 0)) p1 = p + 29 *(*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 { *(*int8)(unsafe.Pointer((*TStrAccum)(unsafe.Pointer(p)).FzText + uintptr((*TStrAccum)(unsafe.Pointer(p)).FnChar))) = 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+1664, 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) } *(*int8)(unsafe.Pointer((*Tsqlite3_str)(unsafe.Pointer(p)).FzText + uintptr((*Tsqlite3_str)(unsafe.Pointer(p)).FnChar))) = 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 + 29 *(*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(32)) if p != 0 { if db != 0 { v1 = *(*int32)(unsafe.Pointer(db + 136)) } 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(112) defer tls.Free(112) var z uintptr var _ /* acc at bp+72 */ TStrAccum var _ /* zBase at bp+0 */ [70]int8 _ = z _sqlite3StrAccumInit(tls, bp+72, db, bp, int32(70), *(*int32)(unsafe.Pointer(db + 136))) (*(*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(112) defer tls.Free(112) var z uintptr var _ /* acc at bp+72 */ TStrAccum var _ /* zBase at bp+0 */ [70]int8 _ = 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) *(*int8)(unsafe.Pointer(zBuf + uintptr((*(*TStrAccum)(unsafe.Pointer(bp))).FnChar))) = 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 *(*int8)(unsafe.Pointer(zBuf + uintptr((*(*TStrAccum)(unsafe.Pointer(bp))).FnChar))) = 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(256) defer tls.Free(256) var _ /* acc at bp+0 */ TStrAccum var _ /* zMsg at bp+32 */ [210]int8 /* Complete log message */ _sqlite3StrAccumInit(tls, bp, uintptr(0), bp+32, 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 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 p -= 8 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 p -= 8 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.X__builtin___memcpy_chk(tls, bp, in, uint64(64), libc.X__builtin_object_size(tls, bp, 0)) 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.X__builtin___memcpy_chk(tls, uintptr(unsafe.Pointer(&_sqlite3Prng)), uintptr(unsafe.Pointer(&_chacha20_init)), uint64(16), libc.X__builtin_object_size(tls, uintptr(unsafe.Pointer(&_sqlite3Prng)), 0)) if pVfs == uintptr(0) { libc.X__builtin___memset_chk(tls, uintptr(unsafe.Pointer(&_sqlite3Prng))+4*4, 0, uint64(44), libc.X__builtin_object_size(tls, uintptr(unsafe.Pointer(&_sqlite3Prng))+4*4, 0)) } 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) } for int32(1) != 0 { if N <= int32(_sqlite3Prng.Fn) { libc.X__builtin___memcpy_chk(tls, zBuf, uintptr(unsafe.Pointer(&_sqlite3Prng))+64+uintptr(int32(_sqlite3Prng.Fn)-N), uint64(N), libc.X__builtin_object_size(tls, zBuf, 0)) p1 = uintptr(unsafe.Pointer(&_sqlite3Prng)) + 128 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) - N) break } if int32(_sqlite3Prng.Fn) > 0 { libc.X__builtin___memcpy_chk(tls, zBuf, uintptr(unsafe.Pointer(&_sqlite3Prng))+64, uint64(_sqlite3Prng.Fn), libc.X__builtin_object_size(tls, zBuf, 0)) 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.X__builtin___memcpy_chk(tls, uintptr(unsafe.Pointer(&_sqlite3SavedPrng)), uintptr(unsafe.Pointer(&_sqlite3Prng)), uint64(132), libc.X__builtin_object_size(tls, uintptr(unsafe.Pointer(&_sqlite3SavedPrng)), 0)) } func _sqlite3PrngRestoreState(tls *libc.TLS) { libc.X__builtin___memcpy_chk(tls, uintptr(unsafe.Pointer(&_sqlite3Prng)), uintptr(unsafe.Pointer(&_sqlite3SavedPrng)), uint64(132), libc.X__builtin_object_size(tls, uintptr(unsafe.Pointer(&_sqlite3Prng)), 0)) } /************** 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 *(*uintptr)(unsafe.Pointer(ppThread)) = uintptr(0) p = _sqlite3Malloc(tls, uint64(24)) if p == uintptr(0) { return int32(SQLITE_NOMEM) } if int32(int64(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) { 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 { /* 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(int64(z) - int64(zOut)) v18 = z z++ *(*uint8)(unsafe.Pointer(v18)) = uint8(0) } else { 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(int64(z) - int64(zOut)) } *(*uint8)(unsafe.Pointer(z)) = uint8(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) 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 + 16)) -= int32(2) libc.X__builtin___memmove_chk(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, (*TMem)(unsafe.Pointer(pMem)).Fz+2, uint64((*TMem)(unsafe.Pointer(pMem)).Fn), libc.X__builtin_object_size(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, 0)) *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = int8('\000') *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn+int32(1)))) = int8('\000') p1 = pMem + 20 *(*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)) } 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(64) defer tls.Free(64) var _ /* m at bp+0 */ TMem libc.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) (*(*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) } 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(int64(z)-int64(zIn)) - libc.BoolInt32(true) } type Tfloat_t = float32 type float_t = Tfloat_t type Tdouble_t = float64 type double_t = Tdouble_t type t__float2 = struct { F__sinval float32 F__cosval float32 } type t__double2 = struct { F__sinval float64 F__cosval float64 } type Texception = struct { Ftype1 int32 Fname uintptr Farg1 float64 Farg2 float64 Fretval float64 } type exception = Texception // 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.X__builtin___memcpy_chk(tls, bp+8, bp, uint64(8), libc.X__builtin_object_size(tls, bp+8, 0)) rc = libc.BoolInt32(*(*Tu64)(unsafe.Pointer(bp + 8))&(libc.Uint64FromInt32(0x7ff)<>4)) != 0 { return _sqlite3StdType[int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 8))&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) { (*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) { (*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 (*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 int8 _, _, _, _, _ = i, j, quote, v2, v3 if z == uintptr(0) { return } quote = *(*int8)(unsafe.Pointer(z)) if !(int32(_sqlite3CtypeMap[uint8(quote)])&libc.Int32FromInt32(0x80) != 0) { return } if int32(quote) == int32('[') { quote = int8(']') } i = int32(1) j = libc.Int32FromInt32(0) for { if int32(*(*int8)(unsafe.Pointer(z + uintptr(i)))) == int32(quote) { if int32(*(*int8)(unsafe.Pointer(z + uintptr(i+int32(1))))) == int32(quote) { v2 = j j++ *(*int8)(unsafe.Pointer(z + uintptr(v2))) = quote i++ } else { break } } else { v3 = j j++ *(*int8)(unsafe.Pointer(z + uintptr(v3))) = *(*int8)(unsafe.Pointer(z + uintptr(i))) } goto _1 _1: ; i++ } *(*int8)(unsafe.Pointer(z + uintptr(j))) = 0 } func _sqlite3DequoteExpr(tls *libc.TLS, p uintptr) { var v1 int32 _ = v1 if int32(*(*int8)(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 // // /* // ** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken // ** and set the type to INTEGER or FLOAT. "Quoted" integers or floats are those // ** that contain '_' characters that must be removed before further processing. // */ func _sqlite3DequoteNumber(tls *libc.TLS, pParse uintptr, p uintptr) { bp := tls.Alloc(32) defer tls.Free(32) var bHex int32 var pIn, pOut, v1, v3 uintptr var _ /* iValue at bp+0 */ int32 _, _, _, _, _ = bHex, pIn, pOut, v1, v3 if p != 0 { pIn = *(*uintptr)(unsafe.Pointer(p + 8)) pOut = *(*uintptr)(unsafe.Pointer(p + 8)) bHex = libc.BoolInt32(int32(*(*int8)(unsafe.Pointer(pIn))) == int32('0') && (int32(*(*int8)(unsafe.Pointer(pIn + 1))) == int32('x') || int32(*(*int8)(unsafe.Pointer(pIn + 1))) == int32('X'))) (*TExpr)(unsafe.Pointer(p)).Fop = uint8(TK_INTEGER) for { if int32(*(*int8)(unsafe.Pointer(pIn))) != int32('_') { v3 = pOut pOut++ *(*int8)(unsafe.Pointer(v3)) = *(*int8)(unsafe.Pointer(pIn)) if int32(*(*int8)(unsafe.Pointer(pIn))) == int32('e') || int32(*(*int8)(unsafe.Pointer(pIn))) == int32('E') || int32(*(*int8)(unsafe.Pointer(pIn))) == int32('.') { (*TExpr)(unsafe.Pointer(p)).Fop = uint8(TK_FLOAT) } } else { if bHex == 0 && (!(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(pIn + uintptr(-libc.Int32FromInt32(1)))))])&libc.Int32FromInt32(0x04) != 0) || !(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(pIn + 1)))])&libc.Int32FromInt32(0x04) != 0)) || bHex == int32(1) && (!(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(pIn + uintptr(-libc.Int32FromInt32(1)))))])&libc.Int32FromInt32(0x08) != 0) || !(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(pIn + 1)))])&libc.Int32FromInt32(0x08) != 0)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1724, libc.VaList(bp+16, *(*uintptr)(unsafe.Pointer(p + 8)))) } } goto _2 _2: ; v1 = pIn pIn++ if !(*(*int8)(unsafe.Pointer(v1)) != 0) { break } } if bHex != 0 { (*TExpr)(unsafe.Pointer(p)).Fop = uint8(TK_INTEGER) } /* tag-20240227-a: If after dequoting, the number is an integer that ** fits in 32 bits, then it must be converted into EP_IntValue. Other ** parts of the code expect this. See also tag-20240227-b. */ if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_INTEGER) && _sqlite3GetInt32(tls, *(*uintptr)(unsafe.Pointer(p + 8)), bp) != 0 { *(*int32)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fu)) = *(*int32)(unsafe.Pointer(bp)) *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(EP_IntValue) } } } // 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(*(*int8)(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(*(*int8)(unsafe.Pointer((*TToken)(unsafe.Pointer(p)).Fz + uintptr(i))))])&int32(0x80) != 0 { return } goto _1 _1: ; i++ } *(*uint32)(unsafe.Pointer(p + 8)) -= 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 *(*int8)(unsafe.Pointer(z)) != 0 { h = Tu8(int32(h) + int32(_sqlite3UpperToLower[uint8(*(*int8)(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.X__builtin___memcpy_chk(tls, bp+24, x, uint64(8), libc.X__builtin_object_size(tls, bp+24, 0)) *(*Tu64)(unsafe.Pointer(bp + 24)) &= uint64(0xfffffffffc000000) libc.X__builtin___memcpy_chk(tls, bp+8, bp+24, uint64(8), libc.X__builtin_object_size(tls, bp+8, 0)) tx = libc.AtomicLoadPFloat64(x) - *(*float64)(unsafe.Pointer(bp + 8)) libc.X__builtin___memcpy_chk(tls, bp+24, bp, uint64(8), libc.X__builtin_object_size(tls, bp+24, 0)) *(*Tu64)(unsafe.Pointer(bp + 24)) &= uint64(0xfffffffffc000000) libc.X__builtin___memcpy_chk(tls, bp+16, bp+24, uint64(8), libc.X__builtin_object_size(tls, bp+16, 0)) 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 */ *(*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) i = int32(3) - int32(enc) for { if !(i < length && int32(*(*int8)(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(*(*int8)(unsafe.Pointer(z)))])&int32(0x01) != 0 { z += uintptr(incr) } if z >= zEnd { return 0 } /* get sign of significand */ if int32(*(*int8)(unsafe.Pointer(z))) == int32('-') { sign = -int32(1) z += uintptr(incr) } else { if int32(*(*int8)(unsafe.Pointer(z))) == int32('+') { z += uintptr(incr) } } /* copy max significant digits to significand */ for z < zEnd && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(z)))])&int32(0x04) != 0 { s = s*uint64(10) + uint64(int32(*(*int8)(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(*(*int8)(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(*(*int8)(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(*(*int8)(unsafe.Pointer(z))) == int32('e') || int32(*(*int8)(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(*(*int8)(unsafe.Pointer(z))) == int32('-') { esign = -int32(1) z += uintptr(incr) } else { if int32(*(*int8)(unsafe.Pointer(z))) == int32('+') { z += uintptr(incr) } } /* copy digits to exponent */ for z < zEnd && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(z)))])&int32(0x04) != 0 { if e < int32(10000) { v2 = e*int32(10) + (int32(*(*int8)(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(*(*int8)(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) } } if r > +libc.Float64FromFloat64(1.7976931348623157081452742373e+308) { *(*float64)(unsafe.Pointer(pResult)) = float64(+libc.X__builtin_huge_valf(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)) } 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]int8 _, _, _, _ = 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 */ 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 */ 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(*(*int8)(unsafe.Pointer(z))) == int32('0') && (int32(*(*int8)(unsafe.Pointer(z + 1))) == int32('x') || int32(*(*int8)(unsafe.Pointer(z + 1))) == int32('X')) { *(*Tu64)(unsafe.Pointer(bp)) = uint64(0) i = int32(2) for { if !(int32(*(*int8)(unsafe.Pointer(z + uintptr(i)))) == int32('0')) { break } goto _1 _1: ; i++ } k = i for { if !(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(z + uintptr(k))))])&int32(0x08) != 0) { break } *(*Tu64)(unsafe.Pointer(bp)) = *(*Tu64)(unsafe.Pointer(bp))*uint64(16) + uint64(_sqlite3HexToInt(tls, int32(*(*int8)(unsafe.Pointer(z + uintptr(k)))))) goto _2 _2: ; k++ } libc.X__builtin___memcpy_chk(tls, pOut, bp, uint64(8), libc.X__builtin_object_size(tls, pOut, 0)) if k-i > int32(16) { return int32(2) } if int32(*(*int8)(unsafe.Pointer(z + uintptr(k)))) != 0 { return int32(1) } return 0 } else { n = int32(libc.Uint64FromInt32(0x3fffffff) & libc.Xstrspn(tls, z, __ccgo_ts+1768)) if *(*int8)(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(*(*int8)(unsafe.Pointer(zNum))) == int32('-') { neg = int32(1) zNum++ } else { if int32(*(*int8)(unsafe.Pointer(zNum))) == int32('+') { zNum++ } else { if int32(*(*int8)(unsafe.Pointer(zNum))) == int32('0') && (int32(*(*int8)(unsafe.Pointer(zNum + 1))) == int32('x') || int32(*(*int8)(unsafe.Pointer(zNum + 1))) == int32('X')) && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zNum + 2)))])&int32(0x08) != 0 { *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) zNum += uintptr(2) for int32(*(*int8)(unsafe.Pointer(zNum))) == int32('0') { zNum++ } i = 0 for { if !(i < int32(8) && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zNum + uintptr(i))))])&int32(0x08) != 0) { break } *(*Tu32)(unsafe.Pointer(bp)) = *(*Tu32)(unsafe.Pointer(bp))*uint32(16) + uint32(_sqlite3HexToInt(tls, int32(*(*int8)(unsafe.Pointer(zNum + uintptr(i)))))) goto _1 _1: ; i++ } if *(*Tu32)(unsafe.Pointer(bp))&uint32(0x80000000) == uint32(0) && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zNum + uintptr(i))))])&int32(0x08) == 0 { libc.X__builtin___memcpy_chk(tls, pValue, bp, uint64(4), libc.X__builtin_object_size(tls, pValue, 0)) return int32(1) } else { return 0 } } } } if !(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zNum)))])&libc.Int32FromInt32(0x04) != 0) { return 0 } for int32(*(*int8)(unsafe.Pointer(zNum))) == int32('0') { zNum++ } i = 0 for { if v4 = i < int32(11); v4 { v3 = int32(*(*int8)(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 = 0 (*TFpDecode)(unsafe.Pointer(p)).Fz = p + 24 /* Convert negative numbers to positive. Deal with Infinity, 0.0, and ** NaN. */ if *(*float64)(unsafe.Pointer(bp)) < float64(0) { (*TFpDecode)(unsafe.Pointer(p)).Fsign = int8('-') *(*float64)(unsafe.Pointer(bp)) = -*(*float64)(unsafe.Pointer(bp)) } else { if *(*float64)(unsafe.Pointer(bp)) == float64(0) { (*TFpDecode)(unsafe.Pointer(p)).Fsign = int8('+') (*TFpDecode)(unsafe.Pointer(p)).Fn = int32(1) (*TFpDecode)(unsafe.Pointer(p)).FiDP = int32(1) (*TFpDecode)(unsafe.Pointer(p)).Fz = __ccgo_ts + 1784 return } else { (*TFpDecode)(unsafe.Pointer(p)).Fsign = int8('+') } } libc.X__builtin___memcpy_chk(tls, bp+8, bp, uint64(8), libc.X__builtin_object_size(tls, bp+8, 0)) e = int32(*(*Tu64)(unsafe.Pointer(bp + 8)) >> int32(52)) if e&int32(0x7ff) == int32(0x7ff) { (*TFpDecode)(unsafe.Pointer(p)).FisSpecial = int8(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.Uint64FromInt64(24) - libc.Uint64FromInt32(1)) for *(*Tu64)(unsafe.Pointer(bp + 8)) != 0 { v2 = i i-- *(*int8)(unsafe.Pointer(p + 24 + uintptr(v2))) = int8(*(*Tu64)(unsafe.Pointer(bp + 8))%uint64(10) + uint64('0')) *(*Tu64)(unsafe.Pointer(bp + 8)) /= uint64(10) } (*TFpDecode)(unsafe.Pointer(p)).Fn = int32(libc.Uint64FromInt64(24) - libc.Uint64FromInt32(1) - uint64(i)) (*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(*(*int8)(unsafe.Pointer(p + 24 + uintptr(i+int32(1))))) >= int32('5') { iRound = int32(1) v3 = i i-- *(*int8)(unsafe.Pointer(p + 24 + uintptr(v3))) = int8('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 + 24 + uintptr(i+int32(1)) if iRound > mxRound { iRound = mxRound } (*TFpDecode)(unsafe.Pointer(p)).Fn = iRound if int32(*(*int8)(unsafe.Pointer(z + uintptr(iRound)))) >= int32('5') { j = iRound - int32(1) for int32(1) != 0 { *(*int8)(unsafe.Pointer(z + uintptr(j)))++ if int32(*(*int8)(unsafe.Pointer(z + uintptr(j)))) <= int32('9') { break } *(*int8)(unsafe.Pointer(z + uintptr(j))) = int8('0') if j == 0 { v4 = i i-- *(*int8)(unsafe.Pointer((*TFpDecode)(unsafe.Pointer(p)).Fz + uintptr(v4))) = int8('1') (*TFpDecode)(unsafe.Pointer(p)).Fn++ (*TFpDecode)(unsafe.Pointer(p)).FiDP++ break } else { j-- } } } } (*TFpDecode)(unsafe.Pointer(p)).Fz = p + 24 + uintptr(i+int32(1)) for (*TFpDecode)(unsafe.Pointer(p)).Fn > 0 && int32(*(*int8)(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(*(*int8)(unsafe.Pointer(z + uintptr(i))))])&int32(0x04) != 0) { break } v = v*uint64(10) + uint64(*(*int8)(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(*(*int8)(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)) 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 } 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) { return uint32(*(*Tu8)(unsafe.Pointer(p)))<> libc.Int32FromInt32(24)) *(*uint8)(unsafe.Pointer(p + 1)) = uint8(v >> libc.Int32FromInt32(16)) *(*uint8)(unsafe.Pointer(p + 2)) = uint8(v >> libc.Int32FromInt32(8)) *(*uint8)(unsafe.Pointer(p + 3)) = uint8(v) } // 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) { 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 } *(*int8)(unsafe.Pointer(zBlob + uintptr(i/int32(2)))) = int8(int32(_sqlite3HexToInt(tls, int32(*(*int8)(unsafe.Pointer(z + uintptr(i))))))<= 0 { if iA > 0 && libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)< iB+int64(1) { return int32(1) } } *(*Ti64)(unsafe.Pointer(pA)) += iB return 0 } func _sqlite3SubInt64(tls *libc.TLS, pA uintptr, iB Ti64) (r int32) { if iB == int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<= 0 { return int32(1) } *(*Ti64)(unsafe.Pointer(pA)) -= iB return 0 } else { return _sqlite3AddInt64(tls, pA, -iB) } return r } func _sqlite3MulInt64(tls *libc.TLS, pA uintptr, iB Ti64) (r int32) { var iA Ti64 _ = iA iA = *(*Ti64)(unsafe.Pointer(pA)) if iB > 0 { if iA > (libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)< 0 { if iB < (int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)< (libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<= 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 y TLogEst _ = 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 { for x > uint64(255) { y = TLogEst(int32(y) + libc.Int32FromInt32(40)) x >>= uint64(4) } /*OPTIMIZATION-IF-TRUE*/ for x > uint64(15) { y = TLogEst(int32(y) + libc.Int32FromInt32(10)) x >>= uint64(1) } } 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 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.X__builtin___memcpy_chk(tls, bp+8, bp, uint64(8), libc.X__builtin_object_size(tls, bp+8, 0)) 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) /* 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)*uint64(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.X__builtin___memcpy_chk(tls, z, zName, uint64(nName), libc.X__builtin_object_size(tls, z, 0)) *(*int8)(unsafe.Pointer(z + uintptr(nName))) = 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, uint64(nName)) == 0 && int32(*(*int8)(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) { (*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 */ 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(*(*int8)(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 uint64(new_size)*uint64(16) > uint64(SQLITE_MALLOC_SOFT_LIMIT) { new_size = uint32(libc.Uint64FromInt32(SQLITE_MALLOC_SOFT_LIMIT) / libc.Uint64FromInt64(16)) } 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)*uint64(16)) _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(uint64(_sqlite3MallocSize(tls, new_ht)) / libc.Uint64FromInt64(16)) new_size = v1 (*THash)(unsafe.Pointer(pH)).Fhtsize = v1 libc.X__builtin___memset_chk(tls, new_ht, 0, uint64(new_size)*uint64(16), libc.X__builtin_object_size(tls, new_ht, 0)) 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)*16, 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)*16 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 { 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)*16 if (*T_ht)(unsafe.Pointer(pEntry)).Fchain == elem { (*T_ht)(unsafe.Pointer(pEntry)).Fchain = (*THashElem)(unsafe.Pointer(elem)).Fnext } (*T_ht)(unsafe.Pointer(pEntry)).Fcount-- } Xsqlite3_free(tls, elem) (*THash)(unsafe.Pointer(pH)).Fcount-- if (*THash)(unsafe.Pointer(pH)).Fcount == uint32(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) { 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 */ 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(32)) 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 { *(*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)))*16 } 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 + 1848, 1: __ccgo_ts + 1858, 2: __ccgo_ts + 1869, 3: __ccgo_ts + 1881, 4: __ccgo_ts + 1892, 5: __ccgo_ts + 1904, 6: __ccgo_ts + 1911, 7: __ccgo_ts + 1919, 8: __ccgo_ts + 1927, 9: __ccgo_ts + 1932, 10: __ccgo_ts + 1937, 11: __ccgo_ts + 1943, 12: __ccgo_ts + 1957, 13: __ccgo_ts + 1963, 14: __ccgo_ts + 1973, 15: __ccgo_ts + 1978, 16: __ccgo_ts + 1983, 17: __ccgo_ts + 1986, 18: __ccgo_ts + 1992, 19: __ccgo_ts + 1999, 20: __ccgo_ts + 2003, 21: __ccgo_ts + 2013, 22: __ccgo_ts + 2020, 23: __ccgo_ts + 2027, 24: __ccgo_ts + 2034, 25: __ccgo_ts + 2041, 26: __ccgo_ts + 2051, 27: __ccgo_ts + 2060, 28: __ccgo_ts + 2071, 29: __ccgo_ts + 2080, 30: __ccgo_ts + 2086, 31: __ccgo_ts + 2096, 32: __ccgo_ts + 2106, 33: __ccgo_ts + 2111, 34: __ccgo_ts + 2125, 35: __ccgo_ts + 2136, 36: __ccgo_ts + 2141, 37: __ccgo_ts + 2148, 38: __ccgo_ts + 2159, 39: __ccgo_ts + 2164, 40: __ccgo_ts + 2169, 41: __ccgo_ts + 2175, 42: __ccgo_ts + 2181, 43: __ccgo_ts + 2187, 44: __ccgo_ts + 2190, 45: __ccgo_ts + 2194, 46: __ccgo_ts + 2200, 47: __ccgo_ts + 2211, 48: __ccgo_ts + 2222, 49: __ccgo_ts + 2230, 50: __ccgo_ts + 2239, 51: __ccgo_ts + 2246, 52: __ccgo_ts + 2254, 53: __ccgo_ts + 2257, 54: __ccgo_ts + 2260, 55: __ccgo_ts + 2263, 56: __ccgo_ts + 2266, 57: __ccgo_ts + 2269, 58: __ccgo_ts + 2272, 59: __ccgo_ts + 2279, 60: __ccgo_ts + 2285, 61: __ccgo_ts + 2295, 62: __ccgo_ts + 2308, 63: __ccgo_ts + 2319, 64: __ccgo_ts + 2325, 65: __ccgo_ts + 2332, 66: __ccgo_ts + 2341, 67: __ccgo_ts + 2350, 68: __ccgo_ts + 2357, 69: __ccgo_ts + 2370, 70: __ccgo_ts + 2381, 71: __ccgo_ts + 2386, 72: __ccgo_ts + 2394, 73: __ccgo_ts + 2400, 74: __ccgo_ts + 2407, 75: __ccgo_ts + 2419, 76: __ccgo_ts + 2424, 77: __ccgo_ts + 2433, 78: __ccgo_ts + 2438, 79: __ccgo_ts + 2447, 80: __ccgo_ts + 2452, 81: __ccgo_ts + 2457, 82: __ccgo_ts + 2463, 83: __ccgo_ts + 2471, 84: __ccgo_ts + 2479, 85: __ccgo_ts + 2489, 86: __ccgo_ts + 2497, 87: __ccgo_ts + 2504, 88: __ccgo_ts + 2517, 89: __ccgo_ts + 2522, 90: __ccgo_ts + 2534, 91: __ccgo_ts + 2542, 92: __ccgo_ts + 2549, 93: __ccgo_ts + 2560, 94: __ccgo_ts + 2567, 95: __ccgo_ts + 2574, 96: __ccgo_ts + 2584, 97: __ccgo_ts + 2593, 98: __ccgo_ts + 2604, 99: __ccgo_ts + 2610, 100: __ccgo_ts + 2621, 101: __ccgo_ts + 2631, 102: __ccgo_ts + 2641, 103: __ccgo_ts + 2648, 104: __ccgo_ts + 2654, 105: __ccgo_ts + 2664, 106: __ccgo_ts + 2675, 107: __ccgo_ts + 2679, 108: __ccgo_ts + 2688, 109: __ccgo_ts + 2697, 110: __ccgo_ts + 2704, 111: __ccgo_ts + 2714, 112: __ccgo_ts + 2721, 113: __ccgo_ts + 2730, 114: __ccgo_ts + 2740, 115: __ccgo_ts + 2747, 116: __ccgo_ts + 2755, 117: __ccgo_ts + 2769, 118: __ccgo_ts + 2777, 119: __ccgo_ts + 2791, 120: __ccgo_ts + 2802, 121: __ccgo_ts + 2815, 122: __ccgo_ts + 2826, 123: __ccgo_ts + 2832, 124: __ccgo_ts + 2844, 125: __ccgo_ts + 2853, 126: __ccgo_ts + 2861, 127: __ccgo_ts + 2870, 128: __ccgo_ts + 2879, 129: __ccgo_ts + 2886, 130: __ccgo_ts + 2894, 131: __ccgo_ts + 2901, 132: __ccgo_ts + 2912, 133: __ccgo_ts + 2926, 134: __ccgo_ts + 2937, 135: __ccgo_ts + 2945, 136: __ccgo_ts + 2951, 137: __ccgo_ts + 2959, 138: __ccgo_ts + 2967, 139: __ccgo_ts + 2977, 140: __ccgo_ts + 2990, 141: __ccgo_ts + 3000, 142: __ccgo_ts + 3013, 143: __ccgo_ts + 3022, 144: __ccgo_ts + 3033, 145: __ccgo_ts + 3041, 146: __ccgo_ts + 3047, 147: __ccgo_ts + 3059, 148: __ccgo_ts + 3071, 149: __ccgo_ts + 3079, 150: __ccgo_ts + 3091, 151: __ccgo_ts + 3104, 152: __ccgo_ts + 3114, 153: __ccgo_ts + 3124, 154: __ccgo_ts + 3129, 155: __ccgo_ts + 3141, 156: __ccgo_ts + 3153, 157: __ccgo_ts + 3163, 158: __ccgo_ts + 3169, 159: __ccgo_ts + 3179, 160: __ccgo_ts + 3186, 161: __ccgo_ts + 3198, 162: __ccgo_ts + 3209, 163: __ccgo_ts + 3217, 164: __ccgo_ts + 3226, 165: __ccgo_ts + 3235, 166: __ccgo_ts + 3244, 167: __ccgo_ts + 3251, 168: __ccgo_ts + 3262, 169: __ccgo_ts + 3275, 170: __ccgo_ts + 3285, 171: __ccgo_ts + 3292, 172: __ccgo_ts + 3300, 173: __ccgo_ts + 3309, 174: __ccgo_ts + 3315, 175: __ccgo_ts + 3322, 176: __ccgo_ts + 3330, 177: __ccgo_ts + 3338, 178: __ccgo_ts + 3346, 179: __ccgo_ts + 3356, 180: __ccgo_ts + 3365, 181: __ccgo_ts + 3376, 182: __ccgo_ts + 3387, 183: __ccgo_ts + 3398, 184: __ccgo_ts + 3408, 185: __ccgo_ts + 3414, 186: __ccgo_ts + 3425, 187: __ccgo_ts + 3436, 188: __ccgo_ts + 3441, 189: __ccgo_ts + 3449, } 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 = uint64 type u_long = Tu_long type Tushort = uint16 type ushort = Tushort type Tuint = uint32 type uint = Tuint type Tu_quad_t = uint64 type u_quad_t = Tu_quad_t type Tquad_t = int64 type quad_t = Tquad_t type Tqaddr_t = uintptr type qaddr_t = Tqaddr_t type Tcaddr_t = uintptr type caddr_t = Tcaddr_t type Tdaddr_t = int32 type daddr_t = Tdaddr_t type Tfixpt_t = uint32 type fixpt_t = Tfixpt_t type Tblkcnt_t = int64 type blkcnt_t = Tblkcnt_t type Tblksize_t = int32 type blksize_t = Tblksize_t type Tgid_t = uint32 type gid_t = Tgid_t type Tin_addr_t = uint32 type in_addr_t = Tin_addr_t type Tin_port_t = uint16 type in_port_t = Tin_port_t type Tino_t = uint64 type ino_t = Tino_t type Tino64_t = uint64 type ino64_t = Tino64_t type Tkey_t = int32 type key_t = Tkey_t type Tnlink_t = uint16 type nlink_t = Tnlink_t type Tsegsz_t = int32 type segsz_t = Tsegsz_t type Tswblk_t = int32 type swblk_t = Tswblk_t type Tuseconds_t = uint32 type useconds_t = Tuseconds_t type Tsuseconds_t = int32 type suseconds_t = Tsuseconds_t type Tfd_set = struct { Ffds_bits [32]t__int32_t } type fd_set = Tfd_set type Tfd_mask = int32 type fd_mask = Tfd_mask type Tpthread_cond_t = struct { F__sig int64 F__opaque [40]int8 } type pthread_cond_t = Tpthread_cond_t type Tpthread_condattr_t = struct { F__sig int64 F__opaque [8]int8 } type pthread_condattr_t = Tpthread_condattr_t type Tpthread_mutex_t = struct { F__sig int64 F__opaque [56]int8 } type pthread_mutex_t = Tpthread_mutex_t type Tpthread_mutexattr_t = struct { F__sig int64 F__opaque [8]int8 } type pthread_mutexattr_t = Tpthread_mutexattr_t type Tpthread_once_t = struct { F__sig int64 F__opaque [8]int8 } type pthread_once_t = Tpthread_once_t type Tpthread_rwlock_t = struct { F__sig int64 F__opaque [192]int8 } type pthread_rwlock_t = Tpthread_rwlock_t type Tpthread_rwlockattr_t = struct { F__sig int64 F__opaque [16]int8 } type pthread_rwlockattr_t = Tpthread_rwlockattr_t type Tpthread_t = uintptr type pthread_t = Tpthread_t type Tpthread_key_t = uint64 type pthread_key_t = Tpthread_key_t type Tfsblkcnt_t = uint32 type fsblkcnt_t = Tfsblkcnt_t type Tfsfilcnt_t = uint32 type fsfilcnt_t = Tfsfilcnt_t type Tostat = struct { Fst_dev t__uint16_t Fst_ino Tino_t Fst_mode Tmode_t Fst_nlink Tnlink_t Fst_uid t__uint16_t Fst_gid t__uint16_t Fst_rdev t__uint16_t Fst_size t__int32_t Fst_atimespec Ttimespec Fst_mtimespec Ttimespec Fst_ctimespec Ttimespec Fst_blksize t__int32_t Fst_blocks t__int32_t Fst_flags t__uint32_t Fst_gen t__uint32_t } type ostat = Tostat type Tstat = struct { Fst_dev Tdev_t Fst_mode Tmode_t Fst_nlink Tnlink_t Fst_ino t__darwin_ino64_t Fst_uid Tuid_t Fst_gid Tgid_t Fst_rdev Tdev_t Fst_atimespec Ttimespec Fst_mtimespec Ttimespec Fst_ctimespec Ttimespec Fst_birthtimespec Ttimespec Fst_size Toff_t Fst_blocks Tblkcnt_t Fst_blksize Tblksize_t Fst_flags t__uint32_t Fst_gen t__uint32_t Fst_lspare t__int32_t Fst_qspare [2]t__int64_t } type stat = Tstat type Tfilesec_t = uintptr type filesec_t = Tfilesec_t type Tflock = struct { Fl_start Toff_t Fl_len Toff_t Fl_pid Tpid_t Fl_type int16 Fl_whence int16 } type flock = Tflock type Tflocktimeout = struct { Ffl Tflock Ftimeout Ttimespec } type flocktimeout = Tflocktimeout type Tradvisory = struct { Fra_offset Toff_t Fra_count int32 } type radvisory = Tradvisory type Tfsignatures_t = struct { Ffs_file_start Toff_t Ffs_blob_start uintptr Ffs_blob_size Tsize_t Ffs_fsignatures_size Tsize_t Ffs_cdhash [20]int8 Ffs_hash_type int32 } type fsignatures_t = Tfsignatures_t type Tfsignatures = Tfsignatures_t type fsignatures = Tfsignatures type Tfsupplement_t = struct { Ffs_file_start Toff_t Ffs_blob_start Toff_t Ffs_blob_size Tsize_t Ffs_orig_fd int32 } type fsupplement_t = Tfsupplement_t type Tfsupplement = Tfsupplement_t type fsupplement = Tfsupplement type Tfchecklv_t = struct { Flv_file_start Toff_t Flv_error_message_size Tsize_t Flv_error_message uintptr } type fchecklv_t = Tfchecklv_t type Tfchecklv = Tfchecklv_t type fchecklv = Tfchecklv type Tfgetsigsinfo_t = struct { Ffg_file_start Toff_t Ffg_info_request int32 Ffg_sig_is_platform int32 } type fgetsigsinfo_t = Tfgetsigsinfo_t type Tfgetsigsinfo = Tfgetsigsinfo_t type fgetsigsinfo = Tfgetsigsinfo type Tfstore_t = struct { Ffst_flags uint32 Ffst_posmode int32 Ffst_offset Toff_t Ffst_length Toff_t Ffst_bytesalloc Toff_t } type fstore_t = Tfstore_t type Tfstore = Tfstore_t type fstore = Tfstore type Tfpunchhole_t = struct { Ffp_flags uint32 Freserved uint32 Ffp_offset Toff_t Ffp_length Toff_t } type fpunchhole_t = Tfpunchhole_t type Tfpunchhole = Tfpunchhole_t type fpunchhole = Tfpunchhole type Tftrimactivefile_t = struct { Ffta_offset Toff_t Ffta_length Toff_t } type ftrimactivefile_t = Tftrimactivefile_t type Tftrimactivefile = Tftrimactivefile_t type ftrimactivefile = Tftrimactivefile type Tfspecread_t = struct { Ffsr_flags uint32 Freserved uint32 Ffsr_offset Toff_t Ffsr_length Toff_t } type fspecread_t = Tfspecread_t type Tfspecread = Tfspecread_t type fspecread = Tfspecread type Tfattributiontag_t = struct { Fft_flags uint32 Fft_hash uint64 Fft_attribution_name [255]int8 } type fattributiontag_t = Tfattributiontag_t type Tfattributiontag = Tfattributiontag_t type fattributiontag = Tfattributiontag type Tlog2phys = struct { Fl2p_flags uint32 Fl2p_contigbytes Toff_t Fl2p_devoffset Toff_t } type log2phys = Tlog2phys type Tfilesec_property_t = int32 type filesec_property_t = Tfilesec_property_t const _FILESEC_OWNER = 1 const _FILESEC_GROUP = 2 const _FILESEC_UUID = 3 const _FILESEC_MODE = 4 const _FILESEC_ACL = 5 const _FILESEC_GRPUUID = 6 const _FILESEC_ACL_RAW = 100 const _FILESEC_ACL_ALLOCSIZE = 101 type Twinsize = struct { Fws_row uint16 Fws_col uint16 Fws_xpixel uint16 Fws_ypixel uint16 } type winsize = Twinsize type Tttysize = struct { Fts_lines uint16 Fts_cols uint16 Fts_xxx uint16 Fts_yyy uint16 } type ttysize = Tttysize type Taccessx_descriptor = struct { Fad_name_offset uint32 Fad_flags int32 Fad_pad [2]int32 } type accessx_descriptor = Taccessx_descriptor type Tuuid_t = [16]uint8 type uuid_t = Tuuid_t type Tfssearchblock = struct { Freturnattrs uintptr Freturnbuffer uintptr Freturnbuffersize Tsize_t Fmaxmatches Tu_long Ftimelimit Ttimeval Fsearchparams1 uintptr Fsizeofsearchparams1 Tsize_t Fsearchparams2 uintptr Fsizeofsearchparams2 Tsize_t Fsearchattrs Tattrlist } type fssearchblock = Tfssearchblock type Tsearchstate = struct { Fss_union_flags Tuint32_t Fss_union_layer Tuint32_t Fss_fsstate [548]Tu_char } type searchstate = Tsearchstate type Ttimeval64 = struct { Ftv_sec t__int64_t Ftv_usec t__int64_t } type timeval64 = Ttimeval64 type Titimerval = struct { Fit_interval Ttimeval Fit_value Ttimeval } type itimerval = Titimerval type Ttimezone = struct { Ftz_minuteswest int32 Ftz_dsttime int32 } type timezone = Ttimezone type Tclockinfo = struct { Fhz int32 Ftick int32 Ftickadj int32 Fstathz int32 Fprofhz int32 } type clockinfo = Tclockinfo type Tkauth_cred_t = uintptr type kauth_cred_t = Tkauth_cred_t type Tposix_cred_t = uintptr type posix_cred_t = Tposix_cred_t type Tau_id_t = uint32 type au_id_t = Tau_id_t type Tau_asid_t = int32 type au_asid_t = Tau_asid_t type Tau_event_t = uint16 type au_event_t = Tau_event_t type Tau_emod_t = uint16 type au_emod_t = Tau_emod_t type Tau_class_t = uint32 type au_class_t = Tau_class_t type Tau_asflgs_t = uint64 type au_asflgs_t = Tau_asflgs_t type Tau_ctlmode_t = uint8 type au_ctlmode_t = Tau_ctlmode_t type Tau_tid = struct { Fport Tdev_t Fmachine Tu_int32_t } type au_tid = Tau_tid type Tau_tid_t = struct { Fport Tdev_t Fmachine Tu_int32_t } type au_tid_t = Tau_tid_t type Tau_tid_addr = struct { Fat_port Tdev_t Fat_type Tu_int32_t Fat_addr [4]Tu_int32_t } type au_tid_addr = Tau_tid_addr type Tau_tid_addr_t = struct { Fat_port Tdev_t Fat_type Tu_int32_t Fat_addr [4]Tu_int32_t } type au_tid_addr_t = Tau_tid_addr_t type Tau_mask = struct { Fam_success uint32 Fam_failure uint32 } type au_mask = Tau_mask type Tau_mask_t = struct { Fam_success uint32 Fam_failure uint32 } type au_mask_t = Tau_mask_t type Tauditinfo = struct { Fai_auid Tau_id_t Fai_mask Tau_mask_t Fai_termid Tau_tid_t Fai_asid Tau_asid_t } type auditinfo = Tauditinfo type Tauditinfo_t = struct { Fai_auid Tau_id_t Fai_mask Tau_mask_t Fai_termid Tau_tid_t Fai_asid Tau_asid_t } type auditinfo_t = Tauditinfo_t type Tauditinfo_addr = struct { Fai_auid Tau_id_t Fai_mask Tau_mask_t Fai_termid Tau_tid_addr_t Fai_asid Tau_asid_t Fai_flags Tau_asflgs_t } type auditinfo_addr = Tauditinfo_addr type Tauditinfo_addr_t = struct { Fai_auid Tau_id_t Fai_mask Tau_mask_t Fai_termid Tau_tid_addr_t Fai_asid Tau_asid_t Fai_flags Tau_asflgs_t } type auditinfo_addr_t = Tauditinfo_addr_t type Tauditpinfo = struct { Fap_pid Tpid_t Fap_auid Tau_id_t Fap_mask Tau_mask_t Fap_termid Tau_tid_t Fap_asid Tau_asid_t } type auditpinfo = Tauditpinfo type Tauditpinfo_t = struct { Fap_pid Tpid_t Fap_auid Tau_id_t Fap_mask Tau_mask_t Fap_termid Tau_tid_t Fap_asid Tau_asid_t } type auditpinfo_t = Tauditpinfo_t type Tauditpinfo_addr = struct { Fap_pid Tpid_t Fap_auid Tau_id_t Fap_mask Tau_mask_t Fap_termid Tau_tid_addr_t Fap_asid Tau_asid_t Fap_flags Tau_asflgs_t } type auditpinfo_addr = Tauditpinfo_addr type Tauditpinfo_addr_t = struct { Fap_pid Tpid_t Fap_auid Tau_id_t Fap_mask Tau_mask_t Fap_termid Tau_tid_addr_t Fap_asid Tau_asid_t Fap_flags Tau_asflgs_t } type auditpinfo_addr_t = Tauditpinfo_addr_t type Tau_session = struct { Fas_aia_p uintptr Fas_mask Tau_mask_t } type au_session = Tau_session type Tau_session_t = struct { Fas_aia_p uintptr Fas_mask Tau_mask_t } type au_session_t = Tau_session_t type Tau_expire_after = struct { Fage Ttime_t Fsize Tsize_t Fop_type uint8 } type au_expire_after = Tau_expire_after type Tau_expire_after_t = struct { Fage Ttime_t Fsize Tsize_t Fop_type uint8 } type au_expire_after_t = Tau_expire_after_t type Tau_qctrl = struct { Faq_hiwater int32 Faq_lowater int32 Faq_bufsz int32 Faq_delay int32 Faq_minfree int32 } type au_qctrl = Tau_qctrl type Tau_qctrl_t = struct { Faq_hiwater int32 Faq_lowater int32 Faq_bufsz int32 Faq_delay int32 Faq_minfree int32 } type au_qctrl_t = Tau_qctrl_t type Taudit_stat = struct { Fas_version uint32 Fas_numevent uint32 Fas_generated int32 Fas_nonattrib int32 Fas_kernel int32 Fas_audit int32 Fas_auditctl int32 Fas_enqueue int32 Fas_written int32 Fas_wblocked int32 Fas_rblocked int32 Fas_dropped int32 Fas_totalsize int32 Fas_memused uint32 } type audit_stat = Taudit_stat type Tau_stat_t = struct { Fas_version uint32 Fas_numevent uint32 Fas_generated int32 Fas_nonattrib int32 Fas_kernel int32 Fas_audit int32 Fas_auditctl int32 Fas_enqueue int32 Fas_written int32 Fas_wblocked int32 Fas_rblocked int32 Fas_dropped int32 Fas_totalsize int32 Fas_memused uint32 } type au_stat_t = Tau_stat_t type Taudit_fstat = struct { Faf_filesz Tu_int64_t Faf_currsz Tu_int64_t } type audit_fstat = Taudit_fstat type Tau_fstat_t = struct { Faf_filesz Tu_int64_t Faf_currsz Tu_int64_t } type au_fstat_t = Tau_fstat_t type Tau_evclass_map = struct { Fec_number Tau_event_t Fec_class Tau_class_t } type au_evclass_map = Tau_evclass_map type Tau_evclass_map_t = struct { Fec_number Tau_event_t Fec_class Tau_class_t } type au_evclass_map_t = Tau_evclass_map_t type _audit_session_flags = int32 const _AU_SESSION_FLAG_IS_INITIAL = 1 const _AU_SESSION_FLAG_HAS_GRAPHIC_ACCESS = 16 const _AU_SESSION_FLAG_HAS_TTY = 32 const _AU_SESSION_FLAG_IS_REMOTE = 4096 const _AU_SESSION_FLAG_HAS_CONSOLE_ACCESS = 8192 const _AU_SESSION_FLAG_HAS_AUTHENTICATED = 16384 type Tboolean_t = int32 type boolean_t = Tboolean_t type Tnatural_t = uint32 type natural_t = Tnatural_t type Tinteger_t = int32 type integer_t = Tinteger_t type Tvm_offset_t = uint64 type vm_offset_t = Tvm_offset_t type Tvm_size_t = uint64 type vm_size_t = Tvm_size_t type Tmach_vm_address_t = uint64 type mach_vm_address_t = Tmach_vm_address_t type Tmach_vm_offset_t = uint64 type mach_vm_offset_t = Tmach_vm_offset_t type Tmach_vm_size_t = uint64 type mach_vm_size_t = Tmach_vm_size_t type Tvm_map_offset_t = uint64 type vm_map_offset_t = Tvm_map_offset_t type Tvm_map_address_t = uint64 type vm_map_address_t = Tvm_map_address_t type Tvm_map_size_t = uint64 type vm_map_size_t = Tvm_map_size_t type Tvm32_offset_t = uint32 type vm32_offset_t = Tvm32_offset_t type Tvm32_address_t = uint32 type vm32_address_t = Tvm32_address_t type Tvm32_size_t = uint32 type vm32_size_t = Tvm32_size_t type Tmach_port_context_t = uint64 type mach_port_context_t = Tmach_port_context_t type Tmach_port_name_t = uint32 type mach_port_name_t = Tmach_port_name_t type Tmach_port_name_array_t = uintptr type mach_port_name_array_t = Tmach_port_name_array_t type Tmach_port_t = uint32 type mach_port_t = Tmach_port_t type Tmach_port_array_t = uintptr type mach_port_array_t = Tmach_port_array_t type Tmach_port_right_t = uint32 type mach_port_right_t = Tmach_port_right_t type Tmach_port_type_t = uint32 type mach_port_type_t = Tmach_port_type_t type Tmach_port_type_array_t = uintptr type mach_port_type_array_t = Tmach_port_type_array_t type Tmach_port_urefs_t = uint32 type mach_port_urefs_t = Tmach_port_urefs_t type Tmach_port_delta_t = int32 type mach_port_delta_t = Tmach_port_delta_t type Tmach_port_seqno_t = uint32 type mach_port_seqno_t = Tmach_port_seqno_t type Tmach_port_mscount_t = uint32 type mach_port_mscount_t = Tmach_port_mscount_t type Tmach_port_msgcount_t = uint32 type mach_port_msgcount_t = Tmach_port_msgcount_t type Tmach_port_rights_t = uint32 type mach_port_rights_t = Tmach_port_rights_t type Tmach_port_srights_t = uint32 type mach_port_srights_t = Tmach_port_srights_t type Tmach_port_status_t = struct { Fmps_pset Tmach_port_rights_t Fmps_seqno Tmach_port_seqno_t Fmps_mscount Tmach_port_mscount_t Fmps_qlimit Tmach_port_msgcount_t Fmps_msgcount Tmach_port_msgcount_t Fmps_sorights Tmach_port_rights_t Fmps_srights Tboolean_t Fmps_pdrequest Tboolean_t Fmps_nsrequest Tboolean_t Fmps_flags Tnatural_t } type mach_port_status_t = Tmach_port_status_t type Tmach_port_status = Tmach_port_status_t type mach_port_status = Tmach_port_status type Tmach_port_limits_t = struct { Fmpl_qlimit Tmach_port_msgcount_t } type mach_port_limits_t = Tmach_port_limits_t type Tmach_port_limits = Tmach_port_limits_t type mach_port_limits = Tmach_port_limits type Tmach_port_info_ext_t = struct { Fmpie_status Tmach_port_status_t Fmpie_boost_cnt Tmach_port_msgcount_t Freserved [6]Tuint32_t } type mach_port_info_ext_t = Tmach_port_info_ext_t type Tmach_port_info_ext = Tmach_port_info_ext_t type mach_port_info_ext = Tmach_port_info_ext type Tmach_port_guard_info_t = struct { Fmpgi_guard Tuint64_t } type mach_port_guard_info_t = Tmach_port_guard_info_t type Tmach_port_guard_info = Tmach_port_guard_info_t type mach_port_guard_info = Tmach_port_guard_info type Tmach_port_info_t = uintptr type mach_port_info_t = Tmach_port_info_t type Tmach_port_flavor_t = int32 type mach_port_flavor_t = Tmach_port_flavor_t type Tmach_port_qos_t = struct { F__ccgo0 uint32 Flen1 Tnatural_t } type mach_port_qos_t = Tmach_port_qos_t type Tmach_port_qos = Tmach_port_qos_t type mach_port_qos = Tmach_port_qos type Tmach_service_port_info_data_t = struct { Fmspi_string_name [255]int8 Fmspi_domain_type Tuint8_t } type mach_service_port_info_data_t = Tmach_service_port_info_data_t type Tmach_service_port_info = Tmach_service_port_info_data_t type mach_service_port_info = Tmach_service_port_info type Tmach_service_port_info_t = uintptr type mach_service_port_info_t = Tmach_service_port_info_t type Tmach_port_options_t = struct { Fflags Tuint32_t Fmpl Tmach_port_limits_t F__ccgo2_8 struct { Fwork_interval_port [0]Tmach_port_name_t Fservice_port_info [0]Tmach_service_port_info_t Fservice_port_name [0]Tmach_port_name_t Freserved [2]Tuint64_t } } type mach_port_options_t = Tmach_port_options_t type Tmach_port_options = Tmach_port_options_t type mach_port_options = Tmach_port_options type Tmach_port_options_ptr_t = uintptr type mach_port_options_ptr_t = Tmach_port_options_ptr_t type _mach_port_guard_exception_codes = int32 const _kGUARD_EXC_DESTROY = 1 const _kGUARD_EXC_MOD_REFS = 2 const _kGUARD_EXC_INVALID_OPTIONS = 3 const _kGUARD_EXC_SET_CONTEXT = 4 const _kGUARD_EXC_THREAD_SET_STATE = 5 const _kGUARD_EXC_EXCEPTION_BEHAVIOR_ENFORCE = 6 const _kGUARD_EXC_UNGUARDED = 8 const _kGUARD_EXC_INCORRECT_GUARD = 16 const _kGUARD_EXC_IMMOVABLE = 32 const _kGUARD_EXC_STRICT_REPLY = 64 const _kGUARD_EXC_MSG_FILTERED = 128 const _kGUARD_EXC_INVALID_RIGHT = 256 const _kGUARD_EXC_INVALID_NAME = 512 const _kGUARD_EXC_INVALID_VALUE = 1024 const _kGUARD_EXC_INVALID_ARGUMENT = 2048 const _kGUARD_EXC_RIGHT_EXISTS = 4096 const _kGUARD_EXC_KERN_NO_SPACE = 8192 const _kGUARD_EXC_KERN_FAILURE = 16384 const _kGUARD_EXC_KERN_RESOURCE = 32768 const _kGUARD_EXC_SEND_INVALID_REPLY = 65536 const _kGUARD_EXC_SEND_INVALID_VOUCHER = 131072 const _kGUARD_EXC_SEND_INVALID_RIGHT = 262144 const _kGUARD_EXC_RCV_INVALID_NAME = 524288 const _kGUARD_EXC_RCV_GUARDED_DESC = 1048576 const _kGUARD_EXC_MOD_REFS_NON_FATAL = 2097152 const _kGUARD_EXC_IMMOVABLE_NON_FATAL = 4194304 const _kGUARD_EXC_REQUIRE_REPLY_PORT_SEMANTICS = 8388608 type Txucred = struct { Fcr_version Tu_int Fcr_uid Tuid_t Fcr_ngroups int16 Fcr_groups [16]Tgid_t } type xucred = Txucred type Ttext_encoding_t = uint32 type text_encoding_t = Ttext_encoding_t type Tfsobj_type_t = uint32 type fsobj_type_t = Tfsobj_type_t type Tfsobj_tag_t = uint32 type fsobj_tag_t = Tfsobj_tag_t type Tfsfile_type_t = uint32 type fsfile_type_t = Tfsfile_type_t type Tfsvolid_t = uint32 type fsvolid_t = Tfsvolid_t type Tfsobj_id_t = struct { Ffid_objno Tu_int32_t Ffid_generation Tu_int32_t } type fsobj_id_t = Tfsobj_id_t type Tfsobj_id = Tfsobj_id_t type fsobj_id = Tfsobj_id type Tattrgroup_t = uint32 type attrgroup_t = Tattrgroup_t type Tattrlist = struct { Fbitmapcount Tu_short Freserved Tu_int16_t Fcommonattr Tattrgroup_t Fvolattr Tattrgroup_t Fdirattr Tattrgroup_t Ffileattr Tattrgroup_t Fforkattr Tattrgroup_t } type attrlist = Tattrlist type Tattribute_set_t = struct { Fcommonattr Tattrgroup_t Fvolattr Tattrgroup_t Fdirattr Tattrgroup_t Ffileattr Tattrgroup_t Fforkattr Tattrgroup_t } type attribute_set_t = Tattribute_set_t type Tattribute_set = Tattribute_set_t type attribute_set = Tattribute_set type Tattrreference_t = struct { Fattr_dataoffset Tint32_t Fattr_length Tu_int32_t } type attrreference_t = Tattrreference_t type Tattrreference = Tattrreference_t type attrreference = Tattrreference type Tdiskextent = struct { Fstartblock Tu_int32_t Fblockcount Tu_int32_t } type diskextent = Tdiskextent type Textentrecord = [8]Tdiskextent type extentrecord = Textentrecord type Tvol_capabilities_set_t = [4]Tu_int32_t type vol_capabilities_set_t = Tvol_capabilities_set_t type Tvol_capabilities_attr_t = struct { Fcapabilities Tvol_capabilities_set_t Fvalid Tvol_capabilities_set_t } type vol_capabilities_attr_t = Tvol_capabilities_attr_t type Tvol_capabilities_attr = Tvol_capabilities_attr_t type vol_capabilities_attr = Tvol_capabilities_attr type Tvol_attributes_attr_t = struct { Fvalidattr Tattribute_set_t Fnativeattr Tattribute_set_t } type vol_attributes_attr_t = Tvol_attributes_attr_t type Tvol_attributes_attr = Tvol_attributes_attr_t type vol_attributes_attr = Tvol_attributes_attr type Tos_function_t = uintptr type os_function_t = Tos_function_t type Tos_block_t = uintptr type os_block_t = Tos_block_t type Tfsid_t = struct { Fval [2]Tint32_t } type fsid_t = Tfsid_t type Tfsid = Tfsid_t type fsid = Tfsid type Tsecure_boot_cryptex_args_t = struct { Fsbc_version Tu_int32_t Fsbc_4cc Tu_int32_t Fsbc_authentic_manifest_fd int32 Fsbc_user_manifest_fd int32 Fsbc_payload_fd int32 Fsbc_flags Tu_int64_t } type secure_boot_cryptex_args_t = Tsecure_boot_cryptex_args_t type Tsecure_boot_cryptex_args = Tsecure_boot_cryptex_args_t type secure_boot_cryptex_args = Tsecure_boot_cryptex_args type Tgraftdmg_args_un = struct { Fsbc_args [0]Tsecure_boot_cryptex_args_t Fmax_size [512]Tu_int8_t } type graftdmg_args_un = Tgraftdmg_args_un type Tgraft_args = Tgraftdmg_args_un type graft_args = Tgraft_args type Tmount_t = uintptr type mount_t = Tmount_t type Tvnode_t = uintptr type vnode_t = Tvnode_t type Tstatfs = struct { Ff_bsize Tuint32_t Ff_iosize Tint32_t Ff_blocks Tuint64_t Ff_bfree Tuint64_t Ff_bavail Tuint64_t Ff_files Tuint64_t Ff_ffree Tuint64_t Ff_fsid Tfsid_t Ff_owner Tuid_t Ff_type Tuint32_t Ff_flags Tuint32_t Ff_fssubtype Tuint32_t Ff_fstypename [16]int8 Ff_mntonname [1024]int8 Ff_mntfromname [1024]int8 Ff_flags_ext Tuint32_t Ff_reserved [7]Tuint32_t } type statfs = Tstatfs type Tvfsstatfs = struct { Ff_bsize Tuint32_t Ff_iosize Tsize_t Ff_blocks Tuint64_t Ff_bfree Tuint64_t Ff_bavail Tuint64_t Ff_bused Tuint64_t Ff_files Tuint64_t Ff_ffree Tuint64_t Ff_fsid Tfsid_t Ff_owner Tuid_t Ff_flags Tuint64_t Ff_fstypename [16]int8 Ff_mntonname [1024]int8 Ff_mntfromname [1024]int8 Ff_fssubtype Tuint32_t Ff_reserved [2]uintptr } type vfsstatfs = Tvfsstatfs type Tvfsconf = struct { Fvfc_reserved1 Tuint32_t Fvfc_name [15]int8 Fvfc_typenum int32 Fvfc_refcount int32 Fvfc_flags int32 Fvfc_reserved2 Tuint32_t Fvfc_reserved3 Tuint32_t } type vfsconf = Tvfsconf type Tvfsidctl = struct { Fvc_vers int32 Fvc_fsid Tfsid_t Fvc_ptr uintptr Fvc_len Tsize_t Fvc_spare [12]Tu_int32_t } type vfsidctl = Tvfsidctl type Tvfsquery = struct { Fvq_flags Tu_int32_t Fvq_spare [31]Tu_int32_t } type vfsquery = Tvfsquery type Tvfs_server = struct { Fvs_minutes Tint32_t Fvs_server_name [768]Tu_int8_t } type vfs_server = Tvfs_server type Tnetfs_status = struct { F__ccgo_align [0]uint64 Fns_status Tu_int32_t Fns_mountopts [512]int8 Fns_waittime Tuint32_t Fns_threadcount Tuint32_t } type netfs_status = Tnetfs_status type Tfhandle = struct { Ffh_len uint32 Ffh_data [128]uint8 } type fhandle = Tfhandle type Tfhandle_t = struct { Ffh_len uint32 Ffh_data [128]uint8 } type fhandle_t = Tfhandle_t type Tgraftdmg_type_t = uint32 type graftdmg_type_t = Tgraftdmg_type_t const _GRAFTDMG_CRYPTEX_BOOT = 1 const _GRAFTDMG_CRYPTEX_PREBOOT = 2 const _GRAFTDMG_CRYPTEX_DOWNLEVEL = 3 const _GRAFTDMG_CRYPTEX_PDI_NONCE = 6 const _GRAFTDMG_CRYPTEX_EFFECTIVE_AP = 7 const _GRAFTDMG_CRYPTEX_MAX = 7 type Tcryptex_auth_type_t = uint32 type cryptex_auth_type_t = Tcryptex_auth_type_t const _CRYPTEX1_AUTH_ENV_GENERIC = 4 const _CRYPTEX1_AUTH_ENV_GENERIC_SUPPLEMENTAL = 5 const _CRYPTEX_AUTH_PDI_NONCE = 6 const _CRYPTEX_AUTH_MAX = 7 /* ** 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 { FfileId TunixFileId FpLockMutex uintptr FnShared int32 FnLock int32 FeFileLock uint8 FbProcessLock uint8 FpUnused uintptr FnRef int32 FpShmNode uintptr FpNext uintptr FpPrev uintptr FsharedByte uint64 } 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 { 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 FopenFlags int32 FfsFlags uint32 } type unixFile = TunixFile type TunixFile1 = struct { 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 FopenFlags int32 FfsFlags uint32 } 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. */ // 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 + 3459, }, 1: { FzName: __ccgo_ts + 3464, }, 2: { FzName: __ccgo_ts + 3470, }, 3: { FzName: __ccgo_ts + 3477, }, 4: { FzName: __ccgo_ts + 3484, }, 5: { FzName: __ccgo_ts + 3489, }, 6: { FzName: __ccgo_ts + 3495, }, 7: { FzName: __ccgo_ts + 3505, }, 8: { FzName: __ccgo_ts + 3511, }, 9: { FzName: __ccgo_ts + 3516, }, 10: { FzName: __ccgo_ts + 3522, }, 11: { FzName: __ccgo_ts + 3530, }, 12: { FzName: __ccgo_ts + 3536, }, 13: { FzName: __ccgo_ts + 3543, }, 14: { FzName: __ccgo_ts + 3552, }, 15: { FzName: __ccgo_ts + 3559, }, 16: { FzName: __ccgo_ts + 3569, }, 17: { FzName: __ccgo_ts + 3576, }, 18: { FzName: __ccgo_ts + 3590, }, 19: { FzName: __ccgo_ts + 3596, }, 20: { FzName: __ccgo_ts + 3602, }, 21: { FzName: __ccgo_ts + 3609, }, 22: { FzName: __ccgo_ts + 3617, }, 23: { FzName: __ccgo_ts + 3622, }, 24: { FzName: __ccgo_ts + 3629, }, 25: { FzName: __ccgo_ts + 3636, }, 26: { FzName: __ccgo_ts + 3648, }, 27: { FzName: __ccgo_ts + 3657, }, 28: { FzName: __ccgo_ts + 3663, }, } func init() { p := unsafe.Pointer(&_aSyscall) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_posixOpen) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(libc.Xclose) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(libc.Xaccess) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(libc.Xgetcwd) *(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(libc.Xstat) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(libc.Xfstat) *(*uintptr)(unsafe.Add(p, 152)) = __ccgo_fp(libc.Xftruncate) *(*uintptr)(unsafe.Add(p, 176)) = __ccgo_fp(libc.Xfcntl) *(*uintptr)(unsafe.Add(p, 200)) = __ccgo_fp(libc.Xread) *(*uintptr)(unsafe.Add(p, 224)) = __ccgo_fp(libc.Xpread) *(*uintptr)(unsafe.Add(p, 272)) = __ccgo_fp(libc.Xwrite) *(*uintptr)(unsafe.Add(p, 296)) = __ccgo_fp(libc.Xpwrite) *(*uintptr)(unsafe.Add(p, 344)) = __ccgo_fp(libc.Xfchmod) *(*uintptr)(unsafe.Add(p, 392)) = __ccgo_fp(libc.Xunlink) *(*uintptr)(unsafe.Add(p, 416)) = __ccgo_fp(_openDirectory) *(*uintptr)(unsafe.Add(p, 440)) = __ccgo_fp(libc.Xmkdir) *(*uintptr)(unsafe.Add(p, 464)) = __ccgo_fp(libc.Xrmdir) *(*uintptr)(unsafe.Add(p, 488)) = __ccgo_fp(libc.Xfchown) *(*uintptr)(unsafe.Add(p, 512)) = __ccgo_fp(libc.Xgeteuid) *(*uintptr)(unsafe.Add(p, 536)) = __ccgo_fp(libc.Xmmap) *(*uintptr)(unsafe.Add(p, 560)) = __ccgo_fp(libc.Xmunmap) *(*uintptr)(unsafe.Add(p, 608)) = __ccgo_fp(_unixGetpagesize) *(*uintptr)(unsafe.Add(p, 632)) = __ccgo_fp(libc.Xreadlink) *(*uintptr)(unsafe.Add(p, 656)) = __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 !(uint64(i) < libc.Uint64FromInt64(696)/libc.Uint64FromInt64(24)) { 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 !(uint64(i) < libc.Uint64FromInt64(696)/libc.Uint64FromInt64(24)) { 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 !(uint64(i) < libc.Uint64FromInt64(696)/libc.Uint64FromInt64(24)) { 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.Uint64FromInt64(696)/libc.Uint64FromInt64(24))-libc.Int32FromInt32(1)) { break } if libc.Xstrcmp(tls, zName, _aSyscall[i].FzName) == 0 { break } goto _1 _1: ; i++ } } i++ for { if !(i < int32(libc.Uint64FromInt64(696)/libc.Uint64FromInt64(24))) { 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(176) defer tls.Free(176) var fd, v1 int32 var m2 Tmode_t var _ /* statbuf at bp+0 */ Tstat _, _, _ = fd, m2, v1 if m != 0 { v1 = int32(m) } else { v1 = int32(SQLITE_DEFAULT_FILE_PERMISSIONS) } m2 = uint16(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__error(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+3669, libc.VaList(bp+152, z, fd)) fd = -int32(1) if (*(*func(*libc.TLS, uintptr, int32, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[0].FpCurrent})))(tls, __ccgo_ts+3712, O_RDONLY, int32(m)) < 0 { break } } if fd >= 0 { if int32(m) != 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 && int32((*(*Tstat)(unsafe.Pointer(bp))).Fst_mode)&int32(0777) != int32(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) { /* Not a recursive mutex */ Xsqlite3_mutex_enter(tls, _unixBigLock) } func _unixLeaveMutex(tls *libc.TLS) { 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__error(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) { 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 { 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 { FfileId TunixFileId FpLockMutex uintptr FnShared int32 FnLock int32 FeFileLock uint8 FbProcessLock uint8 FpUnused uintptr FnRef int32 FpShmNode uintptr FpNext uintptr FpPrev uintptr FsharedByte uint64 } 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__error(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 + 1664 if zPath == uintptr(0) { zPath = __ccgo_ts + 1664 } Xsqlite3_log(tls, errcode, __ccgo_ts+3722, 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 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(39721)) 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 if pInode != 0 { (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnRef-- if (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnRef == 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 { (*TunixInodeInfo)(unsafe.Pointer((*TunixInodeInfo)(unsafe.Pointer(pInode)).FpPrev)).FpNext = (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpNext } else { _inodeList = (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpNext } if (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpNext != 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(160) defer tls.Free(160) 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 */ /* 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__error(tls)))) return int32(SQLITE_IOERR) } /* On OS X on an msdos filesystem, the inode number is reported ** incorrectly for zero-size files. See ticket #3260. To work ** around this problem (we consider it a bug in OS X, not SQLite) ** we always increase the file size to 1 by writing a single byte ** prior to accessing the inode number. The one byte written is ** an ASCII 'S' character which also happens to be the first byte ** in the header of every SQLite database. In this way, if there ** is a race condition such that another thread has already populated ** the first page of the database, no damage is done. */ if (*(*Tstat)(unsafe.Pointer(bp + 16))).Fst_size == 0 && (*TunixFile)(unsafe.Pointer(pFile)).FfsFlags&uint32(SQLITE_FSFLAGS_IS_MSDOS) != uint32(0) { for cond := true; cond; cond = rc < 0 && *(*int32)(unsafe.Pointer(libc.X__error(tls))) == int32(EINTR) { rc = int32((*(*func(*libc.TLS, int32, uintptr, Tsize_t) Tssize_t)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(11)].FpCurrent})))(tls, fd, __ccgo_ts+3753, uint64(1))) } if rc != int32(1) { _storeLastErrno(tls, pFile, *(*int32)(unsafe.Pointer(libc.X__error(tls)))) return int32(SQLITE_IOERR) } 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__error(tls)))) return int32(SQLITE_IOERR) } } libc.X__builtin___memset_chk(tls, bp, 0, uint64(16), libc.X__builtin_object_size(tls, bp, 0)) (*(*TunixFileId)(unsafe.Pointer(bp))).Fdev = (*(*Tstat)(unsafe.Pointer(bp + 16))).Fst_dev (*(*TunixFileId)(unsafe.Pointer(bp))).Fino = (*(*Tstat)(unsafe.Pointer(bp + 16))).Fst_ino pInode = _inodeList for pInode != 0 && libc.Xmemcmp(tls, bp, pInode, uint64(16)) != 0 { pInode = (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpNext } if pInode == uintptr(0) { pInode = Xsqlite3_malloc64(tls, uint64(88)) if pInode == uintptr(0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pInode, 0, uint64(88), libc.X__builtin_object_size(tls, pInode, 0)) libc.X__builtin___memcpy_chk(tls, pInode, bp, uint64(16), libc.X__builtin_object_size(tls, pInode, 0)) 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) (*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(144) defer tls.Free(144) 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(160) defer tls.Free(160) 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+3755, libc.VaList(bp+152, (*TunixFile)(unsafe.Pointer(pFile)).FzPath)) return } if int32((*(*Tstat)(unsafe.Pointer(bp))).Fst_nlink) == 0 { Xsqlite3_log(tls, int32(SQLITE_WARNING), __ccgo_ts+3779, libc.VaList(bp+152, (*TunixFile)(unsafe.Pointer(pFile)).FzPath)) return } if int32((*(*Tstat)(unsafe.Pointer(bp))).Fst_nlink) > int32(1) { Xsqlite3_log(tls, int32(SQLITE_WARNING), __ccgo_ts+3808, libc.VaList(bp+152, (*TunixFile)(unsafe.Pointer(pFile)).FzPath)) return } if _fileHasMoved(tls, pFile) != 0 { Xsqlite3_log(tls, int32(SQLITE_WARNING), __ccgo_ts+3835, libc.VaList(bp+152, (*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 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_GETLK), libc.VaList(bp+32, 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 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 { (*(*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_SETLK), libc.VaList(bp+32, 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_SETLK), libc.VaList(bp+32, 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 /* 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. */ /* 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)) { (*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 = int32(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__error(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. */ (*(*Tflock)(unsafe.Pointer(bp))).Fl_type = int16(F_WRLCK) 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__error(tls))) rc = _sqliteErrorFromPosixError(tls, tErrno, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(15)< int32(SHARED_LOCK) { /* 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) { if handleNFSUnlock != 0 { /* Error code from system call errors */ divSize = int64(libc.Int32FromInt32(SHARED_SIZE) - libc.Int32FromInt32(1)) (*(*Tflock)(unsafe.Pointer(bp))).Fl_type = int16(F_UNLCK) (*(*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 = divSize if _unixFileLock(tls, pFile, bp) == -int32(1) { tErrno = *(*int32)(unsafe.Pointer(libc.X__error(tls))) rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(8)<= 0 { _robust_close(tls, pFile, (*TunixFile)(unsafe.Pointer(pFile)).Fh, int32(40505)) (*TunixFile)(unsafe.Pointer(pFile)).Fh = -int32(1) } Xsqlite3_free(tls, (*TunixFile)(unsafe.Pointer(pFile)).FpPreallocatedUnused) libc.X__builtin___memset_chk(tls, pFile, 0, uint64(128), libc.X__builtin_object_size(tls, pFile, 0)) 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 _verifyDbFile(tls, pFile) _unixUnlock(tls, id, NO_LOCK) _unixEnterMutex(tls) /* unixFile.pInode is always valid here. Otherwise, a different close ** routine (e.g. nolockClose()) would be called instead. */ 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) 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 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, uint16(0777)) if rc < 0 { /* failed to open/create the lock directory */ tErrno = *(*int32)(unsafe.Pointer(libc.X__error(tls))) if int32(EEXIST) == tErrno { rc = int32(SQLITE_BUSY) } else { rc = _sqliteErrorFromPosixError(tls, tErrno, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(15)< int32(SHARED_LOCK) { reserved = int32(1) } /* Otherwise see if some other process holds it. */ if !(reserved != 0) { /* attempt to get the lock */ lrc = _robust_flock(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, libc.Int32FromInt32(LOCK_EX)|libc.Int32FromInt32(LOCK_NB)) if !(lrc != 0) { /* got the lock, unlock it */ lrc = _robust_flock(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, int32(LOCK_UN)) if lrc != 0 { tErrno = *(*int32)(unsafe.Pointer(libc.X__error(tls))) /* unlock failed with an error */ lrc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(8)< SHARED // ** SHARED -> RESERVED // ** SHARED -> (PENDING) -> EXCLUSIVE // ** RESERVED -> (PENDING) -> EXCLUSIVE // ** PENDING -> EXCLUSIVE // ** // ** flock() only really support EXCLUSIVE locks. We track intermediate // ** lock states in the sqlite3_file structure, but all locks SHARED or // ** above are really EXCLUSIVE locks and exclude all other processes from // ** access the file. // ** // ** This routine will only increase a lock. Use the sqlite3OsUnlock() // ** routine to lower a locking level. // */ func _flockLock(tls *libc.TLS, id uintptr, eFileLock int32) (r int32) { var pFile uintptr var rc, tErrno int32 _, _, _ = pFile, rc, tErrno rc = SQLITE_OK pFile = id /* if we already have a lock, it is exclusive. ** Just adjust level and punt on outta here. */ if int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) > NO_LOCK { (*TunixFile)(unsafe.Pointer(pFile)).FeFileLock = uint8(eFileLock) return SQLITE_OK } /* grab an exclusive lock */ if _robust_flock(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, libc.Int32FromInt32(LOCK_EX)|libc.Int32FromInt32(LOCK_NB)) != 0 { tErrno = *(*int32)(unsafe.Pointer(libc.X__error(tls))) /* didn't get, must be busy */ rc = _sqliteErrorFromPosixError(tls, tErrno, libc.Int32FromInt32(SQLITE_IOERR)|libc.Int32FromInt32(15)< int32(SHARED_LOCK) { reserved = int32(1) } /* Otherwise see if some other process holds it. */ if !(reserved != 0) { /* lock the RESERVED byte */ lrc = _afpSetLock(tls, (*TafpLockingContext)(unsafe.Pointer(context)).FdbPath, pFile, uint64(_sqlite3PendingByte+libc.Int32FromInt32(1)), uint64(1), int32(1)) if SQLITE_OK == lrc { /* if we succeeded in taking the reserved lock, unlock it to restore ** the original state */ lrc = _afpSetLock(tls, (*TafpLockingContext)(unsafe.Pointer(context)).FdbPath, pFile, uint64(_sqlite3PendingByte+libc.Int32FromInt32(1)), uint64(1), 0) } else { /* if we failed to get the lock then someone else must have it */ reserved = int32(1) } if lrc != SQLITE_OK && lrc != int32(SQLITE_BUSY) { rc = lrc } } Xsqlite3_mutex_leave(tls, (*TunixInodeInfo)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FpInode)).FpLockMutex) *(*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. // */ func _afpLock(tls *libc.TLS, id uintptr, eFileLock int32) (r int32) { var context, pFile, pInode uintptr var failed, failed1, failed2, lrc1, lrc1Errno, lrc2, rc, v1, v2, v4 int32 var lk, mask int64 var v3 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = context, failed, failed1, failed2, lk, lrc1, lrc1Errno, lrc2, mask, pFile, pInode, rc, v1, v2, v3, v4 rc = SQLITE_OK pFile = id pInode = (*TunixFile)(unsafe.Pointer(pFile)).FpInode context = (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext /* If there is already a lock of this type or more restrictive on the ** unixFile, do nothing. Don't use the afp_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. */ /* 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 afp_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)) { (*TunixFile)(unsafe.Pointer(pFile)).FeFileLock = uint8(SHARED_LOCK) (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnShared++ (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnLock++ goto afp_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. */ if eFileLock == int32(SHARED_LOCK) || eFileLock == int32(EXCLUSIVE_LOCK) && int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) < int32(PENDING_LOCK) { failed = _afpSetLock(tls, (*TafpLockingContext)(unsafe.Pointer(context)).FdbPath, pFile, uint64(_sqlite3PendingByte), uint64(1), int32(1)) if failed != 0 { rc = failed goto afp_end_lock } } /* If control gets to this point, then actually go ahead and make ** operating system calls for the specified lock. */ if eFileLock == int32(SHARED_LOCK) { lrc1Errno = 0 mask = libc.Int64FromUint32(0xffffffff) | libc.Int64FromInt32(0x7fffffff)< 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. */ failed1 = 0 if eFileLock >= int32(RESERVED_LOCK) && int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) < int32(RESERVED_LOCK) { /* Acquire a RESERVED lock */ failed1 = _afpSetLock(tls, (*TafpLockingContext)(unsafe.Pointer(context)).FdbPath, pFile, uint64(_sqlite3PendingByte+libc.Int32FromInt32(1)), uint64(1), int32(1)) if !(failed1 != 0) { (*TafpLockingContext)(unsafe.Pointer(context)).Freserved = int32(1) } } if !(failed1 != 0) && eFileLock == int32(EXCLUSIVE_LOCK) { /* Acquire an EXCLUSIVE lock */ /* Remove the shared lock before trying the range. we'll need to ** reestablish the shared lock if we can't get the afpUnlock */ v1 = _afpSetLock(tls, (*TafpLockingContext)(unsafe.Pointer(context)).FdbPath, pFile, uint64(_sqlite3PendingByte+libc.Int32FromInt32(2))+(*TunixInodeInfo)(unsafe.Pointer(pInode)).FsharedByte, uint64(1), 0) failed1 = v1 if !(v1 != 0) { failed2 = SQLITE_OK /* now attempt to get the exclusive lock range */ failed1 = _afpSetLock(tls, (*TafpLockingContext)(unsafe.Pointer(context)).FdbPath, pFile, uint64(_sqlite3PendingByte+libc.Int32FromInt32(2)), uint64(SHARED_SIZE), int32(1)) if v3 = failed1 != 0; v3 { v2 = _afpSetLock(tls, (*TafpLockingContext)(unsafe.Pointer(context)).FdbPath, pFile, uint64(_sqlite3PendingByte+libc.Int32FromInt32(2))+(*TunixInodeInfo)(unsafe.Pointer(pInode)).FsharedByte, uint64(1), int32(1)) failed2 = v2 } if v3 && v2 != 0 { /* Can't reestablish the shared lock. Sqlite can't deal, this is ** a critical I/O error */ if failed1&int32(0xff) == int32(SQLITE_IOERR) { v4 = failed2 } else { v4 = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(15)< int32(SHARED_LOCK) { if int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) == int32(EXCLUSIVE_LOCK) { rc = _afpSetLock(tls, (*TafpLockingContext)(unsafe.Pointer(context)).FdbPath, pFile, uint64(_sqlite3PendingByte+libc.Int32FromInt32(2)), uint64(SHARED_SIZE), 0) if rc == SQLITE_OK && (eFileLock == int32(SHARED_LOCK) || (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnShared > int32(1)) { /* only re-establish the shared lock if necessary */ sharedLockByte = int32(uint64(_sqlite3PendingByte+libc.Int32FromInt32(2)) + (*TunixInodeInfo)(unsafe.Pointer(pInode)).FsharedByte) rc = _afpSetLock(tls, (*TafpLockingContext)(unsafe.Pointer(context)).FdbPath, pFile, uint64(sharedLockByte), uint64(1), int32(1)) } else { skipShared = int32(1) } } if rc == SQLITE_OK && int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) >= int32(PENDING_LOCK) { rc = _afpSetLock(tls, (*TafpLockingContext)(unsafe.Pointer(context)).FdbPath, pFile, uint64(_sqlite3PendingByte), uint64(1), 0) } if rc == SQLITE_OK && int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) >= int32(RESERVED_LOCK) && (*TafpLockingContext)(unsafe.Pointer(context)).Freserved != 0 { rc = _afpSetLock(tls, (*TafpLockingContext)(unsafe.Pointer(context)).FdbPath, pFile, uint64(_sqlite3PendingByte+libc.Int32FromInt32(1)), uint64(1), 0) if !(rc != 0) { (*TafpLockingContext)(unsafe.Pointer(context)).Freserved = 0 } } if rc == SQLITE_OK && (eFileLock == int32(SHARED_LOCK) || (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnShared > int32(1)) { (*TunixInodeInfo)(unsafe.Pointer(pInode)).FeFileLock = uint8(SHARED_LOCK) } } if rc == SQLITE_OK && eFileLock == NO_LOCK { /* Decrement the shared lock counter. Release the lock using an ** OS call only when all threads in this same process have released ** the lock. */ sharedLockByte1 = uint64(_sqlite3PendingByte+libc.Int32FromInt32(2)) + (*TunixInodeInfo)(unsafe.Pointer(pInode)).FsharedByte (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnShared-- if (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnShared == 0 { if !(skipShared != 0) { rc = _afpSetLock(tls, (*TafpLockingContext)(unsafe.Pointer(context)).FdbPath, pFile, sharedLockByte1, uint64(1), 0) } if !(rc != 0) { (*TunixInodeInfo)(unsafe.Pointer(pInode)).FeFileLock = uint8(NO_LOCK) (*TunixFile)(unsafe.Pointer(pFile)).FeFileLock = uint8(NO_LOCK) } } if rc == SQLITE_OK { (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnLock-- if (*TunixInodeInfo)(unsafe.Pointer(pInode)).FnLock == 0 { _closePendingFds(tls, pFile) } } } Xsqlite3_mutex_leave(tls, (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex) if rc == SQLITE_OK { (*TunixFile)(unsafe.Pointer(pFile)).FeFileLock = uint8(eFileLock) } return rc } // C documentation // // /* // ** Close a file & cleanup AFP specific locking context // */ func _afpClose(tls *libc.TLS, id uintptr) (r int32) { var pFile, pInode uintptr var rc int32 _, _, _ = pFile, pInode, rc rc = SQLITE_OK pFile = id _afpUnlock(tls, id, NO_LOCK) _unixEnterMutex(tls) if (*TunixFile)(unsafe.Pointer(pFile)).FpInode != 0 { pInode = (*TunixFile)(unsafe.Pointer(pFile)).FpInode 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->aPending. 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) Xsqlite3_free(tls, (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext) rc = _closeUnixFile(tls, id) _unixLeaveMutex(tls) return rc } /* ** The code above is the AFP lock implementation. The code is specific ** to MacOSX and does not work on other unix platforms. No alternative ** is available. If you don't compile for a mac, then the "unix-afp" ** VFS is not available. ** ********************* End of the AFP lock implementation ********************** ******************************************************************************/ /****************************************************************************** *************************** Begin NFS Locking ********************************/ // C documentation // // /* // ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock // ** must be either NO_LOCK or SHARED_LOCK. // ** // ** If the locking level of the file descriptor is already at or below // ** the requested locking level, this routine is a no-op. // */ func _nfsUnlock(tls *libc.TLS, id uintptr, eFileLock int32) (r int32) { return _posixUnlock(tls, id, eFileLock, int32(1)) } /* ** The code above is the NFS lock implementation. The code is specific ** to MacOSX and does not work on other unix platforms. No alternative ** is available. ** ********************* End of the NFS lock implementation ********************** ******************************************************************************/ /****************************************************************************** **************** Non-locking sqlite3_file methods ***************************** ** ** The next division contains implementations for all methods of the ** sqlite3_file object other than the locking methods. The locking ** methods were defined in divisions above (one locking method per ** division). Those methods that are common to all locking modes ** are gather together into this division. */ // C documentation // // /* // ** Seek to the offset passed as the second argument, then read cnt // ** bytes into pBuf. Return the number of bytes actually read. // ** // ** To avoid stomping the errno value on a failed read the lastErrno value // ** is set before returning. // */ func _seekAndRead(tls *libc.TLS, id uintptr, offset Tsqlite3_int64, pBuf uintptr, cnt int32) (r int32) { var got, prior int32 _, _ = got, prior prior = 0 for cond := true; cond; cond = got > 0 { got = int32((*(*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, uint64(cnt), offset)) if got == cnt { break } if got < 0 { if *(*int32)(unsafe.Pointer(libc.X__error(tls))) == int32(EINTR) { got = int32(1) continue } prior = 0 _storeLastErrno(tls, id, *(*int32)(unsafe.Pointer(libc.X__error(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 /* 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.X__builtin___memcpy_chk(tls, pBuf, (*TunixFile)(unsafe.Pointer(pFile)).FpMapRegion+uintptr(offset), uint64(amt), libc.X__builtin_object_size(tls, pBuf, 0)) return SQLITE_OK } else { nCopy = int32((*TunixFile)(unsafe.Pointer(pFile)).FmmapSize - offset) libc.X__builtin___memcpy_chk(tls, pBuf, (*TunixFile)(unsafe.Pointer(pFile)).FpMapRegion+uintptr(offset), uint64(nCopy), libc.X__builtin_object_size(tls, pBuf, 0)) 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): fallthrough case int32(EDEVERR): 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+32) } // 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 /* 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]int8)(unsafe.Pointer(bp)))[ii]) != int32('/')) { break } goto _1 _1: ; ii-- } if ii > 0 { (*(*[513]int8)(unsafe.Pointer(bp)))[ii] = int8('\000') } else { if int32((*(*[513]int8)(unsafe.Pointer(bp)))[0]) != int32('/') { (*(*[513]int8)(unsafe.Pointer(bp)))[0] = int8('.') } (*(*[513]int8)(unsafe.Pointer(bp)))[int32(1)] = 0 } fd = _robust_open(tls, bp, libc.Int32FromInt32(O_RDONLY)|libc.Int32FromInt32(O_BINARY), uint16(0)) if fd >= 0 { } *(*int32)(unsafe.Pointer(pFd)) = fd if fd >= 0 { return SQLITE_OK } return _unixLogErrorAtLine(tls, _sqlite3CantopenError(tls, int32(42111)), __ccgo_ts+3576, bp, int32(42111)) } // 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 */ /* Unix cannot, but some systems may return SQLITE_FULL from here. This ** line is to test that doing so does not cause any problems. */ rc = _full_fsync(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, isFullsync, isDataOnly) if rc != 0 { _storeLastErrno(tls, pFile, *(*int32)(unsafe.Pointer(libc.X__error(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__error(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) for { if !(iWrite < nSize+int64(nBlk)-int64(1)) { break } if iWrite >= nSize { iWrite = nSize - int64(1) } nWrite = _seekAndWrite(tls, pFile, iWrite, __ccgo_ts+1664, 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__error(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 + 30 *(*uint16)(unsafe.Pointer(p1)) = uint16(int32(*(*uint16)(unsafe.Pointer(p1))) & ^int32(mask)) } else { p2 = pFile + 30 *(*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+3863, 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(uint64(8) < uint64(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_SET_LOCKPROXYFILE): fallthrough case int32(SQLITE_FCNTL_GET_LOCKPROXYFILE): return _proxyFileControl(tls, id, op, pArg) 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) { 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 + 116)) |= 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 int32(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.X__builtin___memset_chk(tls, bp, 0, uint64(24), libc.X__builtin_object_size(tls, bp, 0)) (*(*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_GETLK), libc.VaList(bp+32, 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_SETLK), libc.VaList(bp+32, 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 */ /* 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 if p != 0 && (*TunixShmNode)(unsafe.Pointer(p)).FnRef == 0 { nShmPerMap = _unixShmRegionPerMap(tls) 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)*8)), uint64((*TunixShmNode)(unsafe.Pointer(p)).FszRegion)) } else { Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer((*TunixShmNode)(unsafe.Pointer(p)).FapRegion + uintptr(i)*8))) } 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(42916)) (*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_GETLK), libc.VaList(bp+32, 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(160) defer tls.Free(160) 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+144 */ 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) } /* 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), uint64(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)*8)) } 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 + 18 *(*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.X__builtin___memset_chk(tls, aLock+uintptr(ofst)*4, 0, uint64(4)*uint64(n), libc.X__builtin_object_size(tls, aLock+uintptr(ofst)*4, 0)) p2 = p + 18 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) & ^int32(mask)) p3 = p + 20 *(*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, int32(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 + 18 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | int32(mask)) *(*int32)(unsafe.Pointer(aLock + uintptr(ofst)*4))++ } } else { /* 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 + 20 *(*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++ } } } } } } /* 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 */ _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 /* Remove connection p from the set of connections associated ** with pShmNode */ Xsqlite3_mutex_enter(tls, (*TunixShmNode)(unsafe.Pointer(pShmNode)).FpShmMutex) pp = pShmNode + 56 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != p) { break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 8 } *(*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 */ _unixEnterMutex(tls) (*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) { 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, uint64((*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, szSyspage int32 var nOrig, nReuse Ti64 var pNew, pOrig, pReq, zErr uintptr var v1 Tsqlite3_int64 _, _, _, _, _, _, _, _, _, _ = flags, h, nOrig, nReuse, pNew, pOrig, pReq, szSyspage, zErr, v1 zErr = __ccgo_ts + 3617 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() */ if pOrig != 0 { szSyspage = (*(*func(*libc.TLS) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(25)].FpCurrent})))(tls) nReuse = (*TunixFile)(unsafe.Pointer(pFd)).FmmapSize & int64(^(szSyspage - libc.Int32FromInt32(1))) 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, uint64(nOrig-nReuse)) } pNew = (*(*func(*libc.TLS, uintptr, Tsize_t, int32, int32, int32, Toff_t) uintptr)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(22)].FpCurrent})))(tls, pReq, uint64(nNew-nReuse), flags, int32(MAP_SHARED), h, nReuse) if pNew != uintptr(-libc.Int32FromInt32(1)) { if pNew != pReq { (*(*func(*libc.TLS, uintptr, Tsize_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(23)].FpCurrent})))(tls, pNew, uint64(nNew-nReuse)) pNew = uintptr(0) } else { pNew = pOrig } } /* 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, uint64(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), uint64(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(43734)) /* 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(144) defer tls.Free(144) var _ /* statbuf at bp+0 */ Tstat 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 } 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. */ /* If p!=0, it must match the iOff value. */ if p != 0 { (*TunixFile)(unsafe.Pointer(pFd)).FnFetchOut-- } else { _unixUnmapfile(tls, pFd) } 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, 8)) = __ccgo_fp(_unixClose) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_unixRead) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_unixWrite) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_unixTruncate) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_unixSync) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_unixFileSize) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_unixLock) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_unixUnlock) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_unixCheckReservedLock) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_unixFileControl) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_unixSectorSize) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_unixDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(_unixShmMap) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_unixShmLock) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_unixShmBarrier) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(_unixShmUnmap) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_unixFetch) *(*uintptr)(unsafe.Add(p, 144)) = __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, 8)) = __ccgo_fp(_nolockClose) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_unixRead) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_unixWrite) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_unixTruncate) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_unixSync) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_unixFileSize) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_nolockLock) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_nolockUnlock) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_nolockCheckReservedLock) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_unixFileControl) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_unixSectorSize) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_unixDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_unixShmLock) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_unixShmBarrier) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(_unixShmUnmap) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_unixFetch) *(*uintptr)(unsafe.Add(p, 144)) = __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, 8)) = __ccgo_fp(_dotlockClose) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_unixRead) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_unixWrite) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_unixTruncate) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_unixSync) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_unixFileSize) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_dotlockLock) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_dotlockUnlock) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_dotlockCheckReservedLock) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_unixFileControl) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_unixSectorSize) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_unixDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_unixShmLock) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_unixShmBarrier) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(_unixShmUnmap) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_unixFetch) *(*uintptr)(unsafe.Add(p, 144)) = __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) } var _flockIoMethods = Tsqlite3_io_methods{ FiVersion: int32(1), } func init() { p := unsafe.Pointer(&_flockIoMethods) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_flockClose) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_unixRead) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_unixWrite) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_unixTruncate) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_unixSync) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_unixFileSize) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_flockLock) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_flockUnlock) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_flockCheckReservedLock) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_unixFileControl) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_unixSectorSize) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_unixDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_unixShmLock) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_unixShmBarrier) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(_unixShmUnmap) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_unixFetch) *(*uintptr)(unsafe.Add(p, 144)) = __ccgo_fp(_unixUnfetch) } func _flockIoFinderImpl(tls *libc.TLS, z uintptr, p uintptr) (r uintptr) { _ = z _ = p return uintptr(unsafe.Pointer(&_flockIoMethods)) } var _flockIoFinder = uintptr(0) func init() { p := unsafe.Pointer(&_flockIoFinder) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_flockIoFinderImpl) } var _afpIoMethods = Tsqlite3_io_methods{ FiVersion: int32(1), } func init() { p := unsafe.Pointer(&_afpIoMethods) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_afpClose) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_unixRead) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_unixWrite) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_unixTruncate) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_unixSync) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_unixFileSize) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_afpLock) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_afpUnlock) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_afpCheckReservedLock) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_unixFileControl) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_unixSectorSize) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_unixDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_unixShmLock) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_unixShmBarrier) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(_unixShmUnmap) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_unixFetch) *(*uintptr)(unsafe.Add(p, 144)) = __ccgo_fp(_unixUnfetch) } func _afpIoFinderImpl(tls *libc.TLS, z uintptr, p uintptr) (r uintptr) { _ = z _ = p return uintptr(unsafe.Pointer(&_afpIoMethods)) } var _afpIoFinder = uintptr(0) func init() { p := unsafe.Pointer(&_afpIoFinder) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_afpIoFinderImpl) } var _proxyIoMethods = Tsqlite3_io_methods{ FiVersion: int32(1), } func init() { p := unsafe.Pointer(&_proxyIoMethods) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_proxyClose) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_unixRead) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_unixWrite) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_unixTruncate) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_unixSync) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_unixFileSize) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_proxyLock) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_proxyUnlock) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_proxyCheckReservedLock) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_unixFileControl) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_unixSectorSize) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_unixDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_unixShmLock) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_unixShmBarrier) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(_unixShmUnmap) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_unixFetch) *(*uintptr)(unsafe.Add(p, 144)) = __ccgo_fp(_unixUnfetch) } func _proxyIoFinderImpl(tls *libc.TLS, z uintptr, p uintptr) (r uintptr) { _ = z _ = p return uintptr(unsafe.Pointer(&_proxyIoMethods)) } var _proxyIoFinder = uintptr(0) func init() { p := unsafe.Pointer(&_proxyIoFinder) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_proxyIoFinderImpl) } // C documentation // // /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ var _nfsIoMethods = Tsqlite3_io_methods{ FiVersion: int32(1), } func init() { p := unsafe.Pointer(&_nfsIoMethods) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_unixClose) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_unixRead) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_unixWrite) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_unixTruncate) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_unixSync) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_unixFileSize) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_unixLock) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_nfsUnlock) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_unixCheckReservedLock) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_unixFileControl) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_unixSectorSize) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_unixDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_unixShmLock) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_unixShmBarrier) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(_unixShmUnmap) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_unixFetch) *(*uintptr)(unsafe.Add(p, 144)) = __ccgo_fp(_unixUnfetch) } func _nfsIoFinderImpl(tls *libc.TLS, z uintptr, p uintptr) (r uintptr) { _ = z _ = p return uintptr(unsafe.Pointer(&_nfsIoMethods)) } var _nfsIoFinder = uintptr(0) func init() { p := unsafe.Pointer(&_nfsIoFinder) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_nfsIoFinderImpl) } // C documentation // // /* // ** This "finder" function attempts to determine the best locking strategy // ** for the database file "filePath". It then returns the sqlite3_io_methods // ** object that implements that strategy. // ** // ** This is for MacOSX only. // */ func _autolockIoFinderImpl(tls *libc.TLS, filePath uintptr, pNew uintptr) (r uintptr) { bp := tls.Alloc(2208) defer tls.Free(2208) var i int32 var _ /* fsInfo at bp+0 */ Tstatfs var _ /* lockInfo at bp+2168 */ Tflock _ = i if !(filePath != 0) { /* If filePath==NULL that means we are dealing with a transient file ** that does not need to be locked. */ return uintptr(unsafe.Pointer(&_nolockIoMethods)) } if libc.Xstatfs(tls, filePath, bp) != -int32(1) { if (*(*Tstatfs)(unsafe.Pointer(bp))).Ff_flags&uint32(MNT_RDONLY) != 0 { return uintptr(unsafe.Pointer(&_nolockIoMethods)) } i = 0 for { if !(_aMap[i].FzFilesystem != 0) { break } if libc.Xstrcmp(tls, bp+72, _aMap[i].FzFilesystem) == 0 { return _aMap[i].FpMethods } goto _1 _1: ; i++ } } /* Default case. Handles, amongst others, "nfs". ** Test byte-range lock using fcntl(). If the call succeeds, ** assume that the file-system supports POSIX style locks. */ (*(*Tflock)(unsafe.Pointer(bp + 2168))).Fl_len = int64(1) (*(*Tflock)(unsafe.Pointer(bp + 2168))).Fl_start = 0 (*(*Tflock)(unsafe.Pointer(bp + 2168))).Fl_whence = SEEK_SET (*(*Tflock)(unsafe.Pointer(bp + 2168))).Fl_type = int16(F_RDLCK) if (*(*func(*libc.TLS, int32, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(7)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(pNew)).Fh, int32(F_GETLK), libc.VaList(bp+2200, bp+2168)) != -int32(1) { if libc.Xstrcmp(tls, bp+72, __ccgo_ts+3924) == 0 { return uintptr(unsafe.Pointer(&_nfsIoMethods)) } else { return uintptr(unsafe.Pointer(&_posixIoMethods)) } } else { return uintptr(unsafe.Pointer(&_dotlockIoMethods)) } return r } var _aMap = [6]struct { FzFilesystem uintptr FpMethods uintptr }{ 0: { FzFilesystem: __ccgo_ts + 3897, FpMethods: uintptr(unsafe.Pointer(&_posixIoMethods)), }, 1: { FzFilesystem: __ccgo_ts + 3901, FpMethods: uintptr(unsafe.Pointer(&_posixIoMethods)), }, 2: { FzFilesystem: __ccgo_ts + 3905, FpMethods: uintptr(unsafe.Pointer(&_afpIoMethods)), }, 3: { FzFilesystem: __ccgo_ts + 3911, FpMethods: uintptr(unsafe.Pointer(&_afpIoMethods)), }, 4: { FzFilesystem: __ccgo_ts + 3917, FpMethods: uintptr(unsafe.Pointer(&_nolockIoMethods)), }, 5: {}, } var _autolockIoFinder = uintptr(0) func init() { p := unsafe.Pointer(&_autolockIoFinder) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_autolockIoFinderImpl) } // 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 pCtx, pLockingStyle, pNew, zLockFile, v1, v4, p2, p3 uintptr _, _, _, _, _, _, _, _, _, _ = nFilename, pCtx, pLockingStyle, pNew, rc, zLockFile, v1, v4, p2, p3 pNew = pId rc = SQLITE_OK /* No locking occurs in temporary files */ (*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+3928, int32(SQLITE_POWERSAFE_OVERWRITE)) != 0 { p2 = pNew + 30 *(*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+3933) == 0 { p3 = pNew + 30 *(*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) /* Cache zFilename in the locking context (AFP and dotlock override) for ** proxyLock activation is possible (remote proxy is based on db name) ** zFilename remains valid until file is closed, to support */ (*TunixFile)(unsafe.Pointer(pNew)).FlockingContext = zFilename } if pLockingStyle == uintptr(unsafe.Pointer(&_posixIoMethods)) || pLockingStyle == uintptr(unsafe.Pointer(&_nfsIoMethods)) { _unixEnterMutex(tls) rc = _findInodeInfo(tls, pNew, pNew+16) 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(44242)) h = -int32(1) } _unixLeaveMutex(tls) } else { if pLockingStyle == uintptr(unsafe.Pointer(&_afpIoMethods)) { v4 = Xsqlite3_malloc64(tls, uint64(16)) pCtx = v4 (*TunixFile)(unsafe.Pointer(pNew)).FlockingContext = v4 if pCtx == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { /* NB: zFilename exists and remains valid until the file is closed ** according to requirement F11141. So we do not need to make a ** copy of the filename. */ (*TafpLockingContext)(unsafe.Pointer(pCtx)).FdbPath = zFilename (*TafpLockingContext)(unsafe.Pointer(pCtx)).Freserved = 0 libc.Xsrandomdev(tls) _unixEnterMutex(tls) rc = _findInodeInfo(tls, pNew, pNew+16) if rc != SQLITE_OK { Xsqlite3_free(tls, (*TunixFile)(unsafe.Pointer(pNew)).FlockingContext) _robust_close(tls, pNew, h, int32(44268)) h = -int32(1) } _unixLeaveMutex(tls) } } else { if pLockingStyle == uintptr(unsafe.Pointer(&_dotlockIoMethods)) { 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+3943, 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(44327)) } } 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 + 3951, 3: __ccgo_ts + 3960, 4: __ccgo_ts + 3969, 5: __ccgo_ts + 1677, } // C documentation // // /* // ** Initialize first two members of azTempDirs[] array. // */ func _unixTempFileInit(tls *libc.TLS) { _azTempDirs[0] = libc.Xgetenv(tls, __ccgo_ts+3974) _azTempDirs[int32(1)] = libc.Xgetenv(tls, __ccgo_ts+3988) } // 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(144) defer tls.Free(144) 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 && int32((*(*Tstat)(unsafe.Pointer(bp))).Fst_mode)&int32(S_IFMT) == int32(S_IFDIR) && (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(2)].FpCurrent})))(tls, zDir, int32(03)) == 0 { return zDir } if uint64(i) >= libc.Uint64FromInt64(48)/libc.Uint64FromInt64(8) { 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. */ *(*int8)(unsafe.Pointer(zBuf)) = 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(144) defer tls.Free(144) 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 { Xsqlite3_mutex_enter(tls, (*TunixInodeInfo)(unsafe.Pointer(pInode)).FpLockMutex) flags &= libc.Int32FromInt32(SQLITE_OPEN_READONLY) | libc.Int32FromInt32(SQLITE_OPEN_READWRITE) pp = pInode + 40 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(144) defer tls.Free(144) 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)) = uint16(int32((*(*Tstat)(unsafe.Pointer(bp))).Fst_mode) & int32(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(*(*int8)(unsafe.Pointer(zPath + uintptr(nDb)))) != int32('.') { if int32(*(*int8)(unsafe.Pointer(zPath + uintptr(nDb)))) == int32('-') { libc.X__builtin___memcpy_chk(tls, bp, zPath, uint64(nDb), libc.X__builtin_object_size(tls, bp, 0)) (*(*[513]int8)(unsafe.Pointer(bp)))[nDb] = int8('\000') rc = _getFileMode(tls, bp, pMode, pUid, pGid) break } nDb-- } } else { if flags&int32(SQLITE_OPEN_DELETEONCLOSE) != 0 { *(*Tmode_t)(unsafe.Pointer(pMode)) = uint16(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+4012) 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(2704) defer tls.Free(2704) var ctrlFlags, eType, fd, isAutoProxy, isCreate, isDelete, isExclusive, isNewJrnl, isReadWrite, isReadonly, noLock, openFlags, rc, rc2, useProxy int32 var envforce, p, pReadonly, pUnused, zName uintptr var _ /* fsInfo at bp+0 */ Tstatfs var _ /* gid at bp+2688 */ Tgid_t var _ /* openMode at bp+2682 */ Tmode_t var _ /* uid at bp+2684 */ Tuid_t var _ /* zTmpname at bp+2168 */ [514]int8 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = ctrlFlags, eType, envforce, fd, isAutoProxy, isCreate, isDelete, isExclusive, isNewJrnl, isReadWrite, isReadonly, noLock, openFlags, p, pReadonly, pUnused, rc, rc2, useProxy, 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) isAutoProxy = flags & int32(SQLITE_OPEN_AUTOPROXY) /* 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. */ /* The main DB, main journal, WAL file and super-journal are never ** automatically deleted. Nor are they ever temporary files. */ /* Assert that the upper layer has set one of the "file-type" flags. */ /* 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.X__builtin___memset_chk(tls, p, 0, uint64(128), libc.X__builtin_object_size(tls, p, 0)) 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(16)) 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(). */ } else { if !(zName != 0) { /* If zName is NULL, the upper layer is requesting a temp file. */ rc = _unixGetTempname(tls, (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FmxPathname, bp+2168) if rc != SQLITE_OK { return rc } zName = bp + 2168 /* Generated temporary filenames are always double-zero terminated ** for use by sqlite3_uri_parameter(). */ } } /* 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+2682, bp+2684, bp+2688) if rc != SQLITE_OK { return rc } fd = _robust_open(tls, zName, openFlags, *(*Tmode_t)(unsafe.Pointer(bp + 2682))) if fd < 0 { if isNewJrnl != 0 && *(*int32)(unsafe.Pointer(libc.X__error(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 { useProxy = libc.BoolInt32(!((*(*Tstatfs)(unsafe.Pointer(bp))).Ff_flags&libc.Uint32FromInt32(MNT_LOCAL) != 0)) } if useProxy != 0 { rc = _fillInUnixFile(tls, pVfs, fd, pFile, zPath, ctrlFlags) if rc == SQLITE_OK { rc = _proxyTransformUnixFile(tls, pFile, __ccgo_ts+4058) if rc != SQLITE_OK { /* Use unixClose to clean up the resources added in fillInUnixFile ** and clear all the structure's references. Specifically, ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op */ _unixClose(tls, pFile) return rc } } goto open_finished } } rc = _fillInUnixFile(tls, pVfs, fd, pFile, zPath, ctrlFlags) goto open_finished open_finished: ; if rc != SQLITE_OK { Xsqlite3_free(tls, (*TunixFile)(unsafe.Pointer(p)).FpPreallocatedUnused) } return rc } // C documentation // // /* // ** Delete the file at zPath. If the dirSync argument is true, fsync() // ** the directory after deleting the file. // */ func _unixDelete(tls *libc.TLS, NotUsed uintptr, zPath uintptr, dirSync int32) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var rc int32 var _ /* fd at bp+0 */ int32 _ = rc rc = SQLITE_OK _ = NotUsed if (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(16)].FpCurrent})))(tls, zPath) == -int32(1) { if *(*int32)(unsafe.Pointer(libc.X__error(tls))) == int32(ENOENT) { rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(23)< 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(1)< int32(1) { for { v2 = pPath + 20 *(*int32)(unsafe.Pointer(v2))-- v1 = *(*int32)(unsafe.Pointer(v2)) if !(int32(*(*int8)(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 + 20 v3 = *(*int32)(unsafe.Pointer(v4)) *(*int32)(unsafe.Pointer(v4))++ *(*int8)(unsafe.Pointer((*TDbPath)(unsafe.Pointer(pPath)).FzOut + uintptr(v3))) = int8('/') libc.X__builtin___memcpy_chk(tls, (*TDbPath)(unsafe.Pointer(pPath)).FzOut+uintptr((*TDbPath)(unsafe.Pointer(pPath)).FnUsed), zName, uint64(nName), libc.X__builtin_object_size(tls, (*TDbPath)(unsafe.Pointer(pPath)).FzOut+uintptr((*TDbPath)(unsafe.Pointer(pPath)).FnUsed), 0)) *(*int32)(unsafe.Pointer(pPath + 20)) += nName if (*TDbPath)(unsafe.Pointer(pPath)).Frc == SQLITE_OK { *(*int8)(unsafe.Pointer((*TDbPath)(unsafe.Pointer(pPath)).FzOut + uintptr((*TDbPath)(unsafe.Pointer(pPath)).FnUsed))) = 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__error(tls))) != int32(ENOENT) { (*TDbPath)(unsafe.Pointer(pPath)).Frc = _unixLogErrorAtLine(tls, _sqlite3CantopenError(tls, int32(45020)), __ccgo_ts+3657, zIn, int32(45020)) } } else { if int32((*(*Tstat)(unsafe.Pointer(bp))).Fst_mode)&int32(S_IFMT) == int32(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(45026)) return } got = (*(*func(*libc.TLS, uintptr, uintptr, Tsize_t) Tssize_t)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(26)].FpCurrent})))(tls, zIn, bp+144, libc.Uint64FromInt64(1026)-libc.Uint64FromInt32(2)) if got <= 0 || got >= libc.Int64FromInt64(1026)-libc.Int64FromInt32(2) { (*TDbPath)(unsafe.Pointer(pPath)).Frc = _unixLogErrorAtLine(tls, _sqlite3CantopenError(tls, int32(45031)), __ccgo_ts+3648, zIn, int32(45031)) return } (*(*[1026]int8)(unsafe.Pointer(bp + 144)))[got] = 0 if int32((*(*[1026]int8)(unsafe.Pointer(bp + 144)))[0]) == int32('/') { (*TDbPath)(unsafe.Pointer(pPath)).FnUsed = 0 } else { *(*int32)(unsafe.Pointer(pPath + 20)) -= nName + int32(1) } _appendAllPathElements(tls, pPath, bp+144) } } } } // 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 *(*int8)(unsafe.Pointer(zPath + uintptr(i))) != 0 && int32(*(*int8)(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 !(*(*int8)(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(1056) defer tls.Free(1056) var _ /* path at bp+0 */ TDbPath var _ /* zPwd at bp+24 */ [1026]int8 _ = 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(*(*int8)(unsafe.Pointer(zPath))) != int32('/') { if (*(*func(*libc.TLS, uintptr, Tsize_t) uintptr)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(3)].FpCurrent})))(tls, bp+24, libc.Uint64FromInt64(1026)-libc.Uint64FromInt32(2)) == uintptr(0) { return _unixLogErrorAtLine(tls, _sqlite3CantopenError(tls, int32(45089)), __ccgo_ts+3477, zPath, int32(45089)) } _appendAllPathElements(tls, bp, bp+24) } _appendAllPathElements(tls, bp, zPath) *(*int8)(unsafe.Pointer(zOut + uintptr((*(*TDbPath)(unsafe.Pointer(bp))).FnUsed))) = 0 if (*(*TDbPath)(unsafe.Pointer(bp))).Frc != 0 || (*(*TDbPath)(unsafe.Pointer(bp))).FnUsed < int32(2) { return _sqlite3CantopenError(tls, int32(45095)) } 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 */ // C documentation // // /* // ** The proxyLockingContext has the path and file structures for the remote // ** and local proxy files in it // */ type TproxyLockingContext = struct { FconchFile uintptr FconchFilePath uintptr FlockProxy uintptr FlockProxyPath uintptr FdbPath uintptr FconchHeld int32 FnFails int32 FoldLockingContext uintptr FpOldMethod uintptr } type proxyLockingContext = TproxyLockingContext type TproxyLockingContext1 = struct { FconchFile uintptr FconchFilePath uintptr FlockProxy uintptr FlockProxyPath uintptr FdbPath uintptr FconchHeld int32 FnFails int32 FoldLockingContext uintptr FpOldMethod uintptr } type proxyLockingContext1 = TproxyLockingContext1 // C documentation // // /* // ** The proxy lock file path for the database at dbPath is written into lPath, // ** which must point to valid, writable memory large enough for a maxLen length // ** file path. // */ func _proxyGetLockPath(tls *libc.TLS, dbPath uintptr, lPath uintptr, maxLen Tsize_t) (r int32) { var c int8 var dbLen, i, len1, v2 int32 _, _, _, _, _ = c, dbLen, i, len1, v2 if !(libc.Xconfstr(tls, int32(_CS_DARWIN_USER_TEMP_DIR), lPath, maxLen) != 0) { return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(15)< 0 { /* only mkdir if leaf dir != "." or "/" or ".." */ if i-start > int32(2) || i-start == int32(1) && int32((*(*[1024]int8)(unsafe.Pointer(bp)))[start]) != int32('.') && int32((*(*[1024]int8)(unsafe.Pointer(bp)))[start]) != int32('/') || i-start == int32(2) && int32((*(*[1024]int8)(unsafe.Pointer(bp)))[start]) != int32('.') && int32((*(*[1024]int8)(unsafe.Pointer(bp)))[start+int32(1)]) != int32('.') { (*(*[1024]int8)(unsafe.Pointer(bp)))[i] = int8('\000') if (*(*func(*libc.TLS, uintptr, Tmode_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(18)].FpCurrent})))(tls, bp, uint16(SQLITE_DEFAULT_PROXYDIR_PERMISSIONS)) != 0 { err = *(*int32)(unsafe.Pointer(libc.X__error(tls))) if err != int32(EEXIST) { return err } } } start = i + int32(1) } (*(*[1024]int8)(unsafe.Pointer(bp)))[i] = *(*int8)(unsafe.Pointer(lockPath + uintptr(i))) goto _1 _1: ; i++ } return 0 } // C documentation // // /* // ** Create a new VFS file descriptor (stored in memory obtained from // ** sqlite3_malloc) and open the file named "path" in the file descriptor. // ** // ** The caller is responsible not only for closing the file descriptor // ** but also for freeing the memory associated with the file descriptor. // */ func _proxyCreateUnixFile(tls *libc.TLS, path uintptr, ppFile uintptr, islockfile int32) (r int32) { bp := tls.Alloc(176) defer tls.Free(176) var fd, openFlags, rc, terrno int32 var pNew, pUnused uintptr var _ /* dummyVfs at bp+0 */ Tsqlite3_vfs _, _, _, _, _, _ = fd, openFlags, pNew, pUnused, rc, terrno fd = -int32(1) rc = SQLITE_OK openFlags = libc.Int32FromInt32(O_RDWR) | libc.Int32FromInt32(O_CREAT) | libc.Int32FromInt32(O_NOFOLLOW) terrno = 0 pUnused = libc.UintptrFromInt32(0) /* 1. first try to open/create the file ** 2. if that fails, and this is a lock file (not-conch), try creating ** the parent directories and then try again. ** 3. if that fails, try to open the file read-only ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file */ pUnused = _findReusableFd(tls, path, openFlags) if pUnused != 0 { fd = (*TUnixUnusedFd)(unsafe.Pointer(pUnused)).Ffd } else { pUnused = Xsqlite3_malloc64(tls, uint64(16)) if !(pUnused != 0) { return int32(SQLITE_NOMEM) } } if fd < 0 { fd = _robust_open(tls, path, openFlags, uint16(0)) terrno = *(*int32)(unsafe.Pointer(libc.X__error(tls))) if fd < 0 && *(*int32)(unsafe.Pointer(libc.X__error(tls))) == int32(ENOENT) && islockfile != 0 { if _proxyCreateLockPath(tls, path) == SQLITE_OK { fd = _robust_open(tls, path, openFlags, uint16(0)) } } } if fd < 0 { openFlags = libc.Int32FromInt32(O_RDONLY) | libc.Int32FromInt32(O_NOFOLLOW) fd = _robust_open(tls, path, openFlags, uint16(0)) terrno = *(*int32)(unsafe.Pointer(libc.X__error(tls))) } if fd < 0 { if islockfile != 0 { return int32(SQLITE_BUSY) } switch terrno { case int32(EACCES): return int32(SQLITE_PERM) case int32(EIO): return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(15)< uint64(PATH_MAX) || pathLen < uint64(6) || libc.Xstrlcpy(tls, bp+uintptr(pathLen-uint64(5)), __ccgo_ts+4105, uint64(6)) != uint64(5) { Xsqlite3_snprintf(tls, int32(64), bp+2065, __ccgo_ts+4111, libc.VaList(bp+2144, int32(pathLen))) goto end_breaklock } /* read the conch content */ readLen = uint64((*(*func(*libc.TLS, int32, uintptr, Tsize_t, Toff_t) Tssize_t)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(9)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(conchFile)).Fh, bp+1024, uint64(libc.Int32FromInt32(PROXY_HEADERLEN)+libc.Int32FromInt32(PROXY_HOSTIDLEN)+libc.Int32FromInt32(PATH_MAX)), 0)) if readLen < uint64(libc.Int32FromInt32(PROXY_HEADERLEN)+libc.Int32FromInt32(PROXY_HOSTIDLEN)) { Xsqlite3_snprintf(tls, int32(64), bp+2065, __ccgo_ts+4131, libc.VaList(bp+2144, int32(readLen))) goto end_breaklock } /* write it out to the temporary break file */ fd = _robust_open(tls, bp, libc.Int32FromInt32(O_RDWR)|libc.Int32FromInt32(O_CREAT)|libc.Int32FromInt32(O_EXCL)|libc.Int32FromInt32(O_NOFOLLOW), uint16(0)) if fd < 0 { Xsqlite3_snprintf(tls, int32(64), bp+2065, __ccgo_ts+4151, libc.VaList(bp+2144, *(*int32)(unsafe.Pointer(libc.X__error(tls))))) goto end_breaklock } if (*(*func(*libc.TLS, int32, uintptr, Tsize_t, Toff_t) Tssize_t)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(12)].FpCurrent})))(tls, fd, bp+1024, readLen, 0) != int64(readLen) { Xsqlite3_snprintf(tls, int32(64), bp+2065, __ccgo_ts+4170, libc.VaList(bp+2144, *(*int32)(unsafe.Pointer(libc.X__error(tls))))) goto end_breaklock } if libc.Xrename(tls, bp, cPath) != 0 { Xsqlite3_snprintf(tls, int32(64), bp+2065, __ccgo_ts+4188, libc.VaList(bp+2144, *(*int32)(unsafe.Pointer(libc.X__error(tls))))) goto end_breaklock } rc = 0 libc.Xfprintf(tls, libc.X__stderrp, __ccgo_ts+4207, libc.VaList(bp+2144, cPath)) _robust_close(tls, pFile, (*TunixFile)(unsafe.Pointer(conchFile)).Fh, int32(45758)) (*TunixFile)(unsafe.Pointer(conchFile)).Fh = fd (*TunixFile)(unsafe.Pointer(conchFile)).FopenFlags = libc.Int32FromInt32(O_RDWR) | libc.Int32FromInt32(O_CREAT) goto end_breaklock end_breaklock: ; if rc != 0 { if fd >= 0 { (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(16)].FpCurrent})))(tls, bp) _robust_close(tls, pFile, fd, int32(45766)) } libc.Xfprintf(tls, libc.X__stderrp, __ccgo_ts+4231, libc.VaList(bp+2144, cPath, bp+2065)) } return rc } // C documentation // // /* Take the requested lock on the conch file and break a stale lock if the // ** host id matches. // */ func _proxyConchLock(tls *libc.TLS, pFile uintptr, myHostID uintptr, lockType int32) (r int32) { bp := tls.Alloc(1216) defer tls.Free(1216) var conchFile, pCtx uintptr var len1, nTries, rc int32 var _ /* buf at bp+16 */ Tstat var _ /* conchModTime at bp+0 */ Ttimespec var _ /* tBuf at bp+160 */ [1041]int8 _, _, _, _, _ = conchFile, len1, nTries, pCtx, rc pCtx = (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext conchFile = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchFile rc = SQLITE_OK nTries = 0 libc.X__builtin___memset_chk(tls, bp, 0, uint64(16), libc.X__builtin_object_size(tls, bp, 0)) for cond := true; cond; cond = rc == int32(SQLITE_BUSY) && nTries < int32(3) { rc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(conchFile)).FpMethod)).FxLock})))(tls, conchFile, lockType) nTries++ if rc == int32(SQLITE_BUSY) { if (*(*func(*libc.TLS, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(5)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(conchFile)).Fh, bp+16) != 0 { _storeLastErrno(tls, pFile, *(*int32)(unsafe.Pointer(libc.X__error(tls)))) return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(15)< libc.Int32FromInt32(PROXY_HEADERLEN)+libc.Int32FromInt32(PROXY_HOSTIDLEN) && int32((*(*[1041]int8)(unsafe.Pointer(bp + 160)))[0]) == int32(libc.Int8FromInt32(PROXY_CONCHVERSION)) { /* don't break the lock if the host id doesn't match */ if 0 != libc.Xmemcmp(tls, bp+160+1, myHostID, uint64(PROXY_HOSTIDLEN)) { return int32(SQLITE_BUSY) } } else { /* don't break the lock on short read or a version mismatch */ return int32(SQLITE_BUSY) } _unixSleep(tls, uintptr(0), int32(10000000)) /* wait 10 sec and try the lock again */ continue } if 0 == _proxyBreakConchLock(tls, pFile, myHostID) { rc = SQLITE_OK if lockType == int32(EXCLUSIVE_LOCK) { rc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(conchFile)).FpMethod)).FxLock})))(tls, conchFile, int32(SHARED_LOCK)) } if !(rc != 0) { rc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(conchFile)).FpMethod)).FxLock})))(tls, conchFile, lockType) } } } } return rc } // C documentation // // /* Takes the conch by taking a shared lock and read the contents conch, if // ** lockPath is non-NULL, the host ID and lock file path must match. A NULL // ** lockPath means that the lockPath in the conch file will be used if the // ** host IDs match, or a new lock path will be generated automatically // ** and written to the conch file. // */ func _proxyTakeConch(tls *libc.TLS, pFile uintptr) (r int32) { bp := tls.Alloc(3280) defer tls.Free(3280) var afpCtx, conchFile, pCtx, path, tempLockPath, v4 uintptr var cmode Tmode_t var createConch, err, fd, forceNewLockPath, hostIdMatch, rc, readLen, tryOldLockPath, writeSize int32 var pathLen Tsize_t var _ /* buf at bp+3128 */ Tstat var _ /* lockPath at bp+1061 */ [1024]int8 var _ /* myHostID at bp+0 */ Tuuid_t var _ /* pError at bp+16 */ int32 var _ /* readBuf at bp+20 */ [1041]int8 var _ /* writeBuffer at bp+2085 */ [1041]int8 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = afpCtx, cmode, conchFile, createConch, err, fd, forceNewLockPath, hostIdMatch, pCtx, path, pathLen, rc, readLen, tempLockPath, tryOldLockPath, writeSize, v4 pCtx = (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext if (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchHeld != 0 { return SQLITE_OK } else { conchFile = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchFile *(*int32)(unsafe.Pointer(bp + 16)) = 0 tempLockPath = libc.UintptrFromInt32(0) rc = SQLITE_OK createConch = 0 hostIdMatch = 0 readLen = 0 tryOldLockPath = 0 forceNewLockPath = 0 rc = _proxyGetHostID(tls, bp, bp+16) if rc&int32(0xff) == int32(SQLITE_IOERR) { _storeLastErrno(tls, pFile, *(*int32)(unsafe.Pointer(bp + 16))) goto end_takeconch } rc = _proxyConchLock(tls, pFile, bp, int32(SHARED_LOCK)) if rc != SQLITE_OK { goto end_takeconch } /* read the existing conch file */ readLen = _seekAndRead(tls, conchFile, 0, bp+20, libc.Int32FromInt32(PROXY_HEADERLEN)+libc.Int32FromInt32(PROXY_HOSTIDLEN)+libc.Int32FromInt32(PATH_MAX)) if readLen < 0 { /* I/O error: lastErrno set by seekAndRead */ _storeLastErrno(tls, pFile, (*TunixFile)(unsafe.Pointer(conchFile)).FlastErrno) rc = libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(1)<= uint64(PATH_MAX) { pathLen = uint64(libc.Int32FromInt32(PATH_MAX) - libc.Int32FromInt32(1)) } libc.X__builtin___memcpy_chk(tls, bp+1061, bp+20+uintptr(libc.Int32FromInt32(PROXY_HEADERLEN)+libc.Int32FromInt32(PROXY_HOSTIDLEN)), pathLen, libc.X__builtin_object_size(tls, bp+1061, 0)) (*(*[1024]int8)(unsafe.Pointer(bp + 1061)))[pathLen] = 0 tempLockPath = bp + 1061 tryOldLockPath = int32(1) /* create a copy of the lock path if the conch is taken */ goto end_takeconch } } else { if hostIdMatch != 0 && !(libc.Xstrncmp(tls, (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath, bp+20+uintptr(libc.Int32FromInt32(PROXY_HEADERLEN)+libc.Int32FromInt32(PROXY_HOSTIDLEN)), uint64(readLen-(libc.Int32FromInt32(PROXY_HEADERLEN)+libc.Int32FromInt32(PROXY_HOSTIDLEN)))) != 0) { /* conch host and lock path match */ goto end_takeconch } } } /* if the conch isn't writable and doesn't match, we can't take it */ if (*TunixFile)(unsafe.Pointer(conchFile)).FopenFlags&int32(O_RDWR) == 0 { rc = int32(SQLITE_BUSY) goto end_takeconch } /* either the conch didn't match or we need to create a new one */ if !((*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath != 0) { _proxyGetLockPath(tls, (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FdbPath, bp+1061, uint64(PATH_MAX)) tempLockPath = bp + 1061 /* create a copy of the lock path _only_ if the conch is taken */ } /* update conch with host and path (this will fail if other process ** has a shared lock already), if the host id matches, use the big ** stick. */ libc.Xfutimes(tls, (*TunixFile)(unsafe.Pointer(conchFile)).Fh, libc.UintptrFromInt32(0)) if hostIdMatch != 0 && !(createConch != 0) { if (*TunixFile)(unsafe.Pointer(conchFile)).FpInode != 0 && (*TunixInodeInfo)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(conchFile)).FpInode)).FnShared > 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 { rc = _proxyConchLock(tls, pFile, bp, int32(EXCLUSIVE_LOCK)) } } else { rc = _proxyConchLock(tls, pFile, bp, int32(EXCLUSIVE_LOCK)) } if rc == SQLITE_OK { writeSize = 0 (*(*[1041]int8)(unsafe.Pointer(bp + 2085)))[0] = libc.Int8FromInt32(PROXY_CONCHVERSION) libc.X__builtin___memcpy_chk(tls, bp+2085+1, bp, uint64(PROXY_HOSTIDLEN), libc.X__builtin_object_size(tls, bp+2085+1, 0)) if (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath != libc.UintptrFromInt32(0) { libc.Xstrlcpy(tls, bp+2085+uintptr(libc.Int32FromInt32(PROXY_HEADERLEN)+libc.Int32FromInt32(PROXY_HOSTIDLEN)), (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath, uint64(PATH_MAX)) } else { libc.Xstrlcpy(tls, bp+2085+uintptr(libc.Int32FromInt32(PROXY_HEADERLEN)+libc.Int32FromInt32(PROXY_HOSTIDLEN)), tempLockPath, uint64(PATH_MAX)) } writeSize = int32(uint64(libc.Int32FromInt32(PROXY_HEADERLEN)+libc.Int32FromInt32(PROXY_HOSTIDLEN)) + libc.Xstrlen(tls, bp+2085+uintptr(libc.Int32FromInt32(PROXY_HEADERLEN)+libc.Int32FromInt32(PROXY_HOSTIDLEN)))) _robust_ftruncate(tls, (*TunixFile)(unsafe.Pointer(conchFile)).Fh, int64(writeSize)) rc = _unixWrite(tls, conchFile, bp+2085, writeSize, 0) _full_fsync(tls, (*TunixFile)(unsafe.Pointer(conchFile)).Fh, 0, 0) /* If we created a new conch file (not just updated the contents of a ** valid conch file), try to match the permissions of the database */ if rc == SQLITE_OK && createConch != 0 { err = (*(*func(*libc.TLS, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(5)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(pFile)).Fh, bp+3128) if err == 0 { cmode = uint16(int32((*(*Tstat)(unsafe.Pointer(bp + 3128))).Fst_mode) & (libc.Int32FromInt32(S_IRUSR) | libc.Int32FromInt32(S_IWUSR) | libc.Int32FromInt32(S_IRGRP) | libc.Int32FromInt32(S_IWGRP) | libc.Int32FromInt32(S_IROTH) | libc.Int32FromInt32(S_IWOTH))) /* try to match the database file R/W permissions, ignore failure */ (*(*func(*libc.TLS, int32, Tmode_t) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(14)].FpCurrent})))(tls, (*TunixFile)(unsafe.Pointer(conchFile)).Fh, cmode) } } } (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(conchFile)).FpMethod)).FxUnlock})))(tls, conchFile, int32(SHARED_LOCK)) goto end_takeconch end_takeconch: ; if rc == SQLITE_OK && (*TunixFile)(unsafe.Pointer(pFile)).FopenFlags != 0 { if (*TunixFile)(unsafe.Pointer(pFile)).Fh >= 0 { _robust_close(tls, pFile, (*TunixFile)(unsafe.Pointer(pFile)).Fh, int32(46019)) } (*TunixFile)(unsafe.Pointer(pFile)).Fh = -int32(1) fd = _robust_open(tls, (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FdbPath, (*TunixFile)(unsafe.Pointer(pFile)).FopenFlags, uint16(0)) if fd >= 0 { (*TunixFile)(unsafe.Pointer(pFile)).Fh = fd } else { rc = _sqlite3CantopenError(tls, int32(46027)) /* SQLITE_BUSY? proxyTakeConch called during locking */ } } if rc == SQLITE_OK && !((*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxy != 0) { if tempLockPath != 0 { v4 = tempLockPath } else { v4 = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath } path = v4 rc = _proxyCreateUnixFile(tls, path, pCtx+16, int32(1)) if rc != SQLITE_OK && rc != int32(SQLITE_NOMEM) && tryOldLockPath != 0 { /* we couldn't create the proxy lock file with the old lock file path ** so try again via auto-naming */ forceNewLockPath = int32(1) tryOldLockPath = 0 goto _2 /* go back to the do {} while start point, try again */ } } if rc == SQLITE_OK { /* Need to make a copy of path if we extracted the value ** from the conch file or the path was allocated on the stack */ if tempLockPath != 0 { (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath = _sqlite3DbStrDup(tls, uintptr(0), tempLockPath) if !((*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath != 0) { rc = int32(SQLITE_NOMEM) } } } if rc == SQLITE_OK { (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchHeld = int32(1) if (*TunixFile)(unsafe.Pointer((*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxy)).FpMethod == uintptr(unsafe.Pointer(&_afpIoMethods)) { afpCtx = (*TunixFile)(unsafe.Pointer((*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxy)).FlockingContext (*TafpLockingContext)(unsafe.Pointer(afpCtx)).FdbPath = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath } } else { (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(conchFile)).FpMethod)).FxUnlock})))(tls, conchFile, NO_LOCK) } return rc goto _2 _2: ; if int32(1) != 0 { goto _3 } goto _1 _1: /* in case we need to retry the :auto: lock file - ** we should never get here except via the 'continue' call. */ } return r } // C documentation // // /* // ** If pFile holds a lock on a conch file, then release that lock. // */ func _proxyReleaseConch(tls *libc.TLS, pFile uintptr) (r int32) { var conchFile, pCtx uintptr var rc int32 _, _, _ = conchFile, pCtx, rc rc = SQLITE_OK /* Name of the conch file */ pCtx = (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext conchFile = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchFile if (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchHeld > 0 { rc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(conchFile)).FpMethod)).FxUnlock})))(tls, conchFile, NO_LOCK) } (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchHeld = 0 return rc } // C documentation // // /* // ** Given the name of a database file, compute the name of its conch file. // ** Store the conch filename in memory obtained from sqlite3_malloc64(). // ** Make *pConchPath point to the new name. Return SQLITE_OK on success // ** or SQLITE_NOMEM if unable to obtain memory. // ** // ** The caller is responsible for ensuring that the allocated memory // ** space is eventually freed. // ** // ** *pConchPath is set to NULL if a memory allocation error occurs. // */ func _proxyCreateConchPathname(tls *libc.TLS, dbPath uintptr, pConchPath uintptr) (r int32) { var conchPath, v1 uintptr var i, len1 int32 _, _, _, _ = conchPath, i, len1, v1 /* Loop counter */ len1 = int32(libc.Xstrlen(tls, dbPath)) /* buffer in which to construct conch name */ /* Allocate space for the conch filename and initialize the name to ** the name of the original database file. */ v1 = Xsqlite3_malloc64(tls, uint64(len1+int32(8))) conchPath = v1 *(*uintptr)(unsafe.Pointer(pConchPath)) = v1 if conchPath == uintptr(0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memcpy_chk(tls, conchPath, dbPath, uint64(len1+int32(1)), libc.X__builtin_object_size(tls, conchPath, 0)) /* now insert a "." before the last / character */ i = len1 - int32(1) for { if !(i >= 0) { break } if int32(*(*int8)(unsafe.Pointer(conchPath + uintptr(i)))) == int32('/') { i++ break } goto _2 _2: ; i-- } *(*int8)(unsafe.Pointer(conchPath + uintptr(i))) = int8('.') for i < len1 { *(*int8)(unsafe.Pointer(conchPath + uintptr(i+int32(1)))) = *(*int8)(unsafe.Pointer(dbPath + uintptr(i))) i++ } /* append the "-conch" suffix to the file */ libc.X__builtin___memcpy_chk(tls, conchPath+uintptr(i+int32(1)), __ccgo_ts+4269, uint64(7), libc.X__builtin_object_size(tls, conchPath+uintptr(i+int32(1)), 0)) return SQLITE_OK } // C documentation // // /* Takes a fully configured proxy locking-style unix file and switches // ** the local lock file path // */ func _switchLockProxyPath(tls *libc.TLS, pFile uintptr, path uintptr) (r int32) { var lockProxy, oldPath, pCtx uintptr var rc int32 _, _, _, _ = lockProxy, oldPath, pCtx, rc pCtx = (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext oldPath = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath rc = SQLITE_OK if int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) != NO_LOCK { return int32(SQLITE_BUSY) } /* nothing to do if the path is NULL, :auto: or matches the existing path */ if !(path != 0) || int32(*(*int8)(unsafe.Pointer(path))) == int32('\000') || !(libc.Xstrcmp(tls, path, __ccgo_ts+4058) != 0) || oldPath != 0 && !(libc.Xstrncmp(tls, oldPath, path, uint64(PATH_MAX)) != 0) { return SQLITE_OK } else { lockProxy = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxy (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxy = libc.UintptrFromInt32(0) (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchHeld = 0 if lockProxy != libc.UintptrFromInt32(0) { rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(lockProxy)).FpMethod)).FxClose})))(tls, lockProxy) if rc != 0 { return rc } Xsqlite3_free(tls, lockProxy) } Xsqlite3_free(tls, oldPath) (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath = _sqlite3DbStrDup(tls, uintptr(0), path) } return rc } // C documentation // // /* // ** pFile is a file that has been opened by a prior xOpen call. dbPath // ** is a string buffer at least MAXPATHLEN+1 characters in size. // ** // ** This routine find the filename associated with pFile and writes it // ** int dbPath. // */ func _proxyGetDbPathForUnixFile(tls *libc.TLS, pFile uintptr, dbPath uintptr) (r int32) { var len1 int32 _ = len1 if (*TunixFile)(unsafe.Pointer(pFile)).FpMethod == uintptr(unsafe.Pointer(&_afpIoMethods)) { /* afp style keeps a reference to the db path in the filePath field ** of the struct */ libc.Xstrlcpy(tls, dbPath, (*TafpLockingContext)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FlockingContext)).FdbPath, uint64(PATH_MAX)) } else { if (*TunixFile)(unsafe.Pointer(pFile)).FpMethod == uintptr(unsafe.Pointer(&_dotlockIoMethods)) { /* dot lock style uses the locking context to store the dot lock ** file path */ len1 = int32(libc.Xstrlen(tls, (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext) - libc.Xstrlen(tls, __ccgo_ts+4276)) libc.X__builtin___memcpy_chk(tls, dbPath, (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext, uint64(len1+int32(1)), libc.X__builtin_object_size(tls, dbPath, 0)) } else { /* all other styles use the locking context to store the db file path */ libc.Xstrlcpy(tls, dbPath, (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext, uint64(PATH_MAX)) } } return SQLITE_OK } // C documentation // // /* // ** Takes an already filled in unix file and alters it so all file locking // ** will be performed on the local proxy lock file. The following fields // ** are preserved in the locking context so that they can be restored and // ** the unix structure properly cleaned up at close time: // ** ->lockingContext // ** ->pMethod // */ func _proxyTransformUnixFile(tls *libc.TLS, pFile uintptr, path uintptr) (r int32) { bp := tls.Alloc(3344) defer tls.Free(3344) var err, goLockless, rc int32 var lockPath, pCtx uintptr var _ /* conchInfo at bp+3200 */ Tstat var _ /* dbPath at bp+0 */ [1025]int8 var _ /* fsInfo at bp+1032 */ Tstatfs _, _, _, _, _ = err, goLockless, lockPath, pCtx, rc /* Name of the database file */ lockPath = libc.UintptrFromInt32(0) rc = SQLITE_OK if int32((*TunixFile)(unsafe.Pointer(pFile)).FeFileLock) != NO_LOCK { return int32(SQLITE_BUSY) } _proxyGetDbPathForUnixFile(tls, pFile, bp) if !(path != 0) || int32(*(*int8)(unsafe.Pointer(path))) == int32('\000') || !(libc.Xstrcmp(tls, path, __ccgo_ts+4058) != 0) { lockPath = libc.UintptrFromInt32(0) } else { lockPath = path } pCtx = Xsqlite3_malloc64(tls, uint64(64)) if pCtx == uintptr(0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pCtx, 0, uint64(64), libc.X__builtin_object_size(tls, pCtx, 0)) rc = _proxyCreateConchPathname(tls, bp, pCtx+8) if rc == SQLITE_OK { rc = _proxyCreateUnixFile(tls, (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchFilePath, pCtx, 0) if rc == int32(SQLITE_CANTOPEN) && (*TunixFile)(unsafe.Pointer(pFile)).FopenFlags&int32(O_RDWR) == 0 { goLockless = 0 if (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{_aSyscall[int32(4)].FpCurrent})))(tls, (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchFilePath, bp+3200) == -int32(1) { err = *(*int32)(unsafe.Pointer(libc.X__error(tls))) if err == int32(ENOENT) && libc.Xstatfs(tls, bp, bp+1032) != -int32(1) { goLockless = libc.BoolInt32((*(*Tstatfs)(unsafe.Pointer(bp + 1032))).Ff_flags&uint32(MNT_RDONLY) == uint32(MNT_RDONLY)) } } if goLockless != 0 { (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchHeld = -int32(1) /* read only FS/ lockless */ rc = SQLITE_OK } } } if rc == SQLITE_OK && lockPath != 0 { (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath = _sqlite3DbStrDup(tls, uintptr(0), lockPath) } if rc == SQLITE_OK { (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FdbPath = _sqlite3DbStrDup(tls, uintptr(0), bp) if (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FdbPath == libc.UintptrFromInt32(0) { rc = int32(SQLITE_NOMEM) } } if rc == SQLITE_OK { /* all memory is allocated, proxys are created and assigned, ** switch the locking context and pMethod then return. */ (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FoldLockingContext = (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext = pCtx (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FpOldMethod = (*TunixFile)(unsafe.Pointer(pFile)).FpMethod (*TunixFile)(unsafe.Pointer(pFile)).FpMethod = uintptr(unsafe.Pointer(&_proxyIoMethods)) } else { if (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchFile != 0 { (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer((*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchFile)).FpMethod)).FxClose})))(tls, (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchFile) Xsqlite3_free(tls, (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchFile) } _sqlite3DbFree(tls, uintptr(0), (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath) Xsqlite3_free(tls, (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchFilePath) Xsqlite3_free(tls, pCtx) } return rc } // C documentation // // /* // ** This routine handles sqlite3_file_control() calls that are specific // ** to proxy locking. // */ func _proxyFileControl(tls *libc.TLS, id uintptr, op int32, pArg uintptr) (r int32) { var isProxyStyle, rc int32 var pCtx, pCtx1, pFile, pFile1, proxyPath uintptr _, _, _, _, _, _, _ = isProxyStyle, pCtx, pCtx1, pFile, pFile1, proxyPath, rc switch op { case int32(SQLITE_FCNTL_GET_LOCKPROXYFILE): pFile = id if (*TunixFile)(unsafe.Pointer(pFile)).FpMethod == uintptr(unsafe.Pointer(&_proxyIoMethods)) { pCtx = (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext _proxyTakeConch(tls, pFile) if (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath != 0 { *(*uintptr)(unsafe.Pointer(pArg)) = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath } else { *(*uintptr)(unsafe.Pointer(pArg)) = __ccgo_ts + 4282 } } else { *(*uintptr)(unsafe.Pointer(pArg)) = libc.UintptrFromInt32(0) } return SQLITE_OK case int32(SQLITE_FCNTL_SET_LOCKPROXYFILE): pFile1 = id rc = SQLITE_OK isProxyStyle = libc.BoolInt32((*TunixFile)(unsafe.Pointer(pFile1)).FpMethod == uintptr(unsafe.Pointer(&_proxyIoMethods))) if pArg == libc.UintptrFromInt32(0) || pArg == uintptr(0) { if isProxyStyle != 0 { /* turn off proxy locking - not supported. If support is added for ** switching proxy locking mode off then it will need to fail if ** the journal mode is WAL mode. */ rc = int32(SQLITE_ERROR) } else { /* turn off proxy locking - already off - NOOP */ rc = SQLITE_OK } } else { proxyPath = pArg if isProxyStyle != 0 { pCtx1 = (*TunixFile)(unsafe.Pointer(pFile1)).FlockingContext if !(libc.Xstrcmp(tls, pArg, __ccgo_ts+4058) != 0) || (*TproxyLockingContext)(unsafe.Pointer(pCtx1)).FlockProxyPath != 0 && !(libc.Xstrncmp(tls, (*TproxyLockingContext)(unsafe.Pointer(pCtx1)).FlockProxyPath, proxyPath, uint64(PATH_MAX)) != 0) { rc = SQLITE_OK } else { rc = _switchLockProxyPath(tls, pFile1, proxyPath) } } else { /* turn on proxy file locking */ rc = _proxyTransformUnixFile(tls, pFile1, proxyPath) } } return rc default: /* The call assures that only valid opcodes are sent */ } return int32(SQLITE_ERROR) } /* ** Within this division (the proxying locking implementation) the procedures ** above this point are all utilities. The lock-related methods of the ** proxy-locking sqlite3_io_method object follow. */ // 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 _proxyCheckReservedLock(tls *libc.TLS, id uintptr, pResOut uintptr) (r int32) { var pCtx, pFile, proxy uintptr var rc int32 _, _, _, _ = pCtx, pFile, proxy, rc pFile = id rc = _proxyTakeConch(tls, pFile) if rc == SQLITE_OK { pCtx = (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext if (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchHeld > 0 { proxy = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxy return (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(proxy)).FpMethod)).FxCheckReservedLock})))(tls, proxy, pResOut) } else { /* conchHeld < 0 is lockless */ pResOut = uintptr(0) } } 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. // */ func _proxyLock(tls *libc.TLS, id uintptr, eFileLock int32) (r int32) { var pCtx, pFile, proxy uintptr var rc int32 _, _, _, _ = pCtx, pFile, proxy, rc pFile = id rc = _proxyTakeConch(tls, pFile) if rc == SQLITE_OK { pCtx = (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext if (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchHeld > 0 { proxy = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxy rc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(proxy)).FpMethod)).FxLock})))(tls, proxy, eFileLock) (*TunixFile)(unsafe.Pointer(pFile)).FeFileLock = (*TunixFile)(unsafe.Pointer(proxy)).FeFileLock } else { /* conchHeld < 0 is lockless */ } } return rc } // C documentation // // /* // ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock // ** must be either NO_LOCK or SHARED_LOCK. // ** // ** If the locking level of the file descriptor is already at or below // ** the requested locking level, this routine is a no-op. // */ func _proxyUnlock(tls *libc.TLS, id uintptr, eFileLock int32) (r int32) { var pCtx, pFile, proxy uintptr var rc int32 _, _, _, _ = pCtx, pFile, proxy, rc pFile = id rc = _proxyTakeConch(tls, pFile) if rc == SQLITE_OK { pCtx = (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext if (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchHeld > 0 { proxy = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxy rc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(proxy)).FpMethod)).FxUnlock})))(tls, proxy, eFileLock) (*TunixFile)(unsafe.Pointer(pFile)).FeFileLock = (*TunixFile)(unsafe.Pointer(proxy)).FeFileLock } else { /* conchHeld < 0 is lockless */ } } return rc } // C documentation // // /* // ** Close a file that uses proxy locks. // */ func _proxyClose(tls *libc.TLS, id uintptr) (r int32) { var conchFile, lockProxy, pCtx, pFile uintptr var rc int32 _, _, _, _, _ = conchFile, lockProxy, pCtx, pFile, rc if id != 0 { pFile = id pCtx = (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext lockProxy = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxy conchFile = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchFile rc = SQLITE_OK if lockProxy != 0 { rc = (*(*func(*libc.TLS, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(lockProxy)).FpMethod)).FxUnlock})))(tls, lockProxy, NO_LOCK) if rc != 0 { return rc } rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(lockProxy)).FpMethod)).FxClose})))(tls, lockProxy) if rc != 0 { return rc } Xsqlite3_free(tls, lockProxy) (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxy = uintptr(0) } if conchFile != 0 { if (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchHeld != 0 { rc = _proxyReleaseConch(tls, pFile) if rc != 0 { return rc } } rc = (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(conchFile)).FpMethod)).FxClose})))(tls, conchFile) if rc != 0 { return rc } Xsqlite3_free(tls, conchFile) } _sqlite3DbFree(tls, uintptr(0), (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FlockProxyPath) Xsqlite3_free(tls, (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FconchFilePath) _sqlite3DbFree(tls, uintptr(0), (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FdbPath) /* restore the original locking context and pMethod then close it */ (*TunixFile)(unsafe.Pointer(pFile)).FlockingContext = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FoldLockingContext (*TunixFile)(unsafe.Pointer(pFile)).FpMethod = (*TproxyLockingContext)(unsafe.Pointer(pCtx)).FpOldMethod Xsqlite3_free(tls, pCtx) return (*(*func(*libc.TLS, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*TunixFile)(unsafe.Pointer(pFile)).FpMethod)).FxClose})))(tls, id) } return SQLITE_OK } /* ** 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] */ /* Register all VFSes defined in the aVfs[] array */ i = uint32(0) for { if !(uint64(i) < libc.Uint64FromInt64(1512)/libc.Uint64FromInt64(168)) { break } Xsqlite3_vfs_register(tls, uintptr(unsafe.Pointer(&_aVfs))+uintptr(i)*168, libc.BoolInt32(i == uint32(0))) goto _1 _1: ; i++ } _unixBigLock = _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1)) /* Validate lock assumptions */ /* Number of available locks */ /* 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 */ /* 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 = [9]Tsqlite3_vfs{ 0: { FiVersion: int32(3), FszOsFile: int32(128), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 4300, }, 1: { FiVersion: int32(3), FszOsFile: int32(128), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 4305, }, 2: { FiVersion: int32(3), FszOsFile: int32(128), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 4315, }, 3: { FiVersion: int32(3), FszOsFile: int32(128), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 3933, }, 4: { FiVersion: int32(3), FszOsFile: int32(128), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 4328, }, 5: { FiVersion: int32(3), FszOsFile: int32(128), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 4339, }, 6: { FiVersion: int32(3), FszOsFile: int32(128), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 4350, }, 7: { FiVersion: int32(3), FszOsFile: int32(128), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 4359, }, 8: { FiVersion: int32(3), FszOsFile: int32(128), FmxPathname: int32(MAX_PATHNAME), FzName: __ccgo_ts + 4368, }, } func init() { p := unsafe.Pointer(&_aVfs) *(*uintptr)(unsafe.Add(p, 32)) = uintptr(unsafe.Pointer(&_autolockIoFinder)) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 144)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 152)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 160)) = __ccgo_fp(_unixNextSystemCall) *(*uintptr)(unsafe.Add(p, 200)) = uintptr(unsafe.Pointer(&_nolockIoFinder)) *(*uintptr)(unsafe.Add(p, 208)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 216)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 224)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 232)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 240)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 248)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 256)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 264)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 272)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 280)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 288)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 296)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 304)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 312)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 320)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 328)) = __ccgo_fp(_unixNextSystemCall) *(*uintptr)(unsafe.Add(p, 368)) = uintptr(unsafe.Pointer(&_dotlockIoFinder)) *(*uintptr)(unsafe.Add(p, 376)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 384)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 392)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 400)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 408)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 416)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 424)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 432)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 440)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 448)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 456)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 464)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 472)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 480)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 488)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 496)) = __ccgo_fp(_unixNextSystemCall) *(*uintptr)(unsafe.Add(p, 536)) = uintptr(unsafe.Pointer(&_posixIoFinder)) *(*uintptr)(unsafe.Add(p, 544)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 552)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 560)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 568)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 576)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 584)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 592)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 600)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 608)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 616)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 624)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 632)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 640)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 648)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 656)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 664)) = __ccgo_fp(_unixNextSystemCall) *(*uintptr)(unsafe.Add(p, 704)) = uintptr(unsafe.Pointer(&_posixIoFinder)) *(*uintptr)(unsafe.Add(p, 712)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 720)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 728)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 736)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 744)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 752)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 760)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 768)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 776)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 784)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 792)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 800)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 808)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 816)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 824)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 832)) = __ccgo_fp(_unixNextSystemCall) *(*uintptr)(unsafe.Add(p, 872)) = uintptr(unsafe.Pointer(&_flockIoFinder)) *(*uintptr)(unsafe.Add(p, 880)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 888)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 896)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 904)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 912)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 920)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 928)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 936)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 944)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 952)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 960)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 968)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 976)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 984)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 992)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 1000)) = __ccgo_fp(_unixNextSystemCall) *(*uintptr)(unsafe.Add(p, 1040)) = uintptr(unsafe.Pointer(&_afpIoFinder)) *(*uintptr)(unsafe.Add(p, 1048)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 1056)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 1064)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 1072)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 1080)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 1088)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 1096)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 1104)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 1112)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 1120)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 1128)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 1136)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 1144)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 1152)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 1160)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 1168)) = __ccgo_fp(_unixNextSystemCall) *(*uintptr)(unsafe.Add(p, 1208)) = uintptr(unsafe.Pointer(&_nfsIoFinder)) *(*uintptr)(unsafe.Add(p, 1216)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 1224)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 1232)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 1240)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 1248)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 1256)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 1264)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 1272)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 1280)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 1288)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 1296)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 1304)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 1312)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 1320)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 1328)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 1336)) = __ccgo_fp(_unixNextSystemCall) *(*uintptr)(unsafe.Add(p, 1376)) = uintptr(unsafe.Pointer(&_proxyIoFinder)) *(*uintptr)(unsafe.Add(p, 1384)) = __ccgo_fp(_unixOpen) *(*uintptr)(unsafe.Add(p, 1392)) = __ccgo_fp(_unixDelete) *(*uintptr)(unsafe.Add(p, 1400)) = __ccgo_fp(_unixAccess) *(*uintptr)(unsafe.Add(p, 1408)) = __ccgo_fp(_unixFullPathname) *(*uintptr)(unsafe.Add(p, 1416)) = __ccgo_fp(_unixDlOpen) *(*uintptr)(unsafe.Add(p, 1424)) = __ccgo_fp(_unixDlError) *(*uintptr)(unsafe.Add(p, 1432)) = __ccgo_fp(_unixDlSym) *(*uintptr)(unsafe.Add(p, 1440)) = __ccgo_fp(_unixDlClose) *(*uintptr)(unsafe.Add(p, 1448)) = __ccgo_fp(_unixRandomness) *(*uintptr)(unsafe.Add(p, 1456)) = __ccgo_fp(_unixSleep) *(*uintptr)(unsafe.Add(p, 1464)) = __ccgo_fp(_unixCurrentTime) *(*uintptr)(unsafe.Add(p, 1472)) = __ccgo_fp(_unixGetLastError) *(*uintptr)(unsafe.Add(p, 1480)) = __ccgo_fp(_unixCurrentTimeInt64) *(*uintptr)(unsafe.Add(p, 1488)) = __ccgo_fp(_unixSetSystemCall) *(*uintptr)(unsafe.Add(p, 1496)) = __ccgo_fp(_unixGetSystemCall) *(*uintptr)(unsafe.Add(p, 1504)) = __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 { 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 { 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 + 4379, } func init() { p := unsafe.Pointer(&_memdb_vfs) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_memdbOpen) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_memdbAccess) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_memdbFullPathname) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_memdbDlOpen) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_memdbDlError) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_memdbDlSym) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_memdbDlClose) *(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(_memdbRandomness) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_memdbSleep) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(_memdbGetLastError) *(*uintptr)(unsafe.Add(p, 136)) = __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, 8)) = __ccgo_fp(_memdbClose) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_memdbRead) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_memdbWrite) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_memdbTruncate) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_memdbSync) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_memdbFileSize) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_memdbLock) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_memdbUnlock) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_memdbFileControl) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_memdbDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_memdbFetch) *(*uintptr)(unsafe.Add(p, 144)) = __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)*8)) == 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)*8)) = *(*uintptr)(unsafe.Pointer(_memdb_g.FapMemStore + uintptr(v2)*8)) 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.X__builtin___memset_chk(tls, zBuf, 0, uint64(iAmt), libc.X__builtin_object_size(tls, zBuf, 0)) if iOfst < (*TMemStore)(unsafe.Pointer(p)).Fsz { libc.X__builtin___memcpy_chk(tls, zBuf, (*TMemStore)(unsafe.Pointer(p)).FaData+uintptr(iOfst), uint64((*TMemStore)(unsafe.Pointer(p)).Fsz-iOfst), libc.X__builtin_object_size(tls, zBuf, 0)) } _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.X__builtin___memset_chk(tls, (*TMemStore)(unsafe.Pointer(p)).FaData+uintptr((*TMemStore)(unsafe.Pointer(p)).Fsz), 0, uint64(iOfst-(*TMemStore)(unsafe.Pointer(p)).Fsz), libc.X__builtin_object_size(tls, (*TMemStore)(unsafe.Pointer(p)).FaData+uintptr((*TMemStore)(unsafe.Pointer(p)).Fsz), 0)) } (*TMemStore)(unsafe.Pointer(p)).Fsz = iOfst + int64(iAmt) } libc.X__builtin___memcpy_chk(tls, (*TMemStore)(unsafe.Pointer(p)).FaData+uintptr(iOfst), z, uint64(iAmt), libc.X__builtin_object_size(tls, (*TMemStore)(unsafe.Pointer(p)).FaData+uintptr(iOfst), 0)) _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) 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): 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): 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: 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) 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+4385, 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.X__builtin___memset_chk(tls, pFile, 0, uint64(24), libc.X__builtin_object_size(tls, pFile, 0)) szName = _sqlite3Strlen30(tls, zName) if szName > int32(1) && (int32(*(*int8)(unsafe.Pointer(zName))) == int32('/') || int32(*(*int8)(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)*8)))).FzFName, zName) == 0 { p = *(*uintptr)(unsafe.Pointer(_memdb_g.FapMemStore + uintptr(i)*8)) break } goto _1 _1: ; i++ } if p == uintptr(0) { p = _sqlite3Malloc(tls, uint64(72)+uint64(szName)+uint64(3)) if p == uintptr(0) { Xsqlite3_mutex_leave(tls, pVfsMutex) return int32(SQLITE_NOMEM) } apNew = _sqlite3Realloc(tls, _memdb_g.FapMemStore, uint64(8)*uint64(_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)*8)) = p _memdb_g.FapMemStore = apNew libc.X__builtin___memset_chk(tls, p, 0, uint64(72), libc.X__builtin_object_size(tls, p, 0)) (*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*72 libc.X__builtin___memcpy_chk(tls, (*TMemStore)(unsafe.Pointer(p)).FzFName, zName, uint64(szName+int32(1)), libc.X__builtin_object_size(tls, (*TMemStore)(unsafe.Pointer(p)).FzFName, 0)) (*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(72)) if p == uintptr(0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, p, 0, uint64(72), libc.X__builtin_object_size(tls, p, 0)) (*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+3863, 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+8 */ 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 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.X__builtin___memcpy_chk(tls, pOut, (*TMemStore)(unsafe.Pointer(pStore)).FaData, uint64((*TMemStore)(unsafe.Pointer(pStore)).Fsz), libc.X__builtin_object_size(tls, pOut, 0)) } } return pOut } pBt = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FpBt if pBt == uintptr(0) { return uintptr(0) } szPage = _sqlite3BtreeGetPageSize(tls, pBt) zSql = Xsqlite3_mprintf(tls, __ccgo_ts+4400, libc.VaList(bp+24, 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+4423, 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 + 8)) = uintptr(0) pTo = pOut + uintptr(int64(szPage)*int64(pgno-libc.Int32FromInt32(1))) rc = _sqlite3PagerGet(tls, pPager, uint32(pgno), bp+8, 0) if rc == SQLITE_OK { libc.X__builtin___memcpy_chk(tls, pTo, _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp + 8))), uint64(szPage), libc.X__builtin_object_size(tls, pTo, 0)) } else { libc.X__builtin___memset_chk(tls, pTo, 0, uint64(szPage), libc.X__builtin_object_size(tls, pTo, 0)) } _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) 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+4448, 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+192+8, libc.Uint32FromInt32(1), 2, 0x4) rc = Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp))) libc.SetBitFieldPtr8Uint32(db+192+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 uint64(sz) < uint64(24) { sz = uint32(24) } /*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][124]Tu32 FapSub [0][62]uintptr FaBitmap [496]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 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 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 + 16 + uintptr(bin)*8)) if !(p != 0) { return 0 } } if uint64((*TBitvec)(unsafe.Pointer(p)).FiSize) <= (libc.Uint64FromInt32(BITVEC_SZ)-libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4))/libc.Uint64FromInt64(8)*libc.Uint64FromInt64(8)/libc.Uint64FromInt64(1)*libc.Uint64FromInt32(BITVEC_SZELEM) { return libc.BoolInt32(int32(*(*Tu8)(unsafe.Pointer(p + 16 + uintptr(i/uint32(BITVEC_SZELEM)))))&(int32(1)<<(i&uint32(libc.Int32FromInt32(BITVEC_SZELEM)-libc.Int32FromInt32(1)))) != 0) } else { v1 = i i++ h = uint32(uint64(v1*libc.Uint32FromInt32(1)) % ((libc.Uint64FromInt32(BITVEC_SZ) - libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4)) / libc.Uint64FromInt64(8) * libc.Uint64FromInt64(8) / libc.Uint64FromInt64(4))) for *(*Tu32)(unsafe.Pointer(p + 16 + uintptr(h)*4)) != 0 { if *(*Tu32)(unsafe.Pointer(p + 16 + uintptr(h)*4)) == i { return int32(1) } h = uint32(uint64(h+libc.Uint32FromInt32(1)) % ((libc.Uint64FromInt32(BITVEC_SZ) - libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4)) / libc.Uint64FromInt64(8) * libc.Uint64FromInt64(8) / libc.Uint64FromInt64(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 } i-- for uint64((*TBitvec)(unsafe.Pointer(p)).FiSize) > (libc.Uint64FromInt32(BITVEC_SZ)-libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4))/libc.Uint64FromInt64(8)*libc.Uint64FromInt64(8)/libc.Uint64FromInt64(1)*libc.Uint64FromInt32(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 + 16 + uintptr(bin)*8)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(p + 16 + uintptr(bin)*8)) = _sqlite3BitvecCreate(tls, (*TBitvec)(unsafe.Pointer(p)).FiDivisor) if *(*uintptr)(unsafe.Pointer(p + 16 + uintptr(bin)*8)) == uintptr(0) { return int32(SQLITE_NOMEM) } } p = *(*uintptr)(unsafe.Pointer(p + 16 + uintptr(bin)*8)) } if uint64((*TBitvec)(unsafe.Pointer(p)).FiSize) <= (libc.Uint64FromInt32(BITVEC_SZ)-libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4))/libc.Uint64FromInt64(8)*libc.Uint64FromInt64(8)/libc.Uint64FromInt64(1)*libc.Uint64FromInt32(BITVEC_SZELEM) { p1 = p + 16 + 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 = uint32(uint64(v2*libc.Uint32FromInt32(1)) % ((libc.Uint64FromInt32(BITVEC_SZ) - libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4)) / libc.Uint64FromInt64(8) * libc.Uint64FromInt64(8) / libc.Uint64FromInt64(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 + 16 + uintptr(h)*4)) != 0) { if uint64((*TBitvec)(unsafe.Pointer(p)).FnSet) < (libc.Uint64FromInt32(BITVEC_SZ)-libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4))/libc.Uint64FromInt64(8)*libc.Uint64FromInt64(8)/libc.Uint64FromInt64(4)-libc.Uint64FromInt32(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 + 16 + uintptr(h)*4)) != 0 { if *(*Tu32)(unsafe.Pointer(p + 16 + uintptr(h)*4)) == i { return SQLITE_OK } h++ if uint64(h) >= (libc.Uint64FromInt32(BITVEC_SZ)-libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4))/libc.Uint64FromInt64(8)*libc.Uint64FromInt64(8)/libc.Uint64FromInt64(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 uint64((*TBitvec)(unsafe.Pointer(p)).FnSet) >= (libc.Uint64FromInt32(BITVEC_SZ)-libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4))/libc.Uint64FromInt64(8)*libc.Uint64FromInt64(8)/libc.Uint64FromInt64(4)/libc.Uint64FromInt32(2) { aiValues = _sqlite3DbMallocRaw(tls, uintptr(0), uint64(496)) if aiValues == uintptr(0) { return int32(SQLITE_NOMEM) } else { libc.X__builtin___memcpy_chk(tls, aiValues, p+16, uint64(496), libc.X__builtin_object_size(tls, aiValues, 0)) libc.X__builtin___memset_chk(tls, p+16, 0, uint64(496), libc.X__builtin_object_size(tls, p+16, 0)) (*TBitvec)(unsafe.Pointer(p)).FiDivisor = uint32((uint64((*TBitvec)(unsafe.Pointer(p)).FiSize) + (libc.Uint64FromInt32(BITVEC_SZ)-libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4))/libc.Uint64FromInt64(8)*libc.Uint64FromInt64(8)/libc.Uint64FromInt64(8) - uint64(1)) / ((libc.Uint64FromInt32(BITVEC_SZ) - libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4)) / libc.Uint64FromInt64(8) * libc.Uint64FromInt64(8) / libc.Uint64FromInt64(8))) rc = _sqlite3BitvecSet(tls, p, i) j = uint32(0) for { if !(uint64(j) < (libc.Uint64FromInt32(BITVEC_SZ)-libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4))/libc.Uint64FromInt64(8)*libc.Uint64FromInt64(8)/libc.Uint64FromInt64(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 + 16 + 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 } 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 + 16 + uintptr(bin)*8)) if !(p != 0) { return } } if uint64((*TBitvec)(unsafe.Pointer(p)).FiSize) <= (libc.Uint64FromInt32(BITVEC_SZ)-libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4))/libc.Uint64FromInt64(8)*libc.Uint64FromInt64(8)/libc.Uint64FromInt64(1)*libc.Uint64FromInt32(BITVEC_SZELEM) { p1 = p + 16 + 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.X__builtin___memcpy_chk(tls, aiValues, p+16, uint64(496), libc.X__builtin_object_size(tls, aiValues, 0)) libc.X__builtin___memset_chk(tls, p+16, 0, uint64(496), libc.X__builtin_object_size(tls, p+16, 0)) (*TBitvec)(unsafe.Pointer(p)).FnSet = uint32(0) j = uint32(0) for { if !(uint64(j) < (libc.Uint64FromInt32(BITVEC_SZ)-libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4))/libc.Uint64FromInt64(8)*libc.Uint64FromInt64(8)/libc.Uint64FromInt64(4)) { break } if *(*Tu32)(unsafe.Pointer(aiValues + uintptr(j)*4)) != 0 && *(*Tu32)(unsafe.Pointer(aiValues + uintptr(j)*4)) != i+uint32(1) { h = uint32(uint64((*(*Tu32)(unsafe.Pointer(aiValues + uintptr(j)*4))-libc.Uint32FromInt32(1))*libc.Uint32FromInt32(1)) % ((libc.Uint64FromInt32(BITVEC_SZ) - libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4)) / libc.Uint64FromInt64(8) * libc.Uint64FromInt64(8) / libc.Uint64FromInt64(4))) (*TBitvec)(unsafe.Pointer(p)).FnSet++ for *(*Tu32)(unsafe.Pointer(p + 16 + uintptr(h)*4)) != 0 { h++ if uint64(h) >= (libc.Uint64FromInt32(BITVEC_SZ)-libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4))/libc.Uint64FromInt64(8)*libc.Uint64FromInt64(8)/libc.Uint64FromInt64(4) { h = uint32(0) } } *(*Tu32)(unsafe.Pointer(p + 16 + 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 !(uint64(i) < (libc.Uint64FromInt32(BITVEC_SZ)-libc.Uint64FromInt32(3)*libc.Uint64FromInt64(4))/libc.Uint64FromInt64(8)*libc.Uint64FromInt64(8)/libc.Uint64FromInt64(8)) { break } _sqlite3BitvecDestroy(tls, *(*uintptr)(unsafe.Pointer(p + 16 + uintptr(i)*8))) 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 { FpDirty uintptr FpDirtyTail uintptr FpSynced uintptr 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 { /* 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 { (*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. */ (*TPCache)(unsafe.Pointer(p)).FpDirty = (*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyNext if (*TPCache)(unsafe.Pointer(p)).FpDirty == uintptr(0) { /*OPTIMIZATION-IF-TRUE*/ (*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 { (*TPgHdr)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(pPage)).FpDirtyNext)).FpDirtyPrev = pPage } else { (*TPCache)(unsafe.Pointer(p)).FpDirtyTail = pPage if (*TPCache)(unsafe.Pointer(p)).FbPurgeable != 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) } 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(80) } // 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.X__builtin___memset_chk(tls, p, 0, uint64(80), libc.X__builtin_object_size(tls, p, 0)) (*TPCache)(unsafe.Pointer(p)).FszPage = int32(1) (*TPCache)(unsafe.Pointer(p)).FszExtra = szExtra /* 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 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(uint64((*TPCache)(unsafe.Pointer(pCache)).FszExtra)+(libc.Uint64FromInt64(80)+libc.Uint64FromInt32(7))&uint64(^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 /* 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) 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 pPgHdr = (*Tsqlite3_pcache_page)(unsafe.Pointer(pPage)).FpExtra libc.X__builtin___memset_chk(tls, pPgHdr+32, 0, libc.Uint64FromInt64(80)-uint64(libc.UintptrFromInt32(0)+32), libc.X__builtin_object_size(tls, pPgHdr+32, 0)) (*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*80 libc.X__builtin___memset_chk(tls, (*TPgHdr)(unsafe.Pointer(pPgHdr)).FpExtra, 0, uint64(8), libc.X__builtin_object_size(tls, (*TPgHdr)(unsafe.Pointer(pPgHdr)).FpExtra, 0)) (*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 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++ 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 (*TPCache)(unsafe.Pointer((*TPgHdr)(unsafe.Pointer(p)).FpCache)).FnRefSum-- v2 = p + 56 *(*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)) } } } // C documentation // // /* // ** Increase the reference count of a supplied page by 1. // */ func _sqlite3PcacheRef(tls *libc.TLS, p uintptr) { (*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) { 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 if int32((*TPgHdr)(unsafe.Pointer(p)).Fflags)&(libc.Int32FromInt32(PGHDR_CLEAN)|libc.Int32FromInt32(PGHDR_DONT_WRITE)) != 0 { /*OPTIMIZATION-IF-FALSE*/ p1 = p + 52 *(*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 + 52 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) ^ (libc.Int32FromInt32(PGHDR_DIRTY) | libc.Int32FromInt32(PGHDR_CLEAN))) _pcacheManageDirtyList(tls, p, uint8(PCACHE_DIRTYLIST_ADD)) } } } // 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 _pcacheManageDirtyList(tls, p, uint8(PCACHE_DIRTYLIST_REMOVE)) p1 = p + 52 *(*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 + 52 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(PGHDR_CLEAN)) 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 + 52 *(*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 + 52 *(*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 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 (*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)) } } // 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. */ if (*TPgHdr)(unsafe.Pointer(p)).Fpgno > pgno { _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.X__builtin___memset_chk(tls, (*Tsqlite3_pcache_page)(unsafe.Pointer(pPage1)).FpBuf, 0, uint64((*TPCache)(unsafe.Pointer(pCache)).FszPage), libc.X__builtin_object_size(tls, (*Tsqlite3_pcache_page)(unsafe.Pointer(pPage1)).FpBuf, 0)) 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) { (*(*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(80) defer tls.Free(80) var pTail uintptr var _ /* result at bp+0 */ TPgHdr _ = pTail pTail = bp 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(256) defer tls.Free(256) var i int32 var p, v3 uintptr var _ /* a at bp+0 */ [32]uintptr _, _, _ = i, p, v3 libc.X__builtin___memset_chk(tls, bp, 0, uint64(256), libc.X__builtin_object_size(tls, bp, 0)) 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) { 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) { (*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 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) { (*(*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.Uint64FromInt64(80) + libc.Uint64FromInt32(7)) & uint64(^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))+80) != 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*56 (*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) 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) _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 uint64(p) >= uint64(_pcache1_g.FpStart) && uint64(p) < uint64(_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) Xsqlite3_mutex_leave(tls, _pcache1_g.Fmutex) } else { 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 { 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) if (*TPCache1)(unsafe.Pointer(pCache)).FpFree != 0 || (*TPCache1)(unsafe.Pointer(pCache)).FnPage == uint32(0) && _pcache1InitBulk(tls, pCache) != 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. */ 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*56 (*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 pCache = (*TPgHdr1)(unsafe.Pointer(p)).FpCache 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) { /* 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 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(8)*uint64(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)*8)) 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)*8)) *(*uintptr)(unsafe.Pointer(apNew + uintptr(h)*8)) = 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) { (*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 */ (*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 h = (*TPgHdr1)(unsafe.Pointer(pPage)).FiKey % (*TPCache1)(unsafe.Pointer(pCache)).FnHash pp = (*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(h)*8 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != pPage) { break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 24 } *(*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 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 } _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 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 { pp = (*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(h)*8 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 + 24 } } if h == iStop { break } h = (h + uint32(1)) % (*TPCache1)(unsafe.Pointer(pCache)).FnHash goto _1 _1: } } /******************************************************************************/ /******** sqlite3_pcache Methods **********************************************/ // C documentation // // /* // ** Implementation of the sqlite3_pcache.xInit method. // */ func _pcache1Init(tls *libc.TLS, NotUsed uintptr) (r int32) { _ = NotUsed libc.X__builtin___memset_chk(tls, uintptr(unsafe.Pointer(&_pcache1_g)), 0, uint64(144), libc.X__builtin_object_size(tls, uintptr(unsafe.Pointer(&_pcache1_g)), 0)) /* ** 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))+80, 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.X__builtin___memset_chk(tls, uintptr(unsafe.Pointer(&_pcache1_g)), 0, uint64(144), libc.X__builtin_object_size(tls, uintptr(unsafe.Pointer(&_pcache1_g)), 0)) } // 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 */ sz = int32(uint64(88) + uint64(80)*uint64(_pcache1_g.FseparateCache)) pCache = _sqlite3MallocZero(tls, uint64(sz)) if pCache != 0 { if _pcache1_g.FseparateCache != 0 { pGroup = pCache + 1*88 (*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 + 24 (*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(uint64(szPage+szExtra) + (libc.Uint64FromInt64(56)+libc.Uint64FromInt32(7))&uint64(^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 + 12)) += (*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 + 20 } else { (*TPCache1)(unsafe.Pointer(pCache)).FpnPurgeable = pCache + 48 } 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 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 + 8)) += 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 */ nPinned = (*TPCache1)(unsafe.Pointer(pCache)).FnPage - (*TPCache1)(unsafe.Pointer(pCache)).FnRecyclable 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) } /* 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 _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 + 20)) -= 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)*8)) (*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)*8)) = 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)*8)) 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) 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 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 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. */ 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 + 24 + 40 (*TPgHdr1)(unsafe.Pointer(pPage)).FpLruPrev = pGroup + 24 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 /* The page number really is changing */ Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FpGroup)).Fmutex) /* pPg really is iOld */ hOld = iOld % (*TPCache1)(unsafe.Pointer(pCache)).FnHash pp = (*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(hOld)*8 for *(*uintptr)(unsafe.Pointer(pp)) != pPage { pp = *(*uintptr)(unsafe.Pointer(pp)) + 24 } *(*uintptr)(unsafe.Pointer(pp)) = (*TPgHdr1)(unsafe.Pointer(pPage)).FpNext /* 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)*8)) *(*uintptr)(unsafe.Pointer((*TPCache1)(unsafe.Pointer(pCache)).FapHash + uintptr(hNew)*8)) = 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 Xsqlite3_mutex_enter(tls, (*TPGroup)(unsafe.Pointer(pGroup)).Fmutex) if (*TPCache1)(unsafe.Pointer(pCache)).FnPage != 0 { _pcache1TruncateUnsafe(tls, pCache, uint32(0)) } *(*uint32)(unsafe.Pointer(pGroup + 8)) -= (*TPCache1)(unsafe.Pointer(pCache)).FnMax *(*uint32)(unsafe.Pointer(pGroup + 12)) -= (*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, 16)) = __ccgo_fp(_pcache1Init) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_pcache1Shutdown) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_pcache1Create) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_pcache1Cachesize) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_pcache1Pagecount) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_pcache1Fetch) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_pcache1Unpin) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_pcache1Rekey) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_pcache1Truncate) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_pcache1Destroy) *(*uintptr)(unsafe.Add(p, 96)) = __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.Uint64FromInt64(56) + libc.Uint64FromInt32(7)) & uint64(^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 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) _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 { 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 { FpNextChunk uintptr FaEntry [42]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(56)) 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.Uint64FromInt64(56)+libc.Uint64FromInt32(7))&uint64(^libc.Int32FromInt32(7))) + p (*TRowSet)(unsafe.Pointer(p)).FnFresh = uint16((uint64(N) - (libc.Uint64FromInt64(56)+libc.Uint64FromInt32(7))&uint64(^libc.Int32FromInt32(7))) / libc.Uint64FromInt64(24)) (*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 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(uint64(libc.Int32FromInt32(ROWSET_ALLOCATION_SIZE)-libc.Int32FromInt32(8)) / libc.Uint64FromInt64(24)) } (*TRowSet)(unsafe.Pointer(p)).FnFresh-- v2 = p + 32 v1 = *(*uintptr)(unsafe.Pointer(v2)) *(*uintptr)(unsafe.Pointer(v2)) += 24 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() */ 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 + 50 *(*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(32) defer tls.Free(32) var pTail, v2, v3 uintptr var _ /* head at bp+0 */ TRowSetEntry _, _, _ = pTail, v2, v3 pTail = bp for { 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(320) defer tls.Free(320) var i uint32 var pNext, v3 uintptr var _ /* aBucket at bp+0 */ [40]uintptr _, _, _ = i, pNext, v3 libc.X__builtin___memset_chk(tls, bp, 0, uint64(320), libc.X__builtin_object_size(tls, bp, 0)) 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 !(uint64(i) < libc.Uint64FromInt64(320)/libc.Uint64FromInt64(8)) { 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 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 } } // 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 */ 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 /* 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 + 50 *(*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+8 */ uintptr _, _, _, _, _ = p, pTree, ppPrevTree, v2, p3 /* This routine is never called after sqlite3RowSetNext() */ /* 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 + 40 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+8) (*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 + 50 *(*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 { 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 { FiOffset Ti64 FiHdrOffset Ti64 FpInSavepoint uintptr FnOrig TPgno FiSubRec TPgno FbTruncateOnRelease int32 FaWalData [4]Tu32 } type PagerSavepoint = TPagerSavepoint type TPagerSavepoint1 = struct { 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 { 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 FjournalOff Ti64 FjournalHdr Ti64 FpBackup uintptr FaSavepoint uintptr FnSavepoint int32 FiDataVersion Tu32 FdbFileVers [16]int8 FnMmapOut int32 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)*56 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)*56))).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]int8 _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 if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Ffd)).FpMethods != uintptr(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 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) { _ = 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 */ *(*int8)(unsafe.Pointer(zSuper)) = int8('\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)), uint64(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(*(*int8)(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) } *(*int8)(unsafe.Pointer(zSuper + uintptr(*(*Tu32)(unsafe.Pointer(bp))))) = int8('\000') *(*int8)(unsafe.Pointer(zSuper + uintptr(*(*Tu32)(unsafe.Pointer(bp))+uint32(1)))) = int8('\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) } 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 */ 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]int8{} // 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 */ /* 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)*56))).FiHdrOffset == 0 { (*(*TPagerSavepoint)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).FaSavepoint + uintptr(ii)*56))).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. */ 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.X__builtin___memcpy_chk(tls, zHeader, uintptr(unsafe.Pointer(&_aJournalMagic)), uint64(8), libc.X__builtin_object_size(tls, zHeader, 0)) _sqlite3Put4byte(tls, zHeader+uintptr(8), uint32(0xffffffff)) } else { libc.X__builtin___memset_chk(tls, zHeader, 0, libc.Uint64FromInt64(8)+libc.Uint64FromInt32(4), libc.X__builtin_object_size(tls, zHeader, 0)) } /* The random check-hash initializer */ if int32((*TPager)(unsafe.Pointer(pPager)).FjournalMode) != int32(PAGER_JOURNALMODE_MEMORY) { Xsqlite3_randomness(tls, int32(4), pPager+56) } _sqlite3Put4byte(tls, zHeader+uintptr(libc.Uint64FromInt64(8)+libc.Uint64FromInt32(4)), (*TPager)(unsafe.Pointer(pPager)).FcksumInit) /* The initial database size */ _sqlite3Put4byte(tls, zHeader+uintptr(libc.Uint64FromInt64(8)+libc.Uint64FromInt32(8)), (*TPager)(unsafe.Pointer(pPager)).FdbOrigSize) /* The assumed sector size for this process */ _sqlite3Put4byte(tls, zHeader+uintptr(libc.Uint64FromInt64(8)+libc.Uint64FromInt32(12)), (*TPager)(unsafe.Pointer(pPager)).FsectorSize) /* The page size */ _sqlite3Put4byte(tls, zHeader+uintptr(libc.Uint64FromInt64(8)+libc.Uint64FromInt32(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.X__builtin___memset_chk(tls, zHeader+uintptr(libc.Uint64FromInt64(8)+libc.Uint64FromInt32(20)), 0, uint64(nHeader)-(libc.Uint64FromInt64(8)+libc.Uint64FromInt32(20)), libc.X__builtin_object_size(tls, zHeader+uintptr(libc.Uint64FromInt64(8)+libc.Uint64FromInt32(20)), 0)) /* 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) *(*Ti64)(unsafe.Pointer(pPager + 96)) += 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 */ /* 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)), uint64(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+56) 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 + 96)) += 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 */ 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) /* Calculate the length in bytes and the checksum of zSuper */ nSuper = 0 for { if !(*(*int8)(unsafe.Pointer(zSuper + uintptr(nSuper))) != 0) { break } cksum += uint32(*(*int8)(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 + 96)) += 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)*56))).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)*56 if pgno <= (*TPagerSavepoint)(unsafe.Pointer(p)).FnOrig { rc |= _sqlite3BitvecSet(tls, (*TPagerSavepoint)(unsafe.Pointer(p)).FpInSavepoint, pgno) } 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 _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) { _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. */ 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. */ (*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. */ 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) 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. */ 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) if (*Tsqlite3_file)(unsafe.Pointer((*TPager)(unsafe.Pointer(pPager)).Fjfd)).FpMethods != uintptr(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)) _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) } 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. */ 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 { 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) { _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+12 */ Tu32 var _ /* pPg at bp+0 */ uintptr var _ /* pgno at bp+8 */ TPgno _, _, _, _, _, _, _, _, _, _, _ = aData, isSynced, jfd, ofst, pData, rc, v1, v2, v3, p4, p5 /* True if journal page is synced */ /* isMainJrnl is 0 or 1 */ /* isSavepnt is 0 or 1 */ /* pDone always used on sub-journals */ /* pDone never used on non-savepoint */ aData = (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace /* Temp storage must have already been allocated */ /* 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. */ /* 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+8) 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 + 8)) == uint32(0) || *(*TPgno)(unsafe.Pointer(bp + 8)) == (*TPager)(unsafe.Pointer(pPager)).FlckPgno { return int32(SQLITE_DONE) } if *(*TPgno)(unsafe.Pointer(bp + 8)) > (*TPager)(unsafe.Pointer(pPager)).FdbSize || _sqlite3BitvecTest(tls, pDone, *(*TPgno)(unsafe.Pointer(bp + 8))) != 0 { return SQLITE_OK } if isMainJrnl != 0 { rc = _read32bits(tls, jfd, *(*Ti64)(unsafe.Pointer(pOffset))-int64(4), bp+12) if rc != 0 { return rc } if !(isSavepnt != 0) && _pager_cksum(tls, pPager, aData) != *(*Tu32)(unsafe.Pointer(bp + 12)) { 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 + 8))) rc = v2 } if v3 && v2 != SQLITE_OK { return rc } /* When playing back page 1, restore the nReserve setting */ if *(*TPgno)(unsafe.Pointer(bp + 8)) == 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 + 8))) } 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 + 8))-libc.Uint32FromInt32(1)) * (*TPager)(unsafe.Pointer(pPager)).FpageSize /* 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 + 8)) > (*TPager)(unsafe.Pointer(pPager)).FdbFileSize { (*TPager)(unsafe.Pointer(pPager)).FdbFileSize = *(*TPgno)(unsafe.Pointer(bp + 8)) } if (*TPager)(unsafe.Pointer(pPager)).FpBackup != 0 { _sqlite3BackupUpdate(tls, (*TPager)(unsafe.Pointer(pPager)).FpBackup, *(*TPgno)(unsafe.Pointer(bp + 8)), 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. */ p4 = pPager + 25 *(*Tu8)(unsafe.Pointer(p4)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p4))) | libc.Int32FromInt32(SPILLFLAG_ROLLBACK)) rc = _sqlite3PagerGet(tls, pPager, *(*TPgno)(unsafe.Pointer(bp + 8)), bp, int32(1)) p5 = pPager + 25 *(*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.X__builtin___memcpy_chk(tls, pData, aData, uint64((*TPager)(unsafe.Pointer(pPager)).FpageSize), libc.X__builtin_object_size(tls, pData, 0)) (*(*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 + 8)) == uint32(1) { libc.X__builtin___memcpy_chk(tls, pPager+136, pData+24, uint64(16), libc.X__builtin_object_size(tls, pPager+136, 0)) } _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 int8 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.Int8FromInt32(0) *(*int8)(unsafe.Pointer(zFree + 3)) = v3 v2 = v3 *(*int8)(unsafe.Pointer(zFree + 2)) = v2 v1 = v2 *(*int8)(unsafe.Pointer(zFree + 1)) = v1 *(*int8)(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 } *(*int8)(unsafe.Pointer(zSuperJournal + uintptr(*(*Ti64)(unsafe.Pointer(bp))))) = 0 *(*int8)(unsafe.Pointer(zSuperJournal + uintptr(*(*Ti64)(unsafe.Pointer(bp))+int64(1)))) = 0 zJournal = zSuperJournal for int64(zJournal)-int64(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(*(*int8)(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) 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 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) /* 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.X__builtin___memset_chk(tls, pTmp, 0, uint64(szPage), libc.X__builtin_object_size(tls, pTmp, 0)) _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) { 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) { 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. */ 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 && *(*int8)(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) { *(*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+96, 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(*(*int8)(unsafe.Pointer(zSuper))) != int32('\000')), 0) } if rc == SQLITE_OK && *(*int8)(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.X__builtin___memset_chk(tls, (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace, 0, uint64(4), libc.X__builtin_object_size(tls, (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace, 0)) 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 */ 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.X__builtin___memset_chk(tls, pPager+136, int32(0xff), uint64(16), libc.X__builtin_object_size(tls, pPager+136, 0)) } else { dbFileVers = (*TPgHdr)(unsafe.Pointer(pPg)).FpData + 24 libc.X__builtin___memcpy_chk(tls, pPager+136, dbFileVers, uint64(16), libc.X__builtin_object_size(tls, pPager+136, 0)) } } 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+136) + 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 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 */ 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 + 32 nList++ } goto _1 _1: ; p = (*TPgHdr)(unsafe.Pointer(p)).FpDirty } } else { nList = int32(1) } *(*Tu32)(unsafe.Pointer(pPager + 248 + 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 */ /* 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. */ 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 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 */ /* 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 /* 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+96, pDone, int32(1), int32(1)) } } 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) /* ** 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+96, pDone, int32(1), int32(1)) goto _3 _3: ; ii++ } } /* 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+36) } ii1 = (*TPagerSavepoint)(unsafe.Pointer(pSavepoint)).FiSubRec for { if !(rc == SQLITE_OK && ii1 < (*TPager)(unsafe.Pointer(pPager)).FnSubRec) { break } rc = _pager_playback_one_page(tls, pPager, bp+8, pDone, 0, int32(1)) goto _4 _4: ; ii1++ } } _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 + 15 *(*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 + 15 *(*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 + 232 _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)) 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.X__builtin___memset_chk(tls, pNew+uintptr(pageSize), 0, uint64(8), libc.X__builtin_object_size(tls, pNew+uintptr(pageSize), 0)) } } 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) } (*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 } /* 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.X__builtin___memset_chk(tls, pDest, 0, uint64(N), libc.X__builtin_object_size(tls, pDest, 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. */ 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) { *(*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(). */ 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) { (*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+104) } 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.X__builtin___memset_chk(tls, (*TPgHdr)(unsafe.Pointer(p)).FpExtra, 0, uint64(8), libc.X__builtin_object_size(tls, (*TPgHdr)(unsafe.Pointer(p)).FpExtra, 0)) } else { v2 = _sqlite3MallocZero(tls, uint64(80)+uint64((*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*80 (*TPgHdr)(unsafe.Pointer(p)).Fflags = uint16(PGHDR_MMAP) (*TPgHdr)(unsafe.Pointer(p)).FnRef = int64(1) (*TPgHdr)(unsafe.Pointer(p)).FpPager = pPager } (*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 _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 } 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) 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) 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 */ rc = _sqlite3PagerExclusiveLock(tls, pPager) if rc != SQLITE_OK { return rc } if !((*TPager)(unsafe.Pointer(pPager)).FnoSync != 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) if 0 == iDc&int32(SQLITE_IOCAP_SAFE_APPEND) { libc.X__builtin___memcpy_chk(tls, bp+8, uintptr(unsafe.Pointer(&_aJournalMagic)), uint64(8), libc.X__builtin_object_size(tls, bp+8, 0)) _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)), uint64(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 */ 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.X__builtin___memcpy_chk(tls, pPager+136, pData+24, uint64(16), libc.X__builtin_object_size(tls, pPager+136, 0)) } if pgno > (*TPager)(unsafe.Pointer(pPager)).FdbFileSize { (*TPager)(unsafe.Pointer(pPager)).FdbFileSize = pgno } *(*Tu32)(unsafe.Pointer(pPager + 248 + 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 */ 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++ 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 /* 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 + 248 + 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 { 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) 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+12 */ int32 var _ /* pPager at bp+0 */ uintptr var _ /* szPageDflt at bp+8 */ 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 + 8)) = 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 && *(*int8)(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 && *(*int8)(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) } *(*int8)(unsafe.Pointer(zPathname)) = 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(61898)) } 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. */ pPtr = _sqlite3MallocZero(tls, (libc.Uint64FromInt64(312)+libc.Uint64FromInt32(7))&uint64(^libc.Int32FromInt32(7))+uint64((pcacheSize+libc.Int32FromInt32(7)) & ^libc.Int32FromInt32(7))+uint64(((*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile+libc.Int32FromInt32(7)) & ^libc.Int32FromInt32(7))+uint64(journalFileSize*int32(2))+uint64(__SIZEOF_POINTER__)+uint64(4)+uint64(nPathname)+uint64(1)+uint64(nUriByte)+uint64(nPathname)+uint64(8)+uint64(1)+uint64(nPathname)+uint64(4)+uint64(1)+uint64(3)) if !(pPtr != 0) { _sqlite3DbFree(tls, uintptr(0), zPathname) return int32(SQLITE_NOMEM) } *(*uintptr)(unsafe.Pointer(bp)) = pPtr pPtr += uintptr((libc.Uint64FromInt64(312) + libc.Uint64FromInt32(7)) & uint64(^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.X__builtin___memcpy_chk(tls, pPtr, bp, uint64(__SIZEOF_POINTER__), libc.X__builtin_object_size(tls, pPtr, 0)) 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.X__builtin___memcpy_chk(tls, pPtr, zPathname, uint64(nPathname), libc.X__builtin_object_size(tls, pPtr, 0)) pPtr += uintptr(nPathname + int32(1)) if zUri != 0 { libc.X__builtin___memcpy_chk(tls, pPtr, zUri, uint64(nUriByte), libc.X__builtin_object_size(tls, pPtr, 0)) pPtr += uintptr(nUriByte) } else { pPtr++ } } /* Fill in Pager.zJournal */ if nPathname > 0 { (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FzJournal = pPtr libc.X__builtin___memcpy_chk(tls, pPtr, zPathname, uint64(nPathname), libc.X__builtin_object_size(tls, pPtr, 0)) pPtr += uintptr(nPathname) libc.X__builtin___memcpy_chk(tls, pPtr, __ccgo_ts+4490, uint64(8), libc.X__builtin_object_size(tls, pPtr, 0)) 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.X__builtin___memcpy_chk(tls, pPtr, zPathname, uint64(nPathname), libc.X__builtin_object_size(tls, pPtr, 0)) pPtr += uintptr(nPathname) libc.X__builtin___memcpy_chk(tls, pPtr, __ccgo_ts+4499, uint64(4), libc.X__builtin_object_size(tls, pPtr, 0)) 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 && *(*int8)(unsafe.Pointer(zFilename)) != 0) { goto _2 } *(*int32)(unsafe.Pointer(bp + 12)) = 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+12) v4 = libc.BoolInt32(*(*int32)(unsafe.Pointer(bp + 12))&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 + 12))&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))) if *(*Tu32)(unsafe.Pointer(bp + 8)) < (*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 + 8)) = uint32(SQLITE_MAX_DEFAULT_PAGE_SIZE) } else { *(*Tu32)(unsafe.Pointer(bp + 8)) = (*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+4504, 0)) if iDc&int32(SQLITE_IOCAP_IMMUTABLE) != 0 || Xsqlite3_uri_boolean(tls, (*TPager)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FzFilename, __ccgo_ts+4511, 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 { rc = _sqlite3PagerSetPagesize(tls, *(*uintptr)(unsafe.Pointer(bp)), bp+8, -int32(1)) } /* Initialize the PCache object. */ if rc == SQLITE_OK { nExtra = (nExtra + int32(7)) & ^libc.Int32FromInt32(7) if !(memDb != 0) { v5 = __ccgo_fp(_pagerStress) } else { v5 = uintptr(0) } rc = _sqlite3PcacheOpen(tls, int32(*(*Tu32)(unsafe.Pointer(bp + 8))), 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) (*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) _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)) _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(*(*int8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(1))))) != 0 || int32(*(*int8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(2))))) != 0 || int32(*(*int8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(3))))) != 0 || int32(*(*int8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(4))))) != 0 { zName-- } p = zName - uintptr(4) - uintptr(8) 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))) *(*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 */ 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.X__builtin___memset_chk(tls, (*TPgHdr)(unsafe.Pointer(pPg)).FpData, 0, uint64((*TPager)(unsafe.Pointer(pPager)).FpageSize), libc.X__builtin_object_size(tls, (*TPgHdr)(unsafe.Pointer(pPg)).FpData, 0)) } else { *(*Tu32)(unsafe.Pointer(pPager + 248 + 1*4))++ rc = _readDbPage(tls, pPg) if rc != SQLITE_OK { goto pager_acquire_err } } } return SQLITE_OK goto pager_acquire_err pager_acquire_err: ; 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(32) defer tls.Free(32) var bMmapOk, rc int32 var _ /* iFrame at bp+8 */ Tu32 var _ /* pData at bp+16 */ uintptr var _ /* pPg at bp+0 */ uintptr _, _ = bMmapOk, rc rc = SQLITE_OK *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*Tu32)(unsafe.Pointer(bp + 8)) = 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)) /* 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(62747)) } if bMmapOk != 0 && (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { rc = _sqlite3WalFindFrame(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, pgno, bp+8) if rc != SQLITE_OK { *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) return rc } } if bMmapOk != 0 && *(*Tu32)(unsafe.Pointer(bp + 8)) == uint32(0) { *(*uintptr)(unsafe.Pointer(bp + 16)) = 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+16) if rc == SQLITE_OK && *(*uintptr)(unsafe.Pointer(bp + 16)) != 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 + 16)), bp) } else { _sqlite3OsUnfetch(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int64(pgno-libc.Uint32FromInt32(1))*(*TPager)(unsafe.Pointer(pPager)).FpageSize, *(*uintptr)(unsafe.Pointer(bp + 16))) } if *(*uintptr)(unsafe.Pointer(bp)) != 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 *(*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 pPage = _sqlite3PcacheFetch(tls, (*TPager)(unsafe.Pointer(pPager)).FpPCache, pgno, 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) { if int32((*TDbPage)(unsafe.Pointer(pPg)).Fflags)&int32(PGHDR_MMAP) != 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 */ /* 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 /* 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 */ /* 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) } } } /* 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 { (*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 } (*TPager)(unsafe.Pointer(pPager)).FsubjInMemory = uint8(subjInMemory) if int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_READER) { 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 } } 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. */ 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 + 52 *(*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 + 96)) += int64(8) + (*TPager)(unsafe.Pointer(pPager)).FpageSize (*TPager)(unsafe.Pointer(pPager)).FnRec++ rc = _sqlite3BitvecSet(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal, (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno) rc |= _addToSavepointBitvecs(tls, pPager, (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno) 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. */ /* 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 } } /* 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. */ if (*TPager)(unsafe.Pointer(pPager)).FpInJournal != uintptr(0) && _sqlite3BitvecTestNotNull(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal, (*TPgHdr)(unsafe.Pointer(pPg)).Fpgno) == 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 + 52 *(*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 + 52 *(*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. */ p1 = pPager + 25 *(*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) } } 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 { ii = 0 for { if !(ii < nPage) { break } pPage1 = _sqlite3PagerLookup(tls, pPager, pg1+uint32(ii)) if pPage1 != 0 { p5 = pPage1 + 52 *(*Tu16)(unsafe.Pointer(p5)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p5))) | libc.Int32FromInt32(PGHDR_NEED_SYNC)) _sqlite3PagerUnrefNotNull(tls, pPage1) } goto _4 _4: ; ii++ } } p6 = pPager + 25 *(*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 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) { 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 + 52 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(PGHDR_DONT_WRITE)) p2 = pPg + 52 *(*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 /* 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. */ _ = isDirectMode if !((*TPager)(unsafe.Pointer(pPager)).FchangeCountDone != 0) && (*TPager)(unsafe.Pointer(pPager)).FdbSize > uint32(0) { /* Reference to page 1 */ /* Open page 1 of the file for writing. */ rc = _sqlite3PagerGet(tls, pPager, uint32(1), bp, 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 { 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 + 248 + 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.X__builtin___memcpy_chk(tls, pPager+136, pCopy, uint64(16), libc.X__builtin_object_size(tls, pPager+136, 0)) (*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) { 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 if rc == SQLITE_OK { 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 */ /* 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 } 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) } 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 { 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) 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++ /* 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) { (*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. */ 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) } } /* 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.Uint64FromInt64(80)+libc.Uint64FromInt32(5)*libc.Uint64FromInt64(8)))) 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) { eStat -= int32(SQLITE_DBSTATUS_CACHE_HIT) *(*Tu64)(unsafe.Pointer(pnVal)) += uint64(*(*Tu32)(unsafe.Pointer(pPager + 248 + uintptr(eStat)*4))) if reset != 0 { *(*Tu32)(unsafe.Pointer(pPager + 248 + 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 */ /* 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(56)*uint64(nSavepoint)) if !(aNew != 0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, aNew+uintptr(nCurrent)*56, 0, uint64(nSavepoint-nCurrent)*uint64(56), libc.X__builtin_object_size(tls, aNew+uintptr(nCurrent)*56, 0)) (*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)*56))).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)*56))).FiOffset = (*TPager)(unsafe.Pointer(pPager)).FjournalOff } else { (*(*TPagerSavepoint)(unsafe.Pointer(aNew + uintptr(ii)*56))).FiOffset = int64((*TPager)(unsafe.Pointer(pPager)).FsectorSize) } (*(*TPagerSavepoint)(unsafe.Pointer(aNew + uintptr(ii)*56))).FiSubRec = (*TPager)(unsafe.Pointer(pPager)).FnSubRec (*(*TPagerSavepoint)(unsafe.Pointer(aNew + uintptr(ii)*56))).FpInSavepoint = _sqlite3BitvecCreate(tls, (*TPager)(unsafe.Pointer(pPager)).FdbSize) (*(*TPagerSavepoint)(unsafe.Pointer(aNew + uintptr(ii)*56))).FbTruncateOnRelease = int32(1) if !((*(*TPagerSavepoint)(unsafe.Pointer(aNew + uintptr(ii)*56))).FpInSavepoint != 0) { return int32(SQLITE_NOMEM) } if (*TPager)(unsafe.Pointer(pPager)).FpWal != uintptr(0) { _sqlite3WalSavepoint(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, aNew+uintptr(ii)*56+36) } (*TPager)(unsafe.Pointer(pPager)).FnSavepoint = ii + int32(1) goto _1 _1: ; ii++ } return rc } func _sqlite3PagerOpenSavepoint(tls *libc.TLS, pPager uintptr, nSavepoint int32) (r int32) { 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 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)*56))).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)*56 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) } (*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))*56 } pSavepoint = v3 rc = _pagerPlaybackSavepoint(tls, pPager, pSavepoint) } } } 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]int8{} // 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 */ /* In order to be able to rollback, an in-memory database must journal ** the page we are moving from. */ 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 } /* 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 + 52 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) & ^libc.Int32FromInt32(PGHDR_NEED_SYNC)) pPgOld = _sqlite3PagerLookup(tls, pPager, pgno) if pPgOld != 0 { if (*TPgHdr)(unsafe.Pointer(pPgOld)).FnRef > int64(1) { _sqlite3PagerUnrefNotNull(tls, pPgOld) return _sqlite3CorruptError(tls, int32(64313)) } p4 = pPg + 52 *(*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 { _sqlite3BitvecClear(tls, (*TPager)(unsafe.Pointer(pPager)).FpInJournal, needSyncPgno, (*TPager)(unsafe.Pointer(pPager)).FpTmpSpace) } return rc } p5 = *(*uintptr)(unsafe.Pointer(bp)) + 52 *(*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) { (*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) { 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) { 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 */ /* 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. */ /* 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 { if eMode != int32(PAGER_JOURNALMODE_MEMORY) && eMode != int32(PAGER_JOURNALMODE_OFF) { eMode = int32(eOld) } } if eMode != int32(eOld) { /* Change the journal mode. */ (*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. */ 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) if state == PAGER_OPEN { rc = _sqlite3PagerSharedLock(tls, pPager) } if int32((*TPager)(unsafe.Pointer(pPager)).FeState) == int32(PAGER_READER) { 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) } } } } 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) { 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 + 112 } // C documentation // // /* // ** Unless this is an in-memory or temporary database, clear the pager cache. // */ func _sqlite3PagerClearCache(tls *libc.TLS, pPager uintptr) { 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+4521, 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 */ 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 /* 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+296) } _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 */ 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 /* 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) { _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 { 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(uint64(8) * uint64(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.X__builtin___memset_chk(tls, apNew+uintptr((*TWal)(unsafe.Pointer(pWal)).FnWiData)*8, 0, uint64(8)*uint64(iPage+libc.Int32FromInt32(1)-(*TWal)(unsafe.Pointer(pWal)).FnWiData), libc.X__builtin_object_size(tls, apNew+uintptr((*TWal)(unsafe.Pointer(pWal)).FnWiData)*8, 0)) (*TWal)(unsafe.Pointer(pWal)).FapWiData = apNew (*TWal)(unsafe.Pointer(pWal)).FnWiData = iPage + int32(1) } /* Request a pointer to the required page from the VFS */ if int32((*TWal)(unsafe.Pointer(pWal)).FexclusiveMode) == int32(WAL_HEAPMEMORY_MODE) { *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPage)*8)) = _sqlite3MallocZero(tls, libc.Uint64FromInt64(2)*uint64(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint64FromInt32(HASHTABLE_NPAGE)*libc.Uint64FromInt64(4)) if !(*(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPage)*8)) != 0) { rc = int32(SQLITE_NOMEM) } } else { rc = _sqlite3OsShmMap(tls, (*TWal)(unsafe.Pointer(pWal)).FpDbFd, iPage, int32(libc.Uint64FromInt64(2)*uint64(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint64FromInt32(HASHTABLE_NPAGE)*libc.Uint64FromInt64(4)), int32((*TWal)(unsafe.Pointer(pWal)).FwriteLock), (*TWal)(unsafe.Pointer(pWal)).FapWiData+uintptr(iPage)*8) 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 + 66 *(*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)*8)) return rc } func _walIndexPage(tls *libc.TLS, pWal uintptr, iPage int32, ppPage uintptr) (r int32) { var v1 uintptr var v2 bool _, _ = v1, v2 if v2 = (*TWal)(unsafe.Pointer(pWal)).FnWiData <= iPage; !v2 { v1 = *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPage)*8)) *(*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) { return *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData)) + uintptr(libc.Uint64FromInt64(48)/libc.Uint64FromInt32(2))*4 } // C documentation // // /* // ** Return a pointer to the WalIndexHdr structure in the wal-index. // */ func _walIndexHdr(tls *libc.TLS, pWal uintptr) (r uintptr) { 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 } 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 } } } *(*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(uint64(libc.UintptrFromInt32(0) + 40)) (*TWal)(unsafe.Pointer(pWal)).Fhdr.FisInit = uint8(1) (*TWal)(unsafe.Pointer(pWal)).Fhdr.FiVersion = uint32(WALINDEX_MAX_VERSION) _walChecksumBytes(tls, int32(1), pWal+72, nCksum, uintptr(0), pWal+72+40) /* Possible TSAN false-positive. See tag-20200519-1 */ libc.X__builtin___memcpy_chk(tls, aHdr+1*48, pWal+72, uint64(48), libc.X__builtin_object_size(tls, aHdr+1*48, 0)) _walShmBarrier(tls, pWal) libc.X__builtin___memcpy_chk(tls, aHdr, pWal+72, uint64(48), libc.X__builtin_object_size(tls, aHdr, 0)) } // 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 + 72 + 24 _sqlite3Put4byte(tls, aFrame, iPage) _sqlite3Put4byte(tls, aFrame+4, nTruncate) if (*TWal)(unsafe.Pointer(pWal)).FiReCksum == uint32(0) { libc.X__builtin___memcpy_chk(tls, aFrame+8, pWal+72+32, uint64(8), libc.X__builtin_object_size(tls, aFrame+8, 0)) 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.X__builtin___memset_chk(tls, aFrame+8, 0, uint64(16), libc.X__builtin_object_size(tls, aFrame+8, 0)) } } // 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 + 72 + 24 /* Page number of the frame */ /* 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+72+32, aFrame+8, uint64(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) { 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+8) 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.Uint64FromInt64(48)*libc.Uint64FromInt32(2)+libc.Uint64FromInt64(40))/libc.Uint64FromInt64(4))*4 (*TWalHashLoc)(unsafe.Pointer(pLoc)).FiZero = uint32(0) } else { (*TWalHashLoc)(unsafe.Pointer(pLoc)).FiZero = uint32(libc.Uint64FromInt32(HASHTABLE_NPAGE) - (libc.Uint64FromInt64(48)*libc.Uint64FromInt32(2)+libc.Uint64FromInt64(40))/libc.Uint64FromInt64(4) + uint64((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((uint64(iFrame+uint32(HASHTABLE_NPAGE)) - (libc.Uint64FromInt32(HASHTABLE_NPAGE) - (libc.Uint64FromInt64(48)*libc.Uint64FromInt32(2)+libc.Uint64FromInt64(40))/libc.Uint64FromInt64(4)) - uint64(1)) / uint64(HASHTABLE_NPAGE)) 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) if iHash == 0 { return *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData)) + uintptr((libc.Uint64FromInt64(48)*libc.Uint64FromInt32(2)+libc.Uint64FromInt64(40))/libc.Uint64FromInt64(4)+uint64(iFrame)-uint64(1))*4)) } return *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iHash)*8)) + uintptr((uint64(iFrame-uint32(1))-(libc.Uint64FromInt32(HASHTABLE_NPAGE)-(libc.Uint64FromInt64(48)*libc.Uint64FromInt32(2)+libc.Uint64FromInt64(40))/libc.Uint64FromInt64(4)))%uint64(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(32) defer tls.Free(32) var i, iLimit, nByte int32 var _ /* sLoc at bp+0 */ TWalHashLoc _, _, _ = i, iLimit, nByte /* Hash table location */ iLimit = 0 /* Used to iterate through aHash[] */ 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) */ 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) 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(int64((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash) - int64((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno+uintptr(iLimit)*4)) libc.X__builtin___memset_chk(tls, (*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno+uintptr(iLimit)*4, 0, uint64(nByte), libc.X__builtin_object_size(tls, (*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno+uintptr(iLimit)*4, 0)) } // 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(32) defer tls.Free(32) 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) /* 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(int64((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash+uintptr(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))*2) - int64((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno)) libc.X__builtin___memset_chk(tls, (*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno, 0, uint64(nByte), libc.X__builtin_object_size(tls, (*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno, 0)) } /* 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) } /* 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(66239)) } goto _1 _1: ; iKey = _walNextHash(tls, iKey) } *(*Tu32)(unsafe.Pointer((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno + uintptr(idx-int32(1))*4)) = iPage *(*Tht_slot)(unsafe.Pointer((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash + uintptr(iKey)*2)) = uint16(idx) } 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 uint64 var _ /* aBuf at bp+8 */ [32]Tu8 var _ /* aShare at bp+40 */ uintptr var _ /* nSize at bp+0 */ Ti64 var _ /* nTruncate at bp+52 */ Tu32 var _ /* pgno at bp+48 */ 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. */ 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.X__builtin___memset_chk(tls, pWal+72, 0, uint64(48), libc.X__builtin_object_size(tls, pWal+72, 0)) 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.X__builtin___memcpy_chk(tls, pWal+72+32, bp+8+16, uint64(8), libc.X__builtin_object_size(tls, pWal+72+32, 0)) /* 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+72+24) if *(*Tu32)(unsafe.Pointer(pWal + 72 + 24)) != _sqlite3Get4byte(tls, bp+8+24) || *(*Tu32)(unsafe.Pointer(pWal + 72 + 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(66371)) goto finished } /* Malloc a buffer to read frames into. */ szFrame = szPage + int32(WAL_FRAME_HDRSIZE) aFrame = Xsqlite3_malloc64(tls, uint64(szFrame)+(libc.Uint64FromInt64(2)*uint64(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint64FromInt32(HASHTABLE_NPAGE)*libc.Uint64FromInt64(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 uint64(iLastFrame) < libc.Uint64FromInt32(HASHTABLE_NPAGE)-(libc.Uint64FromInt64(48)*libc.Uint64FromInt32(2)+libc.Uint64FromInt64(40))/libc.Uint64FromInt64(4)+uint64(iPg*uint32(HASHTABLE_NPAGE)) { v2 = uint64(iLastFrame) } else { v2 = libc.Uint64FromInt32(HASHTABLE_NPAGE) - (libc.Uint64FromInt64(48)*libc.Uint64FromInt32(2)+libc.Uint64FromInt64(40))/libc.Uint64FromInt64(4) + uint64(iPg*uint32(HASHTABLE_NPAGE)) } /* Index of last frame read */ iLast = uint32(v2) if iPg == uint32(0) { v3 = uint64(0) } else { v3 = libc.Uint64FromInt32(HASHTABLE_NPAGE) - (libc.Uint64FromInt64(48)*libc.Uint64FromInt32(2)+libc.Uint64FromInt64(40))/libc.Uint64FromInt64(4) + uint64((iPg-uint32(1))*uint32(HASHTABLE_NPAGE)) } iFirst = uint32(uint64(1) + v3) rc = _walIndexPage(tls, pWal, int32(iPg), bp+40) if *(*uintptr)(unsafe.Pointer(bp + 40)) == uintptr(0) { break } *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPg)*8)) = 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+48, bp+52, aData, aFrame) if !(isValid != 0) { break } rc = _walIndexAppend(tls, pWal, iFrame, *(*Tu32)(unsafe.Pointer(bp + 48))) if rc != SQLITE_OK { break } /* If nTruncate is non-zero, this is a commit record. */ if *(*Tu32)(unsafe.Pointer(bp + 52)) != 0 { (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame = iFrame (*TWal)(unsafe.Pointer(pWal)).Fhdr.FnPage = *(*Tu32)(unsafe.Pointer(bp + 52)) (*TWal)(unsafe.Pointer(pWal)).Fhdr.FszPage = uint16(szPage&libc.Int32FromInt32(0xff00) | szPage>>libc.Int32FromInt32(16)) aFrameCksum[0] = *(*Tu32)(unsafe.Pointer(pWal + 72 + 24)) aFrameCksum[int32(1)] = *(*Tu32)(unsafe.Pointer(pWal + 72 + 24 + 1*4)) } goto _4 _4: ; iFrame++ } *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(iPg)*8)) = *(*uintptr)(unsafe.Pointer(bp + 40)) if iPg == uint32(0) { v5 = libc.Uint64FromInt64(48)*libc.Uint64FromInt32(2) + libc.Uint64FromInt64(40) } else { v5 = uint64(0) } nHdr = uint32(v5) nHdr32 = uint32(uint64(nHdr) / uint64(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.X__builtin___memcpy_chk(tls, *(*uintptr)(unsafe.Pointer(bp + 40))+uintptr(nHdr32)*4, aPrivate+uintptr(nHdr32)*4, libc.Uint64FromInt64(2)*uint64(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint64FromInt32(HASHTABLE_NPAGE)*libc.Uint64FromInt64(4)-uint64(nHdr), libc.X__builtin_object_size(tls, *(*uintptr)(unsafe.Pointer(bp + 40))+uintptr(nHdr32)*4, 0)) if iFrame <= iLast { break } goto _1 _1: ; iPg++ } Xsqlite3_free(tls, aFrame) } goto finished finished: ; if rc == SQLITE_OK { *(*Tu32)(unsafe.Pointer(pWal + 72 + 24)) = aFrameCksum[0] *(*Tu32)(unsafe.Pointer(pWal + 72 + 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) } _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)*32 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++ } } *(*uintptr)(unsafe.Pointer(paRight)) = aLeft *(*int32)(unsafe.Pointer(pnRight)) = iOut libc.X__builtin___memcpy_chk(tls, aLeft, aTmp, uint64(2)*uint64(iOut), libc.X__builtin_object_size(tls, aLeft, 0)) } // 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). */ iLast = (*TWal)(unsafe.Pointer(pWal)).Fhdr.FmxFrame /* Allocate space for the WalIterator object. */ nSegment = _walFramePage(tls, iLast) + int32(1) nByte = int64(uint64(40) + uint64(nSegment-libc.Int32FromInt32(1))*uint64(32) + uint64(iLast)*uint64(2)) if iLast > uint32(HASHTABLE_NPAGE) { v1 = uint32(HASHTABLE_NPAGE) } else { v1 = iLast } p = Xsqlite3_malloc64(tls, uint64(nByte)+uint64(2)*uint64(v1)) if !(p != 0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, p, 0, uint64(nByte), libc.X__builtin_object_size(tls, p, 0)) (*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 + 24)) = int32(iLast - (*(*TWalHashLoc)(unsafe.Pointer(bp))).FiZero) } else { *(*int32)(unsafe.Pointer(bp + 24)) = int32((int64((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash) - int64((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaPgno)) / 4) } aIndex = p + 8 + uintptr((*TWalIterator)(unsafe.Pointer(p)).FnSegment)*32 + uintptr((*(*TWalHashLoc)(unsafe.Pointer(bp))).FiZero)*2 (*(*TWalHashLoc)(unsafe.Pointer(bp))).FiZero++ j = 0 for { if !(j < *(*int32)(unsafe.Pointer(bp + 24))) { 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+24) (*(*TWalSegment)(unsafe.Pointer(p + 8 + uintptr(i)*32))).FiZero = int32((*(*TWalHashLoc)(unsafe.Pointer(bp))).FiZero) (*(*TWalSegment)(unsafe.Pointer(p + 8 + uintptr(i)*32))).FnEntry = *(*int32)(unsafe.Pointer(bp + 24)) (*(*TWalSegment)(unsafe.Pointer(p + 8 + uintptr(i)*32))).FaIndex = aIndex (*(*TWalSegment)(unsafe.Pointer(p + 8 + uintptr(i)*32))).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 { 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 *(*Tu32)(unsafe.Pointer(pInfo + 4 + uintptr(i)*4)) = iMark _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) } 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 /* 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(67181)) } 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+8, bp+12) { if libc.AtomicLoadPInt32(db+432) != 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 + 12)) <= nBackfill || *(*Tu32)(unsafe.Pointer(bp + 12)) > mxSafeFrame || *(*Tu32)(unsafe.Pointer(bp + 8)) > mxPage { continue } iOffset = int64(WAL_HDRSIZE) + int64(*(*Tu32)(unsafe.Pointer(bp + 12))-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 + 8))-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 { *(*Tu32)(unsafe.Pointer(pInfo)) = mxSafeFrame } } /* 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 { 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) 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+4576, 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 */ /* 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. */ /* 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.X__builtin___memcpy_chk(tls, bp+8, aHdr, uint64(48), libc.X__builtin_object_size(tls, bp+8, 0)) /* Possible TSAN false-positive */ _walShmBarrier(tls, pWal) libc.X__builtin___memcpy_chk(tls, bp+56, aHdr+1*48, uint64(48), libc.X__builtin_object_size(tls, bp+56, 0)) if libc.Xmemcmp(tls, bp+8, bp+56, uint64(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.Uint64FromInt64(48)-libc.Uint64FromInt64(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+72, bp+8, uint64(48)) != 0 { *(*int32)(unsafe.Pointer(pChanged)) = int32(1) libc.X__builtin___memcpy_chk(tls, pWal+72, bp+8, uint64(48), libc.X__builtin_object_size(tls, pWal+72, 0)) (*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. */ rc = _walIndexPage(tls, pWal, 0, bp) if rc != SQLITE_OK { /* READONLY changed to OK in walIndexPage */ if rc == libc.Int32FromInt32(SQLITE_READONLY)|libc.Int32FromInt32(5)<writeLock ** is zero, which prevents the SHM from growing */ } /* 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+52 */ Tu32 var _ /* pDummy at bp+40 */ uintptr var _ /* pgno at bp+48 */ 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 */ /* 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.Uint64FromInt64(2)*uint64(libc.Int32FromInt32(HASHTABLE_NPAGE)*libc.Int32FromInt32(2))+libc.Uint64FromInt32(HASHTABLE_NPAGE)*libc.Uint64FromInt64(4)), 0, bp+40) /* SQLITE_OK not possible for read-only connection */ if rc != libc.Int32FromInt32(SQLITE_READONLY)|libc.Int32FromInt32(5)<hdr. */ libc.X__builtin___memcpy_chk(tls, pWal+72, _walIndexHdr(tls, pWal), uint64(48), libc.X__builtin_object_size(tls, pWal+72, 0)) /* 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+72+32, bp+8+16, uint64(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 */ 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 + 72 + 24)) aSaveCksum[int32(1)] = *(*Tu32)(unsafe.Pointer(pWal + 72 + 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+48, bp+52, 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 + 52)) != 0 { rc = -int32(1) break } goto _3 _3: ; iOffset += int64(szFrame) } *(*Tu32)(unsafe.Pointer(pWal + 72 + 24)) = aSaveCksum[0] *(*Tu32)(unsafe.Pointer(pWal + 72 + 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)*8))) *(*uintptr)(unsafe.Pointer((*TWal)(unsafe.Pointer(pWal)).FapWiData + uintptr(i)*8)) = 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 */ /* Not currently locked */ /* useWal may only be set for read/write connections */ /* 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) { 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 = *(*Tu32)(unsafe.Pointer(pInfo + 4 + uintptr(i)*4)) if mxReadMark <= thisMark && thisMark <= mxFrame { 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 { *(*Tu32)(unsafe.Pointer(pInfo + 4 + uintptr(i)*4)) = mxFrame 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 { 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 = *(*Tu32)(unsafe.Pointer(pInfo)) + uint32(1) _walShmBarrier(tls, pWal) if *(*Tu32)(unsafe.Pointer(pInfo + 4 + uintptr(mxI)*4)) != mxReadMark || libc.Xmemcmp(tls, _walIndexHdr(tls, pWal), pWal+72, uint64(48)) != 0 { _walUnlockShared(tls, pWal, int32(3)+mxI) return -int32(1) } else { (*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 > *(*Tu32)(unsafe.Pointer(pInfo))) { break } /* Offset of wal file entry */ rc = _walHashGet(tls, pWal, _walFramePage(tls, i), bp+8) if rc != SQLITE_OK { break } 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, uint64(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 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) (*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 if pSnapshot != 0 { if libc.Xmemcmp(tls, pSnapshot, pWal+72, uint64(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+72, uint64(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) /* 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+72+32, uint64(8)) != 0) && (*TWalIndexHdr)(unsafe.Pointer(pSnapshot)).FmxFrame >= (*TWalCkptInfo)(unsafe.Pointer(pInfo)).FnBackfillAttempted { libc.X__builtin___memcpy_chk(tls, pWal+72, pSnapshot, uint64(48), libc.X__builtin_object_size(tls, pWal+72, 0)) *(*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(32) defer tls.Free(32) 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. */ /* 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) for { v2 = uint32(*(*Tht_slot)(unsafe.Pointer((*(*TWalHashLoc)(unsafe.Pointer(bp))).FaHash + uintptr(iKey)*2))) 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 { iRead = iFrame } v3 = nCollide nCollide-- if v3 == 0 { *(*Tu32)(unsafe.Pointer(piRead)) = uint32(0) return _sqlite3CorruptError(tls, int32(68469)) } 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) 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. */ 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+72, _walIndexHdr(tls, pWal), uint64(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) 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) } /* 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 { 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 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, int64(uint64(iOffset)+uint64(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. */ 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.Uint64FromInt64(4)*libc.Uint64FromInt32(2)), iCksumOff) *(*Tu32)(unsafe.Pointer(pWal + 72 + 24)) = _sqlite3Get4byte(tls, aBuf) *(*Tu32)(unsafe.Pointer(pWal + 72 + 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+64 */ [2]Tu32 var _ /* aWalHdr at bp+32 */ [32]Tu8 var _ /* iWrite at bp+72 */ 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 */ /* If this frame set completes a transaction, then nTruncate>0. If ** nTruncate==0 then this frame set does not complete the transaction. */ pLive = _walIndexHdr(tls, pWal) if libc.Xmemcmp(tls, pWal+72, pLive, uint64(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+32, uint32(libc.Int32FromInt32(WAL_MAGIC)|libc.Int32FromInt32(SQLITE_BIGENDIAN))) _sqlite3Put4byte(tls, bp+32+4, uint32(WAL_MAX_VERSION)) _sqlite3Put4byte(tls, bp+32+8, uint32(szPage)) _sqlite3Put4byte(tls, bp+32+12, (*TWal)(unsafe.Pointer(pWal)).FnCkpt) if (*TWal)(unsafe.Pointer(pWal)).FnCkpt == uint32(0) { Xsqlite3_randomness(tls, int32(8), pWal+72+32) } libc.X__builtin___memcpy_chk(tls, bp+32+16, pWal+72+32, uint64(8), libc.X__builtin_object_size(tls, bp+32+16, 0)) _walChecksumBytes(tls, int32(1), bp+32, libc.Int32FromInt32(WAL_HDRSIZE)-libc.Int32FromInt32(2)*libc.Int32FromInt32(4), uintptr(0), bp+64) _sqlite3Put4byte(tls, bp+32+24, (*(*[2]Tu32)(unsafe.Pointer(bp + 64)))[0]) _sqlite3Put4byte(tls, bp+32+28, (*(*[2]Tu32)(unsafe.Pointer(bp + 64)))[int32(1)]) (*TWal)(unsafe.Pointer(pWal)).FszPage = uint32(szPage) (*TWal)(unsafe.Pointer(pWal)).Fhdr.FbigEndCksum = uint8(SQLITE_BIGENDIAN) *(*Tu32)(unsafe.Pointer(pWal + 72 + 24)) = (*(*[2]Tu32)(unsafe.Pointer(bp + 64)))[0] *(*Tu32)(unsafe.Pointer(pWal + 72 + 24 + 1*4)) = (*(*[2]Tu32)(unsafe.Pointer(bp + 64)))[int32(1)] (*TWal)(unsafe.Pointer(pWal)).FtruncateOnCommit = uint8(1) rc = _sqlite3OsWrite(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, bp+32, 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(68992)) /* 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 + 72)) = uint32(0) _walFindFrame(tls, pWal, (*TPgHdr)(unsafe.Pointer(p)).Fpgno, bp+72) if *(*Tu32)(unsafe.Pointer(bp + 72)) >= iFirst { iOff = int64(WAL_HDRSIZE) + int64(*(*Tu32)(unsafe.Pointer(bp + 72))-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 + 72)) < (*TWal)(unsafe.Pointer(pWal)).FiReCksum { (*TWal)(unsafe.Pointer(pWal)).FiReCksum = *(*Tu32)(unsafe.Pointer(bp + 72)) } pData = (*TPgHdr)(unsafe.Pointer(p)).FpData rc = _sqlite3OsWrite(tls, (*TWal)(unsafe.Pointer(pWal)).FpWalFd, pData, szPage, iOff) if rc != 0 { return rc } p3 = p + 52 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) & ^libc.Int32FromInt32(PGHDR_WAL_APPEND)) goto _2 } } iFrame++ 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 + 52 *(*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++ } } if bSync != 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 } 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) 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 */ /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ 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(69252)) } 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) } if pnCkpt != 0 { *(*int32)(unsafe.Pointer(pnCkpt)) = int32((*TWalCkptInfo)(unsafe.Pointer(_walCkptInfo(tls, pWal))).FnBackfill) } } } 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.X__builtin___memset_chk(tls, pWal+72, 0, uint64(48), libc.X__builtin_object_size(tls, pWal+72, 0)) } /* 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 /* 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. */ 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 { _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 if libc.Xmemcmp(tls, pWal+72+24, uintptr(unsafe.Pointer(&_aZero)), uint64(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.X__builtin___memcpy_chk(tls, pRet, pWal+72, uint64(48), libc.X__builtin_object_size(tls, pRet, 0)) *(*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+72+32, uint64(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 { FnKey Ti64 FpPayload uintptr FnPayload Tu32 FnLocal Tu16 FnSize Tu16 } 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 { FeState Tu8 FcurFlags Tu8 FcurPagerFlags Tu8 Fhints Tu8 FskipNext int32 FpBtree uintptr FaOverflow uintptr FpKey uintptr FpBt uintptr FpNext uintptr Finfo TCellInfo FnKey Ti64 FpgnoRoot TPgno FiPage Ti8 FcurIntKey Tu8 Fix Tu16 FaiIdx [19]Tu16 FpKeyInfo uintptr FpPage uintptr FapPage [19]uintptr } 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 FnRow Ti64 } 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 FnRow Ti64 } 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) { 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 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. */ /* Check for locking consistency */ /* We should already hold a lock on the database connection */ /* Unless the database is sharable and unlocked, then BtShared.db ** should already be set correctly. */ 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 } 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) { if (*TBtree)(unsafe.Pointer(p)).Fsharable != 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) i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } p = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32))).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 i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } p = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32))).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]int8{'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*). */ /* Default value for SHARED_LOCK_TRACE macro if shared-cache is disabled ** or if the lock tracking is disabled. This is always the value for ** release builds. */ // 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 /* 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. */ /* 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). */ 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) { p2 = pBt + 40 *(*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 + 120 for *(*uintptr)(unsafe.Pointer(ppIter)) != 0 { pLock = *(*uintptr)(unsafe.Pointer(ppIter)) if (*TBtLock)(unsafe.Pointer(pLock)).FpBtree == p { *(*uintptr)(unsafe.Pointer(ppIter)) = (*TBtLock)(unsafe.Pointer(pLock)).FpNext if (*TBtLock)(unsafe.Pointer(pLock)).FiTable != uint32(1) { Xsqlite3_free(tls, pLock) } } else { ppIter = pLock + 16 } } if (*TBtShared)(unsafe.Pointer(pBt)).FpWriter == p { (*TBtShared)(unsafe.Pointer(pBt)).FpWriter = uintptr(0) p1 = pBt + 40 *(*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 + 40 *(*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 + 40 *(*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 } (*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 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 (*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) { (*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 + 144 + uintptr(i)*8))) 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 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.X__builtin___memset_chk(tls, pKey+uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FnKey), 0, uint64(libc.Int32FromInt32(9)+libc.Int32FromInt32(8)), libc.X__builtin_object_size(tls, pKey+uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FnKey), 0)) (*TBtCursor)(unsafe.Pointer(pCur)).FpKey = pKey } else { Xsqlite3_free(tls, pKey) } } else { rc = int32(SQLITE_NOMEM) } } 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 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(71421)) } 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 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) 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) { 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) { 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 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) { (*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 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 } /* The super-journal page number must never be used as a pointer map page */ if key == uint32(0) { *(*int32)(unsafe.Pointer(pRC)) = _sqlite3CorruptError(tls, int32(71619)) 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(*(*int8)(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(71632)) goto ptrmap_exit } offset = int32(libc.Uint32FromInt32(5) * (key - iPtrmap - libc.Uint32FromInt32(1))) if offset < 0 { *(*int32)(unsafe.Pointer(pRC)) = _sqlite3CorruptError(tls, int32(71637)) goto ptrmap_exit } 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 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(71682)) } *(*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(71690)) } 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))-int64(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) { _ = 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 */ 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 */ 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(int64(pIter)-int64(pCell)))) } 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 */ 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(int64(pIter)-int64(pCell)))) } 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 pEnd = pIter + uintptr(9) for { v1 = pIter pIter++ if !(int32(*(*Tu8)(unsafe.Pointer(v1)))&int32(0x80) != 0 && pIter < pEnd) { break } } return uint16(int64(pIter) - int64(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(int64(pIter)-int64(pCell)))) } 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 } (*(*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 uint64(pCell) < uint64((*TMemPage)(unsafe.Pointer(pSrc)).FaDataEnd) && uint64(pCell+uintptr((*(*TCellInfo)(unsafe.Pointer(bp))).FnLocal)) > uint64((*TMemPage)(unsafe.Pointer(pSrc)).FaDataEnd) { *(*int32)(unsafe.Pointer(pRC)) = _sqlite3CorruptError(tls, int32(72130)) 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 */ 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) 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(72188)) } if iFree != 0 { iFree2 = int32(*(*uint8)(unsafe.Pointer(data + uintptr(iFree))))< usableSize-int32(4) { return _sqlite3CorruptError(tls, int32(72191)) } 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(72199)) } if iFree2 != 0 { if iFree+sz > iFree2 { return _sqlite3CorruptError(tls, int32(72202)) } sz2 = int32(*(*uint8)(unsafe.Pointer(data + uintptr(iFree2+int32(2)))))< usableSize { return _sqlite3CorruptError(tls, int32(72204)) } libc.X__builtin___memmove_chk(tls, data+uintptr(iFree+sz+sz2), data+uintptr(iFree+sz), uint64(iFree2-(iFree+sz)), libc.X__builtin_object_size(tls, data+uintptr(iFree+sz+sz2), 0)) sz += sz2 } else { if iFree+sz > usableSize { return _sqlite3CorruptError(tls, int32(72208)) } } cbrk = top + sz libc.X__builtin___memmove_chk(tls, data+uintptr(cbrk), data+uintptr(top), uint64(iFree-top), libc.X__builtin_object_size(tls, data+uintptr(cbrk), 0)) 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.X__builtin___memcpy_chk(tls, temp, data, uint64(usableSize), libc.X__builtin_object_size(tls, temp, 0)) 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(72241)) } 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(72247)) } *(*Tu8)(unsafe.Pointer(pAddr1)) = uint8(cbrk >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(pAddr1 + 1)) = uint8(cbrk) libc.X__builtin___memcpy_chk(tls, data+uintptr(cbrk), src+uintptr(pc), uint64(size), libc.X__builtin_object_size(tls, data+uintptr(cbrk), 0)) goto _2 _2: ; i++ } } *(*uint8)(unsafe.Pointer(data + uintptr(hdr+int32(7)))) = uint8(0) goto defragment_out defragment_out: ; if int32(*(*uint8)(unsafe.Pointer(data + uintptr(hdr+int32(7)))))+cbrk-iCellFirst != (*TMemPage)(unsafe.Pointer(pPage)).FnFree { return _sqlite3CorruptError(tls, int32(72261)) } *(*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.X__builtin___memset_chk(tls, data+uintptr(iCellFirst), 0, uint64(cbrk-iCellFirst), libc.X__builtin_object_size(tls, data+uintptr(iCellFirst), 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.X__builtin___memcpy_chk(tls, aData+uintptr(iAddr), aData+uintptr(pc), uint64(2), libc.X__builtin_object_size(tls, aData+uintptr(iAddr), 0)) 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(72318)) 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(72340)) } 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 */ /* Minimum cell size is 4 */ gap = int32((*TMemPage)(unsafe.Pointer(pPage)).FcellOffset) + int32(2)*int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) /* 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(72388)) } } else { if top > int32((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) { return _sqlite3CorruptError(tls, int32(72391)) } } /* 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 { v1 = int32(int64(pSpace) - int64(data)) g2 = v1 *(*int32)(unsafe.Pointer(pIdx)) = v1 if g2 <= gap { return _sqlite3CorruptError(tls, int32(72408)) } 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 { 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) *(*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[] */ /* Minimum cell size is 4 */ /* 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(72492)) } /* 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(72504)) } 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(72507)) } 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(72520)) } 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(72526)) } 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 */ 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(72595)) } } } 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(72619)) } } } 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 */ 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(72670)) } for int32(1) != 0 { if pc > iCellLast { /* Freeblock off the end of the page */ return _sqlite3CorruptError(tls, int32(72675)) } next = uint32(int32(*(*Tu8)(unsafe.Pointer(data + uintptr(pc))))< uint32(0) { /* Freeblock not in ascending order */ return _sqlite3CorruptError(tls, int32(72685)) } if uint32(pc)+size > uint32(usableSize) { /* Last freeblock extends past page end */ return _sqlite3CorruptError(tls, int32(72689)) } } /* 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(72701)) } (*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(*(*Tu8)(unsafe.Pointer(data + uintptr(cellOffset+i*int32(2)))))< iCellLast { return _sqlite3CorruptError(tls, int32(72732)) } 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(72737)) } 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 */ 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(72769)) } (*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(72783)) } /* 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. */ (*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 if int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_FAST_SECURE) != 0 { libc.X__builtin___memset_chk(tls, data+uintptr(hdr), 0, uint64((*TBtShared)(unsafe.Pointer(pBt)).FusableSize-uint32(hdr)), libc.X__builtin_object_size(tls, data+uintptr(hdr), 0)) } *(*uint8)(unsafe.Pointer(data + uintptr(hdr))) = uint8(int8(flags)) if flags&int32(PTF_LEAF) == 0 { v1 = int32(12) } else { v1 = int32(8) } first = uint16(int32(hdr) + v1) libc.X__builtin___memset_chk(tls, data+uintptr(int32(hdr)+int32(1)), 0, uint64(4), libc.X__builtin_object_size(tls, data+uintptr(int32(hdr)+int32(1)), 0)) *(*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) (*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) } 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 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 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) { 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 if pgno > _btreePagecount(tls, pBt) { *(*uintptr)(unsafe.Pointer(ppPage)) = uintptr(0) return _sqlite3CorruptError(tls, int32(72926)) } 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 } } *(*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) { _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) { _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(72998)) } (*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) if (*TMemPage)(unsafe.Pointer(pPage)).FisInit != 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 return _sqlite3InvokeBusyHandler(tls, (*TBtShared)(unsafe.Pointer(pBt)).Fdb+672) } // 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(*(*int8)(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+4602) == 0 || isTempDb != 0 && _sqlite3TempInMemory(tls, db) != 0 || vfsFlags&int32(SQLITE_OPEN_MEMORY) != 0) /* flags fit in 8 bits */ /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */ /* A BTREE_SINGLE database is always a temporary and/or ephemeral */ 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(72)) 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.X__builtin___memcpy_chk(tls, zFullPathname, zFilename, uint64(nFilename), libc.X__builtin_object_size(tls, zFullPathname, 0)) } 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)*32))).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. */ /* Suppress false-positive compiler warning from PVS-Studio */ libc.X__builtin___memset_chk(tls, bp+16, 0, uint64(8), libc.X__builtin_object_size(tls, bp+16, 0)) pBt = _sqlite3MallocZero(tls, uint64(152)) if pBt == uintptr(0) { rc = int32(SQLITE_NOMEM) goto btree_open_out } rc = _sqlite3PagerOpen(tls, pVfs, pBt, zFilename, int32(136), 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 + 40 *(*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 + 40 *(*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+52, int32(nReserve)) if rc != 0 { goto btree_open_out } (*TBtShared)(unsafe.Pointer(pBt)).FusableSize = (*TBtShared)(unsafe.Pointer(pBt)).FpageSize - uint32(nReserve) /* 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)*32))).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 uint64((*TBtree)(unsafe.Pointer(p)).FpBt) < uint64((*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 && uint64((*TBtree)(unsafe.Pointer((*TBtree)(unsafe.Pointer(pSib)).FpNext)).FpBt) < uint64((*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+8) } } if mutexOpen != 0 { Xsqlite3_mutex_leave(tls, mutexOpen) } 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 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 /* This routine is called only by btreeCursor() when allocating the ** first write cursor for the BtShared object */ (*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.X__builtin___memset_chk(tls, pCur, 0, uint64(296), libc.X__builtin_object_size(tls, pCur, 0)) 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.X__builtin___memset_chk(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace, 0, uint64(8), libc.X__builtin_object_size(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace, 0)) *(*uintptr)(unsafe.Pointer(pBt + 136)) += 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 + 136)) -= 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. */ _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. */ 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. */ _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) } 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 _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 _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 _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 _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 _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) } if pageSize >= int32(512) && pageSize <= int32(SQLITE_MAX_PAGE_SIZE) && (pageSize-int32(1))&pageSize == 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+52, nReserve) (*TBtShared)(unsafe.Pointer(pBt)).FusableSize = (*TBtShared)(unsafe.Pointer(pBt)).FpageSize - uint32(uint16(nReserve)) if iFix != 0 { p1 = pBt + 40 *(*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 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) if newFlag >= 0 { p1 = (*TBtree)(unsafe.Pointer(p)).FpBt + 40 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(BTS_FAST_SECURE)) p2 = (*TBtree)(unsafe.Pointer(p)).FpBt + 40 *(*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+12 */ int32 var _ /* nPageFile at bp+8 */ 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 + 8)) = uint32(0) /* Number of pages in the database file */ 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+8) 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, uint64(4)) != 0 { nPage = *(*Tu32)(unsafe.Pointer(bp + 8)) } 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)), uint64(16)) != 0 { goto page1_init_failed } if int32(*(*Tu8)(unsafe.Pointer(page1 + 18))) > int32(2) { p1 = pBt + 40 *(*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 + 12)) = 0 rc = _sqlite3PagerOpenWal(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, bp+12) if rc != SQLITE_OK { goto page1_init_failed } else { if *(*int32)(unsafe.Pointer(bp + 12)) == 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+4611, uint64(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 } /* 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 + 40 *(*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+52, int32(pageSize-usableSize)) return rc } if nPage > *(*Tu32)(unsafe.Pointer(bp + 8)) { if _sqlite3WritableSchema(tls, (*TBtShared)(unsafe.Pointer(pBt)).Fdb) == 0 { rc = _sqlite3CorruptError(tls, int32(73936)) goto page1_init_failed } else { nPage = *(*Tu32)(unsafe.Pointer(bp + 8)) } } /* 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 + 40 *(*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) } (*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 if int32((*TBtShared)(unsafe.Pointer(pBt)).FinTransaction) == TRANS_NONE && (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 != uintptr(0) { pPage1 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 (*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 if (*TBtShared)(unsafe.Pointer(pBt)).FnPage > uint32(0) { return SQLITE_OK } pP1 = (*TBtShared)(unsafe.Pointer(pBt)).FpPage1 data = (*TMemPage)(unsafe.Pointer(pP1)).FaData rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pP1)).FpDbPage) if rc != 0 { return rc } libc.X__builtin___memcpy_chk(tls, data, uintptr(unsafe.Pointer(&_zMagicHeader)), uint64(16), libc.X__builtin_object_size(tls, data, 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) *(*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.X__builtin___memset_chk(tls, data+24, 0, uint64(libc.Int32FromInt32(100)-libc.Int32FromInt32(24)), libc.X__builtin_object_size(tls, data+24, 0)) _zeroPage(tls, pP1, libc.Int32FromInt32(PTF_INTKEY)|libc.Int32FromInt32(PTF_LEAF)|libc.Int32FromInt32(PTF_LEAFDATA)) p1 = pBt + 40 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(BTS_PAGESIZE_FIXED)) _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) /* 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 } if (*Tsqlite3)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).Fdb)).Fflags&uint64(SQLITE_ResetDatabase) != 0 && int32(_sqlite3PagerIsreadonly(tls, pPager)) == 0 { p1 = pBt + 40 *(*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 (*TBtShared)(unsafe.Pointer(pBt)).FpWriter = p p8 = pBt + 40 *(*Tu16)(unsafe.Pointer(p8)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p8))) & ^libc.Int32FromInt32(BTS_EXCLUSIVE)) if wrflag > int32(1) { p9 = pBt + 40 *(*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) } } _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 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(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*i))))< (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize) { return _sqlite3CorruptError(tls, int32(74403)) } 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(74412)) } 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(74424)) } _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+8 */ int32 _, _, _ = iDbPage, nextOvfl, pPager /* The page that contains a pointer to pDbPage */ iDbPage = (*TMemPage)(unsafe.Pointer(pDbPage)).Fpgno pPager = (*TBtShared)(unsafe.Pointer(pBt)).FpPager if iDbPage < uint32(3) { return _sqlite3CorruptError(tls, int32(74459)) } /* Move page iDbPage from its current location to page number iFreePage */ *(*int32)(unsafe.Pointer(bp + 8)) = _sqlite3PagerMovepage(tls, pPager, (*TMemPage)(unsafe.Pointer(pDbPage)).FpDbPage, iFreePage, isCommit) if *(*int32)(unsafe.Pointer(bp + 8)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 8)) } (*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 + 8)) = _setChildPtrmaps(tls, pDbPage) if *(*int32)(unsafe.Pointer(bp + 8)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 8)) } } else { nextOvfl = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pDbPage)).FaData) if nextOvfl != uint32(0) { _ptrmapPut(tls, pBt, nextOvfl, uint8(PTRMAP_OVERFLOW2), iFreePage, bp+8) if *(*int32)(unsafe.Pointer(bp + 8)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 8)) } } } /* 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 + 8)) = _btreeGetPage(tls, pBt, iPtrPage, 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)) } *(*int32)(unsafe.Pointer(bp + 8)) = _modifyPagePointer(tls, *(*uintptr)(unsafe.Pointer(bp)), iDbPage, iFreePage, eType) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) if *(*int32)(unsafe.Pointer(bp + 8)) == SQLITE_OK { _ptrmapPut(tls, pBt, iFreePage, eType, iPtrPage, bp+8) } } return *(*int32)(unsafe.Pointer(bp + 8)) } // 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(48) defer tls.Free(48) var dbSize, iNear, nFreeList TPgno var eMode Tu8 var rc int32 var _ /* eType at bp+0 */ Tu8 var _ /* iFreePg at bp+24 */ TPgno var _ /* iFreePg at bp+8 */ TPgno var _ /* iPtrPage at bp+4 */ TPgno var _ /* pFreePg at bp+16 */ uintptr var _ /* pFreePg at bp+40 */ uintptr var _ /* pLastPg at bp+32 */ uintptr _, _, _, _, _ = dbSize, eMode, iNear, nFreeList, rc 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(74557)) } if int32(*(*Tu8)(unsafe.Pointer(bp))) == int32(PTRMAP_FREEPAGE) { if bCommit == 0 { rc = _allocateBtreePage(tls, pBt, bp+16, bp+8, iLastPg, uint8(BTALLOC_EXACT)) if rc != SQLITE_OK { return rc } _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) } } else { eMode = uint8(BTALLOC_ANY) /* Mode parameter for allocateBtreePage() */ iNear = uint32(0) /* nearby parameter for allocateBtreePage() */ rc = _btreeGetPage(tls, pBt, iLastPg, bp+32, 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 + 24)) > nFin { dbSize = _btreePagecount(tls, pBt) rc = _allocateBtreePage(tls, pBt, bp+40, bp+24, iNear, eMode) if rc != SQLITE_OK { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 32))) return rc } _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 40))) if *(*TPgno)(unsafe.Pointer(bp + 24)) > dbSize { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 32))) return _sqlite3CorruptError(tls, int32(74609)) } } rc = _relocatePage(tls, pBt, *(*uintptr)(unsafe.Pointer(bp + 32)), *(*Tu8)(unsafe.Pointer(bp)), *(*TPgno)(unsafe.Pointer(bp + 4)), *(*TPgno)(unsafe.Pointer(bp + 24)), bCommit) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 32))) 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) 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(74677)) } 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 pBt = (*TBtree)(unsafe.Pointer(p)).FpBt pPager = (*TBtShared)(unsafe.Pointer(pBt)).FpPager _invalidateAllOverflowCache(tls, pBt) 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(74728)) } 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)*32))).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)*32))).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(74755)) } 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) } } 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 (*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) } } // 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) /* 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 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 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 _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) if rc2 != SQLITE_OK { rc = rc2 } } if int32((*TBtree)(unsafe.Pointer(p)).FinTrans) == int32(TRANS_WRITE) { 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))) } (*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) /* 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 _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. */ } _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 */ /* 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. */ /* Assert that the caller has opened the required transaction. */ if iTable <= uint32(1) { if iTable < uint32(1) { return _sqlite3CorruptError(tls, int32(75219)) } else { if _btreePagecount(tls, pBt) == uint32(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.Uint64FromInt64(296) + libc.Uint64FromInt32(7)) & uint64(^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.X__builtin___memset_chk(tls, p, 0, uint64(libc.UintptrFromInt32(0)+32), libc.X__builtin_object_size(tls, p, 0)) } // 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) 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. */ _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+48) } else { } } func _sqlite3BtreeCursorIsValidNN(tls *libc.TLS, pCur uintptr) (r int32) { 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) { _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 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 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) { _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((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload) - int64((*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) { _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) { 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+12 */ Tu8 var _ /* pPage at bp+0 */ uintptr var _ /* pgno at bp+8 */ TPgno _, _, _, _, _ = iGuess, next, rc, v1, v2 next = uint32(0) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) rc = SQLITE_OK /* 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+12, bp+8) if rc == SQLITE_OK && int32(*(*Tu8)(unsafe.Pointer(bp + 12))) == int32(PTRMAP_OVERFLOW2) && *(*TPgno)(unsafe.Pointer(bp + 8)) == ovfl { next = iGuess rc = int32(SQLITE_DONE) } } } if rc == SQLITE_OK { if ppPage == uintptr(0) { v1 = int32(PAGER_GET_READONLY) } else { v1 = 0 } rc = _btreeGetPage(tls, pBt, ovfl, bp, v1) 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.X__builtin___memcpy_chk(tls, pPayload, pBuf, uint64(nByte), libc.X__builtin_object_size(tls, pPayload, 0)) } else { /* Copy data from page to buffer (a read operation) */ libc.X__builtin___memcpy_chk(tls, pBuf, pPayload, uint64(nByte), libc.X__builtin_object_size(tls, pBuf, 0)) } 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 */ if int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) { return _sqlite3CorruptError(tls, int32(75624)) } _getCellInfo(tls, pCur) aPayload = (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload if uint64(int64(aPayload)-int64((*TMemPage)(unsafe.Pointer(pPage)).FaData)) > uint64((*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(75639)) } /* 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) { if _sqlite3FaultSim(tls, int32(413)) != 0 { aNew = uintptr(0) } else { aNew = _sqlite3Realloc(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow, uint64(nOvfl*int32(2))*uint64(4)) } if aNew == uintptr(0) { return int32(SQLITE_NOMEM) } else { (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow = aNew } } libc.X__builtin___memset_chk(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow, 0, uint64(nOvfl)*uint64(4), libc.X__builtin_object_size(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FaOverflow, 0)) p1 = pCur + 1 *(*Tu8)(unsafe.Pointer(p1)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p1))) | libc.Int32FromInt32(BTCF_ValidOvfl)) } else { /* Sanity check the validity of the overflow page cache */ /* 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 } } 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(75710)) } *(*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. */ 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)) /* due to (6) */ libc.X__builtin___memcpy_chk(tls, bp+4, aWrite, uint64(4), libc.X__builtin_object_size(tls, bp+4, 0)) 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.X__builtin___memcpy_chk(tls, aWrite, bp+4, uint64(4), libc.X__builtin_object_size(tls, aWrite, 0)) } 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(75794)) } 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) { 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) } 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 { 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 amt = int32((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnLocal) if amt > int32(int64((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FaDataEnd)-int64((*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. */ if 0 > int32(int64((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FaDataEnd)-int64((*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FpPayload)) { v1 = 0 } else { v1 = int32(int64((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpPage)).FaDataEnd) - int64((*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 if int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) >= libc.Int32FromInt32(BTCURSOR_MAX_DEPTH)-libc.Int32FromInt32(1) { return _sqlite3CorruptError(tls, int32(75932)) } (*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 + 88 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*2)) = (*TBtCursor)(unsafe.Pointer(pCur)).Fix *(*uintptr)(unsafe.Pointer(pCur + 144 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*8)) = (*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+136, 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(75946)) } if rc != 0 { v3 = pCur + 84 *(*Ti8)(unsafe.Pointer(v3))-- v2 = *(*Ti8)(unsafe.Pointer(v3)) (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = *(*uintptr)(unsafe.Pointer(pCur + 144 + uintptr(v2)*8)) } 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 (*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 + 88 + uintptr(int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)-int32(1))*2)) pLeaf = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage v3 = pCur + 84 *(*Ti8)(unsafe.Pointer(v3))-- v2 = *(*Ti8)(unsafe.Pointer(v3)) (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = *(*uintptr)(unsafe.Pointer(pCur + 144 + uintptr(v2)*8)) _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 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 + 84 *(*Ti8)(unsafe.Pointer(v2))-- v1 = *(*Ti8)(unsafe.Pointer(v2)) if !(v1 != 0) { break } _releasePageNotNull(tls, *(*uintptr)(unsafe.Pointer(pCur + 144 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*8))) } v3 = *(*uintptr)(unsafe.Pointer(pCur + 144)) (*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 { if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) >= int32(CURSOR_REQUIRESEEK) { if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) == int32(CURSOR_FAULT) { return (*TBtCursor)(unsafe.Pointer(pCur)).FskipNext } _sqlite3BtreeClearCursor(tls, pCur) } rc = _getAndInitPage(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpBt, (*TBtCursor)(unsafe.Pointer(pCur)).FpgnoRoot, pCur+136, 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 /* 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). */ 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(76081)) } 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(76093)) } 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 for { if v2 = rc == SQLITE_OK; v2 { v1 = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage pPage = v1 } if !(v2 && !((*TMemPage)(unsafe.Pointer(v1)).Fleaf != 0)) { break } pgno = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(pPage)).FaData+uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(int32(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix)))))<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 /* 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) { *(*int32)(unsafe.Pointer(pRes)) = -int32(1) return SQLITE_OK } return rc } 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. */ lwr = 0 upr = int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) - int32(1) 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(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx))))<= (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd { return _sqlite3CorruptError(tls, int32(76342)) } } } _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 { (*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 } } } idx = (lwr + upr) >> int32(1) /* idx = (lwr+upr)/2; */ goto _2 _2: } if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 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(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*lwr))))<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 xRecordCompare = _sqlite3VdbeFindCompare(tls, pIdxKey) (*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).FerrCode = uint8(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(76538)) } 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) { *(*int32)(unsafe.Pointer(pRes)) = -int32(1) return SQLITE_OK } return rc } goto bypass_moveto_root bypass_moveto_root: ; 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. */ 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(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx))))< (*TBtShared)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pCur)).FpBt)).FnPage { rc = _sqlite3CorruptError(tls, int32(76625)) 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.X__builtin___memset_chk(tls, pCellKey+uintptr(nCell), 0, uint64(nOverrun), libc.X__builtin_object_size(tls, pCellKey+uintptr(nCell), 0)) /* 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) } } if c1 < 0 { lwr = idx + int32(1) } else { if c1 > 0 { upr = idx - int32(1) } else { *(*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(76657)) } goto moveto_index_finish } } if lwr > upr { break } idx = (lwr + upr) >> int32(1) /* idx = (lwr+upr)/2 */ goto _5 _5: } if (*TMemPage)(unsafe.Pointer(pPage)).Fleaf != 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(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*lwr))))<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(76688)) } *(*Tu16)(unsafe.Pointer(pCur + 88 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*2)) = uint16(lwr) *(*uintptr)(unsafe.Pointer(pCur + 144 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*8)) = (*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+136, 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(76699)) } if rc != 0 { v11 = pCur + 84 *(*Ti8)(unsafe.Pointer(v11))-- v10 = *(*Ti8)(unsafe.Pointer(v11)) (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = *(*uintptr)(unsafe.Pointer(pCur + 144 + uintptr(v10)*8)) 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) 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 /* Currently this interface is only called by the OP_IfSizeBetween ** opcode and the OP_Count opcode with P3=1. In either case, ** the cursor will always be valid unless the btree is empty. */ if int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID { return 0 } 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 + 144 + uintptr(i)*8)))).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 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((*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 + 86 *(*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(76800)) } 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 */ (*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 + 86 *(*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 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(76893)) } 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(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx))))<= mxPage { return _sqlite3CorruptError(tls, int32(76983)) } 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 { rc = _ptrmapGet(tls, pBt, nearby, bp+8, uintptr(0)) if rc != 0 { return rc } if int32(*(*Tu8)(unsafe.Pointer(bp + 8))) == 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(77039)) } else { rc = _btreeGetUnusedPage(tls, pBt, iTrunk, bp, 0) } if rc != 0 { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) goto end_allocate_page } /* 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 */ rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) if rc != 0 { goto end_allocate_page } *(*TPgno)(unsafe.Pointer(pPgno)) = iTrunk libc.X__builtin___memcpy_chk(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData, uint64(4), libc.X__builtin_object_size(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, 0)) *(*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(77068)) 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.X__builtin___memcpy_chk(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData, uint64(4), libc.X__builtin_object_size(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, 0)) } else { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FpDbPage) if rc != SQLITE_OK { goto end_allocate_page } libc.X__builtin___memcpy_chk(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FaData, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData, uint64(4), libc.X__builtin_object_size(tls, (*TMemPage)(unsafe.Pointer(pPrevTrunk)).FaData, 0)) } } else { iNewTrunk = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData+8) if iNewTrunk > mxPage { rc = _sqlite3CorruptError(tls, int32(77102)) goto end_allocate_page } rc = _btreeGetUnusedPage(tls, pBt, iNewTrunk, bp+16, 0) if rc != SQLITE_OK { goto end_allocate_page } rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FpDbPage) if rc != SQLITE_OK { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) goto end_allocate_page } libc.X__builtin___memcpy_chk(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FaData, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData, uint64(4), libc.X__builtin_object_size(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FaData, 0)) _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FaData+4, k-uint32(1)) libc.X__builtin___memcpy_chk(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FaData+8, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData+12, uint64((k-uint32(1))*uint32(4)), libc.X__builtin_object_size(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FaData+8, 0)) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) if !(pPrevTrunk != 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(77167)) 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.X__builtin___memcpy_chk(tls, aData+uintptr(uint32(8)+closest*uint32(4)), aData+uintptr(uint32(4)+k*uint32(4)), uint64(4), libc.X__builtin_object_size(tls, aData+uintptr(uint32(8)+closest*uint32(4)), 0)) } _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 + 24)) = uintptr(0) rc = _btreeGetUnusedPage(tls, pBt, (*TBtShared)(unsafe.Pointer(pBt)).FnPage, bp+24, bNoContent) if rc == SQLITE_OK { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).FpDbPage) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 24))) } 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 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) } } goto end_allocate_page end_allocate_page: ; _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) _releasePage(tls, pPrevTrunk) 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(32) defer tls.Free(32) var iTrunk TPgno var nFree, nLeaf Tu32 var pPage1 uintptr var v1, v3, v5 int32 var v2, v4, v6 bool var _ /* pPage at bp+8 */ uintptr var _ /* pTrunk at bp+0 */ uintptr var _ /* rc at bp+16 */ 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 */ if iPage < uint32(2) || iPage > (*TBtShared)(unsafe.Pointer(pBt)).FnPage { return _sqlite3CorruptError(tls, int32(77294)) } if pMemPage != 0 { *(*uintptr)(unsafe.Pointer(bp + 8)) = pMemPage _sqlite3PagerRef(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FpDbPage) } else { *(*uintptr)(unsafe.Pointer(bp + 8)) = _btreePageLookup(tls, pBt, iPage) } /* Increment the free page count on pPage1 */ *(*int32)(unsafe.Pointer(bp + 16)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FpDbPage) if *(*int32)(unsafe.Pointer(bp + 16)) != 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 + 8)) != 0); v2 { v1 = _btreeGetPage(tls, pBt, iPage, bp+8, 0) *(*int32)(unsafe.Pointer(bp + 16)) = v1 } if v4 = v2 && v1 != 0; !v4 { v3 = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FpDbPage) *(*int32)(unsafe.Pointer(bp + 16)) = v3 } if v4 || v3 != 0 { goto freepage_out } libc.X__builtin___memset_chk(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FaData, 0, uint64((*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FpBt)).FpageSize), libc.X__builtin_object_size(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FaData, 0)) } /* 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+16) if *(*int32)(unsafe.Pointer(bp + 16)) != 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 + 16)) = _sqlite3CorruptError(tls, int32(77341)) goto freepage_out } *(*int32)(unsafe.Pointer(bp + 16)) = _btreeGetPage(tls, pBt, iTrunk, bp, 0) if *(*int32)(unsafe.Pointer(bp + 16)) != SQLITE_OK { goto freepage_out } nLeaf = _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaData+4) if nLeaf > (*TBtShared)(unsafe.Pointer(pBt)).FusableSize/uint32(4)-uint32(2) { *(*int32)(unsafe.Pointer(bp + 16)) = _sqlite3CorruptError(tls, int32(77352)) 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 + 16)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) if *(*int32)(unsafe.Pointer(bp + 16)) == 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 + 8)) != 0 && int32((*TBtShared)(unsafe.Pointer(pBt)).FbtsFlags)&int32(BTS_SECURE_DELETE) == 0 { _sqlite3PagerDontWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FpDbPage) } *(*int32)(unsafe.Pointer(bp + 16)) = _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 + 8)) == uintptr(0); v6 { v5 = _btreeGetPage(tls, pBt, iPage, bp+8, 0) *(*int32)(unsafe.Pointer(bp + 16)) = v5 } if v6 && SQLITE_OK != v5 { goto freepage_out } *(*int32)(unsafe.Pointer(bp + 16)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FpDbPage) if *(*int32)(unsafe.Pointer(bp + 16)) != SQLITE_OK { goto freepage_out } _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FaData, iTrunk) _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FaData+4, uint32(0)) _sqlite3Put4byte(tls, (*TMemPage)(unsafe.Pointer(pPage1)).FaData+32, iPage) goto freepage_out freepage_out: ; if *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 { (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FisInit = uint8(0) } _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 16)) } 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+8 */ uintptr _, _, _, _, _, _, _, _ = nOvfl, ovflPageSize, ovflPgno, pBt, rc, v1, v2, v3 if pCell+uintptr((*TCellInfo)(unsafe.Pointer(pInfo)).FnSize) > (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd { /* Cell extends past end of page */ return _sqlite3CorruptError(tls, int32(77441)) } ovflPgno = _sqlite3Get4byte(tls, pCell+uintptr((*TCellInfo)(unsafe.Pointer(pInfo)).FnSize)-uintptr(4)) pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt 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) for { v1 = nOvfl nOvfl-- if !(v1 != 0) { break } *(*TPgno)(unsafe.Pointer(bp)) = uint32(0) *(*uintptr)(unsafe.Pointer(bp + 8)) = 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(77458)) } if nOvfl != 0 { rc = _getOverflowPage(tls, pBt, ovflPgno, bp+8, bp) if rc != 0 { return rc } } if v3 = *(*uintptr)(unsafe.Pointer(bp + 8)) != 0; !v3 { v2 = _btreePageLookup(tls, pBt, ovflPgno) *(*uintptr)(unsafe.Pointer(bp + 8)) = v2 } if (v3 || v2 != uintptr(0)) && _sqlite3PagerPageRefcount(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).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(77478)) } else { rc = _freePage2(tls, pBt, *(*uintptr)(unsafe.Pointer(bp + 8)), ovflPgno) } if *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 { _sqlite3PagerUnref(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).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 /* pPage is not necessarily writeable since pCell might be auxiliary ** buffer space that is separate from the pPage buffer area */ /* 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 /* 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 { 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.X__builtin___memcpy_chk(tls, pPayload, pSrc, uint64(nSrc), libc.X__builtin_object_size(tls, pPayload, 0)) libc.X__builtin___memset_chk(tls, pPayload+uintptr(nSrc), 0, uint64(nPayload-nSrc), libc.X__builtin_object_size(tls, pPayload+uintptr(nSrc), 0)) 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. */ /* If pPayload is part of the data area of pPage, then make sure pPage ** is still writeable */ if nSrc >= n { libc.X__builtin___memcpy_chk(tls, pPayload, pSrc, uint64(n), libc.X__builtin_object_size(tls, pPayload, 0)) } else { if nSrc > 0 { n = nSrc libc.X__builtin___memcpy_chk(tls, pPayload, pSrc, uint64(n), libc.X__builtin_object_size(tls, pPayload, 0)) } else { libc.X__builtin___memset_chk(tls, pPayload, 0, uint64(n), libc.X__builtin_object_size(tls, pPayload, 0)) } } 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. */ /* If pPrior is part of the data area of pPage, then make sure pPage ** is still writeable */ _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 } data = (*TMemPage)(unsafe.Pointer(pPage)).FaData ptr = (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx) pc = uint32(int32(*(*Tu8)(unsafe.Pointer(ptr)))< (*TBtShared)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FpBt)).FusableSize { *(*int32)(unsafe.Pointer(pRC)) = _sqlite3CorruptError(tls, int32(77734)) 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.X__builtin___memset_chk(tls, data+uintptr(hdr+int32(1)), 0, uint64(4), libc.X__builtin_object_size(tls, data+uintptr(hdr+int32(1)), 0)) *(*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.X__builtin___memmove_chk(tls, ptr, ptr+uintptr(2), uint64(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-idx)), libc.X__builtin_object_size(tls, ptr, 0)) *(*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 */ if (*TMemPage)(unsafe.Pointer(pPage)).FnOverflow != 0 || sz+int32(2) > (*TMemPage)(unsafe.Pointer(pPage)).FnFree { if pTemp != 0 { libc.X__builtin___memcpy_chk(tls, pTemp, pCell, uint64(sz), libc.X__builtin_object_size(tls, pTemp, 0)) 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. */ *(*uintptr)(unsafe.Pointer(pPage + 40 + uintptr(j)*8)) = 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. */ /* Overflows in sorted order */ /* Overflows are sequential */ } else { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) if rc != SQLITE_OK { return rc } data = (*TMemPage)(unsafe.Pointer(pPage)).FaData rc = _allocateSpace(tls, pPage, sz, bp) if rc != 0 { return rc } /* The allocateSpace() routine guarantees the following properties ** if it returns successfully */ *(*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.X__builtin___memcpy_chk(tls, data+uintptr(*(*int32)(unsafe.Pointer(bp))+int32(4)), pCell+uintptr(4), uint64(sz-int32(4)), libc.X__builtin_object_size(tls, data+uintptr(*(*int32)(unsafe.Pointer(bp))+int32(4)), 0)) _sqlite3Put4byte(tls, data+uintptr(*(*int32)(unsafe.Pointer(bp))), iChild) pIns = (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(i*int32(2)) libc.X__builtin___memmove_chk(tls, pIns+uintptr(2), pIns, uint64(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-i)), libc.X__builtin_object_size(tls, pIns+uintptr(2), 0)) *(*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))))++ } 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 */ 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. */ *(*uintptr)(unsafe.Pointer(pPage + 40 + uintptr(j)*8)) = 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. */ /* Overflows in sorted order */ /* Overflows are sequential */ } else { rc = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(pPage)).FpDbPage) if rc != SQLITE_OK { return rc } data = (*TMemPage)(unsafe.Pointer(pPage)).FaData rc = _allocateSpace(tls, pPage, sz, bp) if rc != 0 { return rc } /* The allocateSpace() routine guarantees the following properties ** if it returns successfully */ *(*int32)(unsafe.Pointer(pPage + 20)) -= int32(uint16(libc.Int32FromInt32(2) + sz)) libc.X__builtin___memcpy_chk(tls, data+uintptr(*(*int32)(unsafe.Pointer(bp))), pCell, uint64(sz), libc.X__builtin_object_size(tls, data+uintptr(*(*int32)(unsafe.Pointer(bp))), 0)) pIns = (*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(i*int32(2)) libc.X__builtin___memmove_chk(tls, pIns+uintptr(2), pIns, uint64(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-i)), libc.X__builtin_object_size(tls, pIns+uintptr(2), 0)) *(*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))))++ } 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 for N > 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)*8))) } else { } 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) { *(*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)*8))) return *(*Tu16)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(p)).FszCell + uintptr(N)*2)) } func _cachedCellSize(tls *libc.TLS, p uintptr, N int32) (r Tu16) { 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 */ j = uint32(int32(*(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(5)))))< uint32(usableSize) { j = uint32(0) } libc.X__builtin___memcpy_chk(tls, pTmp+uintptr(j), aData+uintptr(j), uint64(uint32(usableSize)-j), libc.X__builtin_object_size(tls, pTmp+uintptr(j), 0)) k = 0 for { if !(k < libc.Int32FromInt32(NB)*libc.Int32FromInt32(2) && *(*int32)(unsafe.Pointer(pCArray + 80 + uintptr(k)*4)) <= i) { break } goto _1 _1: ; k++ } pSrcEnd = *(*uintptr)(unsafe.Pointer(pCArray + 32 + uintptr(k)*8)) pData = pEnd for int32(1) != 0 { pCell = *(*uintptr)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*8)) sz = *(*Tu16)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FszCell + uintptr(i)*2)) if uint64(pCell) >= uint64(aData+uintptr(j)) && uint64(pCell) < uint64(pEnd) { if uint64(pCell+uintptr(sz)) > uint64(pEnd) { return _sqlite3CorruptError(tls, int32(78123)) } pCell = pTmp + uintptr(int64(pCell)-int64(aData)) } else { if uint64(pCell+uintptr(sz)) > uint64(pSrcEnd) && uint64(pCell) < uint64(pSrcEnd) { return _sqlite3CorruptError(tls, int32(78128)) } } pData -= uintptr(sz) *(*Tu8)(unsafe.Pointer(pCellptr)) = uint8((int64(pData) - int64(aData)) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(pCellptr + 1)) = uint8(int64(pData) - int64(aData)) pCellptr += uintptr(2) if pData < pCellptr { return _sqlite3CorruptError(tls, int32(78134)) } libc.X__builtin___memmove_chk(tls, pData, pCell, uint64(sz), libc.X__builtin_object_size(tls, pData, 0)) i++ if i >= iEnd { break } if *(*int32)(unsafe.Pointer(pCArray + 80 + uintptr(k)*4)) <= i { k++ pSrcEnd = *(*uintptr)(unsafe.Pointer(pCArray + 32 + uintptr(k)*8)) } } /* 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((int64(pData) - int64(aData)) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(5)) + 1)) = uint8(int64(pData) - int64(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 */ /* 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 + 80 + uintptr(k)*4)) <= i) { break } goto _1 _1: ; k++ } pEnd = *(*uintptr)(unsafe.Pointer(pCArray + 32 + uintptr(k)*8)) for int32(1) != 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 int64(pData)-int64(pBegin) < int64(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 */ if uint64(*(*uintptr)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*8))+uintptr(sz)) > uint64(pEnd) && uint64(*(*uintptr)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*8))) < uint64(pEnd) { _sqlite3CorruptError(tls, int32(78219)) return int32(1) } libc.X__builtin___memmove_chk(tls, pSlot, *(*uintptr)(unsafe.Pointer((*TCellArray)(unsafe.Pointer(pCArray)).FapCell + uintptr(i)*8)), uint64(sz), libc.X__builtin_object_size(tls, pSlot, 0)) *(*Tu8)(unsafe.Pointer(pCellptr)) = uint8((int64(pSlot) - int64(aData)) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(pCellptr + 1)) = uint8(int64(pSlot) - int64(aData)) pCellptr += uintptr(2) i++ if i >= iEnd { break } if *(*int32)(unsafe.Pointer(pCArray + 80 + uintptr(k)*4)) <= i { k++ pEnd = *(*uintptr)(unsafe.Pointer(pCArray + 32 + uintptr(k)*8)) } } *(*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)*8)) if uint64(pCell) >= uint64(pStart) && uint64(pCell) < uint64(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))) iOfst = int32(uint16(int64(pCell) - int64(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.Uint64FromInt64(40)/libc.Uint64FromInt64(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 */ if iOld < iNew { nShift = _pageFreeArray(tls, pPg, iOld, iNew-iOld, pCArray) if nShift > nCell { return _sqlite3CorruptError(tls, int32(78341)) } libc.X__builtin___memmove_chk(tls, (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx, (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx+uintptr(nShift*int32(2)), uint64(nCell*int32(2)), libc.X__builtin_object_size(tls, (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx, 0)) nCell -= nShift } if iNewEnd < iOldEnd { nTail = _pageFreeArray(tls, pPg, iNewEnd, iOldEnd-iNewEnd, pCArray) 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 pCellptr = (*TMemPage)(unsafe.Pointer(pPg)).FaCellIdx libc.X__builtin___memmove_chk(tls, pCellptr+uintptr(nAdd*int32(2)), pCellptr, uint64(nCell*int32(2)), libc.X__builtin_object_size(tls, pCellptr+uintptr(nAdd*int32(2)), 0)) 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.X__builtin___memmove_chk(tls, pCellptr+2, pCellptr, uint64((nCell-iCell)*int32(2)), libc.X__builtin_object_size(tls, pCellptr+2, 0)) } 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 */ 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((int64(*(*uintptr)(unsafe.Pointer(bp))) - int64(aData)) >> libc.Int32FromInt32(8)) *(*Tu8)(unsafe.Pointer(aData + uintptr(hdr+int32(5)) + 1)) = uint8(int64(*(*uintptr)(unsafe.Pointer(bp))) - int64(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(78415)) } _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(144) defer tls.Free(144) var pBt, pOut, pStop, v1, v3, v4 uintptr var v2 Tu8 var _ /* b at bp+32 */ TCellArray var _ /* pCell at bp+16 */ uintptr var _ /* pNew at bp+0 */ uintptr var _ /* pgnoNew at bp+12 */ TPgno var _ /* rc at bp+8 */ int32 var _ /* szCell at bp+24 */ Tu16 _, _, _, _, _, _, _ = pBt, pOut, pStop, v1, v2, v3, v4 pBt = (*TMemPage)(unsafe.Pointer(pPage)).FpBt /* Page number of pNew */ if int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) == 0 { return _sqlite3CorruptError(tls, int32(78455)) } /* dbfuzz001.test */ /* 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 + 8)) = _allocateBtreePage(tls, pBt, bp, bp+12, uint32(0), uint8(0)) if *(*int32)(unsafe.Pointer(bp + 8)) == SQLITE_OK { pOut = pSpace + 4 *(*uintptr)(unsafe.Pointer(bp + 16)) = *(*uintptr)(unsafe.Pointer(pPage + 40)) *(*Tu16)(unsafe.Pointer(bp + 24)) = (*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pPage)).FxCellSize})))(tls, pPage, *(*uintptr)(unsafe.Pointer(bp + 16))) _zeroPage(tls, *(*uintptr)(unsafe.Pointer(bp)), libc.Int32FromInt32(PTF_INTKEY)|libc.Int32FromInt32(PTF_LEAFDATA)|libc.Int32FromInt32(PTF_LEAF)) (*(*TCellArray)(unsafe.Pointer(bp + 32))).FnCell = int32(1) (*(*TCellArray)(unsafe.Pointer(bp + 32))).FpRef = pPage (*(*TCellArray)(unsafe.Pointer(bp + 32))).FapCell = bp + 16 (*(*TCellArray)(unsafe.Pointer(bp + 32))).FszCell = bp + 24 *(*uintptr)(unsafe.Pointer(bp + 32 + 32)) = (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd *(*int32)(unsafe.Pointer(bp + 32 + 80)) = int32(2) *(*int32)(unsafe.Pointer(bp + 8)) = _rebuildPage(tls, bp+32, 0, int32(1), *(*uintptr)(unsafe.Pointer(bp))) if *(*int32)(unsafe.Pointer(bp + 8)) != 0 { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 8)) } (*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 + 24)))) /* 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 + 12)), uint8(PTRMAP_BTREE), (*TMemPage)(unsafe.Pointer(pParent)).Fpgno, bp+8) if int32(*(*Tu16)(unsafe.Pointer(bp + 24))) > int32((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FminLocal) { _ptrmapPutOvflPtr(tls, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 16)), bp+8) } } /* 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 + 16)) = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(int32(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell)-int32(1))))))<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.X__builtin___memset_chk(tls, bp+100, 0, uint64(5), libc.X__builtin_object_size(tls, bp+100, 0)) libc.X__builtin___memset_chk(tls, bp+112, 0, uint64(104), libc.X__builtin_object_size(tls, bp+112, 0)) pBt = (*TMemPage)(unsafe.Pointer(pParent)).FpBt /* 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(). */ if !(aOvflSpace != 0) { return int32(SQLITE_NOMEM) } /* 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 { 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(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pParent)).FaCellIdx + uintptr(int32(2)*(i+nxDiv-int32((*TMemPage)(unsafe.Pointer(pParent)).FnOverflow))))))< 0 { if limit < int32(*(*Tu16)(unsafe.Pointer(pOld + 28))) { *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(78900)) goto balance_cleanup } limit = int32(*(*Tu16)(unsafe.Pointer(pOld + 28))) j = 0 for { if !(j < limit) { break } *(*uintptr)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 112))).FapCell + uintptr((*(*TCellArray)(unsafe.Pointer(bp + 112))).FnCell)*8)) = aData + uintptr(int32(maskPage)&(int32(*(*Tu8)(unsafe.Pointer(piCell)))< 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(79001)) goto balance_cleanup } (*(*[5]int32)(unsafe.Pointer(bp + 72)))[k-int32(1)] = 0 cntNew[k-int32(1)] = (*(*TCellArray)(unsafe.Pointer(bp + 112))).FnCell } sz1 = int32(2) + int32(_cachedCellSize(tls, bp+112, cntNew[i]-int32(1))) *(*int32)(unsafe.Pointer(bp + 72 + uintptr(i)*4)) -= sz1 if !(leafData != 0) { if cntNew[i] < (*(*TCellArray)(unsafe.Pointer(bp + 112))).FnCell { sz1 = int32(2) + int32(_cachedCellSize(tls, bp+112, cntNew[i])) } else { sz1 = 0 } } *(*int32)(unsafe.Pointer(bp + 72 + uintptr(i+int32(1))*4)) += sz1 cntNew[i]-- } for cntNew[i] < (*(*TCellArray)(unsafe.Pointer(bp + 112))).FnCell { sz1 = int32(2) + int32(_cachedCellSize(tls, bp+112, cntNew[i])) if (*(*[5]int32)(unsafe.Pointer(bp + 72)))[i]+sz1 > usableSpace { break } *(*int32)(unsafe.Pointer(bp + 72 + uintptr(i)*4)) += sz1 cntNew[i]++ if !(leafData != 0) { if cntNew[i] < (*(*TCellArray)(unsafe.Pointer(bp + 112))).FnCell { sz1 = int32(2) + int32(_cachedCellSize(tls, bp+112, cntNew[i])) } else { sz1 = 0 } } *(*int32)(unsafe.Pointer(bp + 72 + uintptr(i+int32(1))*4)) -= sz1 } if cntNew[i] >= (*(*TCellArray)(unsafe.Pointer(bp + 112))).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(79034)) 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 + 72)))[i] /* Size of sibling on the right */ szLeft = (*(*[5]int32)(unsafe.Pointer(bp + 72)))[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+112, d) for cond := true; cond; cond = r >= 0 { szR = int32(_cachedCellSize(tls, bp+112, r)) szD = int32(*(*Tu16)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 112))).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 + 72)))[i] = szRight (*(*[5]int32)(unsafe.Pointer(bp + 72)))[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(79078)) 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. */ /* ** Allocate k new pages. Reuse old pages where possible. */ pageFlags = int32(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer((*(*[3]uintptr)(unsafe.Pointer(bp + 8)))[0])).FaData))) i = 0 for { if !(i < k) { break } if i < nOld { v17 = (*(*[3]uintptr)(unsafe.Pointer(bp + 8)))[i] (*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[i] = v17 *(*uintptr)(unsafe.Pointer(bp + 216)) = v17 (*(*[3]uintptr)(unsafe.Pointer(bp + 8)))[i] = uintptr(0) *(*int32)(unsafe.Pointer(bp)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 216)))).FpDbPage) nNew++ if _sqlite3PagerPageRefcount(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 216)))).FpDbPage) != int32(1)+libc.BoolInt32(i == iParentIdx-nxDiv) && *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(79111)) } if *(*int32)(unsafe.Pointer(bp)) != 0 { goto balance_cleanup } } else { if bBulk != 0 { v18 = uint32(1) } else { v18 = *(*TPgno)(unsafe.Pointer(bp + 92)) } *(*int32)(unsafe.Pointer(bp)) = _allocateBtreePage(tls, pBt, bp+216, bp+92, v18, uint8(0)) if *(*int32)(unsafe.Pointer(bp)) != 0 { goto balance_cleanup } _zeroPage(tls, *(*uintptr)(unsafe.Pointer(bp + 216)), pageFlags) (*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[i] = *(*uintptr)(unsafe.Pointer(bp + 216)) nNew++ cntOld[i] = (*(*TCellArray)(unsafe.Pointer(bp + 112))).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 + 216)))).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 + 32)))[i])).Fpgno 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 + 32)))[j])).Fpgno < (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[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 + 32)))[i])).Fpgno pgnoB = (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[iB])).Fpgno pgnoTemp = uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize + uint32(1) fgA = (*TDbPage)(unsafe.Pointer((*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[i])).FpDbPage)).Fflags fgB = (*TDbPage)(unsafe.Pointer((*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[iB])).FpDbPage)).Fflags _sqlite3PagerRekey(tls, (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[i])).FpDbPage, pgnoTemp, fgB) _sqlite3PagerRekey(tls, (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[iB])).FpDbPage, pgnoA, fgA) _sqlite3PagerRekey(tls, (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[i])).FpDbPage, pgnoB, fgB) (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[i])).Fpgno = pgnoB (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[iB])).Fpgno = pgnoA } goto _20 _20: ; i++ } _sqlite3Put4byte(tls, pRight, (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[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 + 32 } else { v22 = bp + 8 } pOld1 = *(*uintptr)(unsafe.Pointer(v22 + uintptr(nOld-int32(1))*8)) libc.X__builtin___memcpy_chk(tls, (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[nNew-int32(1)])).FaData+8, (*TMemPage)(unsafe.Pointer(pOld1)).FaData+8, uint64(4), libc.X__builtin_object_size(tls, (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[nNew-int32(1)])).FaData+8, 0)) } /* 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 + 32)))[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 + 112))).FnCell) { break } pCell = *(*uintptr)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 112))).FapCell + uintptr(i)*8)) for i == cntOldNext { iOld++ if iOld < nNew { v25 = (*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[iOld] } else { v25 = (*(*[3]uintptr)(unsafe.Pointer(bp + 8)))[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 + 32)))[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] || !(uint64(pCell) >= uint64((*TMemPage)(unsafe.Pointer(pOld2)).FaData) && uint64(pCell) < uint64((*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+112, 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 + 32)))[i] j = cntNew[i] pCell1 = *(*uintptr)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 112))).FapCell + uintptr(j)*8)) sz2 = int32(*(*Tu16)(unsafe.Pointer((*(*TCellArray)(unsafe.Pointer(bp + 112))).FszCell + uintptr(j)*2))) + int32(leafCorrection) pTemp1 = aOvflSpace + uintptr(iOvflSpace) if !((*TMemPage)(unsafe.Pointer(pNew2)).Fleaf != 0) { libc.X__builtin___memcpy_chk(tls, (*TMemPage)(unsafe.Pointer(pNew2)).FaData+8, pCell1, uint64(4), libc.X__builtin_object_size(tls, (*TMemPage)(unsafe.Pointer(pNew2)).FaData+8, 0)) } 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 + 112))).FapCell + uintptr(j)*8)), bp+224) pCell1 = pTemp1 sz2 = int32(4) + _sqlite3PutVarint(tls, pCell1+4, uint64((*(*TCellInfo)(unsafe.Pointer(bp + 224))).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 + 112))).FszCell + uintptr(j)*2))) == int32(4) { sz2 = int32((*(*func(*libc.TLS, uintptr, uintptr) Tu16)(unsafe.Pointer(&struct{ uintptr }{(*TMemPage)(unsafe.Pointer(pParent)).FxCellSize})))(tls, pParent, pCell1)) } } } iOvflSpace += sz2 k = 0 for { if !(k < libc.Int32FromInt32(NB)*libc.Int32FromInt32(2) && *(*int32)(unsafe.Pointer(bp + 112 + 80 + uintptr(k)*4)) <= j) { break } goto _28 _28: ; k++ } pSrcEnd = *(*uintptr)(unsafe.Pointer(bp + 112 + 32 + uintptr(k)*8)) if uint64(pCell1) < uint64(pSrcEnd) && uint64(pCell1+uintptr(sz2)) > uint64(pSrcEnd) { *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(79311)) 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 } 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 if (*(*[5]Tu8)(unsafe.Pointer(bp + 100)))[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. */ /* Verify condition (2): If cells are moving right, update iPg ** only after iPg+1 has already been updated. */ 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 + 112))).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 + 32)))[iPg], iOld1, iNew1, nNewCell, bp+112) if *(*int32)(unsafe.Pointer(bp)) != 0 { goto balance_cleanup } (*(*[5]Tu8)(unsafe.Pointer(bp + 100)))[iPg]++ (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[iPg])).FnFree = usableSpace - (*(*[5]int32)(unsafe.Pointer(bp + 72)))[iPg] } goto _29 _29: ; i++ } /* All pages have been processed exactly once */ if isRoot != 0 && int32((*TMemPage)(unsafe.Pointer(pParent)).FnCell) == 0 && int32((*TMemPage)(unsafe.Pointer(pParent)).FhdrOffset) <= (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[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. */ *(*int32)(unsafe.Pointer(bp)) = _defragmentPage(tls, (*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[0], -int32(1)) _copyNodeContent(tls, (*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[0], pParent, bp) _freePage(tls, (*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[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 + 32)))[i])).FaData+8) _ptrmapPut(tls, pBt, key, uint8(PTRMAP_BTREE), (*TMemPage)(unsafe.Pointer((*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[i])).Fpgno, bp) goto _33 _33: ; i++ } } } /* 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 + 8)))[i], bp) goto _34 _34: ; i++ } /* ** Cleanup before returning. */ goto balance_cleanup balance_cleanup: ; _sqlite3DbFree(tls, uintptr(0), (*(*TCellArray)(unsafe.Pointer(bp + 112))).FapCell) i = 0 for { if !(i < nOld) { break } _releasePage(tls, (*(*[3]uintptr)(unsafe.Pointer(bp + 8)))[i]) goto _35 _35: ; i++ } i = 0 for { if !(i < nNew) { break } _releasePage(tls, (*(*[5]uintptr)(unsafe.Pointer(bp + 32)))[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(32) defer tls.Free(32) var pBt uintptr var _ /* pChild at bp+8 */ uintptr var _ /* pgnoChild at bp+16 */ TPgno var _ /* rc at bp+0 */ int32 _ = pBt /* Return value from subprocedures */ *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) /* Pointer to a new child page */ *(*TPgno)(unsafe.Pointer(bp + 16)) = uint32(0) /* Page number of the new child page */ pBt = (*TMemPage)(unsafe.Pointer(pRoot)).FpBt /* The BTree */ /* 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+8, bp+16, (*TMemPage)(unsafe.Pointer(pRoot)).Fpgno, uint8(0)) _copyNodeContent(tls, pRoot, *(*uintptr)(unsafe.Pointer(bp + 8)), bp) if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 { _ptrmapPut(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 16)), 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 + 8))) return *(*int32)(unsafe.Pointer(bp)) } /* Copy the overflow cells from pRoot to pChild */ libc.X__builtin___memcpy_chk(tls, *(*uintptr)(unsafe.Pointer(bp + 8))+28, pRoot+28, uint64((*TMemPage)(unsafe.Pointer(pRoot)).FnOverflow)*uint64(2), libc.X__builtin_object_size(tls, *(*uintptr)(unsafe.Pointer(bp + 8))+28, 0)) libc.X__builtin___memcpy_chk(tls, *(*uintptr)(unsafe.Pointer(bp + 8))+40, pRoot+40, uint64((*TMemPage)(unsafe.Pointer(pRoot)).FnOverflow)*uint64(8), libc.X__builtin_object_size(tls, *(*uintptr)(unsafe.Pointer(bp + 8))+40, 0)) (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).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 + 8)))).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 + 16))) *(*uintptr)(unsafe.Pointer(ppChild)) = *(*uintptr)(unsafe.Pointer(bp + 8)) 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(79543)) } 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. */ rc = _balance_deeper(tls, pPage, pCur+144+1*8) if rc == SQLITE_OK { (*TBtCursor)(unsafe.Pointer(pCur)).FiPage = int8(1) (*TBtCursor)(unsafe.Pointer(pCur)).Fix = uint16(0) *(*Tu16)(unsafe.Pointer(pCur + 88)) = uint16(0) *(*uintptr)(unsafe.Pointer(pCur + 144)) = pPage (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = *(*uintptr)(unsafe.Pointer(pCur + 144 + 1*8)) } } 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(79603)) } else { pParent = *(*uintptr)(unsafe.Pointer(pCur + 144 + uintptr(iPage-int32(1))*8)) iIdx = int32(*(*Tu16)(unsafe.Pointer(pCur + 88 + 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. */ 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-- (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = *(*uintptr)(unsafe.Pointer(pCur + 144 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*8)) } } } } 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.X__builtin___memset_chk(tls, pDest+uintptr(i), 0, uint64(iAmt-i), libc.X__builtin_object_size(tls, pDest+uintptr(i), 0)) } } 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), uint64(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.X__builtin___memmove_chk(tls, pDest, (*TBtreePayload)(unsafe.Pointer(pX)).FpData+uintptr(iOffset), uint64(iAmt), libc.X__builtin_object_size(tls, pDest, 0)) } } 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 */ /* 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) 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(79767)) } 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(79795)) } 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(160) defer tls.Free(160) var idx int32 var newCell, oldCell, p, pPage, v3, p1, p4, p5 uintptr var ovfl TPgno var v2 Tu16 var _ /* info at bp+104 */ TCellInfo var _ /* info at bp+128 */ 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+56 */ 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) /* 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(79876)) } } /* 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)) } } /* 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. */ if (*TBtCursor)(unsafe.Pointer(pCur)).FpKeyInfo == uintptr(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 */ 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) } } 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. */ /* 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 + 56))).FpData = (*TBtreePayload)(unsafe.Pointer(pX)).FpKey (*(*TBtreePayload)(unsafe.Pointer(bp + 56))).FnData = int32((*TBtreePayload)(unsafe.Pointer(pX)).FnKey) (*(*TBtreePayload)(unsafe.Pointer(bp + 56))).FnZero = 0 return _btreeOverwriteCell(tls, pCur, bp+56) } } } pPage = (*TBtCursor)(unsafe.Pointer(pCur)).FpPage 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(79999)) } else { *(*int32)(unsafe.Pointer(bp)) = _btreeComputeFreeSpace(tls, pPage) } if *(*int32)(unsafe.Pointer(bp)) != 0 { return *(*int32)(unsafe.Pointer(bp)) } } newCell = (*TBtShared)(unsafe.Pointer((*TBtree)(unsafe.Pointer(p)).FpBt)).FpTmpSpace 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+104) if (*(*TCellInfo)(unsafe.Pointer(bp + 104))).FnPayload != uint32((*(*TCellInfo)(unsafe.Pointer(bp + 104))).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 } } idx = int32((*TBtCursor)(unsafe.Pointer(pCur)).Fix) (*TBtCursor)(unsafe.Pointer(pCur)).Finfo.FnSize = uint16(0) if *(*int32)(unsafe.Pointer(bp + 4)) == 0 { if idx >= int32((*TMemPage)(unsafe.Pointer(pPage)).FnCell) { return _sqlite3CorruptError(tls, int32(80041)) } *(*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(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*idx))))< (*TMemPage)(unsafe.Pointer(pPage)).FaDataEnd { return _sqlite3CorruptError(tls, int32(80071)) } libc.X__builtin___memcpy_chk(tls, oldCell, newCell, uint64(*(*int32)(unsafe.Pointer(bp + 8))), libc.X__builtin_object_size(tls, oldCell, 0)) return SQLITE_OK } _dropCell(tls, pPage, idx, int32((*(*TCellInfo)(unsafe.Pointer(bp + 128))).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 { v3 = pCur + 86 *(*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 { } } *(*int32)(unsafe.Pointer(bp)) = _insertCellFast(tls, pPage, idx, newCell, *(*int32)(unsafe.Pointer(bp + 8))) /* 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 { 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 { (*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.X__builtin___memcpy_chk(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpKey, (*TBtreePayload)(unsafe.Pointer(pX)).FpKey, uint64((*TBtreePayload)(unsafe.Pointer(pX)).FnKey), libc.X__builtin_object_size(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpKey, 0)) } } (*TBtCursor)(unsafe.Pointer(pCur)).FeState = uint8(CURSOR_REQUIRESEEK) (*TBtCursor)(unsafe.Pointer(pCur)).FnKey = (*TBtreePayload)(unsafe.Pointer(pX)).FnKey } } 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(32) defer tls.Free(32) 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+24 */ uintptr var _ /* pPageIn at bp+8 */ uintptr var _ /* pgnoNew at bp+16 */ 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(80173)) } nRem = (*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload if nIn == nRem && nIn < uint32((*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pDest)).FpPage)).FmaxLocal) { libc.X__builtin___memcpy_chk(tls, aOut, aIn, uint64(nIn), libc.X__builtin_object_size(tls, aOut, 0)) (*TBtShared)(unsafe.Pointer(pBt)).FnPreformatSize = int32(int64(nIn) + (int64(aOut) - int64((*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 + 8)) = 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(int64(nOut) + (int64(aOut) - int64((*TBtShared)(unsafe.Pointer(pBt)).FpTmpSpace))) if nOut < (*TBtCursor)(unsafe.Pointer(pSrc)).Finfo.FnPayload { pPgnoOut = aOut + uintptr(nOut) *(*int32)(unsafe.Pointer(pBt + 144)) += int32(4) } if nRem > nIn { if aIn+uintptr(nIn)+uintptr(4) > (*TMemPage)(unsafe.Pointer((*TBtCursor)(unsafe.Pointer(pSrc)).FpPage)).FaDataEnd { return _sqlite3CorruptError(tls, int32(80198)) } 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) { if nIn > uint32(0) { if nOut < nIn { v2 = nOut } else { v2 = nIn } nCopy = int32(v2) libc.X__builtin___memcpy_chk(tls, aOut, aIn, uint64(nCopy), libc.X__builtin_object_size(tls, aOut, 0)) nOut -= uint32(nCopy) nIn -= uint32(nCopy) aOut += uintptr(nCopy) aIn += uintptr(nCopy) } if nOut > uint32(0) { _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) *(*int32)(unsafe.Pointer(bp)) = _sqlite3PagerGet(tls, pSrcPager, ovflIn, bp+8, int32(PAGER_GET_READONLY)) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { aIn = _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) 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 + 24)) = uintptr(0) *(*int32)(unsafe.Pointer(bp)) = _allocateBtreePage(tls, pBt, bp+24, bp+16, uint32(0), uint8(0)) _sqlite3Put4byte(tls, pPgnoOut, *(*TPgno)(unsafe.Pointer(bp + 16))) if (*TBtShared)(unsafe.Pointer(pBt)).FautoVacuum != 0 && pPageOut != 0 { _ptrmapPut(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 16)), uint8(PTRMAP_OVERFLOW2), (*TMemPage)(unsafe.Pointer(pPageOut)).Fpgno, bp) } _releasePage(tls, pPageOut) pPageOut = *(*uintptr)(unsafe.Pointer(bp + 24)) 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 + 8))) 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 */ 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) if *(*int32)(unsafe.Pointer(bp)) != 0 || int32((*TBtCursor)(unsafe.Pointer(pCur)).FeState) != CURSOR_VALID { return *(*int32)(unsafe.Pointer(bp)) } } else { return _sqlite3CorruptError(tls, int32(80294)) } } 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(80303)) } pCell = (*TMemPage)(unsafe.Pointer(pPage)).FaData + uintptr(int32((*TMemPage)(unsafe.Pointer(pPage)).FmaskPage)&(int32(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*iCellIdx))))< 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) 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 + 144 + uintptr(iCellDepth+int32(1))*8)))).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(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pLeaf)).FaCellIdx + uintptr(int32(2)*(int32((*TMemPage)(unsafe.Pointer(pLeaf)).FnCell)-int32(1))))))< iCellDepth { _releasePageNotNull(tls, (*TBtCursor)(unsafe.Pointer(pCur)).FpPage) (*TBtCursor)(unsafe.Pointer(pCur)).FiPage-- for int32((*TBtCursor)(unsafe.Pointer(pCur)).FiPage) > iCellDepth { v2 = pCur + 84 v1 = *(*Ti8)(unsafe.Pointer(v2)) *(*Ti8)(unsafe.Pointer(v2))-- _releasePage(tls, *(*uintptr)(unsafe.Pointer(pCur + 144 + uintptr(v1)*8))) } (*TBtCursor)(unsafe.Pointer(pCur)).FpPage = *(*uintptr)(unsafe.Pointer(pCur + 144 + uintptr((*TBtCursor)(unsafe.Pointer(pCur)).FiPage)*8)) *(*int32)(unsafe.Pointer(bp)) = _balance(tls, pCur) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if int32(bPreserve) > int32(1) { (*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(48) defer tls.Free(48) var pBt uintptr var ptfFlags int32 var _ /* eType at bp+32 */ Tu8 var _ /* iPtrPage at bp+36 */ TPgno var _ /* pPageMove at bp+24 */ uintptr var _ /* pRoot at bp+0 */ uintptr var _ /* pgnoMove at bp+16 */ TPgno var _ /* pgnoRoot at bp+8 */ TPgno var _ /* rc at bp+12 */ int32 _, _ = pBt, ptfFlags pBt = (*TBtree)(unsafe.Pointer(p)).FpBt /* Page-type flags for the root page of new table */ 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+8) if *(*TPgno)(unsafe.Pointer(bp + 8)) > _btreePagecount(tls, pBt) { return _sqlite3CorruptError(tls, int32(80517)) } *(*TPgno)(unsafe.Pointer(bp + 8))++ /* The new root-page may not be allocated on a pointer-map page, or the ** PENDING_BYTE page. */ for *(*TPgno)(unsafe.Pointer(bp + 8)) == _ptrmapPageno(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 8))) || *(*TPgno)(unsafe.Pointer(bp + 8)) == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) { *(*TPgno)(unsafe.Pointer(bp + 8))++ } /* 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 + 12)) = _allocateBtreePage(tls, pBt, bp+24, bp+16, *(*TPgno)(unsafe.Pointer(bp + 8)), uint8(BTALLOC_EXACT)) if *(*int32)(unsafe.Pointer(bp + 12)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 12)) } if *(*TPgno)(unsafe.Pointer(bp + 16)) != *(*TPgno)(unsafe.Pointer(bp + 8)) { /* 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 + 32)) = uint8(0) *(*TPgno)(unsafe.Pointer(bp + 36)) = 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 + 12)) = _saveAllCursors(tls, pBt, uint32(0), uintptr(0)) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 24))) if *(*int32)(unsafe.Pointer(bp + 12)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 12)) } /* Move the page currently at pgnoRoot to pgnoMove. */ *(*int32)(unsafe.Pointer(bp + 12)) = _btreeGetPage(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 8)), bp, 0) if *(*int32)(unsafe.Pointer(bp + 12)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 12)) } *(*int32)(unsafe.Pointer(bp + 12)) = _ptrmapGet(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 8)), bp+32, bp+36) if int32(*(*Tu8)(unsafe.Pointer(bp + 32))) == int32(PTRMAP_ROOTPAGE) || int32(*(*Tu8)(unsafe.Pointer(bp + 32))) == int32(PTRMAP_FREEPAGE) { *(*int32)(unsafe.Pointer(bp + 12)) = _sqlite3CorruptError(tls, int32(80565)) } if *(*int32)(unsafe.Pointer(bp + 12)) != SQLITE_OK { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 12)) } *(*int32)(unsafe.Pointer(bp + 12)) = _relocatePage(tls, pBt, *(*uintptr)(unsafe.Pointer(bp)), *(*Tu8)(unsafe.Pointer(bp + 32)), *(*TPgno)(unsafe.Pointer(bp + 36)), *(*TPgno)(unsafe.Pointer(bp + 16)), 0) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) /* Obtain the page at pgnoRoot */ if *(*int32)(unsafe.Pointer(bp + 12)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 12)) } *(*int32)(unsafe.Pointer(bp + 12)) = _btreeGetPage(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 8)), bp, 0) if *(*int32)(unsafe.Pointer(bp + 12)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 12)) } *(*int32)(unsafe.Pointer(bp + 12)) = _sqlite3PagerWrite(tls, (*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FpDbPage) if *(*int32)(unsafe.Pointer(bp + 12)) != SQLITE_OK { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 12)) } } else { *(*uintptr)(unsafe.Pointer(bp)) = *(*uintptr)(unsafe.Pointer(bp + 24)) } /* Update the pointer-map and meta-data with the new root-page number. */ _ptrmapPut(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 8)), uint8(PTRMAP_ROOTPAGE), uint32(0), bp+12) if *(*int32)(unsafe.Pointer(bp + 12)) != 0 { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 12)) } /* 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. */ *(*int32)(unsafe.Pointer(bp + 12)) = _sqlite3BtreeUpdateMeta(tls, p, int32(4), *(*TPgno)(unsafe.Pointer(bp + 8))) if *(*int32)(unsafe.Pointer(bp + 12)) != 0 { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp))) return *(*int32)(unsafe.Pointer(bp + 12)) } } else { *(*int32)(unsafe.Pointer(bp + 12)) = _allocateBtreePage(tls, pBt, bp, bp+8, uint32(1), uint8(0)) if *(*int32)(unsafe.Pointer(bp + 12)) != 0 { return *(*int32)(unsafe.Pointer(bp + 12)) } } 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) *(*TPgno)(unsafe.Pointer(piTable)) = *(*TPgno)(unsafe.Pointer(bp + 8)) 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(48) defer tls.Free(48) var hdr, i, v2 int32 var pCell uintptr var _ /* info at bp+16 */ TCellInfo var _ /* pPage at bp+0 */ uintptr var _ /* rc at bp+8 */ int32 _, _, _, _ = hdr, i, pCell, v2 if pgno > _btreePagecount(tls, pBt) { return _sqlite3CorruptError(tls, int32(80655)) } *(*int32)(unsafe.Pointer(bp + 8)) = _getAndInitPage(tls, pBt, pgno, bp, 0) if *(*int32)(unsafe.Pointer(bp + 8)) != 0 { return *(*int32)(unsafe.Pointer(bp + 8)) } 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 + 8)) = _sqlite3CorruptError(tls, int32(80662)) 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(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaCellIdx + uintptr(int32(2)*i))))< _btreePagecount(tls, pBt) { return _sqlite3CorruptError(tls, int32(80766)) } *(*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+8, 0) if *(*int32)(unsafe.Pointer(bp)) != 0 { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) 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+16) if iTable == *(*TPgno)(unsafe.Pointer(bp + 16)) { /* 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 + 8)), bp) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp)) } } else { _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) *(*int32)(unsafe.Pointer(bp)) = _btreeGetPage(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 16)), bp+24, 0) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp)) } *(*int32)(unsafe.Pointer(bp)) = _relocatePage(tls, pBt, *(*uintptr)(unsafe.Pointer(bp + 24)), uint8(PTRMAP_ROOTPAGE), uint32(0), iTable, 0) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 24))) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp)) } *(*uintptr)(unsafe.Pointer(bp + 24)) = uintptr(0) *(*int32)(unsafe.Pointer(bp)) = _btreeGetPage(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 16)), bp+24, 0) _freePage(tls, *(*uintptr)(unsafe.Pointer(bp + 24)), bp) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 24))) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp)) } *(*int32)(unsafe.Pointer(piMoved)) = int32(*(*TPgno)(unsafe.Pointer(bp + 16))) } /* 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 + 16))-- for *(*TPgno)(unsafe.Pointer(bp + 16)) == uint32(_sqlite3PendingByte)/(*TBtShared)(unsafe.Pointer(pBt)).FpageSize+libc.Uint32FromInt32(1) || _ptrmapPageno(tls, pBt, *(*TPgno)(unsafe.Pointer(bp + 16))) == *(*TPgno)(unsafe.Pointer(bp + 16)) { *(*TPgno)(unsafe.Pointer(bp + 16))-- } *(*int32)(unsafe.Pointer(bp)) = _sqlite3BtreeUpdateMeta(tls, p, int32(4), *(*TPgno)(unsafe.Pointer(bp + 16))) } else { _freePage(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), bp) _releasePage(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) } 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) 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 _sqlite3BtreeEnter(tls, p) 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) { (*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.AtomicLoadPInt32(db+432) != 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(*(*Tu8)(unsafe.Pointer((*TMemPage)(unsafe.Pointer(pPage)).FaCellIdx + uintptr(int32(2)*iIdx))))<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+4617, libc.VaList(bp+8, iPage)) return int32(1) } if _getPageReferenced(tls, pCheck, iPage) != 0 { _checkAppendMsg(tls, pCheck, __ccgo_ts+4640, 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+4770, 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 + 4809 } else { v2 = __ccgo_ts + 4814 } _checkAppendMsg(tls, pCheck, __ccgo_ts+4835, 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 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+24 */ TCellInfo var _ /* pPage at bp+8 */ uintptr var _ /* x at bp+16 */ 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 + 4861 (*TIntegrityCk)(unsafe.Pointer(pCheck)).Fv1 = iPage v1 = _btreeGetPage(tls, pBt, iPage, bp+8, 0) rc = v1 if v1 != 0 { _checkAppendMsg(tls, pCheck, __ccgo_ts+4879, libc.VaList(bp+56, 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 pc = uint32(int32(*(*Tu8)(unsafe.Pointer(pCellIdx)))< usableSize-uint32(4) { _checkAppendMsg(tls, pCheck, __ccgo_ts+5033, libc.VaList(bp+56, 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+24) if pc+uint32((*(*TCellInfo)(unsafe.Pointer(bp + 24))).FnSize) > usableSize { _checkAppendMsg(tls, pCheck, __ccgo_ts+5063, 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 + 24))).FnKey > *(*Ti64)(unsafe.Pointer(bp))) } else { v5 = libc.BoolInt32((*(*TCellInfo)(unsafe.Pointer(bp + 24))).FnKey >= *(*Ti64)(unsafe.Pointer(bp))) } if v5 != 0 { _checkAppendMsg(tls, pCheck, __ccgo_ts+5087, libc.VaList(bp+56, (*(*TCellInfo)(unsafe.Pointer(bp + 24))).FnKey)) } *(*Ti64)(unsafe.Pointer(bp)) = (*(*TCellInfo)(unsafe.Pointer(bp + 24))).FnKey keyCanBeEqual = 0 /* Only the first key on the page may ==maxKey */ } /* Check the content overflow list */ if (*(*TCellInfo)(unsafe.Pointer(bp + 24))).FnPayload > uint32((*(*TCellInfo)(unsafe.Pointer(bp + 24))).FnLocal) { /* First page of the overflow chain */ nPage = ((*(*TCellInfo)(unsafe.Pointer(bp + 24))).FnPayload - uint32((*(*TCellInfo)(unsafe.Pointer(bp + 24))).FnLocal) + usableSize - uint32(5)) / (usableSize - uint32(4)) pgnoOvfl = _sqlite3Get4byte(tls, pCell+uintptr(int32((*(*TCellInfo)(unsafe.Pointer(bp + 24))).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+5111, 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(int32(*(*Tu8)(unsafe.Pointer(data + uintptr(cellStart+i*int32(2)))))< 0 { /* Enforced by btreeComputeFreeSpace() */ size1 = int32(*(*Tu8)(unsafe.Pointer(data + uintptr(i+int32(2)))))<= *(*Tu32)(unsafe.Pointer(bp + 16))>>libc.Int32FromInt32(16) { _checkAppendMsg(tls, pCheck, __ccgo_ts+5136, libc.VaList(bp+56, *(*Tu32)(unsafe.Pointer(bp + 16))>>int32(16), iPage)) break } else { nFrag = int32(uint32(nFrag) + (*(*Tu32)(unsafe.Pointer(bp + 16))>>libc.Int32FromInt32(16) - prev&libc.Uint32FromInt32(0xffff) - libc.Uint32FromInt32(1))) prev = *(*Tu32)(unsafe.Pointer(bp + 16)) } } 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+5173, libc.VaList(bp+56, 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, aCnt uintptr, nRoot int32, mxErr int32, pnErr uintptr, pzOut uintptr) (r int32) { bp := tls.Alloc(272) defer tls.Free(272) var bCkFreelist, bPartial int32 var i, mx, mxInHdr TPgno var pBt uintptr var savedDbFlags Tu64 var _ /* notUsed at bp+232 */ Ti64 var _ /* sCheck at bp+0 */ TIntegrityCk var _ /* zErr at bp+128 */ [100]int8 _, _, _, _, _, _, _ = 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 */ /* aRoot[0]==0 means this is a partial check */ if *(*TPgno)(unsafe.Pointer(aRoot)) == uint32(0) { bPartial = int32(1) if *(*TPgno)(unsafe.Pointer(aRoot + 1*4)) != uint32(1) { bCkFreelist = 0 } } _sqlite3BtreeEnter(tls, p) libc.X__builtin___memset_chk(tls, bp, 0, uint64(128), libc.X__builtin_object_size(tls, bp, 0)) (*(*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+72, uintptr(0), bp+128, 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 + 5225 _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+5236, libc.VaList(bp+248, mx, mxInHdr)) } } else { if _sqlite3Get4byte(tls, (*TMemPage)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).FpPage1)).FaData+64) != uint32(0) { _checkAppendMsg(tls, bp, __ccgo_ts+5281, 0) } } } *(*Tu64)(unsafe.Pointer((*TBtShared)(unsafe.Pointer(pBt)).Fdb + 48)) &= ^libc.Uint64FromInt32(SQLITE_CellSizeCk) i = uint32(0) for { if !(int32(i) < nRoot && (*(*TIntegrityCk)(unsafe.Pointer(bp))).FmxErr != 0) { break } (*(*TIntegrityCk)(unsafe.Pointer(bp))).FnRow = 0 if *(*TPgno)(unsafe.Pointer(aRoot + uintptr(i)*4)) != 0 { 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+232, 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 } 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) /* 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) } 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 */ /* 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 + 40 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(BTS_NO_WAL)) if iVersion == int32(1) { p2 = pBt + 40 *(*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 + 40 *(*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.Uint64FromInt64(136) + libc.Uint64FromInt32(7)) & uint64(^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(448) defer tls.Free(448) 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+3863, libc.VaList(bp+432, (*(*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+5388, libc.VaList(bp+432, zDb)) return uintptr(0) } return (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(pDb)).FaDb + uintptr(i)*32))).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+5408, 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+5439, 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(72)) 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 /* 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.X__builtin___memcpy_chk(tls, zOut, zIn, uint64(nCopy), libc.X__builtin_object_size(tls, zOut, 0)) *(*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 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(32) defer tls.Free(32) 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+8 */ int32 var _ /* pPg at bp+16 */ uintptr var _ /* pSrcPg at bp+0 */ uintptr var _ /* pSrcPg at bp+24 */ 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+16) 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)) 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. */ 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) } 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) /* 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+8) iPg = uint32(nDestTruncate) for { if !(rc == SQLITE_OK && iPg <= uint32(*(*int32)(unsafe.Pointer(bp + 8)))) { 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+16, 0) if rc == SQLITE_OK { rc = _sqlite3PagerWrite(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) } } 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 + 24)) = uintptr(0) iSrcPg1 = uint32(iOff/int64(pgszSrc) + libc.Int64FromInt32(1)) rc = _sqlite3PagerGet(tls, pSrcPager, iSrcPg1, bp+24, 0) if rc == SQLITE_OK { zData = _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp + 24))) rc = _sqlite3OsWrite(tls, pFile, zData, pgszSrc, iOff) } _sqlite3PagerUnref(tls, *(*uintptr)(unsafe.Pointer(bp + 24))) 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) } 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)) rc = Xsqlite3_backup_finish(tls, bp) if rc == SQLITE_OK { p1 = (*TBtree)(unsafe.Pointer(pTo)).FpBt + 40 *(*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)) } 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 _ = v1 if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Int) != 0 { (*TMem)(unsafe.Pointer(p)).Fn = _sqlite3Int64ToText(tls, *(*Ti64)(unsafe.Pointer(p)), 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+5479, libc.VaList(bp+40, v1)) *(*int8)(unsafe.Pointer(zBuf + uintptr((*(*TStrAccum)(unsafe.Pointer(bp))).FnChar))) = 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 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 } /* 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)) 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 /* If the bPreserve flag is set to true, then the memory cell must already ** contain a valid string or blob value. */ 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.X__builtin___memcpy_chk(tls, (*TMem)(unsafe.Pointer(pMem)).FzMalloc, (*TMem)(unsafe.Pointer(pMem)).Fz, uint64((*TMem)(unsafe.Pointer(pMem)).Fn), libc.X__builtin_object_size(tls, (*TMem)(unsafe.Pointer(pMem)).FzMalloc, 0)) } if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Dyn) != 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 + 20 *(*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 if (*TMem)(unsafe.Pointer(pMem)).FszMalloc < szNew { return _sqlite3VdbeMemGrow(tls, pMem, szNew, 0) } (*TMem)(unsafe.Pointer(pMem)).Fz = (*TMem)(unsafe.Pointer(pMem)).FzMalloc p1 = pMem + 20 *(*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)) { *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = 0 p1 = pMem + 20 *(*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 + 20 *(*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) { *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = 0 p3 = pMem + 20 *(*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) } *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = 0 *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn+int32(1)))) = 0 *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn+int32(2)))) = 0 p1 = pMem + 20 *(*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 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 + 20 *(*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 /* 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.X__builtin___memset_chk(tls, (*TMem)(unsafe.Pointer(pMem)).Fz+uintptr((*TMem)(unsafe.Pointer(pMem)).Fn), 0, uint64(*(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu))), libc.X__builtin_object_size(tls, (*TMem)(unsafe.Pointer(pMem)).Fz+uintptr((*TMem)(unsafe.Pointer(pMem)).Fn), 0)) *(*int32)(unsafe.Pointer(pMem + 16)) += *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pMem)).Fu)) p1 = pMem + 20 *(*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) { 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) 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) (*TMem)(unsafe.Pointer(pMem)).Fenc = uint8(SQLITE_UTF8) p1 = pMem + 20 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | (libc.Int32FromInt32(MEM_Str) | libc.Int32FromInt32(MEM_Term))) if bForce != 0 { p2 = pMem + 20 *(*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(112) defer tls.Free(112) var _ /* ctx at bp+0 */ Tsqlite3_context var _ /* t at bp+56 */ TMem libc.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) libc.X__builtin___memset_chk(tls, bp+56, 0, uint64(56), libc.X__builtin_object_size(tls, bp+56, 0)) (*(*TMem)(unsafe.Pointer(bp + 56))).Fflags = uint16(MEM_Null) (*(*TMem)(unsafe.Pointer(bp + 56))).Fdb = (*TMem)(unsafe.Pointer(pMem)).Fdb (*(*Tsqlite3_context)(unsafe.Pointer(bp))).FpOut = bp + 56 (*(*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 + 56))).Fdb)).Fenc (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*TFuncDef)(unsafe.Pointer(pFunc)).FxFinalize})))(tls, bp) /* IMP: R-24505-23230 */ if (*TMem)(unsafe.Pointer(pMem)).FszMalloc > 0 { _sqlite3DbFreeNN(tls, (*TMem)(unsafe.Pointer(pMem)).Fdb, (*TMem)(unsafe.Pointer(pMem)).FzMalloc) } libc.X__builtin___memcpy_chk(tls, pMem, bp+56, uint64(56), libc.X__builtin_object_size(tls, pMem, 0)) 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(64) defer tls.Free(64) var _ /* ctx at bp+0 */ Tsqlite3_context libc.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) _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) { if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Agg) != 0 { _sqlite3VdbeMemFinalize(tls, p, *(*uintptr)(unsafe.Pointer(p))) } if int32((*TMem)(unsafe.Pointer(p)).Fflags)&int32(MEM_Dyn) != 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) { 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) { 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 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) { 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 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) p3 = pMem + 20 *(*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 + 16)) &= ^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) { (*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 // // /* // ** Set the iIdx'th entry of array aMem[] to contain integer value val. // */ func _sqlite3MemSetArrayInt64(tls *libc.TLS, aMem uintptr, iIdx int32, val Ti64) { _sqlite3VdbeMemSetInt64(tls, aMem+uintptr(iIdx)*56, val) } // 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 _vdbeMemClear(tls, pMem) if zPType != 0 { v1 = zPType } else { v1 = __ccgo_ts + 1664 } *(*uintptr)(unsafe.Pointer(pMem)) = 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 _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 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 + 136))) } 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) _sqlite3VdbeMemShallowCopy(tls, pTo, pFrom, eType) } func _sqlite3VdbeMemShallowCopy(tls *libc.TLS, pTo uintptr, pFrom uintptr, srcType int32) { var p1, p2 uintptr _, _ = p1, p2 if int32((*TMem)(unsafe.Pointer(pTo)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { _vdbeClrCopy(tls, pTo, pFrom, srcType) return } libc.X__builtin___memcpy_chk(tls, pTo, pFrom, uint64(libc.UintptrFromInt32(0)+24), libc.X__builtin_object_size(tls, pTo, 0)) if int32((*TMem)(unsafe.Pointer(pFrom)).Fflags)&int32(MEM_Static) == 0 { p1 = pTo + 20 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^(libc.Int32FromInt32(MEM_Dyn) | libc.Int32FromInt32(MEM_Static) | libc.Int32FromInt32(MEM_Ephem))) p2 = pTo + 20 *(*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 if int32((*TMem)(unsafe.Pointer(pTo)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { _vdbeMemClearExternAndSetNull(tls, pTo) } libc.X__builtin___memcpy_chk(tls, pTo, pFrom, uint64(libc.UintptrFromInt32(0)+24), libc.X__builtin_object_size(tls, pTo, 0)) p1 = pTo + 20 *(*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 + 20 *(*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) { _sqlite3VdbeMemRelease(tls, pTo) libc.X__builtin___memcpy_chk(tls, pTo, pFrom, uint64(56), libc.X__builtin_object_size(tls, pTo, 0)) (*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 */ /* 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 + 136)) } else { iLimit = int32(SQLITE_MAX_LENGTH) } if nByte < 0 { if int32(enc) == int32(SQLITE_UTF8) { nByte = int64(libc.Xstrlen(tls, z)) } else { nByte = 0 for { if !(nByte <= int64(iLimit) && int32(*(*int8)(unsafe.Pointer(z + uintptr(nByte))))|int32(*(*int8)(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.X__builtin___memcpy_chk(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, z, uint64(nAlloc), libc.X__builtin_object_size(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, 0)) } 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(84060)) } 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 { *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr(amt))) = 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 */ /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() ** that both the BtShared and database handle mutexes are held. */ (*TMem)(unsafe.Pointer(pMem)).Fz = _sqlite3BtreePayloadFetch(tls, pCur, bp) 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 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 + 20 *(*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(int64((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fz)) { if _sqlite3VdbeMemMakeWriteable(tls, pVal) != SQLITE_OK { return uintptr(0) } } _sqlite3VdbeMemNulTerminate(tls, pVal) /* IMP: R-31275-44060 */ } else { _sqlite3VdbeMemStringify(tls, pVal, enc, uint8(0)) } if int32((*Tsqlite3_value)(unsafe.Pointer(pVal)).Fenc) == int32(enc) & ^libc.Int32FromInt32(SQLITE_UTF16_ALIGNED) { 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) } 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) { 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(56)) 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(uint64(56)*uint64(nCol) + (libc.Uint64FromInt64(40)+libc.Uint64FromInt32(7))&uint64(^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 { (*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem = pRec + uintptr((libc.Uint64FromInt64(40)+libc.Uint64FromInt32(7))&uint64(^libc.Int32FromInt32(7))) i = 0 for { if !(i < nCol) { break } (*(*TMem)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem + uintptr(i)*56))).Fflags = uint16(MEM_Null) (*(*TMem)(unsafe.Pointer((*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem + uintptr(i)*56))).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)*56) return (*TUnpackedRecord)(unsafe.Pointer(pRec)).FaMem + uintptr((*TValueNewStat4Ctx)(unsafe.Pointer(p)).FiVal)*56 } 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(80) defer tls.Free(80) 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 */ pList = *(*uintptr)(unsafe.Pointer(p + 32)) if pList != 0 { nVal = (*TExprList)(unsafe.Pointer(pList)).FnExpr } pFunc = _sqlite3FindFunction(tls, db, *(*uintptr)(unsafe.Pointer(p + 8)), nVal, enc, uint8(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(8)*uint64(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)*32))).FpExpr, enc, aff, apVal+uintptr(i)*8) if *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*8)) == 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.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) (*(*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+3863, libc.VaList(bp+64, Xsqlite3_value_text(tls, pVal))) } else { _sqlite3ValueApplyAffinity(tls, pVal, aff, uint8(SQLITE_UTF8)) 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)*8))) 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(48) defer tls.Free(48) var aff Tu8 var nVal, negInt, op, rc, v1, v2 int32 var pLeft, zNeg, zVal, p3 uintptr var _ /* iVal at bp+8 */ Ti64 var _ /* pVal at bp+0 */ uintptr _, _, _, _, _, _, _, _, _, _, _ = aff, nVal, negInt, op, pLeft, rc, zNeg, zVal, v1, v2, p3 zVal = uintptr(0) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) negInt = int32(1) zNeg = __ccgo_ts + 1664 rc = SQLITE_OK 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(). */ if op == int32(TK_CAST) { aff = uint8(_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. Except - do not do this ** for hexadecimal literals. */ if op == int32(TK_UMINUS) { pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) == int32(TK_INTEGER) || int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) == int32(TK_FLOAT) { if (*TExpr)(unsafe.Pointer(pLeft)).Fflags&uint32(libc.Int32FromInt32(EP_IntValue)) != uint32(0) || int32(*(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pLeft + 8))))) != int32('0') || int32(*(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pLeft + 8)) + 1))) & ^libc.Int32FromInt32(0x20) != int32('X') { pExpr = pLeft op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) negInt = -int32(1) zNeg = __ccgo_ts + 5486 } } } 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(&(*TExpr)(unsafe.Pointer(pExpr)).Fu)))*int64(negInt)) } else { if op == int32(TK_INTEGER) && 0 == _sqlite3DecOrHexToI64(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), bp+8) { _sqlite3VdbeMemSetInt64(tls, *(*uintptr)(unsafe.Pointer(bp)), *(*Ti64)(unsafe.Pointer(bp + 8))*int64(negInt)) } else { zVal = _sqlite3MPrintf(tls, db, __ccgo_ts+5488, libc.VaList(bp+24, 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 int32(affinity) == int32(SQLITE_AFF_BLOB) { if op == int32(TK_FLOAT) { _sqlite3AtoF(tls, (*Tsqlite3_value)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fz, *(*uintptr)(unsafe.Pointer(bp)), (*Tsqlite3_value)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fn, uint8(SQLITE_UTF8)) (*Tsqlite3_value)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fflags = uint16(MEM_Real) } else { if op == int32(TK_INTEGER) { /* This case is required by -9223372036854775808 and other strings ** that look like integers but cannot be handled by the ** sqlite3DecOrHexToI64() call above. */ _sqlite3ValueApplyAffinity(tls, *(*uintptr)(unsafe.Pointer(bp)), uint8(SQLITE_AFF_NUMERIC), uint8(SQLITE_UTF8)) } } } else { _sqlite3ValueApplyAffinity(tls, *(*uintptr)(unsafe.Pointer(bp)), affinity, uint8(SQLITE_UTF8)) } 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)) + 20 *(*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)< uint32(nRec) || iHdr >= *(*Tu32)(unsafe.Pointer(bp + 4)) { return _sqlite3CorruptError(tls, int32(84745)) } iField = int64(*(*Tu32)(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 += uint32(uint8(v3)) if iHdr > *(*Tu32)(unsafe.Pointer(bp + 4)) { return _sqlite3CorruptError(tls, int32(84751)) } szField = _sqlite3VdbeSerialTypeLen(tls, *(*Tu32)(unsafe.Pointer(bp))) iField += int64(szField) goto _2 _2: ; i++ } if iField > int64(nRec) { return _sqlite3CorruptError(tls, int32(84757)) } 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-int64(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)*56) 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 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(304)) if p == uintptr(0) { return uintptr(0) } libc.X__builtin___memset_chk(tls, p+136, 0, libc.Uint64FromInt64(304)-uint64(libc.UintptrFromInt32(0)+136), libc.X__builtin_object_size(tls, p+136, 0)) (*TVdbe)(unsafe.Pointer(p)).Fdb = db if (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe != 0 { (*TVdbe1)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpVdbe)).FppVPrev = p + 16 } (*TVdbe)(unsafe.Pointer(p)).FpVNext = (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe (*TVdbe)(unsafe.Pointer(p)).FppVPrev = db + 8 (*Tsqlite3)(unsafe.Pointer(db)).FpVdbe = p (*TVdbe)(unsafe.Pointer(p)).FpParse = pParse (*TParse)(unsafe.Pointer(pParse)).FpVdbe = p _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) } (*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 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.X__builtin___memcpy_chk(tls, pB+212, pA+212, uint64(36), libc.X__builtin_object_size(tls, pB+212, 0)) *(*Tu32)(unsafe.Pointer(pB + 212 + 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.Uint64FromInt32(1024) / libc.Uint64FromInt64(24)) } /* 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 + 136 + 5*4))) { _sqlite3OomFault(tls, (*TParse)(unsafe.Pointer(p)).Fdb) return int32(SQLITE_NOMEM) } pNew = _sqlite3DbRealloc(tls, (*TParse)(unsafe.Pointer(p)).Fdb, (*TVdbe)(unsafe.Pointer(v)).FaOp, uint64(nNew)*uint64(24)) if pNew != 0 { (*TParse)(unsafe.Pointer(p)).FszOpAlloc = _sqlite3DbMallocSize(tls, (*TParse)(unsafe.Pointer(p)).Fdb, pNew) (*TVdbe)(unsafe.Pointer(v)).FnOpAlloc = int32(uint64((*TParse)(unsafe.Pointer(p)).FszOpAlloc) / uint64(24)) (*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) { if _growOpArray(tls, p, int32(1)) != 0 { return int32(1) } 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)*24 (*TVdbeOp)(unsafe.Pointer(pOp)).Fp4type = int8(-libc.Int32FromInt32(3)) (*TVdbeOp)(unsafe.Pointer(pOp)).Fp4.Fi = 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 if (*TVdbe)(unsafe.Pointer(p)).FnOpAlloc <= i { return _growOp3(tls, p, op, p1, p2, p3) } (*TVdbe)(unsafe.Pointer(p)).FnOp++ pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(i)*24 (*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)*24 (*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 (*TVdbeOp)(unsafe.Pointer(pOp)).Fp4.Fi = 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 int8 var i, v3 int32 var z uintptr _, _, _, _, _, _ = ap, c, i, z, v2, v3 ap = va i = 0 for { v2 = *(*int8)(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 nByte = int32(uint64(56) + uint64(nArg-libc.Int32FromInt32(1))*uint64(8)) pCtx = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(nByte)) if pCtx == uintptr(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.X__builtin___memcpy_chk(tls, p4copy, zP4, uint64(8), libc.X__builtin_object_size(tls, p4copy, 0)) } 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 + 68 *(*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(nNewSize)*uint64(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 if (*TParse)(unsafe.Pointer(p)).FnLabelAlloc+(*TParse)(unsafe.Pointer(p)).FnLabel < 0 { _resizeResolveLabel(tls, p, v, j) } else { /* 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)*24))).Fopcode) == int32(OP_Expire) { (*(*TOp)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaOp + 1*24))).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 /* tag-20230419-1 */ libc.SetBitFieldPtr16Uint32(p+200, libc.Uint32FromInt32(1), 6, 0x40) libc.SetBitFieldPtr16Uint32(p+200, libc.Uint32FromInt32(0), 7, 0x80) pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr((*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1))*24 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+200, libc.Uint32FromInt32(0), 6, 0x40) } fallthrough case int32(OP_AutoCommit): fallthrough case OP_Savepoint: libc.SetBitFieldPtr16Uint32(p+200, libc.Uint32FromInt32(1), 7, 0x80) case int32(OP_Checkpoint): fallthrough case int32(OP_Vacuum): fallthrough case int32(OP_JournalMode): libc.SetBitFieldPtr16Uint32(p+200, libc.Uint32FromInt32(0), 6, 0x40) libc.SetBitFieldPtr16Uint32(p+200, libc.Uint32FromInt32(1), 7, 0x80) case int32(OP_Init): 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): n = (*(*TOp)(unsafe.Pointer(pOp + uintptr(-libc.Int32FromInt32(1))*24))).Fp1 if n > nMaxArgs { nMaxArgs = n } /* Fall through into the default case */ 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. */ /* True because of tag-20230419-1 */ (*TOp)(unsafe.Pointer(pOp)).Fp2 = *(*int32)(unsafe.Pointer(aLabel + uintptr(^(*TOp)(unsafe.Pointer(pOp)).Fp2)*4)) } /* OPFLG_JUMP opcodes never have P2==0, though OPFLG_JUMP0 opcodes ** might */ /* Jumps never go off the end of the bytecode array */ 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. */ } pOp -= 24 } 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 } // C documentation // // /* // ** Return the address of the next instruction to be inserted. // */ func _sqlite3VdbeCurrentAddr(tls *libc.TLS, p uintptr) (r int32) { 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 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */ _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 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)*24 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) 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 += 24 } *(*int32)(unsafe.Pointer(p + 144)) += 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) { (*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, p, addr))).Fopcode = iNewOpcode } func _sqlite3VdbeChangeP1(tls *libc.TLS, p uintptr, addr int32, val int32) { (*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, p, addr))).Fp1 = val } func _sqlite3VdbeChangeP2(tls *libc.TLS, p uintptr, addr int32, val int32) { (*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, p, addr))).Fp2 = val } func _sqlite3VdbeChangeP3(tls *libc.TLS, p uintptr, addr int32, val int32) { (*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, p, addr))).Fp3 = val } func _sqlite3VdbeChangeP5(tls *libc.TLS, p uintptr, p5 Tu16) { if (*TVdbe)(unsafe.Pointer(p)).FnOp > 0 { (*(*TOp)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr((*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1))*24))).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) { (*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) { 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) { _freeEphemeralFunction(tls, db, (*Tsqlite3_context)(unsafe.Pointer(p)).FpFunc) _sqlite3DbNNFreeNN(tls, db, p) } func _freeP4(tls *libc.TLS, db uintptr, p4type int32, p4 uintptr) { 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 if aOp != 0 { pOp = aOp + uintptr(nOp-int32(1))*24 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 -= 24 } _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 } pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(addr)*24 _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))*24))).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 { (*TOp)(unsafe.Pointer(pOp)).Fp4type = 0 *(*uintptr)(unsafe.Pointer(pOp + 16)) = uintptr(0) } if n < 0 { _sqlite3VdbeChangeP4(tls, p, int32((int64(pOp)-int64((*TVdbe)(unsafe.Pointer(p)).FaOp))/24), 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 db = (*TVdbe)(unsafe.Pointer(p)).Fdb if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { if n != -int32(11) { _freeP4(tls, db, n, *(*uintptr)(unsafe.Pointer(bp))) } return } if addr < 0 { addr = (*TVdbe)(unsafe.Pointer(p)).FnOp - int32(1) } pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(addr)*24 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 *). */ (*TOp)(unsafe.Pointer(pOp)).Fp4.Fi = int32(int64(*(*uintptr)(unsafe.Pointer(bp)))) (*TOp)(unsafe.Pointer(pOp)).Fp4type = int8(-libc.Int32FromInt32(3)) } else { if *(*uintptr)(unsafe.Pointer(bp)) != uintptr(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 if (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FmallocFailed != 0 { _freeP4(tls, (*TVdbe)(unsafe.Pointer(p)).Fdb, n, pP4) } else { pOp = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr((*TVdbe)(unsafe.Pointer(p)).FnOp-int32(1))*24 (*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 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) { 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)*24 } 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)) Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5493, libc.VaList(bp+40, int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField))) j = 0 for { if !(j < int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField)) { break } pColl = *(*uintptr)(unsafe.Pointer(pKeyInfo + 32 + uintptr(j)*8)) if pColl != 0 { v2 = (*TCollSeq)(unsafe.Pointer(pColl)).FzName } else { v2 = __ccgo_ts + 1664 } zColl = v2 if libc.Xstrcmp(tls, zColl, __ccgo_ts+5498) == 0 { zColl = __ccgo_ts + 5505 } if int32(*(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags + uintptr(j))))&int32(KEYINFO_ORDER_DESC) != 0 { v3 = __ccgo_ts + 5486 } else { v3 = __ccgo_ts + 1664 } if int32(*(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags + uintptr(j))))&int32(KEYINFO_ORDER_BIGNULL) != 0 { v4 = __ccgo_ts + 5507 } else { v4 = __ccgo_ts + 1664 } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5510, libc.VaList(bp+40, v3, v4, zColl)) goto _1 _1: ; j++ } Xsqlite3_str_append(tls, bp, __ccgo_ts+5518, int32(1)) case -int32(2): pColl1 = *(*uintptr)(unsafe.Pointer(pOp + 16)) Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5534, libc.VaList(bp+40, (*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+5543, libc.VaList(bp+40, (*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+5543, libc.VaList(bp+40, (*TFuncDef)(unsafe.Pointer(pDef1)).FzName, int32((*TFuncDef)(unsafe.Pointer(pDef1)).FnArg))) case -int32(13): Xsqlite3_str_appendf(tls, bp, __ccgo_ts+1425, libc.VaList(bp+40, *(*Ti64)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))))) case -int32(3): Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5550, libc.VaList(bp+40, (*TOp)(unsafe.Pointer(pOp)).Fp4.Fi)) case -int32(12): Xsqlite3_str_appendf(tls, bp, __ccgo_ts+1392, libc.VaList(bp+40, *(*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+1425, libc.VaList(bp+40, *(*Ti64)(unsafe.Pointer(pMem)))) } else { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Real) != 0 { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+1392, libc.VaList(bp+40, *(*float64)(unsafe.Pointer(pMem)))) } else { if int32((*TMem)(unsafe.Pointer(pMem)).Fflags)&int32(MEM_Null) != 0 { zP4 = __ccgo_ts + 1665 } else { zP4 = __ccgo_ts + 5553 } } } } case -int32(11): pVtab = (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FpVtab Xsqlite3_str_appendf(tls, bp, __ccgo_ts+5560, libc.VaList(bp+40, 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+5568, libc.VaList(bp+40, v6, *(*Tu32)(unsafe.Pointer(ai + uintptr(i)*4)))) goto _5 _5: ; i++ } Xsqlite3_str_append(tls, bp, __ccgo_ts+5573, int32(1)) case -int32(4): zP4 = __ccgo_ts + 5575 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 + 5520, 1: __ccgo_ts + 5522, 2: __ccgo_ts + 5524, 3: __ccgo_ts + 5529, } // 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) { *(*TyDbMask)(unsafe.Pointer(p + 204)) |= libc.Uint32FromInt32(1) << i if i != int32(1) && _sqlite3BtreeSharable(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FaDb + uintptr(i)*32))).FpBt) != 0 { *(*TyDbMask)(unsafe.Pointer(p + 208)) |= 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 += 56 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)*56 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 += 56 v1 = p if !(v1 < pEnd) { break } } return } for { /* 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 += 56 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 (*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(uint64((*TMem)(unsafe.Pointer(pSub)).Fn) / uint64(8)) apSub = (*TMem)(unsafe.Pointer(pSub)).Fz } i = 0 for { if !(i < nSub) { break } nRow += (*TSubProgram)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(apSub + uintptr(i)*8)))).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 j = 0 for { if !(i >= (*TSubProgram)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(apSub + uintptr(j)*8)))).FnOp) { break } i -= (*TSubProgram)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(apSub + uintptr(j)*8)))).FnOp goto _3 _3: ; j++ } aOp = (*TSubProgram)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(apSub + uintptr(j)*8)))).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)*24))).Fp4type) == -int32(4) { nByte = int32(uint64(nSub+libc.Int32FromInt32(1)) * uint64(8)) j1 = 0 for { if !(j1 < nSub) { break } if *(*uintptr)(unsafe.Pointer(apSub + uintptr(j1)*8)) == *(*uintptr)(unsafe.Pointer(aOp + uintptr(i)*24 + 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)*8)) = *(*uintptr)(unsafe.Pointer(aOp + uintptr(i)*24 + 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(uint64(nSub) * uint64(8)) nRow += (*TSubProgram)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aOp + uintptr(i)*24 + 16)))).FnOp } } if eMode == 0 { break } if int32((*(*TOp)(unsafe.Pointer(aOp + uintptr(i)*24))).Fopcode) == int32(OP_Explain) { break } if int32((*(*TOp)(unsafe.Pointer(aOp + uintptr(i)*24))).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.Uint64FromInt64(112)+libc.Uint64FromInt32(7))&uint64(^libc.Int32FromInt32(7))) apCsr = aMem + uintptr((*TVdbeFrame)(unsafe.Pointer(p)).FnChildMem)*56 i = 0 for { if !(i < (*TVdbeFrame)(unsafe.Pointer(p)).FnChildCsr) { break } if *(*uintptr)(unsafe.Pointer(apCsr + uintptr(i)*8)) != 0 { _sqlite3VdbeFreeCursorNN(tls, (*TVdbeFrame)(unsafe.Pointer(p)).Fv, *(*uintptr)(unsafe.Pointer(apCsr + uintptr(i)*8))) } goto _1 _1: ; i++ } _releaseMemArray(tls, aMem, (*TVdbeFrame)(unsafe.Pointer(p)).FnChildMem) _sqlite3VdbeDeleteAuxData(tls, (*TVdbe)(unsafe.Pointer((*TVdbeFrame)(unsafe.Pointer(p)).Fv)).Fdb, p+64, -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+8 */ 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*56 /* First Mem of result set */ bListSubprogs = libc.BoolInt32(int32(Tbft(*(*uint16)(unsafe.Pointer(p + 200))&0xc>>2)) == int32(1) || (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_TriggerEQP) != uint64(0)) /* Current opcode */ /* 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. */ pSub = (*TVdbe)(unsafe.Pointer(p)).FaMem + 9*56 } 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 + 200))&0xc>>2)) == int32(2)), p+48, bp, bp+8) if rc == SQLITE_OK { pOp = *(*uintptr)(unsafe.Pointer(bp + 8)) + uintptr(*(*int32)(unsafe.Pointer(bp)))*24 if libc.AtomicLoadPInt32(db+432) != 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 + 200))&0xc>>2)) == int32(2) { _sqlite3VdbeMemSetInt64(tls, pMem, int64((*TOp)(unsafe.Pointer(pOp)).Fp1)) _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(1)*56, int64((*TOp)(unsafe.Pointer(pOp)).Fp2)) _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(2)*56, int64((*TOp)(unsafe.Pointer(pOp)).Fp3)) _sqlite3VdbeMemSetStr(tls, pMem+uintptr(3)*56, zP4, int64(-int32(1)), uint8(SQLITE_UTF8), __ccgo_fp(Xsqlite3_free)) } else { _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(0)*56, int64(*(*int32)(unsafe.Pointer(bp)))) _sqlite3VdbeMemSetStr(tls, pMem+uintptr(1)*56, _sqlite3OpcodeName(tls, int32((*TOp)(unsafe.Pointer(pOp)).Fopcode)), int64(-int32(1)), uint8(SQLITE_UTF8), libc.UintptrFromInt32(0)) _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(2)*56, int64((*TOp)(unsafe.Pointer(pOp)).Fp1)) _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(3)*56, int64((*TOp)(unsafe.Pointer(pOp)).Fp2)) _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(4)*56, int64((*TOp)(unsafe.Pointer(pOp)).Fp3)) /* pMem+5 for p4 is done last */ _sqlite3VdbeMemSetInt64(tls, pMem+uintptr(6)*56, int64((*TOp)(unsafe.Pointer(pOp)).Fp5)) _sqlite3VdbeMemSetNull(tls, pMem+uintptr(7)*56) _sqlite3VdbeMemSetStr(tls, pMem+uintptr(5)*56, zP4, int64(-int32(1)), uint8(SQLITE_UTF8), __ccgo_fp(Xsqlite3_free)) } (*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 { FpSpace uintptr 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) { if pBuf == uintptr(0) { nByte = nByte 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 } } return pBuf } // C documentation // // /* // ** Rewind the VDBE back to the beginning in preparation for // ** running it. // */ func _sqlite3VdbeRewind(tls *libc.TLS, p uintptr) { /* There should be at least one opcode. */ (*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 */ (*TVdbe)(unsafe.Pointer(p)).FpVList = (*TParse)(unsafe.Pointer(pParse)).FpVList (*TParse)(unsafe.Pointer(pParse)).FpVList = uintptr(0) db = (*TVdbe)(unsafe.Pointer(p)).Fdb 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.Uint64FromInt64(24) * uint64((*TVdbe)(unsafe.Pointer(p)).FnOp)) /* Bytes of opcode memory used */ (*(*TReusableSpace)(unsafe.Pointer(bp + 8))).FpSpace = (*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(n) /* Unused opcode memory */ (*(*TReusableSpace)(unsafe.Pointer(bp + 8))).FnFree = int64(((*TParse)(unsafe.Pointer(pParse)).FszOpAlloc - n) & ^libc.Int32FromInt32(7)) /* Bytes of unused memory */ _resolveP2Values(tls, p, bp) libc.SetBitFieldPtr16Uint32(p+200, 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+200, uint32((*TParse)(unsafe.Pointer(pParse)).Fexplain), 2, 0xc) (*TVdbe)(unsafe.Pointer(p)).FnResColumn = uint16(int32(12) - int32(4)*int32(Tbft(*(*uint16)(unsafe.Pointer(p + 200))&0xc>>2))) } libc.SetBitFieldPtr16Uint32(p+200, 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(uint64(nMem)*uint64(56))) (*TVdbe)(unsafe.Pointer(p)).FaVar = _allocSpace(tls, bp+8, uintptr(0), int64(uint64(nVar)*uint64(56))) (*TVdbe)(unsafe.Pointer(p)).FapArg = _allocSpace(tls, bp+8, uintptr(0), int64(uint64(*(*int32)(unsafe.Pointer(bp)))*uint64(8))) (*TVdbe)(unsafe.Pointer(p)).FapCsr = _allocSpace(tls, bp+8, uintptr(0), int64(uint64(nCursor)*uint64(8))) 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(uint64(nMem)*uint64(56))) (*TVdbe)(unsafe.Pointer(p)).FaVar = _allocSpace(tls, bp+8, (*TVdbe)(unsafe.Pointer(p)).FaVar, int64(uint64(nVar)*uint64(56))) (*TVdbe)(unsafe.Pointer(p)).FapArg = _allocSpace(tls, bp+8, (*TVdbe)(unsafe.Pointer(p)).FapArg, int64(uint64(*(*int32)(unsafe.Pointer(bp)))*uint64(8))) (*TVdbe)(unsafe.Pointer(p)).FapCsr = _allocSpace(tls, bp+8, (*TVdbe)(unsafe.Pointer(p)).FapCsr, int64(uint64(nCursor)*uint64(8))) } } 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.X__builtin___memset_chk(tls, (*TVdbe)(unsafe.Pointer(p)).FapCsr, 0, uint64(nCursor)*uint64(8), libc.X__builtin_object_size(tls, (*TVdbe)(unsafe.Pointer(p)).FapCsr, 0)) } _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.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: _sqlite3BtreeCloseCursor(tls, *(*uintptr)(unsafe.Pointer(pCx + 48))) case int32(CURTYPE_VTAB): pVCur = *(*uintptr)(unsafe.Pointer(pCx + 48)) pModule = (*Tsqlite3_vtab)(unsafe.Pointer((*Tsqlite3_vtab_cursor)(unsafe.Pointer(pVCur)).FpVtab)).FpModule (*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)*8)) if pC != 0 { _sqlite3VdbeFreeCursorNN(tls, p, pC) *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(i)*8)) = 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+296, -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 } _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+296, -int32(1), 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(56)*uint64(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 if (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).FmallocFailed != 0 { return int32(SQLITE_NOMEM) } pColName = (*TVdbe)(unsafe.Pointer(p)).FaColName + uintptr(idx+var1*int32((*TVdbe)(unsafe.Pointer(p)).FnResAlloc))*56 rc = _sqlite3VdbeMemSetStr(tls, pColName, zName, int64(-int32(1)), uint8(SQLITE_UTF8), xDel) 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+12 */ Tu32 var _ /* pSuperJrnl at bp+0 */ uintptr var _ /* res at bp+8 */ 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)*32))).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)*32))).Fsafety_level) != int32(PAGER_SYNCHRONOUS_OFF) && _aMJNeeded[_sqlite3PagerGetJournalMode(tls, pPager)] != 0 && _sqlite3PagerIsMemdb(tls, pPager) == 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+5595, libc.VaList(bp+24, zSuper)) _sqlite3OsDelete(tls, pVfs, zSuper, 0) break } else { if retryCount == int32(1) { Xsqlite3_log(tls, int32(SQLITE_FULL), __ccgo_ts+5609, libc.VaList(bp+24, zSuper)) } } } retryCount++ Xsqlite3_randomness(tls, int32(4), bp+12) Xsqlite3_snprintf(tls, int32(13), zSuper+uintptr(nMainFile), __ccgo_ts+5624, libc.VaList(bp+24, *(*Tu32)(unsafe.Pointer(bp + 12))>>libc.Int32FromInt32(8)&uint32(0xffffff), *(*Tu32)(unsafe.Pointer(bp + 12))&uint32(0xff))) /* The antipenultimate character of the super-journal name must ** be "9" to avoid name collisions when using 8+3 filenames. */ rc = _sqlite3OsAccess(tls, pVfs, zSuper, SQLITE_ACCESS_EXISTS, bp+8) } 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)*32))).FpBt if _sqlite3BtreeTxnState(tls, pBt3) == int32(SQLITE_TXN_WRITE) { zFile = _sqlite3BtreeGetJournalname(tls, pBt3) if zFile == uintptr(0) { goto _4 /* Ignore TEMP and :memory: databases */ } 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)*32))).FpBt if pBt4 != 0 { rc = _sqlite3BtreeCommitPhaseOne(tls, pBt4, zSuper) } goto _7 _7: ; i++ } _sqlite3OsCloseFree(tls, *(*uintptr)(unsafe.Pointer(bp))) 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)*32))).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) 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)*32))).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 + 200))&0x40>>6)) != 0) || mrc != int32(SQLITE_INTERRUPT) { if (mrc == int32(SQLITE_NOMEM) || mrc == int32(SQLITE_FULL)) && int32(Tbft(*(*uint16)(unsafe.Pointer(p + 200))&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 + 200))&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 + 200))&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 + 48)) &= ^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 + 200))&0x40>>6)) != 0) { (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite-- } if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 200))&0x80>>7)) != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeRead-- } (*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) } 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 if int32((*TVdbe)(unsafe.Pointer(p)).FeVdbeState) >= int32(VDBE_READY_STATE) { rc = _sqlite3VdbeReset(tls, p) } _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 + 24 } } } // 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 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 db = (*TVdbe)(unsafe.Pointer(p)).Fdb _sqlite3VdbeClearObject(tls, db, p) if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(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 rc = _sqlite3BtreeTableMoveto(tls, *(*uintptr)(unsafe.Pointer(p + 48)), (*TVdbeCursor)(unsafe.Pointer(p)).FmovetoTarget, 0, bp) if rc != 0 { return rc } if *(*int32)(unsafe.Pointer(bp)) != 0 { return _sqlite3CorruptError(tls, int32(88615)) } (*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 rc = _sqlite3BtreeCursorRestore(tls, *(*uintptr)(unsafe.Pointer(p + 48)), 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) { if _sqlite3BtreeCursorHasMoved(tls, *(*uintptr)(unsafe.Pointer(p + 48))) != 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 { return uint32(_sqlite3SmallTypeSizes[serial_type]) } return r } func _sqlite3VdbeOneByteSerialTypeLen(tls *libc.TLS, serial_type Tu8) (r Tu8) { 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 += 56 u++ v3 = u if int32(v3) >= int32((*TUnpackedRecord)(unsafe.Pointer(p)).FnField) { break } } if d > uint32(nKey) && u != 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)*56) } (*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(112) defer tls.Free(112) var rc int32 var v1, v2 uintptr var _ /* c1 at bp+0 */ TMem var _ /* c2 at bp+56 */ 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+56, (*TMem)(unsafe.Pointer(pMem1)).Fdb, uint16(MEM_Null)) _sqlite3VdbeMemShallowCopy(tls, bp, pMem1, int32(MEM_Ephem)) _sqlite3VdbeMemShallowCopy(tls, bp+56, pMem2, int32(MEM_Ephem)) v1 = _sqlite3ValueText(tls, bp, (*TCollSeq)(unsafe.Pointer(pColl)).Fenc) v2 = _sqlite3ValueText(tls, bp+56, (*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 + 56))).Fn, v2) } _sqlite3VdbeMemReleaseMalloc(tls, bp) _sqlite3VdbeMemReleaseMalloc(tls, bp+56) 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 *(*int8)(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(). */ 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, uint64(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 x float64 var y Ti64 var v1, v2 int32 _, _, _, _ = 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) } if float64(i) < r { v2 = -int32(1) } else { v2 = libc.BoolInt32(float64(i) > 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 /* 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) } /* 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). */ 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 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(80) defer tls.Free(80) 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+64 */ Tu32 var _ /* serial_type at bp+68 */ 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 + 64)) = uint32(*(*uint8)(unsafe.Pointer(aKey1 + 1))) if *(*Tu32)(unsafe.Pointer(bp + 64)) < uint32(0x80) { idx1 = uint32(2) } else { idx1 = uint32(int32(1) + int32(_sqlite3GetVarint32(tls, aKey1+1, bp+64))) } *(*Tu32)(unsafe.Pointer(bp)) = uint32(*(*uint8)(unsafe.Pointer(aKey1))) d1 = *(*Tu32)(unsafe.Pointer(bp)) + _sqlite3VdbeSerialTypeLen(tls, *(*Tu32)(unsafe.Pointer(bp + 64))) i = int32(1) pRhs += 56 } 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(89580))) return 0 /* Corruption */ } /* Only needed by assert() statements */ 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 + 68)) = uint32(*(*uint8)(unsafe.Pointer(aKey1 + uintptr(idx1)))) if *(*Tu32)(unsafe.Pointer(bp + 68)) >= uint32(10) { if *(*Tu32)(unsafe.Pointer(bp + 68)) == uint32(10) { v2 = -int32(1) } else { v2 = +libc.Int32FromInt32(1) } rc = v2 } else { if *(*Tu32)(unsafe.Pointer(bp + 68)) == uint32(0) { rc = -int32(1) } else { if *(*Tu32)(unsafe.Pointer(bp + 68)) == 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 + 68)), 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 + 68)) = uint32(*(*uint8)(unsafe.Pointer(aKey1 + uintptr(idx1)))) if *(*Tu32)(unsafe.Pointer(bp + 68)) >= 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 + 68)) == uint32(10) { v3 = -int32(1) } else { v3 = +libc.Int32FromInt32(1) } rc = v3 } else { if *(*Tu32)(unsafe.Pointer(bp + 68)) == uint32(0) { rc = -int32(1) } else { if *(*Tu32)(unsafe.Pointer(bp + 68)) == 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 { } } } } else { _sqlite3VdbeSerialGet(tls, aKey1+uintptr(d1), *(*Tu32)(unsafe.Pointer(bp + 68)), 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 + 68)) = uint32(*(*uint8)(unsafe.Pointer(aKey1 + uintptr(idx1)))) if *(*Tu32)(unsafe.Pointer(bp + 68)) >= uint32(0x80) { _sqlite3GetVarint32(tls, aKey1+uintptr(idx1), bp+68) } if *(*Tu32)(unsafe.Pointer(bp + 68)) < uint32(12) { rc = -int32(1) } else { if !(*(*Tu32)(unsafe.Pointer(bp + 68))&libc.Uint32FromInt32(0x01) != 0) { rc = +libc.Int32FromInt32(1) } else { (*(*TMem)(unsafe.Pointer(bp + 8))).Fn = int32((*(*Tu32)(unsafe.Pointer(bp + 68)) - 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(89661))) return 0 /* Corruption */ } else { if *(*uintptr)(unsafe.Pointer(pKeyInfo + 32 + uintptr(i)*8)) != 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 + 32 + uintptr(i)*8)), pPKey2+31) } 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, uint64(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 { *(*Tu32)(unsafe.Pointer(bp + 68)) = uint32(*(*uint8)(unsafe.Pointer(aKey1 + uintptr(idx1)))) if *(*Tu32)(unsafe.Pointer(bp + 68)) >= uint32(0x80) { _sqlite3GetVarint32(tls, aKey1+uintptr(idx1), bp+68) } if *(*Tu32)(unsafe.Pointer(bp + 68)) < uint32(12) || *(*Tu32)(unsafe.Pointer(bp + 68))&uint32(0x01) != 0 { rc = -int32(1) } else { nStr = int32((*(*Tu32)(unsafe.Pointer(bp + 68)) - uint32(12)) / uint32(2)) if d1+uint32(nStr) > uint32(nKey1) { (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FerrCode = uint8(_sqlite3CorruptError(tls, int32(89691))) 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, uint64(nCmp1)) if rc == 0 { rc = nStr - (*TMem)(unsafe.Pointer(pRhs)).Fn } } } } } else { *(*Tu32)(unsafe.Pointer(bp + 68)) = uint32(*(*uint8)(unsafe.Pointer(aKey1 + uintptr(idx1)))) if *(*Tu32)(unsafe.Pointer(bp + 68)) == uint32(0) || *(*Tu32)(unsafe.Pointer(bp + 68)) == uint32(10) || *(*Tu32)(unsafe.Pointer(bp + 68)) == uint32(7) && _serialGet7(tls, aKey1+uintptr(d1), bp+8) != 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 + 68)) == uint32(0) || int32((*TMem)(unsafe.Pointer(pRhs)).Fflags)&int32(MEM_Null) != 0) { rc = -rc } } /* See comment below */ return rc } i++ if i == int32((*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FnField) { break } pRhs += 56 d1 += _sqlite3VdbeSerialTypeLen(tls, *(*Tu32)(unsafe.Pointer(bp + 68))) if d1 > uint32(nKey1) { break } idx1 += uint32(_sqlite3VarintLen(tls, uint64(*(*Tu32)(unsafe.Pointer(bp + 68))))) if idx1 >= *(*Tu32)(unsafe.Pointer(bp)) { (*TUnpackedRecord)(unsafe.Pointer(pPKey2)).FerrCode = uint8(_sqlite3CorruptError(tls, int32(89742))) 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). */ /* 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. */ (*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))) 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) } } } 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 *(*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 } } 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(89905))) 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), *(*uintptr)(unsafe.Pointer(pPKey2 + 16)), uint64(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) } } } } } } 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 + 16)) = *(*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 + 32)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(p + 16)) = (*(*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(128) defer tls.Free(128) 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+64 */ 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 */ nCellKey = int64(_sqlite3BtreePayloadSize(tls, pCur)) /* 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) } 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+64) *(*Ti64)(unsafe.Pointer(rowid)) = *(*Ti64)(unsafe.Pointer(bp + 64)) _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(90063)) } // 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(64) defer tls.Free(64) var nCellKey Ti64 var pCur uintptr var rc int32 var _ /* m at bp+0 */ TMem _, _, _ = nCellKey, pCur, rc nCellKey = 0 pCur = *(*uintptr)(unsafe.Pointer(pC + 48)) 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(90096)) } _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) { (*Tsqlite3)(unsafe.Pointer(db)).FnChange = nChange *(*Ti64)(unsafe.Pointer(db + 128)) += 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+200, 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+200, 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 if v != 0 { pMem = (*TVdbe)(unsafe.Pointer(v)).FaVar + uintptr(iVar-int32(1))*56 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) { if iVar >= int32(32) { *(*Tu32)(unsafe.Pointer(v + 284)) |= uint32(0x80000000) } else { *(*Tu32)(unsafe.Pointer(v + 284)) |= 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)*24 if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_PureFunc) { if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fp5)&int32(NC_IsCheck) != 0 { zContext = __ccgo_ts + 5667 } else { if int32((*TVdbeOp)(unsafe.Pointer(pOp)).Fp5)&int32(NC_GenCol) != 0 { zContext = __ccgo_ts + 5686 } else { zContext = __ccgo_ts + 5705 } } zMsg = Xsqlite3_mprintf(tls, __ccgo_ts+5714, 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 if p != 0 { i = 0 for { if !(i < nField) { break } pMem = (*TUnpackedRecord)(unsafe.Pointer(p)).FaMem + uintptr(i)*56 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(144) defer tls.Free(144) 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.X__builtin___memset_chk(tls, bp, 0, uint64(136), libc.X__builtin_object_size(tls, bp, 0)) 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)*56)) } else { iKey2 = iKey1 } } (*(*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)*56) 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 + 200))&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+5750, 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+5795, 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 _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(90484)) } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if (*TVdbe)(unsafe.Pointer(v)).FstartTime > 0 { _invokeProfileCallback(tls, db, v) } 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) 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)*56) (*(*TMem)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaVar + uintptr(i)*56))).Fflags = uint16(MEM_Null) goto _1 _1: ; i++ } if (*TVdbe)(unsafe.Pointer(p)).Fexpmask != 0 { libc.SetBitFieldPtr16Uint32(p+200, 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 { return uintptr(0) } p2 = p + 20 *(*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(p)), 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(56)) if pNew == uintptr(0) { return uintptr(0) } libc.X__builtin___memset_chk(tls, pNew, 0, uint64(56), libc.X__builtin_object_size(tls, pNew, 0)) libc.X__builtin___memcpy_chk(tls, pNew, pOrig, uint64(libc.UintptrFromInt32(0)+24), libc.X__builtin_object_size(tls, pNew, 0)) p1 = pNew + 20 *(*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 + 20 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) & ^(libc.Int32FromInt32(MEM_Static) | libc.Int32FromInt32(MEM_Dyn))) p3 = pNew + 20 *(*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 + 20 *(*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 */ 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) { 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) } } 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) { _setResultStrOrError(tls, pCtx, z, n, uint8(0), xDel) } func Xsqlite3_result_blob64(tls *libc.TLS, pCtx uintptr, z uintptr, n Tsqlite3_uint64, xDel uintptr) { 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) { _sqlite3VdbeMemSetDouble(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, rVal) } func Xsqlite3_result_error(tls *libc.TLS, pCtx uintptr, z uintptr, n int32) { (*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) { (*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) { _sqlite3VdbeMemSetInt64(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, int64(iVal)) } func Xsqlite3_result_int64(tls *libc.TLS, pCtx uintptr, iVal Ti64) { _sqlite3VdbeMemSetInt64(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, iVal) } func Xsqlite3_result_null(tls *libc.TLS, pCtx uintptr) { _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 _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 (*TMem)(unsafe.Pointer(pOut)).FeSubtype = uint8(eSubtype & uint32(0xff)) p1 = pOut + 20 *(*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) { _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) { 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) { _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) { _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) { _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 _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 if n > uint64(*(*int32)(unsafe.Pointer((*TMem)(unsafe.Pointer(pOut)).Fdb + 136))) { 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) { (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FisError = int32(SQLITE_TOOBIG) _sqlite3VdbeMemSetStr(tls, (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut, __ccgo_ts+5835, 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) { _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 if int32((*TMem)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut)).Fflags)&int32(MEM_Int) != 0 { p1 = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut + 20 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(MEM_Int)) p2 = (*Tsqlite3_context)(unsafe.Pointer(pCtx)).FpOut + 20 *(*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)*32))).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)*32))).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 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 + 200))&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.AtomicStorePInt32(db+432, 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+184) } else { } (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive++ if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 200))&0x40>>6)) == 0 { (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeWrite++ } if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 200))&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) goto restart_step } } } if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 200))&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) { (*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 { (*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 */ 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(91278)) } 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) } } 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) { 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) { 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) { 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(80) defer tls.Free(80) var iOff, rc, v1 int32 var pOut, pRhs, zBuf uintptr var sz Tu32 var _ /* dummy at bp+0 */ int32 var _ /* iSerial at bp+64 */ 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(91399)) } 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 { 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) if _sqlite3BtreeEof(tls, (*TValueList)(unsafe.Pointer(pRhs)).FpCsr) != 0 { rc = int32(SQLITE_DONE) } } if rc == SQLITE_OK { /* Raw content of current row */ libc.X__builtin___memset_chk(tls, bp+8, 0, uint64(56), libc.X__builtin_object_size(tls, bp+8, 0)) 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 + 64)) = uint32(*(*Tu8)(unsafe.Pointer(zBuf + 1))) v1 = libc.Int32FromInt32(1) } else { v1 = int32(_sqlite3GetVarint32(tls, zBuf+1, bp+64)) } iOff = int32(1) + int32(uint8(v1)) _sqlite3VdbeSerialGet(tls, zBuf+uintptr(iOff), *(*Tu32)(unsafe.Pointer(bp + 64)), 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 + 72 } 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 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(pMem)) = (*Tsqlite3_context)(unsafe.Pointer(p)).FpFunc if (*TMem)(unsafe.Pointer(pMem)).Fz != 0 { libc.X__builtin___memset_chk(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, 0, uint64(nByte), libc.X__builtin_object_size(tls, (*TMem)(unsafe.Pointer(pMem)).Fz, 0)) } } 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) { 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 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 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(32)) 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) { 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) } 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)*56 } 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 { (*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 + 20 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) & ^libc.Int32FromInt32(MEM_Static)) p2 = pOut + 20 *(*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 + 5858, 1: __ccgo_ts + 5863, 2: __ccgo_ts + 5870, 3: __ccgo_ts + 5873, 4: __ccgo_ts + 5876, 5: __ccgo_ts + 5879, 6: __ccgo_ts + 5882, 7: __ccgo_ts + 5885, 8: __ccgo_ts + 5893, 9: __ccgo_ts + 5896, 10: __ccgo_ts + 5903, 11: __ccgo_ts + 5911, } 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 Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 200))&0xc>>2)) != 0 { if useType > 0 { goto columnName_end } if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 200))&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 + 200))&0xc>>2))-int32(8)]) ret = uintptr(unsafe.Pointer(&_azExplainColNames16data)) + uintptr(i)*2 } else { ret = _azExplainColNames8[N+int32(8)*int32(Tbft(*(*uint16)(unsafe.Pointer(p + 200))&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)*56) } else { ret = Xsqlite3_value_text(tls, (*TVdbe)(unsafe.Pointer(p)).FaColName+uintptr(N)*56) } /* A malloc may have failed inside of the _text() call. If this ** is the case, clear the mallocFailed flag and return NULL. */ 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(92003)) } 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(92007))) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).Fdb)).Fmutex) Xsqlite3_log(tls, int32(SQLITE_MISUSE), __ccgo_ts+5918, libc.VaList(bp+8, (*TVdbe)(unsafe.Pointer(p)).FzSql)) return _sqlite3MisuseError(tls, int32(92011)) } 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)*56 _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. */ 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+200, 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))*56 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) { 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))*56, 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))*56, 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))*56, 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) { 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): 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))*56, 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 + 136))) { rc = int32(SQLITE_TOOBIG) } else { 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 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)*56, (*TVdbe)(unsafe.Pointer(pFrom)).FaVar+uintptr(i)*56) 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) } if (*TVdbe)(unsafe.Pointer(pTo)).Fexpmask != 0 { libc.SetBitFieldPtr16Uint32(pTo+200, libc.Uint32FromInt32(1), 0, 0x3) } if (*TVdbe)(unsafe.Pointer(pFrom)).Fexpmask != 0 { libc.SetBitFieldPtr16Uint32(pFrom+200, 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 + 200)) & 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 + 200)) & 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 + 200))&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 + 200))&0x100>>8)) != 0) { /* No reprepare necessary */ libc.SetBitFieldPtr16Uint32(v+200, uint32(eMode), 2, 0xc) rc = SQLITE_OK } else { libc.SetBitFieldPtr16Uint32(v+200, uint32(eMode), 2, 0xc) rc = _sqlite3Reprepare(tls, v) libc.SetBitFieldPtr16Uint32(v+200, libc.BoolUint32(eMode == libc.Int32FromInt32(2)), 8, 0x100) } } } } } if int32(Tbft(*(*uint16)(unsafe.Pointer(v + 200))&0xc>>2)) != 0 { (*TVdbe)(unsafe.Pointer(v)).FnResColumn = uint16(int32(12) - int32(4)*int32(Tbft(*(*uint16)(unsafe.Pointer(v + 200))&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 (*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 + 212 + uintptr(op)*4)) if resetFlag != 0 { *(*Tu32)(unsafe.Pointer(pVdbe + 212 + 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.X__builtin___memset_chk(tls, (*TUnpackedRecord)(unsafe.Pointer(pRet)).FaMem, 0, uint64(56)*uint64(int32((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FnKeyField)+libc.Int32FromInt32(1)), libc.X__builtin_object_size(tls, (*TUnpackedRecord)(unsafe.Pointer(pRet)).FaMem, 0)) _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(92547)) 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) { nRec = _sqlite3BtreePayloadSize(tls, *(*uintptr)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr + 48))) aRec = _sqlite3DbMallocRaw(tls, db, uint64(nRec)) if !(aRec != 0) { goto preupdate_old_out } rc = _sqlite3BtreePayload(tls, *(*uintptr)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr + 48)), uint32(0), nRec, aRec) if rc == SQLITE_OK { (*TPreUpdate)(unsafe.Pointer(p)).FpUnpacked = _vdbeUnpackRecord(tls, p+32, 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)*56 *(*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)*16))).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(92670)) 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)*56 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+32, (*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)*56 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. */ if !((*TPreUpdate)(unsafe.Pointer(p)).FaNew != 0) { (*TPreUpdate)(unsafe.Pointer(p)).FaNew = _sqlite3DbMallocZero(tls, db, uint64(56)*uint64((*TVdbeCursor)(unsafe.Pointer((*TPreUpdate)(unsafe.Pointer(p)).FpCsr)).FnField)) if !((*TPreUpdate)(unsafe.Pointer(p)).FaNew != 0) { rc = int32(SQLITE_NOMEM) goto preupdate_new_out } } pMem = (*TPreUpdate)(unsafe.Pointer(p)).FaNew + uintptr(iIdx)*56 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)*56) 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 *(*int8)(unsafe.Pointer(zSql)) != 0 { n = _sqlite3GetToken(tls, zSql, bp) 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(128) defer tls.Free(128) 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+40 */ 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 + 136))) if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeExec > int32(1) { for *(*int8)(unsafe.Pointer(zRawSql)) != 0 { zStart = zRawSql for { v1 = zRawSql zRawSql++ if !(int32(*(*int8)(unsafe.Pointer(v1))) != int32('\n') && *(*int8)(unsafe.Pointer(zRawSql)) != 0) { break } } Xsqlite3_str_append(tls, bp+8, __ccgo_ts+5958, int32(3)) Xsqlite3_str_append(tls, bp+8, zStart, int32(int64(zRawSql)-int64(zStart))) } } else { if int32((*TVdbe)(unsafe.Pointer(p)).FnVar) == 0 { Xsqlite3_str_append(tls, bp+8, zRawSql, _sqlite3Strlen30(tls, zRawSql)) } else { for *(*int8)(unsafe.Pointer(zRawSql)) != 0 { n = _findNextHostParameter(tls, zRawSql, bp+4) Xsqlite3_str_append(tls, bp+8, zRawSql, n) zRawSql += uintptr(n) if *(*int32)(unsafe.Pointer(bp + 4)) == 0 { break } if int32(*(*int8)(unsafe.Pointer(zRawSql))) == int32('?') { if *(*int32)(unsafe.Pointer(bp + 4)) > int32(1) { _sqlite3GetInt32(tls, zRawSql+1, bp) } else { *(*int32)(unsafe.Pointer(bp)) = nextIndex } } else { *(*int32)(unsafe.Pointer(bp)) = _sqlite3VdbeParameterIndex(tls, p, zRawSql, *(*int32)(unsafe.Pointer(bp + 4))) } 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 pVar = (*TVdbe)(unsafe.Pointer(p)).FaVar + uintptr(*(*int32)(unsafe.Pointer(bp))-int32(1))*56 if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&int32(MEM_Null) != 0 { Xsqlite3_str_append(tls, bp+8, __ccgo_ts+1665, 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+1425, libc.VaList(bp+104, *(*Ti64)(unsafe.Pointer(pVar)))) } else { if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&int32(MEM_Real) != 0 { Xsqlite3_str_appendf(tls, bp+8, __ccgo_ts+5479, libc.VaList(bp+104, *(*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.X__builtin___memset_chk(tls, bp+40, 0, uint64(56), libc.X__builtin_object_size(tls, bp+40, 0)) (*(*TMem)(unsafe.Pointer(bp + 40))).Fdb = db _sqlite3VdbeMemSetStr(tls, bp+40, (*TMem)(unsafe.Pointer(pVar)).Fz, int64((*TMem)(unsafe.Pointer(pVar)).Fn), enc, libc.UintptrFromInt32(0)) if int32(SQLITE_NOMEM) == _sqlite3VdbeChangeEncoding(tls, bp+40, int32(SQLITE_UTF8)) { (*(*TStrAccum)(unsafe.Pointer(bp + 8))).FaccError = uint8(SQLITE_NOMEM) (*(*TStrAccum)(unsafe.Pointer(bp + 8))).FnAlloc = uint32(0) } pVar = bp + 40 } nOut = (*TMem)(unsafe.Pointer(pVar)).Fn Xsqlite3_str_appendf(tls, bp+8, __ccgo_ts+5962, libc.VaList(bp+104, nOut, (*TMem)(unsafe.Pointer(pVar)).Fz)) if int32(enc) != int32(SQLITE_UTF8) { _sqlite3VdbeMemRelease(tls, bp+40) } } else { if int32((*TMem)(unsafe.Pointer(pVar)).Fflags)&int32(MEM_Zero) != 0 { Xsqlite3_str_appendf(tls, bp+8, __ccgo_ts+5969, libc.VaList(bp+104, *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pVar)).Fu)))) } else { /* Number of bytes of the blob to include in output */ Xsqlite3_str_append(tls, bp+8, __ccgo_ts+5982, int32(2)) nOut1 = (*TMem)(unsafe.Pointer(pVar)).Fn i = 0 for { if !(i < nOut1) { break } Xsqlite3_str_appendf(tls, bp+8, __ccgo_ts+5985, libc.VaList(bp+104, int32(*(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pVar)).Fz + uintptr(i))))&int32(0xff))) goto _3 _3: ; i++ } Xsqlite3_str_append(tls, bp+8, __ccgo_ts+5990, 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)*56 } 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.Uint64FromInt64(128) + libc.Uint64FromInt32(2)*libc.Uint64FromInt64(4)*uint64(nField) + uint64(v2)) if *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(iCur)*8)) != 0 { /*OPTIMIZATION-IF-FALSE*/ _sqlite3VdbeFreeCursorNN(tls, p, *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(iCur)*8))) *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr(iCur)*8)) = 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. */ 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)*8)) = v4 libc.X__builtin___memset_chk(tls, pCx, 0, uint64(libc.UintptrFromInt32(0)+40), libc.X__builtin_object_size(tls, pCx, 0)) (*TVdbeCursor)(unsafe.Pointer(pCx)).FeCurType = eCurType (*TVdbeCursor)(unsafe.Pointer(pCx)).FnField = int16(nField) (*TVdbeCursor)(unsafe.Pointer(pCx)).FaOffset = pCx + 120 + uintptr(nField)*4 if int32(eCurType) == CURTYPE_BTREE { *(*uintptr)(unsafe.Pointer(pCx + 48)) = (*TMem)(unsafe.Pointer(pMem)).Fz + uintptr(libc.Uint64FromInt64(128)+libc.Uint64FromInt32(2)*libc.Uint64FromInt64(4)*uint64(nField)) _sqlite3BtreeCursorZero(tls, *(*uintptr)(unsafe.Pointer(pCx + 48))) } 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 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 + 20 *(*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 + 20 *(*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 + 20 *(*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 int8, enc Tu8) { var p1 uintptr _ = p1 if int32(affinity) >= int32(SQLITE_AFF_NUMERIC) { 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 + 20 *(*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, int8(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 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) { 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))) } 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 pOut = (*TVdbe)(unsafe.Pointer(p)).FaMem + uintptr((*TVdbeOp)(unsafe.Pointer(pOp)).Fp2)*56 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) i = (*TOp)(unsafe.Pointer(pOp)).Fp3 mx = i + (*TOp)(unsafe.Pointer(pOp)).Fp4.Fi for { if !(i < mx) { break } p = aMem + uintptr(i)*56 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))) if len1 > *(*int32)(unsafe.Pointer(db + 136)) { 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 + 48))) { 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 + 48)), uint32(iOffset), uint32(len1), pBuf) if rc != 0 { return rc } *(*int8)(unsafe.Pointer(pBuf + uintptr(len1))) = 0 *(*int8)(unsafe.Pointer(pBuf + uintptr(len1+int32(1)))) = 0 *(*int8)(unsafe.Pointer(pBuf + uintptr(len1+int32(2)))) = 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 + 48))) } else { pBuf = (*TVdbeTxtBlbCache)(unsafe.Pointer(pCache)).FpCValue } _sqlite3RCStrRef(tls, pBuf) if t&int32(1) != 0 { rc = _sqlite3VdbeMemSetStr(tls, pDest, pBuf, int64(len1), uint8(encoding), __ccgo_fp(_sqlite3RCStrUnref)) p2 = pDest + 20 *(*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 + 48)), 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) { *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pDest)).Fz + uintptr(len1))) = 0 p3 = pDest + 20 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(MEM_Term)) } } p4 = pDest + 20 *(*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 + 1134, 1: __ccgo_ts + 1146, 2: __ccgo_ts + 1151, 3: __ccgo_ts + 1129, 4: __ccgo_ts + 1665, } // 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(976) defer tls.Free(976) 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 int8 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, savedAnalysisLimit, 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+712 */ [3]int32 var _ /* iA at bp+8 */ Ti64 var _ /* iB at bp+0 */ Ti64 var _ /* iMeta at bp+104 */ int32 var _ /* iMeta at bp+108 */ int32 var _ /* iMoved at bp+608 */ int32 var _ /* initData at bp+640 */ TInitData var _ /* m at bp+552 */ TMem var _ /* nChange at bp+616 */ Ti64 var _ /* nEntry at bp+96 */ Ti64 var _ /* nErr at bp+680 */ int32 var _ /* nullFunc at bp+856 */ TFuncDef var _ /* pVCur at bp+784 */ uintptr var _ /* pgno at bp+624 */ TPgno var _ /* r at bp+120 */ TUnpackedRecord var _ /* r at bp+168 */ TUnpackedRecord var _ /* r at bp+208 */ TUnpackedRecord var _ /* r at bp+464 */ TUnpackedRecord var _ /* r at bp+512 */ TUnpackedRecord var _ /* res at bp+112 */ int32 var _ /* res at bp+160 */ int32 var _ /* res at bp+248 */ int32 var _ /* res at bp+320 */ int32 var _ /* res at bp+376 */ int32 var _ /* res at bp+392 */ int32 var _ /* res at bp+396 */ int32 var _ /* res at bp+400 */ int32 var _ /* res at bp+456 */ int32 var _ /* rowid at bp+504 */ Ti64 var _ /* rowid at bp+928 */ Tsqlite_int64 var _ /* sContext at bp+800 */ Tsqlite3_context var _ /* sMem at bp+24 */ TMem var _ /* sMem at bp+728 */ TMem var _ /* t at bp+80 */ Tu32 var _ /* uA at bp+16 */ Tu64 var _ /* v at bp+312 */ Ti64 var _ /* v at bp+384 */ Ti64 var _ /* v at bp+88 */ Tu64 var _ /* val at bp+696 */ Ti64 var _ /* x at bp+256 */ TMem var _ /* x at bp+328 */ TBtreePayload var _ /* x at bp+408 */ TBtreePayload var _ /* x at bp+704 */ Ti64 var _ /* z at bp+688 */ uintptr var _ /* zErr at bp+632 */ uintptr var _ /* zErr at bp+792 */ 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, savedAnalysisLimit, 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 ***/ /* 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 + 212 + 4*4)) 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) { 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 + 136)) { goto too_big } (*TOp)(unsafe.Pointer(pOp)).Fopcode = uint8(OP_String) /* Fall through to the next case, OP_String */ /* 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 */ 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 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 += 56 _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: ; pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*56 (*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 */ 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 * * * ** Synopsis: r[P2]=parameter(P1) ** ** Transfer the values of bound parameter P1 into register P2 */ _19: ; /* Value being transferred */ pVar = (*TVdbe)(unsafe.Pointer(p)).FaVar + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1-int32(1))*56 if _sqlite3VdbeMemTooBig(tls, pVar) != 0 { goto too_big } pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { _sqlite3VdbeMemSetNull(tls, pOut) } libc.X__builtin___memcpy_chk(tls, pOut, pVar, uint64(libc.UintptrFromInt32(0)+24), libc.X__builtin_object_size(tls, pOut, 0)) p192 = pOut + 20 *(*Tu16)(unsafe.Pointer(p192)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p192))) & ^(libc.Int32FromInt32(MEM_Dyn) | libc.Int32FromInt32(MEM_Ephem))) p193 = pOut + 20 *(*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 pIn1 = aMem + uintptr(p1)*56 pOut = aMem + uintptr(p2)*56 for { _sqlite3VdbeMemMove(tls, pOut, pIn1) if int32((*TMem)(unsafe.Pointer(pOut)).Fflags)&int32(MEM_Ephem) != 0 && _sqlite3VdbeMemMakeWriteable(tls, pOut) != 0 { goto no_mem } pIn1 += 56 pOut += 56 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)*56 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 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 + 20 *(*Tu16)(unsafe.Pointer(p196)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p196))) & ^libc.Int32FromInt32(MEM_Subtype)) } v197 = n1 n1-- if v197 == 0 { break } pOut += 56 pIn1 += 56 } 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)*56 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 _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)*56 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 _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: ; (*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)*56 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((int64(pOp)-int64(aOp))/24) + 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)*56 pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 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 + 136))) { 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.X__builtin___memcpy_chk(tls, (*TMem)(unsafe.Pointer(pOut)).Fz, (*TMem)(unsafe.Pointer(pIn2)).Fz, uint64((*TMem)(unsafe.Pointer(pIn2)).Fn), libc.X__builtin_object_size(tls, (*TMem)(unsafe.Pointer(pOut)).Fz, 0)) (*TMem)(unsafe.Pointer(pIn2)).Fflags = flags2 } libc.X__builtin___memcpy_chk(tls, (*TMem)(unsafe.Pointer(pOut)).Fz+uintptr((*TMem)(unsafe.Pointer(pIn2)).Fn), (*TMem)(unsafe.Pointer(pIn1)).Fz, uint64((*TMem)(unsafe.Pointer(pIn1)).Fn), libc.X__builtin_object_size(tls, (*TMem)(unsafe.Pointer(pOut)).Fz+uintptr((*TMem)(unsafe.Pointer(pIn2)).Fn), 0)) (*TMem)(unsafe.Pointer(pIn1)).Fflags = flags1 if int32(encoding) > int32(SQLITE_UTF8) { nByte &= int64(^libc.Int32FromInt32(1)) } *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pOut)).Fz + uintptr(nByte))) = 0 *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pOut)).Fz + uintptr(nByte+int64(1)))) = 0 p199 = pOut + 20 *(*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)*56 type1 = (*TMem)(unsafe.Pointer(pIn1)).Fflags pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 type2 = (*TMem)(unsafe.Pointer(pIn2)).Fflags pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 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)*56 pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 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 { /* If shifting by a negative amount, shift in the other direction */ if iB1 < 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.X__builtin___memcpy_chk(tls, bp+16, bp+8, uint64(8), libc.X__builtin_object_size(tls, bp+16, 0)) 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 */ pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*56 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)*56 pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 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. */ 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 = int8(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) 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 + 20 *(*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 + 20 *(*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)) } } } } 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 */ 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. */ (*TMem)(unsafe.Pointer(pIn3)).Fflags = flags3 (*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 */ 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: ; 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 { aPermute = *(*uintptr)(unsafe.Pointer(pOp + uintptr(-libc.Int32FromInt32(1))*24 + 16)) + uintptr(1)*4 } n2 = (*TOp)(unsafe.Pointer(pOp)).Fp3 pKeyInfo = *(*uintptr)(unsafe.Pointer(pOp + 16)) 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 pColl = *(*uintptr)(unsafe.Pointer(pKeyInfo + 32 + uintptr(i)*8)) bRev = int32(*(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pKeyInfo)).FaSortFlags + uintptr(i)))) & int32(KEYINFO_ORDER_DESC) iCompare = _sqlite3MemCompare(tls, aMem+uintptr(uint32(p11)+idx)*56, aMem+uintptr(uint32(p21)+idx)*56, 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)*56))).Fflags)&int32(MEM_Null) != 0 || int32((*(*TMem)(unsafe.Pointer(aMem + uintptr(uint32(p21)+idx)*56))).Fflags)&int32(MEM_Null) != 0) { iCompare = -iCompare } if bRev != 0 { iCompare = -iCompare } break } goto _210 _210: ; i++ } 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 */ if iCompare < 0 { pOp = aOp + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1-int32(1))*24 } else { if iCompare == 0 { pOp = aOp + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2-int32(1))*24 } else { pOp = aOp + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3-int32(1))*24 } } 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)*56, int32(2)) v21 = _sqlite3VdbeBooleanValue(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56, 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)*56 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 */ _sqlite3VdbeMemSetInt64(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56, int64(_sqlite3VdbeBooleanValue(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*56, (*TOp)(unsafe.Pointer(pOp)).Fp3)^(*TOp)(unsafe.Pointer(pOp)).Fp4.Fi)) 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)*56 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 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)*56 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 _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 */ if (*TVdbe)(unsafe.Pointer(p)).FpFrame != 0 { iAddr = uint32(int32((int64(pOp) - int64((*TVdbe)(unsafe.Pointer(p)).FaOp)) / 24)) 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)*56, (*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)*56, 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)*56 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: ; if (*TOp)(unsafe.Pointer(pOp)).Fp1 >= 0 { pC = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) if (*TOp)(unsafe.Pointer(pOp)).Fp3 < int32((*TVdbeCursor)(unsafe.Pointer(pC)).FnHdrParsed) { serialType = *(*Tu32)(unsafe.Pointer(pC + 120 + 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) << ((*TOp)(unsafe.Pointer(pOp)).Fp4.Fi - int32(1))) } } else { typeMask = uint16(int32(1) << (Xsqlite3_value_type(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56) - 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)*56))).Fflags)&int32(MEM_Null) != 0 || int32((*(*TMem)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56))).Fflags)&int32(MEM_Null) != 0 { _sqlite3VdbeMemSetNull(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56) } else { _sqlite3VdbeMemSetInt64(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56, 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)*56 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: ; pC1 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) if pC1 != 0 && (*TVdbeCursor)(unsafe.Pointer(pC1)).FnullRow != 0 { _sqlite3VdbeMemSetNull(tls, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56) 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 */ pC2 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) pOut = (*TVdbe)(unsafe.Pointer(p)).FaMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 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 + 48))) != 0 { _sqlite3VdbeMemSetNull(tls, pOut) } else { _sqlite3VdbeMemSetInt64(tls, pOut, _sqlite3BtreeOffset(tls, *(*uintptr)(unsafe.Pointer(pC2 + 48)))) } } 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 */ pC3 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) p22 = uint32((*TOp)(unsafe.Pointer(pOp)).Fp2) goto op_column_restart op_column_restart: ; aOffset = (*TVdbeCursor)(unsafe.Pointer(pC3)).FaOffset 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)*56 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)*56 _sqlite3VdbeMemSetNull(tls, pDest) goto op_column_out } } else { pCrsr = *(*uintptr)(unsafe.Pointer(pC3 + 48)) if (*TVdbeCursor)(unsafe.Pointer(pC3)).FdeferredMoveto != 0 { if v215 = *(*uintptr)(unsafe.Pointer(pC3 + 16)) != 0; v215 { v214 = *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pC3 + 16)) + 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 } } (*TVdbeCursor)(unsafe.Pointer(pC3)).FpayloadSize = _sqlite3BtreePayloadSize(tls, pCrsr) (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow = _sqlite3BtreePayloadFetch(tls, pCrsr, pC3+108) /* 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 /* Conditional skipped */ goto op_column_read_header } } else { if _sqlite3BtreeCursorHasMoved(tls, *(*uintptr)(unsafe.Pointer(pC3 + 48))) != 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.X__builtin___memset_chk(tls, bp+24, 0, uint64(56), libc.X__builtin_object_size(tls, bp+24, 0)) rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, *(*uintptr)(unsafe.Pointer(pC3 + 48)), *(*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 + 80)) = v222 v221 = v222 *(*Tu32)(unsafe.Pointer(pC3 + 120 + uintptr(i1)*4)) = v221 if v221 < uint32(0x80) { zHdr++ offset64 += uint64(_sqlite3VdbeOneByteSerialTypeLen(tls, uint8(*(*Tu32)(unsafe.Pointer(bp + 80))))) } else { zHdr += uintptr(_sqlite3GetVarint32(tls, zHdr, bp+80)) *(*Tu32)(unsafe.Pointer(pC3 + 120 + uintptr(i1)*4)) = *(*Tu32)(unsafe.Pointer(bp + 80)) offset64 += uint64(_sqlite3VdbeSerialTypeLen(tls, *(*Tu32)(unsafe.Pointer(bp + 80)))) } 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(int64(zHdr) - int64(zData)) if (*TVdbeCursor)(unsafe.Pointer(pC3)).FaRow == uintptr(0) { _sqlite3VdbeMemRelease(tls, bp+24) } goto _220 _219: ; *(*Tu32)(unsafe.Pointer(bp + 80)) = 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)*56 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 + 80)) = *(*Tu32)(unsafe.Pointer(pC3 + 120 + 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. */ pDest = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 if int32((*TMem)(unsafe.Pointer(pDest)).Fflags)&(libc.Int32FromInt32(MEM_Agg)|libc.Int32FromInt32(MEM_Dyn)) != 0 { _sqlite3VdbeMemSetNull(tls, pDest) } 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 + 80)) < uint32(12) { _sqlite3VdbeSerialGet(tls, zData, *(*Tu32)(unsafe.Pointer(bp + 80)), pDest) } else { v224 = int32((*(*Tu32)(unsafe.Pointer(bp + 80)) - 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 + 136)) { 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.X__builtin___memcpy_chk(tls, (*TMem)(unsafe.Pointer(pDest)).Fz, zData, uint64(len1), libc.X__builtin_object_size(tls, (*TMem)(unsafe.Pointer(pDest)).Fz, 0)) *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pDest)).Fz + uintptr(len1))) = 0 *(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pDest)).Fz + uintptr(len1+int32(1)))) = 0 (*TMem)(unsafe.Pointer(pDest)).Fflags = _aFlag1[*(*Tu32)(unsafe.Pointer(bp + 80))&uint32(1)] } } else { (*TMem)(unsafe.Pointer(pDest)).Fenc = encoding /* 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 + 80)) >= uint32(12) && (*(*Tu32)(unsafe.Pointer(bp + 80))&uint32(1) == uint32(0) || int32(p5) == int32(OPFLAG_BYTELENARG))) || _sqlite3VdbeSerialTypeLen(tls, *(*Tu32)(unsafe.Pointer(bp + 80))) == 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 + 80)), pDest) } else { rc = _vdbeColumnFromOverflow(tls, pC3, int32(p22), int32(*(*Tu32)(unsafe.Pointer(bp + 80))), 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))*24 goto _187 } else { rc = _sqlite3CorruptError(tls, int32(96311)) 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: ; pTab = *(*uintptr)(unsafe.Pointer(pOp + 16)) aCol = (*TTable)(unsafe.Pointer(pTab)).FaCol pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*56 i2 = 0 for { if !(i2 < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } if int32((*(*TColumn)(unsafe.Pointer(aCol + uintptr(i2)*16))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { if int32((*(*TColumn)(unsafe.Pointer(aCol + uintptr(i2)*16))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { goto _226 } if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 { pIn1 += 56 goto _226 } } _applyAffinity(tls, pIn1, (*(*TColumn)(unsafe.Pointer(aCol + uintptr(i2)*16))).Faffinity, encoding) if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) == 0 { switch int32(uint32(*(*uint8)(unsafe.Pointer(aCol + uintptr(i2)*16 + 8)) & 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): 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 + 20 *(*Tu16)(unsafe.Pointer(p227)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p227))) | libc.Int32FromInt32(MEM_IntReal)) p228 = pIn1 + 20 *(*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 + 20 *(*Tu16)(unsafe.Pointer(p229)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p229))) | libc.Int32FromInt32(MEM_Real)) p230 = pIn1 + 20 *(*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 += 56 goto _226 _226: ; i2++ } goto _187 goto vdbe_type_error vdbe_type_error: ; _sqlite3VdbeError(tls, p, __ccgo_ts+6078, libc.VaList(bp+944, _vdbeMemTypeName(tls, pIn1), _sqlite3StdType[int32(uint32(*(*uint8)(unsafe.Pointer(aCol + uintptr(i2)*16 + 8))&0xf0>>4))-int32(1)], (*TTable)(unsafe.Pointer(pTab)).FzName, (*(*TColumn)(unsafe.Pointer(aCol + uintptr(i2)*16))).FzCnName)) rc = libc.Int32FromInt32(SQLITE_CONSTRAINT) | libc.Int32FromInt32(12)<= -int64(140737488355328) { p231 = pIn1 + 20 *(*Tu16)(unsafe.Pointer(p231)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p231))) | libc.Int32FromInt32(MEM_IntReal)) p232 = pIn1 + 20 *(*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 + 20 *(*Tu16)(unsafe.Pointer(p233)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p233))) | libc.Int32FromInt32(MEM_Real)) p234 = pIn1 + 20 *(*Tu16)(unsafe.Pointer(p234)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p234))) & ^(libc.Int32FromInt32(MEM_Int) | libc.Int32FromInt32(MEM_Str))) } } zAffinity++ if int32(*(*int8)(unsafe.Pointer(zAffinity))) == 0 { break } pIn1 += 56 } 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)) pData0 = aMem + uintptr(nField)*56 nField = (*TOp)(unsafe.Pointer(pOp)).Fp2 pLast = pData0 + uintptr(nField-int32(1))*56 /* Identify the output register */ pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 /* Apply the requested affinity to all inputs */ if zAffinity1 != 0 { pRec = pData0 for cond := true; cond; cond = *(*int8)(unsafe.Pointer(zAffinity1)) != 0 { _applyAffinity(tls, pRec, *(*int8)(unsafe.Pointer(zAffinity1)), encoding) if int32(*(*int8)(unsafe.Pointer(zAffinity1))) == int32(SQLITE_AFF_REAL) && int32((*TMem)(unsafe.Pointer(pRec)).Fflags)&int32(MEM_Int) != 0 { p235 = pRec + 20 *(*Tu16)(unsafe.Pointer(p235)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p235))) | libc.Int32FromInt32(MEM_IntReal)) p236 = pRec + 20 *(*Tu16)(unsafe.Pointer(p236)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p236))) & ^libc.Int32FromInt32(MEM_Int)) } zAffinity1++ pRec += 56 } } /* 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 { 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(). */ (*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 + 20 *(*Tu16)(unsafe.Pointer(p237)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p237))) & ^libc.Int32FromInt32(MEM_IntReal)) p238 = pRec + 20 *(*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 { 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 -= 56 } /* 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 + 136))) { 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 + 20 *(*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))) } 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.X__builtin___memcpy_chk(tls, bp+88, pRec, uint64(8), libc.X__builtin_object_size(tls, bp+88, 0)) } else { *(*Tu64)(unsafe.Pointer(bp + 88)) = uint64(*(*Ti64)(unsafe.Pointer(pRec))) } len11 = uint32(_sqlite3SmallTypeSizes[serial_type]) switch len11 { default: *(*Tu8)(unsafe.Pointer(zPayload + 7)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 88)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 88)) >>= uint64(8) *(*Tu8)(unsafe.Pointer(zPayload + 6)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 88)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 88)) >>= uint64(8) fallthrough case uint32(6): *(*Tu8)(unsafe.Pointer(zPayload + 5)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 88)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 88)) >>= uint64(8) *(*Tu8)(unsafe.Pointer(zPayload + 4)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 88)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 88)) >>= uint64(8) fallthrough case uint32(4): *(*Tu8)(unsafe.Pointer(zPayload + 3)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 88)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 88)) >>= uint64(8) fallthrough case uint32(3): *(*Tu8)(unsafe.Pointer(zPayload + 2)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 88)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 88)) >>= uint64(8) fallthrough case uint32(2): *(*Tu8)(unsafe.Pointer(zPayload + 1)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 88)) & libc.Uint64FromInt32(0xff)) *(*Tu64)(unsafe.Pointer(bp + 88)) >>= uint64(8) fallthrough case uint32(1): *(*Tu8)(unsafe.Pointer(zPayload)) = uint8(*(*Tu64)(unsafe.Pointer(bp + 88)) & 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.X__builtin___memcpy_chk(tls, zPayload, (*TMem)(unsafe.Pointer(pRec)).Fz, uint64((*TMem)(unsafe.Pointer(pRec)).Fn), libc.X__builtin_object_size(tls, zPayload, 0)) zPayload += uintptr((*TMem)(unsafe.Pointer(pRec)).Fn) } } else { zHdr1 += uintptr(_sqlite3PutVarint(tls, zHdr1, uint64(serial_type))) if (*TMem)(unsafe.Pointer(pRec)).Fn != 0 { libc.X__builtin___memcpy_chk(tls, zPayload, (*TMem)(unsafe.Pointer(pRec)).Fz, uint64((*TMem)(unsafe.Pointer(pRec)).Fn), libc.X__builtin_object_size(tls, zPayload, 0)) zPayload += uintptr((*TMem)(unsafe.Pointer(pRec)).Fn) } } } if pRec == pLast { break } pRec += 56 } 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: ; pCrsr1 = *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) + 48)) if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 { *(*Ti64)(unsafe.Pointer(bp + 96)) = _sqlite3BtreeRowCountEst(tls, pCrsr1) } else { *(*Ti64)(unsafe.Pointer(bp + 96)) = 0 /* Not needed. Only used to silence a warning. */ rc = _sqlite3BtreeCount(tls, db, pCrsr1, bp+96) if rc != 0 { goto abort_due_to_error } } pOut = _out2Prerelease(tls, p, pOp) *(*Ti64)(unsafe.Pointer(pOut)) = *(*Ti64)(unsafe.Pointer(bp + 96)) 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. */ 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+6119, 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. */ 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(32)+uint64(nName)+uint64(1)) if pNew != 0 { (*TSavepoint)(unsafe.Pointer(pNew)).FzName = pNew + 1*32 libc.X__builtin___memcpy_chk(tls, (*TSavepoint)(unsafe.Pointer(pNew)).FzName, zName, uint64(nName+int32(1)), libc.X__builtin_object_size(tls, (*TSavepoint)(unsafe.Pointer(pNew)).FzName, 0)) /* 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 { 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+6170, libc.VaList(bp+944, 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+6192, 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((int64(pOp) - int64(aOp)) / 24) (*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)*32))).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+6246, 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((int64(pOp) - int64(aOp)) / 24) (*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 + 6301 } else { if iRollback != 0 { v251 = __ccgo_ts + 6349 } else { v251 = __ccgo_ts + 6392 } v250 = v251 } _sqlite3VdbeError(tls, p, v250, 0) rc = int32(SQLITE_ERROR) goto abort_due_to_error } /* 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 + 104)) = 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)) { if (*TVdbe)(unsafe.Pointer(p)).FiStatement == 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 } } if rc == SQLITE_OK && (*TOp)(unsafe.Pointer(pOp)).Fp5 != 0 && (*(*int32)(unsafe.Pointer(bp + 104)) != (*TOp)(unsafe.Pointer(pOp)).Fp3 || (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).FiGeneration != (*TOp)(unsafe.Pointer(pOp)).Fp4.Fi) { /* ** 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+6433) /* 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)*32))).FpSchema)).Fschema_cookie != *(*int32)(unsafe.Pointer(bp + 104)) { _sqlite3ResetOneSchema(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1) } libc.SetBitFieldPtr16Uint32(p+200, 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+200, 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: ; iDb = (*TOp)(unsafe.Pointer(pOp)).Fp1 iCookie = (*TOp)(unsafe.Pointer(pOp)).Fp3 _sqlite3BtreeGetMeta(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FpBt, iCookie, bp+108) pOut = _out2Prerelease(tls, p, pOp) *(*Ti64)(unsafe.Pointer(pOut)) = int64(*(*int32)(unsafe.Pointer(bp + 108))) 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: ; pDb1 = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*32 /* 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 + 44)) |= 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+200, 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: ; pCur = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) if pCur != 0 && (*TVdbeCursor)(unsafe.Pointer(pCur)).FpgnoRoot == uint32((*TOp)(unsafe.Pointer(pOp)).Fp2) { /* Guaranteed by the code generator */ _sqlite3BtreeClearCursor(tls, *(*uintptr)(unsafe.Pointer(pCur + 48))) 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: ; if int32(Tbft(*(*uint16)(unsafe.Pointer(p + 200))&0x3>>0)) == int32(1) { rc = libc.Int32FromInt32(SQLITE_ABORT) | libc.Int32FromInt32(2)<>2))), 2, 0x4) *(*uintptr)(unsafe.Pointer(pCx + 16)) = *(*uintptr)(unsafe.Pointer(pOrig + 16)) 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 + 16)), (*TVdbeCursor)(unsafe.Pointer(pCx)).FpgnoRoot, int32(BTREE_WRCSR), (*TVdbeCursor)(unsafe.Pointer(pCx)).FpKeyInfo, *(*uintptr)(unsafe.Pointer(pCx + 48))) /* 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 */ 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: ; 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. */ /* Only used when number of columns is zero */ (*(*TMem)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56))).Fn = 0 (*(*TMem)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56))).Fz = __ccgo_ts + 1664 } pCx1 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) 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. */ (*TVdbeCursor)(unsafe.Pointer(pCx1)).FseqCount = 0 (*TVdbeCursor)(unsafe.Pointer(pCx1)).FcacheStatus = uint32(CACHE_STALE) rc = _sqlite3BtreeClearTable(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 16)), 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+16, 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 + 16)), 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) { rc = _sqlite3BtreeCreateTable(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 16)), pCx1+68, int32(BTREE_BLOBKEY)|int32((*TOp)(unsafe.Pointer(pOp)).Fp5)) if rc == SQLITE_OK { rc = _sqlite3BtreeCursor(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 16)), (*TVdbeCursor)(unsafe.Pointer(pCx1)).FpgnoRoot, int32(BTREE_WRCSR), pKeyInfo2, *(*uintptr)(unsafe.Pointer(pCx1 + 48))) } (*TVdbeCursor)(unsafe.Pointer(pCx1)).FisTable = uint8(0) } else { (*TVdbeCursor)(unsafe.Pointer(pCx1)).FpgnoRoot = uint32(SCHEMA_ROOT) rc = _sqlite3BtreeCursor(tls, *(*uintptr)(unsafe.Pointer(pCx1 + 16)), uint32(SCHEMA_ROOT), int32(BTREE_WRCSR), uintptr(0), *(*uintptr)(unsafe.Pointer(pCx1 + 48))) (*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 + 16))) } } } 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: ; 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)) 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: ; pC4 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) v255 = pC4 + 24 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. If P2 is 0 or negative then the pseudo-cursor ** will return NULL for every column. */ _83: ; 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 + 48)) = _sqlite3BtreeFakeValidCursor(tls) 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 */ _sqlite3VdbeFreeCursor(tls, p, *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8))) *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) = 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: ; /* jump0, in3, group, ncycle */ _87: ; /* jump0, in3, group, ncycle */ _86: ; /* jump0, in3, group, ncycle */ _85: ; /* Only interested in == results */ pC5 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) 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 */ /* 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)*56 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 + 48)), bp+112) 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 { if oc&int32(0x0001) == libc.Int32FromInt32(OP_SeekGT)&libc.Int32FromInt32(0x0001) { oc-- } } else { if c2 < 0 { if oc&int32(0x0001) == libc.Int32FromInt32(OP_SeekLT)&libc.Int32FromInt32(0x0001) { oc++ } } } } rc = _sqlite3BtreeTableMoveto(tls, *(*uintptr)(unsafe.Pointer(pC5 + 48)), int64(uint64(iKey)), 0, bp+112) (*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 + 48)), uint32(BTREE_SEEK_EQ)) != 0 { eqOnly = int32(1) } nField2 = (*TOp)(unsafe.Pointer(pOp)).Fp4.Fi (*(*TUnpackedRecord)(unsafe.Pointer(bp + 120))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC5)).FpKeyInfo (*(*TUnpackedRecord)(unsafe.Pointer(bp + 120))).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 + 120))).Fdefault_rc = int8(v256) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 120))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 (*(*TUnpackedRecord)(unsafe.Pointer(bp + 120))).FeqSeen = uint8(0) rc = _sqlite3BtreeIndexMoveto(tls, *(*uintptr)(unsafe.Pointer(pC5 + 48)), bp+120, bp+112) if rc != SQLITE_OK { goto abort_due_to_error } if eqOnly != 0 && int32((*(*TUnpackedRecord)(unsafe.Pointer(bp + 120))).FeqSeen) == 0 { goto seek_not_found } } if oc >= int32(OP_SeekGE) { if *(*int32)(unsafe.Pointer(bp + 112)) < 0 || *(*int32)(unsafe.Pointer(bp + 112)) == 0 && oc == int32(OP_SeekGT) { *(*int32)(unsafe.Pointer(bp + 112)) = 0 rc = _sqlite3BtreeNext(tls, *(*uintptr)(unsafe.Pointer(pC5 + 48)), 0) if rc != SQLITE_OK { if rc == int32(SQLITE_DONE) { rc = SQLITE_OK *(*int32)(unsafe.Pointer(bp + 112)) = int32(1) } else { goto abort_due_to_error } } } else { *(*int32)(unsafe.Pointer(bp + 112)) = 0 } } else { if *(*int32)(unsafe.Pointer(bp + 112)) > 0 || *(*int32)(unsafe.Pointer(bp + 112)) == 0 && oc == int32(OP_SeekLT) { *(*int32)(unsafe.Pointer(bp + 112)) = 0 rc = _sqlite3BtreePrevious(tls, *(*uintptr)(unsafe.Pointer(pC5 + 48)), 0) if rc != SQLITE_OK { if rc == int32(SQLITE_DONE) { rc = SQLITE_OK *(*int32)(unsafe.Pointer(bp + 112)) = 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 + 112)) = _sqlite3BtreeEof(tls, *(*uintptr)(unsafe.Pointer(pC5 + 48))) } } goto seek_not_found seek_not_found: ; if *(*int32)(unsafe.Pointer(bp + 112)) != 0 { goto jump_to_p2 } else { if eqOnly != 0 { pOp += 24 /* 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: ; /* 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. */ pC6 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*(*TOp)(unsafe.Pointer(pOp + 1*24))).Fp1)*8)) if !(_sqlite3BtreeCursorIsValidNN(tls, *(*uintptr)(unsafe.Pointer(pC6 + 48))) != 0) { goto _187 } nStep = (*TOp)(unsafe.Pointer(pOp)).Fp1 (*(*TUnpackedRecord)(unsafe.Pointer(bp + 168))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC6)).FpKeyInfo (*(*TUnpackedRecord)(unsafe.Pointer(bp + 168))).FnField = uint16(*(*int32)(unsafe.Pointer(pOp + 1*24 + 16))) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 168))).Fdefault_rc = 0 (*(*TUnpackedRecord)(unsafe.Pointer(bp + 168))).FaMem = aMem + uintptr((*(*TOp)(unsafe.Pointer(pOp + 1*24))).Fp3)*56 *(*int32)(unsafe.Pointer(bp + 160)) = 0 /* Not needed. Only used to silence a warning. */ _258: ; if !(int32(1) != 0) { goto _257 } rc = _sqlite3VdbeIdxKeyCompare(tls, db, pC6, bp+168, bp+160) if rc != 0 { goto abort_due_to_error } if !(*(*int32)(unsafe.Pointer(bp + 160)) > 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 += 24 goto jump_to_p2 _259: ; if *(*int32)(unsafe.Pointer(bp + 160)) >= 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 + 48)), 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: ; pC7 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) 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: ; pCur1 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) 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: ; pC8 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) if int32((*TVdbeCursor)(unsafe.Pointer(pC8)).FseekHit) >= (*TOp)(unsafe.Pointer(pOp)).Fp4.Fi { goto _187 } /* Fall through into OP_NotFound */ _95: ; /* jump, in3, ncycle */ _94: ; /* jump, in3, ncycle */ _93: ; pC9 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 208))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 (*(*TUnpackedRecord)(unsafe.Pointer(bp + 208))).FnField = uint16((*TOp)(unsafe.Pointer(pOp)).Fp4.Fi) if int32((*(*TUnpackedRecord)(unsafe.Pointer(bp + 208))).FnField) > 0 { /* Key values in an array of registers */ (*(*TUnpackedRecord)(unsafe.Pointer(bp + 208))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC9)).FpKeyInfo (*(*TUnpackedRecord)(unsafe.Pointer(bp + 208))).Fdefault_rc = 0 rc = _sqlite3BtreeIndexMoveto(tls, *(*uintptr)(unsafe.Pointer(pC9 + 48)), bp+208, pC9+36) } else { /* Composite key generated by OP_MakeRecord */ if int32((*TMem)(unsafe.Pointer((*(*TUnpackedRecord)(unsafe.Pointer(bp + 208))).FaMem)).Fflags)&int32(MEM_Zero) != 0 { v260 = _sqlite3VdbeMemExpandBlob(tls, (*(*TUnpackedRecord)(unsafe.Pointer(bp + 208))).FaMem) } else { v260 = 0 } rc = v260 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 + 208))).FaMem)).Fn, (*TMem)(unsafe.Pointer((*(*TUnpackedRecord)(unsafe.Pointer(bp + 208))).FaMem)).Fz, pIdxKey) (*TUnpackedRecord)(unsafe.Pointer(pIdxKey)).Fdefault_rc = 0 rc = _sqlite3BtreeIndexMoveto(tls, *(*uintptr)(unsafe.Pointer(pC9 + 48)), pIdxKey, pC9+36) _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 + 208))).FnField)) { break } if int32((*(*TMem)(unsafe.Pointer((*(*TUnpackedRecord)(unsafe.Pointer(bp + 208))).FaMem + uintptr(ii1)*56))).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((*TOp)(unsafe.Pointer(pOp)).Fp4.Fi) } } 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)*56 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 + 256)) = 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 + 256)) = *(*TMem)(unsafe.Pointer(pIn3)) _applyAffinity(tls, bp+256, int8(SQLITE_AFF_NUMERIC), encoding) if int32((*(*TMem)(unsafe.Pointer(bp + 256))).Fflags)&int32(MEM_Int) == 0 { goto jump_to_p2 } iKey1 = uint64(*(*Ti64)(unsafe.Pointer(bp + 256))) goto notExistsWithKey } /* Fall through into OP_NotExists */ _96: ; /* jump, in3, ncycle */ pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 iKey1 = uint64(*(*Ti64)(unsafe.Pointer(pIn3))) goto notExistsWithKey notExistsWithKey: ; pC10 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) pCrsr2 = *(*uintptr)(unsafe.Pointer(pC10 + 48)) *(*int32)(unsafe.Pointer(bp + 248)) = 0 rc = _sqlite3BtreeTableMoveto(tls, pCrsr2, int64(iKey1), 0, bp+248) (*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 + 248)) if *(*int32)(unsafe.Pointer(bp + 248)) != 0 { if (*TOp)(unsafe.Pointer(pOp)).Fp2 == 0 { rc = _sqlite3CorruptError(tls, int32(98565)) } 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 */ pOut = _out2Prerelease(tls, p, pOp) v263 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) + 24 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 + 312)) = 0 *(*int32)(unsafe.Pointer(bp + 320)) = 0 pOut = _out2Prerelease(tls, p, pOp) pC11 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) /* 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. */ /* 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 + 48)), bp+320) if rc != SQLITE_OK { goto abort_due_to_error } if *(*int32)(unsafe.Pointer(bp + 320)) != 0 { *(*Ti64)(unsafe.Pointer(bp + 312)) = int64(1) /* IMP: R-61914-48074 */ } else { *(*Ti64)(unsafe.Pointer(bp + 312)) = _sqlite3BtreeIntegerKey(tls, *(*uintptr)(unsafe.Pointer(pC11 + 48))) if *(*Ti64)(unsafe.Pointer(bp + 312)) >= int64(libc.Uint64FromInt32(0x7fffffff)<>1)) != 0 { rc = int32(SQLITE_FULL) /* IMP: R-17817-00630 */ goto abort_due_to_error } if *(*Ti64)(unsafe.Pointer(bp + 312)) < *(*Ti64)(unsafe.Pointer(pMem))+int64(1) { *(*Ti64)(unsafe.Pointer(bp + 312)) = *(*Ti64)(unsafe.Pointer(pMem)) + int64(1) } *(*Ti64)(unsafe.Pointer(pMem)) = *(*Ti64)(unsafe.Pointer(bp + 312)) } 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. */ /* We cannot be in random rowid mode if this is ** an AUTOINCREMENT table. */ cnt1 = 0 for { Xsqlite3_randomness(tls, int32(8), bp+312) *(*Ti64)(unsafe.Pointer(bp + 312)) &= int64(libc.Uint64FromInt32(0x7fffffff)<> libc.Int32FromInt32(1) *(*Ti64)(unsafe.Pointer(bp + 312))++ /* Ensure that v is greater than zero */ goto _268 _268: ; v265 = _sqlite3BtreeTableMoveto(tls, *(*uintptr)(unsafe.Pointer(pC11 + 48)), int64(uint64(*(*Ti64)(unsafe.Pointer(bp + 312)))), 0, bp+320) rc = v265 if v267 = v265 == SQLITE_OK && *(*int32)(unsafe.Pointer(bp + 320)) == 0; v267 { cnt1++ v266 = cnt1 } if !(v267 && v266 < int32(100)) { break } } if rc != 0 { goto abort_due_to_error } if *(*int32)(unsafe.Pointer(bp + 320)) == 0 { rc = int32(SQLITE_FULL) /* IMP: R-38219-53002 */ goto abort_due_to_error } /* 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 + 312)) 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)*56 pC12 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) pKey = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 (*(*TBtreePayload)(unsafe.Pointer(bp + 328))).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) { zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TVdbeCursor)(unsafe.Pointer(pC12)).FiDb)*32))).FzDbSName pTab1 = *(*uintptr)(unsafe.Pointer(pOp + 16)) } 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 + 328))).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 } 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 + 328))).FnKey } } (*(*TBtreePayload)(unsafe.Pointer(bp + 328))).FpData = (*TMem)(unsafe.Pointer(pData)).Fz (*(*TBtreePayload)(unsafe.Pointer(bp + 328))).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 + 328))).FnZero = *(*int32)(unsafe.Pointer(&(*TMem)(unsafe.Pointer(pData)).Fu)) } else { (*(*TBtreePayload)(unsafe.Pointer(bp + 328))).FnZero = 0 } (*(*TBtreePayload)(unsafe.Pointer(bp + 328))).FpKey = uintptr(0) rc = _sqlite3BtreeInsert(tls, *(*uintptr)(unsafe.Pointer(pC12 + 48)), bp+328, 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 { 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 + 328))).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 */ pDest1 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) pSrc = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*8)) if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 { v271 = *(*Ti64)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56)) } else { v271 = 0 } iKey2 = v271 rc = _sqlite3BtreeTransferRow(tls, *(*uintptr)(unsafe.Pointer(pDest1 + 48)), *(*uintptr)(unsafe.Pointer(pSrc + 48)), 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 pC13 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) /* 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) { zDb1 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TVdbeCursor)(unsafe.Pointer(pC13)).FiDb)*32))).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 + 48))) } } else { zDb1 = uintptr(0) pTab2 = uintptr(0) } /* Invoke the pre-update-hook if required. */ if (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 && pTab2 != 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 */ rc = _sqlite3BtreeDelete(tls, *(*uintptr)(unsafe.Pointer(pC13 + 48)), 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) } } 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)*8)) pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 nKeyCol = (*TOp)(unsafe.Pointer(pOp)).Fp4.Fi *(*int32)(unsafe.Pointer(bp + 376)) = 0 rc = _sqlite3VdbeSorterCompare(tls, pC14, pIn3, nKeyCol, bp+376) if rc != 0 { goto abort_due_to_error } if *(*int32)(unsafe.Pointer(bp + 376)) != 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)*56 pC15 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) rc = _sqlite3VdbeSorterRowkey(tls, pC15, pOut) 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)*8)))).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) pC16 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) pCrsr3 = *(*uintptr)(unsafe.Pointer(pC16 + 48)) /* 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(). */ n3 = _sqlite3BtreePayloadSize(tls, pCrsr3) if n3 > uint32(*(*int32)(unsafe.Pointer(db + 136))) { 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) pC17 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) 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 + 384)) = (*TVdbeCursor)(unsafe.Pointer(pC17)).FmovetoTarget } else { if int32((*TVdbeCursor)(unsafe.Pointer(pC17)).FeCurType) == int32(CURTYPE_VTAB) { pVtab = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pC17 + 48)))).FpVtab pModule = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FpModule rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_module)(unsafe.Pointer(pModule)).FxRowid})))(tls, *(*uintptr)(unsafe.Pointer(pC17 + 48)), bp+384) _sqlite3VtabImportErrmsg(tls, p, pVtab) if rc != 0 { goto abort_due_to_error } } else { 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 + 384)) = _sqlite3BtreeIntegerKey(tls, *(*uintptr)(unsafe.Pointer(pC17 + 48))) } } } *(*Ti64)(unsafe.Pointer(pOut)) = *(*Ti64)(unsafe.Pointer(bp + 384)) 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: ; pC18 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) 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 + 48)) = _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 { _sqlite3BtreeClearCursor(tls, *(*uintptr)(unsafe.Pointer(pC18 + 48))) } 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: ; pC19 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) pCrsr4 = *(*uintptr)(unsafe.Pointer(pC19 + 48)) *(*int32)(unsafe.Pointer(bp + 392)) = 0 if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode) == int32(OP_SeekEnd) { (*TVdbeCursor)(unsafe.Pointer(pC19)).FseekResult = -int32(1) if _sqlite3BtreeCursorIsValidNN(tls, pCrsr4) != 0 { goto _187 } } rc = _sqlite3BtreeLast(tls, pCrsr4, bp+392) (*TVdbeCursor)(unsafe.Pointer(pC19)).FnullRow = uint8(*(*int32)(unsafe.Pointer(bp + 392))) (*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 + 392)) != 0 { goto jump_to_p2 } } goto _187 /* Opcode: IfSizeBetween P1 P2 P3 P4 * ** ** Let N be the approximate number of rows in the table or index ** with cursor P1 and let X be 10*log2(N) if N is positive or -1 ** if N is zero. ** ** Jump to P2 if X is in between P3 and P4, inclusive. */ _111: ; pC20 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) pCrsr5 = *(*uintptr)(unsafe.Pointer(pC20 + 48)) rc = _sqlite3BtreeFirst(tls, pCrsr5, bp+396) if rc != 0 { goto abort_due_to_error } if *(*int32)(unsafe.Pointer(bp + 396)) != 0 { sz = int64(-int32(1)) /* -Infinity encoding */ } else { sz = _sqlite3BtreeRowCountEst(tls, pCrsr5) sz = int64(_sqlite3LogEst(tls, uint64(sz))) } *(*int32)(unsafe.Pointer(bp + 396)) = libc.BoolInt32(sz >= int64((*TOp)(unsafe.Pointer(pOp)).Fp3) && sz <= int64((*TOp)(unsafe.Pointer(pOp)).Fp4.Fi)) if *(*int32)(unsafe.Pointer(bp + 396)) != 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 + 212 + 2*4))++ /* Fall through into OP_Rewind */ /* 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: ; pC21 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) *(*int32)(unsafe.Pointer(bp + 400)) = int32(1) if int32((*TVdbeCursor)(unsafe.Pointer(pC21)).FeCurType) == int32(CURTYPE_SORTER) { rc = _sqlite3VdbeSorterRewind(tls, pC21, bp+400) } else { pCrsr6 = *(*uintptr)(unsafe.Pointer(pC21 + 48)) rc = _sqlite3BtreeFirst(tls, pCrsr6, bp+400) (*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 + 400))) if (*TOp)(unsafe.Pointer(pOp)).Fp2 > 0 { if *(*int32)(unsafe.Pointer(bp + 400)) != 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)*8)) rc = _sqlite3VdbeSorterNext(tls, db, pC22) goto next_tail _115: ; /* jump, ncycle */ pC22 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) rc = _sqlite3BtreePrevious(tls, *(*uintptr)(unsafe.Pointer(pC22 + 48)), (*TOp)(unsafe.Pointer(pOp)).Fp3) goto next_tail _116: ; /* jump, ncycle */ pC22 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) rc = _sqlite3BtreeNext(tls, *(*uintptr)(unsafe.Pointer(pC22 + 48)), (*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 + 212 + 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: ; pC23 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 if int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&int32(OPFLAG_NCHANGE) != 0 { (*TVdbe)(unsafe.Pointer(p)).FnChange++ } 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 + 408))).FnKey = int64((*TMem)(unsafe.Pointer(pIn2)).Fn) (*(*TBtreePayload)(unsafe.Pointer(bp + 408))).FpKey = (*TMem)(unsafe.Pointer(pIn2)).Fz (*(*TBtreePayload)(unsafe.Pointer(bp + 408))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 (*(*TBtreePayload)(unsafe.Pointer(bp + 408))).FnMem = uint16((*TOp)(unsafe.Pointer(pOp)).Fp4.Fi) 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 + 48)), bp+408, int32((*TOp)(unsafe.Pointer(pOp)).Fp5)&(libc.Int32FromInt32(OPFLAG_APPEND)|libc.Int32FromInt32(OPFLAG_SAVEPOSITION)|libc.Int32FromInt32(OPFLAG_PREFORMAT)), v274) (*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: ; pC24 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 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: ; pC25 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) pCrsr7 = *(*uintptr)(unsafe.Pointer(pC25 + 48)) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 464))).FpKeyInfo = (*TVdbeCursor)(unsafe.Pointer(pC25)).FpKeyInfo (*(*TUnpackedRecord)(unsafe.Pointer(bp + 464))).FnField = uint16((*TOp)(unsafe.Pointer(pOp)).Fp3) (*(*TUnpackedRecord)(unsafe.Pointer(bp + 464))).Fdefault_rc = 0 (*(*TUnpackedRecord)(unsafe.Pointer(bp + 464))).FaMem = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 rc = _sqlite3BtreeIndexMoveto(tls, pCrsr7, bp+464, bp+456) if rc != 0 { goto abort_due_to_error } if *(*int32)(unsafe.Pointer(bp + 456)) == 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(99862)) goto abort_due_to_error } _sqlite3VdbeMemInit(tls, bp+552, db, uint16(0)) rc = _sqlite3VdbeMemFromBtreeZeroOffset(tls, pCur2, uint32(nCellKey), bp+552) if rc != 0 { goto abort_due_to_error } res10 = _sqlite3VdbeRecordCompareWithSkip(tls, (*(*TMem)(unsafe.Pointer(bp + 552))).Fn, (*(*TMem)(unsafe.Pointer(bp + 552))).Fz, bp+512, 0) _sqlite3VdbeMemReleaseMalloc(tls, bp+552) /* End of inlined sqlite3VdbeIdxKeyCompare() */ if int32((*TOp)(unsafe.Pointer(pOp)).Fopcode)&int32(1) == libc.Int32FromInt32(OP_IdxLT)&libc.Int32FromInt32(1) { res10 = -res10 } else { res10++ } 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: ; 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 *(*int32)(unsafe.Pointer(bp + 608)) = 0 /* Not needed. Only to silence a warning. */ rc = _sqlite3BtreeDropTable(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb2)*32))).FpBt, (*TOp)(unsafe.Pointer(pOp)).Fp1, bp+608) (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(MEM_Int) *(*Ti64)(unsafe.Pointer(pOut)) = int64(*(*int32)(unsafe.Pointer(bp + 608))) if rc != 0 { goto abort_due_to_error } if *(*int32)(unsafe.Pointer(bp + 608)) != 0 { _sqlite3RootPageMoved(tls, db, iDb2, uint32(*(*int32)(unsafe.Pointer(bp + 608))), uint32((*TOp)(unsafe.Pointer(pOp)).Fp1)) /* All OP_Destroy operations occur on the same btree */ 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 + 616)) = 0 rc = _sqlite3BtreeClearTable(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*32))).FpBt, int32(uint32((*TOp)(unsafe.Pointer(pOp)).Fp1)), bp+616) if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 { *(*Ti64)(unsafe.Pointer(p + 56)) += *(*Ti64)(unsafe.Pointer(bp + 616)) if (*TOp)(unsafe.Pointer(pOp)).Fp3 > 0 { *(*Ti64)(unsafe.Pointer(aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56)) += *(*Ti64)(unsafe.Pointer(bp + 616)) } } 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: ; pC29 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) if int32((*TVdbeCursor)(unsafe.Pointer(pC29)).FeCurType) == int32(CURTYPE_SORTER) { _sqlite3VdbeSorterReset(tls, db, *(*uintptr)(unsafe.Pointer(pC29 + 48))) } else { rc = _sqlite3BtreeClearTableOfCursor(tls, *(*uintptr)(unsafe.Pointer(pC29 + 48))) 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 + 624)) = uint32(0) pDb3 = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*32 rc = _sqlite3BtreeCreateTable(tls, (*TDb)(unsafe.Pointer(pDb3)).FpBt, bp+624, (*TOp)(unsafe.Pointer(pOp)).Fp3) if rc != 0 { goto abort_due_to_error } *(*Ti64)(unsafe.Pointer(pOut)) = int64(*(*TPgno)(unsafe.Pointer(bp + 624))) goto _187 /* Opcode: SqlExec P1 P2 * P4 * ** ** Run the SQL statement or statements specified in the P4 string. ** ** The P1 parameter is a bitmask of options: ** ** 0x0001 Disable Auth and Trace callbacks while the statements ** in P4 are running. ** ** 0x0002 Set db->nAnalysisLimit to P2 while the statements in ** P4 are running. ** */ _132: ; (*Tsqlite3)(unsafe.Pointer(db)).FnSqlExec++ *(*uintptr)(unsafe.Pointer(bp + 632)) = uintptr(0) xAuth = (*Tsqlite3)(unsafe.Pointer(db)).FxAuth mTrace = (*Tsqlite3)(unsafe.Pointer(db)).FmTrace savedAnalysisLimit = (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit if (*TOp)(unsafe.Pointer(pOp)).Fp1&int32(0x0001) != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).FmTrace = uint8(0) } if (*TOp)(unsafe.Pointer(pOp)).Fp1&int32(0x0002) != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit = (*TOp)(unsafe.Pointer(pOp)).Fp2 } rc = Xsqlite3_exec(tls, db, *(*uintptr)(unsafe.Pointer(pOp + 16)), uintptr(0), uintptr(0), bp+632) (*Tsqlite3)(unsafe.Pointer(db)).FnSqlExec-- (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth (*Tsqlite3)(unsafe.Pointer(db)).FmTrace = mTrace (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit = savedAnalysisLimit if *(*uintptr)(unsafe.Pointer(bp + 632)) != 0 || rc != 0 { _sqlite3VdbeError(tls, p, __ccgo_ts+3863, libc.VaList(bp+944, *(*uintptr)(unsafe.Pointer(bp + 632)))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 632))) 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 if *(*uintptr)(unsafe.Pointer(pOp + 16)) == uintptr(0) { _sqlite3SchemaClear(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb3)*32))).FpSchema) *(*Tu32)(unsafe.Pointer(db + 44)) &= uint32(^libc.Int32FromInt32(DBFLAG_SchemaKnownOk)) rc = _sqlite3InitOne(tls, db, iDb3, p+168, uint32((*TOp)(unsafe.Pointer(pOp)).Fp5)) *(*Tu32)(unsafe.Pointer(db + 44)) |= uint32(DBFLAG_SchemaChange) libc.SetBitFieldPtr16Uint32(p+200, libc.Uint32FromInt32(0), 0, 0x3) } else { zSchema = __ccgo_ts + 6478 (*(*TInitData)(unsafe.Pointer(bp + 640))).Fdb = db (*(*TInitData)(unsafe.Pointer(bp + 640))).FiDb = iDb3 (*(*TInitData)(unsafe.Pointer(bp + 640))).FpzErrMsg = p + 168 (*(*TInitData)(unsafe.Pointer(bp + 640))).FmInitFlags = uint32(0) (*(*TInitData)(unsafe.Pointer(bp + 640))).FmxPage = _sqlite3BtreeLastPage(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb3)*32))).FpBt) zSql = _sqlite3MPrintf(tls, db, __ccgo_ts+6492, libc.VaList(bp+944, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb3)*32))).FzDbSName, zSchema, *(*uintptr)(unsafe.Pointer(pOp + 16)))) if zSql == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy = uint8(1) (*(*TInitData)(unsafe.Pointer(bp + 640))).Frc = SQLITE_OK (*(*TInitData)(unsafe.Pointer(bp + 640))).FnInitRow = uint32(0) rc = Xsqlite3_exec(tls, db, zSql, __ccgo_fp(_sqlite3InitCallback), bp+640, uintptr(0)) if rc == SQLITE_OK { rc = (*(*TInitData)(unsafe.Pointer(bp + 640))).Frc } if rc == SQLITE_OK && (*(*TInitData)(unsafe.Pointer(bp + 640))).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(100155)) } _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: ; 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+1) the text of an error message describing any problems. ** If no problems are found, store a NULL in register (P1+1). ** ** The register (P1) contains one less than the maximum number of allowed ** errors. At most reg(P1) 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 */ nRoot = (*TOp)(unsafe.Pointer(pOp)).Fp2 aRoot = *(*uintptr)(unsafe.Pointer(pOp + 16)) pnErr = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*56 pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1+int32(1))*56 rc = _sqlite3BtreeIntegrityCheck(tls, db, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp5)*32))).FpBt, aRoot+1*4, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56, nRoot, int32(*(*Ti64)(unsafe.Pointer(pnErr)))+int32(1), bp+680, bp+688) _sqlite3VdbeMemSetNull(tls, pIn1) if *(*int32)(unsafe.Pointer(bp + 680)) == 0 { } else { if rc != 0 { Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 688))) goto abort_due_to_error } else { *(*Ti64)(unsafe.Pointer(pnErr)) -= int64(*(*int32)(unsafe.Pointer(bp + 680)) - int32(1)) _sqlite3VdbeMemSetStr(tls, pIn1, *(*uintptr)(unsafe.Pointer(bp + 688)), 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)*56 pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Blob) == 0 { if _sqlite3VdbeMemSetRowSet(tls, pIn1) != 0 { goto no_mem } } _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)*56 if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Blob) == 0 || _sqlite3RowSetNext(tls, (*TMem)(unsafe.Pointer(pIn1)).Fz, bp+696) == 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)*56, *(*Ti64)(unsafe.Pointer(bp + 696))) } 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)*56 pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 iSet = (*TOp)(unsafe.Pointer(pOp)).Fp4.Fi /* 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 } } 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. P2 might be zero, if there is ** no possibility that an IGNORE exception will be raised. ** 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)*56 /* 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 + 136 + 10*4)) { rc = int32(SQLITE_ERROR) _sqlite3VdbeError(tls, p, __ccgo_ts+6535, 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 if (*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr == 0 { nMem++ } nByte2 = int32((libc.Uint64FromInt64(112)+libc.Uint64FromInt32(7))&uint64(^libc.Int32FromInt32(7)) + uint64(nMem)*uint64(56) + uint64((*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr)*uint64(8) + uint64(((*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((int64(pOp) - int64(aOp)) / 24) (*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.Uint64FromInt64(112)+libc.Uint64FromInt32(7))&uint64(^libc.Int32FromInt32(7))) + uintptr((*TVdbeFrame)(unsafe.Pointer(pFrame2)).FnChildMem)*56 pMem1 = pFrame2 + uintptr((libc.Uint64FromInt64(112)+libc.Uint64FromInt32(7))&uint64(^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 += 56 } } else { pFrame2 = (*TMem)(unsafe.Pointer(pRt)).Fz } (*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 (*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.Uint64FromInt64(112)+libc.Uint64FromInt32(7))&uint64(^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)*56 (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FaOnce = (*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr)*8 libc.X__builtin___memset_chk(tls, (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FaOnce, 0, uint64(((*TSubProgram)(unsafe.Pointer(pProgram)).FnOp+int32(7))/int32(8)), libc.X__builtin_object_size(tls, (*TVdbeFrame)(unsafe.Pointer(pFrame2)).FaOnce, 0)) 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))*24 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)*24))).Fp1)*56 _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 + 792)) += int64((*TOp)(unsafe.Pointer(pOp)).Fp2) } else { if (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 { *(*Ti64)(unsafe.Pointer(db + 784)) += int64((*TOp)(unsafe.Pointer(pOp)).Fp2) } else { *(*Ti64)(unsafe.Pointer(p + 80)) += 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)*56 } else { pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*56 } _sqlite3VdbeMemIntegerify(tls, pIn1) pIn2 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 _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)*56 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)*56 pIn3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 pOut = _out2Prerelease(tls, p, pOp) *(*Ti64)(unsafe.Pointer(bp + 704)) = *(*Ti64)(unsafe.Pointer(pIn1)) if v282 = *(*Ti64)(unsafe.Pointer(bp + 704)) <= 0; !v282 { if *(*Ti64)(unsafe.Pointer(pIn3)) > 0 { v281 = *(*Ti64)(unsafe.Pointer(pIn3)) } else { v281 = 0 } } if v282 || _sqlite3AddInt64(tls, bp+704, 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 + 704)) } 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)*56 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)*56 if *(*Ti64)(unsafe.Pointer(pIn1)) > int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<= 0) { break } *(*uintptr)(unsafe.Pointer(pCtx1 + 48 + uintptr(i4)*8)) = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2+i4)*56 goto _283 _283: ; i4-- } } (*TMem)(unsafe.Pointer(pMem2)).Fn++ 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+48) } 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+48) } /* 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+3863, libc.VaList(bp+944, 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 { i4 = (*(*TOp)(unsafe.Pointer(pOp + uintptr(-libc.Int32FromInt32(1))*24))).Fp1 if i4 != 0 { _sqlite3VdbeMemSetInt64(tls, aMem+uintptr(i4)*56, 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 } } 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: ; pMem3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*56 if (*TOp)(unsafe.Pointer(pOp)).Fp3 != 0 { rc = _sqlite3VdbeMemAggValue(tls, pMem3, aMem+uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56, *(*uintptr)(unsafe.Pointer(pOp + 16))) pMem3 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 } else { rc = _sqlite3VdbeMemFinalize(tls, pMem3, *(*uintptr)(unsafe.Pointer(pOp + 16))) } if rc != 0 { _sqlite3VdbeError(tls, p, __ccgo_ts+3863, libc.VaList(bp+944, 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 */ (*(*[3]int32)(unsafe.Pointer(bp + 712)))[0] = 0 v284 = -libc.Int32FromInt32(1) (*(*[3]int32)(unsafe.Pointer(bp + 712)))[int32(2)] = v284 (*(*[3]int32)(unsafe.Pointer(bp + 712)))[int32(1)] = v284 rc = _sqlite3Checkpoint(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp1, (*TOp)(unsafe.Pointer(pOp)).Fp2, bp+712+1*4, bp+712+2*4) if rc != 0 { if rc != int32(SQLITE_BUSY) { goto abort_due_to_error } rc = SQLITE_OK (*(*[3]int32)(unsafe.Pointer(bp + 712)))[0] = int32(1) } i5 = 0 pMem4 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp3)*56 for { if !(i5 < int32(3)) { break } _sqlite3VdbeMemSetInt64(tls, pMem4, int64((*(*[3]int32)(unsafe.Pointer(bp + 712)))[i5])) goto _285 _285: ; i5++ pMem4 += 56 } 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 pBt1 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*32))).FpBt pPager = _sqlite3BtreePager(tls, pBt1) eOld = _sqlite3PagerGetJournalMode(tls, pPager) if eNew == -int32(1) { eNew = eOld } 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 + 6572 } else { v286 = __ccgo_ts + 6577 } _sqlite3VdbeError(tls, p, __ccgo_ts+6584, libc.VaList(bp+944, 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. */ 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: ; if (*TOp)(unsafe.Pointer(pOp)).Fp2 != 0 { v288 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 } else { v288 = uintptr(0) } rc = _sqlite3RunVacuum(tls, p+168, 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: ; pBt2 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*32))).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: ; if !((*TOp)(unsafe.Pointer(pOp)).Fp1 != 0) { _sqlite3ExpirePreparedStatements(tls, db, (*TOp)(unsafe.Pointer(pOp)).Fp2) } else { libc.SetBitFieldPtr16Uint32(p+200, 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: ; pC30 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) _sqlite3BtreeCursorPin(tls, *(*uintptr)(unsafe.Pointer(pC30 + 48))) goto _187 /* Opcode: CursorUnlock P1 * * * * ** ** Unlock the btree to which cursor P1 is pointing so that it can be ** written by other cursors. */ _162: ; pC31 = *(*uintptr)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FapCsr + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*8)) _sqlite3BtreeCursorUnpin(tls, *(*uintptr)(unsafe.Pointer(pC31 + 48))) 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+3863, libc.VaList(bp+944, Xsqlite3_value_text(tls, pDest2))) rc = (*(*Tsqlite3_context)(unsafe.Pointer(bp + 800))).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)*8)) if (*TVdbeCursor)(unsafe.Pointer(pCur6)).FnullRow != 0 { goto _187 } pVtab5 = (*Tsqlite3_vtab_cursor)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pCur6 + 48)))).FpVtab pModule5 = (*Tsqlite3_vtab)(unsafe.Pointer(pVtab5)).FpModule /* 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 + 48))) _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 + 48))) 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 + 48)) |= uint64(SQLITE_LegacyAlter) pVtab6 = (*TVTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pOp + 16)))).FpVtab pName = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*56 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 + 48)) &= ^libc.Uint64FromInt32(SQLITE_LegacyAlter) } _sqlite3VtabImportErrmsg(tls, p, pVtab6) libc.SetBitFieldPtr16Uint32(p+200, 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 + 928)) = 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 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)*56 i7 = 0 for { if !(i7 < nArg1) { break } *(*uintptr)(unsafe.Pointer(apArg1 + uintptr(i7)*8)) = pX1 pX1 += 56 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+928) (*Tsqlite3)(unsafe.Pointer(db)).FvtabOnConflict = vtabOnConflict _sqlite3VtabImportErrmsg(tls, p, pVtab7) if rc == SQLITE_OK && (*TOp)(unsafe.Pointer(pOp)).Fp1 != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FlastRowid = *(*Tsqlite_int64)(unsafe.Pointer(bp + 928)) } 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)*32))).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)*32))).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: ; 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)*56 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 + 48 + uintptr(i8)*8)) = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2+i8)*56 goto _292 _292: ; i8-- } } (*TMem)(unsafe.Pointer(pOut)).Fflags = uint16(int32((*TMem)(unsafe.Pointer(pOut)).Fflags) & ^(libc.Int32FromInt32(MEM_TypeMask)|libc.Int32FromInt32(MEM_Zero)) | int32(MEM_Null)) (*(*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+48) /* 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+3863, libc.VaList(bp+944, Xsqlite3_value_text(tls, pOut))) rc = (*Tsqlite3_context)(unsafe.Pointer(pCtx2)).FisError } _sqlite3VdbeDeleteAuxData(tls, db, p+296, (*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 } } 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)*56 p293 = pIn1 + 20 *(*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)*56 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 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)*56 pOut = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp2)*56 if int32((*TMem)(unsafe.Pointer(pIn1)).Fflags)&int32(MEM_Null) != 0 { p294 = pOut + 20 *(*Tu16)(unsafe.Pointer(p294)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p294))) & ^libc.Int32FromInt32(MEM_Subtype)) } else { p295 = pOut + 20 *(*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: ; pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*56 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)) *(*int8)(unsafe.Pointer(p296)) = int8(int32(*(*int8)(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: ; pIn1 = aMem + uintptr((*TOp)(unsafe.Pointer(pOp)).Fp1)*56 h1 = _filterHash(tls, aMem, pOp) h1 %= uint64((*TMem)(unsafe.Pointer(pIn1)).Fn * libc.Int32FromInt32(8)) if int32(*(*int8)(unsafe.Pointer((*TMem)(unsafe.Pointer(pIn1)).Fz + uintptr(h1/uint64(8)))))&(int32(1)<<(h1&uint64(7))) == 0 { *(*Tu32)(unsafe.Pointer(p + 212 + 8*4))++ goto jump_to_p2 } else { *(*Tu32)(unsafe.Pointer(p + 212 + 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. */ /* OP_Init is always instruction 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+6675, libc.VaList(bp+944, 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) } } } 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)*24))).Fopcode) == int32(OP_Once) { (*(*TOp)(unsafe.Pointer((*TVdbe)(unsafe.Pointer(p)).FaOp + uintptr(i9)*24))).Fp1 = 0 } goto _300 _300: ; i9++ } (*TOp)(unsafe.Pointer(pOp)).Fp1 = 0 } (*TOp)(unsafe.Pointer(pOp)).Fp1++ *(*Tu32)(unsafe.Pointer(p + 212 + 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 */ 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 += 24 } /* 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) 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)) if int32((*TVdbeCursor)(unsafe.Pointer(pC)).FnHdrParsed) > int32((*TIncrblob)(unsafe.Pointer(p)).FiCol) { v1 = *(*Tu32)(unsafe.Pointer(pC + 120 + uintptr((*TIncrblob)(unsafe.Pointer(p)).FiCol)*4)) } else { v1 = uint32(0) } type1 = v1 if type1 < uint32(12) { if type1 == uint32(0) { v2 = __ccgo_ts + 1650 } else { if type1 == uint32(7) { v3 = __ccgo_ts + 6713 } else { v3 = __ccgo_ts + 6718 } v2 = v3 } zErr = _sqlite3MPrintf(tls, (*TIncrblob)(unsafe.Pointer(p)).Fdb, __ccgo_ts+6726, 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 + 120 + 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 + 48)) _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+6755, libc.VaList(bp+8, iRow)) rc = int32(SQLITE_ERROR) } else { zErr = _sqlite3MPrintf(tls, (*TIncrblob)(unsafe.Pointer(p)).Fdb, __ccgo_ts+3863, libc.VaList(bp+8, Xsqlite3_errmsg(tls, (*TIncrblob)(unsafe.Pointer(p)).Fdb))) } } } *(*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(448) defer tls.Free(448) var aOp, pBlob, pFKey, pIdx, pTab, v, zFault, v7 uintptr var iCol, iDb, j, j1, nAttempt, rc, v6 int32 var _ /* sParse at bp+8 */ 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(56)) for int32(1) != 0 { _sqlite3ParseObjectInit(tls, bp+8, 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+8, uint32(0), zTable, zDb) if pTab != 0 && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { pTab = uintptr(0) _sqlite3ErrorMsg(tls, bp+8, __ccgo_ts+6775, libc.VaList(bp+440, zTable)) } if pTab != 0 && !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { pTab = uintptr(0) _sqlite3ErrorMsg(tls, bp+8, __ccgo_ts+6805, libc.VaList(bp+440, zTable)) } if pTab != 0 && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { pTab = uintptr(0) _sqlite3ErrorMsg(tls, bp+8, __ccgo_ts+6841, libc.VaList(bp+440, zTable)) } if !(pTab != 0) { if (*(*TParse)(unsafe.Pointer(bp + 8))).FzErrMsg != 0 { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = (*(*TParse)(unsafe.Pointer(bp + 8))).FzErrMsg (*(*TParse)(unsafe.Pointer(bp + 8))).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))*32))).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)*16))).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+6862, libc.VaList(bp+440, 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 { pFKey = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 64))).FpFKey for { if !(pFKey != 0) { break } j = 0 for { if !(j < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) { break } if (*(*TsColMap)(unsafe.Pointer(pFKey + 64 + uintptr(j)*16))).FiFrom == iCol { zFault = __ccgo_ts + 6883 } 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 + 6895 } 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+6903, libc.VaList(bp+440, zFault)) rc = int32(SQLITE_ERROR) _sqlite3BtreeLeaveAll(tls, db) goto blob_open_out } } (*TIncrblob)(unsafe.Pointer(pBlob)).FpStmt = _sqlite3VdbeCreate(tls, bp+8) 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)) aOp = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint64FromInt64(24)/libc.Uint64FromInt64(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 { /* 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*24))).Fopcode = uint8(OP_OpenWrite) } (*(*TVdbeOp)(unsafe.Pointer(aOp + 1*24))).Fp2 = int32((*TTable)(unsafe.Pointer(pTab)).Ftnum) (*(*TVdbeOp)(unsafe.Pointer(aOp + 1*24))).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*24))).Fp4type = int8(-libc.Int32FromInt32(3)) *(*int32)(unsafe.Pointer(aOp + 1*24 + 16)) = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) + int32(1) (*(*TVdbeOp)(unsafe.Pointer(aOp + 3*24))).Fp2 = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) (*(*TParse)(unsafe.Pointer(bp + 8))).FnVar = 0 (*(*TParse)(unsafe.Pointer(bp + 8))).FnMem = int32(1) (*(*TParse)(unsafe.Pointer(bp + 8))).FnTab = int32(1) _sqlite3VdbeMakeReady(tls, v, bp+8) } } (*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+8) } 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 + 3863 } else { v7 = libc.UintptrFromInt32(0) } _sqlite3ErrorWithMsg(tls, db, rc, v7, libc.VaList(bp+440, *(*uintptr)(unsafe.Pointer(bp)))) _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp))) _sqlite3ParseObjectReset(tls, bp+8) 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(102671)) } 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. */ _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) _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(102771)) } 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 + 3863 } 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))) } } rc = _sqlite3ApiExit(tls, db, rc) 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 { FiReadOff Ti64 FiEof Ti64 FnAlloc int32 FnKey int32 FpFd uintptr FaAlloc uintptr FaKey uintptr FaBuffer uintptr FnBuffer int32 FaMap uintptr FpIncr uintptr } type PmaReader = TPmaReader /* Incrementally read one PMA */ type TPmaWriter = struct { FeFWErr int32 FaBuffer uintptr FnBuffer int32 FiBufStart int32 FiBufEnd int32 FiWriteOff Ti64 FpFd uintptr } 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 { FpThread uintptr FbDone int32 FnPMA int32 FpSorter uintptr FpUnpacked uintptr Flist TSorterList FxCompare TSorterCompare Ffile TSorterFile Ffile2 TSorterFile } type SortSubtask = TSortSubtask /* A sub-task in the sort process */ type TSorterFile = struct { FpFd uintptr FiEof Ti64 } type SorterFile = TSorterFile /* Temporary file object wrapper */ type TSorterList = struct { FpList uintptr FaMemory uintptr FszPMA Ti64 } type SorterList = TSorterList /* In-memory list of records */ type TIncrMerger = struct { FpTask uintptr FpMerger uintptr FiStartOff Ti64 FmxSz int32 FbEof int32 FbUseThread int32 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 { FpFd uintptr 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 { 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 { FpTask uintptr FpMerger uintptr FiStartOff Ti64 FmxSz int32 FbEof int32 FbUseThread int32 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 { FeFWErr int32 FaBuffer uintptr FnBuffer int32 FiBufStart int32 FiBufEnd int32 FiWriteOff Ti64 FpFd uintptr } 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.X__builtin___memset_chk(tls, pReadr, 0, uint64(80), libc.X__builtin_object_size(tls, pReadr, 0)) } // 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 } /* 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) } /* 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) 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.X__builtin___memcpy_chk(tls, (*TPmaReader)(unsafe.Pointer(p)).FaAlloc, (*TPmaReader)(unsafe.Pointer(p)).FaBuffer+uintptr(iBuf), uint64(nAvail), libc.X__builtin_object_size(tls, (*TPmaReader)(unsafe.Pointer(p)).FaAlloc, 0)) *(*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.X__builtin___memcpy_chk(tls, (*TPmaReader)(unsafe.Pointer(p)).FaAlloc+uintptr(nByte-nRem), *(*uintptr)(unsafe.Pointer(bp)), uint64(nCopy), libc.X__builtin_object_size(tls, (*TPmaReader)(unsafe.Pointer(p)).FaAlloc+uintptr(nByte-nRem), 0)) 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 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+40, (*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+40) } 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 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, uint64((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 { 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 */ 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 } } 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 { 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 + 136 + 11*4)) } /* Do not allow the total number of threads (main thread + all workers) ** to exceed the maximum merge count */ szKeyInfo = int32(uint64(40) + uint64(int32((*TKeyInfo)(unsafe.Pointer((*TVdbeCursor)(unsafe.Pointer(pCsr)).FpKeyInfo)).FnKeyField)-libc.Int32FromInt32(1))*uint64(8)) sz = int32(uint64(192) + uint64(nWorker)*uint64(96)) pSorter = _sqlite3DbMallocZero(tls, db, uint64(sz+szKeyInfo)) *(*uintptr)(unsafe.Pointer(pCsr + 48)) = 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.X__builtin___memcpy_chk(tls, pKeyInfo, (*TVdbeCursor)(unsafe.Pointer(pCsr)).FpKeyInfo, uint64(szKeyInfo), libc.X__builtin_object_size(tls, pKeyInfo, 0)) (*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 + 96 + uintptr(i)*96 (*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 { (*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 + 32)) == uintptr(0) || *(*uintptr)(unsafe.Pointer(pKeyInfo + 32)) == (*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 + 8)) _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 { _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.X__builtin___memset_chk(tls, pTask, 0, uint64(96), libc.X__builtin_object_size(tls, pTask, 0)) } // 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(int64(libc.Int32FromInt32(SQLITE_ERROR))) _sqlite3ThreadJoin(tls, (*TSortSubtask)(unsafe.Pointer(pTask)).FpThread, bp) rc = int32(int64(*(*uintptr)(unsafe.Pointer(bp)))) (*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) { 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 + 96 + uintptr(i)*96 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 */ for N < nReader { N += N } nByte = int32(uint64(32) + uint64(N)*(libc.Uint64FromInt64(4)+libc.Uint64FromInt64(80))) 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*32 (*TMergeEngine)(unsafe.Pointer(pNew)).FaTree = (*TMergeEngine)(unsafe.Pointer(pNew)).FaReadr + uintptr(N)*80 } 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)*80) 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 + 40))).FpFd != 0 { _sqlite3OsCloseFree(tls, (*(*TSorterFile)(unsafe.Pointer(pIncr + 40))).FpFd) } if (*(*TSorterFile)(unsafe.Pointer(pIncr + 40 + 1*16))).FpFd != 0 { _sqlite3OsCloseFree(tls, (*(*TSorterFile)(unsafe.Pointer(pIncr + 40 + 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) 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 + 96 + uintptr(i)*96 _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 pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 48)) if pSorter != 0 { _sqlite3VdbeSorterReset(tls, db, pSorter) Xsqlite3_free(tls, (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory) _sqlite3DbFree(tls, db, pSorter) *(*uintptr)(unsafe.Pointer(pCsr + 48)) = 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(32) defer tls.Free(32) *(*Ti64)(unsafe.Pointer(bp)) = _nByte var _ /* chunksize at bp+16 */ 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 + 16)) = libc.Int32FromInt32(4) * libc.Int32FromInt32(1024) _sqlite3OsFileControlHint(tls, pFd, int32(SQLITE_FCNTL_CHUNK_SIZE), bp+16) _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+8 */ int32 var _ /* pFinal at bp+0 */ uintptr _, _ = pp, res *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) pp = bp *(*int32)(unsafe.Pointer(bp + 8)) = 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+8, p1+libc.UintptrFromInt32(1)*16, (*TSorterRecord)(unsafe.Pointer(p1)).FnVal, p2+libc.UintptrFromInt32(1)*16, (*TSorterRecord)(unsafe.Pointer(p2)).FnVal) if res <= 0 { *(*uintptr)(unsafe.Pointer(pp)) = p1 pp = p1 + 8 p1 = *(*uintptr)(unsafe.Pointer(p1 + 8)) if p1 == uintptr(0) { *(*uintptr)(unsafe.Pointer(pp)) = p2 break } } else { *(*uintptr)(unsafe.Pointer(pp)) = p2 pp = p2 + 8 p2 = *(*uintptr)(unsafe.Pointer(p2 + 8)) *(*int32)(unsafe.Pointer(bp + 8)) = 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(512) defer tls.Free(512) 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.X__builtin___memset_chk(tls, bp, 0, uint64(512), libc.X__builtin_object_size(tls, bp, 0)) for p != 0 { if (*TSorterList)(unsafe.Pointer(pList)).FaMemory != 0 { if p == (*TSorterList)(unsafe.Pointer(pList)).FaMemory { pNext = uintptr(0) } else { pNext = (*TSorterList)(unsafe.Pointer(pList)).FaMemory + uintptr(*(*int32)(unsafe.Pointer(&(*TSorterRecord)(unsafe.Pointer(p)).Fu))) } } else { pNext = *(*uintptr)(unsafe.Pointer(p + 8)) } *(*uintptr)(unsafe.Pointer(p + 8)) = 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.Uint64FromInt64(512)/libc.Uint64FromInt64(8))) { 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 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.X__builtin___memset_chk(tls, p, 0, uint64(48), libc.X__builtin_object_size(tls, p, 0)) (*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.X__builtin___memcpy_chk(tls, (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer+uintptr((*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd), pData+uintptr(nData-nRem), uint64(nCopy), libc.X__builtin_object_size(tls, (*TPmaWriter)(unsafe.Pointer(p)).FaBuffer+uintptr((*TPmaWriter)(unsafe.Pointer(p)).FiBufEnd), 0)) *(*int32)(unsafe.Pointer(p + 24)) += 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 + 32)) += int64((*TPmaWriter)(unsafe.Pointer(p)).FnBuffer) } 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.X__builtin___memset_chk(tls, p, 0, uint64(48), libc.X__builtin_object_size(tls, p, 0)) 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.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 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+64) } /* 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 + 8)) _vdbePmaWriteVarint(tls, bp, uint64((*TSorterRecord)(unsafe.Pointer(p)).FnVal)) _vdbePmaWriteBlob(tls, bp, p+libc.UintptrFromInt32(1)*16, (*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+64+8) } 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)*80) /* 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))*80 pReadr2 = (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(iPrev|libc.Int32FromInt32(0x0001))*80 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 + 96 + uintptr(iTest)*96 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+96+uintptr(nWorker)*96, pSorter+56) } else { aMem = (*TSortSubtask)(unsafe.Pointer(pTask)).Flist.FaMemory pCtx = pTask (*TVdbeSorter)(unsafe.Pointer(pSorter)).FiPrev = uint8((int64(pTask) - t__predefined_ptrdiff_t(pSorter+96)) / 96) (*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 */ pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 48)) *(*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 + 92 *(*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 + 92 *(*Tu8)(unsafe.Pointer(p2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p2))) & libc.Int32FromInt32(SORTER_TYPE_TEXT)) } else { (*TVdbeSorter)(unsafe.Pointer(pSorter)).FtypeMask = uint8(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(uint64((*TMem)(unsafe.Pointer(pVal)).Fn) + uint64(16)) 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 } } (*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(int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList) - int64((*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 + 80 *(*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(&(*TSorterRecord)(unsafe.Pointer(pNew)).Fu)) = int32(int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList) - int64((*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FaMemory)) } } else { pNew = _sqlite3Malloc(tls, uint64(nReq)) if pNew == uintptr(0) { return int32(SQLITE_NOMEM) } *(*uintptr)(unsafe.Pointer(pNew + 8)) = (*TVdbeSorter)(unsafe.Pointer(pSorter)).Flist.FpList } libc.X__builtin___memcpy_chk(tls, pNew+libc.UintptrFromInt32(1)*16, (*TMem)(unsafe.Pointer(pVal)).Fz, uint64((*TMem)(unsafe.Pointer(pVal)).Fn), libc.X__builtin_object_size(tls, pNew+libc.UintptrFromInt32(1)*16, 0)) (*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(64) defer tls.Free(64) var iEof, iStart Ti64 var nKey, rc, rc2 int32 var pMerger, pOut, pReader, pTask uintptr var _ /* dummy at bp+48 */ 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 + 40 + 1*16 pTask = (*TIncrMerger)(unsafe.Pointer(pIncr)).FpTask pMerger = (*TIncrMerger)(unsafe.Pointer(pIncr)).FpMerger _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)))*80 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) rc = _vdbeMergeEngineStep(tls, (*TIncrMerger)(unsafe.Pointer(pIncr)).FpMerger, bp+48) } 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(int64(_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 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 + 40)) *(*TSorterFile)(unsafe.Pointer(pIncr + 40)) = *(*TSorterFile)(unsafe.Pointer(pIncr + 40 + 1*16)) *(*TSorterFile)(unsafe.Pointer(pIncr + 40 + 1*16)) = f0 } if rc == SQLITE_OK { if (*(*TSorterFile)(unsafe.Pointer(pIncr + 40))).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 + 40)) = *(*TSorterFile)(unsafe.Pointer(pIncr + 40 + 1*16)) if (*(*TSorterFile)(unsafe.Pointer(pIncr + 40))).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(72)) } 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) } 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 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)*80 p2 = (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr + uintptr(i2)*80 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 /* 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 */ /* eMode is always INCRINIT_NORMAL in single-threaded mode */ /* Verify that the MergeEngine is assigned to a single thread */ (*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))*80) } else { rc = _vdbePmaReaderIncrInit(tls, (*TMergeEngine)(unsafe.Pointer(pMerger)).FaReadr+uintptr(i)*80, 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 */ 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+40) if rc == SQLITE_OK { rc = _vdbeSorterOpenTempFile(tls, db, int64(mxSz), pIncr+40+1*16) } } else { /*if( !pIncr->bUseThread )*/ if (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FpFd == uintptr(0) { rc = _vdbeSorterOpenTempFile(tls, db, (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FiEof, pTask+80) (*TSortSubtask)(unsafe.Pointer(pTask)).Ffile2.FiEof = 0 } if rc == SQLITE_OK { (*(*TSorterFile)(unsafe.Pointer(pIncr + 40 + 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. */ 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(int64(_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 { 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)*80 rc = _vdbePmaReaderInit(tls, pTask, pTask+64, 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)*80 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+72) } } 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))*80))).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. */ 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 + 96 + uintptr(iTask)*96 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)*80+72) } else { 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 + 96 *(*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 + 96 + uintptr(i)*96))).FxCompare = xCompare goto _1 _1: ; i++ } rc = _vdbeSorterMergeTreeBuild(tls, pSorter, bp) if rc == SQLITE_OK { if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUseThreads != 0 { pReadr = uintptr(0) pLast = pSorter + 96 + uintptr(int32((*TVdbeSorter)(unsafe.Pointer(pSorter)).FnTask)-int32(1))*96 rc = _vdbeSortAllocUnpacked(tls, pLast) if rc == SQLITE_OK { pReadr = _sqlite3DbMallocZero(tls, db, uint64(80)) (*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+72) 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)*80))).FpIncr pIncr = v3 if v3 != 0 { _vdbeIncrMergerSetThreads(tls, pIncr) } 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)*80 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 */ pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 48)) /* 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+96, pSorter+56) } 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. */ 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. */ 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 */ pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 48)) if (*TVdbeSorter)(unsafe.Pointer(pSorter)).FbUsePMA != 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 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 + 8)) *(*uintptr)(unsafe.Pointer(pFree + 8)) = 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)))*80 } *(*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)*16 } 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 */ pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 48)) 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.X__builtin___memcpy_chk(tls, (*TMem)(unsafe.Pointer(pOut)).Fz, pKey, uint64(*(*int32)(unsafe.Pointer(bp))), libc.X__builtin_object_size(tls, (*TMem)(unsafe.Pointer(pOut)).Fz, 0)) 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 */ pSorter = *(*uintptr)(unsafe.Pointer(pCsr + 48)) 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) } 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)*56))).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 { FpMethod uintptr FnChunkSize int32 FnSpill int32 FpFirst uintptr Fendpoint TFilePoint Freadpoint TFilePoint Fflags int32 FpVfs uintptr FzJournal uintptr } type MemJournal = TMemJournal type TFilePoint = struct { FiOffset Tsqlite3_int64 FpChunk uintptr } 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 { FiOffset Tsqlite3_int64 FpChunk uintptr } type FilePoint1 = TFilePoint1 /* ** This structure is a subclass of sqlite3_file. Each open memory-journal ** is an instance of this class. */ type TMemJournal1 = struct { FpMethod uintptr FnChunkSize int32 FnSpill int32 FpFirst uintptr Fendpoint TFilePoint Freadpoint TFilePoint Fflags int32 FpVfs uintptr FzJournal uintptr } 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.X__builtin___memset_chk(tls, p, 0, uint64(80), libc.X__builtin_object_size(tls, p, 0)) 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+8, 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. */ if iOfst > 0 && iOfst != (*TMemJournal)(unsafe.Pointer(p)).Fendpoint.FiOffset { _memjrnlTruncate(tls, pJfd, iOfst) } if iOfst == 0 && (*TMemJournal)(unsafe.Pointer(p)).FpFirst != 0 { libc.X__builtin___memcpy_chk(tls, (*TMemJournal)(unsafe.Pointer(p)).FpFirst+8, zBuf, uint64(iAmt), libc.X__builtin_object_size(tls, (*TMemJournal)(unsafe.Pointer(p)).FpFirst+8, 0)) } 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 if iChunkOffset == 0 { /* New chunk is required to extend the file. */ pNew = Xsqlite3_malloc(tls, int32(libc.Uint64FromInt64(16)+uint64((*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(uint64(libc.Int32FromInt32(8)+libc.Int32FromInt32(MEMJOURNAL_DFLT_FILECHUNKSIZE)) - libc.Uint64FromInt64(16)) } (*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(80) { v1 = (*Tsqlite3_vfs)(unsafe.Pointer(pVfs)).FszOsFile } else { v1 = libc.Int32FromInt64(80) } 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)) { 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 { pExpr = (*TExpr)(unsafe.Pointer(pExpr)).FpRight continue } else { if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) { if _sqlite3WalkSelect(tls, pWalker, *(*uintptr)(unsafe.Pointer(pExpr + 32))) != 0 { return int32(WRC_Abort) } } else { if *(*uintptr)(unsafe.Pointer(pExpr + 32)) != 0 { if _sqlite3WalkExprList(tls, pWalker, *(*uintptr)(unsafe.Pointer(pExpr + 32))) != 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(pExpr + 64)), 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 += 32 } } 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 + 60 + 4))&0x4>>2)) != 0 && _sqlite3WalkExprList(tls, pWalker, *(*uintptr)(unsafe.Pointer(pItem + 88))) != 0 { return int32(WRC_Abort) } goto _1 _1: ; i-- pItem += 104 } } 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(&(*TWalker)(unsafe.Pointer(pWalker)).Fu))) } return WRC_Continue } func _incrAggFunctionDepth(tls *libc.TLS, pExpr uintptr, N int32) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* w at bp+0 */ TWalker if N > 0 { libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_incrAggDepth) *(*int32)(unsafe.Pointer(bp + 40)) = 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(80) defer tls.Free(80) var db, pDup, pOrig uintptr var _ /* temp at bp+0 */ TExpr _, _, _ = db, pDup, pOrig /* The database connection */ pOrig = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(iCol)*32))).FpExpr 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) { pDup = _sqlite3ExprAddCollateString(tls, pParse, pDup, *(*uintptr)(unsafe.Pointer(pExpr + 8))) } libc.X__builtin___memcpy_chk(tls, bp, pDup, uint64(72), libc.X__builtin_object_size(tls, bp, 0)) libc.X__builtin___memcpy_chk(tls, pDup, pExpr, uint64(72), libc.X__builtin_object_size(tls, pDup, 0)) libc.X__builtin___memcpy_chk(tls, pExpr, bp, uint64(72), libc.X__builtin_object_size(tls, pExpr, 0)) if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { if *(*uintptr)(unsafe.Pointer(pExpr + 64)) != uintptr(0) { (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 64)))).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 + 16 + 4)) & 0x3 >> 0)) if eEName != int32(ENAME_TAB) && (eEName != int32(ENAME_ROWID) || pbRowid == uintptr(0)) { return 0 } zSpan = (*TExprList_item)(unsafe.Pointer(pItem)).FzEName n = 0 for { if !(*(*int8)(unsafe.Pointer(zSpan + uintptr(n))) != 0 && int32(*(*int8)(unsafe.Pointer(zSpan + uintptr(n)))) != int32('.')) { break } goto _1 _1: ; n++ } if zDb != 0 && (Xsqlite3_strnicmp(tls, zSpan, zDb, n) != 0 || int32(*(*int8)(unsafe.Pointer(zDb + uintptr(n)))) != 0) { return 0 } zSpan += uintptr(n + int32(1)) n = 0 for { if !(*(*int8)(unsafe.Pointer(zSpan + uintptr(n))) != 0 && int32(*(*int8)(unsafe.Pointer(zSpan + uintptr(n)))) != int32('.')) { break } goto _2 _2: ; n++ } if zTab != 0 && (Xsqlite3_strnicmp(tls, zSpan, zTab, n) != 0 || int32(*(*int8)(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) pExTab = *(*uintptr)(unsafe.Pointer(pExpr + 64)) if (*TTable)(unsafe.Pointer(pExTab)).FtabFlags&uint32(TF_HasGenerated) != uint32(0) && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pExTab)).FaCol + uintptr(n)*16))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { if int32((*TTable)(unsafe.Pointer(pExTab)).FnCol) >= int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(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.Uint64FromInt64(8)*libc.Uint64FromInt32(8)) { n = int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(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 *(*uintptr)(unsafe.Pointer(pNew + 64)) = (*TSrcItem)(unsafe.Pointer(pMatch)).FpTab *(*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 if Xsqlite3_strnicmp(tls, zTab, __ccgo_ts+6937, int32(7)) != 0 { return 0 } zLegacy = (*TTable)(unsafe.Pointer(pTab)).FzName if libc.Xstrcmp(tls, zLegacy+uintptr(7), __ccgo_ts+6945+7) == 0 { if _sqlite3StrICmp(tls, zTab+uintptr(7), __ccgo_ts+6964+7) == 0 { return int32(1) } if pSchema == uintptr(0) { return 0 } if _sqlite3StrICmp(tls, zTab+uintptr(7), __ccgo_ts+6478+7) == 0 { return int32(1) } if _sqlite3StrICmp(tls, zTab+uintptr(7), __ccgo_ts+6983+7) == 0 { return int32(1) } } else { if _sqlite3StrICmp(tls, zTab+uintptr(7), __ccgo_ts+6983+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, pRight uintptr, pNC uintptr, pExpr uintptr) (r int32) { bp := tls.Alloc(64) defer tls.Free(64) 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, zCol, zErr, v10 uintptr var hCol, hCol1 Tu8 var v7, v8 uint32 var _ /* bRowid at bp+8 */ 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, zCol, 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 */ zCol = *(*uintptr)(unsafe.Pointer(pRight + 8)) /* the name context cannot be NULL. */ /* The Z in X.Y.Z cannot be NULL */ /* 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 } if _sqlite3StrICmp(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32))).FzDbSName, zDb) == 0 { pSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32))).FpSchema break } goto _1 _1: ; i++ } if i == (*Tsqlite3)(unsafe.Pointer(db)).FnDb && _sqlite3StrICmp(tls, __ccgo_ts+6997, 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 */ 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 if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 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 pEList = (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSelect)).FpEList j = 0 for { if !(j < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } *(*int32)(unsafe.Pointer(bp + 8)) = 0 /* True if possible rowid match */ if !(_sqlite3MatchEName(tls, pEList+8+uintptr(j)*32, zCol, zTab, zDb, bp+8) != 0) { goto _3 } if *(*int32)(unsafe.Pointer(bp + 8)) == 0 { if cnt > 0 { if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 4))&0x400>>10)) == 0 || _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pItem + 72)), 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)*32+16+4, libc.Uint32FromInt32(1), 6, 0x40) /* rowid cannot be part of a USING clause - assert() this. */ if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(j)*32 + 16 + 4))&0x80>>7)) != 0 { break } goto _3 _3: ; j++ } if hit != 0 || zTab == uintptr(0) { goto _2 } } 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+7002) != 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 } } } if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && (*TSrcItem)(unsafe.Pointer(pItem)).FzAlias != 0 { _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), pExpr+64) } } 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 + 60 + 4))&0x400>>10)) == 0 || _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pItem + 72)), 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 + 60 + 4))&0x2000>>13)) != 0 { _sqlite3SrcItemColumnUsed(tls, pItem, j) } break } goto _4 _4: ; j++ pCol += 16 } 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 += 104 } if pMatch != 0 { (*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TSrcItem)(unsafe.Pointer(pMatch)).FiCursor *(*uintptr)(unsafe.Pointer(pExpr + 64)) = (*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(*(*uintptr)(unsafe.Pointer(pExpr + 64)))).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) 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 || _isValidSchemaTableName(tls, zTab, (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab, uintptr(0)) != 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+7004, 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+7008, 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 + 16)) if pUpsert != 0 && _sqlite3StrICmp(tls, __ccgo_ts+7012, 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 += 16 } 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) { if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(iCol) *(*uintptr)(unsafe.Pointer(pExpr + 64)) = pTab eNewExprOp = int32(TK_COLUMN) } else { (*TExpr)(unsafe.Pointer(pExpr)).FiTable = (*TUpsert)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNC + 16)))).FregData + int32(_sqlite3TableColumnToStorage(tls, pTab, int16(iCol))) eNewExprOp = int32(TK_REGISTER) } } else { *(*uintptr)(unsafe.Pointer(pExpr + 64)) = 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(&(*TNameContext)(unsafe.Pointer(pNC)).FuNC)) + (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 = int8(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 + 216)) |= v7 } else { if iCol >= int32(32) { v8 = uint32(0xffffffff) } else { v8 = libc.Uint32FromInt32(1) << iCol } *(*Tu32)(unsafe.Pointer(pParse + 220)) |= 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 + 60 + 4))&0x2000>>13)) != 0) { cnt = cntTab if int32(uint32(*(*uint16)(unsafe.Pointer(pMatch + 60 + 4))&0x2000>>13)) == 0 { (*TExpr)(unsafe.Pointer(pExpr)).FiColumn = int16(-int32(1)) } (*TExpr)(unsafe.Pointer(pExpr)).FaffExpr = int8(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 + 16)) j = 0 for { if !(j < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } zAs = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(j)*32))).FzEName if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(j)*32 + 16 + 4))&0x3>>0)) == ENAME_NAME && Xsqlite3_stricmp(tls, zAs, zCol) == 0 { pOrig = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(j)*32))).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+7021, libc.VaList(bp+24, 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+7052, libc.VaList(bp+24, zAs)) return int32(WRC_Abort) } if _sqlite3ExprVectorSize(tls, pOrig) != int32(1) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7089, 0) return int32(WRC_Abort) } _resolveAlias(tls, pParse, pEList, j, pExpr, nSubquery) cnt = int32(1) pMatch = uintptr(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) { 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+7107, libc.VaList(bp+24, zCol)) (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_STRING) libc.X__builtin___memset_chk(tls, pExpr+64, 0, uint64(8), libc.X__builtin_object_size(tls, pExpr+64, 0)) 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. */ 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 + 7142 *(*uintptr)(unsafe.Pointer(pExpr + 32)) = *(*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 + 7151 } else { v10 = __ccgo_ts + 7166 } zErr = v10 if zDb != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7188, libc.VaList(bp+24, zErr, zDb, zTab, zCol)) } else { if zTab != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7201, libc.VaList(bp+24, zErr, zTab, zCol)) } else { if cnt == 0 && (*TExpr)(unsafe.Pointer(pRight)).Fflags&uint32(libc.Int32FromInt32(EP_DblQuoted)) != uint32(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7211, libc.VaList(bp+24, zErr, zCol)) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7272, libc.VaList(bp+24, 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) } /* 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 pMatch != 0 { if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) >= 0 { *(*TBitmask)(unsafe.Pointer(pMatch + 80)) |= _sqlite3ExprColUsed(tls, pExpr) } else { libc.SetBitFieldPtr16Uint32(pMatch+60+4, libc.Uint32FromInt32(1), 14, 0x4000) } } (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(eNewExprOp) goto lookupname_end lookupname_end: ; if cnt == int32(1) { 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 { (*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)*104 v1 = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab *(*uintptr)(unsafe.Pointer(p + 64)) = v1 pTab = v1 (*TExpr)(unsafe.Pointer(p)).FiTable = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor if int32((*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 64)))).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)*16))).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.Uint64FromInt64(8)*libc.Uint64FromInt32(8)) { v3 = int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(8)) - libc.Int32FromInt32(1) } else { v3 = iCol } *(*TBitmask)(unsafe.Pointer(pItem + 80)) |= 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 + 7279 if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_IdxExpr) != 0 { zIn = __ccgo_ts + 7307 } else { if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_IsCheck) != 0 { zIn = __ccgo_ts + 7325 } else { if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&int32(NC_GenCol) != 0 { zIn = __ccgo_ts + 7343 } } } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7361, 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) } _sqlite3AtoF(tls, *(*uintptr)(unsafe.Pointer(p + 8)), bp, _sqlite3Strlen30(tls, *(*uintptr)(unsafe.Pointer(p + 8))), uint8(SQLITE_UTF8)) 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, 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, zDb, zId, zTable, zType, v12, v4, v5, v6, v7, v8, p10, p11, p9 pNC = *(*uintptr)(unsafe.Pointer(pWalker + 40)) pParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse 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 pItem = pSrcList + 8 (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_COLUMN) *(*uintptr)(unsafe.Pointer(pExpr + 64)) = (*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 = int8(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.Uint64FromInt64(32)/libc.Uint64FromInt64(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++ } *(*int32)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pExpr)).Fu)) = 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.Uint64FromInt64(32)/libc.Uint64FromInt64(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) pRight = pExpr } else { pLeft = (*TExpr)(unsafe.Pointer(pExpr)).FpLeft if (*TNameContext)(unsafe.Pointer(pNC)).FncFlags&(libc.Int32FromInt32(NC_IdxExpr)|libc.Int32FromInt32(NC_GenCol)) != 0 { _notValidImpl(tls, pParse, pNC, __ccgo_ts+7381, uintptr(0), pExpr) } pRight = (*TExpr)(unsafe.Pointer(pExpr)).FpRight if int32((*TExpr)(unsafe.Pointer(pRight)).Fop) == int32(TK_ID) { zDb = uintptr(0) } else { zDb = *(*uintptr)(unsafe.Pointer(pLeft + 8)) pLeft = (*TExpr)(unsafe.Pointer(pRight)).FpLeft pRight = (*TExpr)(unsafe.Pointer(pRight)).FpRight } zTable = *(*uintptr)(unsafe.Pointer(pLeft + 8)) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenRemap(tls, pParse, pExpr, pRight) _sqlite3RenameTokenRemap(tls, pParse, pExpr+64, pLeft) } } return _lookupName(tls, pParse, zDb, zTable, pRight, pNC, pExpr) /* Resolve function names */ fallthrough case int32(TK_FUNCTION): pList = *(*uintptr)(unsafe.Pointer(pExpr + 32)) 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(pExpr + 64)))).FeFrmType) != int32(TK_FILTER) { v5 = *(*uintptr)(unsafe.Pointer(pExpr + 64)) } else { v5 = uintptr(0) } pWin = v5 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*32))).FpExpr) if (*TExpr)(unsafe.Pointer(pExpr)).FiTable < 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7398, 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(*(*int8)(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+7462, 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. */ 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+7498, uintptr(0), pExpr) } } else { /* 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)) { if pDef != 0 && (*TFuncDef)(unsafe.Pointer(pDef)).FxValue == uintptr(0) && pWin != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7526, 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 + 7569 } else { zType = __ccgo_ts + 7576 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7586, 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+7614, libc.VaList(bp+8, pExpr)) (*TNameContext)(unsafe.Pointer(pNC)).FnNcErr++ } else { if wrong_num_args != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7636, 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+7680, 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 + 40)) &= ^(libc.Int32FromInt32(NC_AllowWin) | v7) } } else { if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) || (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 { is_agg = int32(1) } } _sqlite3WalkExprList(tls, pWalker, pList) if is_agg != 0 { if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 { _sqlite3WalkExprList(tls, pWalker, *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 32))) } if pWin != 0 { pSel = (*TNameContext)(unsafe.Pointer(pNC)).FpWinSelect 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 + 40)) |= 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(pExpr + 64)))).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 } if pNC2 != 0 && pDef != 0 { p10 = pExpr + 2 *(*Tu8)(unsafe.Pointer(p10)) = Tu8(uint32(*(*Tu8)(unsafe.Pointer(p10))) + (*TNameContext)(unsafe.Pointer(pNC2)).FnNestedSelect) p11 = pNC2 + 40 *(*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 + 40)) |= 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+7728, pExpr, pExpr) } else { _sqlite3WalkSelect(tls, pWalker, *(*uintptr)(unsafe.Pointer(pExpr + 32))) } if nRef != (*TNameContext)(unsafe.Pointer(pNC)).FnRef { *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_VarSelect)) *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)) + 4)) |= uint32(SF_Correlated) } *(*int32)(unsafe.Pointer(pNC + 40)) |= int32(NC_Subquery) } case int32(TK_VARIABLE): 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+7739, pExpr, pExpr) } case int32(TK_IS): fallthrough case int32(TK_ISNOT): pRight1 = _sqlite3ExprSkipCollateAndLikely(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) /* 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 } nLeft = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_BETWEEN) { nRight = _sqlite3ExprVectorSize(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)) + 8))).FpExpr) if nRight == nLeft { nRight = _sqlite3ExprVectorSize(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)) + 8 + 1*32))).FpExpr) } } else { nRight = _sqlite3ExprVectorSize(tls, (*TExpr)(unsafe.Pointer(pExpr)).FpRight) } if nLeft != nRight { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7089, 0) _sqlite3RecordErrorOffsetOfExpr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr) } break } 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) { 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)*32 + 16 + 4))&0x3>>0)) == ENAME_NAME && Xsqlite3_stricmp(tls, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*32))).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(64) defer tls.Free(64) 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 */ pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList /* Resolve all names in the ORDER BY term expression */ libc.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) (*(*TNameContext)(unsafe.Pointer(bp))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp))).FpSrcList = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc *(*uintptr)(unsafe.Pointer(bp + 16)) = 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)*32))).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+7750, 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 + 136 + 2*4)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7806, 0) return int32(1) } i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr) { break } libc.SetBitFieldPtr16Uint32(pOrderBy+8+uintptr(i)*32+16+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 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 + 16 + 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+7840, 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) { *(*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(&(*TExpr)(unsafe.Pointer(pNew)).Fu)) = *(*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 for int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pParent)).FpLeft)).Fop) == int32(TK_COLLATE) { pParent = (*TExpr)(unsafe.Pointer(pParent)).FpLeft } (*TExpr)(unsafe.Pointer(pParent)).FpLeft = pNew } _sqlite3ExprDelete(tls, db, pE) (*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 24))).FiOrderByCol = uint16(*(*int32)(unsafe.Pointer(bp))) } libc.SetBitFieldPtr16Uint32(pItem+16+4, libc.Uint32FromInt32(1), 2, 0x4) } else { moreToDo = int32(1) } goto _2 _2: ; i++ pItem += 32 } 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)*32 + 16 + 4))&0x4>>2)) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7846, 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 + 136 + 2*4)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7907, libc.VaList(bp+8, zType)) return int32(1) } pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList /* 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 + 24))).FiOrderByCol != 0 { if int32((*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 24))).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 + 24))).FiOrderByCol)-int32(1), (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr, 0) } goto _1 _1: ; i++ pItem += 32 } 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(pExpr + 64)) _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(48) defer tls.Free(48) var _ /* sWalker at bp+0 */ TWalker if (*TSelect)(unsafe.Pointer(pSelect)).FpWin != 0 { libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_resolveRemoveWindowsCb) *(*uintptr)(unsafe.Pointer(bp + 40)) = 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 */ 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(*(*int8)(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 + 24))).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 + 24))).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 + 24))).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)*32))).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 + 24))).FiOrderByCol = uint16(j + int32(1)) } goto _2 _2: ; j++ } goto _1 _1: ; i++ pItem += 32 } 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(64) defer tls.Free(64) 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 */ if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Resolved) != 0 { return int32(WRC_Prune) } pOuterNC = *(*uintptr)(unsafe.Pointer(pWalker + 40)) 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 { *(*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.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) (*(*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 (*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)*104 /* Test of tag-20240424-1*/ 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) } /* 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.SetBitFieldPtr16Uint32(pItem+60+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. */ pGroupBy = (*TSelect)(unsafe.Pointer(p)).FpGroupBy if pGroupBy != 0 || (*(*TNameContext)(unsafe.Pointer(bp))).FncFlags&int32(NC_HasAgg) != 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. */ *(*uintptr)(unsafe.Pointer(bp + 16)) = (*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+7938, 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)*104 if int32(uint32(*(*uint16)(unsafe.Pointer(pItem1 + 60 + 4))&0x4>>2)) != 0 && _sqlite3ResolveExprListNames(tls, bp, *(*uintptr)(unsafe.Pointer(pItem1 + 88))) != 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+7840) != 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+7977) != 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+7983, 0) return int32(WRC_Abort) } goto _6 _6: ; i++ pItem2 += 32 } } /* 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(48) defer tls.Free(48) 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 + 40)) &= ^(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 + 40)) = pNC *(*int32)(unsafe.Pointer((*(*TWalker)(unsafe.Pointer(bp))).FpParse + 316)) += (*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) } _sqlite3WalkExprNN(tls, bp, pExpr) *(*int32)(unsafe.Pointer((*(*TWalker)(unsafe.Pointer(bp))).FpParse + 316)) -= (*TExpr)(unsafe.Pointer(pExpr)).FnHeight *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32((*TNameContext)(unsafe.Pointer(pNC)).FncFlags & (libc.Int32FromInt32(NC_HasAgg) | libc.Int32FromInt32(NC_HasWin))) *(*int32)(unsafe.Pointer(pNC + 40)) |= 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(48) defer tls.Free(48) 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 + 40)) = 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 + 40)) &= ^(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)*32))).FpExpr if pExpr == uintptr(0) { goto _1 } *(*int32)(unsafe.Pointer((*(*TWalker)(unsafe.Pointer(bp))).FpParse + 316)) += (*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 + 316)) -= (*TExpr)(unsafe.Pointer(pExpr)).FnHeight 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 + 40)) &= ^(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 + 40)) |= 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(48) defer tls.Free(48) var _ /* w at bp+0 */ TWalker (*(*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 + 40)) = 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(176) defer tls.Free(176) var rc, v1 int32 var _ /* sNC at bp+112 */ TNameContext var _ /* sSrc at bp+0 */ TSrcList _, _ = rc, v1 libc.X__builtin___memset_chk(tls, bp+112, 0, uint64(56), libc.X__builtin_object_size(tls, bp+112, 0)) libc.X__builtin___memset_chk(tls, bp, 0, uint64(112), libc.X__builtin_object_size(tls, bp, 0)) 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*32))).FpSchema { /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP ** schema elements */ type1 |= int32(NC_FromDDL) } } (*(*TNameContext)(unsafe.Pointer(bp + 112))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp + 112))).FpSrcList = bp (*(*TNameContext)(unsafe.Pointer(bp + 112))).FncFlags = type1 | int32(NC_IsDDL) v1 = _sqlite3ResolveExprNames(tls, bp+112, pExpr) rc = v1 if v1 != SQLITE_OK { return rc } if pList != 0 { rc = _sqlite3ResolveExprListNames(tls, bp+112, 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 int8) { if iCol < 0 || iCol >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { return int8(SQLITE_AFF_INTEGER) } return (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*16))).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 int8) { 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) && *(*uintptr)(unsafe.Pointer(pExpr + 64)) != uintptr(0) { return _sqlite3TableColumnAffinity(tls, *(*uintptr)(unsafe.Pointer(pExpr + 64)), int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)) } if op == int32(TK_SELECT) { return _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).FpEList + 8))).FpExpr) } if op == int32(TK_CAST) { return _sqlite3AffinityType(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), uintptr(0)) } if op == int32(TK_SELECT_COLUMN) { return _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 32)))).FpEList + 8 + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)*32))).FpExpr) } if op == int32(TK_VECTOR) { return _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)) + 8))).FpExpr) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Skip)|libc.Int32FromInt32(EP_IfNullRow)) != uint32(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 + 32)) ii = int32(1) for { if !(ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } res |= _sqlite3ExprDataType(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*32))).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))*32))).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 _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) { 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) { pExpr = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)) + 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) && *(*uintptr)(unsafe.Pointer(p + 64)) != uintptr(0) || op == int32(TK_COLUMN) || op == int32(TK_TRIGGER) { v1 = int32((*TExpr)(unsafe.Pointer(p)).FiColumn) j = v1 if v1 >= 0 { zColl = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 64)))).FaCol+uintptr(j)*16) 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) { p = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 32)) + 8))).FpExpr continue } if op == int32(TK_COLLATE) { 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 */ if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) == uint32(0) && *(*uintptr)(unsafe.Pointer(p + 32)) != uintptr(0) && !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 32)))).FnExpr) { break } if (*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 32)) + 8 + uintptr(i)*32))).FpExpr)).Fflags&uint32(libc.Int32FromInt32(EP_Collate)) != uint32(0) { pNext = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 32)) + 8 + uintptr(i)*32))).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 } 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 int8) (r int8) { var aff1 int8 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 int8(SQLITE_AFF_NUMERIC) } else { return int8(SQLITE_AFF_BLOB) } } else { /* One side is a column, the other is not. Use the columns affinity. */ if int32(aff1) <= int32(SQLITE_AFF_NONE) { v1 = int32(aff2) } else { v1 = int32(aff1) } return int8(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 int8) { var aff int8 _ = aff 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 + 32)))).FpEList + 8))).FpExpr, aff) } else { if int32(aff) == 0 { aff = int8(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 int8) (r int32) { var aff int8 _ = 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 = uint8(_sqlite3ExprAffinity(tls, pExpr2)) aff = uint8(int32(uint8(_sqlite3CompareAffinity(tls, pExpr1, int8(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 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) { return (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).FnExpr } else { if int32(op) == int32(TK_SELECT) { return (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).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) { if _sqlite3ExprIsVector(tls, pVector) != 0 { if int32((*TExpr)(unsafe.Pointer(pVector)).Fop) == int32(TK_SELECT) || int32((*TExpr)(unsafe.Pointer(pVector)).Fop2) == int32(TK_SELECT) { return (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pVector + 32)))).FpEList + 8 + uintptr(i)*32))).FpExpr } else { return (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pVector + 32)) + 8 + uintptr(i)*32))).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) { /* 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) { ppVector = *(*uintptr)(unsafe.Pointer(pVector + 32)) + 8 + uintptr(iField)*32 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 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) { *(*uintptr)(unsafe.Pointer(ppExpr)) = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pVector + 32)))).FpEList + 8 + uintptr(iField)*32))).FpExpr return regSelect + iField } if int32(op) == int32(TK_VECTOR) { *(*uintptr)(unsafe.Pointer(ppExpr)) = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pVector + 32)) + 8 + uintptr(iField)*32))).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(32) defer tls.Free(32) 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+16 */ 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)) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return } if nLeft != _sqlite3ExprVectorSize(tls, pRight) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7089, 0) return } 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 + 16)) = uintptr(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+16, bp+4) addrCmp = _sqlite3VdbeCurrentAddr(tls, v) _codeCompare(tls, pParse, *(*uintptr)(unsafe.Pointer(bp + 8)), *(*uintptr)(unsafe.Pointer(bp + 16)), 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 { _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 + 136 + 3*4)) if nHeight > mxHeight { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8042, 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)*32))).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 + 32)), bp) } else { if *(*uintptr)(unsafe.Pointer(p + 32)) != 0 { _heightOfExprList(tls, *(*uintptr)(unsafe.Pointer(p + 32)), 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 + 32))) } } (*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 + 52)) = 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 (tag-20240227-a): 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. // ** See also tag-20240227-b. // */ 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 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)) /* tag-20240227-a */ } } pNew = _sqlite3DbMallocRawNN(tls, db, uint64(72)+uint64(nExtra)) if pNew != 0 { libc.X__builtin___memset_chk(tls, pNew, 0, uint64(72), libc.X__builtin_object_size(tls, pNew, 0)) (*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(&(*TExpr)(unsafe.Pointer(pNew)).Fu)) = *(*int32)(unsafe.Pointer(bp)) } else { *(*uintptr)(unsafe.Pointer(pNew + 8)) = pNew + 1*72 if (*TToken)(unsafe.Pointer(pToken)).Fn != 0 { libc.X__builtin___memcpy_chk(tls, *(*uintptr)(unsafe.Pointer(pNew + 8)), (*TToken)(unsafe.Pointer(pToken)).Fz, uint64((*TToken)(unsafe.Pointer(pToken)).Fn), libc.X__builtin_object_size(tls, *(*uintptr)(unsafe.Pointer(pNew + 8)), 0)) } *(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNew + 8)) + uintptr((*TToken)(unsafe.Pointer(pToken)).Fn))) = 0 if dequote != 0 && int32(_sqlite3CtypeMap[uint8(*(*int8)(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) { _sqlite3ExprDelete(tls, db, pLeft) _sqlite3ExprDelete(tls, db, pRight) } else { 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(72)) if p != 0 { libc.X__builtin___memset_chk(tls, p, 0, uint64(72), libc.X__builtin_object_size(tls, p, 0)) (*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 + 32)) = pSelect *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_xIsSelect) | libc.Int32FromInt32(EP_Subquery)) _sqlite3ExprSetHeightAndFlags(tls, pParse, pExpr) } else { _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) ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } pExpr = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(ii)*32))).FpExpr if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_VECTOR) { nExprElem = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).FnExpr } else { nExprElem = int32(1) } if nExprElem != nElem { if nExprElem > int32(1) { v2 = __ccgo_ts + 8090 } else { v2 = __ccgo_ts + 1664 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8092, libc.VaList(bp+8, nExprElem, v2, nElem)) break } pSel = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(pExpr + 32)), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(SF_Values), uintptr(0)) *(*uintptr)(unsafe.Pointer(pExpr + 32)) = 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+1784) } 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 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) } *(*int32)(unsafe.Pointer(pNew + 52)) = int32(int64((*TToken)(unsafe.Pointer(pToken)).Fz) - int64((*TParse)(unsafe.Pointer(pParse)).FzTail)) if pList != 0 && (*TExprList)(unsafe.Pointer(pList)).FnExpr > *(*int32)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb + 136 + 6*4)) && !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8136, libc.VaList(bp+8, pToken)) } *(*uintptr)(unsafe.Pointer(pNew + 32)) = pList *(*Tu32)(unsafe.Pointer(pNew + 4)) |= uint32(libc.Int32FromInt32(EP_HasFunc)) _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+8170, 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) { return } if pExpr == uintptr(0) { _sqlite3ExprListDelete(tls, db, pOrderBy) return } if *(*uintptr)(unsafe.Pointer(pExpr + 32)) == uintptr(0) || (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).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(pExpr + 64)))).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 + 32)) = pOrderBy (*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) 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+8220, 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 } z = *(*uintptr)(unsafe.Pointer(pExpr + 8)) if int32(*(*int8)(unsafe.Pointer(z + 1))) == 0 { /* Wildcard of the form "?". Assign the next variable number */ v2 = pParse + 304 *(*TynVar)(unsafe.Pointer(v2))++ v1 = *(*TynVar)(unsafe.Pointer(v2)) x = v1 } else { doAdd = 0 if int32(*(*int8)(unsafe.Pointer(z))) == int32('?') { if n == uint32(2) { /*OPTIMIZATION-IF-TRUE*/ *(*Ti64)(unsafe.Pointer(bp)) = int64(int32(*(*int8)(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 + 136 + 9*4))) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8240, libc.VaList(bp+16, *(*int32)(unsafe.Pointer(db + 136 + 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 + 304 *(*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 + 136 + 9*4)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8283, 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) { var pLeft uintptr _ = pLeft goto exprDeleteRestart exprDeleteRestart: ; 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 */ if (*TExpr)(unsafe.Pointer(p)).FpRight != 0 { _sqlite3ExprDeleteNN(tls, db, (*TExpr)(unsafe.Pointer(p)).FpRight) } else { if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) != uint32(0) { _sqlite3SelectDelete(tls, db, *(*uintptr)(unsafe.Pointer(p + 32))) } else { _sqlite3ExprListDelete(tls, db, *(*uintptr)(unsafe.Pointer(p + 32))) if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { _sqlite3WindowDelete(tls, db, *(*uintptr)(unsafe.Pointer(p + 64))) } } } if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 && int32((*TExpr)(unsafe.Pointer(p)).Fop) != int32(TK_SELECT_COLUMN) { pLeft = (*TExpr)(unsafe.Pointer(p)).FpLeft if !((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_Static)) != libc.Uint32FromInt32(0)) && !((*TExpr)(unsafe.Pointer(pLeft)).Fflags&uint32(libc.Int32FromInt32(EP_Static)) != libc.Uint32FromInt32(0)) { /* Avoid unnecessary recursion on unary operators */ _sqlite3DbNNFreeNN(tls, db, p) p = pLeft goto exprDeleteRestart } else { _sqlite3ExprDeleteNN(tls, db, pLeft) } } } 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. // ** // ** Return 0 if the delete was successfully deferred. Return non-zero // ** if the delete happened immediately because of an OOM. // */ func _sqlite3ExprDeferredDelete(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r int32) { return libc.BoolInt32(uintptr(0) == _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(uint64(libc.UintptrFromInt32(0) + 16)) } if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_Reduced)) != uint32(0) { return int32(uint64(libc.UintptrFromInt32(0) + 44)) } return int32(72) } // 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 /* Only one flag value allowed */ if 0 == flags || (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_FullSize)) != uint32(0) { nSize = int32(72) } else { if (*TExpr)(unsafe.Pointer(p)).FpLeft != 0 || *(*uintptr)(unsafe.Pointer(p + 32)) != 0 { nSize = int32(uint64(libc.UintptrFromInt32(0)+44) | libc.Uint64FromInt32(EP_Reduced)) } else { nSize = int32(uint64(libc.UintptrFromInt32(0)+16) | libc.Uint64FromInt32(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(uint64(nByte) + (libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(p + 8)))&libc.Uint64FromInt32(0x3fffffff) + libc.Uint64FromInt32(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 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) } 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 */ /* 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) } 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)))&uint64(0x3fffffff) + uint64(1)) nAlloc = int32((libc.Uint64FromInt64(72) + uint64(nToken) + libc.Uint64FromInt32(7)) & uint64(^libc.Int32FromInt32(7))) } else { nToken = 0 nAlloc = int32((libc.Uint64FromInt64(72) + libc.Uint64FromInt32(7)) & uint64(^libc.Int32FromInt32(7))) } } (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc = _sqlite3DbMallocRawNN(tls, db, uint64(nAlloc)) staticFlag = uint32(0) } pNew = (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc 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.X__builtin___memcpy_chk(tls, (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc, p, uint64(nNewSize), libc.X__builtin_object_size(tls, (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc, 0)) } else { nSize = uint32(_exprStructSize(tls, p)) libc.X__builtin___memcpy_chk(tls, (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc, p, uint64(nSize), libc.X__builtin_object_size(tls, (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc, 0)) if uint64(nSize) < uint64(72) { libc.X__builtin___memset_chk(tls, (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc+uintptr(nSize), 0, uint64(72)-uint64(nSize), libc.X__builtin_object_size(tls, (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc+uintptr(nSize), 0)) } nNewSize = int32(72) } /* 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. */ if nToken > 0 { v1 = (*(*TEdupBuf)(unsafe.Pointer(bp))).FzAlloc + uintptr(nNewSize) *(*uintptr)(unsafe.Pointer(pNew + 8)) = v1 zToken = v1 libc.X__builtin___memcpy_chk(tls, zToken, *(*uintptr)(unsafe.Pointer(p + 8)), uint64(nToken), libc.X__builtin_object_size(tls, zToken, 0)) 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 + 32)) = _sqlite3SelectDup(tls, db, *(*uintptr)(unsafe.Pointer(p + 32)), dupFlags) } else { if int32((*TExpr)(unsafe.Pointer(p)).Fop) != int32(TK_ORDER) { v2 = dupFlags } else { v2 = 0 } *(*uintptr)(unsafe.Pointer(pNew + 32)) = _sqlite3ExprListDup(tls, db, *(*uintptr)(unsafe.Pointer(p + 32)), v2) } if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { *(*uintptr)(unsafe.Pointer(pNew + 64)) = _sqlite3WindowDup(tls, db, pNew, *(*uintptr)(unsafe.Pointer(p + 64))) } /* 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 } 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 } 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.X__builtin___memcpy_chk(tls, pEdupBuf, bp, uint64(8), libc.X__builtin_object_size(tls, pEdupBuf, 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(uint64(64) + uint64(48)*uint64((*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 + 16 + uintptr(i)*48))).FpSelect = _sqlite3SelectDup(tls, db, (*(*TCte)(unsafe.Pointer(p + 16 + uintptr(i)*48))).FpSelect, 0) (*(*TCte)(unsafe.Pointer(pRet + 16 + uintptr(i)*48))).FpCols = _sqlite3ExprListDup(tls, db, (*(*TCte)(unsafe.Pointer(p + 16 + uintptr(i)*48))).FpCols, 0) (*(*TCte)(unsafe.Pointer(pRet + 16 + uintptr(i)*48))).FzName = _sqlite3DbStrDup(tls, db, (*(*TCte)(unsafe.Pointer(p + 16 + uintptr(i)*48))).FzName) (*(*TCte)(unsafe.Pointer(pRet + 16 + uintptr(i)*48))).FeM10d = (*(*TCte)(unsafe.Pointer(p + 16 + uintptr(i)*48))).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 + 40)) pWin = *(*uintptr)(unsafe.Pointer(pExpr + 64)) _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 + 40)) { v1 = WRC_Continue } else { v1 = int32(WRC_Prune) } return v1 } func _gatherSelectWindows(tls *libc.TLS, p uintptr) { bp := tls.Alloc(48) defer tls.Free(48) 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 + 40)) = 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 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) 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+16+4, libc.Uint32FromInt32(0), 2, 0x4) (*TExprList_item)(unsafe.Pointer(pItem)).Fu = (*TExprList_item)(unsafe.Pointer(pOldItem)).Fu goto _1 _1: ; i++ pItem += 32 pOldItem += 32 } 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 uint64 var v2 Tu32 _, _, _, _, _, _, _, _, _ = i, nByte, pNew, pNewItem, pOldItem, pTab, v1, v2, v4 if p == uintptr(0) { return uintptr(0) } if (*TSrcList)(unsafe.Pointer(p)).FnSrc > 0 { v1 = uint64(104) * uint64((*TSrcList)(unsafe.Pointer(p)).FnSrc-libc.Int32FromInt32(1)) } else { v1 = uint64(0) } nByte = int32(uint64(112) + 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)*104 pOldItem = p + 8 + uintptr(i)*104 (*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 (*TSrcItem)(unsafe.Pointer(pNewItem)).FregResult = (*TSrcItem)(unsafe.Pointer(pOldItem)).FregResult if int32(uint32(*(*uint16)(unsafe.Pointer(pNewItem + 60 + 4))&0x2>>1)) != 0 { *(*uintptr)(unsafe.Pointer(pNewItem + 88)) = _sqlite3DbStrDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 88))) } else { if int32(uint32(*(*uint16)(unsafe.Pointer(pNewItem + 60 + 4))&0x4>>2)) != 0 { *(*uintptr)(unsafe.Pointer(pNewItem + 88)) = _sqlite3ExprListDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 88)), flags) } else { *(*Tu32)(unsafe.Pointer(&(*TSrcItem)(unsafe.Pointer(pNewItem)).Fu1)) = *(*Tu32)(unsafe.Pointer(&(*TSrcItem)(unsafe.Pointer(pOldItem)).Fu1)) } } (*TSrcItem)(unsafe.Pointer(pNewItem)).Fu2 = (*TSrcItem)(unsafe.Pointer(pOldItem)).Fu2 if int32(uint32(*(*uint16)(unsafe.Pointer(pNewItem + 60 + 4))&0x100>>8)) != 0 { (*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNewItem + 96)))).FnUse++ } 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 + 60 + 4))&0x400>>10)) != 0 { *(*uintptr)(unsafe.Pointer(pNewItem + 72)) = _sqlite3IdListDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 72))) } else { *(*uintptr)(unsafe.Pointer(pNewItem + 72)) = _sqlite3ExprDup(tls, db, *(*uintptr)(unsafe.Pointer(pOldItem + 72)), 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 if p == uintptr(0) { return uintptr(0) } pNew = _sqlite3DbMallocRawNN(tls, db, uint64(24)+uint64((*TIdList)(unsafe.Pointer(p)).FnId-libc.Int32FromInt32(1))*uint64(16)) 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)*16 pOldItem = p + 8 + uintptr(i)*16 (*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 p = pDup for { if !(p != 0) { break } pNew = _sqlite3DbMallocRawNN(tls, db, uint64(128)) 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 + 80 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, libc.Uint64FromInt64(40)+libc.Uint64FromInt64(32)*libc.Uint64FromInt32(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(40)+uint64((*TExprList)(unsafe.Pointer(pList)).FnAlloc-libc.Int32FromInt32(1))*uint64(32)) 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)*32 *(*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)*32 *(*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+8306, 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) if pSubExpr == uintptr(0) { goto _4 } pList = _sqlite3ExprListAppend(tls, pParse, pList, pSubExpr) if pList != 0 { (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr((*TExprList)(unsafe.Pointer(pList)).FnExpr-int32(1))*32))).FzEName = (*(*TIdList_item)(unsafe.Pointer(pColumns + 8 + uintptr(i)*16))).FzName (*(*TIdList_item)(unsafe.Pointer(pColumns + 8 + uintptr(i)*16))).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)*32))).FpExpr /* 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 } pItem = p + 8 + uintptr((*TExprList)(unsafe.Pointer(p)).FnExpr-int32(1))*32 if iSortOrder == -int32(1) { iSortOrder = SQLITE_SO_ASC } (*TExprList_item)(unsafe.Pointer(pItem)).Ffg.FsortFlags = uint8(iSortOrder) if eNulls != -int32(1) { libc.SetBitFieldPtr16Uint32(pItem+16+4, libc.Uint32FromInt32(1), 5, 0x20) if iSortOrder != eNulls { p1 = pItem + 16 *(*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 if pList != 0 { pItem = pList + 8 + uintptr((*TExprList)(unsafe.Pointer(pList)).FnExpr-int32(1))*32 (*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 if pList != 0 { pItem = pList + 8 + uintptr((*TExprList)(unsafe.Pointer(pList)).FnExpr-int32(1))*32 if (*TExprList_item)(unsafe.Pointer(pItem)).FzEName == uintptr(0) { (*TExprList_item)(unsafe.Pointer(pItem)).FzEName = _sqlite3DbSpanDup(tls, db, zStart, zEnd) libc.SetBitFieldPtr16Uint32(pItem+16+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 + 136 + 2*4)) if pEList != 0 && (*TExprList)(unsafe.Pointer(pEList)).FnExpr > mx { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8336, 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 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 += 32 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) i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } pExpr = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*32))).FpExpr 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+8359) == 0 { return uint32(EP_IsTrue) } if _sqlite3StrICmp(tls, zIn, __ccgo_ts+8364) == 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 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) return libc.BoolInt32(int32(*(*int8)(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 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 // // /* // ** pExpr is a TK_FUNCTION node. Try to determine whether or not the // ** function is a constant function. A function is constant if all of // ** the following are true: // ** // ** (1) It is a scalar function (not an aggregate or window function) // ** (2) It has either the SQLITE_FUNC_CONSTANT or SQLITE_FUNC_SLOCHNG // ** property. // ** (3) All of its arguments are constants // ** // ** This routine sets pWalker->eCode to 0 if pExpr is not a constant. // ** It makes no changes to pWalker->eCode if pExpr is constant. In // ** every case, it returns WRC_Abort. // ** // ** Called as a service subroutine from exprNodeIsConstant(). // */ func _exprNodeIsConstantFunction(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var db, pDef, pList, v1 uintptr var n int32 var v2 bool _, _, _, _, _, _ = db, n, pDef, pList, v1, v2 /* The database */ if v2 = (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_TokenOnly)) != uint32(0); !v2 { v1 = *(*uintptr)(unsafe.Pointer(pExpr + 32)) pList = v1 } if v2 || v1 == uintptr(0) { n = 0 } else { n = (*TExprList)(unsafe.Pointer(pList)).FnExpr _sqlite3WalkExprList(tls, pWalker, pList) if int32((*TWalker)(unsafe.Pointer(pWalker)).FeCode) == 0 { return int32(WRC_Abort) } } db = (*TParse)(unsafe.Pointer((*TWalker)(unsafe.Pointer(pWalker)).FpParse)).Fdb 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)).FxFinalize != uintptr(0) || (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(libc.Int32FromInt32(SQLITE_FUNC_CONSTANT)|libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG)) == uint32(0) || (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0) return int32(WRC_Abort) } return int32(WRC_Prune) } // 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 { if (*TWalker)(unsafe.Pointer(pWalker)).FpParse != 0 { return _exprNodeIsConstantFunction(tls, pWalker, pExpr) } 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(&(*TWalker)(unsafe.Pointer(pWalker)).Fu)) { return WRC_Continue } fallthrough case int32(TK_IF_NULL_ROW): fallthrough case int32(TK_REGISTER): fallthrough case int32(TK_DOT): fallthrough case int32(TK_RAISE): (*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, pParse uintptr, p uintptr, initFlag int32) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* w at bp+0 */ TWalker (*(*TWalker)(unsafe.Pointer(bp))).FeCode = uint16(initFlag) (*(*TWalker)(unsafe.Pointer(bp))).FpParse = pParse (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_exprNodeIsConstant) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkFail) _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. // ** // ** The pParse parameter may be NULL. But if it is NULL, there is no way // ** to determine if function calls are constant or not, and hence all // ** function calls will be considered to be non-constant. If pParse is // ** not NULL, then a function call might be constant, depending on the // ** function and on its parameters. // */ func _sqlite3ExprIsConstant(tls *libc.TLS, pParse uintptr, p uintptr) (r int32) { return _exprIsConst(tls, pParse, p, int32(1)) } // 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, pParse uintptr, p uintptr) (r int32) { return _exprIsConst(tls, pParse, p, int32(2)) } // C documentation // // /* // ** This routine examines sub-SELECT statements as an expression is being // ** walked as part of sqlite3ExprIsTableConstant(). Sub-SELECTs are considered // ** constant as long as they are uncorrelated - meaning that they do not // ** contain any terms from outer contexts. // */ func _exprSelectWalkTableConstant(tls *libc.TLS, pWalker uintptr, pSelect uintptr) (r int32) { if (*TSelect)(unsafe.Pointer(pSelect)).FselFlags&uint32(SF_Correlated) != uint32(0) { (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(0) return int32(WRC_Abort) } return int32(WRC_Prune) } // 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. // ** // ** Consider uncorrelated subqueries to be constants if the bAllowSubq // ** parameter is true. // */ func _sqlite3ExprIsTableConstant(tls *libc.TLS, p uintptr, iCur int32, bAllowSubq int32) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* w at bp+0 */ TWalker (*(*TWalker)(unsafe.Pointer(bp))).FeCode = uint16(3) (*(*TWalker)(unsafe.Pointer(bp))).FpParse = uintptr(0) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_exprNodeIsConstant) if bAllowSubq != 0 { (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_exprSelectWalkTableConstant) } else { (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkFail) } *(*int32)(unsafe.Pointer(bp + 40)) = iCur _sqlite3WalkExpr(tls, bp, p) return int32((*(*TWalker)(unsafe.Pointer(bp))).FeCode) } // 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. // ** // ** (2a) pExpr cannot use subqueries unless the bAllowSubq parameter is // ** true and the subquery is non-correlated // ** // ** (2b) pExpr cannot use 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, bAllowSubq int32) (r int32) { var jj int32 var pSrc uintptr _, _ = jj, pSrc pSrc = pSrcList + 8 + uintptr(iSrc)*104 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 + 52)) != (*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 + 52)) == (*(*TSrcItem)(unsafe.Pointer(pSrcList + 8 + uintptr(jj)*104))).FiCursor { if int32((*(*TSrcItem)(unsafe.Pointer(pSrcList + 8 + uintptr(jj)*104))).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { return 0 /* restriction (6) */ } break } goto _1 _1: ; jj++ } } /* Rules (1), (2a), and (2b) handled by the following: */ return _sqlite3ExprIsTableConstant(tls, pExpr, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor, bAllowSubq) } // 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 + 40)) /* 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)*32))).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(48) defer tls.Free(48) 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 + 40)) = 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) { return _exprIsConst(tls, uintptr(0), p, int32(4)+int32(isInit)) } // 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 */ if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_IntValue) != 0 { *(*int32)(unsafe.Pointer(pValue)) = *(*int32)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(p)).Fu)) 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 { *(*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 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 } 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): return libc.BoolInt32((*TExpr)(unsafe.Pointer(p)).Fflags&uint32(libc.Int32FromInt32(EP_CanBeNull)) != uint32(0) || *(*uintptr)(unsafe.Pointer(p + 64)) == uintptr(0) || int32((*TExpr)(unsafe.Pointer(p)).FiColumn) >= 0 && (*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 64)))).FaCol != uintptr(0) && int32((*TExpr)(unsafe.Pointer(p)).FiColumn) < int32((*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 64)))).FnCol) && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 64)))).FaCol + uintptr((*TExpr)(unsafe.Pointer(p)).FiColumn)*16 + 8))&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 int8) (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): /* 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+8370) == 0 { return int32(1) } if _sqlite3StrICmp(tls, z, __ccgo_ts+8378) == 0 { return int32(1) } if _sqlite3StrICmp(tls, z, __ccgo_ts+8384) == 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 + 8370, 1: __ccgo_ts + 8378, 2: __ccgo_ts + 8384, } ii = 0 for { if !(ii < int32(libc.Uint64FromInt64(24)/libc.Uint64FromInt64(8))) { 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)*16))).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 + 32)) 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 */ } /* 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 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 /* 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 /* 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)*32))).FpExpr if int32((*TExpr)(unsafe.Pointer(pRes)).Fop) != int32(TK_COLUMN) { return uintptr(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, pParse uintptr, pIn uintptr) (r int32) { var pLHS uintptr var res int32 _, _ = pLHS, res pLHS = (*TExpr)(unsafe.Pointer(pIn)).FpLeft (*TExpr)(unsafe.Pointer(pIn)).FpLeft = uintptr(0) res = _sqlite3ExprIsConstant(tls, pParse, 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 int8 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 */ mustBeUnique = libc.BoolInt32(inFlags&uint32(IN_INDEX_LOOP) != uint32(0)) v2 = pParse + 52 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 + 32)))).FpEList i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pEList)).FnExpr) { break } if _sqlite3ExprCanBeNull(tls, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*32))).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 /* Because of isCandidateForInOpt(p) */ /* Because of isCandidateForInOpt(p) */ /* 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) _sqlite3CodeVerifySchema(tls, pParse, iDb) _sqlite3TableLock(tls, pParse, iDb, (*TTable)(unsafe.Pointer(pTab)).Ftnum, uint8(0), (*TTable)(unsafe.Pointer(pTab)).FzName) /* 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+8388, 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)*32))).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. */ 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.Uint64FromInt64(8)*libc.Uint64FromInt32(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)*32))).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 } if pReq != uintptr(0) && _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pReq)).FzName, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(j)*8))) != 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++ } 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(80) defer tls.Free(80) 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+40 */ int8 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 /* 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+8510, libc.VaList(bp+56, (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).FselId)) } _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).FregReturn, (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).FiAddr) _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenDup), iTab, (*TExpr)(unsafe.Pointer(pExpr)).FiTable) _sqlite3VdbeJumpHere(tls, v, addrOnce) return } /* Begin coding the subroutine */ *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Subrtn)) v2 = pParse + 56 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).FregReturn = v1 (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).FiAddr = _sqlite3VdbeAddOp2(tls, v, int32(OP_BeginSubrtn), 0, (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).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 + 32)) pEList = (*TSelect)(unsafe.Pointer(pSelect)).FpEList if addrOnce != 0 { v3 = __ccgo_ts + 1664 } else { v3 = __ccgo_ts + 8533 } _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+8545, libc.VaList(bp+56, 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 } /* OOM will cause exit after sqlite3Select() */ i = 0 for { if !(i < nVal) { break } p = _sqlite3VectorFieldSubexpr(tls, pLeft, i) *(*uintptr)(unsafe.Pointer(pKeyInfo + 32 + uintptr(i)*8)) = _sqlite3BinaryCompareCollSeq(tls, pParse, p, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*32))).FpExpr) goto _5 _5: ; i++ } } } else { if *(*uintptr)(unsafe.Pointer(pExpr + 32)) != uintptr(0) { pList = *(*uintptr)(unsafe.Pointer(pExpr + 32)) *(*int8)(unsafe.Pointer(bp + 40)) = _sqlite3ExprAffinity(tls, pLeft) if int32(*(*int8)(unsafe.Pointer(bp + 40))) <= int32(SQLITE_AFF_NONE) { *(*int8)(unsafe.Pointer(bp + 40)) = int8(SQLITE_AFF_BLOB) } else { if int32(*(*int8)(unsafe.Pointer(bp + 40))) == int32(SQLITE_AFF_REAL) { *(*int8)(unsafe.Pointer(bp + 40)) = int8(SQLITE_AFF_NUMERIC) } } if pKeyInfo != 0 { *(*uintptr)(unsafe.Pointer(pKeyInfo + 32)) = _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, pParse, 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+40, int32(1)) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxInsert), iTab, r2, r1, int32(1)) goto _6 _6: ; i1-- pItem += 32 } _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 */ _sqlite3VdbeAddOp3(tls, v, int32(OP_Return), (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).FregReturn, (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).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 if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return 0 } pSel = *(*uintptr)(unsafe.Pointer(pExpr + 32)) /* 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+8564, libc.VaList(bp+48, (*TSelect)(unsafe.Pointer(pSel)).FselId)) _sqlite3VdbeAddOp2(tls, v, int32(OP_Gosub), (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).FregReturn, (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).FiAddr) return (*TExpr)(unsafe.Pointer(pExpr)).FiTable } /* Begin coding the subroutine */ *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_Subrtn)) v2 = pParse + 56 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).FregReturn = v1 (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).FiAddr = _sqlite3VdbeAddOp2(tls, v, int32(OP_BeginSubrtn), 0, (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).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 + 1664 } else { v3 = __ccgo_ts + 8533 } _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+8582, libc.VaList(bp+48, 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 + 56)) += 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+1784) if pLimit != 0 { (*TExpr)(unsafe.Pointer(pLimit)).FaffExpr = int8(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+8603) (*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 */ _sqlite3VdbeAddOp3(tls, v, int32(OP_Return), (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).FregReturn, (*(*struct { FiAddr int32 FregReturn int32 })(unsafe.Pointer(pExpr + 64))).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 + 32)))).FpEList)).FnExpr { _sqlite3SubselectError(tls, pParse, (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pIn + 32)))).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 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(nVector)*(libc.Uint64FromInt64(4)+libc.Uint64FromInt64(1))+uint64(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 /* 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) /* 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. */ (*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 pList = *(*uintptr)(unsafe.Pointer(pExpr + 32)) 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)*32))).FpExpr, bp+12) if regCkNull != 0 && _sqlite3ExprCanBeNull(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*32))).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(*(*int8)(unsafe.Pointer(zAff)))) } else { if rLhs != r2 { v6 = int32(OP_Ne) } else { v6 = int32(OP_IsNull) } op1 = v6 _sqlite3VdbeAddOp4(tls, v, op1, rLhs, destIfFalse, r2, pColl, -int32(2)) _sqlite3VdbeChangeP5(tls, v, uint16(int32(*(*int8)(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)) /* 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(&(*TExpr)(unsafe.Pointer(pExpr)).Fu)) if negFlag != 0 { i = -i } _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), i, iMem) } else { z = *(*uintptr)(unsafe.Pointer(pExpr + 8)) 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+9, 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 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)*16 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+8635, libc.VaList(bp+8, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)) } else { savedSelfTab = (*TParse)(unsafe.Pointer(pParse)).FiSelfTab p2 = pCol + 14 *(*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 + 14 *(*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 _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 + 56)) += nResult i = 0 for { if !(i < nResult) { break } _sqlite3ExprCodeFactorable(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 32)) + 8 + uintptr(i)*32))).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(80) defer tls.Free(80) var aff int8 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 nFarg = (*TExprList)(unsafe.Pointer(pFarg)).FnExpr /* 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) _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)*32))).FpExpr, target) goto _1 _1: ; i++ } _setDoNotMergeFlagOnCopy(tls, v) _sqlite3VdbeResolveLabel(tls, v, endCoalesce) case int32(INLINEFUNC_iif): libc.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 0)) (*(*TExpr)(unsafe.Pointer(bp))).Fop = uint8(TK_CASE) *(*uintptr)(unsafe.Pointer(bp + 32)) = 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. */ 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() */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + 1*32))).FpExpr, -int32(1)), target) case int32(INLINEFUNC_expr_implies_expr): /* Compare two expressions using sqlite3ExprImpliesExpr() */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), _sqlite3ExprImpliesExpr(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + 1*32))).FpExpr, -int32(1)), target) case int32(INLINEFUNC_implies_nonnull_row): pA1 = (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + 1*32))).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 + 8665, 1: __ccgo_ts + 8670, 2: __ccgo_ts + 8675, 3: __ccgo_ts + 6718, 4: __ccgo_ts + 6713, 5: __ccgo_ts + 8683, } aff = _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr) if int32(aff) <= int32(SQLITE_AFF_NONE) { v2 = __ccgo_ts + 8691 } 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 } exprAff = uint8(_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 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+21, 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(176) defer tls.Free(176) var aListelem, db, db1, pAggInfo, pAggInfo1, pCol, pCol1, pColl, pDef, pDel, pEList, pFarg, pInfo, pLeft, pLeft1, pLeft2, pTab, pTab1, pTab2, pTest, pX, v, z, 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+80 */ 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, 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 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 { op = int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) } } switch op { case int32(TK_AGG_COLUMN): pAggInfo = (*TExpr)(unsafe.Pointer(pExpr)).FpAggInfo 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)*24 if !((*TAggInfo)(unsafe.Pointer(pAggInfo)).FdirectMode != 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)*16))).Faffinity) == int32(SQLITE_AFF_REAL) { _sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), target) } } } return target } else { if *(*uintptr)(unsafe.Pointer(pExpr + 64)) == 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 } } } /* Otherwise, fall thru into the TK_COLUMN case */ 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) aff = int32(_sqlite3TableColumnAffinity(tls, *(*uintptr)(unsafe.Pointer(pExpr + 64)), int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn))) if aff > int32(SQLITE_AFF_BLOB) { _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) pTab1 = *(*uintptr)(unsafe.Pointer(pExpr + 64)) if iCol < 0 { return -int32(1) - (*TParse)(unsafe.Pointer(pParse)).FiSelfTab } pCol1 = (*TTable)(unsafe.Pointer(pTab1)).FaCol + uintptr(iCol)*16 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+8635, libc.VaList(bp+160, (*TColumn)(unsafe.Pointer(pCol1)).FzCnName)) return 0 } p3 = pCol1 + 14 *(*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 + 14 *(*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 } } iReg = _sqlite3ExprCodeGetColumn(tls, pParse, *(*uintptr)(unsafe.Pointer(pExpr + 64)), 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): _codeReal(tls, v, *(*uintptr)(unsafe.Pointer(pExpr + 8)), 0, target) return target case int32(TK_STRING): _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. */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Null), 0, target) return target case int32(TK_BLOB): z = *(*uintptr)(unsafe.Pointer(pExpr + 8)) + 2 n = _sqlite3Strlen30(tls, z) - int32(1) 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): _sqlite3VdbeAddOp2(tls, v, int32(OP_Variable), int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn), target) 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) _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))) 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): 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 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) { _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): 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): _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 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8696, libc.VaList(bp+160, pExpr)) } else { 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(pExpr + 64)))).FregResult } if (*TParse)(unsafe.Pointer(pParse)).FokConstFactor != 0 && _sqlite3ExprIsConstantNotJoin(tls, pParse, 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)) } pFarg = *(*uintptr)(unsafe.Pointer(pExpr + 32)) if pFarg != 0 { v8 = (*TExprList)(unsafe.Pointer(pFarg)).FnExpr } else { v8 = 0 } nFarg = v8 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+8723, libc.VaList(bp+160, pExpr)) break } if (*TFuncDef)(unsafe.Pointer(pDef)).FfuncFlags&uint32(SQLITE_FUNC_INLINE) != uint32(0) && pFarg != uintptr(0) { return _exprCodeInlineFunction(tls, pParse, pFarg, int32(int64((*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, pParse, (*(*TExprList_item)(unsafe.Pointer(pFarg + 8 + uintptr(i)*32))).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)*32))).FpExpr) } goto _9 _9: ; i++ } if pFarg != 0 { if constMask != 0 { r1 = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 56)) += 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) { exprOp = (*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pFarg + 8))).FpExpr)).Fop if int32(exprOp) == int32(TK_COLUMN) || int32(exprOp) == int32(TK_AGG_COLUMN) { (*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*32))).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 + 32)))).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 } n1 = _sqlite3ExprVectorSize(tls, pLeft2) if (*TExpr)(unsafe.Pointer(pExpr)).FiTable != n1 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8306, libc.VaList(bp+160, (*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 WHERE-clause push-down ** optimization. Clear subtypes as subtypes may not cross a subquery ** boundary. */ _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): pTab2 = *(*uintptr)(unsafe.Pointer(pExpr + 64)) 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))) _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)*16))).Faffinity) == int32(SQLITE_AFF_REAL) { _sqlite3VdbeAddOp1(tls, v, int32(OP_RealAffinity), target) } case int32(TK_VECTOR): _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7089, 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 { if !((*TAggInfo)(unsafe.Pointer(pAggInfo1)).FdirectMode != 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)*24))).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) (*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 pEList = *(*uintptr)(unsafe.Pointer(pExpr + 32)) 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.X__builtin___memset_chk(tls, bp+80, 0, uint64(72), libc.X__builtin_object_size(tls, bp+80, 0)) (*(*TExpr)(unsafe.Pointer(bp + 80))).Fop = uint8(TK_EQ) (*(*TExpr)(unsafe.Pointer(bp + 80))).FpLeft = pDel pTest = bp + 80 /* 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 { (*(*TExpr)(unsafe.Pointer(bp + 80))).FpRight = (*(*TExprList_item)(unsafe.Pointer(aListelem + uintptr(i1)*32))).FpExpr } else { pTest = (*(*TExprList_item)(unsafe.Pointer(aListelem + uintptr(i1)*32))).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))*32))).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))*32))).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): if !((*TParse)(unsafe.Pointer(pParse)).FpTriggerTab != 0) && !((*TParse)(unsafe.Pointer(pParse)).Fnested != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+8747, 0) return 0 } if int32((*TExpr)(unsafe.Pointer(pExpr)).FaffExpr) == int32(OE_Abort) { _sqlite3MayAbort(tls, pParse) } 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 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 + 16 + 4))&0x8>>3)) != 0 && _sqlite3ExprCompare(tls, uintptr(0), (*TExprList_item)(unsafe.Pointer(pItem)).FpExpr, pExpr, -int32(1)) == 0 { return *(*int32)(unsafe.Pointer(pItem + 24)) } goto _1 _1: ; pItem += 32 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 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 + 56 *(*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))*32 libc.SetBitFieldPtr16Uint32(pItem1+16+4, libc.BoolUint32(regDest < libc.Int32FromInt32(0)), 3, 0x8) if regDest < 0 { v5 = pParse + 56 *(*int32)(unsafe.Pointer(v5))++ v4 = *(*int32)(unsafe.Pointer(v5)) regDest = v4 } *(*int32)(unsafe.Pointer(pItem1 + 24)) = 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, pParse, 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 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, pParse, 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 /* 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 + 24))).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, pParse, 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 += 32 } 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(224) defer tls.Free(224) var db, pDel uintptr var _ /* compLeft at bp+72 */ TExpr var _ /* compRight at bp+144 */ TExpr var _ /* exprAnd at bp+0 */ TExpr var _ /* regFree1 at bp+216 */ int32 _, _ = db, pDel /* The x<=z term */ *(*int32)(unsafe.Pointer(bp + 216)) = 0 /* Temporary use register */ pDel = uintptr(0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb libc.X__builtin___memset_chk(tls, bp+72, 0, uint64(72), libc.X__builtin_object_size(tls, bp+72, 0)) libc.X__builtin___memset_chk(tls, bp+144, 0, uint64(72), libc.X__builtin_object_size(tls, bp+144, 0)) libc.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 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 + 72 (*(*TExpr)(unsafe.Pointer(bp))).FpRight = bp + 144 (*(*TExpr)(unsafe.Pointer(bp + 72))).Fop = uint8(TK_GE) (*(*TExpr)(unsafe.Pointer(bp + 72))).FpLeft = pDel (*(*TExpr)(unsafe.Pointer(bp + 72))).FpRight = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)) + 8))).FpExpr (*(*TExpr)(unsafe.Pointer(bp + 144))).Fop = uint8(TK_LE) (*(*TExpr)(unsafe.Pointer(bp + 144))).FpLeft = pDel (*(*TExpr)(unsafe.Pointer(bp + 144))).FpRight = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)) + 8 + 1*32))).FpExpr _exprToRegister(tls, pDel, _exprCodeVector(tls, pParse, pDel, bp+216)) 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 + 216))) } _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 if v == uintptr(0) { return } /* Existence of VDBE checked by caller */ if pExpr == uintptr(0) { return } /* No way this can happen */ 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))) 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(_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 if v == uintptr(0) { return } /* Existence of VDBE checked by caller */ if pExpr == uintptr(0) { return } /* 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 */ 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))) 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(&(*TExpr)(unsafe.Pointer(pA)).Fu)) == *(*int32)(unsafe.Pointer(&(*TExpr)(unsafe.Pointer(pB)).Fu)) { 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) } } 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) } 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(pA + 64)), *(*uintptr)(unsafe.Pointer(pB + 64)), 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 + 32)), *(*uintptr)(unsafe.Pointer(pB + 32)), 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)*32))).FpExpr pExprB = (*(*TExprList_item)(unsafe.Pointer(pB + 8 + uintptr(i)*32))).FpExpr if int32((*(*TExprList_item)(unsafe.Pointer(pA + 8 + uintptr(i)*32))).Ffg.FsortFlags) != int32((*(*TExprList_item)(unsafe.Pointer(pB + 8 + uintptr(i)*32))).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 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 } return _exprImpliesNotNull(tls, pParse, (*TExpr)(unsafe.Pointer(p)).FpLeft, pNN, iTab, int32(1)) case int32(TK_BETWEEN): pList = *(*uintptr)(unsafe.Pointer(p + 32)) 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*32))).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(&(*TWalker)(unsafe.Pointer(pWalker)).Fu)) == (*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 + 32)))).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 */ _sqlite3WalkExpr(tls, pWalker, (*TExpr)(unsafe.Pointer(pExpr)).FpLeft) _bothImplyNotNullRow(tls, pWalker, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)) + 8))).FpExpr, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)) + 8 + 1*32))).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 */ if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) == int32(TK_COLUMN) && *(*uintptr)(unsafe.Pointer(pLeft + 64)) != uintptr(0) && int32((*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pLeft + 64)))).FeTabType) == int32(TABTYP_VTAB) || int32((*TExpr)(unsafe.Pointer(pRight)).Fop) == int32(TK_COLUMN) && *(*uintptr)(unsafe.Pointer(pRight + 64)) != uintptr(0) && int32((*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pRight + 64)))).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(48) defer tls.Free(48) 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 + 40)) = 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 + 40)))).FiCur && int32(_sqlite3TableColumnToIndex(tls, (*TIdxCover)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalker + 40)))).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(64) defer tls.Free(64) var _ /* w at bp+0 */ TWalker var _ /* xcov at bp+48 */ TIdxCover libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*TIdxCover)(unsafe.Pointer(bp + 48))).FiCur = iCur (*(*TIdxCover)(unsafe.Pointer(bp + 48))).FpIdx = pIdx (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_exprIdxCover) *(*uintptr)(unsafe.Pointer(bp + 40)) = bp + 48 _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 { Fdb uintptr FpRef uintptr FnExclude Ti64 FaiExclude uintptr } 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 + 40)) 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 + 16)) += 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)*uint64(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)*104))).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 + 40)) pSrc = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc if (*TRefSrcList)(unsafe.Pointer(p)).FnExclude != 0 { *(*Ti64)(unsafe.Pointer(p + 16)) -= 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 + 40)) 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)*104))).FiCursor { p3 = pWalker + 36 *(*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 + 36 *(*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(80) defer tls.Free(80) var _ /* w at bp+0 */ TWalker var _ /* x at bp+48 */ TRefSrcList libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) libc.X__builtin___memset_chk(tls, bp+48, 0, uint64(32), libc.X__builtin_object_size(tls, bp+48, 0)) (*(*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 + 40)) = bp + 48 (*(*TRefSrcList)(unsafe.Pointer(bp + 48))).Fdb = (*TParse)(unsafe.Pointer(pParse)).Fdb (*(*TRefSrcList)(unsafe.Pointer(bp + 48))).FpRef = pSrcList _sqlite3WalkExprList(tls, bp, *(*uintptr)(unsafe.Pointer(pExpr + 32))) if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 { _sqlite3WalkExprList(tls, bp, *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 32))) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { _sqlite3WalkExpr(tls, bp, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 64)))).FpFilter) } if (*(*TRefSrcList)(unsafe.Pointer(bp + 48))).FaiExclude != 0 { _sqlite3DbNNFreeNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*(*TRefSrcList)(unsafe.Pointer(bp + 48))).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 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)*24))).FpCExpr == pExpr { pExpr = _sqlite3ExprDup(tls, db, pExpr, 0) if pExpr != 0 && !(_sqlite3ExprDeferredDelete(tls, pParse, pExpr) != 0) { (*(*TAggInfo_col)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(iAgg)*24))).FpCExpr = pExpr } } } else { if iAgg < (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc && (*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(iAgg)*32))).FpFExpr == pExpr { pExpr = _sqlite3ExprDup(tls, db, pExpr, 0) if pExpr != 0 && !(_sqlite3ExprDeferredDelete(tls, pParse, pExpr) != 0) { (*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(iAgg)*32))).FpFExpr = 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.X__builtin___memset_chk(tls, pWalker, 0, uint64(48), libc.X__builtin_object_size(tls, pWalker, 0)) (*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(24), pInfo+32, 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(32), pInfo+48, 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 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 += 24 } k = _addAggInfoColumn(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pAggInfo) if k < 0 { /* OOM on resize */ return } pCol = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr(k)*24 (*TAggInfo_col)(unsafe.Pointer(pCol)).FpTab = *(*uintptr)(unsafe.Pointer(pExpr + 64)) (*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 += 32 } } 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: ; (*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(80) defer tls.Free(80) 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 + 40)) pParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse pSrcList = (*TNameContext)(unsafe.Pointer(pNC)).FpSrcList pAggInfo = *(*uintptr)(unsafe.Pointer(pNC + 16)) switch int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) { default: 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.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 0)) (*(*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) } (*(*TAggInfo_col)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr((*(*TExpr)(unsafe.Pointer(bp))).FiAgg)*24))).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 } 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 += 104 } /* 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 += 32 } 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 { pItem1 = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc + uintptr(i)*32 (*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFExpr = pExpr if *(*uintptr)(unsafe.Pointer(pExpr + 32)) != 0 { v5 = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).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)) if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 && (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pItem1)).FpFunc)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) == uint32(0) { v7 = pParse + 52 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 + 32)) 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 + 32)) + 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 + 52 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 */ (*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(48) defer tls.Free(48) 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 + 40)) = pNC (*(*TWalker)(unsafe.Pointer(bp))).FpParse = uintptr(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 += 32 } } } // 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 + 56 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) return v1 } v4 = pParse + 31 *(*Tu8)(unsafe.Pointer(v4))-- v3 = *(*Tu8)(unsafe.Pointer(v4)) return *(*int32)(unsafe.Pointer(pParse + 232 + 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.Uint64FromInt64(32)/libc.Uint64FromInt64(4)) { v2 = pParse + 31 v1 = *(*Tu8)(unsafe.Pointer(v2)) *(*Tu8)(unsafe.Pointer(v2))++ *(*int32)(unsafe.Pointer(pParse + 232 + 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 + 44)) += nReg *(*int32)(unsafe.Pointer(pParse + 40)) -= nReg } else { i = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 56)) += 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)*32 + 24)) >= iMin { iMin = *(*int32)(unsafe.Pointer(pList + 8 + uintptr(i)*32 + 24)) + 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+6937, 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+8797, 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+8825, libc.VaList(bp+8, zDb, zDb, bTemp, zWhen, bNoDQS)) if bTemp == 0 { _sqlite3NestedParse(tls, pParse, __ccgo_ts+9000, 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+9174, libc.VaList(bp+8, zDb, zDb)) if bTemp == 0 { _sqlite3NestedParse(tls, pParse, __ccgo_ts+9321, 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 } 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)*32))).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+9472, 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+9531, zName) { goto exit_rename_table } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+9537, 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+9564, 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+9748, 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+10053, zDb) != 0 { _sqlite3NestedParse(tls, pParse, __ccgo_ts+10069, 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+10127, 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 + 56 *(*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+10392, 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+10405, 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 if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return } pNew = (*TParse)(unsafe.Pointer(pParse)).FpNewTable iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pNew)).FpSchema) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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))*16 pDflt = _sqlite3ColumnExpr(tls, pNew, pCol) pTab = _sqlite3FindTable(tls, db, zTab, zDb) /* 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+10443, 0) return } if (*TTable)(unsafe.Pointer(pNew)).FpIndex != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+10475, 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. */ if pDflt != 0 && int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pDflt)).FpLeft)).Fop) == int32(TK_NULL) { pDflt = uintptr(0) } if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != 0 && (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pNew + 64))).FpFKey != 0 && pDflt != 0 { _sqlite3ErrorIfNotEmpty(tls, pParse, zDb, zTab, __ccgo_ts+10502) } if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 8))&0xf>>0)) != 0 && !(pDflt != 0) { _sqlite3ErrorIfNotEmpty(tls, pParse, zDb, zTab, __ccgo_ts+10561) } /* 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) if rc != SQLITE_OK { return } if !(*(*uintptr)(unsafe.Pointer(bp)) != 0) { _sqlite3ErrorIfNotEmpty(tls, pParse, zDb, zTab, __ccgo_ts+10614) } _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+10660) } } /* 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(*(*int8)(unsafe.Pointer(zEnd))) == int32(';') || int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zEnd)))])&int32(0x01) != 0) { v1 = zEnd zEnd-- *(*int8)(unsafe.Pointer(v1)) = int8('\000') } /* substr() operations on characters, but addColOffset is in bytes. So we ** have to use printf() to translate between these units: */ _sqlite3NestedParse(tls, pParse, __ccgo_ts+10687, libc.VaList(bp+16, zDb, (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pNew + 64))).FaddColOffset, zCol, (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pNew + 64))).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 + 8))&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+10833, 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. */ 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+11186, 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+11220, 0) goto exit_begin_add_column } if SQLITE_OK != _isAlterableTable(tls, pParse, pTab) { goto exit_begin_add_column } _sqlite3MayAbort(tls, pParse) 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(104)) 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 nAlloc = (int32((*TTable)(unsafe.Pointer(pNew)).FnCol)-int32(1))/int32(8)*int32(8) + int32(8) (*TTable)(unsafe.Pointer(pNew)).FaCol = _sqlite3DbMallocZero(tls, db, uint64(16)*uint64(nAlloc)) (*TTable)(unsafe.Pointer(pNew)).FzName = _sqlite3MPrintf(tls, db, __ccgo_ts+11250, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) if !((*TTable)(unsafe.Pointer(pNew)).FaCol != 0) || !((*TTable)(unsafe.Pointer(pNew)).FzName != 0) { goto exit_begin_add_column } libc.X__builtin___memcpy_chk(tls, (*TTable)(unsafe.Pointer(pNew)).FaCol, (*TTable)(unsafe.Pointer(pTab)).FaCol, uint64(16)*uint64((*TTable)(unsafe.Pointer(pNew)).FnCol), libc.X__builtin_object_size(tls, (*TTable)(unsafe.Pointer(pNew)).FaCol, 0)) i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pNew)).FnCol)) { break } pCol = (*TTable)(unsafe.Pointer(pNew)).FaCol + uintptr(i)*16 (*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++ } (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pNew + 64))).FpDfltList = _sqlite3ExprListDup(tls, db, (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 64))).FpDfltList, 0) (*TTable)(unsafe.Pointer(pNew)).FpSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FpSchema (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pNew + 64))).FaddColOffset = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 64))).FaddColOffset 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 + 11269 } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { zType = __ccgo_ts + 11274 } if zType != 0 { if bDrop != 0 { v1 = __ccgo_ts + 11288 } else { v1 = __ccgo_ts + 11305 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+11323, 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) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iSchema)*32))).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)*16))).FzCnName, zOld) { break } goto _1 _1: ; iCol++ } if iCol == int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+11341, 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+1664, 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 } bQuote = int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer((*TToken)(unsafe.Pointer(pNew)).Fz)))]) & int32(0x80) _sqlite3NestedParse(tls, pParse, __ccgo_ts+11362, 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+11544, 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+10392, 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 if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != int32(PARSE_MODE_UNMAP) { pNew = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(32)) 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+64) } 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(64) defer tls.Free(64) 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) if (*TSelect)(unsafe.Pointer((*(*TCte)(unsafe.Pointer(pWith + 16))).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 + 16 + uintptr(i)*48))).FpSelect libc.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) (*(*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 + 16 + uintptr(i)*48))).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 ii = 0 for { if !(ii < (*TIdList)(unsafe.Pointer(pIdList)).FnId) { break } _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), (*(*TIdList_item)(unsafe.Pointer(pIdList + 8 + uintptr(ii)*16))).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)*32))).FzEName != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pList + 8 + uintptr(i)*32 + 16 + 4))&0x3>>0)) == ENAME_NAME { _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*32))).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)*104))).FzName) if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104 + 60 + 4))&0x400>>10)) == 0 { _sqlite3WalkExpr(tls, pWalker, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104 + 72))) } else { _unmapColumnIdlistNames(tls, pParse, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104 + 72))) } 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(48) defer tls.Free(48) var eMode Tu8 var _ /* sWalker at bp+0 */ TWalker _ = eMode eMode = (*TParse)(unsafe.Pointer(pParse)).FeParseMode libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*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(48) defer tls.Free(48) var i int32 var _ /* sWalker at bp+0 */ TWalker _ = i if pEList != 0 { libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*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)*32 + 16 + 4))&0x3>>0)) == ENAME_NAME { _sqlite3RenameTokenRemap(tls, pParse, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*32))).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 + 416 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)) + 24 } 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 + 40)) 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 == *(*uintptr)(unsafe.Pointer(pExpr + 64)) { _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)) + 24 } *(*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 *(*int8)(unsafe.Pointer(zWhen)) != 0 { v1 = __ccgo_ts + 11675 } else { v1 = __ccgo_ts + 1664 } zErr = _sqlite3MPrintf(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, __ccgo_ts+11677, 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)*32))).FzEName if int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(i)*32 + 16 + 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)*16))).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+11700, int32(7)) != 0 { return _sqlite3CorruptError(tls, int32(117285)) } 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(117296)) } (*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+11708, libc.VaList(bp+8, zNew)) if zQuot == uintptr(0) { return int32(SQLITE_NOMEM) } else { nQuot = int64(_sqlite3Strlen30(tls, zQuot) - int32(1)) } 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.X__builtin___memcpy_chk(tls, zOut, zSql, uint64(nSql), libc.X__builtin_object_size(tls, zOut, 0)) for (*TRenameCtx)(unsafe.Pointer(pRename)).FpList != 0 { pBest = _renameColumnTokenNext(tls, pRename) if zNew != 0 { if bQuote == 0 && _sqlite3IsIdChar(tls, uint8(*(*int8)(unsafe.Pointer((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz)))) != 0 { nReplace = uint32(nNew) zReplace = zNew } else { nReplace = uint32(nQuot) zReplace = zQuot if int32(*(*int8)(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.X__builtin___memcpy_chk(tls, zBuf1, (*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz, uint64((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn), libc.X__builtin_object_size(tls, zBuf1, 0)) *(*int8)(unsafe.Pointer(zBuf1 + uintptr((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn))) = 0 _sqlite3Dequote(tls, zBuf1) if int32(*(*int8)(unsafe.Pointer((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz + uintptr((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn)))) == int32('\'') { v1 = __ccgo_ts + 11675 } else { v1 = __ccgo_ts + 1664 } Xsqlite3_snprintf(tls, int32(nSql*int64(2)), zBuf2, __ccgo_ts+11714, libc.VaList(bp+8, zBuf1, v1)) zReplace = zBuf2 nReplace = uint32(_sqlite3Strlen30(tls, zReplace)) } iOff = int32(int64((*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fz) - int64(zSql)) if (*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn != nReplace { libc.X__builtin___memmove_chk(tls, zOut+uintptr(uint32(iOff)+nReplace), zOut+uintptr(uint32(iOff)+(*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn), uint64(uint32(nOut)-(uint32(iOff)+(*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn)), libc.X__builtin_object_size(tls, zOut+uintptr(uint32(iOff)+nReplace), 0)) nOut = int32(uint32(nOut) + (nReplace - (*TRenameToken)(unsafe.Pointer(pBest)).Ft.Fn)) *(*int8)(unsafe.Pointer(zOut + uintptr(nOut))) = int8('\000') } libc.X__builtin___memcpy_chk(tls, zOut+uintptr(iOff), zReplace, uint64(nReplace), libc.X__builtin_object_size(tls, zOut+uintptr(iOff), 0)) _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.SetBitFieldPtr16Uint32(pEList+8+uintptr(i)*32+16+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(64) defer tls.Free(64) 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.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) (*(*TNameContext)(unsafe.Pointer(bp))).FpParse = pParse (*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))*32))).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 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)*104 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) } 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 + 16)) = 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)*104))).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(512) defer tls.Free(512) 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+32 */ TParse var _ /* sWalker at bp+456 */ 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*8))) zTable = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 4*8))) iCol = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 5*8))) zNew = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 6*8))) bQuote = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 7*8))) bTemp = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 8*8))) 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)*16))).FzCnName libc.X__builtin___memset_chk(tls, bp, 0, uint64(32), libc.X__builtin_object_size(tls, bp, 0)) 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+32, zDb, db, zSql, bTemp) /* Find tokens that need to be replaced. */ libc.X__builtin___memset_chk(tls, bp+456, 0, uint64(48), libc.X__builtin_object_size(tls, bp+456, 0)) (*(*TWalker)(unsafe.Pointer(bp + 456))).FpParse = bp + 32 (*(*TWalker)(unsafe.Pointer(bp + 456))).FxExprCallback = __ccgo_fp(_renameColumnExprCb) (*(*TWalker)(unsafe.Pointer(bp + 456))).FxSelectCallback = __ccgo_fp(_renameColumnSelectCb) *(*uintptr)(unsafe.Pointer(bp + 456 + 40)) = bp (*(*TRenameCtx)(unsafe.Pointer(bp))).FpTab = pTab if rc != SQLITE_OK { goto renameColumnFunc_done } if (*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable != 0 { if int32((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable)).FeTabType) == int32(TABTYP_VIEW) { pSelect = (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable)).Fu))).FpSelect *(*Tu32)(unsafe.Pointer(pSelect + 4)) &= uint32(^libc.Int32FromInt32(SF_View)) (*(*TParse)(unsafe.Pointer(bp + 32))).Frc = SQLITE_OK _sqlite3SelectPrep(tls, bp+32, pSelect, uintptr(0)) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { v2 = int32(SQLITE_NOMEM) } else { v2 = (*(*TParse)(unsafe.Pointer(bp + 32))).Frc } rc = v2 if rc == SQLITE_OK { _sqlite3WalkSelect(tls, bp+456, pSelect) } if rc != SQLITE_OK { goto renameColumnFunc_done } } else { if int32((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable)).FeTabType) == TABTYP_NORM { /* A regular table */ bFKOnly = Xsqlite3_stricmp(tls, zTable, (*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable)).FzName) (*(*TRenameCtx)(unsafe.Pointer(bp))).FpTab = (*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable if bFKOnly == 0 { if iCol < int32((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable)).FnCol) { _renameTokenFind(tls, bp+32, bp, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable)).FaCol + uintptr(iCol)*16))).FzCnName) } if (*(*TRenameCtx)(unsafe.Pointer(bp))).FiCol < 0 { _renameTokenFind(tls, bp+32, bp, (*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable+52) } _sqlite3WalkExprList(tls, bp+456, (*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable)).FpCheck) pIdx = (*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable)).FpIndex for { if !(pIdx != 0) { break } _sqlite3WalkExprList(tls, bp+456, (*TIndex)(unsafe.Pointer(pIdx)).FaColExpr) goto _3 _3: ; pIdx = (*TIndex)(unsafe.Pointer(pIdx)).FpNext } pIdx = (*(*TParse)(unsafe.Pointer(bp + 32))).FpNewIndex for { if !(pIdx != 0) { break } _sqlite3WalkExprList(tls, bp+456, (*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 + 32))).FpNewTable)).FnCol)) { break } pExpr = _sqlite3ColumnExpr(tls, (*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable, (*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable)).FaCol+uintptr(i)*16) _sqlite3WalkExpr(tls, bp+456, pExpr) goto _5 _5: ; i++ } } pFKey = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTable + 64))).FpFKey for { if !(pFKey != 0) { break } i = 0 for { if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) { break } if bFKOnly == 0 && (*(*TsColMap)(unsafe.Pointer(pFKey + 64 + uintptr(i)*16))).FiFrom == iCol { _renameTokenFind(tls, bp+32, bp, pFKey+64+uintptr(i)*16) } if 0 == Xsqlite3_stricmp(tls, (*TFKey)(unsafe.Pointer(pFKey)).FzTo, zTable) && 0 == Xsqlite3_stricmp(tls, (*(*TsColMap)(unsafe.Pointer(pFKey + 64 + uintptr(i)*16))).FzCol, zOld) { _renameTokenFind(tls, bp+32, bp, (*(*TsColMap)(unsafe.Pointer(pFKey + 64 + uintptr(i)*16))).FzCol) } goto _7 _7: ; i++ } goto _6 _6: ; pFKey = (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom } } } } else { if (*(*TParse)(unsafe.Pointer(bp + 32))).FpNewIndex != 0 { _sqlite3WalkExprList(tls, bp+456, (*TIndex)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewIndex)).FaColExpr) _sqlite3WalkExpr(tls, bp+456, (*TIndex)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewIndex)).FpPartIdxWhere) } else { rc = _renameResolveTrigger(tls, bp+32) if rc != SQLITE_OK { goto renameColumnFunc_done } pStep = (*TTrigger)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTrigger)).Fstep_list for { if !(pStep != 0) { break } if (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget != 0 { pTarget = _sqlite3LocateTable(tls, bp+32, 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+32, bp, pUpsertSet, zOld) } _renameColumnIdlistNames(tls, bp+32, bp, (*TTriggerStep)(unsafe.Pointer(pStep)).FpIdList, zOld) _renameColumnElistNames(tls, bp+32, 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 + 32))).FpTriggerTab == pTab { _renameColumnIdlistNames(tls, bp+32, bp, (*TTrigger)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTrigger)).FpColumns, zOld) } /* Find tokens to edit in various expressions and selects */ _renameWalkTrigger(tls, bp+456, (*(*TParse)(unsafe.Pointer(bp + 32))).FpNewTrigger) } } 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 + 32))).FzErrMsg != 0 { _renameColumnParseError(tls, context, __ccgo_ts+1664, *(*uintptr)(unsafe.Pointer(argv + 1*8)), *(*uintptr)(unsafe.Pointer(argv + 2*8)), bp+32) } else { Xsqlite3_result_error_code(tls, context, rc) } } } _renameParseCleanup(tls, bp+32) _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 + 40)) 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 == *(*uintptr)(unsafe.Pointer(pExpr + 64)) { _renameTokenFind(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, p, pExpr+64) } 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 + 40)) 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) { return int32(WRC_Abort) } i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) { break } pItem = pSrc + 8 + uintptr(i)*104 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(560) defer tls.Free(560) 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+424 */ TRenameCtx var _ /* sNC at bp+504 */ TNameContext var _ /* sParse at bp+0 */ TParse var _ /* sWalker at bp+456 */ 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*8))) zOld = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 4*8))) zNew = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 5*8))) bTemp = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 6*8))) _ = 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.X__builtin___memset_chk(tls, bp+424, 0, uint64(32), libc.X__builtin_object_size(tls, bp+424, 0)) (*(*TRenameCtx)(unsafe.Pointer(bp + 424))).FpTab = _sqlite3FindTable(tls, db, zOld, zDb) libc.X__builtin___memset_chk(tls, bp+456, 0, uint64(48), libc.X__builtin_object_size(tls, bp+456, 0)) (*(*TWalker)(unsafe.Pointer(bp + 456))).FpParse = bp (*(*TWalker)(unsafe.Pointer(bp + 456))).FxExprCallback = __ccgo_fp(_renameTableExprCb) (*(*TWalker)(unsafe.Pointer(bp + 456))).FxSelectCallback = __ccgo_fp(_renameTableSelectCb) *(*uintptr)(unsafe.Pointer(bp + 456 + 40)) = bp + 424 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.X__builtin___memset_chk(tls, bp+504, 0, uint64(56), libc.X__builtin_object_size(tls, bp+504, 0)) (*(*TNameContext)(unsafe.Pointer(bp + 504))).FpParse = bp *(*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+504) if (*(*TParse)(unsafe.Pointer(bp))).FnErr != 0 { rc = (*(*TParse)(unsafe.Pointer(bp))).Frc } else { _sqlite3WalkSelect(tls, bp+456, (*(*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)) { pFKey = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 64))).FpFKey for { if !(pFKey != 0) { break } if Xsqlite3_stricmp(tls, (*TFKey)(unsafe.Pointer(pFKey)).FzTo, zOld) == 0 { _renameTokenFind(tls, bp, bp+424, (*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 + 424))).FpTab = pTab if isLegacy == 0 { _sqlite3WalkExprList(tls, bp+456, (*TTable)(unsafe.Pointer(pTab)).FpCheck) } _renameTokenFind(tls, bp, bp+424, (*TTable)(unsafe.Pointer(pTab)).FzName) } } } else { if (*(*TParse)(unsafe.Pointer(bp))).FpNewIndex != 0 { _renameTokenFind(tls, bp, bp+424, (*TIndex)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewIndex)).FzName) if isLegacy == 0 { _sqlite3WalkExpr(tls, bp+456, (*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 + 424))).FpTab)).FpSchema == (*TTrigger)(unsafe.Pointer(pTrigger)).FpTabSchema { _renameTokenFind(tls, bp, bp+424, (*TTrigger)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTrigger)).Ftable) } if isLegacy == 0 { rc = _renameResolveTrigger(tls, bp) if rc == SQLITE_OK { _renameWalkTrigger(tls, bp+456, 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+424, (*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)*104 if 0 == Xsqlite3_stricmp(tls, (*TSrcItem)(unsafe.Pointer(pItem)).FzName, zOld) { _renameTokenFind(tls, bp, bp+424, (*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+424, 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*8))) } else { if (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg != 0 { _renameColumnParseError(tls, context, __ccgo_ts+1664, *(*uintptr)(unsafe.Pointer(argv + 1*8)), *(*uintptr)(unsafe.Pointer(argv + 2*8)), bp) } else { Xsqlite3_result_error_code(tls, context, rc) } } } _renameParseCleanup(tls, bp) _renameTokenFree(tls, db, (*(*TRenameCtx)(unsafe.Pointer(bp + 424))).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 + 40)), 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(512) defer tls.Free(512) var db, pSelect, zDb, zInput uintptr var i, rc, v1 int32 var xAuth Tsqlite3_xauth var _ /* sCtx at bp+424 */ TRenameCtx var _ /* sParse at bp+0 */ TParse var _ /* sWalker at bp+456 */ 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*8))) 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.X__builtin___memset_chk(tls, bp+424, 0, uint64(32), libc.X__builtin_object_size(tls, bp+424, 0)) libc.X__builtin___memset_chk(tls, bp+456, 0, uint64(48), libc.X__builtin_object_size(tls, bp+456, 0)) (*(*TWalker)(unsafe.Pointer(bp + 456))).FpParse = bp (*(*TWalker)(unsafe.Pointer(bp + 456))).FxExprCallback = __ccgo_fp(_renameQuotefixExprCb) (*(*TWalker)(unsafe.Pointer(bp + 456))).FxSelectCallback = __ccgo_fp(_renameColumnSelectCb) *(*uintptr)(unsafe.Pointer(bp + 456 + 40)) = bp + 424 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+456, pSelect) } } else { _sqlite3WalkExprList(tls, bp+456, (*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+456, _sqlite3ColumnExpr(tls, (*(*TParse)(unsafe.Pointer(bp))).FpNewTable, (*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTable)).FaCol+uintptr(i)*16)) goto _2 _2: ; i++ } } } else { if (*(*TParse)(unsafe.Pointer(bp))).FpNewIndex != 0 { _sqlite3WalkExprList(tls, bp+456, (*TIndex)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewIndex)).FaColExpr) _sqlite3WalkExpr(tls, bp+456, (*TIndex)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewIndex)).FpPartIdxWhere) } else { rc = _renameResolveTrigger(tls, bp) if rc == SQLITE_OK { _renameWalkTrigger(tls, bp+456, (*(*TParse)(unsafe.Pointer(bp))).FpNewTrigger) } } } if rc == SQLITE_OK { rc = _renameEditSql(tls, context, bp+424, zInput, uintptr(0), 0) } _renameTokenFree(tls, db, (*(*TRenameCtx)(unsafe.Pointer(bp + 424))).FpList) } if rc != SQLITE_OK { if _sqlite3WritableSchema(tls, db) != 0 && rc == int32(SQLITE_ERROR) { Xsqlite3_result_value(tls, context, *(*uintptr)(unsafe.Pointer(argv + 1*8))) } 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(480) defer tls.Free(480) var bNoDQS, bTemp, flags, i1, i2, isLegacy, rc int32 var db, zDb, zInput, zWhen uintptr var xAuth Tsqlite3_xauth var _ /* sNC at bp+424 */ 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*8))) bTemp = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 4*8))) isLegacy = int32((*Tsqlite3)(unsafe.Pointer(db)).Fflags & libc.Uint64FromInt32(SQLITE_LegacyAlter)) zWhen = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 5*8))) bNoDQS = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 6*8))) 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 + 48)) &= uint64(^(libc.Int32FromInt32(SQLITE_DqsDML) | libc.Int32FromInt32(SQLITE_DqsDDL))) } rc = _renameParseSql(tls, bp, zDb, db, zInput, bTemp) *(*Tu64)(unsafe.Pointer(db + 48)) |= 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.X__builtin___memset_chk(tls, bp+424, 0, uint64(56), libc.X__builtin_object_size(tls, bp+424, 0)) (*(*TNameContext)(unsafe.Pointer(bp + 424))).FpParse = bp _sqlite3SelectPrep(tls, bp, (*(*struct { FpSelect uintptr })(unsafe.Pointer(&(*TTable)(unsafe.Pointer((*(*TParse)(unsafe.Pointer(bp))).FpNewTable)).Fu))).FpSelect, bp+424) 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*8)), *(*uintptr)(unsafe.Pointer(argv + 3*8)), 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(464) defer tls.Free(464) 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*8))) iCol = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 2*8))) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iSchema)*32))).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(118255)) goto drop_column_done } pCol = _renameTokenFind(tls, bp, uintptr(0), (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*16))).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))*16))).FzCnName) zEnd = (*TRenameToken)(unsafe.Pointer(pEnd)).Ft.Fz } else { zEnd = zSql + uintptr((*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 64))).FaddColOffset) for int32(*(*int8)(unsafe.Pointer((*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz))) != 0 && int32(*(*int8)(unsafe.Pointer((*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz))) != int32(',') { (*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz-- } } zNew = _sqlite3MPrintf(tls, db, __ccgo_ts+11719, libc.VaList(bp+432, int64((*TRenameToken)(unsafe.Pointer(pCol)).Ft.Fz)-int64(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 aff int8 var db, pPk, pTab, v, zCol, zDb, v1, v3, v5, v8 uintptr _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, aff, 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. */ 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) { goto exit_drop_column } iCol = _sqlite3ColumnIndex(tls, pTab, zCol) if iCol < 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+11341, 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)*16))).FcolFlags)&(libc.Int32FromInt32(COLFLAG_PRIMKEY)|libc.Int32FromInt32(COLFLAG_UNIQUE)) != 0 { if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*16))).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 { v1 = __ccgo_ts + 11726 } else { v1 = __ccgo_ts + 6001 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+11738, 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+11766, libc.VaList(bp+8, zCol)) goto exit_drop_column } /* Edit the sqlite_schema table */ iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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+1664, 0) _renameFixQuotes(tls, pParse, zDb, libc.BoolInt32(iDb == int32(1))) _sqlite3NestedParse(tls, pParse, __ccgo_ts+11814, 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+11935, 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)*16))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { pPk = uintptr(0) nField = 0 v = _sqlite3GetVdbe(tls, pParse) v3 = pParse + 52 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 + 56 *(*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 + 56)) += int32((*TTable)(unsafe.Pointer(pTab)).FnCol) } else { pPk = _sqlite3PrimaryKeyIndex(tls, pTab) *(*int32)(unsafe.Pointer(pParse + 56)) += 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 + 56 *(*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)*16))).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 { aff = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*16))).Faffinity if int32(aff) == int32(SQLITE_AFF_REAL) { (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*16))).Faffinity = int8(SQLITE_AFF_NUMERIC) } _sqlite3ExprCodeGetColumnOfTable(tls, v, pTab, iCur, i, regOut) (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*16))).Faffinity = aff } 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.Uint64FromInt64(360)/libc.Uint64FromInt64(72))) } 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 + 11953, }, 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 + 11974, }, 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 + 11994, }, 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 + 12013, }, 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 + 12032, }, } func init() { p := unsafe.Pointer(&_aAlterTableFuncs) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_renameColumnFunc) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_renameTableFunc) *(*uintptr)(unsafe.Add(p, 168)) = __ccgo_fp(_renameTableTest) *(*uintptr)(unsafe.Add(p, 240)) = __ccgo_fp(_dropColumnFunc) *(*uintptr)(unsafe.Add(p, 312)) = __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 } pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32 /* Create new statistic tables if they do not exist, or clear them ** if they do already exist. */ i = 0 for { if !(i < int32(libc.Uint64FromInt64(48)/libc.Uint64FromInt64(16))) { 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+12135, 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+12158, 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+12188, 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 } _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 + 12055, FzCols: __ccgo_ts + 12068, }, 1: { FzName: __ccgo_ts + 12081, FzCols: __ccgo_ts + 12094, }, 2: { FzName: __ccgo_ts + 12122, }, } /* ** 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 { Fdb uintptr FnEst TtRowcnt FnRow TtRowcnt FnLimit int32 FnCol int32 FnKeyCol int32 FnSkipAhead Tu8 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 { FanDLt uintptr FanEq uintptr FanLt uintptr Fu struct { FaRowid [0]uintptr FiRowid Ti64 } FnRowid Tu32 FisPSample Tu8 FiCol int32 FiHash Tu32 } type StatSample = TStatSample type TStatSample1 = struct { FanDLt uintptr FanEq uintptr FanLt uintptr Fu struct { FaRowid [0]uintptr FiRowid Ti64 } FnRowid Tu32 FisPSample Tu8 FiCol int32 FiHash Tu32 } type StatSample1 = TStatSample1 type TStatAccum1 = struct { Fdb uintptr FnEst TtRowcnt FnRow TtRowcnt FnLimit int32 FnCol int32 FnKeyCol int32 FnSkipAhead Tu8 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) { if (*TStatSample)(unsafe.Pointer(p)).FnRowid != 0 { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(p + 24))) (*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) { if (*TStatSample)(unsafe.Pointer(p)).FnRowid != 0 { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(p + 24))) } *(*uintptr)(unsafe.Pointer(p + 24)) = _sqlite3DbMallocRawNN(tls, db, uint64(n)) if *(*uintptr)(unsafe.Pointer(p + 24)) != 0 { (*TStatSample)(unsafe.Pointer(p)).FnRowid = uint32(n) libc.X__builtin___memcpy_chk(tls, *(*uintptr)(unsafe.Pointer(p + 24)), pData, uint64(n), libc.X__builtin_object_size(tls, *(*uintptr)(unsafe.Pointer(p + 24)), 0)) } 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) { if (*TStatSample)(unsafe.Pointer(p)).FnRowid != 0 { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(p + 24))) } (*TStatSample)(unsafe.Pointer(p)).FnRowid = uint32(0) *(*Ti64)(unsafe.Pointer(p + 24)) = 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.X__builtin___memcpy_chk(tls, (*TStatSample)(unsafe.Pointer(pTo)).FanEq, (*TStatSample)(unsafe.Pointer(pFrom)).FanEq, uint64(8)*uint64((*TStatAccum)(unsafe.Pointer(p)).FnCol), libc.X__builtin_object_size(tls, (*TStatSample)(unsafe.Pointer(pTo)).FanEq, 0)) libc.X__builtin___memcpy_chk(tls, (*TStatSample)(unsafe.Pointer(pTo)).FanLt, (*TStatSample)(unsafe.Pointer(pFrom)).FanLt, uint64(8)*uint64((*TStatAccum)(unsafe.Pointer(p)).FnCol), libc.X__builtin_object_size(tls, (*TStatSample)(unsafe.Pointer(pTo)).FanLt, 0)) libc.X__builtin___memcpy_chk(tls, (*TStatSample)(unsafe.Pointer(pTo)).FanDLt, (*TStatSample)(unsafe.Pointer(pFrom)).FanDLt, uint64(8)*uint64((*TStatAccum)(unsafe.Pointer(p)).FnCol), libc.X__builtin_object_size(tls, (*TStatSample)(unsafe.Pointer(pTo)).FanDLt, 0)) if (*TStatSample)(unsafe.Pointer(pFrom)).FnRowid != 0 { _sampleSetRowid(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, pTo, int32((*TStatSample)(unsafe.Pointer(pFrom)).FnRowid), *(*uintptr)(unsafe.Pointer(pFrom + 24))) } else { _sampleSetRowidInt64(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, pTo, *(*Ti64)(unsafe.Pointer(pFrom + 24))) } } // 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)*48) 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)*48) 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))) nColUp = nCol nKeyCol = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) /* Allocate the space required for the StatAccum object */ n = int32(uint64(136) + uint64(8)*uint64(nColUp)) /* StatAccum.anDLt */ n = int32(uint64(n) + libc.Uint64FromInt64(8)*uint64(nColUp)) /* StatAccum.anEq */ if mxSample != 0 { n = int32(uint64(n) + (libc.Uint64FromInt64(8)*uint64(nColUp) + libc.Uint64FromInt64(48)*uint64(nCol+mxSample) + libc.Uint64FromInt64(8)*libc.Uint64FromInt32(3)*uint64(nColUp)*uint64(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*8)))) (*TStatAccum)(unsafe.Pointer(p)).FnRow = uint64(0) (*TStatAccum)(unsafe.Pointer(p)).FnLimit = int32(Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv + 3*8)))) (*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*136 (*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*8)))) /* 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)*48 pSpace = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(mxSample+nCol)*48 i = 0 for { if !(i < mxSample+nCol) { break } (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*48))).FanEq = pSpace pSpace += uintptr(libc.Uint64FromInt64(8) * uint64(nColUp)) (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*48))).FanLt = pSpace pSpace += uintptr(libc.Uint64FromInt64(8) * uint64(nColUp)) (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*48))).FanDLt = pSpace pSpace += uintptr(libc.Uint64FromInt64(8) * uint64(nColUp)) goto _3 _3: ; i++ } i = 0 for { if !(i < nCol) { break } (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).FaBest + uintptr(i)*48))).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(136), __ccgo_fp(_statAccumDestructor)) } var _statInitFuncdef = TFuncDef{ FnArg: int8(4), FfuncFlags: uint32(SQLITE_UTF8), FzName: __ccgo_ts + 12206, } func init() { p := unsafe.Pointer(&_statInitFuncdef) *(*uintptr)(unsafe.Add(p, 24)) = __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 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)) 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) /* 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) /* 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)*48 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 } 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)*48 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.X__builtin___memmove_chk(tls, pMin, pMin+1*48, uint64(48)*uint64((*TStatAccum)(unsafe.Pointer(p)).FnSample-(*TStatAccum)(unsafe.Pointer(p)).FiMin-libc.Int32FromInt32(1)), libc.X__builtin_object_size(tls, pMin, 0)) pSample = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FnSample-int32(1))*48 (*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. */ /* Insert the new sample */ pSample = (*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FnSample)*48 _sampleCopy(tls, p, pSample, pNew) (*TStatAccum)(unsafe.Pointer(p)).FnSample++ /* Zero the first nEqZero entries in the anEq[] array. */ libc.X__builtin___memset_chk(tls, (*TStatSample)(unsafe.Pointer(pSample)).FanEq, 0, uint64(8)*uint64(nEqZero), libc.X__builtin_object_size(tls, (*TStatSample)(unsafe.Pointer(pSample)).FanEq, 0)) 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)*48))).FisPSample != 0 { goto _2 } if iMin < 0 || _sampleIsBetter(tls, p, (*TStatAccum)(unsafe.Pointer(p)).Fa+uintptr(iMin)*48, (*TStatAccum)(unsafe.Pointer(p)).Fa+uintptr(i)*48) != 0 { iMin = i } goto _2 _2: ; i++ } (*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)*48 *(*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)*48) != 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 } 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)*48))).FanEq + uintptr(j1)*8)) == uint64(0) { *(*TtRowcnt)(unsafe.Pointer((*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr(i)*48))).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*8))) _ = argc _ = context 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*8))) == int32(SQLITE_INTEGER) { _sampleSetRowidInt64(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, p+40, Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(argv + 2*8)))) } else { _sampleSetRowid(tls, (*TStatAccum)(unsafe.Pointer(p)).Fdb, p+40, Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + 2*8))), Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv + 2*8)))) } 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)*48) != 0 { _sampleCopy(tls, p, (*TStatAccum)(unsafe.Pointer(p)).FaBest+uintptr(i)*48, 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 + 12216, } func init() { p := unsafe.Pointer(&_statPushFuncdef) *(*uintptr)(unsafe.Add(p, 24)) = __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(80) defer tls.Free(80) 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+32 */ 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*8))) 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+12226, libc.VaList(bp+72, 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+12231, libc.VaList(bp+72, iVal)) 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)*48 if (*TStatSample)(unsafe.Pointer(pS)).FnRowid == uint32(0) { Xsqlite3_result_int64(tls, context, *(*Ti64)(unsafe.Pointer(pS + 24))) } else { Xsqlite3_result_blob(tls, context, *(*uintptr)(unsafe.Pointer(pS + 24)), int32((*TStatSample)(unsafe.Pointer(pS)).FnRowid), uintptr(-libc.Int32FromInt32(1))) } } } else { aCnt = uintptr(0) switch eCall { case int32(STAT_GET_NEQ): aCnt = (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiGet)*48))).FanEq case int32(STAT_GET_NLT): aCnt = (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiGet)*48))).FanLt default: aCnt = (*(*TStatSample)(unsafe.Pointer((*TStatAccum)(unsafe.Pointer(p)).Fa + uintptr((*TStatAccum)(unsafe.Pointer(p)).FiGet)*48))).FanDLt (*TStatAccum)(unsafe.Pointer(p)).FiGet++ break } _sqlite3StrAccumInit(tls, bp+32, 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+32, __ccgo_ts+12237, libc.VaList(bp+72, *(*TtRowcnt)(unsafe.Pointer(aCnt + uintptr(i1)*8)))) goto _3 _3: ; i1++ } if (*(*Tsqlite3_str)(unsafe.Pointer(bp + 32))).FnChar != 0 { (*(*Tsqlite3_str)(unsafe.Pointer(bp + 32))).FnChar-- } _sqlite3ResultStrAccum(tls, context, bp+32) } } _ = argc } var _statGetFuncdef = TFuncDef{ FnArg: int8(libc.Int32FromInt32(1) + libc.Int32FromInt32(IsStat4)), FfuncFlags: uint32(SQLITE_UTF8), FzName: __ccgo_ts + 12243, } func init() { p := unsafe.Pointer(&_statGetFuncdef) *(*uintptr)(unsafe.Add(p, 24)) = __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)) _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 addrGotoEnd, addrIsNull, addrNext, addrNextRow, 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, addrGotoEnd, addrIsNull, addrNext, addrNextRow, 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) 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+12252, (*TTable)(unsafe.Pointer(pTab)).FzName, uint32('\\')) == 0 { /* Do not gather statistics on system tables */ return } iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_ANALYZE), (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0), (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FzDbSName) != 0 { return } if (*Tsqlite3)(unsafe.Pointer(db)).FxPreUpdateCallback != 0 { pStat1 = _sqlite3DbMallocZero(tls, db, libc.Uint64FromInt64(104)+libc.Uint64FromInt32(13)) if pStat1 == uintptr(0) { return } (*TTable)(unsafe.Pointer(pStat1)).FzName = pStat1 + 1*104 libc.X__builtin___memcpy_chk(tls, (*TTable)(unsafe.Pointer(pStat1)).FzName, __ccgo_ts+12055, uint64(13), libc.X__builtin_object_size(tls, (*TTable)(unsafe.Pointer(pStat1)).FzName, 0)) (*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 + 100))&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 + 100))&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(): ** ** regChng = 0 ** Rewind csr ** if eof(csr){ ** stat_init() with count = 0; ** goto end_of_scan; ** } ** count() ** stat_init() ** 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. */ _sqlite3VdbeAddOp3(tls, v, int32(OP_OpenRead), iIdxCur, int32((*TIndex)(unsafe.Pointer(pIdx)).Ftnum), iDb) _sqlite3VdbeSetP4KeyInfo(tls, pParse, pIdx) /* Implementation of the following: ** ** regChng = 0 ** Rewind csr ** if eof(csr){ ** stat_init() with count = 0; ** goto end_of_scan; ** } ** count() ** stat_init() ** goto chng_addr_0; */ _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit, regTemp2) /* Arguments to stat_init(): ** (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)) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol), regRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_Count), iIdxCur, regTemp, libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) != uint32(0))) _sqlite3VdbeAddFunctionCall(tls, pParse, 0, regStat+int32(1), regStat, int32(4), uintptr(unsafe.Pointer(&_statInitFuncdef)), 0) addrGotoEnd = _sqlite3VdbeAddOp1(tls, v, int32(OP_Rewind), iIdxCur) _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(4)*uint64(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)*8))) _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) { 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)))) _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)) } } _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. */ if (*TIndex)(unsafe.Pointer(pIdx)).FpPartIdxWhere != 0 { /* Partial indexes might get a zero-entry in sqlite_stat1. But ** an empty table is omitted from sqlite_stat1. */ _sqlite3VdbeJumpHere(tls, v, addrGotoEnd) addrGotoEnd = 0 } _callStatGet(tls, pParse, regStat, STAT_GET_STAT1, regStat1) _sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), regTabname, int32(3), regTemp, __ccgo_ts+12262, 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) /* No STAT4 data is generated if the number of rows is zero */ if addrGotoEnd == 0 { _sqlite3VdbeAddOp2(tls, v, int32(OP_Cast), regStat1, int32(SQLITE_AFF_INTEGER)) addrGotoEnd = _sqlite3VdbeAddOp1(tls, v, int32(OP_IfNot), regStat1) } 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 + 100))&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 */ } 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 */ if addrGotoEnd != 0 { _sqlite3VdbeJumpHere(tls, v, addrGotoEnd) } 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) _sqlite3VdbeAddOp4(tls, v, int32(OP_MakeRecord), regTabname, int32(3), regTemp, __ccgo_ts+12262, 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)*32))).FpSchema _sqlite3BeginWriteOperation(tls, pParse, 0, iDb) iStatCur = (*TParse)(unsafe.Pointer(pParse)).FnTab *(*int32)(unsafe.Pointer(pParse + 52)) += 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 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 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 + 52)) += int32(3) if pOnlyIdx != 0 { _openStatTable(tls, pParse, iDb, iStatCur, (*TIndex)(unsafe.Pointer(pOnlyIdx)).FzName, __ccgo_ts+12266) } else { _openStatTable(tls, pParse, iDb, iStatCur, (*TTable)(unsafe.Pointer(pTab)).FzName, __ccgo_ts+12270) } _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. */ if SQLITE_OK != _sqlite3ReadSchema(tls, pParse) { return } 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)*32))).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 + 1664 } i = 0 for { if !(*(*int8)(unsafe.Pointer(z)) != 0 && i < nOut) { break } v = uint64(0) for { v2 = int32(*(*int8)(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(*(*int8)(unsafe.Pointer(z))) == int32(' ') { z++ } goto _1 _1: ; i++ } if pIndex != 0 { libc.SetBitFieldPtr16Uint32(pIndex+100, libc.Uint32FromInt32(0), 2, 0x4) libc.SetBitFieldPtr16Uint32(pIndex+100, libc.Uint32FromInt32(0), 6, 0x40) for *(*int8)(unsafe.Pointer(z)) != 0 { if Xsqlite3_strglob(tls, __ccgo_ts+12274, z) == 0 { libc.SetBitFieldPtr16Uint32(pIndex+100, libc.Uint32FromInt32(1), 2, 0x4) } else { if Xsqlite3_strglob(tls, __ccgo_ts+12285, 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+12295, z) == 0 { libc.SetBitFieldPtr16Uint32(pIndex+100, libc.Uint32FromInt32(1), 6, 0x40) } } } for int32(*(*int8)(unsafe.Pointer(z))) != 0 && int32(*(*int8)(unsafe.Pointer(z))) != int32(' ') { z++ } for int32(*(*int8)(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+100, 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(160) defer tls.Free(160) var aiRowEst, pIndex, pInfo, pTable, z uintptr var nCol int32 var _ /* fakeIdx at bp+0 */ TIndex _, _, _, _, _, _ = aiRowEst, nCol, pIndex, pInfo, pTable, z pInfo = pData _ = NotUsed _ = argc if argv == uintptr(0) || *(*uintptr)(unsafe.Pointer(argv)) == uintptr(0) || *(*uintptr)(unsafe.Pointer(argv + 2*8)) == 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*8)) == uintptr(0) { pIndex = uintptr(0) } else { if Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(argv)), *(*uintptr)(unsafe.Pointer(argv + 1*8))) == 0 { pIndex = _sqlite3PrimaryKeyIndex(tls, pTable) } else { pIndex = _sqlite3FindIndex(tls, (*TanalysisInfo)(unsafe.Pointer(pInfo)).Fdb, *(*uintptr)(unsafe.Pointer(argv + 1*8)), (*TanalysisInfo)(unsafe.Pointer(pInfo)).FzDatabase) } } z = *(*uintptr)(unsafe.Pointer(argv + 2*8)) 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(8)*uint64(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+100, libc.Uint32FromInt32(0), 2, 0x4) _decodeIntArray(tls, z, nCol, aiRowEst, (*TIndex)(unsafe.Pointer(pIndex)).FaiRowLogEst, pIndex) libc.SetBitFieldPtr16Uint32(pIndex+100, 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 + 48)) |= uint32(TF_HasStat1) } } else { (*(*TIndex)(unsafe.Pointer(bp))).FszIdxRow = (*TTable)(unsafe.Pointer(pTable)).FszTabRow _decodeIntArray(tls, z, int32(1), uintptr(0), pTable+58, bp) (*TTable)(unsafe.Pointer(pTable)).FszTabRow = (*(*TIndex)(unsafe.Pointer(bp))).FszIdxRow *(*Tu32)(unsafe.Pointer(pTable + 48)) |= 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 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)*40 _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))*40 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)*40))).FanDLt + uintptr(iCol)*8)) != *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(i+int32(1))*40))).FanDLt + uintptr(iCol)*8)) { sumEq += *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(i)*40))).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[] */ 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) if pIdx == uintptr(0) { continue } if (*TIndex)(unsafe.Pointer(pIdx)).FaSample != uintptr(0) { /* The same index appears in sqlite_stat4 under multiple names */ continue } if !((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 100))&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(uint64(40) * uint64(nSample)) nByte = int32(uint64(nByte) + libc.Uint64FromInt64(8)*uint64(nIdxCol)*libc.Uint64FromInt32(3)*uint64(nSample)) nByte = int32(uint64(nByte) + uint64(nIdxCol)*libc.Uint64FromInt64(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)*40 (*TIndex)(unsafe.Pointer(pIdx)).FaAvgEq = pSpace pSpace += uintptr(nIdxCol) * 8 *(*Tu32)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable + 48)) |= uint32(TF_HasStat4) i = 0 for { if !(i < nSample) { break } (*(*TIndexSample)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSample + uintptr(i)*40))).FanEq = pSpace pSpace += uintptr(nIdxCol) * 8 (*(*TIndexSample)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSample + uintptr(i)*40))).FanLt = pSpace pSpace += uintptr(nIdxCol) * 8 (*(*TIndexSample)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSample + uintptr(i)*40))).FanDLt = pSpace pSpace += uintptr(nIdxCol) * 8 goto _1 _1: ; i++ } } 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)*40 _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.X__builtin___memcpy_chk(tls, (*TIndexSample)(unsafe.Pointer(pSample)).Fp, Xsqlite3_column_blob(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(4)), uint64((*TIndexSample)(unsafe.Pointer(pSample)).Fn), libc.X__builtin_object_size(tls, (*TIndexSample)(unsafe.Pointer(pSample)).Fp, 0)) } (*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 if v2 = (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_Stat4)) == uint32(0); v2 { v1 = _sqlite3FindTable(tls, db, __ccgo_ts+12081, zDb) pStat4 = v1 } if v2 && v1 != uintptr(0) && int32((*TTable)(unsafe.Pointer(pStat4)).FeTabType) == TABTYP_NORM { rc = _loadStatTbl(tls, db, __ccgo_ts+12307, __ccgo_ts+12376, 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)*32))).FpSchema /* Clear any prior statistics */ i = (*THash)(unsafe.Pointer(pSchema + 8)).Ffirst for { if !(i != 0) { break } pTab = (*THashElem)(unsafe.Pointer(i)).Fdata *(*Tu32)(unsafe.Pointer(pTab + 48)) &= uint32(^libc.Int32FromInt32(TF_HasStat1)) goto _1 _1: ; i = (*THashElem)(unsafe.Pointer(i)).Fnext } i = (*THash)(unsafe.Pointer(pSchema + 32)).Ffirst for { if !(i != 0) { break } pIdx = (*THashElem)(unsafe.Pointer(i)).Fdata libc.SetBitFieldPtr16Uint32(pIdx+100, 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)*32))).FzDbSName v3 = _sqlite3FindTable(tls, db, __ccgo_ts+12055, (*(*TanalysisInfo)(unsafe.Pointer(bp))).FzDatabase) pStat1 = v3 if v3 != 0 && int32((*TTable)(unsafe.Pointer(pStat1)).FeTabType) == TABTYP_NORM { zSql = _sqlite3MPrintf(tls, db, __ccgo_ts+12428, libc.VaList(bp+24, (*(*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 */ i = (*THash)(unsafe.Pointer(pSchema + 32)).Ffirst for { if !(i != 0) { break } pIdx1 = (*THashElem)(unsafe.Pointer(i)).Fdata if !(int32(uint32(*(*uint16)(unsafe.Pointer(pIdx1 + 100))&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 + 32)).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)*32))).FzDbSName, zName) == 0 || iDb == 0 && _sqlite3StrICmp(tls, __ccgo_ts+6997, 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(64) defer tls.Free(64) var aNew, db, pNew, pNewSchema, pPager, zFile, zName uintptr var i, iDb, rc int32 var _ /* flags at bp+16 */ uint32 var _ /* pNewBt at bp+40 */ uintptr var _ /* pVfs at bp+32 */ uintptr var _ /* zErr at bp+8 */ uintptr var _ /* zErrDyn at bp+24 */ 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 + 8)) = uintptr(0) /* New array of Db pointers */ pNew = uintptr(0) /* Db object for the newly attached database */ *(*uintptr)(unsafe.Pointer(bp + 24)) = uintptr(0) _ = NotUsed zFile = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) zName = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) if zFile == uintptr(0) { zFile = __ccgo_ts + 1664 } if zName == uintptr(0) { zName = __ccgo_ts + 1664 } if int32(uint32(*(*uint8)(unsafe.Pointer(db + 192 + 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 + 40)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 32)) = Xsqlite3_vfs_find(tls, __ccgo_ts+4379) if *(*uintptr)(unsafe.Pointer(bp + 32)) == uintptr(0) { return } rc = _sqlite3BtreeOpen(tls, *(*uintptr)(unsafe.Pointer(bp + 32)), __ccgo_ts+12469, db, bp+40, 0, int32(SQLITE_OPEN_MAIN_DB)) if rc == SQLITE_OK { pNewSchema = _sqlite3SchemaGet(tls, db, *(*uintptr)(unsafe.Pointer(bp + 40))) 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)*32 if (*TDb)(unsafe.Pointer(pNew)).FpBt != 0 { _sqlite3BtreeClose(tls, (*TDb)(unsafe.Pointer(pNew)).FpBt) } (*TDb)(unsafe.Pointer(pNew)).FpBt = *(*uintptr)(unsafe.Pointer(bp + 40)) (*TDb)(unsafe.Pointer(pNew)).FpSchema = pNewSchema } else { _sqlite3BtreeClose(tls, *(*uintptr)(unsafe.Pointer(bp + 40))) 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 + 136 + 7*4))+int32(2) { *(*uintptr)(unsafe.Pointer(bp + 24)) = _sqlite3MPrintf(tls, db, __ccgo_ts+12472, libc.VaList(bp+56, *(*int32)(unsafe.Pointer(db + 136 + 7*4)))) goto attach_error } i = 0 for { if !(i < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } if _sqlite3DbIsNamed(tls, db, i, zName) != 0 { *(*uintptr)(unsafe.Pointer(bp + 24)) = _sqlite3MPrintf(tls, db, __ccgo_ts+12509, libc.VaList(bp+56, 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+696 { aNew = _sqlite3DbMallocRawNN(tls, db, libc.Uint64FromInt64(32)*libc.Uint64FromInt32(3)) if aNew == uintptr(0) { return } libc.X__builtin___memcpy_chk(tls, aNew, (*Tsqlite3)(unsafe.Pointer(db)).FaDb, libc.Uint64FromInt64(32)*libc.Uint64FromInt32(2), libc.X__builtin_object_size(tls, aNew, 0)) } else { aNew = _sqlite3DbRealloc(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FaDb, uint64(32)*uint64((*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)*32 libc.X__builtin___memset_chk(tls, pNew, 0, uint64(32), libc.X__builtin_object_size(tls, pNew, 0)) /* 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 + 16)) = (*Tsqlite3)(unsafe.Pointer(db)).FopenFlags rc = _sqlite3ParseUri(tls, (*Tsqlite3_vfs)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpVfs)).FzName, zFile, bp+16, bp+32, bp, bp+8) if rc != SQLITE_OK { if rc == int32(SQLITE_NOMEM) { _sqlite3OomFault(tls, db) } Xsqlite3_result_error(tls, context, *(*uintptr)(unsafe.Pointer(bp + 8)), -int32(1)) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) return } *(*uint32)(unsafe.Pointer(bp + 16)) |= uint32(SQLITE_OPEN_MAIN_DB) rc = _sqlite3BtreeOpen(tls, *(*uintptr)(unsafe.Pointer(bp + 32)), *(*uintptr)(unsafe.Pointer(bp)), db, pNew+8, 0, int32(*(*uint32)(unsafe.Pointer(bp + 16)))) (*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 + 24)) = _sqlite3MPrintf(tls, db, __ccgo_ts+12539, 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 + 24)) = _sqlite3MPrintf(tls, db, __ccgo_ts+12568, 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 + 44)) &= uint32(^libc.Int32FromInt32(DBFLAG_SchemaKnownOk)) if !(int32(uint32(*(*uint8)(unsafe.Pointer(db + 192 + 8))&0x4>>2)) != 0) { rc = _sqlite3Init(tls, db, bp+24) } _sqlite3BtreeLeaveAll(tls, db) } if rc != 0 { if !(int32(uint32(*(*uint8)(unsafe.Pointer(db + 192 + 8))&0x4>>2)) != 0) { iDb = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1) if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FpBt != 0 { _sqlite3BtreeClose(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FpBt) (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FpBt = uintptr(0) (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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+12664, libc.VaList(bp+136, zName)) goto detach_error } if i < int32(2) { Xsqlite3_snprintf(tls, int32(128), bp, __ccgo_ts+12685, 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+12711, 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. */ pEntry = (*THash)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).FpSchema + 56)).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(64) defer tls.Free(64) 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.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) (*(*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) { 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)) 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 + 12733, } func init() { p := unsafe.Pointer(&_detach_func) *(*uintptr)(unsafe.Add(p, 24)) = __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 + 12747, } func init() { p := unsafe.Pointer(&_attach_func) *(*uintptr)(unsafe.Add(p, 24)) = __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 + 40)) 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+12761, 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 + 40)) 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+12785, 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+60+4, libc.Uint32FromInt32(1), 9, 0x200) } (*TSrcItem)(unsafe.Pointer(pItem)).FpSchema = (*TDbFixer)(unsafe.Pointer(pFix)).FpSchema libc.SetBitFieldPtr16Uint32(pItem+60+4, libc.Uint32FromInt32(1), 7, 0x80) } if int32(uint32(*(*uint16)(unsafe.Pointer(pList + 8 + uintptr(i)*104 + 60 + 4))&0x400>>10)) == 0 && _sqlite3WalkExpr(tls, pFix+8, *(*uintptr)(unsafe.Pointer(pList + 8 + uintptr(i)*104 + 72))) != 0 { return int32(WRC_Abort) } goto _1 _1: ; i++ pItem += 104 } 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 + 16 + uintptr(i)*48))).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 (*TDbFixer)(unsafe.Pointer(pFix)).FpParse = pParse (*TDbFixer)(unsafe.Pointer(pFix)).FzDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FzDbSName (*TDbFixer)(unsafe.Pointer(pFix)).FpSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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 + 8 + 40)) = 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(128) defer tls.Free(128) var res int32 var _ /* s at bp+0 */ TSelect _ = res res = 0 if pList != 0 { libc.X__builtin___memset_chk(tls, bp, 0, uint64(128), libc.X__builtin_object_size(tls, bp, 0)) (*(*TSelect)(unsafe.Pointer(bp))).FpSrc = pList res = _sqlite3WalkSelect(tls, pFix+8, bp) } return res } func _sqlite3FixSelect(tls *libc.TLS, pFix uintptr, pSelect uintptr) (r int32) { return _sqlite3WalkSelect(tls, pFix+8, pSelect) } func _sqlite3FixExpr(tls *libc.TLS, pFix uintptr, pExpr uintptr) (r int32) { return _sqlite3WalkExpr(tls, pFix+8, pExpr) } func _sqlite3FixTriggerStep(tls *libc.TLS, pFix uintptr, pStep uintptr) (r int32) { var pUp uintptr _ = pUp for pStep != 0 { if _sqlite3WalkSelect(tls, pFix+8, (*TTriggerStep)(unsafe.Pointer(pStep)).FpSelect) != 0 || _sqlite3WalkExpr(tls, pFix+8, (*TTriggerStep)(unsafe.Pointer(pStep)).FpWhere) != 0 || _sqlite3WalkExprList(tls, pFix+8, (*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+8, (*TUpsert)(unsafe.Pointer(pUp)).FpUpsertTarget) != 0 || _sqlite3WalkExpr(tls, pFix+8, (*TUpsert)(unsafe.Pointer(pUp)).FpUpsertTargetWhere) != 0 || _sqlite3WalkExprList(tls, pFix+8, (*TUpsert)(unsafe.Pointer(pUp)).FpUpsertSet) != 0 || _sqlite3WalkExpr(tls, pFix+8, (*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+12831, 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)*32))).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+12854, libc.VaList(bp+8, zTab, zCol)) if (*Tsqlite3)(unsafe.Pointer(db)).FnDb > int32(2) || iDb != 0 { z = Xsqlite3_mprintf(tls, __ccgo_ts+12860, libc.VaList(bp+8, zDb, z)) } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12866, 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 */ 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 { iSrc = 0 for { if !(iSrc < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) { break } if (*TExpr)(unsafe.Pointer(pExpr)).FiTable == (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(iSrc)*104))).FiCursor { pTab = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(iSrc)*104))).FpTab break } goto _1 _1: ; iSrc++ } } iCol = int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) if pTab == uintptr(0) { return } if iCol >= 0 { zCol = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*16))).FzCnName } else { if int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) >= 0 { zCol = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TTable)(unsafe.Pointer(pTab)).FiPKey)*16))).FzCnName } else { zCol = __ccgo_ts + 8378 } } 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. */ 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+12893, 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) { (*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 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)*24 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(uint64(24) * uint64((*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 + 156 v3 = *(*int32)(unsafe.Pointer(v4)) *(*int32)(unsafe.Pointer(v4))++ p = (*TParse)(unsafe.Pointer(pToplevel)).FaTableLock + uintptr(v3)*24 (*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)*32))).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 i = 0 for { if !(i < (*TParse)(unsafe.Pointer(pParse)).FnTableLock) { break } p = (*TParse)(unsafe.Pointer(pParse)).FaTableLock + uintptr(i)*24 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 db = (*TParse)(unsafe.Pointer(pParse)).Fdb 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 } /* 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) } } if v != 0 { if (*TParse)(unsafe.Pointer(pParse)).FbReturning != 0 { pReturning = *(*uintptr)(unsafe.Pointer(pParse + 208)) 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. */ _sqlite3VdbeJumpHere(tls, v, 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+6997) == 0 { i = 0 } else { return uintptr(0) } } p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32))).FpSchema+8, zName) if p == uintptr(0) && Xsqlite3_strnicmp(tls, zName, __ccgo_ts+6937, int32(7)) == 0 { if i == int32(1) { if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6964+7) == 0 || _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6983+7) == 0 || _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6478+7) == 0 { p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).FpSchema+8, __ccgo_ts+6945) } } else { if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6983+7) == 0 { p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32))).FpSchema+8, __ccgo_ts+6478) } } } } else { /* Match against TEMP first */ p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).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 } p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32))).FpSchema+8, zName) if p != 0 { break } goto _2 _2: ; i++ } if p == uintptr(0) && Xsqlite3_strnicmp(tls, zName, __ccgo_ts+6937, int32(7)) == 0 { if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6983+7) == 0 { p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema+8, __ccgo_ts+6478) } else { if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6964+7) == 0 { p = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).FpSchema+8, __ccgo_ts+6945) } } } } 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+576, zName) if pMod == uintptr(0) && Xsqlite3_strnicmp(tls, zName, __ccgo_ts+12908, 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 + 12916 } else { v1 = __ccgo_ts + 12929 } zMsg = v1 if zDbase != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7201, libc.VaList(bp+8, zMsg, zDbase, zName)) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+7272, libc.VaList(bp+8, zMsg, zName)) } } else { } 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 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)*32))).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+6937, int32(7)) == 0 { if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6478+7) == 0 { return __ccgo_ts + 6983 } if _sqlite3StrICmp(tls, zName+uintptr(7), __ccgo_ts+6945+7) == 0 { return __ccgo_ts + 6964 } } 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. */ 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)*32))).FpSchema if zDb != 0 && _sqlite3DbIsNamed(tls, db, j, zDb) == 0 { goto _1 } p = _sqlite3HashFind(tls, pSchema+32, 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 + 100))&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 pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FpSchema + 32 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 + 44)) |= 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)*32 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)*32)) = *(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32)) } 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+696 { libc.X__builtin___memcpy_chk(tls, db+696, (*Tsqlite3)(unsafe.Pointer(db)).FaDb, libc.Uint64FromInt32(2)*libc.Uint64FromInt64(32), libc.X__builtin_object_size(tls, db+696, 0)) _sqlite3DbFree(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FaDb) (*Tsqlite3)(unsafe.Pointer(db)).FaDb = db + 696 } } // 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 if iDb >= 0 { p1 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FpSchema + 114 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(DB_ResetWanted)) p2 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).FpSchema + 114 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(DB_ResetWanted)) *(*Tu32)(unsafe.Pointer(db + 44)) &= 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)*32))).FpSchema)).FschemaFlags)&int32(DB_ResetWanted) == int32(DB_ResetWanted) { _sqlite3SchemaClear(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32))).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)*32 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)*32))).FpSchema + 114 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(DB_ResetWanted)) } } goto _1 _1: ; i++ } *(*Tu32)(unsafe.Pointer(db + 44)) &= 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 + 44)) &= 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 pList = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 64))).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 + 64))).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))*32))).FpExpr) (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt)-int32(1))*32))).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 + 64))).FpDfltList == uintptr(0) { return uintptr(0) } if (*TExprList)(unsafe.Pointer((*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 64))).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 + 64))).FpDfltList + 8 + uintptr(int32((*TColumn)(unsafe.Pointer(pCol)).FiDflt)-int32(1))*32))).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 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.X__builtin___memcpy_chk(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName+uintptr(n), zColl, uint64(nColl), libc.X__builtin_object_size(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName+uintptr(n), 0)) p1 = pCol + 14 *(*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 *(*int8)(unsafe.Pointer(z)) != 0 { z++ } if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_HASTYPE) != 0 { for cond := true; cond; cond = *(*int8)(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 v1 = (*TTable)(unsafe.Pointer(pTable)).FaCol pCol = v1 if v1 != uintptr(0) { i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTable)).FnCol)) { break } _sqlite3DbFree(tls, db, (*TColumn)(unsafe.Pointer(pCol)).FzCnName) goto _2 _2: ; i++ pCol += 16 } _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 + 64))).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 + 64))).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 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+32, zName, uintptr(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 { _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 */ } 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. */ if !(pTable != 0) { return } if v3 = (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0); v3 { v2 = pTable + 44 *(*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 /* Zero-length table names are allowed */ pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32 p = _sqlite3HashInsert(tls, (*TDb)(unsafe.Pointer(pDb)).FpSchema+8, zTabName, uintptr(0)) _sqlite3DeleteTable(tls, db, p) *(*Tu32)(unsafe.Pointer(db + 44)) |= 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+6478) _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)*32 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+6997, zName) { break } goto _1 _1: ; i-- pDb -= 32 } } 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 if (*TToken)(unsafe.Pointer(pName2)).Fn > uint32(0) { if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12943, 0) return -int32(1) } *(*uintptr)(unsafe.Pointer(pUnqual)) = pName2 iDb = _sqlite3FindDb(tls, db, pName1) if iDb < 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12960, libc.VaList(bp+8, pName1)) return -int32(1) } } else { 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 + 192 + 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*8))) != 0 || Xsqlite3_stricmp(tls, zTblName, *(*uintptr)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).Finit1.FazInit + 2*8))) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1664, 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+6937, int32(7)) || _sqlite3ReadOnlyShadowTables(tls, db) != 0 && _sqlite3ShadowTableName(tls, db, zName) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+12980, 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 + 100))&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)*16))).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 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)*16))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { n++ } goto _1 _1: ; i++ } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*16))).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 + 56 *(*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 + 6945 } else { v1 = __ccgo_ts + 6478 } 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+13022, 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 + 11269 } else { v2 = __ccgo_ts + 9531 } 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) } zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FzDbSName if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && isTemp == int32(1) { v3 = __ccgo_ts + 6945 } else { v3 = __ccgo_ts + 6478 } 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)*32))).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 + 11269 } else { v4 = __ccgo_ts + 9531 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13063, libc.VaList(bp+16, v4, *(*uintptr)(unsafe.Pointer(bp)))) } else { _sqlite3CodeVerifySchema(tls, pParse, iDb) _sqlite3ForceNotReadOnly(tls, pParse) } goto begin_table_error } if _sqlite3FindIndex(tls, db, zName, zDb1) != uintptr(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13084, libc.VaList(bp+16, zName)) goto begin_table_error } } pTable = _sqlite3DbMallocZero(tls, db, uint64(104)) if pTable == uintptr(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)*32))).FpSchema (*TTable)(unsafe.Pointer(pTable)).FnTabRef = uint32(1) (*TTable)(unsafe.Pointer(pTable)).FnRowLogEst = int16(200) (*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 + 56 *(*int32)(unsafe.Pointer(v9))++ v8 = *(*int32)(unsafe.Pointer(v9)) v7 = v8 (*TParse)(unsafe.Pointer(pParse)).FregRowid = v7 reg1 = v7 v12 = pParse + 56 *(*int32)(unsafe.Pointer(v12))++ v11 = *(*int32)(unsafe.Pointer(v12)) v10 = v11 (*TParse)(unsafe.Pointer(pParse)).FregRoot = v10 reg2 = v10 v14 = pParse + 56 *(*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 { (*TParse)(unsafe.Pointer(pParse)).Fu1.FaddrCrTab = _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]int8{ 0: int8(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*32))).FpSchema + 56 _sqlite3HashInsert(tls, pHash, pRet+196, 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+13119, 0) } else { } (*TParse)(unsafe.Pointer(pParse)).FbReturning = uint8(1) pRet = _sqlite3DbMallocZero(tls, db, uint64(240)) if pRet == uintptr(0) { _sqlite3ExprListDelete(tls, db, pList) return } *(*uintptr)(unsafe.Pointer(pParse + 208)) = 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+196, __ccgo_ts+13153, libc.VaList(bp+8, pParse)) (*TReturning)(unsafe.Pointer(pRet)).FretTrig.FzName = pRet + 196 (*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*32))).FpSchema (*TReturning)(unsafe.Pointer(pRet)).FretTrig.FpTabSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).FpSchema (*TReturning)(unsafe.Pointer(pRet)).FretTrig.Fstep_list = pRet + 88 (*TReturning)(unsafe.Pointer(pRet)).FretTStep.Fop = uint8(TK_RETURNING) (*TReturning)(unsafe.Pointer(pRet)).FretTStep.FpTrig = pRet + 16 (*TReturning)(unsafe.Pointer(pRet)).FretTStep.FpExprList = pList pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).FpSchema + 56 if _sqlite3HashInsert(tls, pHash, pRet+196, pRet+16) == pRet+16 { _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(48) defer tls.Free(48) *(*TToken)(unsafe.Pointer(bp)) = _sName *(*TToken)(unsafe.Pointer(bp + 16)) = _sType var aNew, db, p, pCol, z, zType, v1, p4 uintptr var affinity int8 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 = int8(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 + 136 + 2*4)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13173, libc.VaList(bp+40, (*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 + 16))).Fn >= uint32(16) && Xsqlite3_strnicmp(tls, (*(*TToken)(unsafe.Pointer(bp + 16))).Fz+uintptr((*(*TToken)(unsafe.Pointer(bp + 16))).Fn-libc.Uint32FromInt32(6)), __ccgo_ts+13196, int32(6)) == 0 { (*(*TToken)(unsafe.Pointer(bp + 16))).Fn -= uint32(6) for (*(*TToken)(unsafe.Pointer(bp + 16))).Fn > uint32(0) && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer((*(*TToken)(unsafe.Pointer(bp + 16))).Fz + uintptr((*(*TToken)(unsafe.Pointer(bp + 16))).Fn-uint32(1)))))])&int32(0x01) != 0 { (*(*TToken)(unsafe.Pointer(bp + 16))).Fn-- } if (*(*TToken)(unsafe.Pointer(bp + 16))).Fn >= uint32(9) && Xsqlite3_strnicmp(tls, (*(*TToken)(unsafe.Pointer(bp + 16))).Fz+uintptr((*(*TToken)(unsafe.Pointer(bp + 16))).Fn-libc.Uint32FromInt32(9)), __ccgo_ts+13203, int32(9)) == 0 { (*(*TToken)(unsafe.Pointer(bp + 16))).Fn -= uint32(9) for (*(*TToken)(unsafe.Pointer(bp + 16))).Fn > uint32(0) && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer((*(*TToken)(unsafe.Pointer(bp + 16))).Fz + uintptr((*(*TToken)(unsafe.Pointer(bp + 16))).Fn-uint32(1)))))])&int32(0x01) != 0 { (*(*TToken)(unsafe.Pointer(bp + 16))).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 + 16))).Fn >= uint32(3) { _sqlite3DequoteToken(tls, bp+16) i = 0 for { if !(i < int32(SQLITE_N_STDTYPE)) { break } if (*(*TToken)(unsafe.Pointer(bp + 16))).Fn == uint32(_sqlite3StdTypeLen[i]) && Xsqlite3_strnicmp(tls, (*(*TToken)(unsafe.Pointer(bp + 16))).Fz, _sqlite3StdType[i], int32((*(*TToken)(unsafe.Pointer(bp + 16))).Fn)) == 0 { (*(*TToken)(unsafe.Pointer(bp + 16))).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 + 16))).Fn)+libc.BoolInt64((*(*TToken)(unsafe.Pointer(bp + 16))).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.X__builtin___memcpy_chk(tls, z, (*(*TToken)(unsafe.Pointer(bp))).Fz, uint64((*(*TToken)(unsafe.Pointer(bp))).Fn), libc.X__builtin_object_size(tls, z, 0)) *(*int8)(unsafe.Pointer(z + uintptr((*(*TToken)(unsafe.Pointer(bp))).Fn))) = 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)*16))).FhName) == int32(hName) && _sqlite3StrICmp(tls, z, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(p)).FaCol + uintptr(i)*16))).FzCnName) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13213, libc.VaList(bp+40, 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)+libc.Int64FromInt32(1))*uint64(16)) 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)*16 libc.X__builtin___memset_chk(tls, pCol, 0, uint64(16), libc.X__builtin_object_size(tls, pCol, 0)) (*TColumn)(unsafe.Pointer(pCol)).FzCnName = z (*TColumn)(unsafe.Pointer(pCol)).FhName = hName if (*(*TToken)(unsafe.Pointer(bp + 16))).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+8, uint32(eType), 4, 0xf0) (*TColumn)(unsafe.Pointer(pCol)).FszEst = szEst } else { zType = z + uintptr(_sqlite3Strlen30(tls, z)) + uintptr(1) libc.X__builtin___memcpy_chk(tls, zType, (*(*TToken)(unsafe.Pointer(bp + 16))).Fz, uint64((*(*TToken)(unsafe.Pointer(bp + 16))).Fn), libc.X__builtin_object_size(tls, zType, 0)) *(*int8)(unsafe.Pointer(zType + uintptr((*(*TToken)(unsafe.Pointer(bp + 16))).Fn))) = 0 _sqlite3Dequote(tls, zType) (*TColumn)(unsafe.Pointer(pCol)).Faffinity = _sqlite3AffinityType(tls, zType, pCol) p4 = pCol + 14 *(*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))*16 libc.SetBitFieldPtr8Uint32(pCol+8, uint32(uint8(onError)), 0, 0xf) *(*Tu32)(unsafe.Pointer(p + 48)) |= 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 } if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn))) == int32((*TTable)(unsafe.Pointer(p)).FnCol)-int32(1) { libc.SetBitFieldPtr16Uint32(pIdx+100, 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 int8) { bp := tls.Alloc(16) defer tls.Free(16) var aff int8 var h Tu32 var x Tu8 var zChar uintptr var _ /* v at bp+0 */ int32 _, _, _, _ = aff, h, x, zChar h = uint32(0) aff = int8(SQLITE_AFF_NUMERIC) zChar = uintptr(0) for *(*int8)(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(96) defer tls.Free(96) 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))*16 if !(_sqlite3ExprIsConstantOrFunction(tls, pExpr, uint8(isInit)) != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13239, libc.VaList(bp+80, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)) } else { if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13284, 0) } else { libc.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 0)) (*(*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 + 14 *(*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+13325, 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+13377, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) goto primary_key_exit } *(*Tu32)(unsafe.Pointer(pTab + 48)) |= uint32(TF_HasPrimaryKey) if pList == uintptr(0) { iCol = int32((*TTable)(unsafe.Pointer(pTab)).FnCol) - int32(1) pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*16 _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)*32))).FpExpr) _sqlite3StringToId(tls, pCExpr) if int32((*TExpr)(unsafe.Pointer(pCExpr)).Fop) == int32(TK_ID) { 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)*16))).FzCnName) == 0 { pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*16 _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 + 8))&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+52, pCExpr1) } (*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(iCol) (*TTable)(unsafe.Pointer(pTab)).FkeyConf = uint8(onError) *(*Tu32)(unsafe.Pointer(pTab + 48)) |= 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+13418, 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)*32))).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+112, int32(1)) } else { zStart++ for { if !(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zStart)))])&int32(0x01) != 0) { break } goto _1 _1: ; zStart++ } for int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zEnd + uintptr(-libc.Int32FromInt32(1)))))])&int32(0x01) != 0 { zEnd-- } (*(*TToken)(unsafe.Pointer(bp))).Fz = zStart (*(*TToken)(unsafe.Pointer(bp))).Fn = uint32(int32(int64(zEnd) - int64((*(*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)*16, 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 } 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)*16) } 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))*16 if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) == int32(PARSE_MODE_DECLARE_VTAB) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13474, 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+13517, (*TToken)(unsafe.Pointer(pType)).Fz, int32(7)) == 0 { /* no-op */ } else { if (*TToken)(unsafe.Pointer(pType)).Fn == uint32(6) && Xsqlite3_strnicmp(tls, __ccgo_ts+13525, (*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 + 14 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | int32(eType)) *(*Tu32)(unsafe.Pointer(pTab + 48)) |= 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+13532, 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 _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)*32))).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 !(*(*int8)(unsafe.Pointer(z)) != 0) { break } if int32(*(*int8)(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++ *(*int8)(unsafe.Pointer(z + uintptr(v2))) = int8('"') } j = 0 for { if !(*(*uint8)(unsafe.Pointer(zIdent + uintptr(j))) != 0) { break } v4 = i i++ *(*int8)(unsafe.Pointer(z + uintptr(v4))) = int8(*(*uint8)(unsafe.Pointer(zIdent + uintptr(j)))) if int32(*(*uint8)(unsafe.Pointer(zIdent + uintptr(j)))) == int32('"') { v5 = i i++ *(*int8)(unsafe.Pointer(z + uintptr(v5))) = int8('"') } goto _3 _3: ; j++ } if needQuote != 0 { v6 = i i++ *(*int8)(unsafe.Pointer(z + uintptr(v6))) = int8('"') } *(*int8)(unsafe.Pointer(z + uintptr(i))) = 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 += 16 } n += _identLength(tls, (*TTable)(unsafe.Pointer(p)).FzName) if n < int32(50) { zSep = __ccgo_ts + 1664 zSep2 = __ccgo_ts + 13563 zEnd = __ccgo_ts + 5518 } else { zSep = __ccgo_ts + 13565 zSep2 = __ccgo_ts + 13569 zEnd = __ccgo_ts + 13574 } 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+13577, 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))++ *(*int8)(unsafe.Pointer(zStmt + uintptr(v2))) = int8('(') 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) zType = _azType1[int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity)-int32(SQLITE_AFF_BLOB)] len1 = _sqlite3Strlen30(tls, zType) libc.X__builtin___memcpy_chk(tls, zStmt+uintptr(*(*int32)(unsafe.Pointer(bp))), zType, uint64(len1), libc.X__builtin_object_size(tls, zStmt+uintptr(*(*int32)(unsafe.Pointer(bp))), 0)) *(*int32)(unsafe.Pointer(bp)) += len1 goto _3 _3: ; i++ pCol += 16 } Xsqlite3_snprintf(tls, n-*(*int32)(unsafe.Pointer(bp)), zStmt+uintptr(*(*int32)(unsafe.Pointer(bp))), __ccgo_ts+3863, libc.VaList(bp+16, zEnd)) return zStmt } var _azType1 = [6]uintptr{ 0: __ccgo_ts + 1664, 1: __ccgo_ts + 13591, 2: __ccgo_ts + 13597, 3: __ccgo_ts + 13602, 4: __ccgo_ts + 13607, 5: __ccgo_ts + 13597, } // 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 } nByte = int32((libc.Uint64FromInt64(8) + libc.Uint64FromInt64(2) + libc.Uint64FromInt64(2) + libc.Uint64FromInt32(1)) * uint64(N)) zExtra = _sqlite3DbMallocZero(tls, db, uint64(nByte)) if zExtra == uintptr(0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memcpy_chk(tls, zExtra, (*TIndex)(unsafe.Pointer(pIdx)).FazColl, uint64(8)*uint64((*TIndex)(unsafe.Pointer(pIdx)).FnColumn), libc.X__builtin_object_size(tls, zExtra, 0)) (*TIndex)(unsafe.Pointer(pIdx)).FazColl = zExtra zExtra += uintptr(uint64(8) * uint64(N)) libc.X__builtin___memcpy_chk(tls, zExtra, (*TIndex)(unsafe.Pointer(pIdx)).FaiRowLogEst, uint64(2)*uint64(int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)+libc.Int32FromInt32(1)), libc.X__builtin_object_size(tls, zExtra, 0)) (*TIndex)(unsafe.Pointer(pIdx)).FaiRowLogEst = zExtra zExtra += uintptr(uint64(2) * uint64(N)) libc.X__builtin___memcpy_chk(tls, zExtra, (*TIndex)(unsafe.Pointer(pIdx)).FaiColumn, uint64(2)*uint64((*TIndex)(unsafe.Pointer(pIdx)).FnColumn), libc.X__builtin_object_size(tls, zExtra, 0)) (*TIndex)(unsafe.Pointer(pIdx)).FaiColumn = zExtra zExtra += uintptr(uint64(2) * uint64(N)) libc.X__builtin___memcpy_chk(tls, zExtra, (*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder, uint64((*TIndex)(unsafe.Pointer(pIdx)).FnColumn), libc.X__builtin_object_size(tls, zExtra, 0)) (*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder = zExtra (*TIndex)(unsafe.Pointer(pIdx)).FnColumn = uint16(N) libc.SetBitFieldPtr16Uint32(pIdx+100, 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 += 16 } 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)) if int32(x) < 0 { v2 = int32(1) } else { v2 = int32((*(*TColumn)(unsafe.Pointer(aCol + uintptr(x)*16))).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 j = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(iCol)*2))) i = 0 for { if !(i < nKey) { break } 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)*8)), *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(iCol)*8))) == 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)*16))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { if x < int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(8))-libc.Int32FromInt32(1) { m |= libc.Uint64FromInt32(1) << x } } goto _1 _1: ; j-- } (*TIndex)(unsafe.Pointer(pIdx)).FcolNotIdxed = ^m /* 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 + 192 + 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)*16))).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*16 + 8))&0xf>>0)) == OE_None { libc.SetBitFieldPtr8Uint32((*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*16+8, libc.Uint32FromInt32(OE_Abort), 0, 0xf) } goto _1 _1: ; i++ } *(*Tu32)(unsafe.Pointer(pTab + 48)) |= uint32(TF_HasNotNull) } /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY ** into BTREE_BLOBKEY. */ if (*TParse)(unsafe.Pointer(pParse)).Fu1.FaddrCrTab != 0 { _sqlite3VdbeChangeP3(tls, v, (*TParse)(unsafe.Pointer(pParse)).Fu1.FaddrCrTab, 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)*16))).FzCnName) pList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp, 0)) if pList == uintptr(0) { *(*Tu32)(unsafe.Pointer(pTab + 48)) &= 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+52) } (*(*TExprList_item)(unsafe.Pointer(pList + 8))).Ffg.FsortFlags = (*TParse)(unsafe.Pointer(pParse)).FiPkSortOrder (*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 + 48)) &= uint32(^libc.Int32FromInt32(TF_WithoutRowid)) return } pPk = _sqlite3PrimaryKeyIndex(tls, pTab) } else { pPk = _sqlite3PrimaryKeyIndex(tls, pTab) /* ** 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)*8)) = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(i)*8)) *(*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.SetBitFieldPtr16Uint32(pPk+100, libc.Uint32FromInt32(1), 5, 0x20) if !(int32(uint32(*(*uint8)(unsafe.Pointer(db + 192 + 8))&0x2>>1)) != 0) { libc.SetBitFieldPtr16Uint32(pPk+100, 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) { _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 + 100))&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)*8)) = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(i)*8)) if *(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaSortOrder + uintptr(i))) != 0 { /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */ libc.SetBitFieldPtr16Uint32(pIdx+100, libc.Uint32FromInt32(1), 10, 0x400) } j++ } goto _9 _9: ; i++ } 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)*16))).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)*16))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(j)*2)) = int16(i) *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(j)*8)) = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) j++ } goto _11 _11: ; i++ } _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(*(*int8)(unsafe.Pointer(zName + uintptr(nName)))) != int32('_') { return 0 } pMod = _sqlite3HashFind(tls, db+576, *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).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 */ pMod = _sqlite3HashFind(tls, db+576, *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).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 } 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 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(*(*int8)(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 + 48)) |= 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 } *(*int8)(unsafe.Pointer(zTail)) = 0 pTab = _sqlite3FindTable(tls, db, zName, uintptr(0)) *(*int8)(unsafe.Pointer(zTail)) = int8('_') 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(112) defer tls.Free(112) var addrInsLoop, addrTop, iCsr, iDb, ii, ii1, n, nNG, regRec, regRowid, regYield, v10, v4, v6, v8 int32 var colFlags Tu32 var db, p, pCol, pDb, pEnd2, pIdx, pOld, pSchema, pSelTab, pX, v, zStmt, zType, zType2, v11, v13, v5, v7, v9 uintptr var v12 Ti16 var _ /* dest at bp+0 */ TSelectDest _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addrInsLoop, addrTop, colFlags, db, iCsr, iDb, ii, ii1, n, nNG, p, pCol, pDb, pEnd2, pIdx, pOld, pSchema, pSelTab, pX, regRec, regRowid, regYield, v, zStmt, zType, zType2, v10, v11, v12, v13, 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 + 48)) |= 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+1664, 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 + 48)) |= 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 + 48)) |= uint32(TF_Strict) ii = 0 for { if !(ii < int32((*TTable)(unsafe.Pointer(p)).FnCol)) { break } pCol = (*TTable)(unsafe.Pointer(p)).FaCol + uintptr(ii)*16 if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 8))&0xf0>>4)) == COLTYPE_CUSTOM { if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_HASTYPE) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13613, libc.VaList(bp+48, (*TTable)(unsafe.Pointer(p)).FzName, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, _sqlite3ColumnType(tls, pCol, __ccgo_ts+1664))) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13646, libc.VaList(bp+48, (*TTable)(unsafe.Pointer(p)).FzName, (*TColumn)(unsafe.Pointer(pCol)).FzCnName)) } return } else { if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 8))&0xf0>>4)) == int32(COLTYPE_ANY) { (*TColumn)(unsafe.Pointer(pCol)).Faffinity = int8(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 + 8))&0xf>>0)) == OE_None { libc.SetBitFieldPtr8Uint32(pCol+8, libc.Uint32FromInt32(OE_Abort), 0, 0xf) *(*Tu32)(unsafe.Pointer(p + 48)) |= uint32(TF_HasNotNull) } goto _1 _1: ; ii++ } } /* 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+13673, 0) return } if (*TTable)(unsafe.Pointer(p)).FtabFlags&uint32(TF_HasPrimaryKey) == uint32(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13723, libc.VaList(bp+48, (*TTable)(unsafe.Pointer(p)).FzName)) return } *(*Tu32)(unsafe.Pointer(p + 48)) |= 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)*16))).FcolFlags) if colFlags&uint32(COLFLAG_GENERATED) != uint32(0) { pX = _sqlite3ColumnExpr(tls, p, (*TTable)(unsafe.Pointer(p)).FaCol+uintptr(ii1)*16) 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)*16, _sqlite3ExprAlloc(tls, db, int32(TK_NULL), uintptr(0), 0)) } } else { nNG++ } goto _2 _2: ; ii1++ } if nNG == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13755, 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 + 9531 zType2 = __ccgo_ts + 13799 } else { /* A view */ zType = __ccgo_ts + 11269 zType2 = __ccgo_ts + 13805 } /* 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 { /* Write cursor on the new table */ 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 + 52 v4 = *(*int32)(unsafe.Pointer(v5)) *(*int32)(unsafe.Pointer(v5))++ iCsr = v4 v7 = pParse + 56 *(*int32)(unsafe.Pointer(v7))++ v6 = *(*int32)(unsafe.Pointer(v7)) regYield = v6 v9 = pParse + 56 *(*int32)(unsafe.Pointer(v9))++ v8 = *(*int32)(unsafe.Pointer(v9)) regRec = v8 v11 = pParse + 56 *(*int32)(unsafe.Pointer(v11))++ v10 = *(*int32)(unsafe.Pointer(v11)) regRowid = v10 _sqlite3MayAbort(tls, pParse) _sqlite3VdbeAddOp3(tls, v, int32(OP_OpenWrite), iCsr, (*TParse)(unsafe.Pointer(pParse)).FregRoot, iDb) _sqlite3VdbeChangeP5(tls, v, uint16(OPFLAG_P2ISREG)) 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, int8(SQLITE_AFF_BLOB)) if pSelTab == uintptr(0) { return } v12 = (*TTable)(unsafe.Pointer(pSelTab)).FnCol (*TTable)(unsafe.Pointer(p)).FnNVCol = v12 (*TTable)(unsafe.Pointer(p)).FnCol = v12 (*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), iCsr, regRowid) _sqlite3VdbeAddOp3(tls, v, int32(OP_Insert), iCsr, regRec, regRowid) _sqlite3VdbeGoto(tls, v, addrInsLoop) _sqlite3VdbeJumpHere(tls, v, addrInsLoop) _sqlite3VdbeAddOp1(tls, v, int32(OP_Close), iCsr) } /* Compute the complete text of the CREATE statement */ if pSelect != 0 { zStmt = _createTableStmt(tls, db, p) } else { if tabOpts != 0 { v13 = pParse + 288 } else { v13 = pEnd } pEnd2 = v13 n = int32(int64((*TToken)(unsafe.Pointer(pEnd2)).Fz) - int64((*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fz)) if int32(*(*int8)(unsafe.Pointer((*TToken)(unsafe.Pointer(pEnd2)).Fz))) != int32(';') { n = int32(uint32(n) + (*TToken)(unsafe.Pointer(pEnd2)).Fn) } zStmt = _sqlite3MPrintf(tls, db, __ccgo_ts+13810, libc.VaList(bp+48, 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+13825, libc.VaList(bp+48, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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)*32 if (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).FpSeqTab == uintptr(0) { _sqlite3NestedParse(tls, pParse, __ccgo_ts+13923, libc.VaList(bp+48, (*TDb)(unsafe.Pointer(pDb)).FzDbSName)) } } /* Reparse everything to update our internal data structures */ _sqlite3VdbeAddParseSchemaOp(tls, v, iDb, _sqlite3MPrintf(tls, db, __ccgo_ts+13965, libc.VaList(bp+48, (*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(0x0001), 0, 0, _sqlite3MPrintf(tls, db, __ccgo_ts+13999, libc.VaList(bp+48, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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 pOld = _sqlite3HashInsert(tls, pSchema+8, (*TTable)(unsafe.Pointer(p)).FzName, p) if pOld != 0 { /* Malloc must have failed inside HashInsert() */ _sqlite3OomFault(tls, db) return } (*TParse)(unsafe.Pointer(pParse)).FpNewTable = uintptr(0) *(*Tu32)(unsafe.Pointer(db + 44)) |= 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. */ if libc.Xstrcmp(tls, (*TTable)(unsafe.Pointer(p)).FzName, __ccgo_ts+10053) == 0 { (*TSchema)(unsafe.Pointer((*TTable)(unsafe.Pointer(p)).FpSchema)).FpSeqTab = p } } if !(pSelect != 0) && int32((*TTable)(unsafe.Pointer(p)).FeTabType) == TABTYP_NORM { if (*TToken)(unsafe.Pointer(pCons)).Fz == uintptr(0) { pCons = pEnd } (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(p + 64))).FaddColOffset = int32(13) + int32(int64((*TToken)(unsafe.Pointer(pCons)).Fz)-int64((*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(128) defer tls.Free(128) var db, p, z uintptr var iDb, n int32 var _ /* pName at bp+112 */ uintptr var _ /* sEnd at bp+0 */ TToken var _ /* sFix at bp+16 */ TDbFixer _, _, _, _, _ = db, iDb, n, p, z *(*uintptr)(unsafe.Pointer(bp + 112)) = uintptr(0) db = (*TParse)(unsafe.Pointer(pParse)).Fdb if int32((*TParse)(unsafe.Pointer(pParse)).FnVar) > 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14020, 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 + 48)) |= uint32(TF_NoVisibleRowid) /* Never allow rowid in view */ _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp+112) iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(p)).FpSchema) _sqlite3FixInit(tls, bp+16, pParse, iDb, __ccgo_ts+11269, *(*uintptr)(unsafe.Pointer(bp + 112))) if _sqlite3FixSelect(tls, bp+16, 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 if int32(*(*int8)(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(int64((*(*TToken)(unsafe.Pointer(bp))).Fz) - int64((*TToken)(unsafe.Pointer(pBegin)).Fz)) z = (*TToken)(unsafe.Pointer(pBegin)).Fz for int32(_sqlite3CtypeMap[uint8(*(*int8)(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 */ 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. */ /* 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+14056, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTable)).FzName)) return int32(1) } /* 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. */ 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, int8(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+54, pTable+8) if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 && int32((*TTable)(unsafe.Pointer(pTable)).FnCol) == (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSel)).FpEList)).FnExpr { _sqlite3SubqueryColumnTypes(tls, pParse, pTable, pSel, int8(SQLITE_AFF_NONE)) } } else { /* CREATE VIEW name AS... without an argument list. Construct ** the column names from the SELECT statement that defines the view. */ (*TTable)(unsafe.Pointer(pTable)).FnCol = (*TTable)(unsafe.Pointer(pSelTab)).FnCol (*TTable)(unsafe.Pointer(pTable)).FaCol = (*TTable)(unsafe.Pointer(pSelTab)).FaCol *(*Tu32)(unsafe.Pointer(pTable + 48)) |= (*TTable)(unsafe.Pointer(pSelTab)).FtabFlags & uint32(COLFLAG_NOINSERT) (*TTable)(unsafe.Pointer(pSelTab)).FnCol = 0 (*TTable)(unsafe.Pointer(pSelTab)).FaCol = uintptr(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 + 114 *(*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) { 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 if !(int32((*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(idx)*32))).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)*32))).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)*32))).FpSchema + 114 *(*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 pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32 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 + 32 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+14086, 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+14101, libc.VaList(bp+8, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*32))).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 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) _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]int8 _, _ = i, zDbName zDbName = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*32))).FzDbSName i = int32(1) for { if !(i <= int32(4)) { break } Xsqlite3_snprintf(tls, int32(24), bp, __ccgo_ts+14168, libc.VaList(bp+32, i)) if _sqlite3FindTable(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, bp, zDbName) != 0 { _sqlite3NestedParse(tls, pParse, __ccgo_ts+12158, 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)*32 v = _sqlite3GetVdbe(tls, pParse) _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 { _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+14182, 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+14227, 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+6937, int32(7)) == 0 { if Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName+uintptr(7), __ccgo_ts+3484, int32(4)) == 0 { return 0 } if Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName+uintptr(7), __ccgo_ts+7739, 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 } if _sqlite3ReadSchema(tls, pParse) != 0 { goto exit_drop_table } if noErr != 0 { (*Tsqlite3)(unsafe.Pointer(db)).FsuppressErr++ } 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) /* 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 + 6945 } else { v1 = __ccgo_ts + 6478 } zTab = v1 zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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+14294, 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+14322, 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+14356, 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+12270, (*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 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+14388, libc.VaList(bp+8, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(p)).FaCol + uintptr(iCol)*16))).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+14451, 0) goto fk_end } else { nCol = (*TExprList)(unsafe.Pointer(pFromCol)).FnExpr } } nByte = int64(uint64(80) + uint64(nCol-libc.Int32FromInt32(1))*uint64(16) + uint64((*TToken)(unsafe.Pointer(pTo)).Fn) + uint64(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)*32))).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 (*TFKey)(unsafe.Pointer(pFKey)).FpNextFrom = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(p + 64))).FpFKey z = pFKey + 64 + uintptr(nCol)*16 (*TFKey)(unsafe.Pointer(pFKey)).FzTo = z if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenMap(tls, pParse, z, pTo) } libc.X__builtin___memcpy_chk(tls, z, (*TToken)(unsafe.Pointer(pTo)).Fz, uint64((*TToken)(unsafe.Pointer(pTo)).Fn), libc.X__builtin_object_size(tls, z, 0)) *(*int8)(unsafe.Pointer(z + uintptr((*TToken)(unsafe.Pointer(pTo)).Fn))) = 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 + 64))).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)*16))).FzCnName, (*(*TExprList_item)(unsafe.Pointer(pFromCol + 8 + uintptr(i)*32))).FzEName) == 0 { (*(*TsColMap)(unsafe.Pointer(pFKey + 64 + uintptr(i)*16))).FiFrom = j break } goto _3 _3: ; j++ } if j >= int32((*TTable)(unsafe.Pointer(p)).FnCol) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14545, libc.VaList(bp+8, (*(*TExprList_item)(unsafe.Pointer(pFromCol + 8 + uintptr(i)*32))).FzEName)) goto fk_end } if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) { _sqlite3RenameTokenRemap(tls, pParse, pFKey+64+uintptr(i)*16, (*(*TExprList_item)(unsafe.Pointer(pFromCol + 8 + uintptr(i)*32))).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)*32))).FzEName) (*(*TsColMap)(unsafe.Pointer(pFKey + 64 + uintptr(i)*16))).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)*32))).FzEName) } libc.X__builtin___memcpy_chk(tls, z, (*(*TExprList_item)(unsafe.Pointer(pToCol + 8 + uintptr(i)*32))).FzEName, uint64(n), libc.X__builtin_object_size(tls, z, 0)) *(*int8)(unsafe.Pointer(z + uintptr(n))) = 0 z += uintptr(n + int32(1)) goto _4 _4: ; i++ } } (*TFKey)(unsafe.Pointer(pFKey)).FisDeferred = uint8(0) *(*Tu8)(unsafe.Pointer(pFKey + 45)) = uint8(flags & libc.Int32FromInt32(0xff)) /* ON DELETE action */ *(*Tu8)(unsafe.Pointer(pFKey + 45 + 1)) = uint8(flags >> libc.Int32FromInt32(8) & libc.Int32FromInt32(0xff)) /* ON UPDATE action */ pNextTo = _sqlite3HashInsert(tls, (*TTable)(unsafe.Pointer(p)).FpSchema+80, (*TFKey)(unsafe.Pointer(pFKey)).FzTo, pFKey) if pNextTo == pFKey { _sqlite3OomFault(tls, db) goto fk_end } if pNextTo != 0 { (*TFKey)(unsafe.Pointer(pFKey)).FpNextTo = pNextTo (*TFKey)(unsafe.Pointer(pNextTo)).FpPrevTo = pFKey } /* Link the foreign key to the table as the last step. */ (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(p + 64))).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 + 64))).FpFKey pFKey = v2 if v2 == uintptr(0) { return } /* 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 + 52 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ /* The table that is indexed */ iTab = v1 v4 = pParse + 52 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)*32))).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) /* Open the sorter cursor if we are to use one. */ v6 = pParse + 52 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 + 100))&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.Uint64FromInt64(160)+libc.Uint64FromInt32(7))&uint64(^libc.Int32FromInt32(7)) + (uint64(8)*uint64(nCol)+uint64(7))&uint64(^libc.Int32FromInt32(7)) + (uint64(2)*uint64(int32(nCol)+libc.Int32FromInt32(1))+uint64(2)*uint64(nCol)+uint64(1)*uint64(nCol)+uint64(7))&uint64(^libc.Int32FromInt32(7))) /* Index.aSortOrder */ p = _sqlite3DbMallocZero(tls, db, uint64(nByte+nExtra)) if p != 0 { pExtra = p + uintptr((libc.Uint64FromInt64(160)+libc.Uint64FromInt32(7))&uint64(^libc.Int32FromInt32(7))) (*TIndex)(unsafe.Pointer(p)).FazColl = pExtra pExtra += uintptr((libc.Uint64FromInt64(8)*uint64(nCol) + libc.Uint64FromInt32(7)) & uint64(^libc.Int32FromInt32(7))) (*TIndex)(unsafe.Pointer(p)).FaiRowLogEst = pExtra pExtra += uintptr(uint64(2) * uint64(int32(nCol)+libc.Int32FromInt32(1))) (*TIndex)(unsafe.Pointer(p)).FaiColumn = pExtra pExtra += uintptr(uint64(2) * uint64(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)*32 + 16 + 4))&0x20>>5)) != 0 { sf = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*32))).Ffg.FsortFlags if int32(sf) == 0 || int32(sf) == int32(3) { v2 = __ccgo_ts + 14591 } else { v2 = __ccgo_ts + 14597 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14602, 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(176) defer tls.Free(176) 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+96 */ uintptr var _ /* prevCol at bp+112 */ TToken var _ /* sFix at bp+0 */ TDbFixer var _ /* zExtra at bp+104 */ 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 + 96)) = uintptr(0) /* For looping over pList */ nExtra = 0 /* Number of extra columns needed */ *(*uintptr)(unsafe.Pointer(bp + 104)) = uintptr(0) /* Extra space after the Index object */ pPk = uintptr(0) /* PRIMARY KEY index for WITHOUT ROWID tables */ if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto exit_create_index } 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. */ iDb = _sqlite3TwoPartName(tls, pParse, pName1, pName2, bp+96) if iDb < 0 { goto exit_create_index } /* 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*32))).FpSchema { iDb = int32(1) } } _sqlite3FixInit(tls, bp, pParse, iDb, __ccgo_ts+14630, *(*uintptr)(unsafe.Pointer(bp + 96))) if _sqlite3FixSrcList(tls, bp, pTblName) != 0 { /* Because the parser constructs pTblName from a single identifier, ** sqlite3FixSrcList can never fail. */ } pTab = _sqlite3LocateTableItem(tls, pParse, uint32(0), pTblName+8) if pTab == uintptr(0) { goto exit_create_index } if iDb == int32(1) && (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FpSchema != (*TTable)(unsafe.Pointer(pTab)).FpSchema { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14636, libc.VaList(bp+136, (*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 { 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)*32 if Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab)).FzName, __ccgo_ts+6937, int32(7)) == 0 && int32((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy) == 0 && pTblName != uintptr(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14686, libc.VaList(bp+136, (*TTable)(unsafe.Pointer(pTab)).FzName)) goto exit_create_index } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14714, 0) goto exit_create_index } if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+14739, 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 + 96)) != 0 { zName = _sqlite3NameFromToken(tls, db, *(*uintptr)(unsafe.Pointer(bp + 96))) if zName == uintptr(0) { goto exit_create_index } if SQLITE_OK != _sqlite3CheckObjectName(tls, pParse, zName, __ccgo_ts+14630, (*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+14773, libc.VaList(bp+136, 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+14807, libc.VaList(bp+136, zName)) } else { _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+14831, libc.VaList(bp+136, (*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 { *(*int8)(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 + 6945 } else { v2 = __ccgo_ts + 6478 } 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))*16 p3 = pCol + 14 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(COLFLAG_UNIQUE)) _sqlite3TokenInit(tls, bp+112, (*TColumn)(unsafe.Pointer(pCol)).FzCnName) pList = _sqlite3ExprListAppend(tls, pParse, uintptr(0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+112, 0)) if pList == uintptr(0) { goto exit_create_index } _sqlite3ExprListSetSortOrder(tls, pList, sortOrder, -int32(1)) } else { _sqlite3ExprListCheckLength(tls, pParse, pList, __ccgo_ts+14630) 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)*32))).FpExpr if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLLATE) { 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 pIndex = _sqlite3AllocateIndexObject(tls, db, int16((*TExprList)(unsafe.Pointer(pList)).FnExpr+nExtraCol), nName+nExtra+int32(1), bp+104) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { goto exit_create_index } (*TIndex)(unsafe.Pointer(pIndex)).FzName = *(*uintptr)(unsafe.Pointer(bp + 104)) *(*uintptr)(unsafe.Pointer(bp + 104)) += uintptr(nName + int32(1)) libc.X__builtin___memcpy_chk(tls, (*TIndex)(unsafe.Pointer(pIndex)).FzName, zName, uint64(nName+int32(1)), libc.X__builtin_object_size(tls, (*TIndex)(unsafe.Pointer(pIndex)).FzName, 0)) (*TIndex)(unsafe.Pointer(pIndex)).FpTable = pTab (*TIndex)(unsafe.Pointer(pIndex)).FonError = uint8(onError) libc.SetBitFieldPtr16Uint32(pIndex+100, libc.BoolUint32(onError != libc.Int32FromInt32(OE_None)), 3, 0x8) libc.SetBitFieldPtr16Uint32(pIndex+100, uint32(idxType), 0, 0x3) (*TIndex)(unsafe.Pointer(pIndex)).FpSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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) } /* 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+14854, 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+100, libc.Uint32FromInt32(0), 3, 0x8) libc.SetBitFieldPtr16Uint32(pIndex+100, libc.Uint32FromInt32(1), 12, 0x1000) } else { j = int32((*TExpr)(unsafe.Pointer(pCExpr)).FiColumn) if j < 0 { j = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) } else { if int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*16 + 8))&0xf>>0)) == 0 { libc.SetBitFieldPtr16Uint32(pIndex+100, libc.Uint32FromInt32(0), 3, 0x8) } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*16))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { libc.SetBitFieldPtr16Uint32(pIndex+100, libc.Uint32FromInt32(1), 11, 0x800) libc.SetBitFieldPtr16Uint32(pIndex+100, 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) { zColl = *(*uintptr)(unsafe.Pointer((*TExprList_item)(unsafe.Pointer(pListItem)).FpExpr + 8)) nColl = _sqlite3Strlen30(tls, zColl) + int32(1) libc.X__builtin___memcpy_chk(tls, *(*uintptr)(unsafe.Pointer(bp + 104)), zColl, uint64(nColl), libc.X__builtin_object_size(tls, *(*uintptr)(unsafe.Pointer(bp + 104)), 0)) zColl = *(*uintptr)(unsafe.Pointer(bp + 104)) *(*uintptr)(unsafe.Pointer(bp + 104)) += uintptr(nColl) nExtra -= nColl } else { if j >= 0 { zColl = _sqlite3ColumnColl(tls, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(j)*16) } } 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)*8)) = 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 += 32 } /* 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))) 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)*8)) = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FazColl + uintptr(j)*8)) *(*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++ } } 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)*8)) = 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 */ _recomputeColumnsNotIndexed(tls, pIndex) if pTblName != uintptr(0) && int32((*TIndex)(unsafe.Pointer(pIndex)).FnColumn) >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { libc.SetBitFieldPtr16Uint32(pIndex+100, 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+100, 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 } 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 } 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)*8)) z2 = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(k)*8)) 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+14915, libc.VaList(bp+136, 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+100, 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. */ if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 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+14957, 0) (*TParse)(unsafe.Pointer(pParse)).Frc = _sqlite3CorruptError(tls, int32(125741)) goto exit_create_index } } p = _sqlite3HashInsert(tls, (*TIndex)(unsafe.Pointer(pIndex)).FpSchema+32, (*TIndex)(unsafe.Pointer(pIndex)).FzName, pIndex) if p != 0 { /* Malloc must have failed */ _sqlite3OomFault(tls, db) goto exit_create_index } *(*Tu32)(unsafe.Pointer(db + 44)) |= uint32(DBFLAG_SchemaChange) } else { if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) || pTblName != uintptr(0) { v12 = pParse + 56 *(*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 */ if pStart != 0 { n1 = int32(uint32(int32(int64((*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fz)-int64((*TToken)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 96)))).Fz))) + (*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fn) if int32(*(*int8)(unsafe.Pointer((*TToken)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 96)))).Fz + uintptr(n1-int32(1))))) == int32(';') { n1-- } /* A named index with an explicit CREATE INDEX statement */ if onError == OE_None { v13 = __ccgo_ts + 1664 } else { v13 = __ccgo_ts + 14974 } zStmt = _sqlite3MPrintf(tls, db, __ccgo_ts+14982, libc.VaList(bp+136, v13, n1, (*TToken)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 96)))).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+15002, libc.VaList(bp+136, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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+15061, libc.VaList(bp+136, (*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) { (*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 + 16 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 + 40 } break goto _14 _14: ; ppFrom = pThis + 40 } } _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.Uint64FromInt64(10)/libc.Uint64FromInt64(2)) < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) { v1 = int32(libc.Uint64FromInt64(10) / libc.Uint64FromInt64(2)) } else { v1 = int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol) } nCopy = v1 /* Indexes with default row estimates should not have stat1 data */ /* 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 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)) } *(*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.X__builtin___memcpy_chk(tls, a+1*2, uintptr(unsafe.Pointer(&_aVal)), uint64(nCopy)*uint64(2), libc.X__builtin_object_size(tls, a+1*2, 0)) i = nCopy + int32(1) for { if !(i <= int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) { break } *(*TLogEst)(unsafe.Pointer(a + uintptr(i)*2)) = int16(23) goto _3 _3: ; i++ } 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 } /* Never called with prior non-OOM errors */ 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+15088, 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 + 100))&0x3>>0)) != SQLITE_IDXTYPE_APPDEF { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15106, 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)*32))).FzDbSName if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { v1 = __ccgo_ts + 6945 } else { v1 = __ccgo_ts + 6478 } 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+15179, libc.VaList(bp+8, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FzDbSName, (*TIndex)(unsafe.Pointer(pIndex)).FzName)) _sqlite3ClearStatTables(tls, pParse, iDb, __ccgo_ts+12266, (*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.X__builtin___memset_chk(tls, z+uintptr(n*int64(szEntry)), 0, uint64(szEntry), libc.X__builtin_object_size(tls, z+uintptr(n*int64(szEntry)), 0)) *(*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(24)) if pList == uintptr(0) { return uintptr(0) } } else { pNew = _sqlite3DbRealloc(tls, db, pList, uint64(24)+uint64((*TIdList)(unsafe.Pointer(pList)).FnId)*uint64(16)) 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)*16))).FzName = _sqlite3NameFromToken(tls, db, pToken) if int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) >= int32(PARSE_MODE_RENAME) && (*(*TIdList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*16))).FzName != 0 { _sqlite3RenameTokenMap(tls, pParse, (*(*TIdList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*16))).FzName, pToken) } return pList } // C documentation // // /* // ** Delete an IdList. // */ func _sqlite3IdListDelete(tls *libc.TLS, db uintptr, pList uintptr) { var i int32 _ = i if pList == uintptr(0) { return } /* 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)*16))).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 i = 0 for { if !(i < (*TIdList)(unsafe.Pointer(pList)).FnId) { break } if _sqlite3StrICmp(tls, (*(*TIdList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*16))).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 */ /* 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+15239, 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(112)+uint64(nAlloc-libc.Int64FromInt32(1))*uint64(104)) if pNew == uintptr(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)*104)) = *(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104)) goto _1 _1: ; i-- } *(*int32)(unsafe.Pointer(pSrc)) += nExtra /* Zero the newly allocated slots */ libc.X__builtin___memset_chk(tls, pSrc+8+uintptr(iStart)*104, 0, uint64(104)*uint64(nExtra), libc.X__builtin_object_size(tls, pSrc+8+uintptr(iStart)*104, 0)) i = iStart for { if !(i < iStart+nExtra) { break } (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104))).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 /* Cannot have C without B */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb if pList == uintptr(0) { pList = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(112)) if pList == uintptr(0) { return uintptr(0) } (*TSrcList)(unsafe.Pointer(pList)).FnAlloc = uint32(1) (*TSrcList)(unsafe.Pointer(pList)).FnSrc = int32(1) libc.X__builtin___memset_chk(tls, pList+8, 0, uint64(104), libc.X__builtin_object_size(tls, pList+8, 0)) (*(*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))*104 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 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 + 52 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 += 104 } } } // 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 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 + 60 + 4))&0x2>>1)) != 0 { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 88))) } if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 4))&0x4>>2)) != 0 { _sqlite3ExprListDelete(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 88))) } _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 + 60 + 4))&0x400>>10)) != 0 { _sqlite3IdListDelete(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 72))) } else { if *(*uintptr)(unsafe.Pointer(pItem + 72)) != 0 { _sqlite3ExprDelete(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 72))) } } goto _1 _1: ; i++ pItem += 104 } _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 + 15275 } else { v1 = __ccgo_ts + 15278 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15284, libc.VaList(bp+8, v1)) goto append_from_error } p = _sqlite3SrcListAppend(tls, pParse, p, pTable, pDatabase) if p == uintptr(0) { goto append_from_error } pItem = p + 8 + uintptr((*TSrcList)(unsafe.Pointer(p)).FnSrc-int32(1))*104 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) } 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+60+4, libc.Uint32FromInt32(1), 13, 0x2000) } } if pOnUsing == uintptr(0) { *(*uintptr)(unsafe.Pointer(pItem + 72)) = uintptr(0) } else { if (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpUsing != 0 { libc.SetBitFieldPtr16Uint32(pItem+60+4, libc.Uint32FromInt32(1), 10, 0x400) *(*uintptr)(unsafe.Pointer(pItem + 72)) = (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpUsing } else { *(*uintptr)(unsafe.Pointer(pItem + 72)) = (*TOnOrUsing)(unsafe.Pointer(pOnUsing)).FpOn } } return p goto append_from_error append_from_error: ; _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 if p != 0 && (*TToken)(unsafe.Pointer(pIndexedBy)).Fn > uint32(0) { pItem = p + 8 + uintptr((*TSrcList)(unsafe.Pointer(p)).FnSrc-int32(1))*104 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+60+4, libc.Uint32FromInt32(1), 0, 0x1) } else { *(*uintptr)(unsafe.Pointer(pItem + 88)) = _sqlite3NameFromToken(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pIndexedBy) libc.SetBitFieldPtr16Uint32(pItem+60+4, libc.Uint32FromInt32(1), 1, 0x2) /* 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 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.X__builtin___memcpy_chk(tls, p1+8+1*104, p2+8, uint64((*TSrcList)(unsafe.Pointer(p2)).FnSrc)*uint64(104), libc.X__builtin_object_size(tls, p1+8+1*104, 0)) _sqlite3DbFree(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, p2) p11 = p1 + 8 + 60 *(*Tu8)(unsafe.Pointer(p11)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p11))) | libc.Int32FromInt32(JT_LTORJ)&int32((*(*TSrcItem)(unsafe.Pointer(p1 + 8 + 1*104))).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))*104 *(*uintptr)(unsafe.Pointer(pItem + 88)) = pList libc.SetBitFieldPtr16Uint32(pItem+60+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))*104))).Ffg.Fjointype (*(*TSrcItem)(unsafe.Pointer(p + 8 + uintptr(i)*104))).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)*104))).Ffg.Fjointype)&int32(JT_RIGHT) == 0) { break } goto _4 _4: ; i-- } i-- for { p7 = p + 8 + uintptr(i)*104 + 60 *(*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 db = (*TParse)(unsafe.Pointer(pParse)).Fdb if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_TRANSACTION), __ccgo_ts+15320, 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)*32))).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 isRollback = libc.BoolInt32(eType == int32(TK_ROLLBACK)) if isRollback != 0 { v1 = __ccgo_ts + 15326 } else { v1 = __ccgo_ts + 15335 } 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) 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 + 15320, 1: __ccgo_ts + 15342, 2: __ccgo_ts + 15326, } // 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*32))).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+15350, 0) (*TParse)(unsafe.Pointer(pParse)).Frc = rc return int32(1) } (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).FpBt = *(*uintptr)(unsafe.Pointer(bp)) 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) { 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+12854, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TTable)(unsafe.Pointer(pTab)).FiPKey)*16))).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 */ /* Needed for schema access */ iDb = 0 pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb for { if !(iDb < (*Tsqlite3)(unsafe.Pointer(db)).FnDb) { break } 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 += 32 } } // 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) { 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)*32))).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+15443, 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 + 100))&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 { i = 0 for { if !(i < nCol) { break } zColl = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i)*8)) if zColl == uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) { v2 = uintptr(0) } else { v2 = _sqlite3LocateCollSeq(tls, pParse, zColl) } *(*uintptr)(unsafe.Pointer(pKey + 32 + uintptr(i)*8)) = v2 *(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pKey)).FaSortFlags + uintptr(i))) = *(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder + uintptr(i))) goto _1 _1: ; i++ } if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 100))&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+100, 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.X__builtin___memcpy_chk(tls, pColl, pColl2, uint64(40), libc.X__builtin_object_size(tls, pColl, 0)) (*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) } } 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+648, zName) if uintptr(0) == pColl && create != 0 { nName = _sqlite3Strlen30(tls, zName) + int32(1) pColl = _sqlite3DbMallocZero(tls, db, libc.Uint64FromInt32(3)*libc.Uint64FromInt64(40)+uint64(nName)) if pColl != 0 { pDel = uintptr(0) (*(*TCollSeq)(unsafe.Pointer(pColl))).FzName = pColl + 3*40 (*(*TCollSeq)(unsafe.Pointer(pColl))).Fenc = uint8(SQLITE_UTF8) (*(*TCollSeq)(unsafe.Pointer(pColl + 1*40))).FzName = pColl + 3*40 (*(*TCollSeq)(unsafe.Pointer(pColl + 1*40))).Fenc = uint8(SQLITE_UTF16LE) (*(*TCollSeq)(unsafe.Pointer(pColl + 2*40))).FzName = pColl + 3*40 (*(*TCollSeq)(unsafe.Pointer(pColl + 2*40))).Fenc = uint8(SQLITE_UTF16BE) libc.X__builtin___memcpy_chk(tls, (*(*TCollSeq)(unsafe.Pointer(pColl))).FzName, zName, uint64(nName), libc.X__builtin_object_size(tls, (*(*TCollSeq)(unsafe.Pointer(pColl))).FzName, 0)) pDel = _sqlite3HashInsert(tls, db+648, (*(*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). */ 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 if zName != 0 { pColl = _findCollSeqEntry(tls, db, zName, create) if pColl != 0 { pColl += uintptr(int32(enc)-int32(1)) * 40 } } 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) { (*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) } if p == uintptr(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15519, 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 /* 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)*8)) for { if !(p != 0) { break } if _sqlite3StrICmp(tls, (*TFuncDef)(unsafe.Pointer(p)).FzName, zFunc) == 0 { return p } goto _1 _1: ; p = *(*uintptr)(unsafe.Pointer(p + 64)) } 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)*72))).FzName nName = _sqlite3Strlen30(tls, zName) h = (int32(*(*int8)(unsafe.Pointer(zName))) + nName) % int32(SQLITE_FUNC_HASH_SZ) pOther = _sqlite3FunctionSearch(tls, h, zName) if pOther != 0 { (*(*TFuncDef)(unsafe.Pointer(aDef + uintptr(i)*72))).FpNext = (*TFuncDef)(unsafe.Pointer(pOther)).FpNext (*TFuncDef)(unsafe.Pointer(pOther)).FpNext = aDef + uintptr(i)*72 } else { (*(*TFuncDef)(unsafe.Pointer(aDef + uintptr(i)*72))).FpNext = uintptr(0) *(*uintptr)(unsafe.Pointer(aDef + uintptr(i)*72 + 64)) = *(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)) + uintptr(h)*8)) *(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)) + uintptr(h)*8)) = aDef + uintptr(i)*72 } 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 */ nName = _sqlite3Strlen30(tls, zName) /* First search for a match amongst the application-defined functions. */ p = _sqlite3HashFind(tls, db+624, 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(*(*int8)(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(72)+uint64(nName)+uint64(1)) pBest = v1 } if v2 && v1 != uintptr(0) { (*TFuncDef)(unsafe.Pointer(pBest)).FzName = pBest + 1*72 (*TFuncDef)(unsafe.Pointer(pBest)).FnArg = int8(uint16(nArg)) (*TFuncDef)(unsafe.Pointer(pBest)).FfuncFlags = uint32(enc) libc.X__builtin___memcpy_chk(tls, pBest+1*72, zName, uint64(nName+int32(1)), libc.X__builtin_object_size(tls, pBest+1*72, 0)) 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+624, (*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(912) defer tls.Free(912) var pElem, pSchema, pTab, p3 uintptr var _ /* temp1 at bp+0 */ THash var _ /* temp2 at bp+24 */ THash var _ /* xdb at bp+48 */ Tsqlite3 _, _, _, _ = pElem, pSchema, pTab, p3 pSchema = p libc.X__builtin___memset_chk(tls, bp+48, 0, uint64(856), libc.X__builtin_object_size(tls, bp+48, 0)) *(*THash)(unsafe.Pointer(bp)) = (*TSchema)(unsafe.Pointer(pSchema)).FtblHash *(*THash)(unsafe.Pointer(bp + 24)) = (*TSchema)(unsafe.Pointer(pSchema)).FtrigHash _sqlite3HashInit(tls, pSchema+56) _sqlite3HashClear(tls, pSchema+32) pElem = (*THash)(unsafe.Pointer(bp + 24)).Ffirst for { if !(pElem != 0) { break } _sqlite3DeleteTrigger(tls, bp+48, (*THashElem)(unsafe.Pointer(pElem)).Fdata) goto _1 _1: ; pElem = (*THashElem)(unsafe.Pointer(pElem)).Fnext } _sqlite3HashClear(tls, bp+24) _sqlite3HashInit(tls, pSchema+8) pElem = (*THash)(unsafe.Pointer(bp)).Ffirst for { if !(pElem != 0) { break } pTab = (*THashElem)(unsafe.Pointer(pElem)).Fdata _sqlite3DeleteTable(tls, bp+48, pTab) goto _2 _2: ; pElem = (*THashElem)(unsafe.Pointer(pElem)).Fnext } _sqlite3HashClear(tls, bp) _sqlite3HashClear(tls, pSchema+80) (*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 + 114 *(*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(120), __ccgo_fp(_sqlite3SchemaClear)) } else { p = _sqlite3DbMallocZero(tls, uintptr(0), uint64(120)) } if !(p != 0) { _sqlite3OomFault(tls, db) } else { if 0 == int32((*TSchema)(unsafe.Pointer(p)).Ffile_format) { _sqlite3HashInit(tls, p+8) _sqlite3HashInit(tls, p+32) _sqlite3HashInit(tls, p+56) _sqlite3HashInit(tls, p+80) (*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 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+60+4, libc.Uint32FromInt32(1), 9, 0x200) if pTab != 0 { (*TTable)(unsafe.Pointer(pTab)).FnTabRef++ if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 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 + 64))).Fp)).FeVtabRisk) > libc.BoolInt32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Fflags&uint64(SQLITE_TrustedSchema) != uint64(0)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15550, 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) } 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+15583, 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+15612, 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(48) defer tls.Free(48) 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 { (*(*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)*32))).FzDbSName) } 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(96) defer tls.Free(96) 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+80 */ [2]int32 var _ /* iDataCur at bp+0 */ int32 var _ /* iIdxCur at bp+4 */ int32 var _ /* sContext at bp+8 */ TAuthContext var _ /* sNC at bp+24 */ 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.X__builtin___memset_chk(tls, bp+8, 0, uint64(16), libc.X__builtin_object_size(tls, bp+8, 0)) db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto delete_from_cleanup } /* 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) rcauth = _sqlite3AuthCheck(tls, pParse, int32(SQLITE_DELETE), (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0), (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FzDbSName) if rcauth == int32(SQLITE_DENY) { goto delete_from_cleanup } /* Assign cursor numbers to the table and all its indices. */ v3 = pParse + 52 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.X__builtin___memset_chk(tls, bp+24, 0, uint64(56), libc.X__builtin_object_size(tls, bp+24, 0)) (*(*TNameContext)(unsafe.Pointer(bp + 24))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp + 24))).FpSrcList = pTabList if _sqlite3ResolveExprNames(tls, bp+24, 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 + 24))).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) v13 = pParse + 56 *(*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) nPk = int16((*TIndex)(unsafe.Pointer(pPk)).FnKeyCol) iPk = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 56)) += int32(nPk) v15 = pParse + 52 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+80) 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 } _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 + 56 *(*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.X__builtin___memset_chk(tls, aToOpen, int32(1), uint64(nIdx+int32(1)), libc.X__builtin_object_size(tls, aToOpen, 0)) *(*Tu8)(unsafe.Pointer(aToOpen + uintptr(nIdx+int32(1)))) = uint8(0) if (*(*[2]int32)(unsafe.Pointer(bp + 80)))[0] >= 0 { *(*Tu8)(unsafe.Pointer(aToOpen + uintptr((*(*[2]int32)(unsafe.Pointer(bp + 80)))[0]-iTabCur))) = uint8(0) } if (*(*[2]int32)(unsafe.Pointer(bp + 80)))[int32(1)] >= 0 { *(*Tu8)(unsafe.Pointer(aToOpen + uintptr((*(*[2]int32)(unsafe.Pointer(bp + 80)))[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 + 56 *(*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) 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 { /* 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 { _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) } /* OP_Found will use a composite key */ } else { addrLoop = _sqlite3VdbeAddOp3(tls, v, int32(OP_RowSetRead), iRowSet, 0, iKey) } } /* Delete the row */ if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { pVTab = _sqlite3GetVTable(tls, db, pTab) _sqlite3VtabMakeWritable(tls, pParse, pTab) _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 + 80)))[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+15650) } 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. */ /* 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 + 56)) += 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 } 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 + 100))&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 + 100))&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 pOp = (*TVdbe)(unsafe.Pointer((*Tsqlite3_context)(unsafe.Pointer(context)).FpVdbe)).FaOp + uintptr((*Tsqlite3_context)(unsafe.Pointer(context)).FiOp-int32(1))*24 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) { (*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 if Xsqlite3_user_data(tls, context) == uintptr(0) { v1 = 0 } else { v1 = -int32(1) } mask = v1 pColl = _sqlite3GetFuncCollSeq(tls, context) 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)*8))) == int32(SQLITE_NULL) { return } if _sqlite3MemCompare(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(iBest)*8)), *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*8)), pColl)^mask >= 0 { iBest = i } goto _2 _2: ; i++ } Xsqlite3_result_value(tls, context, *(*uintptr)(unsafe.Pointer(argv + uintptr(iBest)*8))) } // 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 /* 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 + 6718, 1: __ccgo_ts + 6713, 2: __ccgo_ts + 8670, 3: __ccgo_ts + 8665, 4: __ccgo_ts + 1650, } // 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 _ = 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(int64(z)-int64(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 _ = 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 _ = 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*8))) 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*8))) 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*8))) 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, uint64(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+16 */ 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)*8 _sqlite3StrAccumInit(tls, bp+16, db, uintptr(0), 0, *(*int32)(unsafe.Pointer(db + 136))) (*(*TStrAccum)(unsafe.Pointer(bp + 16))).FprintfFlags = uint8(SQLITE_PRINTF_SQLFUNC) Xsqlite3_str_appendf(tls, bp+16, zFormat, libc.VaList(bp+56, bp)) n = int32((*(*TStrAccum)(unsafe.Pointer(bp + 16))).FnChar) Xsqlite3_result_text(tls, context, _sqlite3StrAccumFinish(tls, bp+16), 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 if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) == int32(SQLITE_NULL) || argc == int32(3) && Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + 2*8))) == int32(SQLITE_NULL) { return } p0type = Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) p1 = int64(Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8)))) 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 } } 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*8)))) if p2 < 0 { p2 = -p2 negP2 = int32(1) } } else { p2 = int64(*(*int32)(unsafe.Pointer(Xsqlite3_context_db_handle(tls, context) + 136))) } 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 } } 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(int64(z2)-int64(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 if argc == int32(2) { if int32(SQLITE_NULL) == Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) { return } n = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) 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+15680, 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) if nByte > int64(*(*int32)(unsafe.Pointer(db + 136))) { 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 */ if z2 != 0 { z1 = _contextMalloc(tls, context, int64(n)+int64(1)) if z1 != 0 { i = 0 for { if !(i < n) { break } *(*int8)(unsafe.Pointer(z1 + uintptr(i))) = int8(int32(*(*int8)(unsafe.Pointer(z2 + uintptr(i)))) & ^(int32(_sqlite3CtypeMap[uint8(*(*int8)(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 */ if z2 != 0 { z1 = _contextMalloc(tls, context, int64(n)+int64(1)) if z1 != 0 { i = 0 for { if !(i < n) { break } *(*int8)(unsafe.Pointer(z1 + uintptr(i))) = int8(_sqlite3UpperToLower[uint8(*(*int8)(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 + 8)))))&int32(0xc0) == int32(0x80) { *(*uintptr)(unsafe.Pointer(bp + 8))++ } } } 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]int8)(unsafe.Pointer(bp + 18)))[0] = int8(c & uint32(^(int32(_sqlite3CtypeMap[uint8(c)]) & libc.Int32FromInt32(0x20)))) (*(*[3]int8)(unsafe.Pointer(bp + 18)))[int32(1)] = int8(_sqlite3UpperToLower[uint8(c)]) (*(*[3]int8)(unsafe.Pointer(bp + 18)))[int32(2)] = 0 } else { (*(*[3]int8)(unsafe.Pointer(bp + 18)))[0] = int8(c) (*(*[3]int8)(unsafe.Pointer(bp + 18)))[int32(1)] = 0 } for int32(1) != 0 { *(*uintptr)(unsafe.Pointer(bp + 8)) += uintptr(libc.Xstrcspn(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), bp+18)) if int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8))))) == 0 { break } *(*uintptr)(unsafe.Pointer(bp + 8))++ bMatch1 = _patternCompare(tls, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 8)), pInfo, matchOther) if bMatch1 != int32(SQLITE_NOMATCH) { return bMatch1 } } } else { for { if int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8))))) < int32(0x80) { v10 = *(*uintptr)(unsafe.Pointer(bp + 8)) *(*uintptr)(unsafe.Pointer(bp + 8))++ v9 = uint32(*(*Tu8)(unsafe.Pointer(v10))) } else { v9 = _sqlite3Utf8Read(tls, bp+8) } v8 = v9 c2 = v8 if !(v8 != uint32(0)) { break } if c2 != c { continue } bMatch2 = _patternCompare(tls, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 8)), 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+8) 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 + 8))))) < int32(0x80) { v12 = *(*uintptr)(unsafe.Pointer(bp + 8)) *(*uintptr)(unsafe.Pointer(bp + 8))++ v11 = uint32(*(*Tu8)(unsafe.Pointer(v12))) } else { v11 = _sqlite3Utf8Read(tls, bp+8) } 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 + 8))))) == 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+8 */ 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*8))) == 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 + 136 + 8*4)) { Xsqlite3_result_error(tls, context, __ccgo_ts+15686, -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 + 8)) = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 2*8))) if *(*uintptr)(unsafe.Pointer(bp + 8)) == uintptr(0) { return } if _sqlite3Utf8CharLen(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), -int32(1)) != int32(1) { Xsqlite3_result_error(tls, context, __ccgo_ts+15719, -int32(1)) return } escape = _sqlite3Utf8Read(tls, bp+8) if escape == uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchAll) || escape == uint32((*TcompareInfo)(unsafe.Pointer(pInfo)).FmatchOne) { libc.X__builtin___memcpy_chk(tls, bp, pInfo, uint64(4), libc.X__builtin_object_size(tls, bp, 0)) 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*8))) 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*8)), 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+3863, libc.VaList(bp+8, Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))))) } // 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 _ = 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 _ = 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]int8{ 0: int8('0'), 1: int8('1'), 2: int8('2'), 3: int8('3'), 4: int8('4'), 5: int8('5'), 6: int8('6'), 7: int8('7'), 8: int8('8'), 9: int8('9'), 10: int8('A'), 11: int8('B'), 12: int8('C'), 13: int8('D'), 14: int8('E'), 15: int8('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. */ switch Xsqlite3_value_type(tls, pValue) { case int32(SQLITE_FLOAT): r1 = Xsqlite3_value_double(tls, pValue) Xsqlite3_str_appendf(tls, pStr, __ccgo_ts+15764, 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+15772, libc.VaList(bp+16, r1)) } } case int32(SQLITE_INTEGER): Xsqlite3_str_appendf(tls, pStr, __ccgo_ts+1425, 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)) /* 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 } *(*int8)(unsafe.Pointer(zText + uintptr(i*int32(2)+int32(2)))) = _hexdigits[int32(*(*int8)(unsafe.Pointer(zBlob + uintptr(i))))>>int32(4)&int32(0x0F)] *(*int8)(unsafe.Pointer(zText + uintptr(i*int32(2)+int32(3)))) = _hexdigits[int32(*(*int8)(unsafe.Pointer(zBlob + uintptr(i))))&int32(0x0F)] goto _1 _1: ; i++ } *(*int8)(unsafe.Pointer(zText + uintptr(nBlob*int64(2)+int64(2)))) = int8('\'') *(*int8)(unsafe.Pointer(zText + uintptr(nBlob*int64(2)+int64(3)))) = int8('\000') *(*int8)(unsafe.Pointer(zText)) = int8('X') *(*int8)(unsafe.Pointer(zText + 1)) = int8('\'') (*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+15780, libc.VaList(bp+16, zArg)) default: Xsqlite3_str_append(tls, pStr, __ccgo_ts+1665, 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) _ = argc _sqlite3StrAccumInit(tls, bp, db, uintptr(0), 0, *(*int32)(unsafe.Pointer(db + 136))) _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)*8))) 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(int64(zOut)-int64(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 _ = argc pBlob = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv))) n = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv))) /* 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++ *(*int8)(unsafe.Pointer(v3)) = _hexdigits[int32(c)>>int32(4)&int32(0xf)] v4 = z z++ *(*int8)(unsafe.Pointer(v4)) = _hexdigits[int32(c)&int32(0xf)] goto _2 _2: ; i++ pBlob++ } *(*int8)(unsafe.Pointer(z)) = 0 Xsqlite3_result_text64(tls, context, zHex, uint64(int64(z)-int64(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 + 1664 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) if argc == int32(2) { zPass = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) nPass = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) } 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 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))++ 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 + 136))) { 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.X__builtin___memcpy_chk(tls, zOut+uintptr(j), zRep, uint64(nRep), libc.X__builtin_object_size(tls, zOut+uintptr(j), 0)) j += nRep i += nPattern - int32(1) } goto _1 _1: ; i++ } libc.X__builtin___memcpy_chk(tls, zOut+uintptr(j), zStr+uintptr(i), uint64(nStr-i), libc.X__builtin_object_size(tls, zOut+uintptr(j), 0)) j += nStr - i *(*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)))) 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*8))) 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(uint64(int64(nChar))*(libc.Uint64FromInt64(8)+libc.Uint64FromInt64(4)))) if azChar == uintptr(0) { return } aLen = azChar + uintptr(nChar)*8 z = zCharSet nChar = libc.Int32FromInt32(0) for { if !(*(*uint8)(unsafe.Pointer(z)) != 0) { break } *(*uintptr)(unsafe.Pointer(azChar + uintptr(nChar)*8)) = 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(int64(z) - int64(*(*uintptr)(unsafe.Pointer(azChar + uintptr(nChar)*8)))) goto _4 _4: ; nChar++ } } } } if nChar > 0 { flags = int32(int64(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)*8)), uint64(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)*8)), uint64(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 + 11675, } // 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)*8)))) 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)*8)))) if k > 0 { v = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*8))) if v != uintptr(0) { if j > 0 && nSep > 0 { libc.X__builtin___memcpy_chk(tls, z+uintptr(j), zSep, uint64(nSep), libc.X__builtin_object_size(tls, z+uintptr(j), 0)) j += int64(nSep) } libc.X__builtin___memcpy_chk(tls, z+uintptr(j), v, uint64(k), libc.X__builtin_object_size(tls, z+uintptr(j), 0)) j += k } } goto _2 _2: ; i++ } *(*int8)(unsafe.Pointer(z + uintptr(j))) = 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+1664) } // 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)*8, 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]int8 _, _, _, _, _, _, _ = code, i, j, prevcode, zIn, v3, v4 zIn = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) if zIn == uintptr(0) { zIn = __ccgo_ts + 1664 } 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]int8)(unsafe.Pointer(bp)))[0] = int8(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]int8)(unsafe.Pointer(bp)))[v3] = int8(code + int32('0')) } } else { prevcode = uint8(0) } goto _2 _2: ; i++ } for j < int32(4) { v4 = j j++ (*(*[8]int8)(unsafe.Pointer(bp)))[v4] = int8('0') } (*(*[8]int8)(unsafe.Pointer(bp)))[j] = 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+15783, 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+12893, -int32(1)) return } if argc == int32(2) { zProc = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) } 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 { FrSum float64 FrErr float64 FiSum Ti64 Fcnt Ti64 Fapprox Tu8 Fovrfl Tu8 } type SumCtx = TSumCtx type TSumCtx1 = struct { FrSum float64 FrErr float64 FiSum Ti64 Fcnt Ti64 Fapprox Tu8 Fovrfl Tu8 } 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 _ = 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 _ = 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) { (*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+15663, -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 { Fn Ti64 } type CountCtx = TCountCtx type TCountCtx1 = struct { 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. */ } 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(56)) 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 if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv))) == int32(SQLITE_NULL) { return } pGCC = Xsqlite3_aggregate_context(tls, context, int32(48)) 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 + 136))) if argc == int32(1) { if !(firstTerm != 0) { Xsqlite3_str_appendchar(tls, pGCC, int32(1), int8(',')) } else { (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnFirstSepLength = int32(1) } } else { if !(firstTerm != 0) { zSep = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) nSep = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) 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((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum+libc.Int32FromInt32(1))*uint64(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((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum)*uint64(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*8))) } } *(*int32)(unsafe.Pointer(pGCC + 32)) += 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 _ = 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(48)) /* 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 + 32)) -= int32(1) if (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths != uintptr(0) { if (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum > 0 { nVS += *(*int32)(unsafe.Pointer((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths)) libc.X__builtin___memmove_chk(tls, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths+uintptr(1)*4, uint64((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FnAccum-libc.Int32FromInt32(1))*uint64(4), libc.X__builtin_object_size(tls, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).FpnSepLengths, 0)) } } 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.X__builtin___memmove_chk(tls, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FzText, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FzText+uintptr(nVS), uint64((*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FnChar), libc.X__builtin_object_size(tls, (*TGroupConcatCtx)(unsafe.Pointer(pGCC)).Fstr.FzText, 0)) } 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+15788, int32(2)) 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+15794, nArg, int32(SQLITE_UTF8), pInfo, __ccgo_fp(_likeFunc), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0)) pDef = _sqlite3FindFunction(tls, db, __ccgo_ts+15794, 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 if !(*(*uintptr)(unsafe.Pointer(pExpr + 32)) != 0) { return 0 } nExpr = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).FnExpr 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.X__builtin___memcpy_chk(tls, aWc, (*TFuncDef)(unsafe.Pointer(pDef)).FpUserData, uint64(3), libc.X__builtin_object_size(tls, aWc, 0)) if nExpr < int32(3) { *(*int8)(unsafe.Pointer(aWc + 3)) = 0 } else { pEscape = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)) + 8 + 2*32))).FpExpr if int32((*TExpr)(unsafe.Pointer(pEscape)).Fop) != int32(TK_STRING) { return 0 } zEscape = *(*uintptr)(unsafe.Pointer(pEscape + 8)) if int32(*(*int8)(unsafe.Pointer(zEscape))) == 0 || int32(*(*int8)(unsafe.Pointer(zEscape + 1))) != 0 { return 0 } if int32(*(*int8)(unsafe.Pointer(zEscape))) == int32(*(*int8)(unsafe.Pointer(aWc))) { return 0 } if int32(*(*int8)(unsafe.Pointer(zEscape))) == int32(*(*int8)(unsafe.Pointer(aWc + 1))) { return 0 } *(*int8)(unsafe.Pointer(aWc + 3)) = *(*int8)(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 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 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*8))) if x <= float64(0) { return } default: return } ans = libc.Xlog(tls, x) / b } else { switch int32(int64(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 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 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*8))) 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*8))) 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) { _ = 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 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.Uint64FromInt64(7920)/libc.Uint64FromInt64(72))) } /* ** 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(int64(libc.Int32FromInt32(INLINEFUNC_implies_nonnull_row))), FzName: __ccgo_ts + 15799, }, 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(int64(libc.Int32FromInt32(INLINEFUNC_expr_compare))), FzName: __ccgo_ts + 15819, }, 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(int64(libc.Int32FromInt32(INLINEFUNC_expr_implies_expr))), FzName: __ccgo_ts + 15832, }, 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(int64(libc.Int32FromInt32(INLINEFUNC_affinity))), FzName: __ccgo_ts + 15850, }, 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 + 15859, }, 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 + 15867, }, 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 + 15867, }, 7: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15882, }, 8: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 15908, }, 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(int64(libc.Int32FromInt32(INLINEFUNC_unlikely))), FzName: __ccgo_ts + 15933, }, 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(int64(libc.Int32FromInt32(INLINEFUNC_unlikely))), FzName: __ccgo_ts + 15942, }, 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(int64(libc.Int32FromInt32(INLINEFUNC_unlikely))), FzName: __ccgo_ts + 15953, }, 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(int64(libc.Int32FromInt32(INLINEFUNC_sqlite_offset))), FzName: __ccgo_ts + 15960, }, 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(int64(libc.Int32FromInt32(1))), FzName: __ccgo_ts + 15974, }, 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(int64(libc.Int32FromInt32(1))), FzName: __ccgo_ts + 15974, }, 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(int64(libc.Int32FromInt32(2))), FzName: __ccgo_ts + 15980, }, 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(int64(libc.Int32FromInt32(2))), FzName: __ccgo_ts + 15980, }, 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(int64(libc.Int32FromInt32(3))), FzName: __ccgo_ts + 15986, }, 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(int64(libc.Int32FromInt32(3))), FzName: __ccgo_ts + 15986, }, 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 + 15991, }, 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 + 15991, }, 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 + 15991, }, 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(int64(libc.Int32FromInt32(1))), FzName: __ccgo_ts + 15995, }, 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(int64(libc.Int32FromInt32(1))), FzName: __ccgo_ts + 15995, }, 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(int64(libc.Int32FromInt32(1))), FzName: __ccgo_ts + 15995, }, 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 + 15999, }, 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 + 16006, }, 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 + 16014, }, 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 + 16021, }, 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 + 16034, }, 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 + 16040, }, 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 + 16047, }, 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 + 16054, }, 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 + 16062, }, 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 + 16067, }, 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 + 16071, }, 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 + 16071, }, 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 + 16077, }, 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 + 16083, }, 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 + 16089, }, 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 + 16093, }, 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 + 16093, }, 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 + 16099, }, 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 + 16099, }, 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 + 16106, }, 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 + 16106, }, 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 + 16106, }, 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 + 16116, }, 48: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 16123, }, 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 + 16130, }, 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 + 16141, }, 51: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16148, }, 52: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_SLOCHNG) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16163, }, 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 + 16180, }, 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 + 16191, }, 55: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 16197, }, 56: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 16215, }, 57: { FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_UTF8) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_FUNC_NEEDCOLL)), FzName: __ccgo_ts + 16223, }, 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 + 16237, }, 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 + 16245, }, 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 + 16254, }, 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 + 16254, }, 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 + 16261, }, 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 + 16261, }, 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 + 16271, }, 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 + 16275, }, 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 + 16281, }, 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 + 16285, }, 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 + 16285, }, 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 + 16291, }, 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 + 16291, }, 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 + 16304, }, 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 + 16315, }, 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 + 15794, }, 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 + 15794, }, 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 + 7142, }, 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 + 7142, }, 77: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16320, }, 78: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 1237, }, 79: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 1245, }, 80: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16325, }, 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 + 16331, }, 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(int64(libc.Int32FromInt32(1))), FzName: __ccgo_ts + 16334, }, 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(int64(libc.Int32FromInt32(1))), FzName: __ccgo_ts + 16338, }, 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(int64(libc.Int32FromInt32(2))), FzName: __ccgo_ts + 16344, }, 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 + 16334, }, 86: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16349, }, 87: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16353, }, 88: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16357, }, 89: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16363, }, 90: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16367, }, 91: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16372, }, 92: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16377, }, 93: { FnArg: int8(2), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16382, }, 94: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16388, }, 95: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16392, }, 96: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16396, }, 97: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16400, }, 98: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16405, }, 99: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16410, }, 100: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16415, }, 101: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16421, }, 102: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16427, }, 103: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16433, }, 104: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16438, }, 105: { FnArg: int8(1), FfuncFlags: uint32(libc.Int32FromInt32(SQLITE_FUNC_BUILTIN) | libc.Int32FromInt32(SQLITE_FUNC_CONSTANT) | libc.Int32FromInt32(SQLITE_UTF8)), FzName: __ccgo_ts + 16446, }, 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 + 16454, }, 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 + 16457, }, 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 + 7142, }, 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(int64(libc.Int32FromInt32(INLINEFUNC_iif))), FzName: __ccgo_ts + 16462, }, } func init() { p := unsafe.Pointer(&_aBuiltinFunc) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 168)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 240)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 312)) = __ccgo_fp(_soundexFunc) *(*uintptr)(unsafe.Add(p, 384)) = __ccgo_fp(_loadExt) *(*uintptr)(unsafe.Add(p, 456)) = __ccgo_fp(_loadExt) *(*uintptr)(unsafe.Add(p, 528)) = __ccgo_fp(_compileoptionusedFunc) *(*uintptr)(unsafe.Add(p, 600)) = __ccgo_fp(_compileoptiongetFunc) *(*uintptr)(unsafe.Add(p, 672)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 744)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 816)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 888)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 960)) = __ccgo_fp(_trimFunc) *(*uintptr)(unsafe.Add(p, 1032)) = __ccgo_fp(_trimFunc) *(*uintptr)(unsafe.Add(p, 1104)) = __ccgo_fp(_trimFunc) *(*uintptr)(unsafe.Add(p, 1176)) = __ccgo_fp(_trimFunc) *(*uintptr)(unsafe.Add(p, 1248)) = __ccgo_fp(_trimFunc) *(*uintptr)(unsafe.Add(p, 1320)) = __ccgo_fp(_trimFunc) *(*uintptr)(unsafe.Add(p, 1392)) = __ccgo_fp(_minmaxFunc) *(*uintptr)(unsafe.Add(p, 1536)) = __ccgo_fp(_minmaxStep) *(*uintptr)(unsafe.Add(p, 1544)) = __ccgo_fp(_minMaxFinalize) *(*uintptr)(unsafe.Add(p, 1552)) = __ccgo_fp(_minMaxValue) *(*uintptr)(unsafe.Add(p, 1608)) = __ccgo_fp(_minmaxFunc) *(*uintptr)(unsafe.Add(p, 1752)) = __ccgo_fp(_minmaxStep) *(*uintptr)(unsafe.Add(p, 1760)) = __ccgo_fp(_minMaxFinalize) *(*uintptr)(unsafe.Add(p, 1768)) = __ccgo_fp(_minMaxValue) *(*uintptr)(unsafe.Add(p, 1824)) = __ccgo_fp(_typeofFunc) *(*uintptr)(unsafe.Add(p, 1896)) = __ccgo_fp(_subtypeFunc) *(*uintptr)(unsafe.Add(p, 1968)) = __ccgo_fp(_lengthFunc) *(*uintptr)(unsafe.Add(p, 2040)) = __ccgo_fp(_bytelengthFunc) *(*uintptr)(unsafe.Add(p, 2112)) = __ccgo_fp(_instrFunc) *(*uintptr)(unsafe.Add(p, 2184)) = __ccgo_fp(_printfFunc) *(*uintptr)(unsafe.Add(p, 2256)) = __ccgo_fp(_printfFunc) *(*uintptr)(unsafe.Add(p, 2328)) = __ccgo_fp(_unicodeFunc) *(*uintptr)(unsafe.Add(p, 2400)) = __ccgo_fp(_charFunc) *(*uintptr)(unsafe.Add(p, 2472)) = __ccgo_fp(_absFunc) *(*uintptr)(unsafe.Add(p, 2544)) = __ccgo_fp(_roundFunc) *(*uintptr)(unsafe.Add(p, 2616)) = __ccgo_fp(_roundFunc) *(*uintptr)(unsafe.Add(p, 2688)) = __ccgo_fp(_upperFunc) *(*uintptr)(unsafe.Add(p, 2760)) = __ccgo_fp(_lowerFunc) *(*uintptr)(unsafe.Add(p, 2832)) = __ccgo_fp(_hexFunc) *(*uintptr)(unsafe.Add(p, 2904)) = __ccgo_fp(_unhexFunc) *(*uintptr)(unsafe.Add(p, 2976)) = __ccgo_fp(_unhexFunc) *(*uintptr)(unsafe.Add(p, 3048)) = __ccgo_fp(_concatFunc) *(*uintptr)(unsafe.Add(p, 3192)) = __ccgo_fp(_concatwsFunc) *(*uintptr)(unsafe.Add(p, 3408)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 3480)) = __ccgo_fp(_randomFunc) *(*uintptr)(unsafe.Add(p, 3552)) = __ccgo_fp(_randomBlob) *(*uintptr)(unsafe.Add(p, 3624)) = __ccgo_fp(_nullifFunc) *(*uintptr)(unsafe.Add(p, 3696)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 3768)) = __ccgo_fp(_sourceidFunc) *(*uintptr)(unsafe.Add(p, 3840)) = __ccgo_fp(_errlogFunc) *(*uintptr)(unsafe.Add(p, 3912)) = __ccgo_fp(_quoteFunc) *(*uintptr)(unsafe.Add(p, 3984)) = __ccgo_fp(_last_insert_rowid) *(*uintptr)(unsafe.Add(p, 4056)) = __ccgo_fp(_changes) *(*uintptr)(unsafe.Add(p, 4128)) = __ccgo_fp(_total_changes) *(*uintptr)(unsafe.Add(p, 4200)) = __ccgo_fp(_replaceFunc) *(*uintptr)(unsafe.Add(p, 4272)) = __ccgo_fp(_zeroblobFunc) *(*uintptr)(unsafe.Add(p, 4344)) = __ccgo_fp(_substrFunc) *(*uintptr)(unsafe.Add(p, 4416)) = __ccgo_fp(_substrFunc) *(*uintptr)(unsafe.Add(p, 4488)) = __ccgo_fp(_substrFunc) *(*uintptr)(unsafe.Add(p, 4560)) = __ccgo_fp(_substrFunc) *(*uintptr)(unsafe.Add(p, 4632)) = __ccgo_fp(_sumStep) *(*uintptr)(unsafe.Add(p, 4640)) = __ccgo_fp(_sumFinalize) *(*uintptr)(unsafe.Add(p, 4648)) = __ccgo_fp(_sumFinalize) *(*uintptr)(unsafe.Add(p, 4656)) = __ccgo_fp(_sumInverse) *(*uintptr)(unsafe.Add(p, 4704)) = __ccgo_fp(_sumStep) *(*uintptr)(unsafe.Add(p, 4712)) = __ccgo_fp(_totalFinalize) *(*uintptr)(unsafe.Add(p, 4720)) = __ccgo_fp(_totalFinalize) *(*uintptr)(unsafe.Add(p, 4728)) = __ccgo_fp(_sumInverse) *(*uintptr)(unsafe.Add(p, 4776)) = __ccgo_fp(_sumStep) *(*uintptr)(unsafe.Add(p, 4784)) = __ccgo_fp(_avgFinalize) *(*uintptr)(unsafe.Add(p, 4792)) = __ccgo_fp(_avgFinalize) *(*uintptr)(unsafe.Add(p, 4800)) = __ccgo_fp(_sumInverse) *(*uintptr)(unsafe.Add(p, 4848)) = __ccgo_fp(_countStep) *(*uintptr)(unsafe.Add(p, 4856)) = __ccgo_fp(_countFinalize) *(*uintptr)(unsafe.Add(p, 4864)) = __ccgo_fp(_countFinalize) *(*uintptr)(unsafe.Add(p, 4872)) = __ccgo_fp(_countInverse) *(*uintptr)(unsafe.Add(p, 4920)) = __ccgo_fp(_countStep) *(*uintptr)(unsafe.Add(p, 4928)) = __ccgo_fp(_countFinalize) *(*uintptr)(unsafe.Add(p, 4936)) = __ccgo_fp(_countFinalize) *(*uintptr)(unsafe.Add(p, 4944)) = __ccgo_fp(_countInverse) *(*uintptr)(unsafe.Add(p, 4992)) = __ccgo_fp(_groupConcatStep) *(*uintptr)(unsafe.Add(p, 5000)) = __ccgo_fp(_groupConcatFinalize) *(*uintptr)(unsafe.Add(p, 5008)) = __ccgo_fp(_groupConcatValue) *(*uintptr)(unsafe.Add(p, 5016)) = __ccgo_fp(_groupConcatInverse) *(*uintptr)(unsafe.Add(p, 5064)) = __ccgo_fp(_groupConcatStep) *(*uintptr)(unsafe.Add(p, 5072)) = __ccgo_fp(_groupConcatFinalize) *(*uintptr)(unsafe.Add(p, 5080)) = __ccgo_fp(_groupConcatValue) *(*uintptr)(unsafe.Add(p, 5088)) = __ccgo_fp(_groupConcatInverse) *(*uintptr)(unsafe.Add(p, 5136)) = __ccgo_fp(_groupConcatStep) *(*uintptr)(unsafe.Add(p, 5144)) = __ccgo_fp(_groupConcatFinalize) *(*uintptr)(unsafe.Add(p, 5152)) = __ccgo_fp(_groupConcatValue) *(*uintptr)(unsafe.Add(p, 5160)) = __ccgo_fp(_groupConcatInverse) *(*uintptr)(unsafe.Add(p, 5208)) = __ccgo_fp(_likeFunc) *(*uintptr)(unsafe.Add(p, 5280)) = __ccgo_fp(_likeFunc) *(*uintptr)(unsafe.Add(p, 5352)) = __ccgo_fp(_likeFunc) *(*uintptr)(unsafe.Add(p, 5552)) = __ccgo_fp(_xCeil) *(*uintptr)(unsafe.Add(p, 5568)) = __ccgo_fp(_ceilingFunc) *(*uintptr)(unsafe.Add(p, 5624)) = __ccgo_fp(_xCeil) *(*uintptr)(unsafe.Add(p, 5640)) = __ccgo_fp(_ceilingFunc) *(*uintptr)(unsafe.Add(p, 5696)) = __ccgo_fp(_xFloor) *(*uintptr)(unsafe.Add(p, 5712)) = __ccgo_fp(_ceilingFunc) *(*uintptr)(unsafe.Add(p, 5768)) = __ccgo_fp(libc.Xtrunc) *(*uintptr)(unsafe.Add(p, 5784)) = __ccgo_fp(_ceilingFunc) *(*uintptr)(unsafe.Add(p, 5856)) = __ccgo_fp(_logFunc) *(*uintptr)(unsafe.Add(p, 5928)) = __ccgo_fp(_logFunc) *(*uintptr)(unsafe.Add(p, 6000)) = __ccgo_fp(_logFunc) *(*uintptr)(unsafe.Add(p, 6072)) = __ccgo_fp(_logFunc) *(*uintptr)(unsafe.Add(p, 6144)) = __ccgo_fp(_logFunc) *(*uintptr)(unsafe.Add(p, 6200)) = __ccgo_fp(libc.Xexp) *(*uintptr)(unsafe.Add(p, 6216)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 6272)) = __ccgo_fp(libc.Xpow) *(*uintptr)(unsafe.Add(p, 6288)) = __ccgo_fp(_math2Func) *(*uintptr)(unsafe.Add(p, 6344)) = __ccgo_fp(libc.Xpow) *(*uintptr)(unsafe.Add(p, 6360)) = __ccgo_fp(_math2Func) *(*uintptr)(unsafe.Add(p, 6416)) = __ccgo_fp(libc.Xfmod) *(*uintptr)(unsafe.Add(p, 6432)) = __ccgo_fp(_math2Func) *(*uintptr)(unsafe.Add(p, 6488)) = __ccgo_fp(libc.Xacos) *(*uintptr)(unsafe.Add(p, 6504)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 6560)) = __ccgo_fp(libc.Xasin) *(*uintptr)(unsafe.Add(p, 6576)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 6632)) = __ccgo_fp(libc.Xatan) *(*uintptr)(unsafe.Add(p, 6648)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 6704)) = __ccgo_fp(libc.Xatan2) *(*uintptr)(unsafe.Add(p, 6720)) = __ccgo_fp(_math2Func) *(*uintptr)(unsafe.Add(p, 6776)) = __ccgo_fp(libc.Xcos) *(*uintptr)(unsafe.Add(p, 6792)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 6848)) = __ccgo_fp(libc.Xsin) *(*uintptr)(unsafe.Add(p, 6864)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 6920)) = __ccgo_fp(libc.Xtan) *(*uintptr)(unsafe.Add(p, 6936)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 6992)) = __ccgo_fp(libc.Xcosh) *(*uintptr)(unsafe.Add(p, 7008)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 7064)) = __ccgo_fp(libc.Xsinh) *(*uintptr)(unsafe.Add(p, 7080)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 7136)) = __ccgo_fp(libc.Xtanh) *(*uintptr)(unsafe.Add(p, 7152)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 7208)) = __ccgo_fp(libc.Xacosh) *(*uintptr)(unsafe.Add(p, 7224)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 7280)) = __ccgo_fp(libc.Xasinh) *(*uintptr)(unsafe.Add(p, 7296)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 7352)) = __ccgo_fp(libc.Xatanh) *(*uintptr)(unsafe.Add(p, 7368)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 7424)) = __ccgo_fp(libc.Xsqrt) *(*uintptr)(unsafe.Add(p, 7440)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 7496)) = __ccgo_fp(_degToRad) *(*uintptr)(unsafe.Add(p, 7512)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 7568)) = __ccgo_fp(_radToDeg) *(*uintptr)(unsafe.Add(p, 7584)) = __ccgo_fp(_math1Func) *(*uintptr)(unsafe.Add(p, 7656)) = __ccgo_fp(_piFunc) *(*uintptr)(unsafe.Add(p, 7728)) = __ccgo_fp(_signFunc) *(*uintptr)(unsafe.Add(p, 7800)) = __ccgo_fp(_versionFunc) *(*uintptr)(unsafe.Add(p, 7872)) = __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 + 64))).FzCol /* Name of left-most parent key column */ /* The caller is responsible for zeroing output parameters. */ /* 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)*16))).FzCnName, zKey) != 0) { return 0 } } } else { if paiCol != 0 { aiCol = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(nCol)*uint64(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 + 100))&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 + 64 + uintptr(i)*16))).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)*16) if !(zDfltColl != 0) { zDfltColl = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) } if _sqlite3StrICmp(tls, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i1)*8)), zDfltColl) != 0 { break } zIdxCol = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pParent)).FaCol + uintptr(iCol)*16))).FzCnName j = 0 for { if !(j < nCol) { break } if _sqlite3StrICmp(tls, (*(*TsColMap)(unsafe.Pointer(pFKey + 64 + uintptr(j)*16))).FzCol, zIdxCol) == 0 { if aiCol != 0 { *(*int32)(unsafe.Pointer(aiCol + uintptr(i1)*4)) = (*(*TsColMap)(unsafe.Pointer(pFKey + 64 + uintptr(j)*16))).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+16466, 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)))) 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. */ _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)*16 (*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 = int8(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 { *(*uintptr)(unsafe.Pointer(pExpr + 64)) = 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(64) defer tls.Free(64) 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) 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 + 64))).FiFrom } iCol = int16(v3) zCol = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FaCol + uintptr(iCol)*16))).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) i = 0 for { if !(i < int32((*TIndex)(unsafe.Pointer(pIdx)).FnKeyCol)) { break } iCol1 = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(i)*2)) pLeft1 = _exprTableRegister(tls, pParse, pTab, regData, iCol1) pRight1 = _sqlite3Expr(tls, db, int32(TK_ID), (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol1)*16))).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.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) (*(*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+80, (*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)*32))).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 + 64))).FpFKey for { if !(pFKey != 0) { break } _fkTriggerDelete(tls, db, *(*uintptr)(unsafe.Pointer(pFKey + 48))) *(*uintptr)(unsafe.Pointer(pFKey + 48)) = uintptr(0) _fkTriggerDelete(tls, db, *(*uintptr)(unsafe.Pointer(pFKey + 48 + 1*8))) *(*uintptr)(unsafe.Pointer(pFKey + 48 + 1*8)) = 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) /* VDBE has already been allocated */ if _sqlite3FkReferences(tls, pTab) == uintptr(0) { p = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 64))).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 + 64 + uintptr(i)*16))).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)*16 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 + 48)) && int32(*(*Tu8)(unsafe.Pointer(pFKey + 45))) == int32(OE_SetNull) || p == *(*uintptr)(unsafe.Pointer(pFKey + 48 + 1*8)) && int32(*(*Tu8)(unsafe.Pointer(pFKey + 45 + 1))) == int32(OE_SetNull) { 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(48) defer tls.Free(48) 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+32 */ uintptr var _ /* aiFree at bp+8 */ uintptr var _ /* iCol at bp+16 */ int32 var _ /* pIdx at bp+0 */ uintptr var _ /* pIdx at bp+24 */ 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. */ /* 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)*32))).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 + 64))).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 + 8)) = 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+8) != 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 + 64 + uintptr(i)*16))).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 } if *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 { aiCol = *(*uintptr)(unsafe.Pointer(bp + 8)) } else { *(*int32)(unsafe.Pointer(bp + 16)) = (*(*TsColMap)(unsafe.Pointer(pFKey + 64))).FiFrom aiCol = bp + 16 } 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) } /* 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)*16))).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 + 8))) 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 + 24)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 32)) = 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) { /* 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+24, bp+32) != 0 { if !(isIgnoreErrors != 0) || (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return } goto _5 } /* 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 + 52 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 + 24)), pFKey, *(*uintptr)(unsafe.Pointer(bp + 32)), regNew, -int32(1)) } if regOld != 0 { eAction = int32(*(*Tu8)(unsafe.Pointer(pFKey + 45 + 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 + 64 + uintptr(i)*16))).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 } 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 + 64))).FpFKey != 0) } else { /* Check if any child key columns are being modified. */ p = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 64))).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+8) != 0 { return uintptr(0) } i = 0 for { if !(i < (*TFKey)(unsafe.Pointer(pFKey)).FnCol) { break } *(*TToken)(unsafe.Pointer(bp + 16)) = TToken{ Fz: __ccgo_ts + 7008, Fn: uint32(3), } /* Literal "old" token */ *(*TToken)(unsafe.Pointer(bp + 32)) = TToken{ Fz: __ccgo_ts + 7004, Fn: uint32(3), } /* tFromCol = OLD.tToCol */ if *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 { v2 = *(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)) + uintptr(i)*4)) } else { v2 = (*(*TsColMap)(unsafe.Pointer(pFKey + 64))).FiFrom } iFromCol = v2 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+64, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(v3)*16))).FzCnName) _sqlite3TokenInit(tls, bp+48, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FaCol + uintptr(iFromCol)*16))).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+16, 0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+64, 0)), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+48, 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+16, 0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+64, 0)), _sqlite3PExpr(tls, pParse, int32(TK_DOT), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+32, 0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+64, 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+32, 0), _sqlite3ExprAlloc(tls, db, int32(TK_ID), bp+64, 0)) } else { if action == int32(OE_SetDflt) { pCol = (*TTable)(unsafe.Pointer((*TFKey)(unsafe.Pointer(pFKey)).FpFrom)).FaCol + uintptr(iFromCol)*16 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+48, 0) } goto _1 _1: ; i++ } _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer(bp + 8))) 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+5637) if pRaise != 0 { (*TExpr)(unsafe.Pointer(pRaise)).FaffExpr = int8(OE_Abort) } pSrc = _sqlite3SrcListAppend(tls, pParse, uintptr(0), uintptr(0), uintptr(0)) if pSrc != 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)*32))).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, libc.Uint64FromInt64(72)+libc.Uint64FromInt64(96)+uint64(nFrom)+uint64(1)) if pTrigger != 0 { v4 = pTrigger + 1*72 (*TTrigger)(unsafe.Pointer(pTrigger)).Fstep_list = v4 pStep = v4 (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget = pStep + 1*96 libc.X__builtin___memcpy_chk(tls, (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget, zFrom, uint64(nFrom), libc.X__builtin_object_size(tls, (*TTriggerStep)(unsafe.Pointer(pStep)).FzTarget, 0)) (*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) } 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 + 48 + uintptr(iAction)*8)) = 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 */ pFKey = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab + 64))).FpFKey for { if !(pFKey != 0) { break } /* 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+80, 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. */ /* Delete any triggers created to implement actions for this FK. */ _fkTriggerDelete(tls, db, *(*uintptr)(unsafe.Pointer(pFKey + 48))) _fkTriggerDelete(tls, db, *(*uintptr)(unsafe.Pointer(pFKey + 48 + 1*8))) 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 v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe 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) _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 int8 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)*16))).Faffinity } else { if int32(x) == -int32(1) { aff = int8(SQLITE_AFF_INTEGER) } else { aff = _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(n)*32))).FpExpr) } } if int32(aff) < int32(SQLITE_AFF_BLOB) { aff = int8(SQLITE_AFF_BLOB) } if int32(aff) > int32(SQLITE_AFF_NUMERIC) { aff = int8(SQLITE_AFF_NUMERIC) } *(*int8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FzColAff + uintptr(n))) = aff goto _1 _1: ; n++ } *(*int8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FzColAff + uintptr(n))) = 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)*16))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { v3 = j j++ *(*int8)(unsafe.Pointer(zColAff + uintptr(v3))) = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*16))).Faffinity } goto _1 _1: ; i++ } for cond := true; cond; cond = j >= 0 && int32(*(*int8)(unsafe.Pointer(zColAff + uintptr(j)))) <= int32(SQLITE_AFF_BLOB) { v4 = j j-- *(*int8)(unsafe.Pointer(zColAff + uintptr(v4))) = 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) (*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 } i = int32(libc.Xstrlen(tls, zColAff) & libc.Uint64FromInt32(0x3fffffff)) if i != 0 { if iReg != 0 { _sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), iReg, i, 0, zColAff, i) } else { _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) 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 { 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 { p1 = pWalker + 36 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | int32((*(*TColumn)(unsafe.Pointer((*TTable1)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalker + 40)))).FaCol + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)*16))).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(64) defer tls.Free(64) 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 /* 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)) v2 = libc.Int32FromInt32(0) jj = v2 ii = v2 for { if !(*(*int8)(unsafe.Pointer(zP4 + uintptr(jj))) != 0) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(ii)*16))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { goto _1 } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(ii)*16))).FcolFlags)&int32(COLFLAG_STORED) != 0 { *(*int8)(unsafe.Pointer(zP4 + uintptr(jj))) = int8(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)*16))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { p4 = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*16 + 14 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | libc.Int32FromInt32(COLFLAG_NOTAVAIL)) } goto _3 _3: ; i++ } *(*uintptr)(unsafe.Pointer(bp + 40)) = 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)*16 if int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags)&int32(COLFLAG_NOTAVAIL) != 0 { p6 = pCol + 14 *(*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 + 14 *(*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) x = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(i))) + iRegStore _sqlite3ExprCodeGeneratedColumn(tls, pParse, pTab, pCol, x) p8 = pCol + 14 *(*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+8635, libc.VaList(bp+56, (*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 */ 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)*32))).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 p = (*TParse)(unsafe.Pointer(pParse)).FpAinc for { if !(p != 0) { break } pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr((*TAutoincInfo)(unsafe.Pointer(p)).FiDb)*32 memId = (*TAutoincInfo)(unsafe.Pointer(p)).FregCtr iRec = _sqlite3GetTempReg(tls, pParse) _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.Uint64FromInt64(20)/libc.Uint64FromInt64(4)), uintptr(unsafe.Pointer(&_autoIncEnd)), _iLn2) if aOp == uintptr(0) { break } (*(*TVdbeOp)(unsafe.Pointer(aOp))).Fp1 = memId + int32(1) (*(*TVdbeOp)(unsafe.Pointer(aOp + 1*24))).Fp2 = memId + int32(1) (*(*TVdbeOp)(unsafe.Pointer(aOp + 2*24))).Fp1 = memId - int32(1) (*(*TVdbeOp)(unsafe.Pointer(aOp + 2*24))).Fp3 = iRec (*(*TVdbeOp)(unsafe.Pointer(aOp + 3*24))).Fp2 = iRec (*(*TVdbeOp)(unsafe.Pointer(aOp + 3*24))).Fp3 = memId + int32(1) (*(*TVdbeOp)(unsafe.Pointer(aOp + 3*24))).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 // // /* // ** If argument pVal is a Select object returned by an sqlite3MultiValues() // ** that was able to use the co-routine optimization, finish coding the // ** co-routine. // */ func _sqlite3MultiValuesEnd(tls *libc.TLS, pParse uintptr, pVal uintptr) { var pItem uintptr _ = pItem if pVal != 0 && (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pVal)).FpSrc)).FnSrc > 0 { pItem = (*TSelect)(unsafe.Pointer(pVal)).FpSrc + 8 _sqlite3VdbeEndCoroutine(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, (*TSrcItem)(unsafe.Pointer(pItem)).FregReturn) _sqlite3VdbeJumpHere(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, (*TSrcItem)(unsafe.Pointer(pItem)).FaddrFillSub-int32(1)) } } // C documentation // // /* // ** Return true if all expressions in the expression-list passed as the // ** only argument are constant. // */ func _exprListIsConstant(tls *libc.TLS, pParse uintptr, pRow uintptr) (r int32) { var ii int32 _ = ii ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer(pRow)).FnExpr) { break } if 0 == _sqlite3ExprIsConstant(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pRow + 8 + uintptr(ii)*32))).FpExpr) { return 0 } goto _1 _1: ; ii++ } return int32(1) } // C documentation // // /* // ** Return true if all expressions in the expression-list passed as the // ** only argument are both constant and have no affinity. // */ func _exprListIsNoAffinity(tls *libc.TLS, pParse uintptr, pRow uintptr) (r int32) { var ii int32 var pExpr uintptr _, _ = ii, pExpr if _exprListIsConstant(tls, pParse, pRow) == 0 { return 0 } ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer(pRow)).FnExpr) { break } pExpr = (*(*TExprList_item)(unsafe.Pointer(pRow + 8 + uintptr(ii)*32))).FpExpr if 0 != int32(_sqlite3ExprAffinity(tls, pExpr)) { return 0 } goto _1 _1: ; ii++ } return int32(1) } // C documentation // // /* // ** This function is called by the parser for the second and subsequent // ** rows of a multi-row VALUES clause. Argument pLeft is the part of // ** the VALUES clause already parsed, argument pRow is the vector of values // ** for the new row. The Select object returned represents the complete // ** VALUES clause, including the new row. // ** // ** There are two ways in which this may be achieved - by incremental // ** coding of a co-routine (the "co-routine" method) or by returning a // ** Select object equivalent to the following (the "UNION ALL" method): // ** // ** "pLeft UNION ALL SELECT pRow" // ** // ** If the VALUES clause contains a lot of rows, this compound Select // ** object may consume a lot of memory. // ** // ** When the co-routine method is used, each row that will be returned // ** by the VALUES clause is coded into part of a co-routine as it is // ** passed to this function. The returned Select object is equivalent to: // ** // ** SELECT * FROM ( // ** Select object to read co-routine // ** ) // ** // ** The co-routine method is used in most cases. Exceptions are: // ** // ** a) If the current statement has a WITH clause. This is to avoid // ** statements like: // ** // ** WITH cte AS ( VALUES('x'), ('y') ... ) // ** SELECT * FROM cte AS a, cte AS b; // ** // ** This will not work, as the co-routine uses a hard-coded register // ** for its OP_Yield instructions, and so it is not possible for two // ** cursors to iterate through it concurrently. // ** // ** b) The schema is currently being parsed (i.e. the VALUES clause is part // ** of a schema item like a VIEW or TRIGGER). In this case there is no VM // ** being generated when parsing is taking place, and so generating // ** a co-routine is not possible. // ** // ** c) There are non-constant expressions in the VALUES clause (e.g. // ** the VALUES clause is part of a correlated sub-query). // ** // ** d) One or more of the values in the first row of the VALUES clause // ** has an affinity (i.e. is a CAST expression). This causes problems // ** because the complex rules SQLite uses (see function // ** sqlite3SubqueryColumnTypes() in select.c) to determine the effective // ** affinity of such a column for all rows require access to all values in // ** the column simultaneously. // */ func _sqlite3MultiValues(tls *libc.TLS, pParse uintptr, pLeft uintptr, pRow uintptr) (r uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var f, v1 int32 var p, pRet, pSelect, v, v2 uintptr var _ /* dest at bp+0 */ TSelectDest _, _, _, _, _, _, _ = f, p, pRet, pSelect, v, v1, v2 if (*TParse)(unsafe.Pointer(pParse)).FbHasWith != 0 || (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Finit1.Fbusy != 0 || _exprListIsConstant(tls, pParse, pRow) == 0 || (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pLeft)).FpSrc)).FnSrc == 0 && _exprListIsNoAffinity(tls, pParse, (*TSelect)(unsafe.Pointer(pLeft)).FpEList) == 0 || int32((*TParse)(unsafe.Pointer(pParse)).FeParseMode) != PARSE_MODE_NORMAL { /* The co-routine method cannot be used. Fall back to UNION ALL. */ pSelect = uintptr(0) f = libc.Int32FromInt32(SF_Values) | libc.Int32FromInt32(SF_MultiValue) if (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pLeft)).FpSrc)).FnSrc != 0 { _sqlite3MultiValuesEnd(tls, pParse, pLeft) f = int32(SF_Values) } else { if (*TSelect)(unsafe.Pointer(pLeft)).FpPrior != 0 { /* In this case set the SF_MultiValue flag only if it was set on pLeft */ f = int32(uint32(f) & (*TSelect)(unsafe.Pointer(pLeft)).FselFlags) } } pSelect = _sqlite3SelectNew(tls, pParse, pRow, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(f), uintptr(0)) *(*Tu32)(unsafe.Pointer(pLeft + 4)) &= uint32(^libc.Int32FromInt32(SF_MultiValue)) if pSelect != 0 { (*TSelect)(unsafe.Pointer(pSelect)).Fop = uint8(TK_ALL) (*TSelect)(unsafe.Pointer(pSelect)).FpPrior = pLeft pLeft = pSelect } } else { p = uintptr(0) /* SrcItem that reads from co-routine */ if (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pLeft)).FpSrc)).FnSrc == 0 { /* Co-routine has not yet been started and the special Select object ** that accesses the co-routine has not yet been created. This block ** does both those things. */ v = _sqlite3GetVdbe(tls, pParse) pRet = _sqlite3SelectNew(tls, pParse, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(0), uintptr(0)) /* Ensure the database schema has been read. This is to ensure we have ** the correct text encoding. */ if (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmDbFlags&uint32(DBFLAG_SchemaKnownOk) == uint32(0) { _sqlite3ReadSchema(tls, pParse) } if pRet != 0 { (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pRet)).FpSrc)).FnSrc = int32(1) (*TSelect)(unsafe.Pointer(pRet)).FpPrior = (*TSelect)(unsafe.Pointer(pLeft)).FpPrior (*TSelect)(unsafe.Pointer(pRet)).Fop = (*TSelect)(unsafe.Pointer(pLeft)).Fop (*TSelect)(unsafe.Pointer(pLeft)).FpPrior = uintptr(0) (*TSelect)(unsafe.Pointer(pLeft)).Fop = uint8(TK_SELECT) p = (*TSelect)(unsafe.Pointer(pRet)).FpSrc + 8 (*TSrcItem)(unsafe.Pointer(p)).FpSelect = pLeft libc.SetBitFieldPtr16Uint32(p+60+4, libc.Uint32FromInt32(1), 5, 0x20) (*TSrcItem)(unsafe.Pointer(p)).FaddrFillSub = _sqlite3VdbeCurrentAddr(tls, v) + int32(1) v2 = pParse + 56 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) (*TSrcItem)(unsafe.Pointer(p)).FregReturn = v1 (*TSrcItem)(unsafe.Pointer(p)).FiCursor = -int32(1) *(*Tu32)(unsafe.Pointer(&(*TSrcItem)(unsafe.Pointer(p)).Fu1)) = uint32(2) _sqlite3VdbeAddOp3(tls, v, int32(OP_InitCoroutine), (*TSrcItem)(unsafe.Pointer(p)).FregReturn, 0, (*TSrcItem)(unsafe.Pointer(p)).FaddrFillSub) _sqlite3SelectDestInit(tls, bp, int32(SRT_Coroutine), (*TSrcItem)(unsafe.Pointer(p)).FregReturn) /* Allocate registers for the output of the co-routine. Do so so ** that there are two unused registers immediately before those ** used by the co-routine. This allows the code in sqlite3Insert() ** to use these registers directly, instead of copying the output ** of the co-routine to a separate array for processing. */ (*(*TSelectDest)(unsafe.Pointer(bp))).FiSdst = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(3) (*(*TSelectDest)(unsafe.Pointer(bp))).FnSdst = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pLeft)).FpEList)).FnExpr *(*int32)(unsafe.Pointer(pParse + 56)) += int32(2) + (*(*TSelectDest)(unsafe.Pointer(bp))).FnSdst *(*Tu32)(unsafe.Pointer(pLeft + 4)) |= uint32(SF_MultiValue) _sqlite3Select(tls, pParse, pLeft, bp) (*TSrcItem)(unsafe.Pointer(p)).FregResult = (*(*TSelectDest)(unsafe.Pointer(bp))).FiSdst pLeft = pRet } } else { p = (*TSelect)(unsafe.Pointer(pLeft)).FpSrc + 8 *(*Tu32)(unsafe.Pointer(&(*TSrcItem)(unsafe.Pointer(p)).Fu1))++ } if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 { if (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(p)).FpSelect)).FpEList)).FnExpr != (*TExprList)(unsafe.Pointer(pRow)).FnExpr { _sqlite3SelectWrongNumTermsError(tls, pParse, (*TSrcItem)(unsafe.Pointer(p)).FpSelect) } else { _sqlite3ExprCodeExprList(tls, pParse, pRow, (*TSrcItem)(unsafe.Pointer(p)).FregResult, 0, uint8(0)) _sqlite3VdbeAddOp1(tls, (*TParse)(unsafe.Pointer(pParse)).FpVdbe, int32(OP_Yield), (*TSrcItem)(unsafe.Pointer(p)).FregReturn) } } _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pRow) } return pLeft } // 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(160) defer tls.Free(160) var aRegIdx, db, pIdx, pIpk, pItem, pList, pNx, pTab, pTrigger, pVTab, pX, v, v10, v14, v17, v19, v7 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, v13, v16, v18, v20, v24, v25, v5, v6, v8, v9 int32 var appendFlag, bIdListInOrder, useTempTable, withoutRowid Tu8 var colFlags, v22 Tu32 var _ /* dest at bp+8 */ TSelectDest var _ /* iDataCur at bp+0 */ int32 var _ /* iIdxCur at bp+4 */ int32 var _ /* isReplace at bp+112 */ int32 var _ /* sNC at bp+56 */ TNameContext var _ /* tmask at bp+48 */ 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, pItem, pList, pNx, pTab, pTrigger, pVTab, pX, rc, regAutoinc, regCols, regData, regFromSelect, regIns, regRec, regRowCount, regRowid, regTempRowid, regYield, srcTab, useTempTable, v, withoutRowid, y, v1, v10, v13, v14, v16, v17, v18, v19, v20, v22, v24, v25, 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 if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto insert_cleanup } (*(*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. */ pTab = _sqlite3SrcListLookup(tls, pParse, pTabList) if pTab == uintptr(0) { goto insert_cleanup } iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab)).FpSchema) if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_INSERT), (*TTable)(unsafe.Pointer(pTab)).FzName, uintptr(0), (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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+48) isView = libc.BoolInt32(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW)) /* 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 { 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 + 56)) += 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 { (*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)*16 + 8)) = -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)*16))).FzName, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*16))).FzCnName) == 0 { *(*int32)(unsafe.Pointer(pColumn + 8 + uintptr(i)*16 + 8)) = j if i != j { bIdListInOrder = uint8(0) } if j == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { ipkColumn = i } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*16))).FcolFlags)&(libc.Int32FromInt32(COLFLAG_STORED)|libc.Int32FromInt32(COLFLAG_VIRTUAL)) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16511, libc.VaList(bp+128, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*16))).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)*16))).FzName) != 0 && !(withoutRowid != 0) { ipkColumn = i bIdListInOrder = uint8(0) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16552, libc.VaList(bp+128, pTabList+8, (*(*TIdList_item)(unsafe.Pointer(pColumn + 8 + uintptr(i)*16))).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 */ if (*TSrcList)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpSrc)).FnSrc == int32(1) && int32(uint32(*(*uint16)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSelect)).FpSrc + 8 + 60 + 4))&0x20>>5)) != 0 && (*TSelect)(unsafe.Pointer(pSelect)).FpPrior == uintptr(0) { pItem = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc + 8 (*(*TSelectDest)(unsafe.Pointer(bp + 8))).FiSDParm = (*TSrcItem)(unsafe.Pointer(pItem)).FregReturn regFromSelect = (*TSrcItem)(unsafe.Pointer(pItem)).FregResult nColumn = (*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSelect)).FpEList)).FnExpr _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+16584, libc.VaList(bp+128, pItem)) if bIdListInOrder != 0 && nColumn == int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { regData = regFromSelect regRowid = regData - int32(1) if int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) { v5 = int32(1) } else { v5 = 0 } regIns = regRowid - v5 } } else { v7 = pParse + 56 *(*int32)(unsafe.Pointer(v7))++ v6 = *(*int32)(unsafe.Pointer(v7)) /* Top of the co-routine */ regYield = v6 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 { v8 = regData } else { v8 = 0 } (*(*TSelectDest)(unsafe.Pointer(bp + 8))).FiSdst = v8 (*(*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 if rc != 0 || (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto insert_cleanup } _sqlite3VdbeEndCoroutine(tls, v, regYield) _sqlite3VdbeJumpHere(tls, v, addrTop-int32(1)) /* label B: */ 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" */ v10 = pParse + 52 v9 = *(*int32)(unsafe.Pointer(v10)) *(*int32)(unsafe.Pointer(v10))++ srcTab = v9 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.X__builtin___memset_chk(tls, bp+56, 0, uint64(56), libc.X__builtin_object_size(tls, bp+56, 0)) (*(*TNameContext)(unsafe.Pointer(bp + 56))).FpParse = pParse srcTab = -int32(1) if pList != 0 { nColumn = (*TExprList)(unsafe.Pointer(pList)).FnExpr if _sqlite3ResolveExprListNames(tls, bp+56, 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)*16))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { ipkColumn-- } goto _11 _11: ; i-- } } /* Make sure the number of columns in the source data matches the number ** of columns to be inserted into the table. */ 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)*16))).FcolFlags)&int32(COLFLAG_NOINSERT) != 0 { nHidden++ } goto _12 _12: ; i++ } } if nColumn != int32((*TTable)(unsafe.Pointer(pTab)).FnCol)-nHidden { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+16592, libc.VaList(bp+128, 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+16644, libc.VaList(bp+128, 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: ... */ v20 = _sqlite3VdbeAddOp1(tls, v, int32(OP_Yield), (*(*TSelectDest)(unsafe.Pointer(bp + 8))).FiSDParm) addrCont = v20 addrInsTop = v20 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 i = 0 for { if !(i < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } 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 _21 } v22 = uint32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*16))).FcolFlags) colFlags = v22 if v22&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 _21 } 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 + 48))&int32(TRIGGER_BEFORE) != 0 { _sqlite3VdbeAddOp1(tls, v, int32(OP_SoftNull), iRegStore) } goto _21 } 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)*16), iRegStore) goto _21 } } } } if pColumn != 0 { j = 0 for { if !(j < (*TIdList)(unsafe.Pointer(pColumn)).FnId && *(*int32)(unsafe.Pointer(pColumn + 8 + uintptr(j)*16 + 8)) != i) { break } goto _23 _23: ; 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)*16), iRegStore) goto _21 } 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)*16), iRegStore) goto _21 } 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)*32))).FpExpr y = _sqlite3ExprCodeTarget(tls, pParse, pX, iRegStore) if y != iRegStore { if (*TExpr)(unsafe.Pointer(pX)).Fflags&uint32(libc.Int32FromInt32(EP_Subquery)) != uint32(0) { v24 = int32(OP_Copy) } else { v24 = int32(OP_SCopy) } _sqlite3VdbeAddOp2(tls, v, v24, y, iRegStore) } } } goto _21 _21: ; i++ iRegStore++ } /* Run the BEFORE and INSTEAD OF triggers, if there are any */ endOfLoop = _sqlite3VdbeMakeLabel(tls, pParse) if *(*int32)(unsafe.Pointer(bp + 48))&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 { if useTempTable != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), srcTab, ipkColumn, regCols) } else { /* Otherwise useTempTable is true */ _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ipkColumn)*32))).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. */ _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)*32))).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)*32))).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) { v25 = int32(OE_Abort) } else { v25 = onError } _sqlite3VdbeChangeP5(tls, v, uint16(v25)) _sqlite3MayAbort(tls, pParse) } else { *(*int32)(unsafe.Pointer(bp + 112)) = 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+112, 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 + 112)) == 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+16736) } 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) { if int32((*TExpr)(unsafe.Pointer(pExpr)).FiColumn) >= 0 { if *(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalker + 40)) + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiColumn)*4)) >= 0 { p1 = pWalker + 36 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(CKCNSTRNT_COLUMN)) } } else { p2 = pWalker + 36 *(*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(48) defer tls.Free(48) var p1 uintptr var _ /* w at bp+0 */ TWalker _ = p1 libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*TWalker)(unsafe.Pointer(bp))).FeCode = uint16(0) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_checkConstraintExprNode) *(*uintptr)(unsafe.Pointer(bp + 40)) = aiChng _sqlite3WalkExpr(tls, bp, pExpr) if !(chngRowid != 0) { p1 = bp + 36 *(*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 [8]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 [8]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) { 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)*16))).Fix return (*(*TIndexListTerm)(unsafe.Pointer((*(*struct { FnIdx int32 FaIdx uintptr })(unsafe.Pointer(pIter + 8))).FaIdx + uintptr(i)*16))).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(80) defer tls.Free(80) 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+8 */ TIndexIterator var _ /* x at bp+32 */ 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 /* 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)*16 /* non-zero if column is generated */ onError = int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 8)) & 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 { } } else { if b2ndPass != 0 && !(isGenerated != 0) { goto _1 } } iReg = int32(_sqlite3TableColumnToStorage(tls, pTab, int16(i))) + regNewData + int32(1) switch onError { case int32(OE_Replace): addr1 = _sqlite3VdbeAddOp1(tls, v, int32(OP_NotNull), iReg) 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+12854, libc.VaList(bp+64, (*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)*32))).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)*32))).FzEName 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)*8))) x1 = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pPk)).FaiColumn + uintptr(i)*2))) 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 */ 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 */ nConflictCk = _sqlite3VdbeCurrentAddr(tls, v) - addrConflictCk 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 + 32)) = *(*TVdbeOp)(unsafe.Pointer(_sqlite3VdbeGetOp(tls, v, addrConflictCk))) if int32((*(*TVdbeOp)(unsafe.Pointer(bp + 32))).Fopcode) != int32(OP_IdxRowid) { if int32(_sqlite3OpcodeProperty[(*(*TVdbeOp)(unsafe.Pointer(bp + 32))).Fopcode])&int32(OPFLG_JUMP) != 0 { p2 = lblRecheckOk } else { p2 = (*(*TVdbeOp)(unsafe.Pointer(bp + 32))).Fp2 } if int32((*(*TVdbeOp)(unsafe.Pointer(bp + 32))).Fp4type) == -int32(3) { v16 = uintptr(int64(*(*int32)(unsafe.Pointer(bp + 32 + 16)))) } else { v16 = *(*uintptr)(unsafe.Pointer(bp + 32 + 16)) } zP4 = v16 _sqlite3VdbeAddOp4(tls, v, int32((*(*TVdbeOp)(unsafe.Pointer(bp + 32))).Fopcode), (*(*TVdbeOp)(unsafe.Pointer(bp + 32))).Fp1, p2, (*(*TVdbeOp)(unsafe.Pointer(bp + 32))).Fp3, zP4, int32((*(*TVdbeOp)(unsafe.Pointer(bp + 32))).Fp4type)) _sqlite3VdbeChangeP5(tls, v, (*(*TVdbeOp)(unsafe.Pointer(bp + 32))).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+8, bp) } /* If the IPK constraint is a REPLACE, run it last */ if ipkTop != 0 { _sqlite3VdbeGoto(tls, v, ipkTop) _sqlite3VdbeJumpHere(tls, v, ipkBottom) } /* Recheck all uniqueness constraints after replace triggers have run */ 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) _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 */ v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* 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 */ 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 + 100))&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 + 100))&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 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 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 if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 100))&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 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) { if _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pSrc)).FaColExpr + 8 + uintptr(i)*32))).FpExpr, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pDest)).FaColExpr + 8 + uintptr(i)*32))).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)*8)), *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pDest)).FazColl + uintptr(i)*8))) != 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 */ 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) } } /* 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 if (*TExprList)(unsafe.Pointer(pEList)).FnExpr != int32(1) { return 0 /* The result set must have exactly one column */ } 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)*16 pSrcCol = (*TTable)(unsafe.Pointer(pSrc)).FaCol + uintptr(i)*16 /* 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 + 8))&0xf>>0)) != 0 && !(int32(uint32(*(*uint8)(unsafe.Pointer(pSrcCol + 8))&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) 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] */ if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ForeignKeys) != uint64(0) && (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pDest + 64))).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) } } 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 } _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)*8)) 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 + 100))&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 + 100))&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+8 */ uintptr var _ /* zLeftover at bp+0 */ uintptr _, _, _, _, _, _ = azCols, azVals, callbackIsInit, i, nCol, rc rc = SQLITE_OK /* Tail of unprocessed SQL */ *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) /* The current SQL statement */ azCols = uintptr(0) /* True if callback data is initialized */ if !(_sqlite3SafetyCheckOk(tls, db) != 0) { return _sqlite3MisuseError(tls, int32(136304)) } if zSql == uintptr(0) { zSql = __ccgo_ts + 1664 } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) _sqlite3Error(tls, db, SQLITE_OK) for rc == SQLITE_OK && *(*int8)(unsafe.Pointer(zSql)) != 0 { nCol = 0 azVals = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) rc = Xsqlite3_prepare_v2(tls, db, zSql, -int32(1), bp+8, bp) if rc != SQLITE_OK { continue } if !(*(*uintptr)(unsafe.Pointer(bp + 8)) != 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 + 8))) /* 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 + 8))) azCols = _sqlite3DbMallocRaw(tls, db, uint64(libc.Int32FromInt32(2)*nCol+libc.Int32FromInt32(1))*uint64(8)) if azCols == uintptr(0) { goto exec_out } i = 0 for { if !(i < nCol) { break } *(*uintptr)(unsafe.Pointer(azCols + uintptr(i)*8)) = Xsqlite3_column_name(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), i) /* sqlite3VdbeSetColName() installs column names as UTF8 ** strings so there is no way for sqlite3_column_name() to fail. */ goto _1 _1: ; i++ } callbackIsInit = int32(1) } if rc == int32(SQLITE_ROW) { azVals = azCols + uintptr(nCol)*8 i = 0 for { if !(i < nCol) { break } *(*uintptr)(unsafe.Pointer(azVals + uintptr(i)*8)) = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), i) if !(*(*uintptr)(unsafe.Pointer(azVals + uintptr(i)*8)) != 0) && Xsqlite3_column_type(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), i) != int32(SQLITE_NULL) { _sqlite3OomFault(tls, db) goto exec_out } goto _2 _2: ; i++ } *(*uintptr)(unsafe.Pointer(azVals + uintptr(i)*8)) = 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 + 8))) *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) _sqlite3Error(tls, db, int32(SQLITE_ABORT)) goto exec_out } } if rc != int32(SQLITE_ROW) { rc = _sqlite3VdbeFinalize(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) zSql = *(*uintptr)(unsafe.Pointer(bp)) for int32(_sqlite3CtypeMap[uint8(*(*int8)(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 + 8)) != 0 { _sqlite3VdbeFinalize(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) } _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) } } 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, 8)) = __ccgo_fp(Xsqlite3_aggregate_count) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(Xsqlite3_bind_blob) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(Xsqlite3_bind_double) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(Xsqlite3_bind_int) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(Xsqlite3_bind_int64) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(Xsqlite3_bind_null) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(Xsqlite3_bind_parameter_count) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(Xsqlite3_bind_parameter_index) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(Xsqlite3_bind_parameter_name) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(Xsqlite3_bind_text) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(Xsqlite3_bind_text16) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(Xsqlite3_bind_value) *(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(Xsqlite3_busy_handler) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(Xsqlite3_busy_timeout) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(Xsqlite3_changes) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(Xsqlite3_close) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(Xsqlite3_collation_needed) *(*uintptr)(unsafe.Add(p, 144)) = __ccgo_fp(Xsqlite3_collation_needed16) *(*uintptr)(unsafe.Add(p, 152)) = __ccgo_fp(Xsqlite3_column_blob) *(*uintptr)(unsafe.Add(p, 160)) = __ccgo_fp(Xsqlite3_column_bytes) *(*uintptr)(unsafe.Add(p, 168)) = __ccgo_fp(Xsqlite3_column_bytes16) *(*uintptr)(unsafe.Add(p, 176)) = __ccgo_fp(Xsqlite3_column_count) *(*uintptr)(unsafe.Add(p, 184)) = __ccgo_fp(Xsqlite3_column_database_name) *(*uintptr)(unsafe.Add(p, 192)) = __ccgo_fp(Xsqlite3_column_database_name16) *(*uintptr)(unsafe.Add(p, 200)) = __ccgo_fp(Xsqlite3_column_decltype) *(*uintptr)(unsafe.Add(p, 208)) = __ccgo_fp(Xsqlite3_column_decltype16) *(*uintptr)(unsafe.Add(p, 216)) = __ccgo_fp(Xsqlite3_column_double) *(*uintptr)(unsafe.Add(p, 224)) = __ccgo_fp(Xsqlite3_column_int) *(*uintptr)(unsafe.Add(p, 232)) = __ccgo_fp(Xsqlite3_column_int64) *(*uintptr)(unsafe.Add(p, 240)) = __ccgo_fp(Xsqlite3_column_name) *(*uintptr)(unsafe.Add(p, 248)) = __ccgo_fp(Xsqlite3_column_name16) *(*uintptr)(unsafe.Add(p, 256)) = __ccgo_fp(Xsqlite3_column_origin_name) *(*uintptr)(unsafe.Add(p, 264)) = __ccgo_fp(Xsqlite3_column_origin_name16) *(*uintptr)(unsafe.Add(p, 272)) = __ccgo_fp(Xsqlite3_column_table_name) *(*uintptr)(unsafe.Add(p, 280)) = __ccgo_fp(Xsqlite3_column_table_name16) *(*uintptr)(unsafe.Add(p, 288)) = __ccgo_fp(Xsqlite3_column_text) *(*uintptr)(unsafe.Add(p, 296)) = __ccgo_fp(Xsqlite3_column_text16) *(*uintptr)(unsafe.Add(p, 304)) = __ccgo_fp(Xsqlite3_column_type) *(*uintptr)(unsafe.Add(p, 312)) = __ccgo_fp(Xsqlite3_column_value) *(*uintptr)(unsafe.Add(p, 320)) = __ccgo_fp(Xsqlite3_commit_hook) *(*uintptr)(unsafe.Add(p, 328)) = __ccgo_fp(Xsqlite3_complete) *(*uintptr)(unsafe.Add(p, 336)) = __ccgo_fp(Xsqlite3_complete16) *(*uintptr)(unsafe.Add(p, 344)) = __ccgo_fp(Xsqlite3_create_collation) *(*uintptr)(unsafe.Add(p, 352)) = __ccgo_fp(Xsqlite3_create_collation16) *(*uintptr)(unsafe.Add(p, 360)) = __ccgo_fp(Xsqlite3_create_function) *(*uintptr)(unsafe.Add(p, 368)) = __ccgo_fp(Xsqlite3_create_function16) *(*uintptr)(unsafe.Add(p, 376)) = __ccgo_fp(Xsqlite3_create_module) *(*uintptr)(unsafe.Add(p, 384)) = __ccgo_fp(Xsqlite3_data_count) *(*uintptr)(unsafe.Add(p, 392)) = __ccgo_fp(Xsqlite3_db_handle) *(*uintptr)(unsafe.Add(p, 400)) = __ccgo_fp(Xsqlite3_declare_vtab) *(*uintptr)(unsafe.Add(p, 408)) = __ccgo_fp(Xsqlite3_enable_shared_cache) *(*uintptr)(unsafe.Add(p, 416)) = __ccgo_fp(Xsqlite3_errcode) *(*uintptr)(unsafe.Add(p, 424)) = __ccgo_fp(Xsqlite3_errmsg) *(*uintptr)(unsafe.Add(p, 432)) = __ccgo_fp(Xsqlite3_errmsg16) *(*uintptr)(unsafe.Add(p, 440)) = __ccgo_fp(Xsqlite3_exec) *(*uintptr)(unsafe.Add(p, 448)) = __ccgo_fp(Xsqlite3_expired) *(*uintptr)(unsafe.Add(p, 456)) = __ccgo_fp(Xsqlite3_finalize) *(*uintptr)(unsafe.Add(p, 464)) = __ccgo_fp(Xsqlite3_free) *(*uintptr)(unsafe.Add(p, 472)) = __ccgo_fp(Xsqlite3_free_table) *(*uintptr)(unsafe.Add(p, 480)) = __ccgo_fp(Xsqlite3_get_autocommit) *(*uintptr)(unsafe.Add(p, 488)) = __ccgo_fp(Xsqlite3_get_auxdata) *(*uintptr)(unsafe.Add(p, 496)) = __ccgo_fp(Xsqlite3_get_table) *(*uintptr)(unsafe.Add(p, 512)) = __ccgo_fp(Xsqlite3_interrupt) *(*uintptr)(unsafe.Add(p, 520)) = __ccgo_fp(Xsqlite3_last_insert_rowid) *(*uintptr)(unsafe.Add(p, 528)) = __ccgo_fp(Xsqlite3_libversion) *(*uintptr)(unsafe.Add(p, 536)) = __ccgo_fp(Xsqlite3_libversion_number) *(*uintptr)(unsafe.Add(p, 544)) = __ccgo_fp(Xsqlite3_malloc) *(*uintptr)(unsafe.Add(p, 552)) = __ccgo_fp(Xsqlite3_mprintf) *(*uintptr)(unsafe.Add(p, 560)) = __ccgo_fp(Xsqlite3_open) *(*uintptr)(unsafe.Add(p, 568)) = __ccgo_fp(Xsqlite3_open16) *(*uintptr)(unsafe.Add(p, 576)) = __ccgo_fp(Xsqlite3_prepare) *(*uintptr)(unsafe.Add(p, 584)) = __ccgo_fp(Xsqlite3_prepare16) *(*uintptr)(unsafe.Add(p, 592)) = __ccgo_fp(Xsqlite3_profile) *(*uintptr)(unsafe.Add(p, 600)) = __ccgo_fp(Xsqlite3_progress_handler) *(*uintptr)(unsafe.Add(p, 608)) = __ccgo_fp(Xsqlite3_realloc) *(*uintptr)(unsafe.Add(p, 616)) = __ccgo_fp(Xsqlite3_reset) *(*uintptr)(unsafe.Add(p, 624)) = __ccgo_fp(Xsqlite3_result_blob) *(*uintptr)(unsafe.Add(p, 632)) = __ccgo_fp(Xsqlite3_result_double) *(*uintptr)(unsafe.Add(p, 640)) = __ccgo_fp(Xsqlite3_result_error) *(*uintptr)(unsafe.Add(p, 648)) = __ccgo_fp(Xsqlite3_result_error16) *(*uintptr)(unsafe.Add(p, 656)) = __ccgo_fp(Xsqlite3_result_int) *(*uintptr)(unsafe.Add(p, 664)) = __ccgo_fp(Xsqlite3_result_int64) *(*uintptr)(unsafe.Add(p, 672)) = __ccgo_fp(Xsqlite3_result_null) *(*uintptr)(unsafe.Add(p, 680)) = __ccgo_fp(Xsqlite3_result_text) *(*uintptr)(unsafe.Add(p, 688)) = __ccgo_fp(Xsqlite3_result_text16) *(*uintptr)(unsafe.Add(p, 696)) = __ccgo_fp(Xsqlite3_result_text16be) *(*uintptr)(unsafe.Add(p, 704)) = __ccgo_fp(Xsqlite3_result_text16le) *(*uintptr)(unsafe.Add(p, 712)) = __ccgo_fp(Xsqlite3_result_value) *(*uintptr)(unsafe.Add(p, 720)) = __ccgo_fp(Xsqlite3_rollback_hook) *(*uintptr)(unsafe.Add(p, 728)) = __ccgo_fp(Xsqlite3_set_authorizer) *(*uintptr)(unsafe.Add(p, 736)) = __ccgo_fp(Xsqlite3_set_auxdata) *(*uintptr)(unsafe.Add(p, 744)) = __ccgo_fp(Xsqlite3_snprintf) *(*uintptr)(unsafe.Add(p, 752)) = __ccgo_fp(Xsqlite3_step) *(*uintptr)(unsafe.Add(p, 760)) = __ccgo_fp(Xsqlite3_table_column_metadata) *(*uintptr)(unsafe.Add(p, 768)) = __ccgo_fp(Xsqlite3_thread_cleanup) *(*uintptr)(unsafe.Add(p, 776)) = __ccgo_fp(Xsqlite3_total_changes) *(*uintptr)(unsafe.Add(p, 784)) = __ccgo_fp(Xsqlite3_trace) *(*uintptr)(unsafe.Add(p, 792)) = __ccgo_fp(Xsqlite3_transfer_bindings) *(*uintptr)(unsafe.Add(p, 800)) = __ccgo_fp(Xsqlite3_update_hook) *(*uintptr)(unsafe.Add(p, 808)) = __ccgo_fp(Xsqlite3_user_data) *(*uintptr)(unsafe.Add(p, 816)) = __ccgo_fp(Xsqlite3_value_blob) *(*uintptr)(unsafe.Add(p, 824)) = __ccgo_fp(Xsqlite3_value_bytes) *(*uintptr)(unsafe.Add(p, 832)) = __ccgo_fp(Xsqlite3_value_bytes16) *(*uintptr)(unsafe.Add(p, 840)) = __ccgo_fp(Xsqlite3_value_double) *(*uintptr)(unsafe.Add(p, 848)) = __ccgo_fp(Xsqlite3_value_int) *(*uintptr)(unsafe.Add(p, 856)) = __ccgo_fp(Xsqlite3_value_int64) *(*uintptr)(unsafe.Add(p, 864)) = __ccgo_fp(Xsqlite3_value_numeric_type) *(*uintptr)(unsafe.Add(p, 872)) = __ccgo_fp(Xsqlite3_value_text) *(*uintptr)(unsafe.Add(p, 880)) = __ccgo_fp(Xsqlite3_value_text16) *(*uintptr)(unsafe.Add(p, 888)) = __ccgo_fp(Xsqlite3_value_text16be) *(*uintptr)(unsafe.Add(p, 896)) = __ccgo_fp(Xsqlite3_value_text16le) *(*uintptr)(unsafe.Add(p, 904)) = __ccgo_fp(Xsqlite3_value_type) *(*uintptr)(unsafe.Add(p, 912)) = __ccgo_fp(Xsqlite3_vmprintf) *(*uintptr)(unsafe.Add(p, 920)) = __ccgo_fp(Xsqlite3_overload_function) *(*uintptr)(unsafe.Add(p, 928)) = __ccgo_fp(Xsqlite3_prepare_v2) *(*uintptr)(unsafe.Add(p, 936)) = __ccgo_fp(Xsqlite3_prepare16_v2) *(*uintptr)(unsafe.Add(p, 944)) = __ccgo_fp(Xsqlite3_clear_bindings) *(*uintptr)(unsafe.Add(p, 952)) = __ccgo_fp(Xsqlite3_create_module_v2) *(*uintptr)(unsafe.Add(p, 960)) = __ccgo_fp(Xsqlite3_bind_zeroblob) *(*uintptr)(unsafe.Add(p, 968)) = __ccgo_fp(Xsqlite3_blob_bytes) *(*uintptr)(unsafe.Add(p, 976)) = __ccgo_fp(Xsqlite3_blob_close) *(*uintptr)(unsafe.Add(p, 984)) = __ccgo_fp(Xsqlite3_blob_open) *(*uintptr)(unsafe.Add(p, 992)) = __ccgo_fp(Xsqlite3_blob_read) *(*uintptr)(unsafe.Add(p, 1000)) = __ccgo_fp(Xsqlite3_blob_write) *(*uintptr)(unsafe.Add(p, 1008)) = __ccgo_fp(Xsqlite3_create_collation_v2) *(*uintptr)(unsafe.Add(p, 1016)) = __ccgo_fp(Xsqlite3_file_control) *(*uintptr)(unsafe.Add(p, 1024)) = __ccgo_fp(Xsqlite3_memory_highwater) *(*uintptr)(unsafe.Add(p, 1032)) = __ccgo_fp(Xsqlite3_memory_used) *(*uintptr)(unsafe.Add(p, 1040)) = __ccgo_fp(Xsqlite3_mutex_alloc) *(*uintptr)(unsafe.Add(p, 1048)) = __ccgo_fp(Xsqlite3_mutex_enter) *(*uintptr)(unsafe.Add(p, 1056)) = __ccgo_fp(Xsqlite3_mutex_free) *(*uintptr)(unsafe.Add(p, 1064)) = __ccgo_fp(Xsqlite3_mutex_leave) *(*uintptr)(unsafe.Add(p, 1072)) = __ccgo_fp(Xsqlite3_mutex_try) *(*uintptr)(unsafe.Add(p, 1080)) = __ccgo_fp(Xsqlite3_open_v2) *(*uintptr)(unsafe.Add(p, 1088)) = __ccgo_fp(Xsqlite3_release_memory) *(*uintptr)(unsafe.Add(p, 1096)) = __ccgo_fp(Xsqlite3_result_error_nomem) *(*uintptr)(unsafe.Add(p, 1104)) = __ccgo_fp(Xsqlite3_result_error_toobig) *(*uintptr)(unsafe.Add(p, 1112)) = __ccgo_fp(Xsqlite3_sleep) *(*uintptr)(unsafe.Add(p, 1120)) = __ccgo_fp(Xsqlite3_soft_heap_limit) *(*uintptr)(unsafe.Add(p, 1128)) = __ccgo_fp(Xsqlite3_vfs_find) *(*uintptr)(unsafe.Add(p, 1136)) = __ccgo_fp(Xsqlite3_vfs_register) *(*uintptr)(unsafe.Add(p, 1144)) = __ccgo_fp(Xsqlite3_vfs_unregister) *(*uintptr)(unsafe.Add(p, 1152)) = __ccgo_fp(Xsqlite3_threadsafe) *(*uintptr)(unsafe.Add(p, 1160)) = __ccgo_fp(Xsqlite3_result_zeroblob) *(*uintptr)(unsafe.Add(p, 1168)) = __ccgo_fp(Xsqlite3_result_error_code) *(*uintptr)(unsafe.Add(p, 1176)) = __ccgo_fp(Xsqlite3_test_control) *(*uintptr)(unsafe.Add(p, 1184)) = __ccgo_fp(Xsqlite3_randomness) *(*uintptr)(unsafe.Add(p, 1192)) = __ccgo_fp(Xsqlite3_context_db_handle) *(*uintptr)(unsafe.Add(p, 1200)) = __ccgo_fp(Xsqlite3_extended_result_codes) *(*uintptr)(unsafe.Add(p, 1208)) = __ccgo_fp(Xsqlite3_limit) *(*uintptr)(unsafe.Add(p, 1216)) = __ccgo_fp(Xsqlite3_next_stmt) *(*uintptr)(unsafe.Add(p, 1224)) = __ccgo_fp(Xsqlite3_sql) *(*uintptr)(unsafe.Add(p, 1232)) = __ccgo_fp(Xsqlite3_status) *(*uintptr)(unsafe.Add(p, 1240)) = __ccgo_fp(Xsqlite3_backup_finish) *(*uintptr)(unsafe.Add(p, 1248)) = __ccgo_fp(Xsqlite3_backup_init) *(*uintptr)(unsafe.Add(p, 1256)) = __ccgo_fp(Xsqlite3_backup_pagecount) *(*uintptr)(unsafe.Add(p, 1264)) = __ccgo_fp(Xsqlite3_backup_remaining) *(*uintptr)(unsafe.Add(p, 1272)) = __ccgo_fp(Xsqlite3_backup_step) *(*uintptr)(unsafe.Add(p, 1280)) = __ccgo_fp(Xsqlite3_compileoption_get) *(*uintptr)(unsafe.Add(p, 1288)) = __ccgo_fp(Xsqlite3_compileoption_used) *(*uintptr)(unsafe.Add(p, 1296)) = __ccgo_fp(Xsqlite3_create_function_v2) *(*uintptr)(unsafe.Add(p, 1304)) = __ccgo_fp(Xsqlite3_db_config) *(*uintptr)(unsafe.Add(p, 1312)) = __ccgo_fp(Xsqlite3_db_mutex) *(*uintptr)(unsafe.Add(p, 1320)) = __ccgo_fp(Xsqlite3_db_status) *(*uintptr)(unsafe.Add(p, 1328)) = __ccgo_fp(Xsqlite3_extended_errcode) *(*uintptr)(unsafe.Add(p, 1336)) = __ccgo_fp(Xsqlite3_log) *(*uintptr)(unsafe.Add(p, 1344)) = __ccgo_fp(Xsqlite3_soft_heap_limit64) *(*uintptr)(unsafe.Add(p, 1352)) = __ccgo_fp(Xsqlite3_sourceid) *(*uintptr)(unsafe.Add(p, 1360)) = __ccgo_fp(Xsqlite3_stmt_status) *(*uintptr)(unsafe.Add(p, 1368)) = __ccgo_fp(Xsqlite3_strnicmp) *(*uintptr)(unsafe.Add(p, 1376)) = __ccgo_fp(Xsqlite3_unlock_notify) *(*uintptr)(unsafe.Add(p, 1384)) = __ccgo_fp(Xsqlite3_wal_autocheckpoint) *(*uintptr)(unsafe.Add(p, 1392)) = __ccgo_fp(Xsqlite3_wal_checkpoint) *(*uintptr)(unsafe.Add(p, 1400)) = __ccgo_fp(Xsqlite3_wal_hook) *(*uintptr)(unsafe.Add(p, 1408)) = __ccgo_fp(Xsqlite3_blob_reopen) *(*uintptr)(unsafe.Add(p, 1416)) = __ccgo_fp(Xsqlite3_vtab_config) *(*uintptr)(unsafe.Add(p, 1424)) = __ccgo_fp(Xsqlite3_vtab_on_conflict) *(*uintptr)(unsafe.Add(p, 1432)) = __ccgo_fp(Xsqlite3_close_v2) *(*uintptr)(unsafe.Add(p, 1440)) = __ccgo_fp(Xsqlite3_db_filename) *(*uintptr)(unsafe.Add(p, 1448)) = __ccgo_fp(Xsqlite3_db_readonly) *(*uintptr)(unsafe.Add(p, 1456)) = __ccgo_fp(Xsqlite3_db_release_memory) *(*uintptr)(unsafe.Add(p, 1464)) = __ccgo_fp(Xsqlite3_errstr) *(*uintptr)(unsafe.Add(p, 1472)) = __ccgo_fp(Xsqlite3_stmt_busy) *(*uintptr)(unsafe.Add(p, 1480)) = __ccgo_fp(Xsqlite3_stmt_readonly) *(*uintptr)(unsafe.Add(p, 1488)) = __ccgo_fp(Xsqlite3_stricmp) *(*uintptr)(unsafe.Add(p, 1496)) = __ccgo_fp(Xsqlite3_uri_boolean) *(*uintptr)(unsafe.Add(p, 1504)) = __ccgo_fp(Xsqlite3_uri_int64) *(*uintptr)(unsafe.Add(p, 1512)) = __ccgo_fp(Xsqlite3_uri_parameter) *(*uintptr)(unsafe.Add(p, 1520)) = __ccgo_fp(Xsqlite3_vsnprintf) *(*uintptr)(unsafe.Add(p, 1528)) = __ccgo_fp(Xsqlite3_wal_checkpoint_v2) *(*uintptr)(unsafe.Add(p, 1536)) = __ccgo_fp(Xsqlite3_auto_extension) *(*uintptr)(unsafe.Add(p, 1544)) = __ccgo_fp(Xsqlite3_bind_blob64) *(*uintptr)(unsafe.Add(p, 1552)) = __ccgo_fp(Xsqlite3_bind_text64) *(*uintptr)(unsafe.Add(p, 1560)) = __ccgo_fp(Xsqlite3_cancel_auto_extension) *(*uintptr)(unsafe.Add(p, 1568)) = __ccgo_fp(Xsqlite3_load_extension) *(*uintptr)(unsafe.Add(p, 1576)) = __ccgo_fp(Xsqlite3_malloc64) *(*uintptr)(unsafe.Add(p, 1584)) = __ccgo_fp(Xsqlite3_msize) *(*uintptr)(unsafe.Add(p, 1592)) = __ccgo_fp(Xsqlite3_realloc64) *(*uintptr)(unsafe.Add(p, 1600)) = __ccgo_fp(Xsqlite3_reset_auto_extension) *(*uintptr)(unsafe.Add(p, 1608)) = __ccgo_fp(Xsqlite3_result_blob64) *(*uintptr)(unsafe.Add(p, 1616)) = __ccgo_fp(Xsqlite3_result_text64) *(*uintptr)(unsafe.Add(p, 1624)) = __ccgo_fp(Xsqlite3_strglob) *(*uintptr)(unsafe.Add(p, 1632)) = __ccgo_fp(Xsqlite3_value_dup) *(*uintptr)(unsafe.Add(p, 1640)) = __ccgo_fp(Xsqlite3_value_free) *(*uintptr)(unsafe.Add(p, 1648)) = __ccgo_fp(Xsqlite3_result_zeroblob64) *(*uintptr)(unsafe.Add(p, 1656)) = __ccgo_fp(Xsqlite3_bind_zeroblob64) *(*uintptr)(unsafe.Add(p, 1664)) = __ccgo_fp(Xsqlite3_value_subtype) *(*uintptr)(unsafe.Add(p, 1672)) = __ccgo_fp(Xsqlite3_result_subtype) *(*uintptr)(unsafe.Add(p, 1680)) = __ccgo_fp(Xsqlite3_status64) *(*uintptr)(unsafe.Add(p, 1688)) = __ccgo_fp(Xsqlite3_strlike) *(*uintptr)(unsafe.Add(p, 1696)) = __ccgo_fp(Xsqlite3_db_cacheflush) *(*uintptr)(unsafe.Add(p, 1704)) = __ccgo_fp(Xsqlite3_system_errno) *(*uintptr)(unsafe.Add(p, 1712)) = __ccgo_fp(Xsqlite3_trace_v2) *(*uintptr)(unsafe.Add(p, 1720)) = __ccgo_fp(Xsqlite3_expanded_sql) *(*uintptr)(unsafe.Add(p, 1728)) = __ccgo_fp(Xsqlite3_set_last_insert_rowid) *(*uintptr)(unsafe.Add(p, 1736)) = __ccgo_fp(Xsqlite3_prepare_v3) *(*uintptr)(unsafe.Add(p, 1744)) = __ccgo_fp(Xsqlite3_prepare16_v3) *(*uintptr)(unsafe.Add(p, 1752)) = __ccgo_fp(Xsqlite3_bind_pointer) *(*uintptr)(unsafe.Add(p, 1760)) = __ccgo_fp(Xsqlite3_result_pointer) *(*uintptr)(unsafe.Add(p, 1768)) = __ccgo_fp(Xsqlite3_value_pointer) *(*uintptr)(unsafe.Add(p, 1776)) = __ccgo_fp(Xsqlite3_vtab_nochange) *(*uintptr)(unsafe.Add(p, 1784)) = __ccgo_fp(Xsqlite3_value_nochange) *(*uintptr)(unsafe.Add(p, 1792)) = __ccgo_fp(Xsqlite3_vtab_collation) *(*uintptr)(unsafe.Add(p, 1800)) = __ccgo_fp(Xsqlite3_keyword_count) *(*uintptr)(unsafe.Add(p, 1808)) = __ccgo_fp(Xsqlite3_keyword_name) *(*uintptr)(unsafe.Add(p, 1816)) = __ccgo_fp(Xsqlite3_keyword_check) *(*uintptr)(unsafe.Add(p, 1824)) = __ccgo_fp(Xsqlite3_str_new) *(*uintptr)(unsafe.Add(p, 1832)) = __ccgo_fp(Xsqlite3_str_finish) *(*uintptr)(unsafe.Add(p, 1840)) = __ccgo_fp(Xsqlite3_str_appendf) *(*uintptr)(unsafe.Add(p, 1848)) = __ccgo_fp(Xsqlite3_str_vappendf) *(*uintptr)(unsafe.Add(p, 1856)) = __ccgo_fp(Xsqlite3_str_append) *(*uintptr)(unsafe.Add(p, 1864)) = __ccgo_fp(Xsqlite3_str_appendall) *(*uintptr)(unsafe.Add(p, 1872)) = __ccgo_fp(Xsqlite3_str_appendchar) *(*uintptr)(unsafe.Add(p, 1880)) = __ccgo_fp(Xsqlite3_str_reset) *(*uintptr)(unsafe.Add(p, 1888)) = __ccgo_fp(Xsqlite3_str_errcode) *(*uintptr)(unsafe.Add(p, 1896)) = __ccgo_fp(Xsqlite3_str_length) *(*uintptr)(unsafe.Add(p, 1904)) = __ccgo_fp(Xsqlite3_str_value) *(*uintptr)(unsafe.Add(p, 1912)) = __ccgo_fp(Xsqlite3_create_window_function) *(*uintptr)(unsafe.Add(p, 1928)) = __ccgo_fp(Xsqlite3_stmt_isexplain) *(*uintptr)(unsafe.Add(p, 1936)) = __ccgo_fp(Xsqlite3_value_frombind) *(*uintptr)(unsafe.Add(p, 1944)) = __ccgo_fp(Xsqlite3_drop_modules) *(*uintptr)(unsafe.Add(p, 1952)) = __ccgo_fp(Xsqlite3_hard_heap_limit64) *(*uintptr)(unsafe.Add(p, 1960)) = __ccgo_fp(Xsqlite3_uri_key) *(*uintptr)(unsafe.Add(p, 1968)) = __ccgo_fp(Xsqlite3_filename_database) *(*uintptr)(unsafe.Add(p, 1976)) = __ccgo_fp(Xsqlite3_filename_journal) *(*uintptr)(unsafe.Add(p, 1984)) = __ccgo_fp(Xsqlite3_filename_wal) *(*uintptr)(unsafe.Add(p, 1992)) = __ccgo_fp(Xsqlite3_create_filename) *(*uintptr)(unsafe.Add(p, 2000)) = __ccgo_fp(Xsqlite3_free_filename) *(*uintptr)(unsafe.Add(p, 2008)) = __ccgo_fp(Xsqlite3_database_file_object) *(*uintptr)(unsafe.Add(p, 2016)) = __ccgo_fp(Xsqlite3_txn_state) *(*uintptr)(unsafe.Add(p, 2024)) = __ccgo_fp(Xsqlite3_changes64) *(*uintptr)(unsafe.Add(p, 2032)) = __ccgo_fp(Xsqlite3_total_changes64) *(*uintptr)(unsafe.Add(p, 2040)) = __ccgo_fp(Xsqlite3_autovacuum_pages) *(*uintptr)(unsafe.Add(p, 2048)) = __ccgo_fp(Xsqlite3_error_offset) *(*uintptr)(unsafe.Add(p, 2056)) = __ccgo_fp(Xsqlite3_vtab_rhs_value) *(*uintptr)(unsafe.Add(p, 2064)) = __ccgo_fp(Xsqlite3_vtab_distinct) *(*uintptr)(unsafe.Add(p, 2072)) = __ccgo_fp(Xsqlite3_vtab_in) *(*uintptr)(unsafe.Add(p, 2080)) = __ccgo_fp(Xsqlite3_vtab_in_first) *(*uintptr)(unsafe.Add(p, 2088)) = __ccgo_fp(Xsqlite3_vtab_in_next) *(*uintptr)(unsafe.Add(p, 2096)) = __ccgo_fp(Xsqlite3_deserialize) *(*uintptr)(unsafe.Add(p, 2104)) = __ccgo_fp(Xsqlite3_serialize) *(*uintptr)(unsafe.Add(p, 2112)) = __ccgo_fp(Xsqlite3_db_name) *(*uintptr)(unsafe.Add(p, 2120)) = __ccgo_fp(Xsqlite3_value_encoding) *(*uintptr)(unsafe.Add(p, 2128)) = __ccgo_fp(Xsqlite3_is_interrupted) *(*uintptr)(unsafe.Add(p, 2136)) = __ccgo_fp(Xsqlite3_stmt_explain) *(*uintptr)(unsafe.Add(p, 2144)) = __ccgo_fp(Xsqlite3_get_clientdata) *(*uintptr)(unsafe.Add(p, 2152)) = __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 = 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+12893, 0) } return int32(SQLITE_ERROR) } if zProc != 0 { v1 = zProc } else { v1 = __ccgo_ts + 16756 } 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.Uint64FromInt64(8)/libc.Uint64FromInt64(8)) && handle == uintptr(0)) { break } zAltFile = Xsqlite3_mprintf(tls, __ccgo_ts+12854, libc.VaList(bp+16, zFile, _azEndings[ii])) if zAltFile == uintptr(0) { return int32(SQLITE_NOMEM) } if nMsg+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.X__builtin___memcpy_chk(tls, zAltEntry, __ccgo_ts+16779, uint64(8), libc.X__builtin_object_size(tls, zAltEntry, 0)) iFile = ncFile - int32(1) for { if !(iFile >= 0 && !(int32(*(*int8)(unsafe.Pointer(zFile + uintptr(iFile)))) == libc.Int32FromUint8('/'))) { break } goto _3 _3: ; iFile-- } iFile++ if Xsqlite3_strnicmp(tls, zFile+uintptr(iFile), __ccgo_ts+16788, int32(3)) == 0 { iFile += int32(3) } iEntry = int32(8) for { v5 = int32(*(*int8)(unsafe.Pointer(zFile + uintptr(iFile)))) c = v5 if !(v5 != 0 && c != int32('.')) { break } if int32(_sqlite3CtypeMap[uint8(c)])&int32(0x02) != 0 { v6 = iEntry iEntry++ *(*int8)(unsafe.Pointer(zAltEntry + uintptr(v6))) = int8(_sqlite3UpperToLower[uint32(c)]) } goto _4 _4: ; iFile++ } libc.X__builtin___memcpy_chk(tls, zAltEntry+uintptr(iEntry), __ccgo_ts+16792, uint64(6), libc.X__builtin_object_size(tls, zAltEntry+uintptr(iEntry), 0)) zEntry = zAltEntry xInit = _sqlite3OsDlSym(tls, pVfs, handle, zEntry) } if xInit == uintptr(0) { if pzErrMsg != 0 { nMsg += libc.Xstrlen(tls, zEntry) + uint64(300) v7 = Xsqlite3_malloc64(tls, nMsg) *(*uintptr)(unsafe.Pointer(bp)) = v7 *(*uintptr)(unsafe.Pointer(pzErrMsg)) = v7 if *(*uintptr)(unsafe.Pointer(bp)) != 0 { /* zErrmsg would be NULL if not so */ Xsqlite3_snprintf(tls, int32(nMsg), *(*uintptr)(unsafe.Pointer(bp)), __ccgo_ts+16798, 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(8)*uint64((*Tsqlite3)(unsafe.Pointer(db)).FnExtension+libc.Int32FromInt32(1))) if aHandle == uintptr(0) { return int32(SQLITE_NOMEM) } if (*Tsqlite3)(unsafe.Pointer(db)).FnExtension > 0 { libc.X__builtin___memcpy_chk(tls, aHandle, (*Tsqlite3)(unsafe.Pointer(db)).FaExtension, uint64(8)*uint64((*Tsqlite3)(unsafe.Pointer(db)).FnExtension), libc.X__builtin_object_size(tls, aHandle, 0)) } _sqlite3DbFree(tls, db, (*Tsqlite3)(unsafe.Pointer(db)).FaExtension) (*Tsqlite3)(unsafe.Pointer(db)).FaExtension = aHandle v9 = db + 236 v8 = *(*int32)(unsafe.Pointer(v9)) *(*int32)(unsafe.Pointer(v9))++ *(*uintptr)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaExtension + uintptr(v8)*8)) = 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 { /* zErrmsg would be NULL if not so */ Xsqlite3_snprintf(tls, int32(nMsg), *(*uintptr)(unsafe.Pointer(bp)), __ccgo_ts+16873, 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 + 16750, } 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 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)*8))) 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 + 48)) |= uint64(libc.Int32FromInt32(SQLITE_LoadExtension) | libc.Int32FromInt32(SQLITE_LoadExtFunc)) } else { *(*Tu64)(unsafe.Pointer(db + 48)) &= ^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)*8)) == xInit { break } goto _1 _1: ; i++ } if i == _sqlite3Autoext.FnExt { nByte = uint64(_sqlite3Autoext.FnExt+libc.Uint32FromInt32(1)) * uint64(8) 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)*8)) = xInit _sqlite3Autoext.FnExt++ } } Xsqlite3_mutex_leave(tls, mutex) 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)*8)) == xInit { _sqlite3Autoext.FnExt-- *(*uintptr)(unsafe.Pointer(_sqlite3Autoext.FaExt + uintptr(i)*8)) = *(*uintptr)(unsafe.Pointer(_sqlite3Autoext.FaExt + uintptr(_sqlite3Autoext.FnExt)*8)) 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)*8)) } 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+16910, 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 + 5893, 1: __ccgo_ts + 16949, 2: __ccgo_ts + 9531, 3: __ccgo_ts + 16953, 4: __ccgo_ts + 16958, 5: __ccgo_ts + 16961, 6: __ccgo_ts + 16971, 7: __ccgo_ts + 16981, 8: __ccgo_ts + 16987, 9: __ccgo_ts + 16991, 10: __ccgo_ts + 16996, 11: __ccgo_ts + 17001, 12: __ccgo_ts + 17009, 13: __ccgo_ts + 17020, 14: __ccgo_ts + 17023, 15: __ccgo_ts + 17030, 16: __ccgo_ts + 16991, 17: __ccgo_ts + 16996, 18: __ccgo_ts + 17037, 19: __ccgo_ts + 17042, 20: __ccgo_ts + 17045, 21: __ccgo_ts + 17052, 22: __ccgo_ts + 16987, 23: __ccgo_ts + 16991, 24: __ccgo_ts + 17058, 25: __ccgo_ts + 17063, 26: __ccgo_ts + 17068, 27: __ccgo_ts + 16991, 28: __ccgo_ts + 17072, 29: __ccgo_ts + 16996, 30: __ccgo_ts + 17080, 31: __ccgo_ts + 17084, 32: __ccgo_ts + 17089, 33: __ccgo_ts + 12270, 34: __ccgo_ts + 12266, 35: __ccgo_ts + 17095, 36: __ccgo_ts + 17100, 37: __ccgo_ts + 17105, 38: __ccgo_ts + 16949, 39: __ccgo_ts + 16991, 40: __ccgo_ts + 17110, 41: __ccgo_ts + 17117, 42: __ccgo_ts + 17124, 43: __ccgo_ts + 9531, 44: __ccgo_ts + 17132, 45: __ccgo_ts + 5896, 46: __ccgo_ts + 17138, 47: __ccgo_ts + 16949, 48: __ccgo_ts + 16991, 49: __ccgo_ts + 17143, 50: __ccgo_ts + 17148, 51: __ccgo_ts + 16334, 52: __ccgo_ts + 17153, 53: __ccgo_ts + 17166, 54: __ccgo_ts + 17175, 55: __ccgo_ts + 17182, 56: __ccgo_ts + 17193, } // C documentation // // /* Definitions of all built-in pragmas */ type TPragmaName = struct { FzName uintptr FePragTyp Tu8 FmPragFlg Tu8 FiPragCName Tu8 FnPragCName Tu8 FiArg Tu64 } type PragmaName = TPragmaName var _aPragmaName = [67]TPragmaName{ 0: { FzName: __ccgo_ts + 17201, FePragTyp: uint8(PragTyp_ANALYSIS_LIMIT), FmPragFlg: uint8(PragFlg_Result0), }, 1: { FzName: __ccgo_ts + 17216, FePragTyp: uint8(PragTyp_HEADER_VALUE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NoColumns1) | libc.Int32FromInt32(PragFlg_Result0)), FiArg: uint64(BTREE_APPLICATION_ID), }, 2: { FzName: __ccgo_ts + 17231, 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 + 17243, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_AutoIndex), }, 4: { FzName: __ccgo_ts + 17259, FePragTyp: uint8(PragTyp_BUSY_TIMEOUT), FmPragFlg: uint8(PragFlg_Result0), FiPragCName: uint8(56), FnPragCName: uint8(1), }, 5: { FzName: __ccgo_ts + 17182, 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 + 17272, FePragTyp: uint8(PragTyp_CACHE_SPILL), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq) | libc.Int32FromInt32(PragFlg_NoColumns1)), }, 7: { FzName: __ccgo_ts + 17284, FePragTyp: uint8(PragTyp_CASE_SENSITIVE_LIKE), FmPragFlg: uint8(PragFlg_NoColumns), }, 8: { FzName: __ccgo_ts + 17304, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_CellSizeCk), }, 9: { FzName: __ccgo_ts + 17320, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_CkptFullFSync), }, 10: { FzName: __ccgo_ts + 17341, FePragTyp: uint8(PragTyp_COLLATION_LIST), FmPragFlg: uint8(PragFlg_Result0), FiPragCName: uint8(38), FnPragCName: uint8(2), }, 11: { FzName: __ccgo_ts + 17356, FePragTyp: uint8(PragTyp_COMPILE_OPTIONS), FmPragFlg: uint8(PragFlg_Result0), }, 12: { FzName: __ccgo_ts + 17372, 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 + 17386, FePragTyp: uint8(PragTyp_HEADER_VALUE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_ReadOnly) | libc.Int32FromInt32(PragFlg_Result0)), FiArg: uint64(BTREE_DATA_VERSION), }, 14: { FzName: __ccgo_ts + 17399, FePragTyp: uint8(PragTyp_DATABASE_LIST), FmPragFlg: uint8(PragFlg_Result0), FiPragCName: uint8(47), FnPragCName: uint8(3), }, 15: { FzName: __ccgo_ts + 17413, 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 + 17432, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_DeferFKs), }, 17: { FzName: __ccgo_ts + 17451, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_NullCallback), }, 18: { FzName: __ccgo_ts + 17474, FePragTyp: uint8(PragTyp_ENCODING), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), }, 19: { FzName: __ccgo_ts + 17483, 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 + 17501, 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 + 17518, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_ForeignKeys), }, 22: { FzName: __ccgo_ts + 17531, FePragTyp: uint8(PragTyp_HEADER_VALUE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_ReadOnly) | libc.Int32FromInt32(PragFlg_Result0)), }, 23: { FzName: __ccgo_ts + 17546, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_FullColNames), }, 24: { FzName: __ccgo_ts + 17564, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_FullFSync), }, 25: { FzName: __ccgo_ts + 17574, FePragTyp: uint8(PragTyp_FUNCTION_LIST), FmPragFlg: uint8(PragFlg_Result0), FiPragCName: uint8(27), FnPragCName: uint8(6), }, 26: { FzName: __ccgo_ts + 17588, FePragTyp: uint8(PragTyp_HARD_HEAP_LIMIT), FmPragFlg: uint8(PragFlg_Result0), }, 27: { FzName: __ccgo_ts + 17604, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_IgnoreChecks), }, 28: { FzName: __ccgo_ts + 17629, FePragTyp: uint8(PragTyp_INCREMENTAL_VACUUM), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_NoColumns)), }, 29: { FzName: __ccgo_ts + 17648, 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 + 17659, 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 + 17670, 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 + 17682, 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 + 17698, FePragTyp: uint8(PragTyp_JOURNAL_MODE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq)), }, 34: { FzName: __ccgo_ts + 17711, FePragTyp: uint8(PragTyp_JOURNAL_SIZE_LIMIT), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq)), }, 35: { FzName: __ccgo_ts + 17730, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_LegacyAlter), }, 36: { FzName: __ccgo_ts + 17749, FePragTyp: uint8(PragTyp_LOCK_PROXY_FILE), FmPragFlg: uint8(PragFlg_NoColumns1), }, 37: { FzName: __ccgo_ts + 17765, FePragTyp: uint8(PragTyp_LOCKING_MODE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq)), }, 38: { FzName: __ccgo_ts + 17778, FePragTyp: uint8(PragTyp_PAGE_COUNT), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq)), }, 39: { FzName: __ccgo_ts + 17793, FePragTyp: uint8(PragTyp_MMAP_SIZE), }, 40: { FzName: __ccgo_ts + 17803, FePragTyp: uint8(PragTyp_MODULE_LIST), FmPragFlg: uint8(PragFlg_Result0), FiPragCName: uint8(9), FnPragCName: uint8(1), }, 41: { FzName: __ccgo_ts + 17815, FePragTyp: uint8(PragTyp_OPTIMIZE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result1) | libc.Int32FromInt32(PragFlg_NeedSchema)), }, 42: { FzName: __ccgo_ts + 17824, FePragTyp: uint8(PragTyp_PAGE_COUNT), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq)), }, 43: { FzName: __ccgo_ts + 17835, FePragTyp: uint8(PragTyp_PAGE_SIZE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq) | libc.Int32FromInt32(PragFlg_NoColumns1)), }, 44: { FzName: __ccgo_ts + 17845, FePragTyp: uint8(PragTyp_PRAGMA_LIST), FmPragFlg: uint8(PragFlg_Result0), FiPragCName: uint8(9), FnPragCName: uint8(1), }, 45: { FzName: __ccgo_ts + 17857, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_QueryOnly), }, 46: { FzName: __ccgo_ts + 17868, FePragTyp: uint8(PragTyp_INTEGRITY_CHECK), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_Result1) | libc.Int32FromInt32(PragFlg_SchemaOpt)), }, 47: { FzName: __ccgo_ts + 17880, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(libc.Int32FromInt32(0x00004)) << libc.Int32FromInt32(32), }, 48: { FzName: __ccgo_ts + 17897, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_RecTriggers), }, 49: { FzName: __ccgo_ts + 17916, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_ReverseOrder), }, 50: { FzName: __ccgo_ts + 17942, FePragTyp: uint8(PragTyp_HEADER_VALUE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NoColumns1) | libc.Int32FromInt32(PragFlg_Result0)), FiArg: uint64(BTREE_SCHEMA_VERSION), }, 51: { FzName: __ccgo_ts + 17957, FePragTyp: uint8(PragTyp_SECURE_DELETE), FmPragFlg: uint8(PragFlg_Result0), }, 52: { FzName: __ccgo_ts + 17971, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_ShortColNames), }, 53: { FzName: __ccgo_ts + 17990, FePragTyp: uint8(PragTyp_SHRINK_MEMORY), FmPragFlg: uint8(PragFlg_NoColumns), }, 54: { FzName: __ccgo_ts + 18004, FePragTyp: uint8(PragTyp_SOFT_HEAP_LIMIT), FmPragFlg: uint8(PragFlg_Result0), }, 55: { FzName: __ccgo_ts + 18020, FePragTyp: uint8(PragTyp_SYNCHRONOUS), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_SchemaReq) | libc.Int32FromInt32(PragFlg_NoColumns1)), }, 56: { FzName: __ccgo_ts + 18032, 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), }, 57: { FzName: __ccgo_ts + 18043, FePragTyp: uint8(PragTyp_TABLE_LIST), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NeedSchema) | libc.Int32FromInt32(PragFlg_Result1)), FiPragCName: uint8(15), FnPragCName: uint8(6), }, 58: { FzName: __ccgo_ts + 18054, 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), }, 59: { FzName: __ccgo_ts + 18066, FePragTyp: uint8(PragTyp_TEMP_STORE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), }, 60: { FzName: __ccgo_ts + 18077, FePragTyp: uint8(PragTyp_TEMP_STORE_DIRECTORY), FmPragFlg: uint8(PragFlg_NoColumns1), }, 61: { FzName: __ccgo_ts + 18098, FePragTyp: uint8(PragTyp_THREADS), FmPragFlg: uint8(PragFlg_Result0), }, 62: { FzName: __ccgo_ts + 18106, FePragTyp: uint8(PragTyp_FLAG), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_Result0) | libc.Int32FromInt32(PragFlg_NoColumns1)), FiArg: uint64(SQLITE_TrustedSchema), }, 63: { FzName: __ccgo_ts + 18121, FePragTyp: uint8(PragTyp_HEADER_VALUE), FmPragFlg: uint8(libc.Int32FromInt32(PragFlg_NoColumns1) | libc.Int32FromInt32(PragFlg_Result0)), FiArg: uint64(BTREE_USER_VERSION), }, 64: { FzName: __ccgo_ts + 18134, FePragTyp: uint8(PragTyp_WAL_AUTOCHECKPOINT), }, 65: { FzName: __ccgo_ts + 18153, FePragTyp: uint8(PragTyp_WAL_CHECKPOINT), FmPragFlg: uint8(PragFlg_NeedSchema), FiPragCName: uint8(50), FnPragCName: uint8(3), }, 66: { FzName: __ccgo_ts + 18168, 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 *********************/ /* ** When the 0x10 bit of PRAGMA optimize is set, any ANALYZE commands ** will be run with an analysis_limit set to the lessor of the value of ** the following macro or to the actual analysis_limit if it is non-zero, ** in order to prevent PRAGMA optimize from running for too long. ** ** The value of 2000 is chosen emperically so that the worst-case run-time ** for PRAGMA optimize does not exceed 100 milliseconds against a variety ** of test databases on a RaspberryPI-4 compiled using -Os and without ** -DSQLITE_DEBUG. Of course, your mileage may vary. For the purpose of ** this paragraph, "worst-case" means that ANALYZE ends up being ** run on every table in the database. The worst case typically only ** happens if PRAGMA optimize is run on a database file for which ANALYZE ** has not been previously run and the 0x10000 flag is included so that ** all tables are analyzed. The usual case for PRAGMA optimize is that ** no ANALYZE commands will be run at all, or if any ANALYZE happens it ** will be against a single table, so that expected timing for PRAGMA ** optimize on a PI-4 is more like 1 millisecond or less with the 0x10000 ** flag or less than 100 microseconds without the 0x10000 flag. ** ** An analysis limit of 2000 is almost always sufficient for the query ** planner to fully characterize an index. The additional accuracy from ** a larger analysis is not usually helpful. */ // 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(*(*int8)(unsafe.Pointer(z)))])&int32(0x04) != 0 { return uint8(_sqlite3Atoi(tls, z)) } n = _sqlite3Strlen30(tls, z) i = 0 for { if !(i < int32(libc.Uint64FromInt64(8)/libc.Uint64FromInt64(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]int8{'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+18184) { return int32(PAGER_LOCKINGMODE_EXCLUSIVE) } if 0 == _sqlite3StrICmp(tls, z, __ccgo_ts+18194) { 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+8691) { return BTREE_AUTOVACUUM_NONE } if 0 == _sqlite3StrICmp(tls, z, __ccgo_ts+18201) { return int32(BTREE_AUTOVACUUM_FULL) } if 0 == _sqlite3StrICmp(tls, z, __ccgo_ts+18206) { 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(*(*int8)(unsafe.Pointer(z))) >= int32('0') && int32(*(*int8)(unsafe.Pointer(z))) <= int32('2') { return int32(*(*int8)(unsafe.Pointer(z))) - int32('0') } else { if _sqlite3StrICmp(tls, z, __ccgo_ts+17143) == 0 { return int32(1) } else { if _sqlite3StrICmp(tls, z, __ccgo_ts+18218) == 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*32))).FpBt != uintptr(0) { if !((*Tsqlite3)(unsafe.Pointer(db)).FautoCommit != 0) || _sqlite3BtreeTxnState(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).FpBt) != SQLITE_TXN_NONE { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18225, 0) return int32(SQLITE_ERROR) } _sqlite3BtreeClose(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).FpBt) (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).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 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 += 32 } } } // 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 + 18287 case int32(OE_SetDflt): zName = __ccgo_ts + 18296 case int32(OE_Cascade): zName = __ccgo_ts + 18308 case int32(OE_Restrict): zName = __ccgo_ts + 18316 default: zName = __ccgo_ts + 18325 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) { if eMode == int32(libc.Uint64FromInt64(48)/libc.Uint64FromInt64(8)) { return uintptr(0) } return _azModeName[eMode] } var _azModeName = [6]uintptr{ 0: __ccgo_ts + 18335, 1: __ccgo_ts + 18342, 2: __ccgo_ts + 18350, 3: __ccgo_ts + 18354, 4: __ccgo_ts + 18218, 5: __ccgo_ts + 18363, } // 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.Uint64FromInt64(1608)/libc.Uint64FromInt64(24)) - 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)*24 } 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 } 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 + 18388 } else { if (*TFuncDef)(unsafe.Pointer(p)).FxFinalize != uintptr(0) { zType = __ccgo_ts + 18390 } else { zType = __ccgo_ts + 8090 } } _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+18392, 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 + 18367, 2: __ccgo_ts + 18372, 3: __ccgo_ts + 18380, } // 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(256) defer tls.Free(256) 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, iAddr1, iBt, iCol, iCol1, iCookie, iDb, iDbLast, iEnd, iIdxDb, iLevel, iReg, iTab, 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, n, nBtree, nCheck, nHidden, nIdx, nIndex, nLimit, p11, p3, p4, r1, r11, r2, rc, regResult, regRow, res1, showInternFunc, size, size1, size2, uniqOk, x1, v10, v15, v18, v2, v26, v33, v38, v4, v43, v45, v47, v48, v50, v64, v66, v67, v68, v69, v72, v75, v76 int32 var aOp, aOp1, aOp2, aOp3, aOp4, aOp5, aRoot, db, j, j1, k1, k2, k4, p, p1, pBt, pBt1, pBt2, pCheck, pCol, pCol1, pColExpr, pColl, pDb, pEnc, pFK, pFK1, pFile, pFile1, pHash, pIdx, pIdx1, pIdx3, pIdx4, pIdx5, pIdx6, pIdx7, pMod, pObjTab, pPager, pPager1, pPager2, pPager3, pParent, pPk, pPk1, pPragma, pPrior, pSchema, pTab, pTab1, pTab10, pTab11, pTab12, 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, v65, v70, v8 uintptr var azOrigin [3]uintptr var cnum Ti16 var enc Tu8 var iPrior Tsqlite3_int64 var iRange, szThreshold TLogEst var mask Tu64 var opMask Tu32 var _ /* N at bp+144 */ Tsqlite3_int64 var _ /* N at bp+152 */ Tsqlite3_int64 var _ /* N at bp+160 */ Tsqlite3_int64 var _ /* N at bp+168 */ Tsqlite3_int64 var _ /* aFcntl at bp+8 */ [4]uintptr var _ /* aiCols at bp+104 */ uintptr var _ /* iDataCur at bp+116 */ int32 var _ /* iIdxCur at bp+120 */ int32 var _ /* iLimit at bp+48 */ Ti64 var _ /* iLimit at bp+56 */ int32 var _ /* jmp3 at bp+136 */ int32 var _ /* mxErr at bp+112 */ int32 var _ /* pDfltValue at bp+128 */ uintptr var _ /* pDummy at bp+88 */ uintptr var _ /* pId at bp+0 */ uintptr var _ /* pIdx at bp+96 */ uintptr var _ /* proxy_file_path at bp+80 */ uintptr var _ /* res at bp+72 */ int32 var _ /* size at bp+60 */ int32 var _ /* sz at bp+64 */ Tsqlite3_int64 var _ /* x at bp+40 */ Ti64 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = a1, a11, aOp, aOp1, aOp2, aOp3, aOp4, aOp5, 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, iAddr1, iBt, iCol, iCol1, iCookie, iDb, iDbLast, iEnd, iIdxDb, iLevel, iPrior, iRange, iReg, iTab, 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, n, nBtree, nCheck, nHidden, nIdx, nIndex, nLimit, opMask, p, p1, p11, p3, p4, pBt, pBt1, pBt2, pCheck, pCol, pCol1, pColExpr, pColl, pDb, pEnc, pFK, pFK1, pFile, pFile1, pHash, pIdx, pIdx1, pIdx3, pIdx4, pIdx5, pIdx6, pIdx7, pMod, pObjTab, pPager, pPager1, pPager2, pPager3, pParent, pPk, pPk1, pPragma, pPrior, pSchema, pTab, pTab1, pTab10, pTab11, pTab12, pTab2, pTab3, pTab4, pTab5, pTab6, pTab7, pTab8, pTab9, pTbls, pVTab, r1, r11, r2, rc, regResult, regRow, res1, 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, v48, v5, v50, v64, v65, v66, v67, v68, v69, v70, v72, v75, v76, 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)*32 /* 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+18399, libc.VaList(bp+184, pValue)) } else { zRight = _sqlite3NameFromToken(tls, db, pValue) } 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 + 8)))[0] = uintptr(0) (*(*[4]uintptr)(unsafe.Pointer(bp + 8)))[int32(1)] = zLeft (*(*[4]uintptr)(unsafe.Pointer(bp + 8)))[int32(2)] = zRight (*(*[4]uintptr)(unsafe.Pointer(bp + 8)))[int32(3)] = uintptr(0) (*Tsqlite3)(unsafe.Pointer(db)).FbusyHandler.FnBusy = 0 rc = Xsqlite3_file_control(tls, db, zDb, int32(SQLITE_FCNTL_PRAGMA), bp+8) if rc == SQLITE_OK { _sqlite3VdbeSetNumCols(tls, v, int32(1)) _sqlite3VdbeSetColName(tls, v, 0, COLNAME_NAME, (*(*[4]uintptr)(unsafe.Pointer(bp + 8)))[0], uintptr(-libc.Int32FromInt32(1))) _returnSingleText(tls, v, (*(*[4]uintptr)(unsafe.Pointer(bp + 8)))[0]) Xsqlite3_free(tls, (*(*[4]uintptr)(unsafe.Pointer(bp + 8)))[0]) goto pragma_out } if rc != int32(SQLITE_NOTFOUND) { if (*(*[4]uintptr)(unsafe.Pointer(bp + 8)))[0] != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+3863, libc.VaList(bp+184, (*(*[4]uintptr)(unsafe.Pointer(bp + 8)))[0])) Xsqlite3_free(tls, (*(*[4]uintptr)(unsafe.Pointer(bp + 8)))[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 + 56)) += int32(2) aOp = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint64FromInt64(36)/libc.Uint64FromInt64(4)), uintptr(unsafe.Pointer(&_getCacheSize)), _iLn3) if 0 != 0 { break } (*(*TVdbeOp)(unsafe.Pointer(aOp))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp + 1*24))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp + 6*24))).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) (*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 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) if zRight != 0 { if Xsqlite3_stricmp(tls, zRight, __ccgo_ts+18403) == 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)*32))).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 + 40)) = 0 _sqlite3CodeVerifySchema(tls, pParse, iDb) v5 = pParse + 56 *(*int32)(unsafe.Pointer(v5))++ v4 = *(*int32)(unsafe.Pointer(v5)) iReg = v4 if int32(_sqlite3UpperToLower[uint8(*(*int8)(unsafe.Pointer(zLeft)))]) == int32('p') { _sqlite3VdbeAddOp2(tls, v, int32(OP_Pagecount), iDb, iReg) } else { if zRight != 0 && _sqlite3DecOrHexToI64(tls, zRight, bp+40) == 0 { if *(*Ti64)(unsafe.Pointer(bp + 40)) < 0 { *(*Ti64)(unsafe.Pointer(bp + 40)) = 0 } else { if *(*Ti64)(unsafe.Pointer(bp + 40)) > libc.Int64FromUint32(0xfffffffe) { *(*Ti64)(unsafe.Pointer(bp + 40)) = libc.Int64FromUint32(0xfffffffe) } } } else { *(*Ti64)(unsafe.Pointer(bp + 40)) = 0 } _sqlite3VdbeAddOp3(tls, v, int32(OP_MaxPgcnt), iDb, iReg, int32(*(*Ti64)(unsafe.Pointer(bp + 40)))) } _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 + 18194 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) { 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)*32))).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) } if eMode == int32(PAGER_LOCKINGMODE_EXCLUSIVE) { zRet = __ccgo_ts + 18184 } _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)*32))).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 + 48)) = int64(-int32(2)) if zRight != 0 { _sqlite3DecOrHexToI64(tls, zRight, bp+48) if *(*Ti64)(unsafe.Pointer(bp + 48)) < int64(-int32(1)) { *(*Ti64)(unsafe.Pointer(bp + 48)) = int64(-int32(1)) } } *(*Ti64)(unsafe.Pointer(bp + 48)) = _sqlite3PagerJournalSizeLimit(tls, pPager1, *(*Ti64)(unsafe.Pointer(bp + 48))) _returnSingleInt(tls, v, *(*Ti64)(unsafe.Pointer(bp + 48))) 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 if !(zRight != 0) { _returnSingleInt(tls, v, int64(_sqlite3BtreeGetAutoVacuum(tls, pBt2))) } else { eAuto = _getAutoVacuum(tls, zRight) (*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.Uint64FromInt64(20)/libc.Uint64FromInt64(4)), uintptr(unsafe.Pointer(&_setMeta6)), _iLn11) if 0 != 0 { break } (*(*TVdbeOp)(unsafe.Pointer(aOp1))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp1 + 1*24))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp1 + 2*24))).Fp2 = iAddr + int32(4) (*(*TVdbeOp)(unsafe.Pointer(aOp1 + 4*24))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp1 + 4*24))).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 + 56)) = 0 if zRight == uintptr(0) || !(_sqlite3GetInt32(tls, zRight, bp+56) != 0) || *(*int32)(unsafe.Pointer(bp + 56)) <= 0 { *(*int32)(unsafe.Pointer(bp + 56)) = int32(0x7fffffff) } _sqlite3BeginWriteOperation(tls, pParse, 0, iDb) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), *(*int32)(unsafe.Pointer(bp + 56)), 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): 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): 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 + 60)) = int32(1) if _sqlite3GetInt32(tls, zRight, bp+60) != 0 { _sqlite3BtreeSetSpillSize(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, *(*int32)(unsafe.Pointer(bp + 60))) } if _sqlite3GetBoolean(tls, zRight, libc.BoolUint8(*(*int32)(unsafe.Pointer(bp + 60)) != 0)) != 0 { *(*Tu64)(unsafe.Pointer(db + 48)) |= uint64(SQLITE_CacheSpill) } else { *(*Tu64)(unsafe.Pointer(db + 48)) &= ^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): if zRight != 0 { _sqlite3DecOrHexToI64(tls, zRight, bp+64) if *(*Tsqlite3_int64)(unsafe.Pointer(bp + 64)) < 0 { *(*Tsqlite3_int64)(unsafe.Pointer(bp + 64)) = _sqlite3Config.FszMmap } if (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0) { (*Tsqlite3)(unsafe.Pointer(db)).FszMmap = *(*Tsqlite3_int64)(unsafe.Pointer(bp + 64)) } ii3 = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1) for { if !(ii3 >= 0) { break } if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii3)*32))).FpBt != 0 && (ii3 == iDb || (*TToken)(unsafe.Pointer(pId2)).Fn == uint32(0)) { _sqlite3BtreeSetMmapLimit(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii3)*32))).FpBt, *(*Tsqlite3_int64)(unsafe.Pointer(bp + 64))) } goto _11 _11: ; ii3-- } } *(*Tsqlite3_int64)(unsafe.Pointer(bp + 64)) = int64(-int32(1)) rc = Xsqlite3_file_control(tls, db, zDb, int32(SQLITE_FCNTL_MMAP_SIZE), bp+64) if rc == SQLITE_OK { _returnSingleInt(tls, v, *(*Tsqlite3_int64)(unsafe.Pointer(bp + 64))) } 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 *(*int8)(unsafe.Pointer(zRight)) != 0 { rc = _sqlite3OsAccess(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, zRight, int32(SQLITE_ACCESS_READWRITE), bp+72) if rc != SQLITE_OK || *(*int32)(unsafe.Pointer(bp + 72)) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18408, 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 *(*int8)(unsafe.Pointer(zRight)) != 0 { Xsqlite3_temp_directory = Xsqlite3_mprintf(tls, __ccgo_ts+3863, libc.VaList(bp+184, zRight)) } else { Xsqlite3_temp_directory = uintptr(0) } } Xsqlite3_mutex_leave(tls, _sqlite3MutexAlloc(tls, int32(SQLITE_MUTEX_STATIC_VFS1))) break /* ** PRAGMA [schema.]lock_proxy_file ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path" ** ** Return or set the value of the lock_proxy_file flag. Changing ** the value sets a specific file to be used for database access locks. ** */ fallthrough case int32(PragTyp_LOCK_PROXY_FILE): if !(zRight != 0) { pPager2 = _sqlite3BtreePager(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) *(*uintptr)(unsafe.Pointer(bp + 80)) = libc.UintptrFromInt32(0) pFile = _sqlite3PagerFile(tls, pPager2) _sqlite3OsFileControlHint(tls, pFile, int32(SQLITE_FCNTL_GET_LOCKPROXYFILE), bp+80) _returnSingleText(tls, v, *(*uintptr)(unsafe.Pointer(bp + 80))) } else { pPager3 = _sqlite3BtreePager(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) pFile1 = _sqlite3PagerFile(tls, pPager3) if *(*int8)(unsafe.Pointer(zRight)) != 0 { res1 = _sqlite3OsFileControl(tls, pFile1, int32(SQLITE_FCNTL_SET_LOCKPROXYFILE), zRight) } else { res1 = _sqlite3OsFileControl(tls, pFile1, int32(SQLITE_FCNTL_SET_LOCKPROXYFILE), libc.UintptrFromInt32(0)) } if res1 != SQLITE_OK { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+18433, 0) goto pragma_out } } 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+18463, 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 + 48)) |= mask } } else { *(*Tu64)(unsafe.Pointer(db + 48)) &= ^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+18516) == 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 { 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) if (*TPragmaName)(unsafe.Pointer(pPragma)).FiArg != 0 { v14 = __ccgo_ts + 18522 } else { v14 = __ccgo_ts + 18530 } if int32(uint32(*(*uint8)(unsafe.Pointer(pCol + 8))&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+184, i-nHidden, (*TColumn)(unsafe.Pointer(pCol)).FzCnName, _sqlite3ColumnType(tls, pCol, __ccgo_ts+1664), v15, v16, k, isHidden)) goto _12 _12: ; i++ pCol += 16 } } } 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)*32))).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)*32))).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+18537, libc.VaList(bp+184, (*TTable)(unsafe.Pointer(pTab1)).FzName)) if zSql != 0 { *(*uintptr)(unsafe.Pointer(bp + 88)) = uintptr(0) Xsqlite3_prepare(tls, db, zSql, -int32(1), bp+88, uintptr(0)) Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp + 88))) _sqlite3DbFree(tls, db, zSql) } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3ErrorMsg(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpParse, __ccgo_ts+1634, 0) (*TParse)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FpParse)).Frc = int32(SQLITE_NOMEM) } pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii4)*32))).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 + 11269 } else { if int32((*TTable)(unsafe.Pointer(pTab2)).FeTabType) == int32(TABTYP_VTAB) { zType = __ccgo_ts + 13517 } else { if (*TTable)(unsafe.Pointer(pTab2)).FtabFlags&uint32(TF_Shadow) != 0 { zType = __ccgo_ts + 18553 } else { zType = __ccgo_ts + 9531 } } } _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+18560, libc.VaList(bp+184, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(ii4)*32))).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) 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)*16))).FzCnName } _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+18567, libc.VaList(bp+184, i1, int32(cnum), v22)) if (*TPragmaName)(unsafe.Pointer(pPragma)).FiArg != 0 { _sqlite3VdbeMultiLoad(tls, v, int32(4), __ccgo_ts+18572, libc.VaList(bp+184, int32(*(*Tu8)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaSortOrder + uintptr(i1)))), *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(i1)*8)), 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 + 18577, 1: __ccgo_ts + 18579, 2: __ccgo_ts + 17020, } _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+18581, libc.VaList(bp+184, i2, (*TIndex)(unsafe.Pointer(pIdx1)).FzName, libc.BoolInt32(int32((*TIndex)(unsafe.Pointer(pIdx1)).FonError) != OE_None), azOrigin[int32(uint32(*(*uint16)(unsafe.Pointer(pIdx1 + 100))&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)*32))).FpBt == uintptr(0) { goto _24 } _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+18587, libc.VaList(bp+184, i3, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i3)*32))).FzDbSName, _sqlite3BtreeGetFilename(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i3)*32))).FpBt))) goto _24 _24: ; i3++ } case int32(PragTyp_COLLATION_LIST): i4 = 0 (*TParse)(unsafe.Pointer(pParse)).FnMem = int32(2) p = (*THash)(unsafe.Pointer(db + 648)).Ffirst for { if !(p != 0) { break } pColl = (*THashElem)(unsafe.Pointer(p)).Fdata v26 = i4 i4++ _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+18591, libc.VaList(bp+184, 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)*8)) for { if !(p1 != 0) { break } _pragmaFunclistLine(tls, v, p1, int32(1), showInternFunc) goto _28 _28: ; p1 = *(*uintptr)(unsafe.Pointer(p1 + 64)) } goto _27 _27: ; i5++ } j = (*THash)(unsafe.Pointer(db + 624)).Ffirst for { if !(j != 0) { break } p1 = (*THashElem)(unsafe.Pointer(j)).Fdata _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 + 576)).Ffirst for { if !(j1 != 0) { break } pMod = (*THashElem)(unsafe.Pointer(j1)).Fdata _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+8090, libc.VaList(bp+184, (*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.Uint64FromInt64(1608)/libc.Uint64FromInt64(24))) { break } _sqlite3VdbeMultiLoad(tls, v, int32(1), __ccgo_ts+8090, libc.VaList(bp+184, _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 + 64))).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+18594, libc.VaList(bp+184, i7, j2, (*TFKey)(unsafe.Pointer(pFK)).FzTo, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab5)).FaCol + uintptr((*(*TsColMap)(unsafe.Pointer(pFK + 64 + uintptr(j2)*16))).FiFrom)*16))).FzCnName, (*(*TsColMap)(unsafe.Pointer(pFK + 64 + uintptr(j2)*16))).FzCol, _actionName(tls, *(*Tu8)(unsafe.Pointer(pFK + 45 + 1))), _actionName(tls, *(*Tu8)(unsafe.Pointer(pFK + 45))), __ccgo_ts+18603)) 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 + 56)) += int32(4) v34 = pParse + 56 *(*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)*32))).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 + 64))).FpFKey == uintptr(0) { continue } iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTab6)).FpSchema) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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) i8 = int32(1) pFK1 = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab6 + 64))).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 + 96)) = 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+96, uintptr(0)) if x1 == 0 { if *(*uintptr)(unsafe.Pointer(bp + 96)) == 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 + 96)))).Ftnum), iDb) _sqlite3VdbeSetP4KeyInfo(tls, pParse, *(*uintptr)(unsafe.Pointer(bp + 96))) } } else { k2 = uintptr(0) break } goto _35 _35: ; i8++ pFK1 = (*TFKey)(unsafe.Pointer(pFK1)).FpNextFrom } 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) i8 = int32(1) pFK1 = (*(*struct { FaddColOffset int32 FpFKey uintptr FpDfltList uintptr })(unsafe.Pointer(pTab6 + 64))).FpFKey for { if !(pFK1 != 0) { break } pParent = _sqlite3FindTable(tls, db, (*TFKey)(unsafe.Pointer(pFK1)).FzTo, zDb) *(*uintptr)(unsafe.Pointer(bp + 96)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 104)) = uintptr(0) if pParent != 0 { x1 = _sqlite3FkLocateIndex(tls, pParse, pParent, pFK1, bp+96, bp+104) } 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 + 104)) != 0 { v38 = *(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 104)) + uintptr(j3)*4)) } else { v38 = (*(*TsColMap)(unsafe.Pointer(pFK1 + 64 + uintptr(j3)*16))).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 + 96)) != 0 { _sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), regRow, (*TFKey)(unsafe.Pointer(pFK1)).FnCol, 0, _sqlite3IndexAffinityStr(tls, db, *(*uintptr)(unsafe.Pointer(bp + 96))), (*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) } } /* 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+18608, libc.VaList(bp+184, (*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 + 104))) 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(*(*int8)(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. */ 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 + 112)) = int32(SQLITE_INTEGRITY_CHECK_ERROR_MAX) if zRight != 0 { if _sqlite3GetInt32(tls, (*TToken)(unsafe.Pointer(pValue)).Fz, bp+112) != 0 { if *(*int32)(unsafe.Pointer(bp + 112)) <= 0 { *(*int32)(unsafe.Pointer(bp + 112)) = int32(SQLITE_INTEGRITY_CHECK_ERROR_MAX) } } else { if iDb >= 0 { v39 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FzDbSName } else { v39 = uintptr(0) } pObjTab = _sqlite3LocateTable(tls, pParse, uint32(0), zRight, v39) } } _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), *(*int32)(unsafe.Pointer(bp + 112))-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[] */ 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. */ pTbls = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i9)*32))).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++ } goto _41 _41: ; x2 = (*THashElem)(unsafe.Pointer(x2)).Fnext } if cnt == 0 { goto _40 } if pObjTab != 0 { cnt++ } aRoot = _sqlite3DbMallocRawNN(tls, db, uint64(4)*uint64(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)+cnt) _sqlite3ClearTempRegCache(tls, pParse) /* Do the b-tree integrity checks */ _sqlite3VdbeAddOp4(tls, v, int32(OP_IntegrityCk), int32(1), cnt, int32(8), 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+18612, libc.VaList(bp+184, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i9)*32))).FzDbSName)), -int32(6)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(2), int32(3), int32(3)) _integrityCheckResultRow(tls, v) _sqlite3VdbeJumpHere(tls, v, addr1) /* Check that the indexes all have the right number of rows */ if pObjTab != 0 { v48 = int32(1) } else { v48 = 0 } cnt = v48 _sqlite3VdbeLoadString(tls, v, int32(2), __ccgo_ts+18636) x2 = (*THash)(unsafe.Pointer(pTbls)).Ffirst for { if !(x2 != 0) { break } iTab = 0 pTab9 = (*THashElem)(unsafe.Pointer(x2)).Fdata if pObjTab != 0 && pObjTab != pTab9 { goto _49 } if (*TTable)(unsafe.Pointer(pTab9)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { v50 = cnt cnt++ iTab = v50 } else { iTab = cnt pIdx5 = (*TTable)(unsafe.Pointer(pTab9)).FpIndex for { if !(pIdx5 != 0) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx5 + 100))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { break } iTab++ goto _51 _51: ; pIdx5 = (*TIndex)(unsafe.Pointer(pIdx5)).FpNext } } pIdx5 = (*TTable)(unsafe.Pointer(pTab9)).FpIndex for { if !(pIdx5 != 0) { break } if (*TIndex)(unsafe.Pointer(pIdx5)).FpPartIdxWhere == uintptr(0) { addr1 = _sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), int32(8)+cnt, 0, int32(8)+iTab) _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) } cnt++ goto _52 _52: ; pIdx5 = (*TIndex)(unsafe.Pointer(pIdx5)).FpNext } goto _49 _49: ; x2 = (*THashElem)(unsafe.Pointer(x2)).Fnext } /* Make sure all the indices are constructed correctly. */ x2 = (*THash)(unsafe.Pointer(pTbls)).Ffirst for { if !(x2 != 0) { break } pTab10 = (*THashElem)(unsafe.Pointer(x2)).Fdata pPrior = uintptr(0) r1 = -int32(1) /* Maximum non-virtual column number */ if pObjTab != 0 && pObjTab != pTab10 { goto _53 } if !(int32((*TTable)(unsafe.Pointer(pTab10)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) { goto _53 } if isQuick != 0 || (*TTable)(unsafe.Pointer(pTab10)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { pPk1 = uintptr(0) r2 = 0 } else { pPk1 = _sqlite3PrimaryKeyIndex(tls, pTab10) 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, pTab10, int32(OP_OpenRead), uint8(0), int32(1), uintptr(0), bp+116, bp+120) /* 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 pIdx6 = (*TTable)(unsafe.Pointer(pTab10)).FpIndex for { if !(pIdx6 != 0) { break } _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, int32(8)+j4) /* index entries counter */ goto _54 _54: ; pIdx6 = (*TIndex)(unsafe.Pointer(pIdx6)).FpNext j4++ } _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), *(*int32)(unsafe.Pointer(bp + 116)), 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. */ if (*TTable)(unsafe.Pointer(pTab10)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { mxCol = -int32(1) j4 = 0 for { if !(j4 < int32((*TTable)(unsafe.Pointer(pTab10)).FnCol)) { break } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab10)).FaCol + uintptr(j4)*16))).FcolFlags)&int32(COLFLAG_VIRTUAL) == 0 { mxCol++ } goto _55 _55: ; j4++ } if mxCol == int32((*TTable)(unsafe.Pointer(pTab10)).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, pTab10))).FnColumn) - int32(1) } if mxCol >= 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), *(*int32)(unsafe.Pointer(bp + 116)), 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 + 116)), 0, r2, int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol)) _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), r2) zErr = _sqlite3MPrintf(tls, db, __ccgo_ts+18665, libc.VaList(bp+184, (*TTable)(unsafe.Pointer(pTab10)).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 + 116)), j4, r2+j4) goto _56 _56: ; 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(pTab10)).FtabFlags&uint32(TF_Strict) != uint32(0)) j4 = 0 for { if !(j4 < int32((*TTable)(unsafe.Pointer(pTab10)).FnCol)) { break } pCol1 = (*TTable)(unsafe.Pointer(pTab10)).FaCol + uintptr(j4)*16 /* Check datatypes (besides NOT NULL) */ if j4 == int32((*TTable)(unsafe.Pointer(pTab10)).FiPKey) { goto _57 } if bStrict != 0 { doTypeCheck = libc.BoolInt32(int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 8))&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 + 8))&0xf>>0)) == 0 && !(doTypeCheck != 0) { goto _57 } /* 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, pTab10, *(*int32)(unsafe.Pointer(bp + 116)), j4, int32(3)) p11 = -int32(1) p3 = int32(3) } else { if (*TColumn)(unsafe.Pointer(pCol1)).FiDflt != 0 { *(*uintptr)(unsafe.Pointer(bp + 128)) = uintptr(0) _sqlite3ValueFromExpr(tls, db, _sqlite3ColumnExpr(tls, pTab10, pCol1), (*Tsqlite3)(unsafe.Pointer(db)).Fenc, uint8((*TColumn)(unsafe.Pointer(pCol1)).Faffinity), bp+128) if *(*uintptr)(unsafe.Pointer(bp + 128)) != 0 { p4 = Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(bp + 128))) _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(bp + 128))) } } p11 = *(*int32)(unsafe.Pointer(bp + 116)) if !((*TTable)(unsafe.Pointer(pTab10)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) { p3 = int32(_sqlite3TableColumnToIndex(tls, _sqlite3PrimaryKeyIndex(tls, pTab10), int16(j4))) } else { p3 = int32(_sqlite3TableColumnToStorage(tls, pTab10, int16(j4))) } } labelError = _sqlite3VdbeMakeLabel(tls, pParse) labelOk = _sqlite3VdbeMakeLabel(tls, pParse) if int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 8))&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, pTab10, j4, int32(3)) jmp3 = _sqlite3VdbeAddOp2(tls, v, int32(OP_NotNull), int32(3), labelOk) } zErr1 = _sqlite3MPrintf(tls, db, __ccgo_ts+18701, libc.VaList(bp+184, (*TTable)(unsafe.Pointer(pTab10)).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) _sqlite3VdbeChangeP5(tls, v, uint16(_aStdTypeMask[int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 8))&0xf0>>4))-int32(1)])) zErr1 = _sqlite3MPrintf(tls, db, __ccgo_ts+18721, libc.VaList(bp+184, _sqlite3StdType[int32(uint32(*(*uint8)(unsafe.Pointer(pCol1 + 8))&0xf0>>4))-int32(1)], (*TTable)(unsafe.Pointer(pTab10)).FzName, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab10)).FaCol + uintptr(j4)*16))).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+18743, libc.VaList(bp+184, (*TTable)(unsafe.Pointer(pTab10)).FzName, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab10)).FaCol + uintptr(j4)*16))).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, pTab10, *(*int32)(unsafe.Pointer(bp + 116)), j4, int32(3)) } _sqlite3VdbeAddOp4(tls, v, int32(OP_Affinity), int32(3), int32(1), 0, __ccgo_ts+18766, -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+18768, libc.VaList(bp+184, (*TTable)(unsafe.Pointer(pTab10)).FzName, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab10)).FaCol + uintptr(j4)*16))).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 _57 _57: ; j4++ } /* Verify CHECK constraints */ if (*TTable)(unsafe.Pointer(pTab10)).FpCheck != 0 && (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_IgnoreChecks) == uint64(0) { pCheck = _sqlite3ExprListDup(tls, db, (*TTable)(unsafe.Pointer(pTab10)).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 + 116)) + 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)*32))).FpExpr, addrCkFault, 0) goto _58 _58: ; 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+18788, libc.VaList(bp+184, (*TTable)(unsafe.Pointer(pTab10)).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 pIdx6 = (*TTable)(unsafe.Pointer(pTab10)).FpIndex for { if !(pIdx6 != 0) { break } ckUniq = _sqlite3VdbeMakeLabel(tls, pParse) if pPk1 == pIdx6 { goto _59 } r1 = _sqlite3GenerateIndexKey(tls, pParse, pIdx6, *(*int32)(unsafe.Pointer(bp + 116)), 0, 0, bp+136, pPrior, r1) pPrior = pIdx6 _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 + 120))+j4, ckUniq, r1, int32((*TIndex)(unsafe.Pointer(pIdx6)).FnColumn)) _sqlite3VdbeLoadString(tls, v, int32(3), __ccgo_ts+18818) _sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(7), int32(3), int32(3)) _sqlite3VdbeLoadString(tls, v, int32(4), __ccgo_ts+18823) _sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(4), int32(3), int32(3)) jmp5 = _sqlite3VdbeLoadString(tls, v, int32(4), (*TIndex)(unsafe.Pointer(pIdx6)).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(pTab10)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) { _sqlite3VdbeAddOp2(tls, v, int32(OP_IdxRowid), *(*int32)(unsafe.Pointer(bp + 120))+j4, int32(3)) jmp7 = _sqlite3VdbeAddOp3(tls, v, int32(OP_Eq), int32(3), 0, r1+int32((*TIndex)(unsafe.Pointer(pIdx6)).FnColumn)-int32(1)) _sqlite3VdbeLoadString(tls, v, int32(3), __ccgo_ts+18844) _sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(7), int32(3), int32(3)) _sqlite3VdbeLoadString(tls, v, int32(4), __ccgo_ts+18880) _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(pIdx6)).FnKeyCol)) { break } if *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx6)).FazColl + uintptr(kk)*8)) == uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) { goto _60 } if label6 == 0 { label6 = _sqlite3VdbeMakeLabel(tls, pParse) } _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), *(*int32)(unsafe.Pointer(bp + 120))+j4, kk, int32(3)) _sqlite3VdbeAddOp3(tls, v, int32(OP_Ne), int32(3), label6, r1+kk) goto _60 _60: ; kk++ } if label6 != 0 { jmp6 = _sqlite3VdbeAddOp0(tls, v, int32(OP_Goto)) _sqlite3VdbeResolveLabel(tls, v, label6) _sqlite3VdbeLoadString(tls, v, int32(3), __ccgo_ts+18818) _sqlite3VdbeAddOp3(tls, v, int32(OP_Concat), int32(7), int32(3), int32(3)) _sqlite3VdbeLoadString(tls, v, int32(4), __ccgo_ts+18891) _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(pIdx6)).FonError) != OE_None { uniqOk = _sqlite3VdbeMakeLabel(tls, pParse) kk = 0 for { if !(kk < int32((*TIndex)(unsafe.Pointer(pIdx6)).FnKeyCol)) { break } iCol1 = int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx6)).FaiColumn + uintptr(kk)*2))) if iCol1 >= 0 && int32(uint32(*(*uint8)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab10)).FaCol + uintptr(iCol1)*16 + 8))&0xf>>0)) != 0 { goto _61 } _sqlite3VdbeAddOp2(tls, v, int32(OP_IsNull), r1+kk, uniqOk) goto _61 _61: ; kk++ } jmp61 = _sqlite3VdbeAddOp1(tls, v, int32(OP_Next), *(*int32)(unsafe.Pointer(bp + 120))+j4) _sqlite3VdbeGoto(tls, v, uniqOk) _sqlite3VdbeJumpHere(tls, v, jmp61) _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IdxGT), *(*int32)(unsafe.Pointer(bp + 120))+j4, uniqOk, r1, int32((*TIndex)(unsafe.Pointer(pIdx6)).FnKeyCol)) _sqlite3VdbeLoadString(tls, v, int32(3), __ccgo_ts+18918) _sqlite3VdbeGoto(tls, v, jmp5) _sqlite3VdbeResolveLabel(tls, v, uniqOk) } _sqlite3VdbeJumpHere(tls, v, jmp4) _sqlite3ResolvePartIdxLabel(tls, pParse, *(*int32)(unsafe.Pointer(bp + 136))) goto _59 _59: ; pIdx6 = (*TIndex)(unsafe.Pointer(pIdx6)).FpNext j4++ } } _sqlite3VdbeAddOp2(tls, v, int32(OP_Next), *(*int32)(unsafe.Pointer(bp + 116)), loopTop) _sqlite3VdbeJumpHere(tls, v, loopTop-int32(1)) if pPk1 != 0 { _sqlite3ReleaseTempRange(tls, pParse, r2, int32((*TIndex)(unsafe.Pointer(pPk1)).FnKeyCol)) } goto _53 _53: ; 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 } pTab11 = (*THashElem)(unsafe.Pointer(x2)).Fdata if pObjTab != 0 && pObjTab != pTab11 { goto _62 } if int32((*TTable)(unsafe.Pointer(pTab11)).FeTabType) == TABTYP_NORM { goto _62 } if !(int32((*TTable)(unsafe.Pointer(pTab11)).FeTabType) == libc.Int32FromInt32(TABTYP_VTAB)) { goto _62 } if int32((*TTable)(unsafe.Pointer(pTab11)).FnCol) <= 0 { zMod = *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab11 + 64))).FazArg)) if _sqlite3HashFind(tls, db+576, zMod) == uintptr(0) { goto _62 } } _sqlite3ViewGetColumnNames(tls, pParse, pTab11) if (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab11 + 64))).Fp == uintptr(0) { goto _62 } pVTab = (*TVTable)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab11 + 64))).Fp)).FpVtab if pVTab == uintptr(0) { goto _62 } if (*Tsqlite3_vtab)(unsafe.Pointer(pVTab)).FpModule == uintptr(0) { goto _62 } if (*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(pVTab)).FpModule)).FiVersion < int32(4) { goto _62 } if (*Tsqlite3_module)(unsafe.Pointer((*Tsqlite3_vtab)(unsafe.Pointer(pVTab)).FpModule)).FxIntegrity == uintptr(0) { goto _62 } _sqlite3VdbeAddOp3(tls, v, int32(OP_VCheck), i9, int32(3), isQuick) (*TTable)(unsafe.Pointer(pTab11)).FnTabRef++ _sqlite3VdbeAppendP4(tls, v, pTab11, -int32(16)) a11 = _sqlite3VdbeAddOp1(tls, v, int32(OP_IsNull), int32(3)) _integrityCheckResultRow(tls, v) _sqlite3VdbeJumpHere(tls, v, a11) goto _62 goto _62 _62: ; x2 = (*THashElem)(unsafe.Pointer(x2)).Fnext } goto _40 _40: ; i9++ } aOp2 = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint64FromInt64(28)/libc.Uint64FromInt64(4)), uintptr(unsafe.Pointer(&_endCode)), _iLn21) if aOp2 != 0 { (*(*TVdbeOp)(unsafe.Pointer(aOp2))).Fp2 = int32(1) - *(*int32)(unsafe.Pointer(bp + 112)) (*(*TVdbeOp)(unsafe.Pointer(aOp2 + 2*24))).Fp4type = int8(-libc.Int32FromInt32(1)) *(*uintptr)(unsafe.Pointer(aOp2 + 2*24 + 16)) = __ccgo_ts + 18945 (*(*TVdbeOp)(unsafe.Pointer(aOp2 + 5*24))).Fp4type = int8(-libc.Int32FromInt32(1)) *(*uintptr)(unsafe.Pointer(aOp2 + 5*24 + 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 } _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 { v64 = int32((*struct { FzName uintptr Fenc Tu8 })(unsafe.Pointer(pEnc)).Fenc) } else { v64 = int32(SQLITE_UTF16LE) } enc = uint8(v64) (*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema)).Fenc = enc _sqlite3SetTextEncoding(tls, db, enc) break } goto _63 _63: ; pEnc += 16 } if !((*struct { FzName uintptr Fenc Tu8 })(unsafe.Pointer(pEnc)).FzName != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19006, libc.VaList(bp+184, 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.Uint64FromInt64(8)/libc.Uint64FromInt64(4)), uintptr(unsafe.Pointer(&_setCookie)), 0) if 0 != 0 { break } (*(*TVdbeOp)(unsafe.Pointer(aOp3))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp3 + 1*24))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp3 + 1*24))).Fp2 = iCookie (*(*TVdbeOp)(unsafe.Pointer(aOp3 + 1*24))).Fp3 = _sqlite3Atoi(tls, zRight) (*(*TVdbeOp)(unsafe.Pointer(aOp3 + 1*24))).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*24))).Fopcode = uint8(OP_Noop) } } else { aOp4 = _sqlite3VdbeAddOpList(tls, v, int32(libc.Uint64FromInt64(12)/libc.Uint64FromInt64(4)), uintptr(unsafe.Pointer(&_readCookie)), 0) if 0 != 0 { break } (*(*TVdbeOp)(unsafe.Pointer(aOp4))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp4 + 1*24))).Fp1 = iDb (*(*TVdbeOp)(unsafe.Pointer(aOp4 + 1*24))).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 { v66 = i10 i10++ v65 = Xsqlite3_compileoption_get(tls, v66) zOpt = v65 if !(v65 != 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 { v67 = iDb } else { v67 = libc.Int32FromInt32(SQLITE_MAX_ATTACHED) + libc.Int32FromInt32(2) } iBt = v67 eMode2 = SQLITE_CHECKPOINT_PASSIVE if zRight != 0 { if _sqlite3StrICmp(tls, zRight, __ccgo_ts+18201) == 0 { eMode2 = int32(SQLITE_CHECKPOINT_FULL) } else { if _sqlite3StrICmp(tls, zRight, __ccgo_ts+19031) == 0 { eMode2 = int32(SQLITE_CHECKPOINT_RESTART) } else { if _sqlite3StrICmp(tls, zRight, __ccgo_ts+18354) == 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) { v68 = int32(int64((*Tsqlite3)(unsafe.Pointer(db)).FpWalArg)) } else { v68 = 0 } _returnSingleInt(tls, v, int64(v68)) 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: ** ** 0x00001 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. ** ** 0x00002 Run ANALYZE on tables that might benefit. On by default. ** See below for additional information. ** ** 0x00010 Run all ANALYZE operations using an analysis_limit that ** is the lessor of the current analysis_limit and the ** SQLITE_DEFAULT_OPTIMIZE_LIMIT compile-time option. ** The default value of SQLITE_DEFAULT_OPTIMIZE_LIMIT is ** currently (2024-02-19) set to 2000, which is such that ** the worst case run-time for PRAGMA optimize on a 100MB ** database will usually be less than 100 milliseconds on ** a RaspberryPI-4 class machine. On by default. ** ** 0x10000 Look at tables to see if they need to be reanalyzed ** due to growth or shrinkage even if they have not been ** queried during the current connection. Off by default. ** ** The default MASK is and always shall be 0x0fffe. In the current ** implementation, the default mask only covers the 0x00002 optimization, ** though additional optimizations that are covered by 0x0fffe might be ** added in the future. Optimizations that are off by default and must ** be explicitly requested have masks of 0x10000 or greater. ** ** 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 0x00002 is set. ** ** (2) The table is an ordinary table, not a virtual table or view. ** ** (3) The table name does not begin with "sqlite_". ** ** (4) One or more of the following is true: ** (4a) The 0x10000 MASK bit is set. ** (4b) One or more indexes on the table lacks an entry ** in the sqlite_stat1 table. ** (4c) 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. ** ** (5) One or more of the following is true: ** (5a) One or more indexes on the table lacks an entry ** in the sqlite_stat1 table. (Same as 4a) ** (5b) The number of rows in the table has increased or decreased by ** 10-fold. In other words, the current size of the table is ** 10 times larger than the size in sqlite_stat1 or else the ** current size is less than 1/10th the size in sqlite_stat1. ** ** The rules for when tables are analyzed are likely to change in ** future releases. Future versions of SQLite might accept a string ** literal argument to this pragma that contains a mnemonic description ** of the options rather than a bitmap. */ fallthrough case int32(PragTyp_OPTIMIZE): /* Analysis limit to use */ nCheck = 0 /* Number of tables to be optimized */ nBtree = 0 /* Number of indexes on the current table */ if zRight != 0 { opMask = uint32(_sqlite3Atoi(tls, zRight)) if opMask&uint32(0x02) == uint32(0) { break } } else { opMask = uint32(0xfffe) } if opMask&uint32(0x10) == uint32(0) { nLimit = 0 } else { if (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit > 0 && (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit < int32(SQLITE_DEFAULT_OPTIMIZE_LIMIT) { nLimit = 0 } else { nLimit = int32(SQLITE_DEFAULT_OPTIMIZE_LIMIT) } } v70 = pParse + 52 v69 = *(*int32)(unsafe.Pointer(v70)) *(*int32)(unsafe.Pointer(v70))++ iTabCur = v69 if zDb != 0 { v72 = iDb } else { v72 = (*Tsqlite3)(unsafe.Pointer(db)).FnDb - int32(1) } iDbLast = v72 for { if !(iDb <= iDbLast) { break } if iDb == int32(1) { goto _71 } _sqlite3CodeVerifySchema(tls, pParse, iDb) pSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FpSchema k4 = (*THash)(unsafe.Pointer(pSchema + 8)).Ffirst for { if !(k4 != 0) { break } pTab12 = (*THashElem)(unsafe.Pointer(k4)).Fdata /* This only works for ordinary tables */ if !(int32((*TTable)(unsafe.Pointer(pTab12)).FeTabType) == libc.Int32FromInt32(TABTYP_NORM)) { goto _73 } /* Do not scan system tables */ if 0 == Xsqlite3_strnicmp(tls, (*TTable)(unsafe.Pointer(pTab12)).FzName, __ccgo_ts+6937, int32(7)) { goto _73 } /* Find the size of the table as last recorded in sqlite_stat1. ** If any index is unanalyzed, then the threshold is -1 to ** indicate a new, unanalyzed index */ szThreshold = (*TTable)(unsafe.Pointer(pTab12)).FnRowLogEst nIndex = 0 pIdx7 = (*TTable)(unsafe.Pointer(pTab12)).FpIndex for { if !(pIdx7 != 0) { break } nIndex++ if !(int32(uint32(*(*uint16)(unsafe.Pointer(pIdx7 + 100))&0x80>>7)) != 0) { szThreshold = int16(-int32(1)) /* Always analyze if any index lacks statistics */ } goto _74 _74: ; pIdx7 = (*TIndex)(unsafe.Pointer(pIdx7)).FpNext } /* If table pTab has not been used in a way that would benefit from ** having analysis statistics during the current session, then skip it, ** unless the 0x10000 MASK bit is set. */ if (*TTable)(unsafe.Pointer(pTab12)).FtabFlags&uint32(TF_MaybeReanalyze) != uint32(0) { /* Check for size change if stat1 has been used for a query */ } else { if opMask&uint32(0x10000) != 0 { /* Check for size change if 0x10000 is set */ } else { if (*TTable)(unsafe.Pointer(pTab12)).FpIndex != uintptr(0) && int32(szThreshold) < 0 { /* Do analysis if unanalyzed indexes exists */ } else { /* Otherwise, we can skip this table */ goto _73 } } } nCheck++ if nCheck == int32(2) { /* If ANALYZE might be invoked two or more times, hold a write ** transaction for efficiency */ _sqlite3BeginWriteOperation(tls, pParse, 0, iDb) } nBtree += nIndex + int32(1) /* Reanalyze if the table is 10 times larger or smaller than ** the last analysis. Unconditional reanalysis if there are ** unanalyzed indexes. */ _sqlite3OpenTable(tls, pParse, iTabCur, iDb, pTab12, int32(OP_OpenRead)) if int32(szThreshold) >= 0 { iRange = int16(33) /* 10x size change */ if int32(szThreshold) >= int32(iRange) { v75 = int32(szThreshold) - int32(iRange) } else { v75 = -int32(1) } _sqlite3VdbeAddOp4Int(tls, v, int32(OP_IfSizeBetween), iTabCur, int32(uint32(_sqlite3VdbeCurrentAddr(tls, v)+int32(2))+opMask&uint32(1)), v75, int32(szThreshold)+int32(iRange)) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_Rewind), iTabCur, int32(uint32(_sqlite3VdbeCurrentAddr(tls, v)+int32(2))+opMask&uint32(1))) } zSubSql = _sqlite3MPrintf(tls, db, __ccgo_ts+19039, libc.VaList(bp+184, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FzDbSName, (*TTable)(unsafe.Pointer(pTab12)).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 { if nLimit != 0 { v76 = int32(0x02) } else { v76 = 00 } _sqlite3VdbeAddOp4(tls, v, int32(OP_SqlExec), v76, nLimit, 0, zSubSql, -int32(6)) } goto _73 _73: ; k4 = (*THashElem)(unsafe.Pointer(k4)).Fnext } goto _71 _71: ; iDb++ } _sqlite3VdbeAddOp0(tls, v, int32(OP_Expire)) /* In a schema with a large number of tables and indexes, scale back ** the analysis_limit to avoid excess run-time in the worst case. */ if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) && nLimit > 0 && nBtree > int32(100) { nLimit = int32(100) * nLimit / nBtree if nLimit < int32(100) { nLimit = int32(100) } aOp5 = _sqlite3VdbeGetOp(tls, v, 0) iEnd = _sqlite3VdbeCurrentAddr(tls, v) iAddr1 = 0 for { if !(iAddr1 < iEnd) { break } if int32((*(*TVdbeOp)(unsafe.Pointer(aOp5 + uintptr(iAddr1)*24))).Fopcode) == int32(OP_SqlExec) { (*(*TVdbeOp)(unsafe.Pointer(aOp5 + uintptr(iAddr1)*24))).Fp2 = nLimit } goto _77 _77: ; iAddr1++ } } 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: 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+144) == SQLITE_OK { Xsqlite3_soft_heap_limit64(tls, *(*Tsqlite3_int64)(unsafe.Pointer(bp + 144))) } _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+152) == SQLITE_OK { iPrior = Xsqlite3_hard_heap_limit64(tls, int64(-int32(1))) if *(*Tsqlite3_int64)(unsafe.Pointer(bp + 152)) > 0 && (iPrior == 0 || iPrior > *(*Tsqlite3_int64)(unsafe.Pointer(bp + 152))) { Xsqlite3_hard_heap_limit64(tls, *(*Tsqlite3_int64)(unsafe.Pointer(bp + 152))) } } _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+160) == SQLITE_OK && *(*Tsqlite3_int64)(unsafe.Pointer(bp + 160)) >= 0 { Xsqlite3_limit(tls, db, int32(SQLITE_LIMIT_WORKER_THREADS), int32(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 160))&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+168) == SQLITE_OK && *(*Tsqlite3_int64)(unsafe.Pointer(bp + 168)) >= 0 { (*Tsqlite3)(unsafe.Pointer(db)).FnAnalysisLimit = int32(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 168)) & 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 + 18948, Fenc: uint8(SQLITE_UTF8), }, 1: { FzName: __ccgo_ts + 18953, Fenc: uint8(SQLITE_UTF8), }, 2: { FzName: __ccgo_ts + 18959, Fenc: uint8(SQLITE_UTF16LE), }, 3: { FzName: __ccgo_ts + 18968, Fenc: uint8(SQLITE_UTF16BE), }, 4: { FzName: __ccgo_ts + 18977, Fenc: uint8(SQLITE_UTF16LE), }, 5: { FzName: __ccgo_ts + 18985, Fenc: uint8(SQLITE_UTF16BE), }, 6: { FzName: __ccgo_ts + 18993, }, 7: { FzName: __ccgo_ts + 19000, }, 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 { 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 { 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 int8 var i, j, rc int32 var pPragma, pTab uintptr var _ /* acc at bp+0 */ TStrAccum var _ /* zBuf at bp+32 */ [200]int8 _, _, _, _, _, _ = cSep, i, j, pPragma, pTab, rc pPragma = pAux pTab = uintptr(0) cSep = int8('(') _ = argc _ = argv _sqlite3StrAccumInit(tls, bp, uintptr(0), bp+32, int32(200), 0) Xsqlite3_str_appendall(tls, bp, __ccgo_ts+19057) 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+19072, libc.VaList(bp+240, int32(cSep), _pragCName[j])) cSep = int8(',') goto _1 _1: ; i++ j++ } if i == 0 { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+19079, libc.VaList(bp+240, (*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+19085) j++ } if int32((*TPragmaName)(unsafe.Pointer(pPragma)).FmPragFlg)&(libc.Int32FromInt32(PragFlg_SchemaOpt)|libc.Int32FromInt32(PragFlg_SchemaReq)) != 0 { Xsqlite3_str_appendall(tls, bp, __ccgo_ts+19097) j++ } Xsqlite3_str_append(tls, bp, __ccgo_ts+5518, int32(1)) _sqlite3StrAccumFinish(tls, bp) rc = Xsqlite3_declare_vtab(tls, db, bp+32) if rc == SQLITE_OK { pTab = Xsqlite3_malloc(tls, int32(48)) if pTab == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, pTab, 0, uint64(48), libc.X__builtin_object_size(tls, pTab, 0)) (*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+3863, libc.VaList(bp+240, 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 (*Tsqlite3_index_constraint)(unsafe.Pointer(pConstraint)).FiColumn < int32((*TPragmaVtab)(unsafe.Pointer(pTab)).FiHidden) { goto _1 } if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(pConstraint)).Fop) != int32(SQLITE_INDEX_CONSTRAINT_EQ) { goto _1 } if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(pConstraint)).Fusable) == 0 { return int32(SQLITE_CONSTRAINT) } j = (*Tsqlite3_index_constraint)(unsafe.Pointer(pConstraint)).FiColumn - int32((*TPragmaVtab)(unsafe.Pointer(pTab)).FiHidden) 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) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedCost = libc.Float64FromInt32(20) (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FestimatedRows = int64(20) if seen[int32(1)] != 0 { 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(40)) if pCsr == uintptr(0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pCsr, 0, uint64(40), libc.X__builtin_object_size(tls, pCsr, 0)) (*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) (*TPragmaVtabCursor)(unsafe.Pointer(pCsr)).FiRowid = 0 i = 0 for { if !(i < int32(libc.Uint64FromInt64(16)/libc.Uint64FromInt64(8))) { break } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(pCsr + 24 + uintptr(i)*8))) *(*uintptr)(unsafe.Pointer(pCsr + 24 + uintptr(i)*8)) = 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++ 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)*8))) if zText != 0 { *(*uintptr)(unsafe.Pointer(pCsr + 24 + uintptr(j)*8)) = Xsqlite3_mprintf(tls, __ccgo_ts+3863, libc.VaList(bp+40, zText)) if *(*uintptr)(unsafe.Pointer(pCsr + 24 + uintptr(j)*8)) == 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 + 136 + 1*4))) Xsqlite3_str_appendall(tls, bp, __ccgo_ts+19112) if *(*uintptr)(unsafe.Pointer(pCsr + 24 + 1*8)) != 0 { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+19120, libc.VaList(bp+40, *(*uintptr)(unsafe.Pointer(pCsr + 24 + 1*8)))) } Xsqlite3_str_appendall(tls, bp, (*TPragmaName)(unsafe.Pointer((*TPragmaVtab)(unsafe.Pointer(pTab)).FpName)).FzName) if *(*uintptr)(unsafe.Pointer(pCsr + 24)) != 0 { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+19124, libc.VaList(bp+40, *(*uintptr)(unsafe.Pointer(pCsr + 24)))) } 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+8, uintptr(0)) Xsqlite3_free(tls, zSql) if rc != SQLITE_OK { (*TPragmaVtab)(unsafe.Pointer(pTab)).Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+3863, libc.VaList(bp+40, 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 + 24 + uintptr(i-int32((*TPragmaVtab)(unsafe.Pointer(pTab)).FiHidden))*8)), -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, 16)) = __ccgo_fp(_pragmaVtabConnect) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_pragmaVtabBestIndex) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_pragmaVtabDisconnect) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_pragmaVtabOpen) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_pragmaVtabClose) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_pragmaVtabFilter) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_pragmaVtabNext) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_pragmaVtabEof) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_pragmaVtabColumn) *(*uintptr)(unsafe.Add(p, 96)) = __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 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) } 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+19158, libc.VaList(bp+8, *(*uintptr)(unsafe.Pointer(azObj)), *(*uintptr)(unsafe.Pointer(azObj + 1*8)), _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(141840)) } else { if *(*uintptr)(unsafe.Pointer(azObj + 1*8)) != 0 { v1 = *(*uintptr)(unsafe.Pointer(azObj + 1*8)) } else { v1 = __ccgo_ts + 5520 } zObj = v1 z = _sqlite3MPrintf(tls, db, __ccgo_ts+19186, libc.VaList(bp+8, zObj)) if zExtra != 0 && *(*int8)(unsafe.Pointer(zExtra)) != 0 { z = _sqlite3MPrintf(tls, db, __ccgo_ts+19217, libc.VaList(bp+8, z, zExtra)) } *(*uintptr)(unsafe.Pointer((*TInitData)(unsafe.Pointer(pData)).FpzErrMsg)) = z (*TInitData)(unsafe.Pointer(pData)).Frc = _sqlite3CorruptError(tls, int32(141847)) } } } } } var _azAlterType = [3]uintptr{ 0: __ccgo_ts + 19128, 1: __ccgo_ts + 19135, 2: __ccgo_ts + 19147, } // 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 _ = NotUsed _ = argc *(*Tu32)(unsafe.Pointer(db + 44)) |= 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) } if *(*uintptr)(unsafe.Pointer(argv + 3*8)) == uintptr(0) { _corruptSchema(tls, pData, argv, uintptr(0)) } else { if *(*uintptr)(unsafe.Pointer(argv + 4*8)) != 0 && int32('c') == int32(_sqlite3UpperToLower[uint8(*(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(argv + 4*8)))))]) && int32('r') == int32(_sqlite3UpperToLower[uint8(*(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(argv + 4*8)) + 1)))]) { saved_iDb = (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb /* Return code from sqlite3_prepare() */ (*Tsqlite3)(unsafe.Pointer(db)).Finit1.FiDb = uint8(iDb) if _sqlite3GetUInt32(tls, *(*uintptr)(unsafe.Pointer(argv + 3*8)), db+192) == 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+14957) } } libc.SetBitFieldPtr8Uint32(db+192+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*8)), -int32(1), uint32(0), uintptr(0), bp, uintptr(0)) rc = (*Tsqlite3)(unsafe.Pointer(db)).FerrCode (*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 + 192 + 8))&0x1>>0)) != 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*8)) == uintptr(0) || *(*uintptr)(unsafe.Pointer(argv + 4*8)) != uintptr(0) && int32(*(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(argv + 4*8))))) != 0 { _corruptSchema(tls, pData, argv, uintptr(0)) } else { pIndex = _sqlite3FindIndex(tls, db, *(*uintptr)(unsafe.Pointer(argv + 1*8)), (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FzDbSName) if pIndex == uintptr(0) { _corruptSchema(tls, pData, argv, __ccgo_ts+19225) } else { if _sqlite3GetUInt32(tls, *(*uintptr)(unsafe.Pointer(argv + 3*8)), pIndex+88) == 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+14957) } } } } } } 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(144) defer tls.Free(144) 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+72 */ TInitData var _ /* meta at bp+48 */ [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))) (*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 + 9531 if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { v2 = __ccgo_ts + 6945 } else { v2 = __ccgo_ts + 6478 } 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 + 8603 (*(*[6]uintptr)(unsafe.Pointer(bp)))[int32(4)] = __ccgo_ts + 19238 (*(*[6]uintptr)(unsafe.Pointer(bp)))[int32(5)] = uintptr(0) (*(*TInitData)(unsafe.Pointer(bp + 72))).Fdb = db (*(*TInitData)(unsafe.Pointer(bp + 72))).FiDb = iDb (*(*TInitData)(unsafe.Pointer(bp + 72))).Frc = SQLITE_OK (*(*TInitData)(unsafe.Pointer(bp + 72))).FpzErrMsg = pzErrMsg (*(*TInitData)(unsafe.Pointer(bp + 72))).FmInitFlags = mFlags (*(*TInitData)(unsafe.Pointer(bp + 72))).FnInitRow = uint32(0) (*(*TInitData)(unsafe.Pointer(bp + 72))).FmxPage = uint32(0) _sqlite3InitCallback(tls, bp+72, int32(5), bp, uintptr(0)) *(*Tu32)(unsafe.Pointer(db + 44)) &= uint32(mask) if (*(*TInitData)(unsafe.Pointer(bp + 72))).Frc != 0 { rc = (*(*TInitData)(unsafe.Pointer(bp + 72))).Frc goto error_out } /* Create a cursor to hold the database open */ pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32 if (*TDb)(unsafe.Pointer(pDb)).FpBt == uintptr(0) { p3 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).FpSchema + 114 *(*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.Uint64FromInt64(20)/libc.Uint64FromInt64(4))) { break } _sqlite3BtreeGetMeta(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt, i+int32(1), bp+48+uintptr(i)*4) goto _4 _4: ; i++ } if (*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_ResetDatabase) != uint64(0) { libc.X__builtin___memset_chk(tls, bp+48, 0, uint64(20), libc.X__builtin_object_size(tls, bp+48, 0)) } (*TSchema)(unsafe.Pointer((*TDb)(unsafe.Pointer(pDb)).FpSchema)).Fschema_cookie = (*(*[5]int32)(unsafe.Pointer(bp + 48)))[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 + 48)))[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 + 48)))[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 + 48)))[libc.Int32FromInt32(BTREE_TEXT_ENCODING)-libc.Int32FromInt32(1)]&int32(3) != int32((*Tsqlite3)(unsafe.Pointer(db)).Fenc) { _sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+12568) 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 + 48)))[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 + 48)))[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+19310) 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 + 48)))[libc.Int32FromInt32(BTREE_FILE_FORMAT)-libc.Int32FromInt32(1)] >= int32(4) { *(*Tu64)(unsafe.Pointer(db + 48)) &= ^libc.Uint64FromInt32(SQLITE_LegacyFileFmt) } /* Read the schema information out of the schema tables */ (*(*TInitData)(unsafe.Pointer(bp + 72))).FmxPage = _sqlite3BtreeLastPage(tls, (*TDb)(unsafe.Pointer(pDb)).FpBt) zSql = _sqlite3MPrintf(tls, db, __ccgo_ts+19334, libc.VaList(bp+120, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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+72, uintptr(0)) (*Tsqlite3)(unsafe.Pointer(db)).FxAuth = xAuth if rc == SQLITE_OK { rc = (*(*TInitData)(unsafe.Pointer(bp + 72))).Frc } _sqlite3DbFree(tls, db, zSql) if rc == SQLITE_OK { _sqlite3AnalysisLoad(tls, db, iDb) } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { rc = int32(SQLITE_NOMEM) _sqlite3ResetAllSchemasOfConnection(tls, db) pDb = (*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32 } 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)*32))).FpSchema + 114 *(*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 } if !(int32((*TSchema)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32))).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 if !((*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0) { rc = _sqlite3Init(tls, db, pParse+8) 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 + 44)) |= 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 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)*32))).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. */ if pSchema != 0 { i = 0 for { if !(int32(1) != 0) { break } if (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32))).FpSchema == pSchema { break } goto _1 _1: ; i++ } } 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 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) } (*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) (*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 if _sqlite3FaultSim(tls, int32(300)) != 0 { pCleanup = uintptr(0) _sqlite3OomFault(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb) } else { pCleanup = _sqlite3DbMallocRaw(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(24)) } 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.X__builtin___memset_chk(tls, pParse+uintptr(uint64(libc.UintptrFromInt32(0)+8)), 0, uint64(libc.UintptrFromInt32(0)+232)-uint64(libc.UintptrFromInt32(0)+8), libc.X__builtin_object_size(tls, pParse+uintptr(uint64(libc.UintptrFromInt32(0)+8)), 0)) libc.X__builtin___memset_chk(tls, pParse+uintptr(uint64(libc.UintptrFromInt32(0)+288)), 0, libc.Uint64FromInt64(424)-uint64(libc.UintptrFromInt32(0)+288), libc.X__builtin_object_size(tls, pParse+uintptr(uint64(libc.UintptrFromInt32(0)+288)), 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+1634, 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(448) defer tls.Free(448) 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.X__builtin___memset_chk(tls, bp+uintptr(uint64(libc.UintptrFromInt32(0)+8)), 0, uint64(libc.UintptrFromInt32(0)+232)-uint64(libc.UintptrFromInt32(0)+8), libc.X__builtin_object_size(tls, bp+uintptr(uint64(libc.UintptrFromInt32(0)+8)), 0)) libc.X__builtin___memset_chk(tls, bp+uintptr(uint64(libc.UintptrFromInt32(0)+288)), 0, libc.Uint64FromInt64(424)-uint64(libc.UintptrFromInt32(0)+288), libc.X__builtin_object_size(tls, bp+uintptr(uint64(libc.UintptrFromInt32(0)+288)), 0)) (*(*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 { } if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { _sqlite3ErrorMsg(tls, bp, __ccgo_ts+1634, 0) v1 = libc.Int32FromInt32(SQLITE_NOMEM) rc = v1 (*Tsqlite3)(unsafe.Pointer(db)).FerrCode = v1 goto end_prepare } /* 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)*32))).FpBt if pBt != 0 { rc = _sqlite3BtreeSchemaLocked(tls, pBt) if rc != 0 { zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(i)*32))).FzDbSName _sqlite3ErrorWithMsg(tls, db, rc, __ccgo_ts+19368, libc.VaList(bp+432, zDb)) goto end_prepare } } goto _2 _2: ; i++ } } if (*Tsqlite3)(unsafe.Pointer(db)).FpDisconnect != 0 { _sqlite3VtabUnlockList(tls, db) } if nBytes >= 0 && (nBytes == 0 || int32(*(*int8)(unsafe.Pointer(zSql + uintptr(nBytes-int32(1))))) != 0) { mxLen = *(*int32)(unsafe.Pointer(db + 136 + 1*4)) if nBytes > mxLen { _sqlite3ErrorWithMsg(tls, db, int32(SQLITE_TOOBIG), __ccgo_ts+19398, 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(int64((*(*TParse)(unsafe.Pointer(bp))).FzTail)-int64(zSqlCopy)) _sqlite3DbFree(tls, db, zSqlCopy) } else { (*(*TParse)(unsafe.Pointer(bp))).FzTail = zSql + uintptr(nBytes) } } else { _sqlite3RunParser(tls, bp, zSql) } 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(int64((*(*TParse)(unsafe.Pointer(bp))).FzTail)-int64(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) } rc = (*(*TParse)(unsafe.Pointer(bp))).Frc if (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg != 0 { _sqlite3ErrorWithMsg(tls, db, rc, __ccgo_ts+3863, libc.VaList(bp+432, (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg)) _sqlite3DbFree(tls, db, (*(*TParse)(unsafe.Pointer(bp))).FzErrMsg) } else { _sqlite3Error(tls, db, rc) } } else { *(*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(142654)) } 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) 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(*(*int8)(unsafe.Pointer(z + uintptr(sz)))) != 0 || int32(*(*int8)(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(int64(*(*uintptr)(unsafe.Pointer(bp)))-int64(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) /* 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) /* 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) /* 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 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 { _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(128) defer tls.Free(128) 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(128)) pNew = v1 pAllocated = v1 if pNew == uintptr(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 + 148 *(*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(112)) } (*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 { } 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.Uint64FromInt64(21)/libc.Uint64FromInt64(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.Uint64FromInt64(21)/libc.Uint64FromInt64(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 + 11675 zSp2 = __ccgo_ts + 11675 if pB == uintptr(0) { zSp1++ } if pC == uintptr(0) { zSp2++ } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19417, libc.VaList(bp+8, pA, zSp1, pB, zSp2, pC)) jointype = int32(JT_INNER) } return jointype } /* 0123456789 123456789 123456789 123 */ var _zKeyText = [34]int8{'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 += 16 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 if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 4))&0x2000>>13)) != 0 { pResults = (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpSelect)).FpEList libc.SetBitFieldPtr16Uint32(pResults+8+uintptr(iCol)*32+16+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 */ /* Both or neither are NULL */ i = iStart for { if !(i <= iEnd) { break } iCol = _sqlite3ColumnIndex(tls, (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104))).FpTab, zCol) if iCol >= 0 && (bIgnoreHidden == 0 || libc.BoolInt32(int32((*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104))).FpTab)).FaCol+uintptr(iCol)*16)).FcolFlags)&int32(COLFLAG_HIDDEN) != 0) == 0) { if piTab != 0 { _sqlite3SrcItemColumnUsed(tls, pSrc+8+uintptr(i)*104, 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 for p != 0 { *(*Tu32)(unsafe.Pointer(p + 4)) |= joinFlag *(*int32)(unsafe.Pointer(p + 52)) = iTable if int32((*TExpr)(unsafe.Pointer(p)).Fop) == int32(TK_FUNCTION) { if *(*uintptr)(unsafe.Pointer(p + 32)) != 0 { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 32)))).FnExpr) { break } _sqlite3SetJoinExpr(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 32)) + 8 + uintptr(i)*32))).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 + 52)) == 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) { if *(*uintptr)(unsafe.Pointer(p + 32)) != 0 { i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 32)))).FnExpr) { break } _unsetJoinExpr(tls, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 32)) + 8 + uintptr(i)*32))).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*104 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 + 60 + 4))&0x400>>10)) != 0 || *(*uintptr)(unsafe.Pointer(pRight + 72)) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19447, 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)*16)).FcolFlags)&int32(COLFLAG_HIDDEN) != 0 { goto _3 } zName = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pRightTab)).FaCol + uintptr(j)*16))).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 { (*(*TIdList_item)(unsafe.Pointer(pUsing + 8 + uintptr((*TIdList)(unsafe.Pointer(pUsing)).FnId-int32(1))*16))).FzName = _sqlite3DbStrDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, zName) } } goto _3 _3: ; j++ } if pUsing != 0 { libc.SetBitFieldPtr16Uint32(pRight+60+4, libc.Uint32FromInt32(1), 10, 0x400) libc.SetBitFieldPtr16Uint32(pRight+60+4, libc.Uint32FromInt32(1), 12, 0x1000) *(*uintptr)(unsafe.Pointer(pRight + 72)) = 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 + 60 + 4))&0x400>>10)) != 0 { pList = *(*uintptr)(unsafe.Pointer(pRight + 72)) db = (*TParse)(unsafe.Pointer(pParse)).Fdb j = 0 for { if !(j < (*TIdList)(unsafe.Pointer(pList)).FnId) { break } /* Equality constraint. pE1 == pE2 */ zName1 = (*(*TIdList_item)(unsafe.Pointer(pList + 8 + uintptr(j)*16))).FzName iRightCol = _sqlite3ColumnIndex(tls, pRightTab, zName1) if iRightCol < 0 || _tableAndColumnIndex(tls, pSrc, 0, i, zName1, bp, bp+4, int32(uint32(*(*uint16)(unsafe.Pointer(pRight + 60 + 4))&0x1000>>12))) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19497, 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)))*104, *(*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 + 60 + 4))&0x1000>>12))) != 0 { if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 8 + uintptr(*(*int32)(unsafe.Pointer(bp)))*104 + 60 + 4))&0x400>>10)) == 0 || _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(*(*int32)(unsafe.Pointer(bp)))*104 + 72)), zName1) < 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+19561, 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)))*104, *(*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) if pEq != 0 { *(*Tu32)(unsafe.Pointer(pEq + 4)) |= joinType *(*int32)(unsafe.Pointer(pEq + 52)) = (*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 + 72)) != 0 { _sqlite3SetJoinExpr(tls, *(*uintptr)(unsafe.Pointer(pRight + 72)), (*TSrcItem)(unsafe.Pointer(pRight)).FiCursor, joinType) (*TSelect)(unsafe.Pointer(p)).FpWhere = _sqlite3ExprAnd(tls, pParse, (*TSelect)(unsafe.Pointer(p)).FpWhere, *(*uintptr)(unsafe.Pointer(pRight + 72))) *(*uintptr)(unsafe.Pointer(pRight + 72)) = uintptr(0) libc.SetBitFieldPtr16Uint32(pRight+60+4, libc.Uint32FromInt32(1), 11, 0x800) } } goto _1 _1: ; i++ pRight += 104 pLeft += 104 } return 0 } var _tkCoalesce = TToken{ Fz: __ccgo_ts + 7142, 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 + 56 *(*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 */ /* 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. */ if nPrefixReg != 0 { regBase = regData - nPrefixReg } else { regBase = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 56)) += nBase } 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 + 56)) += (*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.X__builtin___memset_chk(tls, (*TKeyInfo)(unsafe.Pointer(pKI)).FaSortFlags, 0, uint64((*TKeyInfo)(unsafe.Pointer(pKI)).FnKeyField), libc.X__builtin_object_size(tls, (*TKeyInfo)(unsafe.Pointer(pKI)).FaSortFlags, 0)) /* 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 + 56 *(*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 + 56)) += 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)*32))).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++ } _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) */ 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) { _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 + 56)) += nPrefixReg } (*TSelectDest)(unsafe.Pointer(pDest)).FiSdst = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 56)) += 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 + 56)) += 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)*32 + 24))) j = v5 if v5 > 0 { *(*Tu16)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpEList + 8 + uintptr(j-int32(1))*32 + 24)) = 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)*32 + 24))) > 0 { nResultCol-- regOrig = 0 } goto _6 _6: ; i++ } } (*(*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 { (*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 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) } if pSort != 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) _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 { _pushOntoSorter(tls, pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg) } else { /* 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 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)*32 + 24)))-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: 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(uint64(N+X)*(libc.Uint64FromInt64(8)+libc.Uint64FromInt32(1)) - uint64(8)) p = _sqlite3DbMallocRawNN(tls, db, uint64(40)+uint64(nExtra)) if p != 0 { (*TKeyInfo)(unsafe.Pointer(p)).FaSortFlags = p + 32 + uintptr(N+X)*8 (*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.X__builtin___memset_chk(tls, p+1*40, 0, uint64(nExtra), libc.X__builtin_object_size(tls, p+1*40, 0)) } else { return _sqlite3OomFault(tls, db) } return p } // C documentation // // /* // ** Deallocate a KeyInfo object // */ func _sqlite3KeyInfoUnref(tls *libc.TLS, p uintptr) { if p != 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 { (*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 { i = iStart pItem = pList + 8 + uintptr(iStart)*32 for { if !(i < nExpr) { break } *(*uintptr)(unsafe.Pointer(pInfo + 32 + uintptr(i-iStart)*8)) = _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 += 32 } } 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 + 19598 case int32(TK_INTERSECT): z = __ccgo_ts + 19608 case int32(TK_EXCEPT): z = __ccgo_ts + 19618 default: z = __ccgo_ts + 19625 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+19631, 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 nKey = (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr - (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat if (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat == 0 || nKey == int32(1) { if (*TSortCtx)(unsafe.Pointer(pSort)).FnOBSat != 0 { v1 = __ccgo_ts + 19654 } else { v1 = __ccgo_ts + 1664 } _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+19668, libc.VaList(bp+8, v1)) } else { _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+19699, libc.VaList(bp+8, nKey)) } 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) } } if int32((*TSortCtx)(unsafe.Pointer(pSort)).FsortFlags)&int32(SORTFLAG_UseSorter) != 0 { v3 = pParse + 56 *(*int32)(unsafe.Pointer(v3))++ v2 = *(*int32)(unsafe.Pointer(v3)) regSortOut = v2 v5 = pParse + 52 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) _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)*32 + 24))) == 0 { iCol++ } goto _6 _6: ; i++ } i = nColumn - int32(1) for { if !(i >= 0) { break } if *(*Tu16)(unsafe.Pointer(aOutEx + uintptr(i)*32 + 24)) != 0 { iRead = int32(*(*Tu16)(unsafe.Pointer(aOutEx + uintptr(i)*32 + 24))) - 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): _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: 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(144) defer tls.Free(144) var iCol, iDb, j int32 var p, p1, pS, pS1, pTab, pTabList, zType uintptr var _ /* sNC at bp+24 */ TNameContext var _ /* sNC at bp+80 */ TNameContext var _ /* zOrigCol at bp+16 */ uintptr var _ /* zOrigDb at bp+0 */ uintptr var _ /* zOrigTab at bp+8 */ uintptr _, _, _, _, _, _, _, _, _, _ = iCol, iDb, j, p, p1, pS, pS1, pTab, pTabList, zType zType = uintptr(0) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(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)*104))).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)*104))).FpTab pS = (*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(j)*104))).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 } 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)*32))).FpExpr (*(*TNameContext)(unsafe.Pointer(bp + 24))).FpSrcList = (*TSelect)(unsafe.Pointer(pS)).FpSrc (*(*TNameContext)(unsafe.Pointer(bp + 24))).FpNext = pNC (*(*TNameContext)(unsafe.Pointer(bp + 24))).FpParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse zType = _columnTypeImpl(tls, bp+24, p, bp, bp+8, bp+16) } } else { /* A real table or a CTE table */ if iCol < 0 { iCol = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) } if iCol < 0 { zType = __ccgo_ts + 1138 *(*uintptr)(unsafe.Pointer(bp + 16)) = __ccgo_ts + 17132 } else { *(*uintptr)(unsafe.Pointer(bp + 16)) = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*16))).FzCnName zType = _sqlite3ColumnType(tls, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(iCol)*16, uintptr(0)) } *(*uintptr)(unsafe.Pointer(bp + 8)) = (*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)*32))).FzDbSName } } case int32(TK_SELECT): pS1 = *(*uintptr)(unsafe.Pointer(pExpr + 32)) p1 = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS1)).FpEList + 8))).FpExpr (*(*TNameContext)(unsafe.Pointer(bp + 80))).FpSrcList = (*TSelect)(unsafe.Pointer(pS1)).FpSrc (*(*TNameContext)(unsafe.Pointer(bp + 80))).FpNext = pNC (*(*TNameContext)(unsafe.Pointer(bp + 80))).FpParse = (*TNameContext)(unsafe.Pointer(pNC)).FpParse zType = _columnTypeImpl(tls, bp+80, p1, bp, bp+8, bp+16) break } if pzOrigDb != 0 { *(*uintptr)(unsafe.Pointer(pzOrigDb)) = *(*uintptr)(unsafe.Pointer(bp)) *(*uintptr)(unsafe.Pointer(pzOrigTab)) = *(*uintptr)(unsafe.Pointer(bp + 8)) *(*uintptr)(unsafe.Pointer(pzOrigCol)) = *(*uintptr)(unsafe.Pointer(bp + 16)) } 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(80) defer tls.Free(80) var i int32 var p, v, zType uintptr var _ /* sNC at bp+0 */ TNameContext var _ /* zOrigCol at bp+72 */ uintptr var _ /* zOrigDb at bp+56 */ uintptr var _ /* zOrigTab at bp+64 */ 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)*32))).FpExpr *(*uintptr)(unsafe.Pointer(bp + 56)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 64)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 72)) = uintptr(0) zType = _columnTypeImpl(tls, bp, p, bp+56, bp+64, bp+72) /* 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 + 56)), uintptr(-libc.Int32FromInt32(1))) _sqlite3VdbeSetColName(tls, v, i, int32(COLNAME_TABLE), *(*uintptr)(unsafe.Pointer(bp + 64)), uintptr(-libc.Int32FromInt32(1))) _sqlite3VdbeSetColName(tls, v, i, int32(COLNAME_COLUMN), *(*uintptr)(unsafe.Pointer(bp + 72)), 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 (*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)*32))).FpExpr /* Agg processing has not run yet */ /* Covering idx not yet coded */ if (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*32))).FzEName != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pEList + 8 + uintptr(i)*32 + 16 + 4))&0x3>>0)) == ENAME_NAME { /* An AS clause always takes first priority */ zName = (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(i)*32))).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 = *(*uintptr)(unsafe.Pointer(p + 64)) if iCol < 0 { iCol = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) } if iCol < 0 { zCol = __ccgo_ts + 17132 } else { zCol = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*16))).FzCnName } if fullName != 0 { zName1 = uintptr(0) zName1 = _sqlite3MPrintf(tls, db, __ccgo_ts+12854, 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)*32))).FzEName if z == uintptr(0) { v2 = _sqlite3MPrintf(tls, db, __ccgo_ts+19745, 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+8 */ 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+8) if pEList != 0 { nCol = (*TExprList)(unsafe.Pointer(pEList)).FnExpr aCol = _sqlite3DbMallocZero(tls, db, uint64(16)*uint64(nCol)) if nCol > int32(32767) { nCol = int32(32767) } } else { nCol = 0 aCol = uintptr(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)*32 /* 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 + 16 + 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 } 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) && *(*uintptr)(unsafe.Pointer(pColExpr + 64)) != uintptr(0) { /* For columns use the column name name */ iCol = int32((*TExpr)(unsafe.Pointer(pColExpr)).FiColumn) pTab = *(*uintptr)(unsafe.Pointer(pColExpr + 64)) if iCol < 0 { iCol = int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) } if iCol >= 0 { v3 = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*16))).FzCnName } else { v3 = __ccgo_ts + 17132 } zName = v3 } else { if int32((*TExpr)(unsafe.Pointer(pColExpr)).Fop) == int32(TK_ID) { zName = *(*uintptr)(unsafe.Pointer(pColExpr + 8)) } else { /* Use the original text of the column expression as its name */ /* pointer comparison intended */ } } } if zName != 0 && !(_sqlite3IsTrueOrFalse(tls, zName) != 0) { zName = _sqlite3DbStrDup(tls, db, zName) } else { zName = _sqlite3MPrintf(tls, db, __ccgo_ts+19745, libc.VaList(bp+40, 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+8, zName) pCollide = v4 } if !(v5 && v4 != uintptr(0)) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pCollide + 16 + 4))&0x80>>7)) != 0 { p6 = pCol + 14 *(*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(*(*int8)(unsafe.Pointer(zName + uintptr(j))))])&int32(0x04) != 0) { break } goto _7 _7: ; j-- } if int32(*(*int8)(unsafe.Pointer(zName + uintptr(j)))) == int32(':') { nName = j } } *(*Tu32)(unsafe.Pointer(bp))++ v8 = *(*Tu32)(unsafe.Pointer(bp)) zName = _sqlite3MPrintf(tls, db, __ccgo_ts+19754, libc.VaList(bp+40, 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 + 16 + 4))&0x100>>8)) != 0 { p9 = pCol + 14 *(*Tu16)(unsafe.Pointer(p9)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p9))) | libc.Int32FromInt32(COLFLAG_NOEXPAND)) } if zName != 0 && _sqlite3HashInsert(tls, bp+8, zName, pX) == pX { _sqlite3OomFault(tls, db) } goto _1 _1: ; i++ pCol += 16 } _sqlite3HashClear(tls, bp+8) if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { j = 0 for { if !(j < i) { break } _sqlite3DbFree(tls, db, (*(*TColumn)(unsafe.Pointer(aCol + uintptr(j)*16))).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 int8) { bp := tls.Alloc(64) defer tls.Free(64) var a, db, p, pCol, pColl, pS2, zType, p4, p5 uintptr var i, j, m int32 var k, n Ti64 var _ /* sNC at bp+0 */ TNameContext _, _, _, _, _, _, _, _, _, _, _, _, _, _ = a, db, i, j, k, m, n, p, pCol, pColl, pS2, zType, p4, p5 db = (*TParse)(unsafe.Pointer(pParse)).Fdb 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.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) (*(*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 } m = 0 pS2 = pSelect *(*Tu32)(unsafe.Pointer(pTab + 48)) |= uint32(int32((*TColumn)(unsafe.Pointer(pCol)).FcolFlags) & libc.Int32FromInt32(COLFLAG_NOINSERT)) p = (*(*TExprList_item)(unsafe.Pointer(a + uintptr(i)*32))).FpExpr /* pCol->szEst = ... // Column size est for SELECT tables never used */ (*TColumn)(unsafe.Pointer(pCol)).Faffinity = _sqlite3ExprAffinity(tls, p) for int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) <= int32(SQLITE_AFF_NONE) && (*TSelect)(unsafe.Pointer(pS2)).FpNext != uintptr(0) { m |= _sqlite3ExprDataType(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS2)).FpEList + 8 + uintptr(i)*32))).FpExpr) pS2 = (*TSelect)(unsafe.Pointer(pS2)).FpNext (*TColumn)(unsafe.Pointer(pCol)).Faffinity = _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS2)).FpEList + 8 + uintptr(i)*32))).FpExpr) } 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(pS2)).FpNext != 0 || pS2 != pSelect) { pS2 = (*TSelect)(unsafe.Pointer(pS2)).FpNext for { if !(pS2 != 0) { break } m |= _sqlite3ExprDataType(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pS2)).FpEList + 8 + uintptr(i)*32))).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 = int8(SQLITE_AFF_BLOB) } else { if int32((*TColumn)(unsafe.Pointer(pCol)).Faffinity) >= int32(SQLITE_AFF_NUMERIC) && m&int32(0x02) != 0 { (*TColumn)(unsafe.Pointer(pCol)).Faffinity = int8(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 = int8(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 + 19762 } 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 { k = 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+k+int64(2))) p4 = pCol + 14 *(*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.X__builtin___memcpy_chk(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName+uintptr(n+int64(1)), zType, uint64(k+int64(1)), libc.X__builtin_object_size(tls, (*TColumn)(unsafe.Pointer(pCol)).FzCnName+uintptr(n+int64(1)), 0)) p5 = pCol + 14 *(*Tu16)(unsafe.Pointer(p5)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p5))) | libc.Int32FromInt32(COLFLAG_HASTYPE)) } } pColl = _sqlite3ExprCollSeq(tls, pParse, p) if pColl != 0 { _sqlite3ColumnSetColl(tls, db, pCol, (*TCollSeq)(unsafe.Pointer(pColl)).FzName) } goto _1 _1: ; i++ pCol += 16 } (*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 int8) (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 + 48)) &= ^libc.Uint64FromInt32(SQLITE_FullColNames) *(*Tu64)(unsafe.Pointer(db + 48)) |= 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(104)) 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) _sqlite3ColumnsFromExprList(tls, pParse, (*TSelect)(unsafe.Pointer(pSelect)).FpEList, pTab+54, pTab+8) _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 { v3 = pParse + 56 *(*int32)(unsafe.Pointer(v3))++ v2 = *(*int32)(unsafe.Pointer(v3)) v1 = v2 iLimit = v1 (*TSelect)(unsafe.Pointer(p)).FiLimit = v1 v = _sqlite3GetVdbe(tls, pParse) 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 + 56 *(*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) } /* 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)*32))).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)*32 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 + 24))).FiOrderByCol)-int32(1)) if pColl == uintptr(0) { pColl = (*Tsqlite3)(unsafe.Pointer(db)).FpDfltColl } (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*32))).FpExpr = _sqlite3ExprAddCollateString(tls, pParse, pTerm, (*TCollSeq)(unsafe.Pointer(pColl)).FzName) } *(*uintptr)(unsafe.Pointer(pRet + 32 + uintptr(i)*8)) = pColl *(*Tu8)(unsafe.Pointer((*TKeyInfo)(unsafe.Pointer(pRet)).FaSortFlags + uintptr(i))) = (*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*32))).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(48) defer tls.Free(48) 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+19766, 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)*104 + 60 + 4))&0x40>>6)) != 0 { iCurrent = (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104))).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 + 52 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 + 52 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 + 56 *(*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+19815, 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+19857, 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+19863, 0) _sqlite3Select(tls, pParse, p, bp) (*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)) for cond := true; cond; cond = int32(1) != 0 { if (*TSelect)(unsafe.Pointer(p)).FpWin != 0 { return -int32(1) } if (*TSelect)(unsafe.Pointer(p)).FpPrior == uintptr(0) { break } p = (*TSelect)(unsafe.Pointer(p)).FpPrior nRow += bShowAll } if nRow == int32(1) { v1 = __ccgo_ts + 1664 } else { v1 = __ccgo_ts + 3753 } _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+19878, 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(144) defer tls.Free(144) 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+88 */ TSelectDest var _ /* nLimit at bp+40 */ int32 var _ /* uniondest at bp+48 */ 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. */ /* Calling function guarantees this much */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb pPrior = (*TSelect)(unsafe.Pointer(p)).FpPrior *(*TSelectDest)(unsafe.Pointer(bp)) = *(*TSelectDest)(unsafe.Pointer(pDest)) v = _sqlite3GetVdbe(tls, pParse) /* The VDBE already created by calling function */ /* Create the destination temporary table if necessary */ if int32((*(*TSelectDest)(unsafe.Pointer(bp))).FeDest) == int32(SRT_EphemTab) { _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. */ 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+19901, 0) _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19916, 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 + 40)) = 0 /* Initialize to suppress harmless compiler warning */ (*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+19598, 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+40) != 0 && *(*int32)(unsafe.Pointer(bp + 40)) > 0 && int32((*TSelect)(unsafe.Pointer(p)).FnSelectRow) > int32(_sqlite3LogEst(tls, uint64(*(*int32)(unsafe.Pointer(bp + 40))))) { (*TSelect)(unsafe.Pointer(p)).FnSelectRow = _sqlite3LogEst(tls, uint64(*(*int32)(unsafe.Pointer(bp + 40)))) } 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. */ /* 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 + 52 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ unionTab = v1 addr1 = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), unionTab, 0) *(*int32)(unsafe.Pointer(p + 20)) = addr1 *(*Tu32)(unsafe.Pointer(_findRightmost(tls, p) + 4)) |= uint32(SF_UsesEphemeral) } /* Code the SELECT statements to our left */ _sqlite3SelectDestInit(tls, bp+48, priorOp, unionTab) rc = _sqlite3Select(tls, pParse, pPrior, bp+48) 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 { 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 + 48))).FeDest = op _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19935, libc.VaList(bp+136, _sqlite3SelectOpName(tls, int32((*TSelect)(unsafe.Pointer(p)).Fop)))) rc = _sqlite3Select(tls, pParse, p, bp+48) 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. */ 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: /* 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 + 52 v3 = *(*int32)(unsafe.Pointer(v4)) *(*int32)(unsafe.Pointer(v4))++ tab1 = v3 v6 = pParse + 52 v5 = *(*int32)(unsafe.Pointer(v6)) *(*int32)(unsafe.Pointer(v6))++ tab2 = v5 addr2 = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), tab1, 0) *(*int32)(unsafe.Pointer(p + 20)) = addr2 *(*Tu32)(unsafe.Pointer(_findRightmost(tls, p) + 4)) |= uint32(SF_UsesEphemeral) /* Code the SELECTs to our left into temporary table "tab1". */ _sqlite3SelectDestInit(tls, bp+88, int32(SRT_Union), tab1) rc = _sqlite3Select(tls, pParse, pPrior, bp+88) if rc != 0 { goto multi_select_end } /* Code the current SELECT into temporary table "tab2" */ addr2 = _sqlite3VdbeAddOp2(tls, v, int32(OP_OpenEphemeral), tab2, 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 + 88))).FiSDParm = tab2 _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+19935, libc.VaList(bp+136, _sqlite3SelectOpName(tls, int32((*TSelect)(unsafe.Pointer(p)).Fop)))) rc = _sqlite3Select(tls, pParse, p, bp+88) 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 } 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 */ 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 + 32 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 += 8 } 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 */ 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+19956, 0) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20002, 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) 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: _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(96) defer tls.Free(96) 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+40 */ 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 */ /* "Managed" code needs this. Ticket #3382. */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe /* 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) pOrderBy = (*TSelect)(unsafe.Pointer(p)).FpOrderBy 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 } if int32((*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem + 24))).FiOrderByCol) == i { break } goto _2 _2: ; j++ pItem += 32 } 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(&(*TExpr)(unsafe.Pointer(pNew)).Fu)) = 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)*32 + 24)) = 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(4)*uint64(nOrderBy+libc.Int32FromInt32(1))) if aPermute != 0 { *(*Tu32)(unsafe.Pointer(aPermute)) = uint32(nOrderBy) i = int32(1) pItem1 = pOrderBy + 8 for { if !(i <= nOrderBy) { break } *(*Tu32)(unsafe.Pointer(aPermute + uintptr(i)*4)) = uint32(int32((*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem1 + 24))).FiOrderByCol) - int32(1)) goto _5 _5: ; i++ pItem1 += 32 } 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 regPrev = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 56)) += nExpr + int32(1) _sqlite3VdbeAddOp2(tls, v, int32(OP_Integer), 0, regPrev) pKeyDup = _sqlite3KeyInfoAlloc(tls, db, nExpr, int32(1)) if pKeyDup != 0 { i = 0 for { if !(i < nExpr) { break } *(*uintptr)(unsafe.Pointer(pKeyDup + 32 + uintptr(i)*8)) = _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++ 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 (*TSelect)(unsafe.Pointer(pSplit)).FpPrior = uintptr(0) (*TSelect)(unsafe.Pointer(pPrior)).FpNext = uintptr(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+7840) _sqlite3ResolveOrderGroupBy(tls, pParse, pPrior, (*TSelect)(unsafe.Pointer(pPrior)).FpOrderBy, __ccgo_ts+7840) /* Compute the limit registers */ _computeLimitRegisters(tls, pParse, p, labelEnd) if (*TSelect)(unsafe.Pointer(p)).FiLimit != 0 && op == int32(TK_ALL) { v10 = pParse + 56 *(*int32)(unsafe.Pointer(v10))++ v9 = *(*int32)(unsafe.Pointer(v10)) regLimitA = v9 v12 = pParse + 56 *(*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 + 56 *(*int32)(unsafe.Pointer(v16))++ v15 = *(*int32)(unsafe.Pointer(v16)) regAddrA = v15 v18 = pParse + 56 *(*int32)(unsafe.Pointer(v18))++ v17 = *(*int32)(unsafe.Pointer(v18)) regAddrB = v17 v20 = pParse + 56 *(*int32)(unsafe.Pointer(v20))++ v19 = *(*int32)(unsafe.Pointer(v20)) regOutA = v19 v22 = pParse + 56 *(*int32)(unsafe.Pointer(v22))++ v21 = *(*int32)(unsafe.Pointer(v22)) regOutB = v21 _sqlite3SelectDestInit(tls, bp, int32(SRT_Coroutine), regAddrA) _sqlite3SelectDestInit(tls, bp+40, int32(SRT_Coroutine), regAddrB) _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+20084, libc.VaList(bp+88, _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+20095, 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+20100, 0) _sqlite3Select(tls, pParse, p, bp+40) (*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+40, 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 + 40))).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(80) defer tls.Free(80) 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 + 52)) == (*TSubstContext)(unsafe.Pointer(pSubst)).FiTable { *(*int32)(unsafe.Pointer(pExpr + 52)) = (*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) pCopy = (*(*TExprList_item)(unsafe.Pointer((*TSubstContext)(unsafe.Pointer(pSubst)).FpEList + 8 + uintptr(iColumn)*32))).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.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 0)) (*(*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 + 52)), (*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(&(*TExpr)(unsafe.Pointer(pExpr)).Fu)) = _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)*32))).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 + 5498 } 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 + 32)), int32(1)) } else { _substExprList(tls, pSubst, *(*uintptr)(unsafe.Pointer(pExpr + 32))) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { pWin = *(*uintptr)(unsafe.Pointer(pExpr + 64)) (*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)*32))).FpExpr = _substExpr(tls, pSubst, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*32))).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 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 + 60 + 4))&0x4>>2)) != 0 { _substExprList(tls, pSubst, *(*uintptr)(unsafe.Pointer(pItem + 88))) } goto _4 _4: ; i-- pItem += 104 } 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 + 40)) 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 + 80)) |= _sqlite3ExprColUsed(tls, pExpr) return WRC_Continue } func _recomputeColumnsUsed(tls *libc.TLS, pSelect uintptr, pSrcItem uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* w at bp+0 */ TWalker if (*TSrcItem)(unsafe.Pointer(pSrcItem)).FpTab == uintptr(0) { return } libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_recomputeColumnsUsedExpr) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop) *(*uintptr)(unsafe.Pointer(bp + 40)) = 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 { if !(int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 4))&0x40>>6)) != 0) || *(*int32)(unsafe.Pointer(aCsrMap + uintptr((*TSrcItem)(unsafe.Pointer(pItem)).FiCursor+int32(1))*4)) == 0 { v3 = pParse + 52 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 += 104 } } // 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 + 40)) 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+44) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_OuterON)) != uint32(0) { _renumberCursorDoMapping(tls, pWalker, pExpr+52) } 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(48) defer tls.Free(48) var _ /* w at bp+0 */ TWalker _srclistRenumberCursors(tls, pParse, aCsrMap, (*TSelect)(unsafe.Pointer(p)).FpSrc, iExcept) libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) *(*uintptr)(unsafe.Pointer(bp + 40)) = 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 int8 var ii int32 var pList, pSub1 uintptr _, _, _, _ = aff, ii, pList, pSub1 pList = (*TSelect)(unsafe.Pointer(p)).FpEList ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } aff = _sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*32))).FpExpr) pSub1 = (*TSelect)(unsafe.Pointer(p)).FpPrior for { if !(pSub1 != 0) { break } if int32(_sqlite3ExprAffinity(tls, (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(pSub1)).FpEList + 8 + uintptr(ii)*32))).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(96) defer tls.Free(96) 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+48 */ 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. */ if (*Tsqlite3)(unsafe.Pointer(db)).FdbOptFlags&uint32(libc.Int32FromInt32(SQLITE_QueryFlattener)) != uint32(0) { return 0 } pSrc = (*TSelect)(unsafe.Pointer(p)).FpSrc pSubitem = pSrc + 8 + uintptr(iFrom)*104 iParent = (*TSrcItem)(unsafe.Pointer(pSubitem)).FiCursor pSub = (*TSrcItem)(unsafe.Pointer(pSubitem)).FpSelect if (*TSelect)(unsafe.Pointer(p)).FpWin != 0 || (*TSelect)(unsafe.Pointer(pSub)).FpWin != 0 { return 0 } /* Restriction (25) */ pSubSrc = (*TSelect)(unsafe.Pointer(pSub)).FpSrc /* 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) } /* 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 */ /* 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 } 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)*32 + 24))) == 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)+libc.Int64FromInt32(1))*uint64(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) /* 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 + 148 *(*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 } 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)*104))).Ffg.Fjointype) & int32(JT_LTORJ)) 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)*104 if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 4))&0x400>>10)) != 0 { _sqlite3IdListDelete(tls, db, *(*uintptr)(unsafe.Pointer(pItem + 72))) } *(*TSrcItem)(unsafe.Pointer(pItem)) = *(*TSrcItem)(unsafe.Pointer(pSubSrc + 8 + uintptr(i)*104)) p9 = pItem + 60 *(*Tu8)(unsafe.Pointer(p9)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p9))) | int32(ltorj)) iNewParent = (*(*TSrcItem)(unsafe.Pointer(pSubSrc + 8 + uintptr(i)*104))).FiCursor libc.X__builtin___memset_chk(tls, pSubSrc+8+uintptr(i)*104, 0, uint64(104), libc.X__builtin_object_size(tls, pSubSrc+8+uintptr(i)*104, 0)) goto _8 _8: ; i++ } p10 = pSrc + 8 + uintptr(iFrom)*104 + 60 *(*Tu8)(unsafe.Pointer(p10)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p10))) & libc.Int32FromInt32(JT_LTORJ)) p11 = pSrc + 8 + uintptr(iFrom)*104 + 60 *(*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)*32 + 24)) = uint16(0) goto _12 _12: ; i++ } (*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 + 48))).FpParse = pParse (*(*TSubstContext)(unsafe.Pointer(bp + 48))).FiTable = iParent (*(*TSubstContext)(unsafe.Pointer(bp + 48))).FiNewTable = iNewParent (*(*TSubstContext)(unsafe.Pointer(bp + 48))).FisOuterJoin = isOuterJoin (*(*TSubstContext)(unsafe.Pointer(bp + 48))).FpEList = (*TSelect)(unsafe.Pointer(pSub)).FpEList (*(*TSubstContext)(unsafe.Pointer(bp + 48))).FpCList = _findLeftmostExprlist(tls, pSub) _substSelect(tls, bp+48, 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) /* 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)*104) 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 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))*8)) 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((*TWhereConst)(unsafe.Pointer(pConst)).FnConst*int32(2))*uint64(8)) 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))*8)) = pColumn *(*uintptr)(unsafe.Pointer((*TWhereConst)(unsafe.Pointer(pConst)).FapExpr + uintptr((*TWhereConst)(unsafe.Pointer(pConst)).FnConst*int32(2)-int32(1))*8)) = 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 if int32((*TExpr)(unsafe.Pointer(pRight)).Fop) == int32(TK_COLUMN) && _sqlite3ExprIsConstant(tls, (*TWhereConst)(unsafe.Pointer(pConst)).FpParse, pLeft) != 0 { _constInsert(tls, pConst, pRight, pLeft, pExpr) } if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) == int32(TK_COLUMN) && _sqlite3ExprIsConstant(tls, (*TWhereConst)(unsafe.Pointer(pConst)).FpParse, 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))*8)) 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)) (*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))*8)), 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 + 40)) 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(96) defer tls.Free(96) var nChng int32 var _ /* w at bp+40 */ 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 + 103 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.X__builtin___memset_chk(tls, bp+40, 0, uint64(48), libc.X__builtin_object_size(tls, bp+40, 0)) (*(*TWalker)(unsafe.Pointer(bp + 40))).FpParse = pParse (*(*TWalker)(unsafe.Pointer(bp + 40))).FxExprCallback = __ccgo_fp(_propagateConstantExprRewrite) (*(*TWalker)(unsafe.Pointer(bp + 40))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop) (*(*TWalker)(unsafe.Pointer(bp + 40))).FxSelectCallback2 = uintptr(0) (*(*TWalker)(unsafe.Pointer(bp + 40))).FwalkerDepth = 0 *(*uintptr)(unsafe.Pointer(bp + 40 + 40)) = bp _sqlite3WalkExpr(tls, bp+40, (*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) { 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. // ** // ** NAME AMBIGUITY // ** // ** This optimization is called the "WHERE-clause push-down optimization". // ** // ** Do not confuse this optimization with another unrelated optimization // ** with a similar name: The "MySQL push-down optimization" causes WHERE // ** clause terms that can be evaluated using only the index and without // ** reference to the table are run first, so that if they are false, // ** unnecessary table seeks are avoided. // ** // ** RULES // ** // ** 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 WHERE-clause 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(48) defer tls.Free(48) 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)*104 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 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 ii = 0 for { if !(ii < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } pColl = _sqlite3ExprCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(ii)*32))).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, int32(1)) != 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 */ if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 4))&0x8>>3)) != 0 || int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 4))&0x100>>8)) != 0 { return 0 } pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab pSub = (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect 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)*32 + 24)) if int32(iCol) > 0 { iCol-- if int32(iCol) >= int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(8)) { v3 = int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(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.Uint64FromInt64(8)*libc.Uint64FromInt32(8))-libc.Int32FromInt32(1) { v5 = libc.Uint64FromInt32(1) << j } else { v5 = libc.Uint64FromInt32(1) << (int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(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)*32))).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) pEList = *(*uintptr)(unsafe.Pointer(pFunc + 32)) 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) } zFunc = *(*uintptr)(unsafe.Pointer(pFunc + 8)) if _sqlite3StrICmp(tls, zFunc, __ccgo_ts+15991) == 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+15995) == 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 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 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 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 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) } 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 + 88)) 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+20106, libc.VaList(bp+8, zIndexedBy, 0)) (*TParse)(unsafe.Pointer(pParse)).FcheckSchema = uint8(1) return int32(SQLITE_ERROR) } *(*uintptr)(unsafe.Pointer(pFrom + 96)) = 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 + 24)) != 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)*32))).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(128)) if pNew == uintptr(0) { return int32(WRC_Abort) } libc.X__builtin___memset_chk(tls, bp, 0, uint64(16), libc.X__builtin_object_size(tls, bp, 0)) 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)) *(*Tu32)(unsafe.Pointer(p + 4)) |= uint32(SF_Converted) (*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 + 60 + 4))&0x4>>2)) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20124, 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 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 + 16 + uintptr(i)*48))).FzName) == 0 { *(*uintptr)(unsafe.Pointer(ppContext)) = p return p + 16 + uintptr(i)*48 } 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 { (*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 */ 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 + 60 + 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) } pTab = _sqlite3DbMallocZero(tls, db, uint64(104)) 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) *(*Tu32)(unsafe.Pointer(pTab + 48)) |= 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) if int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 60 + 4))&0x2>>1)) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20147, libc.VaList(bp+16, *(*uintptr)(unsafe.Pointer(pFrom + 88)))) return int32(2) } libc.SetBitFieldPtr16Uint32(pFrom+60+4, libc.Uint32FromInt32(1), 8, 0x100) *(*uintptr)(unsafe.Pointer(pFrom + 96)) = 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 i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) { break } pItem = pSrc + 8 + uintptr(i)*104 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+60+4, libc.Uint32FromInt32(1), 6, 0x40) if (*TSelect)(unsafe.Pointer(pRecTerm)).FselFlags&uint32(SF_Recursive) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20167, 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 + 52 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 + 20210 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 { (*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+20233, 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+54, pTab+8) if bMayRecursive != 0 { if (*TSelect)(unsafe.Pointer(pSel)).FselFlags&uint32(SF_Recursive) != 0 { (*TCte)(unsafe.Pointer(pCte)).FzCteErr = __ccgo_ts + 20271 } else { (*TCte)(unsafe.Pointer(pCte)).FzCteErr = __ccgo_ts + 20305 } _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) { (*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 v1 = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(104)) 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+20343, 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+54, pTab+8) (*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1)) (*TTable)(unsafe.Pointer(pTab)).FeTabType = uint8(TABTYP_VIEW) (*TTable)(unsafe.Pointer(pTab)).FnRowLogEst = int16(200) /* The usual case - do not allow ROWID on a subquery */ *(*Tu32)(unsafe.Pointer(pTab + 48)) |= 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 += 104 if int32(uint32(*(*uint16)(unsafe.Pointer(pBase + 60 + 4))&0x400>>10)) == 0 { continue } if *(*uintptr)(unsafe.Pointer(pBase + 72)) == uintptr(0) { continue } if _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pBase + 72)), 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) } 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 + 148 *(*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(64)) 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 } if (*TSrcItem)(unsafe.Pointer(pFrom)).FpTab != 0 { goto _3 } if (*TSrcItem)(unsafe.Pointer(pFrom)).FzName == uintptr(0) { pSel = (*TSrcItem)(unsafe.Pointer(pFrom)).FpSelect /* A sub-query in the FROM clause of a SELECT */ 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 } else { /* An ordinary table or view name in the FROM clause */ 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+20347, 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) } 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*32))).FpSchema { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20386, 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 + 60 + 4))&0x80>>7)) != 0 && (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).Fp != uintptr(0) && int32((*TVTable)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).Fp)).FeVtabRisk) > libc.BoolInt32((*Tsqlite3)(unsafe.Pointer(db)).Fflags&uint64(SQLITE_TrustedSchema) != uint64(0)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+15550, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName)) } } 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 + 60 + 4))&0x2>>1)) != 0 && _sqlite3IndexedByLookup(tls, pParse, pFrom) != 0 { return int32(WRC_Abort) } goto _3 _3: ; i++ pFrom += 104 } /* Process NATURAL keywords, and ON and USING clauses of joins. */ 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)*32))).FpExpr if int32((*TExpr)(unsafe.Pointer(pE)).Fop) == int32(TK_ASTERISK) { break } 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)*32))).FpExpr elistFlags |= (*TExpr)(unsafe.Pointer(pE)).Fflags pRight = (*TExpr)(unsafe.Pointer(pE)).FpRight 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)*32))).FpExpr) if pNew != 0 { (*(*TExprList_item)(unsafe.Pointer(pNew + 8 + uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*32))).FzEName = (*(*TExprList_item)(unsafe.Pointer(a + uintptr(k)*32))).FzEName libc.SetBitFieldPtr16Uint32(pNew+8+uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*32+16+4, uint32(int32(uint32(*(*uint16)(unsafe.Pointer(a + uintptr(k)*32 + 16 + 4))&0x3>>0))), 0, 0x3) (*(*TExprList_item)(unsafe.Pointer(a + uintptr(k)*32))).FzEName = uintptr(0) } (*(*TExprList_item)(unsafe.Pointer(a + uintptr(k)*32))).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) { zTName = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pE)).FpLeft + 8)) iErrOfst = *(*int32)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pE)).FpRight + 52)) } else { iErrOfst = *(*int32)(unsafe.Pointer(pE + 52)) } 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 } if int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 60 + 4))&0x2000>>13)) != 0 { pNestedFrom = (*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pFrom)).FpSelect)).FpEList } 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)*32))).FzDbSName } else { v10 = __ccgo_ts + 7002 } zSchemaName = v10 } if i+int32(1) < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc && int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 1*104 + 60 + 4))&0x400>>10)) != 0 && int32(selFlags)&int32(SF_NestedFrom) != 0 { pUsing = *(*uintptr)(unsafe.Pointer(pFrom + 1*104 + 72)) ii = 0 for { if !(ii < (*TIdList)(unsafe.Pointer(pUsing)).FnId) { break } zUName = (*(*TIdList_item)(unsafe.Pointer(pUsing + 8 + uintptr(ii)*16))).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))*32 (*TExprList_item)(unsafe.Pointer(pX)).FzEName = _sqlite3MPrintf(tls, db, __ccgo_ts+20417, libc.VaList(bp+8, zUName)) libc.SetBitFieldPtr16Uint32(pX+16+4, libc.Uint32FromInt32(ENAME_TAB), 0, 0x3) libc.SetBitFieldPtr16Uint32(pX+16+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)*16))).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)*32 + 16 + 4))&0x3>>0)) == int32(ENAME_ROWID) { goto _12 } if zTName != 0 && pNestedFrom != 0 && _sqlite3MatchEName(tls, pNestedFrom+8+uintptr(j)*32, 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)*16)).FcolFlags)&int32(COLFLAG_HIDDEN) != 0 { goto _12 } if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab1)).FaCol + uintptr(j)*16))).FcolFlags)&int32(COLFLAG_NOEXPAND) != 0 && zTName == uintptr(0) && int32(selFlags)&int32(SF_NestedFrom) == 0 { goto _12 } } tableSeen = int32(1) if i > 0 && zTName == uintptr(0) && int32(selFlags)&int32(SF_NestedFrom) == 0 { if int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 60 + 4))&0x400>>10)) != 0 && _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pFrom + 72)), 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))*32 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) { (*TExprList_item)(unsafe.Pointer(pX1)).FzEName = _sqlite3DbStrDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pNestedFrom + 8 + uintptr(j)*32))).FzEName) } else { (*TExprList_item)(unsafe.Pointer(pX1)).FzEName = _sqlite3MPrintf(tls, db, __ccgo_ts+20422, 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+16+4, uint32(v13), 0, 0x3) if int32(uint32(*(*uint16)(unsafe.Pointer(pFrom + 60 + 4))&0x400>>10)) != 0 && _sqlite3IdListIndex(tls, *(*uintptr)(unsafe.Pointer(pFrom + 72)), 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)*16))).FcolFlags)&int32(COLFLAG_NOEXPAND) != 0 { libc.SetBitFieldPtr16Uint32(pX1+16+4, libc.Uint32FromInt32(1), 8, 0x100) } } else { if longNames != 0 { (*TExprList_item)(unsafe.Pointer(pX1)).FzEName = _sqlite3MPrintf(tls, db, __ccgo_ts+12854, libc.VaList(bp+8, zTabName, zName)) libc.SetBitFieldPtr16Uint32(pX1+16+4, libc.Uint32FromInt32(ENAME_NAME), 0, 0x3) } else { (*TExprList_item)(unsafe.Pointer(pX1)).FzEName = _sqlite3DbStrDup(tls, db, zName) libc.SetBitFieldPtr16Uint32(pX1+16+4, libc.Uint32FromInt32(ENAME_NAME), 0, 0x3) } } goto _12 _12: ; j++ } goto _8 _8: ; i++ pFrom += 104 } if !(tableSeen != 0) { if zTName != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20431, libc.VaList(bp+8, zTName)) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20449, 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 + 136 + 2*4)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20469, 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(48) defer tls.Free(48) 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 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 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, int8(SQLITE_AFF_NONE)) } } goto _1 _1: ; i++ pFrom += 104 } } // 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(48) defer tls.Free(48) 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) { 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 *(*int32)(unsafe.Pointer(pNC + 40)) |= 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)*32))).FpFExpr _sqlite3ExprAnalyzeAggList(tls, pNC, *(*uintptr)(unsafe.Pointer(pExpr + 32))) if (*TExpr)(unsafe.Pointer(pExpr)).FpLeft != 0 { _sqlite3ExprAnalyzeAggList(tls, pNC, *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pExpr)).FpLeft + 32))) } if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(libc.Int32FromInt32(EP_WinFunc)) != uint32(0) { _sqlite3ExprAnalyzeAggregates(tls, pNC, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 64)))).FpFilter) } goto _1 _1: ; i++ } *(*int32)(unsafe.Pointer(pNC + 40)) &= ^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 (*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)*24))).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 } pCol = (*TAggInfo)(unsafe.Pointer(pAggInfo)).FaCol + uintptr((*TExpr)(unsafe.Pointer(pExpr)).FiAgg)*24 (*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(48) defer tls.Free(48) var i int32 var _ /* w at bp+0 */ TWalker _ = i libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*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)*32))).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) { (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 56)) += (*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 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 if *(*uintptr)(unsafe.Pointer(pE + 32)) == uintptr(0) || (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pE + 32)))).FnExpr != int32(1) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20500, 0) (*TAggInfo_func)(unsafe.Pointer(pFunc)).FiDistinct = -int32(1) } else { pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, *(*uintptr)(unsafe.Pointer(pE + 32)), 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+20551, libc.VaList(bp+8, (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFunc)).FzName)) } } if (*TAggInfo_func)(unsafe.Pointer(pFunc)).FiOBTab >= 0 { nExtra = 0 pOBList = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFExpr)).FpLeft + 32)) 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 */ nExtra += (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFExpr + 32)))).FnExpr } if (*TAggInfo_func)(unsafe.Pointer(pFunc)).FbUseSubtype != 0 { nExtra += (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFExpr + 32)))).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+20584, libc.VaList(bp+8, (*TFuncDef)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pFunc)).FpFunc)).FzName)) } goto _1 _1: ; i++ pFunc += 32 } } // 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 } pList = *(*uintptr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr + 32)) if (*TAggInfo_func)(unsafe.Pointer(pF)).FiOBTab >= 0 { /* Loop counter */ nArg = (*TExprList)(unsafe.Pointer(pList)).FnExpr regAgg = _sqlite3GetTempRange(tls, pParse, nArg) if int32((*TAggInfo_func)(unsafe.Pointer(pF)).FbOBPayload) == 0 { nKey = 0 } else { nKey = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr)).FpLeft + 32)))).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) } _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) } 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 += 32 } } // 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 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 pList = *(*uintptr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr + 32)) 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((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr + 64)))).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 + 56 *(*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 */ nArg = (*TExprList)(unsafe.Pointer(pList)).FnExpr pOBList = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer((*TAggInfo_func)(unsafe.Pointer(pF)).FpFExpr)).FpLeft + 32)) 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) /* 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 += 32 } if !(pColl != 0) { pColl = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FpDfltColl } if regHit == 0 && (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnAccumulator != 0 { v8 = pParse + 56 *(*int32)(unsafe.Pointer(v8))++ v7 = *(*int32)(unsafe.Pointer(v8)) regHit = v7 } _sqlite3VdbeAddOp4(tls, v, int32(OP_CollSeq), regHit, 0, 0, pColl, -int32(2)) } _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 += 32 } 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 } _sqlite3ExprCode(tls, pParse, (*TAggInfo_col)(unsafe.Pointer(pC)).FpCExpr, (*TAggInfo)(unsafe.Pointer(pAggInfo)).FiFirstReg+i) goto _9 _9: ; i++ pC += 24 } (*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 + 100))&0x3>>0)) == libc.Int32FromInt32(SQLITE_IDXTYPE_PRIMARYKEY)))) if bCover != 0 { v1 = __ccgo_ts + 20617 } else { v1 = __ccgo_ts + 1664 } if bCover != 0 { v2 = (*TIndex)(unsafe.Pointer(pIdx)).FzName } else { v2 = __ccgo_ts + 1664 } _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+20640, 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 + 40)) /* 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+8603) 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(48) defer tls.Free(48) var _ /* sWalker at bp+0 */ TWalker libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*TWalker)(unsafe.Pointer(bp))).FpParse = pParse (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_havingToWhereExprCb) *(*uintptr)(unsafe.Pointer(bp + 40)) = 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 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)*104 if (*TSrcItem)(unsafe.Pointer(pItem)).FpSelect == uintptr(0) { continue } if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 4))&0x20>>5)) != 0 { continue } if (*TSrcItem)(unsafe.Pointer(pItem)).FzName == uintptr(0) { continue } 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 */ if Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(pExpr + 8)), __ccgo_ts+16285) != 0 { return 0 } /* Is count() */ if *(*uintptr)(unsafe.Pointer(pExpr + 32)) != 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 */ /* 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(112)) 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)*104 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)*104 if int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 4))&0x100>>8)) != 0 { pCteUse = *(*uintptr)(unsafe.Pointer(pItem + 96)) 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*104))).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 -= 104 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(208) defer tls.Free(208) 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+72 */ TSelectDest var _ /* pMinMaxOrderBy at bp+64 */ uintptr var _ /* sDistinct at bp+0 */ TDistinctCtx var _ /* sNC at bp+112 */ TNameContext var _ /* sSort at bp+16 */ 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 + 64)) = uintptr(0) /* Flag for min/max queries */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb v = _sqlite3GetVdbe(tls, pParse) if p == uintptr(0) || (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return int32(1) } if _sqlite3AuthCheck(tls, pParse, int32(SQLITE_SELECT), uintptr(0), uintptr(0), uintptr(0)) != 0 { return int32(1) } if int32((*TSelectDest)(unsafe.Pointer(pDest)).FeDest) <= int32(SRT_DistQueue) { /* 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 } /* 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+20652, libc.VaList(bp+176, 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 { goto select_end } pTabList = (*TSelect)(unsafe.Pointer(p)).FpSrc isAgg = libc.BoolInt32((*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Aggregate) != uint32(0)) libc.X__builtin___memset_chk(tls, bp+16, 0, uint64(48), libc.X__builtin_object_size(tls, bp+16, 0)) (*(*TSortCtx)(unsafe.Pointer(bp + 16))).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)*104 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 */ /* 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 + 60 *(*Tu8)(unsafe.Pointer(p3)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p3))) & ^libc.Int32FromInt32(JT_LEFT)) } else { p4 = pItem + 60 *(*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)*104 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 + 60 *(*Tu8)(unsafe.Pointer(p6)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p6))) & ^libc.Int32FromInt32(JT_RIGHT)) } else { p7 = pI2 + 60 *(*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)*104 + 60 *(*Tu8)(unsafe.Pointer(p9)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p9))) & ^libc.Int32FromInt32(JT_LTORJ)) if int32((*(*TSrcItem)(unsafe.Pointer(pTabList + 8 + uintptr(j)*104))).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+20706, libc.VaList(bp+176, 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 + 60 + 4))&0x100>>8)) != 0 && int32((*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 96)))).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 } /* 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*104))).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 + 16))).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)*104 /* 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+1664, (*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) || (*TSrcItem)(unsafe.Pointer(pItem1)).FaddrFillSub != 0 { goto _10 } /* The code for a subquery should only be generated once. */ /* 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 + 316)) += _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 + 60 + 4))&0x100>>8)) == 0 || int32((*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem1 + 96)))).FeM10d) != M10d_Yes && (*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem1 + 96)))).FnUse < int32(2)) && _pushDownWhereTerms(tls, pParse, pSub1, (*TSelect)(unsafe.Pointer(p)).FpWhere, pTabList, i) != 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 + 56 *(*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+72, int32(SRT_Coroutine), (*TSrcItem)(unsafe.Pointer(pItem1)).FregReturn) _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+20746, libc.VaList(bp+176, pItem1)) _sqlite3Select(tls, pParse, pSub1, bp+72) (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem1)).FpTab)).FnRowLogEst = (*TSelect)(unsafe.Pointer(pSub1)).FnSelectRow libc.SetBitFieldPtr16Uint32(pItem1+60+4, libc.Uint32FromInt32(1), 5, 0x20) (*TSrcItem)(unsafe.Pointer(pItem1)).FregResult = (*(*TSelectDest)(unsafe.Pointer(bp + 72))).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 + 60 + 4))&0x100>>8)) != 0 && (*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem1 + 96)))).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 + 96)) _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 + 56 *(*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+60+4, libc.Uint32FromInt32(1), 4, 0x10) if int32(uint32(*(*uint16)(unsafe.Pointer(pItem1 + 60 + 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+72, int32(SRT_EphemTab), (*TSrcItem)(unsafe.Pointer(pItem1)).FiCursor) _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+20761, libc.VaList(bp+176, pItem1)) _sqlite3Select(tls, pParse, pSub1, bp+72) (*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 + 60 + 4))&0x100>>8)) != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pItem1 + 60 + 4))&0x8>>3)) == 0 { pCteUse1 = *(*uintptr)(unsafe.Pointer(pItem1 + 96)) (*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 + 316)) -= _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 + 16))).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 */ (*(*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 + 16))).FpOrderBy != 0 { pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, (*(*TSortCtx)(unsafe.Pointer(bp + 16))).FpOrderBy, 0, (*TExprList)(unsafe.Pointer(pEList)).FnExpr) v18 = pParse + 52 v17 = *(*int32)(unsafe.Pointer(v18)) *(*int32)(unsafe.Pointer(v18))++ (*(*TSortCtx)(unsafe.Pointer(bp + 16))).FiECursor = v17 (*(*TSortCtx)(unsafe.Pointer(bp + 16))).FaddrSortIndex = _sqlite3VdbeAddOp4(tls, v, int32(OP_OpenEphemeral), (*(*TSortCtx)(unsafe.Pointer(bp + 16))).FiECursor, (*TExprList)(unsafe.Pointer((*(*TSortCtx)(unsafe.Pointer(bp + 16))).FpOrderBy)).FnExpr+int32(1)+(*TExprList)(unsafe.Pointer(pEList)).FnExpr, 0, pKeyInfo, -int32(8)) } else { (*(*TSortCtx)(unsafe.Pointer(bp + 16))).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)*32 + 16 + 4))&0x40>>6)) == 0) { break } _sqlite3ExprDelete(tls, db, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(ii)*32))).FpExpr) _sqlite3DbFree(tls, db, (*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(ii)*32))).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)*32 + 16 + 4))&0x40>>6)) == 0 { (*TExpr)(unsafe.Pointer((*(*TExprList_item)(unsafe.Pointer(pEList + 8 + uintptr(ii)*32))).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 + 16))).FaddrSortIndex >= 0 { _sqlite3VdbeChangeOpcode(tls, v, (*(*TSortCtx)(unsafe.Pointer(bp + 16))).FaddrSortIndex, uint8(OP_SorterOpen)) p21 = bp + 16 + 36 *(*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 + 52 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) } /* Begin the database scan. */ pWInfo = _sqlite3WhereBegin(tls, pParse, pTabList, pWhere, (*(*TSortCtx)(unsafe.Pointer(bp + 16))).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 + 16))).FpOrderBy != 0 { (*(*TSortCtx)(unsafe.Pointer(bp + 16))).FnOBSat = _sqlite3WhereIsOrdered(tls, pWInfo) (*(*TSortCtx)(unsafe.Pointer(bp + 16))).FlabelOBLopt = _sqlite3WhereOrderByLimitOptLabel(tls, pWInfo) if (*(*TSortCtx)(unsafe.Pointer(bp + 16))).FnOBSat == (*TExprList)(unsafe.Pointer((*(*TSortCtx)(unsafe.Pointer(bp + 16))).FpOrderBy)).FnExpr { (*(*TSortCtx)(unsafe.Pointer(bp + 16))).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 + 16))).FaddrSortIndex >= 0 && (*(*TSortCtx)(unsafe.Pointer(bp + 16))).FpOrderBy == uintptr(0) { _sqlite3VdbeChangeToNoop(tls, v, (*(*TSortCtx)(unsafe.Pointer(bp + 16))).FaddrSortIndex) } if pWin != 0 { addrGosub = _sqlite3VdbeMakeLabel(tls, pParse) iCont = _sqlite3VdbeMakeLabel(tls, pParse) iBreak = _sqlite3VdbeMakeLabel(tls, pParse) v26 = pParse + 56 *(*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 + 16))).FlabelOBLopt = 0 _selectInnerLoop(tls, pParse, p, -int32(1), bp+16, 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+16, 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 + 24))).FiAlias = uint16(0) goto _27 _27: ; k-- pItem2 += 32 } k = (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr pItem2 = pGroupBy + 8 for { if !(k > 0) { break } (*(*struct { FiOrderByCol Tu16 FiAlias Tu16 })(unsafe.Pointer(pItem2 + 24))).FiAlias = uint16(0) goto _28 _28: ; k-- pItem2 += 32 } 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 + 16))).FpOrderBy != 0 && (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr == (*TExprList)(unsafe.Pointer((*(*TSortCtx)(unsafe.Pointer(bp + 16))).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 + 16))).FpOrderBy + 8 + uintptr(ii1)*32))).Ffg.FsortFlags) & int32(KEYINFO_ORDER_DESC)) (*(*TExprList_item)(unsafe.Pointer(pGroupBy + 8 + uintptr(ii1)*32))).Ffg.FsortFlags = sortFlags goto _29 _29: ; ii1++ } if _sqlite3ExprListCompare(tls, pGroupBy, (*(*TSortCtx)(unsafe.Pointer(bp + 16))).FpOrderBy, -int32(1)) == 0 { orderByGrp = int32(1) } } } else { (*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(56)) 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.X__builtin___memset_chk(tls, bp+112, 0, uint64(56), libc.X__builtin_object_size(tls, bp+112, 0)) (*(*TNameContext)(unsafe.Pointer(bp + 112))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp + 112))).FpSrcList = pTabList *(*uintptr)(unsafe.Pointer(bp + 112 + 16)) = 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+112, pEList) _sqlite3ExprAnalyzeAggList(tls, bp+112, (*(*TSortCtx)(unsafe.Pointer(bp + 16))).FpOrderBy) if pHaving != 0 { if pGroupBy != 0 { _havingToWhere(tls, pParse, p) pWhere = (*TSelect)(unsafe.Pointer(p)).FpWhere } _sqlite3ExprAnalyzeAggregates(tls, bp+112, 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+64) } else { minMaxFlag = uint8(WHERE_ORDERBY_NORMAL) } _analyzeAggFuncArgs(tls, pAggInfo, bp+112) 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 + 32)) != uintptr(0) { pExpr = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr + 32)) + 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 + 52 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 + 56 *(*int32)(unsafe.Pointer(v35))++ v34 = *(*int32)(unsafe.Pointer(v35)) iUseFlag = v34 v37 = pParse + 56 *(*int32)(unsafe.Pointer(v37))++ v36 = *(*int32)(unsafe.Pointer(v37)) iAbortFlag = v36 v39 = pParse + 56 *(*int32)(unsafe.Pointer(v39))++ v38 = *(*int32)(unsafe.Pointer(v39)) regOutputRow = v38 addrOutputRow = _sqlite3VdbeMakeLabel(tls, pParse) v41 = pParse + 56 *(*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 + 56)) += (*TExprList)(unsafe.Pointer(pGroupBy)).FnExpr iBMem = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 56)) += (*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+112) } _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 + 20777 } else { v44 = __ccgo_ts + 20786 } _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+19631, libc.VaList(bp+176, 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)*24))).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)*24 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 + 52 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 + 16))).FpOrderBy = uintptr(0) _sqlite3VdbeChangeToNoop(tls, v, (*(*TSortCtx)(unsafe.Pointer(bp + 16))).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)*32))).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+16, 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 + 52 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 + 60 + 4))&0x1>>0)) != 0) { pIdx = (*TTable)(unsafe.Pointer(pTab1)).FpIndex for { if !(pIdx != 0) { break } if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 100))&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) _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)*32))).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)*32))).FpFunc)).FfuncFlags&uint32(SQLITE_FUNC_NEEDCOLL) != 0 { break } goto _55 _55: ; i++ } if i == (*TAggInfo)(unsafe.Pointer(pAggInfo)).FnFunc { v57 = pParse + 56 *(*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 { pDistinct1 = *(*uintptr)(unsafe.Pointer((*(*TAggInfo_func)(unsafe.Pointer((*TAggInfo)(unsafe.Pointer(pAggInfo)).FaFunc))).FpFExpr + 32)) 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. */ _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. */ pWInfo = _sqlite3WhereBegin(tls, pParse, pTabList, pWhere, *(*uintptr)(unsafe.Pointer(bp + 64)), 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 + 16))).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+20777) } /* 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 + 16))).FpOrderBy != 0 { _generateSortTail(tls, pParse, p, bp+16, (*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: ; _sqlite3ExprListDelete(tls, db, *(*uintptr)(unsafe.Pointer(bp + 64))) _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(8)*uint64((*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+3863, libc.VaList(bp+8, *(*uintptr)(unsafe.Pointer(colv + uintptr(i)*8)))) if z == uintptr(0) { goto malloc_failed } v3 = p + 28 v2 = *(*Tu32)(unsafe.Pointer(v3)) *(*Tu32)(unsafe.Pointer(v3))++ *(*uintptr)(unsafe.Pointer((*TTabResult)(unsafe.Pointer(p)).FazResult + uintptr(v2)*8)) = 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+20795, 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)*8)) == uintptr(0) { z = uintptr(0) } else { n = _sqlite3Strlen30(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*8))) + int32(1) z = Xsqlite3_malloc64(tls, uint64(n)) if z == uintptr(0) { goto malloc_failed } libc.X__builtin___memcpy_chk(tls, z, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*8)), uint64(n), libc.X__builtin_object_size(tls, z, 0)) } v6 = p + 28 v5 = *(*Tu32)(unsafe.Pointer(v6)) *(*Tu32)(unsafe.Pointer(v6))++ *(*uintptr)(unsafe.Pointer((*TTabResult)(unsafe.Pointer(p)).FazResult + uintptr(v5)*8)) = 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(64) defer tls.Free(64) 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(8)*uint64((*(*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) *(*uintptr)(unsafe.Pointer((*(*TTabResult)(unsafe.Pointer(bp))).FazResult)) = uintptr(int64((*(*TTabResult)(unsafe.Pointer(bp))).FnData)) if rc&int32(0xff) == int32(SQLITE_ABORT) { Xsqlite3_free_table(tls, (*(*TTabResult)(unsafe.Pointer(bp))).FazResult+1*8) 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+3863, libc.VaList(bp+48, (*(*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*8) return rc } if (*(*TTabResult)(unsafe.Pointer(bp))).FnAlloc > (*(*TTabResult)(unsafe.Pointer(bp))).FnData { azNew = _sqlite3Realloc(tls, (*(*TTabResult)(unsafe.Pointer(bp))).FazResult, uint64(8)*uint64((*(*TTabResult)(unsafe.Pointer(bp))).FnData)) if azNew == uintptr(0) { Xsqlite3_free_table(tls, (*(*TTabResult)(unsafe.Pointer(bp))).FazResult+1*8) (*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*8 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 -= 8 n = int32(int64(*(*uintptr)(unsafe.Pointer(azResult)))) i = int32(1) for { if !(i < n) { break } if *(*uintptr)(unsafe.Pointer(azResult + uintptr(i)*8)) != 0 { Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(azResult + uintptr(i)*8))) } 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 */ pTmpSchema = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + 1*32))).FpSchema p = (*THash)(unsafe.Pointer(pTmpSchema + 56)).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) { (*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(128) defer tls.Free(128) 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+8 */ 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 */ /* pName1->z might be NULL, but not pName1 itself */ 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+20860, 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*32))).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 } _sqlite3FixInit(tls, bp+8, pParse, iDb, __ccgo_ts+20906, *(*uintptr)(unsafe.Pointer(bp))) if _sqlite3FixSrcList(tls, bp+8, 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+20914, 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+20955, 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) { goto trigger_cleanup } if _sqlite3CheckObjectName(tls, pParse, zName, __ccgo_ts+20906, (*TTable)(unsafe.Pointer(pTab)).FzName) != 0 { goto trigger_cleanup } 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)*32))).FpSchema+56, zName) != 0 { if !(noErr != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+20995, libc.VaList(bp+112, *(*uintptr)(unsafe.Pointer(bp)))) } else { _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+6937, int32(7)) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21021, 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 + 21059 } else { v1 = __ccgo_ts + 21066 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21072, libc.VaList(bp+112, 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+21109, libc.VaList(bp+112, 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)*32))).FzDbSName if isTemp != 0 { v2 = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).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 + 6945 } else { v3 = __ccgo_ts + 6478 } 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(72)) 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)*32))).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) (*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 { } 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+192+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(160) defer tls.Free(160) var db, pHash, pLink, pStep, pTab, pTrig, v, z, zName uintptr var iDb int32 var _ /* nameToken at bp+96 */ 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+96, (*TTrigger)(unsafe.Pointer(pTrig)).FzName) _sqlite3FixInit(tls, bp, pParse, iDb, __ccgo_ts+20906, bp+96) 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) { (*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+21155, libc.VaList(bp+120, (*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+21203, libc.VaList(bp+120, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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+21278, libc.VaList(bp+120, zName)), uint16(0)) } } if (*Tsqlite3)(unsafe.Pointer(db)).Finit1.Fbusy != 0 { pLink = pTrig pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FpSchema + 56 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) (*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) _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 !(*(*int8)(unsafe.Pointer(z + uintptr(i))) != 0) { break } if int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(z + uintptr(i))))])&int32(0x01) != 0 { *(*int8)(unsafe.Pointer(z + uintptr(i))) = int8(' ') } 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(96)) 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(96)+uint64((*TToken)(unsafe.Pointer(pName)).Fn)+uint64(1)) if pTriggerStep != 0 { z = pTriggerStep + 1*96 libc.X__builtin___memcpy_chk(tls, z, (*TToken)(unsafe.Pointer(pName)).Fz, uint64((*TToken)(unsafe.Pointer(pName)).Fn), libc.X__builtin_object_size(tls, z, 0)) _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 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 } zDb = (*(*TSrcItem)(unsafe.Pointer(pName + 8))).FzDatabase zName = (*(*TSrcItem)(unsafe.Pointer(pName + 8))).FzName 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 } pTrigger = _sqlite3HashFind(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(j)*32))).FpSchema+56, zName) if pTrigger != 0 { break } goto _1 _1: ; i++ } if !(pTrigger != 0) { if !(noErr != 0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21307, 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) pTable = _tableOfTrigger(tls, pTrigger) if pTable != 0 { code = int32(SQLITE_DROP_TRIGGER) zDb = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FzDbSName if libc.Bool(!(libc.Int32FromInt32(OMIT_TEMPDB) != 0)) && iDb == int32(1) { v1 = __ccgo_ts + 6945 } else { v1 = __ccgo_ts + 6478 } 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+21327, libc.VaList(bp+8, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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 pHash = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FpSchema + 56 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 + 88 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)) + 64 } } } _sqlite3DeleteTrigger(tls, db, pTrigger) *(*Tu32)(unsafe.Pointer(db + 44)) |= 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)*32))).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*32))).FpSchema == uintptr(0) { return 0 } if (*THash)(unsafe.Pointer((*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).FpSchema+56)).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) 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. */ (*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 + 21389 } else { v1 = __ccgo_ts + 21396 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21403, 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) { 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)) 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*32))).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) { 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 } if int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pTerm)).FpRight)).Fop) != int32(TK_ASTERISK) { return 0 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21451, 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)*32))).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)*16)).FcolFlags)&int32(COLFLAG_HIDDEN) != 0 { goto _2 } pNewExpr = _sqlite3Expr(tls, db, int32(TK_ID), (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(jj)*16))).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))*32 (*TExprList_item)(unsafe.Pointer(pItem)).FzEName = _sqlite3DbStrDup(tls, db, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(jj)*16))).FzCnName) libc.SetBitFieldPtr16Uint32(pItem+16+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)*32))).FzEName != uintptr(0) { pItem1 = pNew + 8 + uintptr((*TExprList)(unsafe.Pointer(pNew)).FnExpr-int32(1))*32 (*TExprList_item)(unsafe.Pointer(pItem1)).FzEName = _sqlite3DbStrDup(tls, db, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*32))).FzEName) libc.SetBitFieldPtr16Uint32(pItem1+16+4, uint32(int32(uint32(*(*uint16)(unsafe.Pointer(pList + 8 + uintptr(i)*32 + 16 + 4))&0x3>>0))), 0, 0x3) } } goto _1 _1: ; i++ } return pNew } // C documentation // // /* If the Expr node is a subquery or an EXISTS operator or an IN operator that // ** uses a subquery, and if the subquery is SF_Correlated, then mark the // ** expression as EP_VarSelect. // */ func _sqlite3ReturningSubqueryVarSelect(tls *libc.TLS, NotUsed uintptr, pExpr uintptr) (r int32) { _ = NotUsed if (*TExpr)(unsafe.Pointer(pExpr)).Fflags&uint32(EP_xIsSelect) != uint32(0) && (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).FselFlags&uint32(SF_Correlated) != uint32(0) { *(*Tu32)(unsafe.Pointer(pExpr + 4)) |= uint32(libc.Int32FromInt32(EP_VarSelect)) } return WRC_Continue } // C documentation // // /* // ** If the SELECT references the table pWalker->u.pTab, then do two things: // ** // ** (1) Mark the SELECT as as SF_Correlated. // ** (2) Set pWalker->eCode to non-zero so that the caller will know // ** that (1) has happened. // */ func _sqlite3ReturningSubqueryCorrelated(tls *libc.TLS, pWalker uintptr, pSelect uintptr) (r int32) { var i int32 var pSrc uintptr _, _ = i, pSrc pSrc = (*TSelect)(unsafe.Pointer(pSelect)).FpSrc i = 0 for { if !(i < (*TSrcList)(unsafe.Pointer(pSrc)).FnSrc) { break } if (*(*TSrcItem)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104))).FpTab == *(*uintptr)(unsafe.Pointer(pWalker + 40)) { *(*Tu32)(unsafe.Pointer(pSelect + 4)) |= uint32(SF_Correlated) (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(1) break } goto _1 _1: ; i++ } return WRC_Continue } // C documentation // // /* // ** Scan the expression list that is the argument to RETURNING looking // ** for subqueries that depend on the table which is being modified in the // ** statement that is hosting the RETURNING clause (pTab). Mark all such // ** subqueries as SF_Correlated. If the subqueries are part of an // ** expression, mark the expression as EP_VarSelect. // ** // ** https://sqlite.org/forum/forumpost/2c83569ce8945d39 // */ func _sqlite3ProcessReturningSubqueries(tls *libc.TLS, pEList uintptr, pTab uintptr) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* w at bp+0 */ TWalker libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_sqlite3ExprWalkNoop) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_sqlite3ReturningSubqueryCorrelated) *(*uintptr)(unsafe.Pointer(bp + 40)) = pTab _sqlite3WalkExprList(tls, bp, pEList) if (*(*TWalker)(unsafe.Pointer(bp))).FeCode != 0 { (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_sqlite3ReturningSubqueryVarSelect) (*(*TWalker)(unsafe.Pointer(bp))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop) _sqlite3WalkExprList(tls, bp, pEList) } } // 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(304) defer tls.Free(304) var db, pCol, pNew, pReturning, v, v2 uintptr var i, nCol, reg, v1 int32 var _ /* sFrom at bp+128 */ TSrcList var _ /* sNC at bp+240 */ 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 if !((*TParse)(unsafe.Pointer(pParse)).FbReturning != 0) { /* This RETURNING trigger must be for a different statement as ** this statement lacks a RETURNING clause. */ return } pReturning = *(*uintptr)(unsafe.Pointer(pParse + 208)) if pTrigger != pReturning+16 { /* This RETURNING trigger is for a different statement */ return } libc.X__builtin___memset_chk(tls, bp, 0, uint64(128), libc.X__builtin_object_size(tls, bp, 0)) libc.X__builtin___memset_chk(tls, bp+128, 0, uint64(112), libc.X__builtin_object_size(tls, bp+128, 0)) (*(*TSelect)(unsafe.Pointer(bp))).FpEList = _sqlite3ExprListDup(tls, db, (*TReturning)(unsafe.Pointer(pReturning)).FpReturnEL, 0) (*(*TSelect)(unsafe.Pointer(bp))).FpSrc = bp + 128 (*(*TSrcList)(unsafe.Pointer(bp + 128))).FnSrc = int32(1) (*(*TSrcItem)(unsafe.Pointer(bp + 128 + 8))).FpTab = pTab (*(*TSrcItem)(unsafe.Pointer(bp + 128 + 8))).FzName = (*TTable)(unsafe.Pointer(pTab)).FzName /* tag-20240424-1 */ (*(*TSrcItem)(unsafe.Pointer(bp + 128 + 8))).FiCursor = -int32(1) _sqlite3SelectPrep(tls, pParse, bp, uintptr(0)) if (*TParse)(unsafe.Pointer(pParse)).FnErr == 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.X__builtin___memset_chk(tls, bp+240, 0, uint64(56), libc.X__builtin_object_size(tls, bp+240, 0)) if (*TReturning)(unsafe.Pointer(pReturning)).FnRetCol == 0 { (*TReturning)(unsafe.Pointer(pReturning)).FnRetCol = (*TExprList)(unsafe.Pointer(pNew)).FnExpr v2 = pParse + 52 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ (*TReturning)(unsafe.Pointer(pReturning)).FiRetCur = v1 } (*(*TNameContext)(unsafe.Pointer(bp + 240))).FpParse = pParse *(*int32)(unsafe.Pointer(bp + 240 + 16)) = regIn (*(*TNameContext)(unsafe.Pointer(bp + 240))).FncFlags = int32(NC_UBaseReg) (*TParse)(unsafe.Pointer(pParse)).FeTriggerOp = (*TTrigger)(unsafe.Pointer(pTrigger)).Fop (*TParse)(unsafe.Pointer(pParse)).FpTriggerTab = pTab if _sqlite3ResolveExprListNames(tls, bp+240, pNew) == SQLITE_OK && !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) { nCol = (*TExprList)(unsafe.Pointer(pNew)).FnExpr reg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) _sqlite3ProcessReturningSubqueries(tls, pNew, pTab) *(*int32)(unsafe.Pointer(pParse + 56)) += 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)*32))).FpExpr /* 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(64) defer tls.Free(64) 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 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) if (*TTriggerStep)(unsafe.Pointer(pStep)).FzSpan != 0 { _sqlite3VdbeAddOp4(tls, v, int32(OP_Trace), int32(0x7fffffff), int32(1), 0, _sqlite3MPrintf(tls, db, __ccgo_ts+6675, libc.VaList(bp+48, (*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: 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) { 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(496) defer tls.Free(496) var db, pPrg, pProgram, pTop, pWhen, v, v1, v2 uintptr var iEndTrigger int32 var _ /* sNC at bp+0 */ TNameContext var _ /* sSubParse at bp+56 */ 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 */ /* 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(40)) 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(48)) 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 + 28)) = uint32(0xffffffff) *(*Tu32)(unsafe.Pointer(pPrg + 28 + 1*4)) = uint32(0xffffffff) /* Allocate and populate a new Parse context to use for coding the ** trigger sub-program. */ _sqlite3ParseObjectInit(tls, bp+56, db) libc.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) (*(*TNameContext)(unsafe.Pointer(bp))).FpParse = bp + 56 (*(*TParse)(unsafe.Pointer(bp + 56))).FpTriggerTab = pTab (*(*TParse)(unsafe.Pointer(bp + 56))).FpToplevel = pTop (*(*TParse)(unsafe.Pointer(bp + 56))).FzAuthContext = (*TTrigger)(unsafe.Pointer(pTrigger)).FzName (*(*TParse)(unsafe.Pointer(bp + 56))).FeTriggerOp = (*TTrigger)(unsafe.Pointer(pTrigger)).Fop (*(*TParse)(unsafe.Pointer(bp + 56))).FnQueryLoop = (*TParse)(unsafe.Pointer(pParse)).FnQueryLoop (*(*TParse)(unsafe.Pointer(bp + 56))).FprepFlags = (*TParse)(unsafe.Pointer(pParse)).FprepFlags v = _sqlite3GetVdbe(tls, bp+56) if v != 0 { if (*TTrigger)(unsafe.Pointer(pTrigger)).FzName != 0 { _sqlite3VdbeChangeP4(tls, v, -int32(1), _sqlite3MPrintf(tls, db, __ccgo_ts+21493, libc.VaList(bp+488, (*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+56) _sqlite3ExprIfFalse(tls, bp+56, pWhen, iEndTrigger, int32(SQLITE_JUMPIFNULL)) } _sqlite3ExprDelete(tls, db, pWhen) } /* Code the trigger program into the sub-vdbe. */ _codeTriggerProgram(tls, bp+56, (*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+56) if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 { (*TSubProgram)(unsafe.Pointer(pProgram)).FaOp = _sqlite3VdbeTakeOpArray(tls, v, pProgram+8, pTop+144) } (*TSubProgram)(unsafe.Pointer(pProgram)).FnMem = (*(*TParse)(unsafe.Pointer(bp + 56))).FnMem (*TSubProgram)(unsafe.Pointer(pProgram)).FnCsr = (*(*TParse)(unsafe.Pointer(bp + 56))).FnTab (*TSubProgram)(unsafe.Pointer(pProgram)).Ftoken = pTrigger *(*Tu32)(unsafe.Pointer(pPrg + 28)) = (*(*TParse)(unsafe.Pointer(bp + 56))).Foldmask *(*Tu32)(unsafe.Pointer(pPrg + 28 + 1*4)) = (*(*TParse)(unsafe.Pointer(bp + 56))).Fnewmask _sqlite3VdbeDelete(tls, v) } else { _transferParseError(tls, pParse, bp+56) } _sqlite3ParseObjectReset(tls, bp+56) 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 /* 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) /* 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 + 56 *(*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 */ 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. */ /* 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) 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 + 28 + 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 pCol = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(i)*16 if (*TColumn)(unsafe.Pointer(pCol)).FiDflt != 0 { *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) enc = (*Tsqlite3)(unsafe.Pointer(_sqlite3VdbeDb(tls, v))).Fenc _sqlite3ValueFromExpr(tls, _sqlite3VdbeDb(tls, v), _sqlite3ColumnExpr(tls, pTab, pCol), enc, uint8((*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)) /* Cannot index rowid */ if int32(iIdxCol) >= 0 { return libc.BoolInt32(*(*int32)(unsafe.Pointer(aXRef + uintptr(iIdxCol)*4)) >= 0) } return _sqlite3ExprReferencesUpdatedColumn(tls, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(iCol)*32))).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(48) defer tls.Free(48) 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) if pSrc != 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))) } } 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)*32))).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(128) defer tls.Free(128) 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+80 */ [2]int32 var _ /* bReplace at bp+88 */ int32 var _ /* iNotUsed1 at bp+92 */ int32 var _ /* iNotUsed2 at bp+96 */ int32 var _ /* sContext at bp+0 */ TAuthContext var _ /* sNC at bp+16 */ TNameContext var _ /* tmask at bp+72 */ 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 + 88)) = 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.X__builtin___memset_chk(tls, bp, 0, uint64(16), libc.X__builtin_object_size(tls, bp, 0)) db = (*TParse)(unsafe.Pointer(pParse)).Fdb if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { goto update_cleanup } /* 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+72) isView = libc.BoolInt32(int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VIEW)) /* 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 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 + 52 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(4)*uint64(int32((*TTable)(unsafe.Pointer(pTab)).FnCol)+nIdx+libc.Int32FromInt32(1))+uint64(nIdx)+uint64(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.X__builtin___memset_chk(tls, aToOpen, int32(1), uint64(nIdx+int32(1)), libc.X__builtin_object_size(tls, aToOpen, 0)) *(*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.X__builtin___memset_chk(tls, bp+16, 0, uint64(56), libc.X__builtin_object_size(tls, bp+16, 0)) (*(*TNameContext)(unsafe.Pointer(bp + 16))).FpParse = pParse (*(*TNameContext)(unsafe.Pointer(bp + 16))).FpSrcList = pTabList *(*uintptr)(unsafe.Pointer(bp + 16 + 16)) = pUpsert (*(*TNameContext)(unsafe.Pointer(bp + 16))).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)*32))).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+16, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*32))).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)*16))).FhName) == int32(hCol) && _sqlite3StrICmp(tls, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*16))).FzCnName, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*32))).FzEName) == 0 { if j == int32((*TTable)(unsafe.Pointer(pTab)).FiPKey) { chngRowid = uint8(1) pRowidExpr = (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*32))).FpExpr iRowidExpr = i } else { if pPk != 0 && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*16))).FcolFlags)&int32(COLFLAG_PRIMKEY) != 0 { chngPk = uint8(1) } else { if int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*16))).FcolFlags)&int32(COLFLAG_GENERATED) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21507, libc.VaList(bp+112, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*16))).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)*32))).FzEName) != 0 { j = -int32(1) chngRowid = uint8(1) pRowidExpr = (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*32))).FpExpr iRowidExpr = i } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21543, libc.VaList(bp+112, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(i)*32))).FzEName)) (*TParse)(unsafe.Pointer(pParse)).FcheckSchema = uint8(1) goto update_cleanup } } if j < 0 { v11 = __ccgo_ts + 8378 } else { v11 = (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*16))).FzCnName } rc = _sqlite3AuthCheck(tls, pParse, int32(SQLITE_UPDATE), (*TTable)(unsafe.Pointer(pTab)).FzName, v11, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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++ } 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)*16))).FcolFlags)&int32(COLFLAG_GENERATED) == 0 { goto _12 } if _sqlite3ExprReferencesUpdatedColumn(tls, _sqlite3ColumnExpr(tls, pTab, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(i)*16), 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 + 88)) = 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 + 56 *(*int32)(unsafe.Pointer(v16))++ v15 = *(*int32)(unsafe.Pointer(v16)) reg = v15 *(*int32)(unsafe.Pointer(pParse + 56)) += 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 + 56 *(*int32)(unsafe.Pointer(v19))++ v18 = *(*int32)(unsafe.Pointer(v19)) reg = v18 *(*int32)(unsafe.Pointer(pParse + 56)) += int32((*TIndex)(unsafe.Pointer(pIdx)).FnColumn) if onError == int32(OE_Default) && int32((*TIndex)(unsafe.Pointer(pIdx)).FonError) == int32(OE_Replace) { *(*int32)(unsafe.Pointer(bp + 88)) = 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 + 56 *(*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 + 88)) != 0 { /* If REPLACE conflict resolution might be invoked, open cursors on all ** indexes in case they are needed to delete records. */ libc.X__builtin___memset_chk(tls, aToOpen, int32(1), uint64(nIdx+int32(1)), libc.X__builtin_object_size(tls, aToOpen, 0)) } 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. */ regRowSet = *(*int32)(unsafe.Pointer(aRegIdx + uintptr(nAllIdx)*4)) v24 = pParse + 56 *(*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 + 56)) += int32((*TTable)(unsafe.Pointer(pTab)).FnCol) } if chngKey != 0 || pTrigger != 0 || hasFK != 0 { v26 = pParse + 56 *(*int32)(unsafe.Pointer(v26))++ v25 = *(*int32)(unsafe.Pointer(v26)) regNewRowid = v25 } regNew = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 56)) += 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+16, 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+80) bFinishSeek = _sqlite3WhereUsesDeferredSeek(tls, pWInfo) if eOnePass != int32(ONEPASS_SINGLE) { _sqlite3MultiWrite(tls, pParse) if eOnePass == int32(ONEPASS_MULTI) { iCur = (*(*[2]int32)(unsafe.Pointer(bp + 80)))[int32(1)] if iCur >= 0 && iCur != iDataCur && *(*Tu8)(unsafe.Pointer(aToOpen + uintptr(iCur-iBaseCur))) != 0 { eOnePass = ONEPASS_OFF } } } } 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 + 56 *(*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 } _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 + 92)) = 0 *(*int32)(unsafe.Pointer(bp + 96)) = 0 /* Open every index that needs updating. */ if eOnePass != ONEPASS_OFF { if (*(*[2]int32)(unsafe.Pointer(bp + 80)))[0] >= 0 { *(*Tu8)(unsafe.Pointer(aToOpen + uintptr((*(*[2]int32)(unsafe.Pointer(bp + 80)))[0]-iBaseCur))) = uint8(0) } if (*(*[2]int32)(unsafe.Pointer(bp + 80)))[int32(1)] >= 0 { *(*Tu8)(unsafe.Pointer(aToOpen + uintptr((*(*[2]int32)(unsafe.Pointer(bp + 80)))[int32(1)]-iBaseCur))) = uint8(0) } } if eOnePass == int32(ONEPASS_MULTI) && nIdx-libc.BoolInt32((*(*[2]int32)(unsafe.Pointer(bp + 80)))[int32(1)] >= 0) > 0 { addrOnce = _sqlite3VdbeAddOp0(tls, v, int32(OP_Once)) } _sqlite3OpenTableAndIndices(tls, pParse, pTab, int32(OP_OpenWrite), uint8(0), iBaseCur, aToOpen, bp+92, bp+96) if addrOnce != 0 { _sqlite3VdbeJumpHereOrPopInst(tls, v, addrOnce) } } /* Top of the update loop */ if eOnePass != ONEPASS_OFF { if (*(*[2]int32)(unsafe.Pointer(bp + 80)))[0] != iDataCur && (*(*[2]int32)(unsafe.Pointer(bp + 80)))[int32(1)] != iDataCur { _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. */ if chngRowid != 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)*16))).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 _sqlite3VdbeAddOp3(tls, v, int32(OP_Column), iEph, nOff+j, k) } else { _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pChanges + 8 + uintptr(j)*32))).FpExpr, k) } } else { if 0 == *(*int32)(unsafe.Pointer(bp + 72))&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) { _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+21562) } 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. */ v2 = pParse + 52 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 + 56)) += 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) 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)))*32))).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)))*32))).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 + 56 *(*int32)(unsafe.Pointer(v5))++ v4 = *(*int32)(unsafe.Pointer(v5)) regRec = v4 v7 = pParse + 56 *(*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 } 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)))*32))).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) 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 */ 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(88)) 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(240) defer tls.Free(240) var iCursor, ii, jj, nClause, nn, rc int32 var pExpr, pIdx, pTab, pTarget, pTerm, v2 uintptr var v3 bool var _ /* sCol at bp+56 */ [2]TExpr var _ /* sNC at bp+0 */ TNameContext var _ /* zWhich at bp+200 */ [16]int8 _, _, _, _, _, _, _, _, _, _, _, _, _ = 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 */ /* Resolve all symbolic names in the conflict-target clause, which ** includes both the list of columns and the optional partial-index ** WHERE clause. */ libc.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) (*(*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 */ 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.X__builtin___memset_chk(tls, bp+56, 0, uint64(144), libc.X__builtin_object_size(tls, bp+56, 0)) (*(*[2]TExpr)(unsafe.Pointer(bp + 56)))[0].Fop = uint8(TK_COLLATE) (*(*[2]TExpr)(unsafe.Pointer(bp + 56)))[0].FpLeft = bp + 56 + 1*72 (*(*[2]TExpr)(unsafe.Pointer(bp + 56)))[int32(1)].Fop = uint8(TK_COLUMN) (*(*[2]TExpr)(unsafe.Pointer(bp + 56)))[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 + 56 + 8)) = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(ii)*8)) if int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(ii)*2))) == -int32(2) { pExpr = (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(ii)*32))).FpExpr if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_COLLATE) { (*(*[2]TExpr)(unsafe.Pointer(bp + 56)))[0].FpLeft = pExpr pExpr = bp + 56 } } else { (*(*[2]TExpr)(unsafe.Pointer(bp + 56)))[0].FpLeft = bp + 56 + 1*72 (*(*[2]TExpr)(unsafe.Pointer(bp + 56)))[int32(1)].FiColumn = *(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaiColumn + uintptr(ii)*2)) pExpr = bp + 56 } jj = 0 for { if !(jj < nn) { break } if _sqlite3ExprCompare(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer(pTarget + 8 + uintptr(jj)*32))).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]int8)(unsafe.Pointer(bp + 200)))[0] = 0 } else { Xsqlite3_snprintf(tls, int32(16), bp+200, __ccgo_ts+21575, libc.VaList(bp+224, nClause+int32(1))) } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+21579, libc.VaList(bp+224, bp+200)) 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 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 + 56)) += nPk i = 0 for { if !(i < nPk) { break } 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+12943, -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)*16))).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) /* 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+21652, uint64(3)) == 0 || libc.Xstrncmp(tls, zSubSql, __ccgo_ts+21656, uint64(3)) == 0) { rc = _execSql(tls, db, pzErrMsg, zSubSql) if rc != SQLITE_OK { break } } } 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 + 56 *(*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+21660) return int32(SQLITE_ERROR) /* IMP: R-12218-18073 */ } if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive > int32(1) { _sqlite3SetString(tls, pzErrMsg, db, __ccgo_ts+21700) 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+21743) return int32(SQLITE_ERROR) } zOut = Xsqlite3_value_text(tls, pOut) *(*uint32)(unsafe.Pointer(db + 76)) &= uint32(^libc.Int32FromInt32(SQLITE_OPEN_READONLY)) *(*uint32)(unsafe.Pointer(db + 76)) |= uint32(libc.Int32FromInt32(SQLITE_OPEN_CREATE) | libc.Int32FromInt32(SQLITE_OPEN_READWRITE)) } else { zOut = __ccgo_ts + 1664 } /* 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 + 48)) |= uint64(libc.Int32FromInt32(SQLITE_WriteSchema) | libc.Int32FromInt32(SQLITE_IgnoreChecks)) *(*Tu32)(unsafe.Pointer(db + 44)) |= uint32(libc.Int32FromInt32(DBFLAG_PreferBuiltin) | libc.Int32FromInt32(DBFLAG_Vacuum)) *(*Tu64)(unsafe.Pointer(db + 48)) &= ^(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+21784) goto end_of_vacuum } *(*Tu32)(unsafe.Pointer(db + 44)) |= 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)*32))).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)*32))).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+15320) 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+21811, libc.VaList(bp+24, zDbMain)) if rc != SQLITE_OK { goto end_of_vacuum } rc = _execSqlF(tls, db, pzErrMsg, __ccgo_ts+21919, 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+21973, libc.VaList(bp+24, zDbMain)) *(*Tu32)(unsafe.Pointer(db + 44)) &= 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+22124, libc.VaList(bp+24, zDbMain)) if rc != 0 { goto end_of_vacuum } /* Copy Btree meta values */ i = 0 for { if !(i < int32(libc.Uint64FromInt64(10)/libc.Uint64FromInt64(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)) } 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(48)+uint64(nName)+uint64(1)) if pMod == uintptr(0) { _sqlite3OomFault(tls, db) return uintptr(0) } zCopy = pMod + 1*48 libc.X__builtin___memcpy_chk(tls, zCopy, zName, uint64(nName+int32(1)), libc.X__builtin_object_size(tls, zCopy, 0)) (*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+576, 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 + 576)).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)*8)) != uintptr(0) && libc.Xstrcmp(tls, *(*uintptr)(unsafe.Pointer(azNames + uintptr(ii)*8)), (*TModule)(unsafe.Pointer(pMod)).FzName) != 0) { break } goto _2 _2: ; ii++ } if *(*uintptr)(unsafe.Pointer(azNames + uintptr(ii)*8)) != 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) { (*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) } _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 pVtab = (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).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 (*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) pVTable = (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 64))).Fp (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 64))).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. */ for pVTable != 0 { db2 = (*TVTable)(unsafe.Pointer(pVTable)).Fdb pNext = (*TVTable)(unsafe.Pointer(pVTable)).FpNext if db2 == db { pRet = pVTable (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 64))).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 } 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 ppVTab = p + 64 + 16 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)) + 40 } } // 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 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 if (*Tsqlite3)(unsafe.Pointer(db)).FpnBytesFreed == uintptr(0) { _vtabDisconnectAll(tls, uintptr(0), p) } if (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 64))).FazArg != 0 { i = 0 for { if !(i < (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 64))).FnArg) { break } if i != int32(1) { _sqlite3DbFree(tls, db, *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 64))).FazArg + uintptr(i)*8))) } goto _1 _1: ; i++ } _sqlite3DbFree(tls, db, (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(p + 64))).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 nBytes = int64(uint64(8) * uint64(libc.Int32FromInt32(2)+(*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTable + 64))).FnArg)) if (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTable + 64))).FnArg+int32(3) >= *(*int32)(unsafe.Pointer(db + 136 + 2*4)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+13173, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTable)).FzName)) } azModuleArg = _sqlite3DbRealloc(tls, db, (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTable + 64))).FazArg, uint64(nBytes)) if azModuleArg == uintptr(0) { _sqlite3DbFree(tls, db, zArg) } else { v2 = pTable + 64 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ i = v1 *(*uintptr)(unsafe.Pointer(azModuleArg + uintptr(i)*8)) = zArg *(*uintptr)(unsafe.Pointer(azModuleArg + uintptr(i+int32(1))*8)) = uintptr(0) (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTable + 64))).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 } (*TTable)(unsafe.Pointer(pTable)).FeTabType = uint8(TABTYP_VTAB) db = (*TParse)(unsafe.Pointer(pParse)).Fdb _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)) (*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fn = uint32(int32(t__predefined_ptrdiff_t((*TToken)(unsafe.Pointer(pModuleName)).Fz+uintptr((*TToken)(unsafe.Pointer(pModuleName)).Fn)) - int64((*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 + 64))).FazArg != 0 { iDb = _sqlite3SchemaToIndex(tls, db, (*TTable)(unsafe.Pointer(pTable)).FpSchema) /* 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 + 64))).FazArg)), (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FaDb + uintptr(iDb)*32))).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 } _addArgumentToVtab(tls, pParse) (*TParse)(unsafe.Pointer(pParse)).FsArg.Fz = uintptr(0) if (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).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(int64((*TToken)(unsafe.Pointer(pEnd)).Fz)-int64((*TParse)(unsafe.Pointer(pParse)).FsNameToken.Fz))) + (*TToken)(unsafe.Pointer(pEnd)).Fn } zStmt = _sqlite3MPrintf(tls, db, __ccgo_ts+22254, libc.VaList(bp+8, pParse+272)) /* 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+22278, libc.VaList(bp+8, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).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+22377, libc.VaList(bp+8, (*TTable)(unsafe.Pointer(pTab)).FzName, zStmt)) _sqlite3VdbeAddParseSchemaOp(tls, v, iDb, zWhere, uint16(0)) _sqlite3DbFree(tls, db, zStmt) v2 = pParse + 56 *(*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 _sqlite3MarkAllShadowTablesOf(tls, db, pTab) pOld = _sqlite3HashInsert(tls, pSchema+8, zName, pTab) if pOld != 0 { _sqlite3OomFault(tls, db) /* 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 + 384 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 { (*TToken)(unsafe.Pointer(pArg)).Fn = uint32(int32(t__predefined_ptrdiff_t((*TToken)(unsafe.Pointer(p)).Fz+uintptr((*TToken)(unsafe.Pointer(p)).Fn)) - int64((*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(64) defer tls.Free(64) 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+32 */ 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 + 64))).FnArg *(*uintptr)(unsafe.Pointer(bp + 32)) = uintptr(0) azArg = (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).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+22396, libc.VaList(bp+48, (*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(48)) 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 + 64))).FazArg + 1*8)) = (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + uintptr(iDb)*32))).FzDbSName /* Invoke the virtual table constructor */ (*(*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+16, bp+32) _sqlite3DeleteTable(tls, db, pTab) (*Tsqlite3)(unsafe.Pointer(db)).FpVtabCtx = (*(*TVtabCtx)(unsafe.Pointer(bp))).FpPrior if rc == int32(SQLITE_NOMEM) { _sqlite3OomFault(tls, db) } if SQLITE_OK != rc { if *(*uintptr)(unsafe.Pointer(bp + 32)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(pzErr)) = _sqlite3MPrintf(tls, db, __ccgo_ts+22438, libc.VaList(bp+48, zModuleName)) } else { *(*uintptr)(unsafe.Pointer(pzErr)) = _sqlite3MPrintf(tls, db, __ccgo_ts+3863, libc.VaList(bp+48, *(*uintptr)(unsafe.Pointer(bp + 32)))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 32))) } _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.X__builtin___memset_chk(tls, (*TVTable)(unsafe.Pointer(pVTable)).FpVtab, 0, uint64(24), libc.X__builtin_object_size(tls, (*TVTable)(unsafe.Pointer(pVTable)).FpVtab, 0)) (*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 + 22468 *(*uintptr)(unsafe.Pointer(pzErr)) = _sqlite3MPrintf(tls, db, zFormat, libc.VaList(bp+48, zModuleName)) _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 + 64))).Fp (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).Fp = pVTable iCol = 0 for { if !(iCol < int32((*TTable)(unsafe.Pointer(pTab)).FnCol)) { break } zType = _sqlite3ColumnType(tls, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(iCol)*16, __ccgo_ts+1664) i = 0 nType = _sqlite3Strlen30(tls, zType) i = 0 for { if !(i < nType) { break } if 0 == Xsqlite3_strnicmp(tls, __ccgo_ts+17023, zType+uintptr(i), int32(6)) && (i == 0 || int32(*(*int8)(unsafe.Pointer(zType + uintptr(i-int32(1))))) == int32(' ')) && (int32(*(*int8)(unsafe.Pointer(zType + uintptr(i+int32(6))))) == int32('\000') || int32(*(*int8)(unsafe.Pointer(zType + uintptr(i+int32(6))))) == int32(' ')) { break } goto _3 _3: ; i++ } if i < nType { if *(*int8)(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 } *(*int8)(unsafe.Pointer(zType + uintptr(j))) = *(*int8)(unsafe.Pointer(zType + uintptr(j+nDel))) goto _5 _5: ; j++ } if int32(*(*int8)(unsafe.Pointer(zType + uintptr(i)))) == int32('\000') && i > 0 { *(*int8)(unsafe.Pointer(zType + uintptr(i-int32(1)))) = int8('\000') } p6 = (*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*16 + 14 *(*Tu16)(unsafe.Pointer(p6)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p6))) | libc.Int32FromInt32(COLFLAG_HIDDEN)) *(*Tu32)(unsafe.Pointer(pTab + 48)) |= uint32(TF_HasHidden) oooHidden = uint16(TF_OOOHidden) } else { *(*Tu32)(unsafe.Pointer(pTab + 48)) |= 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 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 + 64))).FazArg)) pMod = _sqlite3HashFind(tls, db+576, zMod) if !(pMod != 0) { zModule = *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).FazArg)) _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22514, 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+3863, 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(uint64(8) * uint64(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.X__builtin___memset_chk(tls, aVTrans+uintptr((*Tsqlite3)(unsafe.Pointer(db)).FnVTrans)*8, 0, uint64(8)*uint64(ARRAY_INCR), libc.X__builtin_object_size(tls, aVTrans+uintptr((*Tsqlite3)(unsafe.Pointer(db)).FnVTrans)*8, 0)) (*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 + 572 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*uintptr)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaVTrans + uintptr(v1)*8)) = 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)*32))).FzDbSName) /* Locate the required virtual table module */ zMod = *(*uintptr)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).FazArg)) pMod = _sqlite3HashFind(tls, db+576, 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+22514, 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(448) defer tls.Free(448) var i, initBusy, rc int32 var pCtx, pIdx, pNew, pTab, z, v3 uintptr var v2 Ti16 var _ /* sParse at bp+0 */ TParse var _ /* tokenType at bp+424 */ int32 _, _, _, _, _, _, _, _, _, _ = i, initBusy, pCtx, pIdx, pNew, pTab, rc, z, v2, v3 rc = SQLITE_OK /* Verify that the first two keywords in the CREATE TABLE statement ** really are "CREATE" and "TABLE". If this is not the case, then ** sqlite3_declare_vtab() is being misused. */ z = zCreateTable i = 0 for { if !(_aKeyword1[i] != 0) { break } *(*int32)(unsafe.Pointer(bp + 424)) = 0 for cond := true; cond; cond = *(*int32)(unsafe.Pointer(bp + 424)) == int32(TK_SPACE) { z += uintptr(_sqlite3GetToken(tls, z, bp+424)) } if *(*int32)(unsafe.Pointer(bp + 424)) != int32(_aKeyword1[i]) { _sqlite3ErrorWithMsg(tls, db, int32(SQLITE_ERROR), __ccgo_ts+22533, 0) return int32(SQLITE_ERROR) } goto _1 _1: ; i++ } 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(156227))) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return _sqlite3MisuseError(tls, int32(156229)) } pTab = (*TVtabCtx)(unsafe.Pointer(pCtx)).FpTab _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. */ 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) { 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 + 64))).FpDfltList) v2 = (*TTable)(unsafe.Pointer(pNew)).FnCol (*TTable)(unsafe.Pointer(pTab)).FnCol = v2 (*TTable)(unsafe.Pointer(pTab)).FnNVCol = v2 *(*Tu32)(unsafe.Pointer(pTab + 48)) |= (*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) 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 { (*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 { v3 = __ccgo_ts + 3863 } else { v3 = uintptr(0) } _sqlite3ErrorWithMsg(tls, db, int32(SQLITE_ERROR), v3, libc.VaList(bp+440, (*(*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) rc = _sqlite3ApiExit(tls, db, rc) Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) return rc } var _aKeyword1 = [3]Tu8{ 0: uint8(TK_CREATE), 1: uint8(TK_TABLE), } // 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)*32))).FzDbSName) if pTab != uintptr(0) && int32((*TTable)(unsafe.Pointer(pTab)).FeTabType) == int32(TABTYP_VTAB) && (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).Fp != uintptr(0) { p = (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).Fp for { if !(p != 0) { break } 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 } (*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 { (*TVTable)(unsafe.Pointer(p)).FpVtab = uintptr(0) (*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).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)*8)) 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)*8)))).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(uint64(libc.UintptrFromInt32(0)+136))) 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(uint64(libc.UintptrFromInt32(0)+128))) 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)*8)) == 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 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)*8)) 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 + 48)) &= ^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 + 48)) |= 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+8 */ uintptr var _ /* xSFunc at bp+0 */ uintptr _, _, _, _, _ = pMod, pNew, pTab, pVtab, rc *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 8)) = 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 } pTab = *(*uintptr)(unsafe.Pointer(pExpr + 64)) 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 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+8) if rc == 0 { return pDef } /* Create a new ephemeral function definition for the overloaded ** function */ pNew = _sqlite3DbMallocZero(tls, db, uint64(72)+uint64(_sqlite3Strlen30(tls, (*TFuncDef)(unsafe.Pointer(pDef)).FzName))+uint64(1)) if pNew == uintptr(0) { return pDef } *(*TFuncDef)(unsafe.Pointer(pNew)) = *(*TFuncDef)(unsafe.Pointer(pDef)) (*TFuncDef)(unsafe.Pointer(pNew)).FzName = pNew + 1*72 libc.X__builtin___memcpy_chk(tls, pNew+1*72, (*TFuncDef)(unsafe.Pointer(pDef)).FzName, uint64(_sqlite3Strlen30(tls, (*TFuncDef)(unsafe.Pointer(pDef)).FzName)+int32(1)), libc.X__builtin_object_size(tls, pNew+1*72, 0)) (*TFuncDef)(unsafe.Pointer(pNew)).FxSFunc = *(*uintptr)(unsafe.Pointer(bp)) (*TFuncDef)(unsafe.Pointer(pNew)).FpUserData = *(*uintptr)(unsafe.Pointer(bp + 8)) *(*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 i = 0 for { if !(i < (*TParse)(unsafe.Pointer(pToplevel)).FnVtabLock) { break } if pTab == *(*uintptr)(unsafe.Pointer((*TParse)(unsafe.Pointer(pToplevel)).FapVtabLock + uintptr(i)*8)) { return } goto _2 _2: ; i++ } n = int32(uint64((*TParse)(unsafe.Pointer(pToplevel)).FnVtabLock+libc.Int32FromInt32(1)) * uint64(8)) apVtabLock = _sqlite3Realloc(tls, (*TParse)(unsafe.Pointer(pToplevel)).FapVtabLock, uint64(n)) if apVtabLock != 0 { (*TParse)(unsafe.Pointer(pToplevel)).FapVtabLock = apVtabLock v4 = pToplevel + 312 v3 = *(*int32)(unsafe.Pointer(v4)) *(*int32)(unsafe.Pointer(v4))++ *(*uintptr)(unsafe.Pointer((*TParse)(unsafe.Pointer(pToplevel)).FapVtabLock + uintptr(v3)*8)) = 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(104)) 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 (*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1)) *(*Tu32)(unsafe.Pointer(pTab + 48)) |= 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+3863, 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 + 48)) |= 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) { return int32(_aMap1[int32((*Tsqlite3)(unsafe.Pointer(db)).FvtabOnConflict)-int32(1)]) } var _aMap1 = [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(156723)) } else { 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(156745)) 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 { FpWInfo uintptr FpOuter uintptr Fop Tu8 FhasOr Tu8 FnTerm int32 FnSlot int32 FnBase int32 Fa uintptr FaStatic [8]TWhereTerm } type WhereClause = TWhereClause type TWhereMaskSet = struct { FbVarSelect int32 Fn int32 Fix [64]int32 } type WhereMaskSet = TWhereMaskSet type TWhereOrInfo = struct { Fwc TWhereClause Findexable TBitmask } type WhereOrInfo = TWhereOrInfo type TWhereAndInfo = struct { Fwc TWhereClause } type WhereAndInfo = TWhereAndInfo type TWhereLevel = struct { 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 FnotReady TBitmask } type WhereLevel = TWhereLevel type TWhereLoop = struct { 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 [8]byte } FwsFlags Tu32 FnLTerm Tu16 FnSkip Tu16 FnLSlot Tu16 FaLTerm uintptr FpNextLoop uintptr FaLTermSpace [3]uintptr } type WhereLoop = TWhereLoop type TWherePath = struct { FmaskLoop TBitmask FrevLoop TBitmask FnRow TLogEst FrCost TLogEst FrUnsorted TLogEst FisOrdered Ti8 FaLoop uintptr } type WherePath = TWherePath type TWhereTerm = struct { 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 int8 FiEquiv uint8 FnEquiv uint8 FaiCur [11]int32 FaiColumn [11]Ti16 } type WhereScan = TWhereScan type TWhereOrCost = struct { Fprereq TBitmask FrRun TLogEst FnOut TLogEst } type WhereOrCost = TWhereOrCost type TWhereOrSet = struct { Fn Tu16 Fa [3]TWhereOrCost } type WhereOrSet = TWhereOrSet type TWhereMemBlock = struct { FpNext uintptr 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 { FpNext uintptr 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 { 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 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 { 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 [8]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 { Fprereq TBitmask FrRun TLogEst FnOut TLogEst } 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 { Fn Tu16 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 { FmaskLoop TBitmask FrevLoop TBitmask FnRow TLogEst FrCost TLogEst FrUnsorted TLogEst FisOrdered Ti8 FaLoop uintptr } 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 { 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 int8 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 { FpWInfo uintptr FpOuter uintptr Fop Tu8 FhasOr Tu8 FnTerm int32 FnSlot int32 FnBase int32 Fa uintptr 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 { 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 { 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 { 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__ccgo68 uint8 FnRowOut TLogEst FiTop int32 FiEndWhere int32 FpLoops uintptr FpMemToFree uintptr 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 + 22546 } if i == -int32(1) { return __ccgo_ts + 17132 } return (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr(i)*16))).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 if bAnd != 0 { Xsqlite3_str_append(tls, pStr, __ccgo_ts+22553, int32(5)) } if nTerm > int32(1) { Xsqlite3_str_append(tls, pStr, __ccgo_ts+22559, int32(1)) } i = 0 for { if !(i < nTerm) { break } if i != 0 { Xsqlite3_str_append(tls, pStr, __ccgo_ts+13563, 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+5518, int32(1)) } Xsqlite3_str_append(tls, pStr, zOp, int32(1)) if nTerm > int32(1) { Xsqlite3_str_append(tls, pStr, __ccgo_ts+22559, int32(1)) } i = 0 for { if !(i < nTerm) { break } if i != 0 { Xsqlite3_str_append(tls, pStr, __ccgo_ts+13563, int32(1)) } Xsqlite3_str_append(tls, pStr, __ccgo_ts+5520, int32(1)) goto _2 _2: ; i++ } if nTerm > int32(1) { Xsqlite3_str_append(tls, pStr, __ccgo_ts+5518, 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+22561, 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+22553, int32(5)) } if i >= int32(nSkip) { v2 = __ccgo_ts + 22564 } else { v2 = __ccgo_ts + 22569 } 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+22577) 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+22579) } Xsqlite3_str_append(tls, pStr, __ccgo_ts+5518, 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 int8 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+32 */ [100]int8 _, _, _, _, _, _, _, _, _, _, _, _, _, _ = 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)*104 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+32, int32(100), int32(SQLITE_MAX_LENGTH)) (*(*TStrAccum)(unsafe.Pointer(bp))).FprintfFlags = uint8(SQLITE_PRINTF_INTERNAL) if isSearch != 0 { v2 = __ccgo_ts + 22581 } else { v2 = __ccgo_ts + 22588 } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22593, libc.VaList(bp+144, v2, pItem)) if flags&uint32(libc.Int32FromInt32(WHERE_IPK)|libc.Int32FromInt32(WHERE_VIRTUALTABLE)) == uint32(0) { zFmt = uintptr(0) pIdx = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex if !((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 100))&0x3>>0)) == int32(SQLITE_IDXTYPE_PRIMARYKEY) { if isSearch != 0 { zFmt = __ccgo_ts + 11726 } } else { if flags&uint32(WHERE_PARTIALIDX) != 0 { zFmt = __ccgo_ts + 22599 } else { if flags&uint32(WHERE_AUTO_INDEX) != 0 { zFmt = __ccgo_ts + 22632 } else { if flags&uint32(WHERE_IDX_ONLY) != 0 { zFmt = __ccgo_ts + 22657 } else { zFmt = __ccgo_ts + 22675 } } } } if zFmt != 0 { Xsqlite3_str_append(tls, bp, __ccgo_ts+22684, int32(7)) Xsqlite3_str_appendf(tls, bp, zFmt, libc.VaList(bp+144, (*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 + 17132 Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22692, libc.VaList(bp+144, zRowid)) if flags&uint32(libc.Int32FromInt32(WHERE_COLUMN_EQ)|libc.Int32FromInt32(WHERE_COLUMN_IN)) != 0 { cRangeOp = int8('=') } else { if flags&uint32(WHERE_BOTH_LIMIT) == uint32(WHERE_BOTH_LIMIT) { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22723, libc.VaList(bp+144, zRowid)) cRangeOp = int8('<') } else { if flags&uint32(WHERE_BTM_LIMIT) != 0 { cRangeOp = int8('>') } else { cRangeOp = int8('<') } } } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22733, libc.VaList(bp+144, int32(cRangeOp))) } else { if flags&uint32(WHERE_VIRTUALTABLE) != uint32(0) { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22738, libc.VaList(bp+144, (*(*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+22765, 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(160) defer tls.Free(160) var db, pItem, pLoop, pTab, v, z, zMsg uintptr var i, ret int32 var _ /* str at bp+0 */ TStrAccum var _ /* zBuf at bp+32 */ [100]int8 _, _, _, _, _, _, _, _, _ = db, i, pItem, pLoop, pTab, ret, v, z, zMsg ret = 0 pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*104 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+32, int32(100), int32(SQLITE_MAX_LENGTH)) (*(*TStrAccum)(unsafe.Pointer(bp))).FprintfFlags = uint8(SQLITE_PRINTF_INTERNAL) Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22776, libc.VaList(bp+144, 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+22564, libc.VaList(bp+144, (*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr((*TTable)(unsafe.Pointer(pTab)).FiPKey)*16))).FzCnName)) } else { Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22797, 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+22553, int32(5)) } Xsqlite3_str_appendf(tls, bp, __ccgo_ts+22564, libc.VaList(bp+144, z)) goto _1 _1: ; i++ } } Xsqlite3_str_append(tls, bp, __ccgo_ts+5518, 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 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 + 18 *(*Tu16)(unsafe.Pointer(p1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p1))) | libc.Int32FromInt32(TERM_LIKECOND)) } else { p2 = pTerm + 18 *(*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)*56 (*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) { return } /* Adjust base and n to skip over SQLITE_AFF_BLOB and SQLITE_AFF_NONE ** entries at the beginning and end of the affinity string. */ for n > 0 && int32(*(*int8)(unsafe.Pointer(zAff))) <= int32(SQLITE_AFF_BLOB) { n-- base++ zAff++ } for n > int32(1) && int32(*(*int8)(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, *(*int8)(unsafe.Pointer(zAff + uintptr(i))))) == int32(SQLITE_AFF_BLOB) || _sqlite3ExprNeedsNoAffinityChange(tls, p, *(*int8)(unsafe.Pointer(zAff + uintptr(i)))) != 0 { *(*int8)(unsafe.Pointer(zAff + uintptr(i))) = int8(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 + 32)) 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 */ pOrigRhs = (*TSelect)(unsafe.Pointer(pSelect)).FpEList if pSelect == *(*uintptr)(unsafe.Pointer(pNew + 32)) { pOrigLhs = *(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pNew)).FpLeft + 32)) } 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)*8)))).FpExpr == pX { iField = (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*8)) + 32))).FiField - int32(1) if (*(*TExprList_item)(unsafe.Pointer(pOrigRhs + 8 + uintptr(iField)*32))).FpExpr == uintptr(0) { goto _2 } /* Duplicate PK column */ pRhs = _sqlite3ExprListAppend(tls, pParse, pRhs, (*(*TExprList_item)(unsafe.Pointer(pOrigRhs + 8 + uintptr(iField)*32))).FpExpr) (*(*TExprList_item)(unsafe.Pointer(pOrigRhs + 8 + uintptr(iField)*32))).FpExpr = uintptr(0) if pOrigLhs != 0 { pLhs = _sqlite3ExprListAppend(tls, pParse, pLhs, (*(*TExprList_item)(unsafe.Pointer(pOrigLhs + 8 + uintptr(iField)*32))).FpExpr) (*(*TExprList_item)(unsafe.Pointer(pOrigLhs + 8 + uintptr(iField)*32))).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 + 32)) = 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)*32 + 24)) = 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 */ 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)) } iReg = iTarget i = 0 for { if !(i < iEq) { break } if *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*8)) != 0 && (*TWhereTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(i)*8)))).FpExpr == pX { _disableTerm(tls, pLevel, pTerm) return iTarget } goto _1 _1: ; i++ } 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)*8)))).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 + 32)))).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(4)*uint64(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(4)*uint64(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) *(*Tu32)(unsafe.Pointer(pLoop + 48)) |= uint32(WHERE_IN_ABLE) if (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 72))).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 + 48)) |= uint32(WHERE_IN_EARLYOUT) } i = (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 72))).FnIn (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 72))).FnIn += nEq (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 72))).FaInLoop = _sqlite3WhereRealloc(tls, (*TWhereClause)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpWC)).FpWInfo, (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 72))).FaInLoop, uint64(20)*uint64((*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 72))).FnIn)) pIn = (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 72))).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)*8)))).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 + 72))).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 nEq = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq nSkip = (*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip pIdx = (*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex /* 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 + 56)) += nReg zAff = _sqlite3DbStrDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, _sqlite3IndexAffinityStr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pIdx)) 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)) 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 */ j = int32(nSkip) for { if !(j < int32(nEq)) { break } pTerm = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*8)) /* 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 { *(*int8)(unsafe.Pointer(zAff + uintptr(j))) = int8(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 { if int32(_sqlite3CompareAffinity(tls, pRight, *(*int8)(unsafe.Pointer(zAff + uintptr(j))))) == int32(SQLITE_AFF_BLOB) { *(*int8)(unsafe.Pointer(zAff + uintptr(j))) = int8(SQLITE_AFF_BLOB) } if _sqlite3ExprNeedsNoAffinityChange(tls, pRight, *(*int8)(unsafe.Pointer(zAff + uintptr(j)))) != 0 { *(*int8)(unsafe.Pointer(zAff + uintptr(j))) = int8(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.SetBitFieldPtr8Uint32(pWInfo+68, 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(4)*uint64(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 } 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 if p != 0 && _sqlite3ExprIsVector(tls, p) != 0 { if (*TExpr)(unsafe.Pointer(p)).Fflags&uint32(EP_xIsSelect) != uint32(0) { v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe iSelect = _sqlite3CodeSubselect(tls, pParse, p) _sqlite3VdbeAddOp3(tls, v, int32(OP_Copy), iSelect, iReg, nReg-int32(1)) } else { pList = *(*uintptr)(unsafe.Pointer(p + 32)) i = 0 for { if !(i < nReg) { break } _sqlite3ExprCode(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*32))).FpExpr, iReg+i) goto _1 _1: ; i++ } } } else { _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 + 18 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(TERM_CODED)) } goto _1 _1: ; i++ pTerm += 56 } } // 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 + 856 + uintptr(iLevel)*104 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 } (*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)) 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 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 // // /* // ** Loop pLoop is a WHERE_INDEXED level that uses at least one IN(...) // ** operator. Return true if level pLoop is guaranteed to visit only one // ** row for each key generated for the index. // */ func _whereLoopIsOneRow(tls *libc.TLS, pLoop uintptr) (r int32) { var ii int32 _ = ii if (*TIndex)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex)).FonError != 0 && int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnSkip) == 0 && int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq) == int32((*TIndex)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FpIndex)).FnKeyCol) { ii = 0 for { if !(ii < int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq)) { break } if int32((*TWhereTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(ii)*8)))).FeOperator)&(libc.Int32FromInt32(WO_IS)|libc.Int32FromInt32(WO_ISNULL)) != 0 { return 0 } goto _1 _1: ; ii++ } return int32(1) } return 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(112) defer tls.Free(112) 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+16 */ TExpr var _ /* zStartAff at bp+8 */ 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 + 104 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)*104 iCur = (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor (*TWhereLevel)(unsafe.Pointer(pLevel)).FnotReady = notReady & ^_sqlite3WhereGetMask(tls, pWInfo+592, 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. */ if int32((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom) > 0 && int32((*(*TSrcItem)(unsafe.Pointer(pTabItem))).Ffg.Fjointype)&int32(JT_LEFT) != 0 { v5 = pParse + 56 *(*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 + 856 + uintptr(j)*104))).FiLeftJoin != 0 { break } if (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 856 + uintptr(j)*104))).FpRJ != 0 { break } goto _6 _6: ; j-- } addrHalt = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 856 + uintptr(j)*104))).FaddrBrk /* Special case of a FROM clause subquery implemented as a co-routine */ if int32(uint32(*(*uint16)(unsafe.Pointer(pTabItem + 60 + 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)*8)) 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 + 52 v9 = *(*int32)(unsafe.Pointer(v10)) *(*int32)(unsafe.Pointer(v10))++ iTab = v9 v12 = pParse + 56 *(*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 { _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) j = 0 for { if !(j < nConstraint) { break } pTerm = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm + uintptr(j)*8)) 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 + 72))).FnIn) { break } pOp = _sqlite3VdbeGetOp(tls, v, (*(*TInLoop)(unsafe.Pointer((*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 72))).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 + 32))).FiField pLeft = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpLeft if iFld > 0 { (*TExpr)(unsafe.Pointer(pCompare)).FpLeft = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pLeft + 32)) + 8 + uintptr(iFld-int32(1))*32))).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. */ pTerm = *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pLoop)).FaLTerm)) v21 = pParse + 56 *(*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)*8)) } 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)*8)) } 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), } /* Make sure the ordering.. */ /* ... of the TK_xx values... */ /* ... is correct. */ pX = (*TWhereTerm)(unsafe.Pointer(pStart)).FpExpr /* 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)]) } 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 /* Transitive constraints */ v28 = pParse + 56 *(*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 if testOp != int32(OP_Noop) { v33 = pParse + 56 *(*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 /* 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)*8)) 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 */ } 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)*8)) 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)*16 + 8))&0xf>>0)) == 0 || j == -int32(2) { bSeekPastNull = uint8(1) } } } /* 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) { nExtraReg = int32(1) bSeekPastNull = uint8(1) v40 = pParse + 56 *(*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+8) if *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 && nTop != 0 { zEndAff = _sqlite3DbStrDup(tls, db, *(*uintptr)(unsafe.Pointer(bp + 8))+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 + 8)) != 0 { _updateRangeAffinityStr(tls, pRight2, int32(nBtm), *(*uintptr)(unsafe.Pointer(bp + 8))+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 + 8))) 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) } } /* Load the value for the inequality constraint at the end of the ** range (if any). */ nConstraint1 = int32(nEq) if pRangeEnd != 0 { pRight3 = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pRangeEnd)).FpExpr)).FpRight _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 { } 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 + 8)) != 0 { _sqlite3DbNNFreeNN(tls, db, *(*uintptr)(unsafe.Pointer(bp + 8))) } 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 */ _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: */ } /* Record the instruction used to terminate the loop. */ if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(WHERE_ONEROW) != 0 || (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 72))).FnIn != 0 && regBignull == 0 && _whereLoopIsOneRow(tls, pLoop) != 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 { } 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 + 52 v44 = *(*int32)(unsafe.Pointer(v45)) *(*int32)(unsafe.Pointer(v45))++ /* Potential covering index (or NULL) */ iCovCur = v44 v47 = pParse + 56 *(*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)) pOrWc = *(*uintptr)(unsafe.Pointer(pTerm + 32)) (*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(112)+uint64(nNotReady)*uint64(104)) 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.X__builtin___memcpy_chk(tls, pOrTab+8, pTabItem, uint64(104), libc.X__builtin_object_size(tls, pOrTab+8, 0)) origSrc = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 k = int32(1) for { if !(k <= nNotReady) { break } libc.X__builtin___memcpy_chk(tls, pOrTab+8+uintptr(k)*104, origSrc+uintptr((*(*TWhereLevel)(unsafe.Pointer(pLevel + uintptr(k)*104))).FiFrom)*104, uint64(104), libc.X__builtin_object_size(tls, pOrTab+8+uintptr(k)*104, 0)) 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 + 56 *(*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 + 52 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 + 56 *(*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)*56))).FpExpr if (*TWhereClause)(unsafe.Pointer(pWC)).Fa+uintptr(iTerm)*56 == pTerm { goto _55 } if int32((*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iTerm)*56))).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)*56))).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+22805, 0) ii = 0 for { if !(ii < (*TWhereClause)(unsafe.Pointer(pOrWc)).FnTerm) { break } pOrTerm = (*TWhereClause)(unsafe.Pointer(pOrWc)).Fa + uintptr(ii)*56 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+22820, libc.VaList(bp+96, ii+int32(1))) pSubWInfo = _sqlite3WhereBegin(tls, pParse, pOrTab, pOrExpr, uintptr(0), uintptr(0), uintptr(0), uint16(WHERE_OR_SUBCLAUSE), iCovCur) if pSubWInfo != 0 { addrExplain = _sqlite3WhereExplainOneScan(tls, pParse, pOrTab, pSubWInfo+856, 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 + 68))&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 + 856))).FpWLoop 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 + 100))&0x3>>0)) == libc.Int32FromInt32(SQLITE_IDXTYPE_PRIMARYKEY))) { pCov = (*TWhereLoop)(unsafe.Pointer(pSubLoop)).Fu.Fbtree.FpIndex } else { pCov = uintptr(0) } if _sqlite3WhereUsesDeferredSeek(tls, pSubWInfo) != 0 { libc.SetBitFieldPtr8Uint32(pWInfo+68, 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) *(*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 */ (*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 { if int32(uint32(*(*uint16)(unsafe.Pointer(pTabItem + 60 + 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. ** ** This optimization of causing simple query restrictions to occur before ** more complex one is call the "push-down" optimization in MySQL. Here ** in SQLite, the name is "MySQL push-down", since there is also another ** totally unrelated optimization called "WHERE-clause push-down". ** Sometimes the qualifier is omitted, resulting in an ambiguity, so beware. */ 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+68, libc.Uint32FromInt32(1), 1, 0x2) goto _61 } pE = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr 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+592, *(*int32)(unsafe.Pointer(pE + 52))) 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 + 18 *(*Tu16)(unsafe.Pointer(p62)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p62))) | libc.Int32FromInt32(TERM_CODED)) goto _61 _61: ; j-- pTerm += 56 } 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 pAlt = _sqlite3WhereFindTerm(tls, pWC, iCur, (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 32))).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 + 32)))).FpEList)).FnExpr > int32(1) { goto _63 } *(*TExpr)(unsafe.Pointer(bp + 16)) = *(*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pAlt)).FpExpr)) (*(*TExpr)(unsafe.Pointer(bp + 16))).FpLeft = (*TExpr)(unsafe.Pointer(pE1)).FpLeft _sqlite3ExprIfFalse(tls, pParse, bp+16, addrCont, int32(SQLITE_JUMPIFNULL)) p64 = pAlt + 18 *(*Tu16)(unsafe.Pointer(p64)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p64))) | libc.Int32FromInt32(TERM_CODED)) goto _63 _63: ; j-- pTerm += 56 } /* 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)*104))).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) (*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) { goto _67 } if int32((*TSrcItem)(unsafe.Pointer(pTabItem)).Ffg.Fjointype)&int32(JT_LTORJ) != 0 { goto _67 } _sqlite3ExprIfFalse(tls, pParse, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr, addrCont, int32(SQLITE_JUMPIFNULL)) p68 = pTerm + 18 *(*Tu16)(unsafe.Pointer(p68)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p68))) | libc.Int32FromInt32(TERM_CODED)) goto _67 _67: ; j++ pTerm += 56 } _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(128) defer tls.Free(128) var addrCont, iCol, iCur, iIdxCur, iPk, jmp, k, nPk, r, v3 int32 var mAll TBitmask var pLoop, pParse, pPk, pRJ, pRight, 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, pRight, 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 + 104 pLoop = (*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop pTabItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*104 mAll = uint64(0) _sqlite3VdbeExplain(tls, pParse, uint8(1), __ccgo_ts+22829, libc.VaList(bp+120, (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pTabItem)).FpTab)).FzName)) k = 0 for { if !(k < iLevel) { break } pRight = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*(*TWhereLevel)(unsafe.Pointer(pWInfo + 856 + uintptr(k)*104))).FiFrom)*104 mAll |= (*TWhereLoop1)(unsafe.Pointer((*(*TWhereLevel)(unsafe.Pointer(pWInfo + 856 + uintptr(k)*104))).FpWLoop)).FmaskSelf if int32(uint32(*(*uint16)(unsafe.Pointer(pRight + 60 + 4))&0x20>>5)) != 0 { _sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, (*TSrcItem)(unsafe.Pointer(pRight)).FregResult, (*TSrcItem)(unsafe.Pointer(pRight)).FregResult+(*TExprList)(unsafe.Pointer((*TSelect)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pRight)).FpSelect)).FpEList)).FnExpr-int32(1)) } _sqlite3VdbeAddOp1(tls, v, int32(OP_NullRow), (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 856 + uintptr(k)*104))).FiTabCur) iIdxCur = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 856 + uintptr(k)*104))).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)*56 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.X__builtin___memcpy_chk(tls, bp+8, pTabItem, uint64(104), libc.X__builtin_object_size(tls, bp+8, 0)) (*(*TSrcItem)(unsafe.Pointer(bp + 8))).Ffg.Fjointype = uint8(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 + 56 *(*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 + 56)) += 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) (*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(56)*uint64((*TWhereClause)(unsafe.Pointer(pWC)).FnSlot)*uint64(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.X__builtin___memcpy_chk(tls, (*TWhereClause)(unsafe.Pointer(pWC)).Fa, pOld, uint64(56)*uint64((*TWhereClause)(unsafe.Pointer(pWC)).FnTerm), libc.X__builtin_object_size(tls, (*TWhereClause)(unsafe.Pointer(pWC)).Fa, 0)) (*TWhereClause)(unsafe.Pointer(pWC)).FnSlot = (*TWhereClause)(unsafe.Pointer(pWC)).FnSlot * int32(2) } v3 = pWC + 20 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ v1 = v2 idx = v1 pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(v1)*56 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.X__builtin___memset_chk(tls, pTerm+20, 0, libc.Uint64FromInt64(56)-uint64(libc.UintptrFromInt32(0)+20), libc.X__builtin_object_size(tls, pTerm+20, 0)) 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) { 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) { (*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 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 { c = uint16(libc.Int32FromInt32(WO_EQ) << (op - libc.Int32FromInt32(TK_EQ))) } } } 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 } pList = *(*uintptr)(unsafe.Pointer(pExpr + 32)) pLeft = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 1*32))).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) } else { if op == int32(TK_STRING) { 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 { zNew = *(*uintptr)(unsafe.Pointer(pPrefix + 8)) *(*int8)(unsafe.Pointer(zNew + uintptr(cnt))) = 0 v3 = libc.Int32FromInt32(0) iTo = v3 iFrom = v3 for { if !(iFrom < cnt) { break } if int32(*(*int8)(unsafe.Pointer(zNew + uintptr(iFrom)))) == int32((*(*[4]Tu8)(unsafe.Pointer(bp)))[int32(3)]) { iFrom++ } v4 = iTo iTo++ *(*int8)(unsafe.Pointer(zNew + uintptr(v4))) = *(*int8)(unsafe.Pointer(zNew + uintptr(iFrom))) goto _2 _2: ; iFrom++ } *(*int8)(unsafe.Pointer(zNew + uintptr(iTo))) = 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) && *(*uintptr)(unsafe.Pointer(pLeft + 64)) != 0 && int32((*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pLeft + 64)))).FeTabType) == int32(TABTYP_VTAB) { isNum = _sqlite3AtoF(tls, zNew, bp+8, iTo, uint8(SQLITE_UTF8)) if isNum <= 0 { if iTo == int32(1) && int32(*(*int8)(unsafe.Pointer(zNew))) == int32('-') { isNum = +libc.Int32FromInt32(1) } else { *(*int8)(unsafe.Pointer(zNew + uintptr(iTo-int32(1))))++ isNum = _sqlite3AtoF(tls, zNew, bp+8, iTo, uint8(SQLITE_UTF8)) *(*int8)(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)) if *(*int32)(unsafe.Pointer(pisComplete)) != 0 && *(*int8)(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+8 */ 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) { pList = *(*uintptr)(unsafe.Pointer(pExpr + 32)) 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*32))).FpExpr if int32((*TExpr)(unsafe.Pointer(pCol)).Fop) == int32(TK_COLUMN) && int32((*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pCol + 64)))).FeTabType) == int32(TABTYP_VTAB) { i = 0 for { if !(i < int32(libc.Uint64FromInt64(64)/libc.Uint64FromInt64(16))) { break } 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 if int32((*TExpr)(unsafe.Pointer(pCol)).Fop) == int32(TK_COLUMN) && int32((*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pCol + 64)))).FeTabType) == int32(TABTYP_VTAB) { pVtab = (*TVTable)(unsafe.Pointer(_sqlite3GetVTable(tls, db, *(*uintptr)(unsafe.Pointer(pCol + 64))))).FpVtab 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+8) if i >= int32(SQLITE_INDEX_CONSTRAINT_FUNCTION) { *(*uint8)(unsafe.Pointer(peOp2)) = uint8(i) *(*uintptr)(unsafe.Pointer(ppRight)) = (*(*TExprList_item)(unsafe.Pointer(pList + 8 + 1*32))).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 if int32((*TExpr)(unsafe.Pointer(pLeft)).Fop) == int32(TK_COLUMN) && int32((*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pLeft + 64)))).FeTabType) == int32(TABTYP_VTAB) { res++ } if pRight != 0 && (int32((*TExpr)(unsafe.Pointer(pRight)).Fop) == int32(TK_COLUMN) && int32((*TTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pRight + 64)))).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 + 16981, FeOp2: uint8(SQLITE_INDEX_CONSTRAINT_MATCH), }, 1: { FzOp: __ccgo_ts + 16315, FeOp2: uint8(SQLITE_INDEX_CONSTRAINT_GLOB), }, 2: { FzOp: __ccgo_ts + 15794, FeOp2: uint8(SQLITE_INDEX_CONSTRAINT_LIKE), }, 3: { FzOp: __ccgo_ts + 22843, 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 + 52)) = *(*int32)(unsafe.Pointer(pBase + 52)) } } // 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)*56))).FiParent = iParent (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iChild)*56))).FtruthProb = (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iParent)*56))).FtruthProb (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(iParent)*56))).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(pTerm + 32)))).Fwc.FnTerm { return (*TWhereAndInfo)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pTerm + 32)))).Fwc.Fa + uintptr(N)*56 } 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 } 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 { 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 } 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)*56 /* 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. */ v1 = _sqlite3DbMallocZero(tls, db, uint64(496)) pOrInfo = v1 *(*uintptr)(unsafe.Pointer(pTerm + 32)) = v1 if pOrInfo == uintptr(0) { return } p2 = pTerm + 18 *(*Tu16)(unsafe.Pointer(p2)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p2))) | libc.Int32FromInt32(TERM_ORINFO)) pOrWc = pOrInfo libc.X__builtin___memset_chk(tls, pOrWc+40, 0, uint64(448), libc.X__builtin_object_size(tls, pOrWc+40, 0)) _sqlite3WhereClauseInit(tls, pOrWc, pWInfo) _sqlite3WhereSplit(tls, pOrWc, pExpr, uint8(TK_OR)) _sqlite3WhereExprAnalyze(tls, pSrc, pOrWc) if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return } /* ** 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 { chngToIN = uint64(0) pAndInfo = _sqlite3DbMallocRawNN(tls, db, uint64(488)) if pAndInfo != 0 { b = uint64(0) *(*uintptr)(unsafe.Pointer(pOrTerm + 32)) = pAndInfo p4 = pOrTerm + 18 *(*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.X__builtin___memset_chk(tls, pAndWC+40, 0, uint64(448), libc.X__builtin_object_size(tls, pAndWC+40, 0)) _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 } 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+592, (*TWhereTerm)(unsafe.Pointer(pAndTerm)).FleftCursor) } goto _5 _5: ; j++ pAndTerm += 56 } } 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+592, (*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)*56 b1 |= _sqlite3WhereGetMask(tls, pWInfo+592, (*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 += 56 } /* ** 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*56, 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 } p12 = pOrTerm + 18 *(*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. */ goto _11 } if chngToIN&_sqlite3WhereGetMask(tls, pWInfo+592, (*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. */ goto _11 } iColumn = (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pOrTerm + 32))).FleftColumn iCursor = (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor pLeft = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pOrTerm)).FpExpr)).FpLeft break goto _11 _11: ; i-- pOrTerm += 56 } if i < 0 { /* No candidate table+column was found. This can only occur ** on the second iteration */ 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 } if (*TWhereTerm)(unsafe.Pointer(pOrTerm)).FleftCursor != iCursor { p14 = pOrTerm + 18 *(*Tu16)(unsafe.Pointer(p14)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p14))) & ^libc.Int32FromInt32(TERM_OK)) } else { if (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pOrTerm + 32))).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 + 18 *(*Tu16)(unsafe.Pointer(p15)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p15))) | libc.Int32FromInt32(TERM_OK)) } } } goto _13 _13: ; i-- pOrTerm += 56 } 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 } 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 += 56 } pDup = _sqlite3ExprDup(tls, db, pLeft1, 0) pNew = _sqlite3PExpr(tls, pParse, int32(TK_IN), pDup, uintptr(0)) if pNew != 0 { _transferJoinMarkings(tls, pNew, pExpr) *(*uintptr)(unsafe.Pointer(pNew + 32)) = 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 int8 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)*104))).FpSelect) if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104 + 60 + 4))&0x400>>10)) == 0 { mask |= _sqlite3WhereExprUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104 + 72))) } if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104 + 60 + 4))&0x4>>2)) != 0 { mask |= _sqlite3WhereExprListUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(pSrc + 8 + uintptr(i)*104 + 88))) } 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)*104))).FiCursor pIdx = (*TTable)(unsafe.Pointer((*(*TSrcItem)(unsafe.Pointer(pFrom + 8 + uintptr(j)*104))).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 } if _sqlite3ExprCompareSkip(tls, pExpr, (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(i)*32))).FpExpr, iCur) == 0 && !(_sqlite3ExprIsConstant(tls, uintptr(0), (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(i)*32))).FpExpr) != 0) { *(*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. */ if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_VECTOR) && (op >= int32(TK_GT) && op <= int32(TK_GE)) { pExpr = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)) + 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)*104))).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(48) defer tls.Free(48) var c, v12 int8 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+24 */ [2]int32 var _ /* eOp2 at bp+16 */ uint8 var _ /* isComplete at bp+8 */ int32 var _ /* noCase at bp+12 */ int32 var _ /* pLeft at bp+40 */ uintptr var _ /* pRight at bp+32 */ 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 + 8)) = 0 /* RHS of LIKE/GLOB ends with wildcard */ *(*int32)(unsafe.Pointer(bp + 12)) = 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 + 16)) = uint8(0) /* Number of elements on left side vector */ if (*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0 { return } pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*56 pMaskSet = pWInfo + 592 pExpr = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr /* Because malloc() has not failed */ (*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) { 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 + 32))) } else { (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight = _sqlite3WhereExprListUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(pExpr + 32))) } 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 + 32)) != uintptr(0) { prereqAll = _sqlite3WhereExprUsageNN(tls, pMaskSet, pExpr) } else { prereqAll = prereqLeft | (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight } } if (*TWhereMaskSet)(unsafe.Pointer(pMaskSet)).FbVarSelect != 0 { p1 = pTerm + 18 *(*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 + 52))) 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+22850, 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+22850, 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 + 32))).FiField > 0 { pLeft = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pLeft + 32)) + 8 + uintptr((*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 32))).FiField-int32(1))*32))).FpExpr } if _exprMightBeIndexed(tls, pSrc, bp+24, pLeft, op) != 0 { (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor = (*(*[2]int32)(unsafe.Pointer(bp + 24)))[0] (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 32))).FleftColumn = (*(*[2]int32)(unsafe.Pointer(bp + 24)))[int32(1)] (*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator = uint16(int32(_operatorMask(tls, op)) & int32(opMask)) } if op == int32(TK_IS) { p3 = pTerm + 18 *(*Tu16)(unsafe.Pointer(p3)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p3))) | libc.Int32FromInt32(TERM_IS)) } if pRight != 0 && _exprMightBeIndexed(tls, pSrc, bp+24, pRight, op) != 0 && !((*TExpr)(unsafe.Pointer(pRight)).Fflags&uint32(libc.Int32FromInt32(EP_FixedCol)) != libc.Uint32FromInt32(0)) { eExtraOp = uint16(0) /* Extra bits for pNew->eOperator */ 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)*56 _markTermAsChild(tls, pWC, idxNew, idxTerm) if op == int32(TK_IS) { p4 = pNew + 18 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | libc.Int32FromInt32(TERM_IS)) } pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*56 p5 = pTerm + 18 *(*Tu16)(unsafe.Pointer(p5)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p5))) | libc.Int32FromInt32(TERM_COPIED)) if _termIsEquivalence(tls, pParse, pDup) != 0 { p6 = pTerm + 20 *(*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 + 18 *(*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 + 24)))[0] (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pNew + 32))).FleftColumn = (*(*[2]int32)(unsafe.Pointer(bp + 24)))[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) { (*TExpr)(unsafe.Pointer(pExpr)).Fop = uint8(TK_TRUEFALSE) /* See tag-20230504-1 */ *(*uintptr)(unsafe.Pointer(pExpr + 8)) = __ccgo_ts + 8364 *(*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) { pList = *(*uintptr)(unsafe.Pointer(pExpr + 32)) 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)*32))).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)*56 _markTermAsChild(tls, pWC, idxNew1, idxTerm) goto _8 _8: ; i++ } } else { if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_OR) { _exprAnalyzeOrTerm(tls, pSrc, pWC, idxTerm) pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*56 } 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)*56 (*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 + 32))).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)*56 p9 = pTerm + 18 *(*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+8, bp+12) != 0 { /* Name of collating sequence */ wtFlags = uint16(libc.Int32FromInt32(TERM_LIKEOPT) | libc.Int32FromInt32(TERM_VIRTUAL) | libc.Int32FromInt32(TERM_DYNAMIC)) pLeft2 = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)) + 8 + 1*32))).FpExpr pStr2 = _sqlite3ExprDup(tls, db, *(*uintptr)(unsafe.Pointer(bp)), 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 + 12)) != 0 && !((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FmallocFailed != 0) { p10 = pTerm + 18 *(*Tu16)(unsafe.Pointer(p10)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p10))) | libc.Int32FromInt32(TERM_LIKE)) i1 = 0 for { v12 = *(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)) + 8)) + uintptr(i1))) c = v12 if !(int32(v12) != 0) { break } *(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)) + 8)) + uintptr(i1))) = int8(int32(c) & ^(int32(_sqlite3CtypeMap[uint8(c)]) & libc.Int32FromInt32(0x20))) *(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pStr2 + 8)) + uintptr(i1))) = int8(_sqlite3UpperToLower[uint8(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 + 12)) != 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 + 8)) = 0 } c1 = _sqlite3UpperToLower[c1] } *(*Tu8)(unsafe.Pointer(pC)) = uint8(int32(c1) + int32(1)) } if *(*int32)(unsafe.Pointer(bp + 12)) != 0 { v13 = __ccgo_ts + 22891 } 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)*56 if *(*int32)(unsafe.Pointer(bp + 8)) != 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)*56 p17 = pTerm + 18 *(*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 + 32))).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 + 32)))).FpPrior == uintptr(0) || (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).FselFlags&uint32(SF_Values) != 0) && (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).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)*56 + 32 + 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 + 32)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 40)) = uintptr(0) res = _isAuxiliaryVtabOperator(tls, db, pExpr, bp+16, bp+40, bp+32) for { v19 = res res-- if !(v19 > 0) { break } prereqExpr = _sqlite3WhereExprUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(bp + 32))) prereqColumn = _sqlite3WhereExprUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(bp + 40))) if prereqExpr&prereqColumn == uint64(0) { pNewExpr2 = _sqlite3PExpr(tls, pParse, int32(TK_MATCH), uintptr(0), _sqlite3ExprDup(tls, db, *(*uintptr)(unsafe.Pointer(bp + 32)), 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 + 52)) = *(*int32)(unsafe.Pointer(pExpr + 52)) } idxNew5 = _whereClauseInsert(tls, pWC, pNewExpr2, uint16(libc.Int32FromInt32(TERM_VIRTUAL)|libc.Int32FromInt32(TERM_DYNAMIC))) pNewTerm1 = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxNew5)*56 (*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FprereqRight = prereqExpr (*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FleftCursor = (*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 40)))).FiTable (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pNewTerm1 + 32))).FleftColumn = int32((*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 40)))).FiColumn) (*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FeOperator = uint16(WO_AUX) (*TWhereTerm)(unsafe.Pointer(pNewTerm1)).FeMatchOp = *(*uint8)(unsafe.Pointer(bp + 16)) _markTermAsChild(tls, pWC, idxNew5, idxTerm) pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(idxTerm)*56 p20 = pTerm + 18 *(*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 + 40)) *(*uintptr)(unsafe.Pointer(bp + 40)) = *(*uintptr)(unsafe.Pointer(bp + 32)) *(*uintptr)(unsafe.Pointer(bp + 32)) = 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)*56 *(*TBitmask)(unsafe.Pointer(pTerm + 40)) |= 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 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(&(*TExpr)(unsafe.Pointer(pVal)).Fu)) = *(*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)*56 (*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 /* 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)*56))).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 */ goto _1 } if (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(ii)*56))).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)*56))).FleftCursor != iCsr { return } if (*(*TWhereTerm)(unsafe.Pointer((*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(ii)*56))).FprereqRight != uint64(0) { 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)*32))).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)*32))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_BIGNULL) != 0 { return } goto _2 _2: ; ii++ } } /* All conditions are met. Add the terms to the where-clause object. */ if (*TSelect)(unsafe.Pointer(p)).FiOffset != 0 && (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Compound) == uint32(0) { _whereAddLimitExpr(tls, pWC, (*TSelect)(unsafe.Pointer(p)).FiOffset, (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpLimit)).FpRight, iCsr, int32(SQLITE_INDEX_CONSTRAINT_OFFSET)) } if (*TSelect)(unsafe.Pointer(p)).FiOffset == 0 || (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Compound) == uint32(0) { _whereAddLimitExpr(tls, pWC, (*TSelect)(unsafe.Pointer(p)).FiLimit, (*TExpr)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpLimit)).FpLeft, iCsr, int32(SQLITE_INDEX_CONSTRAINT_LIMIT)) } } } // 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.Uint64FromInt64(448) / libc.Uint64FromInt64(56)) (*TWhereClause)(unsafe.Pointer(pWC)).Fa = pWC + 40 } // 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 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))*56 for int32(1) != 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 { _whereOrInfoDelete(tls, db, *(*uintptr)(unsafe.Pointer(a + 32))) } else { _whereAndInfoDelete(tls, db, *(*uintptr)(unsafe.Pointer(a + 32))) } } if a == aLast { break } a += 56 } } } // 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) } 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 + 32))) } else { if *(*uintptr)(unsafe.Pointer(p + 32)) != 0 { mask |= _sqlite3WhereExprListUsage(tls, pMaskSet, *(*uintptr)(unsafe.Pointer(p + 32))) } } } 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) { mask |= _sqlite3WhereExprListUsage(tls, pMaskSet, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 64)))).FpPartition) mask |= _sqlite3WhereExprListUsage(tls, pMaskSet, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 64)))).FpOrderBy) mask |= _sqlite3WhereExprUsage(tls, pMaskSet, (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 64)))).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) { 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)*32))).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 + 60 + 4))&0x4>>2)) == 0 { return } pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab pArgs = *(*uintptr)(unsafe.Pointer(pItem + 88)) 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)*16))).FcolFlags)&int32(COLFLAG_HIDDEN) == 0 { k++ } if k >= int32((*TTable)(unsafe.Pointer(pTab)).FnCol) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22898, 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) *(*uintptr)(unsafe.Pointer(pColRef + 64)) = pTab *(*TBitmask)(unsafe.Pointer(pItem + 80)) |= _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)*32))).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 + 68))&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 + 856 + uintptr(int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)-int32(1))*104 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 + 68))&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 + 856 + uintptr(i)*104 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) { 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.X__builtin___memcpy_chk(tls, aiCur, pWInfo+40, libc.Uint64FromInt64(4)*libc.Uint64FromInt32(2), libc.X__builtin_object_size(tls, aiCur, 0)) 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 + 68)) & 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.X__builtin___memcpy_chk(tls, pDest+8, pSrc+8, uint64((*TWhereOrSet)(unsafe.Pointer(pDest)).Fn)*uint64(16), libc.X__builtin_object_size(tls, pDest+8, 0)) } // 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 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.X__builtin___memcpy_chk(tls, pNew, pOld, (*TWhereMemBlock)(unsafe.Pointer(pOldBlk)).Fsz, libc.X__builtin_object_size(tls, pNew, 0)) } 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 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 // // /* // ** Term pTerm is guaranteed to be a WO_IN term. It may be a component term // ** of a vector IN expression of the form "(x, y, ...) IN (SELECT ...)". // ** This function checks to see if the term is compatible with an index // ** column with affinity idxaff (one of the SQLITE_AFF_XYZ values). If so, // ** it returns a pointer to the name of the collation sequence (e.g. "BINARY" // ** or "NOCASE") used by the comparison in pTerm. If it is not compatible // ** with affinity idxaff, NULL is returned. // */ func _indexInAffinityOk(tls *libc.TLS, pParse uintptr, pTerm uintptr, idxaff Tu8) (r uintptr) { bp := tls.Alloc(80) defer tls.Free(80) var iField int32 var pRet, pX, v1 uintptr var _ /* inexpr at bp+0 */ TExpr _, _, _, _ = iField, pRet, pX, v1 pX = (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr if _sqlite3ExprIsVector(tls, (*TExpr)(unsafe.Pointer(pX)).FpLeft) != 0 { iField = (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 32))).FiField - int32(1) (*(*TExpr)(unsafe.Pointer(bp))).Fflags = uint32(0) (*(*TExpr)(unsafe.Pointer(bp))).Fop = uint8(TK_EQ) (*(*TExpr)(unsafe.Pointer(bp))).FpLeft = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(pX)).FpLeft + 32)) + 8 + uintptr(iField)*32))).FpExpr (*(*TExpr)(unsafe.Pointer(bp))).FpRight = (*(*TExprList_item)(unsafe.Pointer((*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pX + 32)))).FpEList + 8 + uintptr(iField)*32))).FpExpr pX = bp } if _sqlite3IndexAffinityOk(tls, pX, int8(idxaff)) != 0 { pRet = _sqlite3ExprCompareCollSeq(tls, pParse, pX) if pRet != 0 { v1 = (*TCollSeq)(unsafe.Pointer(pRet)).FzName } else { v1 = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) } return v1 } 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, zCollName, v2, v5 uintptr var v3, v6 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _ = iColumn, iCur, j, k, pColl, pParse, pTerm, pWC, pX, zCollName, v2, v3, v5, v6 /* The term being tested */ k = (*TWhereScan)(unsafe.Pointer(pScan)).Fk /* Where to start scanning */ pWC = (*TWhereScan)(unsafe.Pointer(pScan)).FpWC for int32(1) != 0 { iColumn = *(*Ti16)(unsafe.Pointer(pScan + 88 + uintptr(int32((*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv)-int32(1))*2)) iCur = *(*int32)(unsafe.Pointer(pScan + 44 + uintptr(int32((*TWhereScan)(unsafe.Pointer(pScan)).FiEquiv)-int32(1))*4)) for cond := true; cond; cond = pWC != uintptr(0) { pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr(k)*56 for { if !(k < (*TWhereClause)(unsafe.Pointer(pWC)).FnTerm) { break } if (*TWhereTerm)(unsafe.Pointer(pTerm)).FleftCursor == iCur && (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 32))).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.Uint64FromInt64(44)/libc.Uint64FromInt64(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 + 44 + uintptr(j)*4)) == (*TExpr)(unsafe.Pointer(pX)).FiTable && int32(*(*Ti16)(unsafe.Pointer(pScan + 88 + 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 + 44 + uintptr(j)*4)) = (*TExpr)(unsafe.Pointer(pX)).FiTable *(*Ti16)(unsafe.Pointer(pScan + 88 + 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 int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&int32(WO_IN) != 0 { zCollName = _indexInAffinityOk(tls, pParse, pTerm, uint8((*TWhereScan)(unsafe.Pointer(pScan)).Fidxaff)) if !(zCollName != 0) { goto _1 } } else { if !(_sqlite3IndexAffinityOk(tls, pX, (*TWhereScan)(unsafe.Pointer(pScan)).Fidxaff) != 0) { goto _1 } pColl = _sqlite3ExprCompareCollSeq(tls, pParse, pX) if pColl != 0 { v5 = (*TCollSeq)(unsafe.Pointer(pColl)).FzName } else { v5 = uintptr(unsafe.Pointer(&_sqlite3StrBINARY)) } zCollName = v5 } if _sqlite3StrICmp(tls, zCollName, (*TWhereScan)(unsafe.Pointer(pScan)).FzCollName) != 0 { goto _1 } } if v6 = int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FeOperator)&(libc.Int32FromInt32(WO_EQ)|libc.Int32FromInt32(WO_IS)) != 0; v6 { pX = (*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpRight } if v6 && pX != libc.UintptrFromInt32(0) && int32((*TExpr)(unsafe.Pointer(pX)).Fop) == int32(TK_COLUMN) && (*TExpr)(unsafe.Pointer(pX)).FiTable == *(*int32)(unsafe.Pointer(pScan + 44)) && int32((*TExpr)(unsafe.Pointer(pX)).FiColumn) == int32(*(*Ti16)(unsafe.Pointer(pScan + 88))) { goto _1 } (*TWhereScan)(unsafe.Pointer(pScan)).FpWC = pWC (*TWhereScan)(unsafe.Pointer(pScan)).Fk = k + int32(1) return pTerm } } goto _1 _1: ; k++ pTerm += 56 } 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 = 0 (*TWhereScan)(unsafe.Pointer(pScan)).FzCollName = uintptr(0) (*TWhereScan)(unsafe.Pointer(pScan)).FopMask = opMask (*TWhereScan)(unsafe.Pointer(pScan)).Fk = 0 *(*int32)(unsafe.Pointer(pScan + 44)) = 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)*16))).Faffinity (*TWhereScan)(unsafe.Pointer(pScan)).FzCollName = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(j)*8)) } else { if iColumn == -int32(2) { (*TWhereScan)(unsafe.Pointer(pScan)).FpIdxExpr = (*(*TExprList_item)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FaColExpr + 8 + uintptr(j)*32))).FpExpr (*TWhereScan)(unsafe.Pointer(pScan)).FzCollName = *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(j)*8)) *(*Ti16)(unsafe.Pointer(pScan + 88)) = int16(-libc.Int32FromInt32(2)) return _whereScanInitIndexExpr(tls, pScan) } } } } else { if iColumn == -int32(2) { return uintptr(0) } } *(*Ti16)(unsafe.Pointer(pScan + 88)) = 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(112) defer tls.Free(112) 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)*8)) i = 0 for { if !(i < (*TExprList)(unsafe.Pointer(pList)).FnExpr) { break } p = _sqlite3ExprSkipCollateAndLikely(tls, (*(*TExprList_item)(unsafe.Pointer(pList + 8 + uintptr(i)*32))).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)*32))).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 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)*16 + 8)) & 0xf >> 0)) } else { if j == -int32(1) { return int32(1) } else { 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)*32))).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 += 24 } } /* ** 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) { /* 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 + 52)) != (*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 int8 _ = 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 } 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 } if (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 32))).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 + 32))).FleftColumn)*16))).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 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)*104 pTable = (*TSrcItem)(unsafe.Pointer(pSrc)).FpTab pWCEnd = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((*TWhereClause)(unsafe.Pointer(pWC)).FnTerm)*56 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) != 0 { pPartial = _sqlite3ExprAnd(tls, pParse, pPartial, _sqlite3ExprDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pExpr, 0)) } if _termCanDriveIndex(tls, pTerm, pSrc, notReady) != 0 { iCol = (*(*struct { FleftColumn int32 FiField int32 })(unsafe.Pointer(pTerm + 32))).FleftColumn if iCol >= int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(8)) { v2 = libc.Uint64FromInt32(1) << (int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(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.Uint64FromInt64(8)*libc.Uint64FromInt32(8)) { v8 = libc.Uint64FromInt32(1) << (int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(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 + 32))).FleftColumn) pColl = _sqlite3ExprCompareCollSeq(tls, pParse, pX) /* 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)*8)) = 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 += 56 } /* 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 + 48)) |= 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 + 60 + 4))&0x20>>5)) != 0 { _sqlite3VdbeChangeP2(tls, v, addrCounter, regBase+n) _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+60+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) 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 + 56 *(*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)*104 pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab 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)*56 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) != 0 { _sqlite3ExprIfFalse(tls, pParse, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr, addrCont, int32(SQLITE_JUMPIFNULL)) } goto _3 _3: ; pTerm += 56 } 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 } _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 + 48)) &= 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 + 856 + uintptr(iLevel)*104 pTabItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*104 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 pTab = (*TSrcItem)(unsafe.Pointer(pSrc)).FpTab /* 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 + 18 *(*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 } 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 } 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 + 18 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | libc.Int32FromInt32(TERM_OK)) goto _1 _1: ; i++ pTerm += 56 } /* 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)*32))).FpExpr /* Skip over constant terms in the ORDER BY clause */ if _sqlite3ExprIsConstant(tls, uintptr(0), pExpr) != 0 { goto _5 } /* Virtual tables are unable to deal with NULLS FIRST */ if int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*32))).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 { 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 */ (*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)*16) 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 && !(int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 60 + 4))&0x4000>>14)) != 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(96)+(libc.Uint64FromInt64(12)+libc.Uint64FromInt64(8))*uint64(nTerm)+uint64(8)*uint64(nOrderBy)+uint64(40)+uint64(8)*uint64(nTerm)) if pIdxInfo == uintptr(0) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+1634, 0) return uintptr(0) } pHidden = pIdxInfo + 1*96 pIdxCons = pHidden + 32 + uintptr(nTerm)*8 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 + 32))).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 + 20)) |= 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. */ 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+3863, libc.VaList(bp+8, _sqlite3ErrStr(tls, rc))) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+3863, libc.VaList(bp+8, (*Tsqlite3_vtab)(unsafe.Pointer(pVtab)).FzErrMsg)) } } } if (*TVTable)(unsafe.Pointer((*(*struct { FnArg int32 FazArg uintptr Fp uintptr })(unsafe.Pointer(pTab + 64))).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 /* 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 + 100))&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))*40))).FanLt + uintptr(n-int32(1))*8)) != *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(iSamp)*40))).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)*40))).Fn, (*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(iSamp)*40))).Fp, pRec) if res < 0 { iLower = *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(iSamp)*40))).FanLt + uintptr(n-int32(1))*8)) + *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(iSamp)*40))).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)*40))).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 */ *(*TtRowcnt)(unsafe.Pointer(aStat)) = *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(i)*40))).FanLt + uintptr(iCol)*8)) *(*TtRowcnt)(unsafe.Pointer(aStat + 1*8)) = *(*TtRowcnt)(unsafe.Pointer((*(*TIndexSample)(unsafe.Pointer(aSample + uintptr(i)*40))).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)*40))).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)) } } } 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 int8) { if !((*TIndex)(unsafe.Pointer(pIdx)).FzColAff != 0) { if _sqlite3IndexAffinityStr(tls, db, pIdx) == uintptr(0) { return int8(SQLITE_AFF_BLOB) } } return *(*int8)(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(32) defer tls.Free(32) 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+8 */ uintptr var _ /* pVal at bp+16 */ 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 = uint8(_sqlite3IndexColumnAffinity(tls, db, p, nEq)) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Value extracted from pLower */ *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) /* Value extracted from pUpper */ *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) /* Value extracted from record */ pColl = _sqlite3LocateCollSeq(tls, pParse, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(p)).FazColl + uintptr(nEq)*8))) 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+8) if *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 { v1 = 0 } else { v1 = (*TIndex)(unsafe.Pointer(p)).FnSample } nUpper = v1 } if *(*uintptr)(unsafe.Pointer(bp)) != 0 || *(*uintptr)(unsafe.Pointer(bp + 8)) != 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)*40))).Fp, (*(*TIndexSample)(unsafe.Pointer((*TIndex)(unsafe.Pointer(p)).FaSample + uintptr(i)*40))).Fn, nEq, bp+16) if rc == SQLITE_OK && *(*uintptr)(unsafe.Pointer(bp)) != 0 { res = _sqlite3MemCompare(tls, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 16)), pColl) if res >= 0 { nLower++ } } if rc == SQLITE_OK && *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 { res1 = _sqlite3MemCompare(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), *(*uintptr)(unsafe.Pointer(bp + 16)), 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 { } _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(bp))) _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) 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)] } 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)) } } else { nNew = int16(10) } 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 } } } 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 /* 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 */ 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)*32))).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 } 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 + 80 (*TWhereLoop)(unsafe.Pointer(p)).FnLTerm = uint16(0) (*TWhereLoop)(unsafe.Pointer(p)).FnLSlot = uint16(int32(libc.Uint64FromInt64(24) / libc.Uint64FromInt64(8))) (*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+80 { _sqlite3DbFreeNN(tls, db, (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm) (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm = p + 80 (*TWhereLoop)(unsafe.Pointer(p)).FnLSlot = uint16(int32(libc.Uint64FromInt64(24) / libc.Uint64FromInt64(8))) } _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(8)*uint64(n)) if paNew == uintptr(0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memcpy_chk(tls, paNew, (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm, uint64(8)*uint64((*TWhereLoop)(unsafe.Pointer(p)).FnLSlot), libc.X__builtin_object_size(tls, paNew, 0)) if (*TWhereLoop)(unsafe.Pointer(p)).FaLTerm != p+80 { _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.X__builtin___memset_chk(tls, pTo, 0, uint64(libc.UintptrFromInt32(0)+56), libc.X__builtin_object_size(tls, pTo, 0)) return int32(SQLITE_NOMEM) } libc.X__builtin___memcpy_chk(tls, pTo, pFrom, uint64(libc.UintptrFromInt32(0)+56), libc.X__builtin_object_size(tls, pTo, 0)) libc.X__builtin___memcpy_chk(tls, (*TWhereLoop)(unsafe.Pointer(pTo)).FaLTerm, (*TWhereLoop)(unsafe.Pointer(pFrom)).FaLTerm, uint64((*TWhereLoop)(unsafe.Pointer(pTo)).FnLTerm)*uint64(8), libc.X__builtin_object_size(tls, (*TWhereLoop)(unsafe.Pointer(pTo)).FaLTerm, 0)) 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) { _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 _sqlite3WhereClauseClear(tls, pWInfo+104) 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) */ 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)*8)) == 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)*8)) == *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pX)).FaLTerm + uintptr(i)*8)) { 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. */ /* whereLoopAddBtree() always generates and inserts the automatic index ** case first. Hence compatible candidate WhereLoops never have a larger ** rSetup. Call this SETUP-INVARIANT */ /* 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) { /* SETUP-INVARIANT above */ break /* Cause p to be overwritten by pTemplate */ } goto _1 _1: ; ppPrev = p + 72 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+80, 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(104)) 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 + 72 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 + 100))&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 */ i = (*TWhereClause)(unsafe.Pointer(pWC)).FnBase pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa for { if !(i > 0) { break } 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)*8)) 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)*56 == 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)*104))).Ffg.Fjointype)&(libc.Int32FromInt32(JT_LEFT)|libc.Int32FromInt32(JT_LTORJ)) == 0 { *(*Tu32)(unsafe.Pointer(pLoop + 48)) |= 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 + 18 *(*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 += 56 } 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 int8 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 = 0 pLhs = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer((*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr)).FpLeft + 32)) + 8 + uintptr(i)*32))).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 + 32)))).FpEList + 8 + uintptr(i)*32))).FpExpr } else { pRhs = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pRhs + 32)) + 8 + uintptr(i)*32))).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)*8))) != 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(128) defer tls.Free(128) 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+112 */ 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 if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 { return (*TParse)(unsafe.Pointer(pParse)).Frc } 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 { 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 + 100))&0x4>>2)) != 0 || int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 100))&0x100>>8)) != 0 { if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 100))&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 + 100))&0x100>>8)) != 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 60 + 4))&0x2>>1)) == 0 { opMask &= ^(libc.Int32FromInt32(WO_EQ) | libc.Int32FromInt32(WO_IN) | libc.Int32FromInt32(WO_IS)) } } 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 + 44 *(*uint8)(unsafe.Pointer(p2)) = uint8(int32(*(*uint8)(unsafe.Pointer(p2))) | libc.Int32FromInt32(SQLITE_BLDF1_UNIQUE)) } else { p3 = pBuilder + 44 *(*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 + 52 v4 = *(*Tu16)(unsafe.Pointer(v5)) *(*Tu16)(unsafe.Pointer(v5))++ *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(v4)*8)) = pTerm (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = (saved_prereq | (*TWhereTerm)(unsafe.Pointer(pTerm)).FprereqRight) & ^(*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf 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) /* 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)*8)) != 0 && (*TWhereTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(i)*8)))).FpExpr == pExpr { nIn = 0 } goto _6 _6: ; i++ } } else { if *(*uintptr)(unsafe.Pointer(pExpr + 32)) != 0 && (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).FnExpr != 0 { /* "x IN (value, value, ...)" */ nIn = int32(_sqlite3LogEst(tls, uint64((*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).FnExpr))) } } if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 100))&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 + 48)) |= uint32(WHERE_IN_SEEKSCAN) } else { goto _1 } } } *(*Tu32)(unsafe.Pointer(pNew + 48)) |= 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 + 48)) |= uint32(WHERE_COLUMN_EQ) 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 + 100))&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 + 48)) |= uint32(WHERE_ONEROW) } else { *(*Tu32)(unsafe.Pointer(pNew + 48)) |= uint32(WHERE_UNQ_WANTED) } } if int32((*(*TWhereScan)(unsafe.Pointer(bp))).FiEquiv) > int32(1) { *(*Tu32)(unsafe.Pointer(pNew + 48)) |= uint32(WHERE_TRANSCONS) } } else { if int32(eOp)&int32(WO_ISNULL) != 0 { *(*Tu32)(unsafe.Pointer(pNew + 48)) |= 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 + 48)) |= 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*56 if _whereLoopResize(tls, db, pNew, int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnLTerm)+int32(1)) != 0 { break } /* OOM */ v8 = pNew + 52 v7 = *(*Tu16)(unsafe.Pointer(v8)) *(*Tu16)(unsafe.Pointer(v8))++ *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(v7)*8)) = pTop *(*Tu32)(unsafe.Pointer(pNew + 48)) |= uint32(WHERE_TOP_LIMIT) (*TWhereLoop)(unsafe.Pointer(pNew)).Fu.Fbtree.FnTop = uint16(1) } } else { *(*Tu32)(unsafe.Pointer(pNew + 48)) |= 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))*8)) } 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). */ 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) if int32((*TWhereTerm)(unsafe.Pointer(pTerm)).FtruthProb) <= 0 && int32(*(*Ti16)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiColumn + uintptr(saved_nEq)*2))) >= 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 + 112)) = 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+112) } else { rc = _whereInScanEst(tls, pParse, pBuilder, *(*uintptr)(unsafe.Pointer(pExpr1 + 32)), bp+112) } if rc == int32(SQLITE_NOTFOUND) { rc = SQLITE_OK } if rc != SQLITE_OK { break } /* Jump out of the pTerm loop */ if *(*TtRowcnt)(unsafe.Pointer(bp + 112)) != 0 { (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = _sqlite3LogEst(tls, *(*TtRowcnt)(unsafe.Pointer(bp + 112))) if nEq == int32(1) && int32((*TWhereLoop)(unsafe.Pointer(pNew)).FnOut)+int32(10) > int32(*(*TLogEst)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pProbe)).FaiRowLogEst))) { p14 = pTerm + 18 *(*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 + 45 *(*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 + 112)) == 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 estimated cost of visiting selected rows in the ** index. The estimate is the sum of two values: ** 1. The cost of doing one search-by-key to find the first matching ** entry ** 2. Stepping forward in the index pNew->nOut times to find all ** additional matching entries. */ if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 100))&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)) } rCostIdx = _sqlite3LogEstAdd(tls, rLogSize, rCostIdx) /* Estimate the cost of running the loop. If all data is coming ** from the index, then this is just the cost of doing the index ** lookup and scan. But if some data is coming out of the main table, ** we also have to add in the cost of doing pNew->nOut searches to ** locate the row in the main table that corresponds to the index entry. */ (*TWhereLoop)(unsafe.Pointer(pNew)).FrRun = 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 + 100))&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. */ 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 + 100))&0x40>>6)) == 0 && int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 100))&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 + 52 v23 = *(*Tu16)(unsafe.Pointer(v24)) *(*Tu16)(unsafe.Pointer(v24))++ *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(v23)*8)) = uintptr(0) *(*Tu32)(unsafe.Pointer(pNew + 48)) |= 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 + 100))&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)*32))).FpExpr) if pExpr == uintptr(0) { goto _2 } if (int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_COLUMN) || int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_AGG_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)*32))).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 + 52)) == 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 += 56 } 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)*32))).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 + 40)) 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 + 40)))).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 + 100))&0x1000>>12)) != 0 && _exprIsCoveredByIndex(tls, pExpr, pIdx, (*TCoveringIndexCheck1)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pWalk + 40)))).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(64) defer tls.Free(64) var i, rc int32 var _ /* ck at bp+0 */ TCoveringIndexCheck1 var _ /* w at bp+16 */ 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 + 100))&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.Uint64FromInt64(8)*libc.Uint64FromInt32(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.X__builtin___memset_chk(tls, bp+16, 0, uint64(48), libc.X__builtin_object_size(tls, bp+16, 0)) (*(*TWalker)(unsafe.Pointer(bp + 16))).FxExprCallback = __ccgo_fp(_whereIsCoveringIndexWalkCallback) (*(*TWalker)(unsafe.Pointer(bp + 16))).FxSelectCallback = __ccgo_fp(_sqlite3SelectWalkNoop) *(*uintptr)(unsafe.Pointer(bp + 16 + 40)) = bp _sqlite3WalkSelect(tls, bp+16, (*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 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, uintptr(0), pRight) != 0) { return } if !(_sqlite3IsBinary(tls, _sqlite3ExprCompareCollSeq(tls, pParse, pPart)) != 0) { return } if int32((*TExpr)(unsafe.Pointer(pLeft)).FiColumn) < 0 { return } aff = uint8((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr((*TExpr)(unsafe.Pointer(pLeft)).FiColumn)*16))).Faffinity) if int32(aff) >= int32(SQLITE_AFF_TEXT) { if pItem != 0 { db = (*TParse)(unsafe.Pointer(pParse)).Fdb p = _sqlite3DbMallocRaw(tls, db, uint64(32)) 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 + 104 _sqlite3ParserAddCleanup(tls, pParse, __ccgo_fp(_whereIndexedExprCleanup), pArg) } } } else { if int32((*TExpr)(unsafe.Pointer(pLeft)).FiColumn) < int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(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(176) defer tls.Free(176) 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+164 */ Ti16 var _ /* aiRowEstPk at bp+160 */ [2]TLogEst var _ /* m at bp+168 */ 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 + 164)) = 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)*104 pTab = (*TSrcItem)(unsafe.Pointer(pSrc)).FpTab pWC = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpWC if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 60 + 4))&0x2>>1)) != 0 { /* An INDEXED BY clause specifies a particular index to use */ pProbe = *(*uintptr)(unsafe.Pointer(pSrc + 96)) } 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.X__builtin___memset_chk(tls, bp, 0, uint64(160), libc.X__builtin_object_size(tls, bp, 0)) (*(*TIndex)(unsafe.Pointer(bp))).FnKeyCol = uint16(1) (*(*TIndex)(unsafe.Pointer(bp))).FnColumn = uint16(1) (*(*TIndex)(unsafe.Pointer(bp))).FaiColumn = bp + 164 (*(*TIndex)(unsafe.Pointer(bp))).FaiRowLogEst = bp + 160 (*(*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+100, libc.Uint32FromInt32(SQLITE_IDXTYPE_IPK), 0, 0x3) (*(*[2]TLogEst)(unsafe.Pointer(bp + 160)))[0] = (*TTable)(unsafe.Pointer(pTab)).FnRowLogEst (*(*[2]TLogEst)(unsafe.Pointer(bp + 160)))[int32(1)] = 0 pFirst = (*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pSrc)).FpTab)).FpIndex if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 60 + 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 + 60 + 4))&0x2>>1)) != 0) && !(int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 60 + 4))&0x1>>0)) != 0) && (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_WithoutRowid) == uint32(0) && !(int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 60 + 4))&0x8>>3)) != 0) && !(int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 60 + 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)*56 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) (*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 += 56 } } /* 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 + 100))&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 */ if int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 100))&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 + 100))&0x20>>5)) != 0 { *(*TBitmask)(unsafe.Pointer(bp + 168)) = uint64(0) (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(libc.Int32FromInt32(WHERE_IDX_ONLY) | libc.Int32FromInt32(WHERE_INDEXED)) } else { *(*TBitmask)(unsafe.Pointer(bp + 168)) = (*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+168, 0, uintptr(0)) } (*TWhereLoop)(unsafe.Pointer(pNew)).FwsFlags = uint32(WHERE_INDEXED) if *(*TBitmask)(unsafe.Pointer(bp + 168)) == libc.Uint64FromInt32(1)<<(int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(8))-libc.Int32FromInt32(1)) || int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 100))&0x1000>>12)) != 0 && !(int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 100))&0x800>>11)) != 0) && *(*TBitmask)(unsafe.Pointer(bp + 168)) != uint64(0) { isCov = _whereIsCoveringIndex(tls, pWInfo, pProbe, (*TSrcItem)(unsafe.Pointer(pSrc)).FiCursor) if isCov == uint32(0) { } else { *(*TBitmask)(unsafe.Pointer(bp + 168)) = uint64(0) *(*Tu32)(unsafe.Pointer(pNew + 48)) |= isCov if isCov&uint32(WHERE_IDX_ONLY) != 0 { } else { } } } else { if *(*TBitmask)(unsafe.Pointer(bp + 168)) == 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 + 60 + 4))&0x2>>1)) != 0 || *(*TBitmask)(unsafe.Pointer(bp + 168)) == uint64(0) && int32(uint32(*(*uint16)(unsafe.Pointer(pProbe + 100))&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 + 168)) != 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 + 104 ii = 0 for { if !(ii < (*TWhereClause)(unsafe.Pointer(pWC2)).FnTerm) { break } pTerm1 = (*TWhereClause)(unsafe.Pointer(pWC2)).Fa + uintptr(ii)*56 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 + 48)) |= uint32(TF_MaybeReanalyze) } _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 + 60 + 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) { 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 // // /* // ** Return true if the first nCons constraints in the pUsage array are // ** marked as in-use (have argvIndex>0). False otherwise. // */ func _allConstraintsUsed(tls *libc.TLS, aUsage uintptr, nCons int32) (r int32) { var ii int32 _ = ii ii = 0 for { if !(ii < nCons) { break } if (*(*Tsqlite3_index_constraint_usage)(unsafe.Pointer(aUsage + uintptr(ii)*8))).FargvIndex <= 0 { return 0 } goto _1 _1: ; ii++ } return int32(1) } // 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*96 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)*104 nConstraint = (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint *(*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 + 8)) i = 0 for { if !(i < nConstraint) { break } pTerm = (*TWhereClause)(unsafe.Pointer(pWC)).Fa + uintptr((*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons)).FiTermOffset)*56 (*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.X__builtin___memset_chk(tls, pUsage, 0, uint64(8)*uint64(nConstraint), libc.X__builtin_object_size(tls, pUsage, 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.X__builtin___memset_chk(tls, (*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm, 0, uint64(8)*uint64(nConstraint), libc.X__builtin_object_size(tls, (*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm, 0)) libc.X__builtin___memset_chk(tls, pNew+24, 0, uint64(24), libc.X__builtin_object_size(tls, pNew+24, 0)) pIdxCons = *(*uintptr)(unsafe.Pointer(pIdxInfo + 8)) 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)*8)) != uintptr(0) || int32((*Tsqlite3_index_constraint)(unsafe.Pointer(pIdxCons)).Fusable) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+22971, 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)*56 *(*TBitmask)(unsafe.Pointer(pNew)) |= (*TWhereTerm)(unsafe.Pointer(pTerm1)).FprereqRight *(*uintptr)(unsafe.Pointer((*TWhereLoop)(unsafe.Pointer(pNew)).FaLTerm + uintptr(iTerm)*8)) = 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*96 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)*56))).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*96 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 + 24)) &= ^m } else { if bHandle > 0 { *(*Tu32)(unsafe.Pointer(pHidden + 24)) |= 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*96 pVal = uintptr(0) rc = SQLITE_OK if iCons < 0 || iCons >= (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint { rc = _sqlite3MisuseError(tls, int32(166453)) /* EV: R-30545-25046 */ } else { if *(*uintptr)(unsafe.Pointer(pH + 32 + uintptr(iCons)*8)) == 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)*56 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+32+uintptr(iCons)*8) } pVal = *(*uintptr)(unsafe.Pointer(pH + 32 + uintptr(iCons)*8)) } *(*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*96 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 */ 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)*104 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 { 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)) 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)) 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)*56))).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(720) defer tls.Free(720) var i, iCur, j, once, rc int32 var pItem, pNew, pOrTerm, pOrWC, pOrWCEnd, pTerm, pWC, pWCEnd, pWInfo uintptr var _ /* sCur at bp+600 */ TWhereOrSet var _ /* sPrev at bp+656 */ TWhereOrSet var _ /* sSubBuild at bp+488 */ TWhereLoopBuilder var _ /* sSum at bp+544 */ 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)*56 pNew = (*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)).FpNew libc.X__builtin___memset_chk(tls, bp+544, 0, uint64(56), libc.X__builtin_object_size(tls, bp+544, 0)) pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pNew)).FiTab)*104 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(pTerm + 32)))).Findexable&(*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf != uint64(0) { pOrWC = *(*uintptr)(unsafe.Pointer(pTerm + 32)) pOrWCEnd = (*TWhereClause)(unsafe.Pointer(pOrWC)).Fa + uintptr((*TWhereClause)(unsafe.Pointer(pOrWC)).FnTerm)*56 once = int32(1) *(*TWhereLoopBuilder)(unsafe.Pointer(bp + 488)) = *(*TWhereLoopBuilder)(unsafe.Pointer(pBuilder)) (*(*TWhereLoopBuilder)(unsafe.Pointer(bp + 488))).FpOrSet = bp + 600 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 + 488))).FpWC = *(*uintptr)(unsafe.Pointer(pOrTerm + 32)) } 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 + 488))).FpWC = bp } else { goto _2 } } (*(*TWhereOrSet)(unsafe.Pointer(bp + 600))).Fn = uint16(0) if int32((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pItem)).FpTab)).FeTabType) == int32(TABTYP_VTAB) { rc = _whereLoopAddVirtual(tls, bp+488, mPrereq, mUnusable) } else { rc = _whereLoopAddBtree(tls, bp+488, mPrereq) } if rc == SQLITE_OK { rc = _whereLoopAddOr(tls, bp+488, mPrereq, mUnusable) } if int32((*(*TWhereOrSet)(unsafe.Pointer(bp + 600))).Fn) == 0 { (*(*TWhereOrSet)(unsafe.Pointer(bp + 544))).Fn = uint16(0) break } else { if once != 0 { _whereOrMove(tls, bp+544, bp+600) once = 0 } else { _whereOrMove(tls, bp+656, bp+544) (*(*TWhereOrSet)(unsafe.Pointer(bp + 544))).Fn = uint16(0) i = 0 for { if !(i < int32((*(*TWhereOrSet)(unsafe.Pointer(bp + 656))).Fn)) { break } j = 0 for { if !(j < int32((*(*TWhereOrSet)(unsafe.Pointer(bp + 600))).Fn)) { break } _whereOrInsert(tls, bp+544, (*(*TWhereOrCost)(unsafe.Pointer(bp + 656 + 8 + uintptr(i)*16))).Fprereq|(*(*TWhereOrCost)(unsafe.Pointer(bp + 600 + 8 + uintptr(j)*16))).Fprereq, _sqlite3LogEstAdd(tls, (*(*TWhereOrCost)(unsafe.Pointer(bp + 656 + 8 + uintptr(i)*16))).FrRun, (*(*TWhereOrCost)(unsafe.Pointer(bp + 600 + 8 + uintptr(j)*16))).FrRun), _sqlite3LogEstAdd(tls, (*(*TWhereOrCost)(unsafe.Pointer(bp + 656 + 8 + uintptr(i)*16))).FnOut, (*(*TWhereOrCost)(unsafe.Pointer(bp + 600 + 8 + uintptr(j)*16))).FnOut)) goto _4 _4: ; j++ } goto _3 _3: ; i++ } } } goto _2 _2: ; pOrTerm += 56 } (*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.X__builtin___memset_chk(tls, pNew+24, 0, uint64(24), libc.X__builtin_object_size(tls, pNew+24, 0)) i = 0 for { if !(rc == SQLITE_OK && i < int32((*(*TWhereOrSet)(unsafe.Pointer(bp + 544))).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 + 544 + 8 + uintptr(i)*16))).FrRun) + int32(1)) (*TWhereLoop)(unsafe.Pointer(pNew)).FnOut = (*(*TWhereOrCost)(unsafe.Pointer(bp + 544 + 8 + uintptr(i)*16))).FnOut (*TWhereLoop)(unsafe.Pointer(pNew)).Fprereq = (*(*TWhereOrCost)(unsafe.Pointer(bp + 544 + 8 + uintptr(i)*16))).Fprereq rc = _whereLoopInsert(tls, pBuilder, pNew) goto _5 _5: ; i++ } } goto _1 _1: ; pTerm += 56 } 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)*104 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 */ (*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 + 48)) += uint32(SQLITE_QUERY_PLANNER_LIMIT_INCR) (*TWhereLoop)(unsafe.Pointer(pNew)).FmaskSelf = _sqlite3WhereGetMask(tls, pWInfo+592, (*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*104 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+592, (*TSrcItem)(unsafe.Pointer(p)).FiCursor) } goto _2 _2: ; p += 104 } 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+22997, 0) rc = SQLITE_OK } else { break } } goto _1 _1: ; iTab++ pItem += 104 } _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. */ 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.Uint64FromInt64(8)*libc.Uint64FromInt32(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)*8)) 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)*104))).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)*32))).FpExpr) pColl2 = _sqlite3ExprCompareCollSeq(tls, pParse, (*TWhereTerm)(unsafe.Pointer(pTerm)).FpExpr) 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 + 100))&0x4>>2)) != 0 { return 0 } else { nKeyCol = (*TIndex)(unsafe.Pointer(pIndex)).FnKeyCol nColumn = (*TIndex)(unsafe.Pointer(pIndex)).FnColumn /* 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 */ 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)*8)))).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)*8)))).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)*8)))).FpExpr == pX { 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)*16 + 8))&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)*32))).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)*32))).FpExpr) if _sqlite3StrICmp(tls, (*TCollSeq)(unsafe.Pointer(pColl)).FzName, *(*uintptr)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIndex)).FazColl + uintptr(j)*8))) != 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)*32))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_DESC) { isMatch = uint8(0) } } else { rev = uint8(int32(revIdx) ^ int32((*(*TExprList_item)(unsafe.Pointer(pOrderBy + 8 + uintptr(i)*32))).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)*32))).Ffg.FsortFlags)&int32(KEYINFO_ORDER_BIGNULL) != 0 { if j == int32((*TWhereLoop)(unsafe.Pointer(pLoop)).Fu.Fbtree.FnEq) { *(*Tu32)(unsafe.Pointer(pLoop + 48)) |= 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.Uint64FromInt64(8)*libc.Uint64FromInt32(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 /* 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)) } } } 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 /* 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((uint64(32) + uint64(8)*uint64(nLoop)) * uint64(mxChoice) * uint64(2)) nSpace = int32(uint64(nSpace) + libc.Uint64FromInt64(2)*uint64(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.X__builtin___memset_chk(tls, aFrom, 0, uint64(32), libc.X__builtin_object_size(tls, aFrom, 0)) 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) * 8 } 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.X__builtin___memset_chk(tls, aSortCost, 0, uint64(2)*uint64(nOrderBy), libc.X__builtin_object_size(tls, aSortCost, 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) nFrom = int32(1) 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. */ 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.X__builtin___memcpy_chk(tls, (*TWherePath)(unsafe.Pointer(pTo)).FaLoop, (*TWherePath)(unsafe.Pointer(pFrom)).FaLoop, uint64(8)*uint64(iLoop), libc.X__builtin_object_size(tls, (*TWherePath)(unsafe.Pointer(pTo)).FaLoop, 0)) *(*uintptr)(unsafe.Pointer((*TWherePath)(unsafe.Pointer(pTo)).FaLoop + uintptr(iLoop)*8)) = 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+23032, 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++ } /* Load the lowest cost path into pWInfo */ iLoop = 0 for { if !(iLoop < nLoop) { break } pLevel = pWInfo + 856 + uintptr(iLoop)*104 v14 = *(*uintptr)(unsafe.Pointer((*TWherePath)(unsafe.Pointer(pFrom)).FaLoop + uintptr(iLoop)*8)) 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)*104))).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))*8)), 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+68, 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) } /* vvv--- See check-in [12ad822d9b827777] on 2023-03-16 ---vvv */ } 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))*8)))).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))*8)), bp+16)) if rc1 == (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr { libc.SetBitFieldPtr8Uint32(pWInfo+68, 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+68, 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))*8)), bp+24)) if nOrder == (*TExprList)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy)).FnExpr { libc.SetBitFieldPtr8Uint32(pWInfo+68, 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 // // /* // ** This routine implements a heuristic designed to improve query planning. // ** This routine is called in between the first and second call to // ** wherePathSolver(). Hence the name "Interstage" "Heuristic". // ** // ** The first call to wherePathSolver() (hereafter just "solver()") computes // ** the best path without regard to the order of the outputs. The second call // ** to the solver() builds upon the first call to try to find an alternative // ** path that satisfies the ORDER BY clause. // ** // ** This routine looks at the results of the first solver() run, and for // ** every FROM clause term in the resulting query plan that uses an equality // ** constraint against an index, disable other WhereLoops for that same // ** FROM clause term that would try to do a full-table scan. This prevents // ** an index search from being converted into a full-table scan in order to // ** satisfy an ORDER BY clause, since even though we might get slightly better // ** performance using the full-scan without sorting if the output size // ** estimates are very precise, we might also get severe performance // ** degradation using the full-scan if the output size estimate is too large. // ** It is better to err on the side of caution. // ** // ** Except, if the first solver() call generated a full-table scan in an outer // ** loop then stop this analysis at the first full-scan, since the second // ** solver() run might try to swap that full-scan for another in order to // ** get the output into the correct order. In other words, we allow a // ** rewrite like this: // ** // ** First Solver() Second Solver() // ** |-- SCAN t1 |-- SCAN t2 // ** |-- SEARCH t2 `-- SEARCH t1 // ** `-- SORT USING B-TREE // ** // ** The purpose of this routine is to disallow rewrites such as: // ** // ** First Solver() Second Solver() // ** |-- SEARCH t1 |-- SCAN t2 <--- bad! // ** |-- SEARCH t2 `-- SEARCH t1 // ** `-- SORT USING B-TREE // ** // ** See test cases in test/whereN.test for the real-world query that // ** originally provoked this heuristic. // */ func _whereInterstageHeuristic(tls *libc.TLS, pWInfo uintptr) { var i int32 var iTab Tu8 var p, pLoop uintptr _, _, _, _ = i, iTab, p, pLoop i = 0 for { if !(i < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)) { break } p = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 856 + uintptr(i)*104))).FpWLoop if p == uintptr(0) { break } if (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags&uint32(WHERE_VIRTUALTABLE) != uint32(0) { goto _1 } if (*TWhereLoop)(unsafe.Pointer(p)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_COLUMN_EQ)|libc.Int32FromInt32(WHERE_COLUMN_NULL)|libc.Int32FromInt32(WHERE_COLUMN_IN)) != uint32(0) { iTab = (*TWhereLoop)(unsafe.Pointer(p)).FiTab pLoop = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpLoops for { if !(pLoop != 0) { break } if int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FiTab) != int32(iTab) { goto _2 } if (*TWhereLoop)(unsafe.Pointer(pLoop)).FwsFlags&uint32(libc.Int32FromInt32(WHERE_CONSTRAINT)|libc.Int32FromInt32(WHERE_AUTO_INDEX)) != uint32(0) { /* Auto-index and index-constrained loops allowed to remain */ goto _2 } (*TWhereLoop)(unsafe.Pointer(pLoop)).Fprereq = uint64(-libc.Int32FromInt32(1)) /* Prevent 2nd solver() from using this one */ goto _2 _2: ; pLoop = (*TWhereLoop)(unsafe.Pointer(pLoop)).FpNextLoop } } else { break } goto _1 _1: ; i++ } } // 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(112) defer tls.Free(112) 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 } 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 + 60 + 4))&0x2>>1)) != 0 || int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 4))&0x1>>0)) != 0 { return 0 } iCur = (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor pWC = pWInfo + 104 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 } 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.Uint64FromInt64(24)/libc.Uint64FromInt64(8)) { goto _1 } if int32(uint32(*(*uint16)(unsafe.Pointer(pIdx + 100))&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)*8)) = 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 + 100))&0x20>>5)) != 0 || (*TSrcItem)(unsafe.Pointer(pItem)).FcolUsed&(*TIndex)(unsafe.Pointer(pIdx)).FcolNotIdxed == uint64(0) { *(*Tu32)(unsafe.Pointer(pLoop + 48)) |= 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 + 856))).FpWLoop = pLoop (*TWhereLoop)(unsafe.Pointer(pLoop)).FmaskSelf = uint64(1) /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */ (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 856))).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 + 48)) |= 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(48) defer tls.Free(48) var _ /* w at bp+0 */ TWalker libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*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 */ /* These two preconditions checked by the caller combine to guarantee ** condition (1) of the header comment */ tabUsed = _sqlite3WhereExprListUsage(tls, pWInfo+592, (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpResultSet) if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpOrderBy != 0 { tabUsed |= _sqlite3WhereExprListUsage(tls, pWInfo+592, (*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 + 856 + uintptr(i)*104))).FpWLoop pItem = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpTabList + 8 + uintptr((*TWhereLoop)(unsafe.Pointer(pLoop)).FiTab)*104 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)*56 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 + 52)) != (*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 + 52)) == (*TSrcItem)(unsafe.Pointer(pItem)).FiCursor { break /* restriction (5) */ } goto _2 _2: ; pTerm += 56 } 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 + 18 *(*Tu16)(unsafe.Pointer(p4)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p4))) | libc.Int32FromInt32(TERM_CODED)) } goto _3 _3: ; pTerm += 56 } if i != int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)-int32(1) { nByte = int32(uint64(int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)-libc.Int32FromInt32(1)-i) * uint64(104)) libc.X__builtin___memmove_chk(tls, pWInfo+856+uintptr(i)*104, pWInfo+856+uintptr(i+int32(1))*104, uint64(nByte), libc.X__builtin_object_size(tls, pWInfo+856+uintptr(i)*104, 0)) } (*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel-- 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 i = 0 for { if !(i < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)) { break } pLoop = (*(*TWhereLevel)(unsafe.Pointer(pWInfo + 856 + uintptr(i)*104))).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)*104 pTab = (*TSrcItem)(unsafe.Pointer(pItem)).FpTab if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_HasStat1) == uint32(0) { break } *(*Tu32)(unsafe.Pointer(pTab + 48)) |= uint32(TF_MaybeReanalyze) 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 + 48)) |= uint32(WHERE_BLOOMFILTER) *(*Tu32)(unsafe.Pointer(pLoop + 48)) &= uint32(^libc.Int32FromInt32(WHERE_IDX_ONLY)) } } nSearch = TLogEst(int32(nSearch) + int32((*TWhereLoop)(unsafe.Pointer(pLoop)).FnOut)) goto _1 _1: ; i++ } } // C documentation // // /* // ** Expression Node callback for sqlite3ExprCanReturnSubtype(). // ** // ** Only a function call is able to return a subtype. So if the node // ** is not a function call, return WRC_Prune immediately. // ** // ** A function call is able to return a subtype if it has the // ** SQLITE_RESULT_SUBTYPE property. // ** // ** Assume that every function is able to pass-through a subtype from // ** one of its argument (using sqlite3_result_value()). Most functions // ** are not this way, but we don't have a mechanism to distinguish those // ** that are from those that are not, so assume they all work this way. // ** That means that if one of its arguments is another function and that // ** other function is able to return a subtype, then this function is // ** able to return a subtype. // */ func _exprNodeCanReturnSubtype(tls *libc.TLS, pWalker uintptr, pExpr uintptr) (r int32) { var db, pDef uintptr var n, v1 int32 _, _, _, _ = db, n, pDef, v1 if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) != int32(TK_FUNCTION) { return int32(WRC_Prune) } db = (*TParse)(unsafe.Pointer((*TWalker)(unsafe.Pointer(pWalker)).FpParse)).Fdb if *(*uintptr)(unsafe.Pointer(pExpr + 32)) != 0 { v1 = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pExpr + 32)))).FnExpr } else { v1 = 0 } n = v1 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) { (*TWalker)(unsafe.Pointer(pWalker)).FeCode = uint16(1) return int32(WRC_Prune) } return WRC_Continue } // C documentation // // /* // ** Return TRUE if expression pExpr is able to return a subtype. // ** // ** A TRUE return does not guarantee that a subtype will be returned. // ** It only indicates that a subtype return is possible. False positives // ** are acceptable as they only disable an optimization. False negatives, // ** on the other hand, can lead to incorrect answers. // */ func _sqlite3ExprCanReturnSubtype(tls *libc.TLS, pParse uintptr, pExpr uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var _ /* w at bp+0 */ TWalker libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*TWalker)(unsafe.Pointer(bp))).FpParse = pParse (*(*TWalker)(unsafe.Pointer(bp))).FxExprCallback = __ccgo_fp(_exprNodeCanReturnSubtype) _sqlite3WalkExpr(tls, bp, pExpr) return int32((*(*TWalker)(unsafe.Pointer(bp))).FeCode) } // 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 i, j int32 var p, pArg, pExpr, pTab uintptr _, _, _, _, _, _ = i, j, p, pArg, pExpr, pTab 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)*32))).FpExpr } else { if j >= 0 && int32((*(*TColumn)(unsafe.Pointer((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(j)*16))).FcolFlags)&int32(COLFLAG_VIRTUAL) != 0 { pExpr = _sqlite3ColumnExpr(tls, pTab, (*TTable)(unsafe.Pointer(pTab)).FaCol+uintptr(j)*16) } else { goto _1 } } if _sqlite3ExprIsConstant(tls, uintptr(0), pExpr) != 0 { goto _1 } if int32((*TExpr)(unsafe.Pointer(pExpr)).Fop) == int32(TK_FUNCTION) && _sqlite3ExprCanReturnSubtype(tls, pParse, pExpr) != 0 { /* Functions that might set a subtype should not be replaced by the ** value taken from an expression index since the index omits the ** subtype. https://sqlite.org/forum/forumpost/68d284c86b082c3e */ goto _1 } p = _sqlite3DbMallocRaw(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(32)) 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(*(*int8)(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 + 96 _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)*104 if !(int32(uint32(*(*uint16)(unsafe.Pointer(pItem + 60 + 4))&0x100>>8)) != 0) || int32((*TCteUse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pItem + 96)))).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 + 96)) |= 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(80) defer tls.Free(80) 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 */ /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */ /* Variable initialization */ db = (*TParse)(unsafe.Pointer(pParse)).Fdb libc.X__builtin___memset_chk(tls, bp, 0, uint64(56), libc.X__builtin_object_size(tls, bp, 0)) /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ if pOrderBy != 0 && (*TExprList)(unsafe.Pointer(pOrderBy)).FnExpr >= int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(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.Uint64FromInt64(8)*libc.Uint64FromInt32(8)) { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23050, libc.VaList(bp+64, int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(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.Uint64FromInt64(960)) if nTabList > int32(1) { nByteWInfo = int32(uint64(nByteWInfo) + uint64(nTabList-libc.Int32FromInt32(1))*libc.Uint64FromInt64(104)) } pWInfo = _sqlite3DbMallocRawNN(tls, db, uint64(nByteWInfo)+uint64(104)) 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 + 40 + 1*4)) = v2 *(*int32)(unsafe.Pointer(pWInfo + 40)) = 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.X__builtin___memset_chk(tls, pWInfo+65, 0, uint64(libc.UintptrFromInt32(0)+104)-uint64(libc.UintptrFromInt32(0)+65), libc.X__builtin_object_size(tls, pWInfo+65, 0)) libc.X__builtin___memset_chk(tls, pWInfo+856, 0, uint64(104)+uint64(nTabList)*uint64(104), libc.X__builtin_object_size(tls, pWInfo+856, 0)) /* ONEPASS defaults to OFF */ pMaskSet = pWInfo + 592 (*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 + 104 (*(*TWhereLoopBuilder)(unsafe.Pointer(bp))).FpNew = pWInfo + uintptr(nByteWInfo) _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+104, pWInfo) _sqlite3WhereSplit(tls, pWInfo+104, 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) } if (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect != 0 && (*TSelect)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpSelect)).FselFlags&uint32(SF_MultiValue) == uint32(0) { _sqlite3VdbeExplain(tls, pParse, uint8(0), __ccgo_ts+23078, 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)*104))).FiCursor) _sqlite3WhereTabFuncArgs(tls, pParse, pTabList+8+uintptr(ii)*104, pWInfo+104) goto _5 _5: ; ii++ v4 = ii if !(v4 < (*TSrcList)(unsafe.Pointer(pTabList)).FnSrc) { break } } } /* Analyze all of the subexpressions. */ _sqlite3WhereExprAnalyze(tls, pTabList, pWInfo+104) if pSelect != 0 && (*TSelect)(unsafe.Pointer(pSelect)).FpLimit != 0 { _sqlite3WhereAddLimit(tls, pWInfo+104, 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)*56 /* The expression of pT */ if int32((*TWhereTerm)(unsafe.Pointer(pT)).FwtFlags)&int32(TERM_VIRTUAL) != 0 { goto _6 } pX = (*TWhereTerm)(unsafe.Pointer(pT)).FpExpr 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 + 18 *(*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 + 60 *(*Tu16)(unsafe.Pointer(p8)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(p8))) & ^libc.Int32FromInt32(WHERE_WANT_DISTINCT)) } else { if _isDistinctRedundant(tls, pParse, pTabList, pWInfo+104, 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 + 60 *(*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 { _whereInterstageHeuristic(tls, pWInfo) _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 + 70 *(*TLogEst)(unsafe.Pointer(p10)) = TLogEst(int32(*(*TLogEst)(unsafe.Pointer(p10))) - libc.Int32FromInt32(30)) } } 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 } /* 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) } /* 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 + 224 *(*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. */ if int32(wctrlFlags)&int32(WHERE_ONEPASS_DESIRED) != 0 { wsFlags = int32((*TWhereLoop1)(unsafe.Pointer((*(*TWhereLevel)(unsafe.Pointer(pWInfo + 856))).FpWLoop)).FwsFlags) bOnerow = libc.BoolInt32(wsFlags&int32(WHERE_ONEROW) != 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 + 856))).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 + 856 for { if !(ii < nTabList) { break } pTabItem = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*104 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 + 40)) = (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor } _sqlite3OpenTable(tls, pParse, (*TSrcItem)(unsafe.Pointer(pTabItem)).FiCursor, iDb, pTab, op) if int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FeOnePass) == ONEPASS_OFF && int32((*TTable)(unsafe.Pointer(pTab)).FnCol) < int32(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(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(int64(n)), -int32(3)) } _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 */ if !((*TTable)(unsafe.Pointer(pTab)).FtabFlags&libc.Uint32FromInt32(TF_WithoutRowid) == libc.Uint32FromInt32(0)) && int32(uint32(*(*uint16)(unsafe.Pointer(pIx + 100))&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 for pJ != 0 && pJ != pIx { iIndexCur++ pJ = (*TIndex)(unsafe.Pointer(pJ)).FpNext } op1 = int32(OP_OpenWrite) *(*int32)(unsafe.Pointer(pWInfo + 40 + 1*4)) = iIndexCur } else { if iAuxArg != 0 && int32(wctrlFlags)&int32(WHERE_OR_SUBCLAUSE) != 0 { iIndexCur = iAuxArg op1 = int32(OP_ReopenIdx) } else { v16 = pParse + 52 v15 = *(*int32)(unsafe.Pointer(v16)) *(*int32)(unsafe.Pointer(v16))++ iIndexCur = v15 if int32(uint32(*(*uint16)(unsafe.Pointer(pIx + 100))&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 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 + 52 v19 = *(*int32)(unsafe.Pointer(v20)) *(*int32)(unsafe.Pointer(v20))++ (*TWhereRightJoin)(unsafe.Pointer(pRJ)).FiMatch = v19 v22 = pParse + 56 *(*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 + 56 *(*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) 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 + 32)) = 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 + 48)) &= 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 += 104 } (*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 + 856 + uintptr(ii)*104 wsFlags1 = int32((*TWhereLoop1)(unsafe.Pointer((*TWhereLevel)(unsafe.Pointer(pLevel)).FpWLoop)).FwsFlags) pSrc = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*104 if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 60 + 4))&0x10>>4)) != 0 { if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 60 + 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) } } if wsFlags1&(libc.Int32FromInt32(WHERE_AUTO_INDEX)|libc.Int32FromInt32(WHERE_BLOOMFILTER)) != 0 { if wsFlags1&int32(WHERE_AUTO_INDEX) != 0 { _constructAutomaticIndex(tls, pParse, pWInfo+104, 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, m, n, n1, nRJ, op, r1, ws, x, v4, v7 int32 var db, p, pIdx, pIdx1, pIn, pIx, pLastOp, pLevel, pLoop, pOp, pParse, pPk, pRJ, pSrc, pTab, pTabItem, pTabList, v, v10, v2, p12 uintptr var v3, v5 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = addr, addrSeek, bEarlyOut, db, i, iDb, iEnd, j, j1, k, last, m, n, n1, nRJ, op, p, pIdx, pIdx1, pIn, pIx, pLastOp, pLevel, pLoop, pOp, pParse, pPk, pRJ, pSrc, 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 + 856 + uintptr(i)*104 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 + 100))&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 + 56)) += 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 + 72))).FnIn > 0 { _sqlite3VdbeResolveLabel(tls, v, (*TWhereLevel)(unsafe.Pointer(pLevel)).FaddrNxt) j1 = (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 72))).FnIn pIn = (*(*struct { FnIn int32 FaInLoop uintptr })(unsafe.Pointer(pLevel + 72))).FaInLoop + uintptr(j1-int32(1))*20 for { if !(j1 > 0) { break } _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) if ws&int32(WHERE_IDX_ONLY) == 0 { pSrc = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*104 if int32(uint32(*(*uint16)(unsafe.Pointer(pSrc + 60 + 4))&0x20>>5)) != 0 { n1 = (*TSrcItem)(unsafe.Pointer(pSrc)).FregResult m = int32((*TTable)(unsafe.Pointer((*TSrcItem)(unsafe.Pointer(pSrc)).FpTab)).FnCol) _sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, n1, n1+m-int32(1)) } _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-- } i = 0 pLevel = pWInfo + 856 for { if !(i < int32((*TWhereInfo)(unsafe.Pointer(pWInfo)).FnLevel)) { break } pIdx1 = uintptr(0) pTabItem = pTabList + 8 + uintptr((*TWhereLevel)(unsafe.Pointer(pLevel)).FiFrom)*104 pTab = (*TSrcItem)(unsafe.Pointer(pTabItem)).FpTab 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 + 60 + 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 + 100))&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)*24 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 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))) } 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. */ } } 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 += 24 v10 = pOp if !(v10 < pLastOp) { break } } } goto _9 _9: ; i++ pLevel += 104 } /* 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 + 38 *(*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 { 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 { FnStep Ti64 FpValue uintptr } 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*8))) { case int32(SQLITE_INTEGER): iVal = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(apArg + 1*8))) case int32(SQLITE_FLOAT): fVal = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(apArg + 1*8))) 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+23096, -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 _ = 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 _ = 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 _ = 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 _ = 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 { 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 _ = 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+23152, -int32(1)) } } (*TNtileCtx)(unsafe.Pointer(p)).FnTotal++ } } func _ntileInvFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) { var p uintptr _ = p _ = 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 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(16)) 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(16)) 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(16)) 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]int8{'r', 'o', 'w', '_', 'n', 'u', 'm', 'b', 'e', 'r'} var _dense_rankName = [11]int8{'d', 'e', 'n', 's', 'e', '_', 'r', 'a', 'n', 'k'} var _rankName = [5]int8{'r', 'a', 'n', 'k'} var _percent_rankName = [13]int8{'p', 'e', 'r', 'c', 'e', 'n', 't', '_', 'r', 'a', 'n', 'k'} var _cume_distName = [10]int8{'c', 'u', 'm', 'e', '_', 'd', 'i', 's', 't'} var _ntileName = [6]int8{'n', 't', 'i', 'l', 'e'} var _last_valueName = [11]int8{'l', 'a', 's', 't', '_', 'v', 'a', 'l', 'u', 'e'} var _nth_valueName = [10]int8{'n', 't', 'h', '_', 'v', 'a', 'l', 'u', 'e'} var _first_valueName = [12]int8{'f', 'i', 'r', 's', 't', '_', 'v', 'a', 'l', 'u', 'e'} var _leadName = [5]int8{'l', 'e', 'a', 'd'} var _lagName = [4]int8{'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*/ /*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.Uint64FromInt64(1080)/libc.Uint64FromInt64(72))) } 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, 24)) = __ccgo_fp(_row_numberStepFunc) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_row_numberValueFunc) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_row_numberValueFunc) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_dense_rankStepFunc) *(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(_dense_rankValueFunc) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_dense_rankValueFunc) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 168)) = __ccgo_fp(_rankStepFunc) *(*uintptr)(unsafe.Add(p, 176)) = __ccgo_fp(_rankValueFunc) *(*uintptr)(unsafe.Add(p, 184)) = __ccgo_fp(_rankValueFunc) *(*uintptr)(unsafe.Add(p, 192)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 240)) = __ccgo_fp(_percent_rankStepFunc) *(*uintptr)(unsafe.Add(p, 248)) = __ccgo_fp(_percent_rankValueFunc) *(*uintptr)(unsafe.Add(p, 256)) = __ccgo_fp(_percent_rankValueFunc) *(*uintptr)(unsafe.Add(p, 264)) = __ccgo_fp(_percent_rankInvFunc) *(*uintptr)(unsafe.Add(p, 312)) = __ccgo_fp(_cume_distStepFunc) *(*uintptr)(unsafe.Add(p, 320)) = __ccgo_fp(_cume_distValueFunc) *(*uintptr)(unsafe.Add(p, 328)) = __ccgo_fp(_cume_distValueFunc) *(*uintptr)(unsafe.Add(p, 336)) = __ccgo_fp(_cume_distInvFunc) *(*uintptr)(unsafe.Add(p, 384)) = __ccgo_fp(_ntileStepFunc) *(*uintptr)(unsafe.Add(p, 392)) = __ccgo_fp(_ntileValueFunc) *(*uintptr)(unsafe.Add(p, 400)) = __ccgo_fp(_ntileValueFunc) *(*uintptr)(unsafe.Add(p, 408)) = __ccgo_fp(_ntileInvFunc) *(*uintptr)(unsafe.Add(p, 456)) = __ccgo_fp(_last_valueStepFunc) *(*uintptr)(unsafe.Add(p, 464)) = __ccgo_fp(_last_valueFinalizeFunc) *(*uintptr)(unsafe.Add(p, 472)) = __ccgo_fp(_last_valueValueFunc) *(*uintptr)(unsafe.Add(p, 480)) = __ccgo_fp(_last_valueInvFunc) *(*uintptr)(unsafe.Add(p, 528)) = __ccgo_fp(_nth_valueStepFunc) *(*uintptr)(unsafe.Add(p, 536)) = __ccgo_fp(_nth_valueFinalizeFunc) *(*uintptr)(unsafe.Add(p, 544)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 552)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 600)) = __ccgo_fp(_first_valueStepFunc) *(*uintptr)(unsafe.Add(p, 608)) = __ccgo_fp(_first_valueFinalizeFunc) *(*uintptr)(unsafe.Add(p, 616)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 624)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 672)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 680)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 688)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 696)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 744)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 752)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 760)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 768)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 816)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 824)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 832)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 840)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 888)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 896)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 904)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 912)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 960)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 968)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 976)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 984)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 1032)) = __ccgo_fp(_noopStepFunc) *(*uintptr)(unsafe.Add(p, 1040)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 1048)) = __ccgo_fp(_noopValueFunc) *(*uintptr)(unsafe.Add(p, 1056)) = __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+23197, 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+23216, 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+23287, 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.Uint64FromInt64(192)/libc.Uint64FromInt64(24))) { 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+8603) } 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 + 40)) pParse = (*TWalker)(unsafe.Pointer(pWalker)).FpParse /* 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)*104))).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(pExpr + 64)) == pWin { 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)*32))).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)) *(*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.X__builtin___memset_chk(tls, pExpr, 0, uint64(72), libc.X__builtin_object_size(tls, pExpr, 0)) (*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 *(*uintptr)(unsafe.Pointer(pExpr + 64)) = (*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 + 40)) 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(96) defer tls.Free(96) var _ /* sRewrite at bp+48 */ TWindowRewrite var _ /* sWalker at bp+0 */ TWalker libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) libc.X__builtin___memset_chk(tls, bp+48, 0, uint64(40), libc.X__builtin_object_size(tls, bp+48, 0)) (*(*TWindowRewrite)(unsafe.Pointer(bp + 48))).FpSub = *(*uintptr)(unsafe.Pointer(ppSub)) (*(*TWindowRewrite)(unsafe.Pointer(bp + 48))).FpWin = pWin (*(*TWindowRewrite)(unsafe.Pointer(bp + 48))).FpSrc = pSrc (*(*TWindowRewrite)(unsafe.Pointer(bp + 48))).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 + 40)) = bp + 48 _sqlite3WalkExprList(tls, bp, pEList) *(*uintptr)(unsafe.Pointer(ppSub)) = (*(*TWindowRewrite)(unsafe.Pointer(bp + 48))).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)*32))).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)*32))).Ffg.FsortFlags = (*(*TExprList_item)(unsafe.Pointer(pAppend + 8 + uintptr(i)*32))).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) { _sqlite3ErrorMsg(tls, (*TWalker)(unsafe.Pointer(pWalker)).FpParse, __ccgo_ts+23350, 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(64) defer tls.Free(64) 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+8 */ 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(104)) if pTab == uintptr(0) { return _sqlite3ErrorToParser(tls, db, int32(SQLITE_NOMEM)) } _sqlite3AggInfoPersistWalkerInit(tls, bp+8, pParse) _sqlite3WalkSelect(tls, bp+8, p) if (*TSelect)(unsafe.Pointer(p)).FselFlags&uint32(SF_Aggregate) == uint32(0) { (*(*TWalker)(unsafe.Pointer(bp + 8))).FxExprCallback = __ccgo_fp(_disallowAggregatesInOrderByCb) (*(*TWalker)(unsafe.Pointer(bp + 8))).FxSelectCallback = uintptr(0) _sqlite3WalkExprList(tls, bp+8, (*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 + 52 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ (*TWindow)(unsafe.Pointer(pMWin)).FiEphCsr = v1 *(*int32)(unsafe.Pointer(pParse + 52)) += 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 } pArgs = *(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 32)) 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 + 56 *(*int32)(unsafe.Pointer(v8))++ v7 = *(*int32)(unsafe.Pointer(v8)) (*TWindow)(unsafe.Pointer(pWin)).FregAccum = v7 v10 = pParse + 56 *(*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+1784)) } 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)) /* 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+60+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, int8(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.X__builtin___memcpy_chk(tls, pTab, pTab2, uint64(104), libc.X__builtin_object_size(tls, pTab, 0)) *(*Tu32)(unsafe.Pointer(pTab + 48)) |= uint32(TF_Ephemeral) (*(*TSrcItem)(unsafe.Pointer((*TSelect)(unsafe.Pointer(p)).FpSrc + 8))).FpTab = pTab pTab = pTab2 libc.X__builtin___memset_chk(tls, bp+8, 0, uint64(48), libc.X__builtin_object_size(tls, bp+8, 0)) (*(*TWalker)(unsafe.Pointer(bp + 8))).FxExprCallback = __ccgo_fp(_sqlite3WindowExtraAggFuncDepth) (*(*TWalker)(unsafe.Pointer(bp + 8))).FxSelectCallback = __ccgo_fp(_sqlite3WalkerDepthIncrease) (*(*TWalker)(unsafe.Pointer(bp + 8))).FxSelectCallback2 = __ccgo_fp(_sqlite3WalkerDepthDecrease) _sqlite3WalkSelect(tls, bp+8, 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) } 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, uintptr(0), 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: */ 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+23376, 0) goto windowAllocErr } pWin = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(144)) 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 + 23408 } else { if (*TWindow)(unsafe.Pointer(pExist)).FpOrderBy != 0 && (*TWindow)(unsafe.Pointer(pWin)).FpOrderBy != 0 { zErr = __ccgo_ts + 23425 } else { if int32((*TWindow)(unsafe.Pointer(pExist)).FbImplicitFrame) == 0 { zErr = __ccgo_ts + 23441 } } } if zErr != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23461, 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 { (*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 { *(*uintptr)(unsafe.Pointer(p + 64)) = 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+23494, 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 + 64 } (*TSelect)(unsafe.Pointer(pSel)).FpWin = pWin (*TWindow)(unsafe.Pointer(pWin)).FppThis = pSel + 112 } 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 + 56)) += nExpr _sqlite3VdbeAddOp3(tls, v, int32(OP_Null), 0, (*TWindow)(unsafe.Pointer(pMWin)).FregPart, (*TWindow)(unsafe.Pointer(pMWin)).FregPart+nExpr-int32(1)) } v2 = pParse + 56 *(*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 + 56 *(*int32)(unsafe.Pointer(v4))++ v3 = *(*int32)(unsafe.Pointer(v4)) (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid = v3 v6 = pParse + 56 *(*int32)(unsafe.Pointer(v6))++ v5 = *(*int32)(unsafe.Pointer(v6)) (*TWindow)(unsafe.Pointer(pMWin)).FregEndRowid = v5 v8 = pParse + 52 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) { pList = *(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 32)) pKeyInfo = _sqlite3KeyInfoFromExprList(tls, pParse, pList, 0, 0) v11 = pParse + 52 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 + 56)) += int32(3) if pKeyInfo != 0 && int32(*(*int8)(unsafe.Pointer((*TFuncDef)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpWFunc)).FzName + 1))) == int32('i') { *(*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 + 52 v12 = *(*int32)(unsafe.Pointer(v13)) *(*int32)(unsafe.Pointer(v13))++ (*TWindow)(unsafe.Pointer(pWin)).FcsrApp = v12 *(*int32)(unsafe.Pointer(pParse + 56)) += 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 + 52 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) _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+1664, -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))) } else { _sqlite3VdbeAddOp2(tls, v, int32(OP_MustBeInt), reg, _sqlite3VdbeCurrentAddr(tls, v)+int32(2)) } _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 + 23541, 1: __ccgo_ts + 23594, 2: __ccgo_ts + 23096, 3: __ccgo_ts + 23645, 4: __ccgo_ts + 23697, } 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 pList = *(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 32)) 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 /* All OVER clauses in the same window function aggregate step must ** be the same. */ 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 { _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 { 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) nArg = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 32)))).FnExpr regArg = _sqlite3GetTempRange(tls, pParse, nArg) _sqlite3ExprCodeExprList(tls, pParse, *(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 32)), 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 { pColl = _sqlite3ExprNNCollSeq(tls, pParse, (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TWindow)(unsafe.Pointer(pWin)).FpOwner + 32)) + 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 { } 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 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 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 + 32)))).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 _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 { _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 + 56)) += 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 + 56 *(*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) 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: 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: /* 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+1664, -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) _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) { return 0 } if regCountdown > 0 { if int32((*TWindow)(unsafe.Pointer(pMWin)).FeFrmType) == int32(TK_RANGE) { addrNextRange = _sqlite3VdbeCurrentAddr(tls, v) 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) } 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 { _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: csr = (*TWindowCodeArg)(unsafe.Pointer(p)).Fend.Fcsr reg = (*TWindowCodeArg)(unsafe.Pointer(p)).Fend.Freg if (*TWindow)(unsafe.Pointer(pMWin)).FregStartRowid != 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(144)) 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)) + 64 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(80) defer tls.Free(80) 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 */ lblWhereEnd = _sqlite3VdbeMakeLabel(tls, pParse) /* Fill in the context object */ libc.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 0)) (*(*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 + 56)) += nInput v2 = pParse + 56 *(*int32)(unsafe.Pointer(v2))++ v1 = *(*int32)(unsafe.Pointer(v2)) regRecord = v1 v4 = pParse + 56 *(*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 + 56 *(*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 + 56 *(*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 + 56)) += nPeer (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fstart.Freg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 56)) += nPeer (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fcurrent.Freg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 56)) += nPeer (*(*TWindowCodeArg)(unsafe.Pointer(bp))).Fend.Freg = (*TParse)(unsafe.Pointer(pParse)).FnMem + int32(1) *(*int32)(unsafe.Pointer(pParse + 56)) += 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 + 56 *(*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 { _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 { 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 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 + 23747 } else { v1 = __ccgo_ts + 23756 } _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23762, 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 + 136 + 4*4)) mxSelect = v2 } if v3 && v2 > 0 && cnt > mxSelect { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23804, 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 // // /* Memory allocator for parser stack resizing. This is a thin wrapper around // ** sqlite3_realloc() that includes a call to sqlite3FaultSim() to facilitate // ** testing. // */ func _parserStackRealloc(tls *libc.TLS, pOld uintptr, newSize Tsqlite3_uint64) (r uintptr) { var v1 uintptr _ = v1 if _sqlite3FaultSim(tls, int32(700)) != 0 { v1 = uintptr(0) } else { v1 = Xsqlite3_realloc(tls, pOld, int32(newSize)) } return v1 } // 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(72)+uint64((*(*TToken)(unsafe.Pointer(bp))).Fn)+uint64(1)) if p != 0 { /* memset(p, 0, sizeof(Expr)); */ (*TExpr)(unsafe.Pointer(p)).Fop = uint8(op) (*TExpr)(unsafe.Pointer(p)).FaffExpr = 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.X__builtin___memset_chk(tls, p+32, 0, uint64(8), libc.X__builtin_object_size(tls, p+32, 0)) libc.X__builtin___memset_chk(tls, p+64, 0, uint64(8), libc.X__builtin_object_size(tls, p+64, 0)) (*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*72 libc.X__builtin___memcpy_chk(tls, *(*uintptr)(unsafe.Pointer(p + 8)), (*(*TToken)(unsafe.Pointer(bp))).Fz, uint64((*(*TToken)(unsafe.Pointer(bp))).Fn), libc.X__builtin_object_size(tls, *(*uintptr)(unsafe.Pointer(p + 8)), 0)) *(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 8)) + uintptr((*(*TToken)(unsafe.Pointer(bp))).Fn))) = 0 *(*int32)(unsafe.Pointer(p + 52)) = int32(int64((*(*TToken)(unsafe.Pointer(bp))).Fz) - int64((*TParse)(unsafe.Pointer(pParse)).FzTail)) if int32(_sqlite3CtypeMap[uint8(*(*int8)(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+23838, 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 // ** YYREALLOC Name of the realloc() function to use // ** YYFREE Name of the free() function to use // ** YYDYNSTACK True if stack space should be extended on heap // ** 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 // ** YY_MIN_DSTRCTR Minimum symbol value that has a destructor // ** YY_MAX_DSTRCTR Maximum symbol value that has a destructor // */ // /************* Begin control #defines *****************************************/ type TYYMINORTYPE = struct { Fyy0 [0]TToken Fyy14 [0]uintptr Fyy59 [0]uintptr Fyy67 [0]uintptr Fyy122 [0]uintptr Fyy132 [0]uintptr Fyy144 [0]int32 Fyy168 [0]uintptr Fyy203 [0]uintptr Fyy211 [0]uintptr Fyy269 [0]TOnOrUsing Fyy286 [0]TTrigEvent Fyy383 [0]struct { Fvalue int32 Fmask int32 } Fyy391 [0]Tu32 Fyy427 [0]uintptr Fyy454 [0]uintptr Fyy462 [0]Tu8 Fyy509 [0]TFrameBound Fyy555 [0]uintptr Fyyinit int32 F__ccgo_pad20 [12]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. */ /* Macro to determine if stack space has the ability to grow using ** heap memory. */ /* Guarantee a minimum number of initial stack slots. */ // 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 = [2142]uint16{ 0: uint16(576), 1: uint16(128), 2: uint16(125), 3: uint16(232), 4: uint16(1622), 5: uint16(549), 6: uint16(576), 7: uint16(1290), 8: uint16(1281), 9: uint16(576), 10: uint16(328), 11: uint16(576), 12: uint16(1300), 13: uint16(212), 14: uint16(576), 15: uint16(128), 16: uint16(125), 17: uint16(232), 18: uint16(578), 19: uint16(412), 20: uint16(578), 21: uint16(391), 22: uint16(1542), 23: uint16(51), 24: uint16(51), 25: uint16(523), 26: uint16(405), 27: uint16(1293), 28: uint16(529), 29: uint16(51), 30: uint16(51), 31: uint16(983), 32: uint16(51), 33: uint16(51), 34: uint16(81), 35: uint16(81), 36: uint16(1107), 37: uint16(61), 38: uint16(61), 39: uint16(984), 40: uint16(1107), 41: uint16(1292), 42: uint16(380), 43: uint16(135), 44: uint16(136), 45: uint16(90), 46: uint16(1228), 47: uint16(1228), 48: uint16(1063), 49: uint16(1066), 50: uint16(1053), 51: uint16(1053), 52: uint16(133), 53: uint16(133), 54: uint16(134), 55: uint16(134), 56: uint16(134), 57: uint16(134), 58: uint16(1577), 59: uint16(412), 60: uint16(287), 61: uint16(287), 62: uint16(7), 63: uint16(287), 64: uint16(287), 65: uint16(422), 66: uint16(1050), 67: uint16(1050), 68: uint16(1064), 69: uint16(1067), 70: uint16(289), 71: uint16(556), 72: uint16(492), 73: uint16(573), 74: uint16(524), 75: uint16(561), 76: uint16(573), 77: uint16(497), 78: uint16(561), 79: uint16(482), 80: uint16(530), 81: uint16(262), 82: uint16(229), 83: uint16(135), 84: uint16(136), 85: uint16(90), 86: uint16(1228), 87: uint16(1228), 88: uint16(1063), 89: uint16(1066), 90: uint16(1053), 91: uint16(1053), 92: uint16(133), 93: uint16(133), 94: uint16(134), 95: uint16(134), 96: uint16(134), 97: uint16(134), 98: uint16(128), 99: uint16(125), 100: uint16(232), 101: uint16(1506), 102: uint16(132), 103: uint16(132), 104: uint16(132), 105: uint16(132), 106: uint16(131), 107: uint16(131), 108: uint16(130), 109: uint16(130), 110: uint16(130), 111: uint16(129), 112: uint16(126), 113: uint16(450), 114: uint16(1204), 115: uint16(1255), 116: uint16(1), 117: uint16(1), 118: uint16(582), 119: uint16(2), 120: uint16(1259), 121: uint16(1571), 122: uint16(420), 123: uint16(1582), 124: uint16(379), 125: uint16(320), 126: uint16(1174), 127: uint16(153), 128: uint16(1174), 129: uint16(1584), 130: uint16(412), 131: uint16(378), 132: uint16(1582), 133: uint16(543), 134: uint16(1341), 135: uint16(330), 136: uint16(111), 137: uint16(570), 138: uint16(570), 139: uint16(570), 140: uint16(293), 141: uint16(1054), 142: uint16(132), 143: uint16(132), 144: uint16(132), 145: uint16(132), 146: uint16(131), 147: uint16(131), 148: uint16(130), 149: uint16(130), 150: uint16(130), 151: uint16(129), 152: uint16(126), 153: uint16(450), 154: uint16(135), 155: uint16(136), 156: uint16(90), 157: uint16(1228), 158: uint16(1228), 159: uint16(1063), 160: uint16(1066), 161: uint16(1053), 162: uint16(1053), 163: uint16(133), 164: uint16(133), 165: uint16(134), 166: uint16(134), 167: uint16(134), 168: uint16(134), 169: uint16(287), 170: uint16(287), 171: uint16(1204), 172: uint16(1205), 173: uint16(1204), 174: uint16(255), 175: uint16(287), 176: uint16(287), 177: uint16(510), 178: uint16(507), 179: uint16(506), 180: uint16(137), 181: uint16(455), 182: uint16(573), 183: uint16(212), 184: uint16(561), 185: uint16(447), 186: uint16(446), 187: uint16(505), 188: uint16(573), 189: uint16(1616), 190: uint16(561), 191: uint16(134), 192: uint16(134), 193: uint16(134), 194: uint16(134), 195: uint16(127), 196: uint16(400), 197: uint16(243), 198: uint16(132), 199: uint16(132), 200: uint16(132), 201: uint16(132), 202: uint16(131), 203: uint16(131), 204: uint16(130), 205: uint16(130), 206: uint16(130), 207: uint16(129), 208: uint16(126), 209: uint16(450), 210: uint16(282), 211: uint16(471), 212: uint16(345), 213: uint16(132), 214: uint16(132), 215: uint16(132), 216: uint16(132), 217: uint16(131), 218: uint16(131), 219: uint16(130), 220: uint16(130), 221: uint16(130), 222: uint16(129), 223: uint16(126), 224: uint16(450), 225: uint16(574), 226: uint16(155), 227: uint16(936), 228: uint16(936), 229: uint16(454), 230: uint16(227), 231: uint16(521), 232: uint16(1236), 233: uint16(412), 234: uint16(1236), 235: uint16(134), 236: uint16(134), 237: uint16(134), 238: uint16(134), 239: uint16(132), 240: uint16(132), 241: uint16(132), 242: uint16(132), 243: uint16(131), 244: uint16(131), 245: uint16(130), 246: uint16(130), 247: uint16(130), 248: uint16(129), 249: uint16(126), 250: uint16(450), 251: uint16(130), 252: uint16(130), 253: uint16(130), 254: uint16(129), 255: uint16(126), 256: uint16(450), 257: uint16(135), 258: uint16(136), 259: uint16(90), 260: uint16(1228), 261: uint16(1228), 262: uint16(1063), 263: uint16(1066), 264: uint16(1053), 265: uint16(1053), 266: uint16(133), 267: uint16(133), 268: uint16(134), 269: uint16(134), 270: uint16(134), 271: uint16(134), 272: uint16(128), 273: uint16(125), 274: uint16(232), 275: uint16(450), 276: uint16(576), 277: uint16(412), 278: uint16(397), 279: uint16(1249), 280: uint16(180), 281: uint16(92), 282: uint16(93), 283: uint16(132), 284: uint16(132), 285: uint16(132), 286: uint16(132), 287: uint16(131), 288: uint16(131), 289: uint16(130), 290: uint16(130), 291: uint16(130), 292: uint16(129), 293: uint16(126), 294: uint16(450), 295: uint16(381), 296: uint16(387), 297: uint16(1204), 298: uint16(383), 299: uint16(81), 300: uint16(81), 301: uint16(135), 302: uint16(136), 303: uint16(90), 304: uint16(1228), 305: uint16(1228), 306: uint16(1063), 307: uint16(1066), 308: uint16(1053), 309: uint16(1053), 310: uint16(133), 311: uint16(133), 312: uint16(134), 313: uint16(134), 314: uint16(134), 315: uint16(134), 316: uint16(132), 317: uint16(132), 318: uint16(132), 319: uint16(132), 320: uint16(131), 321: uint16(131), 322: uint16(130), 323: uint16(130), 324: uint16(130), 325: uint16(129), 326: uint16(126), 327: uint16(450), 328: uint16(131), 329: uint16(131), 330: uint16(130), 331: uint16(130), 332: uint16(130), 333: uint16(129), 334: uint16(126), 335: uint16(450), 336: uint16(556), 337: uint16(1204), 338: uint16(302), 339: uint16(319), 340: uint16(567), 341: uint16(121), 342: uint16(568), 343: uint16(480), 344: uint16(4), 345: uint16(555), 346: uint16(1149), 347: uint16(1657), 348: uint16(1628), 349: uint16(1657), 350: uint16(45), 351: uint16(128), 352: uint16(125), 353: uint16(232), 354: uint16(1204), 355: uint16(1205), 356: uint16(1204), 357: uint16(1250), 358: uint16(571), 359: uint16(1169), 360: uint16(132), 361: uint16(132), 362: uint16(132), 363: uint16(132), 364: uint16(131), 365: uint16(131), 366: uint16(130), 367: uint16(130), 368: uint16(130), 369: uint16(129), 370: uint16(126), 371: uint16(450), 372: uint16(1169), 373: uint16(287), 374: uint16(287), 375: uint16(1169), 376: uint16(1019), 377: uint16(576), 378: uint16(422), 379: uint16(1019), 380: uint16(412), 381: uint16(451), 382: uint16(1602), 383: uint16(582), 384: uint16(2), 385: uint16(1259), 386: uint16(573), 387: uint16(44), 388: uint16(561), 389: uint16(95), 390: uint16(320), 391: uint16(110), 392: uint16(153), 393: uint16(565), 394: uint16(1204), 395: uint16(1205), 396: uint16(1204), 397: uint16(522), 398: uint16(522), 399: uint16(1341), 400: uint16(81), 401: uint16(81), 402: uint16(7), 403: uint16(44), 404: uint16(135), 405: uint16(136), 406: uint16(90), 407: uint16(1228), 408: uint16(1228), 409: uint16(1063), 410: uint16(1066), 411: uint16(1053), 412: uint16(1053), 413: uint16(133), 414: uint16(133), 415: uint16(134), 416: uint16(134), 417: uint16(134), 418: uint16(134), 419: uint16(295), 420: uint16(1149), 421: uint16(1658), 422: uint16(1040), 423: uint16(1658), 424: uint16(1204), 425: uint16(1147), 426: uint16(319), 427: uint16(567), 428: uint16(119), 429: uint16(119), 430: uint16(343), 431: uint16(466), 432: uint16(331), 433: uint16(343), 434: uint16(287), 435: uint16(287), 436: uint16(120), 437: uint16(556), 438: uint16(451), 439: uint16(577), 440: uint16(451), 441: uint16(1169), 442: uint16(1169), 443: uint16(1028), 444: uint16(319), 445: uint16(567), 446: uint16(438), 447: uint16(573), 448: uint16(210), 449: uint16(561), 450: uint16(1339), 451: uint16(1451), 452: uint16(546), 453: uint16(531), 454: uint16(1169), 455: uint16(1169), 456: uint16(1598), 457: uint16(1169), 458: uint16(1169), 459: uint16(416), 460: uint16(319), 461: uint16(567), 462: uint16(243), 463: uint16(132), 464: uint16(132), 465: uint16(132), 466: uint16(132), 467: uint16(131), 468: uint16(131), 469: uint16(130), 470: uint16(130), 471: uint16(130), 472: uint16(129), 473: uint16(126), 474: uint16(450), 475: uint16(1028), 476: uint16(1028), 477: uint16(1030), 478: uint16(1031), 479: uint16(35), 480: uint16(44), 481: uint16(1204), 482: uint16(1205), 483: uint16(1204), 484: uint16(472), 485: uint16(287), 486: uint16(287), 487: uint16(1328), 488: uint16(412), 489: uint16(1307), 490: uint16(372), 491: uint16(1595), 492: uint16(359), 493: uint16(225), 494: uint16(454), 495: uint16(1204), 496: uint16(195), 497: uint16(1328), 498: uint16(573), 499: uint16(1147), 500: uint16(561), 501: uint16(1333), 502: uint16(1333), 503: uint16(274), 504: uint16(576), 505: uint16(1188), 506: uint16(576), 507: uint16(340), 508: uint16(46), 509: uint16(196), 510: uint16(537), 511: uint16(217), 512: uint16(135), 513: uint16(136), 514: uint16(90), 515: uint16(1228), 516: uint16(1228), 517: uint16(1063), 518: uint16(1066), 519: uint16(1053), 520: uint16(1053), 521: uint16(133), 522: uint16(133), 523: uint16(134), 524: uint16(134), 525: uint16(134), 526: uint16(134), 527: uint16(19), 528: uint16(19), 529: uint16(19), 530: uint16(19), 531: uint16(412), 532: uint16(581), 533: uint16(1204), 534: uint16(1259), 535: uint16(511), 536: uint16(1204), 537: uint16(319), 538: uint16(567), 539: uint16(320), 540: uint16(944), 541: uint16(153), 542: uint16(425), 543: uint16(491), 544: uint16(430), 545: uint16(943), 546: uint16(1204), 547: uint16(488), 548: uint16(1341), 549: uint16(1450), 550: uint16(532), 551: uint16(1277), 552: uint16(1204), 553: uint16(1205), 554: uint16(1204), 555: uint16(135), 556: uint16(136), 557: uint16(90), 558: uint16(1228), 559: uint16(1228), 560: uint16(1063), 561: uint16(1066), 562: uint16(1053), 563: uint16(1053), 564: uint16(133), 565: uint16(133), 566: uint16(134), 567: uint16(134), 568: uint16(134), 569: uint16(134), 570: uint16(575), 571: uint16(132), 572: uint16(132), 573: uint16(132), 574: uint16(132), 575: uint16(131), 576: uint16(131), 577: uint16(130), 578: uint16(130), 579: uint16(130), 580: uint16(129), 581: uint16(126), 582: uint16(450), 583: uint16(287), 584: uint16(287), 585: uint16(528), 586: uint16(287), 587: uint16(287), 588: uint16(372), 589: uint16(1595), 590: uint16(1204), 591: uint16(1205), 592: uint16(1204), 593: uint16(1204), 594: uint16(1205), 595: uint16(1204), 596: uint16(573), 597: uint16(486), 598: uint16(561), 599: uint16(573), 600: uint16(889), 601: uint16(561), 602: uint16(412), 603: uint16(1204), 604: uint16(1205), 605: uint16(1204), 606: uint16(886), 607: uint16(40), 608: uint16(22), 609: uint16(22), 610: uint16(220), 611: uint16(243), 612: uint16(525), 613: uint16(1449), 614: uint16(132), 615: uint16(132), 616: uint16(132), 617: uint16(132), 618: uint16(131), 619: uint16(131), 620: uint16(130), 621: uint16(130), 622: uint16(130), 623: uint16(129), 624: uint16(126), 625: uint16(450), 626: uint16(135), 627: uint16(136), 628: uint16(90), 629: uint16(1228), 630: uint16(1228), 631: uint16(1063), 632: uint16(1066), 633: uint16(1053), 634: uint16(1053), 635: uint16(133), 636: uint16(133), 637: uint16(134), 638: uint16(134), 639: uint16(134), 640: uint16(134), 641: uint16(412), 642: uint16(180), 643: uint16(454), 644: uint16(1204), 645: uint16(879), 646: uint16(255), 647: uint16(287), 648: uint16(287), 649: uint16(510), 650: uint16(507), 651: uint16(506), 652: uint16(372), 653: uint16(1595), 654: uint16(1568), 655: uint16(1331), 656: uint16(1331), 657: uint16(576), 658: uint16(889), 659: uint16(505), 660: uint16(573), 661: uint16(44), 662: uint16(561), 663: uint16(559), 664: uint16(1207), 665: uint16(135), 666: uint16(136), 667: uint16(90), 668: uint16(1228), 669: uint16(1228), 670: uint16(1063), 671: uint16(1066), 672: uint16(1053), 673: uint16(1053), 674: uint16(133), 675: uint16(133), 676: uint16(134), 677: uint16(134), 678: uint16(134), 679: uint16(134), 680: uint16(81), 681: uint16(81), 682: uint16(422), 683: uint16(576), 684: uint16(377), 685: uint16(132), 686: uint16(132), 687: uint16(132), 688: uint16(132), 689: uint16(131), 690: uint16(131), 691: uint16(130), 692: uint16(130), 693: uint16(130), 694: uint16(129), 695: uint16(126), 696: uint16(450), 697: uint16(297), 698: uint16(287), 699: uint16(287), 700: uint16(460), 701: uint16(1204), 702: uint16(1205), 703: uint16(1204), 704: uint16(1204), 705: uint16(534), 706: uint16(19), 707: uint16(19), 708: uint16(448), 709: uint16(448), 710: uint16(448), 711: uint16(573), 712: uint16(412), 713: uint16(561), 714: uint16(230), 715: uint16(436), 716: uint16(1187), 717: uint16(535), 718: uint16(319), 719: uint16(567), 720: uint16(363), 721: uint16(432), 722: uint16(1207), 723: uint16(1435), 724: uint16(132), 725: uint16(132), 726: uint16(132), 727: uint16(132), 728: uint16(131), 729: uint16(131), 730: uint16(130), 731: uint16(130), 732: uint16(130), 733: uint16(129), 734: uint16(126), 735: uint16(450), 736: uint16(135), 737: uint16(136), 738: uint16(90), 739: uint16(1228), 740: uint16(1228), 741: uint16(1063), 742: uint16(1066), 743: uint16(1053), 744: uint16(1053), 745: uint16(133), 746: uint16(133), 747: uint16(134), 748: uint16(134), 749: uint16(134), 750: uint16(134), 751: uint16(412), 752: uint16(211), 753: uint16(949), 754: uint16(1169), 755: uint16(1041), 756: uint16(1110), 757: uint16(1110), 758: uint16(494), 759: uint16(547), 760: uint16(547), 761: uint16(1204), 762: uint16(1205), 763: uint16(1204), 764: uint16(7), 765: uint16(539), 766: uint16(1570), 767: uint16(1169), 768: uint16(376), 769: uint16(576), 770: uint16(1169), 771: uint16(5), 772: uint16(1204), 773: uint16(486), 774: uint16(3), 775: uint16(135), 776: uint16(136), 777: uint16(90), 778: uint16(1228), 779: uint16(1228), 780: uint16(1063), 781: uint16(1066), 782: uint16(1053), 783: uint16(1053), 784: uint16(133), 785: uint16(133), 786: uint16(134), 787: uint16(134), 788: uint16(134), 789: uint16(134), 790: uint16(576), 791: uint16(513), 792: uint16(19), 793: uint16(19), 794: uint16(427), 795: uint16(132), 796: uint16(132), 797: uint16(132), 798: uint16(132), 799: uint16(131), 800: uint16(131), 801: uint16(130), 802: uint16(130), 803: uint16(130), 804: uint16(129), 805: uint16(126), 806: uint16(450), 807: uint16(305), 808: uint16(1204), 809: uint16(433), 810: uint16(225), 811: uint16(1204), 812: uint16(385), 813: uint16(19), 814: uint16(19), 815: uint16(273), 816: uint16(290), 817: uint16(371), 818: uint16(516), 819: uint16(366), 820: uint16(515), 821: uint16(260), 822: uint16(412), 823: uint16(538), 824: uint16(1568), 825: uint16(549), 826: uint16(1024), 827: uint16(362), 828: uint16(437), 829: uint16(1204), 830: uint16(1205), 831: uint16(1204), 832: uint16(902), 833: uint16(1552), 834: uint16(132), 835: uint16(132), 836: uint16(132), 837: uint16(132), 838: uint16(131), 839: uint16(131), 840: uint16(130), 841: uint16(130), 842: uint16(130), 843: uint16(129), 844: uint16(126), 845: uint16(450), 846: uint16(135), 847: uint16(136), 848: uint16(90), 849: uint16(1228), 850: uint16(1228), 851: uint16(1063), 852: uint16(1066), 853: uint16(1053), 854: uint16(1053), 855: uint16(133), 856: uint16(133), 857: uint16(134), 858: uint16(134), 859: uint16(134), 860: uint16(134), 861: uint16(412), 862: uint16(1435), 863: uint16(514), 864: uint16(1281), 865: uint16(1204), 866: uint16(1205), 867: uint16(1204), 868: uint16(1204), 869: uint16(1205), 870: uint16(1204), 871: uint16(903), 872: uint16(48), 873: uint16(342), 874: uint16(1568), 875: uint16(1568), 876: uint16(1279), 877: uint16(1627), 878: uint16(1568), 879: uint16(911), 880: uint16(576), 881: uint16(129), 882: uint16(126), 883: uint16(450), 884: uint16(110), 885: uint16(135), 886: uint16(136), 887: uint16(90), 888: uint16(1228), 889: uint16(1228), 890: uint16(1063), 891: uint16(1066), 892: uint16(1053), 893: uint16(1053), 894: uint16(133), 895: uint16(133), 896: uint16(134), 897: uint16(134), 898: uint16(134), 899: uint16(134), 900: uint16(265), 901: uint16(576), 902: uint16(459), 903: uint16(19), 904: uint16(19), 905: uint16(132), 906: uint16(132), 907: uint16(132), 908: uint16(132), 909: uint16(131), 910: uint16(131), 911: uint16(130), 912: uint16(130), 913: uint16(130), 914: uint16(129), 915: uint16(126), 916: uint16(450), 917: uint16(1345), 918: uint16(204), 919: uint16(576), 920: uint16(459), 921: uint16(458), 922: uint16(50), 923: uint16(47), 924: uint16(19), 925: uint16(19), 926: uint16(49), 927: uint16(434), 928: uint16(1105), 929: uint16(573), 930: uint16(497), 931: uint16(561), 932: uint16(412), 933: uint16(428), 934: uint16(108), 935: uint16(1224), 936: uint16(1569), 937: uint16(1554), 938: uint16(376), 939: uint16(205), 940: uint16(550), 941: uint16(550), 942: uint16(81), 943: uint16(81), 944: uint16(132), 945: uint16(132), 946: uint16(132), 947: uint16(132), 948: uint16(131), 949: uint16(131), 950: uint16(130), 951: uint16(130), 952: uint16(130), 953: uint16(129), 954: uint16(126), 955: uint16(450), 956: uint16(135), 957: uint16(136), 958: uint16(90), 959: uint16(1228), 960: uint16(1228), 961: uint16(1063), 962: uint16(1066), 963: uint16(1053), 964: uint16(1053), 965: uint16(133), 966: uint16(133), 967: uint16(134), 968: uint16(134), 969: uint16(134), 970: uint16(134), 971: uint16(480), 972: uint16(576), 973: uint16(1204), 974: uint16(576), 975: uint16(1541), 976: uint16(412), 977: uint16(1435), 978: uint16(969), 979: uint16(315), 980: uint16(1659), 981: uint16(398), 982: uint16(284), 983: uint16(497), 984: uint16(969), 985: uint16(893), 986: uint16(1569), 987: uint16(1569), 988: uint16(376), 989: uint16(376), 990: uint16(1569), 991: uint16(461), 992: uint16(376), 993: uint16(1224), 994: uint16(459), 995: uint16(80), 996: uint16(80), 997: uint16(81), 998: uint16(81), 999: uint16(497), 1000: uint16(374), 1001: uint16(114), 1002: uint16(90), 1003: uint16(1228), 1004: uint16(1228), 1005: uint16(1063), 1006: uint16(1066), 1007: uint16(1053), 1008: uint16(1053), 1009: uint16(133), 1010: uint16(133), 1011: uint16(134), 1012: uint16(134), 1013: uint16(134), 1014: uint16(134), 1015: uint16(132), 1016: uint16(132), 1017: uint16(132), 1018: uint16(132), 1019: uint16(131), 1020: uint16(131), 1021: uint16(130), 1022: uint16(130), 1023: uint16(130), 1024: uint16(129), 1025: uint16(126), 1026: uint16(450), 1027: uint16(1204), 1028: uint16(1505), 1029: uint16(576), 1030: uint16(1204), 1031: uint16(1205), 1032: uint16(1204), 1033: uint16(1366), 1034: uint16(316), 1035: uint16(486), 1036: uint16(281), 1037: uint16(281), 1038: uint16(497), 1039: uint16(431), 1040: uint16(557), 1041: uint16(288), 1042: uint16(288), 1043: uint16(402), 1044: uint16(1340), 1045: uint16(471), 1046: uint16(345), 1047: uint16(298), 1048: uint16(429), 1049: uint16(573), 1050: uint16(576), 1051: uint16(561), 1052: uint16(81), 1053: uint16(81), 1054: uint16(573), 1055: uint16(374), 1056: uint16(561), 1057: uint16(971), 1058: uint16(386), 1059: uint16(132), 1060: uint16(132), 1061: uint16(132), 1062: uint16(132), 1063: uint16(131), 1064: uint16(131), 1065: uint16(130), 1066: uint16(130), 1067: uint16(130), 1068: uint16(129), 1069: uint16(126), 1070: uint16(450), 1071: uint16(231), 1072: uint16(117), 1073: uint16(81), 1074: uint16(81), 1075: uint16(287), 1076: uint16(287), 1077: uint16(231), 1078: uint16(287), 1079: uint16(287), 1080: uint16(576), 1081: uint16(1511), 1082: uint16(576), 1083: uint16(1336), 1084: uint16(1204), 1085: uint16(1205), 1086: uint16(1204), 1087: uint16(139), 1088: uint16(573), 1089: uint16(556), 1090: uint16(561), 1091: uint16(573), 1092: uint16(412), 1093: uint16(561), 1094: uint16(441), 1095: uint16(456), 1096: uint16(969), 1097: uint16(213), 1098: uint16(558), 1099: uint16(1511), 1100: uint16(1513), 1101: uint16(1550), 1102: uint16(969), 1103: uint16(143), 1104: uint16(143), 1105: uint16(145), 1106: uint16(145), 1107: uint16(1368), 1108: uint16(314), 1109: uint16(478), 1110: uint16(444), 1111: uint16(970), 1112: uint16(412), 1113: uint16(850), 1114: uint16(851), 1115: uint16(852), 1116: uint16(135), 1117: uint16(136), 1118: uint16(90), 1119: uint16(1228), 1120: uint16(1228), 1121: uint16(1063), 1122: uint16(1066), 1123: uint16(1053), 1124: uint16(1053), 1125: uint16(133), 1126: uint16(133), 1127: uint16(134), 1128: uint16(134), 1129: uint16(134), 1130: uint16(134), 1131: uint16(357), 1132: uint16(412), 1133: uint16(397), 1134: uint16(1148), 1135: uint16(304), 1136: uint16(135), 1137: uint16(136), 1138: uint16(90), 1139: uint16(1228), 1140: uint16(1228), 1141: uint16(1063), 1142: uint16(1066), 1143: uint16(1053), 1144: uint16(1053), 1145: uint16(133), 1146: uint16(133), 1147: uint16(134), 1148: uint16(134), 1149: uint16(134), 1150: uint16(134), 1151: uint16(1575), 1152: uint16(323), 1153: uint16(6), 1154: uint16(862), 1155: uint16(7), 1156: uint16(135), 1157: uint16(124), 1158: uint16(90), 1159: uint16(1228), 1160: uint16(1228), 1161: uint16(1063), 1162: uint16(1066), 1163: uint16(1053), 1164: uint16(1053), 1165: uint16(133), 1166: uint16(133), 1167: uint16(134), 1168: uint16(134), 1169: uint16(134), 1170: uint16(134), 1171: uint16(409), 1172: uint16(408), 1173: uint16(1511), 1174: uint16(212), 1175: uint16(132), 1176: uint16(132), 1177: uint16(132), 1178: uint16(132), 1179: uint16(131), 1180: uint16(131), 1181: uint16(130), 1182: uint16(130), 1183: uint16(130), 1184: uint16(129), 1185: uint16(126), 1186: uint16(450), 1187: uint16(411), 1188: uint16(118), 1189: uint16(1204), 1190: uint16(116), 1191: uint16(10), 1192: uint16(352), 1193: uint16(265), 1194: uint16(355), 1195: uint16(132), 1196: uint16(132), 1197: uint16(132), 1198: uint16(132), 1199: uint16(131), 1200: uint16(131), 1201: uint16(130), 1202: uint16(130), 1203: uint16(130), 1204: uint16(129), 1205: uint16(126), 1206: uint16(450), 1207: uint16(576), 1208: uint16(324), 1209: uint16(306), 1210: uint16(576), 1211: uint16(306), 1212: uint16(1250), 1213: uint16(469), 1214: uint16(158), 1215: uint16(132), 1216: uint16(132), 1217: uint16(132), 1218: uint16(132), 1219: uint16(131), 1220: uint16(131), 1221: uint16(130), 1222: uint16(130), 1223: uint16(130), 1224: uint16(129), 1225: uint16(126), 1226: uint16(450), 1227: uint16(207), 1228: uint16(1224), 1229: uint16(1126), 1230: uint16(65), 1231: uint16(65), 1232: uint16(470), 1233: uint16(66), 1234: uint16(66), 1235: uint16(412), 1236: uint16(447), 1237: uint16(446), 1238: uint16(882), 1239: uint16(531), 1240: uint16(335), 1241: uint16(258), 1242: uint16(257), 1243: uint16(256), 1244: uint16(1127), 1245: uint16(1233), 1246: uint16(1204), 1247: uint16(1205), 1248: uint16(1204), 1249: uint16(327), 1250: uint16(1235), 1251: uint16(874), 1252: uint16(159), 1253: uint16(576), 1254: uint16(16), 1255: uint16(480), 1256: uint16(1085), 1257: uint16(1040), 1258: uint16(1234), 1259: uint16(1128), 1260: uint16(136), 1261: uint16(90), 1262: uint16(1228), 1263: uint16(1228), 1264: uint16(1063), 1265: uint16(1066), 1266: uint16(1053), 1267: uint16(1053), 1268: uint16(133), 1269: uint16(133), 1270: uint16(134), 1271: uint16(134), 1272: uint16(134), 1273: uint16(134), 1274: uint16(1029), 1275: uint16(576), 1276: uint16(81), 1277: uint16(81), 1278: uint16(1028), 1279: uint16(1040), 1280: uint16(922), 1281: uint16(576), 1282: uint16(463), 1283: uint16(1236), 1284: uint16(576), 1285: uint16(1236), 1286: uint16(1224), 1287: uint16(502), 1288: uint16(107), 1289: uint16(1435), 1290: uint16(923), 1291: uint16(6), 1292: uint16(576), 1293: uint16(410), 1294: uint16(1498), 1295: uint16(882), 1296: uint16(1029), 1297: uint16(480), 1298: uint16(21), 1299: uint16(21), 1300: uint16(1028), 1301: uint16(332), 1302: uint16(1380), 1303: uint16(334), 1304: uint16(53), 1305: uint16(53), 1306: uint16(497), 1307: uint16(81), 1308: uint16(81), 1309: uint16(874), 1310: uint16(1028), 1311: uint16(1028), 1312: uint16(1030), 1313: uint16(445), 1314: uint16(259), 1315: uint16(19), 1316: uint16(19), 1317: uint16(533), 1318: uint16(132), 1319: uint16(132), 1320: uint16(132), 1321: uint16(132), 1322: uint16(131), 1323: uint16(131), 1324: uint16(130), 1325: uint16(130), 1326: uint16(130), 1327: uint16(129), 1328: uint16(126), 1329: uint16(450), 1330: uint16(551), 1331: uint16(301), 1332: uint16(1028), 1333: uint16(1028), 1334: uint16(1030), 1335: uint16(107), 1336: uint16(532), 1337: uint16(545), 1338: uint16(121), 1339: uint16(568), 1340: uint16(1188), 1341: uint16(4), 1342: uint16(1126), 1343: uint16(1576), 1344: uint16(449), 1345: uint16(576), 1346: uint16(462), 1347: uint16(7), 1348: uint16(1282), 1349: uint16(418), 1350: uint16(462), 1351: uint16(350), 1352: uint16(1435), 1353: uint16(576), 1354: uint16(518), 1355: uint16(571), 1356: uint16(544), 1357: uint16(1127), 1358: uint16(121), 1359: uint16(568), 1360: uint16(442), 1361: uint16(4), 1362: uint16(1188), 1363: uint16(464), 1364: uint16(533), 1365: uint16(1180), 1366: uint16(1223), 1367: uint16(9), 1368: uint16(67), 1369: uint16(67), 1370: uint16(487), 1371: uint16(576), 1372: uint16(1128), 1373: uint16(303), 1374: uint16(410), 1375: uint16(571), 1376: uint16(54), 1377: uint16(54), 1378: uint16(451), 1379: uint16(576), 1380: uint16(123), 1381: uint16(944), 1382: uint16(576), 1383: uint16(417), 1384: uint16(576), 1385: uint16(333), 1386: uint16(943), 1387: uint16(1379), 1388: uint16(576), 1389: uint16(236), 1390: uint16(565), 1391: uint16(576), 1392: uint16(1574), 1393: uint16(564), 1394: uint16(68), 1395: uint16(68), 1396: uint16(7), 1397: uint16(576), 1398: uint16(451), 1399: uint16(362), 1400: uint16(419), 1401: uint16(182), 1402: uint16(69), 1403: uint16(69), 1404: uint16(541), 1405: uint16(70), 1406: uint16(70), 1407: uint16(71), 1408: uint16(71), 1409: uint16(540), 1410: uint16(565), 1411: uint16(72), 1412: uint16(72), 1413: uint16(484), 1414: uint16(55), 1415: uint16(55), 1416: uint16(473), 1417: uint16(1180), 1418: uint16(296), 1419: uint16(1040), 1420: uint16(56), 1421: uint16(56), 1422: uint16(296), 1423: uint16(493), 1424: uint16(541), 1425: uint16(119), 1426: uint16(119), 1427: uint16(410), 1428: uint16(1573), 1429: uint16(542), 1430: uint16(569), 1431: uint16(418), 1432: uint16(7), 1433: uint16(120), 1434: uint16(1244), 1435: uint16(451), 1436: uint16(577), 1437: uint16(451), 1438: uint16(465), 1439: uint16(1040), 1440: uint16(1028), 1441: uint16(576), 1442: uint16(1557), 1443: uint16(552), 1444: uint16(476), 1445: uint16(119), 1446: uint16(119), 1447: uint16(527), 1448: uint16(259), 1449: uint16(121), 1450: uint16(568), 1451: uint16(240), 1452: uint16(4), 1453: uint16(120), 1454: uint16(576), 1455: uint16(451), 1456: uint16(577), 1457: uint16(451), 1458: uint16(576), 1459: uint16(477), 1460: uint16(1028), 1461: uint16(576), 1462: uint16(156), 1463: uint16(576), 1464: uint16(57), 1465: uint16(57), 1466: uint16(571), 1467: uint16(576), 1468: uint16(286), 1469: uint16(229), 1470: uint16(410), 1471: uint16(336), 1472: uint16(1028), 1473: uint16(1028), 1474: uint16(1030), 1475: uint16(1031), 1476: uint16(35), 1477: uint16(59), 1478: uint16(59), 1479: uint16(219), 1480: uint16(983), 1481: uint16(60), 1482: uint16(60), 1483: uint16(220), 1484: uint16(73), 1485: uint16(73), 1486: uint16(74), 1487: uint16(74), 1488: uint16(984), 1489: uint16(451), 1490: uint16(75), 1491: uint16(75), 1492: uint16(1028), 1493: uint16(1028), 1494: uint16(1030), 1495: uint16(1031), 1496: uint16(35), 1497: uint16(96), 1498: uint16(216), 1499: uint16(291), 1500: uint16(552), 1501: uint16(565), 1502: uint16(1188), 1503: uint16(318), 1504: uint16(395), 1505: uint16(395), 1506: uint16(394), 1507: uint16(276), 1508: uint16(392), 1509: uint16(576), 1510: uint16(485), 1511: uint16(859), 1512: uint16(474), 1513: uint16(1311), 1514: uint16(410), 1515: uint16(541), 1516: uint16(576), 1517: uint16(417), 1518: uint16(1530), 1519: uint16(1144), 1520: uint16(540), 1521: uint16(399), 1522: uint16(1188), 1523: uint16(292), 1524: uint16(237), 1525: uint16(1153), 1526: uint16(326), 1527: uint16(38), 1528: uint16(23), 1529: uint16(576), 1530: uint16(1040), 1531: uint16(576), 1532: uint16(20), 1533: uint16(20), 1534: uint16(325), 1535: uint16(299), 1536: uint16(119), 1537: uint16(119), 1538: uint16(164), 1539: uint16(76), 1540: uint16(76), 1541: uint16(1529), 1542: uint16(121), 1543: uint16(568), 1544: uint16(120), 1545: uint16(4), 1546: uint16(451), 1547: uint16(577), 1548: uint16(451), 1549: uint16(203), 1550: uint16(576), 1551: uint16(1028), 1552: uint16(141), 1553: uint16(141), 1554: uint16(142), 1555: uint16(142), 1556: uint16(576), 1557: uint16(322), 1558: uint16(39), 1559: uint16(571), 1560: uint16(341), 1561: uint16(1021), 1562: uint16(110), 1563: uint16(264), 1564: uint16(239), 1565: uint16(901), 1566: uint16(900), 1567: uint16(423), 1568: uint16(242), 1569: uint16(908), 1570: uint16(909), 1571: uint16(370), 1572: uint16(173), 1573: uint16(77), 1574: uint16(77), 1575: uint16(43), 1576: uint16(479), 1577: uint16(1310), 1578: uint16(264), 1579: uint16(62), 1580: uint16(62), 1581: uint16(369), 1582: uint16(451), 1583: uint16(1028), 1584: uint16(1028), 1585: uint16(1030), 1586: uint16(1031), 1587: uint16(35), 1588: uint16(1601), 1589: uint16(1192), 1590: uint16(453), 1591: uint16(1092), 1592: uint16(238), 1593: uint16(291), 1594: uint16(565), 1595: uint16(163), 1596: uint16(1309), 1597: uint16(110), 1598: uint16(395), 1599: uint16(395), 1600: uint16(394), 1601: uint16(276), 1602: uint16(392), 1603: uint16(986), 1604: uint16(987), 1605: uint16(859), 1606: uint16(481), 1607: uint16(346), 1608: uint16(264), 1609: uint16(110), 1610: uint16(1032), 1611: uint16(489), 1612: uint16(576), 1613: uint16(1188), 1614: uint16(503), 1615: uint16(1088), 1616: uint16(261), 1617: uint16(261), 1618: uint16(237), 1619: uint16(576), 1620: uint16(326), 1621: uint16(121), 1622: uint16(568), 1623: uint16(1040), 1624: uint16(4), 1625: uint16(347), 1626: uint16(1376), 1627: uint16(413), 1628: uint16(325), 1629: uint16(119), 1630: uint16(119), 1631: uint16(948), 1632: uint16(319), 1633: uint16(567), 1634: uint16(351), 1635: uint16(78), 1636: uint16(78), 1637: uint16(120), 1638: uint16(571), 1639: uint16(451), 1640: uint16(577), 1641: uint16(451), 1642: uint16(79), 1643: uint16(79), 1644: uint16(1028), 1645: uint16(354), 1646: uint16(356), 1647: uint16(576), 1648: uint16(360), 1649: uint16(1092), 1650: uint16(110), 1651: uint16(576), 1652: uint16(974), 1653: uint16(942), 1654: uint16(264), 1655: uint16(123), 1656: uint16(457), 1657: uint16(358), 1658: uint16(239), 1659: uint16(576), 1660: uint16(519), 1661: uint16(451), 1662: uint16(939), 1663: uint16(1104), 1664: uint16(123), 1665: uint16(1104), 1666: uint16(173), 1667: uint16(576), 1668: uint16(1032), 1669: uint16(43), 1670: uint16(63), 1671: uint16(63), 1672: uint16(1324), 1673: uint16(565), 1674: uint16(168), 1675: uint16(168), 1676: uint16(1028), 1677: uint16(1028), 1678: uint16(1030), 1679: uint16(1031), 1680: uint16(35), 1681: uint16(576), 1682: uint16(169), 1683: uint16(169), 1684: uint16(1308), 1685: uint16(872), 1686: uint16(238), 1687: uint16(157), 1688: uint16(1589), 1689: uint16(576), 1690: uint16(86), 1691: uint16(86), 1692: uint16(365), 1693: uint16(89), 1694: uint16(568), 1695: uint16(375), 1696: uint16(4), 1697: uint16(1103), 1698: uint16(941), 1699: uint16(1103), 1700: uint16(123), 1701: uint16(576), 1702: uint16(1040), 1703: uint16(1389), 1704: uint16(64), 1705: uint16(64), 1706: uint16(1188), 1707: uint16(1434), 1708: uint16(119), 1709: uint16(119), 1710: uint16(571), 1711: uint16(576), 1712: uint16(82), 1713: uint16(82), 1714: uint16(563), 1715: uint16(576), 1716: uint16(120), 1717: uint16(165), 1718: uint16(451), 1719: uint16(577), 1720: uint16(451), 1721: uint16(413), 1722: uint16(1362), 1723: uint16(1028), 1724: uint16(144), 1725: uint16(144), 1726: uint16(319), 1727: uint16(567), 1728: uint16(576), 1729: uint16(1374), 1730: uint16(562), 1731: uint16(498), 1732: uint16(279), 1733: uint16(451), 1734: uint16(83), 1735: uint16(83), 1736: uint16(1439), 1737: uint16(576), 1738: uint16(166), 1739: uint16(166), 1740: uint16(576), 1741: uint16(1289), 1742: uint16(554), 1743: uint16(576), 1744: uint16(1280), 1745: uint16(565), 1746: uint16(576), 1747: uint16(12), 1748: uint16(576), 1749: uint16(1268), 1750: uint16(457), 1751: uint16(146), 1752: uint16(146), 1753: uint16(1267), 1754: uint16(576), 1755: uint16(1028), 1756: uint16(1028), 1757: uint16(1030), 1758: uint16(1031), 1759: uint16(35), 1760: uint16(140), 1761: uint16(140), 1762: uint16(1269), 1763: uint16(167), 1764: uint16(167), 1765: uint16(1609), 1766: uint16(160), 1767: uint16(160), 1768: uint16(1359), 1769: uint16(150), 1770: uint16(150), 1771: uint16(149), 1772: uint16(149), 1773: uint16(311), 1774: uint16(1040), 1775: uint16(576), 1776: uint16(312), 1777: uint16(147), 1778: uint16(147), 1779: uint16(313), 1780: uint16(119), 1781: uint16(119), 1782: uint16(222), 1783: uint16(235), 1784: uint16(576), 1785: uint16(1188), 1786: uint16(396), 1787: uint16(576), 1788: uint16(120), 1789: uint16(576), 1790: uint16(451), 1791: uint16(577), 1792: uint16(451), 1793: uint16(1192), 1794: uint16(453), 1795: uint16(1028), 1796: uint16(508), 1797: uint16(291), 1798: uint16(148), 1799: uint16(148), 1800: uint16(1421), 1801: uint16(1612), 1802: uint16(395), 1803: uint16(395), 1804: uint16(394), 1805: uint16(276), 1806: uint16(392), 1807: uint16(85), 1808: uint16(85), 1809: uint16(859), 1810: uint16(87), 1811: uint16(87), 1812: uint16(84), 1813: uint16(84), 1814: uint16(553), 1815: uint16(576), 1816: uint16(294), 1817: uint16(576), 1818: uint16(1426), 1819: uint16(338), 1820: uint16(339), 1821: uint16(1425), 1822: uint16(237), 1823: uint16(300), 1824: uint16(326), 1825: uint16(1416), 1826: uint16(1409), 1827: uint16(1028), 1828: uint16(1028), 1829: uint16(1030), 1830: uint16(1031), 1831: uint16(35), 1832: uint16(325), 1833: uint16(344), 1834: uint16(403), 1835: uint16(483), 1836: uint16(226), 1837: uint16(1307), 1838: uint16(52), 1839: uint16(52), 1840: uint16(58), 1841: uint16(58), 1842: uint16(368), 1843: uint16(1371), 1844: uint16(1502), 1845: uint16(566), 1846: uint16(1501), 1847: uint16(121), 1848: uint16(568), 1849: uint16(221), 1850: uint16(4), 1851: uint16(208), 1852: uint16(268), 1853: uint16(209), 1854: uint16(390), 1855: uint16(1244), 1856: uint16(1549), 1857: uint16(1188), 1858: uint16(1372), 1859: uint16(1370), 1860: uint16(1369), 1861: uint16(1547), 1862: uint16(239), 1863: uint16(184), 1864: uint16(571), 1865: uint16(233), 1866: uint16(421), 1867: uint16(1241), 1868: uint16(95), 1869: uint16(218), 1870: uint16(173), 1871: uint16(1507), 1872: uint16(193), 1873: uint16(43), 1874: uint16(91), 1875: uint16(94), 1876: uint16(178), 1877: uint16(186), 1878: uint16(467), 1879: uint16(188), 1880: uint16(468), 1881: uint16(1422), 1882: uint16(13), 1883: uint16(189), 1884: uint16(190), 1885: uint16(191), 1886: uint16(501), 1887: uint16(451), 1888: uint16(245), 1889: uint16(108), 1890: uint16(238), 1891: uint16(401), 1892: uint16(1428), 1893: uint16(1427), 1894: uint16(1430), 1895: uint16(475), 1896: uint16(404), 1897: uint16(1496), 1898: uint16(197), 1899: uint16(565), 1900: uint16(14), 1901: uint16(490), 1902: uint16(249), 1903: uint16(101), 1904: uint16(1518), 1905: uint16(496), 1906: uint16(349), 1907: uint16(280), 1908: uint16(251), 1909: uint16(201), 1910: uint16(353), 1911: uint16(499), 1912: uint16(252), 1913: uint16(406), 1914: uint16(1270), 1915: uint16(253), 1916: uint16(517), 1917: uint16(1327), 1918: uint16(1326), 1919: uint16(435), 1920: uint16(1325), 1921: uint16(1318), 1922: uint16(103), 1923: uint16(893), 1924: uint16(1296), 1925: uint16(413), 1926: uint16(227), 1927: uint16(407), 1928: uint16(1040), 1929: uint16(1626), 1930: uint16(319), 1931: uint16(567), 1932: uint16(1625), 1933: uint16(1297), 1934: uint16(119), 1935: uint16(119), 1936: uint16(439), 1937: uint16(367), 1938: uint16(1317), 1939: uint16(1295), 1940: uint16(1624), 1941: uint16(526), 1942: uint16(120), 1943: uint16(440), 1944: uint16(451), 1945: uint16(577), 1946: uint16(451), 1947: uint16(1594), 1948: uint16(309), 1949: uint16(1028), 1950: uint16(310), 1951: uint16(373), 1952: uint16(266), 1953: uint16(267), 1954: uint16(457), 1955: uint16(1580), 1956: uint16(1579), 1957: uint16(443), 1958: uint16(138), 1959: uint16(1394), 1960: uint16(552), 1961: uint16(1393), 1962: uint16(11), 1963: uint16(1483), 1964: uint16(384), 1965: uint16(115), 1966: uint16(317), 1967: uint16(1350), 1968: uint16(109), 1969: uint16(536), 1970: uint16(42), 1971: uint16(579), 1972: uint16(382), 1973: uint16(214), 1974: uint16(1349), 1975: uint16(388), 1976: uint16(1198), 1977: uint16(389), 1978: uint16(275), 1979: uint16(277), 1980: uint16(278), 1981: uint16(1028), 1982: uint16(1028), 1983: uint16(1030), 1984: uint16(1031), 1985: uint16(35), 1986: uint16(580), 1987: uint16(1265), 1988: uint16(414), 1989: uint16(1260), 1990: uint16(170), 1991: uint16(415), 1992: uint16(183), 1993: uint16(1534), 1994: uint16(1535), 1995: uint16(1533), 1996: uint16(171), 1997: uint16(154), 1998: uint16(307), 1999: uint16(1532), 2000: uint16(846), 2001: uint16(223), 2002: uint16(224), 2003: uint16(88), 2004: uint16(452), 2005: uint16(215), 2006: uint16(172), 2007: uint16(321), 2008: uint16(234), 2009: uint16(1102), 2010: uint16(152), 2011: uint16(1188), 2012: uint16(1100), 2013: uint16(329), 2014: uint16(185), 2015: uint16(174), 2016: uint16(1223), 2017: uint16(925), 2018: uint16(187), 2019: uint16(241), 2020: uint16(337), 2021: uint16(244), 2022: uint16(1116), 2023: uint16(192), 2024: uint16(175), 2025: uint16(176), 2026: uint16(424), 2027: uint16(426), 2028: uint16(97), 2029: uint16(194), 2030: uint16(98), 2031: uint16(99), 2032: uint16(100), 2033: uint16(177), 2034: uint16(1119), 2035: uint16(1115), 2036: uint16(246), 2037: uint16(247), 2038: uint16(161), 2039: uint16(24), 2040: uint16(248), 2041: uint16(348), 2042: uint16(1238), 2043: uint16(264), 2044: uint16(1108), 2045: uint16(250), 2046: uint16(495), 2047: uint16(199), 2048: uint16(198), 2049: uint16(15), 2050: uint16(861), 2051: uint16(500), 2052: uint16(369), 2053: uint16(254), 2054: uint16(504), 2055: uint16(509), 2056: uint16(512), 2057: uint16(200), 2058: uint16(102), 2059: uint16(25), 2060: uint16(179), 2061: uint16(361), 2062: uint16(26), 2063: uint16(364), 2064: uint16(104), 2065: uint16(891), 2066: uint16(308), 2067: uint16(162), 2068: uint16(105), 2069: uint16(904), 2070: uint16(520), 2071: uint16(106), 2072: uint16(1185), 2073: uint16(1069), 2074: uint16(1155), 2075: uint16(17), 2076: uint16(228), 2077: uint16(27), 2078: uint16(1154), 2079: uint16(283), 2080: uint16(285), 2081: uint16(263), 2082: uint16(978), 2083: uint16(202), 2084: uint16(972), 2085: uint16(123), 2086: uint16(28), 2087: uint16(1175), 2088: uint16(29), 2089: uint16(30), 2090: uint16(1179), 2091: uint16(1171), 2092: uint16(31), 2093: uint16(1173), 2094: uint16(1160), 2095: uint16(41), 2096: uint16(32), 2097: uint16(206), 2098: uint16(548), 2099: uint16(33), 2100: uint16(110), 2101: uint16(1178), 2102: uint16(1083), 2103: uint16(8), 2104: uint16(112), 2105: uint16(1070), 2106: uint16(113), 2107: uint16(1068), 2108: uint16(1072), 2109: uint16(34), 2110: uint16(1073), 2111: uint16(560), 2112: uint16(1125), 2113: uint16(269), 2114: uint16(1124), 2115: uint16(270), 2116: uint16(36), 2117: uint16(18), 2118: uint16(1194), 2119: uint16(1033), 2120: uint16(873), 2121: uint16(151), 2122: uint16(122), 2123: uint16(37), 2124: uint16(393), 2125: uint16(271), 2126: uint16(272), 2127: uint16(572), 2128: uint16(181), 2129: uint16(1193), 2130: uint16(1256), 2131: uint16(1256), 2132: uint16(1256), 2133: uint16(935), 2134: uint16(1256), 2135: uint16(1256), 2136: uint16(1256), 2137: uint16(1256), 2138: uint16(1256), 2139: uint16(1256), 2140: uint16(1256), 2141: uint16(1617), } var _yy_lookahead = [2328]uint16{ 0: uint16(194), 1: uint16(276), 2: uint16(277), 3: uint16(278), 4: uint16(216), 5: uint16(194), 6: uint16(194), 7: uint16(217), 8: uint16(194), 9: uint16(194), 10: uint16(194), 11: uint16(194), 12: uint16(224), 13: uint16(194), 14: uint16(194), 15: uint16(276), 16: uint16(277), 17: uint16(278), 18: uint16(204), 19: uint16(19), 20: uint16(206), 21: uint16(202), 22: uint16(297), 23: uint16(217), 24: uint16(218), 25: uint16(205), 26: uint16(207), 27: uint16(217), 28: uint16(205), 29: uint16(217), 30: uint16(218), 31: uint16(31), 32: uint16(217), 33: uint16(218), 34: uint16(217), 35: uint16(218), 36: uint16(29), 37: uint16(217), 38: uint16(218), 39: uint16(39), 40: uint16(33), 41: uint16(217), 42: uint16(220), 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(312), 59: uint16(19), 60: uint16(240), 61: uint16(241), 62: uint16(316), 63: uint16(240), 64: uint16(241), 65: uint16(194), 66: uint16(46), 67: uint16(47), 68: uint16(48), 69: uint16(49), 70: uint16(22), 71: uint16(254), 72: uint16(65), 73: uint16(253), 74: uint16(254), 75: uint16(255), 76: uint16(253), 77: uint16(194), 78: uint16(255), 79: uint16(194), 80: uint16(263), 81: uint16(258), 82: uint16(259), 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(276), 99: uint16(277), 100: uint16(278), 101: uint16(285), 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(59), 115: uint16(186), 116: uint16(187), 117: uint16(188), 118: uint16(189), 119: uint16(190), 120: uint16(191), 121: uint16(310), 122: uint16(239), 123: uint16(317), 124: uint16(318), 125: uint16(196), 126: uint16(86), 127: uint16(198), 128: uint16(88), 129: uint16(317), 130: uint16(19), 131: uint16(319), 132: uint16(317), 133: uint16(318), 134: uint16(205), 135: uint16(264), 136: uint16(25), 137: uint16(211), 138: uint16(212), 139: uint16(213), 140: uint16(205), 141: uint16(121), 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(240), 170: uint16(241), 171: uint16(116), 172: uint16(117), 173: uint16(118), 174: uint16(119), 175: uint16(240), 176: uint16(241), 177: uint16(122), 178: uint16(123), 179: uint16(124), 180: uint16(69), 181: uint16(298), 182: uint16(253), 183: uint16(194), 184: uint16(255), 185: uint16(106), 186: uint16(107), 187: uint16(132), 188: uint16(253), 189: uint16(141), 190: uint16(255), 191: uint16(54), 192: uint16(55), 193: uint16(56), 194: uint16(57), 195: uint16(58), 196: uint16(207), 197: uint16(268), 198: uint16(102), 199: uint16(103), 200: uint16(104), 201: uint16(105), 202: uint16(106), 203: uint16(107), 204: uint16(108), 205: uint16(109), 206: uint16(110), 207: uint16(111), 208: uint16(112), 209: uint16(113), 210: uint16(214), 211: uint16(128), 212: uint16(129), 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(134), 226: uint16(25), 227: uint16(136), 228: uint16(137), 229: uint16(300), 230: uint16(165), 231: uint16(166), 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(108), 252: uint16(109), 253: uint16(110), 254: uint16(111), 255: uint16(112), 256: uint16(113), 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(276), 273: uint16(277), 274: uint16(278), 275: uint16(113), 276: uint16(194), 277: uint16(19), 278: uint16(22), 279: uint16(23), 280: uint16(194), 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(220), 296: uint16(250), 297: uint16(59), 298: uint16(252), 299: uint16(217), 300: uint16(218), 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(106), 329: uint16(107), 330: uint16(108), 331: uint16(109), 332: uint16(110), 333: uint16(111), 334: uint16(112), 335: uint16(113), 336: uint16(254), 337: uint16(59), 338: uint16(205), 339: uint16(138), 340: uint16(139), 341: uint16(19), 342: uint16(20), 343: uint16(194), 344: uint16(22), 345: uint16(263), 346: uint16(22), 347: uint16(23), 348: uint16(231), 349: uint16(25), 350: uint16(72), 351: uint16(276), 352: uint16(277), 353: uint16(278), 354: uint16(116), 355: uint16(117), 356: uint16(118), 357: uint16(101), 358: uint16(36), 359: uint16(76), 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(89), 373: uint16(240), 374: uint16(241), 375: uint16(92), 376: uint16(73), 377: uint16(194), 378: uint16(194), 379: uint16(73), 380: uint16(19), 381: uint16(59), 382: uint16(188), 383: uint16(189), 384: uint16(190), 385: uint16(191), 386: uint16(253), 387: uint16(81), 388: uint16(255), 389: uint16(151), 390: uint16(196), 391: uint16(25), 392: uint16(198), 393: uint16(71), 394: uint16(116), 395: uint16(117), 396: uint16(118), 397: uint16(311), 398: uint16(312), 399: uint16(205), 400: uint16(217), 401: uint16(218), 402: uint16(316), 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(270), 420: uint16(22), 421: uint16(23), 422: uint16(100), 423: uint16(25), 424: uint16(59), 425: uint16(101), 426: uint16(138), 427: uint16(139), 428: uint16(106), 429: uint16(107), 430: uint16(127), 431: uint16(128), 432: uint16(129), 433: uint16(127), 434: uint16(240), 435: uint16(241), 436: uint16(114), 437: uint16(254), 438: uint16(116), 439: uint16(117), 440: uint16(118), 441: uint16(76), 442: uint16(76), 443: uint16(121), 444: uint16(138), 445: uint16(139), 446: uint16(263), 447: uint16(253), 448: uint16(264), 449: uint16(255), 450: uint16(205), 451: uint16(275), 452: uint16(87), 453: uint16(19), 454: uint16(89), 455: uint16(89), 456: uint16(194), 457: uint16(92), 458: uint16(92), 459: uint16(199), 460: uint16(138), 461: uint16(139), 462: uint16(268), 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(81), 481: uint16(116), 482: uint16(117), 483: uint16(118), 484: uint16(129), 485: uint16(240), 486: uint16(241), 487: uint16(224), 488: uint16(19), 489: uint16(226), 490: uint16(314), 491: uint16(315), 492: uint16(23), 493: uint16(25), 494: uint16(300), 495: uint16(59), 496: uint16(22), 497: uint16(234), 498: uint16(253), 499: uint16(101), 500: uint16(255), 501: uint16(236), 502: uint16(237), 503: uint16(26), 504: uint16(194), 505: uint16(183), 506: uint16(194), 507: uint16(152), 508: uint16(72), 509: uint16(22), 510: uint16(145), 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(217), 528: uint16(218), 529: uint16(217), 530: uint16(218), 531: uint16(19), 532: uint16(189), 533: uint16(59), 534: uint16(191), 535: uint16(23), 536: uint16(59), 537: uint16(138), 538: uint16(139), 539: uint16(196), 540: uint16(135), 541: uint16(198), 542: uint16(232), 543: uint16(283), 544: uint16(232), 545: uint16(140), 546: uint16(59), 547: uint16(287), 548: uint16(205), 549: uint16(275), 550: uint16(116), 551: uint16(205), 552: uint16(116), 553: uint16(117), 554: uint16(118), 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(194), 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(240), 584: uint16(241), 585: uint16(194), 586: uint16(240), 587: uint16(241), 588: uint16(314), 589: uint16(315), 590: uint16(116), 591: uint16(117), 592: uint16(118), 593: uint16(116), 594: uint16(117), 595: uint16(118), 596: uint16(253), 597: uint16(194), 598: uint16(255), 599: uint16(253), 600: uint16(59), 601: uint16(255), 602: uint16(19), 603: uint16(116), 604: uint16(117), 605: uint16(118), 606: uint16(23), 607: uint16(22), 608: uint16(217), 609: uint16(218), 610: uint16(142), 611: uint16(268), 612: uint16(205), 613: uint16(275), 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(194), 643: uint16(300), 644: uint16(59), 645: uint16(23), 646: uint16(119), 647: uint16(240), 648: uint16(241), 649: uint16(122), 650: uint16(123), 651: uint16(124), 652: uint16(314), 653: uint16(315), 654: uint16(194), 655: uint16(236), 656: uint16(237), 657: uint16(194), 658: uint16(117), 659: uint16(132), 660: uint16(253), 661: uint16(81), 662: uint16(255), 663: uint16(205), 664: uint16(59), 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(217), 681: uint16(218), 682: uint16(194), 683: uint16(194), 684: uint16(194), 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(294), 698: uint16(240), 699: uint16(241), 700: uint16(120), 701: uint16(116), 702: uint16(117), 703: uint16(118), 704: uint16(59), 705: uint16(194), 706: uint16(217), 707: uint16(218), 708: uint16(211), 709: uint16(212), 710: uint16(213), 711: uint16(253), 712: uint16(19), 713: uint16(255), 714: uint16(194), 715: uint16(19), 716: uint16(23), 717: uint16(254), 718: uint16(138), 719: uint16(139), 720: uint16(24), 721: uint16(232), 722: uint16(117), 723: uint16(194), 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(264), 753: uint16(108), 754: uint16(76), 755: uint16(23), 756: uint16(127), 757: uint16(128), 758: uint16(129), 759: uint16(311), 760: uint16(312), 761: uint16(116), 762: uint16(117), 763: uint16(118), 764: uint16(316), 765: uint16(87), 766: uint16(306), 767: uint16(89), 768: uint16(308), 769: uint16(194), 770: uint16(92), 771: uint16(22), 772: uint16(59), 773: uint16(194), 774: uint16(22), 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(194), 791: uint16(95), 792: uint16(217), 793: uint16(218), 794: uint16(265), 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(232), 808: uint16(59), 809: uint16(113), 810: uint16(25), 811: uint16(59), 812: uint16(194), 813: uint16(217), 814: uint16(218), 815: uint16(119), 816: uint16(120), 817: uint16(121), 818: uint16(122), 819: uint16(123), 820: uint16(124), 821: uint16(125), 822: uint16(19), 823: uint16(145), 824: uint16(194), 825: uint16(194), 826: uint16(23), 827: uint16(131), 828: uint16(232), 829: uint16(116), 830: uint16(117), 831: uint16(118), 832: uint16(35), 833: uint16(194), 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(194), 863: uint16(66), 864: uint16(194), 865: uint16(116), 866: uint16(117), 867: uint16(118), 868: uint16(116), 869: uint16(117), 870: uint16(118), 871: uint16(74), 872: uint16(242), 873: uint16(294), 874: uint16(194), 875: uint16(194), 876: uint16(206), 877: uint16(23), 878: uint16(194), 879: uint16(25), 880: uint16(194), 881: uint16(111), 882: uint16(112), 883: uint16(113), 884: uint16(25), 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(24), 901: uint16(194), 902: uint16(194), 903: uint16(217), 904: uint16(218), 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(241), 918: uint16(232), 919: uint16(194), 920: uint16(212), 921: uint16(213), 922: uint16(242), 923: uint16(242), 924: uint16(217), 925: uint16(218), 926: uint16(242), 927: uint16(130), 928: uint16(11), 929: uint16(253), 930: uint16(194), 931: uint16(255), 932: uint16(19), 933: uint16(265), 934: uint16(149), 935: uint16(59), 936: uint16(306), 937: uint16(194), 938: uint16(308), 939: uint16(232), 940: uint16(309), 941: uint16(310), 942: uint16(217), 943: uint16(218), 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(194), 972: uint16(194), 973: uint16(59), 974: uint16(194), 975: uint16(239), 976: uint16(19), 977: uint16(194), 978: uint16(25), 979: uint16(254), 980: uint16(303), 981: uint16(304), 982: uint16(23), 983: uint16(194), 984: uint16(25), 985: uint16(126), 986: uint16(306), 987: uint16(306), 988: uint16(308), 989: uint16(308), 990: uint16(306), 991: uint16(271), 992: uint16(308), 993: uint16(117), 994: uint16(286), 995: uint16(217), 996: uint16(218), 997: uint16(217), 998: uint16(218), 999: uint16(194), 1000: uint16(194), 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(59), 1028: uint16(239), 1029: uint16(194), 1030: uint16(116), 1031: uint16(117), 1032: uint16(118), 1033: uint16(260), 1034: uint16(254), 1035: uint16(194), 1036: uint16(240), 1037: uint16(241), 1038: uint16(194), 1039: uint16(233), 1040: uint16(205), 1041: uint16(240), 1042: uint16(241), 1043: uint16(205), 1044: uint16(239), 1045: uint16(128), 1046: uint16(129), 1047: uint16(270), 1048: uint16(265), 1049: uint16(253), 1050: uint16(194), 1051: uint16(255), 1052: uint16(217), 1053: uint16(218), 1054: uint16(253), 1055: uint16(194), 1056: uint16(255), 1057: uint16(143), 1058: uint16(280), 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(118), 1072: uint16(159), 1073: uint16(217), 1074: uint16(218), 1075: uint16(240), 1076: uint16(241), 1077: uint16(118), 1078: uint16(240), 1079: uint16(241), 1080: uint16(194), 1081: uint16(194), 1082: uint16(194), 1083: uint16(239), 1084: uint16(116), 1085: uint16(117), 1086: uint16(118), 1087: uint16(22), 1088: uint16(253), 1089: uint16(254), 1090: uint16(255), 1091: uint16(253), 1092: uint16(19), 1093: uint16(255), 1094: uint16(233), 1095: uint16(194), 1096: uint16(143), 1097: uint16(24), 1098: uint16(263), 1099: uint16(212), 1100: uint16(213), 1101: uint16(194), 1102: uint16(143), 1103: uint16(217), 1104: uint16(218), 1105: uint16(217), 1106: uint16(218), 1107: uint16(261), 1108: uint16(262), 1109: uint16(271), 1110: uint16(254), 1111: uint16(143), 1112: uint16(19), 1113: uint16(7), 1114: uint16(8), 1115: uint16(9), 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(16), 1132: uint16(19), 1133: uint16(22), 1134: uint16(23), 1135: uint16(294), 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(312), 1152: uint16(194), 1153: uint16(214), 1154: uint16(21), 1155: uint16(316), 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(106), 1172: uint16(107), 1173: uint16(286), 1174: uint16(194), 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(207), 1188: uint16(158), 1189: uint16(59), 1190: uint16(160), 1191: uint16(22), 1192: uint16(77), 1193: uint16(24), 1194: uint16(79), 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(194), 1208: uint16(194), 1209: uint16(229), 1210: uint16(194), 1211: uint16(231), 1212: uint16(101), 1213: uint16(80), 1214: uint16(22), 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(288), 1228: uint16(59), 1229: uint16(12), 1230: uint16(217), 1231: uint16(218), 1232: uint16(293), 1233: uint16(217), 1234: uint16(218), 1235: uint16(19), 1236: uint16(106), 1237: uint16(107), 1238: uint16(59), 1239: uint16(19), 1240: uint16(16), 1241: uint16(127), 1242: uint16(128), 1243: uint16(129), 1244: uint16(27), 1245: uint16(115), 1246: uint16(116), 1247: uint16(117), 1248: uint16(118), 1249: uint16(194), 1250: uint16(120), 1251: uint16(59), 1252: uint16(22), 1253: uint16(194), 1254: uint16(24), 1255: uint16(194), 1256: uint16(123), 1257: uint16(100), 1258: uint16(128), 1259: uint16(42), 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(117), 1275: uint16(194), 1276: uint16(217), 1277: uint16(218), 1278: uint16(121), 1279: uint16(100), 1280: uint16(63), 1281: uint16(194), 1282: uint16(245), 1283: uint16(153), 1284: uint16(194), 1285: uint16(155), 1286: uint16(117), 1287: uint16(19), 1288: uint16(115), 1289: uint16(194), 1290: uint16(73), 1291: uint16(214), 1292: uint16(194), 1293: uint16(256), 1294: uint16(161), 1295: uint16(116), 1296: uint16(117), 1297: uint16(194), 1298: uint16(217), 1299: uint16(218), 1300: uint16(121), 1301: uint16(77), 1302: uint16(194), 1303: uint16(79), 1304: uint16(217), 1305: uint16(218), 1306: uint16(194), 1307: uint16(217), 1308: uint16(218), 1309: uint16(117), 1310: uint16(153), 1311: uint16(154), 1312: uint16(155), 1313: uint16(254), 1314: uint16(46), 1315: uint16(217), 1316: uint16(218), 1317: uint16(144), 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(232), 1331: uint16(270), 1332: uint16(153), 1333: uint16(154), 1334: uint16(155), 1335: uint16(115), 1336: uint16(116), 1337: uint16(66), 1338: uint16(19), 1339: uint16(20), 1340: uint16(183), 1341: uint16(22), 1342: uint16(12), 1343: uint16(312), 1344: uint16(254), 1345: uint16(194), 1346: uint16(262), 1347: uint16(316), 1348: uint16(209), 1349: uint16(210), 1350: uint16(266), 1351: uint16(239), 1352: uint16(194), 1353: uint16(194), 1354: uint16(108), 1355: uint16(36), 1356: uint16(85), 1357: uint16(27), 1358: uint16(19), 1359: uint16(20), 1360: uint16(265), 1361: uint16(22), 1362: uint16(183), 1363: uint16(245), 1364: uint16(144), 1365: uint16(94), 1366: uint16(25), 1367: uint16(48), 1368: uint16(217), 1369: uint16(218), 1370: uint16(293), 1371: uint16(194), 1372: uint16(42), 1373: uint16(270), 1374: uint16(256), 1375: uint16(36), 1376: uint16(217), 1377: uint16(218), 1378: uint16(59), 1379: uint16(194), 1380: uint16(25), 1381: uint16(135), 1382: uint16(194), 1383: uint16(115), 1384: uint16(194), 1385: uint16(161), 1386: uint16(140), 1387: uint16(194), 1388: uint16(194), 1389: uint16(15), 1390: uint16(71), 1391: uint16(194), 1392: uint16(312), 1393: uint16(63), 1394: uint16(217), 1395: uint16(218), 1396: uint16(316), 1397: uint16(194), 1398: uint16(59), 1399: uint16(131), 1400: uint16(301), 1401: uint16(302), 1402: uint16(217), 1403: uint16(218), 1404: uint16(85), 1405: uint16(217), 1406: uint16(218), 1407: uint16(217), 1408: uint16(218), 1409: uint16(90), 1410: uint16(71), 1411: uint16(217), 1412: uint16(218), 1413: uint16(19), 1414: uint16(217), 1415: uint16(218), 1416: uint16(245), 1417: uint16(146), 1418: uint16(262), 1419: uint16(100), 1420: uint16(217), 1421: uint16(218), 1422: uint16(266), 1423: uint16(265), 1424: uint16(85), 1425: uint16(106), 1426: uint16(107), 1427: uint16(256), 1428: uint16(312), 1429: uint16(90), 1430: uint16(209), 1431: uint16(210), 1432: uint16(316), 1433: uint16(114), 1434: uint16(60), 1435: uint16(116), 1436: uint16(117), 1437: uint16(118), 1438: uint16(194), 1439: uint16(100), 1440: uint16(121), 1441: uint16(194), 1442: uint16(194), 1443: uint16(145), 1444: uint16(115), 1445: uint16(106), 1446: uint16(107), 1447: uint16(19), 1448: uint16(46), 1449: uint16(19), 1450: uint16(20), 1451: uint16(24), 1452: uint16(22), 1453: uint16(114), 1454: uint16(194), 1455: uint16(116), 1456: uint16(117), 1457: uint16(118), 1458: uint16(194), 1459: uint16(245), 1460: uint16(121), 1461: uint16(194), 1462: uint16(164), 1463: uint16(194), 1464: uint16(217), 1465: uint16(218), 1466: uint16(36), 1467: uint16(194), 1468: uint16(258), 1469: uint16(259), 1470: uint16(256), 1471: uint16(194), 1472: uint16(153), 1473: uint16(154), 1474: uint16(155), 1475: uint16(156), 1476: uint16(157), 1477: uint16(217), 1478: uint16(218), 1479: uint16(150), 1480: uint16(31), 1481: uint16(217), 1482: uint16(218), 1483: uint16(142), 1484: uint16(217), 1485: uint16(218), 1486: uint16(217), 1487: uint16(218), 1488: uint16(39), 1489: uint16(59), 1490: uint16(217), 1491: uint16(218), 1492: uint16(153), 1493: uint16(154), 1494: uint16(155), 1495: uint16(156), 1496: uint16(157), 1497: uint16(149), 1498: uint16(150), 1499: uint16(5), 1500: uint16(145), 1501: uint16(71), 1502: uint16(183), 1503: uint16(245), 1504: uint16(10), 1505: uint16(11), 1506: uint16(12), 1507: uint16(13), 1508: uint16(14), 1509: uint16(194), 1510: uint16(116), 1511: uint16(17), 1512: uint16(129), 1513: uint16(227), 1514: uint16(256), 1515: uint16(85), 1516: uint16(194), 1517: uint16(115), 1518: uint16(194), 1519: uint16(23), 1520: uint16(90), 1521: uint16(25), 1522: uint16(183), 1523: uint16(99), 1524: uint16(30), 1525: uint16(97), 1526: uint16(32), 1527: uint16(22), 1528: uint16(22), 1529: uint16(194), 1530: uint16(100), 1531: uint16(194), 1532: uint16(217), 1533: uint16(218), 1534: uint16(40), 1535: uint16(152), 1536: uint16(106), 1537: uint16(107), 1538: uint16(23), 1539: uint16(217), 1540: uint16(218), 1541: uint16(194), 1542: uint16(19), 1543: uint16(20), 1544: uint16(114), 1545: uint16(22), 1546: uint16(116), 1547: uint16(117), 1548: uint16(118), 1549: uint16(257), 1550: uint16(194), 1551: uint16(121), 1552: uint16(217), 1553: uint16(218), 1554: uint16(217), 1555: uint16(218), 1556: uint16(194), 1557: uint16(133), 1558: uint16(53), 1559: uint16(36), 1560: uint16(23), 1561: uint16(23), 1562: uint16(25), 1563: uint16(25), 1564: uint16(70), 1565: uint16(120), 1566: uint16(121), 1567: uint16(61), 1568: uint16(141), 1569: uint16(7), 1570: uint16(8), 1571: uint16(121), 1572: uint16(78), 1573: uint16(217), 1574: uint16(218), 1575: uint16(81), 1576: uint16(23), 1577: uint16(227), 1578: uint16(25), 1579: uint16(217), 1580: uint16(218), 1581: uint16(131), 1582: uint16(59), 1583: uint16(153), 1584: uint16(154), 1585: uint16(155), 1586: uint16(156), 1587: uint16(157), 1589: uint16(1), 1590: uint16(2), 1591: uint16(59), 1592: uint16(98), 1593: uint16(5), 1594: uint16(71), 1595: uint16(23), 1596: uint16(227), 1597: uint16(25), 1598: uint16(10), 1599: uint16(11), 1600: uint16(12), 1601: uint16(13), 1602: uint16(14), 1603: uint16(83), 1604: uint16(84), 1605: uint16(17), 1606: uint16(23), 1607: uint16(23), 1608: uint16(25), 1609: uint16(25), 1610: uint16(59), 1611: uint16(194), 1612: uint16(194), 1613: uint16(183), 1614: uint16(23), 1615: uint16(23), 1616: uint16(25), 1617: uint16(25), 1618: uint16(30), 1619: uint16(194), 1620: uint16(32), 1621: uint16(19), 1622: uint16(20), 1623: uint16(100), 1624: uint16(22), 1625: uint16(194), 1626: uint16(194), 1627: uint16(133), 1628: uint16(40), 1629: uint16(106), 1630: uint16(107), 1631: uint16(108), 1632: uint16(138), 1633: uint16(139), 1634: uint16(194), 1635: uint16(217), 1636: uint16(218), 1637: uint16(114), 1638: uint16(36), 1639: uint16(116), 1640: uint16(117), 1641: uint16(118), 1642: uint16(217), 1643: uint16(218), 1644: uint16(121), 1645: uint16(194), 1646: uint16(194), 1647: uint16(194), 1648: uint16(23), 1649: uint16(117), 1650: uint16(25), 1651: uint16(194), 1652: uint16(23), 1653: uint16(23), 1654: uint16(25), 1655: uint16(25), 1656: uint16(162), 1657: uint16(194), 1658: uint16(70), 1659: uint16(194), 1660: uint16(145), 1661: uint16(59), 1662: uint16(23), 1663: uint16(153), 1664: uint16(25), 1665: uint16(155), 1666: uint16(78), 1667: uint16(194), 1668: uint16(117), 1669: uint16(81), 1670: uint16(217), 1671: uint16(218), 1672: uint16(194), 1673: uint16(71), 1674: uint16(217), 1675: uint16(218), 1676: uint16(153), 1677: uint16(154), 1678: uint16(155), 1679: uint16(156), 1680: uint16(157), 1681: uint16(194), 1682: uint16(217), 1683: uint16(218), 1684: uint16(194), 1685: uint16(23), 1686: uint16(98), 1687: uint16(25), 1688: uint16(321), 1689: uint16(194), 1690: uint16(217), 1691: uint16(218), 1692: uint16(194), 1693: uint16(19), 1694: uint16(20), 1695: uint16(194), 1696: uint16(22), 1697: uint16(153), 1698: uint16(23), 1699: uint16(155), 1700: uint16(25), 1701: uint16(194), 1702: uint16(100), 1703: uint16(194), 1704: uint16(217), 1705: uint16(218), 1706: uint16(183), 1707: uint16(194), 1708: uint16(106), 1709: uint16(107), 1710: uint16(36), 1711: uint16(194), 1712: uint16(217), 1713: uint16(218), 1714: uint16(237), 1715: uint16(194), 1716: uint16(114), 1717: uint16(243), 1718: uint16(116), 1719: uint16(117), 1720: uint16(118), 1721: uint16(133), 1722: uint16(194), 1723: uint16(121), 1724: uint16(217), 1725: uint16(218), 1726: uint16(138), 1727: uint16(139), 1728: uint16(194), 1729: uint16(194), 1730: uint16(194), 1731: uint16(290), 1732: uint16(289), 1733: uint16(59), 1734: uint16(217), 1735: uint16(218), 1736: uint16(194), 1737: uint16(194), 1738: uint16(217), 1739: uint16(218), 1740: uint16(194), 1741: uint16(194), 1742: uint16(140), 1743: uint16(194), 1744: uint16(194), 1745: uint16(71), 1746: uint16(194), 1747: uint16(244), 1748: uint16(194), 1749: uint16(194), 1750: uint16(162), 1751: uint16(217), 1752: uint16(218), 1753: uint16(194), 1754: uint16(194), 1755: uint16(153), 1756: uint16(154), 1757: uint16(155), 1758: uint16(156), 1759: uint16(157), 1760: uint16(217), 1761: uint16(218), 1762: uint16(194), 1763: uint16(217), 1764: uint16(218), 1765: uint16(194), 1766: uint16(217), 1767: uint16(218), 1768: uint16(257), 1769: uint16(217), 1770: uint16(218), 1771: uint16(217), 1772: uint16(218), 1773: uint16(257), 1774: uint16(100), 1775: uint16(194), 1776: uint16(257), 1777: uint16(217), 1778: uint16(218), 1779: uint16(257), 1780: uint16(106), 1781: uint16(107), 1782: uint16(215), 1783: uint16(299), 1784: uint16(194), 1785: uint16(183), 1786: uint16(192), 1787: uint16(194), 1788: uint16(114), 1789: uint16(194), 1790: uint16(116), 1791: uint16(117), 1792: uint16(118), 1793: uint16(1), 1794: uint16(2), 1795: uint16(121), 1796: uint16(221), 1797: uint16(5), 1798: uint16(217), 1799: uint16(218), 1800: uint16(273), 1801: uint16(197), 1802: uint16(10), 1803: uint16(11), 1804: uint16(12), 1805: uint16(13), 1806: uint16(14), 1807: uint16(217), 1808: uint16(218), 1809: uint16(17), 1810: uint16(217), 1811: uint16(218), 1812: uint16(217), 1813: uint16(218), 1814: uint16(140), 1815: uint16(194), 1816: uint16(246), 1817: uint16(194), 1818: uint16(273), 1819: uint16(295), 1820: uint16(247), 1821: uint16(273), 1822: uint16(30), 1823: uint16(247), 1824: uint16(32), 1825: uint16(269), 1826: uint16(269), 1827: uint16(153), 1828: uint16(154), 1829: uint16(155), 1830: uint16(156), 1831: uint16(157), 1832: uint16(40), 1833: uint16(246), 1834: uint16(273), 1835: uint16(295), 1836: uint16(230), 1837: uint16(226), 1838: uint16(217), 1839: uint16(218), 1840: uint16(217), 1841: uint16(218), 1842: uint16(220), 1843: uint16(261), 1844: uint16(220), 1845: uint16(282), 1846: uint16(220), 1847: uint16(19), 1848: uint16(20), 1849: uint16(244), 1850: uint16(22), 1851: uint16(250), 1852: uint16(141), 1853: uint16(250), 1854: uint16(246), 1855: uint16(60), 1856: uint16(201), 1857: uint16(183), 1858: uint16(261), 1859: uint16(261), 1860: uint16(261), 1861: uint16(201), 1862: uint16(70), 1863: uint16(299), 1864: uint16(36), 1865: uint16(299), 1866: uint16(201), 1867: uint16(38), 1868: uint16(151), 1869: uint16(150), 1870: uint16(78), 1871: uint16(285), 1872: uint16(22), 1873: uint16(81), 1874: uint16(296), 1875: uint16(296), 1876: uint16(43), 1877: uint16(235), 1878: uint16(18), 1879: uint16(238), 1880: uint16(201), 1881: uint16(274), 1882: uint16(272), 1883: uint16(238), 1884: uint16(238), 1885: uint16(238), 1886: uint16(18), 1887: uint16(59), 1888: uint16(200), 1889: uint16(149), 1890: uint16(98), 1891: uint16(247), 1892: uint16(274), 1893: uint16(274), 1894: uint16(235), 1895: uint16(247), 1896: uint16(247), 1897: uint16(247), 1898: uint16(235), 1899: uint16(71), 1900: uint16(272), 1901: uint16(201), 1902: uint16(200), 1903: uint16(158), 1904: uint16(292), 1905: uint16(62), 1906: uint16(291), 1907: uint16(201), 1908: uint16(200), 1909: uint16(22), 1910: uint16(201), 1911: uint16(222), 1912: uint16(200), 1913: uint16(222), 1914: uint16(201), 1915: uint16(200), 1916: uint16(115), 1917: uint16(219), 1918: uint16(219), 1919: uint16(64), 1920: uint16(219), 1921: uint16(228), 1922: uint16(22), 1923: uint16(126), 1924: uint16(221), 1925: uint16(133), 1926: uint16(165), 1927: uint16(222), 1928: uint16(100), 1929: uint16(225), 1930: uint16(138), 1931: uint16(139), 1932: uint16(225), 1933: uint16(219), 1934: uint16(106), 1935: uint16(107), 1936: uint16(24), 1937: uint16(219), 1938: uint16(228), 1939: uint16(219), 1940: uint16(219), 1941: uint16(307), 1942: uint16(114), 1943: uint16(113), 1944: uint16(116), 1945: uint16(117), 1946: uint16(118), 1947: uint16(315), 1948: uint16(284), 1949: uint16(121), 1950: uint16(284), 1951: uint16(222), 1952: uint16(201), 1953: uint16(91), 1954: uint16(162), 1955: uint16(320), 1956: uint16(320), 1957: uint16(82), 1958: uint16(148), 1959: uint16(267), 1960: uint16(145), 1961: uint16(267), 1962: uint16(22), 1963: uint16(279), 1964: uint16(201), 1965: uint16(158), 1966: uint16(281), 1967: uint16(251), 1968: uint16(147), 1969: uint16(146), 1970: uint16(25), 1971: uint16(203), 1972: uint16(250), 1973: uint16(249), 1974: uint16(251), 1975: uint16(248), 1976: uint16(13), 1977: uint16(247), 1978: uint16(195), 1979: uint16(195), 1980: uint16(6), 1981: uint16(153), 1982: uint16(154), 1983: uint16(155), 1984: uint16(156), 1985: uint16(157), 1986: uint16(193), 1987: uint16(193), 1988: uint16(305), 1989: uint16(193), 1990: uint16(208), 1991: uint16(305), 1992: uint16(302), 1993: uint16(214), 1994: uint16(214), 1995: uint16(214), 1996: uint16(208), 1997: uint16(223), 1998: uint16(223), 1999: uint16(214), 2000: uint16(4), 2001: uint16(215), 2002: uint16(215), 2003: uint16(214), 2004: uint16(3), 2005: uint16(22), 2006: uint16(208), 2007: uint16(163), 2008: uint16(15), 2009: uint16(23), 2010: uint16(16), 2011: uint16(183), 2012: uint16(23), 2013: uint16(139), 2014: uint16(151), 2015: uint16(130), 2016: uint16(25), 2017: uint16(20), 2018: uint16(142), 2019: uint16(24), 2020: uint16(16), 2021: uint16(144), 2022: uint16(1), 2023: uint16(142), 2024: uint16(130), 2025: uint16(130), 2026: uint16(61), 2027: uint16(37), 2028: uint16(53), 2029: uint16(151), 2030: uint16(53), 2031: uint16(53), 2032: uint16(53), 2033: uint16(130), 2034: uint16(116), 2035: uint16(1), 2036: uint16(34), 2037: uint16(141), 2038: uint16(5), 2039: uint16(22), 2040: uint16(115), 2041: uint16(161), 2042: uint16(75), 2043: uint16(25), 2044: uint16(68), 2045: uint16(141), 2046: uint16(41), 2047: uint16(115), 2048: uint16(68), 2049: uint16(24), 2050: uint16(20), 2051: uint16(19), 2052: uint16(131), 2053: uint16(125), 2054: uint16(67), 2055: uint16(67), 2056: uint16(96), 2057: uint16(22), 2058: uint16(22), 2059: uint16(22), 2060: uint16(37), 2061: uint16(23), 2062: uint16(22), 2063: uint16(24), 2064: uint16(22), 2065: uint16(59), 2066: uint16(67), 2067: uint16(23), 2068: uint16(149), 2069: uint16(28), 2070: uint16(22), 2071: uint16(25), 2072: uint16(23), 2073: uint16(23), 2074: uint16(23), 2075: uint16(22), 2076: uint16(141), 2077: uint16(34), 2078: uint16(97), 2079: uint16(23), 2080: uint16(23), 2081: uint16(34), 2082: uint16(116), 2083: uint16(22), 2084: uint16(143), 2085: uint16(25), 2086: uint16(34), 2087: uint16(75), 2088: uint16(34), 2089: uint16(34), 2090: uint16(75), 2091: uint16(88), 2092: uint16(34), 2093: uint16(86), 2094: uint16(23), 2095: uint16(22), 2096: uint16(34), 2097: uint16(25), 2098: uint16(24), 2099: uint16(34), 2100: uint16(25), 2101: uint16(93), 2102: uint16(23), 2103: uint16(44), 2104: uint16(142), 2105: uint16(23), 2106: uint16(142), 2107: uint16(23), 2108: uint16(23), 2109: uint16(22), 2110: uint16(11), 2111: uint16(25), 2112: uint16(23), 2113: uint16(25), 2114: uint16(23), 2115: uint16(22), 2116: uint16(22), 2117: uint16(22), 2118: uint16(1), 2119: uint16(23), 2120: uint16(23), 2121: uint16(23), 2122: uint16(22), 2123: uint16(22), 2124: uint16(15), 2125: uint16(141), 2126: uint16(141), 2127: uint16(25), 2128: uint16(25), 2129: uint16(1), 2130: uint16(322), 2131: uint16(322), 2132: uint16(322), 2133: uint16(135), 2134: uint16(322), 2135: uint16(322), 2136: uint16(322), 2137: uint16(322), 2138: uint16(322), 2139: uint16(322), 2140: uint16(322), 2141: uint16(141), 2142: uint16(322), 2143: uint16(322), 2144: uint16(322), 2145: uint16(322), 2146: uint16(322), 2147: uint16(322), 2148: uint16(322), 2149: uint16(322), 2150: uint16(322), 2151: uint16(322), 2152: uint16(322), 2153: uint16(322), 2154: uint16(322), 2155: uint16(322), 2156: uint16(322), 2157: uint16(322), 2158: uint16(322), 2159: uint16(322), 2160: uint16(322), 2161: uint16(322), 2162: uint16(322), 2163: uint16(322), 2164: uint16(322), 2165: uint16(322), 2166: uint16(322), 2167: uint16(322), 2168: uint16(322), 2169: uint16(322), 2170: uint16(322), 2171: uint16(322), 2172: uint16(322), 2173: uint16(322), 2174: uint16(322), 2175: uint16(322), 2176: uint16(322), 2177: uint16(322), 2178: uint16(322), 2179: uint16(322), 2180: uint16(322), 2181: uint16(322), 2182: uint16(322), 2183: uint16(322), 2184: uint16(322), 2185: uint16(322), 2186: uint16(322), 2187: uint16(322), 2188: uint16(322), 2189: uint16(322), 2190: uint16(322), 2191: uint16(322), 2192: uint16(322), 2193: uint16(322), 2194: uint16(322), 2195: uint16(322), 2196: uint16(322), 2197: uint16(322), 2198: uint16(322), 2199: uint16(322), 2200: uint16(322), 2201: uint16(322), 2202: uint16(322), 2203: uint16(322), 2204: uint16(322), 2205: uint16(322), 2206: uint16(322), 2207: uint16(322), 2208: uint16(322), 2209: uint16(322), 2210: uint16(322), 2211: uint16(322), 2212: uint16(322), 2213: uint16(322), 2214: uint16(322), 2215: uint16(322), 2216: uint16(322), 2217: uint16(322), 2218: uint16(322), 2219: uint16(322), 2220: uint16(322), 2221: uint16(322), 2222: uint16(322), 2223: uint16(322), 2224: uint16(322), 2225: uint16(322), 2226: uint16(322), 2227: uint16(322), 2228: uint16(322), 2229: uint16(322), 2230: uint16(322), 2231: uint16(322), 2232: uint16(322), 2233: uint16(322), 2234: uint16(322), 2235: uint16(322), 2236: uint16(322), 2237: uint16(322), 2238: uint16(322), 2239: uint16(322), 2240: uint16(322), 2241: uint16(322), 2242: uint16(322), 2243: uint16(322), 2244: uint16(322), 2245: uint16(322), 2246: uint16(322), 2247: uint16(322), 2248: uint16(322), 2249: uint16(322), 2250: uint16(322), 2251: uint16(322), 2252: uint16(322), 2253: uint16(322), 2254: uint16(322), 2255: uint16(322), 2256: uint16(322), 2257: uint16(322), 2258: uint16(322), 2259: uint16(322), 2260: uint16(322), 2261: uint16(322), 2262: uint16(322), 2263: uint16(322), 2264: uint16(322), 2265: uint16(322), 2266: uint16(322), 2267: uint16(322), 2268: uint16(322), 2269: uint16(322), 2270: uint16(322), 2271: uint16(322), 2272: uint16(322), 2273: uint16(322), 2274: uint16(322), 2275: uint16(322), 2276: uint16(322), 2277: uint16(322), 2278: uint16(322), 2279: uint16(322), 2280: uint16(322), 2281: uint16(322), 2282: uint16(322), 2283: uint16(322), 2284: uint16(322), 2285: uint16(322), 2286: uint16(322), 2287: uint16(322), 2288: uint16(322), 2289: uint16(322), 2290: uint16(322), 2291: uint16(322), 2292: uint16(322), 2293: uint16(322), 2294: uint16(322), 2295: uint16(322), 2296: uint16(322), 2297: uint16(322), 2298: uint16(322), 2299: uint16(322), 2300: uint16(322), 2301: uint16(322), 2302: uint16(322), 2303: uint16(322), 2304: uint16(322), 2305: uint16(322), 2306: uint16(322), 2307: uint16(322), 2308: uint16(322), 2309: uint16(322), 2310: uint16(322), 2311: uint16(322), 2312: uint16(322), 2313: uint16(322), 2314: uint16(322), 2315: uint16(322), 2316: uint16(322), 2317: uint16(322), 2318: uint16(322), 2319: uint16(322), 2320: uint16(322), 2321: uint16(322), 2322: uint16(322), 2323: uint16(322), 2324: uint16(322), 2325: uint16(322), 2326: uint16(322), 2327: uint16(322), } var _yy_shift_ofst = [583]uint16{ 0: uint16(1792), 1: uint16(1588), 2: uint16(1494), 3: uint16(322), 4: uint16(322), 5: uint16(399), 6: uint16(306), 7: uint16(1319), 8: uint16(1339), 9: uint16(1430), 10: uint16(1828), 11: uint16(1828), 12: uint16(1828), 13: uint16(580), 14: uint16(399), 15: uint16(399), 16: uint16(399), 17: uint16(399), 18: uint16(399), 21: uint16(214), 22: uint16(1093), 23: uint16(1828), 24: uint16(1828), 25: uint16(1828), 26: uint16(1828), 27: uint16(1828), 28: uint16(1828), 29: uint16(1828), 30: uint16(1828), 31: uint16(1828), 32: uint16(1828), 33: uint16(1828), 34: uint16(1828), 35: uint16(1828), 36: uint16(1828), 37: uint16(1828), 38: uint16(1130), 39: uint16(1130), 40: uint16(365), 41: uint16(365), 42: uint16(55), 43: uint16(278), 44: uint16(436), 45: uint16(713), 46: uint16(713), 47: uint16(201), 48: uint16(201), 49: uint16(201), 50: uint16(201), 51: uint16(40), 52: uint16(111), 53: uint16(258), 54: uint16(361), 55: uint16(469), 56: uint16(512), 57: uint16(583), 58: uint16(622), 59: uint16(693), 60: uint16(732), 61: uint16(803), 62: uint16(842), 63: uint16(913), 64: uint16(1073), 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(1093), 75: uint16(1093), 76: uint16(1093), 77: uint16(1093), 78: uint16(1093), 79: uint16(1093), 80: uint16(1093), 81: uint16(1093), 82: uint16(1093), 83: uint16(1113), 84: uint16(1093), 85: uint16(1216), 86: uint16(957), 87: uint16(957), 88: uint16(1523), 89: uint16(1602), 90: uint16(1674), 91: uint16(1828), 92: uint16(1828), 93: uint16(1828), 94: uint16(1828), 95: uint16(1828), 96: uint16(1828), 97: uint16(1828), 98: uint16(1828), 99: uint16(1828), 100: uint16(1828), 101: uint16(1828), 102: uint16(1828), 103: uint16(1828), 104: uint16(1828), 105: uint16(1828), 106: uint16(1828), 107: uint16(1828), 108: uint16(1828), 109: uint16(1828), 110: uint16(1828), 111: uint16(1828), 112: uint16(1828), 113: uint16(1828), 114: uint16(1828), 115: uint16(1828), 116: uint16(1828), 117: uint16(1828), 118: uint16(1828), 119: uint16(1828), 120: uint16(1828), 121: uint16(1828), 122: uint16(1828), 123: uint16(1828), 124: uint16(1828), 125: uint16(1828), 126: uint16(1828), 127: uint16(1828), 128: uint16(1828), 129: uint16(1828), 130: uint16(1828), 131: uint16(1828), 132: uint16(1828), 133: uint16(1828), 134: uint16(1828), 135: uint16(1828), 136: uint16(1828), 137: uint16(1828), 138: uint16(1828), 139: uint16(1828), 140: uint16(137), 141: uint16(181), 142: uint16(181), 143: uint16(181), 144: uint16(181), 145: uint16(181), 146: uint16(181), 147: uint16(181), 148: uint16(96), 149: uint16(222), 150: uint16(143), 151: uint16(477), 152: uint16(713), 153: uint16(1133), 154: uint16(1268), 155: uint16(713), 156: uint16(713), 157: uint16(79), 158: uint16(79), 159: uint16(713), 160: uint16(770), 161: uint16(83), 162: uint16(65), 163: uint16(65), 164: uint16(65), 165: uint16(288), 166: uint16(162), 167: uint16(162), 168: uint16(2142), 169: uint16(2142), 170: uint16(696), 171: uint16(696), 172: uint16(696), 173: uint16(238), 174: uint16(474), 175: uint16(474), 176: uint16(474), 177: uint16(474), 178: uint16(1217), 179: uint16(1217), 180: uint16(678), 181: uint16(477), 182: uint16(324), 183: uint16(398), 184: uint16(713), 185: uint16(713), 186: uint16(713), 187: uint16(713), 188: uint16(713), 189: uint16(713), 190: uint16(713), 191: uint16(713), 192: uint16(713), 193: uint16(713), 194: uint16(713), 195: uint16(713), 196: uint16(713), 197: uint16(713), 198: uint16(713), 199: uint16(713), 200: uint16(713), 201: uint16(713), 202: uint16(713), 203: uint16(1220), 204: uint16(366), 205: uint16(366), 206: uint16(713), 207: uint16(917), 208: uint16(283), 209: uint16(283), 210: uint16(434), 211: uint16(434), 212: uint16(605), 213: uint16(605), 214: uint16(1298), 215: uint16(2142), 216: uint16(2142), 217: uint16(2142), 218: uint16(2142), 219: uint16(2142), 220: uint16(2142), 221: uint16(2142), 222: uint16(1179), 223: uint16(1157), 224: uint16(1157), 225: uint16(487), 226: uint16(527), 227: uint16(585), 228: uint16(645), 229: uint16(749), 230: uint16(914), 231: uint16(968), 232: uint16(752), 233: uint16(713), 234: uint16(713), 235: uint16(713), 236: uint16(713), 237: uint16(713), 238: uint16(713), 239: uint16(713), 240: uint16(713), 241: uint16(713), 242: uint16(713), 243: uint16(303), 244: uint16(713), 245: uint16(713), 246: uint16(713), 247: uint16(713), 248: uint16(713), 249: uint16(713), 250: uint16(713), 251: uint16(713), 252: uint16(713), 253: uint16(713), 254: uint16(713), 255: uint16(713), 256: uint16(797), 257: uint16(797), 258: uint16(797), 259: uint16(713), 260: uint16(713), 261: uint16(713), 262: uint16(959), 263: uint16(713), 264: uint16(713), 265: uint16(713), 266: uint16(1169), 267: uint16(1271), 268: uint16(713), 269: uint16(713), 270: uint16(1330), 271: uint16(713), 272: uint16(713), 273: uint16(713), 274: uint16(713), 275: uint16(713), 276: uint16(713), 277: uint16(713), 278: uint16(713), 279: uint16(629), 280: uint16(7), 281: uint16(91), 282: uint16(876), 283: uint16(876), 284: uint16(876), 285: uint16(876), 286: uint16(953), 287: uint16(91), 288: uint16(91), 289: uint16(1246), 290: uint16(1065), 291: uint16(1106), 292: uint16(1374), 293: uint16(1329), 294: uint16(1348), 295: uint16(468), 296: uint16(1348), 297: uint16(1394), 298: uint16(785), 299: uint16(1329), 300: uint16(1329), 301: uint16(785), 302: uint16(1329), 303: uint16(468), 304: uint16(1394), 305: uint16(859), 306: uint16(854), 307: uint16(1402), 308: uint16(1449), 309: uint16(1449), 310: uint16(1449), 311: uint16(1173), 312: uint16(1173), 313: uint16(1173), 314: uint16(1173), 315: uint16(1355), 316: uint16(1355), 317: uint16(1030), 318: uint16(1341), 319: uint16(405), 320: uint16(1230), 321: uint16(1795), 322: uint16(1795), 323: uint16(1711), 324: uint16(1711), 325: uint16(1829), 326: uint16(1829), 327: uint16(1711), 328: uint16(1717), 329: uint16(1719), 330: uint16(1850), 331: uint16(1833), 332: uint16(1860), 333: uint16(1860), 334: uint16(1860), 335: uint16(1860), 336: uint16(1711), 337: uint16(1868), 338: uint16(1740), 339: uint16(1719), 340: uint16(1719), 341: uint16(1740), 342: uint16(1850), 343: uint16(1833), 344: uint16(1740), 345: uint16(1833), 346: uint16(1740), 347: uint16(1711), 348: uint16(1868), 349: uint16(1745), 350: uint16(1843), 351: uint16(1711), 352: uint16(1868), 353: uint16(1887), 354: uint16(1711), 355: uint16(1868), 356: uint16(1711), 357: uint16(1868), 358: uint16(1887), 359: uint16(1801), 360: uint16(1801), 361: uint16(1801), 362: uint16(1855), 363: uint16(1900), 364: uint16(1900), 365: uint16(1887), 366: uint16(1801), 367: uint16(1797), 368: uint16(1801), 369: uint16(1855), 370: uint16(1801), 371: uint16(1801), 372: uint16(1761), 373: uint16(1912), 374: uint16(1830), 375: uint16(1830), 376: uint16(1887), 377: uint16(1711), 378: uint16(1862), 379: uint16(1862), 380: uint16(1875), 381: uint16(1875), 382: uint16(1810), 383: uint16(1815), 384: uint16(1940), 385: uint16(1711), 386: uint16(1807), 387: uint16(1810), 388: uint16(1821), 389: uint16(1823), 390: uint16(1740), 391: uint16(1945), 392: uint16(1963), 393: uint16(1963), 394: uint16(1974), 395: uint16(1974), 396: uint16(1974), 397: uint16(2142), 398: uint16(2142), 399: uint16(2142), 400: uint16(2142), 401: uint16(2142), 402: uint16(2142), 403: uint16(2142), 404: uint16(2142), 405: uint16(2142), 406: uint16(2142), 407: uint16(2142), 408: uint16(2142), 409: uint16(2142), 410: uint16(2142), 411: uint16(2142), 412: uint16(20), 413: uint16(1224), 414: uint16(256), 415: uint16(1111), 416: uint16(1115), 417: uint16(1114), 418: uint16(1192), 419: uint16(1496), 420: uint16(1424), 421: uint16(1505), 422: uint16(1427), 423: uint16(355), 424: uint16(1383), 425: uint16(1537), 426: uint16(1506), 427: uint16(1538), 428: uint16(1553), 429: uint16(1583), 430: uint16(1584), 431: uint16(1591), 432: uint16(1625), 433: uint16(541), 434: uint16(1445), 435: uint16(1562), 436: uint16(1450), 437: uint16(1572), 438: uint16(1515), 439: uint16(1428), 440: uint16(1532), 441: uint16(1592), 442: uint16(1629), 443: uint16(1520), 444: uint16(1630), 445: uint16(1639), 446: uint16(1510), 447: uint16(1544), 448: uint16(1662), 449: uint16(1675), 450: uint16(1551), 451: uint16(48), 452: uint16(1996), 453: uint16(2001), 454: uint16(1983), 455: uint16(1844), 456: uint16(1993), 457: uint16(1994), 458: uint16(1986), 459: uint16(1989), 460: uint16(1874), 461: uint16(1863), 462: uint16(1885), 463: uint16(1991), 464: uint16(1991), 465: uint16(1995), 466: uint16(1876), 467: uint16(1997), 468: uint16(1877), 469: uint16(2004), 470: uint16(2021), 471: uint16(1881), 472: uint16(1894), 473: uint16(1991), 474: uint16(1895), 475: uint16(1965), 476: uint16(1990), 477: uint16(1991), 478: uint16(1878), 479: uint16(1975), 480: uint16(1977), 481: uint16(1978), 482: uint16(1979), 483: uint16(1903), 484: uint16(1918), 485: uint16(2002), 486: uint16(1896), 487: uint16(2034), 488: uint16(2033), 489: uint16(2017), 490: uint16(1925), 491: uint16(1880), 492: uint16(1976), 493: uint16(2018), 494: uint16(1980), 495: uint16(1967), 496: uint16(2005), 497: uint16(1904), 498: uint16(1932), 499: uint16(2025), 500: uint16(2030), 501: uint16(2032), 502: uint16(1921), 503: uint16(1928), 504: uint16(2035), 505: uint16(1987), 506: uint16(2036), 507: uint16(2037), 508: uint16(2038), 509: uint16(2040), 510: uint16(1988), 511: uint16(2006), 512: uint16(2039), 513: uint16(1960), 514: uint16(2041), 515: uint16(2042), 516: uint16(1999), 517: uint16(2023), 518: uint16(2044), 519: uint16(2043), 520: uint16(1919), 521: uint16(2048), 522: uint16(2049), 523: uint16(2050), 524: uint16(2046), 525: uint16(2051), 526: uint16(2053), 527: uint16(1981), 528: uint16(1935), 529: uint16(2056), 530: uint16(2057), 531: uint16(1966), 532: uint16(2047), 533: uint16(2061), 534: uint16(1941), 535: uint16(2060), 536: uint16(2052), 537: uint16(2054), 538: uint16(2055), 539: uint16(2058), 540: uint16(2003), 541: uint16(2012), 542: uint16(2007), 543: uint16(2059), 544: uint16(2015), 545: uint16(2008), 546: uint16(2062), 547: uint16(2071), 548: uint16(2073), 549: uint16(2074), 550: uint16(2072), 551: uint16(2075), 552: uint16(2065), 553: uint16(1962), 554: uint16(1964), 555: uint16(2079), 556: uint16(2060), 557: uint16(2082), 558: uint16(2084), 559: uint16(2085), 560: uint16(2087), 561: uint16(2086), 562: uint16(2089), 563: uint16(2088), 564: uint16(2091), 565: uint16(2093), 566: uint16(2099), 567: uint16(2094), 568: uint16(2095), 569: uint16(2096), 570: uint16(2097), 571: uint16(2100), 572: uint16(2101), 573: uint16(2102), 574: uint16(1998), 575: uint16(1984), 576: uint16(1985), 577: uint16(2000), 578: uint16(2103), 579: uint16(2098), 580: uint16(2109), 581: uint16(2117), 582: uint16(2128), } var _yy_reduce_ofst = [412]int16{ 0: int16(-int32(71)), 1: int16(194), 2: int16(343), 3: int16(835), 4: int16(-int32(180)), 5: int16(-int32(177)), 6: int16(838), 7: int16(-int32(194)), 8: int16(-int32(188)), 9: int16(-int32(185)), 10: int16(-int32(183)), 11: int16(82), 12: int16(183), 13: int16(-int32(65)), 14: int16(133), 15: int16(245), 16: int16(346), 17: int16(407), 18: int16(458), 19: int16(-int32(178)), 20: int16(75), 21: int16(-int32(275)), 22: int16(-int32(4)), 23: int16(310), 24: int16(312), 25: int16(489), 26: int16(575), 27: int16(596), 28: int16(463), 29: int16(686), 30: int16(707), 31: int16(725), 32: int16(780), 33: int16(1098), 34: int16(856), 35: int16(778), 36: int16(1059), 37: int16(1090), 38: int16(708), 39: int16(887), 40: int16(86), 41: int16(448), 42: int16(980), 43: int16(630), 44: int16(680), 45: int16(681), 46: int16(684), 47: int16(796), 48: int16(801), 49: int16(796), 50: int16(801), 51: int16(-int32(261)), 52: int16(-int32(261)), 53: int16(-int32(261)), 54: int16(-int32(261)), 55: int16(-int32(261)), 56: int16(-int32(261)), 57: int16(-int32(261)), 58: int16(-int32(261)), 59: int16(-int32(261)), 60: int16(-int32(261)), 61: int16(-int32(261)), 62: int16(-int32(261)), 63: int16(-int32(261)), 64: int16(-int32(261)), 65: int16(-int32(261)), 66: int16(-int32(261)), 67: int16(-int32(261)), 68: int16(-int32(261)), 69: int16(-int32(261)), 70: int16(-int32(261)), 71: int16(-int32(261)), 72: int16(-int32(261)), 73: int16(-int32(261)), 74: int16(-int32(261)), 75: int16(-int32(261)), 76: int16(-int32(261)), 77: int16(-int32(261)), 78: int16(-int32(261)), 79: int16(-int32(261)), 80: int16(-int32(261)), 81: int16(-int32(261)), 82: int16(-int32(261)), 83: int16(-int32(261)), 84: int16(-int32(261)), 85: int16(-int32(261)), 86: int16(-int32(261)), 87: int16(-int32(261)), 88: int16(391), 89: int16(886), 90: int16(888), 91: int16(1013), 92: int16(1016), 93: int16(1081), 94: int16(1087), 95: int16(1151), 96: int16(1159), 97: int16(1177), 98: int16(1185), 99: int16(1188), 100: int16(1190), 101: int16(1194), 102: int16(1197), 103: int16(1203), 104: int16(1247), 105: int16(1260), 106: int16(1264), 107: int16(1267), 108: int16(1269), 109: int16(1273), 110: int16(1315), 111: int16(1322), 112: int16(1335), 113: int16(1337), 114: int16(1356), 115: int16(1362), 116: int16(1418), 117: int16(1425), 118: int16(1453), 119: int16(1457), 120: int16(1465), 121: int16(1473), 122: int16(1487), 123: int16(1495), 124: int16(1507), 125: int16(1517), 126: int16(1521), 127: int16(1534), 128: int16(1543), 129: int16(1546), 130: int16(1549), 131: int16(1552), 132: int16(1554), 133: int16(1560), 134: int16(1581), 135: int16(1590), 136: int16(1593), 137: int16(1595), 138: int16(1621), 139: int16(1623), 140: int16(-int32(261)), 141: int16(-int32(261)), 142: int16(-int32(261)), 143: int16(-int32(261)), 144: int16(-int32(261)), 145: int16(-int32(261)), 146: int16(-int32(261)), 147: int16(-int32(261)), 148: int16(-int32(261)), 149: int16(-int32(261)), 150: int16(-int32(261)), 151: int16(-int32(186)), 152: int16(-int32(117)), 153: int16(260), 154: int16(263), 155: int16(460), 156: int16(631), 157: int16(-int32(74)), 158: int16(497), 159: int16(-int32(181)), 160: int16(-int32(261)), 161: int16(939), 162: int16(176), 163: int16(274), 164: int16(338), 165: int16(676), 166: int16(-int32(261)), 167: int16(-int32(261)), 168: int16(-int32(261)), 169: int16(-int32(261)), 170: int16(-int32(212)), 171: int16(-int32(212)), 172: int16(-int32(212)), 173: int16(-int32(184)), 174: int16(149), 175: int16(777), 176: int16(1061), 177: int16(1103), 178: int16(265), 179: int16(419), 180: int16(-int32(254)), 181: int16(670), 182: int16(677), 183: int16(677), 184: int16(-int32(11)), 185: int16(-int32(129)), 186: int16(184), 187: int16(488), 188: int16(736), 189: int16(789), 190: int16(805), 191: int16(844), 192: int16(403), 193: int16(529), 194: int16(579), 195: int16(668), 196: int16(783), 197: int16(841), 198: int16(1158), 199: int16(1112), 200: int16(806), 201: int16(861), 202: int16(1095), 203: int16(846), 204: int16(839), 205: int16(1031), 206: int16(-int32(189)), 207: int16(1077), 208: int16(1080), 209: int16(1116), 210: int16(1084), 211: int16(1156), 212: int16(1139), 213: int16(1221), 214: int16(46), 215: int16(1099), 216: int16(1037), 217: int16(1118), 218: int16(1171), 219: int16(1214), 220: int16(1210), 221: int16(1258), 222: int16(-int32(210)), 223: int16(-int32(190)), 224: int16(-int32(176)), 225: int16(-int32(115)), 226: int16(117), 227: int16(262), 228: int16(376), 229: int16(490), 230: int16(511), 231: int16(520), 232: int16(618), 233: int16(639), 234: int16(743), 235: int16(901), 236: int16(907), 237: int16(958), 238: int16(1014), 239: int16(1055), 240: int16(1108), 241: int16(1193), 242: int16(1244), 243: int16(720), 244: int16(1248), 245: int16(1277), 246: int16(1324), 247: int16(1347), 248: int16(1417), 249: int16(1431), 250: int16(1432), 251: int16(1440), 252: int16(1451), 253: int16(1452), 254: int16(1463), 255: int16(1478), 256: int16(1286), 257: int16(1350), 258: int16(1369), 259: int16(1490), 260: int16(1498), 261: int16(1501), 262: int16(773), 263: int16(1509), 264: int16(1513), 265: int16(1528), 266: int16(1292), 267: int16(1367), 268: int16(1535), 269: int16(1536), 270: int16(1477), 271: int16(1542), 272: int16(376), 273: int16(1547), 274: int16(1550), 275: int16(1555), 276: int16(1559), 277: int16(1568), 278: int16(1571), 279: int16(1441), 280: int16(1443), 281: int16(1474), 282: int16(1511), 283: int16(1516), 284: int16(1519), 285: int16(1522), 286: int16(773), 287: int16(1474), 288: int16(1474), 289: int16(1503), 290: int16(1567), 291: int16(1594), 292: int16(1484), 293: int16(1527), 294: int16(1556), 295: int16(1570), 296: int16(1557), 297: int16(1524), 298: int16(1573), 299: int16(1545), 300: int16(1548), 301: int16(1576), 302: int16(1561), 303: int16(1587), 304: int16(1540), 305: int16(1575), 306: int16(1606), 307: int16(1611), 308: int16(1622), 309: int16(1624), 310: int16(1626), 311: int16(1582), 312: int16(1597), 313: int16(1598), 314: int16(1599), 315: int16(1601), 316: int16(1603), 317: int16(1563), 318: int16(1608), 319: int16(1605), 320: int16(1604), 321: int16(1564), 322: int16(1566), 323: int16(1655), 324: int16(1660), 325: int16(1578), 326: int16(1579), 327: int16(1665), 328: int16(1586), 329: int16(1607), 330: int16(1610), 331: int16(1642), 332: int16(1641), 333: int16(1645), 334: int16(1646), 335: int16(1647), 336: int16(1679), 337: int16(1688), 338: int16(1644), 339: int16(1618), 340: int16(1619), 341: int16(1648), 342: int16(1628), 343: int16(1659), 344: int16(1649), 345: int16(1663), 346: int16(1650), 347: int16(1700), 348: int16(1702), 349: int16(1612), 350: int16(1615), 351: int16(1706), 352: int16(1708), 353: int16(1689), 354: int16(1709), 355: int16(1712), 356: int16(1713), 357: int16(1715), 358: int16(1691), 359: int16(1698), 360: int16(1699), 361: int16(1701), 362: int16(1693), 363: int16(1704), 364: int16(1707), 365: int16(1705), 366: int16(1714), 367: int16(1703), 368: int16(1718), 369: int16(1710), 370: int16(1720), 371: int16(1721), 372: int16(1632), 373: int16(1634), 374: int16(1664), 375: int16(1666), 376: int16(1729), 377: int16(1751), 378: int16(1635), 379: int16(1636), 380: int16(1692), 381: int16(1694), 382: int16(1716), 383: int16(1722), 384: int16(1684), 385: int16(1763), 386: int16(1685), 387: int16(1723), 388: int16(1724), 389: int16(1727), 390: int16(1730), 391: int16(1768), 392: int16(1783), 393: int16(1784), 394: int16(1793), 395: int16(1794), 396: int16(1796), 397: int16(1683), 398: int16(1686), 399: int16(1690), 400: int16(1782), 401: int16(1779), 402: int16(1780), 403: int16(1781), 404: int16(1785), 405: int16(1788), 406: int16(1774), 407: int16(1775), 408: int16(1786), 409: int16(1787), 410: int16(1789), 411: int16(1798), } var _yy_default = [583]uint16{ 0: uint16(1663), 1: uint16(1663), 2: uint16(1663), 3: uint16(1491), 4: uint16(1254), 5: uint16(1367), 6: uint16(1254), 7: uint16(1254), 8: uint16(1254), 9: uint16(1254), 10: uint16(1491), 11: uint16(1491), 12: uint16(1491), 13: uint16(1254), 14: uint16(1254), 15: uint16(1254), 16: uint16(1254), 17: uint16(1254), 18: uint16(1254), 19: uint16(1397), 20: uint16(1397), 21: uint16(1544), 22: uint16(1287), 23: uint16(1254), 24: uint16(1254), 25: uint16(1254), 26: uint16(1254), 27: uint16(1254), 28: uint16(1254), 29: uint16(1254), 30: uint16(1254), 31: uint16(1254), 32: uint16(1254), 33: uint16(1254), 34: uint16(1254), 35: uint16(1490), 36: uint16(1254), 37: uint16(1254), 38: uint16(1254), 39: uint16(1254), 40: uint16(1578), 41: uint16(1578), 42: uint16(1254), 43: uint16(1254), 44: uint16(1254), 45: uint16(1254), 46: uint16(1254), 47: uint16(1563), 48: uint16(1562), 49: uint16(1254), 50: uint16(1254), 51: uint16(1254), 52: uint16(1406), 53: uint16(1254), 54: uint16(1413), 55: uint16(1254), 56: uint16(1254), 57: uint16(1254), 58: uint16(1254), 59: uint16(1254), 60: uint16(1492), 61: uint16(1493), 62: uint16(1254), 63: uint16(1254), 64: uint16(1254), 65: uint16(1543), 66: uint16(1545), 67: uint16(1508), 68: uint16(1420), 69: uint16(1419), 70: uint16(1418), 71: uint16(1417), 72: uint16(1526), 73: uint16(1385), 74: uint16(1411), 75: uint16(1404), 76: uint16(1408), 77: uint16(1487), 78: uint16(1488), 79: uint16(1486), 80: uint16(1641), 81: uint16(1493), 82: uint16(1492), 83: uint16(1254), 84: uint16(1407), 85: uint16(1455), 86: uint16(1471), 87: uint16(1454), 88: uint16(1254), 89: uint16(1254), 90: uint16(1254), 91: uint16(1254), 92: uint16(1254), 93: uint16(1254), 94: uint16(1254), 95: uint16(1254), 96: uint16(1254), 97: uint16(1254), 98: uint16(1254), 99: uint16(1254), 100: uint16(1254), 101: uint16(1254), 102: uint16(1254), 103: uint16(1254), 104: uint16(1254), 105: uint16(1254), 106: uint16(1254), 107: uint16(1254), 108: uint16(1254), 109: uint16(1254), 110: uint16(1254), 111: uint16(1254), 112: uint16(1254), 113: uint16(1254), 114: uint16(1254), 115: uint16(1254), 116: uint16(1254), 117: uint16(1254), 118: uint16(1254), 119: uint16(1254), 120: uint16(1254), 121: uint16(1254), 122: uint16(1254), 123: uint16(1254), 124: uint16(1254), 125: uint16(1254), 126: uint16(1254), 127: uint16(1254), 128: uint16(1254), 129: uint16(1254), 130: uint16(1254), 131: uint16(1254), 132: uint16(1254), 133: uint16(1254), 134: uint16(1254), 135: uint16(1254), 136: uint16(1254), 137: uint16(1254), 138: uint16(1254), 139: uint16(1254), 140: uint16(1463), 141: uint16(1470), 142: uint16(1469), 143: uint16(1468), 144: uint16(1477), 145: uint16(1467), 146: uint16(1464), 147: uint16(1457), 148: uint16(1456), 149: uint16(1458), 150: uint16(1459), 151: uint16(1278), 152: uint16(1254), 153: uint16(1275), 154: uint16(1329), 155: uint16(1254), 156: uint16(1254), 157: uint16(1254), 158: uint16(1254), 159: uint16(1254), 160: uint16(1460), 161: uint16(1287), 162: uint16(1448), 163: uint16(1447), 164: uint16(1446), 165: uint16(1254), 166: uint16(1474), 167: uint16(1461), 168: uint16(1473), 169: uint16(1472), 170: uint16(1551), 171: uint16(1615), 172: uint16(1614), 173: uint16(1509), 174: uint16(1254), 175: uint16(1254), 176: uint16(1254), 177: uint16(1254), 178: uint16(1254), 179: uint16(1254), 180: uint16(1578), 181: uint16(1254), 182: uint16(1254), 183: uint16(1254), 184: uint16(1254), 185: uint16(1254), 186: uint16(1254), 187: uint16(1254), 188: uint16(1254), 189: uint16(1254), 190: uint16(1254), 191: uint16(1254), 192: uint16(1254), 193: uint16(1254), 194: uint16(1254), 195: uint16(1254), 196: uint16(1254), 197: uint16(1254), 198: uint16(1254), 199: uint16(1254), 200: uint16(1254), 201: uint16(1254), 202: uint16(1254), 203: uint16(1387), 204: uint16(1578), 205: uint16(1578), 206: uint16(1254), 207: uint16(1287), 208: uint16(1578), 209: uint16(1578), 210: uint16(1388), 211: uint16(1388), 212: uint16(1283), 213: uint16(1283), 214: uint16(1391), 215: uint16(1558), 216: uint16(1358), 217: uint16(1358), 218: uint16(1358), 219: uint16(1358), 220: uint16(1367), 221: uint16(1358), 222: uint16(1254), 223: uint16(1254), 224: uint16(1254), 225: uint16(1254), 226: uint16(1254), 227: uint16(1254), 228: uint16(1254), 229: uint16(1254), 230: uint16(1254), 231: uint16(1254), 232: uint16(1254), 233: uint16(1254), 234: uint16(1254), 235: uint16(1254), 236: uint16(1254), 237: uint16(1548), 238: uint16(1546), 239: uint16(1254), 240: uint16(1254), 241: uint16(1254), 242: uint16(1254), 243: uint16(1254), 244: uint16(1254), 245: uint16(1254), 246: uint16(1254), 247: uint16(1254), 248: uint16(1254), 249: uint16(1254), 250: uint16(1254), 251: uint16(1254), 252: uint16(1254), 253: uint16(1254), 254: uint16(1254), 255: uint16(1254), 256: uint16(1254), 257: uint16(1254), 258: uint16(1254), 259: uint16(1254), 260: uint16(1254), 261: uint16(1254), 262: uint16(1254), 263: uint16(1254), 264: uint16(1254), 265: uint16(1254), 266: uint16(1363), 267: uint16(1254), 268: uint16(1254), 269: uint16(1254), 270: uint16(1254), 271: uint16(1254), 272: uint16(1254), 273: uint16(1254), 274: uint16(1254), 275: uint16(1254), 276: uint16(1254), 277: uint16(1254), 278: uint16(1608), 279: uint16(1254), 280: uint16(1521), 281: uint16(1343), 282: uint16(1363), 283: uint16(1363), 284: uint16(1363), 285: uint16(1363), 286: uint16(1365), 287: uint16(1344), 288: uint16(1342), 289: uint16(1357), 290: uint16(1288), 291: uint16(1261), 292: uint16(1655), 293: uint16(1423), 294: uint16(1412), 295: uint16(1364), 296: uint16(1412), 297: uint16(1652), 298: uint16(1410), 299: uint16(1423), 300: uint16(1423), 301: uint16(1410), 302: uint16(1423), 303: uint16(1364), 304: uint16(1652), 305: uint16(1304), 306: uint16(1630), 307: uint16(1299), 308: uint16(1397), 309: uint16(1397), 310: uint16(1397), 311: uint16(1387), 312: uint16(1387), 313: uint16(1387), 314: uint16(1387), 315: uint16(1391), 316: uint16(1391), 317: uint16(1489), 318: uint16(1364), 319: uint16(1357), 320: uint16(1254), 321: uint16(1655), 322: uint16(1655), 323: uint16(1373), 324: uint16(1373), 325: uint16(1654), 326: uint16(1654), 327: uint16(1373), 328: uint16(1509), 329: uint16(1638), 330: uint16(1432), 331: uint16(1332), 332: uint16(1338), 333: uint16(1338), 334: uint16(1338), 335: uint16(1338), 336: uint16(1373), 337: uint16(1272), 338: uint16(1410), 339: uint16(1638), 340: uint16(1638), 341: uint16(1410), 342: uint16(1432), 343: uint16(1332), 344: uint16(1410), 345: uint16(1332), 346: uint16(1410), 347: uint16(1373), 348: uint16(1272), 349: uint16(1525), 350: uint16(1649), 351: uint16(1373), 352: uint16(1272), 353: uint16(1499), 354: uint16(1373), 355: uint16(1272), 356: uint16(1373), 357: uint16(1272), 358: uint16(1499), 359: uint16(1330), 360: uint16(1330), 361: uint16(1330), 362: uint16(1319), 363: uint16(1254), 364: uint16(1254), 365: uint16(1499), 366: uint16(1330), 367: uint16(1304), 368: uint16(1330), 369: uint16(1319), 370: uint16(1330), 371: uint16(1330), 372: uint16(1596), 373: uint16(1254), 374: uint16(1503), 375: uint16(1503), 376: uint16(1499), 377: uint16(1373), 378: uint16(1588), 379: uint16(1588), 380: uint16(1400), 381: uint16(1400), 382: uint16(1405), 383: uint16(1391), 384: uint16(1494), 385: uint16(1373), 386: uint16(1254), 387: uint16(1405), 388: uint16(1403), 389: uint16(1401), 390: uint16(1410), 391: uint16(1322), 392: uint16(1611), 393: uint16(1611), 394: uint16(1607), 395: uint16(1607), 396: uint16(1607), 397: uint16(1660), 398: uint16(1660), 399: uint16(1558), 400: uint16(1623), 401: uint16(1287), 402: uint16(1287), 403: uint16(1287), 404: uint16(1287), 405: uint16(1623), 406: uint16(1306), 407: uint16(1306), 408: uint16(1288), 409: uint16(1288), 410: uint16(1287), 411: uint16(1623), 412: uint16(1254), 413: uint16(1254), 414: uint16(1254), 415: uint16(1254), 416: uint16(1254), 417: uint16(1254), 418: uint16(1618), 419: uint16(1254), 420: uint16(1553), 421: uint16(1510), 422: uint16(1377), 423: uint16(1254), 424: uint16(1254), 425: uint16(1254), 426: uint16(1254), 427: uint16(1254), 428: uint16(1254), 429: uint16(1254), 430: uint16(1254), 431: uint16(1254), 432: uint16(1254), 433: uint16(1254), 434: uint16(1254), 435: uint16(1254), 436: uint16(1254), 437: uint16(1254), 438: uint16(1254), 439: uint16(1564), 440: uint16(1254), 441: uint16(1254), 442: uint16(1254), 443: uint16(1254), 444: uint16(1254), 445: uint16(1254), 446: uint16(1254), 447: uint16(1254), 448: uint16(1254), 449: uint16(1254), 450: uint16(1254), 451: uint16(1437), 452: uint16(1254), 453: uint16(1257), 454: uint16(1555), 455: uint16(1254), 456: uint16(1254), 457: uint16(1254), 458: uint16(1254), 459: uint16(1254), 460: uint16(1254), 461: uint16(1254), 462: uint16(1254), 463: uint16(1414), 464: uint16(1415), 465: uint16(1378), 466: uint16(1254), 467: uint16(1254), 468: uint16(1254), 469: uint16(1254), 470: uint16(1254), 471: uint16(1254), 472: uint16(1254), 473: uint16(1429), 474: uint16(1254), 475: uint16(1254), 476: uint16(1254), 477: uint16(1424), 478: uint16(1254), 479: uint16(1254), 480: uint16(1254), 481: uint16(1254), 482: uint16(1254), 483: uint16(1254), 484: uint16(1254), 485: uint16(1254), 486: uint16(1651), 487: uint16(1254), 488: uint16(1254), 489: uint16(1254), 490: uint16(1254), 491: uint16(1254), 492: uint16(1254), 493: uint16(1524), 494: uint16(1523), 495: uint16(1254), 496: uint16(1254), 497: uint16(1375), 498: uint16(1254), 499: uint16(1254), 500: uint16(1254), 501: uint16(1254), 502: uint16(1254), 503: uint16(1254), 504: uint16(1254), 505: uint16(1254), 506: uint16(1254), 507: uint16(1254), 508: uint16(1254), 509: uint16(1254), 510: uint16(1254), 511: uint16(1302), 512: uint16(1254), 513: uint16(1254), 514: uint16(1254), 515: uint16(1254), 516: uint16(1254), 517: uint16(1254), 518: uint16(1254), 519: uint16(1254), 520: uint16(1254), 521: uint16(1254), 522: uint16(1254), 523: uint16(1254), 524: uint16(1254), 525: uint16(1254), 526: uint16(1254), 527: uint16(1254), 528: uint16(1254), 529: uint16(1254), 530: uint16(1254), 531: uint16(1254), 532: uint16(1254), 533: uint16(1254), 534: uint16(1254), 535: uint16(1402), 536: uint16(1254), 537: uint16(1254), 538: uint16(1254), 539: uint16(1254), 540: uint16(1254), 541: uint16(1254), 542: uint16(1254), 543: uint16(1254), 544: uint16(1254), 545: uint16(1254), 546: uint16(1254), 547: uint16(1254), 548: uint16(1254), 549: uint16(1254), 550: uint16(1593), 551: uint16(1392), 552: uint16(1254), 553: uint16(1254), 554: uint16(1254), 555: uint16(1254), 556: uint16(1642), 557: uint16(1254), 558: uint16(1254), 559: uint16(1254), 560: uint16(1254), 561: uint16(1352), 562: uint16(1254), 563: uint16(1254), 564: uint16(1254), 565: uint16(1254), 566: uint16(1254), 567: uint16(1254), 568: uint16(1254), 569: uint16(1254), 570: uint16(1254), 571: uint16(1254), 572: uint16(1254), 573: uint16(1634), 574: uint16(1346), 575: uint16(1438), 576: uint16(1254), 577: uint16(1441), 578: uint16(1276), 579: uint16(1254), 580: uint16(1266), 581: uint16(1254), 582: uint16(1254), } /********** 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 = [186]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 FyystackEnd uintptr Fyystack uintptr Fyystk0 [100]TyyStackEntry } type yyParser1 = TyyParser1 type TyyParser = struct { Fyytos uintptr FpParse uintptr FyystackEnd uintptr Fyystack uintptr Fyystk0 [100]TyyStackEntry } type yyParser = TyyParser /* #include */ // C documentation // // /* // ** Try to increase the size of the parser stack. Return the number // ** of errors. Return 0 on success. // */ func _yyGrowStack(tls *libc.TLS, p uintptr) (r int32) { var idx, newSize, oldSize int32 var pNew uintptr _, _, _, _ = idx, newSize, oldSize, pNew oldSize = int32(1) + int32((int64((*TyyParser)(unsafe.Pointer(p)).FyystackEnd)-int64((*TyyParser)(unsafe.Pointer(p)).Fyystack))/24) newSize = oldSize*int32(2) + int32(100) idx = int32((int64((*TyyParser)(unsafe.Pointer(p)).Fyytos) - int64((*TyyParser)(unsafe.Pointer(p)).Fyystack)) / 24) if (*TyyParser)(unsafe.Pointer(p)).Fyystack == p+32 { pNew = _parserStackRealloc(tls, uintptr(0), uint64(newSize)*uint64(24)) if pNew == uintptr(0) { return int32(1) } libc.X__builtin___memcpy_chk(tls, pNew, (*TyyParser)(unsafe.Pointer(p)).Fyystack, uint64(oldSize)*uint64(24), libc.X__builtin_object_size(tls, pNew, 0)) } else { pNew = _parserStackRealloc(tls, (*TyyParser)(unsafe.Pointer(p)).Fyystack, uint64(newSize)*uint64(24)) if pNew == uintptr(0) { return int32(1) } } (*TyyParser)(unsafe.Pointer(p)).Fyystack = pNew (*TyyParser)(unsafe.Pointer(p)).Fyytos = (*TyyParser)(unsafe.Pointer(p)).Fyystack + uintptr(idx)*24 (*TyyParser)(unsafe.Pointer(p)).FyystackEnd = (*TyyParser)(unsafe.Pointer(p)).Fyystack + uintptr(newSize-int32(1))*24 return 0 } /* 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)).Fyystack = yypParser + 32 (*TyyParser)(unsafe.Pointer(yypParser)).FyystackEnd = (*TyyParser)(unsafe.Pointer(yypParser)).Fyystack + uintptr(libc.Int32FromInt32(YYSTACKDEPTH)-libc.Int32FromInt32(1))*24 (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos = (*TyyParser)(unsafe.Pointer(yypParser)).Fyystack (*(*TyyStackEntry)(unsafe.Pointer((*TyyParser)(unsafe.Pointer(yypParser)).Fyystack))).Fstateno = uint16(0) (*(*TyyStackEntry)(unsafe.Pointer((*TyyParser)(unsafe.Pointer(yypParser)).Fyystack))).Fmajor = uint16(0) } // 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(205): /* select */ fallthrough case int32(240): /* selectnowith */ fallthrough case int32(241): /* oneselect */ fallthrough case int32(253): /* values */ fallthrough case int32(255): /* mvalues */ _sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(217): /* term */ fallthrough case int32(218): /* expr */ fallthrough case int32(247): /* where_opt */ fallthrough case int32(249): /* having_opt */ fallthrough case int32(269): /* where_opt_ret */ fallthrough case int32(280): /* case_operand */ fallthrough case int32(282): /* case_else */ fallthrough case int32(285): /* vinto */ fallthrough case int32(292): /* when_clause */ fallthrough case int32(297): /* key_opt */ fallthrough case int32(314): /* filter_clause */ _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(222): /* eidlist_opt */ fallthrough case int32(232): /* sortlist */ fallthrough case int32(233): /* eidlist */ fallthrough case int32(245): /* selcollist */ fallthrough case int32(248): /* groupby_opt */ fallthrough case int32(250): /* orderby_opt */ fallthrough case int32(254): /* nexprlist */ fallthrough case int32(256): /* sclp */ fallthrough case int32(263): /* exprlist */ fallthrough case int32(270): /* setlist */ fallthrough case int32(279): /* paren_exprlist */ fallthrough case int32(281): /* case_exprlist */ fallthrough case int32(313): /* part_opt */ _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(239): /* fullname */ fallthrough case int32(246): /* from */ fallthrough case int32(258): /* seltablist */ fallthrough case int32(259): /* stl_prefix */ fallthrough case int32(264): /* xfullname */ _sqlite3SrcListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(242): /* wqlist */ _sqlite3WithDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(252): /* window_clause */ fallthrough case int32(309): /* windowdefn_list */ _sqlite3WindowListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(265): /* idlist */ fallthrough case int32(272): /* idlist_opt */ _sqlite3IdListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(275): /* filter_over */ fallthrough case int32(310): /* windowdefn */ fallthrough case int32(311): /* window */ fallthrough case int32(312): /* frame_opt */ fallthrough case int32(315): /* over_clause */ _sqlite3WindowDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(288): /* trigger_cmd_list */ fallthrough case int32(293): /* trigger_cmd */ _sqlite3DeleteTriggerStep(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yypminor))) case int32(290): /* trigger_event */ _sqlite3IdListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, (*(*TTrigEvent)(unsafe.Pointer(yypminor))).Fb) case int32(317): /* frame_bound */ fallthrough case int32(318): /* frame_bound_s */ fallthrough case int32(319): /* 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 v2 = pParser v1 = *(*uintptr)(unsafe.Pointer(v2)) *(*uintptr)(unsafe.Pointer(v2)) -= 24 yytos = v1 _yy_destructor(tls, pParser, (*TyyStackEntry)(unsafe.Pointer(yytos)).Fmajor, yytos+8) } // C documentation // // /* // ** Clear all secondary memory allocations from the parser // */ func _sqlite3ParserFinalize(tls *libc.TLS, p uintptr) { var pParser, yytos uintptr _, _ = pParser, yytos pParser = p /* In-lined version of calling yy_pop_parser_stack() for each ** element left in the stack */ yytos = (*TyyParser)(unsafe.Pointer(pParser)).Fyytos for yytos > (*TyyParser)(unsafe.Pointer(pParser)).Fyystack { if int32((*TyyStackEntry)(unsafe.Pointer(yytos)).Fmajor) >= int32(YY_MIN_DSTRCTR) { _yy_destructor(tls, pParser, (*TyyStackEntry)(unsafe.Pointer(yytos)).Fmajor, yytos+8) } yytos -= 24 } if (*TyyParser)(unsafe.Pointer(pParser)).Fyystack != pParser+32 { Xsqlite3_free(tls, (*TyyParser)(unsafe.Pointer(pParser)).Fyystack) } } /* ** 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 } for cond := true; cond; cond = int32(1) != 0 { i = int32(_yy_shift_ofst[stateno]) i += int32(iLookAhead) if int32(_yy_lookahead[i]) != int32(iLookAhead) { /* Fallback token */ iFallback = _yyFallback[iLookAhead] if int32(iFallback) != 0 { /* Fallback loop must terminate */ iLookAhead = iFallback continue } j = i - int32(iLookAhead) + int32(YYWILDCARD) if int32(_yy_lookahead[j]) == int32(YYWILDCARD) && int32(iLookAhead) > 0 { return _yy_action[j] } return _yy_default[stateno] } else { 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 i = int32(_yy_reduce_ofst[stateno]) i += int32(iLookAhead) 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 > (*TyyParser)(unsafe.Pointer(yypParser)).Fyystack { _yy_pop_parser_stack(tls, yypParser) } /* Here code is inserted which will execute if the parser ** stack every overflows */ /******** Begin %stack_overflow code ******************************************/ _sqlite3OomFault(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb) /******** 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 += 24 yytos = (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos if yytos > (*TyyParser)(unsafe.Pointer(yypParser)).FyystackEnd { if _yyGrowStack(tls, yypParser) != 0 { (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos -= 24 _yyStackOverflow(tls, yypParser) return } yytos = (*TyyParser)(unsafe.Pointer(yypParser)).Fyytos } if int32(yyNewState) > int32(YY_MAX_SHIFT) { yyNewState = uint16(int32(yyNewState) + (libc.Int32FromInt32(YY_MIN_REDUCE) - libc.Int32FromInt32(YY_MIN_SHIFTREDUCE))) } (*TyyStackEntry)(unsafe.Pointer(yytos)).Fstateno = yyNewState (*TyyStackEntry)(unsafe.Pointer(yytos)).Fmajor = yyMajor *(*TToken)(unsafe.Pointer(yytos + 8)) = yyMinor } // C documentation // // /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side // ** of that rule */ var _yyRuleInfoLhs = [409]uint16{ 0: uint16(190), 1: uint16(190), 2: uint16(189), 3: uint16(191), 4: uint16(192), 5: uint16(192), 6: uint16(192), 7: uint16(192), 8: uint16(191), 9: uint16(191), 10: uint16(191), 11: uint16(191), 12: uint16(191), 13: uint16(196), 14: uint16(198), 15: uint16(200), 16: uint16(200), 17: uint16(199), 18: uint16(199), 19: uint16(197), 20: uint16(197), 21: uint16(204), 22: uint16(204), 23: uint16(206), 24: uint16(206), 25: uint16(207), 26: uint16(209), 27: uint16(209), 28: uint16(209), 29: uint16(210), 30: uint16(214), 31: uint16(215), 32: uint16(216), 33: uint16(216), 34: uint16(216), 35: uint16(216), 36: uint16(216), 37: uint16(216), 38: uint16(216), 39: uint16(216), 40: uint16(216), 41: uint16(216), 42: uint16(216), 43: uint16(216), 44: uint16(216), 45: uint16(225), 46: uint16(225), 47: uint16(221), 48: uint16(221), 49: uint16(223), 50: uint16(223), 51: uint16(226), 52: uint16(226), 53: uint16(226), 54: uint16(226), 55: uint16(227), 56: uint16(227), 57: uint16(227), 58: uint16(227), 59: uint16(227), 60: uint16(224), 61: uint16(224), 62: uint16(228), 63: uint16(228), 64: uint16(228), 65: uint16(203), 66: uint16(230), 67: uint16(231), 68: uint16(231), 69: uint16(231), 70: uint16(231), 71: uint16(231), 72: uint16(234), 73: uint16(219), 74: uint16(219), 75: uint16(235), 76: uint16(235), 77: uint16(236), 78: uint16(236), 79: uint16(191), 80: uint16(238), 81: uint16(238), 82: uint16(191), 83: uint16(191), 84: uint16(191), 85: uint16(205), 86: uint16(205), 87: uint16(205), 88: uint16(240), 89: uint16(243), 90: uint16(243), 91: uint16(243), 92: uint16(241), 93: uint16(241), 94: uint16(253), 95: uint16(241), 96: uint16(255), 97: uint16(255), 98: uint16(244), 99: uint16(244), 100: uint16(244), 101: uint16(256), 102: uint16(245), 103: uint16(245), 104: uint16(245), 105: uint16(257), 106: uint16(257), 107: uint16(246), 108: uint16(246), 109: uint16(259), 110: uint16(259), 111: uint16(258), 112: uint16(258), 113: uint16(258), 114: uint16(258), 115: uint16(258), 116: uint16(201), 117: uint16(201), 118: uint16(239), 119: uint16(239), 120: uint16(264), 121: uint16(264), 122: uint16(264), 123: uint16(264), 124: uint16(260), 125: uint16(260), 126: uint16(260), 127: uint16(260), 128: uint16(261), 129: uint16(261), 130: uint16(261), 131: uint16(266), 132: uint16(262), 133: uint16(262), 134: uint16(250), 135: uint16(250), 136: uint16(232), 137: uint16(232), 138: uint16(220), 139: uint16(220), 140: uint16(220), 141: uint16(267), 142: uint16(267), 143: uint16(267), 144: uint16(248), 145: uint16(248), 146: uint16(249), 147: uint16(249), 148: uint16(251), 149: uint16(251), 150: uint16(251), 151: uint16(251), 152: uint16(191), 153: uint16(247), 154: uint16(247), 155: uint16(269), 156: uint16(269), 157: uint16(269), 158: uint16(269), 159: uint16(191), 160: uint16(270), 161: uint16(270), 162: uint16(270), 163: uint16(270), 164: uint16(191), 165: uint16(191), 166: uint16(273), 167: uint16(273), 168: uint16(273), 169: uint16(273), 170: uint16(273), 171: uint16(273), 172: uint16(274), 173: uint16(271), 174: uint16(271), 175: uint16(272), 176: uint16(272), 177: uint16(265), 178: uint16(265), 179: uint16(218), 180: uint16(218), 181: uint16(218), 182: uint16(218), 183: uint16(217), 184: uint16(217), 185: uint16(217), 186: uint16(218), 187: uint16(218), 188: uint16(218), 189: uint16(218), 190: uint16(218), 191: uint16(218), 192: uint16(218), 193: uint16(218), 194: uint16(218), 195: uint16(217), 196: uint16(218), 197: uint16(218), 198: uint16(218), 199: uint16(218), 200: uint16(218), 201: uint16(218), 202: uint16(218), 203: uint16(218), 204: uint16(218), 205: uint16(276), 206: uint16(218), 207: uint16(218), 208: uint16(218), 209: uint16(218), 210: uint16(218), 211: uint16(218), 212: uint16(218), 213: uint16(218), 214: uint16(218), 215: uint16(218), 216: uint16(218), 217: uint16(218), 218: uint16(277), 219: uint16(277), 220: uint16(218), 221: uint16(278), 222: uint16(278), 223: uint16(218), 224: uint16(218), 225: uint16(218), 226: uint16(218), 227: uint16(218), 228: uint16(218), 229: uint16(281), 230: uint16(281), 231: uint16(282), 232: uint16(282), 233: uint16(280), 234: uint16(263), 235: uint16(254), 236: uint16(254), 237: uint16(279), 238: uint16(279), 239: uint16(191), 240: uint16(283), 241: uint16(283), 242: uint16(222), 243: uint16(222), 244: uint16(233), 245: uint16(233), 246: uint16(284), 247: uint16(284), 248: uint16(191), 249: uint16(191), 250: uint16(191), 251: uint16(285), 252: uint16(285), 253: uint16(191), 254: uint16(191), 255: uint16(191), 256: uint16(191), 257: uint16(191), 258: uint16(212), 259: uint16(213), 260: uint16(191), 261: uint16(287), 262: uint16(289), 263: uint16(289), 264: uint16(289), 265: uint16(290), 266: uint16(290), 267: uint16(290), 268: uint16(292), 269: uint16(292), 270: uint16(288), 271: uint16(288), 272: uint16(294), 273: uint16(295), 274: uint16(295), 275: uint16(293), 276: uint16(293), 277: uint16(293), 278: uint16(293), 279: uint16(218), 280: uint16(218), 281: uint16(237), 282: uint16(237), 283: uint16(237), 284: uint16(191), 285: uint16(191), 286: uint16(191), 287: uint16(297), 288: uint16(297), 289: uint16(191), 290: uint16(191), 291: uint16(191), 292: uint16(191), 293: uint16(191), 294: uint16(191), 295: uint16(191), 296: uint16(298), 297: uint16(191), 298: uint16(191), 299: uint16(191), 300: uint16(300), 301: uint16(302), 302: uint16(303), 303: uint16(303), 304: uint16(304), 305: uint16(268), 306: uint16(268), 307: uint16(307), 308: uint16(307), 309: uint16(307), 310: uint16(306), 311: uint16(308), 312: uint16(242), 313: uint16(242), 314: uint16(309), 315: uint16(310), 316: uint16(311), 317: uint16(311), 318: uint16(311), 319: uint16(311), 320: uint16(311), 321: uint16(312), 322: uint16(312), 323: uint16(312), 324: uint16(316), 325: uint16(318), 326: uint16(318), 327: uint16(319), 328: uint16(319), 329: uint16(317), 330: uint16(317), 331: uint16(320), 332: uint16(320), 333: uint16(321), 334: uint16(321), 335: uint16(321), 336: uint16(252), 337: uint16(275), 338: uint16(275), 339: uint16(275), 340: uint16(315), 341: uint16(315), 342: uint16(314), 343: uint16(217), 344: uint16(186), 345: uint16(187), 346: uint16(187), 347: uint16(188), 348: uint16(188), 349: uint16(188), 350: uint16(193), 351: uint16(193), 352: uint16(193), 353: uint16(195), 354: uint16(195), 355: uint16(191), 356: uint16(204), 357: uint16(202), 358: uint16(202), 359: uint16(194), 360: uint16(194), 361: uint16(209), 362: uint16(210), 363: uint16(211), 364: uint16(211), 365: uint16(208), 366: uint16(208), 367: uint16(216), 368: uint16(216), 369: uint16(216), 370: uint16(203), 371: uint16(229), 372: uint16(229), 373: uint16(230), 374: uint16(234), 375: uint16(236), 376: uint16(240), 377: uint16(241), 378: uint16(256), 379: uint16(257), 380: uint16(266), 381: uint16(274), 382: uint16(218), 383: uint16(276), 384: uint16(280), 385: uint16(263), 386: uint16(286), 387: uint16(286), 388: uint16(286), 389: uint16(286), 390: uint16(286), 391: uint16(212), 392: uint16(291), 393: uint16(291), 394: uint16(294), 395: uint16(295), 396: uint16(296), 397: uint16(296), 398: uint16(299), 399: uint16(299), 400: uint16(301), 401: uint16(301), 402: uint16(302), 403: uint16(305), 404: uint16(305), 405: uint16(305), 406: uint16(268), 407: uint16(309), 408: uint16(311), } // 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 = [409]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(1)), 96: int8(-int32(5)), 97: int8(-int32(5)), 98: int8(-int32(1)), 99: int8(-int32(1)), 102: int8(-int32(5)), 103: int8(-int32(3)), 104: int8(-int32(5)), 105: int8(-int32(2)), 108: int8(-int32(2)), 109: int8(-int32(2)), 111: int8(-int32(5)), 112: int8(-int32(6)), 113: int8(-int32(8)), 114: int8(-int32(6)), 115: int8(-int32(6)), 117: int8(-int32(2)), 118: int8(-int32(1)), 119: int8(-int32(3)), 120: int8(-int32(1)), 121: int8(-int32(3)), 122: int8(-int32(5)), 123: int8(-int32(3)), 124: int8(-int32(1)), 125: int8(-int32(2)), 126: int8(-int32(3)), 127: int8(-int32(4)), 128: int8(-int32(2)), 129: int8(-int32(4)), 132: int8(-int32(3)), 133: int8(-int32(2)), 135: int8(-int32(3)), 136: int8(-int32(5)), 137: int8(-int32(3)), 138: int8(-int32(1)), 139: int8(-int32(1)), 141: int8(-int32(2)), 142: int8(-int32(2)), 145: int8(-int32(3)), 147: int8(-int32(2)), 149: int8(-int32(2)), 150: int8(-int32(4)), 151: int8(-int32(4)), 152: int8(-int32(6)), 154: int8(-int32(2)), 156: int8(-int32(2)), 157: int8(-int32(2)), 158: int8(-int32(4)), 159: int8(-int32(9)), 160: int8(-int32(5)), 161: int8(-int32(7)), 162: int8(-int32(3)), 163: int8(-int32(5)), 164: int8(-int32(7)), 165: int8(-int32(8)), 167: int8(-int32(2)), 168: int8(-int32(12)), 169: int8(-int32(9)), 170: int8(-int32(5)), 171: int8(-int32(8)), 172: int8(-int32(2)), 173: int8(-int32(2)), 174: int8(-int32(1)), 176: int8(-int32(3)), 177: int8(-int32(3)), 178: int8(-int32(1)), 179: int8(-int32(3)), 180: int8(-int32(1)), 181: int8(-int32(3)), 182: int8(-int32(5)), 183: int8(-int32(1)), 184: int8(-int32(1)), 185: int8(-int32(1)), 186: int8(-int32(1)), 187: int8(-int32(3)), 188: int8(-int32(6)), 189: int8(-int32(5)), 190: int8(-int32(8)), 191: int8(-int32(4)), 192: int8(-int32(6)), 193: int8(-int32(9)), 194: int8(-int32(5)), 195: int8(-int32(1)), 196: int8(-int32(5)), 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(3)), 204: int8(-int32(3)), 205: int8(-int32(2)), 206: int8(-int32(3)), 207: int8(-int32(5)), 208: int8(-int32(2)), 209: int8(-int32(3)), 210: int8(-int32(3)), 211: int8(-int32(4)), 212: int8(-int32(6)), 213: int8(-int32(5)), 214: int8(-int32(2)), 215: int8(-int32(2)), 216: int8(-int32(2)), 217: int8(-int32(3)), 218: int8(-int32(1)), 219: int8(-int32(2)), 220: int8(-int32(5)), 221: int8(-int32(1)), 222: int8(-int32(2)), 223: int8(-int32(5)), 224: int8(-int32(3)), 225: int8(-int32(5)), 226: int8(-int32(5)), 227: int8(-int32(4)), 228: int8(-int32(5)), 229: int8(-int32(5)), 230: int8(-int32(4)), 231: int8(-int32(2)), 235: int8(-int32(3)), 236: int8(-int32(1)), 238: int8(-int32(3)), 239: int8(-int32(12)), 240: int8(-int32(1)), 243: int8(-int32(3)), 244: int8(-int32(5)), 245: int8(-int32(3)), 247: int8(-int32(2)), 248: int8(-int32(4)), 249: int8(-int32(2)), 250: int8(-int32(3)), 251: int8(-int32(2)), 253: int8(-int32(3)), 254: int8(-int32(5)), 255: int8(-int32(6)), 256: int8(-int32(5)), 257: int8(-int32(6)), 258: int8(-int32(2)), 259: int8(-int32(2)), 260: int8(-int32(5)), 261: int8(-int32(11)), 262: int8(-int32(1)), 263: int8(-int32(2)), 265: int8(-int32(1)), 266: int8(-int32(1)), 267: int8(-int32(3)), 269: int8(-int32(2)), 270: int8(-int32(3)), 271: int8(-int32(2)), 272: int8(-int32(3)), 273: int8(-int32(3)), 274: int8(-int32(2)), 275: int8(-int32(9)), 276: int8(-int32(8)), 277: int8(-int32(6)), 278: int8(-int32(3)), 279: int8(-int32(4)), 280: int8(-int32(6)), 281: int8(-int32(1)), 282: int8(-int32(1)), 283: int8(-int32(1)), 284: int8(-int32(4)), 285: int8(-int32(6)), 286: int8(-int32(3)), 288: int8(-int32(2)), 289: int8(-int32(1)), 290: int8(-int32(3)), 291: int8(-int32(1)), 292: int8(-int32(3)), 293: int8(-int32(6)), 294: int8(-int32(7)), 295: int8(-int32(6)), 296: int8(-int32(1)), 297: int8(-int32(8)), 298: int8(-int32(1)), 299: int8(-int32(4)), 300: int8(-int32(8)), 302: int8(-int32(1)), 303: int8(-int32(3)), 304: int8(-int32(1)), 305: int8(-int32(2)), 306: int8(-int32(3)), 307: int8(-int32(1)), 308: int8(-int32(2)), 309: int8(-int32(3)), 310: int8(-int32(6)), 311: int8(-int32(1)), 312: int8(-int32(1)), 313: int8(-int32(3)), 314: int8(-int32(3)), 315: int8(-int32(5)), 316: int8(-int32(5)), 317: int8(-int32(6)), 318: int8(-int32(4)), 319: int8(-int32(5)), 320: int8(-int32(2)), 322: int8(-int32(3)), 323: int8(-int32(6)), 324: int8(-int32(1)), 325: int8(-int32(1)), 326: int8(-int32(2)), 327: int8(-int32(1)), 328: int8(-int32(2)), 329: int8(-int32(2)), 330: int8(-int32(2)), 332: int8(-int32(2)), 333: int8(-int32(2)), 334: int8(-int32(2)), 335: int8(-int32(1)), 336: int8(-int32(2)), 337: int8(-int32(2)), 338: int8(-int32(1)), 339: int8(-int32(1)), 340: int8(-int32(4)), 341: int8(-int32(2)), 342: int8(-int32(5)), 343: int8(-int32(1)), 344: int8(-int32(1)), 345: int8(-int32(2)), 346: int8(-int32(1)), 347: int8(-int32(1)), 348: int8(-int32(2)), 349: int8(-int32(3)), 351: int8(-int32(1)), 352: int8(-int32(2)), 353: int8(-int32(1)), 355: int8(-int32(2)), 356: int8(-int32(1)), 357: int8(-int32(4)), 358: int8(-int32(2)), 359: int8(-int32(1)), 360: int8(-int32(1)), 361: int8(-int32(1)), 362: int8(-int32(1)), 363: int8(-int32(1)), 364: int8(-int32(1)), 365: int8(-int32(2)), 367: int8(-int32(2)), 368: int8(-int32(4)), 369: int8(-int32(2)), 370: int8(-int32(2)), 371: int8(-int32(3)), 372: int8(-int32(1)), 374: int8(-int32(1)), 375: int8(-int32(1)), 376: int8(-int32(1)), 377: int8(-int32(1)), 378: int8(-int32(2)), 379: int8(-int32(1)), 380: 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)), 388: int8(-int32(1)), 389: int8(-int32(1)), 390: int8(-int32(1)), 391: int8(-int32(1)), 393: int8(-int32(3)), 394: int8(-int32(1)), 396: int8(-int32(1)), 399: int8(-int32(1)), 400: int8(-int32(1)), 401: int8(-int32(3)), 402: int8(-int32(2)), 404: int8(-int32(4)), 405: int8(-int32(2)), 407: int8(-int32(1)), 408: 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(144) defer tls.Free(144) var bNot, bNot1, nExpr, yygoto, yysize int32 var n Tu32 var op Tu8 var p, p1, p2, p3, p4, p5, pDot, pFrom, pFromClause, pLeft, pLhs, pList, pList1, pList2, pList3, pList4, pNew, pOld, pRHS, pRhs, pRight, pSelect, pSelectRHS, pSrc, pSubquery, pSubquery1, temp1, temp11, temp2, temp21, temp3, temp4, yymsp, v347, v348, v349, v350, v351 uintptr var yyact uint16 var v352 TToken var _ /* all at bp+104 */ TToken var _ /* as at bp+72 */ TToken var _ /* dest at bp+16 */ TSelectDest var _ /* t at bp+88 */ TToken var _ /* x at bp+56 */ TToken var _ /* yylhsminor at bp+0 */ TYYMINORTYPE _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = bNot, bNot1, n, nExpr, op, p, p1, p2, p3, p4, p5, pDot, pFrom, pFromClause, pLeft, pLhs, pList, pList1, pList2, pList3, pList4, pNew, pOld, pRHS, pRhs, pRight, pSelect, pSelectRHS, pSrc, pSubquery, pSubquery1, temp1, temp11, temp2, temp21, temp3, temp4, yyact, yygoto, yymsp, yysize, v347, v348, v349, v350, v351, v352 /* 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(324): 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(100): goto _23 case uint32(246): 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(106): 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(173): goto _73 case uint32(80): goto _74 case uint32(63): goto _75 case uint32(219): goto _76 case uint32(222): goto _77 case uint32(247): 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(174): 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(97): goto _106 case uint32(96): goto _107 case uint32(98): goto _108 case uint32(99): goto _109 case uint32(134): goto _110 case uint32(101): goto _111 case uint32(144): goto _112 case uint32(234): goto _113 case uint32(237): goto _114 case uint32(242): goto _115 case uint32(102): goto _116 case uint32(103): goto _117 case uint32(104): goto _118 case uint32(117): goto _119 case uint32(105): goto _120 case uint32(258): goto _121 case uint32(259): goto _122 case uint32(110): goto _123 case uint32(107): goto _124 case uint32(108): goto _125 case uint32(109): goto _126 case uint32(111): goto _127 case uint32(112): goto _128 case uint32(113): goto _129 case uint32(114): goto _130 case uint32(115): goto _131 case uint32(131): goto _132 case uint32(116): 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(129): goto _145 case uint32(130): goto _146 case uint32(132): goto _147 case uint32(133): goto _148 case uint32(145): goto _149 case uint32(135): goto _150 case uint32(136): goto _151 case uint32(137): goto _152 case uint32(138): goto _153 case uint32(139): goto _154 case uint32(143): goto _155 case uint32(140): goto _156 case uint32(141): goto _157 case uint32(142): goto _158 case uint32(148): goto _159 case uint32(146): goto _160 case uint32(153): goto _161 case uint32(155): goto _162 case uint32(232): goto _163 case uint32(233): goto _164 case uint32(252): goto _165 case uint32(154): goto _166 case uint32(147): goto _167 case uint32(156): goto _168 case uint32(231): goto _169 case uint32(251): goto _170 case uint32(149): goto _171 case uint32(150): goto _172 case uint32(151): goto _173 case uint32(152): 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(171): goto _189 case uint32(172): 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(181): goto _197 case uint32(182): goto _198 case uint32(184): goto _199 case uint32(183): 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(196): goto _212 case uint32(197): goto _213 case uint32(199): goto _214 case uint32(198): 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(212): goto _228 case uint32(213): goto _229 case uint32(215): goto _230 case uint32(214): goto _231 case uint32(216): goto _232 case uint32(217): goto _233 case uint32(221): goto _234 case uint32(218): goto _235 case uint32(220): 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(229): goto _243 case uint32(230): goto _244 case uint32(235): goto _245 case uint32(236): goto _246 case uint32(243): goto _247 case uint32(238): goto _248 case uint32(239): goto _249 case uint32(282): goto _250 case uint32(240): goto _251 case uint32(241): goto _252 case uint32(244): goto _253 case uint32(245): goto _254 case uint32(248): goto _255 case uint32(249): goto _256 case uint32(250): goto _257 case uint32(253): goto _258 case uint32(254): goto _259 case uint32(255): goto _260 case uint32(256): goto _261 case uint32(257): goto _262 case uint32(260): goto _263 case uint32(261): goto _264 case uint32(262): goto _265 case uint32(263): goto _266 case uint32(264): goto _267 case uint32(266): goto _268 case uint32(265): goto _269 case uint32(267): goto _270 case uint32(287): goto _271 case uint32(268): goto _272 case uint32(288): 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(280): goto _285 case uint32(281): goto _286 case uint32(283): goto _287 case uint32(284): goto _288 case uint32(285): goto _289 case uint32(286): 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(300): goto _302 case uint32(301): goto _303 case uint32(303): goto _304 case uint32(302): goto _305 case uint32(304): goto _306 case uint32(306): goto _307 case uint32(305): 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(321): goto _323 case uint32(322): goto _324 case uint32(323): goto _325 case uint32(327): goto _326 case uint32(325): goto _327 case uint32(328): goto _328 case uint32(326): goto _329 case uint32(330): goto _330 case uint32(329): goto _331 case uint32(331): goto _332 case uint32(332): goto _333 case uint32(334): goto _334 case uint32(333): 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 case uint32(340): goto _341 case uint32(341): goto _342 case uint32(342): goto _343 case uint32(343): goto _344 default: goto _345 } goto _346 _1: ; /* explain ::= EXPLAIN */ if (*TParse)(unsafe.Pointer(pParse)).FpReprepare == uintptr(0) { (*TParse)(unsafe.Pointer(pParse)).Fexplain = uint8(1) } goto _346 _2: ; /* explain ::= EXPLAIN QUERY PLAN */ if (*TParse)(unsafe.Pointer(pParse)).FpReprepare == uintptr(0) { (*TParse)(unsafe.Pointer(pParse)).Fexplain = uint8(2) } goto _346 _3: ; /* cmdx ::= cmd */ _sqlite3FinishCoding(tls, pParse) goto _346 _4: ; /* cmd ::= BEGIN transtype trans_opt */ _sqlite3BeginTransaction(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _5: ; /* transtype ::= */ *(*int32)(unsafe.Pointer(yymsp + 1*24 + 8)) = int32(TK_DEFERRED) goto _346 _7: ; /* transtype ::= DEFERRED */ _6: ; _8: ; _9: ; *(*int32)(unsafe.Pointer(yymsp + 8)) = int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor) /*A-overwrites-X*/ goto _346 _11: ; /* cmd ::= COMMIT|END trans_opt */ _10: ; _sqlite3EndTransaction(tls, pParse, int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24))).Fmajor)) goto _346 _12: ; /* cmd ::= SAVEPOINT nm */ _sqlite3Savepoint(tls, pParse, SAVEPOINT_BEGIN, yymsp+8) goto _346 _13: ; /* cmd ::= RELEASE savepoint_opt nm */ _sqlite3Savepoint(tls, pParse, int32(SAVEPOINT_RELEASE), yymsp+8) goto _346 _14: ; /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */ _sqlite3Savepoint(tls, pParse, int32(SAVEPOINT_ROLLBACK), yymsp+8) goto _346 _15: ; /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */ _sqlite3StartTable(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, yymsp+8, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), 0, 0, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) goto _346 _16: ; /* createkw ::= CREATE */ _disableLookaside(tls, pParse) goto _346 _18: ; /* ifnotexists ::= */ _17: ; _19: ; _20: ; _21: ; _22: ; _23: ; _24: ; *(*int32)(unsafe.Pointer(yymsp + 1*24 + 8)) = 0 goto _346 _25: ; /* ifnotexists ::= IF NOT EXISTS */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = int32(1) goto _346 _26: ; /* temp ::= TEMP */ *(*int32)(unsafe.Pointer(yymsp + 8)) = libc.BoolInt32(int32((*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).Finit1.Fbusy) == 0) goto _346 _27: ; /* create_table_args ::= LP columnlist conslist_opt RP table_option_set */ _sqlite3EndTable(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, *(*Tu32)(unsafe.Pointer(yymsp + 8)), uintptr(0)) goto _346 _28: ; /* create_table_args ::= AS select */ _sqlite3EndTable(tls, pParse, uintptr(0), uintptr(0), uint32(0), *(*uintptr)(unsafe.Pointer(yymsp + 8))) _sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _29: ; /* table_option_set ::= */ *(*Tu32)(unsafe.Pointer(yymsp + 1*24 + 8)) = uint32(0) goto _346 _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))*24 + 8)) | *(*Tu32)(unsafe.Pointer(yymsp + 8)) *(*Tu32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*Tu32)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _31: ; /* table_option ::= WITHOUT nm */ if *(*uint32)(unsafe.Pointer(yymsp + 8 + 8)) == uint32(5) && Xsqlite3_strnicmp(tls, *(*uintptr)(unsafe.Pointer(yymsp + 8)), __ccgo_ts+17132, int32(5)) == 0 { *(*Tu32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = uint32(libc.Int32FromInt32(TF_WithoutRowid) | libc.Int32FromInt32(TF_NoVisibleRowid)) } else { *(*Tu32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = uint32(0) _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23876, libc.VaList(bp+128, *(*uint32)(unsafe.Pointer(yymsp + 8 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8)))) } goto _346 _32: ; /* table_option ::= nm */ if *(*uint32)(unsafe.Pointer(yymsp + 8 + 8)) == uint32(6) && Xsqlite3_strnicmp(tls, *(*uintptr)(unsafe.Pointer(yymsp + 8)), __ccgo_ts+17045, 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+23876, libc.VaList(bp+128, *(*uint32)(unsafe.Pointer(yymsp + 8 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8)))) } *(*Tu32)(unsafe.Pointer(yymsp + 8)) = *(*Tu32)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _33: ; /* columnname ::= nm typetoken */ _sqlite3AddColumn(tls, pParse, *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*TToken)(unsafe.Pointer(yymsp + 8))) goto _346 _35: ; /* typetoken ::= */ _34: ; _36: ; *(*uint32)(unsafe.Pointer(yymsp + 1*24 + 8 + 8)) = uint32(0) *(*uintptr)(unsafe.Pointer(yymsp + 1*24 + 8)) = uintptr(0) goto _346 _37: ; /* typetoken ::= typename LP signed RP */ *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8 + 8)) = uint32(int32(t__predefined_ptrdiff_t(*(*uintptr)(unsafe.Pointer(yymsp + 8))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + 8 + 8)))) - int64(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8))))) goto _346 _38: ; /* typetoken ::= typename LP signed COMMA signed RP */ *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8 + 8)) = uint32(int32(t__predefined_ptrdiff_t(*(*uintptr)(unsafe.Pointer(yymsp + 8))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + 8 + 8)))) - int64(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8))))) goto _346 _39: ; /* typename ::= typename ID|STRING */ *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)) = *(*uint32)(unsafe.Pointer(yymsp + 8 + 8)) + uint32(int32(int64(*(*uintptr)(unsafe.Pointer(yymsp + 8)))-int64(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))))) goto _346 _40: ; /* scanpt ::= */ *(*uintptr)(unsafe.Pointer(yymsp + 1*24 + 8)) = yyLookaheadToken.Fz goto _346 _41: ; /* scantok ::= */ *(*TToken)(unsafe.Pointer(yymsp + 1*24 + 8)) = yyLookaheadToken goto _346 _43: ; /* ccons ::= CONSTRAINT nm */ _42: ; (*TParse)(unsafe.Pointer(pParse)).FconstraintName = *(*TToken)(unsafe.Pointer(yymsp + 8)) goto _346 _44: ; /* ccons ::= DEFAULT scantok term */ _sqlite3AddDefaultValue(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)))) goto _346 _45: ; /* ccons ::= DEFAULT LP expr RP */ _sqlite3AddDefaultValue(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))+uintptr(1), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _46: ; /* ccons ::= DEFAULT PLUS scantok term */ _sqlite3AddDefaultValue(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)))) goto _346 _47: ; /* ccons ::= DEFAULT MINUS scantok term */ p = _sqlite3PExpr(tls, pParse, int32(TK_UMINUS), *(*uintptr)(unsafe.Pointer(yymsp + 8)), uintptr(0)) _sqlite3AddDefaultValue(tls, pParse, p, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)))) goto _346 _48: ; /* ccons ::= DEFAULT scantok ID|INDEXED */ p1 = _tokenExpr(tls, pParse, int32(TK_STRING), *(*TToken)(unsafe.Pointer(yymsp + 8))) if p1 != 0 { _sqlite3ExprIdToTrueFalse(tls, p1) } _sqlite3AddDefaultValue(tls, pParse, p1, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))+uintptr(*(*uint32)(unsafe.Pointer(yymsp + 8 + 8)))) goto _346 _49: ; /* ccons ::= NOT NULL onconf */ _sqlite3AddNotNull(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + 8))) goto _346 _50: ; /* ccons ::= PRIMARY KEY sortorder onconf autoinc */ _sqlite3AddPrimaryKey(tls, pParse, uintptr(0), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) goto _346 _51: ; /* ccons ::= UNIQUE onconf */ _sqlite3CreateIndex(tls, pParse, uintptr(0), uintptr(0), uintptr(0), uintptr(0), *(*int32)(unsafe.Pointer(yymsp + 8)), uintptr(0), uintptr(0), 0, 0, uint8(SQLITE_IDXTYPE_UNIQUE)) goto _346 _52: ; /* ccons ::= CHECK LP expr RP */ _sqlite3AddCheckConstraint(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _53: ; /* ccons ::= REFERENCES nm eidlist_opt refargs */ _sqlite3CreateForeignKey(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + 8))) goto _346 _54: ; /* ccons ::= defer_subclause */ _sqlite3DeferForeignKey(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + 8))) goto _346 _55: ; /* ccons ::= COLLATE ID|STRING */ _sqlite3AddCollateType(tls, pParse, yymsp+8) goto _346 _56: ; /* generated ::= LP expr RP */ _sqlite3AddGenerated(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), uintptr(0)) goto _346 _57: ; /* generated ::= LP expr RP ID */ _sqlite3AddGenerated(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), yymsp+8) goto _346 _58: ; /* autoinc ::= AUTOINCR */ *(*int32)(unsafe.Pointer(yymsp + 8)) = int32(1) goto _346 _59: ; /* refargs ::= */ *(*int32)(unsafe.Pointer(yymsp + 1*24 + 8)) = libc.Int32FromInt32(OE_None) * libc.Int32FromInt32(0x0101) /* EV: R-19803-45884 */ goto _346 _60: ; /* refargs ::= refargs refarg */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) & ^*(*int32)(unsafe.Pointer(yymsp + 8 + 4)) | *(*int32)(unsafe.Pointer(yymsp + 8)) goto _346 _61: ; /* refarg ::= MATCH nm */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = 0 *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 4)) = 0x000000 goto _346 _62: ; /* refarg ::= ON INSERT refact */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = 0 *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8 + 4)) = 0x000000 goto _346 _63: ; /* refarg ::= ON DELETE refact */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*int32)(unsafe.Pointer(yymsp + 8)) *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8 + 4)) = int32(0x0000ff) goto _346 _64: ; /* refarg ::= ON UPDATE refact */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*int32)(unsafe.Pointer(yymsp + 8)) << int32(8) *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8 + 4)) = int32(0x00ff00) goto _346 _65: ; /* refact ::= SET NULL */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = int32(OE_SetNull) /* EV: R-33326-45252 */ goto _346 _66: ; /* refact ::= SET DEFAULT */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = int32(OE_SetDflt) /* EV: R-33326-45252 */ goto _346 _67: ; /* refact ::= CASCADE */ *(*int32)(unsafe.Pointer(yymsp + 8)) = int32(OE_Cascade) /* EV: R-33326-45252 */ goto _346 _68: ; /* refact ::= RESTRICT */ *(*int32)(unsafe.Pointer(yymsp + 8)) = int32(OE_Restrict) /* EV: R-33326-45252 */ goto _346 _69: ; /* refact ::= NO ACTION */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = OE_None /* EV: R-33326-45252 */ goto _346 _70: ; /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = 0 goto _346 _72: ; /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */ _71: ; _73: ; *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*int32)(unsafe.Pointer(yymsp + 8)) goto _346 _75: ; /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ _74: ; _76: ; _77: ; _78: ; *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = int32(1) goto _346 _79: ; /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = 0 goto _346 _80: ; /* tconscomma ::= COMMA */ (*TParse)(unsafe.Pointer(pParse)).FconstraintName.Fn = uint32(0) goto _346 _81: ; /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */ _sqlite3AddPrimaryKey(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), 0) goto _346 _82: ; /* tcons ::= UNIQUE LP sortlist RP onconf */ _sqlite3CreateIndex(tls, pParse, uintptr(0), uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + 8)), uintptr(0), uintptr(0), 0, 0, uint8(SQLITE_IDXTYPE_UNIQUE)) goto _346 _83: ; /* tcons ::= CHECK LP expr RP onconf */ _sqlite3AddCheckConstraint(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _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))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(3))*24+8, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) _sqlite3DeferForeignKey(tls, pParse, *(*int32)(unsafe.Pointer(yymsp + 8))) goto _346 _86: ; /* onconf ::= */ _85: ; *(*int32)(unsafe.Pointer(yymsp + 1*24 + 8)) = int32(OE_Default) goto _346 _87: ; /* onconf ::= ON CONFLICT resolvetype */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*int32)(unsafe.Pointer(yymsp + 8)) goto _346 _88: ; /* resolvetype ::= IGNORE */ *(*int32)(unsafe.Pointer(yymsp + 8)) = int32(OE_Ignore) goto _346 _90: ; /* resolvetype ::= REPLACE */ _89: ; *(*int32)(unsafe.Pointer(yymsp + 8)) = int32(OE_Replace) goto _346 _91: ; /* cmd ::= DROP TABLE ifexists fullname */ _sqlite3DropTable(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), 0, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _92: ; /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */ _sqlite3CreateView(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(8))*24+8, yymsp+uintptr(-libc.Int32FromInt32(4))*24+8, yymsp+uintptr(-libc.Int32FromInt32(3))*24+8, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8))) goto _346 _93: ; /* cmd ::= DROP VIEW ifexists fullname */ _sqlite3DropTable(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), int32(1), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _94: ; /* cmd ::= select */ *(*TSelectDest)(unsafe.Pointer(bp + 16)) = TSelectDest{ FeDest: uint8(SRT_Output), } _sqlite3Select(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), bp+16) _sqlite3SelectDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _95: ; /* select ::= WITH wqlist selectnowith */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _attachWithToSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _96: ; /* select ::= WITH RECURSIVE wqlist selectnowith */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = _attachWithToSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _97: ; /* select ::= selectnowith */ p2 = *(*uintptr)(unsafe.Pointer(yymsp + 8)) if p2 != 0 { _parserDoubleLinkSelect(tls, pParse, p2) } goto _346 _98: ; /* selectnowith ::= selectnowith multiselect_op oneselect */ pRhs = *(*uintptr)(unsafe.Pointer(yymsp + 8)) pLhs = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) if pRhs != 0 && (*TSelect)(unsafe.Pointer(pRhs)).FpPrior != 0 { (*(*TToken)(unsafe.Pointer(bp + 56))).Fn = uint32(0) _parserDoubleLinkSelect(tls, pParse, pRhs) pFrom = _sqlite3SrcListAppendFromTerm(tls, pParse, uintptr(0), uintptr(0), uintptr(0), bp+56, 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))*24 + 8))) (*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))*24 + 8)) != 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))*24 + 8)) = pRhs goto _346 _100: ; /* multiselect_op ::= UNION */ _99: ; *(*int32)(unsafe.Pointer(yymsp + 8)) = int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor) /*A-overwrites-OP*/ goto _346 _101: ; /* multiselect_op ::= UNION ALL */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = int32(TK_ALL) goto _346 _102: ; /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*24 + 8)) = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), uint32(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*24 + 8))), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _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))*24 + 8)) = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), uint32(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*24 + 8))), *(*uintptr)(unsafe.Pointer(yymsp + 8))) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(9))*24 + 8)) != 0 { (*TSelect)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(9))*24 + 8)))).FpWinDefn = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) } else { _sqlite3WindowListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) } goto _346 _104: ; /* values ::= VALUES LP nexprlist RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = _sqlite3SelectNew(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(SF_Values), uintptr(0)) goto _346 _105: ; /* oneselect ::= mvalues */ _sqlite3MultiValuesEnd(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _107: ; /* mvalues ::= values COMMA LP nexprlist RP */ _106: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3MultiValues(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _108: ; /* distinct ::= DISTINCT */ *(*int32)(unsafe.Pointer(yymsp + 8)) = int32(SF_Distinct) goto _346 _109: ; /* distinct ::= ALL */ *(*int32)(unsafe.Pointer(yymsp + 8)) = int32(SF_All) goto _346 _111: ; /* sclp ::= */ _110: ; _112: ; _113: ; _114: ; _115: ; *(*uintptr)(unsafe.Pointer(yymsp + 1*24 + 8)) = uintptr(0) goto _346 _116: ; /* selcollist ::= sclp scanpt expr scanpt as */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) if *(*uint32)(unsafe.Pointer(yymsp + 8 + 8)) > uint32(0) { _sqlite3ExprListSetName(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), yymsp+8, int32(1)) } _sqlite3ExprListSetSpan(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _117: ; /* selcollist ::= sclp scanpt STAR */ p3 = _sqlite3Expr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_ASTERISK), uintptr(0)) _sqlite3ExprSetErrorOffset(tls, p3, int32(int64(*(*uintptr)(unsafe.Pointer(yymsp + 8)))-int64((*TParse)(unsafe.Pointer(pParse)).FzTail))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), p3) goto _346 _118: ; /* selcollist ::= sclp scanpt nm DOT STAR */ pRight = _sqlite3PExpr(tls, pParse, int32(TK_ASTERISK), uintptr(0), uintptr(0)) _sqlite3ExprSetErrorOffset(tls, pRight, int32(int64(*(*uintptr)(unsafe.Pointer(yymsp + 8)))-int64((*TParse)(unsafe.Pointer(pParse)).FzTail))) pLeft = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) pDot = _sqlite3PExpr(tls, pParse, int32(TK_DOT), pLeft, pRight) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), pDot) goto _346 _120: ; /* as ::= AS nm */ _119: ; _121: ; _122: ; *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*TToken)(unsafe.Pointer(yymsp + 8)) goto _346 _124: ; /* from ::= */ _123: ; *(*uintptr)(unsafe.Pointer(yymsp + 1*24 + 8)) = uintptr(0) goto _346 _125: ; /* from ::= FROM seltablist */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + 8)) _sqlite3SrcListShiftJoinType(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _126: ; /* stl_prefix ::= seltablist joinop */ if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) != 0 && (*TSrcList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)))).FnSrc > 0 { (*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) + 8 + uintptr((*TSrcList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)))).FnSrc-int32(1))*104))).Ffg.Fjointype = uint8(*(*int32)(unsafe.Pointer(yymsp + 8))) } goto _346 _127: ; /* seltablist ::= stl_prefix nm dbnm as on_using */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(3))*24+8, yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, uintptr(0), yymsp+8) goto _346 _128: ; /* seltablist ::= stl_prefix nm dbnm as indexed_by on_using */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(4))*24+8, yymsp+uintptr(-libc.Int32FromInt32(3))*24+8, yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, uintptr(0), yymsp+8) _sqlite3SrcListIndexedBy(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(1))*24+8) goto _346 _129: ; /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*24 + 8)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(6))*24+8, yymsp+uintptr(-libc.Int32FromInt32(5))*24+8, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, uintptr(0), yymsp+8) _sqlite3SrcListFuncArgs(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8))) goto _346 _130: ; /* seltablist ::= stl_prefix LP select RP as on_using */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), uintptr(0), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), yymsp+8) goto _346 _131: ; /* seltablist ::= stl_prefix LP seltablist RP as on_using */ if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) == uintptr(0) && *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)) == uint32(0) && *(*uintptr)(unsafe.Pointer(yymsp + 8)) == uintptr(0) && *(*uintptr)(unsafe.Pointer(yymsp + 8 + 8)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) } else { if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) != uintptr(0) && (*TSrcList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)))).FnSrc == int32(1) { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), uintptr(0), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, uintptr(0), yymsp+8) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) != 0 { pNew = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) + 8 + uintptr((*TSrcList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)))).FnSrc-int32(1))*104 pOld = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) + 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+60+4, libc.Uint32FromInt32(1), 13, 0x2000) } if int32(uint32(*(*uint16)(unsafe.Pointer(pOld + 60 + 4))&0x4>>2)) != 0 { *(*uintptr)(unsafe.Pointer(pNew + 88)) = *(*uintptr)(unsafe.Pointer(pOld + 88)) *(*uintptr)(unsafe.Pointer(pOld + 88)) = uintptr(0) libc.SetBitFieldPtr16Uint32(pOld+60+4, libc.Uint32FromInt32(0), 2, 0x4) libc.SetBitFieldPtr16Uint32(pNew+60+4, libc.Uint32FromInt32(1), 2, 0x4) } v347 = libc.UintptrFromInt32(0) (*TSrcItem)(unsafe.Pointer(pOld)).FzDatabase = v347 (*TSrcItem)(unsafe.Pointer(pOld)).FzName = v347 (*TSrcItem)(unsafe.Pointer(pOld)).FpSelect = uintptr(0) } _sqlite3SrcListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8))) } else { _sqlite3SrcListShiftJoinType(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8))) pSubquery = _sqlite3SelectNew(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(SF_NestedFrom), uintptr(0)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = _sqlite3SrcListAppendFromTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), uintptr(0), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, pSubquery, yymsp+8) } } goto _346 _133: ; /* dbnm ::= */ _132: ; *(*uintptr)(unsafe.Pointer(yymsp + 1*24 + 8)) = uintptr(0) *(*uint32)(unsafe.Pointer(yymsp + 1*24 + 8 + 8)) = uint32(0) goto _346 _134: ; /* fullname ::= nm */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+8, 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+8) } *(*uintptr)(unsafe.Pointer(yymsp + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _135: ; /* fullname ::= nm DOT nm */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, yymsp+8) 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+8) } *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _136: ; /* xfullname ::= nm */ *(*uintptr)(unsafe.Pointer(yymsp + 8)) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+8, uintptr(0)) /*A-overwrites-X*/ goto _346 _137: ; /* xfullname ::= nm DOT nm */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, yymsp+8) /*A-overwrites-X*/ goto _346 _138: ; /* xfullname ::= nm DOT nm AS nm */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(4))*24+8, yymsp+uintptr(-libc.Int32FromInt32(2))*24+8) /*A-overwrites-X*/ if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) != 0 { (*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) + 8))).FzAlias = _sqlite3NameFromToken(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, yymsp+8) } goto _346 _139: ; /* xfullname ::= nm AS nm */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, uintptr(0)) /*A-overwrites-X*/ if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) != 0 { (*(*TSrcItem)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) + 8))).FzAlias = _sqlite3NameFromToken(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, yymsp+8) } goto _346 _140: ; /* joinop ::= COMMA|JOIN */ *(*int32)(unsafe.Pointer(yymsp + 8)) = int32(JT_INNER) goto _346 _141: ; /* joinop ::= JOIN_KW JOIN */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = _sqlite3JoinType(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, uintptr(0), uintptr(0)) /*X-overwrites-A*/ goto _346 _142: ; /* joinop ::= JOIN_KW nm JOIN */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3JoinType(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, uintptr(0)) /*X-overwrites-A*/ goto _346 _143: ; /* joinop ::= JOIN_KW nm nm JOIN */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = _sqlite3JoinType(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*24+8, yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8) /*X-overwrites-A*/ goto _346 _144: ; /* on_using ::= ON expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + 8)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)) = uintptr(0) goto _346 _145: ; /* on_using ::= USING LP idlist RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = uintptr(0) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) goto _346 _146: ; /* on_using ::= */ *(*uintptr)(unsafe.Pointer(yymsp + 1*24 + 8)) = uintptr(0) *(*uintptr)(unsafe.Pointer(yymsp + 1*24 + 8 + 8)) = uintptr(0) goto _346 _147: ; /* indexed_by ::= INDEXED BY nm */ *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*TToken)(unsafe.Pointer(yymsp + 8)) goto _346 _148: ; /* indexed_by ::= NOT INDEXED */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = uintptr(0) *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)) = uint32(1) goto _346 _150: ; /* orderby_opt ::= ORDER BY sortlist */ _149: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + 8)) goto _346 _151: ; /* sortlist ::= sortlist COMMA expr sortorder nulls */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) _sqlite3ExprListSetSortOrder(tls, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + 8))) goto _346 _152: ; /* sortlist ::= expr sortorder nulls */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) /*A-overwrites-Y*/ _sqlite3ExprListSetSortOrder(tls, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + 8))) goto _346 _153: ; /* sortorder ::= ASC */ *(*int32)(unsafe.Pointer(yymsp + 8)) = SQLITE_SO_ASC goto _346 _154: ; /* sortorder ::= DESC */ *(*int32)(unsafe.Pointer(yymsp + 8)) = int32(SQLITE_SO_DESC) goto _346 _156: ; /* sortorder ::= */ _155: ; *(*int32)(unsafe.Pointer(yymsp + 1*24 + 8)) = -int32(1) goto _346 _157: ; /* nulls ::= NULLS FIRST */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = SQLITE_SO_ASC goto _346 _158: ; /* nulls ::= NULLS LAST */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = int32(SQLITE_SO_DESC) goto _346 _160: ; /* having_opt ::= */ _159: ; _161: ; _162: ; _163: ; _164: ; _165: ; *(*uintptr)(unsafe.Pointer(yymsp + 1*24 + 8)) = uintptr(0) goto _346 _167: ; /* having_opt ::= HAVING expr */ _166: ; _168: ; _169: ; _170: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + 8)) goto _346 _171: ; /* limit_opt ::= LIMIT expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_LIMIT), *(*uintptr)(unsafe.Pointer(yymsp + 8)), uintptr(0)) goto _346 _172: ; /* limit_opt ::= LIMIT expr OFFSET expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_LIMIT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _173: ; /* limit_opt ::= LIMIT expr COMMA expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_LIMIT), *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) goto _346 _174: ; /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */ _sqlite3SrcListIndexedBy(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(1))*24+8) _sqlite3DeleteFrom(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8)), uintptr(0), uintptr(0)) goto _346 _175: ; /* where_opt_ret ::= RETURNING selcollist */ _sqlite3AddReturning(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = uintptr(0) goto _346 _176: ; /* where_opt_ret ::= WHERE expr RETURNING selcollist */ _sqlite3AddReturning(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) goto _346 _177: ; /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */ _sqlite3SrcListIndexedBy(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(4))*24+8) _sqlite3ExprListCheckLength(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), __ccgo_ts+23903) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) != 0 { pFromClause = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) 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 + 72))).Fn = uint32(0) (*(*TToken)(unsafe.Pointer(bp + 72))).Fz = uintptr(0) pFromClause = _sqlite3SrcListAppendFromTerm(tls, pParse, uintptr(0), uintptr(0), uintptr(0), bp+72, pSubquery1, uintptr(0)) } *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = _sqlite3SrcListAppendList(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), pFromClause) } _sqlite3Update(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*24 + 8)), uintptr(0), uintptr(0), uintptr(0)) goto _346 _178: ; /* setlist ::= setlist COMMA nm EQ expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) _sqlite3ExprListSetName(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, int32(1)) goto _346 _179: ; /* setlist ::= setlist COMMA LP idlist RP EQ expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*24 + 8)) = _sqlite3ExprListAppendVector(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _180: ; /* setlist ::= nm EQ expr */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 8))) _sqlite3ExprListSetName(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))), yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, int32(1)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _181: ; /* setlist ::= LP idlist RP EQ expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3ExprListAppendVector(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _182: ; /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */ _sqlite3Insert(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _183: ; /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */ _sqlite3Insert(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*24 + 8)), uintptr(0)) goto _346 _184: ; /* upsert ::= */ *(*uintptr)(unsafe.Pointer(yymsp + 1*24 + 8)) = uintptr(0) goto _346 _185: ; /* upsert ::= RETURNING selcollist */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = uintptr(0) _sqlite3AddReturning(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _186: ; /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt upsert */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(11))*24 + 8)) = _sqlite3UpsertNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _187: ; /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*24 + 8)) = _sqlite3UpsertNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _188: ; /* upsert ::= ON CONFLICT DO NOTHING returning */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3UpsertNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uintptr(0)) goto _346 _189: ; /* upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*24 + 8)) = _sqlite3UpsertNew(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uintptr(0), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), uintptr(0)) goto _346 _190: ; /* returning ::= RETURNING selcollist */ _sqlite3AddReturning(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _191: ; /* idlist_opt ::= */ *(*uintptr)(unsafe.Pointer(yymsp + 1*24 + 8)) = uintptr(0) goto _346 _192: ; /* idlist_opt ::= LP idlist RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) goto _346 _193: ; /* idlist ::= idlist COMMA nm */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3IdListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), yymsp+8) goto _346 _194: ; /* idlist ::= nm */ *(*uintptr)(unsafe.Pointer(yymsp + 8)) = _sqlite3IdListAppend(tls, pParse, uintptr(0), yymsp+8) /*A-overwrites-Y*/ goto _346 _195: ; /* expr ::= LP expr RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) goto _346 _196: ; /* expr ::= ID|INDEXED|JOIN_KW */ *(*uintptr)(unsafe.Pointer(yymsp + 8)) = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + 8))) /*A-overwrites-X*/ goto _346 _197: ; /* expr ::= nm DOT nm */ temp1 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) temp2 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + 8))) *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3PExpr(tls, pParse, int32(TK_DOT), temp1, temp2) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _198: ; /* expr ::= nm DOT nm DOT nm */ temp11 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8))) temp21 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) temp3 = _tokenExpr(tls, pParse, int32(TK_ID), *(*TToken)(unsafe.Pointer(yymsp + 8))) 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))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _200: ; /* term ::= NULL|FLOAT|BLOB */ _199: ; *(*uintptr)(unsafe.Pointer(yymsp + 8)) = _tokenExpr(tls, pParse, int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor), *(*TToken)(unsafe.Pointer(yymsp + 8))) /*A-overwrites-X*/ goto _346 _201: ; /* term ::= INTEGER */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_INTEGER), yymsp+8, int32(1)) if *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) != 0 { *(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) + 52)) = int32(int64(*(*uintptr)(unsafe.Pointer(yymsp + 8))) - int64((*TParse)(unsafe.Pointer(pParse)).FzTail)) } *(*uintptr)(unsafe.Pointer(yymsp + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _202: ; /* expr ::= VARIABLE */ if !(int32(*(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + 8))))) == int32('#') && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + 8)) + 1)))])&int32(0x04) != 0) { n = *(*uint32)(unsafe.Pointer(yymsp + 8 + 8)) *(*uintptr)(unsafe.Pointer(yymsp + 8)) = _tokenExpr(tls, pParse, int32(TK_VARIABLE), *(*TToken)(unsafe.Pointer(yymsp + 8))) _sqlite3ExprAssignVarNumber(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), 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 + 88)) = *(*TToken)(unsafe.Pointer(yymsp + 8)) /*A-overwrites-X*/ if int32((*TParse)(unsafe.Pointer(pParse)).Fnested) == 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23912, libc.VaList(bp+128, bp+88)) *(*uintptr)(unsafe.Pointer(yymsp + 8)) = uintptr(0) } else { *(*uintptr)(unsafe.Pointer(yymsp + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_REGISTER), uintptr(0), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + 8)) != 0 { _sqlite3GetInt32(tls, (*(*TToken)(unsafe.Pointer(bp + 88))).Fz+1, *(*uintptr)(unsafe.Pointer(yymsp + 8))+44) } } } goto _346 _203: ; /* expr ::= expr COLLATE ID|STRING */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3ExprAddCollateToken(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), yymsp+8, int32(1)) goto _346 _204: ; /* expr ::= CAST LP expr AS typetoken RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_CAST), yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, int32(1)) _sqlite3ExprAttachSubtrees(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), uintptr(0)) goto _346 _205: ; /* 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))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(4))*24+8, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _206: ; /* 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))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(7))*24+8, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8))) _sqlite3ExprAddFunctionOrderBy(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _207: ; /* 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))*24+8, 0) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _208: ; /* 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))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(5))*24+8, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8))) _sqlite3WindowAttach(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _209: ; /* 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))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(8))*24+8, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*24 + 8))) _sqlite3WindowAttach(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + 8))) _sqlite3ExprAddFunctionOrderBy(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _210: ; /* 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))*24+8, 0) _sqlite3WindowAttach(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))), *(*uintptr)(unsafe.Pointer(yymsp + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _211: ; /* term ::= CTIME_KW */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprFunction(tls, pParse, uintptr(0), yymsp+8, 0) *(*uintptr)(unsafe.Pointer(yymsp + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _212: ; /* expr ::= LP nexprlist COMMA expr RP */ pList = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_VECTOR), uintptr(0), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) != 0 { *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) + 32)) = pList if (*TExprList)(unsafe.Pointer(pList)).FnExpr != 0 { *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) + 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 _346 _213: ; /* expr ::= expr AND expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3ExprAnd(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _215: ; /* expr ::= expr OR expr */ _214: ; _216: ; _217: ; _218: ; _219: ; _220: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24))).Fmajor), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _221: ; /* likeop ::= NOT LIKE_KW|MATCH */ *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*TToken)(unsafe.Pointer(yymsp + 8)) *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)) |= uint32(0x80000000) /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/ goto _346 _222: ; /* expr ::= expr likeop expr */ bNot = int32(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)) & uint32(0x80000000)) *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)) &= uint32(0x7fffffff) pList1 = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 8))) pList1 = _sqlite3ExprListAppend(tls, pParse, pList1, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3ExprFunction(tls, pParse, pList1, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, 0) if bNot != 0 { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), uintptr(0)) } if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) != 0 { *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) + 4)) |= uint32(EP_InfixFunc) } goto _346 _223: ; /* expr ::= expr likeop expr ESCAPE expr */ bNot1 = int32(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8 + 8)) & uint32(0x80000000)) *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8 + 8)) &= uint32(0x7fffffff) pList2 = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) pList2 = _sqlite3ExprListAppend(tls, pParse, pList2, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8))) pList2 = _sqlite3ExprListAppend(tls, pParse, pList2, *(*uintptr)(unsafe.Pointer(yymsp + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3ExprFunction(tls, pParse, pList2, yymsp+uintptr(-libc.Int32FromInt32(3))*24+8, 0) if bNot1 != 0 { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), uintptr(0)) } if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) != 0 { *(*Tu32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) + 4)) |= uint32(EP_InfixFunc) } goto _346 _224: ; /* expr ::= expr ISNULL|NOTNULL */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), uintptr(0)) goto _346 _225: ; /* expr ::= expr NOT NULL */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_NOTNULL), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), uintptr(0)) goto _346 _226: ; /* expr ::= expr IS expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_IS), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) _binaryToUnaryIfNull(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), int32(TK_ISNULL)) goto _346 _227: ; /* expr ::= expr IS NOT expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_ISNOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) _binaryToUnaryIfNull(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), int32(TK_NOTNULL)) goto _346 _228: ; /* expr ::= expr IS NOT DISTINCT FROM expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_IS), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) _binaryToUnaryIfNull(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), int32(TK_ISNULL)) goto _346 _229: ; /* expr ::= expr IS DISTINCT FROM expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_ISNOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) _binaryToUnaryIfNull(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), int32(TK_NOTNULL)) goto _346 _231: ; /* expr ::= NOT expr */ _230: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24))).Fmajor), *(*uintptr)(unsafe.Pointer(yymsp + 8)), uintptr(0)) /*A-overwrites-B*/ goto _346 _232: ; /* expr ::= PLUS|MINUS expr */ p4 = *(*uintptr)(unsafe.Pointer(yymsp + 8)) op = uint8(int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24))).Fmajor) + (libc.Int32FromInt32(TK_UPLUS) - libc.Int32FromInt32(TK_PLUS))) if p4 != 0 && int32((*TExpr)(unsafe.Pointer(p4)).Fop) == int32(TK_UPLUS) { (*TExpr)(unsafe.Pointer(p4)).Fop = op *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = p4 } else { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(op), p4, uintptr(0)) /*A-overwrites-B*/ } goto _346 _233: ; /* expr ::= expr PTR expr */ pList3 = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) pList3 = _sqlite3ExprListAppend(tls, pParse, pList3, *(*uintptr)(unsafe.Pointer(yymsp + 8))) *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3ExprFunction(tls, pParse, pList3, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, 0) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _235: ; /* between_op ::= BETWEEN */ _234: ; *(*int32)(unsafe.Pointer(yymsp + 8)) = 0 goto _346 _236: ; /* expr ::= expr between_op expr AND expr */ pList4 = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) pList4 = _sqlite3ExprListAppend(tls, pParse, pList4, *(*uintptr)(unsafe.Pointer(yymsp + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_BETWEEN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) != 0 { *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) + 32)) = pList4 } else { _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, pList4) } if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) != 0 { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), uintptr(0)) } goto _346 _237: ; /* expr ::= expr in_op LP exprlist RP */ if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) == 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))*24 + 8))) if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) != 0 { v348 = __ccgo_ts + 8359 } else { v348 = __ccgo_ts + 8364 } *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3Expr(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_STRING), v348) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) != 0 { _sqlite3ExprIdToTrueFalse(tls, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8))) } } else { pRHS = (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) + 8))).FpExpr if (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)))).FnExpr == int32(1) && _sqlite3ExprIsConstant(tls, pParse, pRHS) != 0 && int32((*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)))).Fop) != int32(TK_VECTOR) { (*(*TExprList_item)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) + 8))).FpExpr = uintptr(0) _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) pRHS = _sqlite3PExpr(tls, pParse, int32(TK_UPLUS), pRHS, uintptr(0)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_EQ), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), pRHS) } else { if (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)))).FnExpr == int32(1) && int32((*TExpr)(unsafe.Pointer(pRHS)).Fop) == int32(TK_SELECT) { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_IN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), uintptr(0)) _sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(pRHS + 32))) *(*uintptr)(unsafe.Pointer(pRHS + 32)) = uintptr(0) _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) } else { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_IN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) == uintptr(0) { _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) } else { if int32((*TExpr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)))).FpLeft)).Fop) == int32(TK_VECTOR) { nExpr = (*TExprList)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)))).FpLeft + 32)))).FnExpr pSelectRHS = _sqlite3ExprListToValues(tls, pParse, nExpr, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) if pSelectRHS != 0 { _parserDoubleLinkSelect(tls, pParse, pSelectRHS) _sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), pSelectRHS) } } else { *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) + 32)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) _sqlite3ExprSetHeightAndFlags(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8))) } } } } if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) != 0 { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), uintptr(0)) } } goto _346 _238: ; /* expr ::= LP select RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_SELECT), uintptr(0), uintptr(0)) _sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _239: ; /* expr ::= expr in_op LP select RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_IN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), uintptr(0)) _sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) != 0 { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), uintptr(0)) } goto _346 _240: ; /* expr ::= expr in_op nm dbnm paren_exprlist */ pSrc = _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8) pSelect = _sqlite3SelectNew(tls, pParse, uintptr(0), pSrc, uintptr(0), uintptr(0), uintptr(0), uintptr(0), uint32(0), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + 8)) != 0 { if pSelect != 0 { v349 = pSrc } else { v349 = uintptr(0) } _sqlite3SrcListFuncArgs(tls, pParse, v349, *(*uintptr)(unsafe.Pointer(yymsp + 8))) } *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_IN), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), uintptr(0)) _sqlite3PExprAddSelect(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), pSelect) if *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) != 0 { *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_NOT), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), uintptr(0)) } goto _346 _241: ; /* expr ::= EXISTS LP select RP */ v350 = _sqlite3PExpr(tls, pParse, int32(TK_EXISTS), uintptr(0), uintptr(0)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = v350 p5 = v350 _sqlite3PExprAddSelect(tls, pParse, p5, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _242: ; /* expr ::= CASE case_operand case_exprlist case_else END */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_CASE), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) != 0 { if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) != 0 { v351 = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) } else { v351 = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) } *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) + 32)) = v351 _sqlite3ExprSetHeightAndFlags(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8))) } else { _sqlite3ExprListDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) } goto _346 _243: ; /* case_exprlist ::= case_exprlist WHEN expr THEN expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _244: ; /* case_exprlist ::= WHEN expr THEN expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _245: ; /* nexprlist ::= nexprlist COMMA expr */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3ExprListAppend(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _246: ; /* nexprlist ::= expr */ *(*uintptr)(unsafe.Pointer(yymsp + 8)) = _sqlite3ExprListAppend(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 8))) /*A-overwrites-Y*/ goto _346 _248: ; /* paren_exprlist ::= LP exprlist RP */ _247: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) goto _346 _249: ; /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */ _sqlite3CreateIndex(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(7))*24+8, yymsp+uintptr(-libc.Int32FromInt32(6))*24+8, _sqlite3SrcListAppend(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(4))*24+8, uintptr(0)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(10))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(11))*24+8, *(*uintptr)(unsafe.Pointer(yymsp + 8)), SQLITE_SO_ASC, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*24 + 8)), 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))*24+8) } goto _346 _251: ; /* uniqueflag ::= UNIQUE */ _250: ; *(*int32)(unsafe.Pointer(yymsp + 8)) = int32(OE_Abort) goto _346 _252: ; /* uniqueflag ::= */ *(*int32)(unsafe.Pointer(yymsp + 1*24 + 8)) = OE_None goto _346 _253: ; /* eidlist ::= eidlist COMMA nm collate sortorder */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _parserAddExprIdListTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + 8))) goto _346 _254: ; /* eidlist ::= nm collate sortorder */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _parserAddExprIdListTerm(tls, pParse, uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + 8))) /*A-overwrites-Y*/ goto _346 _255: ; /* cmd ::= DROP INDEX ifexists fullname */ _sqlite3DropIndex(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _256: ; /* cmd ::= VACUUM vinto */ _sqlite3Vacuum(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _257: ; /* cmd ::= VACUUM nm vinto */ _sqlite3Vacuum(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _258: ; /* cmd ::= PRAGMA nm dbnm */ _sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, yymsp+8, uintptr(0), 0) goto _346 _259: ; /* cmd ::= PRAGMA nm dbnm EQ nmnum */ _sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*24+8, yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, yymsp+8, 0) goto _346 _260: ; /* cmd ::= PRAGMA nm dbnm LP nmnum RP */ _sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(4))*24+8, yymsp+uintptr(-libc.Int32FromInt32(3))*24+8, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, 0) goto _346 _261: ; /* cmd ::= PRAGMA nm dbnm EQ minus_num */ _sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*24+8, yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, yymsp+8, int32(1)) goto _346 _262: ; /* cmd ::= PRAGMA nm dbnm LP minus_num RP */ _sqlite3Pragma(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(4))*24+8, yymsp+uintptr(-libc.Int32FromInt32(3))*24+8, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, int32(1)) goto _346 _263: ; /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */ (*(*TToken)(unsafe.Pointer(bp + 104))).Fz = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) (*(*TToken)(unsafe.Pointer(bp + 104))).Fn = uint32(int32(int64(*(*uintptr)(unsafe.Pointer(yymsp + 8)))-int64(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8))))) + *(*uint32)(unsafe.Pointer(yymsp + 8 + 8)) _sqlite3FinishTrigger(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), bp+104) goto _346 _264: ; /* 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))*24+8, yymsp+uintptr(-libc.Int32FromInt32(6))*24+8, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(10))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*24 + 8))) if *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*24 + 8 + 8)) == uint32(0) { v352 = *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*24 + 8)) } else { v352 = *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*24 + 8)) } *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(10))*24 + 8)) = v352 /*A-overwrites-T*/ goto _346 _265: ; /* trigger_time ::= BEFORE|AFTER */ *(*int32)(unsafe.Pointer(yymsp + 8)) = int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor) /*A-overwrites-X*/ goto _346 _266: ; /* trigger_time ::= INSTEAD OF */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = int32(TK_INSTEAD) goto _346 _267: ; /* trigger_time ::= */ *(*int32)(unsafe.Pointer(yymsp + 1*24 + 8)) = int32(TK_BEFORE) goto _346 _269: ; /* trigger_event ::= DELETE|INSERT */ _268: ; *(*int32)(unsafe.Pointer(yymsp + 8)) = int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor) /*A-overwrites-X*/ *(*uintptr)(unsafe.Pointer(yymsp + 8 + 8)) = uintptr(0) goto _346 _270: ; /* trigger_event ::= UPDATE OF idlist */ *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = int32(TK_UPDATE) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + 8)) goto _346 _272: ; /* when_clause ::= */ _271: ; *(*uintptr)(unsafe.Pointer(yymsp + 1*24 + 8)) = uintptr(0) goto _346 _274: ; /* when_clause ::= WHEN expr */ _273: ; *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + 8)) goto _346 _275: ; /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */ (*TTriggerStep)(unsafe.Pointer((*TTriggerStep)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)))).FpLast)).FpNext = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) (*TTriggerStep)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)))).FpLast = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) goto _346 _276: ; /* trigger_cmd_list ::= trigger_cmd SEMI */ (*TTriggerStep)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)))).FpLast = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) goto _346 _277: ; /* trnm ::= nm DOT nm */ *(*TToken)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*TToken)(unsafe.Pointer(yymsp + 8)) _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23936, 0) goto _346 _278: ; /* tridxby ::= INDEXED BY nm */ _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+24031, 0) goto _346 _279: ; /* tridxby ::= NOT INDEXED */ _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+24115, 0) goto _346 _280: ; /* 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))*24+8, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), uint8(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*24 + 8))), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(8))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _281: ; /* 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))*24+8, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), uint8(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(6))*24 + 8))), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) /*yylhsminor.yy427-overwrites-yymsp[-6].minor.yy144*/ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(7))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _282: ; /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3TriggerDeleteStep(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*24+8, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _283: ; /* 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))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) /*yylhsminor.yy427-overwrites-yymsp[-1].minor.yy555*/ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _284: ; /* expr ::= RAISE LP IGNORE RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = _sqlite3PExpr(tls, pParse, int32(TK_RAISE), uintptr(0), uintptr(0)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) != 0 { (*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)))).FaffExpr = int8(OE_Ignore) } goto _346 _285: ; /* expr ::= RAISE LP raisetype COMMA nm RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = _sqlite3ExprAlloc(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, int32(TK_RAISE), yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, int32(1)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) != 0 { (*TExpr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)))).FaffExpr = int8(*(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8))) } goto _346 _286: ; /* raisetype ::= ROLLBACK */ *(*int32)(unsafe.Pointer(yymsp + 8)) = int32(OE_Rollback) goto _346 _287: ; /* raisetype ::= FAIL */ *(*int32)(unsafe.Pointer(yymsp + 8)) = int32(OE_Fail) goto _346 _288: ; /* cmd ::= DROP TRIGGER ifexists fullname */ _sqlite3DropTrigger(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _346 _289: ; /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */ _sqlite3Attach(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _290: ; /* cmd ::= DETACH database_kw_opt expr */ _sqlite3Detach(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _291: ; /* cmd ::= REINDEX */ _sqlite3Reindex(tls, pParse, uintptr(0), uintptr(0)) goto _346 _292: ; /* cmd ::= REINDEX nm dbnm */ _sqlite3Reindex(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, yymsp+8) goto _346 _293: ; /* cmd ::= ANALYZE */ _sqlite3Analyze(tls, pParse, uintptr(0), uintptr(0)) goto _346 _294: ; /* cmd ::= ANALYZE nm dbnm */ _sqlite3Analyze(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, yymsp+8) goto _346 _295: ; /* cmd ::= ALTER TABLE fullname RENAME TO nm */ _sqlite3AlterRenameTable(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), yymsp+8) goto _346 _296: ; /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */ *(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)) = uint32(int32(int64((*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fz)-int64(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))))) + (*TParse)(unsafe.Pointer(pParse)).FsLastToken.Fn _sqlite3AlterFinishAddColumn(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(1))*24+8) goto _346 _297: ; /* cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */ _sqlite3AlterDropColumn(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), yymsp+8) goto _346 _298: ; /* add_column_fullname ::= fullname */ _disableLookaside(tls, pParse) _sqlite3AlterBeginAddColumn(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _299: ; /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */ _sqlite3AlterRenameColumn(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, yymsp+8) goto _346 _300: ; /* cmd ::= create_vtab */ _sqlite3VtabFinishParse(tls, pParse, uintptr(0)) goto _346 _301: ; /* cmd ::= create_vtab LP vtabarglist RP */ _sqlite3VtabFinishParse(tls, pParse, yymsp+8) goto _346 _302: ; /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */ _sqlite3VtabBeginParse(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(3))*24+8, yymsp+uintptr(-libc.Int32FromInt32(2))*24+8, yymsp+8, *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8))) goto _346 _303: ; /* vtabarg ::= */ _sqlite3VtabArgInit(tls, pParse) goto _346 _305: ; /* vtabargtoken ::= ANY */ _304: ; _306: ; _sqlite3VtabArgExtend(tls, pParse, yymsp+8) goto _346 _308: ; /* with ::= WITH wqlist */ _307: ; _sqlite3WithPush(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), uint8(1)) goto _346 _309: ; /* wqas ::= AS */ *(*Tu8)(unsafe.Pointer(yymsp + 8)) = uint8(M10d_Any) goto _346 _310: ; /* wqas ::= AS MATERIALIZED */ *(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = uint8(M10d_Yes) goto _346 _311: ; /* wqas ::= AS NOT MATERIALIZED */ *(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = uint8(M10d_No) goto _346 _312: ; /* wqitem ::= withnm eidlist_opt wqas LP select RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = _sqlite3CteNew(tls, pParse, yymsp+uintptr(-libc.Int32FromInt32(5))*24+8, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8))) /*A-overwrites-X*/ goto _346 _313: ; /* withnm ::= nm */ (*TParse)(unsafe.Pointer(pParse)).FbHasWith = uint8(1) goto _346 _314: ; /* wqlist ::= wqitem */ *(*uintptr)(unsafe.Pointer(yymsp + 8)) = _sqlite3WithAdd(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + 8))) /*A-overwrites-X*/ goto _346 _315: ; /* wqlist ::= wqlist COMMA wqitem */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = _sqlite3WithAdd(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + 8))) goto _346 _316: ; /* windowdefn_list ::= windowdefn_list COMMA windowdefn */ _sqlite3WindowChain(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + 8)))).FpNextWin = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = *(*uintptr)(unsafe.Pointer(yymsp + 8)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _317: ; /* windowdefn ::= nm AS LP window RP */ if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) != 0 { (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)))).FzName = _sqlite3DbStrNDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)), uint64(*(*uint32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8 + 8)))) } *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _318: ; /* window ::= PARTITION BY nexprlist orderby_opt frame_opt */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), uintptr(0)) goto _346 _319: ; /* window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(5))*24+8) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _320: ; /* window ::= ORDER BY sortlist frame_opt */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), uintptr(0)) goto _346 _321: ; /* window ::= nm ORDER BY sortlist frame_opt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), uintptr(0), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), yymsp+uintptr(-libc.Int32FromInt32(4))*24+8) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _322: ; /* window ::= nm frame_opt */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3WindowAssemble(tls, pParse, *(*uintptr)(unsafe.Pointer(yymsp + 8)), uintptr(0), uintptr(0), yymsp+uintptr(-libc.Int32FromInt32(1))*24+8) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _323: ; /* frame_opt ::= */ *(*uintptr)(unsafe.Pointer(yymsp + 1*24 + 8)) = _sqlite3WindowAlloc(tls, pParse, 0, int32(TK_UNBOUNDED), uintptr(0), int32(TK_CURRENT), uintptr(0), uint8(0)) goto _346 _324: ; /* 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))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)), int32(TK_CURRENT), uintptr(0), *(*Tu8)(unsafe.Pointer(yymsp + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _325: ; /* 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))*24 + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8 + 8)), *(*int32)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8 + 8)), *(*Tu8)(unsafe.Pointer(yymsp + 8))) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(5))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _327: ; /* frame_bound_s ::= frame_bound */ _326: ; *(*TFrameBound)(unsafe.Pointer(bp)) = *(*TFrameBound)(unsafe.Pointer(yymsp + 8)) *(*TFrameBound)(unsafe.Pointer(yymsp + 8)) = *(*TFrameBound)(unsafe.Pointer(bp)) goto _346 _329: ; /* frame_bound_s ::= UNBOUNDED PRECEDING */ _328: ; _330: ; (*(*TFrameBound)(unsafe.Pointer(bp))).FeType = int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24))).Fmajor) (*(*TFrameBound)(unsafe.Pointer(bp))).FpExpr = uintptr(0) *(*TFrameBound)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*TFrameBound)(unsafe.Pointer(bp)) goto _346 _331: ; /* 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))*24 + 8)) *(*TFrameBound)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*TFrameBound)(unsafe.Pointer(bp)) goto _346 _332: ; /* frame_exclude_opt ::= */ *(*Tu8)(unsafe.Pointer(yymsp + 1*24 + 8)) = uint8(0) goto _346 _333: ; /* frame_exclude_opt ::= EXCLUDE frame_exclude */ *(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*Tu8)(unsafe.Pointer(yymsp + 8)) goto _346 _335: ; /* frame_exclude ::= NO OTHERS */ _334: ; *(*Tu8)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = uint8((*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24))).Fmajor) /*A-overwrites-X*/ goto _346 _336: ; /* frame_exclude ::= GROUP|TIES */ *(*Tu8)(unsafe.Pointer(yymsp + 8)) = uint8((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor) /*A-overwrites-X*/ goto _346 _337: ; /* window_clause ::= WINDOW windowdefn_list */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + 8)) goto _346 _338: ; /* filter_over ::= filter_clause over_clause */ if *(*uintptr)(unsafe.Pointer(yymsp + 8)) != 0 { (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + 8)))).FpFilter = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) } else { _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) } *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = *(*uintptr)(unsafe.Pointer(yymsp + 8)) *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _339: ; /* filter_over ::= over_clause */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = *(*uintptr)(unsafe.Pointer(yymsp + 8)) *(*uintptr)(unsafe.Pointer(yymsp + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _340: ; /* filter_over ::= filter_clause */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(144)) 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 + 8)) } else { _sqlite3ExprDelete(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + 8))) } *(*uintptr)(unsafe.Pointer(yymsp + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _341: ; /* over_clause ::= OVER LP window RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) goto _346 _342: ; /* over_clause ::= OVER nm */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = _sqlite3DbMallocZero(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, uint64(144)) if *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) != 0 { (*TWindow)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)))).FzName = _sqlite3DbStrNDup(tls, (*TParse)(unsafe.Pointer(pParse)).Fdb, *(*uintptr)(unsafe.Pointer(yymsp + 8)), uint64(*(*uint32)(unsafe.Pointer(yymsp + 8 + 8)))) } goto _346 _343: ; /* filter_clause ::= FILTER LP WHERE expr RP */ *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = *(*uintptr)(unsafe.Pointer(yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) goto _346 _344: ; /* term ::= QNUMBER */ *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) = _tokenExpr(tls, pParse, int32((*(*TyyStackEntry)(unsafe.Pointer(yymsp))).Fmajor), *(*TToken)(unsafe.Pointer(yymsp + 8))) _sqlite3DequoteNumber(tls, pParse, *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp))))) *(*uintptr)(unsafe.Pointer(yymsp + 8)) = *(*uintptr)(unsafe.Pointer(&*(*TYYMINORTYPE)(unsafe.Pointer(bp)))) goto _346 _345: ; goto _346 /********** End reduce actions ************************************************/ _346: ; yygoto = int32(_yyRuleInfoLhs[yyruleno]) yysize = int32(_yyRuleInfoNRhs[yyruleno]) yyact = _yy_find_reduce_action(tls, (*(*TyyStackEntry)(unsafe.Pointer(yymsp + uintptr(yysize)*24))).Fstateno, uint16(yygoto)) /* There are no SHIFTREDUCE actions on nonterminals because the table ** generator has simplified them to pure REDUCE actions. */ /* It is not possible for a REDUCE to be followed by an error */ yymsp += uintptr(yysize+int32(1)) * 24 (*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 *(*int8)(unsafe.Pointer((*(*TToken)(unsafe.Pointer(bp))).Fz)) != 0 { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+23912, libc.VaList(bp+24, bp)) } else { _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+24200, 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 /* 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 yyact = (*TyyStackEntry)(unsafe.Pointer((*TyyParser)(unsafe.Pointer(yypParser)).Fyytos)).Fstateno for int32(1) != 0 { /* Exit by "break" */ 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 { if _yyGrowStack(tls, yypParser) != 0 { _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 -= 24 _yy_accept(tls, yypParser) return } else { *(*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) { 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]int8{ 0: int8('R'), 1: int8('E'), 2: int8('I'), 3: int8('N'), 4: int8('D'), 5: int8('E'), 6: int8('X'), 7: int8('E'), 8: int8('D'), 9: int8('E'), 10: int8('S'), 11: int8('C'), 12: int8('A'), 13: int8('P'), 14: int8('E'), 15: int8('A'), 16: int8('C'), 17: int8('H'), 18: int8('E'), 19: int8('C'), 20: int8('K'), 21: int8('E'), 22: int8('Y'), 23: int8('B'), 24: int8('E'), 25: int8('F'), 26: int8('O'), 27: int8('R'), 28: int8('E'), 29: int8('I'), 30: int8('G'), 31: int8('N'), 32: int8('O'), 33: int8('R'), 34: int8('E'), 35: int8('G'), 36: int8('E'), 37: int8('X'), 38: int8('P'), 39: int8('L'), 40: int8('A'), 41: int8('I'), 42: int8('N'), 43: int8('S'), 44: int8('T'), 45: int8('E'), 46: int8('A'), 47: int8('D'), 48: int8('D'), 49: int8('A'), 50: int8('T'), 51: int8('A'), 52: int8('B'), 53: int8('A'), 54: int8('S'), 55: int8('E'), 56: int8('L'), 57: int8('E'), 58: int8('C'), 59: int8('T'), 60: int8('A'), 61: int8('B'), 62: int8('L'), 63: int8('E'), 64: int8('F'), 65: int8('T'), 66: int8('H'), 67: int8('E'), 68: int8('N'), 69: int8('D'), 70: int8('E'), 71: int8('F'), 72: int8('E'), 73: int8('R'), 74: int8('R'), 75: int8('A'), 76: int8('B'), 77: int8('L'), 78: int8('E'), 79: int8('L'), 80: int8('S'), 81: int8('E'), 82: int8('X'), 83: int8('C'), 84: int8('L'), 85: int8('U'), 86: int8('D'), 87: int8('E'), 88: int8('L'), 89: int8('E'), 90: int8('T'), 91: int8('E'), 92: int8('M'), 93: int8('P'), 94: int8('O'), 95: int8('R'), 96: int8('A'), 97: int8('R'), 98: int8('Y'), 99: int8('I'), 100: int8('S'), 101: int8('N'), 102: int8('U'), 103: int8('L'), 104: int8('L'), 105: int8('S'), 106: int8('A'), 107: int8('V'), 108: int8('E'), 109: int8('P'), 110: int8('O'), 111: int8('I'), 112: int8('N'), 113: int8('T'), 114: int8('E'), 115: int8('R'), 116: int8('S'), 117: int8('E'), 118: int8('C'), 119: int8('T'), 120: int8('I'), 121: int8('E'), 122: int8('S'), 123: int8('N'), 124: int8('O'), 125: int8('T'), 126: int8('N'), 127: int8('U'), 128: int8('L'), 129: int8('L'), 130: int8('I'), 131: int8('K'), 132: int8('E'), 133: int8('X'), 134: int8('C'), 135: int8('E'), 136: int8('P'), 137: int8('T'), 138: int8('R'), 139: int8('A'), 140: int8('N'), 141: int8('S'), 142: int8('A'), 143: int8('C'), 144: int8('T'), 145: int8('I'), 146: int8('O'), 147: int8('N'), 148: int8('A'), 149: int8('T'), 150: int8('U'), 151: int8('R'), 152: int8('A'), 153: int8('L'), 154: int8('T'), 155: int8('E'), 156: int8('R'), 157: int8('A'), 158: int8('I'), 159: int8('S'), 160: int8('E'), 161: int8('X'), 162: int8('C'), 163: int8('L'), 164: int8('U'), 165: int8('S'), 166: int8('I'), 167: int8('V'), 168: int8('E'), 169: int8('X'), 170: int8('I'), 171: int8('S'), 172: int8('T'), 173: int8('S'), 174: int8('C'), 175: int8('O'), 176: int8('N'), 177: int8('S'), 178: int8('T'), 179: int8('R'), 180: int8('A'), 181: int8('I'), 182: int8('N'), 183: int8('T'), 184: int8('O'), 185: int8('F'), 186: int8('F'), 187: int8('S'), 188: int8('E'), 189: int8('T'), 190: int8('R'), 191: int8('I'), 192: int8('G'), 193: int8('G'), 194: int8('E'), 195: int8('R'), 196: int8('A'), 197: int8('N'), 198: int8('G'), 199: int8('E'), 200: int8('N'), 201: int8('E'), 202: int8('R'), 203: int8('A'), 204: int8('T'), 205: int8('E'), 206: int8('D'), 207: int8('E'), 208: int8('T'), 209: int8('A'), 210: int8('C'), 211: int8('H'), 212: int8('A'), 213: int8('V'), 214: int8('I'), 215: int8('N'), 216: int8('G'), 217: int8('L'), 218: int8('O'), 219: int8('B'), 220: int8('E'), 221: int8('G'), 222: int8('I'), 223: int8('N'), 224: int8('N'), 225: int8('E'), 226: int8('R'), 227: int8('E'), 228: int8('F'), 229: int8('E'), 230: int8('R'), 231: int8('E'), 232: int8('N'), 233: int8('C'), 234: int8('E'), 235: int8('S'), 236: int8('U'), 237: int8('N'), 238: int8('I'), 239: int8('Q'), 240: int8('U'), 241: int8('E'), 242: int8('R'), 243: int8('Y'), 244: int8('W'), 245: int8('I'), 246: int8('T'), 247: int8('H'), 248: int8('O'), 249: int8('U'), 250: int8('T'), 251: int8('E'), 252: int8('R'), 253: int8('E'), 254: int8('L'), 255: int8('E'), 256: int8('A'), 257: int8('S'), 258: int8('E'), 259: int8('A'), 260: int8('T'), 261: int8('T'), 262: int8('A'), 263: int8('C'), 264: int8('H'), 265: int8('B'), 266: int8('E'), 267: int8('T'), 268: int8('W'), 269: int8('E'), 270: int8('E'), 271: int8('N'), 272: int8('O'), 273: int8('T'), 274: int8('H'), 275: int8('I'), 276: int8('N'), 277: int8('G'), 278: int8('R'), 279: int8('O'), 280: int8('U'), 281: int8('P'), 282: int8('S'), 283: int8('C'), 284: int8('A'), 285: int8('S'), 286: int8('C'), 287: int8('A'), 288: int8('D'), 289: int8('E'), 290: int8('F'), 291: int8('A'), 292: int8('U'), 293: int8('L'), 294: int8('T'), 295: int8('C'), 296: int8('A'), 297: int8('S'), 298: int8('E'), 299: int8('C'), 300: int8('O'), 301: int8('L'), 302: int8('L'), 303: int8('A'), 304: int8('T'), 305: int8('E'), 306: int8('C'), 307: int8('R'), 308: int8('E'), 309: int8('A'), 310: int8('T'), 311: int8('E'), 312: int8('C'), 313: int8('U'), 314: int8('R'), 315: int8('R'), 316: int8('E'), 317: int8('N'), 318: int8('T'), 319: int8('_'), 320: int8('D'), 321: int8('A'), 322: int8('T'), 323: int8('E'), 324: int8('I'), 325: int8('M'), 326: int8('M'), 327: int8('E'), 328: int8('D'), 329: int8('I'), 330: int8('A'), 331: int8('T'), 332: int8('E'), 333: int8('J'), 334: int8('O'), 335: int8('I'), 336: int8('N'), 337: int8('S'), 338: int8('E'), 339: int8('R'), 340: int8('T'), 341: int8('M'), 342: int8('A'), 343: int8('T'), 344: int8('C'), 345: int8('H'), 346: int8('P'), 347: int8('L'), 348: int8('A'), 349: int8('N'), 350: int8('A'), 351: int8('L'), 352: int8('Y'), 353: int8('Z'), 354: int8('E'), 355: int8('P'), 356: int8('R'), 357: int8('A'), 358: int8('G'), 359: int8('M'), 360: int8('A'), 361: int8('T'), 362: int8('E'), 363: int8('R'), 364: int8('I'), 365: int8('A'), 366: int8('L'), 367: int8('I'), 368: int8('Z'), 369: int8('E'), 370: int8('D'), 371: int8('E'), 372: int8('F'), 373: int8('E'), 374: int8('R'), 375: int8('R'), 376: int8('E'), 377: int8('D'), 378: int8('I'), 379: int8('S'), 380: int8('T'), 381: int8('I'), 382: int8('N'), 383: int8('C'), 384: int8('T'), 385: int8('U'), 386: int8('P'), 387: int8('D'), 388: int8('A'), 389: int8('T'), 390: int8('E'), 391: int8('V'), 392: int8('A'), 393: int8('L'), 394: int8('U'), 395: int8('E'), 396: int8('S'), 397: int8('V'), 398: int8('I'), 399: int8('R'), 400: int8('T'), 401: int8('U'), 402: int8('A'), 403: int8('L'), 404: int8('W'), 405: int8('A'), 406: int8('Y'), 407: int8('S'), 408: int8('W'), 409: int8('H'), 410: int8('E'), 411: int8('N'), 412: int8('W'), 413: int8('H'), 414: int8('E'), 415: int8('R'), 416: int8('E'), 417: int8('C'), 418: int8('U'), 419: int8('R'), 420: int8('S'), 421: int8('I'), 422: int8('V'), 423: int8('E'), 424: int8('A'), 425: int8('B'), 426: int8('O'), 427: int8('R'), 428: int8('T'), 429: int8('A'), 430: int8('F'), 431: int8('T'), 432: int8('E'), 433: int8('R'), 434: int8('E'), 435: int8('N'), 436: int8('A'), 437: int8('M'), 438: int8('E'), 439: int8('A'), 440: int8('N'), 441: int8('D'), 442: int8('R'), 443: int8('O'), 444: int8('P'), 445: int8('A'), 446: int8('R'), 447: int8('T'), 448: int8('I'), 449: int8('T'), 450: int8('I'), 451: int8('O'), 452: int8('N'), 453: int8('A'), 454: int8('U'), 455: int8('T'), 456: int8('O'), 457: int8('I'), 458: int8('N'), 459: int8('C'), 460: int8('R'), 461: int8('E'), 462: int8('M'), 463: int8('E'), 464: int8('N'), 465: int8('T'), 466: int8('C'), 467: int8('A'), 468: int8('S'), 469: int8('T'), 470: int8('C'), 471: int8('O'), 472: int8('L'), 473: int8('U'), 474: int8('M'), 475: int8('N'), 476: int8('C'), 477: int8('O'), 478: int8('M'), 479: int8('M'), 480: int8('I'), 481: int8('T'), 482: int8('C'), 483: int8('O'), 484: int8('N'), 485: int8('F'), 486: int8('L'), 487: int8('I'), 488: int8('C'), 489: int8('T'), 490: int8('C'), 491: int8('R'), 492: int8('O'), 493: int8('S'), 494: int8('S'), 495: int8('C'), 496: int8('U'), 497: int8('R'), 498: int8('R'), 499: int8('E'), 500: int8('N'), 501: int8('T'), 502: int8('_'), 503: int8('T'), 504: int8('I'), 505: int8('M'), 506: int8('E'), 507: int8('S'), 508: int8('T'), 509: int8('A'), 510: int8('M'), 511: int8('P'), 512: int8('R'), 513: int8('E'), 514: int8('C'), 515: int8('E'), 516: int8('D'), 517: int8('I'), 518: int8('N'), 519: int8('G'), 520: int8('F'), 521: int8('A'), 522: int8('I'), 523: int8('L'), 524: int8('A'), 525: int8('S'), 526: int8('T'), 527: int8('F'), 528: int8('I'), 529: int8('L'), 530: int8('T'), 531: int8('E'), 532: int8('R'), 533: int8('E'), 534: int8('P'), 535: int8('L'), 536: int8('A'), 537: int8('C'), 538: int8('E'), 539: int8('F'), 540: int8('I'), 541: int8('R'), 542: int8('S'), 543: int8('T'), 544: int8('F'), 545: int8('O'), 546: int8('L'), 547: int8('L'), 548: int8('O'), 549: int8('W'), 550: int8('I'), 551: int8('N'), 552: int8('G'), 553: int8('F'), 554: int8('R'), 555: int8('O'), 556: int8('M'), 557: int8('F'), 558: int8('U'), 559: int8('L'), 560: int8('L'), 561: int8('I'), 562: int8('M'), 563: int8('I'), 564: int8('T'), 565: int8('I'), 566: int8('F'), 567: int8('O'), 568: int8('R'), 569: int8('D'), 570: int8('E'), 571: int8('R'), 572: int8('E'), 573: int8('S'), 574: int8('T'), 575: int8('R'), 576: int8('I'), 577: int8('C'), 578: int8('T'), 579: int8('O'), 580: int8('T'), 581: int8('H'), 582: int8('E'), 583: int8('R'), 584: int8('S'), 585: int8('O'), 586: int8('V'), 587: int8('E'), 588: int8('R'), 589: int8('E'), 590: int8('T'), 591: int8('U'), 592: int8('R'), 593: int8('N'), 594: int8('I'), 595: int8('N'), 596: int8('G'), 597: int8('R'), 598: int8('I'), 599: int8('G'), 600: int8('H'), 601: int8('T'), 602: int8('R'), 603: int8('O'), 604: int8('L'), 605: int8('L'), 606: int8('B'), 607: int8('A'), 608: int8('C'), 609: int8('K'), 610: int8('R'), 611: int8('O'), 612: int8('W'), 613: int8('S'), 614: int8('U'), 615: int8('N'), 616: int8('B'), 617: int8('O'), 618: int8('U'), 619: int8('N'), 620: int8('D'), 621: int8('E'), 622: int8('D'), 623: int8('U'), 624: int8('N'), 625: int8('I'), 626: int8('O'), 627: int8('N'), 628: int8('U'), 629: int8('S'), 630: int8('I'), 631: int8('N'), 632: int8('G'), 633: int8('V'), 634: int8('A'), 635: int8('C'), 636: int8('U'), 637: int8('U'), 638: int8('M'), 639: int8('V'), 640: int8('I'), 641: int8('E'), 642: int8('W'), 643: int8('I'), 644: int8('N'), 645: int8('D'), 646: int8('O'), 647: int8('W'), 648: int8('B'), 649: int8('Y'), 650: int8('I'), 651: int8('N'), 652: int8('I'), 653: int8('T'), 654: int8('I'), 655: int8('A'), 656: int8('L'), 657: int8('L'), 658: int8('Y'), 659: int8('P'), 660: int8('R'), 661: int8('I'), 662: int8('M'), 663: int8('A'), 664: int8('R'), 665: int8('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 i = (int32(_sqlite3UpperToLower[uint8(*(*int8)(unsafe.Pointer(z)))])*int32(4) ^ int32(_sqlite3UpperToLower[uint8(*(*int8)(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(*(*int8)(unsafe.Pointer(z))) & ^libc.Int32FromInt32(0x20) != int32(*(*int8)(unsafe.Pointer(zKW))) { goto _1 } if int32(*(*int8)(unsafe.Pointer(z + 1))) & ^libc.Int32FromInt32(0x20) != int32(*(*int8)(unsafe.Pointer(zKW + 1))) { goto _1 } j = int32(2) for j < n && int32(*(*int8)(unsafe.Pointer(z + uintptr(j)))) & ^libc.Int32FromInt32(0x20) == int32(*(*int8)(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, v16, v18, v21, v22, v3, v5, v7, v8 int32 var v17, v6 bool _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = c, delim, i, n, v10, v16, v17, v18, v21, v22, 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) } /* If the next character is a digit, this is a floating point ** number that begins with ".". Fall thru into the next case */ 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(1) != 0) { break } if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x08) == 0 { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32('_') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_QNUMBER) } else { break } } goto _11 _11: ; i++ } } else { i = 0 for { if !(int32(1) != 0) { break } if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x04) == 0 { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32('_') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_QNUMBER) } else { break } } goto _12 _12: ; i++ } if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32('.') { if *(*int32)(unsafe.Pointer(tokenType)) == int32(TK_INTEGER) { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_FLOAT) } i++ for { if !(int32(1) != 0) { break } if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x04) == 0 { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32('_') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_QNUMBER) } else { break } } goto _13 _13: ; i++ } } 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) { if *(*int32)(unsafe.Pointer(tokenType)) == int32(TK_INTEGER) { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_FLOAT) } i += int32(2) for { if !(int32(1) != 0) { break } if int32(_sqlite3CtypeMap[*(*uint8)(unsafe.Pointer(z + uintptr(i)))])&int32(0x04) == 0 { if int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) == int32('_') { *(*int32)(unsafe.Pointer(tokenType)) = int32(TK_QNUMBER) } else { break } } goto _14 _14: ; i++ } } } 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 v17 = c != int32(']'); v17 { v16 = int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) c = v16 } if !(v17 && v16 != 0) { break } goto _15 _15: ; i++ } if c == int32(']') { v18 = int32(TK_ID) } else { v18 = int32(TK_ILLEGAL) } *(*int32)(unsafe.Pointer(tokenType)) = v18 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 _19 _19: ; 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 { v21 = int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) c = v21 if !(v21 != 0) { break } if int32(_sqlite3CtypeMap[uint8(c)])&int32(0x46) != 0 { n++ } else { if c == int32('(') && n > 0 { for { i++ goto _23 _23: ; v22 = int32(*(*uint8)(unsafe.Pointer(z + uintptr(i)))) c = v22 if !(v22 != 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 _20 _20: ; 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 _24 _24: ; 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 _25 _25: ; 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 } /* If it is not a BLOB literal, then it must be an ID, since no ** SQL keywords start with the letter 'x'. Fall through */ 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(2480) defer tls.Free(2480) var db, pEngine, pParentParse uintptr var lastTokenParsed, mxSqlLen, n, nErr int32 var _ /* sEngine at bp+8 */ TyyParser var _ /* tokenType at bp+0 */ int32 var _ /* x at bp+2440 */ 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 */ mxSqlLen = *(*int32)(unsafe.Pointer(db + 136 + 1*4)) if (*Tsqlite3)(unsafe.Pointer(db)).FnVdbeActive == 0 { libc.AtomicStorePInt32(db+432, libc.Int32FromInt32(0)) } (*TParse)(unsafe.Pointer(pParse)).Frc = SQLITE_OK (*TParse)(unsafe.Pointer(pParse)).FzTail = zSql pEngine = bp + 8 _sqlite3ParserInit(tls, pEngine, pParse) 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) { if libc.AtomicLoadPInt32(db+432) != 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(*(*int8)(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) { *(*int32)(unsafe.Pointer(bp)) = _analyzeWindowKeyword(tls, zSql+6) } else { if *(*int32)(unsafe.Pointer(bp)) == int32(TK_OVER) { *(*int32)(unsafe.Pointer(bp)) = _analyzeOverKeyword(tls, zSql+4, lastTokenParsed) } else { if *(*int32)(unsafe.Pointer(bp)) == int32(TK_FILTER) { *(*int32)(unsafe.Pointer(bp)) = _analyzeFilterKeyword(tls, zSql+6, lastTokenParsed) } else { if *(*int32)(unsafe.Pointer(bp)) != int32(TK_QNUMBER) { (*(*TToken)(unsafe.Pointer(bp + 2440))).Fz = zSql (*(*TToken)(unsafe.Pointer(bp + 2440))).Fn = uint32(n) _sqlite3ErrorMsg(tls, pParse, __ccgo_ts+24217, libc.VaList(bp+2464, bp+2440)) 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) if (*TParse)(unsafe.Pointer(pParse)).Frc != SQLITE_OK { break } } _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+3863, libc.VaList(bp+2464, _sqlite3ErrStr(tls, (*TParse)(unsafe.Pointer(pParse)).Frc))) } Xsqlite3_log(tls, (*TParse)(unsafe.Pointer(pParse)).Frc, __ccgo_ts+24242, libc.VaList(bp+2464, (*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 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 *(*int8)(unsafe.Pointer(zSql)) != 0 { switch int32(*(*int8)(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(*(*int8)(unsafe.Pointer(zSql + 1))) != int32('*') { token = uint8(tkOTHER) break } zSql += uintptr(2) for *(*int8)(unsafe.Pointer(zSql)) != 0 && (int32(*(*int8)(unsafe.Pointer(zSql))) != int32('*') || int32(*(*int8)(unsafe.Pointer(zSql + 1))) != int32('/')) { zSql++ } if int32(*(*int8)(unsafe.Pointer(zSql))) == 0 { return 0 } zSql++ token = uint8(tkWS) case int32('-'): /* SQL-style comments from "--" to end of line */ if int32(*(*int8)(unsafe.Pointer(zSql + 1))) != int32('-') { token = uint8(tkOTHER) break } for *(*int8)(unsafe.Pointer(zSql)) != 0 && int32(*(*int8)(unsafe.Pointer(zSql))) != int32('\n') { zSql++ } if int32(*(*int8)(unsafe.Pointer(zSql))) == 0 { return libc.BoolInt32(int32(state) == int32(1)) } token = uint8(tkWS) case int32('['): /* Microsoft-style identifiers in [...] */ zSql++ for *(*int8)(unsafe.Pointer(zSql)) != 0 && int32(*(*int8)(unsafe.Pointer(zSql))) != int32(']') { zSql++ } if int32(*(*int8)(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(*(*int8)(unsafe.Pointer(zSql))) zSql++ for *(*int8)(unsafe.Pointer(zSql)) != 0 && int32(*(*int8)(unsafe.Pointer(zSql))) != c { zSql++ } if int32(*(*int8)(unsafe.Pointer(zSql))) == 0 { return 0 } token = uint8(tkOTHER) default: if int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zSql)))])&int32(0x46) != 0 { nId = int32(1) for { if !(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zSql + uintptr(nId))))])&int32(0x46) != 0) { break } goto _1 _1: ; nId++ } switch int32(*(*int8)(unsafe.Pointer(zSql))) { case int32('c'): fallthrough case int32('C'): if nId == int32(6) && Xsqlite3_strnicmp(tls, zSql, __ccgo_ts+24253, 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+20906, int32(7)) == 0 { token = uint8(tkTRIGGER) } else { if nId == int32(4) && Xsqlite3_strnicmp(tls, zSql, __ccgo_ts+24260, int32(4)) == 0 { token = uint8(tkTEMP) } else { if nId == int32(9) && Xsqlite3_strnicmp(tls, zSql, __ccgo_ts+24265, 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+24275, int32(3)) == 0 { token = uint8(tkEND) } else { if nId == int32(7) && Xsqlite3_strnicmp(tls, zSql, __ccgo_ts+24279, 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, 8)) = __ccgo_fp(_sqlite3RtreeInit) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_sqlite3DbstatRegister) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_sqlite3TestExtInit) *(*uintptr)(unsafe.Add(p, 32)) = __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 uint64(8) <= uint64(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 */ /* 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))+340) != 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))+340) == 0 && _sqlite3Config.FinProgress == 0 { _sqlite3Config.FinProgress = int32(1) libc.X__builtin___memset_chk(tls, uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)), 0, uint64(184), libc.X__builtin_object_size(tls, uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)), 0)) _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))+340, 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 { 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))+340) != 0 { Xsqlite3_os_end(tls) Xsqlite3_reset_auto_extension(tls) libc.AtomicStorePInt32(uintptr(unsafe.Pointer(&_sqlite3Config))+340, 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))+340) != 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(8) { 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 { 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++ } (*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 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)*32))).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)*32))).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.Uint64FromInt64(144)/libc.Uint64FromInt64(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 + 48)) |= uint64(_aFlagOp[i].Fmask) } else { if onoff == 0 { *(*Tu64)(unsafe.Pointer(db + 48)) &= ^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. */ rc = libc.Xmemcmp(tls, pKey1, pKey2, uint64(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) { 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 pDestructor = *(*uintptr)(unsafe.Pointer(p + 64)) 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)*32))).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 + 576)).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 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)*32))).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(181034)) } 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+24287, 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 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)*32))).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)*32 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*32))).FpSchema != 0 { _sqlite3SchemaClear(tls, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer(db)).FaDb + 1*32))).FpSchema) } _sqlite3VtabUnlockList(tls, db) /* Free up the array of auxiliary databases */ _sqlite3CollapseDatabaseArray(tls, db) /* 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 + 624)).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+624) i = (*THash)(unsafe.Pointer(db + 648)).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)*40))).FxDel != 0 { (*(*func(*libc.TLS, uintptr))(unsafe.Pointer(&struct{ uintptr }{(*(*TCollSeq)(unsafe.Pointer(pColl + uintptr(j)*40))).FxDel})))(tls, (*(*TCollSeq)(unsafe.Pointer(pColl + uintptr(j)*40))).FpUser) } goto _4 _4: ; j++ } _sqlite3DbFree(tls, db, pColl) goto _3 _3: ; i = (*THashElem)(unsafe.Pointer(i)).Fnext } _sqlite3HashClear(tls, db+648) i = (*THash)(unsafe.Pointer(db + 576)).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+576) _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*32))).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) 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 _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)*32))).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 + 48)) &= ^(libc.Uint64FromInt32(SQLITE_DeferFKs) | uint64(libc.Int32FromInt32(0x00002))<= 0 && rc < int32(libc.Uint64FromInt64(232)/libc.Uint64FromInt64(8)) && _aMsg[rc] != uintptr(0) { zErr = _aMsg[rc] } break } return zErr } var _aMsg = [29]uintptr{ 0: __ccgo_ts + 24355, 1: __ccgo_ts + 24368, 3: __ccgo_ts + 24384, 4: __ccgo_ts + 24409, 5: __ccgo_ts + 24423, 6: __ccgo_ts + 24442, 7: __ccgo_ts + 1634, 8: __ccgo_ts + 24467, 9: __ccgo_ts + 24504, 10: __ccgo_ts + 24516, 11: __ccgo_ts + 24531, 12: __ccgo_ts + 24564, 13: __ccgo_ts + 24582, 14: __ccgo_ts + 24607, 15: __ccgo_ts + 24636, 17: __ccgo_ts + 6433, 18: __ccgo_ts + 5835, 19: __ccgo_ts + 24653, 20: __ccgo_ts + 24671, 21: __ccgo_ts + 24689, 23: __ccgo_ts + 24723, 25: __ccgo_ts + 24744, 26: __ccgo_ts + 24770, 27: __ccgo_ts + 24793, 28: __ccgo_ts + 24814, } // 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 if count < int32(libc.Uint64FromInt64(12)/libc.Uint64FromInt64(1)) { delay = int32(_delays[count]) prior = int32(_totals[count]) } else { delay = int32(_delays[int32(libc.Uint64FromInt64(12)/libc.Uint64FromInt64(1))-libc.Int32FromInt32(1)]) prior = int32(_totals[int32(libc.Uint64FromInt64(12)/libc.Uint64FromInt64(1))-libc.Int32FromInt32(1)]) + delay*(count-(int32(libc.Uint64FromInt64(12)/libc.Uint64FromInt64(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.AtomicStorePInt32(db+432, libc.Int32FromInt32(1)) } // 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.AtomicLoadPInt32(db+432) != 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 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(181689)) } 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. */ 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+24911, 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)) 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 + 64)) = 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(24)) 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 { (*(*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) 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+24974, 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+3863, 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 + 248)) = 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 + 248)) = 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 + 110 *(*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 + 110 *(*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(int64(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(int64(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) } 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(182304)) } Xsqlite3_mutex_enter(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex) if zDb != 0 && *(*int8)(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+25025, 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.AtomicStorePInt32(db+432, libc.Int32FromInt32(0)) } 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 */ /* 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)*32))).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(182440))) } 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 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(182519)) } 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(182528)) } 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 /* 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(182576)) } /* 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+25046, 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+648, zName) j = 0 for { if !(j < int32(3)) { break } p = aColl + uintptr(j)*40 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_".) */ if limitId < 0 || limitId >= libc.Int32FromInt32(SQLITE_LIMIT_WORKER_THREADS)+libc.Int32FromInt32(1) { return -int32(1) } oldLimit = *(*int32)(unsafe.Pointer(db + 136 + 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 + 136 + 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 int8 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) if (flags&uint32(SQLITE_OPEN_URI) != 0 || *(*Tu8)(unsafe.Pointer(uintptr(unsafe.Pointer(&_sqlite3Config)) + 6)) != 0) && nUri >= int32(5) && libc.Xmemcmp(tls, zUri, __ccgo_ts+25114, uint64(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(*(*int8)(unsafe.Pointer(zUri + uintptr(iIn)))) == libc.Int32FromUint8('&')) goto _1 _1: ; iIn++ } zFile = Xsqlite3_malloc64(tls, nByte) if !(zFile != 0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, zFile, 0, uint64(4), libc.X__builtin_object_size(tls, zFile, 0)) /* 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(*(*int8)(unsafe.Pointer(zUri + 5))) == int32('/') && int32(*(*int8)(unsafe.Pointer(zUri + 6))) == int32('/') { iIn = int32(7) for *(*int8)(unsafe.Pointer(zUri + uintptr(iIn))) != 0 && int32(*(*int8)(unsafe.Pointer(zUri + uintptr(iIn)))) != int32('/') { iIn++ } if iIn != int32(7) && (iIn != int32(16) || libc.Xmemcmp(tls, __ccgo_ts+25120, zUri+7, uint64(9)) != 0) { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+25130, 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 = *(*int8)(unsafe.Pointer(zUri + uintptr(iIn))) c = v2 if !(int32(v2) != 0 && int32(c) != int32('#')) { break } iIn++ if int32(c) == int32('%') && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zUri + uintptr(iIn))))])&int32(0x08) != 0 && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zUri + uintptr(iIn+int32(1)))))])&int32(0x08) != 0 { v3 = iIn iIn++ octet = int32(_sqlite3HexToInt(tls, int32(*(*int8)(unsafe.Pointer(zUri + uintptr(v3)))))) << int32(4) v4 = iIn iIn++ octet += int32(_sqlite3HexToInt(tls, int32(*(*int8)(unsafe.Pointer(zUri + uintptr(v4)))))) 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 = *(*int8)(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 = int8(octet) } else { if eState == int32(1) && (int32(c) == int32('&') || int32(c) == int32('=')) { if int32(*(*int8)(unsafe.Pointer(zFile + uintptr(iOut-int32(1))))) == 0 { /* An empty option name. Ignore this option altogether. */ for *(*int8)(unsafe.Pointer(zUri + uintptr(iIn))) != 0 && int32(*(*int8)(unsafe.Pointer(zUri + uintptr(iIn)))) != int32('#') && int32(*(*int8)(unsafe.Pointer(zUri + uintptr(iIn-int32(1))))) != int32('&') { iIn++ } continue } if int32(c) == int32('&') { v6 = iOut iOut++ *(*int8)(unsafe.Pointer(zFile + uintptr(v6))) = int8('\000') } else { eState = int32(2) } c = 0 } else { if eState == 0 && int32(c) == int32('?') || eState == int32(2) && int32(c) == int32('&') { c = 0 eState = int32(1) } } } v7 = iOut iOut++ *(*int8)(unsafe.Pointer(zFile + uintptr(v7))) = c } if eState == int32(1) { v8 = iOut iOut++ *(*int8)(unsafe.Pointer(zFile + uintptr(v8))) = int8('\000') } libc.X__builtin___memset_chk(tls, zFile+uintptr(iOut), 0, uint64(4), libc.X__builtin_object_size(tls, zFile+uintptr(iOut), 0)) /* 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 *(*int8)(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+25158, zOpt, uint64(3)) == 0 { zVfs = zVal } else { aMode = uintptr(0) zModeType = uintptr(0) mask = 0 limit = 0 if nOpt == int32(5) && libc.Xmemcmp(tls, __ccgo_ts+25162, zOpt, uint64(5)) == 0 { mask = libc.Int32FromInt32(SQLITE_OPEN_SHAREDCACHE) | libc.Int32FromInt32(SQLITE_OPEN_PRIVATECACHE) aMode = uintptr(unsafe.Pointer(&_aCacheMode)) limit = mask zModeType = __ccgo_ts + 25162 } if nOpt == int32(4) && libc.Xmemcmp(tls, __ccgo_ts+25183, zOpt, uint64(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 + 3470 } if aMode != 0 { mode = 0 i = 0 for { if !((*(*struct { Fz uintptr Fmode int32 })(unsafe.Pointer(aMode + uintptr(i)*16))).Fz != 0) { break } z = (*(*struct { Fz uintptr Fmode int32 })(unsafe.Pointer(aMode + uintptr(i)*16))).Fz if nVal == _sqlite3Strlen30(tls, z) && 0 == libc.Xmemcmp(tls, zVal, z, uint64(nVal)) { mode = (*(*struct { Fz uintptr Fmode int32 })(unsafe.Pointer(aMode + uintptr(i)*16))).Fmode break } goto _9 _9: ; i++ } if mode == 0 { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+25198, 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+25218, 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.X__builtin___memset_chk(tls, zFile, 0, uint64(4), libc.X__builtin_object_size(tls, zFile, 0)) zFile += uintptr(4) if nUri != 0 { libc.X__builtin___memcpy_chk(tls, zFile, zUri, uint64(nUri), libc.X__builtin_object_size(tls, zFile, 0)) } libc.X__builtin___memset_chk(tls, zFile+uintptr(nUri), 0, uint64(4), libc.X__builtin_object_size(tls, zFile+uintptr(nUri), 0)) 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+25242, 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 + 25168, Fmode: int32(SQLITE_OPEN_SHAREDCACHE), }, 1: { Fz: __ccgo_ts + 25175, Fmode: int32(SQLITE_OPEN_PRIVATECACHE), }, 2: {}, } var _aOpenMode = [5]struct { Fz uintptr Fmode int32 }{ 0: { Fz: __ccgo_ts + 25188, Fmode: int32(SQLITE_OPEN_READONLY), }, 1: { Fz: __ccgo_ts + 25191, Fmode: int32(SQLITE_OPEN_READWRITE), }, 2: { Fz: __ccgo_ts + 25194, Fmode: libc.Int32FromInt32(SQLITE_OPEN_READWRITE) | libc.Int32FromInt32(SQLITE_OPEN_CREATE), }, 3: { Fz: __ccgo_ts + 18218, 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) && *(*int8)(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(48) defer tls.Free(48) *(*uint32)(unsafe.Pointer(bp)) = _flags var db, v2 uintptr var i, isThreadsafe, rc int32 var v1 uint32 var _ /* zErrMsg at bp+16 */ uintptr var _ /* zOpen at bp+8 */ uintptr _, _, _, _, _, _ = db, i, isThreadsafe, rc, v1, v2 /* True for threadsafe connections */ *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) /* Filename argument to pass to BtreeOpen() */ *(*uintptr)(unsafe.Pointer(bp + 16)) = 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(856)) 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 + 696 (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.FbDisable = uint32(1) (*Tsqlite3)(unsafe.Pointer(db)).Flookaside.Fsz = uint16(0) libc.X__builtin___memcpy_chk(tls, db+136, uintptr(unsafe.Pointer(&_aHardLimit)), uint64(48), libc.X__builtin_object_size(tls, db+136, 0)) *(*int32)(unsafe.Pointer(db + 136 + 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 + 48)) |= 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+648) _sqlite3HashInit(tls, db+576) /* 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+22891, uint8(SQLITE_UTF8), uintptr(0), __ccgo_fp(_nocaseCollatingFunc), uintptr(0)) _createCollation(tls, db, __ccgo_ts+25258, 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)) /* READONLY */ /* READWRITE */ /* READWRITE | CREATE */ if int32(1)<<(*(*uint32)(unsafe.Pointer(bp))&uint32(7))&int32(0x46) == 0 { rc = _sqlite3MisuseError(tls, int32(183248)) /* IMP: R-18321-05872 */ } else { rc = _sqlite3ParseUri(tls, zVfs, zFilename, bp, db, bp+8, bp+16) } if rc != SQLITE_OK { if rc == int32(SQLITE_NOMEM) { _sqlite3OomFault(tls, db) } if *(*uintptr)(unsafe.Pointer(bp + 16)) != 0 { v2 = __ccgo_ts + 3863 } else { v2 = uintptr(0) } _sqlite3ErrorWithMsg(tls, db, rc, v2, libc.VaList(bp+32, *(*uintptr)(unsafe.Pointer(bp + 16)))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) goto opendb_out } /* Open the backend database driver */ rc = _sqlite3BtreeOpen(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, *(*uintptr)(unsafe.Pointer(bp + 8)), db, (*Tsqlite3)(unsafe.Pointer(db)).FaDb+8, 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)*16 } 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 + 8))&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 + 1138 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+25336, libc.VaList(bp+16, zTableName, zColumnName)) rc = int32(SQLITE_ERROR) } if *(*uintptr)(unsafe.Pointer(bp)) != 0 { v3 = __ccgo_ts + 3863 } 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) fd = _sqlite3PagerFile(tls, pPager) 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) 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 + 48)) |= uint64(libc.Int32FromInt32(0x00008)) << libc.Int32FromInt32(32) } else { *(*Tu64)(unsafe.Pointer(db1 + 48)) &= ^(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 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 + 44)) ^= 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))+340) == 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+192+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(*(*int8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(1))))) != 0 || int32(*(*int8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(2))))) != 0 || int32(*(*int8)(unsafe.Pointer(zName + uintptr(-libc.Int32FromInt32(3))))) != 0 || int32(*(*int8)(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.X__builtin___memcpy_chk(tls, p, z, n+uint64(1), libc.X__builtin_object_size(tls, p, 0)) 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) + uint64(10)) i = 0 for { if !(i < nParam*int32(2)) { break } nByte = Tsqlite3_int64(uint64(nByte) + (libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(azParam + uintptr(i)*8))) + libc.Uint64FromInt32(1))) goto _1 _1: ; i++ } v2 = Xsqlite3_malloc64(tls, uint64(nByte)) p = v2 pResult = v2 if p == uintptr(0) { return uintptr(0) } libc.X__builtin___memset_chk(tls, p, 0, uint64(4), libc.X__builtin_object_size(tls, p, 0)) 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)*8))) goto _3 _3: ; i++ } v4 = p p++ *(*int8)(unsafe.Pointer(v4)) = 0 p = _appendText(tls, p, zJournal) p = _appendText(tls, p, zWal) v5 = p p++ *(*int8)(unsafe.Pointer(v5)) = 0 v6 = p p++ *(*int8)(unsafe.Pointer(v6)) = 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 && *(*int8)(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 *(*int8)(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 && *(*int8)(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)*32))).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)*32))).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)*32))).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)*32))).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)*32))).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+25364, 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)*8)), n) == 0 && _sqlite3IsIdChar(tls, uint8(*(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(azCompileOpt + uintptr(i)*8)) + 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)*8)) } 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 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)) + 848 } } // 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 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)) + 848 } (*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) { 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) if rc != 0 { v2 = __ccgo_ts + 25372 } 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(128) defer tls.Free(128) 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 { 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) if !(aDyn != 0) && nArg == int32(libc.Uint64FromInt64(128)/libc.Uint64FromInt64(8)) || aDyn != 0 && nArg == int32(uint64(_sqlite3MallocSize(tls, aDyn))/libc.Uint64FromInt64(8)) { /* The aArg[] array needs to grow. */ pNew = _sqlite3Malloc(tls, uint64(nArg)*uint64(8)*uint64(2)) if pNew != 0 { libc.X__builtin___memcpy_chk(tls, pNew, aArg, uint64(nArg)*uint64(8), libc.X__builtin_object_size(tls, pNew, 0)) 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)*8)) = (*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 + 848 } 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 + 1650, 1: __ccgo_ts + 8359, 2: __ccgo_ts + 8364, 3: __ccgo_ts + 6718, 4: __ccgo_ts + 6718, 5: __ccgo_ts + 6713, 6: __ccgo_ts + 6713, 7: __ccgo_ts + 8670, 8: __ccgo_ts + 8670, 9: __ccgo_ts + 8670, 10: __ccgo_ts + 8670, 11: __ccgo_ts + 25395, 12: __ccgo_ts + 25401, 13: __ccgo_ts + 1664, 14: __ccgo_ts + 1664, 15: __ccgo_ts + 1664, 16: __ccgo_ts + 1664, } // 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]int8{ 9: int8(1), 10: int8(1), 13: int8(1), 32: int8(1), } // C documentation // // /* // ** The set of all space characters recognized by jsonIsspace(). // ** Useful as the second argument to strspn(). // */ var _jsonSpaces = [5]int8{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]int8{ 32: int8(1), 33: int8(1), 35: int8(1), 36: int8(1), 37: int8(1), 38: int8(1), 40: int8(1), 41: int8(1), 42: int8(1), 43: int8(1), 44: int8(1), 45: int8(1), 46: int8(1), 47: int8(1), 48: int8(1), 49: int8(1), 50: int8(1), 51: int8(1), 52: int8(1), 53: int8(1), 54: int8(1), 55: int8(1), 56: int8(1), 57: int8(1), 58: int8(1), 59: int8(1), 60: int8(1), 61: int8(1), 62: int8(1), 63: int8(1), 64: int8(1), 65: int8(1), 66: int8(1), 67: int8(1), 68: int8(1), 69: int8(1), 70: int8(1), 71: int8(1), 72: int8(1), 73: int8(1), 74: int8(1), 75: int8(1), 76: int8(1), 77: int8(1), 78: int8(1), 79: int8(1), 80: int8(1), 81: int8(1), 82: int8(1), 83: int8(1), 84: int8(1), 85: int8(1), 86: int8(1), 87: int8(1), 88: int8(1), 89: int8(1), 90: int8(1), 91: int8(1), 93: int8(1), 94: int8(1), 95: int8(1), 96: int8(1), 97: int8(1), 98: int8(1), 99: int8(1), 100: int8(1), 101: int8(1), 102: int8(1), 103: int8(1), 104: int8(1), 105: int8(1), 106: int8(1), 107: int8(1), 108: int8(1), 109: int8(1), 110: int8(1), 111: int8(1), 112: int8(1), 113: int8(1), 114: int8(1), 115: int8(1), 116: int8(1), 117: int8(1), 118: int8(1), 119: int8(1), 120: int8(1), 121: int8(1), 122: int8(1), 123: int8(1), 124: int8(1), 125: int8(1), 126: int8(1), 127: int8(1), 128: int8(1), 129: int8(1), 130: int8(1), 131: int8(1), 132: int8(1), 133: int8(1), 134: int8(1), 135: int8(1), 136: int8(1), 137: int8(1), 138: int8(1), 139: int8(1), 140: int8(1), 141: int8(1), 142: int8(1), 143: int8(1), 144: int8(1), 145: int8(1), 146: int8(1), 147: int8(1), 148: int8(1), 149: int8(1), 150: int8(1), 151: int8(1), 152: int8(1), 153: int8(1), 154: int8(1), 155: int8(1), 156: int8(1), 157: int8(1), 158: int8(1), 159: int8(1), 160: int8(1), 161: int8(1), 162: int8(1), 163: int8(1), 164: int8(1), 165: int8(1), 166: int8(1), 167: int8(1), 168: int8(1), 169: int8(1), 170: int8(1), 171: int8(1), 172: int8(1), 173: int8(1), 174: int8(1), 175: int8(1), 176: int8(1), 177: int8(1), 178: int8(1), 179: int8(1), 180: int8(1), 181: int8(1), 182: int8(1), 183: int8(1), 184: int8(1), 185: int8(1), 186: int8(1), 187: int8(1), 188: int8(1), 189: int8(1), 190: int8(1), 191: int8(1), 192: int8(1), 193: int8(1), 194: int8(1), 195: int8(1), 196: int8(1), 197: int8(1), 198: int8(1), 199: int8(1), 200: int8(1), 201: int8(1), 202: int8(1), 203: int8(1), 204: int8(1), 205: int8(1), 206: int8(1), 207: int8(1), 208: int8(1), 209: int8(1), 210: int8(1), 211: int8(1), 212: int8(1), 213: int8(1), 214: int8(1), 215: int8(1), 216: int8(1), 217: int8(1), 218: int8(1), 219: int8(1), 220: int8(1), 221: int8(1), 222: int8(1), 223: int8(1), 224: int8(1), 225: int8(1), 226: int8(1), 227: int8(1), 228: int8(1), 229: int8(1), 230: int8(1), 231: int8(1), 232: int8(1), 233: int8(1), 234: int8(1), 235: int8(1), 236: int8(1), 237: int8(1), 238: int8(1), 239: int8(1), 240: int8(1), 241: int8(1), 242: int8(1), 243: int8(1), 244: int8(1), 245: int8(1), 246: int8(1), 247: int8(1), 248: int8(1), 249: int8(1), 250: int8(1), 251: int8(1), 252: int8(1), 253: int8(1), 254: int8(1), 255: int8(1), } // C documentation // // /* Objects */ type TJsonCache = struct { Fdb uintptr FnUsed int32 Fa [4]uintptr } type JsonCache = TJsonCache type TJsonString = struct { FpCtx uintptr FzBuf uintptr FnAlloc Tu64 FnUsed Tu64 FbStatic Tu8 FeErr Tu8 FzSpace [100]int8 } 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 { FpCtx uintptr FzBuf uintptr FnAlloc Tu64 FnUsed Tu64 FbStatic Tu8 FeErr Tu8 FzSpace [100]int8 } 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 + 16 + uintptr(i)*8))) 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 p = Xsqlite3_get_auxdata(tls, ctx, -int32(429938)) if p == uintptr(0) { db = Xsqlite3_context_db_handle(tls, ctx) p = _sqlite3DbMallocZero(tls, db, uint64(48)) 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 + 16))) libc.X__builtin___memmove_chk(tls, p+16, p+16+1*8, uint64(libc.Int32FromInt32(JSON_CACHE_SIZE)-libc.Int32FromInt32(1))*libc.Uint64FromInt64(8), libc.X__builtin_object_size(tls, p+16, 0)) (*TJsonCache)(unsafe.Pointer(p)).FnUsed = libc.Int32FromInt32(JSON_CACHE_SIZE) - libc.Int32FromInt32(1) } (*TJsonParse)(unsafe.Pointer(pParse)).FeEdit = uint8(0) (*TJsonParse)(unsafe.Pointer(pParse)).FnJPRef++ (*TJsonParse)(unsafe.Pointer(pParse)).FbReadOnly = uint8(1) *(*uintptr)(unsafe.Pointer(p + 16 + uintptr((*TJsonCache)(unsafe.Pointer(p)).FnUsed)*8)) = 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 + 16 + uintptr(i)*8)))).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 + 16 + uintptr(i)*8)))).FnJson != nJson { goto _2 } if libc.Xmemcmp(tls, (*TJsonParse)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 16 + uintptr(i)*8)))).FzJson, zJson, uint64(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 + 16 + uintptr(i)*8)) libc.X__builtin___memmove_chk(tls, p+16+uintptr(i)*8, p+16+uintptr(i+int32(1))*8, uint64((*TJsonCache)(unsafe.Pointer(p)).FnUsed-i-libc.Int32FromInt32(1))*uint64(8), libc.X__builtin_object_size(tls, p+16+uintptr(i)*8, 0)) *(*uintptr)(unsafe.Pointer(p + 16 + uintptr((*TJsonCache)(unsafe.Pointer(p)).FnUsed-int32(1))*8)) = tmp i = (*TJsonCache)(unsafe.Pointer(p)).FnUsed - int32(1) } return *(*uintptr)(unsafe.Pointer(p + 16 + uintptr(i)*8)) } 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 + 34 (*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 + 33 *(*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.X__builtin___memcpy_chk(tls, zNew, (*TJsonString)(unsafe.Pointer(p)).FzBuf, (*TJsonString)(unsafe.Pointer(p)).FnUsed, libc.X__builtin_object_size(tls, zNew, 0)) (*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 + 33 *(*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) { if _jsonStringGrow(tls, p, N) != 0 { return } libc.X__builtin___memcpy_chk(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), zIn, uint64(N), libc.X__builtin_object_size(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), 0)) *(*Tu64)(unsafe.Pointer(p + 24)) += 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.X__builtin___memcpy_chk(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), zIn, uint64(N), libc.X__builtin_object_size(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), 0)) *(*Tu64)(unsafe.Pointer(p + 24)) += uint64(N) } } func _jsonAppendRawNZ(tls *libc.TLS, p uintptr, zIn uintptr, N Tu32) { if uint64(N)+(*TJsonString)(unsafe.Pointer(p)).FnUsed >= (*TJsonString)(unsafe.Pointer(p)).FnAlloc { _jsonStringExpandAndAppend(tls, p, zIn, N) } else { libc.X__builtin___memcpy_chk(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), zIn, uint64(N), libc.X__builtin_object_size(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), 0)) *(*Tu64)(unsafe.Pointer(p + 24)) += 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 + 24)) += 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 int8) { var v1 Tu64 var v2 uintptr _, _ = v1, v2 if _jsonStringGrow(tls, p, uint32(1)) != 0 { return } v2 = p + 24 v1 = *(*Tu64)(unsafe.Pointer(v2)) *(*Tu64)(unsafe.Pointer(v2))++ *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v1))) = c } func _jsonAppendChar(tls *libc.TLS, p uintptr, c int8) { 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 + 24 v1 = *(*Tu64)(unsafe.Pointer(v2)) *(*Tu64)(unsafe.Pointer(v2))++ *(*int8)(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 { (*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, 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 int8 _ = c if (*TJsonString)(unsafe.Pointer(p)).FnUsed == uint64(0) { return } c = *(*int8)(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, int8(',')) } // C documentation // // /* c is a control character. Append the canonical JSON representation // ** of that control character to p. // ** // ** This routine assumes that the output buffer has already been enlarged // ** sufficiently to hold the worst-case encoding plus a nul terminator. // */ func _jsonAppendControlChar(tls *libc.TLS, p uintptr, c Tu8) { if _aSpecial[c] != 0 { *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed))) = int8('\\') *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(1)))) = _aSpecial[c] *(*Tu64)(unsafe.Pointer(p + 24)) += uint64(2) } else { *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed))) = int8('\\') *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(1)))) = int8('u') *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(2)))) = int8('0') *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(3)))) = int8('0') *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(4)))) = *(*int8)(unsafe.Pointer(__ccgo_ts + 25408 + uintptr(int32(c)>>int32(4)))) *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(5)))) = *(*int8)(unsafe.Pointer(__ccgo_ts + 25408 + uintptr(int32(c)&int32(0xf)))) *(*Tu64)(unsafe.Pointer(p + 24)) += uint64(6) } } var _aSpecial = [32]int8{ 8: int8('b'), 9: int8('t'), 10: int8('n'), 12: int8('f'), 13: int8('r'), } // 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, v2, v4, v6, v8 uintptr var v1, v3, v5, v7, v9 Tu64 _, _, _, _, _, _, _, _, _, _, _, _, _ = c, k, z, v1, v10, v2, v3, v4, 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 + 24 v1 = *(*Tu64)(unsafe.Pointer(v2)) *(*Tu64)(unsafe.Pointer(v2))++ *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v1))) = int8('"') 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.X__builtin___memcpy_chk(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), z, uint64(k), libc.X__builtin_object_size(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), 0)) *(*Tu64)(unsafe.Pointer(p + 24)) += uint64(k) } break } if k > uint32(0) { libc.X__builtin___memcpy_chk(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), z, uint64(k), libc.X__builtin_object_size(tls, (*TJsonString)(unsafe.Pointer(p)).FzBuf+uintptr((*TJsonString)(unsafe.Pointer(p)).FnUsed), 0)) *(*Tu64)(unsafe.Pointer(p + 24)) += uint64(k) z += uintptr(k) N -= k } c = *(*Tu8)(unsafe.Pointer(z)) if int32(c) == int32('"') || int32(c) == int32('\\') { if (*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(N)+uint64(3) > (*TJsonString)(unsafe.Pointer(p)).FnAlloc && _jsonStringGrow(tls, p, N+uint32(3)) != 0 { return } v4 = p + 24 v3 = *(*Tu64)(unsafe.Pointer(v4)) *(*Tu64)(unsafe.Pointer(v4))++ *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v3))) = int8('\\') v6 = p + 24 v5 = *(*Tu64)(unsafe.Pointer(v6)) *(*Tu64)(unsafe.Pointer(v6))++ *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v5))) = int8(c) } else { if int32(c) == int32('\'') { v8 = p + 24 v7 = *(*Tu64)(unsafe.Pointer(v8)) *(*Tu64)(unsafe.Pointer(v8))++ *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v7))) = int8(c) } else { if (*TJsonString)(unsafe.Pointer(p)).FnUsed+uint64(N)+uint64(7) > (*TJsonString)(unsafe.Pointer(p)).FnAlloc && _jsonStringGrow(tls, p, N+uint32(7)) != 0 { return } _jsonAppendControlChar(tls, p, c) } } z++ N-- } v10 = p + 24 v9 = *(*Tu64)(unsafe.Pointer(v10)) *(*Tu64)(unsafe.Pointer(v10))++ *(*int8)(unsafe.Pointer((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v9))) = int8('"') } // 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(96) defer tls.Free(96) 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+1650, uint32(4)) case int32(SQLITE_FLOAT): _jsonPrintf(tls, int32(100), p, __ccgo_ts+15764, libc.VaList(bp+80, 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.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 0)) (*(*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+25425, -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 if int32((*TJsonString)(unsafe.Pointer(p)).FeErr) == 0 { flags = int32(int64(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+25454, -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) { 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(*(*int8)(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 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 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.X__builtin___memcpy_chk(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob, aOld, uint64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob), libc.X__builtin_object_size(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob, 0)) 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 { v2 = pParse + 8 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 + 8 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 } 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 + 8)) += 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 + 8)) += uint32(5) } } } if aPayload != 0 { *(*Tu32)(unsafe.Pointer(pParse + 8)) += szPayload libc.X__builtin___memcpy_chk(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-szPayload), aPayload, uint64(szPayload), libc.X__builtin_object_size(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-szPayload), 0)) } } // 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.X__builtin___memmove_chk(tls, a+uintptr(int32(1)+delta), a+1, uint64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-(i+uint32(1))), libc.X__builtin_object_size(tls, a+uintptr(int32(1)+delta), 0)) } else { libc.X__builtin___memmove_chk(tls, a+1, a+uintptr(int32(1)-delta), uint64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-(i+uint32(1)-uint32(delta))), libc.X__builtin_object_size(tls, a+1, 0)) } (*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(*(*int8)(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(0x1f) { /* Control characters in JSON5 string literals are ok */ 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+25543, 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)) } 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)) } 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 int8 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(*(*int8)(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 + 44 *(*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(*(*int8)(unsafe.Pointer(z + uintptr(j))))])&int32(0x42) != 0 || int32(*(*int8)(unsafe.Pointer(z + uintptr(j)))) == int32('\\') && _jsonIs4HexB(tls, z+uintptr(j+uint32(1)), bp) != 0 { k = int32(j + uint32(1)) for int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(z + uintptr(k))))])&int32(0x46) != 0 && _json5Whitespace(tls, z+uintptr(k)) == 0 || int32(*(*int8)(unsafe.Pointer(z + uintptr(k)))) == int32('\\') && _jsonIs4HexB(tls, z+uintptr(k+int32(1)), bp) != 0 { k++ } _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(*(*int8)(unsafe.Pointer(z + uintptr(j)))) == int32(':') { j++ } else { if _jsonIsSpace[uint8(*(*int8)(unsafe.Pointer(z + uintptr(j))))] != 0 { /* strspn() is not helpful here */ for cond := true; cond; cond = _jsonIsSpace[uint8(*(*int8)(unsafe.Pointer(z + uintptr(j))))] != 0 { j++ } if int32(*(*int8)(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(*(*int8)(unsafe.Pointer(z + uintptr(j)))) == int32(',') { goto _42 } else { if int32(*(*int8)(unsafe.Pointer(z + uintptr(j)))) == int32('}') { break } else { if _jsonIsSpace[uint8(*(*int8)(unsafe.Pointer(z + uintptr(j))))] != 0 { j += uint32(1) + uint32(libc.Xstrspn(tls, z+uintptr(j+uint32(1)), uintptr(unsafe.Pointer(&_jsonSpaces)))) if int32(*(*int8)(unsafe.Pointer(z + uintptr(j)))) == int32(',') { goto _42 } else { if int32(*(*int8)(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 _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 + 44 *(*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(*(*int8)(unsafe.Pointer(z + uintptr(j)))) == int32(',') { goto _45 } else { if int32(*(*int8)(unsafe.Pointer(z + uintptr(j)))) == int32(']') { break } else { if _jsonIsSpace[uint8(*(*int8)(unsafe.Pointer(z + uintptr(j))))] != 0 { j += uint32(1) + uint32(libc.Xstrspn(tls, z+uintptr(j+uint32(1)), uintptr(unsafe.Pointer(&_jsonSpaces)))) if int32(*(*int8)(unsafe.Pointer(z + uintptr(j)))) == int32(',') { goto _45 } else { if int32(*(*int8)(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 = *(*int8)(unsafe.Pointer(z + uintptr(i))) j = i + uint32(1) for int32(1) != 0 { if _jsonIsOk[uint8(*(*int8)(unsafe.Pointer(z + uintptr(j))))] != 0 { if !(_jsonIsOk[uint8(*(*int8)(unsafe.Pointer(z + uintptr(j+uint32(1)))))] != 0) { j += uint32(1) } else { if !(_jsonIsOk[uint8(*(*int8)(unsafe.Pointer(z + uintptr(j+uint32(2)))))] != 0) { j += uint32(2) } else { j += uint32(3) continue } } } c = *(*int8)(unsafe.Pointer(z + uintptr(j))) if int32(c) == int32(cDelim) { break } else { if int32(c) == int32('\\') { j++ v46 = j c = *(*int8)(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(uint8(c)) && int32(0x80) == int32(uint8(*(*int8)(unsafe.Pointer(z + uintptr(j+uint32(1)))))) && (int32(0xa8) == int32(uint8(*(*int8)(unsafe.Pointer(z + uintptr(j+uint32(2)))))) || int32(0xa9) == int32(uint8(*(*int8)(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(*(*int8)(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) { if int32(c) == 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = j return -int32(1) } /* Control characters are not allowed in canonical JSON string ** literals, but are allowed in JSON5 string literals. */ opcode = uint8(JSONB_TEXT5) (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(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+8359, uint64(4)) == 0 && !(int32(_sqlite3CtypeMap[uint8(*(*int8)(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+8364, uint64(5)) == 0 && !(int32(_sqlite3CtypeMap[uint8(*(*int8)(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(*(*int8)(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) c = *(*int8)(unsafe.Pointer(z + uintptr(i))) if int32(c) <= int32('0') { if int32(c) == int32('0') { if (int32(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('x') || int32(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('X')) && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(2)))))])&int32(0x08) != 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) t = uint8(0x01) j = i + uint32(3) for { if !(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(z + uintptr(j))))])&int32(0x08) != 0) { break } goto _47 _47: ; j++ } goto parse_number_finish } else { if int32(_sqlite3CtypeMap[uint8(*(*int8)(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(*(*int8)(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(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('I') || int32(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('i')) && Xsqlite3_strnicmp(tls, z+uintptr(i+uint32(1)), __ccgo_ts+25469, int32(3)) == 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FhasNonstd = uint8(1) if int32(*(*int8)(unsafe.Pointer(z + uintptr(i)))) == int32('-') { _jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint32(6), __ccgo_ts+25552) } else { _jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint32(5), __ccgo_ts+25559) } if Xsqlite3_strnicmp(tls, z+uintptr(i+uint32(4)), __ccgo_ts+25565, int32(5)) == 0 { v48 = int32(9) } else { v48 = int32(4) } return int32(i + uint32(v48)) } if int32(*(*int8)(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(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('0') { if int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(2)))))])&int32(0x04) != 0 { (*TJsonParse)(unsafe.Pointer(pParse)).FiErr = i + uint32(1) return -int32(1) } else { if (int32(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(2))))) == int32('x') || int32(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(2))))) == int32('X')) && int32(_sqlite3CtypeMap[uint8(*(*int8)(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(*(*int8)(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 = *(*int8)(unsafe.Pointer(z + uintptr(j))) if int32(_sqlite3CtypeMap[uint8(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(*(*int8)(unsafe.Pointer(z + uintptr(j-uint32(1))))) < int32('0') { if int32(*(*int8)(unsafe.Pointer(z + uintptr(j-uint32(1))))) == int32('.') && j-uint32(2) >= i && int32(_sqlite3CtypeMap[uint8(*(*int8)(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 = *(*int8)(unsafe.Pointer(z + uintptr(j+uint32(1)))) if int32(c) == int32('+') || int32(c) == int32('-') { j++ c = *(*int8)(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(*(*int8)(unsafe.Pointer(z + uintptr(j-uint32(1))))) < int32('0') { if int32(*(*int8)(unsafe.Pointer(z + uintptr(j-uint32(1))))) == int32('.') && j-uint32(2) >= i && int32(_sqlite3CtypeMap[uint8(*(*int8)(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: ; if int32(*(*int8)(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) + uint32(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+1650, uint64(4)) == 0 && !(int32(_sqlite3CtypeMap[uint8(*(*int8)(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 = *(*int8)(unsafe.Pointer(z + uintptr(i))) k1 = uint32(0) for { if !(uint64(k1) < libc.Uint64FromInt64(120)/libc.Uint64FromInt64(24)) { 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(*(*int8)(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+25559) } 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(*(*int8)(unsafe.Pointer(zJson + uintptr(i))))] != 0 { i++ } if *(*int8)(unsafe.Pointer(zJson + uintptr(i))) != 0 { i += _json5Whitespace(tls, zJson+uintptr(i)) if *(*int8)(unsafe.Pointer(zJson + uintptr(i))) != 0 { if pCtx != 0 { Xsqlite3_result_error(tls, pCtx, __ccgo_ts+25454, -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+25454, -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(80) defer tls.Free(80) var _ /* px at bp+0 */ TJsonParse libc.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 0)) _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 { 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 + 33 *(*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+1650, uint32(4)) return i + uint32(1) _3: ; _jsonAppendRawNZ(tls, pOut, __ccgo_ts+8359, uint32(4)) return i + uint32(1) _4: ; _jsonAppendRawNZ(tls, pOut, __ccgo_ts+8364, 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(*(*int8)(unsafe.Pointer(zIn))) == int32('-') { _jsonAppendChar(tls, pOut, int8('-')) k++ } else { if int32(*(*int8)(unsafe.Pointer(zIn))) == int32('+') { k++ } } for { if !(k < *(*Tu32)(unsafe.Pointer(bp))) { break } if !(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zIn + uintptr(k))))])&libc.Int32FromInt32(0x08) != 0) { p18 = pOut + 33 *(*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(*(*int8)(unsafe.Pointer(zIn + uintptr(k)))))) } } goto _17 _17: ; k++ } if bOverflow != 0 { v19 = __ccgo_ts + 25473 } else { v19 = __ccgo_ts + 12226 } _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(*(*int8)(unsafe.Pointer(zIn1))) == int32('-') { _jsonAppendChar(tls, pOut, int8('-')) k1++ } if int32(*(*int8)(unsafe.Pointer(zIn1 + uintptr(k1)))) == int32('.') { _jsonAppendChar(tls, pOut, int8('0')) } for { if !(k1 < *(*Tu32)(unsafe.Pointer(bp))) { break } _jsonAppendChar(tls, pOut, *(*int8)(unsafe.Pointer(zIn1 + uintptr(k1)))) if int32(*(*int8)(unsafe.Pointer(zIn1 + uintptr(k1)))) == int32('.') && (k1+uint32(1) == *(*Tu32)(unsafe.Pointer(bp)) || !(int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zIn1 + uintptr(k1+uint32(1)))))])&libc.Int32FromInt32(0x04) != 0)) { _jsonAppendChar(tls, pOut, int8('0')) } goto _20 _20: ; k1++ } goto _16 _10: ; _9: ; _jsonAppendChar(tls, pOut, int8('"')) _jsonAppendRaw(tls, pOut, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i+n), *(*Tu32)(unsafe.Pointer(bp))) _jsonAppendChar(tls, pOut, int8('"')) goto _16 _11: ; sz2 = *(*Tu32)(unsafe.Pointer(bp)) zIn2 = (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(i+n) _jsonAppendChar(tls, pOut, int8('"')) for sz2 > uint32(0) { k2 = uint32(0) for { if !(k2 < sz2 && (_jsonIsOk[uint8(*(*int8)(unsafe.Pointer(zIn2 + uintptr(k2))))] != 0 || int32(*(*int8)(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(*(*int8)(unsafe.Pointer(zIn2))) == int32('"') { _jsonAppendRawNZ(tls, pOut, __ccgo_ts+25571, uint32(2)) zIn2++ sz2-- continue } if int32(*(*int8)(unsafe.Pointer(zIn2))) <= int32(0x1f) { if (*TJsonString)(unsafe.Pointer(pOut)).FnUsed+uint64(7) > (*TJsonString)(unsafe.Pointer(pOut)).FnAlloc && _jsonStringGrow(tls, pOut, uint32(7)) != 0 { break } _jsonAppendControlChar(tls, pOut, uint8(*(*int8)(unsafe.Pointer(zIn2)))) zIn2++ sz2-- continue } if sz2 < uint32(2) { p22 = pOut + 33 *(*Tu8)(unsafe.Pointer(p22)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p22))) | libc.Int32FromInt32(JSTRING_MALFORMED)) break } switch int32(uint8(*(*int8)(unsafe.Pointer(zIn2 + 1)))) { case int32('\''): _jsonAppendChar(tls, pOut, int8('\'')) case int32('v'): _jsonAppendRawNZ(tls, pOut, __ccgo_ts+25574, uint32(6)) case int32('x'): if sz2 < uint32(4) { p23 = pOut + 33 *(*Tu8)(unsafe.Pointer(p23)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p23))) | libc.Int32FromInt32(JSTRING_MALFORMED)) sz2 = uint32(2) break } _jsonAppendRawNZ(tls, pOut, __ccgo_ts+25581, uint32(4)) _jsonAppendRawNZ(tls, pOut, zIn2+2, uint32(2)) zIn2 += uintptr(2) sz2 -= uint32(2) case int32('0'): _jsonAppendRawNZ(tls, pOut, __ccgo_ts+25586, uint32(6)) case int32('\r'): if sz2 > uint32(2) && int32(*(*int8)(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(*(*int8)(unsafe.Pointer(zIn2 + 2)))) || int32(0xa8) != int32(uint8(*(*int8)(unsafe.Pointer(zIn2 + 3)))) && int32(0xa9) != int32(uint8(*(*int8)(unsafe.Pointer(zIn2 + 3)))) { p24 = pOut + 33 *(*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 } zIn2 += uintptr(2) sz2 -= uint32(2) } _jsonAppendChar(tls, pOut, int8('"')) goto _16 _12: ; _jsonAppendString(tls, pOut, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(i+n), *(*Tu32)(unsafe.Pointer(bp))) goto _16 _13: ; _jsonAppendChar(tls, pOut, int8('[')) 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, int8(',')) } if j > iEnd { p25 = pOut + 33 *(*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, int8(']')) goto _16 _14: ; x = 0 _jsonAppendChar(tls, pOut, int8('{')) 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, int8(v26)) } if x&int32(1) != 0 || j > iEnd { p28 = pOut + 33 *(*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, int8('}')) goto _16 _15: ; goto malformed_jsonb malformed_jsonb: ; p29 = pOut + 33 *(*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 // // /* Context for recursion of json_pretty() // */ type TJsonPretty = struct { FpParse uintptr FpOut uintptr FzIndent uintptr FszIndent Tu32 FnIndent Tu32 } type JsonPretty = TJsonPretty type TJsonPretty1 = struct { FpParse uintptr FpOut uintptr FzIndent uintptr FszIndent Tu32 FnIndent Tu32 } type JsonPretty1 = TJsonPretty1 // C documentation // // /* Append indentation to the pretty JSON under construction */ func _jsonPrettyIndent(tls *libc.TLS, pPretty uintptr) { var jj Tu32 _ = jj jj = uint32(0) for { if !(jj < (*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent) { break } _jsonAppendRaw(tls, (*TJsonPretty)(unsafe.Pointer(pPretty)).FpOut, (*TJsonPretty)(unsafe.Pointer(pPretty)).FzIndent, (*TJsonPretty)(unsafe.Pointer(pPretty)).FszIndent) goto _1 _1: ; jj++ } } // 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. // ** // ** This is a variant of jsonTranslateBlobToText() that "pretty-prints" // ** the output. Extra whitespace is inserted to make the JSON easier // ** for humans to read. // ** // ** 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 _jsonTranslateBlobToPrettyText(tls *libc.TLS, pPretty uintptr, i Tu32) (r Tu32) { bp := tls.Alloc(16) defer tls.Free(16) var iEnd, j, n Tu32 var pOut, pParse, p1, p2 uintptr var _ /* sz at bp+0 */ Tu32 _, _, _, _, _, _, _ = iEnd, j, n, pOut, pParse, p1, p2 pParse = (*TJsonPretty)(unsafe.Pointer(pPretty)).FpParse pOut = (*TJsonPretty)(unsafe.Pointer(pPretty)).FpOut n = _jsonbPayloadSize(tls, pParse, i, bp) if n == uint32(0) { p1 = pOut + 33 *(*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 int32(JSONB_ARRAY): j = i + n iEnd = j + *(*Tu32)(unsafe.Pointer(bp)) _jsonAppendChar(tls, pOut, int8('[')) if j < iEnd { _jsonAppendChar(tls, pOut, int8('\n')) (*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent++ for int32((*TJsonString)(unsafe.Pointer(pOut)).FeErr) == 0 { _jsonPrettyIndent(tls, pPretty) j = _jsonTranslateBlobToPrettyText(tls, pPretty, j) if j >= iEnd { break } _jsonAppendRawNZ(tls, pOut, __ccgo_ts+25593, uint32(2)) } _jsonAppendChar(tls, pOut, int8('\n')) (*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent-- _jsonPrettyIndent(tls, pPretty) } _jsonAppendChar(tls, pOut, int8(']')) i = iEnd case int32(JSONB_OBJECT): j = i + n iEnd = j + *(*Tu32)(unsafe.Pointer(bp)) _jsonAppendChar(tls, pOut, int8('{')) if j < iEnd { _jsonAppendChar(tls, pOut, int8('\n')) (*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent++ for int32((*TJsonString)(unsafe.Pointer(pOut)).FeErr) == 0 { _jsonPrettyIndent(tls, pPretty) j = _jsonTranslateBlobToText(tls, pParse, j, pOut) if j > iEnd { p2 = pOut + 33 *(*Tu8)(unsafe.Pointer(p2)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p2))) | libc.Int32FromInt32(JSTRING_MALFORMED)) break } _jsonAppendRawNZ(tls, pOut, __ccgo_ts+25596, uint32(2)) j = _jsonTranslateBlobToPrettyText(tls, pPretty, j) if j >= iEnd { break } _jsonAppendRawNZ(tls, pOut, __ccgo_ts+25593, uint32(2)) } _jsonAppendChar(tls, pOut, int8('\n')) (*TJsonPretty)(unsafe.Pointer(pPretty)).FnIndent-- _jsonPrettyIndent(tls, pPretty) } _jsonAppendChar(tls, pOut, int8('}')) i = iEnd default: i = _jsonTranslateBlobToText(tls, pParse, i, pOut) break } return i } // 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(80) defer tls.Free(80) var aBlob uintptr var n Tu32 var nBlob int32 var _ /* s at bp+8 */ 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.X__builtin___memset_chk(tls, bp+8, 0, uint64(72), libc.X__builtin_object_size(tls, bp+8, 0)) (*(*TJsonParse)(unsafe.Pointer(bp + 8))).FaBlob = aBlob (*(*TJsonParse)(unsafe.Pointer(bp + 8))).FnBlob = uint32(nBlob) n = _jsonbPayloadSize(tls, bp+8, 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) 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 + 52)) += _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.X__builtin___memmove_chk(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(iDel+nIns), (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(iDel+nDel), uint64((*TJsonParse)(unsafe.Pointer(pParse)).FnBlob-(iDel+nDel)), libc.X__builtin_object_size(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(iDel+nIns), 0)) p1 = pParse + 8 *(*Tu32)(unsafe.Pointer(p1)) = Tu32(int64(*(*Tu32)(unsafe.Pointer(p1))) + d) p2 = pParse + 52 *(*int32)(unsafe.Pointer(p2)) = int32(int64(*(*int32)(unsafe.Pointer(p2))) + d) } if nIns != 0 && aIns != 0 { libc.X__builtin___memcpy_chk(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(iDel), aIns, uint64(nIns), libc.X__builtin_object_size(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(iDel), 0)) } } // 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(*(*int8)(unsafe.Pointer(z + uintptr(i)))) != int32('\\') { return i } if int32(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('\n') { i += uint32(2) continue } if int32(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(1))))) == int32('\r') { if i+uint32(2) < n && int32(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(2))))) == int32('\n') { i += uint32(3) } else { i += uint32(2) } continue } if int32(0xe2) == int32(uint8(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(1)))))) && i+uint32(3) < n && int32(0x80) == int32(uint8(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(2)))))) && (int32(0xa8) == int32(uint8(*(*int8)(unsafe.Pointer(z + uintptr(i+uint32(3)))))) || int32(0xa9) == int32(uint8(*(*int8)(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 if n < uint32(2) { *(*Tu32)(unsafe.Pointer(piOut)) = uint32(JSON_INVALID_CHAR) return n } switch int32(uint8(*(*int8)(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(*(*int8)(unsafe.Pointer(z + 6))) == int32('\\') && int32(*(*int8)(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) nLeft -= n } } if nRight == uint32(0) { *(*Tu32)(unsafe.Pointer(bp + 4)) = uint32(0) } else { if rawRight != 0 || int32(*(*int8)(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) 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, uint64(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.X__builtin___memset_chk(tls, pIns, 0, uint64(72), libc.X__builtin_object_size(tls, pIns, 0)) (*TJsonParse)(unsafe.Pointer(pIns)).Fdb = (*TJsonParse)(unsafe.Pointer(pParse)).Fdb if int32(*(*int8)(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(*(*int8)(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 + 47 *(*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(224) defer tls.Free(224) 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+80 */ TJsonParse var _ /* sz at bp+0 */ Tu32 var _ /* v at bp+152 */ TJsonParse var _ /* v at bp+8 */ TJsonParse _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = i, iEnd, j, k, n, nIns, nKey, nn, rawKey, rawLabel, rc, v, x, zKey, zLabel, v3, p4 if int32(*(*int8)(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(*(*int8)(unsafe.Pointer(zPath))) == int32('.') { rawKey = int32(1) x = *(*Tu8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(pParse)).FaBlob + uintptr(iRoot))) zPath++ if int32(*(*int8)(unsafe.Pointer(zPath))) == int32('"') { zKey = zPath + uintptr(1) i = uint32(1) for { if !(*(*int8)(unsafe.Pointer(zPath + uintptr(i))) != 0 && int32(*(*int8)(unsafe.Pointer(zPath + uintptr(i)))) != int32('"')) { break } goto _1 _1: ; i++ } nKey = i - uint32(1) if *(*int8)(unsafe.Pointer(zPath + uintptr(i))) != 0 { i++ } else { return uint32(JSON_LOOKUP_PATHERROR) } rawKey = libc.BoolInt32(libc.Xmemchr(tls, zKey, int32('\\'), uint64(nKey)) == uintptr(0)) } else { zKey = zPath i = uint32(0) for { if !(*(*int8)(unsafe.Pointer(zPath + uintptr(i))) != 0 && int32(*(*int8)(unsafe.Pointer(zPath + uintptr(i)))) != int32('.') && int32(*(*int8)(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) } 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.X__builtin___memset_chk(tls, bp+80, 0, uint64(72), libc.X__builtin_object_size(tls, bp+80, 0)) (*(*TJsonParse)(unsafe.Pointer(bp + 80))).Fdb = (*TJsonParse)(unsafe.Pointer(pParse)).Fdb if rawKey != 0 { v3 = int32(JSONB_TEXTRAW) } else { v3 = int32(JSONB_TEXT5) } _jsonBlobAppendNode(tls, bp+80, uint8(v3), nKey, uintptr(0)) p4 = pParse + 47 *(*Tu8)(unsafe.Pointer(p4)) = Tu8(int32(*(*Tu8)(unsafe.Pointer(p4))) | int32((*(*TJsonParse)(unsafe.Pointer(bp + 80))).Foom)) rc = _jsonCreateEditSubstructure(tls, pParse, bp+8, zPath+uintptr(i)) if !(rc >= libc.Uint32FromUint32(JSON_LOOKUP_PATHERROR)) && _jsonBlobMakeEditable(tls, pParse, (*(*TJsonParse)(unsafe.Pointer(bp + 80))).FnBlob+nKey+(*(*TJsonParse)(unsafe.Pointer(bp + 8))).FnBlob) != 0 { nIns = (*(*TJsonParse)(unsafe.Pointer(bp + 80))).FnBlob + nKey + (*(*TJsonParse)(unsafe.Pointer(bp + 8))).FnBlob _jsonBlobEdit(tls, pParse, j, uint32(0), uintptr(0), nIns) if !((*TJsonParse)(unsafe.Pointer(pParse)).Foom != 0) { /* Because pParse->oom!=0 */ /* Because pPasre->oom!=0 */ libc.X__builtin___memcpy_chk(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(j), (*(*TJsonParse)(unsafe.Pointer(bp + 80))).FaBlob, uint64((*(*TJsonParse)(unsafe.Pointer(bp + 80))).FnBlob), libc.X__builtin_object_size(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(j), 0)) k = j + (*(*TJsonParse)(unsafe.Pointer(bp + 80))).FnBlob libc.X__builtin___memcpy_chk(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(k), zKey, uint64(nKey), libc.X__builtin_object_size(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(k), 0)) k += nKey libc.X__builtin___memcpy_chk(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(k), (*(*TJsonParse)(unsafe.Pointer(bp + 8))).FaBlob, uint64((*(*TJsonParse)(unsafe.Pointer(bp + 8))).FnBlob), libc.X__builtin_object_size(tls, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob+uintptr(k), 0)) if (*TJsonParse)(unsafe.Pointer(pParse)).Fdelta != 0 { _jsonAfterEditSizeAdjust(tls, pParse, iRoot) } } } _jsonParseReset(tls, bp+8) _jsonParseReset(tls, bp+80) return rc } } else { if int32(*(*int8)(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(*(*int8)(unsafe.Pointer(zPath + uintptr(i))))])&int32(0x04) != 0 { k = k*uint32(10) + uint32(*(*int8)(unsafe.Pointer(zPath + uintptr(i)))) - uint32('0') i++ } if i < uint32(2) || int32(*(*int8)(unsafe.Pointer(zPath + uintptr(i)))) != int32(']') { if int32(*(*int8)(unsafe.Pointer(zPath + 1))) == int32('#') { k = _jsonbArrayCount(tls, pParse, iRoot) i = uint32(2) if int32(*(*int8)(unsafe.Pointer(zPath + 2))) == int32('-') && int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zPath + 3)))])&int32(0x04) != 0 { nn = uint32(0) i = uint32(3) for cond := true; cond; cond = int32(_sqlite3CtypeMap[uint8(*(*int8)(unsafe.Pointer(zPath + uintptr(i))))])&int32(0x04) != 0 { nn = nn*uint32(10) + uint32(*(*int8)(unsafe.Pointer(zPath + uintptr(i)))) - uint32('0') i++ } if nn > k { return uint32(JSON_LOOKUP_NOTFOUND) } k -= nn } if int32(*(*int8)(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+152, zPath+uintptr(i+uint32(1))) if !(rc >= libc.Uint32FromUint32(JSON_LOOKUP_PATHERROR)) && _jsonBlobMakeEditable(tls, pParse, (*(*TJsonParse)(unsafe.Pointer(bp + 152))).FnBlob) != 0 { _jsonBlobEdit(tls, pParse, j, uint32(0), (*(*TJsonParse)(unsafe.Pointer(bp + 152))).FaBlob, (*(*TJsonParse)(unsafe.Pointer(bp + 152))).FnBlob) } _jsonParseReset(tls, bp+152) 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(208) defer tls.Free(208) var _ /* s at bp+72 */ TJsonString var _ /* x at bp+0 */ TJsonParse if aBlob == uintptr(0) { return } libc.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 0)) (*(*TJsonParse)(unsafe.Pointer(bp))).FaBlob = aBlob (*(*TJsonParse)(unsafe.Pointer(bp))).FnBlob = nBlob _jsonStringInit(tls, bp+72, ctx) _jsonTranslateBlobToText(tls, bp, uint32(0), bp+72) _jsonReturnString(tls, bp+72, 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 int8 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+25454, -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 = int8(*(*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++ *(*int8)(unsafe.Pointer(zOut + uintptr(v21))) = int8(uint32(0x80) | *(*Tu32)(unsafe.Pointer(bp + 24))&uint32(0x3f)) } else { if *(*Tu32)(unsafe.Pointer(bp + 24)) < uint32(0x10000) { v22 = iOut iOut++ *(*int8)(unsafe.Pointer(zOut + uintptr(v22))) = int8(uint32(0xe0) | *(*Tu32)(unsafe.Pointer(bp + 24))>>libc.Int32FromInt32(12)) v23 = iOut iOut++ *(*int8)(unsafe.Pointer(zOut + uintptr(v23))) = int8(uint32(0x80) | *(*Tu32)(unsafe.Pointer(bp + 24))>>libc.Int32FromInt32(6)&uint32(0x3f)) v24 = iOut iOut++ *(*int8)(unsafe.Pointer(zOut + uintptr(v24))) = int8(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 { v25 = iOut iOut++ *(*int8)(unsafe.Pointer(zOut + uintptr(v25))) = int8(uint32(0xf0) | *(*Tu32)(unsafe.Pointer(bp + 24))>>libc.Int32FromInt32(18)) v26 = iOut iOut++ *(*int8)(unsafe.Pointer(zOut + uintptr(v26))) = int8(uint32(0x80) | *(*Tu32)(unsafe.Pointer(bp + 24))>>libc.Int32FromInt32(12)&uint32(0x3f)) v27 = iOut iOut++ *(*int8)(unsafe.Pointer(zOut + uintptr(v27))) = int8(uint32(0x80) | *(*Tu32)(unsafe.Pointer(bp + 24))>>libc.Int32FromInt32(6)&uint32(0x3f)) v28 = iOut iOut++ *(*int8)(unsafe.Pointer(zOut + uintptr(v28))) = int8(uint32(0x80) | *(*Tu32)(unsafe.Pointer(bp + 24))&uint32(0x3f)) } } } } iIn += szEscape - uint32(1) } else { v29 = iOut iOut++ *(*int8)(unsafe.Pointer(zOut + uintptr(v29))) = c } goto _17 _17: ; iIn++ } /* end for() */ *(*int8)(unsafe.Pointer(zOut + uintptr(iOut))) = 0 Xsqlite3_result_text(tls, pCtx, zOut, int32(iOut), __ccgo_fp(_sqlite3OomClear)) goto _15 _13: ; _12: ; if textOnly != 0 { v30 = 0 } else { v30 = int32(int64(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+25454, -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.X__builtin___memset_chk(tls, pParse, 0, uint64(72), libc.X__builtin_object_size(tls, pParse, 0)) (*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+25425, -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+25454, -int32(1)) _sqlite3DbFree(tls, (*TJsonParse)(unsafe.Pointer(pParse)).Fdb, (*TJsonParse)(unsafe.Pointer(pParse)).FaBlob) libc.X__builtin___memset_chk(tls, pParse, 0, uint64(72), libc.X__builtin_object_size(tls, pParse, 0)) 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(*(*int8)(unsafe.Pointer(z))) == int32('I') { _jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint32(5), __ccgo_ts+25559) } else { if int32(*(*int8)(unsafe.Pointer(z))) == int32('-') && int32(*(*int8)(unsafe.Pointer(z + 1))) == int32('I') { _jsonBlobAppendNode(tls, pParse, uint8(JSONB_FLOAT), uint32(6), __ccgo_ts+25552) } 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+25599, 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(80) defer tls.Free(80) 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) 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)*8))) == int32(SQLITE_NULL) { goto _2 } zPath = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*8))) if zPath == uintptr(0) { Xsqlite3_result_error_nomem(tls, ctx) _jsonParseFree(tls, p) return } if int32(*(*int8)(unsafe.Pointer(zPath))) != int32('$') { goto jsonInsertIntoBlob_patherror } if _jsonFunctionArgToBlob(tls, ctx, *(*uintptr)(unsafe.Pointer(argv + uintptr(i+int32(1))*8)), bp) != 0 { _jsonParseReset(tls, bp) _jsonParseFree(tls, p) return } if int32(*(*int8)(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+25454, -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 */ 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(72)) if p == uintptr(0) { goto json_pfa_oom } libc.X__builtin___memset_chk(tls, p, 0, uint64(72), libc.X__builtin_object_size(tls, p, 0)) (*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.X__builtin___memcpy_chk(tls, (*TJsonParse)(unsafe.Pointer(p)).FaBlob, (*TJsonParse)(unsafe.Pointer(pFromCache)).FaBlob, uint64(nBlob), libc.X__builtin_object_size(tls, (*TJsonParse)(unsafe.Pointer(p)).FaBlob, 0)) 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 } 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.X__builtin___memcpy_chk(tls, zNew, (*TJsonParse)(unsafe.Pointer(p)).FzJson, uint64((*TJsonParse)(unsafe.Pointer(p)).FnJson), libc.X__builtin_object_size(tls, zNew, 0)) (*TJsonParse)(unsafe.Pointer(p)).FzJson = zNew *(*int8)(unsafe.Pointer((*TJsonParse)(unsafe.Pointer(p)).FzJson + uintptr((*TJsonParse)(unsafe.Pointer(p)).FnJson))) = 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+25454, -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(144) defer tls.Free(144) 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(int64(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(144) defer tls.Free(144) 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(144) defer tls.Free(144) var i int32 var _ /* jx at bp+0 */ TJsonString _ = i _jsonStringInit(tls, bp, ctx) _jsonAppendChar(tls, bp, int8('[')) i = 0 for { if !(i < argc) { break } _jsonAppendSeparator(tls, bp) _jsonAppendSqlValue(tls, bp, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*8))) goto _1 _1: ; i++ } _jsonAppendChar(tls, bp, int8(']')) _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*8))) if zPath == uintptr(0) { _jsonParseFree(tls, p) return } if int32(*(*int8)(unsafe.Pointer(zPath))) == int32('$') { v1 = zPath + uintptr(1) } else { v1 = __ccgo_ts + 25617 } 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+25454, -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 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(*(*int8)(unsafe.Pointer(z + uintptr(i))))])&int32(0x06) != 0 || int32(*(*int8)(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(144) defer tls.Free(144) 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(int64(Xsqlite3_user_data(tls, ctx))) _jsonStringInit(tls, bp, ctx) if argc > int32(2) { _jsonAppendChar(tls, bp, int8('[')) } i = int32(1) for { if !(i < argc) { break } /* With a single PATH argument */ zPath = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*8))) if zPath == uintptr(0) { goto json_extract_error } nPath = _sqlite3Strlen30(tls, zPath) if int32(*(*int8)(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 Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*8))) == int32(SQLITE_INTEGER) { _jsonAppendRawNZ(tls, bp, __ccgo_ts+25619, uint32(1)) _jsonAppendRaw(tls, bp, zPath, uint32(nPath)) _jsonAppendRawNZ(tls, bp, __ccgo_ts+5573, uint32(2)) } else { if _jsonAllAlphanum(tls, zPath, nPath) != 0 { _jsonAppendRawNZ(tls, bp, __ccgo_ts+1677, uint32(1)) _jsonAppendRaw(tls, bp, zPath, uint32(nPath)) } else { if int32(*(*int8)(unsafe.Pointer(zPath))) == int32('[') && nPath >= int32(3) && int32(*(*int8)(unsafe.Pointer(zPath + uintptr(nPath-int32(1))))) == int32(']') { _jsonAppendRaw(tls, bp, zPath, uint32(nPath)) } else { _jsonAppendRawNZ(tls, bp, __ccgo_ts+25621, uint32(2)) _jsonAppendRaw(tls, bp, zPath, uint32(nPath)) _jsonAppendRawNZ(tls, bp, __ccgo_ts+25624, 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) 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+1650, uint32(4)) } } else { if j == uint32(JSON_LOOKUP_ERROR) { Xsqlite3_result_error(tls, ctx, __ccgo_ts+25454, -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, int8(']')) _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 */ 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 + 52)) += 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.X__builtin___memcpy_chk(tls, (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTEnd), (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPLabel), uint64(szNew), libc.X__builtin_object_size(tls, (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTEnd), 0)) libc.X__builtin___memcpy_chk(tls, (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTEnd+szNew), (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPValue), uint64(*(*Tu32)(unsafe.Pointer(bp + 16))+nPValue), libc.X__builtin_object_size(tls, (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTEnd+szNew), 0)) } 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.X__builtin___memcpy_chk(tls, (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTEnd), (*TJsonParse)(unsafe.Pointer(pPatch)).FaBlob+uintptr(iPLabel), uint64(szNew), libc.X__builtin_object_size(tls, (*TJsonParse)(unsafe.Pointer(pTarget)).FaBlob+uintptr(iTEnd), 0)) *(*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 + 52)) += 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 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*8)), 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+25454, -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(144) defer tls.Free(144) 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+25626, -int32(1)) return } _jsonStringInit(tls, bp, ctx) _jsonAppendChar(tls, bp, int8('{')) i = 0 for { if !(i < argc) { break } if Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*8))) != int32(SQLITE_TEXT) { Xsqlite3_result_error(tls, ctx, __ccgo_ts+25677, -int32(1)) _jsonStringReset(tls, bp) return } _jsonAppendSeparator(tls, bp) z = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*8))) n = uint32(Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*8)))) _jsonAppendString(tls, bp, z, n) _jsonAppendChar(tls, bp, int8(':')) _jsonAppendSqlValue(tls, bp, *(*uintptr)(unsafe.Pointer(argv + uintptr(i+int32(1))*8))) goto _1 _1: ; i += int32(2) } _jsonAppendChar(tls, bp, int8('}')) _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)*8))) if zPath == uintptr(0) { goto json_remove_done } if int32(*(*int8)(unsafe.Pointer(zPath))) != int32('$') { goto json_remove_patherror } if int32(*(*int8)(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+25454, -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+16237) 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(int64(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 + 25711 } else { v1 = __ccgo_ts + 25715 } _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*8))) if zPath == uintptr(0) { goto json_type_done } if int32(*(*int8)(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+25454, -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_pretty(JSON) // ** json_pretty(JSON, INDENT) // ** // ** Return text that is a pretty-printed rendering of the input JSON. // ** If the argument is not valid JSON, return NULL. // ** // ** The INDENT argument is text that is used for indentation. If omitted, // ** it defaults to four spaces (the same as PostgreSQL). // */ func _jsonPrettyFunc(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { bp := tls.Alloc(176) defer tls.Free(176) var v1 uintptr var v2 bool var _ /* s at bp+0 */ TJsonString var _ /* x at bp+136 */ TJsonPretty _, _ = v1, v2 /* Pretty printing context */ libc.X__builtin___memset_chk(tls, bp+136, 0, uint64(32), libc.X__builtin_object_size(tls, bp+136, 0)) (*(*TJsonPretty)(unsafe.Pointer(bp + 136))).FpParse = _jsonParseFuncArg(tls, ctx, *(*uintptr)(unsafe.Pointer(argv)), uint32(0)) if (*(*TJsonPretty)(unsafe.Pointer(bp + 136))).FpParse == uintptr(0) { return } (*(*TJsonPretty)(unsafe.Pointer(bp + 136))).FpOut = bp _jsonStringInit(tls, bp, ctx) if v2 = argc == int32(1); !v2 { v1 = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) (*(*TJsonPretty)(unsafe.Pointer(bp + 136))).FzIndent = v1 } if v2 || v1 == uintptr(0) { (*(*TJsonPretty)(unsafe.Pointer(bp + 136))).FzIndent = __ccgo_ts + 25722 (*(*TJsonPretty)(unsafe.Pointer(bp + 136))).FszIndent = uint32(4) } else { (*(*TJsonPretty)(unsafe.Pointer(bp + 136))).FszIndent = uint32(libc.Xstrlen(tls, (*(*TJsonPretty)(unsafe.Pointer(bp + 136))).FzIndent)) } _jsonTranslateBlobToPrettyText(tls, bp+136, uint32(0)) _jsonReturnString(tls, bp, uintptr(0), uintptr(0)) _jsonParseFree(tls, (*(*TJsonPretty)(unsafe.Pointer(bp + 136))).FpParse) } // 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(144) defer tls.Free(144) var f Ti64 var flags, res Tu8 var iErr Tu32 var p uintptr var _ /* px at bp+0 */ TJsonParse var _ /* px at bp+72 */ 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*8))) if f < int64(1) || f > int64(15) { Xsqlite3_result_error(tls, ctx, __ccgo_ts+25727, -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.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 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)))) iErr = _jsonbValidityCheck(tls, bp, uint32(0), (*(*TJsonParse)(unsafe.Pointer(bp))).FnBlob, uint32(1)) res = libc.BoolUint8(iErr == uint32(0)) } } break } /* Fall through into interpreting the input as text. See note ** above at tag-20240123-a. */ fallthrough default: if int32(flags)&int32(0x3) == 0 { break } libc.X__builtin___memset_chk(tls, bp+72, 0, uint64(72), libc.X__builtin_object_size(tls, bp+72, 0)) 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(80) defer tls.Free(80) var iErrPos Ti64 var k Tu32 var _ /* s at bp+0 */ TJsonParse _, _ = iErrPos, k iErrPos = 0 _ = argc libc.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 0)) (*(*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 { /* Because s.oom is false */ k = uint32(0) for { if !(k < (*(*TJsonParse)(unsafe.Pointer(bp))).FiErr && *(*int8)(unsafe.Pointer((*(*TJsonParse)(unsafe.Pointer(bp))).FzJson + uintptr(k))) != 0) { break } if int32(*(*int8)(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(136)) if pStr != 0 { if (*TJsonString)(unsafe.Pointer(pStr)).FzBuf == uintptr(0) { _jsonStringInit(tls, pStr, ctx) _jsonAppendChar(tls, pStr, int8('[')) } else { if (*TJsonString)(unsafe.Pointer(pStr)).FnUsed > uint64(1) { _jsonAppendChar(tls, pStr, int8(',')) } } (*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, int8(']')) flags = int32(int64(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+25784, 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 int8 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 = *(*int8)(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 + 24)) -= uint64(i) libc.X__builtin___memmove_chk(tls, z+1, z+uintptr(i+uint32(1)), (*TJsonString)(unsafe.Pointer(pStr)).FnUsed-uint64(1), libc.X__builtin_object_size(tls, z+1, 0)) *(*int8)(unsafe.Pointer(z + uintptr((*TJsonString)(unsafe.Pointer(pStr)).FnUsed))) = 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(136)) if pStr != 0 { if (*TJsonString)(unsafe.Pointer(pStr)).FzBuf == uintptr(0) { _jsonStringInit(tls, pStr, ctx) _jsonAppendChar(tls, pStr, int8('{')) } else { if (*TJsonString)(unsafe.Pointer(pStr)).FnUsed > uint64(1) { _jsonAppendChar(tls, pStr, int8(',')) } } (*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, int8(':')) _jsonAppendSqlValue(tls, pStr, *(*uintptr)(unsafe.Pointer(argv + 1*8))) } } 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, int8('}')) (*TJsonString)(unsafe.Pointer(pStr)).FpCtx = ctx flags = int32(int64(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+25787, 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 { FiHead Tu32 FiValue Tu32 FiEnd Tu32 FnPath Tu32 FiKey Ti64 } type JsonParent = TJsonParent type TJsonParent1 = struct { FiHead Tu32 FiValue Tu32 FiEnd Tu32 FnPath Tu32 FiKey Ti64 } type JsonParent1 = TJsonParent1 type TJsonEachCursor = struct { 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 { 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+25790) if rc == SQLITE_OK { pNew = _sqlite3DbMallocZero(tls, db, uint64(32)) *(*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(264)) if pCur == uintptr(0) { return int32(SQLITE_NOMEM) } (*TJsonEachCursor)(unsafe.Pointer(pCur)).Fdb = (*TJsonEachConnection)(unsafe.Pointer(pVtab)).Fdb _jsonStringZero(tls, pCur+56) *(*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+192) _jsonStringReset(tls, p+56) _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+192, (*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 if int32((*TJsonEachCursor)(unsafe.Pointer(p)).FeType) == int32(JSONB_ARRAY) { _jsonPrintf(tls, int32(30), p+56, __ccgo_ts+25873, 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+192, (*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(*(*int8)(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(*(*int8)(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+56, __ccgo_ts+25880, libc.VaList(bp+16, *(*Tu32)(unsafe.Pointer(bp)), z)) } else { _jsonPrintf(tls, int32(*(*Tu32)(unsafe.Pointer(bp))+uint32(2)), p+56, __ccgo_ts+25888, 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+192, 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+192, 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 int8 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(*(*int8)(unsafe.Pointer(z + uintptr(n)))) == int32('[') || int32(*(*int8)(unsafe.Pointer(z + uintptr(n)))) == int32('.') { *(*Tu32)(unsafe.Pointer(bp)) = uint32(0) cSaved = *(*int8)(unsafe.Pointer(z + uintptr(n))) *(*int8)(unsafe.Pointer(z + uintptr(n))) = 0 x = _jsonLookupStep(tls, p+192, uint32(0), z+uintptr(1), uint32(0)) *(*int8)(unsafe.Pointer(z + uintptr(n))) = cSaved if x >= uint32(JSON_LOOKUP_PATHERROR) { continue } if x+_jsonbPayloadSize(tls, p+192, 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(*(*int8)(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(*(*int8)(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+192, (*TJsonEachCursor)(unsafe.Pointer(p)).Fi, ctx, int32(1)) } else { 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+192, 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+192, 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 */ _ = 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) 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.X__builtin___memset_chk(tls, p+192, 0, uint64(72), libc.X__builtin_object_size(tls, p+192, 0)) (*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+192, 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*8))) if zRoot == uintptr(0) { return SQLITE_OK } if int32(*(*int8)(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(*(*int8)(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+192, 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+56, 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+56, __ccgo_ts+25894, uint32(1)) } (*TJsonEachCursor)(unsafe.Pointer(p)).FnParent = uint32(0) n = _jsonbPayloadSize(tls, p+192, 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+25454, 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, 16)) = __ccgo_fp(_jsonEachConnect) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_jsonEachBestIndex) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_jsonEachDisconnect) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_jsonEachOpenEach) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_jsonEachClose) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_jsonEachFilter) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_jsonEachNext) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_jsonEachEof) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_jsonEachColumn) *(*uintptr)(unsafe.Add(p, 96)) = __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, 16)) = __ccgo_fp(_jsonEachConnect) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_jsonEachBestIndex) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_jsonEachDisconnect) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_jsonEachOpenTree) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_jsonEachClose) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_jsonEachFilter) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_jsonEachNext) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_jsonEachEof) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_jsonEachColumn) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_jsonEachRowid) } // C documentation // // /* // ** Register JSON functions. // */ func _sqlite3RegisterJsonFunctions(tls *libc.TLS) { _sqlite3InsertBuiltinFuncs(tls, uintptr(unsafe.Pointer(&_aJsonFunc)), int32(libc.Uint64FromInt64(2448)/libc.Uint64FromInt64(72))) } var _aJsonFunc = [34]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 + 25896, }, 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(int64(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 25901, }, 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 + 25907, }, 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(int64(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 25918, }, 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 + 25930, }, 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 + 25930, }, 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 + 25948, }, 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 + 25968, }, 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(int64(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 25981, }, 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(int64(libc.Int32FromInt32(JSON_JSON) | libc.Int32FromInt32(0)*libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 25995, }, 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(int64(libc.Int32FromInt32(JSON_SQL) | libc.Int32FromInt32(0)*libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 25998, }, 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 + 26002, }, 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(int64(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 26014, }, 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 + 26027, }, 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(int64(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 26039, }, 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 + 26052, }, 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(int64(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 26063, }, 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(1)*libc.Int32FromInt32(SQLITE_FUNC_RUNONLY) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 26075, }, 18: { 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 + 26075, }, 19: { 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 + 26087, }, 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(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(1)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FzName: __ccgo_ts + 26098, }, 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(0)*libc.Int32FromInt32(SQLITE_SUBTYPE) | libc.Int32FromInt32(0)*libc.Int32FromInt32(SQLITE_RESULT_SUBTYPE)), FpUserData: uintptr(int64(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 26110, }, 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)), FzName: __ccgo_ts + 26123, }, 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(int64(libc.Int32FromInt32(0) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 26136, }, 24: { 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(int64(libc.Int32FromInt32(JSON_ISSET) | libc.Int32FromInt32(0)*libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 26150, }, 25: { 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(int64(libc.Int32FromInt32(JSON_ISSET) | libc.Int32FromInt32(1)*libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 26159, }, 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 + 26169, }, 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 + 26169, }, 28: { 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 + 26179, }, 29: { 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 + 26179, }, 30: { 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 + 26190, }, 31: { 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(int64(libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 26207, }, 32: { 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 + 26225, }, 33: { 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(int64(libc.Int32FromInt32(JSON_BLOB))), FzName: __ccgo_ts + 26243, }, } func init() { p := unsafe.Pointer(&_aJsonFunc) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_jsonRemoveFunc) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_jsonRemoveFunc) *(*uintptr)(unsafe.Add(p, 168)) = __ccgo_fp(_jsonArrayFunc) *(*uintptr)(unsafe.Add(p, 240)) = __ccgo_fp(_jsonArrayFunc) *(*uintptr)(unsafe.Add(p, 312)) = __ccgo_fp(_jsonArrayLengthFunc) *(*uintptr)(unsafe.Add(p, 384)) = __ccgo_fp(_jsonArrayLengthFunc) *(*uintptr)(unsafe.Add(p, 456)) = __ccgo_fp(_jsonErrorFunc) *(*uintptr)(unsafe.Add(p, 528)) = __ccgo_fp(_jsonExtractFunc) *(*uintptr)(unsafe.Add(p, 600)) = __ccgo_fp(_jsonExtractFunc) *(*uintptr)(unsafe.Add(p, 672)) = __ccgo_fp(_jsonExtractFunc) *(*uintptr)(unsafe.Add(p, 744)) = __ccgo_fp(_jsonExtractFunc) *(*uintptr)(unsafe.Add(p, 816)) = __ccgo_fp(_jsonSetFunc) *(*uintptr)(unsafe.Add(p, 888)) = __ccgo_fp(_jsonSetFunc) *(*uintptr)(unsafe.Add(p, 960)) = __ccgo_fp(_jsonObjectFunc) *(*uintptr)(unsafe.Add(p, 1032)) = __ccgo_fp(_jsonObjectFunc) *(*uintptr)(unsafe.Add(p, 1104)) = __ccgo_fp(_jsonPatchFunc) *(*uintptr)(unsafe.Add(p, 1176)) = __ccgo_fp(_jsonPatchFunc) *(*uintptr)(unsafe.Add(p, 1248)) = __ccgo_fp(_jsonPrettyFunc) *(*uintptr)(unsafe.Add(p, 1320)) = __ccgo_fp(_jsonPrettyFunc) *(*uintptr)(unsafe.Add(p, 1392)) = __ccgo_fp(_jsonQuoteFunc) *(*uintptr)(unsafe.Add(p, 1464)) = __ccgo_fp(_jsonRemoveFunc) *(*uintptr)(unsafe.Add(p, 1536)) = __ccgo_fp(_jsonRemoveFunc) *(*uintptr)(unsafe.Add(p, 1608)) = __ccgo_fp(_jsonReplaceFunc) *(*uintptr)(unsafe.Add(p, 1680)) = __ccgo_fp(_jsonReplaceFunc) *(*uintptr)(unsafe.Add(p, 1752)) = __ccgo_fp(_jsonSetFunc) *(*uintptr)(unsafe.Add(p, 1824)) = __ccgo_fp(_jsonSetFunc) *(*uintptr)(unsafe.Add(p, 1896)) = __ccgo_fp(_jsonTypeFunc) *(*uintptr)(unsafe.Add(p, 1968)) = __ccgo_fp(_jsonTypeFunc) *(*uintptr)(unsafe.Add(p, 2040)) = __ccgo_fp(_jsonValidFunc) *(*uintptr)(unsafe.Add(p, 2112)) = __ccgo_fp(_jsonValidFunc) *(*uintptr)(unsafe.Add(p, 2184)) = __ccgo_fp(_jsonArrayStep) *(*uintptr)(unsafe.Add(p, 2192)) = __ccgo_fp(_jsonArrayFinal) *(*uintptr)(unsafe.Add(p, 2200)) = __ccgo_fp(_jsonArrayValue) *(*uintptr)(unsafe.Add(p, 2208)) = __ccgo_fp(_jsonGroupInverse) *(*uintptr)(unsafe.Add(p, 2256)) = __ccgo_fp(_jsonArrayStep) *(*uintptr)(unsafe.Add(p, 2264)) = __ccgo_fp(_jsonArrayFinal) *(*uintptr)(unsafe.Add(p, 2272)) = __ccgo_fp(_jsonArrayValue) *(*uintptr)(unsafe.Add(p, 2280)) = __ccgo_fp(_jsonGroupInverse) *(*uintptr)(unsafe.Add(p, 2328)) = __ccgo_fp(_jsonObjectStep) *(*uintptr)(unsafe.Add(p, 2336)) = __ccgo_fp(_jsonObjectFinal) *(*uintptr)(unsafe.Add(p, 2344)) = __ccgo_fp(_jsonObjectValue) *(*uintptr)(unsafe.Add(p, 2352)) = __ccgo_fp(_jsonGroupInverse) *(*uintptr)(unsafe.Add(p, 2400)) = __ccgo_fp(_jsonObjectStep) *(*uintptr)(unsafe.Add(p, 2408)) = __ccgo_fp(_jsonObjectFinal) *(*uintptr)(unsafe.Add(p, 2416)) = __ccgo_fp(_jsonObjectValue) *(*uintptr)(unsafe.Add(p, 2424)) = __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 !(uint64(i) < libc.Uint64FromInt64(32)/libc.Uint64FromInt64(16) && 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 + 26262, FpModule: uintptr(unsafe.Pointer(&_jsonEachModule)), }, 1: { FzName: __ccgo_ts + 26272, 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 { 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 } type Rtree = TRtree type TRtreeCursor = struct { 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 { FpParent uintptr FiNode Ti64 FnRef int32 FisDirty int32 FzData uintptr FpNext uintptr } type RtreeNode = TRtreeNode type TRtreeCell = struct { FiRowid Ti64 FaCoord [10]TRtreeCoord } type RtreeCell = TRtreeCell type TRtreeConstraint = struct { FiCoord int32 Fop int32 Fu struct { FxGeom [0]uintptr FxQueryFunc [0]uintptr FrValue TRtreeDValue } FpInfo uintptr } type RtreeConstraint = TRtreeConstraint type TRtreeMatchArg = struct { FiSize Tu32 Fcb TRtreeGeomCallback FnParam int32 FapSqlParam uintptr 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 { FrScore TRtreeDValue Fid Tsqlite3_int64 FiLevel Tu8 FeWithin Tu8 FiCell Tu8 } 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 { 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 } 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 { FrScore TRtreeDValue Fid Tsqlite3_int64 FiLevel Tu8 FeWithin Tu8 FiCell Tu8 } 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 { 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 { FiCoord int32 Fop int32 Fu struct { FxGeom [0]uintptr FxQueryFunc [0]uintptr FrValue TRtreeDValue } FpInfo uintptr } 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 { FpParent uintptr 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 { 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 { FiSize Tu32 Fcb TRtreeGeomCallback FnParam int32 FapSqlParam uintptr 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) { var i Tu32 _ = i i = *(*Tu32)(unsafe.Pointer(pCoord)) *(*Tu8)(unsafe.Pointer(p)) = uint8(i >> libc.Int32FromInt32(24) & uint32(0xFF)) *(*Tu8)(unsafe.Pointer(p + 1)) = uint8(i >> libc.Int32FromInt32(16) & uint32(0xFF)) *(*Tu8)(unsafe.Pointer(p + 2)) = uint8(i >> libc.Int32FromInt32(8) & uint32(0xFF)) *(*Tu8)(unsafe.Pointer(p + 3)) = uint8(i >> libc.Int32FromInt32(0) & uint32(0xFF)) return int32(4) } func _writeInt64(tls *libc.TLS, p uintptr, i Ti64) (r int32) { *(*Tu8)(unsafe.Pointer(p)) = uint8(i >> libc.Int32FromInt32(56) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(p + 1)) = uint8(i >> libc.Int32FromInt32(48) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(p + 2)) = uint8(i >> libc.Int32FromInt32(40) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(p + 3)) = uint8(i >> libc.Int32FromInt32(32) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(p + 4)) = uint8(i >> libc.Int32FromInt32(24) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(p + 5)) = uint8(i >> libc.Int32FromInt32(16) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(p + 6)) = uint8(i >> libc.Int32FromInt32(8) & int64(0xFF)) *(*Tu8)(unsafe.Pointer(p + 7)) = uint8(i >> libc.Int32FromInt32(0) & int64(0xFF)) return int32(8) } // C documentation // // /* // ** Increment the reference count of node p. // */ func _nodeReference(tls *libc.TLS, p uintptr) { if p != 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.X__builtin___memset_chk(tls, (*TRtreeNode)(unsafe.Pointer(p)).FzData+2, 0, uint64((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize-int32(2)), libc.X__builtin_object_size(tls, (*TRtreeNode)(unsafe.Pointer(p)).FzData+2, 0)) (*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 + 192 + uintptr(_nodeHash(tls, iNode))*8)) 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 iHash = int32(_nodeHash(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FiNode)) (*TRtreeNode)(unsafe.Pointer(pNode)).FpNext = *(*uintptr)(unsafe.Pointer(pRtree + 192 + uintptr(iHash)*8)) *(*uintptr)(unsafe.Pointer(pRtree + 192 + uintptr(iHash)*8)) = 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 + 192 + uintptr(_nodeHash(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FiNode))*8 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != pNode) { break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 32 } *(*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(40)+uint64((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize)) if pNode != 0 { libc.X__builtin___memset_chk(tls, pNode, 0, uint64(40)+uint64((*TRtree)(unsafe.Pointer(pRtree)).FiNodeSize), libc.X__builtin_object_size(tls, pNode, 0)) (*TRtreeNode)(unsafe.Pointer(pNode)).FzData = pNode + 1*40 (*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))) switch (*TRtreeConstraint)(unsafe.Pointer(p)).Fop { case int32(RTREE_TRUE): return /* Always satisfied */ case int32(RTREE_FALSE): case int32(RTREE_EQ): libc.X__builtin___memcpy_chk(tls, bp, pCellData, uint64(4), libc.X__builtin_object_size(tls, bp, 0)) *(*Tu32)(unsafe.Pointer(bp)) = *(*Tu32)(unsafe.Pointer(bp))>>libc.Int32FromInt32(24)&uint32(0xff) | *(*Tu32)(unsafe.Pointer(bp))>>libc.Int32FromInt32(8)&uint32(0xff00) | *(*Tu32)(unsafe.Pointer(bp))&uint32(0xff)<= val { pCellData += uintptr(4) libc.X__builtin___memcpy_chk(tls, bp+4, pCellData, uint64(4), libc.X__builtin_object_size(tls, bp+4, 0)) *(*Tu32)(unsafe.Pointer(bp + 4)) = *(*Tu32)(unsafe.Pointer(bp + 4))>>libc.Int32FromInt32(24)&uint32(0xff) | *(*Tu32)(unsafe.Pointer(bp + 4))>>libc.Int32FromInt32(8)&uint32(0xff00) | *(*Tu32)(unsafe.Pointer(bp + 4))&uint32(0xff)<>libc.Int32FromInt32(24)&uint32(0xff) | *(*Tu32)(unsafe.Pointer(bp + 8))>>libc.Int32FromInt32(8)&uint32(0xff00) | *(*Tu32)(unsafe.Pointer(bp + 8))&uint32(0xff)<= val { return } default: pCellData += uintptr(4) libc.X__builtin___memcpy_chk(tls, bp+12, pCellData, uint64(4), libc.X__builtin_object_size(tls, bp+12, 0)) *(*Tu32)(unsafe.Pointer(bp + 12)) = *(*Tu32)(unsafe.Pointer(bp + 12))>>libc.Int32FromInt32(24)&uint32(0xff) | *(*Tu32)(unsafe.Pointer(bp + 12))>>libc.Int32FromInt32(8)&uint32(0xff00) | *(*Tu32)(unsafe.Pointer(bp + 12))&uint32(0xff)<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 */ pCellData += uintptr(int32(8) + (*TRtreeConstraint)(unsafe.Pointer(p)).FiCoord*int32(4)) libc.X__builtin___memcpy_chk(tls, bp, pCellData, uint64(4), libc.X__builtin_object_size(tls, bp, 0)) *(*Tu32)(unsafe.Pointer(bp)) = *(*Tu32)(unsafe.Pointer(bp))>>libc.Int32FromInt32(24)&uint32(0xff) | *(*Tu32)(unsafe.Pointer(bp))>>libc.Int32FromInt32(8)&uint32(0xff00) | *(*Tu32)(unsafe.Pointer(bp))&uint32(0xff)<= *(*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) 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)) *(*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 + 88 + uintptr(i)*8))) *(*uintptr)(unsafe.Pointer(p + 88 + uintptr(i)*8)) = uintptr(0) } else { pTemp = *(*uintptr)(unsafe.Pointer(p + 88 + uintptr(i)*8)) *(*uintptr)(unsafe.Pointer(p + 88 + uintptr(i)*8)) = *(*uintptr)(unsafe.Pointer(p + 88 + uintptr(j)*8)) *(*uintptr)(unsafe.Pointer(p + 88 + uintptr(j)*8)) = 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 + 64 } 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) if *(*uintptr)(unsafe.Pointer(pCur + 88 + uintptr(ii)*8)) == uintptr(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+88+uintptr(ii)*8) } return *(*uintptr)(unsafe.Pointer(pCur + 88 + uintptr(ii)*8)) } // 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(nNew)*uint64(24)) if pNew == uintptr(0) { return uintptr(0) } (*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint = pNew (*TRtreeCursor)(unsafe.Pointer(pCur)).FnPointAlloc = nNew } v2 = pCur + 36 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 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 + 128 + 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((int64(pNew)-int64((*TRtreeCursor)(unsafe.Pointer(pCur)).FaPoint))/24) + int32(1) if ii < int32(RTREE_CACHE_SZ) { *(*uintptr)(unsafe.Pointer(pCur + 88 + uintptr(ii)*8)) = *(*uintptr)(unsafe.Pointer(pCur + 88)) } else { _nodeRelease(tls, (*TRtreeCursor)(unsafe.Pointer(pCur)).Fbase.FpVtab, *(*uintptr)(unsafe.Pointer(pCur + 88))) } *(*uintptr)(unsafe.Pointer(pCur + 88)) = 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 + 64 } 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) if *(*uintptr)(unsafe.Pointer(p + 88 + uintptr(i)*8)) != 0 { _nodeRelease(tls, (*TRtreeCursor)(unsafe.Pointer(p)).Fbase.FpVtab, *(*uintptr)(unsafe.Pointer(p + 88 + uintptr(i)*8))) *(*uintptr)(unsafe.Pointer(p + 88 + uintptr(i)*8)) = uintptr(0) } if (*TRtreeCursor)(unsafe.Pointer(p)).FbPoint != 0 { *(*Tu32)(unsafe.Pointer(p + 128 + 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 + 128 + uintptr((*(*TRtreeSearchPoint)(unsafe.Pointer((*TRtreeCursor)(unsafe.Pointer(p)).FaPoint))).FiLevel)*4))-- v2 = p + 36 *(*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 + 88 + 1*8)) = *(*uintptr)(unsafe.Pointer(p + 88 + uintptr(n+int32(1))*8)) *(*uintptr)(unsafe.Pointer(p + 88 + uintptr(n+int32(1))*8)) = 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) 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 { 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+56, 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+26369) if pSrc == uintptr(0) { return int32(SQLITE_ERROR) } pInfo = Xsqlite3_malloc64(tls, uint64(112)+uint64((*TRtreeMatchArg)(unsafe.Pointer(pSrc)).FiSize)) if !(pInfo != 0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pInfo, 0, uint64(112), libc.X__builtin_object_size(tls, pInfo, 0)) pBlob = pInfo + 1*112 libc.X__builtin___memcpy_chk(tls, pBlob, pSrc, uint64((*TRtreeMatchArg)(unsafe.Pointer(pSrc)).FiSize), libc.X__builtin_object_size(tls, pBlob, 0)) (*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 + 56 (*Tsqlite3_rtree_query_info)(unsafe.Pointer(pInfo)).FapSqlParam = (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FapSqlParam if (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).Fcb.FxGeom != 0 { *(*uintptr)(unsafe.Pointer(pCons + 8)) = (*TRtreeMatchArg)(unsafe.Pointer(pBlob)).Fcb.FxGeom } else { (*TRtreeConstraint)(unsafe.Pointer(pCons)).Fop = int32(RTREE_QUERY) *(*uintptr)(unsafe.Pointer(pCons + 8)) = (*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+8 */ int32 var _ /* iNode at bp+24 */ Ti64 var _ /* pLeaf at bp+16 */ 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 + 8)) = 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 + 24)) = 0 eType = Xsqlite3_value_numeric_type(tls, *(*uintptr)(unsafe.Pointer(argv))) if eType == int32(SQLITE_INTEGER) || eType == int32(SQLITE_FLOAT) && 0 == _sqlite3IntFloatCompare(tls, iRowid, Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv)))) { rc = _findLeafNode(tls, pRtree, iRowid, bp+16, bp+24) } else { rc = SQLITE_OK *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) } if rc == SQLITE_OK && *(*uintptr)(unsafe.Pointer(bp + 16)) != uintptr(0) { p = _rtreeSearchPointNew(tls, pCsr, float64(0), uint8(0)) /* Always returns pCsr->sPoint */ *(*uintptr)(unsafe.Pointer(pCsr + 88)) = *(*uintptr)(unsafe.Pointer(bp + 16)) (*TRtreeSearchPoint)(unsafe.Pointer(p)).Fid = *(*Ti64)(unsafe.Pointer(bp + 24)) (*TRtreeSearchPoint)(unsafe.Pointer(p)).FeWithin = uint8(PARTLY_WITHIN) rc = _nodeRowidIndex(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 16)), 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. */ rc = _nodeAcquire(tls, pRtree, int64(1), uintptr(0), bp) if rc == SQLITE_OK && argc > 0 { (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint = Xsqlite3_malloc64(tls, uint64(24)*uint64(argc)) (*TRtreeCursor)(unsafe.Pointer(pCsr)).FnConstraint = argc if !((*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint != 0) { rc = int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint, 0, uint64(24)*uint64(argc), libc.X__builtin_object_size(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint, 0)) libc.X__builtin___memset_chk(tls, pCsr+128, 0, uint64(4)*uint64((*TRtree)(unsafe.Pointer(pRtree)).FiDepth+libc.Int32FromInt32(1)), libc.X__builtin_object_size(tls, pCsr+128, 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)*8))) (*TRtreeConstraint)(unsafe.Pointer(p1)).Fop = int32(*(*int8)(unsafe.Pointer(idxStr + uintptr(ii*int32(2))))) (*TRtreeConstraint)(unsafe.Pointer(p1)).FiCoord = int32(*(*int8)(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)*8)), 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 + 128 (*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)*8))) *(*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) *(*uintptr)(unsafe.Pointer(pCsr + 88)) = *(*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]int8 _, _, _, _, _, _, _, _, _, _, _, _ = 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.X__builtin___memset_chk(tls, bp, 0, uint64(41), libc.X__builtin_object_size(tls, bp, 0)) /* 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++ } ii = 0 for { if !(ii < (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FnConstraint && iIdx < int32(libc.Uint64FromInt64(41)-libc.Uint64FromInt32(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]int8)(unsafe.Pointer(bp)))[v4] = int8(op) v5 = iIdx iIdx++ (*(*[41]int8)(unsafe.Pointer(bp)))[v5] = int8((*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.X__builtin___memcpy_chk(tls, (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr, bp, uint64(iIdx+int32(1)), libc.X__builtin_object_size(tls, (*Tsqlite3_index_info)(unsafe.Pointer(pIdxInfo)).FidxStr, 0)) } 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) 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+16 */ TRtreeCell var _ /* cell at bp+64 */ TRtreeCell var _ /* pChild at bp+8 */ 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 + 8)) = 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+16) if _cellContains(tls, pRtree, bp+16, pCell) != 0 { area = _cellArea(tls, pRtree, bp+16) if bFound == 0 || area < fMinArea { iBest = (*(*TRtreeCell)(unsafe.Pointer(bp + 16))).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+64) area1 = _cellArea(tls, pRtree, bp+64) _cellUnion(tls, pRtree, bp+64, pCell) growth = _cellArea(tls, pRtree, bp+64) - area1 if iCell == 0 || growth < fMinGrowth || growth == fMinGrowth && area1 < fMinArea { fMinGrowth = growth fMinArea = area1 iBest = (*(*TRtreeCell)(unsafe.Pointer(bp + 64))).FiRowid } goto _3 _3: ; iCell++ } } rc = _nodeAcquire(tls, pRtree, iBest, *(*uintptr)(unsafe.Pointer(bp)), bp+8) _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp))) *(*uintptr)(unsafe.Pointer(bp)) = *(*uintptr)(unsafe.Pointer(bp + 8)) 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.X__builtin___memcpy_chk(tls, aSpare, aLeft, uint64(4)*uint64(nLeft), libc.X__builtin_object_size(tls, aSpare, 0)) 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(uint64(int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim)+libc.Int32FromInt32(1)) * (uint64(8) + uint64(nCell)*uint64(4))) aaSorted = Xsqlite3_malloc64(tls, uint64(nByte)) if !(aaSorted != 0) { return int32(SQLITE_NOMEM) } aSpare = aaSorted + uintptr((*TRtree)(unsafe.Pointer(pRtree)).FnDim)*8 + uintptr(int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim)*nCell)*4 libc.X__builtin___memset_chk(tls, aaSorted, 0, uint64(nByte), libc.X__builtin_object_size(tls, aaSorted, 0)) ii = 0 for { if !(ii < int32((*TRtree)(unsafe.Pointer(pRtree)).FnDim)) { break } *(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*8)) = aaSorted + uintptr((*TRtree)(unsafe.Pointer(pRtree)).FnDim)*8 + uintptr(ii*nCell)*4 jj = 0 for { if !(jj < nCell) { break } *(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*8)) + uintptr(jj)*4)) = jj goto _2 _2: ; jj++ } _SortByDimension(tls, pRtree, *(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*8)), 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.X__builtin___memcpy_chk(tls, bp, aCell+uintptr(*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*8)))))*48, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) libc.X__builtin___memcpy_chk(tls, bp+48, aCell+uintptr(*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*8)) + uintptr(nCell-int32(1))*4)))*48, uint64(48), libc.X__builtin_object_size(tls, bp+48, 0)) 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)*8)) + uintptr(kk)*4)))*48) } else { _cellUnion(tls, pRtree, bp+48, aCell+uintptr(*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(ii)*8)) + 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.X__builtin___memcpy_chk(tls, pBboxLeft, aCell+uintptr(*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(iBestDim)*8)))))*48, uint64(48), libc.X__builtin_object_size(tls, pBboxLeft, 0)) libc.X__builtin___memcpy_chk(tls, pBboxRight, aCell+uintptr(*(*int32)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(aaSorted + uintptr(iBestDim)*8)) + uintptr(iBestSplit)*4)))*48, uint64(48), libc.X__builtin_object_size(tls, pBboxRight, 0)) 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)*8)) + 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(32) defer tls.Free(32) var iChild Ti64 var rc, rc2, rc21 int32 var _ /* iCell at bp+8 */ int32 var _ /* pChild at bp+24 */ uintptr var _ /* pLeaf at bp+0 */ uintptr var _ /* pRoot at bp+16 */ uintptr _, _, _, _ = iChild, rc, rc2, rc21 /* Return code */ *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* Index of iDelete cell in pLeaf */ *(*uintptr)(unsafe.Pointer(bp + 16)) = 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+16) /* 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)) } /* 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+8) if rc == SQLITE_OK { rc = _deleteCell(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp)), *(*int32)(unsafe.Pointer(bp + 8)), 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 + 16)))).FzData+2) == int32(1) { *(*uintptr)(unsafe.Pointer(bp + 24)) = uintptr(0) iChild = _nodeGetRowid(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 16)), 0) rc = _nodeAcquire(tls, pRtree, iChild, *(*uintptr)(unsafe.Pointer(bp + 16)), bp+24) /* tag-20210916a */ if rc == SQLITE_OK { rc = _removeNode(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 24)), (*TRtree)(unsafe.Pointer(pRtree)).FiDepth-int32(1)) } rc21 = _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 24))) if rc == SQLITE_OK { rc = rc21 } if rc == SQLITE_OK { (*TRtree)(unsafe.Pointer(pRtree)).FiDepth-- _writeInt16(tls, (*TRtreeNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FzData, (*TRtree)(unsafe.Pointer(pRtree)).FiDepth) (*TRtreeNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).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 + 16))) } else { _nodeRelease(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 16))) } 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) zSql = Xsqlite3_mprintf(tls, __ccgo_ts+26383, 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+26403, 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+26435, 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))*8))) *(*TRtreeValue)(unsafe.Pointer(bp + 8 + uintptr(ii+int32(1))*4)) = _rtreeValueUp(tls, *(*uintptr)(unsafe.Pointer(aData + uintptr(ii+int32(4))*8))) 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))*8))) *(*int32)(unsafe.Pointer(bp + 8 + uintptr(ii+int32(1))*4)) = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(aData + uintptr(ii+int32(4))*8))) 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*8))) != int32(SQLITE_NULL) { (*(*TRtreeCell)(unsafe.Pointer(bp))).FiRowid = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(aData + 2*8))) 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)*8))) 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+26472, 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 + 26617 nRow = int64(RTREE_MIN_ROWEST) rc = Xsqlite3_table_column_metadata(tls, db, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, __ccgo_ts+12055, 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 !(uint64(i) < libc.Uint64FromInt64(24)/libc.Uint64FromInt64(8)) { 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 + 26673, 1: __ccgo_ts + 5896, 2: __ccgo_ts + 17132, } var _rtreeModule = Tsqlite3_module{ FiVersion: int32(4), } func init() { p := unsafe.Pointer(&_rtreeModule) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_rtreeCreate) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_rtreeConnect) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_rtreeBestIndex) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_rtreeDisconnect) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_rtreeDestroy) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_rtreeOpen) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_rtreeClose) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_rtreeFilter) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_rtreeNext) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_rtreeEof) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_rtreeColumn) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_rtreeRowid) *(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(_rtreeUpdate) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_rtreeBeginTransaction) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_rtreeEndTransaction) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(_rtreeEndTransaction) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_rtreeRollback) *(*uintptr)(unsafe.Add(p, 152)) = __ccgo_fp(_rtreeRename) *(*uintptr)(unsafe.Add(p, 160)) = __ccgo_fp(_rtreeSavepoint) *(*uintptr)(unsafe.Add(p, 184)) = __ccgo_fp(_rtreeShadowName) *(*uintptr)(unsafe.Add(p, 192)) = __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+27087, 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+27149, libc.VaList(bp+8, ii)) goto _1 _1: ; ii++ } Xsqlite3_str_appendf(tls, p, __ccgo_ts+27154, libc.VaList(bp+8, zDb, zPrefix)) Xsqlite3_str_appendf(tls, p, __ccgo_ts+27218, libc.VaList(bp+8, zDb, zPrefix)) Xsqlite3_str_appendf(tls, p, __ccgo_ts+27288, 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 + 120 appStmt[int32(1)] = pRtree + 128 appStmt[int32(2)] = pRtree + 136 appStmt[int32(3)] = pRtree + 144 appStmt[int32(4)] = pRtree + 152 appStmt[int32(5)] = pRtree + 160 appStmt[int32(6)] = pRtree + 168 appStmt[int32(7)] = pRtree + 176 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 + 27337 } 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+27445, 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+27490, 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+13563, int32(1)) } if ii1 < int32((*TRtree)(unsafe.Pointer(pRtree)).FnAuxNotNull) { Xsqlite3_str_appendf(tls, p1, __ccgo_ts+27517, libc.VaList(bp+8, ii1, ii1+int32(2), ii1)) } else { Xsqlite3_str_appendf(tls, p1, __ccgo_ts+27539, libc.VaList(bp+8, ii1, ii1+int32(2))) } goto _3 _3: ; ii1++ } Xsqlite3_str_appendf(tls, p1, __ccgo_ts+27547, 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+184, uintptr(0)) Xsqlite3_free(tls, zSql1) } } } return rc } var _azSql = [8]uintptr{ 0: __ccgo_ts + 26678, 1: __ccgo_ts + 26731, 2: __ccgo_ts + 26776, 3: __ccgo_ts + 26828, 4: __ccgo_ts + 26882, 5: __ccgo_ts + 26927, 6: __ccgo_ts + 26985, 7: __ccgo_ts + 27040, } // 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+27563, 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+3863, libc.VaList(bp+16, Xsqlite3_errmsg(tls, db))) } } else { zSql = Xsqlite3_mprintf(tls, __ccgo_ts+27583, libc.VaList(bp+16, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, (*TRtree)(unsafe.Pointer(pRtree)).FzName)) rc = _getIntFromStmt(tls, db, zSql, pRtree+32) if rc != SQLITE_OK { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3863, 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 + 27675, 2: __ccgo_ts + 27718, 3: __ccgo_ts + 27753, 4: __ccgo_ts + 27789, } /* 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+3863, 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*8)))) nName = int32(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(argv + 2*8)))) pRtree = Xsqlite3_malloc64(tls, uint64(968)+uint64(nDb)+uint64(nName*int32(2))+uint64(8)) if !(pRtree != 0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pRtree, 0, uint64(968)+uint64(nDb)+uint64(nName*int32(2))+uint64(8), libc.X__builtin_object_size(tls, pRtree, 0)) (*TRtree)(unsafe.Pointer(pRtree)).FnBusy = uint32(1) (*TRtree)(unsafe.Pointer(pRtree)).Fbase.FpModule = uintptr(unsafe.Pointer(&_rtreeModule)) (*TRtree)(unsafe.Pointer(pRtree)).FzDb = pRtree + 1*968 (*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.X__builtin___memcpy_chk(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, *(*uintptr)(unsafe.Pointer(argv + 1*8)), uint64(nDb), libc.X__builtin_object_size(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, 0)) libc.X__builtin___memcpy_chk(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzName, *(*uintptr)(unsafe.Pointer(argv + 2*8)), uint64(nName), libc.X__builtin_object_size(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzName, 0)) libc.X__builtin___memcpy_chk(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName, *(*uintptr)(unsafe.Pointer(argv + 2*8)), uint64(nName), libc.X__builtin_object_size(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName, 0)) libc.X__builtin___memcpy_chk(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName+uintptr(nName), __ccgo_ts+27826, uint64(6), libc.X__builtin_object_size(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName+uintptr(nName), 0)) /* 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+27832, libc.VaList(bp+8, _rtreeTokenLength(tls, *(*uintptr)(unsafe.Pointer(argv + 3*8))), *(*uintptr)(unsafe.Pointer(argv + 3*8)))) ii = int32(4) for { if !(ii < argc) { break } zArg = *(*uintptr)(unsafe.Pointer(argv + uintptr(ii)*8)) if int32(*(*int8)(unsafe.Pointer(zArg))) == int32('+') { (*TRtree)(unsafe.Pointer(pRtree)).FnAux++ Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+27856, 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+27883, 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+3863, 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+3863, 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+3863, 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*8)), *(*uintptr)(unsafe.Pointer(argv + 2*8)), isCreate) if rc != 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3863, 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) } _rtreeRelease(tls, pRtree) return rc } var _azFormat = [2]uintptr{ 0: __ccgo_ts + 27862, 1: __ccgo_ts + 27873, } // 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(1072) defer tls.Free(1072) var errCode, ii, jj, nData int32 var pOut uintptr var _ /* cell at bp+1008 */ TRtreeCell var _ /* node at bp+0 */ TRtreeNode var _ /* tree at bp+40 */ TRtree _, _, _, _, _ = errCode, ii, jj, nData, pOut _ = nArg libc.X__builtin___memset_chk(tls, bp, 0, uint64(40), libc.X__builtin_object_size(tls, bp, 0)) libc.X__builtin___memset_chk(tls, bp+40, 0, uint64(968), libc.X__builtin_object_size(tls, bp+40, 0)) (*(*TRtree)(unsafe.Pointer(bp + 40))).FnDim = uint8(Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(apArg)))) if int32((*(*TRtree)(unsafe.Pointer(bp + 40))).FnDim) < int32(1) || int32((*(*TRtree)(unsafe.Pointer(bp + 40))).FnDim) > int32(5) { return } (*(*TRtree)(unsafe.Pointer(bp + 40))).FnDim2 = uint8(int32((*(*TRtree)(unsafe.Pointer(bp + 40))).FnDim) * int32(2)) (*(*TRtree)(unsafe.Pointer(bp + 40))).FnBytesPerCell = uint8(int32(8) + int32(8)*int32((*(*TRtree)(unsafe.Pointer(bp + 40))).FnDim)) (*(*TRtreeNode)(unsafe.Pointer(bp))).FzData = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(apArg + 1*8))) if (*(*TRtreeNode)(unsafe.Pointer(bp))).FzData == uintptr(0) { return } nData = Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(apArg + 1*8))) if nData < int32(4) { return } if nData < _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(bp)).FzData+2)*int32((*(*TRtree)(unsafe.Pointer(bp + 40))).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+40, bp, ii, bp+1008) if ii > 0 { Xsqlite3_str_append(tls, pOut, __ccgo_ts+11675, int32(1)) } Xsqlite3_str_appendf(tls, pOut, __ccgo_ts+27886, libc.VaList(bp+1064, (*(*TRtreeCell)(unsafe.Pointer(bp + 1008))).FiRowid)) jj = 0 for { if !(jj < int32((*(*TRtree)(unsafe.Pointer(bp + 40))).FnDim2)) { break } Xsqlite3_str_appendf(tls, pOut, __ccgo_ts+27892, libc.VaList(bp+1064, float64(*(*TRtreeValue)(unsafe.Pointer(bp + 1008 + 8 + uintptr(jj)*4))))) goto _2 _2: ; jj++ } Xsqlite3_str_append(tls, pOut, __ccgo_ts+27896, 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+27898, -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 + 4615 } else { v1 = __ccgo_ts + 1664 } (*TRtreeCheck)(unsafe.Pointer(pCheck)).FzReport = Xsqlite3_mprintf(tls, __ccgo_ts+27931, 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+27938, 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.X__builtin___memcpy_chk(tls, pRet, pNode, uint64(nNode), libc.X__builtin_object_size(tls, pRet, 0)) *(*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+27983, 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 + 28015, 1: __ccgo_ts + 28069, } if *(*uintptr)(unsafe.Pointer(pCheck + 40 + uintptr(bLeaf)*8)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(pCheck + 40 + uintptr(bLeaf)*8)) = _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 + 40 + uintptr(bLeaf)*8)) Xsqlite3_bind_int64(tls, pStmt, int32(1), iKey) rc = Xsqlite3_step(tls, pStmt) if rc == int32(SQLITE_DONE) { if bLeaf != 0 { v1 = __ccgo_ts + 28117 } else { v1 = __ccgo_ts + 28125 } _rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+28134, 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 + 28117 } else { v2 = __ccgo_ts + 28125 } _rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+28179, 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+28237, 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+28285, 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 aNode = _rtreeCheckGetNode(tls, pCheck, iNode, bp) if aNode != 0 { if *(*int32)(unsafe.Pointer(bp)) < int32(4) { _rtreeCheckAppendMsg(tls, pCheck, __ccgo_ts+28352, 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+28386, 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+28416, 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+28471, 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+28502, 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(112) defer tls.Free(112) 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.X__builtin___memset_chk(tls, bp, 0, uint64(88), libc.X__builtin_object_size(tls, bp, 0)) (*(*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+28569, libc.VaList(bp+96, 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+26383, libc.VaList(bp+96, 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+28597, 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+28628, int64((*(*TRtreeCheck)(unsafe.Pointer(bp))).FnLeaf)) _rtreeCheckCount(tls, bp, __ccgo_ts+28635, 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 + 40))) Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp + 40 + 1*8))) *(*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 _ = 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+28643, 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+28662, -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 + 6997 } else { zTab = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apArg + 1*8))) } 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 + 18945 } 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]int8{ 9: int8(1), 10: int8(1), 13: int8(1), 32: int8(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 int8) { for _geopolyIsSpace[*(*uint8)(unsafe.Pointer((*TGeoParse)(unsafe.Pointer(p)).Fz))] != 0 { (*TGeoParse)(unsafe.Pointer(p)).Fz++ } return int8(*(*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 int8 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 = int8(*(*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 = int8(*(*uint8)(unsafe.Pointer(z + uintptr(j)))) if int32(_sqlite3CtypeMap[uint8(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 = int8(*(*uint8)(unsafe.Pointer(z + uintptr(j+int32(1))))) if int32(c) == int32('+') || int32(c) == int32('-') { j++ c = int8(*(*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(48) defer tls.Free(48) var aNew, pOut, v1 uintptr var c int8 var ii, rc int32 var v2 bool var _ /* s at bp+0 */ TGeoParse var _ /* x at bp+32 */ int32 _, _, _, _, _, _, _ = aNew, c, ii, pOut, rc, v1, v2 rc = SQLITE_OK libc.X__builtin___memset_chk(tls, bp, 0, uint64(32), libc.X__builtin_object_size(tls, bp, 0)) (*(*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((*(*TGeoParse)(unsafe.Pointer(bp))).FnAlloc)*uint64(4)*uint64(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 + 32)) = int32(1) (*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex-- /* Remove the redundant vertex at the end */ pOut = Xsqlite3_malloc64(tls, libc.Uint64FromInt64(40)+libc.Uint64FromInt64(4)*libc.Uint64FromInt32(2)*uint64(int64((*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex)-libc.Int64FromInt32(4))) *(*int32)(unsafe.Pointer(bp + 32)) = int32(1) if pOut == uintptr(0) { goto parse_json_err } (*TGeoPoly)(unsafe.Pointer(pOut)).FnVertex = (*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex libc.X__builtin___memcpy_chk(tls, pOut+8, (*(*TGeoParse)(unsafe.Pointer(bp))).Fa, uint64((*(*TGeoParse)(unsafe.Pointer(bp))).FnVertex*int32(2))*uint64(4), libc.X__builtin_object_size(tls, pOut+8, 0)) *(*uint8)(unsafe.Pointer(pOut + 4)) = *(*uint8)(unsafe.Pointer(bp + 32)) *(*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.Uint64FromInt32(4)+libc.Uint64FromInt32(6)*libc.Uint64FromInt64(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 int8 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 = int8('\'') Xsqlite3_str_appendf(tls, x, __ccgo_ts+28735, 0) i = 0 for { if !(i < (*TGeoPoly)(unsafe.Pointer(p)).FnVertex) { break } Xsqlite3_str_appendf(tls, x, __ccgo_ts+28753, 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 = int8(' ') goto _1 _1: ; i++ } Xsqlite3_str_appendf(tls, x, __ccgo_ts+28761, 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)*8))) if z != 0 && *(*int8)(unsafe.Pointer(z)) != 0 { Xsqlite3_str_appendf(tls, x, __ccgo_ts+28769, libc.VaList(bp+8, z)) } goto _2 _2: ; i++ } Xsqlite3_str_appendf(tls, x, __ccgo_ts+28773, 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*8))) B = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 2*8))) C = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 3*8))) D = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 4*8))) E = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 5*8))) F = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 6*8))) _ = 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 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*8))) r = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 2*8))) n = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 3*8))) _ = argc if n < int32(3) || r <= float64(0) { return } if n > int32(1000) { n = int32(1000) } p = Xsqlite3_malloc64(tls, uint64(40)+uint64((n-int32(1))*int32(2))*uint64(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, libc.Uint64FromInt64(40)+libc.Uint64FromInt64(4)*libc.Uint64FromInt32(2)*uint64(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.X__builtin___memset_chk(tls, aCoord, 0, libc.Uint64FromInt64(4)*libc.Uint64FromInt32(4), libc.X__builtin_object_size(tls, aCoord, 0)) } _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.X__builtin___memcpy_chk(tls, pBBox+4, bp, libc.Uint64FromInt64(4)*libc.Uint64FromInt32(4), libc.X__builtin_object_size(tls, pBBox+4, 0)) } 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*8))) y0 = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(argv + 2*8))) 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*8)), 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 { Fx float64 FeType int32 FpSeg uintptr FpNext uintptr } type GeoEvent = TGeoEvent type TGeoSegment = struct { 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 { Fx float64 FeType int32 FpSeg uintptr FpNext uintptr } type GeoEvent1 = TGeoEvent1 type TGeoSegment1 = struct { 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)*48 (*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)*32 (*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)*32 (*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)*32 (*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(uint64(32)*uint64(nVertex)*uint64(2) + uint64(48)*uint64(nVertex) + uint64(24)) p = Xsqlite3_malloc64(tls, uint64(nByte)) if p == uintptr(0) { return -int32(1) } (*TGeoOverlap)(unsafe.Pointer(p)).FaEvent = p + 1*24 (*TGeoOverlap)(unsafe.Pointer(p)).FaSegment = (*TGeoOverlap)(unsafe.Pointer(p)).FaEvent + uintptr(nVertex*int64(2))*32 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.X__builtin___memset_chk(tls, bp, 0, uint64(4), libc.X__builtin_object_size(tls, bp, 0)) 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*8)), 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*8)))) nName = int64(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(argv + 2*8)))) pRtree = Xsqlite3_malloc64(tls, uint64(968)+uint64(nDb)+uint64(nName*int64(2))+uint64(8)) if !(pRtree != 0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pRtree, 0, uint64(968)+uint64(nDb)+uint64(nName*int64(2))+uint64(8), libc.X__builtin_object_size(tls, pRtree, 0)) (*TRtree)(unsafe.Pointer(pRtree)).FnBusy = uint32(1) (*TRtree)(unsafe.Pointer(pRtree)).Fbase.FpModule = uintptr(unsafe.Pointer(&_rtreeModule)) (*TRtree)(unsafe.Pointer(pRtree)).FzDb = pRtree + 1*968 (*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.X__builtin___memcpy_chk(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, *(*uintptr)(unsafe.Pointer(argv + 1*8)), uint64(nDb), libc.X__builtin_object_size(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzDb, 0)) libc.X__builtin___memcpy_chk(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzName, *(*uintptr)(unsafe.Pointer(argv + 2*8)), uint64(nName), libc.X__builtin_object_size(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzName, 0)) libc.X__builtin___memcpy_chk(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName, *(*uintptr)(unsafe.Pointer(argv + 2*8)), uint64(nName), libc.X__builtin_object_size(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName, 0)) libc.X__builtin___memcpy_chk(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName+uintptr(nName), __ccgo_ts+27826, uint64(6), libc.X__builtin_object_size(tls, (*TRtree)(unsafe.Pointer(pRtree)).FzNodeName+uintptr(nName), 0)) /* 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+28786, 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+28808, libc.VaList(bp+8, *(*uintptr)(unsafe.Pointer(argv + uintptr(ii)*8)))) goto _1 _1: ; ii++ } Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+27883, 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+3863, 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*8)), *(*uintptr)(unsafe.Pointer(argv + 2*8)), isCreate) if rc != 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+3863, 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) } _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+32 */ [4]TRtreeCoord var _ /* iCell at bp+12 */ int32 var _ /* iNode at bp+24 */ Ti64 var _ /* pLeaf at bp+16 */ uintptr var _ /* pRoot at bp+0 */ uintptr var _ /* rc at bp+8 */ 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 + 8)) = SQLITE_OK *(*int32)(unsafe.Pointer(bp + 12)) = 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 + 24)) = 0 *(*int32)(unsafe.Pointer(bp + 8)) = _findLeafNode(tls, pRtree, iRowid, bp+16, bp+24) if *(*int32)(unsafe.Pointer(bp + 8)) == SQLITE_OK && *(*uintptr)(unsafe.Pointer(bp + 16)) != uintptr(0) { p = _rtreeSearchPointNew(tls, pCsr, float64(0), uint8(0)) /* Always returns pCsr->sPoint */ *(*uintptr)(unsafe.Pointer(pCsr + 88)) = *(*uintptr)(unsafe.Pointer(bp + 16)) (*TRtreeSearchPoint)(unsafe.Pointer(p)).Fid = *(*Ti64)(unsafe.Pointer(bp + 24)) (*TRtreeSearchPoint)(unsafe.Pointer(p)).FeWithin = uint8(PARTLY_WITHIN) *(*int32)(unsafe.Pointer(bp + 8)) = _nodeRowidIndex(tls, pRtree, *(*uintptr)(unsafe.Pointer(bp + 16)), iRowid, bp+12) (*TRtreeSearchPoint)(unsafe.Pointer(p)).FiCell = uint8(*(*int32)(unsafe.Pointer(bp + 12))) } 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 + 8)) = _nodeAcquire(tls, pRtree, int64(1), uintptr(0), bp) if *(*int32)(unsafe.Pointer(bp + 8)) == SQLITE_OK && idxNum <= int32(3) { _geopolyBBox(tls, uintptr(0), *(*uintptr)(unsafe.Pointer(argv)), bp+32, bp+8) if *(*int32)(unsafe.Pointer(bp + 8)) != 0 { goto geopoly_filter_end } v1 = Xsqlite3_malloc(tls, int32(libc.Uint64FromInt64(24)*libc.Uint64FromInt32(4))) p1 = v1 (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint = v1 (*TRtreeCursor)(unsafe.Pointer(pCsr)).FnConstraint = int32(4) if p1 == uintptr(0) { *(*int32)(unsafe.Pointer(bp + 8)) = int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint, 0, libc.Uint64FromInt64(24)*libc.Uint64FromInt32(4), libc.X__builtin_object_size(tls, (*TRtreeCursor)(unsafe.Pointer(pCsr)).FaConstraint, 0)) libc.X__builtin___memset_chk(tls, pCsr+128, 0, uint64(4)*uint64((*TRtree)(unsafe.Pointer(pRtree)).FiDepth+libc.Int32FromInt32(1)), libc.X__builtin_object_size(tls, pCsr+128, 0)) 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 + 32 + 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 + 32))) 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 + 32 + 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 + 32 + 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 + 32))) 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 + 32 + 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 + 32 + 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 + 32 + 3*4))) } } } if *(*int32)(unsafe.Pointer(bp + 8)) == 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 + 8)) = 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) *(*uintptr)(unsafe.Pointer(pCsr + 88)) = *(*uintptr)(unsafe.Pointer(bp)) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 8)) = _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 + 8)) } // 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 + 17132 (*(*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 + 28812 (*(*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 + 28818 (*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+56, 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*8))) != int32(SQLITE_NULL)) if newRowidValid != 0 { v2 = Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(aData + 1*8))) } 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*8))) != 0) || oldRowid != newRowid) { _geopolyBBox(tls, uintptr(0), *(*uintptr)(unsafe.Pointer(aData + 2*8)), 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+28827, 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) if Xsqlite3_value_nochange(tls, *(*uintptr)(unsafe.Pointer(aData + 2*8))) != 0 { Xsqlite3_bind_null(tls, pUp, int32(2)) } else { p = uintptr(0) if v4 = Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(aData + 2*8))) == int32(SQLITE_TEXT); v4 { v3 = _geopolyFuncParam(tls, uintptr(0), *(*uintptr)(unsafe.Pointer(aData + 2*8)), 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*8))) } 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))*8))) 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+28867) == 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+28883) == 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, 8)) = __ccgo_fp(_geopolyCreate) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_geopolyConnect) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_geopolyBestIndex) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_rtreeDisconnect) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_rtreeDestroy) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_rtreeOpen) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_rtreeClose) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_geopolyFilter) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_rtreeNext) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_rtreeEof) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_geopolyColumn) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_rtreeRowid) *(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(_geopolyUpdate) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_rtreeBeginTransaction) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_rtreeEndTransaction) *(*uintptr)(unsafe.Add(p, 128)) = __ccgo_fp(_rtreeEndTransaction) *(*uintptr)(unsafe.Add(p, 136)) = __ccgo_fp(_rtreeEndTransaction) *(*uintptr)(unsafe.Add(p, 144)) = __ccgo_fp(_geopolyFindFunction) *(*uintptr)(unsafe.Add(p, 152)) = __ccgo_fp(_rtreeRename) *(*uintptr)(unsafe.Add(p, 160)) = __ccgo_fp(_rtreeSavepoint) *(*uintptr)(unsafe.Add(p, 184)) = __ccgo_fp(_rtreeShadowName) *(*uintptr)(unsafe.Add(p, 192)) = __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 !(uint64(i) < libc.Uint64FromInt64(288)/libc.Uint64FromInt64(24) && 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 !(uint64(i) < libc.Uint64FromInt64(24)/libc.Uint64FromInt64(24) && 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+29060, 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 + 28898, }, 1: { FnArg: int8(1), FbPure: uint8(1), FzName: __ccgo_ts + 28911, }, 2: { FnArg: int8(1), FbPure: uint8(1), FzName: __ccgo_ts + 28924, }, 3: { FnArg: int8(-int32(1)), FbPure: uint8(1), FzName: __ccgo_ts + 28937, }, 4: { FnArg: int8(2), FbPure: uint8(1), FzName: __ccgo_ts + 28883, }, 5: { FnArg: int8(3), FbPure: uint8(1), FzName: __ccgo_ts + 28949, }, 6: { FnArg: int8(2), FbPure: uint8(1), FzName: __ccgo_ts + 28867, }, 7: { FnArg: int8(1), FzName: __ccgo_ts + 28972, }, 8: { FnArg: int8(1), FbPure: uint8(1), FzName: __ccgo_ts + 28986, }, 9: { FnArg: int8(7), FbPure: uint8(1), FzName: __ccgo_ts + 28999, }, 10: { FnArg: int8(4), FbPure: uint8(1), FzName: __ccgo_ts + 29013, }, 11: { FnArg: int8(1), FbPure: uint8(1), FzName: __ccgo_ts + 29029, }, } func init() { p := unsafe.Pointer(&_aFunc) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_geopolyAreaFunc) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_geopolyBlobFunc) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_geopolyJsonFunc) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_geopolySvgFunc) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_geopolyWithinFunc) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_geopolyContainsPointFunc) *(*uintptr)(unsafe.Add(p, 144)) = __ccgo_fp(_geopolyOverlapFunc) *(*uintptr)(unsafe.Add(p, 168)) = __ccgo_fp(_geopolyDebugFunc) *(*uintptr)(unsafe.Add(p, 192)) = __ccgo_fp(_geopolyBBoxFunc) *(*uintptr)(unsafe.Add(p, 216)) = __ccgo_fp(_geopolyXformFunc) *(*uintptr)(unsafe.Add(p, 240)) = __ccgo_fp(_geopolyRegularFunc) *(*uintptr)(unsafe.Add(p, 264)) = __ccgo_fp(_geopolyCcwFunc) } var _aAgg = [1]struct { FxStep uintptr FxFinal uintptr FzName uintptr }{ 0: { FzName: __ccgo_ts + 29041, }, } func init() { p := unsafe.Pointer(&_aAgg) *(*uintptr)(unsafe.Add(p, 0)) = __ccgo_fp(_geopolyBBoxStep) *(*uintptr)(unsafe.Add(p, 8)) = __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+29068, int32(2), utf8, uintptr(0), __ccgo_fp(_rtreenode), uintptr(0), uintptr(0)) if rc == SQLITE_OK { rc = Xsqlite3_create_function(tls, db, __ccgo_ts+29078, int32(1), utf8, uintptr(0), __ccgo_fp(_rtreedepth), uintptr(0), uintptr(0)) } if rc == SQLITE_OK { rc = Xsqlite3_create_function(tls, db, __ccgo_ts+29089, -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+28812, 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+29100, 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)*8))) 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(uint64(64) + uint64(nArg-libc.Int32FromInt32(1))*uint64(8) + uint64(nArg)*uint64(8)) 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 + 56 + 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)*8)) = Xsqlite3_value_dup(tls, *(*uintptr)(unsafe.Pointer(aArg + uintptr(i)*8))) if *(*uintptr)(unsafe.Pointer((*TRtreeMatchArg)(unsafe.Pointer(pBlob)).FapSqlParam + uintptr(i)*8)) == uintptr(0) { memErr = int32(1) } *(*TRtreeDValue)(unsafe.Pointer(pBlob + 56 + uintptr(i)*8)) = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(aArg + uintptr(i)*8))) 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+26369, __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(32)) 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(32)) 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 { FeStage int32 FdbMain uintptr FdbRbu uintptr FzTarget uintptr FzRbu uintptr FzState uintptr FzStateDb [5]int8 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 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 { FeStage int32 FzTbl uintptr FzDataTbl uintptr FzIdx uintptr FiWalCksum Ti64 FnRow int32 FnProgress Ti64 FiCookie Tu32 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 { 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 { FeStage int32 FzTbl uintptr FzDataTbl uintptr FzIdx uintptr FiWalCksum Ti64 FnRow int32 FnProgress Ti64 FiCookie Tu32 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 { FeStage int32 FdbMain uintptr FdbRbu uintptr FzTarget uintptr FzRbu uintptr FzState uintptr FzStateDb [5]int8 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 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 { 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, p3 uintptr _, _, _, _, _, _, _ = c, v, z, zStart, v1, v2, p3 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+8) switch int32(*(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp))))) { case int32('@'): *(*uintptr)(unsafe.Pointer(bp))++ *(*int32)(unsafe.Pointer(bp + 8))-- ofst = _rbuDeltaGetInt(tls, bp, bp+8) if *(*int32)(unsafe.Pointer(bp + 8)) > 0 && int32(*(*int8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp))))) != int32(',') { /* ERROR: copy command not terminated by ',' */ return -int32(1) } *(*uintptr)(unsafe.Pointer(bp))++ *(*int32)(unsafe.Pointer(bp + 8))-- 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.X__builtin___memcpy_chk(tls, zOut, zSrc+uintptr(ofst), uint64(cnt), libc.X__builtin_object_size(tls, zOut, 0)) zOut += uintptr(cnt) case int32(':'): *(*uintptr)(unsafe.Pointer(bp))++ *(*int32)(unsafe.Pointer(bp + 8))-- total += cnt if total > limit { /* ERROR: insert command gives an output larger than predicted */ return -int32(1) } if int32(cnt) > *(*int32)(unsafe.Pointer(bp + 8)) { /* ERROR: insert count exceeds size of delta */ return -int32(1) } libc.X__builtin___memcpy_chk(tls, zOut, *(*uintptr)(unsafe.Pointer(bp)), uint64(cnt), libc.X__builtin_object_size(tls, zOut, 0)) zOut += uintptr(cnt) *(*uintptr)(unsafe.Pointer(bp)) += uintptr(cnt) *(*int32)(unsafe.Pointer(bp + 8)) = int32(uint32(*(*int32)(unsafe.Pointer(bp + 8))) - cnt) case int32(';'): *(*uintptr)(unsafe.Pointer(bp))++ *(*int32)(unsafe.Pointer(bp + 8))-- *(*int8)(unsafe.Pointer(zOut)) = 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 + 8)) = _lenDelta var size int32 _ = size size = int32(_rbuDeltaGetInt(tls, bp, bp+8)) if int32(*(*int8)(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 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*8))) aDelta = Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) /* Figure out the size of the output */ nOut = _rbuDeltaOutputSize(tls, aDelta, nDelta) if nOut < 0 { Xsqlite3_result_error(tls, context, __ccgo_ts+29110, -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+29110, -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+3863, 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+3863, 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 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)*8))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(i)*8))) 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.X__builtin___memset_chk(tls, pIter, 0, uint64(192), libc.X__builtin_object_size(tls, pIter, 0)) } // 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+29131, uintptr(0), uintptr(0), p+64) } 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+64) (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl = uintptr(0) (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl = 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+64) (*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) zIn = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(argv))) if zIn != 0 { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { if argc == int32(1) || 0 == Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(argv + 1*8))) { Xsqlite3_result_text(tls, pCtx, zIn, -int32(1), libc.UintptrFromInt32(0)) } } else { if libc.Xstrlen(tls, zIn) > uint64(4) && libc.Xmemcmp(tls, __ccgo_ts+26282, zIn, uint64(4)) == 0 { i = int32(4) for { if !(int32(*(*int8)(unsafe.Pointer(zIn + uintptr(i)))) >= int32('0') && int32(*(*int8)(unsafe.Pointer(zIn + uintptr(i)))) <= int32('9')) { break } goto _1 _1: ; i++ } if int32(*(*int8)(unsafe.Pointer(zIn + uintptr(i)))) == int32('_') && *(*int8)(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.X__builtin___memset_chk(tls, pIter, 0, uint64(192), libc.X__builtin_object_size(tls, pIter, 0)) if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v1 = __ccgo_ts + 29302 } else { v1 = __ccgo_ts + 1664 } rc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, pIter, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+29343, libc.VaList(bp+8, v1))) if rc == SQLITE_OK { rc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+8, p+64, __ccgo_ts+29493) } (*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+64) } } 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 { pRet = Xsqlite3_malloc64(tls, uint64(nByte)) if pRet == uintptr(0) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, pRet, 0, uint64(nByte), libc.X__builtin_object_size(tls, pRet, 0)) } } 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.Uint64FromInt32(2)*libc.Uint64FromInt64(8) + libc.Uint64FromInt64(4) + libc.Uint64FromInt32(3)*libc.Uint64FromInt64(1)) * uint64(nCol)) azNew = _rbuMalloc(tls, p, nByte) if azNew != 0 { (*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol = azNew (*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType = azNew + uintptr(nCol)*8 (*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder = (*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(nCol)*8 (*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) + uint64(1) zRet = Xsqlite3_malloc64(tls, nCopy) if zRet != 0 { libc.X__builtin___memcpy_chk(tls, zRet, zStr, nCopy, libc.X__builtin_object_size(tls, zRet, 0)) } 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+3863, 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(48) defer tls.Free(48) 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 (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+29618, libc.VaList(bp+40, 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*8, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+29737, libc.VaList(bp+40, 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*8, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+29758, libc.VaList(bp+40, 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*8, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+29809, libc.VaList(bp+40, 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 !(uint64(i) < libc.Uint64FromInt64(32)/libc.Uint64FromInt64(8)) { 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+8 */ uintptr _, _, _, _ = bIndex, bPartial, iCid, zIdx *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) bIndex = 0 if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { libc.X__builtin___memcpy_chk(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk, uint64(1)*uint64((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol), libc.X__builtin_object_size(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed, 0)) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+29830, libc.VaList(bp+24, (*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 + 8)) = uintptr(0) if zIdx == uintptr(0) { break } if bPartial != 0 { libc.X__builtin___memset_chk(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed, int32(0x01), uint64(1)*uint64((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol), libc.X__builtin_object_size(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed, 0)) } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+8, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+29858, libc.VaList(bp+24, zIdx))) for (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) { iCid = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), int32(1)) if iCid >= 0 { *(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed + uintptr(iCid))) = uint8(1) } if iCid == -int32(2) { libc.X__builtin___memset_chk(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed, int32(0x01), uint64(1)*uint64((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol), libc.X__builtin_object_size(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed, 0)) } } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp + 8))) 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(48) defer tls.Free(48) var bNotNull, bRbuRowid, i, iOrder, iPk, nCol, t, v2 int32 var t1, zCopy, zName, zName1, zType, v3, v4 uintptr var _ /* iTnum at bp+8 */ 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 + 8)) = 0 /* Figure out the type of table this step will deal with. */ _rbuTableType(tls, p, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzTbl, pIter+72, bp+8, pIter+108) 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+20431, libc.VaList(bp+24, (*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 + 8)) } /* 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+64, Xsqlite3_mprintf(tls, __ccgo_ts+29887, libc.VaList(bp+24, (*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+29906, zName, int32(4)) != 0 { zCopy = _rbuStrndup(tls, zName, p+56) *(*int32)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol)*4)) = (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol v3 = pIter + 16 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(v2)*8)) = zCopy } else { if 0 == Xsqlite3_stricmp(tls, __ccgo_ts+29911, 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 + 29921 } else { v4 = __ccgo_ts + 29934 } (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+29943, libc.VaList(bp+24, (*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+64, Xsqlite3_mprintf(tls, __ccgo_ts+29972, libc.VaList(bp+24, (*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)*8))) { 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+29994, libc.VaList(bp+24, (*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)*8)) *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*8)) = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iOrder)*8)) *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iOrder)*8)) = t1 } *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(iOrder)*8)) = _rbuStrndup(tls, zType, p+56) *(*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) } 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 + 1664 i = 0 for { if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) { break } z = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*8)) zList = _rbuMPrintf(tls, p, __ccgo_ts+30021, libc.VaList(bp+8, zList, zSep, z)) zSep = __ccgo_ts + 15431 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 + 1664 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)*8)) zRet = _rbuMPrintf(tls, p, __ccgo_ts+30030, 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+64, Xsqlite3_mprintf(tls, __ccgo_ts+30043, 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+30075, libc.VaList(bp+16, iMax)) } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) } else { zOrder = _rbuObjIterGetPkList(tls, p, pIter, __ccgo_ts+1664, __ccgo_ts+15431, __ccgo_ts+30098) zSelect = _rbuObjIterGetPkList(tls, p, pIter, __ccgo_ts+30104, __ccgo_ts+30111, __ccgo_ts+5518) zList = _rbuObjIterGetPkList(tls, p, pIter, __ccgo_ts+1664, __ccgo_ts+15431, __ccgo_ts+1664) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+30119, 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+30161, 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+8 */ 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 + 1664 iCol = 0 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+29858, libc.VaList(bp+24, (*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++ } zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*8)) } else { zCol = __ccgo_ts + 30181 } } else { zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iCid)*8)) } zLhs = _rbuMPrintf(tls, p, __ccgo_ts+30189, libc.VaList(bp+24, zLhs, zSep, zCol, zCollate)) zOrder = _rbuMPrintf(tls, p, __ccgo_ts+30210, libc.VaList(bp+24, zOrder, zSep, iCol, zCol, zCollate)) zSelect = _rbuMPrintf(tls, p, __ccgo_ts+30246, libc.VaList(bp+24, zSelect, zSep, iCol, zCol)) zSep = __ccgo_ts + 15431 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 + 8)) = uintptr(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+8, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+30273, libc.VaList(bp+24, 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 + 8))) { zSep = __ccgo_ts + 1664 iCol = 0 for { if !(iCol < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol) { break } zQuoted = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), iCol) if zQuoted == uintptr(0) { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } else { if int32(*(*int8)(unsafe.Pointer(zQuoted))) == int32('N') { bFailed = int32(1) break } } zVector = _rbuMPrintf(tls, p, __ccgo_ts+30321, libc.VaList(bp+24, zVector, zSep, zQuoted)) zSep = __ccgo_ts + 15431 goto _2 _2: ; iCol++ } if !(bFailed != 0) { zRet = _rbuMPrintf(tls, p, __ccgo_ts+30328, libc.VaList(bp+24, zLhs, zVector)) } } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp + 8))) } 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 + 1664 /* Set to ", " later on */ zAnd = __ccgo_ts + 1664 /* Set to " AND " later on */ *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* PRAGMA index_xinfo = ? */ if rc == SQLITE_OK { rc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+29858, 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+30340, libc.VaList(bp+16, zRet, zCom, (*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iSeq)*16))).FnSpan, (*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iSeq)*16))).FzSpan, zCollate)) zType = __ccgo_ts + 1664 } 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++ } zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*8)) } else { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { zCol = __ccgo_ts + 30181 } else { zCol = __ccgo_ts + 29911 } } zType = __ccgo_ts + 1138 } else { zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iCid)*8)) zType = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(iCid)*8)) } zRet = Xsqlite3_mprintf(tls, __ccgo_ts+30362, 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 + 30098 } else { v2 = __ccgo_ts + 1664 } zOrder = v2 zImpPK = Xsqlite3_mprintf(tls, __ccgo_ts+30382, libc.VaList(bp+16, zImpPK, zCom, nBind, zCol, zOrder)) } zImpCols = Xsqlite3_mprintf(tls, __ccgo_ts+30403, libc.VaList(bp+16, zImpCols, zCom, nBind, zCol, zType, zCollate)) zWhere = Xsqlite3_mprintf(tls, __ccgo_ts+30436, 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 + 15431 zAnd = __ccgo_ts + 22553 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 + 1664 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)*8)) zList = Xsqlite3_mprintf(tls, __ccgo_ts+30460, libc.VaList(bp+8, zList, zS, zObj, zCol)) } else { zList = Xsqlite3_mprintf(tls, __ccgo_ts+30472, libc.VaList(bp+8, zList, zS)) } zS = __ccgo_ts + 15431 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+30481, 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+30496, 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 + 1664 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+30510, libc.VaList(bp+8, zList, zSep, i, i+int32(1))) zSep = __ccgo_ts + 22553 } goto _1 _1: ; i++ } zList = _rbuMPrintf(tls, p, __ccgo_ts+30522, libc.VaList(bp+8, zList)) } else { zSep1 = __ccgo_ts + 1664 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)*8)) zList = _rbuMPrintf(tls, p, __ccgo_ts+30572, libc.VaList(bp+8, zList, zSep1, zCol, i1+int32(1))) zSep1 = __ccgo_ts + 22553 } 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+30585, 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 int8 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 + 1664 i = 0 for { if !(i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) { break } c = *(*int8)(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+30572, libc.VaList(bp+8, zList, zSep, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*8)), i+int32(1))) zSep = __ccgo_ts + 15431 } else { if int32(c) == int32('d') { zList = _rbuMPrintf(tls, p, __ccgo_ts+30611, libc.VaList(bp+8, zList, zSep, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*8)), *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*8)), i+int32(1))) zSep = __ccgo_ts + 15431 } else { if int32(c) == int32('f') { zList = _rbuMPrintf(tls, p, __ccgo_ts+30641, libc.VaList(bp+8, zList, zSep, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*8)), *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(i)*8)), i+int32(1))) zSep = __ccgo_ts + 15431 } } } 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 } *(*int8)(unsafe.Pointer(zRet + uintptr(i*int32(2)))) = int8('?') if i+int32(1) == nBind { v2 = int32('\000') } else { v2 = int32(',') } *(*int8)(unsafe.Pointer(zRet + uintptr(i*int32(2)+int32(1)))) = int8(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(64) defer tls.Free(64) var z, zCol, zDesc, zIdx, zOrig, zSep, v1 uintptr var _ /* pXInfo at bp+8 */ uintptr var _ /* pXList at bp+0 */ uintptr _, _, _, _, _, _, _ = z, zCol, zDesc, zIdx, zOrig, zSep, v1 z = uintptr(0) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { zSep = __ccgo_ts + 30678 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) /* PRAGMA index_list = (pIter->zTbl) */ *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) /* PRAGMA index_xinfo = */ (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+29830, libc.VaList(bp+24, (*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+17020) == 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+8, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+29858, libc.VaList(bp+24, 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 + 8))) { if Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), int32(5)) != 0 { /* int iCid = sqlite3_column_int(pXInfo, 0); */ zCol = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), int32(2)) if Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), int32(3)) != 0 { v1 = __ccgo_ts + 30098 } else { v1 = __ccgo_ts + 1664 } zDesc = v1 z = _rbuMPrintf(tls, p, __ccgo_ts+30691, libc.VaList(bp+24, z, zSep, zCol, zDesc)) zSep = __ccgo_ts + 15431 } } z = _rbuMPrintf(tls, p, __ccgo_ts+30702, libc.VaList(bp+24, z)) _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp + 8))) } 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+8 */ 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 + 8)) = uintptr(0) /* PRAGMA main.index_xinfo = $zIdx */ zComma = __ccgo_ts + 1664 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+64, __ccgo_ts+30706) 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+8, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+29858, libc.VaList(bp+24, 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 + 8))) { bKey = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), int32(5)) if bKey != 0 { iCid = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), int32(1)) bDesc = Xsqlite3_column_int(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), int32(3)) zCollate = Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), int32(4)) zCols = _rbuMPrintf(tls, p, __ccgo_ts+30756, libc.VaList(bp+24, zCols, zComma, iCid, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(iCid)*8)), zCollate)) if bDesc != 0 { v1 = __ccgo_ts + 30098 } else { v1 = __ccgo_ts + 1664 } zPk = _rbuMPrintf(tls, p, __ccgo_ts+30778, libc.VaList(bp+24, zPk, zComma, iCid, v1)) zComma = __ccgo_ts + 15431 } } zCols = _rbuMPrintf(tls, p, __ccgo_ts+30788, libc.VaList(bp+24, zCols)) _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp + 8))) Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+24, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6997, int32(1), tnum)) _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+30803, libc.VaList(bp+24, zCols, zPk)) Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+24, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6997, 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 + 1664 zSql = uintptr(0) Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+16, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6997, 0, int32(1))) iCol = 0 for { if !((*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && iCol < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol) { break } zPk = __ccgo_ts + 1664 zCol = *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblCol + uintptr(iCol)*8)) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_table_column_metadata(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6997, (*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 + 30865 } if *(*Tu8)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FabNotNull + uintptr(iCol))) != 0 { v2 = __ccgo_ts + 30878 } else { v2 = __ccgo_ts + 1664 } zSql = _rbuMPrintf(tls, p, __ccgo_ts+30888, libc.VaList(bp+16, zSql, zComma, zCol, *(*uintptr)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FazTblType + uintptr(iCol)*8)), zPk, *(*uintptr)(unsafe.Pointer(bp)), v2)) zComma = __ccgo_ts + 15431 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+30915, 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+6997, int32(1), tnum)) if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_WITHOUT_ROWID) { v3 = __ccgo_ts + 30922 } else { v3 = __ccgo_ts + 1664 } _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+30937, 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+6997, 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 { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, pIter+152, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+30969, libc.VaList(bp+8, p+48, (*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 if Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(apVal))) != 0 { *(*Ti64)(unsafe.Pointer(p + 304)) += 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)*8))) 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 int8 var i, iIdxCol, nIdxAlloc, nParen, nSpan, nSpan1, rc2, v3, v4 int32 var _ /* pStmt at bp+0 */ uintptr var _ /* rc at bp+8 */ 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 + 8)) = (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc zRet = uintptr(0) if *(*int32)(unsafe.Pointer(bp + 8)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 8)) = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp, p+64, __ccgo_ts+31026) } if *(*int32)(unsafe.Pointer(bp + 8)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 8)) = 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 + 8)) == 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+8) zSql = v1 (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdxSql = v1 } if zSql != 0 { nParen = 0 iIdxCol = 0 nIdxAlloc = 0 i = 0 for { if !(*(*int8)(unsafe.Pointer(zSql + uintptr(i))) != 0) { break } c = *(*int8)(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(uint64(nIdxAlloc+libc.Int32FromInt32(16))*uint64(16))) if aIdxCol == uintptr(0) { *(*int32)(unsafe.Pointer(bp + 8)) = int32(SQLITE_NOMEM) break } (*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol = aIdxCol nIdxAlloc += int32(16) } if int32(c) == int32('(') { if nParen == 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 = int32(t__predefined_ptrdiff_t(zSql+uintptr(i)) - int64((*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iIdxCol)*16))).FzSpan)) v3 = iIdxCol iIdxCol++ (*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(v3)*16))).FnSpan = nSpan i++ break } } else { if int32(c) == int32(',') && nParen == int32(1) { nSpan1 = int32(t__predefined_ptrdiff_t(zSql+uintptr(i)) - int64((*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iIdxCol)*16))).FzSpan)) v4 = iIdxCol iIdxCol++ (*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(v4)*16))).FnSpan = nSpan1 (*(*TRbuSpan)(unsafe.Pointer((*TRbuObjIter)(unsafe.Pointer(pIter)).FaIdxCol + uintptr(iIdxCol)*16))).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(*(*int8)(unsafe.Pointer(zSql + uintptr(i)))) == int32(c) { if int32(*(*int8)(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(*(*int8)(unsafe.Pointer(zSql + uintptr(i)))) == int32(']') { break } goto _6 _6: ; i++ } } else { if int32(c) == int32('-') && int32(*(*int8)(unsafe.Pointer(zSql + uintptr(i+int32(1))))) == int32('-') { i = i + int32(2) for { if !(*(*int8)(unsafe.Pointer(zSql + uintptr(i))) != 0 && int32(*(*int8)(unsafe.Pointer(zSql + uintptr(i)))) != int32('\n')) { break } goto _7 _7: ; i++ } if int32(*(*int8)(unsafe.Pointer(zSql + uintptr(i)))) == int32('\000') { break } } else { if int32(c) == int32('/') && int32(*(*int8)(unsafe.Pointer(zSql + uintptr(i+int32(1))))) == int32('*') { i = i + int32(2) for { if !(*(*int8)(unsafe.Pointer(zSql + uintptr(i))) != 0 && (int32(*(*int8)(unsafe.Pointer(zSql + uintptr(i)))) != int32('*') || int32(*(*int8)(unsafe.Pointer(zSql + uintptr(i+int32(1))))) != int32('/'))) { break } goto _8 _8: ; i++ } if int32(*(*int8)(unsafe.Pointer(zSql + uintptr(i)))) == int32('\000') { break } i++ } } } } } } } goto _2 _2: ; i++ } if *(*int8)(unsafe.Pointer(zSql + uintptr(i))) != 0 { zRet = _rbuStrndup(tls, zSql+uintptr(i), bp+8) } (*TRbuObjIter)(unsafe.Pointer(pIter)).FnIdxCol = iIdxCol } } rc2 = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) if *(*int32)(unsafe.Pointer(bp + 8)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 8)) = rc2 } } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = *(*int32)(unsafe.Pointer(bp + 8)) 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(128) defer tls.Free(128) 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+24 */ int32 var _ /* zImposterCols at bp+0 */ uintptr var _ /* zImposterPK at bp+8 */ uintptr var _ /* zWhere at bp+16 */ 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 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 + 64 zIdx = (*TRbuObjIter)(unsafe.Pointer(pIter)).FzIdx zLimit = uintptr(0) if nOffset != 0 { zLimit = Xsqlite3_mprintf(tls, __ccgo_ts+31092, libc.VaList(bp+40, 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 + 8)) = uintptr(0) /* Primary key declaration for imposter */ *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) /* WHERE clause on PK columns */ zBind = uintptr(0) zPart = uintptr(0) *(*int32)(unsafe.Pointer(bp + 24)) = 0 zPart = _rbuObjIterGetIndexWhere(tls, p, pIter) zCollist = _rbuObjIterGetIndexCols(tls, p, pIter, bp, bp+8, bp+16, bp+24) zBind = _rbuObjIterGetBindlist(tls, p, *(*int32)(unsafe.Pointer(bp + 24))) /* Create the imposter table used to write to this index. */ Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+40, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6997, 0, int32(1))) Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+40, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6997, int32(1), tnum)) _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+31112, libc.VaList(bp+40, zTbl, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 8)))) Xsqlite3_test_control(tls, int32(SQLITE_TESTCTRL_IMPOSTER), libc.VaList(bp+40, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+6997, 0, 0)) /* Create the statement to insert index entries */ (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol = *(*int32)(unsafe.Pointer(bp + 24)) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+136, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+31177, libc.VaList(bp+40, 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+144, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+31213, libc.VaList(bp+40, zTbl, *(*uintptr)(unsafe.Pointer(bp + 16))))) } /* 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 + 31247 } else { v2 = __ccgo_ts + 31251 } v1 = v2 } else { v1 = __ccgo_ts + 1664 } zSql = Xsqlite3_mprintf(tls, __ccgo_ts+31257, libc.VaList(bp+40, 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+31318, libc.VaList(bp+40, zCollist, p+48, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, zPart, zCollist, zLimit)) } else { if zPart != 0 { v3 = __ccgo_ts + 31247 } else { v3 = __ccgo_ts + 31251 } zSql = Xsqlite3_mprintf(tls, __ccgo_ts+31379, libc.VaList(bp+40, zCollist, p+48, (*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+128, pz, zSql) } else { Xsqlite3_free(tls, zSql) } } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) 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+7008) zNewlist = _rbuObjIterGetOldlist(tls, p, pIter, __ccgo_ts+7004) 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 + 1664 } else { v4 = __ccgo_ts + 31538 } 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 + 31547 } else { v5 = __ccgo_ts + 1664 } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, pIter+136, pz, Xsqlite3_mprintf(tls, __ccgo_ts+31557, libc.VaList(bp+40, 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+144, pz, Xsqlite3_mprintf(tls, __ccgo_ts+31593, libc.VaList(bp+40, zWrite, zTbl1, zWhere1))) } if libc.BoolInt32((*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0)) == 0 && (*TRbuObjIter)(unsafe.Pointer(pIter)).FabIndexed != 0 { zRbuRowid = __ccgo_ts + 1664 if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_EXTERNAL) || (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType == int32(RBU_PK_NONE) { zRbuRowid = __ccgo_ts + 31621 } /* 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 + 31633 } else { v6 = __ccgo_ts + 1664 } _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+31650, libc.VaList(bp+40, p+48, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl, v6, (*TRbuObjIter)(unsafe.Pointer(pIter)).FzDataTbl)) _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+31726, libc.VaList(bp+40, 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+32025, libc.VaList(bp+40, 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 + 1664 zStart1 = uintptr(0) zOrder = uintptr(0) if bRbuRowid != 0 { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v7 = __ccgo_ts + 32124 } else { v7 = __ccgo_ts + 32134 } 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+30181, 0) } else { zOrder = _rbuObjIterGetPkList(tls, p, pIter, __ccgo_ts+1664, __ccgo_ts+15431, __ccgo_ts+1664) } } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v8 = __ccgo_ts + 32145 } else { v8 = __ccgo_ts + 1664 } if zStart1 != 0 { v9 = zStart1 } else { v9 = __ccgo_ts + 1664 } if zOrder != 0 { v10 = __ccgo_ts + 23747 } else { v10 = __ccgo_ts + 1664 } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, pIter+128, pz, Xsqlite3_mprintf(tls, __ccgo_ts+32151, libc.VaList(bp+40, 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 + 184 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)) + 16 } if nUp >= int32(SQLITE_RBU_UPDATE_CACHESIZE) { pp = pIter + 184 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != pUp) { break } goto _2 _2: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 16 } *(*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(uint64(24)+uint64((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol)+uint64(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*24 libc.X__builtin___memcpy_chk(tls, (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FzMask, zMask, uint64((*TRbuObjIter)(unsafe.Pointer(pIter)).FnTblCol), libc.X__builtin_object_size(tls, (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FzMask, 0)) (*TRbuUpdateStmt)(unsafe.Pointer(pUp)).FpNext = (*TRbuObjIter)(unsafe.Pointer(pIter)).FpRbuUpdate (*TRbuObjIter)(unsafe.Pointer(pIter)).FpRbuUpdate = pUp if zSet != 0 { zPrefix = __ccgo_ts + 1664 if (*TRbuObjIter)(unsafe.Pointer(pIter)).FeType != int32(RBU_PK_VTAB) { zPrefix = __ccgo_ts + 31538 } zUpdate = Xsqlite3_mprintf(tls, __ccgo_ts+32199, 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+8, p+64, 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+3863, 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+8 */ int32 _, _ = pRet, rc2 pRet = uintptr(0) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) pRet = _rbuMalloc(tls, p, int64(80)) if pRet == uintptr(0) { return uintptr(0) } *(*int32)(unsafe.Pointer(bp + 8)) = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+32229, libc.VaList(bp+24, p+48))) for *(*int32)(unsafe.Pointer(bp + 8)) == 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+8) case int32(RBU_STATE_IDX): (*TRbuState)(unsafe.Pointer(pRet)).FzIdx = _rbuStrndup(tls, Xsqlite3_column_text(tls, *(*uintptr)(unsafe.Pointer(bp)), int32(1)), bp+8) 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+8) default: *(*int32)(unsafe.Pointer(bp + 8)) = int32(SQLITE_CORRUPT) break } } rc2 = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) if *(*int32)(unsafe.Pointer(bp + 8)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 8)) = rc2 } (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = *(*int32)(unsafe.Pointer(bp + 8)) 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 /* 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+6997, 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+6997) (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState = _rbuMPrintf(tls, p, __ccgo_ts+32259, 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+32287, libc.VaList(bp+8, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState)) libc.X__builtin___memcpy_chk(tls, p+48, __ccgo_ts+3484, uint64(4), libc.X__builtin_object_size(tls, p+48, 0)) } else { libc.X__builtin___memcpy_chk(tls, p+48, __ccgo_ts+6997, uint64(4), libc.X__builtin_object_size(tls, p+48, 0)) } /* If it has not already been created, create the rbu_state table */ _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+32305, libc.VaList(bp+8, p+48)) 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+6997, 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+32371, 0) } else { zExtra = uintptr(0) if libc.Xstrlen(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu) >= uint64(5) && 0 == libc.Xmemcmp(tls, __ccgo_ts+25114, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu, uint64(5)) { zExtra = (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu + 5 for *(*int8)(unsafe.Pointer(zExtra)) != 0 { v1 = zExtra zExtra++ if int32(*(*int8)(unsafe.Pointer(v1))) == int32('?') { break } } if int32(*(*int8)(unsafe.Pointer(zExtra))) == int32('\000') { zExtra = uintptr(0) } } if zExtra == uintptr(0) { v2 = __ccgo_ts + 1664 } else { v2 = __ccgo_ts + 32403 } if zExtra == uintptr(0) { v3 = __ccgo_ts + 1664 } else { v3 = zExtra } zTarget = Xsqlite3_mprintf(tls, __ccgo_ts+32405, libc.VaList(bp+8, Xsqlite3_db_filename(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+6997), 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+32437, -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+32452, 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+32469, -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+6997, int32(SQLITE_FCNTL_RBU), p) } _rbuMPrintfExec(tls, p, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+32485, 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+6997, 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+32513, 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(uint64(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+32485, 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+32531, 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 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+80) 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 { (*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 + 80 /* 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 + 304)) -= 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+24671, 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+64) } } // 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 int8 var eType, i int32 var pIter, pVal uintptr var _ /* pUpdate at bp+8 */ uintptr var _ /* zMask at bp+0 */ uintptr _, _, _, _, _ = c, eType, i, pIter, pVal pIter = p + 80 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) eType = _rbuStepType(tls, p, bp) if eType != 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 + 304)) += 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 + 8)) = uintptr(0) *(*Ti64)(unsafe.Pointer(p + 304)) -= int64((*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FnIndex) _rbuGetUpdateStmt(tls, p, pIter, *(*uintptr)(unsafe.Pointer(bp)), bp+8) if *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 { i = 0 for { if !((*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol) { break } c = *(*int8)(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 + 8)), 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 + 8)), (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol+int32(1), pVal) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _resetAndCollectError(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), p+64) } } } } } } 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+64, __ccgo_ts+32605) 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+32627, 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 rc = _prepareFreeAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+64, Xsqlite3_mprintf(tls, __ccgo_ts+32654, libc.VaList(bp+16, p+48, 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))) 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+64, Xsqlite3_mprintf(tls, __ccgo_ts+32812, 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+32827, 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+8 */ uintptr var _ /* pSql at bp+0 */ uintptr _, _ = i, zSql *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+32847, uintptr(0), uintptr(0), p+64) if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, bp, p+64, __ccgo_ts+32872) } 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+64) } _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+64, __ccgo_ts+32980) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _prepareAndCollectError(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, bp+8, p+64, __ccgo_ts+33045) } 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 + 8)), i+int32(1), Xsqlite3_column_value(tls, *(*uintptr)(unsafe.Pointer(bp)), i)) goto _1 _1: ; i++ } Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_reset(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+33089, uintptr(0), uintptr(0), p+64) } _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp))) _rbuFinalize(tls, p, *(*uintptr)(unsafe.Pointer(bp + 8))) } // 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 + 80 /* 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+18121) _rbuCopyPragma(tls, p, __ccgo_ts+17216) } 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+33114, libc.VaList(bp+16, p+48, (*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 { _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+15335, uintptr(0), uintptr(0), p+64) } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+15335, uintptr(0), uintptr(0), p+64) } (*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 if (*TRbuState)(unsafe.Pointer(pState)).FzTbl != 0 { pIter = p + 80 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+33142, 0) } if rc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).FnStep = (*TRbuState)(unsafe.Pointer(pState)).FnRow rc = _rbuObjIterPrepareAll(tls, p, p+80, (*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+32591, 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+6997, int32(SQLITE_FCNTL_VFS_POINTER), bp) (*(*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]int8 _ = pVfs Xsqlite3_randomness(tls, int32(4), bp) Xsqlite3_snprintf(tls, int32(64), bp+4, __ccgo_ts+33167, 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) (*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+8 */ uintptr _, _, _, _, _ = db, nIndex, p, rc, v1 p = Xsqlite3_user_data(tls, pCtx) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 8)) = 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 rc = _prepareFreeAndCollectError(tls, db, bp, bp+8, Xsqlite3_mprintf(tls, __ccgo_ts+33178, libc.VaList(bp+24, Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apVal)))))) if rc != SQLITE_OK { Xsqlite3_result_error(tls, pCtx, *(*uintptr)(unsafe.Pointer(bp + 8)), -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 + 8))) } // 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+33250, 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+64, __ccgo_ts+33264) } 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+64, __ccgo_ts+33321) 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 uint64 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 = uint64(0) } nTarget = v1 nRbu = libc.Xstrlen(tls, zRbu) nByte = uint64(408) + nTarget + uint64(1) + nRbu + uint64(1) p = Xsqlite3_malloc64(tls, nByte) if p != 0 { pState = uintptr(0) /* Create the custom VFS. */ libc.X__builtin___memset_chk(tls, p, 0, uint64(408), libc.X__builtin_object_size(tls, p, 0)) 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*408 *(*int32)(unsafe.Pointer(bp)) = 0 if zTarget != 0 { (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget = pCsr libc.X__builtin___memcpy_chk(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget, zTarget, nTarget+uint64(1), libc.X__builtin_object_size(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget, 0)) pCsr += uintptr(nTarget + uint64(1)) } (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu = pCsr libc.X__builtin___memcpy_chk(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu, zRbu, nRbu+uint64(1), libc.X__builtin_object_size(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzRbu, 0)) pCsr += uintptr(nRbu + uint64(1)) if zState != 0 { (*Tsqlite3rbu)(unsafe.Pointer(p)).FzState = _rbuMPrintf(tls, p, __ccgo_ts+3863, 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) 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 } } 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+33395, 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 + 33427 } else { v3 = __ccgo_ts + 33434 } (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg = Xsqlite3_mprintf(tls, __ccgo_ts+33441, 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+15320, uintptr(0), uintptr(0), p+64) /* 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+80) } /* 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+17835) _rbuCopyPragma(tls, p, __ccgo_ts+17231) } /* 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+33473, uintptr(0), uintptr(0), p+64) } /* 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+6997, int32(SQLITE_FCNTL_ZIPVFS), uintptr(0)) if frc == SQLITE_OK { (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_exec(tls, db, __ccgo_ts+33489, uintptr(0), uintptr(0), p+64) } } 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(408)) if pRet != 0 { libc.X__builtin___memset_chk(tls, pRet, 0, uint64(408), libc.X__builtin_object_size(tls, pRet, 0)) (*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+33513, zState+uintptr(n-int32(7)), uint64(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 !(uint64(i) < nErrmsg-uint64(8)) { break } if libc.Xmemcmp(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg+uintptr(i), __ccgo_ts+31538, uint64(8)) == 0 { nDel = int32(8) for int32(*(*int8)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg + uintptr(i+uint32(nDel))))) >= int32('0') && int32(*(*int8)(unsafe.Pointer((*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg + uintptr(i+uint32(nDel))))) <= int32('9') { nDel++ } libc.X__builtin___memmove_chk(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg+uintptr(i), (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg+uintptr(i+uint32(nDel)), nErrmsg+uint64(1)-uint64(i)-uint64(nDel), libc.X__builtin_object_size(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FzErrmsg+uintptr(i), 0)) nErrmsg -= uint64(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+15335, uintptr(0), uintptr(0), p+64) } /* 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+15335, uintptr(0), uintptr(0), p+64) } /* Close any open statement handles. */ _rbuObjIterFinalize(tls, p+80) /* 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+33521, 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) _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: } } // 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), } if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != SQLITE_OK && (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc != int32(SQLITE_DONE) { return int32(SQLITE_RBU_STATE_ERROR) } else { 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 } if (*Tsqlite3rbu)(unsafe.Pointer(p)).FeStage == int32(RBU_STAGE_OAL) { if rc == SQLITE_OK { rc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbMain, __ccgo_ts+15335, 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) { if rc == SQLITE_OK { rc = Xsqlite3_exec(tls, (*Tsqlite3rbu)(unsafe.Pointer(p)).FdbRbu, __ccgo_ts+15335, uintptr(0), uintptr(0), uintptr(0)) } if rc == SQLITE_OK { if (*Tsqlite3rbu)(unsafe.Pointer(p)).FzTarget == uintptr(0) { v1 = __ccgo_ts + 15320 } else { v1 = __ccgo_ts + 33473 } 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+33473, 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 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 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 + 192 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0 && *(*uintptr)(unsafe.Pointer(pp)) != p) { break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 88 } 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 + 200 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != 0 && *(*uintptr)(unsafe.Pointer(pp)) != p) { break } goto _2 _2: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 96 } 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)*8))) 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) } } /* 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) { 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.X__builtin___memset_chk(tls, zBuf, 0, uint64(iAmt), libc.X__builtin_object_size(tls, zBuf, 0)) } 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.X__builtin___memset_chk(tls, aBuf+100, 0, uint64(iAmt-int32(100)), libc.X__builtin_object_size(tls, aBuf+100, 0)) _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) { 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+6997) zOpen = Xsqlite3_filename_wal(tls, zOpen) } nOpen = libc.Xstrlen(tls, zOpen) *(*int8)(unsafe.Pointer(zOpen + uintptr(nOpen-uint64(3)))) = int8('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+33582, 0) != 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, 8)) = __ccgo_fp(_rbuVfsClose) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_rbuVfsRead) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_rbuVfsWrite) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_rbuVfsTruncate) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_rbuVfsSync) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_rbuVfsFileSize) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_rbuVfsLock) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_rbuVfsUnlock) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_rbuVfsCheckReservedLock) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_rbuVfsFileControl) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_rbuVfsSectorSize) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_rbuVfsDeviceCharacteristics) *(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(_rbuVfsShmMap) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_rbuVfsShmLock) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_rbuVfsShmBarrier) *(*uintptr)(unsafe.Add(p, 128)) = __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, 8)) = __ccgo_fp(_rbuVfsClose) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_rbuVfsRead) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_rbuVfsWrite) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_rbuVfsTruncate) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_rbuVfsSync) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_rbuVfsFileSize) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_rbuVfsLock) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_rbuVfsUnlock) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_rbuVfsCheckReservedLock) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_rbuVfsFileControl) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_rbuVfsSectorSize) *(*uintptr)(unsafe.Add(p, 96)) = __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) { 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 = uint64(208) + nName + uint64(1) pNew = Xsqlite3_malloc64(tls, nByte) if pNew == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { /* Parent VFS */ libc.X__builtin___memset_chk(tls, pNew, 0, nByte, libc.X__builtin_object_size(tls, pNew, 0)) pParent = Xsqlite3_vfs_find(tls, zParent) if pParent == uintptr(0) { rc = int32(SQLITE_NOTFOUND) } else { libc.X__builtin___memcpy_chk(tls, pNew, uintptr(unsafe.Pointer(&_vfs_template)), uint64(168), libc.X__builtin_object_size(tls, pNew, 0)) (*Trbu_vfs)(unsafe.Pointer(pNew)).Fbase.FmxPathname = (*Tsqlite3_vfs)(unsafe.Pointer(pParent)).FmxPathname (*Trbu_vfs)(unsafe.Pointer(pNew)).Fbase.FszOsFile = int32(uint64(104) + uint64((*Tsqlite3_vfs)(unsafe.Pointer(pParent)).FszOsFile)) (*Trbu_vfs)(unsafe.Pointer(pNew)).FpRealVfs = pParent v1 = pNew + 1*208 zSpace = v1 (*Trbu_vfs)(unsafe.Pointer(pNew)).Fbase.FzName = v1 libc.X__builtin___memcpy_chk(tls, zSpace, zName, nName, libc.X__builtin_object_size(tls, zSpace, 0)) /* 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, 40)) = __ccgo_fp(_rbuVfsOpen) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_rbuVfsDelete) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_rbuVfsAccess) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_rbuVfsFullPathname) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_rbuVfsDlOpen) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_rbuVfsDlError) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_rbuVfsDlSym) *(*uintptr)(unsafe.Add(p, 96)) = __ccgo_fp(_rbuVfsDlClose) *(*uintptr)(unsafe.Add(p, 104)) = __ccgo_fp(_rbuVfsRandomness) *(*uintptr)(unsafe.Add(p, 112)) = __ccgo_fp(_rbuVfsSleep) *(*uintptr)(unsafe.Add(p, 120)) = __ccgo_fp(_rbuVfsCurrentTime) *(*uintptr)(unsafe.Add(p, 128)) = __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]int8{'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 { 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 { 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*8))) iDb = _sqlite3FindDb(tls, db, bp) if iDb < 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+12664, libc.VaList(bp+24, *(*uintptr)(unsafe.Pointer(argv + 3*8)))) 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(40)) if pTab == uintptr(0) { rc = int32(SQLITE_NOMEM) } } if rc == SQLITE_OK { libc.X__builtin___memset_chk(tls, pTab, 0, uint64(40), libc.X__builtin_object_size(tls, pTab, 0)) (*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 + 40)) |= 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 + 40)) |= 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 + 40)) |= 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 + 40)) |= 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(2152)) if pCsr == uintptr(0) { return int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, pCsr, 0, uint64(2152), libc.X__builtin_object_size(tls, pCsr, 0)) (*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)*32))).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.X__builtin___memset_chk(tls, p, 0, uint64(64), libc.X__builtin_object_size(tls, p, 0)) (*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.Uint64FromInt64(2048)/libc.Uint64FromInt64(64))) { break } _statClearPage(tls, pCsr+24+uintptr(i)*64) Xsqlite3_free(tls, (*(*TStatPage)(unsafe.Pointer(pCsr + 24 + uintptr(i)*64))).FaPg) (*(*TStatPage)(unsafe.Pointer(pCsr + 24 + uintptr(i)*64))).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((*TStatPage)(unsafe.Pointer(p)).FnCell+libc.Int32FromInt32(1))*uint64(32)) if (*TStatPage)(unsafe.Pointer(p)).FaCell == uintptr(0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, (*TStatPage)(unsafe.Pointer(p)).FaCell, 0, uint64((*TStatPage)(unsafe.Pointer(p)).FnCell+libc.Int32FromInt32(1))*uint64(32), libc.X__builtin_object_size(tls, (*TStatPage)(unsafe.Pointer(p)).FaCell, 0)) i = 0 for { if !(i < (*TStatPage)(unsafe.Pointer(p)).FnCell) { break } pCell = (*TStatPage)(unsafe.Pointer(p)).FaCell + uintptr(i)*32 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 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(4)*uint64(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 { 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)*32))).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 + 2144)) += (*(*[2]Tsqlite3_int64)(unsafe.Pointer(bp)))[int32(1)] } else { /* Not ZIPVFS: The default page size and offset */ *(*Ti64)(unsafe.Pointer(pCsr + 2144)) += 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.X__builtin___memset_chk(tls, (*TStatPage)(unsafe.Pointer(pPg)).FaPg+uintptr(pgsz), 0, uint64(DBSTAT_PAGE_PADDING_BYTES), libc.X__builtin_object_size(tls, (*TStatPage)(unsafe.Pointer(pPg)).FaPg+uintptr(pgsz), 0)) } rc = _sqlite3PagerGet(tls, _sqlite3BtreePager(tls, pBt), iPg, bp, 0) if rc == SQLITE_OK { a = _sqlite3PagerGetData(tls, *(*uintptr)(unsafe.Pointer(bp))) libc.X__builtin___memcpy_chk(tls, (*TStatPage)(unsafe.Pointer(pPg)).FaPg, a, uint64(pgsz), libc.X__builtin_object_size(tls, (*TStatPage)(unsafe.Pointer(pPg)).FaPg, 0)) _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)*32))).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+24) (*(*TStatPage)(unsafe.Pointer(pCsr + 24))).FiPgno = iRoot (*(*TStatPage)(unsafe.Pointer(pCsr + 24))).FiCell = 0 if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) { v1 = Xsqlite3_mprintf(tls, __ccgo_ts+4097, 0) z = v1 (*(*TStatPage)(unsafe.Pointer(pCsr + 24))).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 + 24 + uintptr((*TStatCursor)(unsafe.Pointer(pCsr)).FiPage)*64 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)*32 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 + 2128)) += int64(nUsable - int32(4)) } else { *(*Ti64)(unsafe.Pointer(pCsr + 2128)) += int64((*TStatCell)(unsafe.Pointer(pCell)).FnLastOvfl) *(*Ti64)(unsafe.Pointer(pCsr + 2120)) += 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 + 33593 v2 = Xsqlite3_mprintf(tls, __ccgo_ts+33602, 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.Uint64FromInt64(2048)/libc.Uint64FromInt64(64)) { _statResetCsr(tls, pCsr) return _sqlite3CorruptError(tls, int32(224206)) } if (*TStatPage)(unsafe.Pointer(p)).FiCell == (*TStatPage)(unsafe.Pointer(p)).FnCell { (*(*TStatPage)(unsafe.Pointer(p + 1*64))).FiPgno = (*TStatPage)(unsafe.Pointer(p)).FiRightChildPg } else { (*(*TStatPage)(unsafe.Pointer(p + 1*64))).FiPgno = (*(*TStatCell)(unsafe.Pointer((*TStatPage)(unsafe.Pointer(p)).FaCell + uintptr((*TStatPage)(unsafe.Pointer(p)).FiCell)*32))).FiChildPg } rc = _statGetPage(tls, pBt, (*(*TStatPage)(unsafe.Pointer(p + 1*64))).FiPgno, p+1*64) (*TStatCursor)(unsafe.Pointer(pCsr)).FnPage++ (*(*TStatPage)(unsafe.Pointer(p + 1*64))).FiCell = 0 if !((*TStatCursor)(unsafe.Pointer(pCsr)).FisAgg != 0) { v4 = Xsqlite3_mprintf(tls, __ccgo_ts+33614, libc.VaList(bp+16, (*TStatPage)(unsafe.Pointer(p)).FzPath, (*TStatPage)(unsafe.Pointer(p)).FiCell)) z = v4 (*(*TStatPage)(unsafe.Pointer(p + 1*64))).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 + 24 + uintptr((*TStatCursor)(unsafe.Pointer(pCsr)).FiPage)*64 (*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 + 33622 case int32(0x0D): /* table leaf */ fallthrough case int32(0x0A): /* index leaf */ (*TStatCursor)(unsafe.Pointer(pCsr)).FzPagetype = __ccgo_ts + 33631 default: (*TStatCursor)(unsafe.Pointer(pCsr)).FzPagetype = __ccgo_ts + 33636 break } *(*int32)(unsafe.Pointer(pCsr + 2108)) += (*TStatPage)(unsafe.Pointer(p1)).FnCell *(*Ti64)(unsafe.Pointer(pCsr + 2120)) += 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+3863, 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)*32))).FnLocal goto _6 _6: ; i++ } *(*Ti64)(unsafe.Pointer(pCsr + 2128)) += 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)*8))) (*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)*8))) } 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)*8))) != 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+33646, libc.VaList(bp+8, (*(*TDb)(unsafe.Pointer((*Tsqlite3)(unsafe.Pointer((*TStatTable)(unsafe.Pointer(pTab)).Fdb)).FaDb + uintptr((*TStatCursor)(unsafe.Pointer(pCsr)).FiDb)*32))).FzDbSName)) if zName != 0 { Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+33801, libc.VaList(bp+8, zName)) } if idxNum&int32(0x08) != 0 { Xsqlite3_str_appendf(tls, pSql, __ccgo_ts+33815, 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+8, 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)*32))).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+33830, uintptr(unsafe.Pointer(&_dbstat_module)), uintptr(0)) } var _dbstat_module = Tsqlite3_module{} func init() { p := unsafe.Pointer(&_dbstat_module) *(*uintptr)(unsafe.Add(p, 8)) = __ccgo_fp(_statConnect) *(*uintptr)(unsafe.Add(p, 16)) = __ccgo_fp(_statConnect) *(*uintptr)(unsafe.Add(p, 24)) = __ccgo_fp(_statBestIndex) *(*uintptr)(unsafe.Add(p, 32)) = __ccgo_fp(_statDisconnect) *(*uintptr)(unsafe.Add(p, 40)) = __ccgo_fp(_statDisconnect) *(*uintptr)(unsafe.Add(p, 48)) = __ccgo_fp(_statOpen) *(*uintptr)(unsafe.Add(p, 56)) = __ccgo_fp(_statClose) *(*uintptr)(unsafe.Add(p, 64)) = __ccgo_fp(_statFilter) *(*uintptr)(unsafe.Add(p, 72)) = __ccgo_fp(_statNext) *(*uintptr)(unsafe.Add(p, 80)) = __ccgo_fp(_statEof) *(*uintptr)(unsafe.Add(p, 88)) = __ccgo_fp(_statColumn) *(*uintptr)(unsafe.Add(p, 96)) = __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 { 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 { *(*float64)(unsafe.Pointer(bp + 8)) = Xsqlite3_value_double(tls, pValue) libc.X__builtin___memcpy_chk(tls, bp, bp+8, uint64(8), libc.X__builtin_object_size(tls, bp, 0)) } _sessionPutI64(tls, aBuf+1, int64(*(*Tu64)(unsafe.Pointer(bp)))) } nByte = int32(9) default: 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.X__builtin___memcpy_chk(tls, aBuf+uintptr(nVarint+int32(1)), z, uint64(n), libc.X__builtin_object_size(tls, aBuf+uintptr(nVarint+int32(1)), 0)) } } 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 + 56 *(*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 + 56 *(*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 { *(*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 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. */ 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, uint64(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.X__builtin___memcpy_chk(tls, aOut, a2, uint64(n2), libc.X__builtin_object_size(tls, aOut, 0)) aOut += uintptr(n2) } else { libc.X__builtin___memcpy_chk(tls, aOut, a1, uint64(n1), libc.X__builtin_object_size(tls, aOut, 0)) 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) 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(48) defer tls.Free(48) var aNew, aNew1, aOld, aOld1, aOut, v2, v4 uintptr var bRequired, i int32 var _ /* aNew1 at bp+16 */ uintptr var _ /* aNew2 at bp+24 */ uintptr var _ /* aOld1 at bp+0 */ uintptr var _ /* aOld2 at bp+8 */ uintptr var _ /* nNew at bp+36 */ int32 var _ /* nNew at bp+44 */ int32 var _ /* nOld at bp+32 */ int32 var _ /* nOld at bp+40 */ int32 _, _, _, _, _, _, _, _, _ = aNew, aNew1, aOld, aOld1, aOut, bRequired, i, v2, v4 *(*uintptr)(unsafe.Pointer(bp)) = aOldRecord1 *(*uintptr)(unsafe.Pointer(bp + 8)) = aOldRecord2 *(*uintptr)(unsafe.Pointer(bp + 16)) = aNewRecord1 *(*uintptr)(unsafe.Pointer(bp + 24)) = aNewRecord2 aOut = *(*uintptr)(unsafe.Pointer(paOut)) if bPatchset == 0 { bRequired = 0 /* Write the old.* vector first. */ i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } aOld = _sessionMergeValue(tls, bp, bp+8, bp+32) aNew = _sessionMergeValue(tls, bp+16, bp+24, bp+36) if *(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i))) != 0 || *(*int32)(unsafe.Pointer(bp + 32)) != *(*int32)(unsafe.Pointer(bp + 36)) || libc.Xmemcmp(tls, aOld, aNew, uint64(*(*int32)(unsafe.Pointer(bp + 36)))) != 0 { if int32(*(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i)))) == 0 { bRequired = int32(1) } libc.X__builtin___memcpy_chk(tls, aOut, aOld, uint64(*(*int32)(unsafe.Pointer(bp + 32))), libc.X__builtin_object_size(tls, aOut, 0)) aOut += uintptr(*(*int32)(unsafe.Pointer(bp + 32))) } 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 + 8)) = aOldRecord2 *(*uintptr)(unsafe.Pointer(bp + 16)) = aNewRecord1 *(*uintptr)(unsafe.Pointer(bp + 24)) = aNewRecord2 i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } aOld1 = _sessionMergeValue(tls, bp, bp+8, bp+40) aNew1 = _sessionMergeValue(tls, bp+16, bp+24, bp+44) if bPatchset == 0 && (*(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(i))) != 0 || *(*int32)(unsafe.Pointer(bp + 40)) == *(*int32)(unsafe.Pointer(bp + 44)) && 0 == libc.Xmemcmp(tls, aOld1, aNew1, uint64(*(*int32)(unsafe.Pointer(bp + 44))))) { v4 = aOut aOut++ *(*Tu8)(unsafe.Pointer(v4)) = uint8('\000') } else { libc.X__builtin___memcpy_chk(tls, aOut, aNew1, uint64(*(*int32)(unsafe.Pointer(bp + 44))), libc.X__builtin_object_size(tls, aOut, 0)) aOut += uintptr(*(*int32)(unsafe.Pointer(bp + 44))) } 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) } 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) } _ = 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 */ 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.X__builtin___memcpy_chk(tls, bp+16, bp+8, uint64(8), libc.X__builtin_object_size(tls, bp+16, 0)) 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, uint64(*(*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(uint64(8)*uint64(nNew))) if apNew == uintptr(0) { if (*TSessionTable)(unsafe.Pointer(pTab)).FnChange == 0 { return int32(SQLITE_ERROR) } return SQLITE_OK } libc.X__builtin___memset_chk(tls, apNew, 0, uint64(8)*uint64(nNew), libc.X__builtin_object_size(tls, apNew, 0)) i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnChange) { break } p = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(i)*8)) 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)*8)) *(*uintptr)(unsafe.Pointer(apNew + uintptr(iHash)*8)) = 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 */ *(*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+12055, 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+33837, 0) } else { if rc == int32(SQLITE_ERROR) { zPragma = Xsqlite3_mprintf(tls, __ccgo_ts+1664, 0) } else { return rc } } } else { zPragma = Xsqlite3_mprintf(tls, __ccgo_ts+33958, 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 = Tsqlite3_int64(uint64(nByte) + libc.Xstrlen(tls, __ccgo_ts+30181)) rc = Xsqlite3_reset(tls, *(*uintptr)(unsafe.Pointer(bp))) if rc == SQLITE_OK { nByte = Tsqlite3_int64(uint64(nByte) + uint64(nDbCol)*(libc.Uint64FromInt64(8)*libc.Uint64FromInt32(2)+libc.Uint64FromInt64(1)+libc.Uint64FromInt32(1)+libc.Uint64FromInt32(1))) pAlloc = _sessionMalloc64(tls, pSession, nByte) if pAlloc == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, pAlloc, 0, uint64(nByte), libc.X__builtin_object_size(tls, pAlloc, 0)) } } if rc == SQLITE_OK { azCol = pAlloc azDflt = azCol + uintptr(nDbCol)*8 pAlloc = azDflt + uintptr(nDbCol)*8 abPK = pAlloc pAlloc = abPK + uintptr(nDbCol) if pzTab != 0 { libc.X__builtin___memcpy_chk(tls, pAlloc, zThis, uint64(nThis+int32(1)), libc.X__builtin_object_size(tls, pAlloc, 0)) *(*uintptr)(unsafe.Pointer(pzTab)) = pAlloc pAlloc += uintptr(nThis + int32(1)) } i = 0 if bRowid != 0 { nName = libc.Xstrlen(tls, __ccgo_ts+30181) libc.X__builtin___memcpy_chk(tls, pAlloc, __ccgo_ts+30181, nName+uint64(1), libc.X__builtin_object_size(tls, pAlloc, 0)) *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*8)) = pAlloc pAlloc += uintptr(nName + uint64(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.X__builtin___memcpy_chk(tls, pAlloc, zName, uint64(nName1+int32(1)), libc.X__builtin_object_size(tls, pAlloc, 0)) *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*8)) = pAlloc pAlloc += uintptr(nName1 + int32(1)) if zDflt != 0 { libc.X__builtin___memcpy_chk(tls, pAlloc, zDflt, uint64(nDflt+int32(1)), libc.X__builtin_object_size(tls, pAlloc, 0)) *(*uintptr)(unsafe.Pointer(azDflt + uintptr(i)*8)) = pAlloc pAlloc += uintptr(nDflt + int32(1)) } else { *(*uintptr)(unsafe.Pointer(azDflt + uintptr(i)*8)) = 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, p3 uintptr var _ /* abPK at bp+0 */ uintptr _, _, _, _ = i, rc, v1, p3 rc = SQLITE_OK if (*TSessionTable)(unsafe.Pointer(pTab)).FnCol == 0 { if pSession == uintptr(0) || (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbImplicitPK != 0 { v1 = pTab + 24 } else { v1 = uintptr(0) } rc = _sessionTableInfo(tls, pSession, db, zDb, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, pTab+16, uintptr(0), pTab+32, pTab+40, 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+12055, (*TSessionTable)(unsafe.Pointer(pTab)).FzName) { (*TSessionTable)(unsafe.Pointer(pTab)).FbStat1 = int32(1) } if pSession != 0 && (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnableSize != 0 { p3 = pSession + 64 *(*Ti64)(unsafe.Pointer(p3)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(p3))) + (uint64(libc.Int32FromInt32(1)+_sessionVarintLen(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol)+(*TSessionTable)(unsafe.Pointer(pTab)).FnCol) + libc.Xstrlen(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName) + libc.Uint64FromInt32(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(48) defer tls.Free(48) var a, v1 uintptr var ii, nOldCol int32 var _ /* abPK at bp+24 */ uintptr var _ /* azCol at bp+8 */ uintptr var _ /* azDflt at bp+16 */ uintptr var _ /* bRowid at bp+32 */ int32 var _ /* nCol at bp+0 */ int32 _, _, _, _ = a, ii, nOldCol, v1 *(*int32)(unsafe.Pointer(bp)) = 0 *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 24)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 32)) = 0 if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbImplicitPK != 0 { v1 = bp + 32 } 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+8, bp+16, bp+24, 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 + 32)) { (*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 + 24)) + uintptr(ii)))) { (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = int32(SQLITE_SCHEMA) } } else { if *(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)) + 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 + 8)) (*TSessionTable)(unsafe.Pointer(pTab)).FnCol = *(*int32)(unsafe.Pointer(bp)) (*TSessionTable)(unsafe.Pointer(pTab)).FazDflt = *(*uintptr)(unsafe.Pointer(bp + 16)) (*TSessionTable)(unsafe.Pointer(pTab)).FabPK = *(*uintptr)(unsafe.Pointer(bp + 24)) *(*uintptr)(unsafe.Pointer(bp + 8)) = a } if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbEnableSize != 0 { *(*Ti64)(unsafe.Pointer(pSession + 64)) += int64(*(*int32)(unsafe.Pointer(bp)) - nOldCol) *(*Ti64)(unsafe.Pointer(pSession + 64)) += int64(_sessionVarintLen(tls, *(*int32)(unsafe.Pointer(bp)))) *(*Ti64)(unsafe.Pointer(pSession + 64)) -= int64(_sessionVarintLen(tls, nOldCol)) } } } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) 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 break } nByte = int32(uint64(nIncr) + (uint64(32) + uint64((*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.X__builtin___memcpy_chk(tls, pNew, pOld, uint64(32), libc.X__builtin_object_size(tls, pNew, 0)) (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord = pNew + 1*32 libc.X__builtin___memcpy_chk(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord, (*TSessionChange)(unsafe.Pointer(pOld)).FaRecord, uint64((*TSessionChange)(unsafe.Pointer(pOld)).FnRecord), libc.X__builtin_object_size(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord, 0)) 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.X__builtin___memcpy_chk(tls, bp+8, bp, uint64(8), libc.X__builtin_object_size(tls, bp+8, 0)) _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.X__builtin___memcpy_chk(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), z, uint64(n1), libc.X__builtin_object_size(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), 0)) *(*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.X__builtin___memcpy_chk(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), z1, uint64(n2), libc.X__builtin_object_size(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord+uintptr((*TSessionChange)(unsafe.Pointer(pNew)).FnRecord), 0)) *(*int32)(unsafe.Pointer(pNew + 8)) += n2 default: 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 + 64)) += 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.X__builtin___memcpy_chk(tls, (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf), zStr, uint64(nStr), libc.X__builtin_object_size(tls, (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf), 0)) *(*int32)(unsafe.Pointer(p + 8)) += 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+16 */ int32 var _ /* sql at bp+0 */ TSessionBuffer _, _, _, _ = ii, zDflt, zSep, v2 *(*TSessionBuffer)(unsafe.Pointer(bp)) = TSessionBuffer{} *(*int32)(unsafe.Pointer(bp + 16)) = SQLITE_OK zSep = __ccgo_ts + 11675 ii = 0 *(*uintptr)(unsafe.Pointer(ppStmt)) = uintptr(0) _sessionAppendPrintf(tls, bp, bp+16, __ccgo_ts+33987, 0) ii = 0 for { if !(ii < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } if *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FazDflt + uintptr(ii)*8)) != 0 { v2 = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FazDflt + uintptr(ii)*8)) } else { v2 = __ccgo_ts + 1665 } zDflt = v2 _sessionAppendPrintf(tls, bp, bp+16, __ccgo_ts+5488, libc.VaList(bp+32, zSep, zDflt)) zSep = __ccgo_ts + 15431 goto _1 _1: ; ii++ } if *(*int32)(unsafe.Pointer(bp + 16)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 16)) = 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 + 16)) } // 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+8 */ int32 _, _ = ii, pp *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 8)) = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc *(*int32)(unsafe.Pointer(bp + 8)) = _sessionPrepareDfltStmt(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, pTab, bp) if *(*int32)(unsafe.Pointer(bp + 8)) == 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)*8 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+8, pp, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, *(*uintptr)(unsafe.Pointer(bp))) } goto _2 _2: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 24 } goto _1 _1: ; ii++ } } (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc = *(*int32)(unsafe.Pointer(bp + 8)) *(*int32)(unsafe.Pointer(bp + 8)) = 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 + 8)) } 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+32 */ float64 var _ /* iVal at bp+24 */ Tsqlite3_int64 var _ /* nByte at bp+40 */ int32 var _ /* nNew at bp+0 */ Ti64 var _ /* p at bp+16 */ 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 + 16)) = 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+16) if *(*uintptr)(unsafe.Pointer(bp + 16)) == 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 + 16))) != int32(SQLITE_NULL)) case int32(SQLITE_FLOAT): fallthrough case int32(SQLITE_INTEGER): if eType == Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) { *(*Tsqlite3_int64)(unsafe.Pointer(bp + 24)) = _sessionGetI64(tls, pCsr) if eType == int32(SQLITE_INTEGER) { bChanged = libc.BoolInt32(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 24)) != Xsqlite3_value_int64(tls, *(*uintptr)(unsafe.Pointer(bp + 16)))) } else { libc.X__builtin___memcpy_chk(tls, bp+32, bp+24, uint64(8), libc.X__builtin_object_size(tls, bp+32, 0)) bChanged = libc.BoolInt32(*(*float64)(unsafe.Pointer(bp + 32)) != Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(bp + 16)))) } } nOld = int32(8) pCsr += uintptr(8) default: nOld = _sessionVarintGet(tls, pCsr, bp+40) pCsr += uintptr(nOld) nOld += *(*int32)(unsafe.Pointer(bp + 40)) if eType == Xsqlite3_value_type(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) && *(*int32)(unsafe.Pointer(bp + 40)) == Xsqlite3_value_bytes(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) && (*(*int32)(unsafe.Pointer(bp + 40)) == 0 || 0 == libc.Xmemcmp(tls, pCsr, Xsqlite3_value_blob(tls, *(*uintptr)(unsafe.Pointer(bp + 16))), uint64(*(*int32)(unsafe.Pointer(bp + 40))))) { bChanged = 0 } pCsr += uintptr(*(*int32)(unsafe.Pointer(bp + 40))) 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 + 16)), 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 + 64)) += 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(80) defer tls.Free(80) 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+56 */ Tsqlite3_int64 var _ /* p at bp+64 */ uintptr var _ /* p at bp+72 */ 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+1664, 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)))*8)) 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 */ (*TSessionTable)(unsafe.Pointer(pTab)).FnEntry++ /* Figure out how large an allocation is required */ *(*Tsqlite3_int64)(unsafe.Pointer(bp + 56)) = int64(32) i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol-(*TSessionTable)(unsafe.Pointer(pTab)).FbRowid) { break } *(*uintptr)(unsafe.Pointer(bp + 64)) = 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+64) } 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+64) } } /* 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 + 64)), bp+56) if rc != SQLITE_OK { goto error_out } goto _2 _2: ; i++ } if (*TSessionTable)(unsafe.Pointer(pTab)).FbRowid != 0 { *(*Tsqlite3_int64)(unsafe.Pointer(bp + 56)) += int64(9) /* Size of rowid field - an integer */ } /* Allocate the change object */ pC = _sessionMalloc64(tls, pSession, *(*Tsqlite3_int64)(unsafe.Pointer(bp + 56))) if !(pC != 0) { rc = int32(SQLITE_NOMEM) goto error_out } else { libc.X__builtin___memset_chk(tls, pC, 0, uint64(32), libc.X__builtin_object_size(tls, pC, 0)) (*TSessionChange)(unsafe.Pointer(pC)).FaRecord = pC + 1*32 } /* 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 + 56)) = 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 + 56)) = int64(9) } i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol-(*TSessionTable)(unsafe.Pointer(pTab)).FbRowid) { break } *(*uintptr)(unsafe.Pointer(bp + 72)) = 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+72) } 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+72) } } _sessionSerializeValue(tls, (*TSessionChange)(unsafe.Pointer(pC)).FaRecord+uintptr(*(*Tsqlite3_int64)(unsafe.Pointer(bp + 56))), *(*uintptr)(unsafe.Pointer(bp + 72)), bp+56) 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 + 56))) (*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)))*8)) *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(*(*int32)(unsafe.Pointer(bp)))*8)) = 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) } } } 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 } } } } *(*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) _ = 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 { _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 + 1664 zRet = uintptr(0) i = 0 for { if !(i < nCol) { break } if *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) != 0 { zRet = Xsqlite3_mprintf(tls, __ccgo_ts+33994, libc.VaList(bp+8, zRet, zSep, zDb1, zTab, *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*8)), zDb2, zTab, *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*8)))) zSep = __ccgo_ts + 22553 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 + 1664 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+34028, libc.VaList(bp+8, zRet, zSep, zDb1, zTab, *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*8)), zDb2, zTab, *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*8)))) zSep = __ccgo_ts + 34069 if zRet == uintptr(0) { break } } goto _1 _1: ; i++ } if bHave == 0 { zRet = Xsqlite3_mprintf(tls, __ccgo_ts+1784, 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 + 34074 } else { v1 = __ccgo_ts + 7002 } zSel = v1 zRet = Xsqlite3_mprintf(tls, __ccgo_ts+34085, 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 + 15431 } else { v2 = __ccgo_ts + 1664 } zRet = Xsqlite3_mprintf(tls, __ccgo_ts+34164, libc.VaList(bp+8, zRet, v2, zDb, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FazCol + uintptr(ii)*8)))) 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+34183, 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(48) defer tls.Free(48) var bHasPk, bMismatch, i, rc int32 var db, zDb, zExpr, v1 uintptr var _ /* abPK at bp+32 */ uintptr var _ /* azCol at bp+40 */ uintptr var _ /* bRowid at bp+28 */ int32 var _ /* d at bp+0 */ TSessionDiffCtx var _ /* nCol at bp+24 */ int32 var _ /* pTo at bp+16 */ uintptr _, _, _, _, _, _, _, _ = bHasPk, bMismatch, db, i, rc, zDb, zExpr, v1 zDb = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FzDb rc = (*Tsqlite3_session)(unsafe.Pointer(pSession)).Frc libc.X__builtin___memset_chk(tls, bp, 0, uint64(16), libc.X__builtin_object_size(tls, bp, 0)) _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+16) if *(*uintptr)(unsafe.Pointer(bp + 16)) == uintptr(0) { goto diff_out } if _sessionInitTable(tls, pSession, *(*uintptr)(unsafe.Pointer(bp + 16)), (*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 + 28)) = 0 *(*uintptr)(unsafe.Pointer(bp + 40)) = uintptr(0) if (*Tsqlite3_session)(unsafe.Pointer(pSession)).FbImplicitPK != 0 { v1 = bp + 28 } else { v1 = uintptr(0) } rc = _sessionTableInfo(tls, uintptr(0), db, zFrom, zTbl, bp+24, uintptr(0), bp+40, uintptr(0), bp+32, v1) if rc == SQLITE_OK { if (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FnCol != *(*int32)(unsafe.Pointer(bp + 24)) { bMismatch = int32(1) } else { i = 0 for { if !(i < *(*int32)(unsafe.Pointer(bp + 24))) { break } if int32(*(*Tu8)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FabPK + uintptr(i)))) != int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 32)) + uintptr(i)))) { bMismatch = int32(1) } if Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 40)) + uintptr(i)*8)), *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FazCol + uintptr(i)*8))) != 0 { bMismatch = int32(1) } if *(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 32)) + uintptr(i))) != 0 { bHasPk = int32(1) } goto _2 _2: ; i++ } } } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 40))) if bMismatch != 0 { if pzErrMsg != 0 { *(*uintptr)(unsafe.Pointer(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+34240, 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 + 16)))).FnCol, zDb, zFrom, (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FzName, (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FazCol, (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FabPK) } /* Find new rows */ if rc == SQLITE_OK { rc = _sessionDiffFindNew(tls, int32(SQLITE_INSERT), pSession, *(*uintptr)(unsafe.Pointer(bp + 16)), zDb, zFrom, zExpr) } /* Find old rows */ if rc == SQLITE_OK { rc = _sessionDiffFindNew(tls, int32(SQLITE_DELETE), pSession, *(*uintptr)(unsafe.Pointer(bp + 16)), zFrom, zDb, zExpr) } /* Find modified rows */ if rc == SQLITE_OK { rc = _sessionDiffFindModified(tls, pSession, *(*uintptr)(unsafe.Pointer(bp + 16)), 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(136)+uint64(nDb)+uint64(1)) if !(pNew != 0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pNew, 0, uint64(136), libc.X__builtin_object_size(tls, pNew, 0)) (*Tsqlite3_session)(unsafe.Pointer(pNew)).Fdb = db (*Tsqlite3_session)(unsafe.Pointer(pNew)).FzDb = pNew + 1*136 (*Tsqlite3_session)(unsafe.Pointer(pNew)).FbEnable = int32(1) libc.X__builtin___memcpy_chk(tls, (*Tsqlite3_session)(unsafe.Pointer(pNew)).FzDb, zDb, uint64(nDb+int32(1)), libc.X__builtin_object_size(tls, (*Tsqlite3_session)(unsafe.Pointer(pNew)).FzDb, 0)) _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)*8)) 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)) + 80 } 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(uint64(80) + uint64(nName) + uint64(1)) pTab = _sessionMalloc64(tls, pSession, int64(nByte)) if !(pTab != 0) { rc = int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, pTab, 0, uint64(80), libc.X__builtin_object_size(tls, pTab, 0)) (*TSessionTable)(unsafe.Pointer(pTab)).FzName = pTab + 1*80 libc.X__builtin___memcpy_chk(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zName, uint64(nName+int32(1)), libc.X__builtin_object_size(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, 0)) ppTab = pSession + 88 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 + 8 *(*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 + 8 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 + 8)) += _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.X__builtin___memcpy_chk(tls, (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf), aBlob, uint64(nBlob), libc.X__builtin_object_size(tls, (*TSessionBuffer)(unsafe.Pointer(p)).FaBuf+uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf), 0)) *(*int32)(unsafe.Pointer(p + 8)) += 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]int8 Xsqlite3_snprintf(tls, int32(libc.Uint64FromInt64(24)-libc.Uint64FromInt32(1)), bp, __ccgo_ts+5550, 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++ *(*int8)(unsafe.Pointer(v1)) = int8('"') for *(*int8)(unsafe.Pointer(zIn)) != 0 { if int32(*(*int8)(unsafe.Pointer(zIn))) == int32('"') { v2 = zOut zOut++ *(*int8)(unsafe.Pointer(v2)) = int8('"') } v3 = zOut zOut++ v4 = zIn zIn++ *(*int8)(unsafe.Pointer(v3)) = *(*int8)(unsafe.Pointer(v4)) } v5 = zOut zOut++ *(*int8)(unsafe.Pointer(v5)) = int8('"') (*TSessionBuffer)(unsafe.Pointer(p)).FnBuf = int32(int64(zOut) - int64((*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.X__builtin___memcpy_chk(tls, bp, bp+16, uint64(8), libc.X__builtin_object_size(tls, bp, 0)) } _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+8 */ TSessionBuffer var _ /* dVal at bp+32 */ float64 var _ /* iVal at bp+24 */ Tsqlite3_int64 var _ /* n at bp+40 */ 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 + 8)) = 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 */ _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 + 24)) = _sessionGetI64(tls, pCsr+1) if eType == int32(SQLITE_INTEGER) { if *(*Tsqlite3_int64)(unsafe.Pointer(bp + 24)) == Xsqlite3_column_int64(tls, pStmt, i) { break } } else { libc.X__builtin___memcpy_chk(tls, bp+32, bp+24, uint64(8), libc.X__builtin_object_size(tls, bp+32, 0)) if *(*float64)(unsafe.Pointer(bp + 32)) == Xsqlite3_column_double(tls, pStmt, i) { break } } } bChanged = int32(1) default: nHdr = int32(1) + _sessionVarintGet(tls, pCsr+1, bp+40) nAdvance = nHdr + *(*int32)(unsafe.Pointer(bp + 40)) if eType == Xsqlite3_column_type(tls, pStmt, i) && *(*int32)(unsafe.Pointer(bp + 40)) == Xsqlite3_column_bytes(tls, pStmt, i) && (*(*int32)(unsafe.Pointer(bp + 40)) == 0 || 0 == libc.Xmemcmp(tls, pCsr+uintptr(nHdr), Xsqlite3_column_blob(tls, pStmt, i), uint64(*(*int32)(unsafe.Pointer(bp + 40))))) { 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+8, pStmt, i, bp) } else { _sessionAppendByte(tls, bp+8, 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 + 8))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FnBuf, bp) } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).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): 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(int64(a)-int64(pStart)), bp) } goto _1 _1: ; i++ } } 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(112) defer tls.Free(112) var i, nSql int32 var zCols, zSep, zSql, v1, v3 uintptr var _ /* nooptest at bp+8 */ TSessionBuffer var _ /* pkfield at bp+24 */ TSessionBuffer var _ /* pkvar at bp+40 */ 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 + 1664 if bRowid != 0 { v1 = __ccgo_ts + 34074 } else { v1 = __ccgo_ts + 7002 } zCols = v1 nSql = -int32(1) *(*TSessionBuffer)(unsafe.Pointer(bp + 8)) = TSessionBuffer{} *(*TSessionBuffer)(unsafe.Pointer(bp + 24)) = TSessionBuffer{} *(*TSessionBuffer)(unsafe.Pointer(bp + 40)) = TSessionBuffer{} _sessionAppendStr(tls, bp+8, __ccgo_ts+34267, bp) if 0 == Xsqlite3_stricmp(tls, __ccgo_ts+12055, zTab) { _sessionAppendStr(tls, bp+8, __ccgo_ts+34271, bp) _sessionAppendStr(tls, bp+24, __ccgo_ts+34295, bp) _sessionAppendStr(tls, bp+40, __ccgo_ts+34304, bp) zCols = __ccgo_ts + 34349 } else { i = 0 for { if !(i < nCol) { break } if *(*Tu8)(unsafe.Pointer(abPK + uintptr(i))) != 0 { _sessionAppendStr(tls, bp+24, zSep, bp) _sessionAppendStr(tls, bp+40, zSep, bp) zSep = __ccgo_ts + 15431 _sessionAppendIdent(tls, bp+24, *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*8)), bp) _sessionAppendPrintf(tls, bp+40, bp, __ccgo_ts+34363, libc.VaList(bp+64, i+int32(1))) } else { _sessionAppendPrintf(tls, bp+8, bp, __ccgo_ts+34367, libc.VaList(bp+64, i+int32(1)+nCol, i+int32(1), zTab, *(*uintptr)(unsafe.Pointer(azCol + uintptr(i)*8)))) } goto _2 _2: ; i++ } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if bIgnoreNoop != 0 { v3 = (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FaBuf } else { v3 = __ccgo_ts + 1664 } zSql = Xsqlite3_mprintf(tls, __ccgo_ts+34394, libc.VaList(bp+64, zCols, v3, zDb, zTab, (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 40))).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 + 8))).FaBuf) Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).FaBuf) Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 40))).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): 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.X__builtin___memcpy_chk(tls, bp, bp+8, uint64(8), libc.X__builtin_object_size(tls, bp, 0)) 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: 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+24 */ uintptr var _ /* rc at bp+16 */ 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 */ /* 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) { *(*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 + 16)) = Xsqlite3_exec(tls, (*Tsqlite3_session)(unsafe.Pointer(pSession)).Fdb, __ccgo_ts+34436, uintptr(0), uintptr(0), uintptr(0)) if *(*int32)(unsafe.Pointer(bp + 16)) != SQLITE_OK { return *(*int32)(unsafe.Pointer(bp + 16)) } Xsqlite3_mutex_enter(tls, Xsqlite3_db_mutex(tls, db)) pTab = (*Tsqlite3_session)(unsafe.Pointer(pSession)).FpTable for { if !(*(*int32)(unsafe.Pointer(bp + 16)) == 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 + 24)) = 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 + 16)) = _sessionReinitTable(tls, pSession, pTab) if *(*int32)(unsafe.Pointer(bp + 16)) == SQLITE_OK && (*TSessionTable)(unsafe.Pointer(pTab)).FnCol != nOldCol { *(*int32)(unsafe.Pointer(bp + 16)) = _sessionUpdateChanges(tls, pSession, pTab) } /* Write a table header */ _sessionAppendTableHdr(tls, bp, bPatchset, pTab, bp+16) /* Build and compile a statement to execute: */ if *(*int32)(unsafe.Pointer(bp + 16)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 16)) = _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+24) } nNoop = (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf i = 0 for { if !(i < (*TSessionTable)(unsafe.Pointer(pTab)).FnChange && *(*int32)(unsafe.Pointer(bp + 16)) == SQLITE_OK) { break } /* Used to iterate through changes */ p = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(i)*8)) for { if !(*(*int32)(unsafe.Pointer(bp + 16)) == SQLITE_OK && p != 0) { break } *(*int32)(unsafe.Pointer(bp + 16)) = _sessionSelectBind(tls, *(*uintptr)(unsafe.Pointer(bp + 24)), (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK, p) if *(*int32)(unsafe.Pointer(bp + 16)) != SQLITE_OK { goto _3 } if Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 24))) == int32(SQLITE_ROW) { if int32((*TSessionChange)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INSERT) { _sessionAppendByte(tls, bp, uint8(SQLITE_INSERT), bp+16) _sessionAppendByte(tls, bp, (*TSessionChange)(unsafe.Pointer(p)).FbIndirect, bp+16) iCol = 0 for { if !(iCol < (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) { break } _sessionAppendCol(tls, bp, *(*uintptr)(unsafe.Pointer(bp + 24)), iCol, bp+16) goto _4 _4: ; iCol++ } } else { *(*int32)(unsafe.Pointer(bp + 16)) = _sessionAppendUpdate(tls, bp, bPatchset, *(*uintptr)(unsafe.Pointer(bp + 24)), p, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK) } } else { if int32((*TSessionChange)(unsafe.Pointer(p)).Fop) != int32(SQLITE_INSERT) { *(*int32)(unsafe.Pointer(bp + 16)) = _sessionAppendDelete(tls, bp, bPatchset, p, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK) } } if *(*int32)(unsafe.Pointer(bp + 16)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 16)) = Xsqlite3_reset(tls, *(*uintptr)(unsafe.Pointer(bp + 24))) } /* 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 + 16)) == SQLITE_OK && (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf > nNoop && (*(*TSessionBuffer)(unsafe.Pointer(bp))).FnBuf > _sessions_strm_chunk_size { *(*int32)(unsafe.Pointer(bp + 16)) = (*(*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 + 24))) 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 + 16)) == 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 + 16)) = (*(*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+34456, uintptr(0), uintptr(0), uintptr(0)) Xsqlite3_mutex_leave(tls, Xsqlite3_db_mutex(tls, db)) return *(*int32)(unsafe.Pointer(bp + 16)) } // 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) 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 */ /* Zero the output variable in case an error occurs. */ *(*uintptr)(unsafe.Pointer(pp)) = uintptr(0) /* Allocate and initialize the iterator structure. */ nByte = int32(152) pRet = Xsqlite3_malloc(tls, nByte) if !(pRet != 0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pRet, 0, uint64(152), libc.X__builtin_object_size(tls, pRet, 0)) (*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 if nMove > 0 { libc.X__builtin___memmove_chk(tls, (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FaBuf, (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FaBuf+uintptr((*TSessionInput)(unsafe.Pointer(pIn)).FiNext), uint64(nMove), libc.X__builtin_object_size(tls, (*TSessionInput)(unsafe.Pointer(pIn)).Fbuf.FaBuf, 0)) } (*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+32, 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.X__builtin___memcpy_chk(tls, aCopy, aData, uint64(nData), libc.X__builtin_object_size(tls, aCopy, 0)) _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 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(228355)) } 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)))) if eType != 0 { if pbEmpty != 0 { *(*int32)(unsafe.Pointer(pbEmpty)) = 0 } *(*uintptr)(unsafe.Pointer(apOut + uintptr(i)*8)) = _sqlite3ValueNew(tls, uintptr(0)) if !(*(*uintptr)(unsafe.Pointer(apOut + uintptr(i)*8)) != 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(228375)) } 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)*8)), (*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(228385)) } else { *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8)) = _sessionGetI64(tls, aVal) if eType == int32(SQLITE_INTEGER) { _sqlite3VdbeMemSetInt64(tls, *(*uintptr)(unsafe.Pointer(apOut + uintptr(i)*8)), *(*Tsqlite3_int64)(unsafe.Pointer(bp + 8))) } else { libc.X__builtin___memcpy_chk(tls, bp+16, bp+8, uint64(8), libc.X__builtin_object_size(tls, bp+16, 0)) _sqlite3VdbeMemSetDouble(tls, *(*uintptr)(unsafe.Pointer(apOut + uintptr(i)*8)), *(*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(228433)) } 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 *(*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+120) 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(uint64((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*uint64(8)*uint64(2) + uint64(*(*int32)(unsafe.Pointer(bp + 4)))) (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FnBuf = 0 _sessionBufferGrow(tls, p+72, int64(nByte), bp) } else { *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(228521)) } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { iPK = uint64(8) * uint64((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol) * uint64(2) libc.X__builtin___memset_chk(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FaBuf, 0, iPK, libc.X__builtin_object_size(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FaBuf, 0)) libc.X__builtin___memcpy_chk(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), uint64(*(*int32)(unsafe.Pointer(bp + 4))), libc.X__builtin_object_size(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Ftblhdr.FaBuf+uintptr(iPK), 0)) (*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))*8 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 /* 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)*8))) goto _1 _1: ; i++ } libc.X__builtin___memset_chk(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue, 0, uint64(8)*uint64((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*uint64(2), libc.X__builtin_object_size(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue, 0)) } /* 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 } _sessionDiscardData(tls, p) (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiCurrent = (*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).Fin.FiNext /* 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) } 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. */ v7 = _sqlite3CorruptError(tls, int32(228607)) (*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(228613)) (*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)*8 } 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)*8 } 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 } if *(*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)*8)) = *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i+(*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FnCol)*8)) if *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*8)) == uintptr(0) { v16 = _sqlite3CorruptError(tls, int32(228657)) (*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)*8)) = 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)*8)) == uintptr(0) { _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*8))) *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(p)).FapValue + uintptr(i)*8)) = 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)*8)) 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)*8)) 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)*8))) 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(64) defer tls.Free(64) var abPK, apVal, pVal, pVal1, v7 uintptr var bIndirect, eType2, iCol, nVar, v1, v2, v3, v5 int32 var eType Tu8 var _ /* nByte at bp+48 */ int32 var _ /* nByte at bp+52 */ int32 var _ /* nCol at bp+24 */ int32 var _ /* rc at bp+0 */ int32 var _ /* sOut at bp+8 */ TSessionBuffer var _ /* sPK at bp+32 */ 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 + 24)) = 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 + 32)) = TSessionBuffer{} /* PK array for current table */ /* Initialize the output buffer */ libc.X__builtin___memset_chk(tls, bp+8, 0, uint64(16), libc.X__builtin_object_size(tls, bp+8, 0)) /* 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+48) *(*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+24) (*(*TSessionBuffer)(unsafe.Pointer(bp + 32))).FnBuf = 0 _sessionAppendBlob(tls, bp+32, (*TSessionInput)(unsafe.Pointer(pInput)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext+nVar), *(*int32)(unsafe.Pointer(bp + 24)), bp) _sessionAppendByte(tls, bp+8, eType, bp) _sessionAppendBlob(tls, bp+8, (*TSessionInput)(unsafe.Pointer(pInput)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext), *(*int32)(unsafe.Pointer(bp + 48)), bp) if *(*int32)(unsafe.Pointer(bp)) != 0 { goto finished_invert } *(*int32)(unsafe.Pointer(pInput + 8)) += *(*int32)(unsafe.Pointer(bp + 48)) Xsqlite3_free(tls, apVal) apVal = uintptr(0) abPK = (*(*TSessionBuffer)(unsafe.Pointer(bp + 32))).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) *(*int32)(unsafe.Pointer(bp)) = _sessionChangesetBufferRecord(tls, pInput, *(*int32)(unsafe.Pointer(bp + 24)), bp+52) _sessionAppendByte(tls, bp+8, uint8(eType2), bp) _sessionAppendByte(tls, bp+8, uint8(bIndirect), bp) _sessionAppendBlob(tls, bp+8, (*TSessionInput)(unsafe.Pointer(pInput)).FaData+uintptr((*TSessionInput)(unsafe.Pointer(pInput)).FiNext), *(*int32)(unsafe.Pointer(bp + 52)), bp) *(*int32)(unsafe.Pointer(pInput + 8)) += *(*int32)(unsafe.Pointer(bp + 52)) if *(*int32)(unsafe.Pointer(bp)) != 0 { goto finished_invert } case int32(SQLITE_UPDATE): if uintptr(0) == apVal { apVal = Xsqlite3_malloc64(tls, uint64(8)*uint64(*(*int32)(unsafe.Pointer(bp + 24)))*uint64(2)) if uintptr(0) == apVal { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) goto finished_invert } libc.X__builtin___memset_chk(tls, apVal, 0, uint64(8)*uint64(*(*int32)(unsafe.Pointer(bp + 24)))*uint64(2), libc.X__builtin_object_size(tls, apVal, 0)) } /* Write the header for the new UPDATE change. Same as the original. */ _sessionAppendByte(tls, bp+8, eType, bp) _sessionAppendByte(tls, bp+8, *(*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 + 24)), uintptr(0), apVal, uintptr(0)) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sessionReadRecord(tls, pInput, *(*int32)(unsafe.Pointer(bp + 24)), uintptr(0), apVal+uintptr(*(*int32)(unsafe.Pointer(bp + 24)))*8, 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 + 24))) { break } if *(*Tu8)(unsafe.Pointer(abPK + uintptr(iCol))) != 0 { v5 = 0 } else { v5 = *(*int32)(unsafe.Pointer(bp + 24)) } pVal = *(*uintptr)(unsafe.Pointer(apVal + uintptr(iCol+v5)*8)) _sessionAppendValue(tls, bp+8, 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 + 24))) { break } if *(*Tu8)(unsafe.Pointer(abPK + uintptr(iCol))) != 0 { v7 = uintptr(0) } else { v7 = *(*uintptr)(unsafe.Pointer(apVal + uintptr(iCol)*8)) } pVal1 = v7 _sessionAppendValue(tls, bp+8, pVal1, bp) goto _6 _6: ; iCol++ } iCol = 0 for { if !(iCol < *(*int32)(unsafe.Pointer(bp + 24))*int32(2)) { break } _sqlite3ValueFree(tls, *(*uintptr)(unsafe.Pointer(apVal + uintptr(iCol)*8))) goto _8 _8: ; iCol++ } libc.X__builtin___memset_chk(tls, apVal, 0, uint64(8)*uint64(*(*int32)(unsafe.Pointer(bp + 24)))*uint64(2), libc.X__builtin_object_size(tls, apVal, 0)) if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { goto finished_invert } default: *(*int32)(unsafe.Pointer(bp)) = _sqlite3CorruptError(tls, int32(229022)) goto finished_invert } if xOutput != 0 && (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).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 + 8))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FnBuf) (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FnBuf = 0 if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { goto finished_invert } } } if pnInverted != 0 && ppInverted != 0 { *(*int32)(unsafe.Pointer(pnInverted)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FnBuf *(*uintptr)(unsafe.Pointer(ppInverted)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FaBuf (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FaBuf = uintptr(0) } else { if (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).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 + 8))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FnBuf) } } goto finished_invert finished_invert: ; Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FaBuf) Xsqlite3_free(tls, apVal) Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 32))).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(80) defer tls.Free(80) var _ /* sInput at bp+0 */ TSessionInput /* Set up the input stream */ libc.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 0)) (*(*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(80) defer tls.Free(80) var rc int32 var _ /* sInput at bp+0 */ TSessionInput _ = rc /* Set up the input stream */ libc.X__builtin___memset_chk(tls, bp, 0, uint64(72), libc.X__builtin_object_size(tls, bp, 0)) (*(*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(32) defer tls.Free(32) var bStat1, ii, nByte, nCol, nU32, nUp int32 var pUp, pp, zSep, zSql uintptr var _ /* buf at bp+8 */ 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(uint64(nU32)*uint64(4))) if (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { libc.X__builtin___memset_chk(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask, 0, uint64(nU32)*uint64(4), libc.X__builtin_object_size(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask, 0)) *(*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)*8)) != 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 + 64 for int32(1) != 0 { nUp++ if 0 == libc.Xmemcmp(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask, (*TSessionUpdate)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FaMask, uint64(nU32)*uint64(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)) + 16 } 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(uint64(24) * uint64(nU32) * uint64(4)) bStat1 = libc.BoolInt32(Xsqlite3_stricmp(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab, __ccgo_ts+12055) == 0) pUp = Xsqlite3_malloc(tls, nByte) if pUp == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) } else { zSep = __ccgo_ts + 1664 libc.X__builtin___memset_chk(tls, bp+8, 0, uint64(16), libc.X__builtin_object_size(tls, bp+8, 0)) (*TSessionUpdate)(unsafe.Pointer(pUp)).FaMask = pUp + 1*24 libc.X__builtin___memcpy_chk(tls, (*TSessionUpdate)(unsafe.Pointer(pUp)).FaMask, (*TSessionApplyCtx)(unsafe.Pointer(p)).FaUpdateMask, uint64(nU32)*uint64(4), libc.X__builtin_object_size(tls, (*TSessionUpdate)(unsafe.Pointer(pUp)).FaMask, 0)) _sessionAppendStr(tls, bp+8, __ccgo_ts+34474, bp) _sessionAppendIdent(tls, bp+8, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FzTab, bp) _sessionAppendStr(tls, bp+8, __ccgo_ts+34487, 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)*8)) != 0 { _sessionAppendStr(tls, bp+8, zSep, bp) _sessionAppendIdent(tls, bp+8, *(*uintptr)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(ii)*8)), bp) _sessionAppendStr(tls, bp+8, __ccgo_ts+34493, bp) _sessionAppendInteger(tls, bp+8, ii*int32(2)+int32(1), bp) zSep = __ccgo_ts + 15431 } goto _2 _2: ; ii++ } /* Create the WHERE clause part of the UPDATE */ zSep = __ccgo_ts + 1664 _sessionAppendStr(tls, bp+8, __ccgo_ts+34498, 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)*8)) != 0 { _sessionAppendStr(tls, bp+8, zSep, bp) if bStat1 != 0 && ii == int32(1) { _sessionAppendStr(tls, bp+8, __ccgo_ts+34506, bp) } else { _sessionAppendIdent(tls, bp+8, *(*uintptr)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(ii)*8)), bp) _sessionAppendStr(tls, bp+8, __ccgo_ts+34581, bp) _sessionAppendInteger(tls, bp+8, ii*int32(2)+int32(2), bp) } zSep = __ccgo_ts + 22553 } goto _3 _3: ; ii++ } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { zSql = (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FaBuf *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_prepare_v2(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).Fdb, zSql, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).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 + 8))).FaBuf) } } } 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(32) defer tls.Free(32) var i, nPk int32 var zSep uintptr var _ /* buf at bp+8 */ TSessionBuffer var _ /* rc at bp+0 */ int32 _, _, _ = i, nPk, zSep zSep = __ccgo_ts + 1664 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*TSessionBuffer)(unsafe.Pointer(bp + 8)) = TSessionBuffer{} nPk = 0 _sessionAppendStr(tls, bp+8, __ccgo_ts+34587, bp) _sessionAppendIdent(tls, bp+8, zTab, bp) _sessionAppendStr(tls, bp+8, __ccgo_ts+34498, 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+8, zSep, bp) _sessionAppendIdent(tls, bp+8, *(*uintptr)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(i)*8)), bp) _sessionAppendStr(tls, bp+8, __ccgo_ts+34493, bp) _sessionAppendInteger(tls, bp+8, i+int32(1), bp) zSep = __ccgo_ts + 22553 } goto _1 _1: ; i++ } if nPk < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol { _sessionAppendStr(tls, bp+8, __ccgo_ts+34605, bp) _sessionAppendInteger(tls, bp+8, (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol+int32(1), bp) _sessionAppendStr(tls, bp+8, __ccgo_ts+34069, bp) zSep = __ccgo_ts + 1664 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+8, zSep, bp) _sessionAppendIdent(tls, bp+8, *(*uintptr)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(i)*8)), bp) _sessionAppendStr(tls, bp+8, __ccgo_ts+34581, bp) _sessionAppendInteger(tls, bp+8, i+int32(1), bp) zSep = __ccgo_ts + 34613 } goto _2 _2: ; i++ } _sessionAppendStr(tls, bp+8, __ccgo_ts+5518, bp) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_prepare_v2(tls, db, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FnBuf, p+8, uintptr(0)) } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).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+6997, zTab, (*TSessionApplyCtx)(unsafe.Pointer(p)).FbRowid, (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol, (*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol, (*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK, p+24) } // 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(32) defer tls.Free(32) var i int32 var _ /* buf at bp+8 */ TSessionBuffer var _ /* rc at bp+0 */ int32 _ = i *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*TSessionBuffer)(unsafe.Pointer(bp + 8)) = TSessionBuffer{} _sessionAppendStr(tls, bp+8, __ccgo_ts+34618, bp) _sessionAppendIdent(tls, bp+8, zTab, bp) _sessionAppendStr(tls, bp+8, __ccgo_ts+22559, bp) i = 0 for { if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) { break } if i != 0 { _sessionAppendStr(tls, bp+8, __ccgo_ts+15431, bp) } _sessionAppendIdent(tls, bp+8, *(*uintptr)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FazCol + uintptr(i)*8)), bp) goto _1 _1: ; i++ } _sessionAppendStr(tls, bp+8, __ccgo_ts+34636, bp) i = int32(1) for { if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) { break } _sessionAppendStr(tls, bp+8, __ccgo_ts+34647, bp) goto _2 _2: ; i++ } _sessionAppendStr(tls, bp+8, __ccgo_ts+5518, bp) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_prepare_v2(tls, db, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FnBuf, p+16, uintptr(0)) } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).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+12055, p) if rc == SQLITE_OK { rc = _sessionPrepare(tls, db, p+16, __ccgo_ts+34651) } if rc == SQLITE_OK { rc = _sessionPrepare(tls, db, p+8, __ccgo_ts+34764) } 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. */ 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(229504)) } 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(32) defer tls.Free(32) var ii, rc int32 var pSelect, v1 uintptr var _ /* nCol at bp+0 */ int32 var _ /* op at bp+4 */ int32 var _ /* pVal at bp+16 */ 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 + 16)) = uintptr(0) Xsqlite3changeset_new(tls, pIter, ii, bp+16) Xsqlite3_bind_int(tls, pSelect, ii+int32(1)+*(*int32)(unsafe.Pointer(bp)), libc.BoolInt32(*(*uintptr)(unsafe.Pointer(bp + 16)) == uintptr(0))) if *(*uintptr)(unsafe.Pointer(bp + 16)) != 0 { rc = _sessionBindValue(tls, pSelect, ii+int32(1), *(*uintptr)(unsafe.Pointer(bp + 16))) } } 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+8 */ 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+104, uint8('T'), bp) _sessionAppendVarint(tls, p+104, (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol, bp) _sessionAppendBlob(tls, p+104, (*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK, (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol, bp) _sessionAppendBlob(tls, p+104, zTab, int32(libc.Xstrlen(tls, zTab))+int32(1), bp) (*TSessionApplyCtx)(unsafe.Pointer(p)).FbRebaseStarted = uint8(1) } if eOp == int32(SQLITE_DELETE) { v1 = int32(SQLITE_DELETE) } else { v1 = int32(SQLITE_INSERT) } _sessionAppendByte(tls, p+104, uint8(v1), bp) _sessionAppendByte(tls, p+104, libc.BoolUint8(eType == libc.Int32FromInt32(SQLITE_CHANGESET_REPLACE)), bp) i = 0 for { if !(i < (*TSessionApplyCtx)(unsafe.Pointer(p)).FnCol) { break } *(*uintptr)(unsafe.Pointer(bp + 8)) = 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+8) } else { Xsqlite3changeset_new(tls, pIter, i, bp+8) } _sessionAppendValue(tls, p+104, *(*uintptr)(unsafe.Pointer(bp + 8)), 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(32) defer tls.Free(32) 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+16 */ uintptr _, _, _ = aBlob, nBlob, res res = 0 Xsqlite3changeset_op(tls, pIter, bp+16, bp+4, bp+8, uintptr(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+88, 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): *(*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(32) defer tls.Free(32) var abPK, pNew, pOld, v1 uintptr var bPatchset, i, rc int32 var _ /* nCol at bp+12 */ int32 var _ /* op at bp+8 */ int32 var _ /* pUp at bp+16 */ uintptr var _ /* zDummy at bp+0 */ uintptr _, _, _, _, _, _, _ = abPK, bPatchset, i, pNew, pOld, rc, v1 rc = SQLITE_OK Xsqlite3changeset_op(tls, pIter, bp, bp+12, bp+8, uintptr(0)) if *(*int32)(unsafe.Pointer(bp + 8)) == 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 + 12)), abPK, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpDelete) if rc == SQLITE_OK && Xsqlite3_bind_parameter_count(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpDelete) > *(*int32)(unsafe.Pointer(bp + 12)) { rc = Xsqlite3_bind_int(tls, (*TSessionApplyCtx)(unsafe.Pointer(p)).FpDelete, *(*int32)(unsafe.Pointer(bp + 12))+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 + 8)) == int32(SQLITE_UPDATE) { *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) bPatchset = libc.BoolInt32(pbRetry == uintptr(0) || (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset != 0) rc = _sessionUpdateFind(tls, pIter, p, bPatchset, bp+16) /* Bind values to the UPDATE statement. */ i = 0 for { if !(rc == SQLITE_OK && i < *(*int32)(unsafe.Pointer(bp + 12))) { break } pOld = *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue + uintptr(i)*8)) pNew = *(*uintptr)(unsafe.Pointer((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FapValue + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol+i)*8)) if *(*Tu8)(unsafe.Pointer((*TSessionApplyCtx)(unsafe.Pointer(p)).FabPK + uintptr(i))) != 0 || bPatchset == 0 && pOld != 0 { rc = _sessionBindValue(tls, *(*uintptr)(unsafe.Pointer(bp + 16)), i*int32(2)+int32(2), pOld) } if rc == SQLITE_OK && pNew != 0 { rc = _sessionBindValue(tls, *(*uintptr)(unsafe.Pointer(bp + 16)), 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 + 16))) rc = Xsqlite3_reset(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) 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 { 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 + 12)), 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 { rc = _sessionApplyOneOp(tls, pIter, pApply, xConflict, pCtx, uintptr(0), uintptr(0)) } else { if *(*int32)(unsafe.Pointer(bp)) != 0 { rc = Xsqlite3_exec(tls, db, __ccgo_ts+34908, 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+34929, 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+8 */ 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 + 8)) = uintptr(0) cons = (*TSessionApplyCtx)(unsafe.Pointer(pApply)).Fconstraints libc.X__builtin___memset_chk(tls, pApply+88, 0, uint64(16), libc.X__builtin_object_size(tls, pApply+88, 0)) *(*int32)(unsafe.Pointer(bp)) = _sessionChangesetStart(tls, bp+8, uintptr(0), uintptr(0), cons.FnBuf, cons.FaBuf, (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FbInvertConstraints, int32(1)) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { nByte = uint64(int32(2)*(*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol) * uint64(8) (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FbPatchset = bPatchset (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FzTab = zTab (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FnCol = (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FnCol (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FabPK = (*TSessionApplyCtx)(unsafe.Pointer(pApply)).FabPK _sessionBufferGrow(tls, *(*uintptr)(unsafe.Pointer(bp + 8))+72, int64(nByte), bp) (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FapValue = (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).Ftblhdr.FaBuf if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { libc.X__builtin___memset_chk(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FapValue, 0, nByte, libc.X__builtin_object_size(tls, (*Tsqlite3_changeset_iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FapValue, 0)) } for *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3changeset_next(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) { *(*int32)(unsafe.Pointer(bp)) = _sessionApplyOneWithRetry(tls, db, *(*uintptr)(unsafe.Pointer(bp + 8)), pApply, xConflict, pCtx) } rc2 = Xsqlite3changeset_finalize(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = rc2 } } 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(352) defer tls.Free(352) var bPatchset, i, nMinCol, nTab, rc, res, schemaMismatch, v2, v3, v4, v6 int32 var v5, v7 bool var _ /* abPK at bp+152 */ uintptr var _ /* nCol at bp+136 */ int32 var _ /* nFk at bp+160 */ int32 var _ /* notUsed at bp+164 */ int32 var _ /* op at bp+140 */ int32 var _ /* sApply at bp+8 */ TSessionApplyCtx var _ /* sIter at bp+168 */ Tsqlite3_changeset_iter var _ /* zNew at bp+144 */ 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 (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FbNoDiscard = int32(1) libc.X__builtin___memset_chk(tls, bp+8, 0, uint64(128), libc.X__builtin_object_size(tls, bp+8, 0)) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FbRebase = libc.BoolUint8(ppRebase != 0 && pnRebase != 0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FbInvertConstraints = libc.BoolInt32(!!(flags&libc.Int32FromInt32(SQLITE_CHANGESETAPPLY_INVERT) != 0)) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).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+34948, uintptr(0), uintptr(0), uintptr(0)) } if rc == SQLITE_OK { rc = Xsqlite3_exec(tls, db, __ccgo_ts+34974, uintptr(0), uintptr(0), uintptr(0)) } for rc == SQLITE_OK && int32(SQLITE_ROW) == Xsqlite3changeset_next(tls, pIter) { Xsqlite3changeset_op(tls, pIter, bp+144, bp+136, bp+140, uintptr(0)) if *(*uintptr)(unsafe.Pointer(bp)) == uintptr(0) || Xsqlite3_strnicmp(tls, *(*uintptr)(unsafe.Pointer(bp + 144)), *(*uintptr)(unsafe.Pointer(bp)), nTab+int32(1)) != 0 { rc = _sessionRetryConstraints(tls, db, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset, *(*uintptr)(unsafe.Pointer(bp)), bp+8, xConflict, pCtx) if rc != SQLITE_OK { break } _sessionUpdateFree(tls, bp+8) Xsqlite3_free(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FazCol) /* cast works around VC++ bug */ Xsqlite3_finalize(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FpDelete) Xsqlite3_finalize(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FpInsert) Xsqlite3_finalize(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FpSelect) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).Fdb = db (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FpDelete = uintptr(0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FpInsert = uintptr(0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FpSelect = uintptr(0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FnCol = 0 (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FazCol = uintptr(0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FabPK = uintptr(0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FbStat1 = 0 (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FbDeferConstraints = int32(1) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FbRebaseStarted = uint8(0) (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FbRowid = 0 libc.X__builtin___memset_chk(tls, bp+8+88, 0, uint64(16), libc.X__builtin_object_size(tls, bp+8+88, 0)) /* 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 + 144)))) if schemaMismatch != 0 { *(*uintptr)(unsafe.Pointer(bp)) = Xsqlite3_mprintf(tls, __ccgo_ts+3863, libc.VaList(bp+328, *(*uintptr)(unsafe.Pointer(bp + 144)))) 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 + 8))).FazCol = *(*uintptr)(unsafe.Pointer(bp)) } else { nMinCol = 0 Xsqlite3changeset_pk(tls, pIter, bp+152, uintptr(0)) rc = _sessionTableInfo(tls, uintptr(0), db, __ccgo_ts+6997, *(*uintptr)(unsafe.Pointer(bp + 144)), bp+8+32, bp, bp+8+40, uintptr(0), bp+8+48, bp+8+124) if rc != SQLITE_OK { break } i = 0 for { if !(i < (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FnCol) { break } if *(*Tu8)(unsafe.Pointer((*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FabPK + uintptr(i))) != 0 { nMinCol = i + int32(1) } goto _1 _1: ; i++ } if (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FnCol == 0 { schemaMismatch = int32(1) Xsqlite3_log(tls, int32(SQLITE_SCHEMA), __ccgo_ts+35004, libc.VaList(bp+328, *(*uintptr)(unsafe.Pointer(bp)))) } else { if (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FnCol < *(*int32)(unsafe.Pointer(bp + 136)) { schemaMismatch = int32(1) Xsqlite3_log(tls, int32(SQLITE_SCHEMA), __ccgo_ts+35048, libc.VaList(bp+328, *(*uintptr)(unsafe.Pointer(bp)), (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FnCol, *(*int32)(unsafe.Pointer(bp + 136)))) } else { if *(*int32)(unsafe.Pointer(bp + 136)) < nMinCol || libc.Xmemcmp(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FabPK, *(*uintptr)(unsafe.Pointer(bp + 152)), uint64(*(*int32)(unsafe.Pointer(bp + 136)))) != 0 { schemaMismatch = int32(1) Xsqlite3_log(tls, int32(SQLITE_SCHEMA), __ccgo_ts+35119, libc.VaList(bp+328, *(*uintptr)(unsafe.Pointer(bp)))) } else { (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FnCol = *(*int32)(unsafe.Pointer(bp + 136)) if 0 == Xsqlite3_stricmp(tls, *(*uintptr)(unsafe.Pointer(bp)), __ccgo_ts+12055) { v2 = _sessionStat1Sql(tls, db, bp+8) rc = v2 if v2 != 0 { break } (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FbStat1 = int32(1) } else { v3 = _sessionSelectRow(tls, db, *(*uintptr)(unsafe.Pointer(bp)), bp+8) rc = v3 if v5 = v3 != 0; !v5 { v4 = _sessionDeleteRow(tls, db, *(*uintptr)(unsafe.Pointer(bp)), bp+8) rc = v4 } if v7 = v5 || v4 != 0; !v7 { v6 = _sessionInsertRow(tls, db, *(*uintptr)(unsafe.Pointer(bp)), bp+8) rc = v6 } if v7 || v6 != 0 { break } (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).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+8, 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+8, xConflict, pCtx) } if rc == SQLITE_OK { Xsqlite3_db_status(tls, db, int32(SQLITE_DBSTATUS_DEFERRED_FKS), bp+160, bp+164, 0) if *(*int32)(unsafe.Pointer(bp + 160)) != 0 { res = int32(SQLITE_CHANGESET_ABORT) libc.X__builtin___memset_chk(tls, bp+168, 0, uint64(152), libc.X__builtin_object_size(tls, bp+168, 0)) (*(*Tsqlite3_changeset_iter)(unsafe.Pointer(bp + 168))).FnCol = *(*int32)(unsafe.Pointer(bp + 160)) res = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{xConflict})))(tls, pCtx, int32(SQLITE_CHANGESET_FOREIGN_KEY), bp+168) if res != SQLITE_CHANGESET_OMIT { rc = int32(SQLITE_CONSTRAINT) } } } Xsqlite3_exec(tls, db, __ccgo_ts+35179, uintptr(0), uintptr(0), uintptr(0)) if flags&int32(SQLITE_CHANGESETAPPLY_NOSAVEPOINT) == 0 { if rc == SQLITE_OK { rc = Xsqlite3_exec(tls, db, __ccgo_ts+35209, uintptr(0), uintptr(0), uintptr(0)) } else { Xsqlite3_exec(tls, db, __ccgo_ts+35233, uintptr(0), uintptr(0), uintptr(0)) Xsqlite3_exec(tls, db, __ccgo_ts+35209, uintptr(0), uintptr(0), uintptr(0)) } } if rc == SQLITE_OK && bPatchset == 0 && (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FbRebase != 0 { *(*uintptr)(unsafe.Pointer(ppRebase)) = (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).Frebase.FaBuf *(*int32)(unsafe.Pointer(pnRebase)) = (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).Frebase.FnBuf (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).Frebase.FaBuf = uintptr(0) } _sessionUpdateFree(tls, bp+8) Xsqlite3_finalize(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FpInsert) Xsqlite3_finalize(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FpDelete) Xsqlite3_finalize(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FpSelect) Xsqlite3_free(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).FazCol) /* cast works around VC++ bug */ Xsqlite3_free(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).Fconstraints.FaBuf) Xsqlite3_free(tls, (*(*TSessionApplyCtx)(unsafe.Pointer(bp + 8))).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 + 48)) |= 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) { *(*Tu64)(unsafe.Pointer(db + 48)) &= ^(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 Frec TSessionBuffer 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(32) defer tls.Free(32) 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+16 */ uintptr var _ /* a1 at bp+8 */ uintptr var _ /* a2 at bp+24 */ 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 if !(pExist != 0) { pNew = Xsqlite3_malloc64(tls, uint64(32)+uint64(nRec)) if !(pNew != 0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pNew, 0, uint64(32), libc.X__builtin_object_size(tls, pNew, 0)) (*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(op2) (*TSessionChange)(unsafe.Pointer(pNew)).FbIndirect = uint8(bIndirect) (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord = pNew + 1*32 if bIndirect == 0 || bRebase == 0 { (*TSessionChange)(unsafe.Pointer(pNew)).FnRecord = nRec libc.X__builtin___memcpy_chk(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord, aRec, uint64(nRec), libc.X__builtin_object_size(tls, (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord, 0)) } 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.X__builtin___memcpy_chk(tls, pOut, pIn, uint64(nIn), libc.X__builtin_object_size(tls, pOut, 0)) pOut += uintptr(nIn) } } pIn += uintptr(nIn) goto _1 _1: ; i++ } (*TSessionChange)(unsafe.Pointer(pNew)).FnRecord = int32(int64(pOut) - int64((*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(uint64(nRec+(*TSessionChange)(unsafe.Pointer(pExist)).FnRecord) + uint64(32)) pNew = Xsqlite3_malloc64(tls, uint64(nByte)) if pNew == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { a1 = (*TSessionChange)(unsafe.Pointer(pExist)).FaRecord a2 = aRec libc.X__builtin___memset_chk(tls, pNew, 0, uint64(nByte), libc.X__builtin_object_size(tls, pNew, 0)) (*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*32 (*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.X__builtin___memcpy_chk(tls, pOut1, a1, uint64(n1), libc.X__builtin_object_size(tls, pOut1, 0)) pOut1 += uintptr(n1) } else { libc.X__builtin___memcpy_chk(tls, pOut1, a2, uint64(n2), libc.X__builtin_object_size(tls, pOut1, 0)) pOut1 += uintptr(n2) } } a1 += uintptr(n1) a2 += uintptr(n2) goto _5 _5: ; i1++ } (*TSessionChange)(unsafe.Pointer(pNew)).FnRecord = int32(int64(pOut1) - int64((*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) } 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(uint64(32) + uint64((*TSessionChange)(unsafe.Pointer(pExist)).FnRecord) + uint64(nRec)) pNew = Xsqlite3_malloc64(tls, uint64(nByte1)) if !(pNew != 0) { Xsqlite3_free(tls, pExist) return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pNew, 0, uint64(32), libc.X__builtin_object_size(tls, pNew, 0)) (*TSessionChange)(unsafe.Pointer(pNew)).FbIndirect = libc.BoolUint8(bIndirect != 0 && (*TSessionChange)(unsafe.Pointer(pExist)).FbIndirect != 0) v7 = pNew + 1*32 (*TSessionChange)(unsafe.Pointer(pNew)).FaRecord = v7 *(*uintptr)(unsafe.Pointer(bp)) = v7 if op1 == int32(SQLITE_INSERT) { /* INSERT + UPDATE */ *(*uintptr)(unsafe.Pointer(bp + 8)) = aRec (*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(SQLITE_INSERT) if bPatchset == 0 { _sessionSkipRecord(tls, bp+8, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) } _sessionMergeRecord(tls, bp, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol, aExist, *(*uintptr)(unsafe.Pointer(bp + 8))) } else { if op1 == int32(SQLITE_DELETE) { /* DELETE + INSERT */ (*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(SQLITE_UPDATE) if bPatchset != 0 { libc.X__builtin___memcpy_chk(tls, *(*uintptr)(unsafe.Pointer(bp)), aRec, uint64(nRec), libc.X__builtin_object_size(tls, *(*uintptr)(unsafe.Pointer(bp)), 0)) *(*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 + 16)) = aExist *(*uintptr)(unsafe.Pointer(bp + 24)) = aRec if bPatchset == 0 { _sessionSkipRecord(tls, bp+16, (*TSessionTable)(unsafe.Pointer(pTab)).FnCol) _sessionSkipRecord(tls, bp+24, (*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 + 16)), *(*uintptr)(unsafe.Pointer(bp + 24))) { Xsqlite3_free(tls, pNew) pNew = uintptr(0) } } else { /* UPDATE + DELETE */ (*TSessionChange)(unsafe.Pointer(pNew)).Fop = uint8(SQLITE_DELETE) if bPatchset != 0 { libc.X__builtin___memcpy_chk(tls, *(*uintptr)(unsafe.Pointer(bp)), aRec, uint64(nRec), libc.X__builtin_object_size(tls, *(*uintptr)(unsafe.Pointer(bp)), 0)) *(*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(int64(*(*uintptr)(unsafe.Pointer(bp))) - int64((*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, uint64(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 (*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+72) } 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.X__builtin___memcpy_chk(tls, bp+8, bp+16, uint64(8), libc.X__builtin_object_size(tls, bp+8, 0)) } 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: 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 { _sessionAppendBlob(tls, pOut, aRec, nRec, bp) } } return *(*int32)(unsafe.Pointer(bp)) } // C documentation // // /* // ** Locate or create a SessionTable object that may be used to add the // ** change currently pointed to by iterator pIter to changegroup pGrp. // ** If successful, set output variable (*ppTab) to point to the table // ** object and return SQLITE_OK. Otherwise, if some error occurs, return // ** an SQLite error code and leave (*ppTab) set to NULL. // */ func _sessionChangesetFindTable(tls *libc.TLS, pGrp uintptr, zTab uintptr, pIter uintptr, ppTab uintptr) (r int32) { bp := tls.Alloc(16) defer tls.Free(16) var nTab, rc int32 var pTab, ppNew uintptr var _ /* abPK at bp+0 */ uintptr var _ /* nCol at bp+8 */ int32 _, _, _, _ = nTab, pTab, ppNew, rc rc = SQLITE_OK pTab = uintptr(0) nTab = int32(libc.Xstrlen(tls, zTab)) *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 8)) = 0 *(*uintptr)(unsafe.Pointer(ppTab)) = uintptr(0) Xsqlite3changeset_pk(tls, pIter, bp, bp+8) /* Search the list for an existing table */ pTab = (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).FpList for { if !(pTab != 0) { break } if 0 == Xsqlite3_strnicmp(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zTab, nTab+int32(1)) { break } goto _1 _1: ; pTab = (*TSessionTable)(unsafe.Pointer(pTab)).FpNext } /* If one was not found above, create a new table now */ if !(pTab != 0) { pTab = Xsqlite3_malloc64(tls, uint64(80)+uint64(*(*int32)(unsafe.Pointer(bp + 8)))+uint64(nTab)+uint64(1)) if !(pTab != 0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pTab, 0, uint64(80), libc.X__builtin_object_size(tls, pTab, 0)) (*TSessionTable)(unsafe.Pointer(pTab)).FnCol = *(*int32)(unsafe.Pointer(bp + 8)) (*TSessionTable)(unsafe.Pointer(pTab)).FabPK = pTab + 1*80 libc.X__builtin___memcpy_chk(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK, *(*uintptr)(unsafe.Pointer(bp)), uint64(*(*int32)(unsafe.Pointer(bp + 8))), libc.X__builtin_object_size(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FabPK, 0)) (*TSessionTable)(unsafe.Pointer(pTab)).FzName = (*TSessionTable)(unsafe.Pointer(pTab)).FabPK + uintptr(*(*int32)(unsafe.Pointer(bp + 8))) libc.X__builtin___memcpy_chk(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, zTab, uint64(nTab+int32(1)), libc.X__builtin_object_size(tls, (*TSessionTable)(unsafe.Pointer(pTab)).FzName, 0)) 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 { Xsqlite3_free(tls, pTab) return rc } } /* 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. */ ppNew = pGrp + 8 for { if !(*(*uintptr)(unsafe.Pointer(ppNew)) != 0) { break } goto _2 _2: ; ppNew = *(*uintptr)(unsafe.Pointer(ppNew)) } *(*uintptr)(unsafe.Pointer(ppNew)) = pTab } /* Check that the table is compatible. */ if !(_sessionChangesetCheckCompat(tls, pTab, *(*int32)(unsafe.Pointer(bp + 8)), *(*uintptr)(unsafe.Pointer(bp))) != 0) { rc = int32(SQLITE_SCHEMA) } *(*uintptr)(unsafe.Pointer(ppTab)) = pTab return rc } // C documentation // // /* // ** Add the change currently indicated by iterator pIter to the hash table // ** belonging to changegroup pGrp. // */ func _sessionOneChangeToHash(tls *libc.TLS, pGrp uintptr, pIter uintptr, bRebase int32) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var aRec, pBuf, pExist, pp uintptr var bPkOnly1, bPkOnly2, iHash, nRec, rc int32 var _ /* bIndirect at bp+8 */ int32 var _ /* nCol at bp+0 */ int32 var _ /* op at bp+4 */ int32 var _ /* pChange at bp+16 */ uintptr var _ /* pTab at bp+24 */ uintptr var _ /* zTab at bp+32 */ uintptr _, _, _, _, _, _, _, _, _ = aRec, bPkOnly1, bPkOnly2, iHash, nRec, pBuf, pExist, pp, rc rc = SQLITE_OK *(*int32)(unsafe.Pointer(bp)) = 0 *(*int32)(unsafe.Pointer(bp + 4)) = 0 iHash = 0 *(*int32)(unsafe.Pointer(bp + 8)) = 0 *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) pExist = uintptr(0) pp = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 24)) = uintptr(0) aRec = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FaData + uintptr((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FiCurrent+int32(2)) nRec = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FiNext - (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FiCurrent - int32(2) /* 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) } } if rc == SQLITE_OK { *(*uintptr)(unsafe.Pointer(bp + 32)) = uintptr(0) Xsqlite3changeset_op(tls, pIter, bp+32, bp, bp+4, bp+8) rc = _sessionChangesetFindTable(tls, pGrp, *(*uintptr)(unsafe.Pointer(bp + 32)), pIter, bp+24) } if rc == SQLITE_OK && *(*int32)(unsafe.Pointer(bp)) < (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).FnCol { pBuf = pGrp + 16 rc = _sessionChangesetExtendRecord(tls, pGrp, *(*uintptr)(unsafe.Pointer(bp + 24)), *(*int32)(unsafe.Pointer(bp)), *(*int32)(unsafe.Pointer(bp + 4)), aRec, nRec, pBuf) aRec = (*TSessionBuffer)(unsafe.Pointer(pBuf)).FaBuf nRec = (*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf } if rc == SQLITE_OK && _sessionGrowHash(tls, uintptr(0), (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset, *(*uintptr)(unsafe.Pointer(bp + 24))) != 0 { rc = int32(SQLITE_NOMEM) } if rc == SQLITE_OK { /* Search for existing entry. If found, remove it from the hash table. ** Code below may link it back in. */ iHash = int32(_sessionChangeHash(tls, *(*uintptr)(unsafe.Pointer(bp + 24)), libc.BoolInt32((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset != 0 && *(*int32)(unsafe.Pointer(bp + 4)) == int32(SQLITE_DELETE)), aRec, (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).FnChange)) pp = (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).FapChange + uintptr(iHash)*8 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 + 4)) == int32(SQLITE_DELETE)) } if _sessionChangeEqual(tls, *(*uintptr)(unsafe.Pointer(bp + 24)), bPkOnly1, (*TSessionChange)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FaRecord, bPkOnly2, aRec) != 0 { pExist = *(*uintptr)(unsafe.Pointer(pp)) *(*uintptr)(unsafe.Pointer(pp)) = (*TSessionChange)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pp)))).FpNext (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).FnEntry-- break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 24 } } if rc == SQLITE_OK { rc = _sessionChangeMerge(tls, *(*uintptr)(unsafe.Pointer(bp + 24)), bRebase, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbPatchset, pExist, *(*int32)(unsafe.Pointer(bp + 4)), *(*int32)(unsafe.Pointer(bp + 8)), aRec, nRec, bp+16) } if rc == SQLITE_OK && *(*uintptr)(unsafe.Pointer(bp + 16)) != 0 { (*TSessionChange)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 16)))).FpNext = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).FapChange + uintptr(iHash)*8)) *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).FapChange + uintptr(iHash)*8)) = *(*uintptr)(unsafe.Pointer(bp + 16)) (*TSessionTable)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).FnEntry++ } if rc == SQLITE_OK { rc = (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Frc } return rc } // 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(16) defer tls.Free(16) var rc int32 var _ /* aRec at bp+0 */ uintptr var _ /* nRec at bp+8 */ int32 _ = rc rc = SQLITE_OK for int32(SQLITE_ROW) == _sessionChangesetNext(tls, pIter, bp, bp+8, uintptr(0)) { rc = _sessionOneChangeToHash(tls, pGrp, pIter, bRebase) if rc != SQLITE_OK { break } } 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(32) defer tls.Free(32) var i int32 var p, pTab uintptr var _ /* buf at bp+8 */ TSessionBuffer var _ /* rc at bp+0 */ int32 _, _, _ = i, p, pTab *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*TSessionBuffer)(unsafe.Pointer(bp + 8)) = TSessionBuffer{} /* 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+8, (*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)*8)) for { if !(p != 0) { break } _sessionAppendByte(tls, bp+8, (*TSessionChange)(unsafe.Pointer(p)).Fop, bp) _sessionAppendByte(tls, bp+8, (*TSessionChange)(unsafe.Pointer(p)).FbIndirect, bp) _sessionAppendBlob(tls, bp+8, (*TSessionChange)(unsafe.Pointer(p)).FaRecord, (*TSessionChange)(unsafe.Pointer(p)).FnRecord, bp) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && xOutput != 0 && (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).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 + 8))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FnBuf) (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).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 + 8))).FnBuf > 0 { *(*int32)(unsafe.Pointer(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{xOutput})))(tls, pOut, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FnBuf) } } else { if ppOut != 0 { *(*uintptr)(unsafe.Pointer(ppOut)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FaBuf if pnOut != 0 { *(*int32)(unsafe.Pointer(pnOut)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FnBuf } (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).FaBuf = uintptr(0) } } } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 8))).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(48)) if p == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, p, 0, uint64(48), libc.X__builtin_object_size(tls, p, 0)) } *(*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+3863, 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 // // /* // ** Add a single change to a changeset-group. // */ func Xsqlite3changegroup_add_change(tls *libc.TLS, pGrp uintptr, pIter uintptr) (r int32) { if (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FiCurrent == (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fin.FiNext || (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Frc != SQLITE_OK || (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbInvert != 0 { /* Iterator does not point to any valid entry or is an INVERT iterator. */ return int32(SQLITE_ERROR) } return _sessionOneChangeToHash(tls, pGrp, pIter, 0) } // 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, (*Tsqlite3_changegroup)(unsafe.Pointer(pGrp)).Frec.FaBuf) 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.X__builtin___memcpy_chk(tls, pOut, a2, uint64(nn2), libc.X__builtin_object_size(tls, pOut, 0)) pOut += uintptr(nn2) } else { libc.X__builtin___memcpy_chk(tls, pOut, a1, uint64(nn1), libc.X__builtin_object_size(tls, pOut, 0)) pOut += uintptr(nn1) } a1 += uintptr(nn1) a2 += uintptr(nn2) goto _1 _1: ; i++ } (*TSessionBuffer)(unsafe.Pointer(pBuf)).FnBuf = int32(int64(pOut) - int64((*TSessionBuffer)(unsafe.Pointer(pBuf)).FaBuf)) } } // 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.X__builtin___memcpy_chk(tls, pOut, a1, uint64(n1), libc.X__builtin_object_size(tls, pOut, 0)) pOut += uintptr(n1) } else { if int32(*(*Tu8)(unsafe.Pointer(a2))) != int32(0xFF) && *(*Tu8)(unsafe.Pointer(a1)) != 0 { bData = int32(1) libc.X__builtin___memcpy_chk(tls, pOut, a2, uint64(n2), libc.X__builtin_object_size(tls, pOut, 0)) 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.X__builtin___memcpy_chk(tls, pOut, a1, uint64(n11), libc.X__builtin_object_size(tls, pOut, 0)) 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(int64(pOut) - int64((*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(48) defer tls.Free(48) var bDone, iHash, v2 int32 var pChange, pTab, zTab uintptr var _ /* aRec at bp+8 */ uintptr var _ /* bNew at bp+20 */ int32 var _ /* nRec at bp+16 */ int32 var _ /* pCsr at bp+40 */ uintptr var _ /* rc at bp+0 */ int32 var _ /* sOut at bp+24 */ TSessionBuffer _, _, _, _, _, _ = bDone, iHash, pChange, pTab, zTab, v2 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 16)) = 0 *(*int32)(unsafe.Pointer(bp + 20)) = 0 pTab = uintptr(0) *(*TSessionBuffer)(unsafe.Pointer(bp + 24)) = TSessionBuffer{} for int32(SQLITE_ROW) == _sessionChangesetNext(tls, pIter, bp+8, bp+16, bp+20) { pChange = uintptr(0) bDone = 0 if *(*int32)(unsafe.Pointer(bp + 20)) != 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 + 20)) = 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+24, uint8(v2), bp) _sessionAppendVarint(tls, bp+24, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol, bp) _sessionAppendBlob(tls, bp+24, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FabPK, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol, bp) _sessionAppendBlob(tls, bp+24, (*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 + 8)), (*TSessionTable)(unsafe.Pointer(pTab)).FnChange)) pChange = *(*uintptr)(unsafe.Pointer((*TSessionTable)(unsafe.Pointer(pTab)).FapChange + uintptr(iHash)*8)) for { if !(pChange != 0) { break } if _sessionChangeEqual(tls, pTab, 0, *(*uintptr)(unsafe.Pointer(bp + 8)), 0, (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord) != 0 { break } goto _3 _3: ; pChange = (*TSessionChange)(unsafe.Pointer(pChange)).FpNext } } if pChange != 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+24, uint8(SQLITE_UPDATE), bp) _sessionAppendByte(tls, bp+24, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect), bp) _sessionAppendBlob(tls, bp+24, (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord, (*TSessionChange)(unsafe.Pointer(pChange)).FnRecord, bp) _sessionAppendBlob(tls, bp+24, *(*uintptr)(unsafe.Pointer(bp + 8)), *(*int32)(unsafe.Pointer(bp + 16)), 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 + 40)) = *(*uintptr)(unsafe.Pointer(bp + 8)) _sessionSkipRecord(tls, bp+40, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol) _sessionAppendByte(tls, bp+24, uint8(SQLITE_INSERT), bp) _sessionAppendByte(tls, bp+24, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect), bp) _sessionAppendRecordMerge(tls, bp+24, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol, *(*uintptr)(unsafe.Pointer(bp + 40)), int32(int64(*(*int32)(unsafe.Pointer(bp + 16)))-(int64(*(*uintptr)(unsafe.Pointer(bp + 40)))-int64(*(*uintptr)(unsafe.Pointer(bp + 8))))), (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord, (*TSessionChange)(unsafe.Pointer(pChange)).FnRecord, bp) } } else { _sessionAppendPartialUpdate(tls, bp+24, pIter, *(*uintptr)(unsafe.Pointer(bp + 8)), *(*int32)(unsafe.Pointer(bp + 16)), (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord, (*TSessionChange)(unsafe.Pointer(pChange)).FnRecord, bp) } default: bDone = int32(1) if int32((*TSessionChange)(unsafe.Pointer(pChange)).Fop) == int32(SQLITE_INSERT) { _sessionAppendByte(tls, bp+24, uint8(SQLITE_DELETE), bp) _sessionAppendByte(tls, bp+24, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect), bp) _sessionAppendRecordMerge(tls, bp+24, (*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FnCol, (*TSessionChange)(unsafe.Pointer(pChange)).FaRecord, (*TSessionChange)(unsafe.Pointer(pChange)).FnRecord, *(*uintptr)(unsafe.Pointer(bp + 8)), *(*int32)(unsafe.Pointer(bp + 16)), bp) } break } } if bDone == 0 { _sessionAppendByte(tls, bp+24, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).Fop), bp) _sessionAppendByte(tls, bp+24, uint8((*Tsqlite3_changeset_iter)(unsafe.Pointer(pIter)).FbIndirect), bp) _sessionAppendBlob(tls, bp+24, *(*uintptr)(unsafe.Pointer(bp + 8)), *(*int32)(unsafe.Pointer(bp + 16)), bp) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && xOutput != 0 && (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).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 + 24))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).FnBuf) (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).FnBuf = 0 } if *(*int32)(unsafe.Pointer(bp)) != 0 { break } } if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).FaBuf) libc.X__builtin___memset_chk(tls, bp+24, 0, uint64(16), libc.X__builtin_object_size(tls, bp+24, 0)) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if xOutput != 0 { if (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).FnBuf > 0 { *(*int32)(unsafe.Pointer(bp)) = (*(*func(*libc.TLS, uintptr, uintptr, int32) int32)(unsafe.Pointer(&struct{ uintptr }{xOutput})))(tls, pOut, (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).FaBuf, (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).FnBuf) } } else { if ppOut != 0 { *(*uintptr)(unsafe.Pointer(ppOut)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).FaBuf *(*int32)(unsafe.Pointer(pnOut)) = (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).FnBuf (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).FaBuf = uintptr(0) } } } Xsqlite3_free(tls, (*(*TSessionBuffer)(unsafe.Pointer(bp + 24))).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(48)) if pNew == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, pNew, 0, uint64(48), libc.X__builtin_object_size(tls, pNew, 0)) } *(*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, (*Tsqlite3_rebaser)(unsafe.Pointer(p)).Fgrp.Frec.FaBuf) 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 { Fapi Tfts5_api Fdb uintptr 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 { Fa uintptr Fn int32 Fi int32 FbFlag Tu8 FbEof Tu8 FiPos Ti64 } type Fts5PoslistReader = TFts5PoslistReader type TFts5PoslistReader1 = struct { Fa uintptr Fn int32 Fi int32 FbFlag Tu8 FbEof Tu8 FiPos Ti64 } type Fts5PoslistReader1 = TFts5PoslistReader1 type TFts5PoslistWriter = struct { FiPrev Ti64 } type Fts5PoslistWriter = TFts5PoslistWriter type TFts5PoslistWriter1 = struct { 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 { FpConfig uintptr FzDataTbl uintptr FnWorkUnit int32 FpHash uintptr FnPendingData int32 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 FiStructVersion Ti64 FpStruct uintptr } type Fts5Index = TFts5Index type TFts5IndexIter = struct { FiRowid Ti64 FpData uintptr FnData int32 FbEof Tu8 } type Fts5IndexIter = TFts5IndexIter type TFts5IndexIter1 = struct { FiRowid Ti64 FpData uintptr FnData int32 FbEof Tu8 } 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 { FpConfig uintptr FpIndex uintptr FbTotalsValid int32 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 { FeType int32 FbEof int32 FbNomatch int32 FiHeight int32 FxNext uintptr FiRowid Ti64 FpNear uintptr FnChild int32 FapChild [1]uintptr } 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 { 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 // ** fts5YYREALLOC Name of the realloc() function to use // ** fts5YYFREE Name of the free() function to use // ** fts5YYDYNSTACK True if stack space should be extended on heap // ** 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 // ** fts5YY_MIN_DSTRCTR Minimum symbol value that has a destructor // ** fts5YY_MAX_DSTRCTR Maximum symbol value that has a destructor // */ // /************* 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 [12]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. */ /* Macro to determine if stack space has the ability to grow using ** heap memory. */ /* Guarantee a minimum number of initial stack slots. */ // 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 Ffts5yystackEnd uintptr Ffts5yystack uintptr Ffts5yystk0 [100]Tfts5yyStackEntry } type fts5yyParser1 = Tfts5yyParser1 type Tfts5yyParser = struct { Ffts5yytos uintptr FpParse uintptr Ffts5yystackEnd uintptr Ffts5yystack uintptr Ffts5yystk0 [100]Tfts5yyStackEntry } type fts5yyParser = Tfts5yyParser /* #include */ /* For builds that do no have a growable stack, fts5yyGrowStack always ** returns an error. */ /* 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)).Ffts5yystack = fts5yypParser + 32 (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yystackEnd = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yystack + uintptr(libc.Int32FromInt32(fts5YYSTACKDEPTH)-libc.Int32FromInt32(1))*24 (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yystack (*(*Tfts5yyStackEntry)(unsafe.Pointer((*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yystack))).Fstateno = uint8(0) (*(*Tfts5yyStackEntry)(unsafe.Pointer((*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yystack))).Fmajor = uint8(0) } // 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(2432)) 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 v2 = pParser v1 = *(*uintptr)(unsafe.Pointer(v2)) *(*uintptr)(unsafe.Pointer(v2)) -= 24 fts5yytos = v1 _fts5yy_destructor(tls, pParser, (*Tfts5yyStackEntry)(unsafe.Pointer(fts5yytos)).Fmajor, fts5yytos+8) } // C documentation // // /* // ** Clear all secondary memory allocations from the parser // */ func _sqlite3Fts5ParserFinalize(tls *libc.TLS, p uintptr) { var fts5yytos, pParser uintptr _, _ = fts5yytos, pParser pParser = p /* In-lined version of calling fts5yy_pop_parser_stack() for each ** element left in the stack */ fts5yytos = (*Tfts5yyParser)(unsafe.Pointer(pParser)).Ffts5yytos for fts5yytos > (*Tfts5yyParser)(unsafe.Pointer(pParser)).Ffts5yystack { if int32((*Tfts5yyStackEntry)(unsafe.Pointer(fts5yytos)).Fmajor) >= int32(fts5YY_MIN_DSTRCTR) { _fts5yy_destructor(tls, pParser, (*Tfts5yyStackEntry)(unsafe.Pointer(fts5yytos)).Fmajor, fts5yytos+8) } fts5yytos -= 24 } } // 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 } for cond := true; cond; cond = int32(1) != 0 { i = int32(_fts5yy_shift_ofst[stateno]) i += int32(iLookAhead) if int32(_fts5yy_lookahead[i]) != int32(iLookAhead) { return _fts5yy_default[stateno] } else { 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 i = int32(_fts5yy_reduce_ofst[stateno]) i += int32(iLookAhead) 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 > (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yystack { _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+35261, 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 += 24 fts5yytos = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos if fts5yytos > (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yystackEnd { if int32(1) != 0 { (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos -= 24 _fts5yyStackOverflow(tls, fts5yypParser) return } fts5yytos = (*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos } if int32(fts5yyNewState) > int32(fts5YY_MAX_SHIFT) { fts5yyNewState = uint8(int32(fts5yyNewState) + (libc.Int32FromInt32(fts5YY_MIN_REDUCE) - libc.Int32FromInt32(fts5YY_MIN_SHIFTREDUCE))) } (*Tfts5yyStackEntry)(unsafe.Pointer(fts5yytos)).Fstateno = fts5yyNewState (*Tfts5yyStackEntry)(unsafe.Pointer(fts5yytos)).Fmajor = fts5yyMajor *(*TFts5Token)(unsafe.Pointer(fts5yytos + 8)) = 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 + 8))) goto _30 _2: ; /* colset ::= MINUS LCP colsetlist RCP */ *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = _sqlite3Fts5ParseColsetInvert(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _30 _3: ; /* colset ::= LCP colsetlist RCP */ *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) goto _30 _4: ; /* colset ::= STRING */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseColset(tls, pParse, uintptr(0), fts5yymsp+8) *(*uintptr)(unsafe.Pointer(fts5yymsp + 8)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _5: ; /* colset ::= MINUS STRING */ *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = _sqlite3Fts5ParseColset(tls, pParse, uintptr(0), fts5yymsp+8) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = _sqlite3Fts5ParseColsetInvert(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8))) goto _30 _6: ; /* colsetlist ::= colsetlist STRING */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseColset(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), fts5yymsp+8) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _7: ; /* colsetlist ::= STRING */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseColset(tls, pParse, uintptr(0), fts5yymsp+8) *(*uintptr)(unsafe.Pointer(fts5yymsp + 8)) = *(*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))*24 + 8)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 8)), uintptr(0)) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*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))*24 + 8)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 8)), uintptr(0)) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*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))*24 + 8)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 8)), uintptr(0)) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _11: ; /* expr ::= colset COLON LP expr RP */ _sqlite3Fts5ParseSetColset(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)), *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8))) *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _12: ; /* expr ::= LP expr RP */ *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) goto _30 _14: ; /* expr ::= exprlist */ _13: ; *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + 8)) *(*uintptr)(unsafe.Pointer(fts5yymsp + 8)) = *(*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))*24 + 8)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 8))) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*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 + 8))) *(*uintptr)(unsafe.Pointer(fts5yymsp + 8)) = *(*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 + 8))) _sqlite3Fts5ParseSetColset(tls, pParse, *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)), *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8))) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _18: ; /* nearset ::= phrase */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNearset(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 8))) *(*uintptr)(unsafe.Pointer(fts5yymsp + 8)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _19: ; /* nearset ::= CARET phrase */ _sqlite3Fts5ParseSetCaret(tls, *(*uintptr)(unsafe.Pointer(fts5yymsp + 8))) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = _sqlite3Fts5ParseNearset(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 8))) goto _30 _20: ; /* nearset ::= STRING LP nearphrases neardist_opt RP */ _sqlite3Fts5ParseNear(tls, pParse, fts5yymsp+uintptr(-libc.Int32FromInt32(4))*24+8) _sqlite3Fts5ParseSetDistance(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)), fts5yymsp+uintptr(-libc.Int32FromInt32(1))*24+8) *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(2))*24 + 8)) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(4))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _21: ; /* nearphrases ::= phrase */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseNearset(tls, pParse, uintptr(0), *(*uintptr)(unsafe.Pointer(fts5yymsp + 8))) *(*uintptr)(unsafe.Pointer(fts5yymsp + 8)) = *(*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))*24 + 8)), *(*uintptr)(unsafe.Pointer(fts5yymsp + 8))) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _23: ; /* neardist_opt ::= */ *(*uintptr)(unsafe.Pointer(fts5yymsp + 1*24 + 8)) = uintptr(0) *(*int32)(unsafe.Pointer(fts5yymsp + 1*24 + 8 + 8)) = 0 goto _30 _24: ; /* neardist_opt ::= COMMA STRING */ *(*TFts5Token)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*TFts5Token)(unsafe.Pointer(fts5yymsp + 8)) goto _30 _25: ; /* phrase ::= phrase PLUS STRING star_opt */ *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) = _sqlite3Fts5ParseTerm(tls, pParse, *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)), fts5yymsp+uintptr(-libc.Int32FromInt32(1))*24+8, *(*int32)(unsafe.Pointer(fts5yymsp + 8))) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(3))*24 + 8)) = *(*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))*24+8, *(*int32)(unsafe.Pointer(fts5yymsp + 8))) *(*uintptr)(unsafe.Pointer(fts5yymsp + uintptr(-libc.Int32FromInt32(1))*24 + 8)) = *(*uintptr)(unsafe.Pointer(&fts5yylhsminor)) goto _30 _27: ; /* star_opt ::= STAR */ *(*int32)(unsafe.Pointer(fts5yymsp + 8)) = int32(1) goto _30 _28: ; /* star_opt ::= */ *(*int32)(unsafe.Pointer(fts5yymsp + 1*24 + 8)) = 0 goto _30 _29: ; goto _30 /********** End reduce actions ************************************************/ _30: ; fts5yygoto = int32(_fts5yyRuleInfoLhs[fts5yyruleno]) fts5yysize = int32(_fts5yyRuleInfoNRhs[fts5yyruleno]) fts5yyact = _fts5yy_find_reduce_action(tls, (*(*Tfts5yyStackEntry)(unsafe.Pointer(fts5yymsp + uintptr(fts5yysize)*24))).Fstateno, uint8(fts5yygoto)) /* There are no SHIFTREDUCE actions on nonterminals because the table ** generator has simplified them to pure REDUCE actions. */ /* It is not possible for a REDUCE to be followed by an error */ fts5yymsp += uintptr(fts5yysize+int32(1)) * 24 (*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+35289, 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 /* 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 fts5yyact = (*Tfts5yyStackEntry)(unsafe.Pointer((*Tfts5yyParser)(unsafe.Pointer(fts5yypParser)).Ffts5yytos)).Fstateno for int32(1) != 0 { /* Exit by "break" */ 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 { if int32(1) != 0 { _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 -= 24 _fts5yy_accept(tls, fts5yypParser) return } else { *(*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" */ /* * Copyright (c) 2002-2017 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1 (the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ // 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.X__builtin___memset_chk(tls, pIter, 0, uint64(40), libc.X__builtin_object_size(tls, pIter, 0)) (*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+24) 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+35320, 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 + 80 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 { _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+40) } } 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(112) defer tls.Free(112) var iCol int32 var zErr uintptr var _ /* ctx at bp+0 */ THighlightContext var _ /* rc at bp+104 */ int32 _, _ = iCol, zErr if nVal != int32(3) { zErr = __ccgo_ts + 35327 Xsqlite3_result_error(tls, pCtx, zErr, -int32(1)) return } iCol = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(apVal))) libc.X__builtin___memset_chk(tls, bp, 0, uint64(104), libc.X__builtin_object_size(tls, bp, 0)) (*(*THighlightContext)(unsafe.Pointer(bp))).FzOpen = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apVal + 1*8))) (*(*THighlightContext)(unsafe.Pointer(bp))).FzClose = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apVal + 2*8))) (*(*THighlightContext)(unsafe.Pointer(bp))).FiRangeEnd = -int32(1) *(*int32)(unsafe.Pointer(bp + 104)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnText})))(tls, pFts, iCol, bp+24, bp+32) if *(*int32)(unsafe.Pointer(bp + 104)) == int32(SQLITE_RANGE) { Xsqlite3_result_text(tls, pCtx, __ccgo_ts+1664, -int32(1), libc.UintptrFromInt32(0)) *(*int32)(unsafe.Pointer(bp + 104)) = SQLITE_OK } else { if (*(*THighlightContext)(unsafe.Pointer(bp))).FzIn != 0 { if *(*int32)(unsafe.Pointer(bp + 104)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 104)) = _fts5CInstIterInit(tls, pApi, pFts, iCol, bp+40) } if *(*int32)(unsafe.Pointer(bp + 104)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 104)) = (*(*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+104, bp, (*(*THighlightContext)(unsafe.Pointer(bp))).FzClose, -int32(1)) } _fts5HighlightAppend(tls, bp+104, 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 + 104)) == 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 + 104)) != SQLITE_OK { Xsqlite3_result_error_code(tls, pCtx, *(*int32)(unsafe.Pointer(bp + 104))) } } /* ** 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(nNew)*uint64(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 int8 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 = 0 i = iStartOff - int32(1) for { if !(i >= 0) { break } c = *(*int8)(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 + 1664 } 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(192) defer tls.Free(192) 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+172 */ int32 var _ /* ic at bp+164 */ int32 var _ /* io at bp+168 */ int32 var _ /* ip at bp+160 */ int32 var _ /* nColSize at bp+112 */ int32 var _ /* nDoc at bp+152 */ int32 var _ /* nDocsize at bp+156 */ int32 var _ /* nInst at bp+108 */ int32 var _ /* nScore at bp+176 */ int32 var _ /* rc at bp+104 */ int32 var _ /* sFinder at bp+120 */ TFts5SFinder _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ = aSeen, i, iBestCol, iBestStart, iCol, ii, jj, nBestScore, nCol, nPhrase, nToken, zEllips, zErr, v1, v5 *(*int32)(unsafe.Pointer(bp + 104)) = SQLITE_OK /* 5th argument to snippet() */ *(*int32)(unsafe.Pointer(bp + 108)) = 0 /* Column containing best snippet */ iBestStart = 0 /* First token of best snippet */ nBestScore = 0 /* Score of best snippet */ *(*int32)(unsafe.Pointer(bp + 112)) = 0 if nVal != int32(5) { zErr = __ccgo_ts + 35377 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.X__builtin___memset_chk(tls, bp, 0, uint64(104), libc.X__builtin_object_size(tls, bp, 0)) iCol = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(apVal))) (*(*THighlightContext)(unsafe.Pointer(bp))).FzOpen = _fts5ValueToText(tls, *(*uintptr)(unsafe.Pointer(apVal + 1*8))) (*(*THighlightContext)(unsafe.Pointer(bp))).FzClose = _fts5ValueToText(tls, *(*uintptr)(unsafe.Pointer(apVal + 2*8))) (*(*THighlightContext)(unsafe.Pointer(bp))).FiRangeEnd = -int32(1) zEllips = _fts5ValueToText(tls, *(*uintptr)(unsafe.Pointer(apVal + 3*8))) nToken = Xsqlite3_value_int(tls, *(*uintptr)(unsafe.Pointer(apVal + 4*8))) 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 + 104)) = int32(SQLITE_NOMEM) } if *(*int32)(unsafe.Pointer(bp + 104)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 104)) = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInstCount})))(tls, pFts, bp+108) } libc.X__builtin___memset_chk(tls, bp+120, 0, uint64(32), libc.X__builtin_object_size(tls, bp+120, 0)) i = 0 for { if !(i < nCol) { break } if iCol < 0 || iCol == i { (*(*TFts5SFinder)(unsafe.Pointer(bp + 120))).FiPos = 0 (*(*TFts5SFinder)(unsafe.Pointer(bp + 120))).FnFirst = 0 *(*int32)(unsafe.Pointer(bp + 104)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnText})))(tls, pFts, i, bp+120+24, bp+152) if *(*int32)(unsafe.Pointer(bp + 104)) != SQLITE_OK { break } *(*int32)(unsafe.Pointer(bp + 104)) = (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxTokenize})))(tls, pFts, (*(*TFts5SFinder)(unsafe.Pointer(bp + 120))).FzDoc, *(*int32)(unsafe.Pointer(bp + 152)), bp+120, __ccgo_fp(_fts5SentenceFinderCb)) if *(*int32)(unsafe.Pointer(bp + 104)) != SQLITE_OK { break } *(*int32)(unsafe.Pointer(bp + 104)) = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnSize})))(tls, pFts, i, bp+156) if *(*int32)(unsafe.Pointer(bp + 104)) != SQLITE_OK { break } ii = 0 for { if !(*(*int32)(unsafe.Pointer(bp + 104)) == SQLITE_OK && ii < *(*int32)(unsafe.Pointer(bp + 108))) { break } *(*int32)(unsafe.Pointer(bp + 104)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInst})))(tls, pFts, ii, bp+160, bp+164, bp+168) if *(*int32)(unsafe.Pointer(bp + 164)) != i { goto _3 } if *(*int32)(unsafe.Pointer(bp + 168)) > *(*int32)(unsafe.Pointer(bp + 156)) { *(*int32)(unsafe.Pointer(bp + 104)) = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)< nBestScore { nBestScore = *(*int32)(unsafe.Pointer(bp + 176)) iBestCol = i iBestStart = *(*int32)(unsafe.Pointer(bp + 172)) *(*int32)(unsafe.Pointer(bp + 112)) = *(*int32)(unsafe.Pointer(bp + 156)) } if *(*int32)(unsafe.Pointer(bp + 104)) == SQLITE_OK && (*(*TFts5SFinder)(unsafe.Pointer(bp + 120))).FnFirst != 0 && *(*int32)(unsafe.Pointer(bp + 156)) > nToken { jj = 0 for { if !(jj < (*(*TFts5SFinder)(unsafe.Pointer(bp + 120))).FnFirst-int32(1)) { break } if *(*int32)(unsafe.Pointer((*(*TFts5SFinder)(unsafe.Pointer(bp + 120))).FaFirst + uintptr(jj+int32(1))*4)) > *(*int32)(unsafe.Pointer(bp + 168)) { break } goto _4 _4: ; jj++ } if *(*int32)(unsafe.Pointer((*(*TFts5SFinder)(unsafe.Pointer(bp + 120))).FaFirst + uintptr(jj)*4)) < *(*int32)(unsafe.Pointer(bp + 168)) { libc.X__builtin___memset_chk(tls, aSeen, 0, uint64(nPhrase), libc.X__builtin_object_size(tls, aSeen, 0)) *(*int32)(unsafe.Pointer(bp + 104)) = _fts5SnippetScore(tls, pApi, pFts, *(*int32)(unsafe.Pointer(bp + 156)), aSeen, i, *(*int32)(unsafe.Pointer((*(*TFts5SFinder)(unsafe.Pointer(bp + 120))).FaFirst + uintptr(jj)*4)), nToken, bp+176, uintptr(0)) if *(*int32)(unsafe.Pointer((*(*TFts5SFinder)(unsafe.Pointer(bp + 120))).FaFirst + uintptr(jj)*4)) == 0 { v5 = int32(120) } else { v5 = int32(100) } *(*int32)(unsafe.Pointer(bp + 176)) += v5 if *(*int32)(unsafe.Pointer(bp + 104)) == SQLITE_OK && *(*int32)(unsafe.Pointer(bp + 176)) > nBestScore { nBestScore = *(*int32)(unsafe.Pointer(bp + 176)) iBestCol = i iBestStart = *(*int32)(unsafe.Pointer((*(*TFts5SFinder)(unsafe.Pointer(bp + 120))).FaFirst + uintptr(jj)*4)) *(*int32)(unsafe.Pointer(bp + 112)) = *(*int32)(unsafe.Pointer(bp + 156)) } } } goto _3 _3: ; ii++ } } goto _2 _2: ; i++ } if *(*int32)(unsafe.Pointer(bp + 104)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 104)) = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnText})))(tls, pFts, iBestCol, bp+24, bp+32) } if *(*int32)(unsafe.Pointer(bp + 104)) == SQLITE_OK && *(*int32)(unsafe.Pointer(bp + 112)) == 0 { *(*int32)(unsafe.Pointer(bp + 104)) = (*(*func(*libc.TLS, uintptr, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxColumnSize})))(tls, pFts, iBestCol, bp+112) } if (*(*THighlightContext)(unsafe.Pointer(bp))).FzIn != 0 { if *(*int32)(unsafe.Pointer(bp + 104)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 104)) = _fts5CInstIterInit(tls, pApi, pFts, iBestCol, bp+40) } (*(*THighlightContext)(unsafe.Pointer(bp))).FiRangeStart = iBestStart (*(*THighlightContext)(unsafe.Pointer(bp))).FiRangeEnd = iBestStart + nToken - int32(1) if iBestStart > 0 { _fts5HighlightAppend(tls, bp+104, 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 + 104)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 104)) = _fts5CInstIterNext(tls, bp+40) } if *(*int32)(unsafe.Pointer(bp + 104)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp + 104)) = (*(*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+104, bp, (*(*THighlightContext)(unsafe.Pointer(bp))).FzClose, -int32(1)) } if (*(*THighlightContext)(unsafe.Pointer(bp))).FiRangeEnd >= *(*int32)(unsafe.Pointer(bp + 112))-int32(1) { _fts5HighlightAppend(tls, bp+104, 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+104, bp, zEllips, -int32(1)) } } if *(*int32)(unsafe.Pointer(bp + 104)) == 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 + 104))) } Xsqlite3_free(tls, (*(*THighlightContext)(unsafe.Pointer(bp))).FzOut) Xsqlite3_free(tls, aSeen) Xsqlite3_free(tls, (*(*TFts5SFinder)(unsafe.Pointer(bp + 120))).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 { FnPhrase int32 Favgdl float64 FaIDF uintptr FaFreq uintptr } type Fts5Bm25Data = TFts5Bm25Data type TFts5Bm25Data1 = struct { FnPhrase int32 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(uint64(32) + uint64(nPhrase*int32(2))*uint64(8)) p = Xsqlite3_malloc64(tls, uint64(nByte)) if p == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, p, 0, uint64(nByte), libc.X__builtin_object_size(tls, p, 0)) (*TFts5Bm25Data)(unsafe.Pointer(p)).FnPhrase = nPhrase (*TFts5Bm25Data)(unsafe.Pointer(p)).FaIDF = p + 1*32 (*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) } 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+16 */ int32 var _ /* io at bp+20 */ int32 var _ /* ip at bp+12 */ int32 var _ /* nInst at bp+8 */ int32 var _ /* nTok at bp+24 */ 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 + 8)) = 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.X__builtin___memset_chk(tls, aFreq, 0, uint64(8)*uint64((*TFts5Bm25Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FnPhrase), libc.X__builtin_object_size(tls, aFreq, 0)) rc = (*(*func(*libc.TLS, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInstCount})))(tls, pFts, bp+8) } i = 0 for { if !(rc == SQLITE_OK && i < *(*int32)(unsafe.Pointer(bp + 8))) { break } rc = (*(*func(*libc.TLS, uintptr, int32, uintptr, uintptr, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*TFts5ExtensionApi)(unsafe.Pointer(pApi)).FxInst})))(tls, pFts, i, bp+12, bp+16, bp+20) if rc == SQLITE_OK { if nVal > *(*int32)(unsafe.Pointer(bp + 16)) { v2 = Xsqlite3_value_double(tls, *(*uintptr)(unsafe.Pointer(apVal + uintptr(*(*int32)(unsafe.Pointer(bp + 16)))*8))) } else { v2 = float64(1) } w = v2 *(*float64)(unsafe.Pointer(aFreq + uintptr(*(*int32)(unsafe.Pointer(bp + 12)))*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+24) D = float64(*(*int32)(unsafe.Pointer(bp + 24))) } /* 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 + 35425, FxFunc: __ccgo_fp(_fts5SnippetFunction), }, 1: { FzFunc: __ccgo_ts + 35433, FxFunc: __ccgo_fp(_fts5HighlightFunction), }, 2: { FzFunc: __ccgo_ts + 35443, FxFunc: __ccgo_fp(_fts5Bm25Function), }, } rc = SQLITE_OK /* To iterate through builtin functions */ i = 0 for { if !(rc == SQLITE_OK && i < int32(libc.Uint64FromInt64(96)/libc.Uint64FromInt64(32))) { 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 + 8)) += _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.X__builtin___memset_chk(tls, pBuf, 0, uint64(16), libc.X__builtin_object_size(tls, pBuf, 0)) } // 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)) if i >= n { /* EOF */ *(*Ti64)(unsafe.Pointer(piOff)) = int64(-int32(1)) return int32(1) } else { iOff = *(*Ti64)(unsafe.Pointer(piOff)) 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) 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 + 8 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 + 8)) += _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 + 8)) += _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.X__builtin___memset_chk(tls, pRet, 0, uint64(nByte), libc.X__builtin_object_size(tls, pRet, 0)) } } 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.X__builtin___memcpy_chk(tls, zRet, pIn, uint64(nIn), libc.X__builtin_object_size(tls, zRet, 0)) *(*int8)(unsafe.Pointer(zRet + uintptr(nIn))) = int8('\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 int8) (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(4096)) 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(*(*int8)(unsafe.Pointer(p))) { case int32('n'): fallthrough case int32('N'): if Xsqlite3_strnicmp(tls, __ccgo_ts+1650, p, int32(4)) == 0 { p = p + 4 } else { p = uintptr(0) } case int32('x'): fallthrough case int32('X'): p++ if int32(*(*int8)(unsafe.Pointer(p))) == int32('\'') { p++ for int32(*(*int8)(unsafe.Pointer(p))) >= int32('a') && int32(*(*int8)(unsafe.Pointer(p))) <= int32('f') || int32(*(*int8)(unsafe.Pointer(p))) >= int32('A') && int32(*(*int8)(unsafe.Pointer(p))) <= int32('F') || int32(*(*int8)(unsafe.Pointer(p))) >= int32('0') && int32(*(*int8)(unsafe.Pointer(p))) <= int32('9') { p++ } if int32(*(*int8)(unsafe.Pointer(p))) == int32('\'') && 0 == (int64(p)-int64(pIn))%int64(2) { p++ } else { p = uintptr(0) } } else { p = uintptr(0) } case int32('\''): p++ for p != 0 { if int32(*(*int8)(unsafe.Pointer(p))) == int32('\'') { p++ if int32(*(*int8)(unsafe.Pointer(p))) != int32('\'') { break } } p++ if int32(*(*int8)(unsafe.Pointer(p))) == 0 { p = uintptr(0) } } default: /* maybe a number */ if int32(*(*int8)(unsafe.Pointer(p))) == int32('+') || int32(*(*int8)(unsafe.Pointer(p))) == int32('-') { p++ } for _fts5_isdigit(tls, *(*int8)(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(*(*int8)(unsafe.Pointer(p))) == int32('.') && _fts5_isdigit(tls, *(*int8)(unsafe.Pointer(p + 1))) != 0 { p += uintptr(2) for _fts5_isdigit(tls, *(*int8)(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 int8 _, _, _, _, _, _ = iIn, iOut, q, v1, v2, v3 iIn = int32(1) iOut = 0 q = *(*int8)(unsafe.Pointer(z)) /* Set stack variable q to the close-quote character */ if int32(q) == int32('[') { q = int8(']') } for *(*int8)(unsafe.Pointer(z + uintptr(iIn))) != 0 { if int32(*(*int8)(unsafe.Pointer(z + uintptr(iIn)))) == int32(q) { if int32(*(*int8)(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++ *(*int8)(unsafe.Pointer(z + uintptr(v1))) = q } } else { v2 = iOut iOut++ v3 = iIn iIn++ *(*int8)(unsafe.Pointer(z + uintptr(v2))) = *(*int8)(unsafe.Pointer(z + uintptr(v3))) } } *(*int8)(unsafe.Pointer(z + uintptr(iOut))) = int8('\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 int8 _ = quote /* Quote character (if any ) */ quote = *(*int8)(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)*16))).FzName != 0) { break } if Xsqlite3_strnicmp(tls, (*(*TFts5Enum)(unsafe.Pointer(aEnum + uintptr(i)*16))).FzName, zEnum, nEnum) == 0 { if iVal >= 0 { return int32(SQLITE_ERROR) } iVal = (*(*TFts5Enum)(unsafe.Pointer(aEnum + uintptr(i)*16))).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(96) defer tls.Free(96) var azArg, p, p1, p2, pDel, pSpace uintptr var bFirst, nByte, nCmd, nPre, v2 int32 var nArg Tsqlite3_int64 var _ /* aDetail at bp+8 */ [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+35448, zCmd, nCmd) == 0 { nByte = int32(libc.Uint64FromInt64(4) * libc.Uint64FromInt32(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(*(*int8)(unsafe.Pointer(p))) == int32(' ') { p++ } if bFirst == 0 && int32(*(*int8)(unsafe.Pointer(p))) == int32(',') { p++ for int32(*(*int8)(unsafe.Pointer(p))) == int32(' ') { p++ } } else { if int32(*(*int8)(unsafe.Pointer(p))) == int32('\000') { break } } if int32(*(*int8)(unsafe.Pointer(p))) < int32('0') || int32(*(*int8)(unsafe.Pointer(p))) > int32('9') { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35455, 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+35486, libc.VaList(bp+80, int32(FTS5_MAX_PREFIX_INDEXES))) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) break } for int32(*(*int8)(unsafe.Pointer(p))) >= int32('0') && int32(*(*int8)(unsafe.Pointer(p))) <= int32('9') && nPre < int32(1000) { nPre = nPre*int32(10) + (int32(*(*int8)(unsafe.Pointer(p))) - int32('0')) p++ } if nPre <= 0 || nPre >= int32(1000) { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35519, 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 } return *(*int32)(unsafe.Pointer(bp)) } if Xsqlite3_strnicmp(tls, __ccgo_ts+35556, zCmd, nCmd) == 0 { p1 = zArg nArg = int64(libc.Xstrlen(tls, zArg) + uint64(1)) azArg = _sqlite3Fts5MallocZero(tls, bp, int64(uint64(8)*uint64(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+35565, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { nArg = 0 for { if !(p1 != 0 && *(*int8)(unsafe.Pointer(p1)) != 0) { break } p2 = _fts5ConfigSkipWhitespace(tls, p1) if int32(*(*int8)(unsafe.Pointer(p2))) == int32('\'') { p1 = _fts5ConfigSkipLiteral(tls, p2) } else { p1 = _fts5ConfigSkipBareword(tls, p2) } if p1 != 0 { libc.X__builtin___memcpy_chk(tls, pSpace, p2, uint64(int64(p1)-int64(p2)), libc.X__builtin_object_size(tls, pSpace, 0)) *(*uintptr)(unsafe.Pointer(azArg + uintptr(nArg)*8)) = pSpace _sqlite3Fts5Dequote(tls, pSpace) pSpace += uintptr(int64(p1) - int64(p2) + int64(1)) p1 = _fts5ConfigSkipWhitespace(tls, p1) } goto _1 _1: ; nArg++ } if p1 == uintptr(0) { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35598, 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+35632, zCmd, nCmd) == 0 { if (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent != FTS5_CONTENT_NORMAL { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35640, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { if *(*int8)(unsafe.Pointer(zArg)) != 0 { (*TFts5Config)(unsafe.Pointer(pConfig)).FeContent = int32(FTS5_CONTENT_EXTERNAL) (*TFts5Config)(unsafe.Pointer(pConfig)).FzContent = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+35672, libc.VaList(bp+80, (*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+35678, zCmd, nCmd) == 0 { if int32(*(*int8)(unsafe.Pointer(zArg))) != int32('0') && int32(*(*int8)(unsafe.Pointer(zArg))) != int32('1') || int32(*(*int8)(unsafe.Pointer(zArg + 1))) != int32('\000') { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35697, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { (*TFts5Config)(unsafe.Pointer(pConfig)).FbContentlessDelete = libc.BoolInt32(int32(*(*int8)(unsafe.Pointer(zArg))) == int32('1')) } return *(*int32)(unsafe.Pointer(bp)) } if Xsqlite3_strnicmp(tls, __ccgo_ts+35740, zCmd, nCmd) == 0 { if (*TFts5Config)(unsafe.Pointer(pConfig)).FzContentRowid != 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35754, 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+35792, zCmd, nCmd) == 0 { if int32(*(*int8)(unsafe.Pointer(zArg))) != int32('0') && int32(*(*int8)(unsafe.Pointer(zArg))) != int32('1') || int32(*(*int8)(unsafe.Pointer(zArg + 1))) != int32('\000') { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35803, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { (*TFts5Config)(unsafe.Pointer(pConfig)).FbColumnsize = libc.BoolInt32(int32(*(*int8)(unsafe.Pointer(zArg))) == int32('1')) } return *(*int32)(unsafe.Pointer(bp)) } if Xsqlite3_strnicmp(tls, __ccgo_ts+5911, zCmd, nCmd) == 0 { *(*[4]TFts5Enum)(unsafe.Pointer(bp + 8)) = [4]TFts5Enum{ 0: { FzName: __ccgo_ts + 8691, FeVal: int32(FTS5_DETAIL_NONE), }, 1: { FzName: __ccgo_ts + 18201, }, 2: { FzName: __ccgo_ts + 35838, FeVal: int32(FTS5_DETAIL_COLUMNS), }, 3: {}, } v2 = _fts5ConfigSetEnum(tls, bp+8, zArg, pConfig+96) *(*int32)(unsafe.Pointer(bp)) = v2 if v2 != 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35846, 0) } return *(*int32)(unsafe.Pointer(bp)) } if Xsqlite3_strnicmp(tls, __ccgo_ts+35877, zCmd, nCmd) == 0 { if int32(*(*int8)(unsafe.Pointer(zArg))) != int32('0') && int32(*(*int8)(unsafe.Pointer(zArg))) != int32('1') || int32(*(*int8)(unsafe.Pointer(zArg + 1))) != int32('\000') { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35887, 0) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { (*TFts5Config)(unsafe.Pointer(pConfig)).FbTokendata = libc.BoolInt32(int32(*(*int8)(unsafe.Pointer(zArg))) == int32('1')) } return *(*int32)(unsafe.Pointer(bp)) } *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35921, libc.VaList(bp+80, 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) { 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))) *(*int32)(unsafe.Pointer(pbQuoted)) = 0 *(*uintptr)(unsafe.Pointer(pzOut)) = uintptr(0) if zOut == uintptr(0) { *(*int32)(unsafe.Pointer(pRc)) = int32(SQLITE_NOMEM) } else { libc.X__builtin___memcpy_chk(tls, zOut, zIn, uint64(nIn+libc.Int64FromInt32(1)), libc.X__builtin_object_size(tls, zOut, 0)) if _fts5_isopenquote(tls, *(*int8)(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 { *(*int8)(unsafe.Pointer(zOut + uintptr(int64(zRet)-int64(zIn)))) = int8('\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+35949) || 0 == Xsqlite3_stricmp(tls, zCol, __ccgo_ts+17132) { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+35954, libc.VaList(bp+8, zCol)) rc = int32(SQLITE_ERROR) } else { if zArg != 0 { if 0 == Xsqlite3_stricmp(tls, zArg, __ccgo_ts+35984) { *(*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+35994, libc.VaList(bp+8, zArg)) rc = int32(SQLITE_ERROR) } } } v2 = p + 24 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*uintptr)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(p)).FazCol + uintptr(v1)*8)) = zCol return rc } // C documentation // // /* // ** Populate the Fts5Config.zContentExprlist string. // */ func _fts5ConfigMakeExprlist(tls *libc.TLS, p uintptr) (r int32) { bp := tls.Alloc(48) defer tls.Free(48) var i int32 var _ /* buf at bp+8 */ TFts5Buffer var _ /* rc at bp+0 */ int32 _ = i *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK *(*TFts5Buffer)(unsafe.Pointer(bp + 8)) = TFts5Buffer{} _sqlite3Fts5BufferAppendPrintf(tls, bp, bp+8, __ccgo_ts+36025, libc.VaList(bp+32, (*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+8, __ccgo_ts+36030, libc.VaList(bp+32, *(*uintptr)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(p)).FazCol + uintptr(i)*8)))) } else { _sqlite3Fts5BufferAppendPrintf(tls, bp, bp+8, __ccgo_ts+36037, libc.VaList(bp+32, i)) } goto _1 _1: ; i++ } } (*TFts5Config)(unsafe.Pointer(p)).FzContentExprlist = (*(*TFts5Buffer)(unsafe.Pointer(bp + 8))).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+28 */ int32 var _ /* bMustBeCol at bp+24 */ int32 var _ /* rc at bp+0 */ int32 var _ /* zOne at bp+8 */ uintptr var _ /* zTwo at bp+16 */ uintptr _, _, _, _, _, _, _, _, _, _, _ = bOption, i, nByte, pRet, z, zOrig, zTail, v1, v2, v4, v5 *(*int32)(unsafe.Pointer(bp)) = SQLITE_OK v1 = Xsqlite3_malloc(tls, int32(200)) pRet = v1 *(*uintptr)(unsafe.Pointer(ppOut)) = v1 if pRet == uintptr(0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pRet, 0, uint64(200), libc.X__builtin_object_size(tls, pRet, 0)) (*TFts5Config)(unsafe.Pointer(pRet)).Fdb = db (*TFts5Config)(unsafe.Pointer(pRet)).FiCookie = -int32(1) nByte = int64(uint64(nArg) * (libc.Uint64FromInt64(8) + libc.Uint64FromInt64(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)*8 } else { v2 = uintptr(0) } (*TFts5Config)(unsafe.Pointer(pRet)).FabUnindexed = v2 (*TFts5Config)(unsafe.Pointer(pRet)).FzDb = _sqlite3Fts5Strndup(tls, bp, *(*uintptr)(unsafe.Pointer(azArg + 1*8)), -int32(1)) (*TFts5Config)(unsafe.Pointer(pRet)).FzName = _sqlite3Fts5Strndup(tls, bp, *(*uintptr)(unsafe.Pointer(azArg + 2*8)), -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+35949) == 0 { *(*uintptr)(unsafe.Pointer(pzErr)) = Xsqlite3_mprintf(tls, __ccgo_ts+36045, libc.VaList(bp+40, (*TFts5Config)(unsafe.Pointer(pRet)).FzName)) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } i = int32(3) for { if !(*(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && i < nArg) { break } zOrig = *(*uintptr)(unsafe.Pointer(azArg + uintptr(i)*8)) *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 16)) = uintptr(0) bOption = 0 *(*int32)(unsafe.Pointer(bp + 24)) = 0 z = _fts5ConfigGobbleWord(tls, bp, zOrig, bp+8, bp+24) z = _fts5ConfigSkipWhitespace(tls, z) if z != 0 && int32(*(*int8)(unsafe.Pointer(z))) == int32('=') { bOption = int32(1) z++ if *(*int32)(unsafe.Pointer(bp + 24)) != 0 { z = uintptr(0) } } z = _fts5ConfigSkipWhitespace(tls, z) if z != 0 && *(*int8)(unsafe.Pointer(z)) != 0 { z = _fts5ConfigGobbleWord(tls, bp, z, bp+16, bp+28) if z != 0 && *(*int8)(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+36074, libc.VaList(bp+40, zOrig)) *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } else { if bOption != 0 { if *(*uintptr)(unsafe.Pointer(bp + 8)) != 0 { v4 = *(*uintptr)(unsafe.Pointer(bp + 8)) } else { v4 = __ccgo_ts + 1664 } if *(*uintptr)(unsafe.Pointer(bp + 16)) != 0 { v5 = *(*uintptr)(unsafe.Pointer(bp + 16)) } else { v5 = __ccgo_ts + 1664 } *(*int32)(unsafe.Pointer(bp)) = _fts5ConfigParseSpecial(tls, pGlobal, pRet, v4, v5, pzErr) } else { *(*int32)(unsafe.Pointer(bp)) = _fts5ConfigParseColumn(tls, pRet, *(*uintptr)(unsafe.Pointer(bp + 8)), *(*uintptr)(unsafe.Pointer(bp + 16)), pzErr) *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) } } } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) 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+36094, 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+36144, 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) if (*TFts5Config)(unsafe.Pointer(pRet)).FeContent == FTS5_CONTENT_NORMAL { zTail = __ccgo_ts + 35632 } else { if (*TFts5Config)(unsafe.Pointer(pRet)).FbColumnsize != 0 { zTail = __ccgo_ts + 36199 } } if zTail != 0 { (*TFts5Config)(unsafe.Pointer(pRet)).FzContent = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+36207, libc.VaList(bp+40, (*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+17132, -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)*8))) 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+36218, 0) i = 0 for { if !(zSql != 0 && i < (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol) { break } if i == 0 { v2 = __ccgo_ts + 1664 } else { v2 = __ccgo_ts + 15431 } zSep = v2 zSql = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+36234, libc.VaList(bp+16, zSql, zSep, *(*uintptr)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FazCol + uintptr(i)*8)))) goto _1 _1: ; i++ } zSql = _sqlite3Fts5Mprintf(tls, bp, __ccgo_ts+36241, libc.VaList(bp+16, zSql, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName, __ccgo_ts+35949)) 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(*(*int8)(unsafe.Pointer(p))) == int32(')') { break } if int32(*(*int8)(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(uintptr(1)+p)-int64(pRank)) if zRank != 0 { libc.X__builtin___memcpy_chk(tls, zRank, pRank, uint64(int64(p)-int64(pRank)), libc.X__builtin_object_size(tls, zRank, 0)) } } else { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_ERROR) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { p = _fts5ConfigSkipWhitespace(tls, p) if int32(*(*int8)(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(*(*int8)(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(uintptr(1)+p)-int64(pArgs)) if zRankArgs != 0 { libc.X__builtin___memcpy_chk(tls, zRankArgs, pArgs, uint64(int64(p)-int64(pArgs)), libc.X__builtin_object_size(tls, zRankArgs, 0)) } } } } } if *(*int32)(unsafe.Pointer(bp)) != SQLITE_OK { Xsqlite3_free(tls, zRank) } 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+8 */ uintptr _, _, _, _, _, _, _, _, _, _ = bVal, nAutomerge, nCrisisMerge, nHashSize, nUsermerge, nVal, pgsz, rc, zIn, v1 rc = SQLITE_OK if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+36267) { 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+36272) { 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+36281) { 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+36291) { 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+36301) { 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+36313) { 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+35949) { zIn = Xsqlite3_value_text(tls, pVal) rc = _sqlite3Fts5ConfigParseRank(tls, zIn, bp, bp+8) 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 + 8)) } else { if rc == int32(SQLITE_ERROR) { rc = SQLITE_OK *(*int32)(unsafe.Pointer(pbBadkey)) = int32(1) } } } else { if 0 == Xsqlite3_stricmp(tls, zKey, __ccgo_ts+36325) { 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+12 */ int32 var _ /* p at bp+0 */ uintptr var _ /* rc at bp+8 */ int32 _, _, _, _, _ = iVersion, pVal, zK, zSelect, zSql zSelect = __ccgo_ts + 36339 *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 8)) = 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+8, zSelect, libc.VaList(bp+24, (*TFts5Config)(unsafe.Pointer(pConfig)).FzDb, (*TFts5Config)(unsafe.Pointer(pConfig)).FzName)) if zSql != 0 { *(*int32)(unsafe.Pointer(bp + 8)) = Xsqlite3_prepare_v2(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb, zSql, -int32(1), bp, uintptr(0)) Xsqlite3_free(tls, zSql) } if *(*int32)(unsafe.Pointer(bp + 8)) == 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+36371) { iVersion = Xsqlite3_value_int(tls, pVal) } else { *(*int32)(unsafe.Pointer(bp + 12)) = 0 _sqlite3Fts5ConfigSetValue(tls, pConfig, zK, pVal, bp+12) } } *(*int32)(unsafe.Pointer(bp + 8)) = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp))) } if *(*int32)(unsafe.Pointer(bp + 8)) == SQLITE_OK && iVersion != int32(FTS5_CURRENT_VERSION) && iVersion != int32(FTS5_CURRENT_VERSION_SECUREDELETE) { *(*int32)(unsafe.Pointer(bp + 8)) = int32(SQLITE_ERROR) if (*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg != 0 { *(*uintptr)(unsafe.Pointer((*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+36379, 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 + 8)) == SQLITE_OK { (*TFts5Config)(unsafe.Pointer(pConfig)).FiCookie = iCookie } return *(*int32)(unsafe.Pointer(bp + 8)) } /* ** 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 { FeType int32 FbEof int32 FbNomatch int32 FiHeight int32 FxNext uintptr FiRowid Ti64 FpNear uintptr FnChild int32 FapChild [1]uintptr } 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 { (*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 int8) (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, *(*int8)(unsafe.Pointer(z))) != 0 { z++ } (*TFts5Token)(unsafe.Pointer(pToken)).Fp = z (*TFts5Token)(unsafe.Pointer(pToken)).Fn = int32(1) switch int32(*(*int8)(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(*(*int8)(unsafe.Pointer(z2))) == int32('"') { z2++ if int32(*(*int8)(unsafe.Pointer(z2))) != int32('"') { break } } if int32(*(*int8)(unsafe.Pointer(z2))) == int32('\000') { _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+36450, 0) return FTS5_EOF } goto _1 _1: ; z2++ } (*TFts5Token)(unsafe.Pointer(pToken)).Fn = int32(int64(z2) - int64(z)) default: if _sqlite3Fts5IsBareword(tls, *(*int8)(unsafe.Pointer(z))) == 0 { _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+36470, libc.VaList(bp+8, z)) return FTS5_EOF } tok = int32(FTS5_STRING) z21 = z + 1 for { if !(_sqlite3Fts5IsBareword(tls, *(*int8)(unsafe.Pointer(z21))) != 0) { break } goto _2 _2: ; z21++ } (*TFts5Token)(unsafe.Pointer(pToken)).Fn = int32(int64(z21) - int64(z)) if (*TFts5Token)(unsafe.Pointer(pToken)).Fn == int32(2) && libc.Xmemcmp(tls, (*TFts5Token)(unsafe.Pointer(pToken)).Fp, __ccgo_ts+36501, uint64(2)) == 0 { tok = int32(FTS5_OR) } if (*TFts5Token)(unsafe.Pointer(pToken)).Fn == int32(3) && libc.Xmemcmp(tls, (*TFts5Token)(unsafe.Pointer(pToken)).Fp, __ccgo_ts+36504, uint64(3)) == 0 { tok = int32(FTS5_NOT) } if (*TFts5Token)(unsafe.Pointer(pToken)).Fn == int32(3) && libc.Xmemcmp(tls, (*TFts5Token)(unsafe.Pointer(pToken)).Fp, __ccgo_ts+31247, uint64(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(80) defer tls.Free(80) var n, nByte, t int32 var pColset, pEngine, pNew, v1 uintptr var _ /* sParse at bp+0 */ TFts5Parse var _ /* token at bp+48 */ TFts5Token var _ /* z at bp+64 */ uintptr _, _, _, _, _, _, _ = n, nByte, pColset, pEngine, pNew, t, v1 *(*uintptr)(unsafe.Pointer(bp + 64)) = zExpr *(*uintptr)(unsafe.Pointer(ppNew)) = uintptr(0) *(*uintptr)(unsafe.Pointer(pzErr)) = uintptr(0) libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) (*(*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+64, bp+48) _sqlite3Fts5Parser(tls, pEngine, t, *(*TFts5Token)(unsafe.Pointer(bp + 48)), 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+16, 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) } } if (*(*TFts5Parse)(unsafe.Pointer(bp))).Frc == SQLITE_OK { v1 = Xsqlite3_malloc(tls, int32(40)) 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(56) (*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot = _sqlite3Fts5MallocZero(tls, bp+16, 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(*(*int8)(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]int8 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] = int8('_') aSpec[int32(1)] = int8('%') aSpec[int32(2)] = 0 } else { aSpec[0] = int8('*') aSpec[int32(1)] = int8('?') aSpec[int32(2)] = int8('[') } for int64(i) <= nText { if int64(i) == nText || int32(*(*int8)(unsafe.Pointer(zText + uintptr(i)))) == int32(aSpec[0]) || int32(*(*int8)(unsafe.Pointer(zText + uintptr(i)))) == int32(aSpec[int32(1)]) || int32(*(*int8)(unsafe.Pointer(zText + uintptr(i)))) == int32(aSpec[int32(2)]) { if _fts5ExprCountChar(tls, zText+uintptr(iFirst), i-iFirst) >= int32(3) { v1 = iOut iOut++ *(*int8)(unsafe.Pointer(zExpr + uintptr(v1))) = int8('"') jj = iFirst for { if !(jj < i) { break } v3 = iOut iOut++ *(*int8)(unsafe.Pointer(zExpr + uintptr(v3))) = *(*int8)(unsafe.Pointer(zText + uintptr(jj))) if int32(*(*int8)(unsafe.Pointer(zText + uintptr(jj)))) == int32('"') { v4 = iOut iOut++ *(*int8)(unsafe.Pointer(zExpr + uintptr(v4))) = int8('"') } goto _2 _2: ; jj++ } v5 = iOut iOut++ *(*int8)(unsafe.Pointer(zExpr + uintptr(v5))) = int8('"') v6 = iOut iOut++ *(*int8)(unsafe.Pointer(zExpr + uintptr(v6))) = int8(' ') } if int32(*(*int8)(unsafe.Pointer(zText + uintptr(i)))) == int32(aSpec[int32(2)]) { i += int32(2) if int32(*(*int8)(unsafe.Pointer(zText + uintptr(i-int32(1))))) == int32('^') { i++ } for int64(i) < nText && int32(*(*int8)(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 } } *(*int8)(unsafe.Pointer(zExpr + uintptr(iOut))) = int8('\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 + 48 + uintptr(i)*8))) 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(48) defer tls.Free(48) var ap, p1 uintptr var i, nPhrase int32 var _ /* sParse at bp+0 */ TFts5Parse _, _, _, _ = ap, i, nPhrase, p1 libc.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) 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(uint64(nPhrase)*uint64(8))) if ap == uintptr(0) { (*(*TFts5Parse)(unsafe.Pointer(bp))).Frc = int32(SQLITE_NOMEM) } else { libc.X__builtin___memmove_chk(tls, ap+uintptr((*TFts5Expr)(unsafe.Pointer(p2)).FnPhrase)*8, ap, uint64((*TFts5Expr)(unsafe.Pointer(p1)).FnPhrase)*uint64(8), libc.X__builtin_object_size(tls, ap+uintptr((*TFts5Expr)(unsafe.Pointer(p2)).FnPhrase)*8, 0)) i = 0 for { if !(i < (*TFts5Expr)(unsafe.Pointer(p2)).FnPhrase) { break } *(*uintptr)(unsafe.Pointer(ap + uintptr(i)*8)) = *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(p2)).FapExprPhrase + uintptr(i)*8)) 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 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(144) defer tls.Free(144) 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+128 */ TFts5PoslistWriter _, _, _, _, _, _, _, _, _, _, _ = aIter, aNew, i, iMin, iPrev, nAlloc, nByte, nIter, p, pIter, rc aIter = bp nIter = 0 nAlloc = int32(4) rc = SQLITE_OK 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(uint64(32) * uint64(nAlloc) * uint64(2)) aNew = Xsqlite3_malloc64(tls, uint64(nByte)) if aNew == uintptr(0) { rc = int32(SQLITE_NOMEM) goto synonym_poslist_out } libc.X__builtin___memcpy_chk(tls, aNew, aIter, uint64(32)*uint64(nIter), libc.X__builtin_object_size(tls, aNew, 0)) 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)*32) 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 + 128)) = 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(176) defer tls.Free(176) var aIter, pPos, pTerm uintptr var bFirst, bFlag, bMatch, i, rc int32 var iAdj, iPos Ti64 var nByte Tsqlite3_int64 var _ /* a at bp+144 */ uintptr var _ /* aStatic at bp+8 */ [4]TFts5PoslistReader var _ /* buf at bp+152 */ TFts5Buffer var _ /* n at bp+136 */ 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 + 32))).FbFirst) _sqlite3Fts5BufferZero(tls, pPhrase+8) /* 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.Uint64FromInt64(128)/libc.Uint64FromInt64(32)) { nByte = int64(uint64(32) * uint64((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm)) aIter = Xsqlite3_malloc64(tls, uint64(nByte)) if !(aIter != 0) { return int32(SQLITE_NOMEM) } } libc.X__builtin___memset_chk(tls, aIter, 0, uint64(32)*uint64((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm), libc.X__builtin_object_size(tls, aIter, 0)) /* Initialize a term iterator for each term in the phrase */ i = 0 for { if !(i < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) { break } pTerm = pPhrase + 32 + uintptr(i)*40 *(*int32)(unsafe.Pointer(bp + 136)) = 0 bFlag = 0 *(*uintptr)(unsafe.Pointer(bp + 144)) = uintptr(0) if (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym != 0 { *(*TFts5Buffer)(unsafe.Pointer(bp + 152)) = TFts5Buffer{} rc = _fts5ExprSynonymList(tls, pTerm, (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid, bp+152, bp+144, bp+136) if rc != 0 { Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 144))) goto ismatch_out } if *(*uintptr)(unsafe.Pointer(bp + 144)) == (*(*TFts5Buffer)(unsafe.Pointer(bp + 152))).Fp { bFlag = int32(1) } } else { *(*uintptr)(unsafe.Pointer(bp + 144)) = (*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpIter)).FpData *(*int32)(unsafe.Pointer(bp + 136)) = (*TFts5IndexIter)(unsafe.Pointer((*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpIter)).FnData } _sqlite3Fts5PoslistReaderInit(tls, *(*uintptr)(unsafe.Pointer(bp + 144)), *(*int32)(unsafe.Pointer(bp + 136)), aIter+uintptr(i)*32) (*(*TFts5PoslistReader)(unsafe.Pointer(aIter + uintptr(i)*32))).FbFlag = uint8(bFlag) if (*(*TFts5PoslistReader)(unsafe.Pointer(aIter + uintptr(i)*32))).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)*32 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+8, 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)*32) != 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)*32))).FbFlag != 0 { Xsqlite3_free(tls, (*(*TFts5PoslistReader)(unsafe.Pointer(aIter + uintptr(i)*32))).Fa) } goto _4 _4: ; i++ } if aIter != bp+8 { Xsqlite3_free(tls, aIter) } return rc } type TFts5LookaheadReader = struct { Fa uintptr Fn int32 Fi int32 FiPos Ti64 FiLookahead Ti64 } type Fts5LookaheadReader = TFts5LookaheadReader type TFts5LookaheadReader1 = struct { Fa uintptr Fn int32 Fi int32 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+12, 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.Uint64FromInt64(192)/libc.Uint64FromInt64(48)) { nByte = int64(uint64(48) * uint64((*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase)) a = _sqlite3Fts5MallocZero(tls, bp+192, nByte) } else { libc.X__builtin___memset_chk(tls, bp, 0, uint64(192), libc.X__builtin_object_size(tls, bp, 0)) } 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)*8)) + 8 _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 + 24 + uintptr(i)*8)))).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 } *(*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 + 24)) (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.Fn = 0 pTerm = pPhrase + 32 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 + 24 + uintptr(i)*8)) if (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase1)).FnTerm > int32(1) || (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase1 + 32))).FpSynonym != 0 || (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FpColset != 0 || (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase1 + 32))).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 + 32))).FpIter _sqlite3Fts5BufferSet(tls, bp, pPhrase1+8, (*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 i = 0 for { if !(i < (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase) { break } pPhrase = *(*uintptr)(unsafe.Pointer(pNear + 24 + uintptr(i)*8)) 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 + 32 + uintptr(j)*40 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+24) 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) { 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 + 48 + uintptr(i)*8))) 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 + 24 + uintptr(i)*8)) (*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 + 48 + uintptr(i1)*8))) 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 + 24)) *(*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 */ /* 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 + 32))).FpSynonym != 0 { *(*Ti64)(unsafe.Pointer(bp + 8)) = _fts5ExprSynonymRowid(tls, pLeft+32, bDesc, uintptr(0)) } else { *(*Ti64)(unsafe.Pointer(bp + 8)) = (*TFts5IndexIter)(unsafe.Pointer((*(*TFts5ExprTerm)(unsafe.Pointer(pLeft + 32))).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 + 24 + uintptr(i)*8)) j = 0 for { if !(j < (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm) { break } pTerm = pPhrase + 32 + uintptr(j)*40 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 + 32 + uintptr(j)*40))).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) 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 + 24)) + 32 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 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 { 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 + 24)) pIter = (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 32))).FpIter (*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 + 24)) + 32))).FpIter 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 + 48)) i = int32(1) for { if !(i < (*TFts5ExprNode)(unsafe.Pointer(pNode)).FnChild) { break } pChild = *(*uintptr)(unsafe.Pointer(pNode + 48 + uintptr(i)*8)) 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 + 48 + uintptr(i)*8)) 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 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 + 48 + uintptr(iChild)*8)) 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. */ 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 + 48)))).FxNext})))(tls, pExpr, *(*uintptr)(unsafe.Pointer(pNode + 48)), 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 + 48)) p2 = *(*uintptr)(unsafe.Pointer(pNode + 48 + 1*8)) 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) } 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 + 48)))).FxNext})))(tls, pExpr, *(*uintptr)(unsafe.Pointer(pNode + 48)), 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: 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 + 48 + uintptr(i)*8)) rc = _fts5ExprNodeFirst(tls, pExpr, *(*uintptr)(unsafe.Pointer(pNode + 48 + uintptr(i)*8))) nEof += (*TFts5ExprNode)(unsafe.Pointer(pChild)).FbEof goto _1 _1: ; i++ } (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid = (*TFts5ExprNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNode + 48)))).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: (*TFts5ExprNode)(unsafe.Pointer(pNode)).FbEof = (*TFts5ExprNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNode + 48)))).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 { 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 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))) } 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 + 32 + uintptr(i)*40 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*40) Xsqlite3_free(tls, pSyn) goto _2 _2: ; pSyn = pNext } goto _1 _1: ; i++ } if (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).Fposlist.FnSpace > 0 { _sqlite3Fts5BufferFree(tls, pPhrase+8) } 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 + 32))).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(uint64(32) + uint64(SZALLOC)*uint64(8)) pRet = Xsqlite3_malloc64(tls, uint64(nByte)) if pRet == uintptr(0) { (*TFts5Parse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, pRet, 0, uint64(nByte), libc.X__builtin_object_size(tls, pRet, 0)) } } else { if (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase%SZALLOC == 0 { nNew = (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase + SZALLOC nByte1 = int64(uint64(32) + uint64(nNew)*uint64(8)) 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) { _sqlite3Fts5ParseNearsetFree(tls, pNear) _sqlite3Fts5ParsePhraseFree(tls, pPhrase) } else { if (*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase > 0 { pLast = *(*uintptr)(unsafe.Pointer(pRet + 24 + uintptr((*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase-int32(1))*8)) 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))*8)) = pPhrase (*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-- (*TFts5ExprNearset)(unsafe.Pointer(pRet)).FnPhrase-- } } } v2 = pRet + 16 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*uintptr)(unsafe.Pointer(pRet + 24 + uintptr(v1)*8)) = 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.Uint64FromInt64(40) + libc.Uint64FromInt64(16) + uint64(nToken) + uint64(1)) pSyn = Xsqlite3_malloc64(tls, uint64(nByte)) if pSyn == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, pSyn, 0, uint64(nByte), libc.X__builtin_object_size(tls, pSyn, 0)) (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpTerm = pSyn + uintptr(40) + uintptr(16) 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.X__builtin___memcpy_chk(tls, (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpTerm, pToken, uint64(nToken), libc.X__builtin_object_size(tls, (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpTerm, 0)) (*TFts5ExprTerm)(unsafe.Pointer(pSyn)).FpSynonym = (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 32 + uintptr((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm-int32(1))*40))).FpSynonym (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 32 + uintptr((*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm-int32(1))*40))).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(72)+uint64(40)*uint64(nNew)) if pNew == uintptr(0) { *(*int32)(unsafe.Pointer(bp)) = int32(SQLITE_NOMEM) } else { if pPhrase == uintptr(0) { libc.X__builtin___memset_chk(tls, pNew, 0, uint64(72), libc.X__builtin_object_size(tls, pNew, 0)) } 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 + 24 v4 = *(*int32)(unsafe.Pointer(v5)) *(*int32)(unsafe.Pointer(v5))++ pTerm = pPhrase + 32 + uintptr(v4)*40 libc.X__builtin___memset_chk(tls, pTerm, 0, uint64(40), libc.X__builtin_object_size(tls, pTerm, 0)) (*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 + 24 + uintptr(i)*8))) 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) { (*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(uint64(8) * uint64((*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(32) defer tls.Free(32) var flags, n, rc, v1, v2 int32 var pConfig uintptr var v3 bool var _ /* sCtx at bp+0 */ TTokenCtx var _ /* z at bp+24 */ uintptr _, _, _, _, _, _, _ = flags, n, pConfig, rc, v1, v2, v3 pConfig = (*TFts5Parse)(unsafe.Pointer(pParse)).FpConfig /* Tokenize return code */ *(*uintptr)(unsafe.Pointer(bp + 24)) = uintptr(0) libc.X__builtin___memset_chk(tls, bp, 0, uint64(24), libc.X__builtin_object_size(tls, bp, 0)) (*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase = pAppend (*(*TTokenCtx)(unsafe.Pointer(bp))).FpConfig = pConfig rc = _fts5ParseStringFromToken(tls, pToken, bp+24) 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 + 24))) n = int32(libc.Xstrlen(tls, *(*uintptr)(unsafe.Pointer(bp + 24)))) rc = _sqlite3Fts5Tokenize(tls, pConfig, flags, *(*uintptr)(unsafe.Pointer(bp + 24)), n, bp, __ccgo_fp(_fts5ParseTokenize)) } Xsqlite3_free(tls, *(*uintptr)(unsafe.Pointer(bp + 24))) 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+16, int64(72)) } else { if (*TFts5ExprPhrase)(unsafe.Pointer((*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase)).FnTerm != 0 { (*(*TFts5ExprTerm)(unsafe.Pointer((*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase + 32 + uintptr((*TFts5ExprPhrase)(unsafe.Pointer((*(*TTokenCtx)(unsafe.Pointer(bp))).FpPhrase)).FnTerm-int32(1))*40))).FbPrefix = uint8(bPrefix) } } *(*uintptr)(unsafe.Pointer((*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase + uintptr((*TFts5Parse)(unsafe.Pointer(pParse)).FnPhrase-int32(1))*8)) = (*(*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(32) defer tls.Free(32) 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+8 */ 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 + 8)) = 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)*8)) pNew = _sqlite3Fts5MallocZero(tls, bp, int64(40)) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { (*TFts5Expr)(unsafe.Pointer(pNew)).FapExprPhrase = _sqlite3Fts5MallocZero(tls, bp, int64(8)) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { (*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot = _sqlite3Fts5MallocZero(tls, bp, int64(56)) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { (*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FpNear = _sqlite3Fts5MallocZero(tls, bp, int64(libc.Uint64FromInt64(32)+libc.Uint64FromInt64(8))) } 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(uint64(8) + uint64((*TFts5Colset)(unsafe.Pointer(pColsetOrig)).FnCol-libc.Int32FromInt32(1))*uint64(4)) pColset = _sqlite3Fts5MallocZero(tls, bp, nByte) if pColset != 0 { libc.X__builtin___memcpy_chk(tls, pColset, pColsetOrig, uint64(nByte), libc.X__builtin_object_size(tls, pColset, 0)) } (*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 + 8))).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 + 32 + uintptr(i)*40 for { if !(p != 0 && *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK) { break } *(*int32)(unsafe.Pointer(bp)) = _fts5ParseTokenize(tls, bp+8, 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 + 8))).FpPhrase + 32 + uintptr(i)*40))).FbPrefix = (*(*TFts5ExprTerm)(unsafe.Pointer(pOrig + 32 + uintptr(i)*40))).FbPrefix (*(*TFts5ExprTerm)(unsafe.Pointer((*(*TTokenCtx)(unsafe.Pointer(bp + 8))).FpPhrase + 32 + uintptr(i)*40))).FbFirst = (*(*TFts5ExprTerm)(unsafe.Pointer(pOrig + 32 + uintptr(i)*40))).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 + 8))).FpPhrase = _sqlite3Fts5MallocZero(tls, bp, int64(72)) } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && (*(*TTokenCtx)(unsafe.Pointer(bp + 8))).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 + 8))).FpPhrase *(*uintptr)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FpNear + 24)) = (*(*TTokenCtx)(unsafe.Pointer(bp + 8))).FpPhrase (*TFts5ExprNearset)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot)).FpNear)).FnPhrase = int32(1) (*TFts5ExprPhrase)(unsafe.Pointer((*(*TTokenCtx)(unsafe.Pointer(bp + 8))).FpPhrase)).FpNode = (*TFts5Expr)(unsafe.Pointer(pNew)).FpRoot if (*TFts5ExprPhrase)(unsafe.Pointer(pOrig)).FnTerm == int32(1) && (*(*TFts5ExprTerm)(unsafe.Pointer(pOrig + 32))).FpSynonym == uintptr(0) && int32((*(*TFts5ExprTerm)(unsafe.Pointer(pOrig + 32))).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 + 8))).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+36508, (*TFts5Token)(unsafe.Pointer(pTok)).Fp, uint64(4)) != 0 { _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+35289, 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 int8 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 = *(*int8)(unsafe.Pointer((*TFts5Token)(unsafe.Pointer(p)).Fp + uintptr(i))) if int32(c) < int32('0') || int32(c) > int32('9') { _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+36513, libc.VaList(bp+8, (*TFts5Token)(unsafe.Pointer(p)).Fn, (*TFts5Token)(unsafe.Pointer(p)).Fp)) return } nNear = nNear*int32(10) + (int32(*(*int8)(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 */ pNew = Xsqlite3_realloc64(tls, p, uint64(8)+uint64(4)*uint64(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+16, int64(uint64(8)+uint64(4)*uint64(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+16, (*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)*8)), z) { break } goto _1 _1: ; iCol++ } if iCol == (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol { _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+21543, libc.VaList(bp+8, z)) } else { pRet = _fts5ParseColset(tls, pParse, pColset, iCol) } Xsqlite3_free(tls, z) } if pRet == uintptr(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(uint64(8) + uint64((*TFts5Colset)(unsafe.Pointer(pOrig)).FnCol-libc.Int32FromInt32(1))*uint64(4)) pRet = _sqlite3Fts5MallocZero(tls, pRc, nByte) if pRet != 0 { libc.X__builtin___memcpy_chk(tls, pRet, pOrig, uint64(nByte), libc.X__builtin_object_size(tls, pRet, 0)) } } 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 { 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+16, pColset) } } } else { i = 0 for { if !(i < (*TFts5ExprNode)(unsafe.Pointer(pNode)).FnChild) { break } _fts5ParseSetColset(tls, pParse, *(*uintptr)(unsafe.Pointer(pNode + 48 + uintptr(i)*8)), 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+36542, 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 + 24)))).FnTerm == int32(1) && (*(*TFts5ExprTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 24)) + 32))).FpSynonym == uintptr(0) && int32((*(*TFts5ExprTerm)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 24)) + 32))).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: (*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(uint64(8) * uint64((*TFts5ExprNode)(unsafe.Pointer(pSub)).FnChild)) libc.X__builtin___memcpy_chk(tls, p+48+uintptr((*TFts5ExprNode)(unsafe.Pointer(p)).FnChild)*8, pSub+48, uint64(nByte), libc.X__builtin_object_size(tls, p+48+uintptr((*TFts5ExprNode)(unsafe.Pointer(p)).FnChild)*8, 0)) *(*int32)(unsafe.Pointer(p + 40)) += (*TFts5ExprNode)(unsafe.Pointer(pSub)).FnChild Xsqlite3_free(tls, pSub) } else { v2 = p + 40 v1 = *(*int32)(unsafe.Pointer(v2)) *(*int32)(unsafe.Pointer(v2))++ *(*uintptr)(unsafe.Pointer(p + 48 + uintptr(v1)*8)) = pSub } for { if !(ii < (*TFts5ExprNode)(unsafe.Pointer(p)).FnChild) { break } if (*TFts5ExprNode)(unsafe.Pointer(p)).FiHeight > (*TFts5ExprNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 48 + uintptr(ii)*8)))).FiHeight+int32(1) { v4 = (*TFts5ExprNode)(unsafe.Pointer(p)).FiHeight } else { v4 = (*TFts5ExprNode)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(p + 48 + uintptr(ii)*8)))).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 + 24)))).FnTerm nByte = int32(uint64(56) + uint64(nTerm)*uint64(8)) pRet = _sqlite3Fts5MallocZero(tls, pParse+16, 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+16, int64(72)) if pPhrase != 0 { if _parseGrowPhraseArray(tls, pParse) != 0 { _fts5ExprPhraseFree(tls, pPhrase) } else { p = *(*uintptr)(unsafe.Pointer(pNear + 24)) + 32 + uintptr(ii)*40 pTo = pPhrase + 32 v3 = pParse + 20 v2 = *(*int32)(unsafe.Pointer(v3)) *(*int32)(unsafe.Pointer(v3))++ *(*uintptr)(unsafe.Pointer((*TFts5Parse)(unsafe.Pointer(pParse)).FapPhrase + uintptr(v2)*8)) = pPhrase (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm = int32(1) (*TFts5ExprTerm)(unsafe.Pointer(pTo)).FpTerm = _sqlite3Fts5Strndup(tls, pParse+16, (*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 + 48 + uintptr(ii)*8)) = _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 */ 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 + 24)))).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(uint64(56) + uint64(8)*uint64(nChild-libc.Int32FromInt32(1))) pRet = _sqlite3Fts5MallocZero(tls, pParse+16, 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 + 24 + uintptr(iPhrase)*8)))).FpNode = pRet if (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pNear + 24 + uintptr(iPhrase)*8)))).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 + 24)) 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 + 32))).FbFirst != 0 { if (*TFts5ExprNearset)(unsafe.Pointer(pNear)).FnPhrase == int32(1) { v2 = __ccgo_ts + 36595 } else { v2 = __ccgo_ts + 36508 } _sqlite3Fts5ParseError(tls, pParse, __ccgo_ts+36602, 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+36652, libc.VaList(bp+8, int32(SQLITE_FTS5_MAX_EXPR_DEPTH))) Xsqlite3_free(tls, pRet) pRet = uintptr(0) } } } } } if pRet == uintptr(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 { if (*TFts5ExprNode)(unsafe.Pointer(pLeft)).FeType == int32(FTS5_AND) { pPrev = *(*uintptr)(unsafe.Pointer(pLeft + 48 + uintptr((*TFts5ExprNode)(unsafe.Pointer(pLeft)).FnChild-int32(1))*8)) } else { pPrev = pLeft } if (*TFts5ExprNode)(unsafe.Pointer(pRight)).FeType == FTS5_EOF { _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 + 48 + uintptr((*TFts5ExprNode)(unsafe.Pointer(pLeft)).FnChild-int32(1))*8)) = 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)*8 libc.X__builtin___memmove_chk(tls, ap, ap+1*8, uint64(8)*uint64((*TFts5ExprNearset)(unsafe.Pointer((*TFts5ExprNode)(unsafe.Pointer(pRight)).FpNear)).FnPhrase), libc.X__builtin_object_size(tls, ap, 0)) (*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)*8)))).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)*8)) 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 { 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(16)*uint64((*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase)) if pRet != 0 { libc.X__builtin___memset_chk(tls, pRet, 0, uint64(16)*uint64((*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase), libc.X__builtin_object_size(tls, pRet, 0)) i = 0 for { if !(i < (*TFts5Expr)(unsafe.Pointer(pExpr)).FnPhrase) { break } pBuf = *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(i)*8)) + 8 pNode = (*TFts5ExprPhrase)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(i)*8)))).FpNode 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 { FpExpr uintptr FaPopulator uintptr FiOff Ti64 } type Fts5ExprCtx1 = TFts5ExprCtx1 type TFts5ExprCtx = struct { 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 && *(*int8)(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)*8)) + 32 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, uint64((*TFts5ExprTerm)(unsafe.Pointer(pT)).FnQueryTerm)) == 0 { rc = _sqlite3Fts5PoslistWriterAppend(tls, *(*uintptr)(unsafe.Pointer((*TFts5Expr)(unsafe.Pointer(pExpr)).FapExprPhrase + uintptr(i)*8))+8, (*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(32) defer tls.Free(32) 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 + 48 + uintptr(i)*8)), 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 + 48 + uintptr(i1)*8)), iRowid) != 0 { bRet = int32(1) } goto _2 _2: ; i1++ } return bRet default: if 0 == _fts5ExprCheckPoslists(tls, *(*uintptr)(unsafe.Pointer(pNode + 48)), iRowid) || 0 != _fts5ExprCheckPoslists(tls, *(*uintptr)(unsafe.Pointer(pNode + 48 + 1*8)), 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)*8)) pNode = (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FpNode rc = SQLITE_OK 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 + 32 if (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym != 0 { pBuf = (*TFts5ExprTerm)(unsafe.Pointer(pTerm)).FpSynonym + 1*40 rc = _fts5ExprSynonymList(tls, pTerm, (*TFts5ExprNode)(unsafe.Pointer(pNode)).FiRowid, pBuf, ppCollist, pnCollist) } else { *(*uintptr)(unsafe.Pointer(ppCollist)) = (*TFts5IndexIter)(unsafe.Pointer((*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 32))).FpIter)).FpData *(*int32)(unsafe.Pointer(pnCollist)) = (*TFts5IndexIter)(unsafe.Pointer((*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 32))).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)*8)) if iToken < 0 || iToken >= (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm { return int32(SQLITE_RANGE) } *(*uintptr)(unsafe.Pointer(ppOut)) = (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 32 + uintptr(iToken)*40))).FpTerm *(*int32)(unsafe.Pointer(pnOut)) = (*(*TFts5ExprTerm)(unsafe.Pointer(pPhrase + 32 + uintptr(iToken)*40))).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)*8)) if iToken < 0 || iToken >= (*TFts5ExprPhrase)(unsafe.Pointer(pPhrase)).FnTerm { return int32(SQLITE_RANGE) } pTerm = pPhrase + 32 + uintptr(iToken)*40 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)*8)) + 32 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 { 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 { 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(40)) pNew = v1 *(*uintptr)(unsafe.Pointer(ppNew)) = v1 if pNew == uintptr(0) { rc = int32(SQLITE_NOMEM) } else { libc.X__builtin___memset_chk(tls, pNew, 0, uint64(40), libc.X__builtin_object_size(tls, pNew, 0)) (*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(uint64(8) * uint64((*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.X__builtin___memset_chk(tls, (*TFts5Hash)(unsafe.Pointer(pNew)).FaSlot, 0, uint64(nByte), libc.X__builtin_object_size(tls, (*TFts5Hash)(unsafe.Pointer(pNew)).FaSlot, 0)) } } 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)*8)) 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.X__builtin___memset_chk(tls, (*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot, 0, uint64((*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot)*uint64(8), libc.X__builtin_object_size(tls, (*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot, 0)) (*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 int8, 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, uint8(bByte), pToken, nToken) p = *(*uintptr)(unsafe.Pointer((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*8)) for { if !(p != 0) { break } zKey = p + 1*48 if int32(*(*int8)(unsafe.Pointer(zKey))) == int32(bByte) && (*TFts5HashEntry)(unsafe.Pointer(p)).FnKey == nToken+int32(1) && libc.Xmemcmp(tls, zKey+1, pToken, uint64(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(uint64(48) + uint64(nToken+libc.Int32FromInt32(1)) + uint64(1) + uint64(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, uint8(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.X__builtin___memset_chk(tls, p, 0, uint64(48), libc.X__builtin_object_size(tls, p, 0)) (*TFts5HashEntry)(unsafe.Pointer(p)).FnAlloc = int32(nByte) zKey1 = p + 1*48 *(*int8)(unsafe.Pointer(zKey1)) = bByte libc.X__builtin___memcpy_chk(tls, zKey1+1, pToken, uint64(nToken), libc.X__builtin_object_size(tls, zKey1+1, 0)) (*TFts5HashEntry)(unsafe.Pointer(p)).FnKey = nToken + int32(1) *(*int8)(unsafe.Pointer(zKey1 + uintptr(nToken+int32(1)))) = int8('\000') (*TFts5HashEntry)(unsafe.Pointer(p)).FnData = int32(uint64(nToken+int32(1)) + uint64(48)) (*TFts5HashEntry)(unsafe.Pointer(p)).FpHashNext = *(*uintptr)(unsafe.Pointer((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*8)) *(*uintptr)(unsafe.Pointer((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iHash)*8)) = p (*TFts5Hash)(unsafe.Pointer(pHash)).FnEntry++ /* Add the first rowid field to the hash-entry */ *(*int32)(unsafe.Pointer(p + 24)) += _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 + 24)) += 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)*8 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 } 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 + 24)) += _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 + 24)) += 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 */ if iCol != int32((*TFts5HashEntry)(unsafe.Pointer(p)).FiCol) { if (*TFts5Hash)(unsafe.Pointer(pHash)).FeDetail == FTS5_DETAIL_FULL { v6 = p + 24 v5 = *(*int32)(unsafe.Pointer(v6)) *(*int32)(unsafe.Pointer(v6))++ *(*Tu8)(unsafe.Pointer(pPtr + uintptr(v5))) = uint8(0x01) *(*int32)(unsafe.Pointer(p + 24)) += _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 + 24)) += _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*48 zKey2 = p2 + 1*48 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, uint64(nMin)) if cmp == 0 { cmp = (*TFts5HashEntry)(unsafe.Pointer(p1)).FnKey - (*TFts5HashEntry)(unsafe.Pointer(p2)).FnKey } if cmp > 0 { /* p2 is smaller */ *(*uintptr)(unsafe.Pointer(ppOut)) = p2 ppOut = p2 + 8 p2 = (*TFts5HashEntry)(unsafe.Pointer(p2)).FpScanNext } else { /* p1 is smaller */ *(*uintptr)(unsafe.Pointer(ppOut)) = p1 ppOut = p1 + 8 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(8)*uint64(nMergeSlot)) if !(ap != 0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, ap, 0, uint64(8)*uint64(nMergeSlot), libc.X__builtin_object_size(tls, ap, 0)) iSlot = 0 for { if !(iSlot < (*TFts5Hash)(unsafe.Pointer(pHash)).FnSlot) { break } pIter = *(*uintptr)(unsafe.Pointer((*TFts5Hash)(unsafe.Pointer(pHash)).FaSlot + uintptr(iSlot)*8)) for { if !(pIter != 0) { break } if pTerm == uintptr(0) || (*TFts5HashEntry)(unsafe.Pointer(pIter)).FnKey >= nTerm && 0 == libc.Xmemcmp(tls, pIter+1*48, pTerm, uint64(nTerm)) { pEntry = pIter (*TFts5HashEntry)(unsafe.Pointer(pEntry)).FpScanNext = uintptr(0) i = 0 for { if !(*(*uintptr)(unsafe.Pointer(ap + uintptr(i)*8)) != 0) { break } pEntry = _fts5HashEntryMerge(tls, pEntry, *(*uintptr)(unsafe.Pointer(ap + uintptr(i)*8))) *(*uintptr)(unsafe.Pointer(ap + uintptr(i)*8)) = uintptr(0) goto _3 _3: ; i++ } *(*uintptr)(unsafe.Pointer(ap + uintptr(i)*8)) = 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)*8))) 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)*8)) for { if !(p != 0) { break } zKey = p + 1*48 if nTerm == (*TFts5HashEntry)(unsafe.Pointer(p)).FnKey && libc.Xmemcmp(tls, zKey, pTerm, uint64(nTerm)) == 0 { break } goto _1 _1: ; p = (*TFts5HashEntry)(unsafe.Pointer(p)).FpHashNext } if p != 0 { nHashPre = int32(uint64(48) + uint64(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.X__builtin___memcpy_chk(tls, pRet+uintptr(nPre), p+uintptr(nHashPre), uint64(nList), libc.X__builtin_object_size(tls, pRet+uintptr(nPre), 0)) 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+24) } // C documentation // // /* // ** Return true if the hash table is empty, false otherwise. // */ func _sqlite3Fts5HashIsEmpty(tls *libc.TLS, pHash uintptr) (r int32) { return libc.BoolInt32((*TFts5Hash)(unsafe.Pointer(pHash)).FnEntry == 0) } func _sqlite3Fts5HashScanNext(tls *libc.TLS, p uintptr) { (*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*48 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(uint64((*TFts5HashEntry)(unsafe.Pointer(p)).FnData) - (uint64(48) + uint64(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 { FnLvl int32 FiSegid int32 FaLvl [1]TFts5DlidxLvl } type Fts5DlidxIter = TFts5DlidxIter type TFts5DlidxLvl = struct { FpData uintptr FiOff int32 FbEof int32 FiFirstOff int32 FiLeafPgno int32 FiRowid Ti64 } type Fts5DlidxLvl = TFts5DlidxLvl type TFts5DlidxWriter = struct { Fpgno int32 FbPrevValid int32 FiPrev Ti64 Fbuf TFts5Buffer } type Fts5DlidxWriter = TFts5DlidxWriter type TFts5Iter = struct { Fbase TFts5IndexIter FpTokenDataIter uintptr FpIndex uintptr Fposlist TFts5Buffer FpColset uintptr FxSetOutputs uintptr FnSeg int32 FbRev int32 FbSkipEmpty Tu8 FiSwitchRowid Ti64 FaFirst uintptr 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 { FpSeg uintptr Fflags int32 FiLeafPgno int32 FpLeaf uintptr FpNextLeaf uintptr FiLeafOffset Ti64 FpTombArray uintptr FxNext uintptr FiTermLeafPgno int32 FiTermLeafOffset int32 FiPgidxOff int32 FiEndofDoclist int32 FiRowidOffset int32 FnRowidOffset int32 FaRowidOffset uintptr FpDlidx uintptr Fterm TFts5Buffer FiRowid Ti64 FnPos int32 FbDel Tu8 } type Fts5SegIter = TFts5SegIter type TFts5DoclistIter = struct { FaEof uintptr FiRowid Ti64 FaPoslist uintptr FnPoslist int32 FnSize int32 } type Fts5DoclistIter = TFts5DoclistIter type TFts5SegWriter = struct { FiSegid int32 Fwriter TFts5PageWriter FiPrevRowid Ti64 FbFirstRowidInDoclist Tu8 FbFirstRowidInPage Tu8 FbFirstTermInPage Tu8 FnLeafWritten int32 FnEmpty int32 FnDlidx int32 FaDlidx uintptr Fbtterm TFts5Buffer FiBtPage int32 } type Fts5SegWriter = TFts5SegWriter type TFts5Structure = struct { FnRef int32 FnWriteCounter Tu64 FnOriginCntr Tu64 FnSegment int32 FnLevel int32 FaLevel [1]TFts5StructureLevel } type Fts5Structure = TFts5Structure type TFts5StructureLevel = struct { FnMerge int32 FnSeg int32 FaSeg uintptr } type Fts5StructureLevel = TFts5StructureLevel type TFts5StructureSegment = struct { FiSegid int32 FpgnoFirst int32 FpgnoLast int32 FiOrigin1 Tu64 FiOrigin2 Tu64 FnPgTombstone int32 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 { FiRowid Ti64 FiPos Ti64 FiIter int32 } 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 { FpConfig uintptr FzDataTbl uintptr FnWorkUnit int32 FpHash uintptr FnPendingData int32 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 FiStructVersion Ti64 FpStruct uintptr } type Fts5Index1 = TFts5Index1 type TFts5DoclistIter1 = struct { FaEof uintptr FiRowid Ti64 FaPoslist uintptr FnPoslist int32 FnSize int32 } 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 { FiSegid int32 FpgnoFirst int32 FpgnoLast int32 FiOrigin1 Tu64 FiOrigin2 Tu64 FnPgTombstone int32 FnEntryTombstone Tu64 FnEntry Tu64 } type Fts5StructureSegment1 = TFts5StructureSegment1 type TFts5StructureLevel1 = struct { FnMerge int32 FnSeg int32 FaSeg uintptr } type Fts5StructureLevel1 = TFts5StructureLevel1 type TFts5Structure1 = struct { FnRef int32 FnWriteCounter Tu64 FnOriginCntr Tu64 FnSegment int32 FnLevel int32 FaLevel [1]TFts5StructureLevel } 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 { Fpgno int32 FbPrevValid int32 FiPrev Ti64 Fbuf TFts5Buffer } type Fts5DlidxWriter1 = TFts5DlidxWriter1 type TFts5SegWriter1 = struct { FiSegid int32 Fwriter TFts5PageWriter FiPrevRowid Ti64 FbFirstRowidInDoclist Tu8 FbFirstRowidInPage Tu8 FbFirstTermInPage Tu8 FnLeafWritten int32 FnEmpty int32 FnDlidx int32 FaDlidx uintptr Fbtterm TFts5Buffer FiBtPage int32 } 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 { FpSeg uintptr Fflags int32 FiLeafPgno int32 FpLeaf uintptr FpNextLeaf uintptr FiLeafOffset Ti64 FpTombArray uintptr FxNext uintptr FiTermLeafPgno int32 FiTermLeafOffset int32 FiPgidxOff int32 FiEndofDoclist int32 FiRowidOffset int32 FnRowidOffset int32 FaRowidOffset uintptr FpDlidx uintptr Fterm TFts5Buffer FiRowid Ti64 FnPos int32 FbDel Tu8 } 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 { Fbase TFts5IndexIter FpTokenDataIter uintptr FpIndex uintptr Fposlist TFts5Buffer FpColset uintptr FxSetOutputs uintptr FnSeg int32 FbRev int32 FbSkipEmpty Tu8 FiSwitchRowid Ti64 FaFirst uintptr 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 { FpData uintptr FiOff int32 FbEof int32 FiFirstOff int32 FiLeafPgno int32 FiRowid Ti64 } type Fts5DlidxLvl1 = TFts5DlidxLvl1 type TFts5DlidxIter1 = struct { 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+60, 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 if nCmp <= 0 { v2 = 0 } else { v2 = libc.Xmemcmp(tls, (*TFts5Buffer)(unsafe.Pointer(pLeft)).Fp, (*TFts5Buffer)(unsafe.Pointer(pRight)).Fp, uint64(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) (*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+36705, iRowid, 0, p+72) } /* 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 { i = 0 for { if !(i < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) { break } Xsqlite3_free(tls, (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(i)*16))).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(uint64(48) + uint64((*TFts5Structure)(unsafe.Pointer(p)).FnLevel-libc.Int32FromInt32(1))*uint64(16)) pNew = _sqlite3Fts5MallocZero(tls, pRc, nByte) if pNew != 0 { libc.X__builtin___memcpy_chk(tls, pNew, p, uint64(nByte), libc.X__builtin_object_size(tls, pNew, 0)) i = 0 for { if !(i < (*TFts5Structure)(unsafe.Pointer(p)).FnLevel) { break } (*(*TFts5StructureLevel)(unsafe.Pointer(pNew + 32 + uintptr(i)*16))).FaSeg = uintptr(0) goto _1 _1: ; i++ } i = 0 for { if !(i < (*TFts5Structure)(unsafe.Pointer(p)).FnLevel) { break } pLvl = pNew + 32 + uintptr(i)*16 nByte = int64(uint64(56) * uint64((*(*TFts5StructureLevel)(unsafe.Pointer(pNew + 32 + uintptr(i)*16))).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)*16))).FaSeg) goto _3 _3: ; i++ } Xsqlite3_free(tls, pNew) return } libc.X__builtin___memcpy_chk(tls, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg, (*(*TFts5StructureLevel)(unsafe.Pointer(p + 32 + uintptr(i)*16))).FaSeg, uint64(nByte), libc.X__builtin_object_size(tls, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg, 0)) 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+36851, uint64(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))*16))).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.X__builtin___memset_chk(tls, bp, 0, uint64(16), libc.X__builtin_object_size(tls, bp, 0)) /* 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+60, 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.X__builtin___memcpy_chk(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), __ccgo_ts+36851, uint64(4), libc.X__builtin_object_size(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), 0)) *(*int32)(unsafe.Pointer(bp + 8)) += int32(4) } *(*int32)(unsafe.Pointer(bp + 8)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), uint64((*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel)) *(*int32)(unsafe.Pointer(bp + 8)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), uint64((*TFts5Structure)(unsafe.Pointer(pStruct)).FnSegment)) *(*int32)(unsafe.Pointer(bp + 8)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp)).Fn), uint64(int64((*TFts5Structure)(unsafe.Pointer(pStruct)).FnWriteCounter))) } iLvl = 0 for { if !(iLvl < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) { break } /* Used to iterate through segments */ pLvl = pStruct + 32 + uintptr(iLvl)*16 _sqlite3Fts5BufferAppendVarint(tls, p+60, bp, int64((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge)) _sqlite3Fts5BufferAppendVarint(tls, p+60, bp, int64((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg)) iSeg = 0 for { if !(iSeg < (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg) { break } pSeg = (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(iSeg)*56 _sqlite3Fts5BufferAppendVarint(tls, p+60, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiSegid)) _sqlite3Fts5BufferAppendVarint(tls, p+60, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoFirst)) _sqlite3Fts5BufferAppendVarint(tls, p+60, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FpgnoLast)) if (*TFts5Structure)(unsafe.Pointer(pStruct)).FnOriginCntr > uint64(0) { _sqlite3Fts5BufferAppendVarint(tls, p+60, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin1)) _sqlite3Fts5BufferAppendVarint(tls, p+60, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FiOrigin2)) _sqlite3Fts5BufferAppendVarint(tls, p+60, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnPgTombstone)) _sqlite3Fts5BufferAppendVarint(tls, p+60, bp, int64((*TFts5StructureSegment)(unsafe.Pointer(pSeg)).FnEntryTombstone)) _sqlite3Fts5BufferAppendVarint(tls, p+60, 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)*16 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)*16 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+60, pStruct, iPromote, int32(1), int32(1)) if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { return } libc.X__builtin___memcpy_chk(tls, (*TFts5StructureLevel)(unsafe.Pointer(pOut)).FaSeg, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg+uintptr(is)*56, uint64(56), libc.X__builtin_object_size(tls, (*TFts5StructureLevel)(unsafe.Pointer(pOut)).FaSeg, 0)) (*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)*16))).FnSeg if nSeg == 0 { return } pSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*16))).FaSeg + uintptr((*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*16))).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)*16))).FnSeg == 0) { break } goto _1 _1: ; iTst-- } if iTst >= 0 { szMax = 0 pTst = pStruct + 32 + uintptr(iTst)*16 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 { (*TFts5DlidxLvl)(unsafe.Pointer(pLvl)).FiOff = int32(1) *(*int32)(unsafe.Pointer(pLvl + 8)) += _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp+1, pLvl+20) *(*int32)(unsafe.Pointer(pLvl + 8)) += 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 + 20)) += 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 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.X__builtin___memset_chk(tls, pLvl, 0, uint64(32), libc.X__builtin_object_size(tls, pLvl, 0)) (*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.X__builtin___memset_chk(tls, pChild, 0, uint64(32), libc.X__builtin_object_size(tls, pChild, 0)) (*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 + 20)) += 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 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.X__builtin___memset_chk(tls, pLvl, 0, uint64(32), libc.X__builtin_object_size(tls, pLvl, 0)) (*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 + 64)) += _sqlite3Fts5GetVarint32(tls, (*TFts5Data)(unsafe.Pointer(pLeaf)).Fp+uintptr((*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff), pIter+68) } } } // 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) } *(*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 */ 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) } (*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 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 + 64)) += _sqlite3Fts5GetVarint32(tls, a+uintptr((*TFts5SegIter)(unsafe.Pointer(pIter)).FiPgidxOff), bp+4) *(*int32)(unsafe.Pointer(pIter + 68)) += *(*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(uint64(nTomb)*uint64(8) + uint64(16)) pNew = _sqlite3Fts5MallocZero(tls, p+60, 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. */ return } if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { libc.X__builtin___memset_chk(tls, pIter, 0, uint64(128), libc.X__builtin_object_size(tls, pIter, 0)) _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) (*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 } 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 + 112 *(*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(nNew)*uint64(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 _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 { 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 + 112 *(*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(48) defer tls.Free(48) var iOff int32 var _ /* iDelta at bp+0 */ Ti64 var _ /* nKeep at bp+8 */ int32 var _ /* nList at bp+36 */ int32 var _ /* nTerm at bp+32 */ int32 var _ /* pList at bp+16 */ uintptr var _ /* zTerm at bp+24 */ uintptr _ = iOff 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 + 112)) += *(*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 + 16)) = uintptr(0) *(*uintptr)(unsafe.Pointer(bp + 24)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 32)) = 0 _sqlite3Fts5HashScanNext(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash) _sqlite3Fts5HashScanEntry(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, bp+24, bp+32, bp+16, bp+36) if *(*uintptr)(unsafe.Pointer(bp + 16)) == uintptr(0) { goto next_none_eof } (*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 + 36)) (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf = *(*int32)(unsafe.Pointer(bp + 36)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = *(*int32)(unsafe.Pointer(bp + 36)) _sqlite3Fts5BufferSet(tls, p+60, pIter+96, *(*int32)(unsafe.Pointer(bp + 32)), *(*uintptr)(unsafe.Pointer(bp + 24))) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(_sqlite3Fts5GetVarint(tls, *(*uintptr)(unsafe.Pointer(bp + 16)), pIter+112)) } 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+36 */ int32 var _ /* nSz at bp+40 */ int32 var _ /* nTerm at bp+32 */ int32 var _ /* pList at bp+16 */ uintptr var _ /* zTerm at bp+24 */ uintptr _, _, _, _, _, _, _, _ = a, bNewTerm, n, pLeaf, v2, v3, v4, p1 pLeaf = (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf bNewTerm = 0 *(*int32)(unsafe.Pointer(bp + 4)) = 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 *(*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. */ 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 + 112 *(*Ti64)(unsafe.Pointer(p1)) = Ti64(uint64(*(*Ti64)(unsafe.Pointer(p1))) + *(*Tu64)(unsafe.Pointer(bp + 8))) } (*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 + 24)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 32)) = 0 *(*int32)(unsafe.Pointer(bp + 36)) = 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+24, bp+32, bp+16, bp+36) } 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 + 36)) (*TFts5Data)(unsafe.Pointer((*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf)).FszLeaf = *(*int32)(unsafe.Pointer(bp + 36)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = *(*int32)(unsafe.Pointer(bp + 36)) + int32(1) _sqlite3Fts5BufferSet(tls, p+60, pIter+96, *(*int32)(unsafe.Pointer(bp + 32)), *(*uintptr)(unsafe.Pointer(bp + 24))) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(_sqlite3Fts5GetVarint(tls, *(*uintptr)(unsafe.Pointer(bp + 16)), pIter+112)) *(*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 } 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+112)) (*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+68) } } 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) } } if *(*int32)(unsafe.Pointer(bp)) > (*TFts5Data)(unsafe.Pointer(pLeaf)).FszLeaf { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<> int32(1) } } } // 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 */ /* 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 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+112, Xsqlite3_mprintf(tls, __ccgo_ts+36879, 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.X__builtin___memset_chk(tls, pIter, 0, uint64(128), libc.X__builtin_object_size(tls, pIter, 0)) (*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 + 8)) |= int32(FTS5_SEGITER_ONETERM) if (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf != 0 { if flags&int32(FTS5INDEX_QUERY_DESC) != 0 { *(*int32)(unsafe.Pointer(pIter + 8)) |= 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). */ } // 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+120, Xsqlite3_mprintf(tls, __ccgo_ts+36963, 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 { 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.X__builtin___memset_chk(tls, pIter, 0, uint64(128), libc.X__builtin_object_size(tls, pIter, 0)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FpSeg = pSeg *(*int32)(unsafe.Pointer(pIter + 8)) |= 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 + 64)) += _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) } } } // 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(48) defer tls.Free(48) var v1 int32 var _ /* n at bp+16 */ int32 var _ /* nList at bp+0 */ int32 var _ /* pLeaf at bp+24 */ uintptr var _ /* pList at bp+32 */ uintptr var _ /* z at bp+8 */ uintptr _ = v1 *(*int32)(unsafe.Pointer(bp)) = 0 *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) *(*int32)(unsafe.Pointer(bp + 16)) = 0 *(*uintptr)(unsafe.Pointer(bp + 24)) = uintptr(0) if pTerm == uintptr(0) || flags&int32(FTS5INDEX_QUERY_SCAN) != 0 { *(*uintptr)(unsafe.Pointer(bp + 32)) = uintptr(0) (*TFts5Index)(unsafe.Pointer(p)).Frc = _sqlite3Fts5HashScanInit(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, pTerm, nTerm) _sqlite3Fts5HashScanEntry(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, bp+8, bp+16, bp+32, bp) if *(*uintptr)(unsafe.Pointer(bp + 32)) != 0 { *(*uintptr)(unsafe.Pointer(bp + 24)) = _fts5IdxMalloc(tls, p, int64(16)) if *(*uintptr)(unsafe.Pointer(bp + 24)) != 0 { (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).Fp = *(*uintptr)(unsafe.Pointer(bp + 32)) } } /* 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(16), pTerm, nTerm, bp+24, bp) if *(*uintptr)(unsafe.Pointer(bp + 24)) != 0 { (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).Fp = *(*uintptr)(unsafe.Pointer(bp + 24)) + 1*16 } *(*uintptr)(unsafe.Pointer(bp + 8)) = pTerm *(*int32)(unsafe.Pointer(bp + 16)) = nTerm *(*int32)(unsafe.Pointer(pIter + 8)) |= int32(FTS5_SEGITER_ONETERM) } if *(*uintptr)(unsafe.Pointer(bp + 24)) != 0 { _sqlite3Fts5BufferSet(tls, p+60, pIter+96, *(*int32)(unsafe.Pointer(bp + 16)), *(*uintptr)(unsafe.Pointer(bp + 8))) v1 = *(*int32)(unsafe.Pointer(bp)) (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).FszLeaf = v1 (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).Fnn = v1 (*TFts5SegIter)(unsafe.Pointer(pIter)).FpLeaf = *(*uintptr)(unsafe.Pointer(bp + 24)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafOffset = int64(_sqlite3Fts5GetVarint(tls, (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).Fp, pIter+112)) (*TFts5SegIter)(unsafe.Pointer(pIter)).FiEndofDoclist = (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 24)))).Fnn if flags&int32(FTS5INDEX_QUERY_DESC) != 0 { *(*int32)(unsafe.Pointer(pIter + 8)) |= 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)*8))) 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)*8))) 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+96) _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.X__builtin___memset_chk(tls, pIter, 0, uint64(128), libc.X__builtin_object_size(tls, pIter, 0)) } // 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 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 + 104 + uintptr(i1)*128 p2 = pIter + 104 + uintptr(i2)*128 (*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+96, p2+96) if res == 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 } 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 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) } if iLeafPgno > (*TFts5SegIter)(unsafe.Pointer(pIter)).FiLeafPgno { _fts5SegIterGotoPage(tls, p, pIter, iLeafPgno) bMove = 0 } } else { for !(_fts5DlidxIterEof(tls, p, pDlidx) != 0) && iMatch < _fts5DlidxIterRowid(tls, pDlidx) { _fts5DlidxIterPrev(tls, p, pDlidx) } iLeafPgno = _fts5DlidxIterPgno(tls, pDlidx) 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+104+uintptr(i)*128) goto _1 _1: ; i++ } _sqlite3Fts5BufferFree(tls, pIter+40) 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 + 104 + uintptr(iEq)*128 (*(*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 + 104 + uintptr(iChanged)*128 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 + 104 + uintptr(iChanged^int32(0x0001))*128 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((int64(pNew) - t__predefined_ptrdiff_t(pIter+104)) / 128) if i == int32(1) { break } pOther = pIter + 104 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + uintptr(i^int32(0x0001))*4))).FiFirst)*128 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 + 104 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*128 (*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 + 104 + uintptr(iFirst)*128 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)) /* If tombstone hash page iPg has not yet been loaded from the ** database, load it now. */ if *(*uintptr)(unsafe.Pointer(pArray + 8 + uintptr(iPg)*8)) == uintptr(0) { *(*uintptr)(unsafe.Pointer(pArray + 8 + uintptr(iPg)*8)) = _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(uint64(232)+uint64(128)*uint64(nSlot-libc.Int64FromInt32(1))+uint64(4)*uint64(nSlot))) if pNew != 0 { (*TFts5Iter)(unsafe.Pointer(pNew)).FnSeg = int32(nSlot) (*TFts5Iter)(unsafe.Pointer(pNew)).FaFirst = pNew + 104 + uintptr(nSlot)*128 (*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 if nChunk > 0 { libc.X__builtin___memcpy_chk(tls, (*TFts5Buffer)(unsafe.Pointer(pContext)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pContext)).Fn), pChunk, uint64(nChunk), libc.X__builtin_object_size(tls, (*TFts5Buffer)(unsafe.Pointer(pContext)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pContext)).Fn), 0)) *(*int32)(unsafe.Pointer(pContext + 8)) += 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 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 + 8)) += _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)) (*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 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 + 8)) += _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))) } 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.X__builtin___memcpy_chk(tls, (*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fn), pChunk+uintptr(iStart), uint64(i-iStart), libc.X__builtin_object_size(tls, (*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fn), 0)) *(*int32)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf + 8)) += 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.X__builtin___memcpy_chk(tls, (*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fn), pChunk+uintptr(iStart), uint64(i-iStart), libc.X__builtin_object_size(tls, (*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf)).Fn), 0)) *(*int32)(unsafe.Pointer((*TPoslistCallbackCtx)(unsafe.Pointer(pCtx)).FpBuf + 8)) += 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. */ 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, v3, v4, p2 uintptr var i int32 var _ /* iCurrent at bp+0 */ int32 _, _, _, _, _, _, _, _ = aCopy, i, p, pEnd, v1, v3, v4, p2 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+40, 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(int64(p) - int64(aCopy)) return } libc.X__builtin___memcpy_chk(tls, (*TFts5Buffer)(unsafe.Pointer(pIter+40)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pIter+40)).Fn), aCopy, uint64(int64(p)-int64(aCopy)), libc.X__builtin_object_size(tls, (*TFts5Buffer)(unsafe.Pointer(pIter+40)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pIter+40)).Fn), 0)) p2 = pIter + 40 + 8 *(*int32)(unsafe.Pointer(p2)) = int32(int64(*(*int32)(unsafe.Pointer(p2))) + (int64(p) - int64(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 } v3 = p p++ aCopy = v3 v4 = p p++ *(*int32)(unsafe.Pointer(bp)) = int32(*(*Tu8)(unsafe.Pointer(v4))) 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) { (*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 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+40) _fts5SegiterPoslist(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex, pSeg, uintptr(0), pIter+40) (*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+40) _fts5SegiterPoslist(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex, pSeg, (*TFts5Iter)(unsafe.Pointer(pIter)).FpColset, pIter+40) (*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 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(int64(aOut) - int64((*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 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 + 60 _sqlite3Fts5BufferZero(tls, pIter+40) _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+40) _fts5SegiterPoslist(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex, pSeg, pColset, pIter+40) (*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 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 { if (*TFts5Config)(unsafe.Pointer(pConfig)).FnCol <= int32(100) { (*TFts5Iter)(unsafe.Pointer(pIter)).FxSetOutputs = __ccgo_fp(_fts5IterSetOutputs_Col100) _sqlite3Fts5BufferSize(tls, pRc, pIter+40, 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 + 104 + uintptr(iEq)*128 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 + 104 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*128 (*(*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 /* Allocate space for the new multi-seg-iterator. */ if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { if iLevel < 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)*16))).FnSeg < nSegment { v1 = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLevel)*16))).FnSeg } else { v1 = nSegment } nSeg = v1 } } v2 = _fts5MultiIterAlloc(tls, p, nSeg) pNew = v2 *(*uintptr)(unsafe.Pointer(ppOut)) = v2 if pNew == uintptr(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+60, 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)*16 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 + 104 + uintptr(v3)*128 _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 + 104 + uintptr(v6)*128 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 += 16 } } else { pLvl = pStruct + 32 + uintptr(iLevel)*16 iSeg = nSeg - int32(1) for { if !(iSeg >= 0) { break } v8 = iIter iIter++ _fts5SegIterInit(tls, p, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg+uintptr(iSeg)*56, pNew+104+uintptr(v8)*128) goto _7 _7: ; iSeg-- } } } /* 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: ; 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 + 104 + 1*128 (*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+112)) (*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 + 8)) |= 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) { 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) { return (*(*TFts5SegIter)(unsafe.Pointer(pIter + 104 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*128))).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 + 104 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*128 *(*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.X__builtin___memset_chk(tls, bp, 0, uint64(252), libc.X__builtin_object_size(tls, bp, 0)) iLvl = 0 for { if !(iLvl < (*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel) { break } iSeg = 0 for { if !(iSeg < (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*16))).FnSeg) { break } iId = (*(*TFts5StructureSegment)(unsafe.Pointer((*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*16))).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 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 { _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(32)*uint64(nLvl)) if aDlidx == uintptr(0) { (*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } else { nByte = uint64(32) * uint64(nLvl-(*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx) libc.X__builtin___memset_chk(tls, aDlidx+uintptr((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx)*32, 0, nByte, libc.X__builtin_object_size(tls, aDlidx+uintptr((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx)*32, 0)) (*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 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 + 1664 } 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 _sqlite3Fts5BufferAppendVarint(tls, p+60, 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+60, pPage+8, uint32(nTerm+int32(FTS5_DATA_PADDING)+(*TFts5Buffer)(unsafe.Pointer(pPage+8)).Fn)) } } /* TODO1: Updating pgidx here. */ *(*int32)(unsafe.Pointer(pPgidx + 8)) += _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 + 8 } } else { nPrefix = _fts5PrefixCompress(tls, nMin, (*TFts5PageWriter)(unsafe.Pointer(pPage)).Fterm.Fp, pTerm) _sqlite3Fts5BufferAppendVarint(tls, p+60, pPage+8, int64(nPrefix)) } /* Append the number of bytes of new data, then the term data itself ** to the page. */ _sqlite3Fts5BufferAppendVarint(tls, p+60, pPage+8, int64(nTerm)-int64(nPrefix)) _sqlite3Fts5BufferAppendBlob(tls, p+60, pPage+8, uint32(nTerm-nPrefix), pTerm+uintptr(nPrefix)) /* Update the Fts5PageWriter.term field. */ _sqlite3Fts5BufferSet(tls, p+60, pPage+40, nTerm, pTerm) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstTermInPage = uint8(0) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInPage = uint8(0) (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FbFirstRowidInDoclist = uint8(1) (*(*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 + 8 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+60, pPage+8, iRowid) } else { _sqlite3Fts5BufferAppendVarint(tls, p+60, 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 + 8 a = aData n = nData 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+60, pPage+8, uint32(nCopy), a) a += uintptr(nCopy) n -= nCopy _fts5WriteFlushLeaf(tls, p, pWriter) } if n > 0 { _sqlite3Fts5BufferAppendBlob(tls, p+60, 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 + 8 if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { 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+40) _sqlite3Fts5BufferFree(tls, pLeaf+8) _sqlite3Fts5BufferFree(tls, pLeaf+24) _sqlite3Fts5BufferFree(tls, pWriter+96) 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.X__builtin___memset_chk(tls, pWriter, 0, uint64(120), libc.X__builtin_object_size(tls, pWriter, 0)) (*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) /* Grow the two buffers to pgsz + padding bytes in size. */ _sqlite3Fts5BufferSize(tls, p+60, pWriter+8+24, uint32(nBuffer)) _sqlite3Fts5BufferSize(tls, p+60, pWriter+8+8, uint32(nBuffer)) if (*TFts5Index)(unsafe.Pointer(p)).FpIdxWriter == uintptr(0) { pConfig = (*TFts5Index)(unsafe.Pointer(p)).FpConfig _fts5IndexPrepareStmt(tls, p, p+96, Xsqlite3_mprintf(tls, __ccgo_ts+37045, 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.X__builtin___memset_chk(tls, (*TFts5SegWriter)(unsafe.Pointer(pWriter)).Fwriter.Fbuf.Fp, 0, uint64(4), libc.X__builtin_object_size(tls, (*TFts5SegWriter)(unsafe.Pointer(pWriter)).Fwriter.Fbuf.Fp, 0)) (*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(32) defer tls.Free(32) var i, iId, iOff, nDiff int32 var iLeafRowid Ti64 var pData, pSeg uintptr var _ /* aHdr at bp+16 */ [4]Tu8 var _ /* buf at bp+0 */ TFts5Buffer _, _, _, _, _, _, _ = i, iId, iLeafRowid, iOff, nDiff, pData, pSeg libc.X__builtin___memset_chk(tls, bp, 0, uint64(16), libc.X__builtin_object_size(tls, bp, 0)) i = 0 for { if !(i < (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg && (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK) { break } pSeg = pIter + 104 + uintptr(i)*128 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 + 16)) = [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)) _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)) + 104 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaFirst + 1*4))).FiFirst)*128 pTerm = _fts5MultiIterTerm(tls, *(*uintptr)(unsafe.Pointer(bp)), bp+144) if v4 = *(*int32)(unsafe.Pointer(bp + 144)) != (*(*TFts5Buffer)(unsafe.Pointer(bp + 128))).Fn; !v4 { if *(*int32)(unsafe.Pointer(bp + 144)) <= 0 { v3 = 0 } else { v3 = libc.Xmemcmp(tls, pTerm, (*(*TFts5Buffer)(unsafe.Pointer(bp + 128))).Fp, uint64(*(*int32)(unsafe.Pointer(bp + 144)))) } } if v4 || v3 != 0 { if pnRem != 0 && (*(*TFts5SegWriter)(unsafe.Pointer(bp + 8))).FnLeafWritten > nRem { break } _sqlite3Fts5BufferSet(tls, p+60, bp+128, *(*int32)(unsafe.Pointer(bp + 144)), 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 + 144)), 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+60, bp+8+8+8, libc.Int64FromInt32(0)) if (*TFts5SegIter)(unsafe.Pointer(pSegIter)).FnPos > 0 { _sqlite3Fts5BufferAppendVarint(tls, p+60, bp+8+8+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+60, bp+8+8+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) if _fts5MultiIterEof(tls, p, *(*uintptr)(unsafe.Pointer(bp))) != 0 { /* Remove the redundant segments from the %_data table */ 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(uint64((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnSeg-nInput) * uint64(56)) libc.X__builtin___memmove_chk(tls, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg+uintptr(nInput)*56, uint64(nMove), libc.X__builtin_object_size(tls, (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg, 0)) } *(*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 { _fts5TrimSegments(tls, p, *(*uintptr)(unsafe.Pointer(bp))) (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FnMerge = nInput } _fts5MultiIterFree(tls, *(*uintptr)(unsafe.Pointer(bp))) _sqlite3Fts5BufferFree(tls, bp+128) 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)*16 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++ } 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+8 */ uintptr _, _, _, _, _ = bRet, iBestLvl, iLvl, nBest, pLvl *(*int32)(unsafe.Pointer(bp)) = nPg bRet = 0 *(*uintptr)(unsafe.Pointer(bp + 8)) = *(*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. */ iLvl = 0 for { if !(iLvl < (*TFts5Structure)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FnLevel) { break } pLvl = *(*uintptr)(unsafe.Pointer(bp + 8)) + 32 + uintptr(iLvl)*16 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 + 8))) } if iBestLvl < 0 { break } bRet = int32(1) _fts5IndexMergeLevel(tls, p, bp+8, iBestLvl, bp) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*(*TFts5StructureLevel)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)) + 32 + uintptr(iBestLvl)*16))).FnMerge == 0 { _fts5StructurePromote(tls, p, iBestLvl+int32(1), *(*uintptr)(unsafe.Pointer(bp + 8))) } if nMin == int32(1) { nMin = int32(2) } } *(*uintptr)(unsafe.Pointer(ppStruct)) = *(*uintptr)(unsafe.Pointer(bp + 8)) 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)*16))).FnSeg >= nCrisis { _fts5IndexMergeLevel(tls, p, bp, iLvl, uintptr(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 { FpIdx uintptr Fwriter TFts5SegWriter } type Fts5FlushCtx = TFts5FlushCtx type TFts5FlushCtx1 = struct { FpIdx uintptr 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) { if (*TFts5Index)(unsafe.Pointer(p)).FpDeleteFromIdx == uintptr(0) { _fts5IndexPrepareStmt(tls, p, p+136, Xsqlite3_mprintf(tls, __ccgo_ts+37102, 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) *(*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.X__builtin___memcpy_chk(tls, aPg+uintptr(nPg), aIdx, uint64(nIdx), libc.X__builtin_object_size(tls, aPg+uintptr(nPg), 0)) nPg += nIdx } Xsqlite3_free(tls, aIdx) /* Write the new page to disk and exit the loop */ _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+60, int64(nIdx+int32(16))) if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { return } libc.X__builtin___memcpy_chk(tls, aIdx, aPg+uintptr(iPgIdx), uint64(nIdx), libc.X__builtin_object_size(tls, aIdx, 0)) /* 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)) 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) } 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++ } /* 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.X__builtin___memcpy_chk(tls, aPg+uintptr(iOff), (*TFts5SegIter)(unsafe.Pointer(pSeg)).Fterm.Fp+uintptr(*(*int32)(unsafe.Pointer(bp + 36))), uint64(*(*int32)(unsafe.Pointer(bp + 44))-*(*int32)(unsafe.Pointer(bp + 36))), libc.X__builtin_object_size(tls, aPg+uintptr(iOff), 0)) iOff += *(*int32)(unsafe.Pointer(bp + 44)) - *(*int32)(unsafe.Pointer(bp + 36)) } } libc.X__builtin___memmove_chk(tls, aPg+uintptr(iOff), aPg+uintptr(iNextOff), uint64(*(*int32)(unsafe.Pointer(bp + 48))), libc.X__builtin_object_size(tls, aPg+uintptr(iOff), 0)) iOff += *(*int32)(unsafe.Pointer(bp + 48)) iNextOff += *(*int32)(unsafe.Pointer(bp + 48)) } } } else { if iStart == int32(4) { /* 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.X__builtin___memmove_chk(tls, (*TFts5Data)(unsafe.Pointer(pTerm)).Fp+uintptr(iTermOff), (*TFts5Data)(unsafe.Pointer(pTerm)).Fp+uintptr((*TFts5Data)(unsafe.Pointer(pTerm)).FszLeaf), uint64(nTermIdx), libc.X__builtin_object_size(tls, (*TFts5Data)(unsafe.Pointer(pTerm)).Fp+uintptr(iTermOff), 0)) _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.X__builtin___memmove_chk(tls, aPg+uintptr(iOff), aPg+uintptr(iNextOff), uint64(nMove), libc.X__builtin_object_size(tls, aPg+uintptr(iOff), 0)) 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) } _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 + 160))+int32(1) { /* The entire doclist will fit on the current leaf. */ libc.X__builtin___memcpy_chk(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), *(*uintptr)(unsafe.Pointer(bp + 152)), uint64(*(*int32)(unsafe.Pointer(bp + 160))), libc.X__builtin_object_size(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), 0)) *(*int32)(unsafe.Pointer(pBuf + 8)) += *(*int32)(unsafe.Pointer(bp + 160)) } 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 + 160)) { *(*Tu64)(unsafe.Pointer(bp + 168)) = uint64(0) iOff += int32(_sqlite3Fts5GetVarint(tls, *(*uintptr)(unsafe.Pointer(bp + 152))+uintptr(iOff), bp+168)) iRowid = Ti64(uint64(iRowid) + *(*Tu64)(unsafe.Pointer(bp + 168))) /* 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 + 160)) && int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 152)) + uintptr(iOff)))) == 0x00 { _fts5FlushSecureDelete(tls, p, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 136)), *(*int32)(unsafe.Pointer(bp + 144)), iRowid) iOff++ if iOff < *(*int32)(unsafe.Pointer(bp + 160)) && int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 152)) + uintptr(iOff)))) == 0x00 { iOff++ *(*int32)(unsafe.Pointer(bp + 160)) = 0 } else { continue } } } else { if int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 152)) + uintptr(iOff))))&int32(0x01) != 0 { _fts5FlushSecureDelete(tls, p, *(*uintptr)(unsafe.Pointer(bp)), *(*uintptr)(unsafe.Pointer(bp + 136)), *(*int32)(unsafe.Pointer(bp + 144)), iRowid) if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK || int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 152)) + uintptr(iOff)))) == int32(0x01) { iOff++ continue } } } } if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && bTermWritten == 0 { _fts5WriteAppendTerm(tls, p, bp+16, *(*int32)(unsafe.Pointer(bp + 144)), *(*uintptr)(unsafe.Pointer(bp + 136))) bTermWritten = int32(1) } if (*(*TFts5SegWriter)(unsafe.Pointer(bp + 16))).FbFirstRowidInPage != 0 { _fts5PutU16(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp, uint16((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)) /* first rowid on page */ *(*int32)(unsafe.Pointer(pBuf + 8)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), uint64(iRowid)) (*(*TFts5SegWriter)(unsafe.Pointer(bp + 16))).FbFirstRowidInPage = uint8(0) _fts5WriteDlidxAppend(tls, p, bp+16, iRowid) } else { iRowidDelta = uint64(iRowid) - uint64(iPrev) *(*int32)(unsafe.Pointer(pBuf + 8)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), iRowidDelta) } if (*TFts5Index)(unsafe.Pointer(p)).Frc != SQLITE_OK { break } iPrev = iRowid if eDetail == int32(FTS5_DETAIL_NONE) { if iOff < *(*int32)(unsafe.Pointer(bp + 160)) && int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 152)) + uintptr(iOff)))) == 0 { v2 = pBuf + 8 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 + 160)) && int32(*(*Tu8)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 152)) + uintptr(iOff)))) == 0 { v4 = pBuf + 8 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+16) } } else { *(*int32)(unsafe.Pointer(bp + 176)) = 0 *(*int32)(unsafe.Pointer(bp + 180)) = 0 nCopy = _fts5GetPoslistSize(tls, *(*uintptr)(unsafe.Pointer(bp + 152))+uintptr(iOff), bp+180, bp+176) if *(*int32)(unsafe.Pointer(bp + 176)) != 0 && bSecureDelete != 0 { _sqlite3Fts5BufferAppendVarint(tls, p+60, pBuf, int64(*(*int32)(unsafe.Pointer(bp + 180)))*int64(2)) iOff += nCopy nCopy = *(*int32)(unsafe.Pointer(bp + 180)) } else { nCopy += *(*int32)(unsafe.Pointer(bp + 180)) } 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.X__builtin___memcpy_chk(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), *(*uintptr)(unsafe.Pointer(bp + 152))+uintptr(iOff), uint64(nCopy), libc.X__builtin_object_size(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), 0)) *(*int32)(unsafe.Pointer(pBuf + 8)) += 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 + 152)) + 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.X__builtin___memcpy_chk(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), pPoslist+uintptr(iPos), uint64(n), libc.X__builtin_object_size(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), 0)) *(*int32)(unsafe.Pointer(pBuf + 8)) += n iPos += n if (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn+(*TFts5Buffer)(unsafe.Pointer(pPgidx)).Fn >= pgsz { _fts5WriteFlushLeaf(tls, p, bp+16) } if iPos >= nCopy { break } } } iOff += nCopy } } } /* TODO2: Doclist terminator written here. */ /* pBuf->p[pBuf->n++] = '\0'; */ if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { _sqlite3Fts5HashScanNext(tls, pHash) } } _fts5WriteFinish(tls, p, bp+16, bp+8) if *(*int32)(unsafe.Pointer(bp + 8)) > 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+60, bp) } _fts5StructureExtendLevel(tls, p+60, *(*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 + 8)) 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 + 8))+(*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 { _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)*16))).FnSeg nMerge = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(i)*16))).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)*16))).FaSeg))).FnPgTombstone == 0 { return uintptr(0) } _fts5StructureRef(tls, pStruct) return pStruct } goto _1 _1: ; i++ } nByte = Tsqlite3_int64(uint64(nByte) + uint64((*TFts5Structure)(unsafe.Pointer(pStruct)).FnLevel+libc.Int32FromInt32(1))*libc.Uint64FromInt64(16)) pNew = _sqlite3Fts5MallocZero(tls, p+60, nByte) if pNew != 0 { nByte = int64(uint64(nSeg) * uint64(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))*16 (*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg = _sqlite3Fts5MallocZero(tls, p+60, 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)*16))).FnSeg) { break } *(*TFts5StructureSegment)(unsafe.Pointer((*TFts5StructureLevel)(unsafe.Pointer(pLvl)).FaSeg + uintptr(iSegOut)*56)) = *(*TFts5StructureSegment)(unsafe.Pointer((*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*16))).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+8 */ int32 var _ /* pNew at bp+0 */ uintptr _, _ = iLvl, pStruct *(*uintptr)(unsafe.Pointer(bp)) = uintptr(0) _fts5IndexFlush(tls, p) pStruct = _fts5StructureRead(tls, p) _fts5StructureInvalidate(tls, p) if pStruct != 0 { *(*uintptr)(unsafe.Pointer(bp)) = _fts5IndexOptimizeStruct(tls, p, pStruct) } _fts5StructureRelease(tls, pStruct) if *(*uintptr)(unsafe.Pointer(bp)) != 0 { iLvl = 0 for { if !((*(*TFts5StructureLevel)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)) + 32 + uintptr(iLvl)*16))).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)*16))).FnSeg > 0 { *(*int32)(unsafe.Pointer(bp + 8)) = int32(FTS5_OPT_WORK_UNIT) _fts5IndexMergeLevel(tls, p, bp, iLvl, bp+8) } _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+60, 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) 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+60, pBuf, uint32(nByte+(*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn)) } } if v2 && 0 == v1 { *(*int32)(unsafe.Pointer(pBuf + 8)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), iDelta) *(*int32)(unsafe.Pointer(pBuf + 8)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), uint64(nData*libc.Int32FromInt32(2))) libc.X__builtin___memcpy_chk(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), (*TFts5Iter)(unsafe.Pointer(pMulti)).Fbase.FpData, uint64(nData), libc.X__builtin_object_size(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), 0)) *(*int32)(unsafe.Pointer(pBuf + 8)) += nData libc.X__builtin___memset_chk(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), 0, uint64(FTS5_DATA_ZERO_PADDING), libc.X__builtin_object_size(tls, (*TFts5Buffer)(unsafe.Pointer(pBuf)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(pBuf)).Fn), 0)) } } 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) 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.X__builtin___memset_chk(tls, pIter, 0, uint64(32), libc.X__builtin_object_size(tls, pIter, 0)) 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.X__builtin___memset_chk(tls, bp+24, 0, uint64(16), libc.X__builtin_object_size(tls, bp+24, 0)) _sqlite3Fts5BufferSize(tls, p+60, 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))) { *(*int32)(unsafe.Pointer(bp + 24 + 8)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp+24)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+24)).Fn), uint64(*(*Ti64)(unsafe.Pointer(bp + 8))-iOut)) iOut = *(*Ti64)(unsafe.Pointer(bp + 8)) _fts5NextRowid(tls, p1, bp, bp+8) } else { *(*int32)(unsafe.Pointer(bp + 24 + 8)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp+24)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+24)).Fn), uint64(*(*Ti64)(unsafe.Pointer(bp + 16))-iOut)) 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 { Fiter TFts5DoclistIter FiPos Ti64 FiOff int32 FaPos uintptr FpNext uintptr } type PrefixMerger = TPrefixMerger type TPrefixMerger1 = struct { Fiter TFts5DoclistIter FiPos Ti64 FiOff int32 FaPos uintptr FpNext uintptr } 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)) + 56 } (*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)) + 56 } (*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(1072) defer tls.Free(1072) 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+1064 */ Ti64 var _ /* out at bp+1032 */ TFts5Buffer var _ /* pHead at bp+1024 */ uintptr var _ /* tmp at bp+1048 */ TFts5Buffer _, _, _, _, _, _, _, _, _, _, _, _ = i, iLastRowid, nMerge, nOut, nTail, nTmp, pI, pNext, pSave, pThis, pThis1, pX *(*uintptr)(unsafe.Pointer(bp + 1024)) = uintptr(0) nOut = 0 *(*TFts5Buffer)(unsafe.Pointer(bp + 1032)) = TFts5Buffer{} *(*TFts5Buffer)(unsafe.Pointer(bp + 1048)) = 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.X__builtin___memset_chk(tls, bp, 0, uint64(64)*uint64(nBuf+libc.Int32FromInt32(1)), libc.X__builtin_object_size(tls, bp, 0)) *(*uintptr)(unsafe.Pointer(bp + 1024)) = bp + uintptr(nBuf)*64 _fts5DoclistIterInit(tls, p1, *(*uintptr)(unsafe.Pointer(bp + 1024))) i = 0 for { if !(i < nBuf) { break } _fts5DoclistIterInit(tls, aBuf+uintptr(i)*16, bp+uintptr(i)*64) _fts5PrefixMergerInsertByRowid(tls, bp+1024, bp+uintptr(i)*64) nOut += (*(*TFts5Buffer)(unsafe.Pointer(aBuf + uintptr(i)*16))).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+60, bp+1032, uint32(nOut)) != 0 { return } for *(*uintptr)(unsafe.Pointer(bp + 1024)) != 0 { *(*int32)(unsafe.Pointer(bp + 1032 + 8)) += _sqlite3Fts5PutVarint(tls, (*TFts5Buffer)(unsafe.Pointer(bp+1032)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+1032)).Fn), uint64((*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 1024)))).Fiter.FiRowid)-uint64(iLastRowid)) iLastRowid = (*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 1024)))).Fiter.FiRowid if (*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 1024)))).FpNext != 0 && iLastRowid == (*TPrefixMerger)(unsafe.Pointer((*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 1024)))).FpNext)).Fiter.FiRowid { /* Merge data from two or more poslists */ *(*Ti64)(unsafe.Pointer(bp + 1064)) = 0 nTmp = int32(FTS5_DATA_ZERO_PADDING) nMerge = 0 pSave = *(*uintptr)(unsafe.Pointer(bp + 1024)) pThis = uintptr(0) nTail = 0 *(*uintptr)(unsafe.Pointer(bp + 1024)) = 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+1024, pSave) pSave = pNext } if *(*uintptr)(unsafe.Pointer(bp + 1024)) == uintptr(0) || (*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 1024)))).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.X__builtin___memcpy_chk(tls, (*TFts5Buffer)(unsafe.Pointer(bp+1032)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+1032)).Fn), (*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 1024)))).FaPos+uintptr((*TPrefixMerger)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 1024)))).FiOff), uint64(nTail), libc.X__builtin_object_size(tls, (*TFts5Buffer)(unsafe.Pointer(bp+1032)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+1032)).Fn), 0)) *(*int32)(unsafe.Pointer(bp + 1032 + 8)) += nTail } *(*uintptr)(unsafe.Pointer(bp + 1024)) = pSave i = 0 for { if !(i < nBuf+int32(1)) { break } pX = bp + uintptr(i)*64 if (*TPrefixMerger)(unsafe.Pointer(pX)).Fiter.FaPoslist != 0 && (*TPrefixMerger)(unsafe.Pointer(pX)).Fiter.FiRowid == iLastRowid { _fts5DoclistIterNext(tls, pX) _fts5PrefixMergerInsertByRowid(tls, bp+1024, pX) } goto _2 _2: ; i++ } } else { /* Copy poslist from pHead to output */ pThis1 = *(*uintptr)(unsafe.Pointer(bp + 1024)) pI = pThis1 libc.X__builtin___memcpy_chk(tls, (*TFts5Buffer)(unsafe.Pointer(bp+1032)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+1032)).Fn), (*TFts5DoclistIter)(unsafe.Pointer(pI)).FaPoslist, uint64((*TFts5DoclistIter)(unsafe.Pointer(pI)).FnPoslist+(*TFts5DoclistIter)(unsafe.Pointer(pI)).FnSize), libc.X__builtin_object_size(tls, (*TFts5Buffer)(unsafe.Pointer(bp+1032)).Fp+uintptr((*TFts5Buffer)(unsafe.Pointer(bp+1032)).Fn), 0)) *(*int32)(unsafe.Pointer(bp + 1032 + 8)) += (*TFts5DoclistIter)(unsafe.Pointer(pI)).FnPoslist + (*TFts5DoclistIter)(unsafe.Pointer(pI)).FnSize _fts5DoclistIterNext(tls, pI) *(*uintptr)(unsafe.Pointer(bp + 1024)) = (*TPrefixMerger)(unsafe.Pointer(pThis1)).FpNext _fts5PrefixMergerInsertByRowid(tls, bp+1024, pThis1) } } _sqlite3Fts5BufferFree(tls, p1) _sqlite3Fts5BufferFree(tls, bp+1048) libc.X__builtin___memset_chk(tls, (*(*TFts5Buffer)(unsafe.Pointer(bp + 1032))).Fp+uintptr((*(*TFts5Buffer)(unsafe.Pointer(bp + 1032))).Fn), 0, uint64(FTS5_DATA_ZERO_PADDING), libc.X__builtin_object_size(tls, (*(*TFts5Buffer)(unsafe.Pointer(bp + 1032))).Fp+uintptr((*(*TFts5Buffer)(unsafe.Pointer(bp + 1032))).Fn), 0)) *(*TFts5Buffer)(unsafe.Pointer(p1)) = *(*TFts5Buffer)(unsafe.Pointer(bp + 1032)) } 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+24 */ int32 var _ /* doclist at bp+8 */ TFts5Buffer var _ /* dummy at bp+28 */ 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(uint64(16)*uint64(nBuf))) pStruct = _fts5StructureRead(tls, p) 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 + 24)) = int32(1) libc.X__builtin___memset_chk(tls, bp+8, 0, uint64(16), libc.X__builtin_object_size(tls, bp+8, 0)) /* 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 + 28)) = 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+60, *(*uintptr)(unsafe.Pointer(bp))) for { if !(_fts5MultiIterEof(tls, p, *(*uintptr)(unsafe.Pointer(bp))) == 0) { break } pSeg = *(*uintptr)(unsafe.Pointer(bp)) + 104 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaFirst + 1*4))).FiFirst)*128 (*(*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+8) iLastRowid = (*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fbase.FiRowid } goto _1 _1: ; _fts5MultiIterNext2(tls, p, *(*uintptr)(unsafe.Pointer(bp)), bp+28) } _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+60, *(*uintptr)(unsafe.Pointer(bp))) for { if !(_fts5MultiIterEof(tls, p, *(*uintptr)(unsafe.Pointer(bp))) == 0) { break } pSeg1 = *(*uintptr)(unsafe.Pointer(bp)) + 104 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaFirst + 1*4))).FiFirst)*128 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) if *(*int32)(unsafe.Pointer(bp + 24)) != 0 { if nTerm < nToken || libc.Xmemcmp(tls, pToken, pTerm, uint64(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 + 8))).Fn > 0 { i = 0 for { if !((*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK && (*(*TFts5Buffer)(unsafe.Pointer(bp + 8))).Fn != 0) { break } i1 = i * nMerge iStore = i1 for { if !(iStore < i1+nMerge) { break } if (*(*TFts5Buffer)(unsafe.Pointer(aBuf + uintptr(iStore)*16))).Fn == 0 { _fts5BufferSwap(tls, bp+8, aBuf+uintptr(iStore)*16) _sqlite3Fts5BufferZero(tls, bp+8) break } goto _4 _4: ; iStore++ } if iStore == i1+nMerge { (*(*func(*libc.TLS, uintptr, uintptr, int32, uintptr))(unsafe.Pointer(&struct{ uintptr }{xMerge})))(tls, p, bp+8, nMerge, aBuf+uintptr(i1)*16) iStore = i1 for { if !(iStore < i1+nMerge) { break } _sqlite3Fts5BufferZero(tls, aBuf+uintptr(iStore)*16) 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+8) iLastRowid = (*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).Fbase.FiRowid goto _2 _2: ; _fts5MultiIterNext2(tls, p, *(*uintptr)(unsafe.Pointer(bp)), bp+24) } 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+8, nMerge, aBuf+uintptr(i)*16) } iFree = i for { if !(iFree < i+nMerge) { break } _sqlite3Fts5BufferFree(tls, aBuf+uintptr(iFree)*16) goto _7 _7: ; iFree++ } goto _6 _6: ; i += nMerge } _fts5MultiIterFree(tls, *(*uintptr)(unsafe.Pointer(bp))) pData = _fts5IdxMalloc(tls, p, int64(uint64(16)+uint64((*(*TFts5Buffer)(unsafe.Pointer(bp + 8))).Fn)+uint64(FTS5_DATA_ZERO_PADDING))) if pData != 0 { (*TFts5Data)(unsafe.Pointer(pData)).Fp = pData + 1*16 v8 = (*(*TFts5Buffer)(unsafe.Pointer(bp + 8))).Fn (*TFts5Data)(unsafe.Pointer(pData)).FszLeaf = v8 (*TFts5Data)(unsafe.Pointer(pData)).Fnn = v8 if (*(*TFts5Buffer)(unsafe.Pointer(bp + 8))).Fn != 0 { libc.X__builtin___memcpy_chk(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp, (*(*TFts5Buffer)(unsafe.Pointer(bp + 8))).Fp, uint64((*(*TFts5Buffer)(unsafe.Pointer(bp + 8))).Fn), libc.X__builtin_object_size(tls, (*TFts5Data)(unsafe.Pointer(pData)).Fp, 0)) } _fts5MultiIterNew2(tls, p, pData, bDesc, ppIter) } _sqlite3Fts5BufferFree(tls, bp+8) } _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) { /* 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+24, p+32) } /* 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) { _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.X__builtin___memset_chk(tls, bp, 0, uint64(48), libc.X__builtin_object_size(tls, bp, 0)) 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+1664, 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(168)) 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+37163, 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+26282, __ccgo_ts+37171, 0, pzErr) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5CreateTable(tls, pConfig, __ccgo_ts+12266, __ccgo_ts+37206, int32(1), pzErr) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5IndexReinit(tls, p) } } } 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 { _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(*(*int8)(unsafe.Pointer(p + uintptr(v2))))) >= int32(0xc0) { if n >= nByte { return 0 } for int32(*(*int8)(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(*(*int8)(unsafe.Pointer(pIn + uintptr(v1))))) >= int32(0xc0) { for i < nIn && int32(*(*int8)(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 /* Add the entry to the main terms index. */ rc = _sqlite3Fts5HashWrite(tls, (*TFts5Index)(unsafe.Pointer(p)).FpHash, (*TFts5Index)(unsafe.Pointer(p)).FiWriteRowid, iCol, iPos, int8('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, int8(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, uint64(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 { FiRowid Ti64 FiPos Ti64 FiIter int32 } 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(uint64(nAlloc)*uint64(8) + uint64(48)) pNew = Xsqlite3_realloc(tls, pIn, nByte) if pNew == uintptr(0) { (*TFts5Index)(unsafe.Pointer(p)).Frc = int32(SQLITE_NOMEM) } else { if pIn == uintptr(0) { libc.X__builtin___memset_chk(tls, pNew, 0, uint64(nByte), libc.X__builtin_object_size(tls, pNew, 0)) } 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 + 40 + uintptr(v2)*8)) = pAppend } 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 + 40 + uintptr(ii)*8))) 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(uint64(nNew) * uint64(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+60, int64(uint64((*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter)*(libc.Uint64FromInt64(32)+libc.Uint64FromInt64(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)*32 } /* 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 + 40 + uintptr(ii)*8)) 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)*32) nByte += (*TFts5Iter)(unsafe.Pointer(p1)).Fbase.FnData } goto _2 _2: ; ii++ } /* Ensure the output buffer is large enough */ if uint32((*TFts5Buffer)(unsafe.Pointer(pIter+40)).Fn)+uint32(nByte+nHit*libc.Int32FromInt32(10)) <= uint32((*TFts5Buffer)(unsafe.Pointer(pIter+40)).FnSpace) { v4 = 0 } else { v4 = _sqlite3Fts5BufferSize(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex+60, pIter+40, uint32(nByte+nHit*int32(10)+(*TFts5Buffer)(unsafe.Pointer(pIter+40)).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(uint64(nNew)*uint64(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 + 40 + uintptr(ii)*8)) 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 + 104))).FpLeaf == uintptr(0) { _sqlite3Fts5BufferSet(tls, (*TFts5Iter)(unsafe.Pointer(pIter)).FpIndex+60, pIter+104+96, (*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 + 40 + uintptr((*TFts5TokenDataIter)(unsafe.Pointer(pSet)).FnIter-int32(1))*8)) } 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+60, bp, (*TFts5Buffer)(unsafe.Pointer(pSmall)).Fn, (*TFts5Buffer)(unsafe.Pointer(pSmall)).Fp) _sqlite3Fts5BufferAppendBlob(tls, p+60, bp, uint32(1), __ccgo_ts+37250) } else { _sqlite3Fts5BufferSet(tls, p+60, bp, nToken, pToken) } if (*TFts5Index)(unsafe.Pointer(p)).Frc != 0 { _sqlite3Fts5IterClose(tls, pNew) break } pNewIter = pNew + 104 if pPrev != 0 { v2 = pPrev + 104 } 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)*16))).FnSeg - int32(1) for { if !(iSeg >= 0) { break } pSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*16))).FaSeg + uintptr(iSeg)*56 bDone = 0 if pPrevIter != 0 { if _fts5BufferCompare(tls, pSmall, pPrevIter+96) != 0 { libc.X__builtin___memcpy_chk(tls, pNewIter, pPrevIter, uint64(128), libc.X__builtin_object_size(tls, pNewIter, 0)) libc.X__builtin___memset_chk(tls, pPrevIter, 0, uint64(128), libc.X__builtin_object_size(tls, pPrevIter, 0)) 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 += 128 if pPrevIter != 0 { pPrevIter += 128 } 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+60, 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 + 104 + uintptr(ii)*128 if 0 == _fts5IsTokendataPrefix(tls, pII+96, pToken, nToken) { _fts5SegIterSetEOF(tls, pII) } if (*TFts5SegIter)(unsafe.Pointer(pII)).FpLeaf != 0 && (!(pSmall != 0) || _fts5BufferCompare(tls, pSmall, pII+96) > 0) { pSmall = pII + 96 } 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 + 40 + uintptr(ii1)*8)) iSeg1 = 0 for { if !(iSeg1 < (*TFts5Iter)(unsafe.Pointer(pIter)).FnSeg) { break } (*(*TFts5SegIter)(unsafe.Pointer(pIter + 104 + uintptr(iSeg1)*128))).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(32) defer tls.Free(32) var bDesc, bTokendata, iIdx, iPrefixIdx, nChar, nIdxChar int32 var pConfig, pSeg, pStruct uintptr var _ /* buf at bp+8 */ 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 + 8)) = TFts5Buffer{} /* If the QUERY_SCAN flag is set, all other flags must be clear. */ if _sqlite3Fts5BufferSize(tls, p+60, bp+8, 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.X__builtin___memcpy_chk(tls, (*(*TFts5Buffer)(unsafe.Pointer(bp + 8))).Fp+1, pToken, uint64(nToken), libc.X__builtin_object_size(tls, (*(*TFts5Buffer)(unsafe.Pointer(bp + 8))).Fp+1, 0)) } 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 + 8))).Fp)) = uint8('0') *(*uintptr)(unsafe.Pointer(bp)) = _fts5SetupTokendataIter(tls, p, (*(*TFts5Buffer)(unsafe.Pointer(bp + 8))).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 + 8))).Fp)) = uint8(libc.Int32FromUint8('0') + iIdx) if pStruct != 0 { _fts5MultiIterNew(tls, p, pStruct, flags|int32(FTS5INDEX_QUERY_SKIPEMPTY), pColset, (*(*TFts5Buffer)(unsafe.Pointer(bp + 8))).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 + 8))).Fp, nToken+int32(1), pColset, bp) if *(*uintptr)(unsafe.Pointer(bp)) == uintptr(0) { } else { _fts5IterSetOutputCb(tls, p+60, *(*uintptr)(unsafe.Pointer(bp))) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { pSeg = *(*uintptr)(unsafe.Pointer(bp)) + 104 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp)))).FaFirst + 1*4))).FiFirst)*128 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+8) } 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 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 _fts5MultiIterNext(tls, p, pIter, 0, 0) if (*TFts5Index)(unsafe.Pointer(p)).Frc == SQLITE_OK { pSeg = pIter + 104 + uintptr((*(*TFts5CResult)(unsafe.Pointer((*TFts5Iter)(unsafe.Pointer(pIter)).FaFirst + 1*4))).FiFirst)*128 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) *(*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 + 40 + uintptr((*(*TFts5TokenDataMap)(unsafe.Pointer(aMap + uintptr(iTest)*24))).FiIter)*8)) *(*uintptr)(unsafe.Pointer(ppOut)) = (*(*TFts5SegIter)(unsafe.Pointer(pMap + 104))).Fterm.Fp + uintptr(1) *(*int32)(unsafe.Pointer(pnOut)) = (*(*TFts5SegIter)(unsafe.Pointer(pMap + 104))).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 ii = 0 for { if !(ii < (*TFts5TokenDataIter)(unsafe.Pointer(pT)).FnIter) { break } pTerm = *(*uintptr)(unsafe.Pointer(pT + 40 + uintptr(ii)*8)) + 104 + 96 if nToken == (*TFts5Buffer)(unsafe.Pointer(pTerm)).Fn-int32(1) && libc.Xmemcmp(tls, pToken, (*TFts5Buffer)(unsafe.Pointer(pTerm)).Fp+uintptr(1), uint64(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)*8)))).Fp)) = uint8(szKey) _fts5PutU32(tls, (*TFts5Data)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(apOut + uintptr(ii)*8)))).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)) 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 */ apOut = _sqlite3Fts5MallocZero(tls, p+60, int64(uint64(8)*uint64(nOut))) szPage = int32(8) + nSlot*szKey ii = 0 for { if !(ii < nOut) { break } pNew = _sqlite3Fts5MallocZero(tls, p+60, int64(uint64(16)+uint64(szPage))) if pNew != 0 { (*TFts5Data)(unsafe.Pointer(pNew)).Fnn = szPage (*TFts5Data)(unsafe.Pointer(pNew)).Fp = pNew + 1*16 *(*uintptr)(unsafe.Pointer(apOut + uintptr(ii)*8)) = 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. */ _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+8 */ 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 + 8)) = 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+8) /* 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 + 8)) + uintptr(iRowid%uint64(*(*int32)(unsafe.Pointer(bp))))*8)), 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)*16))).FnSeg - int32(1) for { if !(iSeg >= 0) { break } pSeg = (*(*TFts5StructureLevel)(unsafe.Pointer(pStruct + 32 + uintptr(iLvl)*16))).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 + 44))) > 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+16) if *(*Ti64)(unsafe.Pointer(bp + 16)) < iDlRowid || bSecureDelete == 0 && *(*Ti64)(unsafe.Pointer(bp + 16)) != iDlRowid { (*TFts5Index)(unsafe.Pointer(p)).Frc = libc.Int32FromInt32(SQLITE_CORRUPT) | libc.Int32FromInt32(1)<> libc.Int32FromInt32(32)) iTokOff = int32(*(*Ti64)(unsafe.Pointer(bp + 32)) & libc.Int64FromInt32(0x7FFFFFFF)) cksum2 ^= _sqlite3Fts5IndexEntryCksum(tls, iRowid, iCol, iTokOff, -int32(1), z, *(*int32)(unsafe.Pointer(bp + 24))) } } goto _3 _3: ; _fts5MultiIterNext(tls, p, *(*uintptr)(unsafe.Pointer(bp + 16)), 0, 0) } _fts5MultiIterFree(tls, *(*uintptr)(unsafe.Pointer(bp + 16))) 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 { FpStmt uintptr FiRowid Ti64 FaPoslist uintptr FnIdx int32 FaIdx [1]int32 } 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 { Fbase Tsqlite3_vtab_cursor FpNext uintptr FaColumnSize uintptr 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+8 */ 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 + 8)) = 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(72)) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5ConfigParse(tls, pGlobal, db, argc, azConfig, bp+8, pzErr) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpConfig = *(*uintptr)(unsafe.Pointer(bp + 8)) (*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 + 8)), bCreate, pTab+32, pzErr) } /* Open the storage sub-system */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5StorageOpen(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.FpIndex, bCreate, pTab+40, pzErr) } /* Call sqlite3_declare_vtab() */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { *(*int32)(unsafe.Pointer(bp)) = _sqlite3Fts5ConfigDeclareVtab(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) } /* Load the initial configuration */ if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { (*TFts5Config)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).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 + 8)))).FpzErrmsg = uintptr(0) } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK && (*TFts5Config)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(bp + 8)))).FeContent == FTS5_CONTENT_NORMAL { *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_vtab_config(tls, db, int32(SQLITE_VTAB_CONSTRAINT_SUPPORT), libc.VaList(bp+24, 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 + 80)) |= int32(SQLITE_INDEX_SCAN_UNIQUE) } func _fts5UsePatternMatch(tls *libc.TLS, pConfig uintptr, p uintptr) (r int32) { 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 if (*TFts5Config)(unsafe.Pointer(pConfig)).FbLock != 0 { (*TFts5Table)(unsafe.Pointer(pTab)).Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+37343, 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) *(*int8)(unsafe.Pointer(idxStr + uintptr(iIdxStr))) = 0 return SQLITE_OK } else { if iCol == nCol+int32(1) { if bSeenRank != 0 { goto _1 } v2 = iIdxStr iIdxStr++ *(*int8)(unsafe.Pointer(idxStr + uintptr(v2))) = int8('r') bSeenRank = int32(1) } else { if iCol >= 0 { bSeenMatch = int32(1) v3 = iIdxStr iIdxStr++ *(*int8)(unsafe.Pointer(idxStr + uintptr(v3))) = int8('M') Xsqlite3_snprintf(tls, int32(6), idxStr+uintptr(iIdxStr), __ccgo_ts+5550, libc.VaList(bp+8, iCol)) idxStr += uintptr(libc.Xstrlen(tls, idxStr+uintptr(iIdxStr))) } } 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 { v5 = iIdxStr iIdxStr++ if int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(FTS5_PATTERN_LIKE) { v6 = int32('L') } else { v6 = int32('G') } *(*int8)(unsafe.Pointer(idxStr + uintptr(v5))) = int8(v6) Xsqlite3_snprintf(tls, int32(6), idxStr+uintptr(iIdxStr), __ccgo_ts+5550, 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 } else { if bSeenEq == 0 && int32((*Tsqlite3_index_constraint)(unsafe.Pointer(p)).Fop) == int32(SQLITE_INDEX_CONSTRAINT_EQ) && iCol < 0 { v8 = iIdxStr iIdxStr++ *(*int8)(unsafe.Pointer(idxStr + uintptr(v8))) = int8('=') 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++ *(*int8)(unsafe.Pointer(idxStr + uintptr(v11))) = int8('<') 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++ *(*int8)(unsafe.Pointer(idxStr + uintptr(v13))) = int8('>') 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++ } } *(*int8)(unsafe.Pointer(idxStr + uintptr(iIdxStr))) = int8('\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(uint64(184) + uint64((*TFts5Config)(unsafe.Pointer(pConfig)).FnCol)*uint64(4)) pCsr = Xsqlite3_malloc64(tls, uint64(nByte)) if pCsr != 0 { pGlobal = (*TFts5FullTable)(unsafe.Pointer(pTab)).FpGlobal libc.X__builtin___memset_chk(tls, pCsr, 0, uint64(nByte), libc.X__builtin_object_size(tls, pCsr, 0)) (*TFts5Cursor)(unsafe.Pointer(pCsr)).FaColumnSize = pCsr + 1*184 (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpNext = (*TFts5Global)(unsafe.Pointer(pGlobal)).FpCsr (*TFts5Global)(unsafe.Pointer(pGlobal)).FpCsr = pCsr v2 = pGlobal + 40 *(*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 + 80)) |= 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.X__builtin___memset_chk(tls, pCsr+32, 0, uint64(184)-uint64(int64(pCsr+32)-int64(pCsr)), libc.X__builtin_object_size(tls, pCsr+32, 0)) } // 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 + 72 for { if !(*(*uintptr)(unsafe.Pointer(pp)) != pCsr) { break } goto _1 _1: ; pp = *(*uintptr)(unsafe.Pointer(pp)) + 8 } *(*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 + 80)) |= 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 + 28 + uintptr(i)*4)) = iOff goto _2 _2: ; i++ } *(*int32)(unsafe.Pointer(pSorter + 28 + uintptr(i)*4)) = int32(t__predefined_ptrdiff_t(aBlob+uintptr(nBlob)) - int64(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 + 80)) |= 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 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 + 80)) &= ^libc.Int32FromInt32(FTS5CSR_REQUIRE_RESEEK) _fts5CsrNewrow(tls, pCsr) if _sqlite3Fts5ExprEof(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr) != 0 { *(*int32)(unsafe.Pointer(pCsr + 80)) |= 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 /* 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 + 80)) |= _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 + 80)) |= 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 + 80)) |= 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+3863, 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+3863, 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(uint64(32) + uint64(4)*uint64(nPhrase-libc.Int32FromInt32(1))) pSorter = Xsqlite3_malloc64(tls, uint64(nByte)) if pSorter == uintptr(0) { return int32(SQLITE_NOMEM) } libc.X__builtin___memset_chk(tls, pSorter, 0, uint64(nByte), libc.X__builtin_object_size(tls, pSorter, 0)) (*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 + 15431 } else { v1 = __ccgo_ts + 1664 } if zRankArgs != 0 { v2 = zRankArgs } else { v2 = __ccgo_ts + 1664 } if bDesc != 0 { v3 = __ccgo_ts + 37382 } else { v3 = __ccgo_ts + 37387 } rc = _fts5PrepareStatement(tls, pSorter, pConfig, __ccgo_ts+37391, 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 { (*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 + 80)) |= 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(*(*int8)(unsafe.Pointer(z))) == int32(' ') { z++ } n = 0 for { if !(*(*int8)(unsafe.Pointer(z + uintptr(n))) != 0 && int32(*(*int8)(unsafe.Pointer(z + uintptr(n)))) != int32(' ')) { break } goto _1 _1: ; n++ } (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan = int32(FTS5_PLAN_SPECIAL) if n == int32(5) && 0 == Xsqlite3_strnicmp(tls, __ccgo_ts+37446, 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+5893, 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+37452, 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+8 */ 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+37480, libc.VaList(bp+24, zRankArgs)) if zSql != 0 { *(*uintptr)(unsafe.Pointer(bp + 8)) = uintptr(0) *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_prepare_v3(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb, zSql, -int32(1), uint32(SQLITE_PREPARE_PERSISTENT), bp+8, uintptr(0)) Xsqlite3_free(tls, zSql) if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { if int32(SQLITE_ROW) == Xsqlite3_step(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FnRankArg = Xsqlite3_column_count(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) nByte = int64(uint64(8) * uint64((*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)*8)) = Xsqlite3_column_value(tls, *(*uintptr)(unsafe.Pointer(bp + 8)), i) goto _1 _1: ; i++ } } (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpRankArgStmt = *(*uintptr)(unsafe.Pointer(bp + 8)) } else { *(*int32)(unsafe.Pointer(bp)) = Xsqlite3_finalize(tls, *(*uintptr)(unsafe.Pointer(bp + 8))) } } } } if *(*int32)(unsafe.Pointer(bp)) == SQLITE_OK { pAux = _fts5FindAuxiliary(tls, pTab, zRank) if pAux == uintptr(0) { (*TFts5FullTable)(unsafe.Pointer(pTab)).Fp.Fbase.FzErrMsg = Xsqlite3_mprintf(tls, __ccgo_ts+37490, libc.VaList(bp+24, 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+8 */ 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 + 8)) = 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+8) } if rc == SQLITE_OK { (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRank = *(*uintptr)(unsafe.Pointer(bp)) (*TFts5Cursor)(unsafe.Pointer(pCsr)).FzRankArgs = *(*uintptr)(unsafe.Pointer(bp + 8)) *(*int32)(unsafe.Pointer(pCsr + 80)) |= 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+37511, libc.VaList(bp+24, 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 + 35443 (*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+37343, 0) return int32(SQLITE_ERROR) } if (*TFts5Cursor)(unsafe.Pointer(pCsr)).FePlan != 0 { _fts5FreeCursorComponents(tls, pCsr) libc.X__builtin___memset_chk(tls, pCsr+32, 0, uint64(184)-uint64(int64(pCsr+32)-int64(pCsr)), libc.X__builtin_object_size(tls, pCsr+32, 0)) } (*TFts5Config)(unsafe.Pointer(pConfig)).FpzErrmsg = pTab + 16 /* Decode the arguments passed through to this function. */ i = 0 for { if !(i < nVal) { break } v2 = iIdxStr iIdxStr++ switch int32(*(*int8)(unsafe.Pointer(idxStr + uintptr(v2)))) { case int32('r'): pRank = *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*8)) case int32('M'): zText = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*8))) if zText == uintptr(0) { zText = __ccgo_ts + 1664 } iCol = 0 for cond := true; cond; cond = int32(*(*int8)(unsafe.Pointer(idxStr + uintptr(iIdxStr)))) >= int32('0') && int32(*(*int8)(unsafe.Pointer(idxStr + uintptr(iIdxStr)))) <= int32('9') { iCol = iCol*int32(10) + (int32(*(*int8)(unsafe.Pointer(idxStr + uintptr(iIdxStr)))) - int32('0')) iIdxStr++ } if int32(*(*int8)(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 + 16 rc = _sqlite3Fts5ExprNew(tls, pConfig, 0, iCol, zText, bp, pzErr) if rc == SQLITE_OK { rc = _sqlite3Fts5ExprAnd(tls, pCsr+64, *(*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(*(*int8)(unsafe.Pointer(idxStr + uintptr(iIdxStr-int32(1))))) == int32('G')) zText1 = Xsqlite3_value_text(tls, *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*8))) iCol = 0 for cond := true; cond; cond = int32(*(*int8)(unsafe.Pointer(idxStr + uintptr(iIdxStr)))) >= int32('0') && int32(*(*int8)(unsafe.Pointer(idxStr + uintptr(iIdxStr)))) <= int32('9') { iCol = iCol*int32(10) + (int32(*(*int8)(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+64, *(*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)*8)) case int32('<'): pRowidLe = *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*8)) default: pRowidGe = *(*uintptr)(unsafe.Pointer(apVal + uintptr(i)*8)) 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. */ 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 { 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+37544, 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+56, pTab+16) if rc == SQLITE_OK { if pRowidEq != uintptr(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) { 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 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 + 16 } else { v1 = uintptr(0) } rc = _sqlite3Fts5StorageStmt(tls, (*TFts5FullTable)(unsafe.Pointer(pTab)).FpStorage, eStmt, pCsr+56, v1) } if rc == SQLITE_OK && (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fcsrflags&int32(FTS5CSR_REQUIRE_CONTENT) != 0 { pTab1 = (*TFts5Cursor)(unsafe.Pointer(pCsr)).Fbase.FpVtab 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 + 80)) &= ^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)<