1use strict;
2use warnings;
3
4use DBI;
5use Test::More;
6use vars qw($test_dsn $test_user $test_password);
7use vars qw($COL_NULLABLE $COL_KEY);
8use lib 't', '.';
9require 'lib.pl';
10
11my $dbh;
12eval {$dbh= DBI->connect($test_dsn, $test_user, $test_password,
13                      { RaiseError => 1, PrintError => 1, AutoCommit => 0 });};
14if ($@) {
15    plan skip_all => "no database connection";
16}
17
18#
19# DROP/CREATE PROCEDURE will give syntax error for these versions
20#
21if ($dbh->{mysql_serverversion} < 50000) {
22    plan skip_all =>
23        "SKIP TEST: You must have MySQL version 5.0 and greater for this test to run";
24}
25plan tests => 16 * 2;
26
27for my $mysql_server_prepare (0, 1) {
28$dbh= DBI->connect("$test_dsn;mysql_server_prepare=$mysql_server_prepare;mysql_server_prepare_disable_fallback=1", $test_user, $test_password,
29                      { RaiseError => 1, PrintError => 1, AutoCommit => 0 });
30
31ok $dbh->do("DROP TABLE IF EXISTS dbd_mysql_t55utf8");
32
33my $create =<<EOT;
34CREATE TABLE dbd_mysql_t55utf8 (
35    name VARCHAR(64) CHARACTER SET utf8,
36    bincol BLOB,
37    shape GEOMETRY,
38    binutf VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_bin,
39    profile TEXT CHARACTER SET utf8
40)
41EOT
42
43ok $dbh->do($create);
44
45my $utf8_str        = "\x{0100}dam";     # "Adam" with a macron.
46my $quoted_utf8_str = "'\x{0100}dam'";
47
48my $blob = "\x{c4}\x{80}dam"; # same as utf8_str but not utf8 encoded
49my $quoted_blob = "'\x{c4}\x{80}dam'";
50
51cmp_ok $dbh->quote($utf8_str), 'eq', $quoted_utf8_str, 'testing quoting of utf 8 string';
52
53cmp_ok $dbh->quote($blob), 'eq', $quoted_blob, 'testing quoting of blob';
54
55#ok $dbh->{mysql_enable_utf8}, "mysql_enable_utf8 survive connect()";
56$dbh->{mysql_enable_utf8}=1;
57
58# GeomFromText() is deprecated as of MySQL 5.7.6, use ST_GeomFromText() instead
59my $geomfromtext = $dbh->{mysql_serverversion} >= 50706 ? 'ST_GeomFromText' : 'GeomFromText';
60my $query = <<EOI;
61INSERT INTO dbd_mysql_t55utf8 (name, bincol, shape, binutf, profile)
62    VALUES (?, ?, $geomfromtext('Point(132865 501937)'), ?, ?)
63EOI
64
65ok $dbh->do($query, {}, $utf8_str, $blob, $utf8_str, $utf8_str), "INSERT query $query\n";
66
67# AsBinary() is deprecated as of MySQL 5.7.6, use ST_AsBinary() instead
68my $asbinary = $dbh->{mysql_serverversion} >= 50706 ? 'ST_AsBinary' : 'AsBinary';
69
70$query = "SELECT name,bincol,$asbinary(shape), binutf, profile FROM dbd_mysql_t55utf8 LIMIT 1";
71
72my $sth = $dbh->prepare($query) or die "$DBI::errstr";
73ok $sth->execute;
74
75my $ref;
76$ref = $sth->fetchrow_arrayref ;
77
78ok defined $ref;
79
80cmp_ok $ref->[0], 'eq', $utf8_str;
81
82cmp_ok $ref->[3], 'eq', $utf8_str;
83cmp_ok $ref->[4], 'eq', $utf8_str;
84
85SKIP: {
86        eval {use Encode;};
87          skip "Can't test is_utf8 tests 'use Encode;' not available", 2, if $@;
88          ok !Encode::is_utf8($ref->[1]), "blob was made utf8!.";
89
90          ok !Encode::is_utf8($ref->[2]), "shape was made utf8!.";
91      }
92
93cmp_ok $ref->[1], 'eq', $blob, "compare $ref->[1] eq $blob";
94
95ok $sth->finish;
96
97ok $dbh->do("DROP TABLE dbd_mysql_t55utf8");
98
99ok $dbh->disconnect;
100}
101