| 1 | package DBstorage::DBI; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | DBstorage::DBI - dbedit driver using DBI interfaces | ||
| 5 | |||
| 6 | =head1 DESCRIPTIONN | ||
| 7 | This is the DBI SQL driver for dbedit | ||
| 8 | |||
| 9 | =head1 TODO | ||
| 10 | Should encapsulate file headers in objects | ||
| 11 | |||
| 12 | =head1 LICENSE | ||
| 13 | |||
| 14 | Copyright (C) 2002 Globewide Network Academy | ||
| 15 | Relased under the SCHEME license see LICENSE.SCHEME.txt for details | ||
| 16 | =cut | ||
| 17 | |||
| 18 | use DBI; | ||
| 19 | use DBstorage; | ||
| 20 | use Symbol; | ||
| 21 | |||
| 22 | @DBstorage::DBI::ISA = qw(DBstorage); | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my $proto = shift; | ||
| 28 | my $class = ref($proto) || $proto; | ||
| 29 | my $connect = shift; | ||
| 30 | my $self = {}; | ||
| 31 | bless ($self, $class); | ||
| 32 | $self->{'dbh'} = DBI->connect($connect); | ||
| 33 | $self->{'nocursor'} = 1; | ||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub DESTROY { | ||
| 38 | my($self) = @_; | ||
| 39 | $self->{'dbh'}->disconnect(); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub find { | ||
| 43 | my $self = shift; | ||
| 44 | my $filename = shift; | ||
| 45 | my $keyref = shift; | ||
| 46 | my $foundref = shift; | ||
| 47 | my $lastref = shift; | ||
| 48 | my($field); | ||
| 49 | my(@field_list) = (); | ||
| 50 | %{$foundref} = (); | ||
| 51 | my ($predicate_list) = $self->predicate_list($keyref, " and "); | ||
| 52 | my ($command); | ||
| 53 | if ($filename !~ /^select /i) { | ||
| 54 | $command = "select * from " . $filename; | ||
| 55 | } else { | ||
| 56 | $command = $filename; | ||
| 57 | } | ||
| 58 | |||
| 59 | if ($command !~ / where /i) { | ||
| 60 | $command .= " where " . $predicate_list; | ||
| 61 | } else { | ||
| 62 | $command .= " and " . $predicate_list; | ||
| 63 | } | ||
| 64 | |||
| 65 | if ($self->{'debug'}) { | ||
| 66 | print "SQL Command: $command"; | ||
| 67 | } | ||
| 68 | $self->{'sth'} = $self->{'dbh'}->prepare($command); | ||
| 69 | $self->{'sth'}->execute; | ||
| 70 | if (defined($self->{'sth'})) { | ||
| 71 | %{$foundref} = %{$self->{'sth'}->fetchrow_hashref}; | ||
| 72 | } | ||
| 73 | $self->{'sth'}->finish; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub get_nth { | ||
| 77 | my $self = shift; | ||
| 78 | my $filename = shift; | ||
| 79 | my $record = shift; | ||
| 80 | my $foundref = shift; | ||
| 81 | my $lastref = shift; | ||
| 82 | $self->{'sth'} = $self->{'dbh'}->prepare($filename); | ||
| 83 | $self->{'sth'}->execute; | ||
| 84 | $self->{'sth'}->fetchrow_arrayref; | ||
| 85 | %{$foundref} = %{$self->{'sth'}->fetchrow_hashref}; | ||
| 86 | $self->{'sth'}->finish; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub attrib { | ||
| 90 | my $self = shift; | ||
| 91 | my $file = shift; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub delete { | ||
| 95 | my $self = shift; | ||
| 96 | my $table = shift; | ||
| 97 | my $keyref = shift; | ||
| 98 | my $delete_all = shift; | ||
| 99 | my($command) = "delete from $table where "; | ||
| 100 | $command .= $self->predicate_list($keyref, " and "); | ||
| 101 | if ($self->{'debug'}) { | ||
| 102 | print "SQL Command: $command<br>"; | ||
| 103 | } | ||
| 104 | $self->{'dbh'}->do($command); | ||
| 105 | |||
| 106 | } | ||
| 107 | |||
| 108 | sub append { | ||
| 109 | my $self = shift; | ||
| 110 | my $table = shift; | ||
| 111 | my $keyref = shift; | ||
| 112 | my($command) = "insert into $table "; | ||
| 113 | my($key); | ||
| 114 | foreach $key (keys %{$keyref}) { | ||
| 115 | if ($keyref->{$key} eq "") { | ||
| 116 | delete $keyref->{$key}; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | my(@fields) = keys %{$keyref}; | ||
| 120 | my(@values) = map {$self->quote($_);} @{$keyref}{@fields}; | ||
| 121 | |||
| 122 | $command .= "(" . join (", ", @fields) . ")"; | ||
| 123 | $command .= " values "; | ||
| 124 | $command .= "(" . join (", ", @values) . ")"; | ||
| 125 | $| = 1; | ||
| 126 | if ($self->{'debug'}) { | ||
| 127 | print "SQL Command: $command<br>\n"; | ||
| 128 | } | ||
| 129 | return $self->{'dbh'}->do($command); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub replace { | ||
| 133 | my $self = shift; | ||
| 134 | my $filename = shift; | ||
| 135 | my $keyref = shift; | ||
| 136 | my $replace_ref = shift; | ||
| 137 | my $replace_all = shift; | ||
| 138 | my($command) = "update $filename "; | ||
| 139 | $command .= " set " . $self->predicate_list_with_null($replace_ref, " , "); | ||
| 140 | $command .= " where " . $self->predicate_list($keyref, " and "); | ||
| 141 | if ($self->{'debug'}) { | ||
| 142 | print "SQL Command: $command<br>"; | ||
| 143 | } | ||
| 144 | $self->{'dbh'}->do($command); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub replace_regexp { | ||
| 148 | my $self = shift; | ||
| 149 | my $filename = shift; | ||
| 150 | my $keyref = shift; | ||
| 151 | my $replace_ref = shift; | ||
| 152 | my $replace_all = shift; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub create { | ||
| 156 | my ($self, $filename, $fieldref) = @_; | ||
| 157 | my ($command) = "create table $filename "; | ||
| 158 | my ($field, @field_list); | ||
| 159 | if ($self->{'debug'}) { | ||
| 160 | print "SQL Command: $command<br>"; | ||
| 161 | } | ||
| 162 | |||
| 163 | foreach $field (keys %{$fieldref}) { | ||
| 164 | push (@field_list, "$field $fieldref->{$field}"); | ||
| 165 | } | ||
| 166 | |||
| 167 | $command .= "(" . join (", ", @field_list) . ")"; | ||
| 168 | $self->{'dbh'}->do($command); | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | sub open { | ||
| 173 | my $self = shift; | ||
| 174 | my $filename = shift; | ||
| 175 | my $filehandle = shift; | ||
| 176 | my $line; | ||
| 177 | my $command; | ||
| 178 | |||
| 179 | if ($filename =~ /^select/i) { | ||
| 180 | $command = $filename; | ||
| 181 | } else { | ||
| 182 | $command = "select * from $filename"; | ||
| 183 | } | ||
| 184 | if ($self->{'debug'}) { | ||
| 185 | print "SQL Command: $command<br>"; | ||
| 186 | } | ||
| 187 | |||
| 188 | $self->{'sth'} = $self->{'dbh'}->prepare($command); | ||
| 189 | $self->{'sth'}->execute; | ||
| 190 | $self->{'FIELDS'} = $self->{'sth'}->{'NAME'}; | ||
| 191 | |||
| 192 | if ($self->{'sth'} eq undef) { | ||
| 193 | return 0; | ||
| 194 | } else { | ||
| 195 | return 1; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | sub read { | ||
| 200 | my $self = shift; | ||
| 201 | my $hashref = shift; | ||
| 202 | my($ref) = $self->{'sth'}->fetchrow_hashref; | ||
| 203 | |||
| 204 | if ($ref eq undef) { | ||
| 205 | return 0; | ||
| 206 | } else { | ||
| 207 | %{$hashref} = %{$ref}; | ||
| 208 | return 1; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | sub read_array { | ||
| 213 | my $self = shift; | ||
| 214 | my $arrayref = shift; | ||
| 215 | my $ref = $self->{'sth'}->fetchrow_arrayref; | ||
| 216 | |||
| 217 | if ($ref eq undef) { | ||
| 218 | return 0; | ||
| 219 | } else { | ||
| 220 | @{$arrayref} = @{$ref}; | ||
| 221 | return 1; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | |||
| 226 | sub close { | ||
| 227 | my $self = shift; | ||
| 228 | $self->{'sth'}->finish(); | ||
| 229 | } | ||
| 230 | |||
| 231 | sub fields { | ||
| 232 | my $self = shift; | ||
| 233 | return @{$self->{'FIELDS'}}; | ||
| 234 | } | ||
| 235 | |||
| 236 | sub type { | ||
| 237 | my $self = shift; | ||
| 238 | } | ||
| 239 | |||
| 240 | sub predicate_list { | ||
| 241 | my $self = shift; | ||
| 242 | my $hashref = shift; | ||
| 243 | my $join = shift; | ||
| 244 | my(@field_list) = (); | ||
| 245 | my($field); | ||
| 246 | |||
| 247 | foreach $field (keys %{$hashref}) { | ||
| 248 | if ($field ne "") { | ||
| 249 | if ($hashref->{$field} ne "") { | ||
| 250 | push(@field_list, "$field = " . | ||
| 251 | $self->quote($hashref->{$field}));; | ||
| 252 | } else { | ||
| 253 | push(@field_list, "($field = '' or $field is null)"); | ||
| 254 | } | ||
| 255 | } | ||
| 256 | } | ||
| 257 | return join($join, @field_list); | ||
| 258 | } | ||
| 259 | |||
| 260 | |||
| 261 | sub predicate_list_with_null { | ||
| 262 | my $self = shift; | ||
| 263 | my $hashref = shift; | ||
| 264 | my $join = shift; | ||
| 265 | my(@field_list) = (); | ||
| 266 | my($field); | ||
| 267 | |||
| 268 | foreach $field (keys %{$hashref}) { | ||
| 269 | if ($hashref->{$field} ne "") { | ||
| 270 | push(@field_list, "$field = " . | ||
| 271 | $self->quote($hashref->{$field}));; | ||
| 272 | } else { | ||
| 273 | push(@field_list, "$field = NULL"); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | return join($join, @field_list); | ||
| 277 | } | ||
| 278 | |||
| 279 | sub errstr { | ||
| 280 | my $self = shift; | ||
| 281 | return $self->{'dbh'}->errstr(); | ||
| 282 | } | ||
| 283 | |||
| 284 | sub quote { | ||
| 285 | my $self = shift; | ||
| 286 | my $item = shift; | ||
| 287 | return $self->{'dbh'}->quote($item); | ||
| 288 | } | ||
| 289 | |||
| 290 | sub do { | ||
| 291 | my $self = shift; | ||
| 292 | my $command = shift; | ||
| 293 | return $self->{'dbh'}->do($command); | ||
| 294 | } | ||
| 295 | |||
| 296 | sub commit { | ||
| 297 | my $self = shift; | ||
| 298 | my $ofile = shift; | ||
| 299 | my $nfile = shift; | ||
| 300 | |||
| 301 | my ($dbh) = $self->{'dbh'}; | ||
| 302 | my ($old_auto_commit) = $dbh->{'AutoCommit'}; | ||
| 303 | my ($old_raise_error) = $dbh->{'RaiseError'}; | ||
| 304 | $dbh->{'AutoCommit'} = 0; | ||
| 305 | $dbh->{'RaiseError'} = 1; | ||
| 306 | eval { | ||
| 307 | $dbh->do("drop table $ofile"); | ||
| 308 | $dbh->do("alter table $nfile rename to $ofile"); | ||
| 309 | $dbh->commit(); | ||
| 310 | }; | ||
| 311 | if ($@) { | ||
| 312 | warn "Transaction aborted because $@"; | ||
| 313 | $dbh->rollback(); | ||
| 314 | } | ||
| 315 | $dbh->{'AutoCommit'} = $old_auto_commit; | ||
| 316 | $dbh->{'RaiseError'} = $old_raise_error; | ||
| 317 | } | ||
| 318 | |||
| 319 | 1; | ||
| 320 | |||
| 321 | |||
| 322 | |||
| 323 | |||
| 324 | |||
| 325 |