Commit | Line | Data |
16dc9970 |
1 | package SQL::Translator::Parser::MySQL; |
2 | |
3 | #----------------------------------------------------- |
4 | # $Id: MySQL.pm,v 1.1.1.1 2002-03-01 02:26:25 kycl4rk Exp $ |
5 | # |
6 | # File : SQL::Translator::Parser::MySQL |
7 | # Programmer : Ken Y. Clark, kclark@logsoft.com |
8 | # Created : 2002/02/27 |
9 | # Purpose : parser for MySQL |
10 | #----------------------------------------------------- |
11 | |
12 | use strict; |
13 | use vars qw( $VERSION ); |
14 | $VERSION = (qw$Revision: 1.1.1.1 $)[-1]; |
15 | |
16 | use SQL::Translator::Parser; |
17 | use base qw[ SQL::Translator::Parser ]; |
18 | |
19 | sub grammar { |
20 | q{ |
21 | { our ( %tables ) } |
22 | |
23 | file : statement(s) { \%tables } |
24 | |
25 | statement : comment |
26 | | create |
27 | | <error> |
28 | |
29 | create : create_table table_name '(' line(s /,/) ')' table_type(?) ';' |
30 | { |
31 | my $i = 0; |
32 | for my $line ( @{ $item[4] } ) { |
33 | if ( $line->{'type'} eq 'field' ) { |
34 | my $field_name = $line->{'name'}; |
35 | $tables{ $item{'table_name'} } |
36 | {'fields'}{$field_name} = |
37 | { %$line, order => $i }; |
38 | $i++; |
39 | |
40 | if ( $line->{'is_primary_key'} ) { |
41 | push |
42 | @{ $tables{ $item{'table_name'} }{'indeces'} }, |
43 | { |
44 | type => 'primary_key', |
45 | fields => [ $field_name ], |
46 | }; |
47 | } |
48 | } |
49 | else { |
50 | push @{ $tables{ $item{'table_name'} }{'indeces'} }, |
51 | $line; |
52 | } |
53 | $tables{ $item{'table_name'} }{'type'} = |
54 | $item{'table_type'}[0]; |
55 | } |
56 | } |
57 | | <error> |
58 | |
59 | line : index |
60 | | field |
61 | | <error> |
62 | |
63 | comment : /^\s*#.*\n/ |
64 | |
65 | blank : /\s*/ |
66 | |
67 | field : field_name data_type not_null(?) default_val(?) auto_inc(?) primary_key(?) |
68 | { |
69 | my $null = defined $item{'not_null'}[0] |
70 | ? $item{'not_null'}[0] : 1 ; |
71 | $return = { |
72 | type => 'field', |
73 | name => $item{'field_name'}, |
74 | data_type => $item{'data_type'}{'type'}, |
75 | size => $item{'data_type'}{'size'}, |
76 | null => $null, |
77 | default => $item{'default_val'}[0], |
78 | is_auto_inc => $item{'auto_inc'}[0], |
79 | is_primary_key => $item{'primary_key'}[0], |
80 | } |
81 | } |
82 | | <error> |
83 | |
84 | index : primary_key_index |
85 | | unique_index |
86 | | normal_index |
87 | |
88 | table_name : WORD |
89 | |
90 | field_name : WORD |
91 | |
92 | index_name : WORD |
93 | |
94 | data_type : WORD field_size(?) |
95 | { |
96 | $return = { |
97 | type => $item[1], |
98 | size => $item[2][0] |
99 | } |
100 | } |
101 | |
102 | field_type : WORD |
103 | |
104 | field_size : '(' num_range ')' { $item{'num_range'} } |
105 | |
106 | num_range : DIGITS ',' DIGITS |
107 | { $return = $item[1].','.$item[3] } |
108 | | DIGITS |
109 | { $return = $item[1] } |
110 | |
111 | |
112 | create_table : /create/i /table/i |
113 | |
114 | not_null : /not/i /null/i { $return = 0 } |
115 | |
116 | default_val : /default/i /(?:')?[\w\d.-]*(?:')?/ { $item[2]=~s/'//g; $return=$item[2] } |
117 | |
118 | auto_inc : /auto_increment/i { 1 } |
119 | |
120 | primary_key : /primary/i /key/i { 1 } |
121 | |
122 | primary_key_index : primary_key index_name(?) '(' field_name(s /,/) ')' |
123 | { |
124 | $return = { |
125 | name => $item{'index_name'}[0], |
126 | type => 'primary_key', |
127 | fields => $item[4], |
128 | } |
129 | } |
130 | |
131 | normal_index : key index_name(?) '(' field_name(s /,/) ')' |
132 | { |
133 | $return = { |
134 | name => $item{'index_name'}[0], |
135 | type => 'normal', |
136 | fields => $item[4], |
137 | } |
138 | } |
139 | |
140 | unique_index : /unique/i key index_name(?) '(' field_name(s /,/) ')' |
141 | { |
142 | $return = { |
143 | name => $item{'index_name'}[0], |
144 | type => 'unique', |
145 | fields => $item[5], |
146 | } |
147 | } |
148 | |
149 | key : /key/i |
150 | | /index/i |
151 | |
152 | table_type : /TYPE=/i /\w+/ { $item[2] } |
153 | |
154 | WORD : /\w+/ |
155 | |
156 | DIGITS : /\d+/ |
157 | |
158 | COMMA : ',' |
159 | |
160 | }; |
161 | } |
162 | |
163 | 1; |
164 | |
165 | #----------------------------------------------------- |
166 | # Where man is not nature is barren. |
167 | # William Blake |
168 | #----------------------------------------------------- |
169 | |
170 | =head1 NAME |
171 | |
172 | SQL::Translator::Parser::MySQL - parser for MySQL |
173 | |
174 | =head1 SYNOPSIS |
175 | |
176 | use SQL::Translator::Parser::MySQL; |
177 | |
178 | =head1 DESCRIPTION |
179 | |
180 | Blah blah blah. |
181 | |
182 | =head1 AUTHOR |
183 | |
184 | Ken Y. Clark, kclark@logsoft.com |
185 | |
186 | =head1 SEE ALSO |
187 | |
188 | perl(1). |
189 | |
190 | =cut |