sqlnbfmt 0.1.4__tar.gz → 0.1.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sqlnbfmt-0.1.4/sqlnbfmt.egg-info → sqlnbfmt-0.1.6}/PKG-INFO +4 -5
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6}/README.md +4 -5
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6}/pyproject.toml +1 -1
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6}/sqlnbfmt/formatter.py +35 -15
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6/sqlnbfmt.egg-info}/PKG-INFO +4 -5
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6}/LICENSE +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6}/setup.cfg +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6}/sqlnbfmt/__init__.py +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6}/sqlnbfmt.egg-info/SOURCES.txt +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6}/sqlnbfmt.egg-info/dependency_links.txt +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6}/sqlnbfmt.egg-info/entry_points.txt +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6}/sqlnbfmt.egg-info/requires.txt +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6}/sqlnbfmt.egg-info/top_level.txt +0 -0
- {sqlnbfmt-0.1.4 → sqlnbfmt-0.1.6}/tests/test_formatter.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: sqlnbfmt
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.6
|
|
4
4
|
Summary: A tool to format SQL code in Jupyter notebooks.
|
|
5
5
|
Author-email: Qing <qingye779@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -61,14 +61,13 @@ pip install pre-commit
|
|
|
61
61
|
2. Add to `.pre-commit-config.yaml`:
|
|
62
62
|
```yaml
|
|
63
63
|
repos:
|
|
64
|
-
- repo:
|
|
64
|
+
- repo: https://github.com/flyersworder/sqlnbfmt
|
|
65
|
+
rev: v0.1.1
|
|
65
66
|
hooks:
|
|
66
67
|
- id: sqlnbfmt
|
|
67
68
|
name: sqlnbfmt
|
|
68
|
-
entry: sqlnbfmt
|
|
69
|
-
language: system
|
|
70
69
|
types: [jupyter]
|
|
71
|
-
args: [--config, config.yaml, --dialect,
|
|
70
|
+
args: [--config, config.yaml, --dialect, postgres]
|
|
72
71
|
```
|
|
73
72
|
Please fun the following command in your CMD for help:
|
|
74
73
|
|
|
@@ -45,14 +45,13 @@ pip install pre-commit
|
|
|
45
45
|
2. Add to `.pre-commit-config.yaml`:
|
|
46
46
|
```yaml
|
|
47
47
|
repos:
|
|
48
|
-
- repo:
|
|
48
|
+
- repo: https://github.com/flyersworder/sqlnbfmt
|
|
49
|
+
rev: v0.1.1
|
|
49
50
|
hooks:
|
|
50
51
|
- id: sqlnbfmt
|
|
51
52
|
name: sqlnbfmt
|
|
52
|
-
entry: sqlnbfmt
|
|
53
|
-
language: system
|
|
54
53
|
types: [jupyter]
|
|
55
|
-
args: [--config, config.yaml, --dialect,
|
|
54
|
+
args: [--config, config.yaml, --dialect, postgres]
|
|
56
55
|
```
|
|
57
56
|
Please fun the following command in your CMD for help:
|
|
58
57
|
|
|
@@ -140,4 +139,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
|
|
|
140
139
|
- All contributors and early adopters who helped shape this tool
|
|
141
140
|
|
|
142
141
|
---
|
|
143
|
-
Made with ♥️ by the sqlnbfmt team
|
|
142
|
+
Made with ♥️ by the sqlnbfmt team
|
|
@@ -223,27 +223,42 @@ class SQLStringFormatter(ast.NodeTransformer):
|
|
|
223
223
|
return "".join(sql_parts), placeholders
|
|
224
224
|
|
|
225
225
|
def format_sql_node(
|
|
226
|
-
self,
|
|
226
|
+
self,
|
|
227
|
+
node: Union[ast.Constant, ast.JoinedStr],
|
|
228
|
+
force_single_line: bool = False,
|
|
229
|
+
in_function: bool = False
|
|
227
230
|
) -> Optional[ast.AST]:
|
|
228
231
|
"""Formats SQL code in AST nodes."""
|
|
229
232
|
try:
|
|
230
233
|
if isinstance(node, ast.Constant) and isinstance(node.value, str):
|
|
231
234
|
if not self.is_likely_sql(node.value):
|
|
232
235
|
return None
|
|
236
|
+
|
|
233
237
|
formatted_sql = format_sql_code(
|
|
234
238
|
node.value,
|
|
235
239
|
self.dialect,
|
|
236
240
|
self.config,
|
|
237
241
|
force_single_line=force_single_line,
|
|
238
242
|
)
|
|
243
|
+
|
|
239
244
|
if formatted_sql != node.value:
|
|
240
245
|
self.changed = True
|
|
241
|
-
|
|
242
|
-
|
|
246
|
+
if in_function and '\n' in formatted_sql:
|
|
247
|
+
# Apply Black-style indentation for function arguments
|
|
248
|
+
lines = formatted_sql.split('\n')
|
|
249
|
+
# Indent all lines including the first SQL line
|
|
250
|
+
indented_lines = [
|
|
251
|
+
" " * 4 + line if line.strip() else line
|
|
252
|
+
for line in lines
|
|
253
|
+
]
|
|
254
|
+
formatted_sql = '\n'.join(indented_lines)
|
|
255
|
+
|
|
256
|
+
# Reconstruct the string with appropriate quotes
|
|
243
257
|
formatted_str = f'"""\n{formatted_sql}\n"""'
|
|
244
258
|
else:
|
|
245
|
-
|
|
246
|
-
|
|
259
|
+
# Ensure newlines are added around the formatted SQL
|
|
260
|
+
formatted_str = f'"""\n{formatted_sql}\n"""'
|
|
261
|
+
|
|
247
262
|
formatted_node = ast.parse(formatted_str).body[0].value
|
|
248
263
|
return formatted_node
|
|
249
264
|
|
|
@@ -265,18 +280,26 @@ class SQLStringFormatter(ast.NodeTransformer):
|
|
|
265
280
|
|
|
266
281
|
if placeholders:
|
|
267
282
|
for placeholder in placeholders.keys():
|
|
268
|
-
if not placeholder:
|
|
283
|
+
if not placeholder:
|
|
269
284
|
continue
|
|
270
|
-
# Remove quotes around placeholders in formatted SQL
|
|
271
285
|
formatted_sql = formatted_sql.replace(f"'{placeholder}'", placeholder)
|
|
272
286
|
formatted_sql = formatted_sql.replace(f'"{placeholder}"', placeholder)
|
|
273
287
|
|
|
274
288
|
if formatted_sql != sql_str:
|
|
275
289
|
self.changed = True
|
|
290
|
+
# Apply Black-style indentation for function arguments
|
|
291
|
+
if in_function and '\n' in formatted_sql:
|
|
292
|
+
lines = formatted_sql.split('\n')
|
|
293
|
+
# Indent all lines including the first SQL line
|
|
294
|
+
indented_lines = [
|
|
295
|
+
" " * 4 + line if line.strip() else line
|
|
296
|
+
for line in lines
|
|
297
|
+
]
|
|
298
|
+
formatted_sql = '\n'.join(indented_lines)
|
|
299
|
+
|
|
276
300
|
# Reconstruct the f-string
|
|
277
301
|
new_values = []
|
|
278
302
|
if not placeholders:
|
|
279
|
-
# If no placeholders, just create a simple f-string
|
|
280
303
|
new_values.append(ast.Constant(value=formatted_sql))
|
|
281
304
|
else:
|
|
282
305
|
pattern = re.compile('|'.join(re.escape(k) for k in placeholders.keys() if k))
|
|
@@ -285,7 +308,6 @@ class SQLStringFormatter(ast.NodeTransformer):
|
|
|
285
308
|
match = pattern.search(formatted_sql, idx)
|
|
286
309
|
if match:
|
|
287
310
|
if match.start() > idx:
|
|
288
|
-
# Add preceding text as Constant
|
|
289
311
|
new_values.append(ast.Constant(value=formatted_sql[idx:match.start()]))
|
|
290
312
|
placeholder = match.group()
|
|
291
313
|
expr_str = placeholders[placeholder].strip()
|
|
@@ -301,23 +323,20 @@ class SQLStringFormatter(ast.NodeTransformer):
|
|
|
301
323
|
))
|
|
302
324
|
idx = match.end()
|
|
303
325
|
else:
|
|
304
|
-
# Add the rest of the string as Constant
|
|
305
326
|
new_values.append(ast.Constant(value=formatted_sql[idx:]))
|
|
306
327
|
break
|
|
307
328
|
|
|
308
329
|
new_node = ast.JoinedStr(values=new_values)
|
|
309
|
-
# Wrap the f-string with adjusted triple quotes
|
|
310
330
|
if '\n' in formatted_sql:
|
|
311
|
-
# Multi-line f-string
|
|
312
331
|
formatted_fstring = ast.JoinedStr(values=[
|
|
313
332
|
ast.Constant(value='\n'),
|
|
314
333
|
*new_node.values,
|
|
315
334
|
ast.Constant(value='\n')
|
|
316
335
|
])
|
|
317
336
|
else:
|
|
318
|
-
# Single-line f-string
|
|
319
337
|
formatted_fstring = new_node
|
|
320
338
|
return formatted_fstring
|
|
339
|
+
|
|
321
340
|
return None
|
|
322
341
|
except SQLFormattingError as e:
|
|
323
342
|
self.logger.warning(f"SQL formatting skipped: {e}")
|
|
@@ -340,12 +359,13 @@ class SQLStringFormatter(ast.NodeTransformer):
|
|
|
340
359
|
if any(name in func_name for name in self.config.function_names):
|
|
341
360
|
for idx, arg in enumerate(node.args):
|
|
342
361
|
if isinstance(arg, (ast.Constant, ast.JoinedStr)):
|
|
343
|
-
|
|
362
|
+
# Format with Black-style indentation for function arguments
|
|
363
|
+
formatted_node = self.format_sql_node(arg, in_function=True)
|
|
344
364
|
if formatted_node:
|
|
345
365
|
node.args[idx] = formatted_node
|
|
346
366
|
for keyword in node.keywords:
|
|
347
367
|
if isinstance(keyword.value, (ast.Constant, ast.JoinedStr)):
|
|
348
|
-
formatted_node = self.format_sql_node(keyword.value)
|
|
368
|
+
formatted_node = self.format_sql_node(keyword.value, in_function=True)
|
|
349
369
|
if formatted_node:
|
|
350
370
|
keyword.value = formatted_node
|
|
351
371
|
return self.generic_visit(node)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: sqlnbfmt
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.6
|
|
4
4
|
Summary: A tool to format SQL code in Jupyter notebooks.
|
|
5
5
|
Author-email: Qing <qingye779@gmail.com>
|
|
6
6
|
License: MIT
|
|
@@ -61,14 +61,13 @@ pip install pre-commit
|
|
|
61
61
|
2. Add to `.pre-commit-config.yaml`:
|
|
62
62
|
```yaml
|
|
63
63
|
repos:
|
|
64
|
-
- repo:
|
|
64
|
+
- repo: https://github.com/flyersworder/sqlnbfmt
|
|
65
|
+
rev: v0.1.1
|
|
65
66
|
hooks:
|
|
66
67
|
- id: sqlnbfmt
|
|
67
68
|
name: sqlnbfmt
|
|
68
|
-
entry: sqlnbfmt
|
|
69
|
-
language: system
|
|
70
69
|
types: [jupyter]
|
|
71
|
-
args: [--config, config.yaml, --dialect,
|
|
70
|
+
args: [--config, config.yaml, --dialect, postgres]
|
|
72
71
|
```
|
|
73
72
|
Please fun the following command in your CMD for help:
|
|
74
73
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|