Blame t/rt_96878_fts_contentless_table.t

Packit 723767
#!/usr/bin/perl
Packit 723767
Packit 723767
# In a contentless FTS table, the columns are hidden from the schema,
Packit 723767
# and therefore SQLite has no information to infer column types, so
Packit 723767
# these are typed as SQLITE_NULL ... and this type conflicts with the
Packit 723767
# constraint on the 'docid' column. So we have to explicitly type that
Packit 723767
# column, using a CAST expression or a call to bind_param().
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;
Packit 723767
Packit 723767
BEGIN { requires_sqlite('3.7.9') }
Packit 723767
BEGIN { plan skip_all => 'FTS3 is disabled for this DBD::SQLite' if !grep /ENABLE_FTS3/, DBD::SQLite::compile_options() }
Packit 723767
Packit 723767
use DBI qw/SQL_INTEGER/;
Packit 723767
plan tests => 8;
Packit 723767
use Test::NoWarnings;
Packit 723767
Packit 723767
my $dbh = connect_ok(RaiseError => 1, AutoCommit => 1);
Packit 723767
Packit 723767
# $dbh->trace(15);
Packit 723767
Packit 723767
my $sql = q{CREATE VIRTUAL TABLE foo USING fts4 (content="", a, b)};
Packit 723767
ok( $dbh->do($sql), 'CREATE TABLE' );
Packit 723767
Packit 723767
Packit 723767
ok($dbh->do("INSERT INTO foo(docid, a, b) VALUES(1, 'a', 'b')"),
Packit 723767
   "insert without bind");
Packit 723767
Packit 723767
# The following yields a constraint error because docid is improperly typed
Packit 723767
# $dbh->do("INSERT INTO foo(docid, a, b) VALUES(?, ?, ?)", {}, qw/2 aa bb/);
Packit 723767
Packit 723767
# This works, thanks to the cast expression
Packit 723767
ok($dbh->do("INSERT INTO foo(docid, a, b) VALUES(CAST(? AS INTEGER), ?, ?)",
Packit 723767
            {}, qw/2 aa bb/),
Packit 723767
   "insert with bind and cast");
Packit 723767
Packit 723767
# This also works, thanks to the bind_param() call
Packit 723767
my $sth = $dbh->prepare("INSERT INTO foo(docid, a, b) VALUES(?, ?, ?)");
Packit 723767
$sth->bind_param(1, 3, SQL_INTEGER);
Packit 723767
$sth->bind_param(2, "aaa");
Packit 723767
$sth->bind_param(3, "bbb");
Packit 723767
ok($sth->execute(),
Packit 723767
   "insert with bind_param and explicit type ");
Packit 723767
Packit 723767
# Check that all terms were properly inserted
Packit 723767
ok( $dbh->do("CREATE VIRTUAL TABLE foo_aux USING fts4aux(foo)"), 'FTS4AUX');
Packit 723767
my $data = $dbh->selectcol_arrayref("select term from foo_aux where col='*'");
Packit 723767
is_deeply ([sort @$data], [qw/a aa aaa b bb bbb/], "terms properly indexed");
Packit 723767