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