*/
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.
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;
}
}
protected function create_recordset($result) {
- return new pgsql_native_moodle_recordset($result);
+ return new pgsql_native_moodle_recordset($result, $this->bytea_oid);
}
/**
$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);
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;
}
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();
}
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;
}
$this->result = null;
}
$this->current = null;
+ $this->blobs = null;
}
}
$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)));