1--TEST--
2MySQL PDO->exec(), affected rows
3--SKIPIF--
4<?php
5require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
6require_once(__DIR__ . 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(__DIR__ . 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
68    exec_and_count(2, $db, 'DROP TABLE IF EXISTS test', 0);
69    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);
70
71    $stmt = $db->query("SHOW VARIABLES LIKE 'secure_file_priv'");
72    if (($row = $stmt->fetch(PDO::FETCH_ASSOC)) && ($row['value'] != '')) {
73        $filename = $row['value'] . DIRECTORY_SEPARATOR  . "pdo_mysql_exec_load_data.csv";
74    } else {
75        $filename =  MySQLPDOTest::getTempDir() . DIRECTORY_SEPARATOR  . "pdo_mysql_exec_load_data.csv";
76    }
77
78    $fp = fopen($filename, "w");
79    fwrite($fp, "1;foo\n");
80    fwrite($fp, "2;bar");
81    fclose($fp);
82
83    $sql = sprintf("LOAD DATA LOCAL INFILE %s INTO TABLE test FIELDS TERMINATED BY ';' LINES TERMINATED  BY '\n'", $db->quote($filename));
84
85    if (exec_and_count(4, $db, $sql, 2)) {
86
87        $stmt = $db->query('SELECT id, col1 FROM test ORDER BY id ASC');
88        $expected = array(array("id" => 1, "col1" => "foo"), array("id" => 2, "col1" => "bar"));
89        $ret = $stmt->fetchAll(PDO::FETCH_ASSOC);
90        foreach ($expected as $offset => $exp) {
91            foreach ($exp as $key => $value) {
92                if ($ret[$offset][$key] != $value) {
93                    printf("Results seem wrong, check manually\n");
94                    var_dump($ret);
95                    var_dump($expected);
96                    break 2;
97                }
98            }
99        }
100    }
101
102    unlink($filename);
103
104    print "done!";
105?>
106--CLEAN--
107<?php
108require __DIR__ . '/mysql_pdo_test.inc';
109$db = MySQLPDOTest::factory();
110$db->exec('DROP TABLE IF EXISTS test');
111?>
112--EXPECT--
113done!
114