Blame t/rt_78833_utf8_flag_for_column_names.t

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;
Packit 723767

Packit 723767
BEGIN {
Packit 723767
	if ( $] >= 5.008005 ) {
Packit 723767
		my $tests = 27;
Packit 723767
		$tests += 2 if has_sqlite('3.6.14');
Packit 723767
		plan( tests => $tests * 2 + 1 );
Packit 723767
	} else {
Packit 723767
		plan( skip_all => 'Unicode is not supported before 5.8.5' );
Packit 723767
	}
Packit 723767
}
Packit 723767
use Test::NoWarnings;
Packit 723767
use Encode;
Packit 723767

Packit 723767
unicode_test("\x{263A}");  # (decoded) smiley character
Packit 723767
unicode_test("\x{0100}");  # (decoded) capital A with macron
Packit 723767

Packit 723767
sub unicode_test {
Packit 723767
    my $unicode = shift;
Packit 723767

Packit 723767
    ok Encode::is_utf8($unicode), "correctly decoded";
Packit 723767

Packit 723767
    my $unicode_encoded = encode_utf8($unicode);
Packit 723767

Packit 723767
    { # tests for an environment where everything is encoded
Packit 723767

Packit 723767
        my $dbh = connect_ok(sqlite_unicode => 0);
Packit 723767
        $dbh->do("pragma foreign_keys = on");
Packit 723767
        my $unicode_quoted = $dbh->quote_identifier($unicode_encoded);
Packit 723767
        $dbh->do("create table $unicode_quoted (id, $unicode_quoted primary key)");
Packit 723767
        $dbh->do("create table bar (id, ref references $unicode_quoted ($unicode_encoded))");
Packit 723767

Packit 723767
        ok $dbh->do("insert into $unicode_quoted values (?, ?)", undef, 1, "text"), "insert successfully";
Packit 723767
        ok $dbh->do("insert into $unicode_quoted (id, $unicode_quoted) values (?, ?)", undef, 2, "text2"), "insert with unicode name successfully";
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->prepare("insert into $unicode_quoted (id) values (:$unicode_encoded)");
Packit 723767
            $sth->bind_param(":$unicode_encoded", 5);
Packit 723767
            $sth->execute;
Packit 723767
            my ($id) = $dbh->selectrow_array("select id from $unicode_quoted where id = :$unicode_encoded", undef, 5);
Packit 723767
            is $id => 5, "unicode placeholders";
Packit 723767
        }
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->prepare("select * from $unicode_quoted where id = ?");
Packit 723767
            $sth->execute(1);
Packit 723767
            my $row = $sth->fetchrow_hashref;
Packit 723767
            is $row->{id} => 1, "got correct row";
Packit 723767
            is $row->{$unicode_encoded} => "text", "got correct (encoded) unicode column data";
Packit 723767
            ok !exists $row->{$unicode}, "(decoded) unicode column does not exist";
Packit 723767
        }
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->prepare("select $unicode_quoted from $unicode_quoted where id = ?");
Packit 723767
            $sth->execute(1);
Packit 723767
            my $row = $sth->fetchrow_hashref;
Packit 723767
            is $row->{$unicode_encoded} => "text", "got correct (encoded) unicode column data";
Packit 723767
            ok !exists $row->{$unicode}, "(decoded) unicode column does not exist";
Packit 723767
        }
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->prepare("select id from $unicode_quoted where $unicode_quoted = ?");
Packit 723767
            $sth->execute("text");
Packit 723767
            my ($id) = $sth->fetchrow_array;
Packit 723767
            is $id => 1, "got correct id by the (encoded) unicode column value";
Packit 723767
        }
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->column_info(undef, undef, $unicode_encoded, $unicode_encoded);
Packit 723767
            my $column_info = $sth->fetchrow_hashref;
Packit 723767
            is $column_info->{COLUMN_NAME} => $unicode_encoded, "column_info returns the correctly encoded column name";
Packit 723767
        }
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->primary_key_info(undef, undef, $unicode_encoded);
Packit 723767
            my $primary_key_info = $sth->fetchrow_hashref;
Packit 723767
            is $primary_key_info->{COLUMN_NAME} => $unicode_encoded, "primary_key_info returns the correctly encoded primary key name";
Packit 723767
        }
Packit 723767

Packit 723767
        if (has_sqlite('3.6.14')) {
Packit 723767
            my $sth = $dbh->foreign_key_info(undef, undef, $unicode_encoded, undef, undef, 'bar');
Packit 723767
            my $foreign_key_info = $sth->fetchrow_hashref;
Packit 723767
            is $foreign_key_info->{PKCOLUMN_NAME} => $unicode_encoded, "foreign_key_info returns the correctly encoded foreign key name";
Packit 723767
        }
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->table_info(undef, undef, $unicode_encoded);
Packit 723767
            my $table_info = $sth->fetchrow_hashref;
Packit 723767
            is $table_info->{TABLE_NAME} => $unicode_encoded, "table_info returns the correctly encoded table name";
Packit 723767
        }
Packit 723767
    }
Packit 723767

Packit 723767
    { # tests for an environment where everything is decoded
Packit 723767

Packit 723767
        my $dbh = connect_ok(sqlite_unicode => 1);
Packit 723767
        $dbh->do("pragma foreign_keys = on");
Packit 723767
        my $unicode_quoted = $dbh->quote_identifier($unicode);
Packit 723767
        $dbh->do("create table $unicode_quoted (id, $unicode_quoted primary key)");
Packit 723767
        $dbh->do("create table bar (id, ref references $unicode_quoted ($unicode_quoted))");
Packit 723767

Packit 723767
        ok $dbh->do("insert into $unicode_quoted values (?, ?)", undef, 1, "text"), "insert successfully";
Packit 723767
        ok $dbh->do("insert into $unicode_quoted (id, $unicode_quoted) values (?, ?)", undef, 2, "text2"), "insert with unicode name successfully";
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->prepare("insert into $unicode_quoted (id) values (:$unicode)");
Packit 723767
            $sth->bind_param(":$unicode", 5);
Packit 723767
            $sth->execute;
Packit 723767
            my ($id) = $dbh->selectrow_array("select id from $unicode_quoted where id = :$unicode", undef, 5);
Packit 723767
            is $id => 5, "unicode placeholders";
Packit 723767
        }
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->prepare("select * from $unicode_quoted where id = ?");
Packit 723767
            $sth->execute(1);
Packit 723767
            my $row = $sth->fetchrow_hashref;
Packit 723767
            is $row->{id} => 1, "got correct row";
Packit 723767
            is $row->{$unicode} => "text", "got correct (decoded) unicode column data";
Packit 723767
            ok !exists $row->{$unicode_encoded}, "(encoded) unicode column does not exist";
Packit 723767
        }
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->prepare("select $unicode_quoted from $unicode_quoted where id = ?");
Packit 723767
            $sth->execute(1);
Packit 723767
            my $row = $sth->fetchrow_hashref;
Packit 723767
            is $row->{$unicode} => "text", "got correct (decoded) unicode column data";
Packit 723767
            ok !exists $row->{$unicode_encoded}, "(encoded) unicode column does not exist";
Packit 723767
        }
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->prepare("select id from $unicode_quoted where $unicode_quoted = ?");
Packit 723767
            $sth->execute("text2");
Packit 723767
            my ($id) = $sth->fetchrow_array;
Packit 723767
            is $id => 2, "got correct id by the (decoded) unicode column value";
Packit 723767
        }
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->column_info(undef, undef, $unicode, $unicode);
Packit 723767
            my $column_info = $sth->fetchrow_hashref;
Packit 723767
            is $column_info->{COLUMN_NAME} => $unicode, "column_info returns the correctly decoded column name";
Packit 723767
        }
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->primary_key_info(undef, undef, $unicode);
Packit 723767
            my $primary_key_info = $sth->fetchrow_hashref;
Packit 723767
            is $primary_key_info->{COLUMN_NAME} => $unicode, "primary_key_info returns the correctly decoded primary key name";
Packit 723767
        }
Packit 723767

Packit 723767
        if (has_sqlite('3.6.14')) {
Packit 723767
            my $sth = $dbh->foreign_key_info(undef, undef, $unicode, undef, undef, 'bar');
Packit 723767
            my $foreign_key_info = $sth->fetchrow_hashref;
Packit 723767
            is $foreign_key_info->{PKCOLUMN_NAME} => $unicode, "foreign_key_info returns the correctly decoded foreign key name";
Packit 723767
        }
Packit 723767

Packit 723767
        {
Packit 723767
            my $sth = $dbh->table_info(undef, undef, $unicode);
Packit 723767
            my $table_info = $sth->fetchrow_hashref;
Packit 723767
            is $table_info->{TABLE_NAME} => $unicode, "table_info returns the correctly decoded table name";
Packit 723767
        }
Packit 723767
    }
Packit 723767
}