Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Results from DB executor functions #109

Merged
merged 5 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/mysql/discovery/executor/mock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::sqlx_types::{mysql::MySqlRow, MySqlPool};
use sea_query::{MysqlQueryBuilder, SelectStatement};

use crate::debug_print;
use crate::{debug_print, sqlx_types::SqlxError};

#[allow(dead_code)]
pub struct Executor {
Expand All @@ -19,7 +19,7 @@ impl IntoExecutor for MySqlPool {
}

impl Executor {
pub async fn fetch_all(&self, select: SelectStatement) -> Vec<MySqlRow> {
pub async fn fetch_all(&self, select: SelectStatement) -> Result<Vec<MySqlRow>, SqlxError> {
let (_sql, _values) = select.build(MysqlQueryBuilder);
debug_print!("{}, {:?}", _sql, _values);

Expand Down
7 changes: 3 additions & 4 deletions src/mysql/discovery/executor/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use sea_query::{MysqlQueryBuilder, SelectStatement};
use sea_query_binder::SqlxBinder;
use sqlx::{mysql::MySqlRow, MySqlPool};

use crate::debug_print;
use crate::{debug_print, sqlx_types::SqlxError};

pub struct Executor {
pool: MySqlPool,
Expand All @@ -19,13 +19,12 @@ impl IntoExecutor for MySqlPool {
}

impl Executor {
pub async fn fetch_all(&self, select: SelectStatement) -> Vec<MySqlRow> {
pub async fn fetch_all(&self, select: SelectStatement) -> Result<Vec<MySqlRow>, SqlxError> {
let (sql, values) = select.build_sqlx(MysqlQueryBuilder);
debug_print!("{}, {:?}", sql, values);

sqlx::query_with(&sql, values)
.fetch_all(&mut self.pool.acquire().await.unwrap())
.fetch_all(&mut self.pool.acquire().await?)
.await
.unwrap()
}
}
65 changes: 33 additions & 32 deletions src/mysql/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::mysql::query::{
ColumnQueryResult, ForeignKeyQueryResult, IndexQueryResult, SchemaQueryBuilder,
TableQueryResult, VersionQueryResult,
};
use crate::sqlx_types::SqlxError;
use futures::future;
use sea_query::{Alias, Iden, IntoIden, SeaRc};

Expand All @@ -31,43 +32,43 @@ impl SchemaDiscovery {
}
}

pub async fn discover(mut self) -> Schema {
self.query = SchemaQueryBuilder::new(self.discover_system().await);
let tables = self.discover_tables().await;
let tables = future::join_all(
pub async fn discover(mut self) -> Result<Schema, SqlxError> {
self.query = SchemaQueryBuilder::new(self.discover_system().await?);
let tables = self.discover_tables().await?;
let tables = future::try_join_all(
tables
.into_iter()
.map(|t| (&self, t))
.map(Self::discover_table_static),
)
.await;
.await?;

Schema {
Ok(Schema {
schema: self.schema.to_string(),
system: self.query.system,
tables,
}
})
}

pub async fn discover_system(&mut self) -> SystemInfo {
let rows = self.executor.fetch_all(self.query.query_version()).await;
pub async fn discover_system(&mut self) -> Result<SystemInfo, SqlxError> {
let rows = self.executor.fetch_all(self.query.query_version()).await?;

#[allow(clippy::never_loop)]
for row in rows.iter() {
let result: VersionQueryResult = row.into();
debug_print!("{:?}", result);
let version = result.parse();
debug_print!("{:?}", version);
return version;
return Ok(version);
}
panic!("failed to discover version")
Err(SqlxError::RowNotFound)
}

pub async fn discover_tables(&mut self) -> Vec<TableInfo> {
pub async fn discover_tables(&mut self) -> Result<Vec<TableInfo>, SqlxError> {
let rows = self
.executor
.fetch_all(self.query.query_tables(self.schema.clone()))
.await;
.await?;

let tables: Vec<TableInfo> = rows
.iter()
Expand All @@ -80,44 +81,44 @@ impl SchemaDiscovery {
})
.collect();

tables
Ok(tables)
}

async fn discover_table_static(params: (&Self, TableInfo)) -> TableDef {
async fn discover_table_static(params: (&Self, TableInfo)) -> Result<TableDef, SqlxError> {
let this = params.0;
let info = params.1;
Self::discover_table(this, info).await
}

pub async fn discover_table(&self, info: TableInfo) -> TableDef {
pub async fn discover_table(&self, info: TableInfo) -> Result<TableDef, SqlxError> {
let table = SeaRc::new(Alias::new(info.name.as_str()));
let columns = self
.discover_columns(self.schema.clone(), table.clone())
.await;
.await?;
let indexes = self
.discover_indexes(self.schema.clone(), table.clone())
.await;
.await?;
let foreign_keys = self
.discover_foreign_keys(self.schema.clone(), table.clone())
.await;
.await?;

TableDef {
Ok(TableDef {
info,
columns,
indexes,
foreign_keys,
}
})
}

pub async fn discover_columns(
&self,
schema: SeaRc<dyn Iden>,
table: SeaRc<dyn Iden>,
) -> Vec<ColumnInfo> {
) -> Result<Vec<ColumnInfo>, SqlxError> {
let rows = self
.executor
.fetch_all(self.query.query_columns(schema.clone(), table.clone()))
.await;
.await?;

let columns = rows
.iter()
Expand All @@ -130,54 +131,54 @@ impl SchemaDiscovery {
})
.collect::<Vec<_>>();

columns
Ok(columns)
}

pub async fn discover_indexes(
&self,
schema: SeaRc<dyn Iden>,
table: SeaRc<dyn Iden>,
) -> Vec<IndexInfo> {
) -> Result<Vec<IndexInfo>, SqlxError> {
let rows = self
.executor
.fetch_all(self.query.query_indexes(schema.clone(), table.clone()))
.await;
.await?;

let results = rows.into_iter().map(|row| {
let result: IndexQueryResult = (&row).into();
debug_print!("{:?}", result);
result
});

parse_index_query_results(Box::new(results))
Ok(parse_index_query_results(Box::new(results))
.map(|index| {
debug_print!("{:?}", index);
index
})
.collect()
.collect())
}

pub async fn discover_foreign_keys(
&self,
schema: SeaRc<dyn Iden>,
table: SeaRc<dyn Iden>,
) -> Vec<ForeignKeyInfo> {
) -> Result<Vec<ForeignKeyInfo>, SqlxError> {
let rows = self
.executor
.fetch_all(self.query.query_foreign_key(schema.clone(), table.clone()))
.await;
.await?;

let results = rows.into_iter().map(|row| {
let result: ForeignKeyQueryResult = (&row).into();
debug_print!("{:?}", result);
result
});

parse_foreign_key_query_results(Box::new(results))
Ok(parse_foreign_key_query_results(Box::new(results))
.map(|index| {
debug_print!("{:?}", index);
index
})
.collect()
.collect())
}
}
4 changes: 2 additions & 2 deletions src/postgres/discovery/executor/mock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::sqlx_types::{postgres::PgRow, PgPool};
use sea_query::{PostgresQueryBuilder, SelectStatement};

use crate::debug_print;
use crate::{debug_print, sqlx_types::SqlxError};

#[allow(dead_code)]
pub struct Executor {
Expand All @@ -19,7 +19,7 @@ impl IntoExecutor for PgPool {
}

impl Executor {
pub async fn fetch_all(&self, select: SelectStatement) -> Vec<PgRow> {
pub async fn fetch_all(&self, select: SelectStatement) -> Result<Vec<PgRow>, SqlxError> {
let (_sql, _values) = select.build(PostgresQueryBuilder);
debug_print!("{}, {:?}", _sql, _values);

Expand Down
7 changes: 3 additions & 4 deletions src/postgres/discovery/executor/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use sea_query::{PostgresQueryBuilder, SelectStatement};
use sea_query_binder::SqlxBinder;
use sqlx::{postgres::PgRow, PgPool};

use crate::debug_print;
use crate::{debug_print, sqlx_types::SqlxError};

pub struct Executor {
pool: PgPool,
Expand All @@ -19,13 +19,12 @@ impl IntoExecutor for PgPool {
}

impl Executor {
pub async fn fetch_all(&self, select: SelectStatement) -> Vec<PgRow> {
pub async fn fetch_all(&self, select: SelectStatement) -> Result<Vec<PgRow>, SqlxError> {
let (sql, values) = select.build_sqlx(PostgresQueryBuilder);
debug_print!("{}, {:?}", sql, values);

sqlx::query_with(&sql, values)
.fetch_all(&mut self.pool.acquire().await.unwrap())
.fetch_all(&mut self.pool.acquire().await?)
.await
.unwrap()
}
}
Loading