Xuanwo commented on code in PR #534: URL: https://github.com/apache/iceberg-rust/pull/534#discussion_r1713021439
########## crates/catalog/sql/src/catalog.rs: ########## @@ -141,21 +142,24 @@ impl SqlCatalog { } /// SQLX Any does not implement PostgresSQL bindings, so we have to do this. - pub async fn execute_statement( - &self, - query: &String, - args: Vec<Option<&String>>, - ) -> Result<Vec<AnyRow>> { - let query_with_placeholders: Cow<str> = - if self.sql_bind_style == SqlBindStyle::DollarNumeric { - let mut query = query.clone(); - for i in 0..args.len() { - query = query.replacen("?", &format!("${}", i + 1), 1); + pub fn build_query(&self, query: &str) -> String { Review Comment: Maybe this function doesn't need to be public? ########## crates/catalog/sql/src/catalog.rs: ########## @@ -141,21 +142,24 @@ impl SqlCatalog { } /// SQLX Any does not implement PostgresSQL bindings, so we have to do this. - pub async fn execute_statement( - &self, - query: &String, - args: Vec<Option<&String>>, - ) -> Result<Vec<AnyRow>> { - let query_with_placeholders: Cow<str> = - if self.sql_bind_style == SqlBindStyle::DollarNumeric { - let mut query = query.clone(); - for i in 0..args.len() { - query = query.replacen("?", &format!("${}", i + 1), 1); + pub fn build_query(&self, query: &str) -> String { + match self.sql_bind_style { + SqlBindStyle::DollarNumeric => { + let mut query = query.to_owned(); + let mut i = 1; + while let Some(pos) = query.find('?') { Review Comment: Do you think it's a good idea to use `chars`, for example: ```rust fn replace_placeholders(query: &str) -> String { let mut result = String::with_capacity(query.len()); let mut count = 1; for c in query.chars() { if c == '?' { result.push_str(&format!("${count}")); count += 1; } else { result.push(c); } } result } ``` In this way, we can replace all `?` in `O(n)` instead of `O(n^2)`. ########## crates/catalog/sql/src/catalog.rs: ########## @@ -141,21 +142,24 @@ impl SqlCatalog { } /// SQLX Any does not implement PostgresSQL bindings, so we have to do this. - pub async fn execute_statement( - &self, - query: &String, - args: Vec<Option<&String>>, - ) -> Result<Vec<AnyRow>> { - let query_with_placeholders: Cow<str> = - if self.sql_bind_style == SqlBindStyle::DollarNumeric { - let mut query = query.clone(); - for i in 0..args.len() { - query = query.replacen("?", &format!("${}", i + 1), 1); + pub fn build_query(&self, query: &str) -> String { + match self.sql_bind_style { + SqlBindStyle::DollarNumeric => { + let mut query = query.to_owned(); + let mut i = 1; + while let Some(pos) = query.find('?') { + query.replace_range(pos..pos + 1, &format!("${}", i)); + i += 1; } - Cow::Owned(query) - } else { - Cow::Borrowed(query) - }; + query + } + _ => query.to_owned(), + } + } + + /// Fetch a vec of AnyRows from a given query + pub async fn fetch_rows(&self, query: &str, args: Vec<Option<&String>>) -> Result<Vec<AnyRow>> { Review Comment: `&String` isn't useful in Rust; how about using `&str` instead? -- 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