]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-17020 dml: native pgsql driver - support for bytea reading
authorskodak <skodak>
Mon, 27 Oct 2008 15:31:11 +0000 (15:31 +0000)
committerskodak <skodak>
Mon, 27 Oct 2008 15:31:11 +0000 (15:31 +0000)
lib/dml/pgsql_native_moodle_database.php
lib/dml/pgsql_native_moodle_recordset.php
lib/dml/simpletest/testdml.php

index 06762ce734480e9c0c8c112482b49a19c324d0fc..130231b2092a5ca17bc3ab9cb5088c640920898a 100644 (file)
@@ -9,8 +9,9 @@ require_once($CFG->libdir.'/dml/pgsql_native_moodle_recordset.php');
  */
 class pgsql_native_moodle_database extends moodle_database {
 
-    protected $pgsql = null;
-    protected $debug  = false;
+    protected $pgsql     = null;
+    protected $debug     = false;
+    protected $bytea_oid = null;
 
     /**
      * Detects if all needed PHP stuff installed.
@@ -94,6 +95,17 @@ class pgsql_native_moodle_database extends moodle_database {
             return false;
         }
         pg_set_client_encoding($this->pgsql, 'utf8');
+        // find out the bytea oid
+        $sql = "select oid from pg_type where typname = 'bytea'";
+        $result = pg_query($this->pgsql, $sql);
+        if ($result === false) {
+            return false;
+        }
+        $this->bytea_oid = pg_fetch_result($result, 0);
+        pg_free_result($result);
+        if ($this->bytea_oid === false) {
+            return false;
+        }
         return true;
     }
 
@@ -513,7 +525,7 @@ class pgsql_native_moodle_database extends moodle_database {
     }
 
     protected function create_recordset($result) {
-        return new pgsql_native_moodle_recordset($result);
+        return new pgsql_native_moodle_recordset($result, $this->bytea_oid);
     }
 
     /**
@@ -548,6 +560,15 @@ class pgsql_native_moodle_database extends moodle_database {
             $this->report_error($sql, $params);
             return false;
         }
+        // find out if there are any blobs
+        $numrows = pg_num_fields($result);
+        $blobs = array();
+        for($i=0; $i<$numrows; $i++) {
+            $type_oid = pg_field_type_oid($result, $i);
+            if ($type_oid == $this->bytea_oid) {
+                $blobs[] = pg_field_name($result, $i);
+            }
+        }
 
         $rows = pg_fetch_all($result);
         pg_free_result($result);
@@ -556,10 +577,15 @@ class pgsql_native_moodle_database extends moodle_database {
         if ($rows) {
             foreach ($rows as $row) {
                 $id = reset($row);
+                if ($blobs) {
+                    foreach ($blobs as $blob) {
+                        $row[$blob] = pg_unescape_bytea($row[$blob]);
+                    }
+                }
                 $return[$id] = (object)$row;
             }
         }
-         
+
         return $return;
     }
 
index 30617e4e23c303e8b12c40a0ded93e4a74ce1612..65d81a0e6439ca47c1f7c61ff26dc6a78a5393e3 100644 (file)
@@ -6,9 +6,22 @@ class pgsql_native_moodle_recordset extends moodle_recordset {
 
     protected $result;
     protected $current; // current row as array
+    protected $bytea_oid;
+    protected $blobs = array();
+
+    public function __construct($result, $bytea_oid) {
+        $this->result    = $result;
+        $this->bytea_oid = $bytea_oid;
+
+        // find out if there are any blobs
+        $numrows = pg_num_fields($result);
+        for($i=0; $i<$numrows; $i++) {
+            $type_oid = pg_field_type_oid($result, $i);
+            if ($type_oid == $this->bytea_oid) {
+                $this->blobs[] = pg_field_name($result, $i);
+            }
+        }
 
-    public function __construct($result) {
-        $this->result  = $result;
         $this->current = $this->fetch_next();
     }
 
@@ -18,6 +31,13 @@ class pgsql_native_moodle_recordset extends moodle_recordset {
 
     private function fetch_next() {
         $row = pg_fetch_assoc($this->result);
+
+        if ($this->blobs) {
+            foreach ($this->blobs as $blob) {
+                $row[$blob] = pg_unescape_bytea($row[$blob]);
+            }
+        }
+
         return $row;
     }
 
@@ -52,5 +72,6 @@ class pgsql_native_moodle_recordset extends moodle_recordset {
             $this->result  = null;
         }
         $this->current = null;
+        $this->blobs   = null;
     }
 }
index ab0c84665e54142ef5e3c8d67365a2d341fe7237..f2034e483b7d0a1b3125f9f5260bcd55d1976e5d 100755 (executable)
@@ -1081,7 +1081,7 @@ class dml_test extends UnitTestCase {
         $blob = file_get_contents($CFG->libdir.'/dml/simpletest/randombinary');
 
         $this->assertTrue($id = $DB->insert_record('testtable', array('description' => $clob, 'image' => $blob)));
-        $this->assertTrue($record = $DB->get_record('testtable', array('id' => $id)));
+        $record = $DB->get_record('testtable', array('id' => $id));
         $this->assertEqual($clob, $record->description);
         $this->assertEqual($blob, $record->image);
         $this->assertEqual($clob, $DB->get_field('testtable', 'description', array('id' => $id)));