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