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
|
--TEST--
MySQL PDO->exec(), affected rows
--EXTENSIONS--
pdo_mysql
--SKIPIF--
<?php
require_once __DIR__ . '/inc/mysql_pdo_test.inc';
MySQLPDOTest::skip();
// Run test only locally - not against remote hosts
$db = MySQLPDOTest::factory();
$stmt = $db->query('SELECT USER() as _user');
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$tmp = explode('@', $row['_user']);
if (count($tmp) < 2)
die("skip Cannot detect if test is run against local or remote database server");
if (($tmp[1] !== 'localhost') && ($tmp[1] !== '127.0.0.1'))
die("skip Test cannot be run against remote database server");
$stmt = $db->query("SHOW VARIABLES LIKE 'secure_file_priv'");
if (($row = $stmt->fetch(PDO::FETCH_ASSOC)) && ($row['value'] != '')) {
if (!is_writable($row['value']))
die("skip secure_file_priv directory not writable: {$row['value']}");
$filename = $row['value'] . DIRECTORY_SEPARATOR . "pdo_mysql_exec_load_data.csv";
if (file_exists($filename) && !is_writable($filename))
die("skip {$filename} not writable");
}
?>
--FILE--
<?php
function exec_and_count($offset, &$db, $sql, $exp) {
try {
$ret = $db->exec($sql);
if ($ret !== $exp) {
printf("[%03d] Expecting '%s'/%s got '%s'/%s when running '%s', [%s] %s\n",
$offset, $exp, gettype($exp), $ret, gettype($ret), $sql,
$db->errorCode(), implode(' ', $db->errorInfo()));
return false;
}
} catch (PDOException $e) {
if (42000 == $db->errorCode()) {
// Error: 1148 SQLSTATE: 42000 (ER_NOT_ALLOWED_COMMAND)
// Load data infile not allowed
return false;
}
printf("[%03d] '%s' has failed, [%s] %s\n",
$offset, $sql, $db->errorCode(), implode(' ', $db->errorInfo()));
return false;
}
return true;
}
require_once __DIR__ . '/inc/mysql_pdo_test.inc';
$db = MySQLPDOTest::factoryWithAttr([PDO::MYSQL_ATTR_LOCAL_INFILE=>true]);
/* affected rows related */
exec_and_count(3, $db, sprintf('CREATE TABLE test_mysql_exec_load_data(id INT NOT NULL PRIMARY KEY, col1 CHAR(10)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE), 0);
$stmt = $db->query("SHOW VARIABLES LIKE 'secure_file_priv'");
if (($row = $stmt->fetch(PDO::FETCH_ASSOC)) && ($row['value'] != '')) {
$filename = $row['value'] . DIRECTORY_SEPARATOR . "pdo_mysql_exec_load_data.csv";
} else {
$filename = MySQLPDOTest::getTempDir() . DIRECTORY_SEPARATOR . "pdo_mysql_exec_load_data.csv";
}
$fp = fopen($filename, "w");
fwrite($fp, "1;foo\n");
fwrite($fp, "2;bar");
fclose($fp);
$sql = sprintf("LOAD DATA LOCAL INFILE %s INTO TABLE test_mysql_exec_load_data FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n'", $db->quote($filename));
if (exec_and_count(4, $db, $sql, 2)) {
$stmt = $db->query('SELECT id, col1 FROM test_mysql_exec_load_data ORDER BY id ASC');
$expected = array(array("id" => 1, "col1" => "foo"), array("id" => 2, "col1" => "bar"));
$ret = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($expected as $offset => $exp) {
foreach ($exp as $key => $value) {
if ($ret[$offset][$key] != $value) {
printf("Results seem wrong, check manually\n");
var_dump($ret);
var_dump($expected);
break 2;
}
}
}
}
unlink($filename);
print "done!";
?>
--CLEAN--
<?php
require __DIR__ . '/inc/mysql_pdo_test.inc';
$db = MySQLPDOTest::factory();
$db->query('DROP TABLE IF EXISTS test_mysql_exec_load_data');
?>
--EXPECT--
done!
|