File: 80ora_charset.t

package info (click to toggle)
libdbd-oracle-perl 1.74-3
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 1,808 kB
  • ctags: 653
  • sloc: ansic: 8,165; perl: 6,942; makefile: 18
file content (133 lines) | stat: -rw-r--r-- 3,025 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!perl -w
use strict;

use Encode;
use Devel::Peek;

use DBI;
use DBD::Oracle qw(ORA_OCI);

use Test::More;

unshift @INC ,'t';
require 'nchar_test_lib.pl';

my $tdata = {
    cols => [
        [ 'ch', 'varchar2(20)', ],
        [ 'nch', 'nvarchar2(20)', ],
        [ 'descr', 'varchar2(50)', ],
    ],
    'dump' => 'DUMP(%s)',
    rows => [
        [
            "\xb0",
            "\xb0",
            'DEGREE SIGN',
        ],
    ],
};

my $table = table();

my $utf8_charset = (ORA_OCI >= 9.2) ? 'AL32UTF8' : 'UTF8';
my $eight_bit_charset = 'WE8ISO8859P1';

my $dbh_utf8;
my $dbh;
SKIP: {
    plan skip_all => "Oracle 9.2 or newer required" unless ORA_OCI >= 9.2;

    if ($ENV{ORA_CHARSET_FAIL}) {
        # Connecting up here breaks because of the charset and ncharset
        # global variables defined in dbdimp.c
        $dbh_utf8 = db_connect(1);
    }
    my $testcount = 8 + insert_test_count( $tdata );

    $dbh = db_connect(0);
    if ($dbh) {
        $dbh->ora_nls_parameters ()->{NLS_CHARACTERSET} =~ m/US7ASCII/ and plan skip_all => "Database is set up as US7ASCII";

        plan tests => $testcount;
    } else {
        plan skip_all => "Unable to connect to Oracle";
    }

    show_test_data( $tdata ,0 );

    drop_table($dbh);
    create_table($dbh, $tdata);
    insert_rows( $dbh, $tdata);

    my ($ch, $nch) = $dbh->selectrow_array("select ch, nch from $table");
    check($ch, $nch, 0);

    unless ($ENV{ORA_CHARSET_FAIL}) {
        $dbh_utf8 = db_connect(1);
    }
    ($ch, $nch) = $dbh_utf8->selectrow_array("select ch, nch from $table");
    check($ch, $nch, 1);
};

sub check {
    my $ch = shift;
    my $nch = shift;
    my $is_utf8 = shift;

    if ($is_utf8) {
        ok(Encode::is_utf8($ch));
        ok(Encode::is_utf8($nch));
    }
    else {
        ok(!Encode::is_utf8($ch));
        ok(!Encode::is_utf8($nch));
    }

    is($ch, "\xb0", "match char");
    is($nch, "\xb0", "match char");
}

sub db_connect
{
    my $utf8 = shift;

    # Make sure we really are overriding the environment settings.
    my ($charset, $ncharset);
    if ($utf8) {
        set_nls_lang_charset($eight_bit_charset);
        set_nls_nchar($eight_bit_charset);
        $charset = $utf8_charset;
        $ncharset = $utf8_charset;
    }
    else {
        set_nls_lang_charset($utf8_charset);
        set_nls_nchar($utf8_charset);
        $charset = $eight_bit_charset;
        $ncharset = $eight_bit_charset;
    }

    my $dsn = oracle_test_dsn();
    my $dbuser = $ENV{ORACLE_USERID} || 'scott/tiger';

    my $p = {
        AutoCommit => 1,
        PrintError => 0,
        FetchHashKeyName => 'NAME_lc',
        ora_envhp  => 0, # force fresh environment (with current NLS env vars)
    };
    $p->{ora_charset} = $charset if $charset;
    $p->{ora_ncharset} = $ncharset if $ncharset;

    my $dbh = DBI->connect($dsn, $dbuser, '', $p);
    return $dbh;
}

END {
    eval {
        local $dbh->{PrintError} = 0;
      drop_table( $dbh ) if $dbh and not $ENV{'DBD_SKIP_TABLE_DROP'};
    };
}

1;