// Code generated by modernc.org/undup from the per-target sqlite_*.go files; DO NOT EDIT.

//go:build (darwin && amd64) || (darwin && arm64) || (freebsd && amd64) || (linux && amd64) || (linux && loong64) || (netbsd && amd64) || (openbsd && amd64) || (windows && (amd64 || arm64))

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

type TColumn = struct {
	FzCnName  uintptr
	F__ccgo8  uint8
	Faffinity int8
	FszEst    Tu8
	FhName    Tu8
	FiDflt    Tu16
	FcolFlags Tu16
}

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
		FnReg [0]int32
		Fsub  [0]struct {
			FiAddr     int32
			FregReturn int32
		}
		FpTab uintptr
	}
}

// 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)(__ccgo_up(zName + uintptr(-libc.Int32FromInt32(1))))) != 0 || int32(**(**int8)(__ccgo_up(zName + uintptr(-libc.Int32FromInt32(2))))) != 0 || int32(**(**int8)(__ccgo_up(zName + uintptr(-libc.Int32FromInt32(3))))) != 0 || int32(**(**int8)(__ccgo_up(zName + uintptr(-libc.Int32FromInt32(4))))) != 0 {
		zName = zName - 1
	}
	p = zName - uintptr(4) - uintptr(8)
	pPager = **(**uintptr)(__ccgo_up(p))
	return (*TPager)(unsafe.Pointer(pPager)).Ffd
}

// 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
//
//	/*
//	** 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
//
//	/*
//	** 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 = ii + 1
	}
	return 0
}

// 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 = ii + 1
	}
	return int32(1)
}

// 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
}

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)) = *(*Tu64)(unsafe.Pointer(v2)) + 1
		**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v1))) = c
	}
}

// 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)) = *(*Tu64)(unsafe.Pointer(v2)) + 1
	**(**int8)(__ccgo_up((*TJsonString)(unsafe.Pointer(p)).FzBuf + uintptr(v1))) = c
}

// 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)(__ccgo_up(argv + uintptr(i)*8)))
		goto _1
	_1:
		;
		i = i + 1
	}
	_jsonAppendChar(tls, bp, int8(']'))
	_jsonReturnString(tls, bp, uintptr(0), uintptr(0))
	Xsqlite3_result_subtype(tls, ctx, uint32(JSON_SUBTYPE))
}

// 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)(__ccgo_up(argv)))
	}
}

// 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 + 88
	**(**uintptr)(__ccgo_up(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)(__ccgo_up(p + 312)) += 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)(__ccgo_up(bp + 8)) = uintptr(0)
					**(**Ti64)(__ccgo_up(p + 312)) -= int64((*Tsqlite3rbu)(unsafe.Pointer(p)).Fobjiter.FnIndex)
					_rbuGetUpdateStmt(tls, p, pIter, **(**uintptr)(__ccgo_up(bp)), bp+8)
					if **(**uintptr)(__ccgo_up(bp + 8)) != 0 {
						i = 0
						for {
							if !((*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK && i < (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol) {
								break
							}
							c = **(**int8)(__ccgo_up(**(**uintptr)(__ccgo_up(bp)) + uintptr(**(**int32)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FaiSrcOrder + uintptr(i)*4)))))
							pVal = Xsqlite3_column_value(tls, (*TRbuObjIter)(unsafe.Pointer(pIter)).FpSelect, i)
							if **(**Tu8)(__ccgo_up((*TRbuObjIter)(unsafe.Pointer(pIter)).FabTblPk + uintptr(i))) != 0 || int32(c) != int32('.') {
								(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = Xsqlite3_bind_value(tls, **(**uintptr)(__ccgo_up(bp + 8)), i+int32(1), pVal)
							}
							goto _1
						_1:
							;
							i = i + 1
						}
						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)(__ccgo_up(bp + 8)), (*TRbuObjIter)(unsafe.Pointer(pIter)).FnCol+int32(1), pVal)
						}
						if (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc == SQLITE_OK {
							Xsqlite3_step(tls, **(**uintptr)(__ccgo_up(bp + 8)))
							(*Tsqlite3rbu)(unsafe.Pointer(p)).Frc = _resetAndCollectError(tls, **(**uintptr)(__ccgo_up(bp + 8)), p+64)
						}
					}
				}
			}
		}
	}
	return (*Tsqlite3rbu)(unsafe.Pointer(p)).Frc
}

// 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(736)
	defer tls.Free(736)
	var _ /* acc at bp+0 */ TStrAccum
	var _ /* zMsg at bp+32 */ [700]int8 /* Complete log message */
	_sqlite3StrAccumInit(tls, bp, uintptr(0), bp+32, int32(700), 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
//
//	/*
//	** 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)(__ccgo_up(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)).FpSTab
		if (*TTable)(unsafe.Pointer(pTab)).FtabFlags&uint32(TF_Ephemeral) != uint32(0) && int32(*(*uint32)(unsafe.Pointer(pFrom + 24 + 4))&0x4>>2) != 0 {
			/* A sub-query in the FROM clause of a SELECT */
			pSel = (*TSubquery)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(pFrom + 72)))).FpSelect
			_sqlite3SubqueryColumnTypes(tls, pParse, pTab, pSel, int8(SQLITE_AFF_NONE))
		}
		goto _1
	_1:
		;
		i = i + 1
		pFrom += 80
	}
}

// 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 uintptr
	_, _, _, _, _ = nStr, zIn, zOut, v1, v2
	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 = zOut + 1
		**(**int8)(__ccgo_up(v1)) = int8('"')
		if zIn != uintptr(0) {
			for **(**int8)(__ccgo_up(zIn)) != 0 {
				if int32(**(**int8)(__ccgo_up(zIn))) == int32('"') {
					v1 = zOut
					zOut = zOut + 1
					**(**int8)(__ccgo_up(v1)) = int8('"')
				}
				v1 = zOut
				zOut = zOut + 1
				v2 = zIn
				zIn = zIn + 1
				**(**int8)(__ccgo_up(v1)) = **(**int8)(__ccgo_up(v2))
			}
		}
		v1 = zOut
		zOut = zOut + 1
		**(**int8)(__ccgo_up(v1)) = int8('"')
		(*TSessionBuffer)(unsafe.Pointer(p)).FnBuf = int32(int64(zOut) - int64((*TSessionBuffer)(unsafe.Pointer(p)).FaBuf))
		**(**Tu8)(__ccgo_up((*TSessionBuffer)(unsafe.Pointer(p)).FaBuf + uintptr((*TSessionBuffer)(unsafe.Pointer(p)).FnBuf))) = uint8(0x00)
	}
}

// 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)(__ccgo_up(aDef + uintptr(i)*72))).FzName
		nName = _sqlite3Strlen30(tls, zName)
		h = (int32(**(**int8)(__ccgo_up(zName))) + nName) % int32(SQLITE_FUNC_HASH_SZ)
		pOther = _sqlite3FunctionSearch(tls, h, zName)
		if pOther != 0 {
			(**(**TFuncDef)(__ccgo_up(aDef + uintptr(i)*72))).FpNext = (*TFuncDef)(unsafe.Pointer(pOther)).FpNext
			(*TFuncDef)(unsafe.Pointer(pOther)).FpNext = aDef + uintptr(i)*72
		} else {
			(**(**TFuncDef)(__ccgo_up(aDef + uintptr(i)*72))).FpNext = uintptr(0)
			*(*uintptr)(unsafe.Pointer(aDef + uintptr(i)*72 + 64)) = **(**uintptr)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)) + uintptr(h)*8))
			**(**uintptr)(__ccgo_up(uintptr(unsafe.Pointer(&_sqlite3BuiltinFunctions)) + uintptr(h)*8)) = aDef + uintptr(i)*72
		}
		goto _1
	_1:
		;
		i = i + 1
	}
}

// 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.<rowid-alias>". 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)(__ccgo_up(zSpan + uintptr(n))) != 0 && int32(**(**int8)(__ccgo_up(zSpan + uintptr(n)))) != int32('.')) {
			break
		}
		goto _1
	_1:
		;
		n = n + 1
	}
	if zDb != 0 && (Xsqlite3_strnicmp(tls, zSpan, zDb, n) != 0 || int32(**(**int8)(__ccgo_up(zDb + uintptr(n)))) != 0) {
		return 0
	}
	zSpan = zSpan + uintptr(n+int32(1))
	n = 0
	for {
		if !(**(**int8)(__ccgo_up(zSpan + uintptr(n))) != 0 && int32(**(**int8)(__ccgo_up(zSpan + uintptr(n)))) != int32('.')) {
			break
		}
		goto _2
	_2:
		;
		n = n + 1
	}
	if zTab != 0 && (Xsqlite3_strnicmp(tls, zSpan, zTab, n) != 0 || int32(**(**int8)(__ccgo_up(zTab + uintptr(n)))) != 0) {
		return 0
	}
	zSpan = 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)(__ccgo_up(pbRowid)) = int32(1)
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
//	*/
func _sqlite3Prepare16(tls *libc.TLS, db uintptr, zSql uintptr, nBytes int32, prepFlags Tu32, ppStmt uintptr, pzTail uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var chars_parsed, rc, sz, sz1 int32
	var z, z1, zSql8 uintptr
	var _ /* zTail8 at bp+0 */ uintptr
	_, _, _, _, _, _, _ = chars_parsed, rc, sz, sz1, z, z1, zSql8
	**(**uintptr)(__ccgo_up(bp)) = uintptr(0)
	rc = SQLITE_OK
	**(**uintptr)(__ccgo_up(ppStmt)) = uintptr(0)
	if !(_sqlite3SafetyCheckOk(tls, db) != 0) || zSql == uintptr(0) {
		return _sqlite3MisuseError(tls, int32(148771))
	}
	/* Make sure nBytes is non-negative and correct.  It should be the
	 ** number of bytes until the end of the input buffer or until the first
	 ** U+0000 character.  If the input nBytes is odd, convert it into
	 ** an even number.  If the input nBytes is negative, then the input
	 ** must be terminated by at least one U+0000 character */
	if nBytes >= 0 {
		z = zSql
		sz = 0
		for {
			if !(sz < nBytes && (int32(**(**int8)(__ccgo_up(z + uintptr(sz)))) != 0 || int32(**(**int8)(__ccgo_up(z + uintptr(sz+int32(1))))) != 0)) {
				break
			}
			goto _1
		_1:
			;
			sz = sz + int32(2)
		}
		nBytes = sz
	} else {
		z1 = zSql
		sz1 = 0
		for {
			if !(int32(**(**int8)(__ccgo_up(z1 + uintptr(sz1)))) != 0 || int32(**(**int8)(__ccgo_up(z1 + uintptr(sz1+int32(1))))) != 0) {
				break
			}
			goto _2
		_2:
			;
			sz1 = sz1 + int32(2)
		}
		nBytes = sz1
	}
	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)(__ccgo_up(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)(__ccgo_up(bp)))-int64(zSql8)))
		**(**uintptr)(__ccgo_up(pzTail)) = zSql + uintptr(_sqlite3Utf16ByteLen(tls, zSql, nBytes, chars_parsed))
	}
	_sqlite3DbFree(tls, db, zSql8)
	rc = _sqlite3ApiExit(tls, db, rc)
	Xsqlite3_mutex_leave(tls, (*Tsqlite3)(unsafe.Pointer(db)).Fmutex)
	return rc
}

// 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)(__ccgo_up(db + 48)) &= ^libc.Uint64FromInt32(SQLITE_FullColNames)
	**(**Tu64)(__ccgo_up(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(120))
	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
//
//	/*
//	** 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)(__ccgo_up((*TTable)(unsafe.Pointer(pTab)).FaCol + uintptr(iCol)*16))).Faffinity
}

func _unicodeSetCategories(tls *libc.TLS, p uintptr, zCat uintptr) (r int32) {
	var z uintptr
	_ = z
	z = zCat
	for **(**int8)(__ccgo_up(z)) != 0 {
		for int32(**(**int8)(__ccgo_up(z))) == int32(' ') || int32(**(**int8)(__ccgo_up(z))) == int32('\t') {
			z = z + 1
		}
		if **(**int8)(__ccgo_up(z)) != 0 && _sqlite3Fts5UnicodeCatParse(tls, z, p+160) != 0 {
			return int32(SQLITE_ERROR)
		}
		for int32(**(**int8)(__ccgo_up(z))) != int32(' ') && int32(**(**int8)(__ccgo_up(z))) != int32('\t') && int32(**(**int8)(__ccgo_up(z))) != int32('\000') {
			z = z + 1
		}
	}
	_sqlite3Fts5UnicodeAscii(tls, p+160, p)
	return SQLITE_OK
}

// 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 v1 uintptr
	_ = v1
	if _sqlite3VdbeMemGrow(tls, pMem, (*TMem)(unsafe.Pointer(pMem)).Fn+int32(3), int32(1)) != 0 {
		return int32(SQLITE_NOMEM)
	}
	**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn))) = 0
	**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn+int32(1)))) = 0
	**(**int8)(__ccgo_up((*TMem)(unsafe.Pointer(pMem)).Fz + uintptr((*TMem)(unsafe.Pointer(pMem)).Fn+int32(2)))) = 0
	v1 = pMem + 20
	*(*Tu16)(unsafe.Pointer(v1)) = Tu16(int32(*(*Tu16)(unsafe.Pointer(v1))) | libc.Int32FromInt32(MEM_Term))
	return SQLITE_OK
}

// 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 <op> <expr>" 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 <op> 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 <op> <expr>".  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)(__ccgo_up(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)(__ccgo_up((*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)(__ccgo_up((*TTable)(unsafe.Pointer((*TIndex)(unsafe.Pointer(pIdx)).FpTable)).FaCol + uintptr(iColumn)*16))).Faffinity
				(*TWhereScan)(unsafe.Pointer(pScan)).FzCollName = **(**uintptr)(__ccgo_up((*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)(__ccgo_up((*TIndex)(unsafe.Pointer(pIdx)).FazColl + uintptr(j)*8))
					**(**Ti16)(__ccgo_up(pScan + 88)) = int16(-libc.Int32FromInt32(2))
					return _whereScanInitIndexExpr(tls, pScan)
				}
			}
		}
	} else {
		if iColumn == -int32(2) {
			return uintptr(0)
		}
	}
	**(**Ti16)(__ccgo_up(pScan + 88)) = int16(iColumn)
	return _whereScanNext(tls, pScan)
}
