From 73465819c9d5105f57a9cf5579c5dc78964b37e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Thu, 17 May 2012 23:11:05 +0200 Subject: Added functions from AntiORM --- sqlparse/functions.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 sqlparse/functions.py (limited to 'sqlparse/functions.py') diff --git a/sqlparse/functions.py b/sqlparse/functions.py new file mode 100644 index 0000000..693fe6a --- /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 in Whitespace: continue + return token_type in Keyword and value == self.type -- cgit v1.2.1 From 4a41241e8fc084d25cde943263ea507509f9ead4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Thu, 17 May 2012 23:18:05 +0200 Subject: Added tests for functions --- sqlparse/functions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sqlparse/functions.py') diff --git a/sqlparse/functions.py b/sqlparse/functions.py index 693fe6a..25dc005 100644 --- a/sqlparse/functions.py +++ b/sqlparse/functions.py @@ -40,5 +40,5 @@ class IsType(): def __call__(self, stream): for token_type, value in stream: - if token_type in Whitespace: continue - return token_type in Keyword and value == self.type + if token_type not in Whitespace: + return token_type in Keyword and value == self.type -- cgit v1.2.1 From 8bd03f158343bd2b83802c1059e15953c72f9f36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22Piranna=22?= Date: Sat, 19 May 2012 19:28:18 +0200 Subject: Fixed comments by Andi --- sqlparse/functions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sqlparse/functions.py') diff --git a/sqlparse/functions.py b/sqlparse/functions.py index 25dc005..aaf6fb8 100644 --- a/sqlparse/functions.py +++ b/sqlparse/functions.py @@ -11,7 +11,7 @@ from sqlparse.pipeline import Pipeline from sqlparse.tokens import Keyword, Whitespace -def GetLimit(stream): +def getlimit(stream): """Function that return the LIMIT of a input SQL """ pipe = Pipeline() @@ -24,7 +24,7 @@ def GetLimit(stream): return result -def GetColumns(stream): +def getcolumns(stream): """Function that return the colums of a SELECT query""" pipe = Pipeline() -- cgit v1.2.1