Skip to content

Commit 29740bd

Browse files
authored
Add option to convert "<>" to "!=" (#18)
1 parent d5cafeb commit 29740bd

File tree

15 files changed

+138
-7
lines changed

15 files changed

+138
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ If there is no configuration file, the default values are used.
8282
| [`remove_table_as_keyword`](docs/options/remove_table_as_keyword.md) | bool | Remove `AS` in table aliases. | true |
8383
| [`remove_redundant_nest`](docs/options/remove_redundant_nest.md) | bool | Remove redundant parentheses. (e.g. `(((foo)))``(foo)`) | true |
8484
| [`complement_sql_id`](docs/options/complement_sql_id.md) | bool | Complement [SQL ID](https://palette-doc.rtfa.as/coding-standards/forSQL/SQL%E3%82%B3%E3%83%BC%E3%83%87%E3%82%A3%E3%83%B3%E3%82%B0%E8%A6%8F%E7%B4%84%EF%BC%88uroboroSQL%EF%BC%89.html#sql-%E8%AD%98%E5%88%A5%E5%AD%90). | false |
85-
| [`convert_double_colon_cast`](docs/options/convert_double_colon_cast.md) | bool | Convert casts by `X::type` to the form `CAST(X AS type)`. | true |
85+
| [`convert_double_colon_cast`](docs/options/convert_double_colon_cast.md) | bool | Convert casts by `X::type` to the form `CAST(X AS type)`. | true |
86+
| [`unify_not_equal`](docs/options/unify_not_equal.md) | bool | Convert comparison operator `<>` to `!=` | true |
8687

8788
## Structure
8889

crates/uroborosql-fmt/src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ fn default_convert_double_colon_cast() -> bool {
6565
true
6666
}
6767

68+
/// unify_not_equalのデフォルト値(true)
69+
fn default_unify_not_equal() -> bool {
70+
true
71+
}
72+
6873
#[derive(Serialize, Deserialize, Debug)]
6974
#[serde(rename_all = "lowercase")]
7075
pub(crate) enum Case {
@@ -138,6 +143,9 @@ pub struct Config {
138143
/// `X::type`のキャストを`CAST(X AS type)`に変換する
139144
#[serde(default = "default_convert_double_colon_cast")]
140145
pub(crate) convert_double_colon_cast: bool,
146+
/// not_equalを!=に統一する
147+
#[serde(default = "default_unify_not_equal")]
148+
pub(crate) unify_not_equal: bool,
141149
}
142150

143151
impl Config {
@@ -158,6 +166,7 @@ impl Config {
158166
remove_redundant_nest: default_remove_redundant_nest(),
159167
complement_sql_id: default_complement_sql_id(),
160168
convert_double_colon_cast: default_convert_double_colon_cast(),
169+
unify_not_equal: default_unify_not_equal(),
161170
}
162171
}
163172

@@ -201,6 +210,7 @@ pub(crate) fn load_never_complement_settings() {
201210
remove_table_as_keyword: false,
202211
remove_redundant_nest: false,
203212
convert_double_colon_cast: false,
213+
unify_not_equal: false,
204214
};
205215

206216
*CONFIG.write().unwrap() = config;

crates/uroborosql-fmt/src/visitor/expr/binary.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use tree_sitter::TreeCursor;
22

33
use crate::{
4+
config::CONFIG,
45
cst::*,
56
error::UroboroSQLFmtError,
67
visitor::{ensure_kind, Visitor},
@@ -27,7 +28,12 @@ impl Visitor {
2728

2829
// 演算子
2930
let op_node = cursor.node();
30-
let op_str = op_node.utf8_text(src.as_ref()).unwrap().to_string();
31+
let mut op_str = op_node.utf8_text(src.as_ref()).unwrap().to_string();
32+
33+
// unify_not_equalがtrueの場合は <> を != に統一する
34+
if CONFIG.read().unwrap().unify_not_equal && op_str == "<>" {
35+
op_str = "!=".to_string();
36+
}
3137

3238
cursor.goto_next_sibling();
3339
// cursor -> _expression

crates/uroborosql-fmt/testfiles/config_test/configs/config1.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"remove_table_as_keyword": false,
1212
"remove_redundant_parentheses": true,
1313
"complement_sql_id": true,
14-
"convert_double_colon_cast": true
14+
"convert_double_colon_cast": true,
15+
"unify_not_equal": true
1516
}

crates/uroborosql-fmt/testfiles/config_test/configs/config2.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"remove_table_as_keyword": true,
1212
"remove_redundant_nest": false,
1313
"complement_sql_id": false,
14-
"convert_double_colon_cast": true
14+
"convert_double_colon_cast": true,
15+
"unify_not_equal": true
1516
}

crates/uroborosql-fmt/testfiles/config_test/configs/config3.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"remove_table_as_keyword": false,
1212
"remove_redundant_parentheses": true,
1313
"complement_sql_id": false,
14-
"convert_double_colon_cast": false
14+
"convert_double_colon_cast": false,
15+
"unify_not_equal": false
1516
}

crates/uroborosql-fmt/testfiles/config_test/configs/config4.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"remove_table_as_keyword": false,
1212
"remove_redundant_nest": false,
1313
"complement_sql_id": false,
14-
"convert_double_colon_cast": false
14+
"convert_double_colon_cast": false,
15+
"unify_not_equal": false
1516
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SELECT /* _SQL_ID_ */
2+
*
3+
FROM
4+
STUDENTS
5+
WHERE
6+
STUDENT_ID != 2
7+
;
8+
SELECT
9+
*
10+
FROM
11+
STUDENTS
12+
WHERE
13+
STUDENT_ID != 2
14+
;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SELECT
2+
*
3+
FROM
4+
students
5+
WHERE
6+
student_id != 2
7+
;
8+
SELECT
9+
*
10+
FROM
11+
students
12+
WHERE
13+
student_id != 2
14+
;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
select
2+
*
3+
from
4+
students
5+
where
6+
student_id <> 2
7+
;
8+
select
9+
*
10+
from
11+
students
12+
where
13+
student_id != 2
14+
;

0 commit comments

Comments
 (0)