1 package SQLAbstraction
;
8 SQLAbstraction - Simplified interface to DBI.
19 use fields qw
/die_on_error dbh dbh_errstr/;
22 my SQLAbstraction
$self = shift;
23 my ( $p, $first ) = @_;
25 if ( not ref $self ) {
26 $self = fields
::new
($self);
29 my @required_params = qw( );
30 my %optional_params = ( die_on_error
=> 1 );
32 # Initialize new properties.
33 $self->init( $p, \
@required_params, \
%optional_params );
35 $self->check_unknown( $p, $first );
40 my SQLAbstraction
$self = shift;
42 my ( $p, $required, $optional ) = @_;
44 foreach my $param ( keys %{$optional} ) {
45 if ( exists( $p->{$param} ) ) {
46 $self->{$param} = $p->{$param};
47 delete( $p->{$param} );
50 $self->{$param} = $optional->{$param};
54 foreach my $param ( @
{$required} ) {
55 if ( exists( $p->{$param} ) ) {
56 $self->{$param} = $p->{$param};
57 delete( $p->{$param} );
60 croak
"Missing required parameter $param for " . ref($self);
66 my SQLAbstraction
$self = shift;
67 my ( $p, $first ) = @_;
69 if ( not( defined($first) ) and ( scalar( %{$p} ) ) ) {
70 my ($callingclass) = caller;
71 croak
"Unknown parameters to " . $callingclass . "::new " . join ", ",
77 my SQLAbstraction
$self = shift;
79 $self->{dbh
}->disconnect()
80 if defined( $self->{dbh
} );
96 die "You need to override Connect()";
102 =item Count( $table, $args )
104 Return the number of records in table $table matching $args. $table should
105 be a string containing the name of a table, $args should be a hash-reference
106 with field-names and values.
112 my ( $table, $args ) = @_;
114 my $dbh = $self->{dbh
};
119 map { push @where, "($_ = ?)"; push @values, $args->{$_}; }
122 my $where = join " and ", @where;
123 my $sql = "select count(*) from $table where $where";
125 my $sth = $dbh->prepare($sql)
126 or die "Prepare failed. $sql\nError: " . $dbh->errstr;
128 $sth->execute(@values)
129 or die "Execute failed. $sql\nError: " . $dbh->errstr;
131 my $aref = $sth->fetchrow_arrayref;
132 my $res = $aref->[0];
137 =item Delete( $table, $args )
139 Delete all records in table $table matching $args. $table should
140 be a string containing the name of a table, $args should be a hash-reference
141 with field-names and values. Returns the number of deleted records.
147 my ( $table, $args ) = @_;
149 my $dbh = $self->{dbh
};
154 map { push @where, "($_ = ?)"; push @values, $args->{$_}; }
157 my $where = join " and ", @where;
158 my $sql = "delete from $table where $where";
160 my $sth = $dbh->prepare_cached($sql)
161 or die "Prepare failed. $sql\nError: " . $dbh->errstr;
163 my $res = $sth->execute(@values);
169 =item Add( $table, $values, $die_on_error )
171 Add a new record to a table. $table should be a string containing the name
172 of a table, $values should be a hash-reference with field-names and values.
173 $die_on_error defaults to $self->{die_on_error}.
174 Returns the primary key assigned to the new record or -1 if the Add failed.
180 my ( $table, $args, $die_on_error ) = @_;
182 $die_on_error = $self->{die_on_error
} unless defined($die_on_error);
184 my $dbh = $self->{dbh
};
189 map { push @fields, "$_ = ?"; push @values, $args->{$_}; }
192 my $fields = join ", ", @fields;
193 my $sql = "insert into $table set $fields";
195 my $sth = $dbh->prepare_cached($sql)
196 or die "Prepare failed. $sql\nError: " . $dbh->errstr;
198 $sth->{PrintError
} = 0;
200 if ( not $sth->execute(@values) ) {
201 if ( $die_on_error ) {
202 die "Execute failed. $sql\nError: " . $dbh->errstr;
205 $self->{dbh_errstr
} = $dbh->errstr;
213 return $self->last_inserted_id();
216 =item Update( $table, $args, $new_values )
218 Update all records matching $args. $table should be a string containing the name of a table, $args and $new_values should be a hash-reference with field-names and values. Returns the number of updated records.
221 $ds->Update( "users", { uid => 1 },
222 { lastname => "Holmlund",
223 firstname => "Mattias" } );
229 my ( $table, $args, $new_values ) = @_;
231 my $dbh = $self->{dbh
};
236 map { push @where, "($_ = ?)"; push @values, $args->{$_}; }
239 my $where = join " and ", @where;
243 map { push @set, "$_ = ?"; push @setvalues, $new_values->{$_}; }
244 sort keys %{$new_values};
246 my $setexpr = join ", ", @set;
248 my $sql = "UPDATE $table SET $setexpr WHERE $where";
250 my $sth = $dbh->prepare($sql)
251 or die "Prepare failed. $sql\nError: " . $dbh->errstr;
253 my $res = $sth->execute( @setvalues, @values )
254 or die "Execute failed. $sql\nError: " . $dbh->errstr;
264 my ( $table, $args, $field, $order ) = @_;
266 my $dbh = $self->{dbh
};
271 map { push @where, "($_ = ?)"; push @values, $args->{$_}; }
274 my $where = join " and ", @where;
278 if ( defined $field ) {
279 $sql = "SELECT $field FROM $table WHERE $where";
282 $sql = "SELECT * FROM $table WHERE $where";
285 if( defined( $order ) and ( scalar( @
{$order} ) > 0 ) ) {
286 $sql .= " ORDER BY " . join( ", ", @
{$order} );
289 my $sth = $dbh->prepare_cached($sql)
290 or die "Prepare failed. $sql\nError: " . $dbh->errstr;
292 my $res = $sth->execute(@values)
293 or die "Execute failed. $sql\nError: " . $dbh->errstr;
298 =item Lookup( $table, $args [, $field] )
300 Retrieve values from a record. $table should be a string containing the name
301 of a table, $args should be a hash-reference with field-names and values.
302 If $field is specified, it should be the name of a field in the record and
303 Lookup will then return the contents of that field. If $field is undef,
304 a hash-reference with all fields and values in the record is returned.
306 If $args fails to identify one unique record, undef is returned.
312 my ( $table, $args, $field ) = @_;
314 my $sth = $self->_BuildLookup( $table, $args, $field );
316 my $row = $sth->fetchrow_hashref;
318 if ( not defined($row) ) {
323 my $row2 = $sth->fetchrow_hashref;
326 die "More than one record returned from $table"
327 if ( defined $row2 );
329 return $row->{$field} if defined $field;
333 =item LookupMany( $table, $args[, $order, $limit] )
335 Retrieve values from several records. $table should be a string
336 containing the name of a table, $args should be a hash-reference with
337 field-names and values. Returns a reference to an array containing
338 hashrefs for each record.
340 $order is an arrayref containing expressions to sort on.
342 $limit can be used to limit the number of records returned. Defaults to
349 my ( $table, $args, $order, $limit ) = @_;
351 my $sth = $self->_BuildLookup( $table, $args, undef, $order );
353 my $res = $sth->fetchall_arrayref( {}, $limit );
359 =item LookupManySql( $sql[, $argarray] )
361 Lookup possibly may records using an SQL expression. $sql should be a
362 string that can be sent to $dbh->prepare(). $argarray can be a
363 reference to an array of arguments that matches $sql.
365 Returns a reference to an array containing hashrefs for each record.
371 my ( $sql, $argarray ) = @_;
373 $argarray = [] if not defined $argarray;
375 my $dbh = $self->{dbh
};
377 my $sth = $dbh->prepare_cached($sql)
378 or die "Prepare failed. $sql\nError: " . $dbh->errstr;
380 my $res = $sth->execute(@
{$argarray})
381 or die "Execute failed. $sql\nError: " . $dbh->errstr;
383 $res = $sth->fetchall_arrayref( {} );
391 Same as Lookup, but returns a dbi statement handle that can be used
392 as an iterator. Can also take several field-arguments.
398 my ( $table, $args, @fields ) = @_;
400 my $dbh = $self->{dbh
};
405 map { push @where, "($_ = ?)"; push @values, $args->{$_}; }
408 my $where = join " and ", @where;
412 if ( scalar(@fields) > 0 ) {
413 $sql = "SELECT " . join( ",", @fields ) . " FROM $table WHERE $where";
416 $sql = "SELECT * FROM $table WHERE $where";
419 my $sth = $dbh->prepare($sql)
420 or die "Prepare failed. $sql\nError: " . $dbh->errstr;
422 my $res = $sth->execute(@values)
423 or die "Execute failed. $sql\nError: " . $dbh->errstr;
425 return undef if $res == 0;
431 my SQLAbstraction
$self = shift;
432 my ( $sqlexpr, $values ) = @_;
434 my $dbh = $self->{dbh
};
436 my $sth = $dbh->prepare($sqlexpr)
437 or die "Prepare failed. $sqlexpr\nError: " . $dbh->errstr;
439 my $res = $sth->execute( @
{$values} )
440 or die "Execute failed. $sqlexpr\nError: " . $dbh->errstr;
442 return ( $res, $sth );
446 my SQLAbstraction
$self = shift;
447 my ( $sqlexpr, $values ) = @_;
449 my ( $res, $sth ) = $self->Sql( $sqlexpr, $values );
456 Return the error message from the latest failed operation.
463 return $self->{dbh
}->errstr;
468 Copyright (C) 2007 Mattias Holmlund.
474 ### Setup coding system