Skip to content

Commit fabc12c

Browse files
committed
fix: pull out query param builder to fn
Signed-off-by: callum-ryan <19956159+callum-ryan@users.noreply.github.com>
1 parent 5a09b18 commit fabc12c

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

crates/catalog/sql/src/catalog.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use std::borrow::Cow;
1918
use std::collections::HashMap;
2019
use std::time::Duration;
2120

@@ -26,7 +25,7 @@ use iceberg::{
2625
Catalog, Error, Namespace, NamespaceIdent, Result, TableCommit, TableCreation, TableIdent,
2726
};
2827
use sqlx::any::{install_default_drivers, AnyPoolOptions, AnyQueryResult, AnyRow};
29-
use sqlx::{Any, AnyConnection, AnyPool, Executor, Pool, Row, Transaction};
28+
use sqlx::{Any, AnyPool, Row, Transaction};
3029
use typed_builder::TypedBuilder;
3130

3231
use crate::error::{from_sqlx_error, no_such_namespace_err};
@@ -143,22 +142,24 @@ impl SqlCatalog {
143142
}
144143

145144
/// SQLX Any does not implement PostgresSQL bindings, so we have to do this.
146-
pub async fn fetch_rows(
147-
&self,
148-
query: &String,
149-
args: Vec<Option<&String>>,
150-
) -> Result<Vec<AnyRow>> {
151-
// TODO: move this out to a function
152-
let query_with_placeholders: Cow<str> =
153-
if self.sql_bind_style == SqlBindStyle::DollarNumeric {
154-
let mut query = query.clone();
155-
for i in 0..args.len() {
156-
query = query.replacen("?", &format!("${}", i + 1), 1);
145+
pub fn build_query(&self, query: &str) -> String {
146+
match self.sql_bind_style {
147+
SqlBindStyle::DollarNumeric => {
148+
let mut query = query.to_owned();
149+
let mut i = 1;
150+
while let Some(pos) = query.find('?') {
151+
query.replace_range(pos..pos + 1, &format!("${}", i));
152+
i += 1;
157153
}
158-
Cow::Owned(query)
159-
} else {
160-
Cow::Borrowed(query)
161-
};
154+
query
155+
}
156+
_ => query.to_owned(),
157+
}
158+
}
159+
160+
/// Fetch a vec of AnyRows from a given query
161+
pub async fn fetch_rows(&self, query: &str, args: Vec<Option<&String>>) -> Result<Vec<AnyRow>> {
162+
let query_with_placeholders = self.build_query(query);
162163

163164
let mut sqlx_query = sqlx::query(&query_with_placeholders);
164165
for arg in args {
@@ -173,21 +174,11 @@ impl SqlCatalog {
173174
/// Execute statements in a transaction, provided or not
174175
pub async fn execute(
175176
&self,
176-
query: &String,
177+
query: &str,
177178
args: Vec<Option<&String>>,
178179
transaction: Option<&mut Transaction<'_, Any>>,
179180
) -> Result<AnyQueryResult> {
180-
// TODO: move this out to a function
181-
let query_with_placeholders: Cow<str> =
182-
if self.sql_bind_style == SqlBindStyle::DollarNumeric {
183-
let mut query = query.clone();
184-
for i in 0..args.len() {
185-
query = query.replacen("?", &format!("${}", i + 1), 1);
186-
}
187-
Cow::Owned(query)
188-
} else {
189-
Cow::Borrowed(query)
190-
};
181+
let query_with_placeholders = self.build_query(query);
191182

192183
let mut sqlx_query = sqlx::query(&query_with_placeholders);
193184
for arg in args {

0 commit comments

Comments
 (0)