diff options
| author | Jesús Leganés Combarro "Piranna <piranna@gmail.com> | 2012-05-19 21:49:20 +0200 |
|---|---|---|
| committer | Jesús Leganés Combarro "Piranna <piranna@gmail.com> | 2012-05-19 21:49:20 +0200 |
| commit | 4a9800a74b8748d2044b285c6b7ca9eba5b626fb (patch) | |
| tree | 588f1bbd73c71d125955f97004446e3ae1808dff | |
| parent | 170bf768caa0651d028382be6578607a44c7571d (diff) | |
| parent | 42abbb1cb3217ba44203361221dd7537837eb05f (diff) | |
| download | sqlparse-4a9800a74b8748d2044b285c6b7ca9eba5b626fb.tar.gz | |
Merge branch 'milestone_0.2.0' of github.com:piranna/sqlparse into milestone_0.2.0
| -rw-r--r-- | sqlparse/__init__.py | 5 | ||||
| -rw-r--r-- | sqlparse/engine/__init__.py | 5 | ||||
| -rw-r--r-- | sqlparse/engine/filter.py | 127 | ||||
| -rw-r--r-- | sqlparse/pipeline.py | 8 |
4 files changed, 67 insertions, 78 deletions
diff --git a/sqlparse/__init__.py b/sqlparse/__init__.py index 1bbb637..1e39f08 100644 --- a/sqlparse/__init__.py +++ b/sqlparse/__init__.py @@ -57,7 +57,6 @@ def split(sql): stack.split_statements = True return [unicode(stmt) for stmt in stack.run(sql)] -from sqlparse.engine.filter import StatementFilter +from sqlparse.engine.filter import statementFilter def split2(stream): - splitter = StatementFilter() - return list(splitter.process(None, stream)) + return list(statementFilter(stream)) diff --git a/sqlparse/engine/__init__.py b/sqlparse/engine/__init__.py index 491d8a0..b38ad76 100644 --- a/sqlparse/engine/__init__.py +++ b/sqlparse/engine/__init__.py @@ -7,7 +7,7 @@ from sqlparse import lexer from sqlparse.engine import grouping -from sqlparse.engine.filter import StatementFilter +from sqlparse.engine.filter import statementFilter # XXX remove this when cleanup is complete Filter = object @@ -45,8 +45,7 @@ class FilterStack(object): if (self.stmtprocess or self.postprocess or self.split_statements or self._grouping): - splitter = StatementFilter() - stream = splitter(stream) + stream = statementFilter(stream) if self._grouping: diff --git a/sqlparse/engine/filter.py b/sqlparse/engine/filter.py index 63fd5d7..a920938 100644 --- a/sqlparse/engine/filter.py +++ b/sqlparse/engine/filter.py @@ -4,34 +4,28 @@ from sqlparse.sql import Statement, Token from sqlparse import tokens as T -class StatementFilter: +def statementFilter(stream): "Filter that split stream at individual statements" - def __init__(self): - self._in_declare = False - self._in_dbldollar = False - self._is_create = False - self._begin_depth = 0 + # init + statementFilter._in_declare = False + statementFilter._in_dbldollar = False + statementFilter._is_create = False + statementFilter._begin_depth = 0 - def _reset(self): - "Set the filter attributes to its default values" - self._in_declare = False - self._in_dbldollar = False - self._is_create = False - self._begin_depth = 0 - - def _change_splitlevel(self, ttype, value): + def _change_splitlevel(ttype, value): "Get the new split level (increase, decrease or remain equal)" # PostgreSQL if (ttype == T.Name.Builtin - and value.startswith('$') and value.endswith('$')): - if self._in_dbldollar: - self._in_dbldollar = False + and value.startswith('$') and value.endswith('$')): + if statementFilter._in_dbldollar: + statementFilter._in_dbldollar = False return -1 - else: - self._in_dbldollar = True - return 1 - elif self._in_dbldollar: + + statementFilter._in_dbldollar = True + return 1 + + elif statementFilter._in_dbldollar: return 0 # ANSI @@ -40,13 +34,13 @@ class StatementFilter: unified = value.upper() - if unified == 'DECLARE' and self._is_create: - self._in_declare = True + if unified == 'DECLARE' and statementFilter._is_create: + statementFilter._in_declare = True return 1 if unified == 'BEGIN': - self._begin_depth += 1 - if self._in_declare or self._is_create: + statementFilter._begin_depth += 1 + if statementFilter._in_declare or statementFilter._is_create: # FIXME(andi): This makes no sense. return 1 return 0 @@ -54,56 +48,59 @@ class StatementFilter: if unified == 'END': # Should this respect a preceeding BEGIN? # In CASE ... WHEN ... END this results in a split level -1. - self._begin_depth = max(0, self._begin_depth - 1) + statementFilter._begin_depth = max(0, statementFilter._begin_depth - 1) return -1 if ttype is T.Keyword.DDL and unified.startswith('CREATE'): - self._is_create = True + statementFilter._is_create = True return 0 if (unified in ('IF', 'FOR') - and self._is_create and self._begin_depth > 0): + and statementFilter._is_create and statementFilter._begin_depth > 0): return 1 # Default return 0 - def __call__(self, stream): - "Process the stream" - consume_ws = False - splitlevel = 0 - stmt = None - stmt_tokens = [] - - # Run over all stream tokens - for ttype, value in stream: - # Yield token if we finished a statement and there's no whitespaces - if consume_ws and ttype not in (T.Whitespace, T.Comment.Single): - stmt.tokens = stmt_tokens - yield stmt - - # Reset filter and prepare to process next statement - self._reset() - consume_ws = False - splitlevel = 0 - stmt = None - - # Create a new statement if we are not currently in one of them - if stmt is None: - stmt = Statement() - stmt_tokens = [] - - # Change current split level (increase, decrease or remain equal) - splitlevel += self._change_splitlevel(ttype, value) - - # Append the token to the current statement - stmt_tokens.append(Token(ttype, value)) - - # Check if we get the end of a statement - if splitlevel <= 0 and ttype is T.Punctuation and value == ';': - consume_ws = True - - # Yield pending statement (if any) - if stmt is not None: + # Process the stream + consume_ws = False + splitlevel = 0 + stmt = None + stmt_tokens = [] + + # Run over all stream tokens + for ttype, value in stream: + # Yield token if we finished a statement and there's no whitespaces + if consume_ws and ttype not in (T.Whitespace, T.Comment.Single): stmt.tokens = stmt_tokens yield stmt + + # Reset filter and prepare to process next statement + _in_declare = False + _in_dbldollar = False + _is_create = False + _begin_depth = 0 + + consume_ws = False + splitlevel = 0 + stmt = None + + # Create a new statement if we are not currently in one of them + if stmt == None: + stmt = Statement() + stmt_tokens = [] + + # Change current split level (increase, decrease or remain equal) + splitlevel += _change_splitlevel(ttype, value) + + # Append the token to the current statement + stmt_tokens.append(Token(ttype, value)) + + # Check if we get the end of a statement + if splitlevel <= 0 and ttype is T.Punctuation and value == ';': + consume_ws = True + + # Yield pending statement (if any) + if stmt: + stmt.tokens = stmt_tokens + yield stmt diff --git a/sqlparse/pipeline.py b/sqlparse/pipeline.py index 34dad19..5973b4c 100644 --- a/sqlparse/pipeline.py +++ b/sqlparse/pipeline.py @@ -17,13 +17,7 @@ class Pipeline(list): # Run the stream over all the filters on the pipeline for filter in self: - # Functions and callable objects (objects with '__call__' method) - if callable(filter): - stream = filter(stream) - - # Normal filters (objects with 'process' method) - else: - stream = filter.process(None, stream) + stream = filter(stream) # If last filter return a generator, staticalize it inside a list if isinstance(stream, GeneratorType): |
