mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-22 16:29:20 +08:00
Make column_name
returns a Result
This commit is contained in:
parent
4ffdb07f31
commit
9ce3b82570
@ -9,14 +9,14 @@ pub struct Column<'stmt> {
|
|||||||
decl_type: Option<&'stmt str>,
|
decl_type: Option<&'stmt str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'stmt> Column<'stmt> {
|
impl Column<'_> {
|
||||||
/// Returns the name of the column.
|
/// Returns the name of the column.
|
||||||
pub fn name(&self) -> &'stmt str {
|
pub fn name(&self) -> &str {
|
||||||
self.name
|
self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the type of the column (`None` for expression).
|
/// Returns the type of the column (`None` for expression).
|
||||||
pub fn decl_type(&self) -> Option<&'stmt str> {
|
pub fn decl_type(&self) -> Option<&str> {
|
||||||
self.decl_type
|
self.decl_type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -27,7 +27,7 @@ impl Statement<'_> {
|
|||||||
let n = self.column_count();
|
let n = self.column_count();
|
||||||
let mut cols = Vec::with_capacity(n as usize);
|
let mut cols = Vec::with_capacity(n as usize);
|
||||||
for i in 0..n {
|
for i in 0..n {
|
||||||
let s = self.column_name(i).unwrap(); // safe to unwrap as we're using only verified indices
|
let s = self.column_name_unwrap(i);
|
||||||
cols.push(s);
|
cols.push(s);
|
||||||
}
|
}
|
||||||
cols
|
cols
|
||||||
@ -39,10 +39,28 @@ impl Statement<'_> {
|
|||||||
self.stmt.column_count()
|
self.stmt.column_count()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the name of the column by index or None on out of bounds access. Panics when column name is not valid UTF-8
|
pub(crate) fn column_name_unwrap(&self, col: usize) -> &str {
|
||||||
pub fn column_name(&self, col: usize) -> Option<&str> {
|
// Just panic if the bounds are wrong for now, we never call this
|
||||||
self.stmt.column_name(col)
|
// without checking first.
|
||||||
.map(|x| str::from_utf8(x.to_bytes()).expect("Invalid UTF-8 sequence in column name"))
|
self.column_name(col).expect("Column out of bounds")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the name assigned to a particular column in the result set
|
||||||
|
/// returned by the prepared statement.
|
||||||
|
///
|
||||||
|
/// ## Failure
|
||||||
|
///
|
||||||
|
/// Returns an `Error::InvalidColumnIndex` if `idx` is outside the valid
|
||||||
|
/// column range for this row.
|
||||||
|
///
|
||||||
|
/// Panics when column name is not valid UTF-8.
|
||||||
|
pub fn column_name(&self, col: usize) -> Result<&str> {
|
||||||
|
self.stmt
|
||||||
|
.column_name(col)
|
||||||
|
.ok_or(Error::InvalidColumnIndex(col))
|
||||||
|
.map(|slice| {
|
||||||
|
str::from_utf8(slice.to_bytes()).expect("Invalid UTF-8 sequence in column name")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the column index in the result set for a given column name.
|
/// Returns the column index in the result set for a given column name.
|
||||||
@ -72,9 +90,11 @@ impl Statement<'_> {
|
|||||||
let n = self.column_count();
|
let n = self.column_count();
|
||||||
let mut cols = Vec::with_capacity(n as usize);
|
let mut cols = Vec::with_capacity(n as usize);
|
||||||
for i in 0..n {
|
for i in 0..n {
|
||||||
let name = self.column_name(i).unwrap(); // safe to unwrap as we're using only verified indices
|
let name = self.column_name_unwrap(i);
|
||||||
let slice = self.stmt.column_decltype(i);
|
let slice = self.stmt.column_decltype(i);
|
||||||
let decl_type = slice.map(|s| str::from_utf8(s.to_bytes()).expect("Invalid UTF-8 sequence in column declaration"));
|
let decl_type = slice.map(|s| {
|
||||||
|
str::from_utf8(s.to_bytes()).expect("Invalid UTF-8 sequence in column declaration")
|
||||||
|
});
|
||||||
cols.push(Column { name, decl_type });
|
cols.push(Column { name, decl_type });
|
||||||
}
|
}
|
||||||
cols
|
cols
|
||||||
@ -83,7 +103,7 @@ impl Statement<'_> {
|
|||||||
|
|
||||||
impl<'stmt> Rows<'stmt> {
|
impl<'stmt> Rows<'stmt> {
|
||||||
/// Get all the column names.
|
/// Get all the column names.
|
||||||
pub fn column_names(&self) -> Option<Vec<&'stmt str>> {
|
pub fn column_names(&self) -> Option<Vec<&str>> {
|
||||||
self.stmt.map(Statement::column_names)
|
self.stmt.map(Statement::column_names)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,24 +113,24 @@ impl<'stmt> Rows<'stmt> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Return the name of the column.
|
/// Return the name of the column.
|
||||||
pub fn column_name(&self, col: usize) -> Option<&'stmt str> {
|
pub fn column_name(&self, col: usize) -> Option<Result<&str>> {
|
||||||
self.stmt.and_then(|x| x.column_name(col))
|
self.stmt.map(|stmt| stmt.column_name(col))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the index of the column.
|
/// Return the index of the column.
|
||||||
pub fn column_index(&self, name: &str) -> Option<Result<usize>> {
|
pub fn column_index(&self, name: &str) -> Option<Result<usize>> {
|
||||||
self.stmt.map(|x| x.column_index(name))
|
self.stmt.map(|stmt| stmt.column_index(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a slice describing the columns of the Rows.
|
/// Returns a slice describing the columns of the Rows.
|
||||||
pub fn columns(&self) -> Option<Vec<Column<'stmt>>> {
|
pub fn columns(&self) -> Option<Vec<Column>> {
|
||||||
self.stmt.map(Statement::columns)
|
self.stmt.map(Statement::columns)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'stmt> Row<'stmt> {
|
impl<'stmt> Row<'stmt> {
|
||||||
/// Get all the column names of the Row.
|
/// Get all the column names of the Row.
|
||||||
pub fn column_names(&self) -> Vec<&'stmt str> {
|
pub fn column_names(&self) -> Vec<&str> {
|
||||||
self.stmt.column_names()
|
self.stmt.column_names()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +140,7 @@ impl<'stmt> Row<'stmt> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Return the name of the column.
|
/// Return the name of the column.
|
||||||
pub fn column_name(&self, col: usize) -> Option<&'stmt str> {
|
pub fn column_name(&self, col: usize) -> Result<&str> {
|
||||||
self.stmt.column_name(col)
|
self.stmt.column_name(col)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,8 +148,9 @@ impl<'stmt> Row<'stmt> {
|
|||||||
pub fn column_index(&self, name: &str) -> Result<usize> {
|
pub fn column_index(&self, name: &str) -> Result<usize> {
|
||||||
self.stmt.column_index(name)
|
self.stmt.column_index(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a slice describing the columns of the Row.
|
/// Returns a slice describing the columns of the Row.
|
||||||
pub fn columns(&self) -> Vec<Column<'stmt>> {
|
pub fn columns(&self) -> Vec<Column> {
|
||||||
self.stmt.columns()
|
self.stmt.columns()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
24
src/row.rs
24
src/row.rs
@ -223,21 +223,27 @@ impl<'stmt> Row<'stmt> {
|
|||||||
let idx = idx.idx(self.stmt)?;
|
let idx = idx.idx(self.stmt)?;
|
||||||
let value = self.stmt.value_ref(idx);
|
let value = self.stmt.value_ref(idx);
|
||||||
FromSql::column_result(value).map_err(|err| match err {
|
FromSql::column_result(value).map_err(|err| match err {
|
||||||
FromSqlError::InvalidType => {
|
FromSqlError::InvalidType => Error::InvalidColumnType(
|
||||||
Error::InvalidColumnType(idx, self.stmt.column_name(idx).unwrap().into(), value.data_type())
|
idx,
|
||||||
}
|
self.stmt.column_name_unwrap(idx).into(),
|
||||||
|
value.data_type(),
|
||||||
|
),
|
||||||
FromSqlError::OutOfRange(i) => Error::IntegralValueOutOfRange(idx, i),
|
FromSqlError::OutOfRange(i) => Error::IntegralValueOutOfRange(idx, i),
|
||||||
FromSqlError::Other(err) => {
|
FromSqlError::Other(err) => {
|
||||||
Error::FromSqlConversionFailure(idx as usize, value.data_type(), err)
|
Error::FromSqlConversionFailure(idx as usize, value.data_type(), err)
|
||||||
}
|
}
|
||||||
#[cfg(feature = "i128_blob")]
|
#[cfg(feature = "i128_blob")]
|
||||||
FromSqlError::InvalidI128Size(_) => {
|
FromSqlError::InvalidI128Size(_) => Error::InvalidColumnType(
|
||||||
Error::InvalidColumnType(idx, self.stmt.column_name(idx).unwrap().into(), value.data_type())
|
idx,
|
||||||
}
|
self.stmt.column_name_unwrap(idx).into(),
|
||||||
|
value.data_type(),
|
||||||
|
),
|
||||||
#[cfg(feature = "uuid")]
|
#[cfg(feature = "uuid")]
|
||||||
FromSqlError::InvalidUuidSize(_) => {
|
FromSqlError::InvalidUuidSize(_) => Error::InvalidColumnType(
|
||||||
Error::InvalidColumnType(idx, self.stmt.column_name(idx).unwrap().into(), value.data_type())
|
idx,
|
||||||
}
|
self.stmt.column_name_unwrap(idx).into(),
|
||||||
|
value.data_type(),
|
||||||
|
),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user