summaryrefslogtreecommitdiff
path: root/sqlparse
diff options
context:
space:
mode:
authorJesús Leganés Combarro "Piranna" <piranna@gmail.com>2012-05-19 22:48:11 +0200
committerJesús Leganés Combarro "Piranna" <piranna@gmail.com>2012-05-19 22:48:11 +0200
commit9f717a9b72124231e561fcd190eafeb021c22bdd (patch)
tree2bef49977c1ba3cd8b28fae6fb7fb7483c59f8f4 /sqlparse
parent057864594e126158c23bc8997647f8a43608959c (diff)
parent0a43050ab370a90f1036f37bf27b082e40419546 (diff)
downloadsqlparse-9f717a9b72124231e561fcd190eafeb021c22bdd.tar.gz
Merge branch 'master' into milestone_0.1.5
Diffstat (limited to 'sqlparse')
-rw-r--r--sqlparse/engine/__init__.py2
-rw-r--r--sqlparse/filters.py14
-rw-r--r--sqlparse/functions.py44
-rw-r--r--sqlparse/sql.py9
-rw-r--r--sqlparse/utils.py4
5 files changed, 71 insertions, 2 deletions
diff --git a/sqlparse/engine/__init__.py b/sqlparse/engine/__init__.py
index 3e2822b..c30b6ca 100644
--- a/sqlparse/engine/__init__.py
+++ b/sqlparse/engine/__init__.py
@@ -61,6 +61,8 @@ class FilterStack(object):
def _run1(stream):
ret = []
for stmt in stream:
+ for i in stmt.flatten():
+ print repr(i)
for filter_ in self.stmtprocess:
filter_.process(self, stmt)
ret.append(stmt)
diff --git a/sqlparse/filters.py b/sqlparse/filters.py
index 0fbcbda..fd1bb2b 100644
--- a/sqlparse/filters.py
+++ b/sqlparse/filters.py
@@ -7,6 +7,8 @@ from warnings import warn
from sqlparse import sql, tokens as T
from sqlparse.engine import FilterStack
+from sqlparse.lexer import tokenize
+from sqlparse.pipeline import Pipeline
from sqlparse.tokens import (Comment, Comparison, Keyword, Name, Punctuation,
String, Whitespace)
from sqlparse.utils import memoize_generator
@@ -687,3 +689,15 @@ class Limit:
return stream[4 - index][1]
return -1
+
+
+def Compact(sql, includePath="sql"):
+ """Function that return a compacted version of the input SQL query"""
+ pipe = Pipeline()
+
+ pipe.append(tokenize)
+ pipe.append(IncludeStatement(includePath))
+ pipe.append(StripComments())
+ pipe.append(StripWhitespace)
+
+ return pipe(sql)
diff --git a/sqlparse/functions.py b/sqlparse/functions.py
new file mode 100644
index 0000000..aaf6fb8
--- /dev/null
+++ b/sqlparse/functions.py
@@ -0,0 +1,44 @@
+'''
+Created on 17/05/2012
+
+@author: piranna
+
+Several utility functions to extract info from the SQL sentences
+'''
+
+from sqlparse.filters import ColumnsSelect, Limit
+from sqlparse.pipeline import Pipeline
+from sqlparse.tokens import Keyword, Whitespace
+
+
+def getlimit(stream):
+ """Function that return the LIMIT of a input SQL """
+ pipe = Pipeline()
+
+ pipe.append(Limit())
+
+ result = pipe(stream)
+ try:
+ return int(result)
+ except ValueError:
+ return result
+
+
+def getcolumns(stream):
+ """Function that return the colums of a SELECT query"""
+ pipe = Pipeline()
+
+ pipe.append(ColumnsSelect())
+
+ return pipe(stream)
+
+
+class IsType():
+ """Functor that return is the statement is of a specific type"""
+ def __init__(self, type):
+ self.type = type
+
+ def __call__(self, stream):
+ for token_type, value in stream:
+ if token_type not in Whitespace:
+ return token_type in Keyword and value == self.type
diff --git a/sqlparse/sql.py b/sqlparse/sql.py
index 05e078d..d18a0a7 100644
--- a/sqlparse/sql.py
+++ b/sqlparse/sql.py
@@ -181,6 +181,13 @@ class TokenList(Token):
else:
yield token
+# def __iter__(self):
+# return self
+#
+# def next(self):
+# for token in self.tokens:
+# yield token
+
def is_group(self):
return True
@@ -255,7 +262,7 @@ class TokenList(Token):
def token_matching(self, idx, funcs):
for token in self.tokens[idx:]:
- for i, func in enumerate(funcs):
+ for func in funcs:
if func(token):
return token
diff --git a/sqlparse/utils.py b/sqlparse/utils.py
index fd6651a..6321353 100644
--- a/sqlparse/utils.py
+++ b/sqlparse/utils.py
@@ -16,10 +16,12 @@ def memoize_generator(func):
cache = {}
def wrapped_func(*args, **kwargs):
- params = (args, kwargs)
+# params = (args, kwargs)
+ params = (args, tuple(sorted(kwargs.items())))
# Look if cached
try:
+ print params
cached = cache[params]
# Not cached, exec and store it