git-svn-id: https://svn.code.sf.net/p/infobot/code/trunk/blootbot@706
c11ca15a-4712-0410-83d8-
924469b57eb5
- $query .= " WHERE ".&hashref2where($where_href) if ($where_href);
+ my $where = ($where_href) ? &hashref2where($where_href) : "";
+ $query .= " WHERE $where" if ($where);
$query .= "$other" if $other;
if (!($sth = $dbh->prepare($query))) {
$query .= "$other" if $other;
if (!($sth = $dbh->prepare($query))) {
&SQLDebug($query);
if (!$sth->execute) {
&ERROR("sqlSelectMany: execute: '$query'");
&SQLDebug($query);
if (!$sth->execute) {
&ERROR("sqlSelectMany: execute: '$query'");
# Todo: Always return array?
sub sqlSelect {
my $sth = &sqlSelectMany(@_);
# Todo: Always return array?
sub sqlSelect {
my $sth = &sqlSelectMany(@_);
+ if (!defined $sth) {
+ &WARN("sqlSelect failed.");
+ return;
+ }
my @retval = $sth->fetchrow_array;
my @retval = $sth->fetchrow_array;
$sth->finish;
if (scalar @retval > 1) {
$sth->finish;
if (scalar @retval > 1) {
sub sqlSelectColHash {
my ($table, $select, $where_href, $type) = @_;
my $sth = &sqlSelectMany($table, $select, $where_href);
sub sqlSelectColHash {
my ($table, $select, $where_href, $type) = @_;
my $sth = &sqlSelectMany($table, $select, $where_href);
+ if (!defined $sth) {
+ &WARN("sqlSelectColhash failed.");
+ return;
+ }
my %retval;
if (defined $type and $type == 2) {
my %retval;
if (defined $type and $type == 2) {
# Note: useful for returning only one/first row of data.
sub sqlSelectRowHash {
my $sth = &sqlSelectMany(@_);
# Note: useful for returning only one/first row of data.
sub sqlSelectRowHash {
my $sth = &sqlSelectMany(@_);
+ if (!defined $sth) {
+ &WARN("sqlSelectRowHash failed.");
+ return;
+ }
my $retval = $sth->fetchrow_hashref();
$sth->finish;
my $retval = $sth->fetchrow_hashref();
$sth->finish;
-# Usage: &sqlSet($table, $data_href, $where_href);
+# Usage: &sqlSet($table, $where_href, $data_href);
# Return: 1 for success, undef for failure.
sub sqlSet {
# Return: 1 for success, undef for failure.
sub sqlSet {
- my ($table, $data_href, $where_href) = @_;
+ my ($table, $where_href, $data_href) = @_;
if (!defined $table or $table =~ /^\s*$/) {
&WARN("sqlSet: table == NULL.");
if (!defined $table or $table =~ /^\s*$/) {
&WARN("sqlSet: table == NULL.");
- my $where = &hashref2where($where_href) if ($where_href);
- my $update = &hashref2update($data_href) if ($data_href);
- my (@k,@v) = &hashref2array($data_href);
+ my $k = (keys %{ $where_href })[0];
+ my $result = &sqlSelect($table, $k, $where_href);
+ &DEBUG("result is not defined :(") if (!defined $result);
- if (!@k or !@v) {
- &WARN("sqlSet: keys or vals is NULL.");
- return;
- }
-
- my $result = &sqlGet($table, join(',', @k), $where);
- if (defined $result) {
+ if (1 or defined $result) {
&sqlUpdate($table, $data_href, $where_href);
} else {
&sqlUpdate($table, $data_href, $where_href);
} else {
+ &DEBUG("doing insert...");
+
+ # hack.
+ my %hash = %{ $where_href };
+ # add data_href values...
+ foreach (keys %{ $data_href }) {
+ $hash{ $_ } = ${ $data_href }{$_};
+ }
+
+ $data_href = \%hash;
&sqlInsert($table, $data_href);
}
&sqlInsert($table, $data_href);
}
- my (@k,@v) = &hashref2array($data_href);
+ my ($k_aref, $v_aref) = &hashref2array($data_href);
+ my @k = @{ $k_aref };
+ my @v = @{ $v_aref };
+
if (!@k or !@v) {
&WARN("sqlInsert: keys or vals is NULL.");
return;
if (!@k or !@v) {
&WARN("sqlInsert: keys or vals is NULL.");
return;
&sqlRaw("Insert($table)", sprintf(
"INSERT %s INTO %s (%s) VALUES (%s)",
&sqlRaw("Insert($table)", sprintf(
"INSERT %s INTO %s (%s) VALUES (%s)",
- $other, $table, join(',',@k), join(',',@v)
+ ($other || ""), $table, join(',',@k), join(',',@v)
- my (@k,@v) = &hashref2array($data_href);
+ my ($k_aref, $v_aref) = &hashref2array($data_href);
+ my @k = @{ $k_aref };
+ my @v = @{ $v_aref };
+
if (!@k or !@v) {
&WARN("sqlReplace: keys or vals is NULL.");
return;
if (!@k or !@v) {
&WARN("sqlReplace: keys or vals is NULL.");
return;
sub hashref2where {
my ($href) = @_;
sub hashref2where {
my ($href) = @_;
+ if (!defined $href) {
+ &WARN("hashref2where: href == NULL.");
+ return;
+ }
+
if (ref($href) ne "HASH") {
if (ref($href) ne "HASH") {
- &WARN("hashref2where: href is not HASH ref.");
+ &WARN("hashref2where: href is not HASH ref (href => $href)");
- return join(', ', map { $_ => $hash{$_} } sort keys %hash);
+ return join(', ', map { $_."=".$hash{$_} } sort keys %hash);