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?>
20--FILE--
21<?php
22	function exec_and_count($offset, &$db, $sql, $exp) {
23
24		try {
25
26			$ret = $db->exec($sql);
27			if ($ret !== $exp) {
28				printf("[%03d] Expecting '%s'/%s got '%s'/%s when running '%s', [%s] %s\n",
29					$offset, $exp, gettype($exp), $ret, gettype($ret), $sql,
30					$db->errorCode(), implode(' ', $db->errorInfo()));
31				return false;
32			}
33
34		} catch (PDOException $e) {
35
36			if (42000 == $db->errorCode()) {
37				// Error: 1148 SQLSTATE: 42000  (ER_NOT_ALLOWED_COMMAND)
38				// Load data infile not allowed
39				return false;
40			}
41
42			printf("[%03d] '%s' has failed, [%s] %s\n",
43				$offset, $sql, $db->errorCode(), implode(' ', $db->errorInfo()));
44			return false;
45		}
46
47		return true;
48	}
49
50	require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
51	$db = MySQLPDOTest::factory();
52	MySQLPDOTest::createTestTable($db, MySQLPDOTest::detect_transactional_mysql_engine($db));
53
54	/* affected rows related */
55	try {
56
57		exec_and_count(2, $db, 'DROP TABLE IF EXISTS test', 0);
58		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);
59
60		$stmt = $db->query("SHOW VARIABLES LIKE 'secure_file_priv'");
61		if (($row = $stmt->fetch(PDO::FETCH_ASSOC)) && ($row['value'] != '')) {
62			$filename = $row['value'] . DIRECTORY_SEPARATOR  . "pdo_mysql_exec_load_data.csv";
63		} else {
64			$filename =  MySQLPDOTest::getTempDir() . DIRECTORY_SEPARATOR  . "pdo_mysql_exec_load_data.csv";
65		}
66
67		$fp = fopen($filename, "w");
68		fwrite($fp, b"1;foo\n");
69		fwrite($fp, b"2;bar");
70		fclose($fp);
71
72		$sql = sprintf("LOAD DATA INFILE %s INTO TABLE test FIELDS TERMINATED BY ';' LINES TERMINATED  BY '\n'", $db->quote($filename));
73
74		if (exec_and_count(4, $db, $sql, 2)) {
75
76			$stmt = $db->query('SELECT id, col1 FROM test ORDER BY id ASC');
77			$expected = array(array("id" => 1, "col1" => "foo"), array("id" => 2, "col1" => "bar"));
78			$ret = $stmt->fetchAll(PDO::FETCH_ASSOC);
79			foreach ($expected as $offset => $exp) {
80				foreach ($exp as $key => $value) {
81					if ($ret[$offset][$key] != $value) {
82						printf("Results seem wrong, check manually\n");
83						var_dump($ret);
84						var_dump($expected);
85						break 2;
86					}
87				}
88			}
89		}
90
91		unlink($filename);
92
93	} catch (PDOException $e) {
94		printf("[001] %s, [%s] %s\n",
95			$e->getMessage(),
96			$db->errorCode(), implode(' ', $db->errorInfo()));
97	}
98
99	print "done!";
100?>
101--CLEAN--
102<?php
103require dirname(__FILE__) . '/mysql_pdo_test.inc';
104$db = MySQLPDOTest::factory();
105$db->exec('DROP TABLE IF EXISTS test');
106?>
107--EXPECTF--
108done!