Przeglądaj źródła

cmd_db: added query subcommand

Daniel-Constantin Mierla 5 lat temu
rodzic
commit
7259482cd0
1 zmienionych plików z 24 dodań i 0 usunięć
  1. 24 0
      kamcli/commands/cmd_db.py

+ 24 - 0
kamcli/commands/cmd_db.py

@@ -74,6 +74,30 @@ def cli(ctx):
     pass
 
 
[email protected]("query", short_help="Run SQL statement")
[email protected](
+    "oformat",
+    "--output-format",
+    "-F",
+    type=click.Choice(["raw", "json", "table", "dict"]),
+    default=None,
+    help="Format the output",
+)
[email protected](
+    "ostyle",
+    "--output-style",
+    "-S",
+    default=None,
+    help="Style of the output (tabulate table format)",
+)
[email protected]("query", metavar="<query>")
+@pass_context
+def db_query(ctx, oformat, ostyle, query):
+    e = create_engine(ctx.gconfig.get("db", "rwurl"))
+    res = e.execute(query.encode("ascii", "ignore").decode())
+    ioutils_dbres_print(ctx, oformat, ostyle, res)
+
+
 @cli.command("connect", short_help="Launch db cli and connect to database")
 @pass_context
 def db_connect(ctx):