82 |
} |
} |
83 |
|
|
84 |
sub CondInsert { |
sub CondInsert { |
85 |
my @values=pop @_; # chop last argument which is array |
my $table = shift @_; |
86 |
my ($table, $name, $key, $fields) = @_; |
my $name = shift @_; |
87 |
my $sql = "SELECT $name FROM $table WHERE ($name = $key)"; |
my $key = shift @_; |
88 |
my $cnt = $pgdbh->do($sql); |
my $fields = shift @_; |
89 |
return 0 if ($cnt > 0); |
my @values=@_; # chop argument(s) are values array |
90 |
return 0 if ($#values == 0); |
my $sql = "SELECT count($name) FROM $table WHERE ($name = ?)"; |
91 |
|
my $sth = $pgdbh->prepare($sql); |
92 |
|
my $ret = $sth->execute($key); |
93 |
|
my ($cnt) = $sth->fetchrow_array(); |
94 |
|
return 0 if (!$cnt == 0); |
95 |
my @valtpl; |
my @valtpl; |
96 |
for (my $i=0; $i<=$#values; $i++) { push @valtpl,"?" } |
for (my $i=0; $i<=$#values; $i++) { push @valtpl,"?" } |
97 |
$sql = "INSERT INTO $table $fields VALUES (".join(",",@valtpl).")"; |
$sql = "INSERT INTO $table $fields VALUES (".join(",",@valtpl).")"; |
98 |
my $sth = $pgdbh->prepare($sql); |
$sth = $pgdbh->prepare($sql); |
99 |
my $ret = $sth->execute(@values); |
$ret = $sth->execute(@values); |
100 |
my $err = $pgdbh->errstr; |
my $err = $pgdbh->errstr; |
101 |
print &Log("[$sql] [".join(",",@values)."]".$err) if ($err); |
print &Log("[$sql] [".join(",",@values)."]".$err) if ($err); |
102 |
return $ret; |
return $ret; |