1--TEST--
2PDO ODBC "long" columns
3--EXTENSIONS--
4pdo_odbc
5--SKIPIF--
6<?php
7require 'ext/pdo/tests/pdo_test.inc';
8PDOTest::skip();
9?>
10--FILE--
11<?php
12// setup: set PDOTEST_DSN environment variable
13//        for MyODBC (MySQL) and MS SQL Server, you need to also set PDOTEST_USER and PDOTEST_PASS
14//
15// can use MS SQL Server on Linux - using unixODBC
16//   -RHEL6.2
17//   -download & instructions: http://www.microsoft.com/en-us/download/details.aspx?id=28160
18//      -Linux6\sqlncli-11.0.1790.0.tar.gz (it calls RHEL6.x 'Linux6' for some reason)
19//   -follow instructions on web page and install script
20//   -may have to specify connection info in connection string without using a DSN (DSN-less connection)
21//      -for example:
22//            set PDOTEST_DSN='odbc:Driver=SQL Server Native Client 11.0;Server=10.200.51.179;Database=testdb'
23//            set PDOTEST_USER=sa
24//            set PDOTEST_PASS=Password01
25//
26// on Windows, the easy way to do this:
27// 1. install MS Access (part of MS Office) and include ODBC (Development tools feature)
28//       install the x86 build of the Drivers. You might not be able to load the x64 drivers.
29// 2. in Control Panel, search for ODBC and open "Setup data sources (ODBC)"
30// 3. click on System DSN tab
31// 4. click Add and choose "Microsoft Access Driver (*.mdb, *.accdb)" driver
32// 5. enter a DSN, ex: accdb12
33// 6. click 'Create' and select a file to save the database as
34//       -otherwise, you'll have to open MS Access, create a database, then load that file in this Window to map it to a DSN
35// 7. set the environment variable PDOTEST_DSN="odbc:<system dsn from step 5>" ex: SET PDOTEST_DSN=odbc:accdb12
36//         -note: on Windows, " is included in environment variable
37//
38// easy way to compile:
39// configure --disable-all --enable-cli --enable-zts --enable-pdo --with-pdo-odbc --enable-debug
40// configure --disable-all --enable-cli --enable-pdo --with-pdo-odbc=unixODBC,/usr,/usr --with-unixODBC=/usr --enable-debug
41//
42
43require 'ext/pdo/tests/pdo_test.inc';
44$db = PDOTest::test_factory('ext/pdo_odbc/tests/common.phpt');
45$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
46
47if (false === $db->exec('CREATE TABLE test_long_columns (id INT NOT NULL PRIMARY KEY, data CLOB)')) {
48    if (false === $db->exec('CREATE TABLE test_long_columns (id INT NOT NULL PRIMARY KEY, data longtext)')) {
49        if (false === $db->exec('CREATE TABLE test_long_columns (id INT NOT NULL PRIMARY KEY, data varchar(4000))')) {
50            die("BORK: don't know how to create a long column here:\n" . implode(", ", $db->errorInfo()));
51        }
52    }
53}
54
55$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
56
57// the driver reads columns in blocks of 255 bytes and then reassembles those blocks into a single buffer.
58// test sizes around 255 to make sure that the reassembly works (and that the column is split into 255 byte blocks by the database)
59// also, test sizes below 255 to make sure that they work - and are not treated as a long column (should be read in a single read)
60$sizes = array(32, 53, 64, 79, 128, 253, 254, 255, 256, 257, 258, 1022, 1023, 1024, 1025, 1026, 510, 511, 512, 513, 514, 1278, 1279, 1280, 1281, 1282, 2046, 2047, 2048, 2049, 2050, 1534, 1535, 1536, 1537, 1538, 3070, 3071, 3072, 3073, 3074, 3998, 3999, 4000);
61
62function alpha_repeat($len) {
63    // use the alphabet instead of 'i' characters to make sure the blocks don't overlap when they are reassembled
64    $out = "";
65    while (strlen($out) < $len) {
66        $out .= "abcdefghijklmnopqrstuvwxyz";
67    }
68    return substr($out, 0, $len);
69}
70
71// don't use Prepared Statements. that fails on MS SQL server (works with Access, MyODBC), which is a separate failure, feature/code-path from what
72// this test does - nice to be able to test using MS SQL server
73foreach ($sizes as $num) {
74    $text = alpha_repeat($num);
75    $db->exec("INSERT INTO test_long_columns VALUES($num, '$text')");
76}
77
78// verify data
79foreach ($db->query('SELECT id, data from test_long_columns ORDER BY LEN(data) ASC') as $row) {
80    $expect = alpha_repeat($row[0]);
81    if (strcmp($expect, $row[1])) {
82        echo "Failed on size $row[id]:\n";
83        printf("Expected %d bytes, got %d\n", strlen($expect), strlen($row['data']));
84        echo ($expect) . "\n";
85        echo ($row['data']) . "\n";
86    } else {
87        echo "Passed on size $row[id]\n";
88    }
89}
90
91echo "Finished\n";
92?>
93--CLEAN--
94<?php
95require 'ext/pdo/tests/pdo_test.inc';
96$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
97$db->exec("DROP TABLE IF EXISTS test_long_columns");
98?>
99--EXPECT--
100Passed on size 32
101Passed on size 53
102Passed on size 64
103Passed on size 79
104Passed on size 128
105Passed on size 253
106Passed on size 254
107Passed on size 255
108Passed on size 256
109Passed on size 257
110Passed on size 258
111Passed on size 510
112Passed on size 511
113Passed on size 512
114Passed on size 513
115Passed on size 514
116Passed on size 1022
117Passed on size 1023
118Passed on size 1024
119Passed on size 1025
120Passed on size 1026
121Passed on size 1278
122Passed on size 1279
123Passed on size 1280
124Passed on size 1281
125Passed on size 1282
126Passed on size 1534
127Passed on size 1535
128Passed on size 1536
129Passed on size 1537
130Passed on size 1538
131Passed on size 2046
132Passed on size 2047
133Passed on size 2048
134Passed on size 2049
135Passed on size 2050
136Passed on size 3070
137Passed on size 3071
138Passed on size 3072
139Passed on size 3073
140Passed on size 3074
141Passed on size 3998
142Passed on size 3999
143Passed on size 4000
144Finished
145