Merge lp:~nahiljain/to-drizzle/parse_create_table into lp:to-drizzle

Proposed by neh
Status: Merged
Merged at revision: 5
Proposed branch: lp:~nahiljain/to-drizzle/parse_create_table
Merge into: lp:to-drizzle
Diff against target: 127 lines (+51/-11)
3 files modified
movetodrizzle/helpers/lexer.py (+2/-1)
movetodrizzle/helpers/parser.py (+17/-10)
tests/parser_createtable_test.py (+32/-0)
To merge this branch: bzr merge lp:~nahiljain/to-drizzle/parse_create_table
Reviewer Review Type Date Requested Status
Jay Pipes Approve
Review via email: mp+28654@code.launchpad.net

Description of the change

1)parser for create table and create schema.
2)test cases for create schema.

To post a comment you must log in.
Revision history for this message
Jay Pipes (jaypipes) wrote :

Looks good so far. I'll merge into trunk.

Remember your code style consistency for next time. :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'movetodrizzle/helpers/lexer.py'
2--- movetodrizzle/helpers/lexer.py 2010-06-25 17:33:48 +0000
3+++ movetodrizzle/helpers/lexer.py 2010-06-28 17:03:27 +0000
4@@ -46,7 +46,8 @@
5 'text' : 'TEXT',
6 'mediumtext' : 'MEDIUMTEXT',
7 'longtext' : 'LONGTEXT',
8- 'enum' : 'ENUM'
9+ 'enum' : 'ENUM',
10+ 'null' : 'NULL'
11
12 }
13
14
15=== modified file 'movetodrizzle/helpers/parser.py'
16--- movetodrizzle/helpers/parser.py 2010-06-25 17:33:48 +0000
17+++ movetodrizzle/helpers/parser.py 2010-06-28 17:03:27 +0000
18@@ -21,14 +21,15 @@
19 return "CreateSchemaStatement(schema=%s) (character_set=%s) (collate=%s) (if_not_exists=%s)" % (self.schema_name, self.character_set,self.collate_name,self.if_not_exists)
20
21 class column():
22- def __init__(self,name,type):
23+ def __init__(self,name,type,not_null):
24 self.name=name;
25 self.type=type;
26+ self.not_null=not_null;
27 def __repr__(self):
28- return "Column(col_name=%s) (col_definition=%s)" % (self.name,self.type)
29+ return "Column(col_name=%s) (col_definition=%s) (not_null=%s)" % (self.name,self.type,self.not_null)
30
31 class CreateTableStatement(SqlStatement):
32- def __init__(self, table_name, list_columns,temporary= False, if_not_exists= False):
33+ def __init__(self, table_name, list_columns,if_not_exists= False,temporary = False):
34 self.table_name= table_name;
35 self.temporary= temporary;
36 self.if_not_exists=if_not_exists;
37@@ -36,7 +37,7 @@
38 def add_column(col):
39 self.columns.append(col);
40 def __repr__(self):
41- return "CreateTableStatement(table=%s) (columns=%s)" % (self.table_name,str(self.columns))
42+ return "CreateTableStatement(table=%s) (if_not_exists=%s) (columns=%s)" % (self.table_name,self.if_not_exists,str(self.columns))
43
44 # Begin grammar rules
45 def p_statement_list(p):
46@@ -56,8 +57,8 @@
47 p[0]=p[1]
48
49 def p_create_table_simple(p):
50- '''create_table_simple : CREATE TABLE identifier LPAREN col_no_comma col_list RPAREN'''
51- p[0]= CreateTableStatement(p[3],[p[5]]+p[6]);
52+ '''create_table_simple : CREATE TABLE if_not_exists identifier LPAREN col_no_comma col_list RPAREN'''
53+ p[0]= CreateTableStatement(p[4],[p[6]]+p[7],p[3]);
54
55 def p_col_list(p):
56 '''col_list : col col_list'''
57@@ -68,12 +69,12 @@
58 p[0]= []
59
60 def p_col(p):
61- '''col : COMMA identifier col_definition'''
62- p[0]= column(p[2],p[3]);
63+ '''col : COMMA identifier col_definition not_null_p'''
64+ p[0]= column(p[2],p[3],p[4]);
65
66 def p_col_no_comma(p):
67- '''col_no_comma : identifier col_definition'''
68- p[0]= column(p[1],p[2]);
69+ '''col_no_comma : identifier col_definition not_null_p'''
70+ p[0]= column(p[1],p[2],p[3]);
71
72 def p_col_definition(p):
73 '''col_definition : BIT
74@@ -110,6 +111,11 @@
75 p[0]=p[1]
76
77
78+def p_not_null_p(p):
79+ '''not_null_p : NOT NULL
80+ | empty'''
81+ p[0]=len(p) > 2
82+
83
84 def p_create_schema(p):
85 '''statement_type : create_schema_simple
86@@ -172,3 +178,4 @@
87 return parser.parse(subject)
88 except Exception as e:
89 print e
90+#print parse_string("CREATE TABLE IF NOT EXISTS test(field1 INT NOT NULL);");
91
92=== added file 'tests/parser_createtable_test.py'
93--- tests/parser_createtable_test.py 1970-01-01 00:00:00 +0000
94+++ tests/parser_createtable_test.py 2010-06-28 17:03:27 +0000
95@@ -0,0 +1,32 @@
96+#!/usr/bin/python
97+import sys
98+import unittest
99+sys.path.append("../movetodrizzle")
100+import helpers.parser as parser
101+
102+class Tester(unittest.TestCase):
103+
104+ def test_create_schema_1(self):
105+ sql = "CREATE TABLE test(field1 INT NOT NULL);"
106+ results = parser.parse_string(sql)
107+ expected_results = [
108+ "CreateTableStatement(table=test) (if_not_exists=False) (columns=[Column(col_name=field1) (col_definition=INT) (not_null=True)])"
109+]
110+ x = 0
111+ for er in expected_results:
112+ self.assertEqual(er, str(results[x]))
113+ x += 1
114+
115+ def test_create_schema_2(self):
116+ sql = "CREATE TABLE IF NOT EXISTS test(field1 INT NOT NULL);"
117+ results = parser.parse_string(sql)
118+ expected_results = [
119+ "CreateTableStatement(table=test) (if_not_exists=True) (columns=[Column(col_name=field1) (col_definition=INT) (not_null=True)])"
120+]
121+ x = 0
122+ for er in expected_results:
123+ self.assertEqual(er, str(results[x]))
124+ x += 1
125+
126+if __name__ == '__main__':
127+ unittest.main()

Subscribers

People subscribed via source and target branches

to all changes: