mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-29 21:00:54 +00:00
refactor: rename ordinal to index
This commit is contained in:
parent
9a31baa15a
commit
d8e4030cb8
@ -8,7 +8,7 @@ pub trait Column {
|
||||
fn name(&self) -> &str;
|
||||
|
||||
/// Returns the (zero-based) position of the column.
|
||||
fn ordinal(&self) -> usize;
|
||||
fn index(&self) -> usize;
|
||||
|
||||
/// Returns type information of the column.
|
||||
fn type_info(&self) -> &<Self::Database as Database>::TypeInfo;
|
||||
|
||||
@ -19,17 +19,17 @@ pub trait Row: 'static + Send + Sync {
|
||||
/// Returns a reference to the columns in the row.
|
||||
fn columns(&self) -> &[<Self::Database as Database>::Column];
|
||||
|
||||
/// Returns the column name, given the ordinal (also known as index) of the column.
|
||||
fn column_name_of(&self, ordinal: usize) -> &str;
|
||||
/// Returns the column name, given the index of the column.
|
||||
fn column_name_of(&self, index: usize) -> &str;
|
||||
|
||||
/// Returns the column name, given the ordinal (also known as index) of the column.
|
||||
fn try_column_name_of(&self, ordinal: usize) -> crate::Result<&str>;
|
||||
/// Returns the column name, given the index of the column.
|
||||
fn try_column_name_of(&self, index: usize) -> crate::Result<&str>;
|
||||
|
||||
/// Returns the column ordinal, given the name of the column.
|
||||
fn ordinal_of(&self, name: &str) -> usize;
|
||||
/// Returns the column index, given the name of the column.
|
||||
fn index_of(&self, name: &str) -> usize;
|
||||
|
||||
/// Returns the column ordinal, given the name of the column.
|
||||
fn try_ordinal_of(&self, name: &str) -> crate::Result<usize>;
|
||||
/// Returns the column index, given the name of the column.
|
||||
fn try_index_of(&self, name: &str) -> crate::Result<usize>;
|
||||
|
||||
/// Returns the decoded value at the index.
|
||||
fn try_get<'r, T, I>(&'r self, index: I) -> crate::Result<T>
|
||||
@ -47,12 +47,12 @@ pub trait Row: 'static + Send + Sync {
|
||||
|
||||
/// A helper trait used for indexing into a [`Row`].
|
||||
pub trait ColumnIndex<R: Row + ?Sized> {
|
||||
/// Returns the ordinal of the column at this index, if present.
|
||||
/// Returns the index of the column at this index, if present.
|
||||
#[allow(clippy::needless_lifetimes)]
|
||||
fn get<'r>(&self, row: &'r R) -> crate::Result<usize>;
|
||||
}
|
||||
|
||||
// access an ordinal by index
|
||||
// access by index
|
||||
impl<R: Row> ColumnIndex<R> for usize {
|
||||
#[allow(clippy::needless_lifetimes)]
|
||||
fn get<'r>(&self, _row: &'r R) -> crate::Result<usize> {
|
||||
@ -62,11 +62,11 @@ impl<R: Row> ColumnIndex<R> for usize {
|
||||
}
|
||||
}
|
||||
|
||||
// access an ordinal by name
|
||||
// access by name
|
||||
impl<R: Row> ColumnIndex<R> for &'_ str {
|
||||
#[allow(clippy::needless_lifetimes)]
|
||||
fn get<'r>(&self, row: &'r R) -> crate::Result<usize> {
|
||||
row.try_ordinal_of(self)
|
||||
row.try_index_of(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -8,15 +8,15 @@ use crate::{MySql, MySqlTypeInfo};
|
||||
#[allow(clippy::module_name_repetitions)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MySqlColumn {
|
||||
ordinal: usize,
|
||||
index: usize,
|
||||
name: ByteString,
|
||||
type_info: MySqlTypeInfo,
|
||||
flags: ColumnFlags,
|
||||
}
|
||||
|
||||
impl MySqlColumn {
|
||||
pub(crate) fn new(ordinal: usize, def: ColumnDefinition) -> Self {
|
||||
Self { type_info: MySqlTypeInfo::new(&def), ordinal, name: def.name, flags: def.flags }
|
||||
pub(crate) fn new(index: usize, def: ColumnDefinition) -> Self {
|
||||
Self { type_info: MySqlTypeInfo::new(&def), index, name: def.name, flags: def.flags }
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,8 +36,8 @@ impl MySqlColumn {
|
||||
}
|
||||
|
||||
/// Returns the (zero-based) position of the column.
|
||||
pub const fn ordinal(&self) -> usize {
|
||||
self.ordinal
|
||||
pub const fn index(&self) -> usize {
|
||||
self.index
|
||||
}
|
||||
|
||||
/// Returns `true` if the column is (or is part of) a `PRIMARY KEY`.
|
||||
@ -69,8 +69,8 @@ impl Column for MySqlColumn {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn ordinal(&self) -> usize {
|
||||
self.ordinal()
|
||||
fn index(&self) -> usize {
|
||||
self.index()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
||||
@ -16,7 +16,7 @@ macro_rules! impl_recv_columns {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
for (ordinal, rem) in (1..=$num_columns).rev().enumerate() {
|
||||
for (index, rem) in (1..=$num_columns).rev().enumerate() {
|
||||
// STATE: remember that we are expecting #rem more columns
|
||||
*$cmd = QueryCommand::ColumnDefinition { rem };
|
||||
|
||||
@ -26,7 +26,7 @@ macro_rules! impl_recv_columns {
|
||||
let packet = read_packet!($(@$blocking)? $stream);
|
||||
|
||||
if $store {
|
||||
columns.push(MySqlColumn::new(ordinal, packet.deserialize()?));
|
||||
columns.push(MySqlColumn::new(index, packet.deserialize()?));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -36,13 +36,13 @@ macro_rules! impl_raw_prepare {
|
||||
|
||||
// TODO: handle EOF for old MySQL
|
||||
|
||||
for (ordinal, rem) in (1..=ok.columns).rev().enumerate() {
|
||||
for (index, rem) in (1..=ok.columns).rev().enumerate() {
|
||||
// STATE: remember that we are expecting #rem more columns
|
||||
*cmd = PrepareCommand::ColumnDefinition { rem };
|
||||
|
||||
let def = read_packet!($(@$blocking)? stream).deserialize()?;
|
||||
|
||||
stmt.columns_mut().push(MySqlColumn::new(ordinal, def));
|
||||
stmt.columns_mut().push(MySqlColumn::new(index, def));
|
||||
}
|
||||
|
||||
// TODO: handle EOF for old MySQL
|
||||
|
||||
@ -41,28 +41,28 @@ impl MySqlRow {
|
||||
&self.columns
|
||||
}
|
||||
|
||||
/// Returns the column name, given the ordinal (also known as index) of the column.
|
||||
/// Returns the column name, given the index of the column.
|
||||
#[must_use]
|
||||
pub fn column_name_of(&self, ordinal: usize) -> &str {
|
||||
self.try_column_name_of(ordinal).unwrap()
|
||||
pub fn column_name_of(&self, index: usize) -> &str {
|
||||
self.try_column_name_of(index).unwrap()
|
||||
}
|
||||
|
||||
/// Returns the column name, given the ordinal (also known as index) of the column.
|
||||
pub fn try_column_name_of(&self, ordinal: usize) -> Result<&str> {
|
||||
/// Returns the column name, given the index of the column.
|
||||
pub fn try_column_name_of(&self, index: usize) -> Result<&str> {
|
||||
self.columns
|
||||
.get(ordinal)
|
||||
.get(index)
|
||||
.map(MySqlColumn::name)
|
||||
.ok_or_else(|| Error::ColumnIndexOutOfBounds { index: ordinal, len: self.len() })
|
||||
.ok_or_else(|| Error::ColumnIndexOutOfBounds { index, len: self.len() })
|
||||
}
|
||||
|
||||
/// Returns the column ordinal, given the name of the column.
|
||||
/// Returns the column index, given the name of the column.
|
||||
#[must_use]
|
||||
pub fn ordinal_of(&self, name: &str) -> usize {
|
||||
self.try_ordinal_of(name).unwrap()
|
||||
pub fn index_of(&self, name: &str) -> usize {
|
||||
self.try_index_of(name).unwrap()
|
||||
}
|
||||
|
||||
/// Returns the column ordinal, given the name of the column.
|
||||
pub fn try_ordinal_of(&self, name: &str) -> Result<usize> {
|
||||
/// Returns the column index, given the name of the column.
|
||||
pub fn try_index_of(&self, name: &str) -> Result<usize> {
|
||||
self.columns
|
||||
.iter()
|
||||
.position(|col| col.name() == name)
|
||||
@ -84,14 +84,14 @@ impl MySqlRow {
|
||||
where
|
||||
I: ColumnIndex<Self>,
|
||||
{
|
||||
let ordinal = index.get(self)?;
|
||||
let index = index.get(self)?;
|
||||
|
||||
let value = self
|
||||
.values
|
||||
.get(ordinal)
|
||||
.ok_or_else(|| Error::ColumnIndexOutOfBounds { len: self.len(), index: ordinal })?;
|
||||
.get(index)
|
||||
.ok_or_else(|| Error::ColumnIndexOutOfBounds { len: self.len(), index })?;
|
||||
|
||||
let column = &self.columns[ordinal];
|
||||
let column = &self.columns[index];
|
||||
|
||||
Ok(MySqlRawValue::new(value, self.format, column.type_info()))
|
||||
}
|
||||
@ -112,20 +112,20 @@ impl Row for MySqlRow {
|
||||
self.columns()
|
||||
}
|
||||
|
||||
fn column_name_of(&self, ordinal: usize) -> &str {
|
||||
self.column_name_of(ordinal)
|
||||
fn column_name_of(&self, index: usize) -> &str {
|
||||
self.column_name_of(index)
|
||||
}
|
||||
|
||||
fn try_column_name_of(&self, ordinal: usize) -> Result<&str> {
|
||||
self.try_column_name_of(ordinal)
|
||||
fn try_column_name_of(&self, index: usize) -> Result<&str> {
|
||||
self.try_column_name_of(index)
|
||||
}
|
||||
|
||||
fn ordinal_of(&self, name: &str) -> usize {
|
||||
self.ordinal_of(name)
|
||||
fn index_of(&self, name: &str) -> usize {
|
||||
self.index_of(name)
|
||||
}
|
||||
|
||||
fn try_ordinal_of(&self, name: &str) -> Result<usize> {
|
||||
self.try_ordinal_of(name)
|
||||
fn try_index_of(&self, name: &str) -> Result<usize> {
|
||||
self.try_index_of(name)
|
||||
}
|
||||
|
||||
fn try_get<'r, T, I>(&'r self, index: I) -> Result<T>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user