Merge branch 'wip-mdl-30121-m22' of git://github.com/rajeshtaneja/moodle into MOODLE_...
[moodle.git] / lib / dml / pgsql_native_moodle_recordset.php
blob70d655e132f888002ac402965f8b6a2052524bcc
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 /**
20 * Native postgresql recordset.
22 * @package core
23 * @subpackage dml
24 * @copyright 2008 Petr Skoda (http://skodak.org)
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->libdir.'/dml/moodle_recordset.php');
32 /**
33 * pgsql specific moodle recordset class
35 class pgsql_native_moodle_recordset extends moodle_recordset {
37 protected $result;
38 protected $current; // current row as array
39 protected $bytea_oid;
40 protected $blobs = array();
42 public function __construct($result, $bytea_oid) {
43 $this->result = $result;
44 $this->bytea_oid = $bytea_oid;
46 // find out if there are any blobs
47 $numrows = pg_num_fields($result);
48 for($i=0; $i<$numrows; $i++) {
49 $type_oid = pg_field_type_oid($result, $i);
50 if ($type_oid == $this->bytea_oid) {
51 $this->blobs[] = pg_field_name($result, $i);
55 $this->current = $this->fetch_next();
58 public function __destruct() {
59 $this->close();
62 private function fetch_next() {
63 $row = pg_fetch_assoc($this->result);
65 if ($this->blobs) {
66 foreach ($this->blobs as $blob) {
67 $row[$blob] = $row[$blob] !== null ? pg_unescape_bytea($row[$blob]) : null;
71 return $row;
74 public function current() {
75 return (object)$this->current;
78 public function key() {
79 /// return first column value as key
80 if (!$this->current) {
81 return false;
83 $key = reset($this->current);
84 return $key;
87 public function next() {
88 $this->current = $this->fetch_next();
91 public function valid() {
92 return !empty($this->current);
95 public function close() {
96 if ($this->result) {
97 pg_free_result($this->result);
98 $this->result = null;
100 $this->current = null;
101 $this->blobs = null;