large_objects.phpt 2.5 KB

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