blib/lib/SQL/Translator/Parser/DBI/SQLServer.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 188 CC 23 SCAR 60.8
CC
23
Cov
0%
parse
552.0
BEGIN
2.0
BEGIN
2.0
CRAP 438.2
1 package SQL::Translator::Parser::DBI::SQLServer;
2  
3 =head1 NAME
4  
5 SQL::Translator::Parser::DBI::SQLServer - parser for SQL Server through DBD::ODBC
6  
7 =head1 SYNOPSIS
8  
9 See SQL::Translator::Parser::DBI.
10  
11 =head1 DESCRIPTION
12  
13 Uses DBI Catalog Methods.
14  
15 =cut
16  
17 use strict;
18 use warnings;
19 use DBI;
20 use SQL::Translator::Schema;
21 use Data::Dumper;
22  
23 our ($DEBUG, @EXPORT_OK);
24 our $VERSION = '1.66';
25 $DEBUG = 0 unless defined $DEBUG;
26  
27 no strict 'refs';
28  
29 sub parse {
30 my ($tr, $dbh) = @_;
31  
32 if ($dbh->{FetchHashKeyName} ne 'NAME_uc') {
33 warn "setting dbh attribute {FetchHashKeyName} to NAME_uc";
34 $dbh->{FetchHashKeyName} = 'NAME_uc';
35 }
36  
37 if ($dbh->{ChopBlanks} != 1) {
38 warn "setting dbh attribute {ChopBlanks} to 1";
39 $dbh->{ChopBlanks} = 1;
40 }
41  
42 my $schema = $tr->schema;
43  
44 my ($sth, @tables, $columns);
45 my $stuff;
46  
47 ### Columns
48  
49 # it is much quicker to slurp back everything all at once rather
50 # than make repeated calls
51  
52 $sth = $dbh->column_info(undef, undef, undef, undef);
53  
54 foreach my $c (@{ $sth->fetchall_arrayref({}) }) {
55 $columns->{ $c->{TABLE_CAT} }->{ $c->{TABLE_SCHEM} }
56 ->{ $c->{TABLE_NAME} }->{columns}->{ $c->{COLUMN_NAME} } = $c;
57 }
58  
59 ### Tables and views
60  
61 # Get a list of the tables and views.
62 $sth = $dbh->table_info();
63 @tables = @{ $sth->fetchall_arrayref({}) };
64  
65 my $h = $dbh->selectall_arrayref(
66 q{
67 SELECT o.name, colid,c.text
68 FROM syscomments c
69 JOIN sysobjects o
70 ON c.id = o.id
71 WHERE o.type ='V'
72 ORDER BY o.name,
73 c.colid
74 }
75 );
76  
77 # View text
78 # I had always thought there was something 'hard' about
79 # reconstructing text from syscomments ..
80 # this seems to work fine and is certainly not complicated!
81  
82 foreach (@{$h}) {
83 $stuff->{view}->{ $_->[0] }->{text} .= $_->[2];
84 }
85  
86 #### objects with indexes.
87 map { $stuff->{indexes}->{ $_->[0] }++ if defined; } @{
88 $dbh->selectall_arrayref(
89 "SELECT DISTINCT object_name(id)
90 FROM sysindexes
91 WHERE indid > 0 and indid < 255 and
92 name not like '_WA_Sys%'"
93 )
94 };
95  
96 ## slurp objects
97 map { $stuff->{ $_->[1] }->{ $_->[0] } = $_; } @{ $dbh->selectall_arrayref("SELECT name,type, id FROM sysobjects") };
98  
99 ### Procedures
100  
101 # This gets legitimate procedures by used the 'supported' API: sp_stored_procedures
102 map {
103 my $n = $_->{PROCEDURE_NAME};
104 $n =~ s/;\d+$//; # Ignore versions for now
105 $_->{name} = $n;
106 $stuff->{procedures}->{$n} = $_;
107 } values %{ $dbh->selectall_hashref("sp_stored_procedures", 'PROCEDURE_NAME') };
108  
109 # And this blasts in the text of 'legit' stored procedures. Do
110 # this rather than calling sp_helptext in a loop.
111  
112 $h = $dbh->selectall_arrayref(
113 q{
114 SELECT o.name, colid,c.text
115 FROM syscomments c
116 JOIN sysobjects o
117 ON c.id = o.id
118 WHERE o.type in ('P', 'FN', 'TF', 'IF')
119 }
120 );
121  
122 foreach (@{$h}) {
123 $stuff->{procedures}->{ $_->[0] }->{text} .= $_->[2]
124 if (defined($stuff->{procedures}->{ $_->[0] }));
125 }
126  
127 ### Defaults
128 ### Rules
129 ### Bind Defaults
130 ### Bind Rules
131  
132 ### Triggers
133 # Since the 'target' of the trigger is defined in the text, we will
134 # just create them independently for now rather than associating them
135 # with a table.
136  
137 $h = $dbh->selectall_arrayref(
138 q{
139 SELECT o.name, colid,c.text
140 FROM syscomments c
141 JOIN sysobjects o
142 ON c.id = o.id
143 JOIN sysobjects o1
144 ON (o.id = o1.instrig OR o.id = o1.deltrig or o.id = o1.updtrig)
145 WHERE o.type ='TR'
146 ORDER BY o.name,
147 c.colid
148 }
149 );
150 foreach (@{$h}) {
151 $stuff->{triggers}->{ $_->[0] }->{text} .= $_->[2];
152 }
153  
154 ### References
155 ### Keys
156  
157 ### Types
158 # Not sure what to do with these?
159 $stuff->{type_info_all} = $dbh->type_info_all;
160  
161 ### Tables
162 # According to the DBI docs, these can be
163  
164 # "TABLE"
165 # "VIEW"
166 # "SYSTEM TABLE"
167 # "GLOBAL TEMPORARY",
168 # "LOCAL TEMPORARY"
169 # "ALIAS"
170 # "SYNONYM"
171  
172 foreach my $table_info (@tables) {
173 next
174 unless (defined($table_info->{TABLE_TYPE}));
175  
176 if ($table_info->{TABLE_TYPE} eq "TABLE") {
177 my $table = $schema->add_table(
178 name => $table_info->{TABLE_NAME},
179 type => $table_info->{TABLE_TYPE},
180 ) || die $schema->error;
181  
182 # find the associated columns
183  
184 my $cols = $columns->{ $table_info->{TABLE_CAT} }
185 ->{ $table_info->{TABLE_SCHEM} }->{ $table_info->{TABLE_NAME} }
186 ->{columns};
187  
188 foreach my $c (values %{$cols}) {
189 my $is_auto_increment = $c->{TYPE_NAME} =~ s#(\(\))? identity##i;
190 my $f = $table->add_field(
191 name => $c->{COLUMN_NAME},
192 data_type => $c->{TYPE_NAME},
193 order => $c->{ORDINAL_POSITION},
194 size => [ $c->{COLUMN_SIZE}, $c->{DECIMAL_DIGITS} ],
195 ) || die $table->error;
196 $f->is_nullable($c->{NULLABLE} == 1);
197 $f->is_auto_increment($is_auto_increment);
198 if (defined $c->{COLUMN_DEF}) {
199 $c->{COLUMN_DEF} =~ s#\('?(.*?)'?\)#$1#;
200 $f->default_value($c->{COLUMN_DEF});
201 }
202 }
203  
204 # add in primary key
205 my $h = $dbh->selectall_hashref(
206 "sp_pkeys
207 [$table_info->{TABLE_NAME}]", 'COLUMN_NAME'
208 );
209 if (scalar keys %{$h} >= 1) {
210 my @c = map { $_->{COLUMN_NAME} }
211 sort { $a->{KEY_SEQ} <=> $b->{KEY_SEQ} } values %{$h};
212  
213 $table->primary_key(@c)
214 if (scalar @c);
215 }
216  
217 # add in foreign keys
218 $h = $dbh->selectall_hashref(
219 "sp_fkeys NULL,
220 \@fktable_name = '[$table_info->{TABLE_NAME}]'", 'FK_NAME'
221 );
222 foreach my $fk (values %{$h}) {
223 my $constraint = $table->add_constraint(
224 name => $fk->{FK_NAME},
225 fields => [ $fk->{FKCOLUMN_NAME} ],
226 );
227 $constraint->type("FOREIGN_KEY");
228 $constraint->on_delete(
229 $fk->{DELETE_RULE} == 0 ? "CASCADE"
230 : $fk->{DELETE_RULE} == 1 ? "NO ACTION"
231 : "SET_NULL"
232 );
233 $constraint->on_update(
234 $fk->{UPDATE_RULE} == 0 ? "CASCADE"
235 : $fk->{UPDATE_RULE} == 1 ? "NO ACTION"
236 : "SET_NULL"
237 );
238 $constraint->reference_table($fk->{PKTABLE_NAME});
239 }
240  
241 # add in any indexes ... how do we tell if the index has
242 # already been created as part of a primary key or other
243 # constraint?
244  
245 if (defined($stuff->{indexes}->{ $table_info->{TABLE_NAME} })) {
246 my $h = $dbh->selectall_hashref(
247 "sp_helpindex
248 [$table_info->{TABLE_NAME}]", 'INDEX_NAME'
249 );
250 foreach (values %{$h}) {
251 my $fields = $_->{'INDEX_KEYS'};
252 $fields =~ s/\s*//g;
253 my $i = $table->add_index(
254 name => $_->{INDEX_NAME},
255 fields => $fields,
256 );
257 if ($_->{'INDEX_DESCRIPTION'} =~ /unique/i) {
258 $i->type('unique');
259  
260 # we could make this a primary key if there
261 # isn't already one defined and if there
262 # aren't any nullable columns in thisindex.
263  
264 if (!defined($table->primary_key())) {
265 $table->primary_key($fields)
266 unless grep { $table->get_field($_)->is_nullable() }
267 split(/,\s*/, $fields);
268 }
269 }
270 }
271 }
272 } elsif ($table_info->{TABLE_TYPE} eq 'VIEW') {
273 next
274 if $table_info->{TABLE_NAME} eq 'sysconstraints'
275 || $table_info->{TABLE_NAME} eq 'syssegments';
276 next if !$stuff->{view}->{ $table_info->{TABLE_NAME} }->{text};
277 my $view = $schema->add_view(name => $table_info->{TABLE_NAME},);
278  
279 my $cols = $columns->{ $table_info->{TABLE_CAT} }
280 ->{ $table_info->{TABLE_SCHEM} }->{ $table_info->{TABLE_NAME} }
281 ->{columns};
282  
283 $view->fields(
284 map { $_->{COLUMN_NAME} }
285 sort { $a->{ORDINAL_POSITION} <=> $b->{ORDINAL_POSITION} } values %{$cols}
286 );
287  
288 $view->sql($stuff->{view}->{ $table_info->{TABLE_NAME} }->{text})
289 if (defined($stuff->{view}->{ $table_info->{TABLE_NAME} }->{text}));
290 }
291 }
292  
293 foreach my $p (values %{ $stuff->{procedures} }) {
294 next if !$p->{text};
295 my $proc = $schema->add_procedure(
296 name => $p->{name},
297 owner => $p->{PROCEDURE_OWNER},
298 comments => $p->{REMARKS},
299 sql => $p->{text},
300 );
301  
302 }
303  
304 ### Permissions
305 ### Groups
306 ### Users
307 ### Aliases
308 ### Logins
309 return 1;
310 }
311  
312 1;
313  
314 =pod
315  
316 =head1 AUTHOR
317  
318 Chris Hilton E<lt>chris@dctank.comE<gt> - Bulk of code from
319 DBI-Sybase parser, I just tweaked it for SQLServer. Thanks.
320  
321 =head1 SEE ALSO
322  
323 DBI, DBD::ODBC, SQL::Translator::Schema.
324  
325 =cut