stringify_uniqueidentifier.phpt 2.5 KB

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