| XAO-FS documentation | Contained in the XAO-FS distribution. |
XAO::DO::FS::Glue::SQL_DBI - DBI/DBD base for XAO::FS drivers
use XAO::Objects;
use base XAO::Objects->load(objname => 'FS::Glue::SQL_DBI');
sub foo {
my $self=shift;
$self->sql_connect(
dsn => 'DBI:mysql:test_fs',
user => 'test',
password => 'test',
);
}
This module provides a base for all XAO::FS SQL drivers that choose to be based on DBI/DBD foundation.
Establishes a connection to the database engine given. Arguments are:
dsn => standard DBI data source user => optional user name password => optional password
Returns true if the database connection is currently established.
Closes connection to the database.
Sends a single query to the database with no anticipation of any results. Can take arguments just the way prepare/execute do.
It is allowed to call sql_do() with an array reference in the second argument.
Executes a previously prepared statement optionally substituting some values, see sql_prepare(). Example:
my $pq=$self->sql_prepare("SELECT a,b FROM c WHERE d=?");
foreach my $value (1..10) {
$self->sql_execute($pq,$i);
...
}
As a shortcut it can also accept a text query in the first argument instead of manually calling sql_prepare() first. It is suggested to do so whenever you plan to call sql_execute() just once.
It is allowed to call sql_execute() with an array reference in the second argument.
Returns a piece of data that should be passed into sql_fetch_row() method.
When done sql_finish() should be called with the return value of sql_execute() as a parameter.
Returns a reference to an array containing next retrieved row. Example:
my $qr=$self->sql_execute("SELECT a,b FROM c");
my $row=$self->sql_fetch_row($qr);
Don't forget to call sql_finish() when you're done.
Frees up whatever internal structures might be occupied as a result of the previous call to sql_execute(). Example:
my $qr=$self->sql_execute("SHOW TABLES");
...
$self->sql_finish($qr);
An optimisation method -- returns a reference to the array containing all first elements of each row of the results set. Example:
my $qr=$self->sql_execute("SELECT unique_id FROM a");
my $ids=$self->sql_first_column($qr);
There is no need to call sql_finish() after sql_first_column().
An optimisation method -- returns a reference to the first row and finished the query.
my $qr=$self->sql_execute("SELECT a,b,c FROM t WHERE d=?",$uid);
my $row=$self->sql_first_row($qr);
There is no need to call sql_finish() after sql_first_row().
Prepares a query for subsequent execution using sql_execute() method. Guaranteed to return a reference of some sort. Example:
my $pq=$self->sql_prepare("SELECT a,b FROM c WHERE d=?");
$self->sql_execute($pq,123);
Copyright (c) 2005 Andrew Maltsev
Copyright (c) 2001-2004 Andrew Maltsev, XAO Inc.
<am@ejelta.com> -- http://ejelta.com/xao/
Further reading: XAO::FS, XAO::DO::FS::Glue.
| XAO-FS documentation | Contained in the XAO-FS distribution. |
############################################################################### package XAO::DO::FS::Glue::SQL_DBI; use strict; use XAO::Objects; use XAO::Utils; use DBI; use base XAO::Objects->load(objname => 'Atom'); use vars qw($VERSION); $VERSION=(0+sprintf('%u.%03u',(q$Id: SQL_DBI.pm,v 2.1 2005/01/14 00:23:54 am Exp $ =~ /\s(\d+)\.(\d+)\s/))) || die "Bad VERSION"; ###############################################################################
sub sql_connect ($%) { my $self=shift; my $args=get_args(\@_); my $dsn=$args->{dsn} || throw $self "sql_connect - no 'dsn' given"; $self->{sql}=DBI->connect($dsn,$args->{user},$args->{password}) || throw $self "sql_connect - can't connect to dsn='$dsn'"; } ###############################################################################
sub sql_connected ($) { return shift->{sql} ? 1 : 0; } ###############################################################################
sub sql_disconnect ($) { my $self=shift; $self->{sql}->disconnect if $self->{sql}; } ###############################################################################
sub sql_do ($$;@) { my ($self,$query,@values)=@_; if(@values && ref $values[0]) { $self->{sql}->do($query,undef,@{$values[0]}) || throw $self "sql_do - SQL error: ".$self->{sql}->errstr; } else { $self->{sql}->do($query,undef,@values) || throw $self "sql_do - SQL error: ".$self->{sql}->errstr; } } sub sql_do_no_error ($$;@) { my ($self,$query)=@_; $self->{sql}->do($query); } ###############################################################################
sub sql_execute ($$;@) { my ($self,$pq,@values)=@_; $pq=$self->sql_prepare($pq) unless ref $pq; $pq->execute(@values && ref($values[0]) ? @{$values[0]} : @values) || throw $self "sql_execute - SQL error: ".$pq->errstr; return $pq; } ###############################################################################
sub sql_fetch_row ($$) { my ($self,$qr)=@_; return $qr->fetchrow_arrayref; } ###############################################################################
sub sql_finish ($$) { my ($self,$qr)=@_; $qr->finish; } ###############################################################################
sub sql_first_column ($$) { my ($self,$qr)=@_; my @col; while(my $row=$qr->fetchrow_arrayref) { push @col,$row->[0]; } $qr->finish; return \@col; } ###############################################################################
sub sql_first_row ($$) { my ($self,$qr)=@_; my $row=$self->sql_fetch_row($qr); $self->sql_finish($qr); return $row; } ###############################################################################
sub sql_prepare ($$) { my ($self,$query)=@_; return $self->{sql}->prepare($query) || throw $self "sql_prepare - SQL error: ".$self->{sql}->errstr; } ############################################################################### 1; __END__