File Coverage

lib/SQL/Admin/Driver/DB2/Producer.pm
Criterion Covered Total %
statement 68 80 85.0
branch 6 10 60.0
condition 7 15 46.6
subroutine 32 38 84.2
pod 0 33 0.0
total 113 176 64.2


line stmt bran cond sub pod time code
1              
2             package SQL::Admin::Driver::DB2::Producer;
3 2     2   2522 use base qw( SQL::Admin::Driver::Base::Producer );
  2         4  
  2         866  
4              
5 2     2   18 use strict;
  2         3  
  2         63  
6 2     2   10 use warnings;
  2         3  
  2         95  
7              
8             our $VERSION = v0.5.0;
9              
10             ######################################################################
11              
12 2     2   820 use SQL::Admin::Driver::DB2::Keywords qw( :all );
  2         6  
  2         4962  
13              
14             our %DATA_TYPE_MAP = (
15             'int2' => 'smallint',
16             'int4' => 'integer',
17             'int8' => 'bigint',
18              
19             'text' => 'varchar',
20             );
21              
22             ######################################################################
23              
24             our $ESCAPE_ALL_IDENTIFIERS = 0;
25             our $ESCAPE_NONRESERVED_KEYWORDS = 0;
26             our $ESCAPE_SQL_KEYWORDS = 0;
27              
28             ######################################################################
29              
30             our %REFERENTIAL_ACTION = (
31             cascade => [ 'CASCADE' ],
32             no_action => [ 'NO', 'ACTION' ],
33             restrict => [ 'RESTRICT' ],
34             set_null => [ 'SET', 'NULL' ],
35             );
36              
37             ######################################################################
38             ######################################################################
39              
40             sub _escape_identifier { # ;
41 124     124   173 my ($self, $identifier) = @_;
42 124         167 my $lc = lc $identifier;
43              
44 124 50 66     947 $identifier = '"' . $identifier . '"'
      33        
      66        
      33        
      33        
45             if $ESCAPE_ALL_IDENTIFIERS
46             || $RESERVED_KEYWORDS{ $lc }
47             || ($ESCAPE_NONRESERVED_KEYWORDS && $NONRESERVED_KEYWORDS{ $lc })
48             || ($ESCAPE_SQL_KEYWORDS && $SQL_KEYWORDS{ $lc })
49             ;
50              
51             ##################################################################
52              
53 124         579 $identifier;
54             }
55              
56              
57             ######################################################################
58             ######################################################################
59             sub data_type { # ;
60 26     26 0 37 my ($self, $data, $parent) = @_;
61              
62 26 100       80 $data = $DATA_TYPE_MAP{lc $data}
63             if exists $DATA_TYPE_MAP{lc $data};
64              
65 26         88 $self->SUPER::data_type ($data, $parent);
66             }
67              
68              
69             ######################################################################
70             ######################################################################
71             sub current_timestamp { # ;
72 4     4 0 41 'CURRENT', 'TIMESTAMP';
73             }
74              
75              
76             ######################################################################
77             ######################################################################
78             sub current_time { # ;
79 1     1 0 5 'CURRENT', 'TIME';
80             }
81              
82              
83             ######################################################################
84             ######################################################################
85             sub current_date { # ;
86 1     1 0 6 'CURRENT', 'DATE';
87             }
88              
89              
90             ######################################################################
91             ######################################################################
92             sub default_clause { # ;
93 9     9 0 16 my ($self, $def) = @_;
94              
95 9         52 'WITH', 'DEFAULT', $self->__apply (%$def);
96             }
97              
98              
99             ######################################################################
100             ######################################################################
101              
102             sub db2_in_tablespace { # ;
103 0     0 0 0 'IN', '"' . $_[1] . '"';
104             }
105              
106              
107             ######################################################################
108             ######################################################################
109             sub db2_data_capture { # ;
110 0     0 0 0 'DATA', 'CAPTURE', $_[1];
111             }
112              
113              
114             ######################################################################
115             ######################################################################
116             sub db2_pctfree { # ;
117 0     0 0 0 'PCTFREE', $_[1];
118             }
119              
120              
121             ######################################################################
122             ######################################################################
123 2     2 0 7 sub db2_locksize { # ;
124             }
125              
126              
127             ######################################################################
128             ######################################################################
129 2     2 0 6 sub db2_append { # ;
130             }
131              
132              
133             ######################################################################
134             ######################################################################
135 2     2 0 8 sub db2_volatile { # ;
136             }
137              
138              
139             ######################################################################
140             ######################################################################
141 2     2 0 6 sub db2_log_index { # ;
142             }
143              
144              
145             ######################################################################
146             ######################################################################
147 1     1 0 4 sub db2_enforced { # ;
148             # $_ ? () : ('NOT'), 'ENFORCED';
149             }
150              
151              
152             ######################################################################
153             ######################################################################
154 1     1 0 4 sub db2_optimize { # ;
155             # $_ ? 'ENABLED' : 'DISABLED', 'QUERY', 'OPTIMIZATION';
156             }
157              
158              
159             ######################################################################
160             ######################################################################
161             sub autoincrement { # ;
162 8     8 0 21 my ($self, $def) = @_;
163 8         10 my $options = do { local $, = ', '; $self->__apply (sequence_options => $def) };
  8         11  
  8         24  
164              
165 8 100       25 $options = '(' . $options . ')'
166             if $options;
167              
168 8         36 'GENERATED', 'AS', 'IDENTITY', $options;
169             }
170              
171              
172             ######################################################################
173             ######################################################################
174              
175             sub sequence_type { # ;
176 6     6 0 11 my ($self, $def) = @_;
177              
178 6         16 'AS', $self->data_type ($def);
179             }
180              
181              
182             ######################################################################
183             ######################################################################
184             sub sequence_options { # ;
185 13     13 0 17 my ($self, $def) = @_;
186              
187 13         49 map $self->__call ($_, $def), (
188             'sequence_start_with',
189             'sequence_increment_by',
190             'sequence_minvalue',
191             'sequence_maxvalue',
192             'sequence_cache',
193             );
194             }
195              
196              
197             ######################################################################
198             ######################################################################
199             sub sequence_start_with { # ;
200 10     10 0 64 map +('START', 'WITH', $_), grep $_ != 1, $_[1];
201             }
202              
203              
204             ######################################################################
205             ######################################################################
206             sub sequence_increment_by { # ;
207 5     5 0 27 map +('INCREMENT', 'BY', $_), grep $_ != 1, $_[1];
208             }
209              
210              
211             ######################################################################
212             ######################################################################
213             sub sequence_minvalue { # ;
214 3     3 0 6 my ($self, $def) = @_;
215              
216 3         18 map +('MINVALUE', $_), grep defined, $def;
217             }
218              
219              
220             ######################################################################
221             ######################################################################
222             sub sequence_maxvalue { # ;
223 3     3 0 6 my ($self, $def) = @_;
224              
225 3         16 map +('MAXVALUE', $_), grep defined, $def;
226             }
227              
228              
229             ######################################################################
230             ######################################################################
231             sub sequence_cache { # ;
232 2     2 0 16 my ($self, $def) = @_;
233              
234 2         15 map +('CACHE', $_), grep $_ != 20, grep defined, $def;
235             }
236              
237              
238             ######################################################################
239             ######################################################################
240              
241             sub alter_table_actions { # ;
242 0     0 0 0 my ($self, $def) = @_;
243              
244 0         0 map $self->__apply (alter_table_action => $_), @$def;
245             }
246              
247              
248             ######################################################################
249             ######################################################################
250             sub alter_table_action { # ;
251 8     8 0 11 my ($self, $def) = @_;
252              
253 8         32 $self->__apply (%$def);
254             }
255              
256              
257             ######################################################################
258             ######################################################################
259             sub delete_rule { # ;
260 1     1 0 4 my ($self, $def) = @_;
261              
262 1         3 map +('ON', 'DELETE', @{ $REFERENTIAL_ACTION{$_} }), grep defined, $def;
  1         8  
263             }
264              
265              
266             ######################################################################
267             ######################################################################
268             sub update_rule { # ;
269 1     1 0 3 my ($self, $def) = @_;
270              
271 1         3 map +('ON', 'UPDATE', @{ $REFERENTIAL_ACTION{$_} }), grep defined, $def;
  1         51  
272             }
273              
274              
275             ######################################################################
276             ######################################################################
277             sub foreign_key_constraint { # ;
278 1     1 0 2 my ($self, $def) = @_;
279              
280             (
281 1         11 $self->SUPER::foreign_key_constraint ($def),
282             $self->__call (db2_enforced => $def),
283             $self->__call (db2_optimize => $def),
284             );
285             }
286              
287              
288             ######################################################################
289             ######################################################################
290              
291             sub create_sequence { # ;
292 7     7 0 8 my ($self, $def) = @_;
293              
294             (
295 7         22 'CREATE',
296             'SEQUENCE',
297             $self->__call (sequence_name => $def),
298             $self->__call (sequence_type => $def),
299             $self->__call (sequence_options => $def),
300             );
301             }
302              
303              
304             ######################################################################
305             ######################################################################
306             sub create_index { # ;
307 3     3 0 6 my ($self, $def) = @_;
308 3         9 join ' ', (
309             'CREATE',
310             $self->__call (index_unique => $def),
311             'INDEX',
312             $self->__call (index_name => $def),
313             'ON',
314             $self->__call (table_name => $def),
315             $self->__call (index_column_list => $def),
316             $self->__call (index_options => $def),
317             );
318             }
319              
320              
321             ######################################################################
322             ######################################################################
323             sub create_table { # ;
324 1     1 0 2 my ($self, $def) = @_;
325              
326 1         4 join ' ', (
327             'CREATE',
328             'TABLE',
329             $self->__call (table_name => $def),
330             $self->__call (table_content => $def),
331             $self->__call (table_options => $def),
332             );
333             }
334              
335              
336             ######################################################################
337             ######################################################################
338             sub alter_table { # ;
339 1     1 0 3 my ($self, $def) = @_;
340              
341 1         2 my @list = map $self->__apply (%$_), @{ $def->{alter_table_actions} };
  1         8  
342              
343 1 50       11 return unless @list;
344              
345             (
346 0           'ALTER',
347             'TABLE',
348             $self->__call (table_name => $def),
349             @list
350             );
351             }
352              
353              
354             ######################################################################
355             ######################################################################
356              
357             sub create_schema { # ;
358 0     0 0   my ($self, $def) = @_;
359              
360             (
361 0           'CREATE',
362             'SCHEMA',
363             $self->_escape_identifier ($def->{schema_identifier}),
364             );
365             }
366              
367              
368             ######################################################################
369             ######################################################################
370              
371             sub select_what { # ;
372 0     0 0   my ($self, $def, $parent) = @_;
373 0           my ($key, @res) = $self->SUPER::select_what ($def, $parent);
374              
375 0 0         ($key, @res) = ('VALUES', '(', @res, ')')
376             unless exists $parent->{select_from};
377              
378 0           ($key, @res);
379             }
380              
381              
382             ######################################################################
383             ######################################################################
384              
385             package SQL::Admin::Driver::DB2::Producer;
386              
387             1;