fn wrap_and_escape(&mutself, s: &str, quote: char) { self.buf.push(quote); let chars = s.chars(); for ch in chars { // escape `quote` by doubling it if ch == quote { self.buf.push(ch);
} self.buf.push(ch);
} self.buf.push(quote);
}
}
impl Deref for Sql { type Target = str;
fn deref(&self) -> &str { self.as_str()
}
}
impl Connection { /// Query the current value of `pragma_name`. /// /// Some pragmas will return multiple rows/values which cannot be retrieved /// with this method. /// /// Prefer [PRAGMA function](https://sqlite.org/pragma.html#pragfunc) introduced in SQLite 3.20: /// `SELECT user_version FROM pragma_user_version;` pubfn pragma_query_value<T, F>(
&self,
schema_name: Option<DatabaseName<'_>>,
pragma_name: &str,
f: F,
) -> Result<T> where
F: FnOnce(&Row<'_>) -> Result<T>,
{ letmut query = Sql::new();
query.push_pragma(schema_name, pragma_name)?; self.query_row(&query, [], f)
}
/// Query the current rows/values of `pragma_name`. /// /// Prefer [PRAGMA function](https://sqlite.org/pragma.html#pragfunc) introduced in SQLite 3.20: /// `SELECT * FROM pragma_collation_list;` pubfn pragma_query<F>(
&self,
schema_name: Option<DatabaseName<'_>>,
pragma_name: &str, mut f: F,
) -> Result<()> where
F: FnMut(&Row<'_>) -> Result<()>,
{ letmut query = Sql::new();
query.push_pragma(schema_name, pragma_name)?; letmut stmt = self.prepare(&query)?; letmut rows = stmt.query([])?; whilelet Some(result_row) = rows.next()? { let row = result_row;
f(row)?;
}
Ok(())
}
/// Query the current value(s) of `pragma_name` associated to /// `pragma_value`. /// /// This method can be used with query-only pragmas which need an argument /// (e.g. `table_info('one_tbl')`) or pragmas which returns value(s) /// (e.g. `integrity_check`). /// /// Prefer [PRAGMA function](https://sqlite.org/pragma.html#pragfunc) introduced in SQLite 3.20: /// `SELECT * FROM pragma_table_info(?1);` pubfn pragma<F, V>(
&self,
schema_name: Option<DatabaseName<'_>>,
pragma_name: &str,
pragma_value: V, mut f: F,
) -> Result<()> where
F: FnMut(&Row<'_>) -> Result<()>,
V: ToSql,
{ letmut sql = Sql::new();
sql.push_pragma(schema_name, pragma_name)?; // The argument may be either in parentheses // or it may be separated from the pragma name by an equal sign. // The two syntaxes yield identical results.
sql.open_brace();
sql.push_value(&pragma_value)?;
sql.close_brace(); letmut stmt = self.prepare(&sql)?; letmut rows = stmt.query([])?; whilelet Some(result_row) = rows.next()? { let row = result_row;
f(row)?;
}
Ok(())
}
/// Set a new value to `pragma_name`. /// /// Some pragmas will return the updated value which cannot be retrieved /// with this method. pubfn pragma_update<V>(
&self,
schema_name: Option<DatabaseName<'_>>,
pragma_name: &str,
pragma_value: V,
) -> Result<()> where
V: ToSql,
{ letmut sql = Sql::new();
sql.push_pragma(schema_name, pragma_name)?; // The argument may be either in parentheses // or it may be separated from the pragma name by an equal sign. // The two syntaxes yield identical results.
sql.push_equal_sign();
sql.push_value(&pragma_value)?; self.execute_batch(&sql)
}
/// Set a new value to `pragma_name` and return the updated value. /// /// Only few pragmas automatically return the updated value. pubfn pragma_update_and_check<F, T, V>(
&self,
schema_name: Option<DatabaseName<'_>>,
pragma_name: &str,
pragma_value: V,
f: F,
) -> Result<T> where
F: FnOnce(&Row<'_>) -> Result<T>,
V: ToSql,
{ letmut sql = Sql::new();
sql.push_pragma(schema_name, pragma_name)?; // The argument may be either in parentheses // or it may be separated from the pragma name by an equal sign. // The two syntaxes yield identical results.
sql.push_equal_sign();
sql.push_value(&pragma_value)?; self.query_row(&sql, [], f)
}
}
fn is_identifier(s: &str) -> bool { let chars = s.char_indices(); for (i, ch) in chars { if i == 0 { if !is_identifier_start(ch) { returnfalse;
}
} elseif !is_identifier_continue(ch) { returnfalse;
}
} true
}
fn is_identifier_start(c: char) -> bool {
c.is_ascii_uppercase() || c == '_' || c.is_ascii_lowercase() || c > '\x7F'
}
fn is_identifier_continue(c: char) -> bool {
c == '$'
|| c.is_ascii_digit()
|| c.is_ascii_uppercase()
|| c == '_'
|| c.is_ascii_lowercase()
|| c > '\x7F'
}
#[cfg(test)] mod test { usesuper::Sql; usecrate::pragma; usecrate::{Connection, DatabaseName, Result};
#[test] fn pragma_query_value() -> Result<()> { let db = Connection::open_in_memory()?; let user_version: i32 = db.pragma_query_value(None, "user_version", |row| row.get(0))?;
assert_eq!(0, user_version);
Ok(())
}
#[test] #[cfg(feature = "modern_sqlite")] fn pragma_func_query_value() -> Result<()> { let db = Connection::open_in_memory()?; let user_version: i32 = db.one_column("SELECT user_version FROM pragma_user_version")?;
assert_eq!(0, user_version);
Ok(())
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.