stringify_uniqueidentifier.phpt 2.5 KB

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