Make column_name() public

Changes it's interface to Option<&str> to accommodate for out of bounds index
This commit is contained in:
Pro 2019-08-22 16:37:13 +02:00
parent 692b4b67dc
commit d46eb23364
2 changed files with 9 additions and 10 deletions

View File

@ -27,7 +27,7 @@ impl Statement<'_> {
let n = self.column_count();
let mut cols = Vec::with_capacity(n as usize);
for i in 0..n {
let s = self.column_name(i);
let s = self.column_name(i).unwrap(); // safe to unwrap as we're using only verified indices
cols.push(s);
}
cols
@ -39,11 +39,10 @@ impl Statement<'_> {
self.stmt.column_count()
}
pub(crate) fn column_name(&self, col: usize) -> &str {
// Just panic if the bounds are wrong for now, we never call this
// without checking first.
let slice = self.stmt.column_name(col).expect("Column out of bounds");
str::from_utf8(slice.to_bytes()).unwrap()
/// 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 fn column_name(&self, col: usize) -> Option<&str> {
self.stmt.column_name(col)
.map(|x| str::from_utf8(x.to_bytes()).expect("Invalid UTF-8 sequence in column name"))
}
/// Returns the column index in the result set for a given column name.
@ -73,7 +72,7 @@ impl Statement<'_> {
let n = self.column_count();
let mut cols = Vec::with_capacity(n as usize);
for i in 0..n {
let name = self.column_name(i);
let name = self.column_name(i).unwrap(); // safe to unwrap as we're using only verified indices
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"));
cols.push(Column { name, decl_type });

View File

@ -224,7 +224,7 @@ impl<'stmt> Row<'stmt> {
let value = self.stmt.value_ref(idx);
FromSql::column_result(value).map_err(|err| match err {
FromSqlError::InvalidType => {
Error::InvalidColumnType(idx, self.stmt.column_name(idx).into(), value.data_type())
Error::InvalidColumnType(idx, self.stmt.column_name(idx).unwrap().into(), value.data_type())
}
FromSqlError::OutOfRange(i) => Error::IntegralValueOutOfRange(idx, i),
FromSqlError::Other(err) => {
@ -232,11 +232,11 @@ impl<'stmt> Row<'stmt> {
}
#[cfg(feature = "i128_blob")]
FromSqlError::InvalidI128Size(_) => {
Error::InvalidColumnType(idx, self.stmt.column_name(idx).into(), value.data_type())
Error::InvalidColumnType(idx, self.stmt.column_name(idx).unwrap().into(), value.data_type())
}
#[cfg(feature = "uuid")]
FromSqlError::InvalidUuidSize(_) => {
Error::InvalidColumnType(idx, self.stmt.column_name(idx).into(), value.data_type())
Error::InvalidColumnType(idx, self.stmt.column_name(idx).unwrap().into(), value.data_type())
}
})
}