mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-23 00:39:20 +08:00
Unify callback parameter signature
This commit is contained in:
parent
7be87e950d
commit
32881d7a76
@ -171,18 +171,11 @@ impl Connection {
|
|||||||
f: F,
|
f: F,
|
||||||
) -> Result<T>
|
) -> Result<T>
|
||||||
where
|
where
|
||||||
F: FnOnce(Row<'_, '_>) -> Result<T>,
|
F: FnOnce(&Row<'_, '_>) -> Result<T>,
|
||||||
{
|
{
|
||||||
let mut query = Sql::new();
|
let mut query = Sql::new();
|
||||||
query.push_pragma(schema_name, pragma_name)?;
|
query.push_pragma(schema_name, pragma_name)?;
|
||||||
let mut stmt = self.prepare(&query)?;
|
self.query_row(&query, NO_PARAMS, f)
|
||||||
let mut rows = stmt.query(NO_PARAMS)?;
|
|
||||||
if let Some(result_row) = rows.next() {
|
|
||||||
let row = result_row?;
|
|
||||||
f(row)
|
|
||||||
} else {
|
|
||||||
Err(Error::QueryReturnedNoRows)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Query the current rows/values of `pragma_name`.
|
/// Query the current rows/values of `pragma_name`.
|
||||||
@ -276,7 +269,7 @@ impl Connection {
|
|||||||
f: F,
|
f: F,
|
||||||
) -> Result<T>
|
) -> Result<T>
|
||||||
where
|
where
|
||||||
F: FnOnce(Row<'_, '_>) -> Result<T>,
|
F: FnOnce(&Row<'_, '_>) -> Result<T>,
|
||||||
{
|
{
|
||||||
let mut sql = Sql::new();
|
let mut sql = Sql::new();
|
||||||
sql.push_pragma(schema_name, pragma_name)?;
|
sql.push_pragma(schema_name, pragma_name)?;
|
||||||
@ -285,14 +278,7 @@ impl Connection {
|
|||||||
// The two syntaxes yield identical results.
|
// The two syntaxes yield identical results.
|
||||||
sql.push_equal_sign();
|
sql.push_equal_sign();
|
||||||
sql.push_value(pragma_value)?;
|
sql.push_value(pragma_value)?;
|
||||||
let mut stmt = self.prepare(&sql)?;
|
self.query_row(&sql, NO_PARAMS, f)
|
||||||
let mut rows = stmt.query(NO_PARAMS)?;
|
|
||||||
if let Some(result_row) = rows.next() {
|
|
||||||
let row = result_row?;
|
|
||||||
f(row)
|
|
||||||
} else {
|
|
||||||
Err(Error::QueryReturnedNoRows)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -333,7 +319,7 @@ mod test {
|
|||||||
fn pragma_query_value() {
|
fn pragma_query_value() {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
let user_version: i32 = db
|
let user_version: i32 = db
|
||||||
.pragma_query_value(None, "user_version", |row| row.get_checked(0))
|
.pragma_query_value(None, "user_version", |row| row.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(0, user_version);
|
assert_eq!(0, user_version);
|
||||||
}
|
}
|
||||||
@ -359,7 +345,7 @@ mod test {
|
|||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
let mut user_version = -1;
|
let mut user_version = -1;
|
||||||
db.pragma_query(None, "user_version", |row| {
|
db.pragma_query(None, "user_version", |row| {
|
||||||
user_version = row.get_checked(0)?;
|
user_version = row.get(0)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -371,7 +357,7 @@ mod test {
|
|||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
let mut user_version = -1;
|
let mut user_version = -1;
|
||||||
db.pragma_query(Some(DatabaseName::Main), "user_version", |row| {
|
db.pragma_query(Some(DatabaseName::Main), "user_version", |row| {
|
||||||
user_version = row.get_checked(0)?;
|
user_version = row.get(0)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -383,7 +369,7 @@ mod test {
|
|||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
let mut columns = Vec::new();
|
let mut columns = Vec::new();
|
||||||
db.pragma(None, "table_info", &"sqlite_master", |row| {
|
db.pragma(None, "table_info", &"sqlite_master", |row| {
|
||||||
let column: String = row.get_checked(1)?;
|
let column: String = row.get(1)?;
|
||||||
columns.push(column);
|
columns.push(column);
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
@ -401,7 +387,7 @@ mod test {
|
|||||||
|
|
||||||
while let Some(row) = rows.next() {
|
while let Some(row) = rows.next() {
|
||||||
let row = row.unwrap();
|
let row = row.unwrap();
|
||||||
let column: String = row.get(1);
|
let column: String = row.get(1).unwrap();
|
||||||
columns.push(column);
|
columns.push(column);
|
||||||
}
|
}
|
||||||
assert_eq!(5, columns.len());
|
assert_eq!(5, columns.len());
|
||||||
@ -417,7 +403,7 @@ mod test {
|
|||||||
fn pragma_update_and_check() {
|
fn pragma_update_and_check() {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = Connection::open_in_memory().unwrap();
|
||||||
let journal_mode: String = db
|
let journal_mode: String = db
|
||||||
.pragma_update_and_check(None, "journal_mode", &"OFF", |row| row.get_checked(0))
|
.pragma_update_and_check(None, "journal_mode", &"OFF", |row| row.get(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!("off", &journal_mode);
|
assert_eq!("off", &journal_mode);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user