MCPcopy Create free account
hub / github.com/wireservice/csvkit / main

Method main

csvkit/utilities/sql2csv.py:54–94  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

52 )
53
54 def main(self):
55 if self.additional_input_expected() and not self.args.query:
56 self.argparser.error('You must provide an input file or piped data.')
57
58 try:
59 engine = create_engine(self.args.connection_string, **parse_list(self.args.engine_option))
60 except ImportError as e:
61 raise ImportError(
62 "You don't appear to have the necessary database backend installed for connection string you're "
63 "trying to use. Available backends include:\n\nPostgreSQL:\tpip install psycopg2\nMySQL:\t\tpip "
64 "install mysql-connector-python OR pip install mysqlclient\n\nFor details on connection strings "
65 "and other backends, please see the SQLAlchemy documentation on dialects at:\n\n"
66 "https://www.sqlalchemy.org/docs/dialects/"
67 ) from e
68
69 connection = engine.connect()
70
71 if self.args.query:
72 query = self.args.query.strip()
73 else:
74 query = ""
75
76 self.input_file = self._open_input_file(self.args.input_path)
77
78 for line in self.input_file:
79 query += line
80
81 self.input_file.close()
82
83 rows = connection.execution_options(**parse_list(self.args.execution_option)).exec_driver_sql(query)
84 output = agate.csv.writer(self.output_file, **self.writer_kwargs)
85
86 if rows.returns_rows:
87 if not self.args.no_header_row:
88 output.writerow(rows._metadata.keys)
89
90 for row in rows:
91 output.writerow(row)
92
93 connection.close()
94 engine.dispose()
95
96
97def launch_new_instance():

Callers

nothing calls this directly

Calls 4

parse_listFunction · 0.90
_open_input_fileMethod · 0.80
closeMethod · 0.80

Tested by

no test coverage detected