|
3 | 3 | """
|
4 | 4 | Additional commands to add to the CLI beyond the OpenAPI spec.
|
5 | 5 | """
|
6 |
| - |
| 6 | +from __future__ import print_function |
| 7 | +import functools |
7 | 8 | import os
|
| 9 | +import sys |
8 | 10 |
|
9 | 11 | import click
|
10 | 12 | import requests
|
@@ -70,6 +72,90 @@ def files_download_cmd(file_id, path):
|
70 | 72 | civis_to_file(file_id, f)
|
71 | 73 |
|
72 | 74 |
|
| 75 | +@click.command('sql') |
| 76 | +@click.option('--dbname', '-d', type=str, required=True, |
| 77 | + help='Execute the query on this Civis Platform database') |
| 78 | +@click.option('--command', '-c', type=str, default=None, |
| 79 | + help='Execute a single input command string') |
| 80 | +@click.option('--filename', '-f', type=click.Path(exists=True), |
| 81 | + help='Execute a query read from the given file') |
| 82 | +@click.option('--output', '-o', type=click.Path(), |
| 83 | + help='Download query results to this file') |
| 84 | +@click.option('--quiet', '-q', is_flag=True, help='Suppress screen output') |
| 85 | +@click.option('-n', type=int, default=100, |
| 86 | + help="Display up to this many rows of the result. Max 100.") |
| 87 | +def sql_cmd(dbname, command, filename, output, quiet, n): |
| 88 | + """\b Execute a SQL query in Civis Platform |
| 89 | +
|
| 90 | + If neither a command nor an input file is specified, read |
| 91 | + the SQL command from stdin. |
| 92 | + If writing to an output file, use a Civis SQL script and write the |
| 93 | + entire query output to the specified file. |
| 94 | + If not writing to an output file, use a Civis Query, and return a |
| 95 | + preview of the results, up to a maximum of 100 rows. |
| 96 | + """ |
| 97 | + if filename: |
| 98 | + with open(filename, 'rt') as f: |
| 99 | + sql = f.read() |
| 100 | + elif not command: |
| 101 | + # Read the SQL query from user input. This also allows use of a heredoc |
| 102 | + lines = [] |
| 103 | + while True: |
| 104 | + try: |
| 105 | + _i = input() |
| 106 | + except (KeyboardInterrupt, EOFError): |
| 107 | + # The end of a heredoc produces an EOFError. |
| 108 | + break |
| 109 | + if not _i: |
| 110 | + break |
| 111 | + else: |
| 112 | + lines.append(_i) |
| 113 | + sql = '\n'.join(lines) |
| 114 | + else: |
| 115 | + sql = command |
| 116 | + |
| 117 | + if not sql: |
| 118 | + # If the user didn't enter a query, exit. |
| 119 | + if not quiet: |
| 120 | + print('Did not receive a SQL query.', file=sys.stderr) |
| 121 | + return |
| 122 | + |
| 123 | + if not quiet: |
| 124 | + print('\nExecuting query...', file=sys.stderr) |
| 125 | + if output: |
| 126 | + fut = civis.io.civis_to_csv(output, sql, database=dbname) |
| 127 | + fut.result() # Block for completion and raise exceptions if any |
| 128 | + if not quiet: |
| 129 | + print("Downloaded the result of the query to %s." % output, |
| 130 | + file=sys.stderr) |
| 131 | + else: |
| 132 | + fut = civis.io.query_civis(sql, database=dbname, |
| 133 | + preview_rows=n, polling_interval=3) |
| 134 | + cols = fut.result()['result_columns'] |
| 135 | + rows = fut.result()['result_rows'] |
| 136 | + if not quiet: |
| 137 | + print('...Query complete.\n', file=sys.stderr) |
| 138 | + print(_str_table_result(cols, rows)) |
| 139 | + |
| 140 | + |
| 141 | +def _str_table_result(cols, rows): |
| 142 | + """Turn a Civis Query result into a readable table.""" |
| 143 | + # Determine the maximum width of each column. |
| 144 | + # First find the width of each element in each row, then find the max |
| 145 | + # width in each position. |
| 146 | + max_len = functools.reduce( |
| 147 | + lambda x, y: [max(z) for z in zip(x, y)], |
| 148 | + [[len(_v) for _v in _r] for _r in [cols] + rows]) |
| 149 | + |
| 150 | + header_str = " | ".join("{0:<{width}}".format(_v, width=_l) |
| 151 | + for _l, _v in zip(max_len, cols)) |
| 152 | + tb_strs = [header_str, len(header_str) * '-'] |
| 153 | + for row in rows: |
| 154 | + tb_strs.append(" | ".join("{0:>{width}}".format(_v, width=_l) |
| 155 | + for _l, _v in zip(max_len, row))) |
| 156 | + return '\n'.join(tb_strs) |
| 157 | + |
| 158 | + |
73 | 159 | @click.command('download')
|
74 | 160 | @click.argument('notebook_id', type=int)
|
75 | 161 | @click.argument('path')
|
|
0 commit comments