1--TEST--
2MySQL PDO->exec(), affected rows
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
8MySQLPDOTest::skip();
9
10// Run test only locally - not against remote hosts
11$db = MySQLPDOTest::factory();
12$stmt = $db->query('SELECT USER() as _user');
13$row = $stmt->fetch(PDO::FETCH_ASSOC);
14$tmp = explode('@', $row['_user']);
15if (count($tmp) < 2)
16    die("skip Cannot detect if test is run against local or remote database server");
17if (($tmp[1] !== 'localhost') && ($tmp[1] !== '127.0.0.1'))
18    die("skip Test cannot be run against remote database server");
19
20$stmt = $db->query("SHOW VARIABLES LIKE 'secure_file_priv'");
21if (($row = $stmt->fetch(PDO::FETCH_ASSOC)) && ($row['value'] != '')) {
22    if (!is_writable($row['value']))
23        die("skip secure_file_priv directory not writable: {$row['value']}");
24
25    $filename = $row['value'] . DIRECTORY_SEPARATOR  . "pdo_mysql_exec_load_data.csv";
26
27    if (file_exists($filename) && !is_writable($filename))
28        die("skip {$filename} not writable");
29}
30
31?>
32--FILE--
33<?php
34    function exec_and_count($offset, &$db, $sql, $exp) {
35
36        try {
37
38            $ret = $db->exec($sql);
39            if ($ret !== $exp) {
40                printf("[%03d] Expecting '%s'/%s got '%s'/%s when running '%s', [%s] %s\n",
41                    $offset, $exp, gettype($exp), $ret, gettype($ret), $sql,
42                    $db->errorCode(), implode(' ', $db->errorInfo()));
43                return false;
44            }
45
46        } catch (PDOException $e) {
47
48            if (42000 == $db->errorCode()) {
49                // Error: 1148 SQLSTATE: 42000  (ER_NOT_ALLOWED_COMMAND)
50                // Load data infile not allowed
51                return false;
52            }
53
54            printf("[%03d] '%s' has failed, [%s] %s\n",
55                $offset, $sql, $db->errorCode(), implode(' ', $db->errorInfo()));
56            return false;
57        }
58
59        return true;
60    }
61
62    require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
63    putenv('PDOTEST_ATTR='.serialize([PDO::MYSQL_ATTR_LOCAL_INFILE=>true]));
64    $db = MySQLPDOTest::factory();
65    MySQLPDOTest::createTestTable($db, MySQLPDOTest::detect_transactional_mysql_engine($db));
66
67    /* affected rows related */
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, "1;foo\n");
81    fwrite($fp, "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    print "done!";
106?>
107--CLEAN--
108<?php
109require __DIR__ . '/mysql_pdo_test.inc';
110$db = MySQLPDOTest::factory();
111$db->exec('DROP TABLE IF EXISTS test');
112?>
113--EXPECT--
114done!
115