Skip to content

Commit a46b2bf

Browse files
committed
refactors to_string and pub(crate)
1 parent 0db0ce2 commit a46b2bf

File tree

14 files changed

+128
-129
lines changed

14 files changed

+128
-129
lines changed

src/behavior.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ pub trait Concat {
77
fn concat(&self, fmts: &fmt::Formatter) -> String;
88
}
99

10-
pub trait ConcatSqlStandard<Clause: PartialEq> {
10+
/// Represents all commands that can be used in a transaction
11+
pub trait TransactionQuery: Concat {}
12+
13+
/// Represents all commands that can be used inside the with method
14+
pub trait WithQuery: Concat {}
15+
16+
pub(crate) trait ConcatSqlStandard<Clause: PartialEq> {
1117
fn concat_from(
1218
&self,
1319
items_raw_before: &Vec<(Clause, String)>,
@@ -22,7 +28,7 @@ pub trait ConcatSqlStandard<Clause: PartialEq> {
2228
let tables = items.join(comma);
2329
format!("FROM{space}{tables}{space}{lb}")
2430
} else {
25-
"".to_owned()
31+
"".to_string()
2632
};
2733

2834
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
@@ -53,7 +59,7 @@ pub trait ConcatSqlStandard<Clause: PartialEq> {
5359
let values = items.join(&sep);
5460
format!("VALUES{space}{lb}{values}{space}{lb}")
5561
} else {
56-
"".to_owned()
62+
"".to_string()
5763
};
5864

5965
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
@@ -79,15 +85,15 @@ pub trait ConcatSqlStandard<Clause: PartialEq> {
7985

8086
format!("WHERE{space}{conditions}{space}{lb}")
8187
} else {
82-
"".to_owned()
88+
"".to_string()
8389
};
8490

8591
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
8692
}
8793
}
8894

8995
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
90-
pub trait ConcatCommon<Clause: PartialEq> {
96+
pub(crate) trait ConcatCommon<Clause: PartialEq> {
9197
fn concat_returning(
9298
&self,
9399
items_raw_before: &Vec<(Clause, String)>,
@@ -102,7 +108,7 @@ pub trait ConcatCommon<Clause: PartialEq> {
102108
let output_names = items.join(comma);
103109
format!("RETURNING{space}{output_names}{space}{lb}")
104110
} else {
105-
"".to_owned()
111+
"".to_string()
106112
};
107113

108114
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
@@ -125,7 +131,7 @@ pub trait ConcatCommon<Clause: PartialEq> {
125131
..
126132
} = fmts;
127133
let sql = if items.is_empty() == false {
128-
let with = items.iter().fold("".to_owned(), |acc, item| {
134+
let with = items.iter().fold("".to_string(), |acc, item| {
129135
let (name, query) = item;
130136
let inner_lb = format!("{lb}{indent}");
131137
let inner_fmts = fmt::Formatter {
@@ -143,15 +149,15 @@ pub trait ConcatCommon<Clause: PartialEq> {
143149

144150
format!("WITH{space}{lb}{with}{space}{lb}")
145151
} else {
146-
"".to_owned()
152+
"".to_string()
147153
};
148154

149155
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
150156
}
151157
}
152158

153159
#[cfg(feature = "sqlite")]
154-
pub trait ConcatSqlite {
160+
pub(crate) trait ConcatSqlite {
155161
fn concat_insert(
156162
&self,
157163
items_raw_before: &Vec<(InsertClause, String)>,
@@ -163,13 +169,13 @@ pub trait ConcatSqlite {
163169
let fmt::Formatter { lb, space, .. } = fmts;
164170

165171
let (clause, sql) = match insert {
166-
(InsertVars::InsertInto, exp) if exp.is_empty() => (InsertClause::InsertInto, "".to_owned()),
172+
(InsertVars::InsertInto, exp) if exp.is_empty() => (InsertClause::InsertInto, "".to_string()),
167173
(InsertVars::InsertInto, exp) => (InsertClause::InsertInto, format!("INSERT INTO{space}{exp}{space}{lb}")),
168174

169-
(InsertVars::InsertOr, exp) if exp.is_empty() => (InsertClause::InsertOr, "".to_owned()),
175+
(InsertVars::InsertOr, exp) if exp.is_empty() => (InsertClause::InsertOr, "".to_string()),
170176
(InsertVars::InsertOr, exp) => (InsertClause::InsertOr, format!("INSERT OR{space}{exp}{space}{lb}")),
171177

172-
(InsertVars::ReplaceInto, exp) if exp.is_empty() => (InsertClause::ReplaceInto, "".to_owned()),
178+
(InsertVars::ReplaceInto, exp) if exp.is_empty() => (InsertClause::ReplaceInto, "".to_string()),
173179
(InsertVars::ReplaceInto, exp) => (
174180
InsertClause::ReplaceInto,
175181
format!("REPLACE INTO{space}{exp}{space}{lb}"),
@@ -193,7 +199,7 @@ pub trait ConcatSqlite {
193199
let joins = join.join(format!("{space}{lb}").as_str());
194200
format!("{joins}{space}{lb}")
195201
} else {
196-
"".to_owned()
202+
"".to_string()
197203
};
198204

199205
concat_raw_before_after(&items_raw_before, &items_raw_after, query, fmts, clause, sql)
@@ -240,19 +246,13 @@ pub trait ConcatSqlite {
240246
let values = values.join(&sep);
241247
(InsertClause::Values, format!("VALUES{space}{lb}{values}{space}{lb}"))
242248
} else {
243-
(InsertClause::Values, "".to_owned())
249+
(InsertClause::Values, "".to_string())
244250
};
245251

246252
concat_raw_before_after(items_raw_before, items_raw_after, query, fmts, clause, sql)
247253
}
248254
}
249255

250-
/// Represents all commands that can be used in a transaction
251-
pub trait TransactionQuery: Concat {}
252-
253-
/// Represents all commands that can be used inside the with method
254-
pub trait WithQuery: Concat {}
255-
256256
impl std::fmt::Display for LogicalOperator {
257257
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
258258
let v = match self {
@@ -263,7 +263,7 @@ impl std::fmt::Display for LogicalOperator {
263263
}
264264
}
265265

266-
pub fn concat_raw_before_after<Clause: PartialEq>(
266+
pub(crate) fn concat_raw_before_after<Clause: PartialEq>(
267267
items_before: &Vec<(Clause, String)>,
268268
items_after: &Vec<(Clause, String)>,
269269
query: String,
@@ -280,14 +280,14 @@ pub fn concat_raw_before_after<Clause: PartialEq>(
280280
format!("{query}{raw_before}{space_before}{sql}{raw_after}{space_after}")
281281
}
282282

283-
pub fn push_unique<T: PartialEq>(list: &mut Vec<T>, value: T) {
283+
pub(crate) fn push_unique<T: PartialEq>(list: &mut Vec<T>, value: T) {
284284
let prev_item = list.iter().find(|&item| *item == value);
285285
if prev_item.is_none() {
286286
list.push(value);
287287
}
288288
}
289289

290-
pub fn raw_queries<Clause: PartialEq>(raw_list: &Vec<(Clause, String)>, clause: &Clause) -> Vec<String> {
290+
pub(crate) fn raw_queries<Clause: PartialEq>(raw_list: &Vec<(Clause, String)>, clause: &Clause) -> Vec<String> {
291291
raw_list
292292
.iter()
293293
.filter(|item| item.0 == *clause)

src/delete/delete.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Delete {
8383
/// # assert_eq!(delete.to_string(), expected);
8484
/// ```
8585
pub fn delete_from(mut self, table_name: &str) -> Self {
86-
self._delete_from = table_name.trim().to_owned();
86+
self._delete_from = table_name.trim().to_string();
8787
self
8888
}
8989

@@ -122,7 +122,7 @@ impl Delete {
122122
/// delete from users WHERE login = 'foo'
123123
/// ```
124124
pub fn raw(mut self, raw_sql: &str) -> Self {
125-
push_unique(&mut self._raw, raw_sql.trim().to_owned());
125+
push_unique(&mut self._raw, raw_sql.trim().to_string());
126126
self
127127
}
128128

@@ -148,7 +148,7 @@ impl Delete {
148148
/// DELETE FROM users where name = 'Foo'
149149
/// ```
150150
pub fn raw_after(mut self, clause: DeleteClause, raw_sql: &str) -> Self {
151-
self._raw_after.push((clause, raw_sql.trim().to_owned()));
151+
self._raw_after.push((clause, raw_sql.trim().to_string()));
152152
self
153153
}
154154

@@ -174,7 +174,7 @@ impl Delete {
174174
/// delete from users WHERE name = 'Bar'
175175
/// ```
176176
pub fn raw_before(mut self, clause: DeleteClause, raw_sql: &str) -> Self {
177-
self._raw_before.push((clause, raw_sql.trim().to_owned()));
177+
self._raw_before.push((clause, raw_sql.trim().to_string()));
178178
self
179179
}
180180

@@ -202,7 +202,7 @@ impl Delete {
202202
/// AND status = 'deactivated'
203203
/// ```
204204
pub fn where_clause(mut self, condition: &str) -> Self {
205-
push_unique(&mut self._where, (LogicalOperator::And, condition.trim().to_owned()));
205+
push_unique(&mut self._where, (LogicalOperator::And, condition.trim().to_string()));
206206
self
207207
}
208208

@@ -230,7 +230,7 @@ impl Delete {
230230
/// OR login = 'bar'
231231
/// ```
232232
pub fn where_or(mut self, condition: &str) -> Self {
233-
push_unique(&mut self._where, (LogicalOperator::Or, condition.trim().to_owned()));
233+
push_unique(&mut self._where, (LogicalOperator::Or, condition.trim().to_string()));
234234
self
235235
}
236236
}
@@ -261,7 +261,7 @@ impl Delete {
261261
/// DELETE FROM users RETURNING id, login
262262
/// ```
263263
pub fn returning(mut self, output_name: &str) -> Self {
264-
push_unique(&mut self._returning, output_name.trim().to_owned());
264+
push_unique(&mut self._returning, output_name.trim().to_string());
265265
self
266266
}
267267

@@ -305,7 +305,7 @@ impl Delete {
305305
/// WHERE id in (select * from deactivated_users)
306306
/// ```
307307
pub fn with(mut self, name: &str, query: impl WithQuery + 'static) -> Self {
308-
self._with.push((name.trim().to_owned(), std::sync::Arc::new(query)));
308+
self._with.push((name.trim().to_string(), std::sync::Arc::new(query)));
309309
self
310310
}
311311
}

src/delete/delete_internal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl Delete {
1515
let table_name = &self._delete_from;
1616
format!("DELETE FROM{space}{table_name}{space}{lb}")
1717
} else {
18-
"".to_owned()
18+
"".to_string()
1919
};
2020

2121
concat_raw_before_after(
@@ -31,7 +31,7 @@ impl Delete {
3131

3232
impl Concat for Delete {
3333
fn concat(&self, fmts: &fmt::Formatter) -> String {
34-
let mut query = "".to_owned();
34+
let mut query = "".to_string();
3535

3636
query = self.concat_raw(query, &fmts, &self._raw);
3737
#[cfg(any(feature = "postgresql", feature = "sqlite"))]
@@ -66,7 +66,7 @@ impl Concat for Delete {
6666
);
6767
}
6868

69-
query.trim_end().to_owned()
69+
query.trim_end().to_string()
7070
}
7171
}
7272

src/insert/insert.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Insert {
8686
/// ```
8787
#[cfg(not(feature = "sqlite"))]
8888
pub fn insert_into(mut self, table_name: &str) -> Self {
89-
self._insert_into = table_name.trim().to_owned();
89+
self._insert_into = table_name.trim().to_string();
9090
self
9191
}
9292

@@ -116,7 +116,7 @@ impl Insert {
116116
/// INSERT INTO users (login) ON CONFLICT do nothing
117117
/// ```
118118
pub fn on_conflict(mut self, conflict: &str) -> Self {
119-
self._on_conflict = conflict.trim().to_owned();
119+
self._on_conflict = conflict.trim().to_string();
120120
self
121121
}
122122

@@ -142,7 +142,7 @@ impl Insert {
142142
/// ```
143143
#[cfg(not(feature = "sqlite"))]
144144
pub fn overriding(mut self, option: &str) -> Self {
145-
self._overriding = option.trim().to_owned();
145+
self._overriding = option.trim().to_string();
146146
self
147147
}
148148

@@ -214,7 +214,7 @@ impl Insert {
214214
/// insert into users (login, name) VALUES ('foo', 'Foo')
215215
/// ```
216216
pub fn raw(mut self, raw_sql: &str) -> Self {
217-
push_unique(&mut self._raw, raw_sql.trim().to_owned());
217+
push_unique(&mut self._raw, raw_sql.trim().to_string());
218218
self
219219
}
220220

@@ -240,7 +240,7 @@ impl Insert {
240240
/// INSERT INTO users (login, name) values ('foo', 'Foo')
241241
/// ```
242242
pub fn raw_after(mut self, clause: InsertClause, raw_sql: &str) -> Self {
243-
self._raw_after.push((clause, raw_sql.trim().to_owned()));
243+
self._raw_after.push((clause, raw_sql.trim().to_string()));
244244
self
245245
}
246246

@@ -266,7 +266,7 @@ impl Insert {
266266
/// insert into users (login, name) VALUES ('bar', 'Bar')
267267
/// ```
268268
pub fn raw_before(mut self, clause: InsertClause, raw_sql: &str) -> Self {
269-
self._raw_before.push((clause, raw_sql.trim().to_owned()));
269+
self._raw_before.push((clause, raw_sql.trim().to_string()));
270270
self
271271
}
272272

@@ -292,7 +292,7 @@ impl Insert {
292292
/// INSERT INTO users (login, name) VALUES ('foo', 'Foo'), ('bar', 'Bar')
293293
/// ```
294294
pub fn values(mut self, value: &str) -> Self {
295-
push_unique(&mut self._values, value.trim().to_owned());
295+
push_unique(&mut self._values, value.trim().to_string());
296296
self
297297
}
298298
}
@@ -324,7 +324,7 @@ impl Insert {
324324
/// INSERT INTO users RETURNING id, login
325325
/// ```
326326
pub fn returning(mut self, output_name: &str) -> Self {
327-
push_unique(&mut self._returning, output_name.trim().to_owned());
327+
push_unique(&mut self._returning, output_name.trim().to_string());
328328
self
329329
}
330330

@@ -374,7 +374,7 @@ impl Insert {
374374
/// FROM active_users
375375
/// ```
376376
pub fn with(mut self, name: &str, query: impl WithQuery + 'static) -> Self {
377-
self._with.push((name.trim().to_owned(), std::sync::Arc::new(query)));
377+
self._with.push((name.trim().to_string(), std::sync::Arc::new(query)));
378378
self
379379
}
380380
}
@@ -412,7 +412,7 @@ impl Insert {
412412
/// The `insert into` clause, this method overrides the previous value and can be used enabling the feature flag `sqlite`
413413
#[cfg(not(doc))]
414414
pub fn insert_into(mut self, expression: &str) -> Self {
415-
self._insert = (InsertVars::InsertInto, expression.trim().to_owned());
415+
self._insert = (InsertVars::InsertInto, expression.trim().to_string());
416416
self
417417
}
418418

@@ -445,7 +445,7 @@ impl Insert {
445445
/// INSERT OR abort into users (login, name)
446446
/// ```
447447
pub fn insert_or(mut self, expression: &str) -> Self {
448-
self._insert = (InsertVars::InsertOr, expression.trim().to_owned());
448+
self._insert = (InsertVars::InsertOr, expression.trim().to_string());
449449
self
450450
}
451451

@@ -471,7 +471,7 @@ impl Insert {
471471
/// REPLACE INTO users (login, name)
472472
/// ```
473473
pub fn replace_into(mut self, expression: &str) -> Self {
474-
self._insert = (InsertVars::ReplaceInto, expression.trim().to_owned());
474+
self._insert = (InsertVars::ReplaceInto, expression.trim().to_string());
475475
self
476476
}
477477
}

0 commit comments

Comments
 (0)