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
=== modified file 'movetodrizzle/helpers/lexer.py'
--- movetodrizzle/helpers/lexer.py 2010-06-25 17:33:48 +0000
+++ movetodrizzle/helpers/lexer.py 2010-06-28 17:03:27 +0000
@@ -46,7 +46,8 @@
46 'text' : 'TEXT',46 'text' : 'TEXT',
47 'mediumtext' : 'MEDIUMTEXT',47 'mediumtext' : 'MEDIUMTEXT',
48 'longtext' : 'LONGTEXT',48 'longtext' : 'LONGTEXT',
49 'enum' : 'ENUM'49 'enum' : 'ENUM',
50 'null' : 'NULL'
5051
51}52}
5253
5354
=== modified file 'movetodrizzle/helpers/parser.py'
--- movetodrizzle/helpers/parser.py 2010-06-25 17:33:48 +0000
+++ movetodrizzle/helpers/parser.py 2010-06-28 17:03:27 +0000
@@ -21,14 +21,15 @@
21 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)21 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)
2222
23class column():23class column():
24 def __init__(self,name,type):24 def __init__(self,name,type,not_null):
25 self.name=name;25 self.name=name;
26 self.type=type;26 self.type=type;
27 self.not_null=not_null;
27 def __repr__(self):28 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)
2930
30class CreateTableStatement(SqlStatement):31class CreateTableStatement(SqlStatement):
31 def __init__(self, table_name, list_columns,temporary= False, if_not_exists= False):32 def __init__(self, table_name, list_columns,if_not_exists= False,temporary = False):
32 self.table_name= table_name;33 self.table_name= table_name;
33 self.temporary= temporary;34 self.temporary= temporary;
34 self.if_not_exists=if_not_exists;35 self.if_not_exists=if_not_exists;
@@ -36,7 +37,7 @@
36 def add_column(col):37 def add_column(col):
37 self.columns.append(col);38 self.columns.append(col);
38 def __repr__(self):39 def __repr__(self):
39 return "CreateTableStatement(table=%s) (columns=%s)" % (self.table_name,str(self.columns))40 return "CreateTableStatement(table=%s) (if_not_exists=%s) (columns=%s)" % (self.table_name,self.if_not_exists,str(self.columns))
4041
41# Begin grammar rules42# Begin grammar rules
42def p_statement_list(p):43def p_statement_list(p):
@@ -56,8 +57,8 @@
56 p[0]=p[1]57 p[0]=p[1]
5758
58def p_create_table_simple(p):59def p_create_table_simple(p):
59 '''create_table_simple : CREATE TABLE identifier LPAREN col_no_comma col_list RPAREN'''60 '''create_table_simple : CREATE TABLE if_not_exists identifier LPAREN col_no_comma col_list RPAREN'''
60 p[0]= CreateTableStatement(p[3],[p[5]]+p[6]);61 p[0]= CreateTableStatement(p[4],[p[6]]+p[7],p[3]);
6162
62def p_col_list(p):63def p_col_list(p):
63 '''col_list : col col_list'''64 '''col_list : col col_list'''
@@ -68,12 +69,12 @@
68 p[0]= []69 p[0]= []
6970
70def p_col(p):71def p_col(p):
71 '''col : COMMA identifier col_definition'''72 '''col : COMMA identifier col_definition not_null_p'''
72 p[0]= column(p[2],p[3]);73 p[0]= column(p[2],p[3],p[4]);
7374
74def p_col_no_comma(p):75def p_col_no_comma(p):
75 '''col_no_comma : identifier col_definition'''76 '''col_no_comma : identifier col_definition not_null_p'''
76 p[0]= column(p[1],p[2]);77 p[0]= column(p[1],p[2],p[3]);
7778
78def p_col_definition(p):79def p_col_definition(p):
79 '''col_definition : BIT80 '''col_definition : BIT
@@ -110,6 +111,11 @@
110 p[0]=p[1]111 p[0]=p[1]
111112
112113
114def p_not_null_p(p):
115 '''not_null_p : NOT NULL
116 | empty'''
117 p[0]=len(p) > 2
118
113119
114def p_create_schema(p):120def p_create_schema(p):
115 '''statement_type : create_schema_simple121 '''statement_type : create_schema_simple
@@ -172,3 +178,4 @@
172 return parser.parse(subject)178 return parser.parse(subject)
173 except Exception as e:179 except Exception as e:
174 print e180 print e
181#print parse_string("CREATE TABLE IF NOT EXISTS test(field1 INT NOT NULL);");
175182
=== added file 'tests/parser_createtable_test.py'
--- tests/parser_createtable_test.py 1970-01-01 00:00:00 +0000
+++ tests/parser_createtable_test.py 2010-06-28 17:03:27 +0000
@@ -0,0 +1,32 @@
1#!/usr/bin/python
2import sys
3import unittest
4sys.path.append("../movetodrizzle")
5import helpers.parser as parser
6
7class Tester(unittest.TestCase):
8
9 def test_create_schema_1(self):
10 sql = "CREATE TABLE test(field1 INT NOT NULL);"
11 results = parser.parse_string(sql)
12 expected_results = [
13 "CreateTableStatement(table=test) (if_not_exists=False) (columns=[Column(col_name=field1) (col_definition=INT) (not_null=True)])"
14]
15 x = 0
16 for er in expected_results:
17 self.assertEqual(er, str(results[x]))
18 x += 1
19
20 def test_create_schema_2(self):
21 sql = "CREATE TABLE IF NOT EXISTS test(field1 INT NOT NULL);"
22 results = parser.parse_string(sql)
23 expected_results = [
24 "CreateTableStatement(table=test) (if_not_exists=True) (columns=[Column(col_name=field1) (col_definition=INT) (not_null=True)])"
25]
26 x = 0
27 for er in expected_results:
28 self.assertEqual(er, str(results[x]))
29 x += 1
30
31if __name__ == '__main__':
32 unittest.main()

Subscribers

People subscribed via source and target branches

to all changes: