]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/db/ORAResult.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / db / ORAResult.php
1 <?php
2
3 use Wikimedia\Rdbms\IDatabase;
4
5 /**
6  * The oci8 extension is fairly weak and doesn't support oci_num_rows, among
7  * other things. We use a wrapper class to handle that and other
8  * Oracle-specific bits, like converting column names back to lowercase.
9  * @ingroup Database
10  */
11 class ORAResult {
12         private $rows;
13         private $cursor;
14         private $nrows;
15
16         private $columns = [];
17
18         private function array_unique_md( $array_in ) {
19                 $array_out = [];
20                 $array_hashes = [];
21
22                 foreach ( $array_in as $item ) {
23                         $hash = md5( serialize( $item ) );
24                         if ( !isset( $array_hashes[$hash] ) ) {
25                                 $array_hashes[$hash] = $hash;
26                                 $array_out[] = $item;
27                         }
28                 }
29
30                 return $array_out;
31         }
32
33         /**
34          * @param IDatabase &$db
35          * @param resource $stmt A valid OCI statement identifier
36          * @param bool $unique
37          */
38         function __construct( &$db, $stmt, $unique = false ) {
39                 $this->db =& $db;
40
41                 $this->nrows = oci_fetch_all( $stmt, $this->rows, 0, -1, OCI_FETCHSTATEMENT_BY_ROW | OCI_NUM );
42                 if ( $this->nrows === false ) {
43                         $e = oci_error( $stmt );
44                         $db->reportQueryError( $e['message'], $e['code'], '', __METHOD__ );
45                         $this->free();
46
47                         return;
48                 }
49
50                 if ( $unique ) {
51                         $this->rows = $this->array_unique_md( $this->rows );
52                         $this->nrows = count( $this->rows );
53                 }
54
55                 if ( $this->nrows > 0 ) {
56                         foreach ( $this->rows[0] as $k => $v ) {
57                                 $this->columns[$k] = strtolower( oci_field_name( $stmt, $k + 1 ) );
58                         }
59                 }
60
61                 $this->cursor = 0;
62                 oci_free_statement( $stmt );
63         }
64
65         public function free() {
66                 unset( $this->db );
67         }
68
69         public function seek( $row ) {
70                 $this->cursor = min( $row, $this->nrows );
71         }
72
73         public function numRows() {
74                 return $this->nrows;
75         }
76
77         public function numFields() {
78                 return count( $this->columns );
79         }
80
81         public function fetchObject() {
82                 if ( $this->cursor >= $this->nrows ) {
83                         return false;
84                 }
85                 $row = $this->rows[$this->cursor++];
86                 $ret = new stdClass();
87                 foreach ( $row as $k => $v ) {
88                         $lc = $this->columns[$k];
89                         $ret->$lc = $v;
90                 }
91
92                 return $ret;
93         }
94
95         public function fetchRow() {
96                 if ( $this->cursor >= $this->nrows ) {
97                         return false;
98                 }
99
100                 $row = $this->rows[$this->cursor++];
101                 $ret = [];
102                 foreach ( $row as $k => $v ) {
103                         $lc = $this->columns[$k];
104                         $ret[$lc] = $v;
105                         $ret[$k] = $v;
106                 }
107
108                 return $ret;
109         }
110 }