1--TEST--
2MySQL PDO->exec(), affected rows
3--SKIPIF--
4<?php
5require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
6require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7MySQLPDOTest::skip();
8
9// Run test only locally - not against remote hosts
10$db = MySQLPDOTest::factory();
11$stmt = $db->query('SELECT USER() as _user');
12$row = $stmt->fetch(PDO::FETCH_ASSOC);
13$tmp = explode('@', $row['_user']);
14if (count($tmp) < 2)
15	die("skip Cannot detect if test is run against local or remote database server");
16if (($tmp[1] !== 'localhost') && ($tmp[1] !== '127.0.0.1'))
17	die("skip Test cannot be run against remote database server");
18
19$stmt = $db->query("SHOW VARIABLES LIKE 'secure_file_priv'");
20if (($row = $stmt->fetch(PDO::FETCH_ASSOC)) && ($row['value'] != '')) {
21	if (!is_writable($row['value']))
22		die("skip secure_file_priv directory not writable: {$row['value']}");
23
24	$filename = $row['value'] . DIRECTORY_SEPARATOR  . "pdo_mysql_exec_load_data.csv";
25
26	if (file_exists($filename) && !is_writable($filename))
27		die("skip {$filename} not writable");
28}
29
30?>
31--FILE--
32<?php
33	function exec_and_count($offset, &$db, $sql, $exp) {
34
35		try {
36
37			$ret = $db->exec($sql);
38			if ($ret !== $exp) {
39				printf("[%03d] Expecting '%s'/%s got '%s'/%s when running '%s', [%s] %s\n",
40					$offset, $exp, gettype($exp), $ret, gettype($ret), $sql,
41					$db->errorCode(), implode(' ', $db->errorInfo()));
42				return false;
43			}
44
45		} catch (PDOException $e) {
46
47			if (42000 == $db->errorCode()) {
48				// Error: 1148 SQLSTATE: 42000  (ER_NOT_ALLOWED_COMMAND)
49				// Load data infile not allowed
50				return false;
51			}
52
53			printf("[%03d] '%s' has failed, [%s] %s\n",
54				$offset, $sql, $db->errorCode(), implode(' ', $db->errorInfo()));
55			return false;
56		}
57
58		return true;
59	}
60
61	require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
62	putenv('PDOTEST_ATTR='.serialize([PDO::MYSQL_ATTR_LOCAL_INFILE=>true]));
63	$db = MySQLPDOTest::factory();
64	MySQLPDOTest::createTestTable($db, MySQLPDOTest::detect_transactional_mysql_engine($db));
65
66	/* affected rows related */
67	try {
68
69		exec_and_count(2, $db, 'DROP TABLE IF EXISTS test', 0);
70		exec_and_count(3, $db, sprintf('CREATE TABLE test(id INT NOT NULL PRIMARY KEY, col1 CHAR(10)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE), 0);
71
72		$stmt = $db->query("SHOW VARIABLES LIKE 'secure_file_priv'");
73		if (($row = $stmt->fetch(PDO::FETCH_ASSOC)) && ($row['value'] != '')) {
74			$filename = $row['value'] . DIRECTORY_SEPARATOR  . "pdo_mysql_exec_load_data.csv";
75		} else {
76			$filename =  MySQLPDOTest::getTempDir() . DIRECTORY_SEPARATOR  . "pdo_mysql_exec_load_data.csv";
77		}
78
79		$fp = fopen($filename, "w");
80		fwrite($fp, b"1;foo\n");
81		fwrite($fp, b"2;bar");
82		fclose($fp);
83
84		$sql = sprintf("LOAD DATA LOCAL INFILE %s INTO TABLE test FIELDS TERMINATED BY ';' LINES TERMINATED  BY '\n'", $db->quote($filename));
85
86		if (exec_and_count(4, $db, $sql, 2)) {
87
88			$stmt = $db->query('SELECT id, col1 FROM test ORDER BY id ASC');
89			$expected = array(array("id" => 1, "col1" => "foo"), array("id" => 2, "col1" => "bar"));
90			$ret = $stmt->fetchAll(PDO::FETCH_ASSOC);
91			foreach ($expected as $offset => $exp) {
92				foreach ($exp as $key => $value) {
93					if ($ret[$offset][$key] != $value) {
94						printf("Results seem wrong, check manually\n");
95						var_dump($ret);
96						var_dump($expected);
97						break 2;
98					}
99				}
100			}
101		}
102
103		unlink($filename);
104
105	} catch (PDOException $e) {
106		printf("[001] %s, [%s] %s\n",
107			$e->getMessage(),
108			$db->errorCode(), implode(' ', $db->errorInfo()));
109	}
110
111	print "done!";
112?>
113--CLEAN--
114<?php
115require dirname(__FILE__) . '/mysql_pdo_test.inc';
116$db = MySQLPDOTest::factory();
117$db->exec('DROP TABLE IF EXISTS test');
118?>
119--EXPECTF--
120done!
121