MDL-17020 dml: native pgsql driver - support for bytea reading

This commit is contained in:
skodak 2008-10-27 15:31:11 +00:00
parent 30fff7b74f
commit db7aea3818
3 changed files with 54 additions and 7 deletions

View 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;
}

View 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;
}
}

View File

@ -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)));