Skip to content

Commit cbb12dd

Browse files
authored
Update README.md
1 parent 4c7b1b5 commit cbb12dd

1 file changed

Lines changed: 69 additions & 19 deletions

File tree

README.md

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
# readsql
22

3-
Convert SQL to most human readable format
3+
Convert SQL to most human readable format. For the time being it upper cases SQL keywords, it might prettify of even suggest improvements of SQL code in the future. It converts SQL code and even SQL-strings in programming languages (only Python at the moment).
44

5-
It can convert SQL code and even SQL strings in other languages (only Python at the moment)
5+
So if we write
6+
7+
```sql
8+
select sushi, avg(price) from tokyo where ocean = 'pacific' group by sushi
9+
```
10+
11+
readsql will help us read it as
12+
13+
```sql
14+
SELECT sushi, AVG(price) FROM tokyo WHERE ocean = 'pacific' GROUP BY sushi
15+
```
616

717
# Installation
818

@@ -12,34 +22,73 @@ It can convert SQL code and even SQL strings in other languages (only Python at
1222

1323
1. Format SQL code provided in command line
1424
- `readsql <SQL_STRING> -s`
15-
2. Format an SQL file
16-
- `readsql <FILE_PATH>`
25+
2. Format an SQL file or folder
26+
- as in a folder, it will look for files ending with .sql or .py
27+
- `readsql <FILE_OR_FOLDER_PATH>`
28+
29+
It supports multiple strings and files or folders as well
30+
31+
1.
32+
```bash
33+
readsql <SQL_STRING1> <SQL_STRING2> -s
34+
```
1735

36+
2. In Python files it looks for `query` strings and formats them
37+
```bash
38+
readsql <FILE_OR_FOLDER_PATH1> <FILE_OR_FOLDER_PATH2>
39+
```
40+
41+
We can look for different strings in Python files with a `-py` arguments
42+
```bash
43+
readsql <FILE_OR_FOLDER_PATH> -py <PY_VAR1> <PY_VAR2>
44+
```
45+
1846
# Usage examples
1947

20-
1. `readsql <SQL_STRING> -s`
21-
- `readsql 'select sushi from tokyo' -s`
22-
- returns `SELECT sushi FROM tokyo`
23-
2. `readsql <FILE_PATH>`
24-
1. `readsql sql_example.sql`
25-
2. given a Python file (ends with `.py`), it looks for variables `query` and formats their insides
26-
- variable to represent SQL code can be changed with `-py` option
27-
- `readsql sql_in_python_variable_example.py -py sql`
28-
- given a Python file with this code
48+
1. `readsql 'select sushi from tokyo' -s` command returns
49+
- `SELECT sushi FROM tokyo`
50+
51+
2. a. `readsql sql_example.sql` command, while `sql_example.sql` is a SQL file with code as below,
52+
```sql
53+
select max(height), avg(mass), min(age) from jungle group by forest where animal=elephant;
54+
```
55+
replaces the file with this code
56+
```sql
57+
SELECT MAX(height), AVG(mass), MIN(age) FROM jungle GROUP BY forest WHERE animal=elephant;
58+
```
59+
60+
2.c. `readsql sql_in_python_variable_example.py` command, while `sql_in_python_variable_example.py` is a Python file with code as below,
2961
```python
3062
def get_query():
3163
limit = 6
3264
sql = f"SELEct speed from world where animal='dolphin' limit {limit}"
3365
return sql
3466
```
35-
converts it to
67+
replaces the file with this code
3668
```python
3769
def get_query():
3870
limit = 6
3971
sql = f"SELECT speed FROM world WHERE animal='dolphin' LIMIT {limit}"
4072
return sql
4173
```
4274

75+
2.c. `readsql sql_in_python_variable_example.py -py sql` command, while `sql_in_python_variable_example.py` is a Python file with code as below,
76+
```python
77+
def get_query():
78+
limit = 6
79+
sql = f"SELEct speed from world where animal='dolphin' limit {limit}"
80+
return sql
81+
```
82+
replaces the file with this code
83+
```python
84+
def get_query():
85+
limit = 6
86+
sql = f"SELECT speed FROM world WHERE animal='dolphin' LIMIT {limit}"
87+
return sql
88+
```
89+
90+
2.d. `readsql tests -n` command outputs all of the formated SQL code in `tests` folder, files are not replaced by the formatted version (`-n` argument stand for not-replace)
91+
4392
# Add a pre-commit hook
4493
How to add a [pre-commit](https://pre-commit.com/) hook of readsql?
4594
```yaml
@@ -51,12 +100,13 @@ repos:
51100
```
52101
53102
# Development
54-
Having the repo cloned
103+
Having the repo cloned dig into
55104
56-
- `python readsql tests/sql_example.sql` converts example SQL code to easier readable format
57-
- `python readsql tests/sql_in_python_example.py` converts example SQL code in Python (it looks for variables `query`)
58-
- we can change the SQL variable with `-py` option `python readsql tests/sql_in_python_variable_example.py -py sql`
59-
- `python readsql "select sushi from tokyo" -s` takes the `"select sushi from tokyo"` string as input and outputs it formatted
105+
- `python -m readsql "select sushi from tokyo" -s` takes the `"select sushi from tokyo"` string as input and outputs it formatted
106+
- `python -m readsql tests/sql_example.sql` converts example SQL code to easier readable format
107+
- `python -m readsql tests/sql_in_python_example.py` converts example SQL code in Python (it looks for variables `query`)
108+
- we can change the SQL variable with `-py` option `python -m readsql tests/sql_in_python_variable_example.py -py sql`
109+
- `python -m readsql tests` formats all Python and SQL files in `tests` folder
60110

61111
# Testing
62112

0 commit comments

Comments
 (0)