prepare($query); if (!empty($params)) foreach ($params as $key => $value) $stmt->bindValue($key, $value, getSQLiteType($value)); $GLOBALS['queries']++; return $stmt->execute(); } /** * Take a SQLite3 database connection and a query string. Execute the query and return the result. */ function db_exec(SQLite3 $db, string $query): bool { $GLOBALS['queries']++; return $db->exec($query); } /** * Take a SQLite3 database connection, a column name, and a value. Execute a COUNT query to see if the value * exists in the column. Return true if the value exists, false otherwise. */ function db_exists(SQLite3 $db, string $table, string $column, mixed $value): bool { $result = db_query($db, "SELECT 1 FROM $table WHERE $column = :v LIMIT 1", [':v' => $value]); return $result->fetchArray(SQLITE3_NUM) !== false; } /** * Return the appropriate SQLite type casting for the value. */ function getSQLiteType(mixed $value): int { return match (true) { is_int($value) => SQLITE3_INTEGER, is_float($value) => SQLITE3_FLOAT, is_null($value) => SQLITE3_NULL, default => SQLITE3_TEXT }; }