large_objects.phpt 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. --TEST--
  2. PDO PgSQL Large Objects
  3. --EXTENSIONS--
  4. pdo
  5. pdo_pgsql
  6. --SKIPIF--
  7. <?php
  8. require __DIR__ . '/config.inc';
  9. require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
  10. PDOTest::skip();
  11. ?>
  12. --FILE--
  13. <?php
  14. require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
  15. $db = PDOTest::test_factory(__DIR__ . '/common.phpt');
  16. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  17. $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
  18. $db->exec('CREATE TABLE test (blobid integer not null primary key, bloboid OID)');
  19. $db->beginTransaction();
  20. $oid = $db->pgsqlLOBCreate();
  21. try {
  22. $stm = $db->pgsqlLOBOpen($oid, 'w+b');
  23. fwrite($stm, "Hello dude\n");
  24. $stmt = $db->prepare("INSERT INTO test (blobid, bloboid) values (?, ?)");
  25. $stmt->bindValue(1, 1);
  26. /* bind as LOB; the oid from the pgsql stream will be inserted instead
  27. * of the stream contents. Binding other streams will attempt to bind
  28. * as bytea, and will most likely lead to an error.
  29. * You can also just bind the $oid in as a string. */
  30. $stmt->bindParam(2, $stm, PDO::PARAM_LOB);
  31. $stmt->execute();
  32. $stm = null;
  33. /* Pull it out */
  34. $stmt = $db->prepare("SELECT * from test");
  35. $stmt->bindColumn('bloboid', $lob, PDO::PARAM_LOB);
  36. $stmt->execute();
  37. echo "Fetching:\n";
  38. while (($row = $stmt->fetch(PDO::FETCH_ASSOC))) {
  39. var_dump($row['blobid']);
  40. var_dump(stream_get_contents($lob));
  41. }
  42. echo "Fetched!\n";
  43. /* Try again, with late bind */
  44. $stmt = $db->prepare("SELECT * from test");
  45. $stmt->execute();
  46. $stmt->bindColumn('bloboid', $lob, PDO::PARAM_LOB);
  47. echo "Fetching late bind:\n";
  48. while (($row = $stmt->fetch(PDO::FETCH_ASSOC))) {
  49. var_dump($row['blobid']);
  50. var_dump(is_int($row['bloboid']));
  51. }
  52. echo "Fetched!\n";
  53. /* Try again, with NO bind */
  54. $stmt = $db->prepare("SELECT * from test");
  55. $stmt->execute();
  56. $stmt->bindColumn('bloboid', $lob, PDO::PARAM_LOB);
  57. echo "Fetching NO bind:\n";
  58. while (($row = $stmt->fetch(PDO::FETCH_ASSOC))) {
  59. var_dump($row['blobid']);
  60. var_dump(is_int($row['bloboid']));
  61. }
  62. echo "Fetched!\n";
  63. } catch (Exception $e) {
  64. /* catch exceptions so that we can guarantee to clean
  65. * up the LOB */
  66. echo "Exception! at line ", $e->getLine(), "\n";
  67. var_dump($e->getMessage());
  68. }
  69. /* Now to remove the large object from the database, so it doesn't
  70. * linger and clutter up the storage */
  71. $db->pgsqlLOBUnlink($oid);
  72. ?>
  73. --EXPECT--
  74. Fetching:
  75. int(1)
  76. string(11) "Hello dude
  77. "
  78. Fetched!
  79. Fetching late bind:
  80. int(1)
  81. bool(true)
  82. Fetched!
  83. Fetching NO bind:
  84. int(1)
  85. bool(true)
  86. Fetched!