diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2016-05-17 21:56:43 +0000 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2016-05-17 21:56:43 +0000 |
commit | f3bfed03b7b4f375310b795725ff7fecdbfb0c69 (patch) | |
tree | 11830a4b8060758111f88f6dc770828555019b34 /src/examples/simpleSQL.py | |
parent | 9a21c3719c424361ea770f0d03bb3ef17165710d (diff) | |
download | pyparsing-git-f3bfed03b7b4f375310b795725ff7fecdbfb0c69.tar.gz |
Update pyparsing code practices
Diffstat (limited to 'src/examples/simpleSQL.py')
-rw-r--r-- | src/examples/simpleSQL.py | 114 |
1 files changed, 22 insertions, 92 deletions
diff --git a/src/examples/simpleSQL.py b/src/examples/simpleSQL.py index e19d5c2..66dc18c 100644 --- a/src/examples/simpleSQL.py +++ b/src/examples/simpleSQL.py @@ -3,30 +3,17 @@ # simple demo of using the parsing library to do simple-minded SQL parsing
# could be extended to include where clauses etc.
#
-# Copyright (c) 2003, Paul McGuire
+# Copyright (c) 2003,2016, Paul McGuire
#
from pyparsing import Literal, CaselessLiteral, Word, delimitedList, Optional, \
Combine, Group, alphas, nums, alphanums, ParseException, Forward, oneOf, quotedString, \
ZeroOrMore, restOfLine, Keyword, upcaseTokens
-def test( str ):
- print(str + " ->")
- try:
- tokens = simpleSQL.parseString( str )
- print("tokens = ", tokens)
- print("tokens.columns =", tokens.columns)
- print("tokens.tables =", tokens.tables)
- print("tokens.where =", tokens.where)
- except ParseException as err:
- print(" "*err.loc + "^")
- print(err)
- print('')
-
-
# define SQL tokens
selectStmt = Forward()
-selectToken = Keyword("select", caseless=True)
-fromToken = Keyword("from", caseless=True)
+SELECT = Keyword("select", caseless=True)
+FROM = Keyword("from", caseless=True)
+WHERE = Keyword("where", caseless=True)
ident = Word( alphas, alphanums + "_$" ).setName("identifier")
columnName = ( delimitedList( ident, ".", combine=True ) ).addParseAction(upcaseTokens)
@@ -58,11 +45,9 @@ whereCondition = Group( whereExpression << whereCondition + ZeroOrMore( ( and_ | or_ ) + whereExpression )
# define the grammar
-selectStmt << ( selectToken +
- ( '*' | columnNameList ).setResultsName( "columns" ) +
- fromToken +
- tableNameList.setResultsName( "tables" ) +
- Optional( Group( CaselessLiteral("where") + whereExpression ), "" ).setResultsName("where") )
+selectStmt <<= (SELECT + ('*' | columnNameList)("columns") +
+ FROM + tableNameList( "tables" ) +
+ Optional(Group(WHERE + whereExpression), "")("where"))
simpleSQL = selectStmt
@@ -70,73 +55,18 @@ simpleSQL = selectStmt oracleSqlComment = "--" + restOfLine
simpleSQL.ignore( oracleSqlComment )
-
-test( "SELECT * from XYZZY, ABC" )
-test( "select * from SYS.XYZZY" )
-test( "Select A from Sys.dual" )
-test( "Select A,B,C from Sys.dual" )
-test( "Select A, B, C from Sys.dual" )
-test( "Select A, B, C from Sys.dual, Table2 " )
-test( "Xelect A, B, C from Sys.dual" )
-test( "Select A, B, C frox Sys.dual" )
-test( "Select" )
-test( "Select &&& frox Sys.dual" )
-test( "Select A from Sys.dual where a in ('RED','GREEN','BLUE')" )
-test( "Select A from Sys.dual where a in ('RED','GREEN','BLUE') and b in (10,20,30)" )
-test( "Select A,b from table1,table2 where table1.id eq table2.id -- test out comparison operators" )
-
-"""
-Test output:
->pythonw -u simpleSQL.py
-SELECT * from XYZZY, ABC ->
-tokens = ['select', '*', 'from', ['XYZZY', 'ABC']]
-tokens.columns = *
-tokens.tables = ['XYZZY', 'ABC']
-
-select * from SYS.XYZZY ->
-tokens = ['select', '*', 'from', ['SYS.XYZZY']]
-tokens.columns = *
-tokens.tables = ['SYS.XYZZY']
-
-Select A from Sys.dual ->
-tokens = ['select', ['A'], 'from', ['SYS.DUAL']]
-tokens.columns = ['A']
-tokens.tables = ['SYS.DUAL']
-
-Select A,B,C from Sys.dual ->
-tokens = ['select', ['A', 'B', 'C'], 'from', ['SYS.DUAL']]
-tokens.columns = ['A', 'B', 'C']
-tokens.tables = ['SYS.DUAL']
-
-Select A, B, C from Sys.dual ->
-tokens = ['select', ['A', 'B', 'C'], 'from', ['SYS.DUAL']]
-tokens.columns = ['A', 'B', 'C']
-tokens.tables = ['SYS.DUAL']
-
-Select A, B, C from Sys.dual, Table2 ->
-tokens = ['select', ['A', 'B', 'C'], 'from', ['SYS.DUAL', 'TABLE2']]
-tokens.columns = ['A', 'B', 'C']
-tokens.tables = ['SYS.DUAL', 'TABLE2']
-
-Xelect A, B, C from Sys.dual ->
-^
-Expected 'select'
-Expected 'select' (0), (1,1)
-
-Select A, B, C frox Sys.dual ->
- ^
-Expected 'from'
-Expected 'from' (15), (1,16)
-
-Select ->
- ^
-Expected '*'
-Expected '*' (6), (1,7)
-
-Select &&& frox Sys.dual ->
- ^
-Expected '*'
-Expected '*' (7), (1,8)
-
->Exit code: 0
-"""
\ No newline at end of file +if __name__ == "__main__":
+ simpleSQL.runTests("""\
+ SELECT * from XYZZY, ABC
+ select * from SYS.XYZZY
+ Select A from Sys.dual
+ Select A,B,C from Sys.dual
+ Select A, B, C from Sys.dual
+ Select A, B, C from Sys.dual, Table2
+ Xelect A, B, C from Sys.dual
+ Select A, B, C frox Sys.dual
+ Select
+ Select &&& frox Sys.dual
+ Select A from Sys.dual where a in ('RED','GREEN','BLUE')
+ Select A from Sys.dual where a in ('RED','GREEN','BLUE') and b in (10,20,30)
+ Select A,b from table1,table2 where table1.id eq table2.id -- test out comparison operators""")
|