Skip to content

Commit

Permalink
allow compressed file (#14)
Browse files Browse the repository at this point in the history
* allow compressed file

* empty commit

* fix
  • Loading branch information
ppq200 authored Oct 5, 2021
1 parent a81a607 commit 15b0cdc
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions sqlcsv/cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import csv
import sys
import gzip
import os

import click

from .command import Command


QUOTING = {
'ALL': csv.QUOTE_ALL,
'MINIMAL': csv.QUOTE_MINIMAL,
Expand Down Expand Up @@ -49,6 +49,13 @@ def _flag_to_bool(spec):
raise ValueError("Unknown nullable spec '{}'".format(spec))


def _open(filename, mode="rt"):
if os.path.splitext(filename)[1] == ".gz":
return gzip.open(filename, mode)
else:
return click.open_file(filename, mode)


@click.group()
@click.option('-u', '--db-url', envvar='SQLCSV_DB_URL',
help='Datasbase connection URL.')
Expand Down Expand Up @@ -103,23 +110,26 @@ def cli(
help='SELECT query string.')
@click.option('-f', '--sqlfile', type=click.File('r'), default=None,
help='SELECT query file.')
@click.option('-o', '--outfile', type=click.File('w'), default=sys.stdout,
@click.option('-o', '--outfile',
type=click.Path(dir_okay=False, writable=True, allow_dash=True), default="-",
help='Output CSV file.')
@click.pass_context
def select(ctx, sql, sqlfile, outfile):
'''Export data from relational databases using SELECT statement and output as CSV.'''
command = ctx.obj
sql = _get_sql(sql, sqlfile)

command.select(sql, outfile)
with _open(outfile, "wt") as f:
command.select(sql, f)


@cli.command()
@click.option('-s', '--sql', type=str, default=None,
help='INSERT query string.')
@click.option('-f', '--sqlfile', type=click.File('r'), default=None,
help='INSERT query file.')
@click.option('-i', '--infile', type=click.File('r'), default=sys.stdin,
@click.option('-i', '--infile',
type=click.Path(dir_okay=False, readable=True, allow_dash=True), default="-",
help='Input CSV file.')
@click.option('-t', '--types', type=str, required=True,
help="Types of each column, comma-separated e.g. 'int,float,string'.")
Expand All @@ -135,4 +145,5 @@ def insert(ctx, sql, sqlfile, infile, types, nullables, chunk_size):
types = tuple(map(_flag_to_type, types.split(',')))
nullables = nullables and tuple(map(_flag_to_bool, nullables.split(',')))

command.insert(sql, infile, types, nullables, chunk_size)
with _open(infile, "rt") as f:
command.insert(sql, f, types, nullables, chunk_size)

0 comments on commit 15b0cdc

Please sign in to comment.