Skip to content

Commit 951b118

Browse files
committed
More API updates
1 parent 5adffcc commit 951b118

File tree

2 files changed

+28
-61
lines changed

2 files changed

+28
-61
lines changed

benches/bench.rs

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,34 @@
1-
use criterion::{black_box, criterion_group, criterion_main, Criterion};
1+
use std::hint::black_box;
2+
3+
use criterion::{criterion_group, criterion_main, Criterion};
24
use sqlformat::*;
35

46
fn simple_query(c: &mut Criterion) {
57
let input = "SELECT * FROM my_table WHERE id = 1";
68
c.bench_function("simple query", |b| {
7-
b.iter(|| {
8-
format(
9-
black_box(input),
10-
black_box(&QueryParams::None),
11-
black_box(&FormatOptions::default()),
12-
)
13-
})
9+
b.iter(|| FormatOptions::default().format(input))
1410
});
1511
}
1612

1713
fn complex_query(c: &mut Criterion) {
1814
let input = "SELECT t1.id, t1.name, t1.title, t1.description, t2.mothers_maiden_name, t2.first_girlfriend\nFROM my_table t1 LEFT JOIN other_table t2 ON t1.id = t2.other_id WHERE t2.order BETWEEN 17 AND 30";
1915
c.bench_function("complex query", |b| {
20-
b.iter(|| {
21-
format(
22-
black_box(input),
23-
black_box(&QueryParams::None),
24-
black_box(&FormatOptions::default()),
25-
)
26-
})
16+
b.iter(|| FormatOptions::default().format(input))
2717
});
2818
}
2919

3020
fn query_with_named_params(c: &mut Criterion) {
3121
let input = "SELECT * FROM my_table WHERE id = :first OR id = :second OR id = :third";
32-
let params = vec![
22+
let params = &[
3323
("first".to_string(), "1".to_string()),
3424
("second".to_string(), "2".to_string()),
3525
("third".to_string(), "3".to_string()),
3626
];
3727
c.bench_function("named params", |b| {
3828
b.iter(|| {
39-
format(
40-
black_box(input),
41-
black_box(&QueryParams::Named(params.clone())),
42-
black_box(&FormatOptions::default()),
43-
)
29+
FormatOptions::default()
30+
.with_params(params.as_ref())
31+
.format(input)
4432
})
4533
});
4634
}
@@ -49,27 +37,15 @@ fn query_with_explicit_indexed_params(c: &mut Criterion) {
4937
let input = "SELECT * FROM my_table WHERE id = ?1 OR id = ?2 OR id = ?0";
5038
let params = vec!["0".to_string(), "1".to_string(), "2".to_string()];
5139
c.bench_function("explicit indexed params", |b| {
52-
b.iter(|| {
53-
format(
54-
black_box(input),
55-
black_box(&QueryParams::Indexed(params.clone())),
56-
black_box(&FormatOptions::default()),
57-
)
58-
})
40+
b.iter(|| FormatOptions::default().with_params(&params).format(input))
5941
});
6042
}
6143

6244
fn query_with_implicit_indexed_params(c: &mut Criterion) {
6345
let input = "SELECT * FROM my_table WHERE id = ? OR id = ? OR id = ?";
6446
let params = vec!["0".to_string(), "1".to_string(), "2".to_string()];
6547
c.bench_function("implicit indexed params", |b| {
66-
b.iter(|| {
67-
format(
68-
black_box(input),
69-
black_box(&QueryParams::Indexed(params.clone())),
70-
black_box(&FormatOptions::default()),
71-
)
72-
})
48+
b.iter(|| FormatOptions::default().with_params(&params).format(input))
7349
});
7450
}
7551

@@ -131,27 +107,15 @@ VALUES
131107

132108
let input = generate_insert_query();
133109
c.bench_function("issue 633", |b| {
134-
b.iter(|| {
135-
format(
136-
black_box(&input),
137-
black_box(&QueryParams::None),
138-
black_box(&FormatOptions::default()),
139-
)
140-
})
110+
b.iter(|| FormatOptions::default().format(&input))
141111
});
142112
}
143113

144114
fn issue_633_2(c: &mut Criterion) {
145115
let input = "SELECT\n d.uuid AS uuid,\n\td.name_of_document AS name,\n\td.slug_name AS slug,\n\td.default_contract_uuid AS default_contract_uuid,\n\ta.uuid AS parent_uuid,\n\ta.name_of_agreement AS agreement_name,\n\td.icon_name AS icon\nFROM `documents` d\nLEFT JOIN agreements a ON a.uuid = d.parent_uuid\n WHERE d.uuid = ? LIMIT 1";
146116
let params = vec!["0".to_string()];
147117
c.bench_function("issue 633 query 2", |b| {
148-
b.iter(|| {
149-
format(
150-
black_box(input),
151-
black_box(&QueryParams::Indexed(params.clone())),
152-
black_box(&FormatOptions::default()),
153-
)
154-
})
118+
b.iter(|| FormatOptions::default().with_params(&params).format(input))
155119
});
156120
}
157121

@@ -171,13 +135,7 @@ fn issue_633_3(c: &mut Criterion) {
171135
}
172136

173137
c.bench_function("issue 633 query 3", |b| {
174-
b.iter(|| {
175-
format(
176-
black_box(&input),
177-
black_box(&QueryParams::None),
178-
black_box(&FormatOptions::default()),
179-
)
180-
})
138+
b.iter(|| FormatOptions::default().format(&input))
181139
});
182140
}
183141

src/lib.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub struct FormatOptions<'a> {
7373

7474
impl<'a> FormatOptions<'a> {
7575
/// Format the SQL query string
76-
fn format(&self, query: &str) -> String {
76+
pub fn format(&self, query: &str) -> String {
7777
let tokens = tokenizer::tokenize(query, self.params.is_named(), self);
7878
formatter::format(&tokens, &self.params, self)
7979
}
@@ -153,6 +153,18 @@ impl<'a> From<Vec<String>> for QueryParams<'a> {
153153
}
154154
}
155155

156+
impl<'a> From<&'a Vec<(String, String)>> for QueryParams<'a> {
157+
fn from(value: &'a Vec<(String, String)>) -> Self {
158+
Self::Named(Cow::Borrowed(value.as_ref()))
159+
}
160+
}
161+
162+
impl<'a> From<&'a Vec<String>> for QueryParams<'a> {
163+
fn from(value: &'a Vec<String>) -> Self {
164+
Self::Indexed(Cow::Borrowed(value.as_ref()))
165+
}
166+
}
167+
156168
impl<'a> From<&'a [(String, String)]> for QueryParams<'a> {
157169
fn from(value: &'a [(String, String)]) -> Self {
158170
Self::Named(Cow::Borrowed(value))
@@ -1776,10 +1788,7 @@ mod tests {
17761788
third;"
17771789
);
17781790

1779-
assert_eq!(
1780-
format(input, &QueryParams::Named(params), &options),
1781-
expected
1782-
);
1791+
assert_eq!(options.with_params(params).format(input), expected);
17831792
}
17841793

17851794
#[test]

0 commit comments

Comments
 (0)