MCPcopy
hub / github.com/dbcli/pgcli / adapter

Function adapter

pgcli/packages/formatter/sqlformatter.py:26–60  ·  view source on GitHub ↗
(data, headers, table_format=None, **kwargs)

Source from the content-addressed store, hash-verified

24
25
26def adapter(data, headers, table_format=None, **kwargs):
27 tables = extract_tables(formatter.query)
28 if len(tables) > 0:
29 table = tables[0]
30 if table[0]:
31 table_name = "{}.{}".format(*table[:2])
32 else:
33 table_name = table[1]
34 else:
35 table_name = "DUAL"
36 if table_format == "sql-insert":
37 h = '", "'.join(headers)
38 yield 'INSERT INTO "{}" ("{}") VALUES'.format(table_name, h)
39 prefix = " "
40 for d in data:
41 values = ", ".join(escape_for_sql_statement(v) for i, v in enumerate(d))
42 yield "{}({})".format(prefix, values)
43 if prefix == " ":
44 prefix = ", "
45 yield ";"
46 if table_format.startswith("sql-update"):
47 s = table_format.split("-")
48 keys = 1
49 if len(s) > 2:
50 keys = int(s[-1])
51 for d in data:
52 yield 'UPDATE "{}" SET'.format(table_name)
53 prefix = " "
54 for i, v in enumerate(d[keys:], keys):
55 yield '{}"{}" = {}'.format(prefix, headers[i], escape_for_sql_statement(v))
56 if prefix == " ":
57 prefix = ", "
58 f = '"{}" = {}'
59 where = (f.format(headers[i], escape_for_sql_statement(d[i])) for i in range(keys))
60 yield "WHERE {};".format(" AND ".join(where))
61
62
63def register_new_formatter(TabularOutputFormatter):

Callers 2

test_output_sql_insertFunction · 0.90
test_output_sql_updateFunction · 0.90

Calls 2

extract_tablesFunction · 0.90
escape_for_sql_statementFunction · 0.85

Tested by 2

test_output_sql_insertFunction · 0.72
test_output_sql_updateFunction · 0.72