1--TEST--
2PDO_DBLIB: Uniqueidentifier column data type stringifying
3--EXTENSIONS--
4pdo_dblib
5--SKIPIF--
6<?php
7require __DIR__ . '/config.inc';
8$db = getDbConnection();
9if (in_array($db->getAttribute(PDO::DBLIB_ATTR_TDS_VERSION), ['4.2', '4.6'])) die('skip feature unsupported by this TDS version');
10?>
11--FILE--
12<?php
13require __DIR__ . '/config.inc';
14
15$db = getDbConnection();
16
17$testGUID = '82A88958-672B-4C22-842F-216E2B88E72A';
18$testGUIDBinary = base64_decode('WImogitnIkyELyFuK4jnKg==');
19
20$sql = "SELECT CAST('$testGUID' as uniqueidentifier) as [guid]";
21
22//--------------------------------------------------------------------------------
23// 1. Get and Set the attribute
24//--------------------------------------------------------------------------------
25$db->setAttribute(PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, true);
26var_dump(true === $db->getAttribute(PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER));
27$db->setAttribute(PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, false);
28var_dump(false === $db->getAttribute(PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER));
29
30
31//--------------------------------------------------------------------------------
32// 2. Binary
33//--------------------------------------------------------------------------------
34$stmt = $db->query($sql);
35$row = $stmt->fetch(PDO::FETCH_ASSOC);
36
37var_dump($row['guid'] === $testGUIDBinary);
38
39
40//--------------------------------------------------------------------------------
41// 3. PDO::ATTR_STRINGIFY_FETCHES must not affect `uniqueidentifier` representation
42//--------------------------------------------------------------------------------
43$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
44$stmt = $db->query($sql);
45$row = $stmt->fetch(PDO::FETCH_ASSOC);
46$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
47
48var_dump($row['guid'] === $testGUIDBinary);
49
50
51//--------------------------------------------------------------------------------
52// 4. Stringifying
53// ! With TDS protocol version <7.0 binary will be returned and the test will fail !
54// TODO: something from PDO::ATTR_SERVER_VERSION, PDO::ATTR_CLIENT_VERSION or PDO::ATTR_SERVER_INFO should be used
55// to get TDS version and skip this test in this case.
56//--------------------------------------------------------------------------------
57$db->setAttribute(PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, true);
58$stmt = $db->query($sql);
59$row = $stmt->fetch(PDO::FETCH_ASSOC);
60
61var_dump($row['guid'] === $testGUID);
62var_dump($row['guid']);
63
64?>
65--EXPECT--
66bool(true)
67bool(true)
68bool(true)
69bool(true)
70bool(true)
71string(36) "82A88958-672B-4C22-842F-216E2B88E72A"
72