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

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

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

// 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(272)
	defer tls.Free(272)
	var db, pCol, pFrom, pNew, pReturning, v, v2 uintptr
	var i, nCol, reg, v1 int32
	var _ /* sNC at bp+208 */ TNameContext
	var _ /* sSelect at bp+0 */ TSelect
	var _ /* uSrc at bp+120 */ struct {
		FfromSpace   [0][88]Tu8
		FsSrc        TSrcList
		F__ccgo_pad2 [80]byte
	}
	_, _, _, _, _, _, _, _, _, _, _ = db, i, nCol, pCol, pFrom, pNew, pReturning, reg, v, v1, v2
	v = (*TParse)(unsafe.Pointer(pParse)).FpVdbe
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	if !(int32(Tbft(*(*uint16)(unsafe.Pointer(pParse + 40))&0x8>>3)) != 0) {
		/* This RETURNING trigger must be for a different statement as
		 ** this statement lacks a RETURNING clause. */
		return
	}
	pReturning = (*(*struct {
		FpReturning uintptr
	})(unsafe.Pointer(&(*TParse)(unsafe.Pointer(pParse)).Fu1))).FpReturning
	if pTrigger != pReturning+16 {
		/* This RETURNING trigger is for a different statement */
		return
	}
	libc.Xmemset(tls, bp, 0, uint64(120))
	libc.Xmemset(tls, bp+120, 0, uint64(88))
	pFrom = bp + 120
	(**(**TSelect)(__ccgo_up(bp))).FpEList = _sqlite3ExprListDup(tls, db, (*TReturning)(unsafe.Pointer(pReturning)).FpReturnEL, 0)
	(**(**TSelect)(__ccgo_up(bp))).FpSrc = pFrom
	(*TSrcList)(unsafe.Pointer(pFrom)).FnSrc = int32(1)
	(*(*TSrcItem)(unsafe.Pointer(pFrom + 8))).FpSTab = pTab
	(*(*TSrcItem)(unsafe.Pointer(pFrom + 8))).FzName = (*TTable)(unsafe.Pointer(pTab)).FzName /* tag-20240424-1 */
	(*(*TSrcItem)(unsafe.Pointer(pFrom + 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)(__ccgo_up(bp))).FpEList)
	pNew = _sqlite3ExpandReturning(tls, pParse, (*TReturning)(unsafe.Pointer(pReturning)).FpReturnEL, pTab)
	if (*TParse)(unsafe.Pointer(pParse)).FnErr == 0 {
		libc.Xmemset(tls, bp+208, 0, uint64(56))
		if (*TReturning)(unsafe.Pointer(pReturning)).FnRetCol == 0 {
			(*TReturning)(unsafe.Pointer(pReturning)).FnRetCol = (*TExprList)(unsafe.Pointer(pNew)).FnExpr
			v2 = pParse + 56
			v1 = *(*int32)(unsafe.Pointer(v2))
			*(*int32)(unsafe.Pointer(v2)) = *(*int32)(unsafe.Pointer(v2)) + 1
			(*TReturning)(unsafe.Pointer(pReturning)).FiRetCur = v1
		}
		(**(**TNameContext)(__ccgo_up(bp + 208))).FpParse = pParse
		*(*int32)(unsafe.Pointer(bp + 208 + 16)) = regIn
		(**(**TNameContext)(__ccgo_up(bp + 208))).FncFlags = int32(NC_UBaseReg)
		(*TParse)(unsafe.Pointer(pParse)).FeTriggerOp = (*TTrigger)(unsafe.Pointer(pTrigger)).Fop
		(*TParse)(unsafe.Pointer(pParse)).FpTriggerTab = pTab
		if _sqlite3ResolveExprListNames(tls, bp+208, 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)(__ccgo_up(pParse + 60)) += 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 = i + 1
			}
			_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
//
//	/*
//	** Process time function arguments.  argv[0] is a date-time stamp.
//	** argv[1] and following are modifiers.  Parse them all and write
//	** the resulting time into the DateTime structure p.  Return 0
//	** on success and 1 if there are any errors.
//	**
//	** If there are zero parameters (if even argv[0] is undefined)
//	** then assume a default value of "now" for argv[0].
//	*/
func _isDate(tls *libc.TLS, context uintptr, argc int32, argv uintptr, p uintptr) (r int32) {
	var eType, i, n, v1 int32
	var z uintptr
	_, _, _, _, _ = eType, i, n, z, v1
	libc.Xmemset(tls, p, 0, uint64(48))
	if argc == 0 {
		if !(_sqlite3NotPureFunc(tls, context) != 0) {
			return int32(1)
		}
		return _setDateTimeToCurrent(tls, context, p)
	}
	v1 = Xsqlite3_value_type(tls, **(**uintptr)(__ccgo_up(argv)))
	eType = v1
	if v1 == int32(SQLITE_FLOAT) || eType == int32(SQLITE_INTEGER) {
		_setRawDateNumber(tls, p, Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(argv))))
	} else {
		z = Xsqlite3_value_text(tls, **(**uintptr)(__ccgo_up(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)(__ccgo_up(argv + uintptr(i)*8)))
		n = Xsqlite3_value_bytes(tls, **(**uintptr)(__ccgo_up(argv + uintptr(i)*8)))
		if z == uintptr(0) || _parseModifier(tls, context, z, n, p, i) != 0 {
			return int32(1)
		}
		goto _2
	_2:
		;
		i = i + 1
	}
	_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
//
//	/*
//	** 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)(__ccgo_up(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 = i + 1
			} else {
				if !(inStr != 0) {
					if int32(c) == int32('{') || int32(c) == int32('[') {
						nNest = nNest + 1
					}
					if int32(c) == int32('}') || int32(c) == int32(']') {
						nNest = nNest - 1
					}
				}
			}
		}
		goto _1
	_1:
		;
		i = i + 1
	}
	if uint64(i) < (*TJsonString)(unsafe.Pointer(pStr)).FnUsed {
		**(**Tu64)(__ccgo_up(pStr + 24)) -= uint64(i)
		libc.Xmemmove(tls, z+1, z+uintptr(i+uint32(1)), (*TJsonString)(unsafe.Pointer(pStr)).FnUsed-uint64(1))
		**(**int8)(__ccgo_up(z + uintptr((*TJsonString)(unsafe.Pointer(pStr)).FnUsed))) = 0
	} else {
		(*TJsonString)(unsafe.Pointer(pStr)).FnUsed = uint64(1)
	}
}

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.Xmemcpy(tls, zNew, z, n)
		**(**int8)(__ccgo_up(zNew + uintptr(n))) = 0
	}
	return zNew
}

// C documentation
//
//	/*
//	** Run the parser and code generator recursively in order to generate
//	** code for the SQL statement given onto the end of the pParse context
//	** currently under construction.  Notes:
//	**
//	**   *  The final OP_Halt is not appended and other initialization
//	**      and finalization steps are omitted because those are handling by the
//	**      outermost parser.
//	**
//	**   *  Built-in SQL functions always take precedence over application-defined
//	**      SQL functions.  In other words, it is not possible to override a
//	**      built-in function.
//	*/
func _sqlite3NestedParse(tls *libc.TLS, pParse uintptr, zFormat uintptr, va uintptr) {
	bp := tls.Alloc(144)
	defer tls.Free(144)
	var ap Tva_list
	var db, zSql uintptr
	var savedDbFlags Tu32
	var _ /* saveBuf at bp+0 */ [136]int8
	_, _, _, _ = ap, db, savedDbFlags, zSql
	db = (*TParse)(unsafe.Pointer(pParse)).Fdb
	savedDbFlags = (*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags
	if (*TParse)(unsafe.Pointer(pParse)).FnErr != 0 {
		return
	}
	if (*TParse)(unsafe.Pointer(pParse)).FeParseMode != 0 {
		return
	}
	/* Nesting should only be of limited depth */
	ap = va
	zSql = _sqlite3VMPrintf(tls, db, zFormat, ap)
	_ = ap
	if zSql == uintptr(0) {
		/* This can result either from an OOM or because the formatted string
		 ** exceeds SQLITE_LIMIT_LENGTH.  In the latter case, we need to set
		 ** an error */
		if !((*Tsqlite3)(unsafe.Pointer(db)).FmallocFailed != 0) {
			(*TParse)(unsafe.Pointer(pParse)).Frc = int32(SQLITE_TOOBIG)
		}
		(*TParse)(unsafe.Pointer(pParse)).FnErr = (*TParse)(unsafe.Pointer(pParse)).FnErr + 1
		return
	}
	(*TParse)(unsafe.Pointer(pParse)).Fnested = (*TParse)(unsafe.Pointer(pParse)).Fnested + 1
	libc.Xmemcpy(tls, bp, pParse+uintptr(uint64(libc.UintptrFromInt32(0)+288)), libc.Uint64FromInt64(424)-uint64(libc.UintptrFromInt32(0)+288))
	libc.Xmemset(tls, pParse+uintptr(uint64(libc.UintptrFromInt32(0)+288)), 0, libc.Uint64FromInt64(424)-uint64(libc.UintptrFromInt32(0)+288))
	**(**Tu32)(__ccgo_up(db + 44)) |= uint32(DBFLAG_PreferBuiltin)
	_sqlite3RunParser(tls, pParse, zSql)
	(*Tsqlite3)(unsafe.Pointer(db)).FmDbFlags = savedDbFlags
	_sqlite3DbFree(tls, db, zSql)
	libc.Xmemcpy(tls, pParse+uintptr(uint64(libc.UintptrFromInt32(0)+288)), bp, libc.Uint64FromInt64(424)-uint64(libc.UintptrFromInt32(0)+288))
	(*TParse)(unsafe.Pointer(pParse)).Fnested = (*TParse)(unsafe.Pointer(pParse)).Fnested - 1
}
