| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | package Data::Fallback::DBI; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Carp qw(confess); | ||
| 7 | |||
| 8 | use DBI; | ||
| 9 | |||
| 10 | use Data::Fallback; | ||
| 11 | use vars qw(@ISA); | ||
| 12 | @ISA = qw(Data::Fallback); | ||
| 13 | |||
| 14 | sub connect { | ||
| 15 | my $self = shift; | ||
| 16 | my $db = $self->get_db; | ||
| 17 | confess "need a \$db to connect with" unless $db; | ||
| 18 | my $dbh = DBI->connect(@{$db}); | ||
| 19 | confess "couldn't connect with " . join(", ", @{$db}) unless $dbh; | ||
| 20 | confess "got a \$DBI::errstr with " . join(", ", @{$db}) . ": $DBI::errstr" if $DBI::errstr; | ||
| 21 | return $dbh; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub _GET { | ||
| 25 | my $self = shift; | ||
| 26 | |||
| 27 | my $return = 0; | ||
| 28 | my $row_hash_ref = $self->get_content; | ||
| 29 | |||
| 30 | if( $row_hash_ref && exists $row_hash_ref->{$self->get_cache_key('item')} ) { | ||
| 31 | $self->{update}{group} = $row_hash_ref; | ||
| 32 | $self->{update}{item} = $row_hash_ref->{$self->get_cache_key('item')}; | ||
| 33 | $self->set_cache('DBI', 'item', $self->get_full_cache_key, $self->{update}{item}); | ||
| 34 | $return = 1; | ||
| 35 | } | ||
| 36 | |||
| 37 | return $return; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub get_full_cache_key { | ||
| 41 | my $self = shift; | ||
| 42 | return $self->get_connect_string . "." . | ||
| 43 | $self->get_cache_key('content') . "." . | ||
| 44 | $self->get_cache_key('primary_key') . "." . | ||
| 45 | $self->get_cache_key('item'); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub get_content { | ||
| 49 | my $self = shift; | ||
| 50 | |||
| 51 | my ($found_in_cache, $hash_ref, $found_at_cache_level) = | ||
| 52 | $self->check_cache('DBI', 'group', $self->get_full_cache_key); | ||
| 53 | |||
| 54 | if($found_in_cache) { | ||
| 55 | # already set in $hash_ref, so we're done | ||
| 56 | } else { | ||
| 57 | my $dbh = $self->get_dbh; | ||
| 58 | $self->{cache}{DBI} ||= {}; | ||
| 59 | $self->{cache}{DBI}{sth} ||= {}; | ||
| 60 | |||
| 61 | my $connect_string = $self->get_connect_string; | ||
| 62 | |||
| 63 | my $sth_key = $connect_string . "." . $self->{hash}{content}; | ||
| 64 | $self->{cache}{DBI}{sth}{$sth_key} ||= $dbh->prepare($self->{hash}{content}); | ||
| 65 | |||
| 66 | my @primary_key = GET_VALUES($self->get_cache_key('primary_key')); | ||
| 67 | |||
| 68 | my $execute = $self->{cache}{DBI}{sth}{$sth_key}->execute(@primary_key); | ||
| 69 | confess "got a \$DBI::errstr with $self->{hash}{content}: $DBI::errstr" if $DBI::errstr; | ||
| 70 | $hash_ref = $self->{cache}{DBI}{sth}{$sth_key}->fetchrow_hashref('NAME_lc'); | ||
| 71 | confess "got a \$DBI::errstr with $self->{hash}{content}: $DBI::errstr" if $DBI::errstr; | ||
| 72 | $self->set_cache('DBI', 'group', $self->get_full_cache_key, $hash_ref) if($hash_ref); | ||
| 73 | } | ||
| 74 | return $hash_ref; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub get_connect_string { | ||
| 78 | my $self = shift; | ||
| 79 | my $db = $self->get_db; | ||
| 80 | return join(",", @{$db}); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub get_db { | ||
| 84 | my $self = shift; | ||
| 85 | return $self->{hash}{db} || $self->{db}; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub get_dbh { | ||
| 89 | my $self = shift; | ||
| 90 | my $db = $self->get_db; | ||
| 91 | |||
| 92 | confess "need a \$db to connect with" unless $db; | ||
| 93 | $self->{dbh_cache} ||= {}; | ||
| 94 | my $connect_string = $self->get_connect_string; | ||
| 95 | my $dbh = $self->{dbh_cache}{$connect_string}; | ||
| 96 | unless($dbh) { | ||
| 97 | $dbh = $self->connect; | ||
| 98 | $self->{dbh_cache}{$connect_string} = $dbh; | ||
| 99 | } | ||
| 100 | return $dbh; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub SET_ITEM { | ||
| 104 | my $self = shift; | ||
| 105 | my $dbh = $self->get_dbh; | ||
| 106 | my $update_item_sql = $self->get_update_item_sql; | ||
| 107 | my $key_name = $self->{hash}{content} . "." . $self->get_cache_key('primary_key') . ".$self->{item}"; | ||
| 108 | $self->{cache}{DBI} ||= {}; | ||
| 109 | $self->{cache}{DBI}{sql} ||= {}; | ||
| 110 | $self->{cache}{DBI}{sql}{$key_name} ||= $update_item_sql; | ||
| 111 | $self->{cache}{DBI}{sth}{$key_name} ||= $dbh->prepare($self->{cache}{DBI}{sql}{$key_name}); | ||
| 112 | confess "got a \$DBI::errstr with $self->{cache}{DBI}{sql}{$key_name}: $DBI::errstr" if $DBI::errstr; | ||
| 113 | |||
| 114 | my @primary_key = GET_VALUES($self->get_cache_key('primary_key')); | ||
| 115 | my $execute = $self->{cache}{DBI}{sth}{$key_name}->execute($self->{update}{item}, @primary_key); | ||
| 116 | confess "got a \$DBI::errstr with $self->{cache}{DBI}{sql}{$key_name}: $DBI::errstr" if $DBI::errstr; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub SET_GROUP { | ||
| 120 | my $self = shift; | ||
| 121 | my $dbh = $self->get_dbh; | ||
| 122 | my ($table, $where) = $self->get_table_where($self->{hash}{content}); | ||
| 123 | my $key_name = $self->{hash}{content} . "." . $self->get_cache_key('primary_key') . ".$self->{item}"; | ||
| 124 | my @primary_key = GET_VALUES($self->get_cache_key('primary_key')); | ||
| 125 | $self->{cache}{DBI} ||= {}; | ||
| 126 | $self->{cache}{DBI}{sql} ||= {}; | ||
| 127 | $self->{cache}{DBI}{sql}{$key_name} ||= $self->{hash}{content}; | ||
| 128 | $self->{cache}{DBI}{sth}{$key_name} ||= $dbh->prepare($self->{cache}{DBI}{sql}{$key_name}); | ||
| 129 | $self->{cache}{DBI}{sth}{$key_name}->execute(@primary_key); | ||
| 130 | if(my @array = $self->{cache}{DBI}{sth}{$key_name}->fetchrow_array) { | ||
| 131 | # record is there, need to UPDATE | ||
| 132 | unless($self->do_update) { | ||
| 133 | } | ||
| 134 | } else { | ||
| 135 | # record is not there, need to SELECT | ||
| 136 | confess "got a \$DBI::errstr with $self->{hash}{content}: $DBI::errstr" if $DBI::errstr; | ||
| 137 | unless($self->do_insert) { | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | sub do_insert { | ||
| 143 | my $self = shift; | ||
| 144 | my $dbh = $self->get_dbh; | ||
| 145 | my ($insert_sql, $args) = $self->get_insert_info; | ||
| 146 | my $connect_string = $self->get_connect_string; | ||
| 147 | $self->{cache}{DBI} ||= {}; | ||
| 148 | $self->{cache}{DBI}{sql} ||= {}; | ||
| 149 | $self->{cache}{DBI}{sql}{$insert_sql} ||= $insert_sql; | ||
| 150 | my $key = $connect_string . "." . $insert_sql; | ||
| 151 | $self->{cache}{DBI}{sth}{$key} ||= $dbh->prepare($self->{cache}{DBI}{sql}{$insert_sql}); | ||
| 152 | my $inserted = $self->{cache}{DBI}{sth}{$key}->execute(@{$args}); | ||
| 153 | confess "got a \$DBI::errstr with $self->{hash}{content}: $DBI::errstr" if $DBI::errstr; | ||
| 154 | return $inserted; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub get_insert_info { | ||
| 158 | my $self = shift; | ||
| 159 | my $table = $self->get_table($self->{hash}{content}); | ||
| 160 | my ($cols, $values) = ('', ''); | ||
| 161 | my @args = (); | ||
| 162 | foreach(sort keys %{$self->{update}{group}}) { | ||
| 163 | $cols .= "$_, "; | ||
| 164 | $values .= "?, "; | ||
| 165 | push @args, $self->{update}{group}{$_}; | ||
| 166 | } | ||
| 167 | foreach($cols, $values) { | ||
| 168 | s/,\s+$//; | ||
| 169 | } | ||
| 170 | my $insert_sql = "INSERT INTO $table (" . $cols . ") VALUES (" . $values . ")"; | ||
| 171 | return ($insert_sql, \@args); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub do_update { | ||
| 175 | my $self = shift; | ||
| 176 | my $dbh = $self->get_dbh; | ||
| 177 | my ($update_sql, $args) = $self->get_update_info; | ||
| 178 | my $connect_string = $self->get_connect_string; | ||
| 179 | my $key = $connect_string . "." . $update_sql; | ||
| 180 | $self->{cache}{DBI} ||= {}; | ||
| 181 | $self->{cache}{DBI}{sql} ||= {}; | ||
| 182 | $self->{cache}{DBI}{sql}{$update_sql} ||= $update_sql; | ||
| 183 | $self->{cache}{DBI}{sth}{$key} ||= $dbh->prepare($self->{cache}{DBI}{sql}{$update_sql}); | ||
| 184 | |||
| 185 | my @primary_key = GET_VALUES($self->get_cache_key('primary_key')); | ||
| 186 | |||
| 187 | my $updated = $self->{cache}{DBI}{sth}{$key}->execute(@{$args}, @primary_key); | ||
| 188 | confess "got a \$DBI::errstr with $self->{hash}{content}: $DBI::errstr" if $DBI::errstr; | ||
| 189 | return $updated; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub get_update_info { | ||
| 193 | my $self = shift; | ||
| 194 | my ($table, $where) = $self->get_table_where($self->{hash}{content}); | ||
| 195 | my ($cols, $values) = ('', ''); | ||
| 196 | my @args = (); | ||
| 197 | my $update_sql = "UPDATE $table SET "; | ||
| 198 | foreach(sort keys %{$self->{update}{group}}) { | ||
| 199 | $update_sql .= "$_ = ?, "; | ||
| 200 | push @args, $self->{update}{group}{$_}; | ||
| 201 | } | ||
| 202 | $update_sql =~ s/,\s+$//; | ||
| 203 | $update_sql .= " WHERE " . $where; | ||
| 204 | return ($update_sql, \@args); | ||
| 205 | } | ||
| 206 | |||
| 207 | sub get_update_item_sql { | ||
| 208 | my $self = shift; | ||
| 209 | |||
| 210 | my ($table, $where) = $self->get_table_where($self->{hash}{content}); | ||
| 211 | my $update_item_sql = "UPDATE $table SET $self->{item} = ? WHERE $where"; | ||
| 212 | return $update_item_sql; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub get_table_where { | ||
| 216 | my $self = shift; | ||
| 217 | my $sql = shift; | ||
| 218 | my $table = $self->get_table($sql); | ||
| 219 | my $where = $self->get_where($sql); | ||
| 220 | return ($table, $where); | ||
| 221 | } | ||
| 222 | |||
| 223 | sub get_table { | ||
| 224 | my $self = shift; | ||
| 225 | my $sql = shift; | ||
| 226 | my $table; | ||
| 227 | if($sql =~ /^\s*select\s+.+?\s+from\s+([a-z0-9_]+)\s+where/si) { | ||
| 228 | $table = $1; | ||
| 229 | } else { | ||
| 230 | die "couldn't find a table name easily in $sql"; | ||
| 231 | } | ||
| 232 | return $table; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub get_where { | ||
| 236 | my $self = shift; | ||
| 237 | my $sql = shift; | ||
| 238 | my $where; | ||
| 239 | if($sql =~ /^\s*select\s+.+?\s+from\s+[a-z0-9_]+\s+where\s+(.+)/si) { | ||
| 240 | $where = $1; | ||
| 241 | } else { | ||
| 242 | die "couldn't get your update where easily from $sql"; | ||
| 243 | } | ||
| 244 | return $where; | ||
| 245 | } | ||
| 246 | |||
| 247 | sub GET_VALUES { | ||
| 248 | my $values=shift; | ||
| 249 | return () unless defined $values; | ||
| 250 | if (ref $values eq "ARRAY") { | ||
| 251 | return @$values; | ||
| 252 | } | ||
| 253 | return ($values); | ||
| 254 | } | ||
| 255 | |||
| 256 | sub DESTROY { | ||
| 257 | my $self = shift; | ||
| 258 | } | ||
| 259 | |||
| 260 | 1; |