|
Packit |
723767 |
#!/usr/bin/perl
|
|
Packit |
723767 |
|
|
Packit |
723767 |
use strict;
|
|
Packit |
723767 |
BEGIN {
|
|
Packit |
723767 |
$| = 1;
|
|
Packit |
723767 |
$^W = 1;
|
|
Packit |
723767 |
}
|
|
Packit |
723767 |
|
|
Packit |
723767 |
use lib "t/lib";
|
|
Packit |
723767 |
use SQLiteTest;
|
|
Packit |
723767 |
use Test::More tests => 37;
|
|
Packit |
723767 |
use Test::NoWarnings;
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Create a database
|
|
Packit |
723767 |
my $dbh = connect_ok( dbfile => 'foo', RaiseError => 1, PrintError => 1, PrintWarn => 1 );
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Create the table
|
|
Packit |
723767 |
ok( $dbh->do(<<'END_SQL'), 'CREATE TABLE' );
|
|
Packit |
723767 |
CREATE TABLE one (
|
|
Packit |
723767 |
id INTEGER NOT NULL,
|
|
Packit |
723767 |
name CHAR (64)
|
|
Packit |
723767 |
)
|
|
Packit |
723767 |
END_SQL
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Test quoting
|
|
Packit |
723767 |
my $quoted = $dbh->quote('test1');
|
|
Packit |
723767 |
is( $quoted, "'test1'", '->quote(test1) ok' );
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Disconnect
|
|
Packit |
723767 |
ok( $dbh->disconnect, '->disconnect' );
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Reconnect
|
|
Packit |
723767 |
$dbh = connect_ok( dbfile => 'foo' );
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Delete the table and recreate it
|
|
Packit |
723767 |
ok( $dbh->do('DROP TABLE one'), 'DROP' );
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Create the table again
|
|
Packit |
723767 |
ok( $dbh->do(<<'END_SQL'), 'CREATE TABLE' );
|
|
Packit |
723767 |
CREATE TABLE one (
|
|
Packit |
723767 |
id INTEGER NULL,
|
|
Packit |
723767 |
name CHAR (64) NULL
|
|
Packit |
723767 |
)
|
|
Packit |
723767 |
END_SQL
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Insert into table
|
|
Packit |
723767 |
ok( $dbh->do("INSERT INTO one VALUES ( 1, 'A' )"), 'INSERT 1' );
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Delete it
|
|
Packit |
723767 |
ok( $dbh->do('DELETE FROM one WHERE id = 1'), 'DELETE 1' );
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# When we "forget" execute, fail with error message
|
|
Packit |
723767 |
SCOPE: {
|
|
Packit |
723767 |
my $sth = $dbh->prepare('SELECT * FROM one WHERE id = 1');
|
|
Packit |
723767 |
isa_ok( $sth, 'DBI::st' );
|
|
Packit |
723767 |
my ($pe) = $sth->{PrintError};
|
|
Packit |
723767 |
$sth->{PrintError} = 0;
|
|
Packit |
723767 |
my $rv = eval {
|
|
Packit |
723767 |
$sth->fetchrow;
|
|
Packit |
723767 |
};
|
|
Packit |
723767 |
$sth->{PrintError} = $pe;
|
|
Packit |
723767 |
ok( $sth->execute, '->execute' );
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# This should fail without error message: No rows returned.
|
|
Packit |
723767 |
my(@row, $ref);
|
|
Packit |
723767 |
SCOPE: {
|
|
Packit |
723767 |
local $^W = 0;
|
|
Packit |
723767 |
is( $sth->fetch, undef, '->fetch returns undef' );
|
|
Packit |
723767 |
}
|
|
Packit |
723767 |
ok( $sth->finish, '->finish' );
|
|
Packit |
723767 |
}
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# This section should exercise the sth->func( '_NumRows' ) private
|
|
Packit |
723767 |
# method by preparing a statement, then finding the number of rows
|
|
Packit |
723767 |
# within it. Prior to execution, this should fail. After execution,
|
|
Packit |
723767 |
# the number of rows affected by the statement will be returned.
|
|
Packit |
723767 |
SCOPE: {
|
|
Packit |
723767 |
my $sth = $dbh->prepare('SELECT * FROM one WHERE id = 1');
|
|
Packit |
723767 |
isa_ok( $sth, 'DBI::st' );
|
|
Packit |
723767 |
is( $sth->rows, -1, '->rows is negative' );
|
|
Packit |
723767 |
ok( $sth->execute, '->execute ok' );
|
|
Packit |
723767 |
is( $sth->rows, 0, '->rows returns 0' );
|
|
Packit |
723767 |
ok( $sth->finish, '->finish' );
|
|
Packit |
723767 |
}
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Test whether or not a field containing a NULL is returned correctly
|
|
Packit |
723767 |
# as undef, or something much more bizarre
|
|
Packit |
723767 |
ok( $dbh->do("INSERT INTO one VALUES ( NULL, 'NULL-valued id' )"), 'INSERT 2' );
|
|
Packit |
723767 |
SCOPE: {
|
|
Packit |
723767 |
my $sth = $dbh->prepare("SELECT id FROM one WHERE id IS NULL");
|
|
Packit |
723767 |
isa_ok( $sth, 'DBI::st' );
|
|
Packit |
723767 |
ok( $sth->execute, '->execute' );
|
|
Packit |
723767 |
is_deeply(
|
|
Packit |
723767 |
$sth->fetchall_arrayref,
|
|
Packit |
723767 |
[ [ undef ] ],
|
|
Packit |
723767 |
'NULL returned ok',
|
|
Packit |
723767 |
);
|
|
Packit |
723767 |
ok( $sth->finish, '->finish' );
|
|
Packit |
723767 |
}
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Delete the test row from the table
|
|
Packit |
723767 |
ok( $dbh->do("DELETE FROM one WHERE id is NULL AND name = 'NULL-valued id'"), 'DELETE' );
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Test whether or not a char field containing a blank is returned
|
|
Packit |
723767 |
# correctly as blank, or something much more bizarre
|
|
Packit |
723767 |
ok( $dbh->do("INSERT INTO one VALUES ( 2, NULL )"), 'INSERT 3' );
|
|
Packit |
723767 |
SCOPE: {
|
|
Packit |
723767 |
my $sth = $dbh->prepare("SELECT name FROM one WHERE id = 2 AND name IS NULL");
|
|
Packit |
723767 |
isa_ok( $sth, 'DBI::st' );
|
|
Packit |
723767 |
ok( $sth->execute, '->execute' );
|
|
Packit |
723767 |
is_deeply(
|
|
Packit |
723767 |
$sth->fetchall_arrayref,
|
|
Packit |
723767 |
[ [ undef ] ],
|
|
Packit |
723767 |
'->fetchall_arrayref',
|
|
Packit |
723767 |
);
|
|
Packit |
723767 |
ok( $sth->finish, '->finish' );
|
|
Packit |
723767 |
}
|
|
Packit |
723767 |
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Delete the test row from the table
|
|
Packit |
723767 |
ok( $dbh->do('DELETE FROM ONE WHERE id = 2 AND name IS NULL'), 'DELETE' );
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Test the new funky routines to list the fields applicable to a SELECT
|
|
Packit |
723767 |
# statement, and not necessarily just those in a table...
|
|
Packit |
723767 |
SCOPE: {
|
|
Packit |
723767 |
my $sth = $dbh->prepare("SELECT * FROM one");
|
|
Packit |
723767 |
isa_ok( $sth, 'DBI::st' );
|
|
Packit |
723767 |
ok( $sth->execute, 'Execute' );
|
|
Packit |
723767 |
ok( $sth->execute, 'Reexecute' );
|
|
Packit |
723767 |
my @row = $sth->fetchrow_array;
|
|
Packit |
723767 |
ok( $sth->finish, '->finish' );
|
|
Packit |
723767 |
}
|
|
Packit |
723767 |
|
|
Packit |
723767 |
# Insert some more data into the test table.........
|
|
Packit |
723767 |
ok( $dbh->do("INSERT INTO one VALUES( 2, 'Gary Shea' )"), 'INSERT 4' );
|
|
Packit |
723767 |
SCOPE: {
|
|
Packit |
723767 |
my $sth = $dbh->prepare("UPDATE one SET id = 3 WHERE name = 'Gary Shea'");
|
|
Packit |
723767 |
isa_ok( $sth, 'DBI::st' );
|
|
Packit |
723767 |
}
|