nitr-std 0.0.0-beta.7

The built-in `nitr.*` standard library for Nitr: dbg, fetch, template, json, http, log, crypto, and the SQLite driver.
Documentation
// SPDX-License-Identifier: MIT OR Apache-2.0
// This file is part of Nitr.
// See https://nitrweb.com/ for more information
// Copyright (C) 2024-present Jose Quintana <joseluisq.net>

use rusqlite::{Connection, OptionalExtension as _};

use crate::db::types::{SqlRow, SqlValue, bind, column_names, read_row};

/// Runs a query and returns its first row, or `None` when it produced no
/// rows — the documented `query_row` contract (`nil`, not an error, for an
/// empty result), so `if row then` works as written.
pub(crate) fn call(
    conn: &Connection,
    sql: &str,
    params: &[SqlValue],
    _max_rows: usize,
) -> Result<Option<SqlRow>, rusqlite::Error> {
    let mut stmt = conn.prepare_cached(sql)?;
    let bound = bind(&stmt, params);
    let columns = column_names(&stmt)?;

    stmt.query_row(bound, |row| read_row(&columns, row))
        .optional()
}