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

//go:build (darwin && amd64) || (darwin && arm64) || (linux && amd64) || (linux && arm64) || (linux && ppc64le) || (linux && riscv64) || (linux && s390x)

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

// 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)(__ccgo_up(pazResult)) = uintptr(0)
	if pnColumn != 0 {
		**(**int32)(__ccgo_up(pnColumn)) = 0
	}
	if pnRow != 0 {
		**(**int32)(__ccgo_up(pnRow)) = 0
	}
	if pzErrMsg != 0 {
		**(**uintptr)(__ccgo_up(pzErrMsg)) = uintptr(0)
	}
	(**(**TTabResult)(__ccgo_up(bp))).FzErrMsg = uintptr(0)
	(**(**TTabResult)(__ccgo_up(bp))).FnRow = uint32(0)
	(**(**TTabResult)(__ccgo_up(bp))).FnColumn = uint32(0)
	(**(**TTabResult)(__ccgo_up(bp))).FnData = uint32(1)
	(**(**TTabResult)(__ccgo_up(bp))).FnAlloc = uint32(20)
	(**(**TTabResult)(__ccgo_up(bp))).Frc = SQLITE_OK
	(**(**TTabResult)(__ccgo_up(bp))).FazResult = Xsqlite3_malloc64(tls, uint64(8)*uint64((**(**TTabResult)(__ccgo_up(bp))).FnAlloc))
	if (**(**TTabResult)(__ccgo_up(bp))).FazResult == uintptr(0) {
		(*Tsqlite3)(unsafe.Pointer(db)).FerrCode = int32(SQLITE_NOMEM)
		return int32(SQLITE_NOMEM)
	}
	**(**uintptr)(__ccgo_up((**(**TTabResult)(__ccgo_up(bp))).FazResult)) = uintptr(0)
	rc = Xsqlite3_exec(tls, db, zSql, __ccgo_fp(_sqlite3_get_table_cb), bp, pzErrMsg)
	**(**uintptr)(__ccgo_up((**(**TTabResult)(__ccgo_up(bp))).FazResult)) = uintptr(libc.Int64FromUint32((**(**TTabResult)(__ccgo_up(bp))).FnData))
	if rc&int32(0xff) == int32(SQLITE_ABORT) {
		Xsqlite3_free_table(tls, (**(**TTabResult)(__ccgo_up(bp))).FazResult+1*8)
		if (**(**TTabResult)(__ccgo_up(bp))).FzErrMsg != 0 {
			if pzErrMsg != 0 {
				Xsqlite3_free(tls, **(**uintptr)(__ccgo_up(pzErrMsg)))
				**(**uintptr)(__ccgo_up(pzErrMsg)) = Xsqlite3_mprintf(tls, __ccgo_ts+3944, libc.VaList(bp+48, (**(**TTabResult)(__ccgo_up(bp))).FzErrMsg))
			}
			Xsqlite3_free(tls, (**(**TTabResult)(__ccgo_up(bp))).FzErrMsg)
		}
		(*Tsqlite3)(unsafe.Pointer(db)).FerrCode = (**(**TTabResult)(__ccgo_up(bp))).Frc /* Assume 32-bit assignment is atomic */
		return (**(**TTabResult)(__ccgo_up(bp))).Frc
	}
	Xsqlite3_free(tls, (**(**TTabResult)(__ccgo_up(bp))).FzErrMsg)
	if rc != SQLITE_OK {
		Xsqlite3_free_table(tls, (**(**TTabResult)(__ccgo_up(bp))).FazResult+1*8)
		return rc
	}
	if (**(**TTabResult)(__ccgo_up(bp))).FnAlloc > (**(**TTabResult)(__ccgo_up(bp))).FnData {
		azNew = _sqlite3Realloc(tls, (**(**TTabResult)(__ccgo_up(bp))).FazResult, uint64(8)*uint64((**(**TTabResult)(__ccgo_up(bp))).FnData))
		if azNew == uintptr(0) {
			Xsqlite3_free_table(tls, (**(**TTabResult)(__ccgo_up(bp))).FazResult+1*8)
			(*Tsqlite3)(unsafe.Pointer(db)).FerrCode = int32(SQLITE_NOMEM)
			return int32(SQLITE_NOMEM)
		}
		(**(**TTabResult)(__ccgo_up(bp))).FazResult = azNew
	}
	**(**uintptr)(__ccgo_up(pazResult)) = (**(**TTabResult)(__ccgo_up(bp))).FazResult + 1*8
	if pnColumn != 0 {
		**(**int32)(__ccgo_up(pnColumn)) = libc.Int32FromUint32((**(**TTabResult)(__ccgo_up(bp))).FnColumn)
	}
	if pnRow != 0 {
		**(**int32)(__ccgo_up(pnRow)) = libc.Int32FromUint32((**(**TTabResult)(__ccgo_up(bp))).FnRow)
	}
	return rc
}

// 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)(__ccgo_up(argv))), __ccgo_ts+3944, libc.VaList(bp+8, Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(argv + 1*8)))))
}

// 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)(__ccgo_up(bp)) = 0
		v1 = _fts5CursorReseek(tls, pCsr, bp)
		rc = v1
		if v1 != 0 || **(**int32)(__ccgo_up(bp)) != 0 {
			return rc
		}
		rc = _sqlite3Fts5ExprNext(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpExpr, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FiLastRowid)
		**(**int32)(__ccgo_up(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)(__ccgo_up(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 = (*TFts5Config)(unsafe.Pointer(pConfig)).FbLock + 1
			rc = Xsqlite3_step(tls, (*TFts5Cursor)(unsafe.Pointer(pCsr)).FpStmt)
			(*TFts5Config)(unsafe.Pointer(pConfig)).FbLock = (*TFts5Config)(unsafe.Pointer(pConfig)).FbLock - 1
			if rc != int32(SQLITE_ROW) {
				**(**int32)(__ccgo_up(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+3944, libc.VaList(bp+16, Xsqlite3_errmsg(tls, (*TFts5Config)(unsafe.Pointer(pConfig)).Fdb)))
				}
			} else {
				rc = SQLITE_OK
				**(**int32)(__ccgo_up(pCsr + 80)) |= int32(FTS5CSR_REQUIRE_DOCSIZE)
			}
			break
		}
	}
	return rc
}

// 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)(__ccgo_up(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(120))
	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)(__ccgo_up((*Tsqlite3)(unsafe.Pointer(db)).FaDb))).FpSchema
	(*TTable)(unsafe.Pointer(pTab)).FiPKey = int16(-int32(1))
	**(**Tu32)(__ccgo_up(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))
	(*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock = (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock + 1
	rc = _vtabCallConstructor(tls, db, pTab, pMod, (*Tsqlite3_module)(unsafe.Pointer(pModule)).FxConnect, bp)
	(*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock = (*Tsqlite3)(unsafe.Pointer(db)).FnSchemaLock - 1
	if rc != 0 {
		_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+3944, libc.VaList(bp+16, **(**uintptr)(__ccgo_up(bp))))
		(*TParse)(unsafe.Pointer(pParse)).Frc = rc
		_sqlite3DbFree(tls, db, **(**uintptr)(__ccgo_up(bp)))
		_sqlite3VtabEponymousTableClear(tls, db, pMod)
	}
	return int32(1)
}

// C documentation
//
//	/*
//	** The table object reference passed as the second argument to this function
//	** must represent a virtual table. This function invokes the xBestIndex()
//	** method of the virtual table with the sqlite3_index_info object that
//	** comes in as the 3rd argument to this function.
//	**
//	** If an error occurs, pParse is populated with an error message and an
//	** appropriate error code is returned.  A return of SQLITE_CONSTRAINT from
//	** xBestIndex is not considered an error.  SQLITE_CONSTRAINT indicates that
//	** the current configuration of "unusable" flags in sqlite3_index_info can
//	** not result in a valid plan.
//	**
//	** Whether or not an error is returned, it is the responsibility of the
//	** caller to eventually free p->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 = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FnSchemaLock + 1
	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 = (*Tsqlite3)(unsafe.Pointer((*TParse)(unsafe.Pointer(pParse)).Fdb)).FnSchemaLock - 1
	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+3944, libc.VaList(bp+8, _sqlite3ErrStr(tls, rc)))
			} else {
				_sqlite3ErrorMsg(tls, pParse, __ccgo_ts+3944, 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
}
