Skip to content

Commit 6773cc0

Browse files
authored
Adds --engine-option to csvsql. (#1256)
feat(csvsql): adds --engine-option
1 parent f63a247 commit 6773cc0

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

CHANGELOG.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Unreleased
22
----------
33

4+
- feat: :doc:`/scripts/csvsql` adds a :code:`--engine-option` option.
45
- feat: :doc:`/scripts/sql2csv` adds a :code:`--execution-option` option.
56
- feat: :doc:`/scripts/sql2csv` uses the ``stream_results=True`` execution option, by default, to not load all data into memory at once.
67

csvkit/cli.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
import ast
23
import argparse
34
import bz2
45
import csv
@@ -560,6 +561,17 @@ def parse_column_identifiers(ids, column_names, column_offset=1, excluded_column
560561
return [c for c in columns if c not in excludes]
561562

562563

564+
def parse_list(pairs):
565+
options = {}
566+
for key, value in pairs:
567+
try:
568+
value = ast.literal_eval(value)
569+
except ValueError:
570+
pass
571+
options[key] = value
572+
return options
573+
574+
563575
# Adapted from https://github.com/pallets/click/blame/main/src/click/utils.py
564576
def _expand_args(args):
565577
out = []

csvkit/utilities/csvsql.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#!/usr/bin/env python
2-
32
import os.path
43
import sys
54

65
import agate
76
import agatesql # noqa: F401
87
from sqlalchemy import create_engine, dialects
98

10-
from csvkit.cli import CSVKitUtility, isatty
9+
from csvkit.cli import CSVKitUtility, isatty, parse_list
1110

1211
try:
1312
import importlib_metadata
@@ -33,6 +32,10 @@ def add_arguments(self):
3332
self.argparser.add_argument(
3433
'--db', dest='connection_string',
3534
help='If present, a SQLAlchemy connection string to use to directly execute generated SQL on a database.')
35+
self.argparser.add_argument(
36+
'--engine-option', dest='engine_option', nargs=2, action='append', default=[],
37+
help="A keyword argument to SQLAlchemy's create_engine(), as a space-separated pair. "
38+
"This option can be specified multiple times. For example: thick_mode True")
3639
self.argparser.add_argument(
3740
'--query', dest='queries', action='append',
3841
help='Execute one or more SQL queries delimited by ";" and output the result of the last query as CSV. '
@@ -137,7 +140,7 @@ def main(self):
137140
# Establish database validity before reading CSV files
138141
if self.args.connection_string:
139142
try:
140-
engine = create_engine(self.args.connection_string)
143+
engine = create_engine(self.args.connection_string, **parse_list(self.args.engine_option))
141144
except ImportError as e:
142145
raise ImportError(
143146
"You don't appear to have the necessary database backend installed for connection string you're "

csvkit/utilities/sql2csv.py

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
#!/usr/bin/env python
2-
import ast
3-
42
import agate
53
from sqlalchemy import create_engine
64

7-
from csvkit.cli import CSVKitUtility
8-
9-
10-
def parse_list(pairs):
11-
options = {}
12-
for key, value in pairs:
13-
try:
14-
value = ast.literal_eval(value)
15-
except ValueError:
16-
pass
17-
options[key] = value
18-
return options
5+
from csvkit.cli import CSVKitUtility, parse_list
196

207

218
class SQL2CSV(CSVKitUtility):

0 commit comments

Comments
 (0)