From d8e4030cb871249faa53792a48b191804967f56c Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Mon, 22 Feb 2021 21:29:05 -0800 Subject: [PATCH] refactor: rename ordinal to index --- sqlx-core/src/column.rs | 2 +- sqlx-core/src/row.rs | 24 +++++----- sqlx-mysql/src/column.rs | 14 +++--- sqlx-mysql/src/connection/executor/columns.rs | 4 +- .../src/connection/executor/raw_prepare.rs | 4 +- sqlx-mysql/src/row.rs | 48 +++++++++---------- 6 files changed, 48 insertions(+), 48 deletions(-) diff --git a/sqlx-core/src/column.rs b/sqlx-core/src/column.rs index 72ebb81e..a5aea66c 100644 --- a/sqlx-core/src/column.rs +++ b/sqlx-core/src/column.rs @@ -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) -> &::TypeInfo; diff --git a/sqlx-core/src/row.rs b/sqlx-core/src/row.rs index c9dae54b..b4cc992c 100644 --- a/sqlx-core/src/row.rs +++ b/sqlx-core/src/row.rs @@ -19,17 +19,17 @@ pub trait Row: 'static + Send + Sync { /// Returns a reference to the columns in the row. fn columns(&self) -> &[::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; + /// Returns the column index, given the name of the column. + fn try_index_of(&self, name: &str) -> crate::Result; /// Returns the decoded value at the index. fn try_get<'r, T, I>(&'r self, index: I) -> crate::Result @@ -47,12 +47,12 @@ pub trait Row: 'static + Send + Sync { /// A helper trait used for indexing into a [`Row`]. pub trait ColumnIndex { - /// 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; } -// access an ordinal by index +// access by index impl ColumnIndex for usize { #[allow(clippy::needless_lifetimes)] fn get<'r>(&self, _row: &'r R) -> crate::Result { @@ -62,11 +62,11 @@ impl ColumnIndex for usize { } } -// access an ordinal by name +// access by name impl ColumnIndex for &'_ str { #[allow(clippy::needless_lifetimes)] fn get<'r>(&self, row: &'r R) -> crate::Result { - row.try_ordinal_of(self) + row.try_index_of(self) } } diff --git a/sqlx-mysql/src/column.rs b/sqlx-mysql/src/column.rs index 771bab7f..78c5c15e 100644 --- a/sqlx-mysql/src/column.rs +++ b/sqlx-mysql/src/column.rs @@ -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] diff --git a/sqlx-mysql/src/connection/executor/columns.rs b/sqlx-mysql/src/connection/executor/columns.rs index e162274b..24b3d628 100644 --- a/sqlx-mysql/src/connection/executor/columns.rs +++ b/sqlx-mysql/src/connection/executor/columns.rs @@ -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()?)); } } diff --git a/sqlx-mysql/src/connection/executor/raw_prepare.rs b/sqlx-mysql/src/connection/executor/raw_prepare.rs index a4046dfe..253e84e6 100644 --- a/sqlx-mysql/src/connection/executor/raw_prepare.rs +++ b/sqlx-mysql/src/connection/executor/raw_prepare.rs @@ -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 diff --git a/sqlx-mysql/src/row.rs b/sqlx-mysql/src/row.rs index e1755293..5ce3410e 100644 --- a/sqlx-mysql/src/row.rs +++ b/sqlx-mysql/src/row.rs @@ -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 { + /// Returns the column index, given the name of the column. + pub fn try_index_of(&self, name: &str) -> Result { self.columns .iter() .position(|col| col.name() == name) @@ -84,14 +84,14 @@ impl MySqlRow { where I: ColumnIndex, { - 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 { - self.try_ordinal_of(name) + fn try_index_of(&self, name: &str) -> Result { + self.try_index_of(name) } fn try_get<'r, T, I>(&'r self, index: I) -> Result