tdfs4ds.utils.view_text¶
tdfs4ds.utils.view_text
¶
The parts of a view definition: header, LOCKING modifier, query body.
One reader for CREATE|REPLACE [RECURSIVE] VIEW <name> [(<columns>)] AS
[LOCKING ...] <query> text, used wherever a view's query is needed: the
EXPLAIN of a process, the dataset sample, the query optimizer, its Approve
step and sql_invariants.keep_locking. Before it there were three regular
expressions, each wrong on a different shape (a AS inside a quoted name, a
(a) UNION (b) body losing its first and last parenthesis, a doubled header
on Approve).
Everything goes through :func:scan_sql, a small lexer that knows Teradata
string literals ('' is the only escape: a backslash is an ordinary
character), quoted identifiers and comments, so no rule ever looks inside a
literal or a comment.
Pure, stdlib only, no database.
__all__
module-attribute
¶
__all__ = ['Token', 'scan_sql', 'code_tokens', 'matching_close', 'ViewText', 'view_body', 'rebuild_view', 'explainable', 'approved_view_ddl']
_NUM_RE
module-attribute
¶
_LOCK_MODES
module-attribute
¶
Token
dataclass
¶
One lexical unit. kind is one of ws, line_comment,
block_comment, str, qident, bq (a back-quoted name, not
Teradata), ident, num, punct. depth is the parenthesis
depth the token sits at (a ( and its ) share the outer depth).
ViewText
dataclass
¶
A view definition split in its parts.
header-CREATE|REPLACE [RECURSIVE] VIEW <name> [(cols)] ASas written (leading comments included), None when the text is a query.name- the view name as written (quotes kept), None without header.columns- the column list of the header as written, None when absent.locking- the leadingLOCKING ... FOR ACCESS/LOCK ROW FOR ACCESSmodifier(s), verbatim, '' when none.locks- the same modifiers one by one.body- the query without header, locking, wrapping parentheses and trailing;.CURRENT VALIDTIMEand similar prefixes stay.
__init__
¶
__init__(header: Optional[str], name: Optional[str], columns: Optional[tuple], locking: str, body: str, locks: tuple = ()) -> None
_quoted_end
¶
End (exclusive) of the quoted run opening at i; a doubled quote is an escaped one. An unterminated run ends at the end of the text.
scan_sql
¶
Every token of sql, whitespace and comments included, in order.
Concatenating t.value of all tokens gives back sql exactly.
code_tokens
¶
The tokens that are neither whitespace nor comments.
matching_close
¶
Index in toks of the ) closing the ( at open_idx, or -1.
_parse_lock
¶
Index after a LOCKING|LOCK ... modifier starting at k, or -1.
_parse_header
¶
(index of the first body token, header text, name, columns) or None.
view_body
¶
Split ddl (a view definition or a plain query) into its parts.
Never raises: unreadable text gives header=None and the stripped text
as body.
explainable
¶
The text to put after EXPLAIN: for a view definition its query
(locking kept), for anything else sql unchanged.
rebuild_view
¶
REPLACE VIEW <view_name_sql> [(cols)] AS + locking + body.
vt_or_body is a :class:ViewText (its columns and locking are used;
locking overrides its locking when given) or a body text, which is read
with :func:view_body so a text that still carries a header never gives a
doubled header.
approved_view_ddl
¶
The REPLACE VIEW statement that installs best_sql as view_name_sql.
best_sql may be a body or a whole definition. The LOCKING modifier(s)
and the column list of original_sql (the view's current definition) are
kept: locking through sql_invariants.keep_locking.