| 1 | # | ||
| 2 | # DESCRIPTION | ||
| 3 | # PerlORM - Object relational mapper (ORM) for Perl. PerlORM is Perl | ||
| 4 | # library that implements object-relational mapping. Its features are | ||
| 5 | # much similar to those of Java's Hibernate library, but interface is | ||
| 6 | # much different and easier to use. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Alexey V. Akimov <akimov_alexey@sourceforge.net> | ||
| 10 | # | ||
| 11 | # COPYRIGHT | ||
| 12 | # Copyright (C) 2005-2006 Alexey V. Akimov | ||
| 13 | # | ||
| 14 | # This library is free software; you can redistribute it and/or | ||
| 15 | # modify it under the terms of the GNU Lesser General Public | ||
| 16 | # License as published by the Free Software Foundation; either | ||
| 17 | # version 2.1 of the License, or (at your option) any later version. | ||
| 18 | # | ||
| 19 | # This library is distributed in the hope that it will be useful, | ||
| 20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 22 | # Lesser General Public License for more details. | ||
| 23 | # | ||
| 24 | # You should have received a copy of the GNU Lesser General Public | ||
| 25 | # License along with this library; if not, write to the Free Software | ||
| 26 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 27 | # | ||
| 28 | |||
| 29 | # This module tries to emulate transactions for old MySQL databases | ||
| 30 | |||
| 31 | package ORM::Db::DBI::MySQLemTa; | ||
| 32 | |||
| 33 | $VERSION = 0.83; | ||
| 34 | |||
| 35 | use base 'ORM::Db::DBI'; | ||
| 36 | |||
| 37 | ## | ||
| 38 | ## CONSTRUCTORS | ||
| 39 | ## | ||
| 40 | |||
| 41 | sub new | ||
| 42 | { | ||
| 43 | my $class = shift; | ||
| 44 | my %arg = @_; | ||
| 45 | |||
| 46 | $arg{driver} = 'mysql'; | ||
| 47 | $class->SUPER::new( %arg ); | ||
| 48 | } | ||
| 49 | |||
| 50 | ## | ||
| 51 | ## CLASS METHODS | ||
| 52 | ## | ||
| 53 | |||
| 54 | sub qc | ||
| 55 | { | ||
| 56 | my $self = shift; | ||
| 57 | my $str = shift; | ||
| 58 | |||
| 59 | if( defined $str ) | ||
| 60 | { | ||
| 61 | $str =~ s/\'/\'\'/g; | ||
| 62 | $str = "'$str'"; | ||
| 63 | } | ||
| 64 | else | ||
| 65 | { | ||
| 66 | $str = 'NULL'; | ||
| 67 | } | ||
| 68 | |||
| 69 | return $str; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub qi | ||
| 73 | { | ||
| 74 | my $self = shift; | ||
| 75 | my $str = shift; | ||
| 76 | |||
| 77 | $str =~ s/\`/\`\`/g; | ||
| 78 | $str = "`$str`"; #" | ||
| 79 | |||
| 80 | return $str; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub qt { $_[0]->qi( $_[1] ); } | ||
| 84 | sub qf { $_[0]->qi( $_[1] ); } | ||
| 85 | |||
| 86 | ## | ||
| 87 | ## OBJECT METHODS | ||
| 88 | ## | ||
| 89 | |||
| 90 | sub begin_transaction | ||
| 91 | { | ||
| 92 | die "'begin_transaction' is not implemented yet for '$_[0]'"; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub commit_transaction | ||
| 96 | { | ||
| 97 | die "'commit_transaction' is not implemented yet for '$_[0]'"; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub rollback_transaction | ||
| 101 | { | ||
| 102 | die "'rollback_transaction' is not implemented yet for '$_[0]'"; | ||
| 103 | } | ||
| 104 | |||
| 105 | ## use: $id = $db->insertid() | ||
| 106 | ## | ||
| 107 | sub insertid | ||
| 108 | { | ||
| 109 | my $self = shift; | ||
| 110 | $self->_db_handler ? $self->_db_handler->{mysql_insertid} : undef; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub table_struct | ||
| 114 | { | ||
| 115 | my $self = shift; | ||
| 116 | my %arg = @_; | ||
| 117 | my $error = ORM::Error->new; | ||
| 118 | my %field; | ||
| 119 | my %defaults; | ||
| 120 | my $res; | ||
| 121 | my $data; | ||
| 122 | |||
| 123 | ## Fetch table structure | ||
| 124 | $res = $self->select( error=>$error, query=>( 'SHOW COLUMNS FROM '.$self->qt($arg{table}) ) ); | ||
| 125 | unless( $error->fatal ) | ||
| 126 | { | ||
| 127 | while( $data = $res->next_row ) | ||
| 128 | { | ||
| 129 | $defaults{$data->{Field}} = $data->{Default}; | ||
| 130 | $field{$data->{Field}} = $arg{class}->_db_type_to_class( $data->{Field}, $data->{Type} ); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | ## Fetch class references | ||
| 135 | if( scalar( %field ) ) | ||
| 136 | { | ||
| 137 | $res = $self->select | ||
| 138 | ( | ||
| 139 | error => $error, | ||
| 140 | query => 'SELECT * FROM '.$self->qt('_ORM_refs').' WHERE class='.$self->qc( $arg{class} ), | ||
| 141 | ); | ||
| 142 | unless( $error->fatal ) | ||
| 143 | { | ||
| 144 | while( $data = $res->next_row ) | ||
| 145 | { | ||
| 146 | if( exists $field{$data->{prop}} ) | ||
| 147 | { | ||
| 148 | $field{$data->{prop}} = $data->{ref_class}; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | $error->upto( $arg{error} ); | ||
| 155 | return \%field, \%defaults; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub _lost_connection | ||
| 159 | { | ||
| 160 | my $self = shift; | ||
| 161 | my $err = shift; | ||
| 162 | |||
| 163 | defined $err && ( $err == 2006 || $err == 2013 ); | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | ## use: $encrypted_password = $db->pwd( $password ) | ||
| 168 | ## | ||
| 169 | sub pwd | ||
| 170 | { | ||
| 171 | my $self = shift; | ||
| 172 | my $pwd = shift; | ||
| 173 | my $st; | ||
| 174 | |||
| 175 | $st = $self->_db_handler && $self->_db_handler->prepare( 'select password('.($self->qc($pwd)).')' ); | ||
| 176 | if( $st ) | ||
| 177 | { | ||
| 178 | $st->execute; | ||
| 179 | return ($st->fetchrow_arrayref)->[0]; | ||
| 180 | } | ||
| 181 | else | ||
| 182 | { | ||
| 183 | return undef; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | ## | ||
| 188 | ## SQL FUNCTIONS | ||
| 189 | ## | ||
| 190 | |||
| 191 | sub _func_concat { shift; ORM::Filter::Func->new( 'CONCAT', @_ ); } |