| 1 | =head1 NAME | ||
| 2 | |||
| 3 | DBIx::SQLEngine::Driver::Mysql - Support DBD::mysql | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | B<DBI Wrapper>: Adds methods to a DBI database handle. | ||
| 8 | |||
| 9 | my $sqldb = DBIx::SQLEngine->new( 'dbi:mysql:test' ); | ||
| 10 | |||
| 11 | B<Portability Subclasses:> Uses driver's idioms or emulation. | ||
| 12 | |||
| 13 | $hash_ary = $sqldb->fetch_select( | ||
| 14 | table => 'students' | ||
| 15 | limit => 5, offset => 10 | ||
| 16 | ); | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | This package provides a subclass of DBIx::SQLEngine which compensates for MySQL's idiosyncrasies. | ||
| 21 | |||
| 22 | =head2 About Driver Subclasses | ||
| 23 | |||
| 24 | You do not need to use this package directly; when you connect to a database, the SQLEngine object is automatically re-blessed in to the appropriate subclass. | ||
| 25 | |||
| 26 | For more information about the underlying driver class, see L<DBD::Mysql>. | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | ######################################################################## | ||
| 31 | |||
| 32 | package DBIx::SQLEngine::Driver::Mysql; | ||
| 33 | |||
| 34 | use strict; | ||
| 35 | use Carp; | ||
| 36 | |||
| 37 | ######################################################################## | ||
| 38 | |||
| 39 | ######################################################################## | ||
| 40 | |||
| 41 | =head1 DRIVER AND DATABASE FLAVORS | ||
| 42 | |||
| 43 | =head2 About DBMS Flavors | ||
| 44 | |||
| 45 | This driver uses the DatabaseFlavors trait in order to accomodate variations between different versions of MySQL. For more information, see L<DBIx::SQLEngine::Driver::Trait::DatabaseFlavors>. | ||
| 46 | |||
| 47 | =head2 Detecting DBMS Flavors | ||
| 48 | |||
| 49 | =over 4 | ||
| 50 | |||
| 51 | =item default_dbms_flavor() | ||
| 52 | |||
| 53 | $sqldb->default_dbms_flavor() : "V3_0" | ||
| 54 | |||
| 55 | By default, it is assumed that we're talking to an early version of MySQL 3.0, without transactions, unions, or stored procedures. | ||
| 56 | |||
| 57 | =item detect_dbms_flavor() | ||
| 58 | |||
| 59 | $sqldb->detect_dbms_flavor() : $flavor_name | ||
| 60 | |||
| 61 | Attempts to determine which version of MySQL we're connected to based on the results of are_transactions_supported() and detect_union_supported(). | ||
| 62 | |||
| 63 | =back | ||
| 64 | |||
| 65 | If you want to take advantage of any advanced features that may be available, first call select_detect_dbms_flavor(). | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | use DBIx::SQLEngine::Driver::Trait::DatabaseFlavors qw( :all !default_dbms_flavor !detect_dbms_flavor ); | ||
| 70 | |||
| 71 | sub default_dbms_flavor { 'V3_0' } | ||
| 72 | |||
| 73 | sub detect_dbms_flavor { | ||
| 74 | my $self = shift; | ||
| 75 | my $guess = 'V3_0'; | ||
| 76 | $guess = 'V3_23' if ( $self->are_transactions_supported() ); | ||
| 77 | $guess = 'V4_0' if ( $self->detect_union_supported() ); | ||
| 78 | return $guess; | ||
| 79 | } | ||
| 80 | |||
| 81 | ######################################################################## | ||
| 82 | |||
| 83 | =head2 Version Classes | ||
| 84 | |||
| 85 | The following subclasses provide support for particular versions of MySQL: | ||
| 86 | |||
| 87 | =over 4 | ||
| 88 | |||
| 89 | =item V3_0 | ||
| 90 | |||
| 91 | This is the earliest version we have a subclass for. Default. | ||
| 92 | No transactions, union selects, or stored procedures. | ||
| 93 | |||
| 94 | =item V3_23 | ||
| 95 | |||
| 96 | This is the first version with support for transactions. | ||
| 97 | No union selects, or stored procedures. | ||
| 98 | |||
| 99 | =item V4_0 | ||
| 100 | |||
| 101 | This is the first version with support for unions in select statements. | ||
| 102 | No stored procedures. | ||
| 103 | |||
| 104 | =item V5_0 | ||
| 105 | |||
| 106 | The version is still in development. It will be the first version to support stored procedures. | ||
| 107 | |||
| 108 | =back | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | FLAVOR_CLASSES: { | ||
| 113 | |||
| 114 | no strict; | ||
| 115 | |||
| 116 | ############################################################ | ||
| 117 | |||
| 118 | package DBIx::SQLEngine::Driver::Mysql::V3_0; | ||
| 119 | @ISA = qw( DBIx::SQLEngine::Driver::Mysql ); | ||
| 120 | |||
| 121 | use DBIx::SQLEngine::Driver::Trait::NoUnions ':all'; | ||
| 122 | use DBIx::SQLEngine::Driver::Trait::NoAdvancedFeatures qw( /transaction/ /storedproc/ ); | ||
| 123 | |||
| 124 | ############################################################ | ||
| 125 | |||
| 126 | package DBIx::SQLEngine::Driver::Mysql::V3_23; | ||
| 127 | @ISA = qw( DBIx::SQLEngine::Driver::Mysql ); | ||
| 128 | |||
| 129 | use DBIx::SQLEngine::Driver::Trait::NoUnions ':all'; | ||
| 130 | use DBIx::SQLEngine::Driver::Trait::NoAdvancedFeatures qw( /storedproc/ ); | ||
| 131 | |||
| 132 | ############################################################ | ||
| 133 | |||
| 134 | package DBIx::SQLEngine::Driver::Mysql::V4_0; | ||
| 135 | @ISA = qw( DBIx::SQLEngine::Driver::Mysql ); | ||
| 136 | |||
| 137 | use DBIx::SQLEngine::Driver::Trait::NoAdvancedFeatures qw( /storedproc/ ); | ||
| 138 | |||
| 139 | ############################################################ | ||
| 140 | |||
| 141 | package DBIx::SQLEngine::Driver::Mysql::V5_0; | ||
| 142 | @ISA = qw( DBIx::SQLEngine::Driver::Mysql ); | ||
| 143 | |||
| 144 | ############################################################ | ||
| 145 | |||
| 146 | } | ||
| 147 | |||
| 148 | ######################################################################## | ||
| 149 | |||
| 150 | ######################################################################## | ||
| 151 | |||
| 152 | =head1 FETCHING DATA (SQL DQL) | ||
| 153 | |||
| 154 | =head2 Methods Used By Complex Queries | ||
| 155 | |||
| 156 | =over 4 | ||
| 157 | |||
| 158 | =item sql_limit() | ||
| 159 | |||
| 160 | Adds support for SQL select limit clause. | ||
| 161 | |||
| 162 | =back | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | sub sql_limit { | ||
| 167 | my $self = shift; | ||
| 168 | my ( $limit, $offset, $sql, @params ) = @_; | ||
| 169 | |||
| 170 | # You can't apply "limit" to non-table fetches like "select LAST_INSERT_ID" | ||
| 171 | if ( $sql =~ /\bfrom\b/i and $limit or $offset) { | ||
| 172 | $limit ||= 1_000_000; # MySQL select with offset requires a limit | ||
| 173 | $sql .= " limit " . ( $offset ? "$offset," : '' ) . $limit; | ||
| 174 | } | ||
| 175 | |||
| 176 | return ($sql, @params); | ||
| 177 | } | ||
| 178 | |||
| 179 | ######################################################################## | ||
| 180 | |||
| 181 | ######################################################################## | ||
| 182 | |||
| 183 | =head1 EDITING DATA (SQL DML) | ||
| 184 | |||
| 185 | =head2 Insert to Add Data | ||
| 186 | |||
| 187 | =over 4 | ||
| 188 | |||
| 189 | =item do_insert_with_sequence() | ||
| 190 | |||
| 191 | $sqldb->do_insert_with_sequence( $sequence_name, %sql_clauses ) : $row_count | ||
| 192 | |||
| 193 | Implemented using _seq_do_insert_postfetch and seq_fetch_current. | ||
| 194 | |||
| 195 | =item seq_fetch_current() | ||
| 196 | |||
| 197 | $sqldb->seq_fetch_current( ) : $current_value | ||
| 198 | |||
| 199 | Implemented using MySQL's "select LAST_INSERT_ID()". Note that this | ||
| 200 | doesn't fetch the current sequence value for a given table, since | ||
| 201 | it doesn't respect the table and field arguments, but merely returns | ||
| 202 | the last sequencial value created during this session. | ||
| 203 | |||
| 204 | =back | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | # $rows = $self->do_insert_with_sequence( $sequence, %clauses ); | ||
| 209 | sub do_insert_with_sequence { | ||
| 210 | (shift)->_seq_do_insert_postfetch( @_ ) | ||
| 211 | } | ||
| 212 | |||
| 213 | # $current_id = $sqldb->seq_fetch_current( ); | ||
| 214 | sub seq_fetch_current { | ||
| 215 | my ($self, $table, $field) = @_; | ||
| 216 | $self->fetch_one_value( sql => 'select LAST_INSERT_ID()' ); | ||
| 217 | } | ||
| 218 | |||
| 219 | ######################################################################## | ||
| 220 | |||
| 221 | ######################################################################## | ||
| 222 | |||
| 223 | =head1 DEFINING STRUCTURES (SQL DDL) | ||
| 224 | |||
| 225 | =head2 Detect Tables and Columns | ||
| 226 | |||
| 227 | =over 4 | ||
| 228 | |||
| 229 | =item sql_detect_table() | ||
| 230 | |||
| 231 | $sqldb->sql_detect_table ( $tablename ) : %sql_select_clauses | ||
| 232 | |||
| 233 | Implemented using MySQL's "select * from $tablename limit 1". | ||
| 234 | |||
| 235 | =back | ||
| 236 | |||
| 237 | =cut | ||
| 238 | |||
| 239 | sub sql_detect_table { | ||
| 240 | my ($self, $tablename) = @_; | ||
| 241 | return ( sql => "select * from $tablename limit 1" ); | ||
| 242 | } | ||
| 243 | |||
| 244 | ######################################################################## | ||
| 245 | |||
| 246 | =head2 Column Type Methods | ||
| 247 | |||
| 248 | =over 4 | ||
| 249 | |||
| 250 | =item dbms_create_column_types() | ||
| 251 | |||
| 252 | $sqldb->dbms_create_column_types () : %column_type_codes | ||
| 253 | |||
| 254 | Implemented using MySQL's blob and auto_increment types. | ||
| 255 | |||
| 256 | =item dbms_create_column_text_long_type() | ||
| 257 | |||
| 258 | $sqldb->dbms_create_column_text_long_type () : $col_type_str | ||
| 259 | |||
| 260 | Implemented using MySQL's blob type. | ||
| 261 | |||
| 262 | =back | ||
| 263 | |||
| 264 | =cut | ||
| 265 | |||
| 266 | sub dbms_create_column_types { | ||
| 267 | 'sequential' => 'int auto_increment primary key', | ||
| 268 | 'binary' => 'blob', | ||
| 269 | } | ||
| 270 | |||
| 271 | sub dbms_create_column_text_long_type { | ||
| 272 | 'blob' | ||
| 273 | } | ||
| 274 | |||
| 275 | ######################################################################## | ||
| 276 | |||
| 277 | ######################################################################## | ||
| 278 | |||
| 279 | =head1 INTERNAL STATEMENT METHODS (DBI STH) | ||
| 280 | |||
| 281 | =head2 Statement Error Handling | ||
| 282 | |||
| 283 | =over 4 | ||
| 284 | |||
| 285 | =item recoverable_query_exceptions() | ||
| 286 | |||
| 287 | $sqldb->recoverable_query_exceptions() : @common_error_messages | ||
| 288 | |||
| 289 | Provides a list of error messages which represent common | ||
| 290 | communication failures or other incidental errors. | ||
| 291 | |||
| 292 | =back | ||
| 293 | |||
| 294 | =cut | ||
| 295 | |||
| 296 | sub recoverable_query_exceptions { | ||
| 297 | 'Lost connection to MySQL server', | ||
| 298 | 'MySQL server has gone away', | ||
| 299 | 'no statement executing', | ||
| 300 | 'fetch without execute', | ||
| 301 | "\Qfetch() without execute()", | ||
| 302 | } | ||
| 303 | |||
| 304 | ######################################################################## | ||
| 305 | |||
| 306 | =head1 SEE ALSO | ||
| 307 | |||
| 308 | See L<DBIx::SQLEngine> for the overall interface and developer documentation. | ||
| 309 | |||
| 310 | See L<DBIx::SQLEngine::Docs::ReadMe> for general information about | ||
| 311 | this distribution, including installation and license information. | ||
| 312 | |||
| 313 | =cut | ||
| 314 | |||
| 315 | ######################################################################## | ||
| 316 | |||
| 317 | 1; |