データベース接続にはDBIとDBD::ODBCを使う。
DBD::Sybaseではプレースホルダーが使えない(?)のでDBD::ODBCしかないが、動きが怪しい。
レコードが存在する場合にupdateしようとしたがうまくいかない。
そこで、レコードが存在する場合deleteしてinsertすることにしたが、これも同一接続内ではうまくいかない。
仕方ないので、select、deleteで一旦切断し、insert用に再度接続するようにした。
以下サンプル。
#!/usr/bin/perl --
use MIME::Parser;
use DBI;
use warnings;
use strict;
my $dbname = 'hoge';
my $user_id = "";
my $item_id = '10';
my $parser = new MIME::Parser;
$parser->output_to_core(1);
my $entity;
eval {
local $SIG{ALRM} = sub { die "timeout" };
alarm 10;
$entity = $parser->parse(\*STDIN) or die;
alarm 0;
};
alarm 0;
if ($@) { exit; }
my $from = $entity->head->get('from');
if ($from =~ /<(.+?\@.+?)>/) {
$from = $1;
}
$user_id = _check_from($dbname, $from);
if ($user_id ne "" and $entity->is_multipart) {
_save_attached_file($entity, $dbname, $user_id, $item_id);
}
exit;
sub _connect_db($$$$) {
my $db_host = shift;
my $db_name = shift;
my $db_user = shift;
my $db_passwd = shift;
my $data_source = "dbi:ODBC:"
. "server=" . $db_host . ";"
. "database=" . $db_name;
my $dbh = DBI->connect($data_source, $db_user, $db_passwd)
or die $DBI::errstr;
$dbh->{AutoCommit} = 0;
$dbh->{RaiseError} = 1;
return $dbh;
}
sub _check_from($$) {
my $dbname = shift;
my $from = shift;
my $user_id = "";
my $db_server = 'windows-db';
my $db_name = $dbname;
my $db_user = 'foo';
my $db_passwd = 'bar';
my $dbh
= _connect_db($db_server, $db_name, $db_user, $db_passwd);
my $stmt = "SELECT user_id"
. " FROM users WITH (NOLOCK)"
. " WHERE email = '$from'";
my $sth = $dbh->prepare($stmt)
or die $dbh->errstr;
$sth->execute or die $dbh->errstr;
while (my $row = $sth->fetchrow_arrayref) {
($user_id) = @$row;
}
$sth->finish;
$dbh->disconnect;
return $user_id;
}
sub _save_attached_file($$$$) {
my $entity = shift;
my $dbname = shift;
my $user_id = shift;
my $item_id = shift;
my $parts_count = $entity->parts;
for (my $i = 1; $i < $parts_count; $i++) {
my $part_entity = $entity->parts($i) ?
$entity->parts($i) :
$entity->parts($i)->parts($i);
my $content_type = $part_entity->head->mime_type;
if ($content_type =~ /\/(html|plain|alternative)/) {
next;
}
my $attachment = $part_entity->bodyhandle->as_string;
my $filename = $part_entity->head->recommended_filename;
my $db_server = 'earth-db';
my $db_name = $dbname;
my $db_user = 'foo';
my $db_passwd = 'bar';
my $dbh
= _connect_db($db_server, $db_name, $db_user, $db_passwd);
my $stmt = "SELECT user_id FROM files WITH(NOLOCK)"
. " WHERE user_id = '$user_id'"
. " AND item_id = '$item_id'";
my $sth = $dbh->prepare($stmt) or die $dbh->errstr;
$sth->execute or die $dbh->errstr;
my $result_user_id = "";
while (my $row = $sth->fetchrow_arrayref) {
($result_user_id) = @$row;
}
$sth->finish;
if ($result_user_id) {
$stmt = "DELETE FROM files"
. " WHERE user_id = '$user_id'"
. " AND item_id = '$item_id'";
$sth = $dbh->prepare($stmt) or die $dbh->errstr;
eval {
$sth->execute;
$dbh->commit;
};
if ($@) {
$dbh->rollback or die $dbh->errstr;
}
}
$dbh->disconnect;
$dbh = _connect_db($db_server, $db_name, $db_user, $db_passwd);
$stmt = "INSERT INTO files ("
. "user_id,"
. "item_id,"
. "content_type,"
. "content,"
. "filename,"
. "date"
. ") VALUES ("
. "'$user_id',"
. "'$item_id',"
. "'$content_type',"
. "?,"
. "'$filename',"
. "GETDATE()"
. ")";
$sth = $dbh->prepare($stmt) or die $dbh->errstr;
$sth->bind_param( 1, $attachment, DBI::SQL_LONGVARBINARY );
eval {
$sth->execute;
$dbh->commit;
};
if ($@) {
$dbh->rollback or die $dbh->errstr;
}
$dbh->disconnect;
last;
}
}
__END__
0 件のコメント:
コメントを投稿