File Coverage

lib/UR/DataSource/MySQL.pm
Criterion Covered Total %
statement 6 95 6.3
branch 0 36 0.0
condition 0 11 0.0
subroutine 2 21 9.5
pod 0 13 0.0
total 8 176 4.5


line stmt bran cond sub pod time code
1             package UR::DataSource::MySQL;
2 2     2   68 use strict;
  2         3  
  2         47  
3 2     2   7 use warnings;
  2         2  
  2         2079  
4              
5             require UR;
6             our $VERSION = "0.46"; # UR $VERSION;
7              
8             UR::Object::Type->define(
9             class_name => 'UR::DataSource::MySQL',
10             is => ['UR::DataSource::RDBMS'],
11             is_abstract => 1,
12             );
13              
14             # RDBMS API
15              
16 0     0 0   sub driver { "mysql" }
17              
18             #sub server {
19             # my $self = shift->_singleton_object();
20             # $self->_init_database;
21             # return $self->_database_file_path;
22             #}
23              
24 0     0 0   sub owner { shift->_singleton_object->login }
25              
26             #sub login {
27             # undef
28             #}
29             #
30             #sub auth {
31             # undef
32             #}
33              
34             # MySQL can't do an 'escape' clause with the 'like' operator
35 0     0     sub _default_sql_like_escape_string { undef };
36 0     0     sub _format_sql_like_escape_string { return }
37              
38 0     0 0   sub can_savepoint { 1;}
39              
40             sub does_support_limit_offset {
41 0     0 0   my($self, $bx) = @_;
42              
43 0           my $tmpl = $bx->template;
44 0 0 0       if ($tmpl->offset and !defined($tmpl->limit)) {
45 0           return 0; # Can't have offset without limit
46             } else {
47 0           return 1;
48             }
49             }
50              
51             sub resolve_limit_offset_clause {
52 0     0 0   my($self, $query_plan) = @_;
53              
54 0           my $limit = $self->_resolve_limit_value_from_query_plan($query_plan);
55 0           my $offset = $self->_resolve_offset_value_from_query_plan($query_plan);
56              
57 0 0 0       if (defined($limit) and $offset) {
    0          
58 0           return sprintf('limit %d, %d', $offset, $limit);
59             } elsif (defined $limit) {
60 0           return sprintf('limit %d', $limit);
61             } else {
62 0           return '';
63             }
64             }
65              
66              
67             *_init_created_dbh = \&init_created_handle;
68             sub init_created_handle
69             {
70 0     0 0   my ($self, $dbh) = @_;
71 0 0         return unless defined $dbh;
72              
73 0           $dbh->{LongTruncOk} = 0;
74 0           return $dbh;
75             }
76              
77             sub _ignore_table {
78 0     0     my $self = shift;
79 0           my $table_name = shift;
80 0 0         return 1 if $table_name =~ /^(pg_|sql_|URMETA)/;
81             }
82              
83             #
84             # for concurrency's sake we need to use dummy tables in place of sequence generators here, too
85             #
86              
87              
88             sub _get_sequence_name_for_table_and_column {
89 0     0     my $self = shift->_singleton_object;
90 0           my ($table_name,$column_name) = @_;
91            
92 0           my $dbh = $self->get_default_handle();
93            
94             # See if the sequence generator "table" is already there
95 0           my $seq_table = sprintf('URMETA_%s_%s_SEQ', $table_name, $column_name);
96             #$DB::single = 1;
97 0 0 0       unless ($self->{'_has_sequence_generator'}->{$seq_table} or
98 0           grep {$_ eq $seq_table} $self->get_table_names() ) {
99 0 0         unless ($dbh->do("CREATE TABLE IF NOT EXISTS $seq_table (next_value integer PRIMARY KEY AUTO_INCREMENT)")) {
100 0           die "Failed to create sequence generator $seq_table: ".$dbh->errstr();
101             }
102             }
103 0           $self->{'_has_sequence_generator'}->{$seq_table} = 1;
104              
105 0           return $seq_table;
106             }
107              
108             sub _get_next_value_from_sequence {
109 0     0     my($self,$sequence_name) = @_;
110              
111 0           my $dbh = $self->get_default_handle();
112              
113             # FIXME can we use a statement handle with a wildcard as the table name here?
114 0 0         unless ($dbh->do("INSERT into $sequence_name values(null)")) {
115 0           die "Failed to INSERT into $sequence_name during id autogeneration: " . $dbh->errstr;
116             }
117              
118 0           my $new_id = $dbh->last_insert_id(undef,undef,$sequence_name,'next_value');
119 0 0         unless (defined $new_id) {
120 0           die "last_insert_id() returned undef during id autogeneration after insert into $sequence_name: " . $dbh->errstr;
121             }
122              
123 0 0         unless($dbh->do("DELETE from $sequence_name where next_value = $new_id")) {
124 0           die "DELETE from $sequence_name for next_value $new_id failed during id autogeneration";
125             }
126              
127 0           return $new_id;
128             }
129              
130              
131             sub get_bitmap_index_details_from_data_dictionary {
132             # Mysql dosen't have bitmap indexes.
133 0     0 0   return [];
134             }
135              
136              
137             sub set_savepoint {
138 0     0 0   my($self,$sp_name) = @_;
139              
140 0           my $dbh = $self->get_default_handle;
141 0           my $sp = $dbh->quote($sp_name);
142 0           $dbh->do("savepoint $sp_name");
143             }
144              
145              
146             sub rollback_to_savepoint {
147 0     0 0   my($self,$sp_name) = @_;
148              
149 0           my $dbh = $self->get_default_handle;
150 0           my $sp = $dbh->quote($sp_name);
151 0           $dbh->do("rollback to savepoint $sp_name");
152             }
153              
154              
155             sub _resolve_order_by_clause_for_column {
156 0     0     my($self, $column_name, $query_plan, $property_meta) = @_;
157              
158 0           my $is_optional = $property_meta->is_optional;
159              
160 0           my $column_clause = $column_name; # default, usual case
161 0 0         if ($is_optional) {
    0          
162 0 0         if ($query_plan->order_by_column_is_descending($column_name)) {
163 0           $column_clause = "CASE WHEN $column_name ISNULL THEN 0 ELSE 1 END, $column_name DESC";
164             } else {
165 0           $column_clause = "CASE WHEN $column_name ISNULL THEN 1 ELSE 0 END, $column_name";
166             }
167             } elsif ($query_plan->order_by_column_is_descending($column_name)) {
168 0           $column_clause = $column_name . ' DESC';
169             }
170 0           return $column_clause;
171             }
172              
173              
174              
175             # FIXME This works on Mysql 4.x (and later?). Mysql5 has a database called
176             # IMFORMATION_SCHEMA that may be more useful for these kinds of queries
177             sub get_unique_index_details_from_data_dictionary {
178 0     0 0   my($self, $owner_name, $table_name) = @_;
179              
180 0           my $dbh = $self->get_default_handle();
181 0 0         return undef unless $dbh;
182              
183             #$table_name = $dbh->quote($table_name);
184              
185 0           my $sql = qq(SHOW INDEX FROM $table_name FROM $owner_name);
186              
187 0           my $sth = $dbh->prepare($sql);
188 0 0         return undef unless $sth;
189              
190 0           $sth->execute();
191              
192 0           my $ret;
193 0           while (my $data = $sth->fetchrow_hashref()) {
194 0 0         next if ($data->{'Non_unique'});
195 0   0       $ret->{$data->{'Key_name'}} ||= [];
196 0           push @{ $ret->{ $data->{'Key_name'} } }, $data->{'Column_name'};
  0            
197             }
198              
199 0           return $ret;
200             }
201              
202             sub get_column_details_from_data_dictionary {
203 0     0 0   my $self = shift;
204              
205             # Mysql seems wierd about the distinction between catalog/database and schema/owner
206             # For 'ur update classes', it works if we just pass in undef for catalog
207             # The passed-in args are: $self,$catalog,$schema,$table,$column
208 0           my $catalog = shift;
209              
210 0           return $self->SUPER::get_column_details_from_data_dictionary(undef, @_);
211             }
212              
213             sub get_foreign_key_details_from_data_dictionary {
214 0     0 0   my $self = shift;
215              
216             # Mysql requires undef in some fields instead of an empty string
217 0 0         my @new_params = map { length($_) ? $_ : undef } @_;
  0            
218              
219 0           return $self->SUPER::get_foreign_key_details_from_data_dictionary(@new_params);
220             }
221              
222             my %ur_data_type_for_vendor_data_type = (
223             # DB type UR Type
224             'TINYINT' => ['Integer', undef],
225             'SMALLINT' => ['Integer', undef],
226             'MEDIUMINT' => ['Integer', undef],
227             'BIGINT' => ['Integer', undef],
228              
229             'BINARY' => ['Text', undef],
230             'VARBINARY' => ['Text', undef],
231             'TINYTEXT' => ['Text', undef],
232             'MEDIUMTEXT' => ['Text', undef],
233             'LONGTEXT' => ['Text', undef],
234              
235             'TINYBLOB' => ['Blob', undef],
236             'MEDIUMBLOB' => ['Blob', undef],
237             'LONGBLOB' => ['Blob', undef],
238             );
239             sub ur_data_type_for_data_source_data_type {
240 0     0 0   my($class,$type) = @_;
241              
242 0           $type = $class->normalize_vendor_type($type);
243 0           my $urtype = $ur_data_type_for_vendor_data_type{$type};
244 0 0         unless (defined $urtype) {
245 0           $urtype = $class->SUPER::ur_data_type_for_data_source_data_type($type);
246             }
247 0           return $urtype;
248             }
249              
250              
251              
252             1;
253              
254             =pod
255              
256             =head1 NAME
257              
258             UR::DataSource::MySQL - MySQL specific subclass of UR::DataSource::RDBMS
259              
260             =head1 DESCRIPTION
261              
262             This module provides the MySQL-specific methods necessary for interacting with
263             MySQL databases
264              
265             =head1 SEE ALSO
266              
267             L, L
268              
269             =cut