Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add in a dependancy on pyparsing and add the start of a basic

parser for SQL statements
Remove the slurpy.egg-info directory
  • Loading branch information...
commit d30fbe4f60479728011064ab5a4054b9785bbd73 1 parent 8b06d58
david reid authored
View
25 slurpy.egg-info/PKG-INFO
@@ -1,25 +0,0 @@
-Metadata-Version: 1.0
-Name: slurpy
-Version: 0.0.1
-Summary: A project to backup Lightroom Catalogs to postgresql.
-Home-page: http://www.david-reid.com/projects/slurpy.html
-Author: david reid
-Author-email: zathrasorama@gmail.com
-License: BSD
-Description: Slurpy
- ======
-
- Overview
- --------
-
- Slurpy is a small python app that aims to allow Lightroom users to backup
- and restore their catalogs. While Lightroom is a great application, the
- catalog system imposes restrictions that ideally would not exist.
-
-
-
-Keywords: database lightroom postgresql backup catalog
-Platform: UNKNOWN
-Classifier: Development Status :: 2 - Pre-Alpha
-Classifier: Topic :: Utilities
-Classifier: License :: OSI Approved :: BSD License
View
9 slurpy.egg-info/SOURCES.txt
@@ -1,9 +0,0 @@
-README
-setup.py
-slurpy/__init__.py
-slurpy/translator.py
-slurpy.egg-info/PKG-INFO
-slurpy.egg-info/SOURCES.txt
-slurpy.egg-info/dependency_links.txt
-slurpy.egg-info/entry_points.txt
-slurpy.egg-info/top_level.txt
View
1  slurpy.egg-info/dependency_links.txt
@@ -1 +0,0 @@
-
View
4 slurpy.egg-info/entry_points.txt
@@ -1,4 +0,0 @@
-
- [console_scripts]
- slurpy = slurpy.lrCatalog:main
-
View
1  slurpy.egg-info/top_level.txt
@@ -1 +0,0 @@
-slurpy
View
48 slurpy/parser.py
@@ -0,0 +1,48 @@
+''' Functions to parse SQL statements.
+ Returns a dict with the parsed values.
+ Failure returns an emoty dict.
+'''
+
+from pyparsing import *
+
+column_stmt = Forward()
+test_stmt = Forward()
+
+LPAR, RPAR, COMMA = map(Suppress, "(),")
+(PKEY,NOT,NULL,UNIQUE,DEFAULT, AUTOINCREMENT) = map(CaselessKeyword,
+ "PRIMARY KEY,NOT,NULL,UNIQUE,DEFAULT,AUTOINCREMENT".split(","))
+
+ident = Word(alphas, alphanums + "_$").setResultsName("identifier")
+signed_num = Combine(Optional(oneOf("+-")) + Word(nums + ",."))
+order = oneOf("ASC DESC", caseless = True)
+quoted = QuotedString("'")
+column_name = Upcase(delimitedList(ident, ".", combine=True))
+
+column_type = oneOf("INTEGER TEXT SERIAL UUID VARCHAR", caseless = True). \
+ setResultsName('type')
+column_size = (LPAR + delimitedList(Word(nums)) + RPAR).setResultsName('size')
+column_type_stmt = (column_type + Optional(column_size))
+
+primary_key = (PKEY + Optional(order) + Optional(AUTOINCREMENT)).setResultsName('pkey')
+null = (Optional(NOT) + NULL).setResultsName('null')
+unique = (UNIQUE).setResultsName('unique')
+default = (DEFAULT + Optional(signed_num) + Optional(quoted)).setResultsName('default')
+
+column_constraints = (primary_key | null | unique | default)
+
+column_stmt << (ident + Optional(column_type_stmt) + ZeroOrMore(column_constraints))
+
+def parse_column_statement(colstmt):
+ ''' Parse an SQL statement used to create a column. '''
+ try:
+ r = column_stmt.parseString(colstmt)
+ rv = {}
+ for k in r.keys():
+ if isinstance(r[k], ParseResults):
+ rv[k] = r[k].asList()
+ else:
+ rv[k] = r[k]
+ return rv
+ except ParseException:
+ return {}
+
View
51 tests/parser_test.py
@@ -0,0 +1,51 @@
+import sys
+import unittest
+
+from slurpy.parser import *
+
+# Generic database tests. Tests for individual database classes should go
+# in their own files.
+
+class TestColumns(unittest.TestCase):
+ def test_001(self):
+ tests = [
+ [ 'abc123', { 'identifier': 'abc123' }],
+ [ 'abc123 integer', { 'identifier': 'abc123',
+ 'type': 'INTEGER'}],
+ [ 'abc123 varchar (20)', { 'identifier': 'abc123',
+ 'type': 'VARCHAR',
+ 'size': ['20']}],
+ [ 'abc123 varchar (20, 40)', { 'identifier': 'abc123',
+ 'type': 'VARCHAR',
+ 'size': ['20','40']}],
+ [ 'abc123 integer primary key',
+ { 'identifier': 'abc123',
+ 'type': 'INTEGER',
+ 'pkey': ['PRIMARY KEY']}],
+ [ 'abc123 integer primary key asc autoincrement',
+ { 'identifier': 'abc123',
+ 'type': 'INTEGER',
+ 'pkey': ['PRIMARY KEY','ASC','AUTOINCREMENT']}],
+ [ 'abc123 integer primary key not null',
+ { 'identifier': 'abc123',
+ 'type': 'INTEGER',
+ 'pkey': ['PRIMARY KEY'],
+ 'null': ['NOT', 'NULL']}],
+ ]
+ for t in tests:
+ _ck = parse_column_statement(t[0])
+ self.assertNotEqual(_ck, {})
+ for k,v in t[1].items():
+ self.assertEqual(_ck.has_key(k), True)
+ self.assertEqual(_ck[k], v)
+# 'abc123 varchar (20)',
+# 'abc123 varchar ( 20, 30)',
+ # 'abc123 varchar ( 20, 30) primary key',
+ ## 'abc123 varchar ( 20, 30) not null primary key',
+ # 'abc123 varchar ( 20, 30) primary key unique',
+ # 'abc123 varchar ( 20, 30) primary key unique default 0',
+ # "abc123 varchar ( 20, 30) primary key unique default 'abc'",
+ # 'abc123 varchar ( 20, 30) primary key desc autoincrement',
+ #
+#]
+
Please sign in to comment.
Something went wrong with that request. Please try again.