#!perl -w
my
$usage
=
"perl dbtest.pl [-b]. -b binds parameters explicitly.\n"
;
my
@data
= (
[
"2001-01-01 01:01:01.111"
,
"a"
x 12],
[
"2002-02-02 02:02:02.222"
,
"b"
x 114],
[
"2003-03-03 03:03:03.333"
,
"c"
x 251],
[
"2004-04-04 04:04:04.444"
,
"d"
x 282],
[
"2005-05-05 05:05:05.555"
,
"e"
x 131]
);
my
%args
;
getopts (
"b"
, \
%args
) or
die
$usage
;
my
$bind
=
$args
{
"b"
};
my
$dbh
=DBI->
connect
() or
die
"Can't connect"
;
$dbh
->{RaiseError} = 1;
$dbh
->{LongReadLen} = 800;
eval
{
$dbh
->
do
(
"DROP TABLE foo"
);
};
my
@types
= (SQL_TYPE_TIMESTAMP, SQL_TIMESTAMP);
my
$type
;
my
@row
;
foreach
$type
(
@types
) {
my
$sth
=
$dbh
->func(
$type
,
"GetTypeInfo"
);
if
(
$sth
) {
@row
=
$sth
->fetchrow();
$sth
->finish();
last
if
@row
;
}
else
{
}
}
die
"Unable to find a suitable test type for date field\n"
unless
@row
;
my
$dbname
=
$dbh
->get_info(17);
my
$datetype
=
$row
[0];
print
"Date type = $datetype\n"
;
$dbh
->
do
(
"CREATE TABLE foo (i INTEGER, time $datetype, str VARCHAR(4000))"
);
my
$sth1
=
$dbh
->prepare(
"INSERT INTO FOO (i,time,str) values (?,?,?)"
);
for
(
my
$i
=0;
$i
<
@data
;
$i
++) {
my
(
$time
,
$str
) = @{
$data
[
$i
]};
print
"Inserting: $i, $time, string length "
.
length
(
$str
).
"\n"
;
if
(
$bind
) {
$sth1
->bind_param (1,
$i
, SQL_INTEGER);
$sth1
->bind_param (2,
$time
, SQL_TIMESTAMP);
$sth1
->bind_param (3,
$str
, SQL_LONGVARCHAR);
$sth1
->execute or
die
(
$DBI::errstr
);
}
else
{
$sth1
->execute (
$i
,
$time
,
$str
) or
die
(
$DBI::errstr
);
}
}
print
"\n"
;
my
$sth2
=
$dbh
->prepare(
"SELECT i,time,str FROM foo"
);
$sth2
->execute or
die
(
$DBI::errstr
);
while
(
my
(
$i
,
$time
,
$str
) =
$sth2
->fetchrow_array()) {
print
"Retrieving: $i, $time, string length "
.
length
(
$str
).
"\t"
;
print
"!time "
if
(
$time
ne
$data
[
$i
][0]);
print
"!string"
if
(
$str
ne
$data
[
$i
][1]);
print
"\n"
;
}
$dbh
->disconnect;
$dbh
=
undef
;
$sth2
=
undef
;