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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
use strict;
use warnings;
use Test::More;
use DBI;
use lib '.', 't';
require 'lib.pl';
$|= 1;
use vars qw($test_dsn $test_user $test_password);
my $dbh;
eval {$dbh= DBI->connect($test_dsn, $test_user, $test_password,
{ RaiseError => 1,
PrintError => 1,
AutoCommit => 1,
mysql_server_prepare => 0 });};
if ($@) {
plan skip_all => "no database connection";
}
plan tests => 78;
ok(defined $dbh, "connecting");
my $sth;
#
# Bug #26604: foreign_key_info() implementation
#
# The tests for this are adapted from the Connector/J test suite.
#
SKIP: {
skip "Server is too old to support INFORMATION_SCHEMA for foreign keys", 16
if !MinimumVersion($dbh, '5.0');
my $have_innodb = 0;
if (!MinimumVersion($dbh, '5.6')) {
my $dummy;
($dummy,$have_innodb)=
$dbh->selectrow_array("SHOW VARIABLES LIKE 'have_innodb'")
or DbiError($dbh->err, $dbh->errstr);
} else {
my $engines = $dbh->selectall_arrayref('SHOW ENGINES');
if (!$engines) {
DbiError($dbh->err, $dbh->errstr);
} else {
STORAGE_ENGINE:
for my $engine (@$engines) {
next STORAGE_ENGINE if lc $engine->[0] ne 'innodb';
next STORAGE_ENGINE if lc $engine->[1] eq 'no';
$have_innodb = 1;
}
}
}
skip "Server doesn't support InnoDB, needed for testing foreign keys", 16
if !$have_innodb;
ok($dbh->do(qq{DROP TABLE IF EXISTS child, parent}), "cleaning up");
ok($dbh->do(qq{CREATE TABLE parent(id INT NOT NULL,
PRIMARY KEY (id)) ENGINE=INNODB}));
ok($dbh->do(qq{CREATE TABLE child(id INT, parent_id INT,
FOREIGN KEY (parent_id)
REFERENCES parent(id) ON DELETE SET NULL)
ENGINE=INNODB}));
$sth= $dbh->foreign_key_info(undef, undef, 'parent', undef, undef, 'child');
my ($info)= $sth->fetchall_arrayref({});
is($info->[0]->{PKTABLE_NAME}, "parent");
is($info->[0]->{PKCOLUMN_NAME}, "id");
is($info->[0]->{FKTABLE_NAME}, "child");
is($info->[0]->{FKCOLUMN_NAME}, "parent_id");
$sth= $dbh->foreign_key_info(undef, undef, 'parent', undef, undef, undef);
($info)= $sth->fetchall_arrayref({});
is($info->[0]->{PKTABLE_NAME}, "parent");
is($info->[0]->{PKCOLUMN_NAME}, "id");
is($info->[0]->{FKTABLE_NAME}, "child");
is($info->[0]->{FKCOLUMN_NAME}, "parent_id");
$sth= $dbh->foreign_key_info(undef, undef, undef, undef, undef, 'child');
($info)= $sth->fetchall_arrayref({});
is($info->[0]->{PKTABLE_NAME}, "parent");
is($info->[0]->{PKCOLUMN_NAME}, "id");
is($info->[0]->{FKTABLE_NAME}, "child");
is($info->[0]->{FKCOLUMN_NAME}, "parent_id");
ok($dbh->do(qq{DROP TABLE IF EXISTS child, parent}), "cleaning up");
};
#
# table_info() tests
#
# These tests assume that no other tables name like 't_dbd_mysql_%' exist on
# the server we are using for testing.
#
SKIP: {
skip "Server can't handle tricky table names", 33
if !MinimumVersion($dbh, '4.1');
my $sth = $dbh->table_info("%", undef, undef, undef);
is(scalar @{$sth->fetchall_arrayref()}, 0, "No catalogs expected");
$sth = $dbh->table_info(undef, "%", undef, undef);
ok(scalar @{$sth->fetchall_arrayref()} > 0, "Some schemas expected");
$sth = $dbh->table_info(undef, undef, undef, "%");
ok(scalar @{$sth->fetchall_arrayref()} > 0, "Some table types expected");
ok($dbh->do(qq{DROP TABLE IF EXISTS t_dbd_mysql_t1, t_dbd_mysql_t11,
t_dbd_mysql_t2, t_dbd_mysqlat2,
`t_dbd_mysql_a'b`,
`t_dbd_mysql_a``b`}),
"cleaning up");
ok($dbh->do(qq{CREATE TABLE t_dbd_mysql_t1 (a INT)}) and
$dbh->do(qq{CREATE TABLE t_dbd_mysql_t11 (a INT)}) and
$dbh->do(qq{CREATE TABLE t_dbd_mysql_t2 (a INT)}) and
$dbh->do(qq{CREATE TABLE t_dbd_mysqlat2 (a INT)}) and
$dbh->do(qq{CREATE TABLE `t_dbd_mysql_a'b` (a INT)}) and
$dbh->do(qq{CREATE TABLE `t_dbd_mysql_a``b` (a INT)}),
"creating test tables");
# $base is our base table name, with the _ escaped to avoid extra matches
my $esc = $dbh->get_info(14); # SQL_SEARCH_PATTERN_ESCAPE
(my $base = "t_dbd_mysql_") =~ s/([_%])/$esc$1/g;
# Test fetching info on a single table
$sth = $dbh->table_info(undef, undef, $base . "t1", undef);
my $info = $sth->fetchall_arrayref({});
is($info->[0]->{TABLE_CAT}, undef);
is($info->[0]->{TABLE_NAME}, "t_dbd_mysql_t1");
is($info->[0]->{TABLE_TYPE}, "TABLE");
is(scalar @$info, 1, "one row expected");
# Test fetching info on a wildcard
$sth = $dbh->table_info(undef, undef, $base . "t1%", undef);
$info = $sth->fetchall_arrayref({});
is($info->[0]->{TABLE_CAT}, undef);
is($info->[0]->{TABLE_NAME}, "t_dbd_mysql_t1");
is($info->[0]->{TABLE_TYPE}, "TABLE");
is($info->[1]->{TABLE_CAT}, undef);
is($info->[1]->{TABLE_NAME}, "t_dbd_mysql_t11");
is($info->[1]->{TABLE_TYPE}, "TABLE");
is(scalar @$info, 2, "two rows expected");
# Test fetching info on a single table with escaped wildcards
$sth = $dbh->table_info(undef, undef, $base . "t2", undef);
$info = $sth->fetchall_arrayref({});
is($info->[0]->{TABLE_CAT}, undef);
is($info->[0]->{TABLE_NAME}, "t_dbd_mysql_t2");
is($info->[0]->{TABLE_TYPE}, "TABLE");
is(scalar @$info, 1, "only one table expected");
# Test fetching info on a single table with ` in name
$sth = $dbh->table_info(undef, undef, $base . "a`b", undef);
$info = $sth->fetchall_arrayref({});
is($info->[0]->{TABLE_CAT}, undef);
is($info->[0]->{TABLE_NAME}, "t_dbd_mysql_a`b");
is($info->[0]->{TABLE_TYPE}, "TABLE");
is(scalar @$info, 1, "only one table expected");
# Test fetching info on a single table with ' in name
$sth = $dbh->table_info(undef, undef, $base . "a'b", undef);
$info = $sth->fetchall_arrayref({});
is($info->[0]->{TABLE_CAT}, undef);
is($info->[0]->{TABLE_NAME}, "t_dbd_mysql_a'b");
is($info->[0]->{TABLE_TYPE}, "TABLE");
is(scalar @$info, 1, "only one table expected");
# Test fetching our tables with a wildcard schema
# NOTE: the performance of this could be bad if the mysql user we
# are connecting as can see lots of databases.
$sth = $dbh->table_info(undef, "%", $base . "%", undef);
$info = $sth->fetchall_arrayref({});
is(scalar @$info, 5, "five tables expected");
# Check that tables() finds and escapes the tables named with quotes
$info = [ $dbh->tables(undef, undef, $base . 'a%') ];
like($info->[0], qr/\.`t_dbd_mysql_a'b`$/, "table with single quote");
like($info->[1], qr/\.`t_dbd_mysql_a``b`$/, "table with back quote");
is(scalar @$info, 2, "two tables expected");
# Clean up
ok($dbh->do(qq{DROP TABLE IF EXISTS t_dbd_mysql_t1, t_dbd_mysql_t11,
t_dbd_mysql_t2, t_dbd_mysqlat2,
`t_dbd_mysql_a'b`,
`t_dbd_mysql_a``b`}),
"cleaning up");
};
#
# view-related table_info tests
#
SKIP: {
skip "Server is too old to support views", 19
if !MinimumVersion($dbh, '5.0');
#
# Bug #26603: (one part) support views in table_info()
#
ok($dbh->do(qq{DROP VIEW IF EXISTS bug26603_v1}) and
$dbh->do(qq{DROP TABLE IF EXISTS bug26603_t1}), "cleaning up");
ok($dbh->do(qq{CREATE TABLE bug26603_t1 (a INT)}) and
$dbh->do(qq{CREATE VIEW bug26603_v1 AS SELECT * FROM bug26603_t1}),
"creating resources");
# Try without any table type specified
$sth = $dbh->table_info(undef, undef, "bug26603%");
my $info = $sth->fetchall_arrayref({});
is($info->[0]->{TABLE_NAME}, "bug26603_t1");
is($info->[0]->{TABLE_TYPE}, "TABLE");
is($info->[1]->{TABLE_NAME}, "bug26603_v1");
is($info->[1]->{TABLE_TYPE}, "VIEW");
is(scalar @$info, 2, "two rows expected");
# Just get the view
$sth = $dbh->table_info(undef, undef, "bug26603%", "VIEW");
$info = $sth->fetchall_arrayref({});
is($info->[0]->{TABLE_NAME}, "bug26603_v1");
is($info->[0]->{TABLE_TYPE}, "VIEW");
is(scalar @$info, 1, "one row expected");
# Just get the table
$sth = $dbh->table_info(undef, undef, "bug26603%", "TABLE");
$info = $sth->fetchall_arrayref({});
is($info->[0]->{TABLE_NAME}, "bug26603_t1");
is($info->[0]->{TABLE_TYPE}, "TABLE");
is(scalar @$info, 1, "one row expected");
# Get both tables and views
$sth = $dbh->table_info(undef, undef, "bug26603%", "'TABLE','VIEW'");
$info = $sth->fetchall_arrayref({});
is($info->[0]->{TABLE_NAME}, "bug26603_t1");
is($info->[0]->{TABLE_TYPE}, "TABLE");
is($info->[1]->{TABLE_NAME}, "bug26603_v1");
is($info->[1]->{TABLE_TYPE}, "VIEW");
is(scalar @$info, 2, "two rows expected");
ok($dbh->do(qq{DROP VIEW IF EXISTS bug26603_v1}) and
$dbh->do(qq{DROP TABLE IF EXISTS bug26603_t1}), "cleaning up");
};
#
# column_info() tests
#
SKIP: {
ok($dbh->do(qq{DROP TABLE IF EXISTS t1}), "cleaning up");
ok($dbh->do(qq{CREATE TABLE t1 (a INT PRIMARY KEY AUTO_INCREMENT,
b INT,
`a_` INT,
`a'b` INT,
bar INT
)}), "creating table");
#
# Bug #26603: (one part) add mysql_is_autoincrement
#
$sth= $dbh->column_info(undef, undef, "t1", 'a');
my ($info)= $sth->fetchall_arrayref({});
is($info->[0]->{mysql_is_auto_increment}, 1);
$sth= $dbh->column_info(undef, undef, "t1", 'b');
($info)= $sth->fetchall_arrayref({});
is($info->[0]->{mysql_is_auto_increment}, 0);
#
# Test that wildcards and odd names are handled correctly
#
$sth= $dbh->column_info(undef, undef, "t1", "a%");
($info)= $sth->fetchall_arrayref({});
is(scalar @$info, 3);
$sth= $dbh->column_info(undef, undef, "t1", "a" . $dbh->get_info(14) . "_");
($info)= $sth->fetchall_arrayref({});
is(scalar @$info, 1);
$sth= $dbh->column_info(undef, undef, "t1", "a'b");
($info)= $sth->fetchall_arrayref({});
is(scalar @$info, 1);
#
# The result set is ordered by TABLE_CAT, TABLE_SCHEM, TABLE_NAME and ORDINAL_POSITION.
#
$sth= $dbh->column_info(undef, undef, "t1", undef);
($info)= $sth->fetchall_arrayref({});
is(join(' ++ ', map { $_->{COLUMN_NAME} } @{$info}), "a ++ b ++ a_ ++ a'b ++ bar");
ok($dbh->do(qq{DROP TABLE IF EXISTS t1}), "cleaning up");
$dbh->disconnect();
};
$dbh->disconnect();
|