Commit c3b42a5d authored by Nigel Kukard's avatar Nigel Kukard
Browse files

* Add support for different queries based on database type

parent 2e8324b5
......@@ -52,9 +52,11 @@ sub Init
# Check if we created
my $dbh = cbp::dbilayer->new($dbconfig->{'DSN'},$dbconfig->{'Username'},$dbconfig->{'Password'},$dbconfig->{'TablePrefix'});
my $dbh = cbp::dbilayer->new($dbconfig->{'DSN'},$dbconfig->{'Username'},$dbconfig->{'Password'},
$dbconfig->{'TablePrefix'});
return undef if (!defined($dbh));
return $dbh;
}
......@@ -66,6 +68,8 @@ sub new
# Iternals
my $self = {
_type => undef,
_dbh => undef,
_error => undef,
......@@ -89,6 +93,10 @@ sub new
return undef;
}
# Try grab database type
$self->{_dsn} =~ /^DBI:([^:]+):/i;
$self->{_type} = (defined($1) && $1 ne "") ? lc($1) : "unknown";
# Create...
bless $self, $class;
return $self;
......@@ -138,6 +146,16 @@ sub connect
}
# Return database type
# Args: none
sub type
{
my $self = shift;
return $self->{_type};
}
# Check database connection
# Args: none
sub _check
......
......@@ -121,17 +121,37 @@ sub DBSelect
# Args: <command statement>
sub DBDo
{
my ($command,@params) = @_;
my (@params) = @_;
my $table_prefix = $dbh->table_prefix();
# Check type of params we have
if (ref($params[0]) eq 'HASH') {
my $queryHash = $params[0];
my $dbType = $dbh->type();
# Check DB type is defined, if not use *
if (defined($queryHash->{$dbType})) {
@params = @{$queryHash->{$dbType}};
} elsif (defined($queryHash->{'*'})) {
@params = @{$queryHash->{'*'}};
} else {
setError("Error executing, database type in query not fund and no '*' query found");
return undef;
}
}
# Grab command and data
my $command = shift(@params);
my @data = @params;
# Replace table prefix macro
$command =~ s/\@TP\@/$table_prefix/g;
# Prepare query
my $sth;
if (!($sth = $dbh->do($command,@params))) {
if (!($sth = $dbh->do($command,@data))) {
setError("Error executing command '$command': ".$dbh->Error());
return undef;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment