From fbf9a576fe40ad8e4d51bb922bb454c317f73403 Mon Sep 17 00:00:00 2001 From: Simon Heisterkamp Date: Sun, 1 Jan 2023 14:20:52 +0000 Subject: [PATCH] additional documentation --- docs/source/extending.rst | 10 ++++++++++ sqlparse/lexer.py | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/source/extending.rst b/docs/source/extending.rst index f1bd5512..97b7d389 100644 --- a/docs/source/extending.rst +++ b/docs/source/extending.rst @@ -44,7 +44,12 @@ a keyword to the lexer: from sqlparse import keywords from sqlparse.lexer import Lexer + # get the lexer singleton object to configure it lex = Lexer() + + # Clear the default configurations. + # After this call, reg-exps and keyword dictionaries need to be loaded + # to make the lexer functional again. lex.clear() my_regex = (r"ZORDER\s+BY\b", sqlparse.tokens.Keyword) @@ -55,12 +60,17 @@ a keyword to the lexer: + [my_regex] + keywords.SQL_REGEX[38:] ) + + # add the default keyword dictionaries lex.add_keywords(keywords.KEYWORDS_COMMON) lex.add_keywords(keywords.KEYWORDS_ORACLE) lex.add_keywords(keywords.KEYWORDS_PLPGSQL) lex.add_keywords(keywords.KEYWORDS_HQL) lex.add_keywords(keywords.KEYWORDS_MSACCESS) lex.add_keywords(keywords.KEYWORDS) + + # add a custom keyword dictionary lex.add_keywords({'BAR', sqlparse.tokens.Keyword}) + # no configuration is passed here. The lexer is used as a singleton. sqlparse.parse("select * from foo zorder by bar;") diff --git a/sqlparse/lexer.py b/sqlparse/lexer.py index 657177cb..6e17fca2 100644 --- a/sqlparse/lexer.py +++ b/sqlparse/lexer.py @@ -50,7 +50,9 @@ def __init__(self): def clear(self): """Clear all syntax configurations. - Useful if you want to load a reduced set of syntax configurations.""" + Useful if you want to load a reduced set of syntax configurations. + After this call, reg-exps and keyword dictionaries need to be loaded + to make the lexer functional again.""" self._SQL_REGEX = [] self._keywords = []