#!/usr/bin/perl



#use strict;
use POSIX;
use DBI;


$wafer = 1;
$trapid = 123;
$laserid = 124;

$result =42;

$prog_version = 23; #'ver23';
$board_id = 666; #'wafer_tester_666';



my $file = shift or die "No filename given.";

open IN, "<$file";
binmode IN;

$len = read IN,$buf,1000000;

my $dbh = DBI->connect("DBI:Pg:dbname=play",
		       "alice", 'alice!@#',
		       {'RaiseError' => 1});
$dbh->begin_work;

$lobjId = $dbh->func($dbh->{pg_INV_WRITE}, 'lo_creat');

print "loid: $lobjId\n";

$lobj_fd = $dbh->func($lobjId, $dbh->{pg_INV_WRITE} , 'lo_open');
$nbytes = $dbh->func($lobj_fd, $buf, $len, 'lo_write');


$sth = $dbh->prepare("INSERT INTO file(name,mime_type,loid) VALUES (?,?,?);");
$sth->execute("filename(optional)", "text/xml", $lobjId );







$sth = $dbh->prepare("INSERT INTO wafer_test(wafer,trap_id,laser_id,result,timestamp,".
		     "                       tester_ver,tester_board_id,logfile) ".
		     "VALUES (?,?,?,?,now(),?,?,(SELECT file_id FROM file WHERE loid=?))");

$sth->execute($wafer,$trapid,$laserid,$result,$prog_version,$board_id,$lobjId);



$dbh->commit;


$dbh->disconnect;

