| 1 | |||
| 2 | package Class::DBI::Lite::mysql; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings 'all'; | ||
| 6 | use base 'Class::DBI::Lite'; | ||
| 7 | use Class::DBI::Lite::TableInfo; | ||
| 8 | use Carp 'confess'; | ||
| 9 | |||
| 10 | |||
| 11 | #============================================================================== | ||
| 12 | sub set_up_table | ||
| 13 | { | ||
| 14 | my $s = shift; | ||
| 15 | |||
| 16 | # Get our columns: | ||
| 17 | my $table = shift; | ||
| 18 | $s->_init_meta( $table ); | ||
| 19 | $s->after_set_up_table; | ||
| 20 | 1; | ||
| 21 | }# end set_up_table() | ||
| 22 | |||
| 23 | |||
| 24 | #============================================================================== | ||
| 25 | sub get_tables | ||
| 26 | { | ||
| 27 | my ($s, $schema) = @_; | ||
| 28 | |||
| 29 | ($schema) = $schema =~ m/DBI\:mysql\:([^:]+)/; | ||
| 30 | |||
| 31 | my $sth = $s->db_Main->prepare(<<""); | ||
| 32 | select table_name | ||
| 33 | from information_schema.tables | ||
| 34 | where table_schema = ? | ||
| 35 | and table_type = 'BASE TABLE' | ||
| 36 | |||
| 37 | $sth->execute( $schema ); | ||
| 38 | |||
| 39 | my @out = ( ); | ||
| 40 | while( my ($name) = $sth->fetchrow ) | ||
| 41 | { | ||
| 42 | push @out, $name; | ||
| 43 | }# end while() | ||
| 44 | $sth->finish(); | ||
| 45 | |||
| 46 | @out ? return @out : return; | ||
| 47 | }# end get_tables() | ||
| 48 | |||
| 49 | |||
| 50 | #============================================================================== | ||
| 51 | sub get_meta_columns | ||
| 52 | { | ||
| 53 | my ($s, $schema, $table) = @_; | ||
| 54 | |||
| 55 | ($schema) = $schema =~ m/DBI\:mysql\:([^:]+)/; | ||
| 56 | # Get our columns: | ||
| 57 | my $sth = $s->db_Main->prepare(<<""); | ||
| 58 | SELECT * | ||
| 59 | FROM information_schema.columns | ||
| 60 | WHERE table_schema = ? | ||
| 61 | AND table_name = ? | ||
| 62 | |||
| 63 | # Simple discovery of fields and PK: | ||
| 64 | $sth->execute( $schema, $table ); | ||
| 65 | my @cols = ( ); | ||
| 66 | my $PK; | ||
| 67 | while( my $rec = $sth->fetchrow_hashref ) | ||
| 68 | { | ||
| 69 | $rec->{ lc($_) } = delete($rec->{$_}) foreach keys(%$rec); | ||
| 70 | # Is this the primary column?: | ||
| 71 | $PK = $rec->{column_name} | ||
| 72 | if $rec->{column_key} && | ||
| 73 | lc($rec->{column_key}) eq 'pri'; | ||
| 74 | push @cols, $rec->{column_name}; | ||
| 75 | }# end while() | ||
| 76 | $sth->finish(); | ||
| 77 | |||
| 78 | confess "Table " . $schema . ".$table doesn't exist or has no columns" | ||
| 79 | unless @cols; | ||
| 80 | |||
| 81 | return { | ||
| 82 | Primary => [ $PK ], | ||
| 83 | Essential => \@cols, | ||
| 84 | All => \@cols, | ||
| 85 | }; | ||
| 86 | }# end get_meta_columns() | ||
| 87 | |||
| 88 | |||
| 89 | #============================================================================== | ||
| 90 | sub after_set_up_table { } | ||
| 91 | |||
| 92 | |||
| 93 | #============================================================================== | ||
| 94 | sub get_table_info | ||
| 95 | { | ||
| 96 | my $s = shift; | ||
| 97 | my $class = ref($s) || $s; | ||
| 98 | my $cur = $class->db_Main->prepare("SHOW COLUMNS FROM " . $class->table); | ||
| 99 | $cur->execute; | ||
| 100 | |||
| 101 | my $info = Class::DBI::Lite::TableInfo->new( $class->table ); | ||
| 102 | |||
| 103 | my %key_types = ( | ||
| 104 | UNI => 'unique', | ||
| 105 | PRI => 'primary_key' | ||
| 106 | ); | ||
| 107 | |||
| 108 | while( my $res = $cur->fetchrow_hashref ) | ||
| 109 | { | ||
| 110 | $res->{lc($_)} = delete($res->{$_}) foreach keys(%$res); | ||
| 111 | |||
| 112 | my ($type) = $res->{type} =~ m/^([^\(\)]+)/; | ||
| 113 | my $length; | ||
| 114 | if( $type =~ m/(text|varchar|char)/i ) | ||
| 115 | { | ||
| 116 | ($length) = $res->{type} =~ m/\((\d+)\)/; | ||
| 117 | }# end if() | ||
| 118 | |||
| 119 | # If it's an enum, we want to provide a list of possible values: | ||
| 120 | my %enum_args = ( ); | ||
| 121 | if( lc($type) eq 'enum' ) | ||
| 122 | { | ||
| 123 | my $val = "$res->{type}"; | ||
| 124 | $val =~ s/^enum\(//; | ||
| 125 | $val =~ s/\)$//; | ||
| 126 | my @vals = grep { length($_) } map { | ||
| 127 | $_ =~ s/^'//; | ||
| 128 | $_ =~ s/'$//; | ||
| 129 | $_; | ||
| 130 | } split /,\s*/, $val; | ||
| 131 | $enum_args{enum_values} = \@vals; | ||
| 132 | }#end if() | ||
| 133 | |||
| 134 | $info->add_column( | ||
| 135 | name => $res->{field}, | ||
| 136 | type => lc($type), | ||
| 137 | length => $length, | ||
| 138 | is_pk => $res->{key} eq 'PRI' ? 1 : 0, | ||
| 139 | is_nullable => $res->{null} eq 'NO' ? 0 : 1, | ||
| 140 | default_value => $res->{default}, | ||
| 141 | key => $key_types{ $res->{key} }, | ||
| 142 | %enum_args, | ||
| 143 | ); | ||
| 144 | }# end while() | ||
| 145 | $cur->finish; | ||
| 146 | |||
| 147 | return $info; | ||
| 148 | }# end get_table_info() | ||
| 149 | |||
| 150 | |||
| 151 | #============================================================================== | ||
| 152 | sub get_last_insert_id | ||
| 153 | { | ||
| 154 | $_[0]->db_Main->{mysql_insertid}; | ||
| 155 | }# end get_last_insert_id() | ||
| 156 | |||
| 157 | |||
| 158 | sub lock_table | ||
| 159 | { | ||
| 160 | my ($s, $table) = @_; | ||
| 161 | |||
| 162 | $s->db_Main->do("lock tables $table read"); | ||
| 163 | }# end lock_table() | ||
| 164 | |||
| 165 | |||
| 166 | sub unlock_table | ||
| 167 | { | ||
| 168 | shift->db_Main->do("unlock tables"); | ||
| 169 | }# end unlock_table() | ||
| 170 | |||
| 171 | 1;# return true: | ||
| 172 |