| 1 | package UR::DataSource::MySQL; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 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 | 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 | 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 | sub _default_sql_like_escape_string { undef }; | ||
| 36 | sub _format_sql_like_escape_string { return } | ||
| 37 | |||
| 38 | sub can_savepoint { 1;} | ||
| 39 | |||
| 40 | sub does_support_limit_offset { | ||
| 41 | my($self, $bx) = @_; | ||
| 42 | |||
| 43 | my $tmpl = $bx->template; | ||
| 44 | if ($tmpl->offset and !defined($tmpl->limit)) { | ||
| 45 | return 0; # Can't have offset without limit | ||
| 46 | } else { | ||
| 47 | return 1; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | sub resolve_limit_offset_clause { | ||
| 52 | my($self, $query_plan) = @_; | ||
| 53 | |||
| 54 | my $limit = $self->_resolve_limit_value_from_query_plan($query_plan); | ||
| 55 | my $offset = $self->_resolve_offset_value_from_query_plan($query_plan); | ||
| 56 | |||
| 57 | if (defined($limit) and $offset) { | ||
| 58 | return sprintf('limit %d, %d', $offset, $limit); | ||
| 59 | } elsif (defined $limit) { | ||
| 60 | return sprintf('limit %d', $limit); | ||
| 61 | } else { | ||
| 62 | return ''; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | *_init_created_dbh = \&init_created_handle; | ||
| 68 | sub init_created_handle | ||
| 69 | { | ||
| 70 | my ($self, $dbh) = @_; | ||
| 71 | return unless defined $dbh; | ||
| 72 | |||
| 73 | $dbh->{LongTruncOk} = 0; | ||
| 74 | return $dbh; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _ignore_table { | ||
| 78 | my $self = shift; | ||
| 79 | my $table_name = shift; | ||
| 80 | 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 | my $self = shift->_singleton_object; | ||
| 90 | my ($table_name,$column_name) = @_; | ||
| 91 | |||
| 92 | my $dbh = $self->get_default_handle(); | ||
| 93 | |||
| 94 | # See if the sequence generator "table" is already there | ||
| 95 | my $seq_table = sprintf('URMETA_%s_%s_SEQ', $table_name, $column_name); | ||
| 96 | #$DB::single = 1; | ||
| 97 | unless ($self->{'_has_sequence_generator'}->{$seq_table} or | ||
| 98 | grep {$_ eq $seq_table} $self->get_table_names() ) { | ||
| 99 | unless ($dbh->do("CREATE TABLE IF NOT EXISTS $seq_table (next_value integer PRIMARY KEY AUTO_INCREMENT)")) { | ||
| 100 | die "Failed to create sequence generator $seq_table: ".$dbh->errstr(); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | $self->{'_has_sequence_generator'}->{$seq_table} = 1; | ||
| 104 | |||
| 105 | return $seq_table; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _get_next_value_from_sequence { | ||
| 109 | my($self,$sequence_name) = @_; | ||
| 110 | |||
| 111 | my $dbh = $self->get_default_handle(); | ||
| 112 | |||
| 113 | # FIXME can we use a statement handle with a wildcard as the table name here? | ||
| 114 | unless ($dbh->do("INSERT into $sequence_name values(null)")) { | ||
| 115 | die "Failed to INSERT into $sequence_name during id autogeneration: " . $dbh->errstr; | ||
| 116 | } | ||
| 117 | |||
| 118 | my $new_id = $dbh->last_insert_id(undef,undef,$sequence_name,'next_value'); | ||
| 119 | unless (defined $new_id) { | ||
| 120 | die "last_insert_id() returned undef during id autogeneration after insert into $sequence_name: " . $dbh->errstr; | ||
| 121 | } | ||
| 122 | |||
| 123 | unless($dbh->do("DELETE from $sequence_name where next_value = $new_id")) { | ||
| 124 | die "DELETE from $sequence_name for next_value $new_id failed during id autogeneration"; | ||
| 125 | } | ||
| 126 | |||
| 127 | return $new_id; | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | sub get_bitmap_index_details_from_data_dictionary { | ||
| 132 | # Mysql dosen't have bitmap indexes. | ||
| 133 | return []; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | sub set_savepoint { | ||
| 138 | my($self,$sp_name) = @_; | ||
| 139 | |||
| 140 | my $dbh = $self->get_default_handle; | ||
| 141 | my $sp = $dbh->quote($sp_name); | ||
| 142 | $dbh->do("savepoint $sp_name"); | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | sub rollback_to_savepoint { | ||
| 147 | my($self,$sp_name) = @_; | ||
| 148 | |||
| 149 | my $dbh = $self->get_default_handle; | ||
| 150 | my $sp = $dbh->quote($sp_name); | ||
| 151 | $dbh->do("rollback to savepoint $sp_name"); | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | sub _resolve_order_by_clause_for_column { | ||
| 156 | my($self, $column_name, $query_plan, $property_meta) = @_; | ||
| 157 | |||
| 158 | my $is_optional = $property_meta->is_optional; | ||
| 159 | |||
| 160 | my $column_clause = $column_name; # default, usual case | ||
| 161 | if ($is_optional) { | ||
| 162 | if ($query_plan->order_by_column_is_descending($column_name)) { | ||
| 163 | $column_clause = "CASE WHEN $column_name ISNULL THEN 0 ELSE 1 END, $column_name DESC"; | ||
| 164 | } else { | ||
| 165 | $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 | $column_clause = $column_name . ' DESC'; | ||
| 169 | } | ||
| 170 | 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 | my($self, $owner_name, $table_name) = @_; | ||
| 179 | |||
| 180 | my $dbh = $self->get_default_handle(); | ||
| 181 | return undef unless $dbh; | ||
| 182 | |||
| 183 | #$table_name = $dbh->quote($table_name); | ||
| 184 | |||
| 185 | my $sql = qq(SHOW INDEX FROM $table_name FROM $owner_name); | ||
| 186 | |||
| 187 | my $sth = $dbh->prepare($sql); | ||
| 188 | return undef unless $sth; | ||
| 189 | |||
| 190 | $sth->execute(); | ||
| 191 | |||
| 192 | my $ret; | ||
| 193 | while (my $data = $sth->fetchrow_hashref()) { | ||
| 194 | next if ($data->{'Non_unique'}); | ||
| 195 | $ret->{$data->{'Key_name'}} ||= []; | ||
| 196 | push @{ $ret->{ $data->{'Key_name'} } }, $data->{'Column_name'}; | ||
| 197 | } | ||
| 198 | |||
| 199 | return $ret; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub get_column_details_from_data_dictionary { | ||
| 203 | 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 | my $catalog = shift; | ||
| 209 | |||
| 210 | return $self->SUPER::get_column_details_from_data_dictionary(undef, @_); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub get_foreign_key_details_from_data_dictionary { | ||
| 214 | my $self = shift; | ||
| 215 | |||
| 216 | # Mysql requires undef in some fields instead of an empty string | ||
| 217 | my @new_params = map { length($_) ? $_ : undef } @_; | ||
| 218 | |||
| 219 | 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 | my($class,$type) = @_; | ||
| 241 | |||
| 242 | $type = $class->normalize_vendor_type($type); | ||
| 243 | my $urtype = $ur_data_type_for_vendor_data_type{$type}; | ||
| 244 | unless (defined $urtype) { | ||
| 245 | $urtype = $class->SUPER::ur_data_type_for_data_source_data_type($type); | ||
| 246 | } | ||
| 247 | 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<UR::DataSource>, L<UR::DataSource::RDBMS> | ||
| 268 | |||
| 269 | =cut |