callum-ryan commented on code in PR #534: URL: https://github.com/apache/iceberg-rust/pull/534#discussion_r1722260283
########## crates/catalog/sql/src/catalog.rs: ########## @@ -167,43 +177,335 @@ impl SqlCatalog { .await .map_err(from_sqlx_error) } + + /// Execute statements in a transaction, provided or not + pub async fn execute( + &self, + query: &str, + args: Vec<Option<&str>>, + transaction: Option<&mut Transaction<'_, Any>>, + ) -> Result<AnyQueryResult> { + let query_with_placeholders = self.replace_placeholders(query); + + let mut sqlx_query = sqlx::query(&query_with_placeholders); + for arg in args { + sqlx_query = sqlx_query.bind(arg); + } + + match transaction { + Some(t) => sqlx_query.execute(&mut **t).await.map_err(from_sqlx_error), + None => { + let mut tx = self.connection.begin().await.map_err(from_sqlx_error)?; + let result = sqlx_query.execute(&mut *tx).await.map_err(from_sqlx_error); + let _ = tx.commit().await.map_err(from_sqlx_error); + result + } + } + } } #[async_trait] impl Catalog for SqlCatalog { async fn list_namespaces( &self, - _parent: Option<&NamespaceIdent>, + parent: Option<&NamespaceIdent>, ) -> Result<Vec<NamespaceIdent>> { - todo!() + let table_namespaces_stmt = format!( + "SELECT {CATALOG_FIELD_TABLE_NAMESPACE} Review Comment: Ok nevermind, I have changed this - we can do the parent filtering with the query return instead of pushing it to SQL. See https://github.com/apache/iceberg-rust/pull/534/commits/b0b731c2ed2cab8ffd3df381b17f989e8b8f6380 ########## crates/catalog/sql/src/catalog.rs: ########## @@ -167,43 +177,335 @@ impl SqlCatalog { .await .map_err(from_sqlx_error) } + + /// Execute statements in a transaction, provided or not + pub async fn execute( + &self, + query: &str, + args: Vec<Option<&str>>, + transaction: Option<&mut Transaction<'_, Any>>, + ) -> Result<AnyQueryResult> { + let query_with_placeholders = self.replace_placeholders(query); + + let mut sqlx_query = sqlx::query(&query_with_placeholders); + for arg in args { + sqlx_query = sqlx_query.bind(arg); + } + + match transaction { + Some(t) => sqlx_query.execute(&mut **t).await.map_err(from_sqlx_error), + None => { + let mut tx = self.connection.begin().await.map_err(from_sqlx_error)?; + let result = sqlx_query.execute(&mut *tx).await.map_err(from_sqlx_error); + let _ = tx.commit().await.map_err(from_sqlx_error); + result + } + } + } } #[async_trait] impl Catalog for SqlCatalog { async fn list_namespaces( &self, - _parent: Option<&NamespaceIdent>, + parent: Option<&NamespaceIdent>, ) -> Result<Vec<NamespaceIdent>> { - todo!() + let table_namespaces_stmt = format!( + "SELECT {CATALOG_FIELD_TABLE_NAMESPACE} + FROM {CATALOG_TABLE_NAME} + WHERE {CATALOG_FIELD_CATALOG_NAME} = ?" + ); + let namespaces_stmt = format!( + "SELECT {NAMESPACE_FIELD_NAME} + FROM {NAMESPACE_TABLE_NAME} + WHERE {CATALOG_FIELD_CATALOG_NAME} = ?" + ); + + match parent { + Some(parent) => { + if self.namespace_exists(parent).await? { + let parent_str = parent.join("."); + let parent_table_namespaces_stmt = format!( + "{table_namespaces_stmt} AND {CATALOG_FIELD_TABLE_NAMESPACE} LIKE CONCAT(?, '%')" Review Comment: https://github.com/apache/iceberg-rust/pull/534/commits/b0b731c2ed2cab8ffd3df381b17f989e8b8f6380 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org