|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00018 00028 defined('MOODLE_INTERNAL') || die(); 00029 00030 require_once($CFG->libdir.'/dml/moodle_recordset.php'); 00031 00035 class pgsql_native_moodle_recordset extends moodle_recordset { 00036 00037 protected $result; 00038 protected $current; // current row as array 00039 protected $bytea_oid; 00040 protected $blobs = array(); 00041 00042 public function __construct($result, $bytea_oid) { 00043 $this->result = $result; 00044 $this->bytea_oid = $bytea_oid; 00045 00046 // find out if there are any blobs 00047 $numrows = pg_num_fields($result); 00048 for($i=0; $i<$numrows; $i++) { 00049 $type_oid = pg_field_type_oid($result, $i); 00050 if ($type_oid == $this->bytea_oid) { 00051 $this->blobs[] = pg_field_name($result, $i); 00052 } 00053 } 00054 00055 $this->current = $this->fetch_next(); 00056 } 00057 00058 public function __destruct() { 00059 $this->close(); 00060 } 00061 00062 private function fetch_next() { 00063 $row = pg_fetch_assoc($this->result); 00064 00065 if ($this->blobs) { 00066 foreach ($this->blobs as $blob) { 00067 $row[$blob] = $row[$blob] !== null ? pg_unescape_bytea($row[$blob]) : null; 00068 } 00069 } 00070 00071 return $row; 00072 } 00073 00074 public function current() { 00075 return (object)$this->current; 00076 } 00077 00078 public function key() { 00080 if (!$this->current) { 00081 return false; 00082 } 00083 $key = reset($this->current); 00084 return $key; 00085 } 00086 00087 public function next() { 00088 $this->current = $this->fetch_next(); 00089 } 00090 00091 public function valid() { 00092 return !empty($this->current); 00093 } 00094 00095 public function close() { 00096 if ($this->result) { 00097 pg_free_result($this->result); 00098 $this->result = null; 00099 } 00100 $this->current = null; 00101 $this->blobs = null; 00102 } 00103 }