summaryrefslogtreecommitdiff
path: root/examples/simpleSQL.py
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2019-10-31 21:10:28 -0700
committerPaul McGuire <ptmcg@users.noreply.github.com>2019-10-31 23:10:28 -0500
commit53d1b4a6f48a53c4c4ec4ac7031362b691c0366d (patch)
tree088ad3cf3561b78a00af4fb2fd474f4a2b8ca70c /examples/simpleSQL.py
parent41752aa52cc97c710474bb2972cceab057b52ad4 (diff)
downloadpyparsing-git-53d1b4a6f48a53c4c4ec4ac7031362b691c0366d.tar.gz
Blacken the project (#141)
Diffstat (limited to 'examples/simpleSQL.py')
-rw-r--r--examples/simpleSQL.py77
1 files changed, 48 insertions, 29 deletions
diff --git a/examples/simpleSQL.py b/examples/simpleSQL.py
index 60582cd..a806ad6 100644
--- a/examples/simpleSQL.py
+++ b/examples/simpleSQL.py
@@ -5,57 +5,75 @@
#
# Copyright (c) 2003,2016, Paul McGuire
#
-from pyparsing import Word, delimitedList, Optional, \
- Group, alphas, alphanums, Forward, oneOf, quotedString, \
- infixNotation, opAssoc, \
- restOfLine, CaselessKeyword, pyparsing_common as ppc
+from pyparsing import (
+ Word,
+ delimitedList,
+ Optional,
+ Group,
+ alphas,
+ alphanums,
+ Forward,
+ oneOf,
+ quotedString,
+ infixNotation,
+ opAssoc,
+ restOfLine,
+ CaselessKeyword,
+ pyparsing_common as ppc,
+)
# define SQL tokens
selectStmt = Forward()
-SELECT, FROM, WHERE, AND, OR, IN, IS, NOT, NULL = map(CaselessKeyword,
- "select from where and or in is not null".split())
+SELECT, FROM, WHERE, AND, OR, IN, IS, NOT, NULL = map(
+ CaselessKeyword, "select from where and or in is not null".split()
+)
NOT_NULL = NOT + NULL
-ident = Word( alphas, alphanums + "_$" ).setName("identifier")
-columnName = delimitedList(ident, ".", combine=True).setName("column name")
+ident = Word(alphas, alphanums + "_$").setName("identifier")
+columnName = delimitedList(ident, ".", combine=True).setName("column name")
columnName.addParseAction(ppc.upcaseTokens)
-columnNameList = Group( delimitedList(columnName))
-tableName = delimitedList(ident, ".", combine=True).setName("table name")
+columnNameList = Group(delimitedList(columnName))
+tableName = delimitedList(ident, ".", combine=True).setName("table name")
tableName.addParseAction(ppc.upcaseTokens)
-tableNameList = Group(delimitedList(tableName))
+tableNameList = Group(delimitedList(tableName))
binop = oneOf("= != < > >= <= eq ne lt le gt ge", caseless=True)
realNum = ppc.real()
intNum = ppc.signed_integer()
-columnRval = realNum | intNum | quotedString | columnName # need to add support for alg expressions
+columnRval = (
+ realNum | intNum | quotedString | columnName
+) # need to add support for alg expressions
whereCondition = Group(
- ( columnName + binop + columnRval ) |
- ( columnName + IN + Group("(" + delimitedList( columnRval ) + ")" )) |
- ( columnName + IN + Group("(" + selectStmt + ")" )) |
- ( columnName + IS + (NULL | NOT_NULL))
- )
+ (columnName + binop + columnRval)
+ | (columnName + IN + Group("(" + delimitedList(columnRval) + ")"))
+ | (columnName + IN + Group("(" + selectStmt + ")"))
+ | (columnName + IS + (NULL | NOT_NULL))
+)
-whereExpression = infixNotation(whereCondition,
- [
- (NOT, 1, opAssoc.RIGHT),
- (AND, 2, opAssoc.LEFT),
- (OR, 2, opAssoc.LEFT),
- ])
+whereExpression = infixNotation(
+ whereCondition,
+ [(NOT, 1, opAssoc.RIGHT), (AND, 2, opAssoc.LEFT), (OR, 2, opAssoc.LEFT),],
+)
# define the grammar
-selectStmt <<= (SELECT + ('*' | columnNameList)("columns") +
- FROM + tableNameList( "tables" ) +
- Optional(Group(WHERE + whereExpression), "")("where"))
+selectStmt <<= (
+ SELECT
+ + ("*" | columnNameList)("columns")
+ + FROM
+ + tableNameList("tables")
+ + Optional(Group(WHERE + whereExpression), "")("where")
+)
simpleSQL = selectStmt
# define Oracle comment format, and ignore them
oracleSqlComment = "--" + restOfLine
-simpleSQL.ignore( oracleSqlComment )
+simpleSQL.ignore(oracleSqlComment)
if __name__ == "__main__":
- simpleSQL.runTests("""\
+ simpleSQL.runTests(
+ """\
# multiple tables
SELECT * from XYZZY, ABC
@@ -92,4 +110,5 @@ if __name__ == "__main__":
# where clause with comparison operator
Select A,b from table1,table2 where table1.id eq table2.id
- """)
+ """
+ )