Neater indentation for SQL used in schemas, closes #148

This commit is contained in:
Simon Willison 2020-09-07 11:12:45 -07:00
commit 59e3d4d171
2 changed files with 12 additions and 11 deletions

View file

@ -7,6 +7,7 @@ import itertools
import json import json
import os import os
import pathlib import pathlib
import textwrap
import uuid import uuid
SQLITE_MAX_VARS = 999 SQLITE_MAX_VARS = 999
@ -658,10 +659,10 @@ class Table(Queryable):
index_name = "idx_{}_{}".format( index_name = "idx_{}_{}".format(
self.name.replace(" ", "_"), "_".join(columns) self.name.replace(" ", "_"), "_".join(columns)
) )
sql = """ sql = textwrap.dedent("""
CREATE {unique}INDEX {if_not_exists}[{index_name}] CREATE {unique}INDEX {if_not_exists}[{index_name}]
ON [{table_name}] ({columns}); ON [{table_name}] ({columns});
""".format( """).strip().format(
index_name=index_name, index_name=index_name,
table_name=self.name, table_name=self.name,
columns=", ".join("[{}]".format(c) for c in columns), columns=", ".join("[{}]".format(c) for c in columns),
@ -779,12 +780,12 @@ class Table(Queryable):
self, columns, fts_version="FTS5", create_triggers=False, tokenize=None self, columns, fts_version="FTS5", create_triggers=False, tokenize=None
): ):
"Enables FTS on the specified columns." "Enables FTS on the specified columns."
sql = """ sql = textwrap.dedent("""
CREATE VIRTUAL TABLE [{table}_fts] USING {fts_version} ( CREATE VIRTUAL TABLE [{table}_fts] USING {fts_version} (
{columns},{tokenize} {columns},{tokenize}
content=[{table}] content=[{table}]
); );
""".format( """).strip().format(
table=self.name, table=self.name,
columns=", ".join("[{}]".format(c) for c in columns), columns=", ".join("[{}]".format(c) for c in columns),
fts_version=fts_version, fts_version=fts_version,
@ -798,7 +799,7 @@ class Table(Queryable):
if create_triggers: if create_triggers:
old_cols = ", ".join("old.[{}]".format(c) for c in columns) old_cols = ", ".join("old.[{}]".format(c) for c in columns)
new_cols = ", ".join("new.[{}]".format(c) for c in columns) new_cols = ", ".join("new.[{}]".format(c) for c in columns)
triggers = """ triggers = textwrap.dedent("""
CREATE TRIGGER [{table}_ai] AFTER INSERT ON [{table}] BEGIN CREATE TRIGGER [{table}_ai] AFTER INSERT ON [{table}] BEGIN
INSERT INTO [{table}_fts] (rowid, {columns}) VALUES (new.rowid, {new_cols}); INSERT INTO [{table}_fts] (rowid, {columns}) VALUES (new.rowid, {new_cols});
END; END;
@ -809,7 +810,7 @@ class Table(Queryable):
INSERT INTO [{table}_fts] ([{table}_fts], rowid, {columns}) VALUES('delete', old.rowid, {old_cols}); INSERT INTO [{table}_fts] ([{table}_fts], rowid, {columns}) VALUES('delete', old.rowid, {old_cols});
INSERT INTO [{table}_fts] (rowid, {columns}) VALUES (new.rowid, {new_cols}); INSERT INTO [{table}_fts] (rowid, {columns}) VALUES (new.rowid, {new_cols});
END; END;
""".format( """).strip().format(
table=self.name, table=self.name,
columns=", ".join("[{}]".format(c) for c in columns), columns=", ".join("[{}]".format(c) for c in columns),
old_cols=old_cols, old_cols=old_cols,