Merge "Add SPARQL client to core"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / resultwrapper / MssqlResultWrapper.php
1 <?php
2
3 namespace Wikimedia\Rdbms;
4
5 use stdClass;
6
7 class MssqlResultWrapper extends ResultWrapper {
8 /** @var int|null */
9 private $mSeekTo = null;
10
11 /**
12 * @return stdClass|bool
13 */
14 public function fetchObject() {
15 $res = $this->result;
16
17 if ( $this->mSeekTo !== null ) {
18 $result = sqlsrv_fetch_object( $res, stdClass::class, [],
19 SQLSRV_SCROLL_ABSOLUTE, $this->mSeekTo );
20 $this->mSeekTo = null;
21 } else {
22 $result = sqlsrv_fetch_object( $res );
23 }
24
25 // Return boolean false when there are no more rows instead of null
26 if ( $result === null ) {
27 return false;
28 }
29
30 return $result;
31 }
32
33 /**
34 * @return array|bool
35 */
36 public function fetchRow() {
37 $res = $this->result;
38
39 if ( $this->mSeekTo !== null ) {
40 $result = sqlsrv_fetch_array( $res, SQLSRV_FETCH_BOTH,
41 SQLSRV_SCROLL_ABSOLUTE, $this->mSeekTo );
42 $this->mSeekTo = null;
43 } else {
44 $result = sqlsrv_fetch_array( $res );
45 }
46
47 // Return boolean false when there are no more rows instead of null
48 if ( $result === null ) {
49 return false;
50 }
51
52 return $result;
53 }
54
55 /**
56 * @param int $row
57 * @return bool
58 */
59 public function seek( $row ) {
60 $res = $this->result;
61
62 // check bounds
63 $numRows = $this->db->numRows( $res );
64 $row = intval( $row );
65
66 if ( $numRows === 0 ) {
67 return false;
68 } elseif ( $row < 0 || $row > $numRows - 1 ) {
69 return false;
70 }
71
72 // Unlike MySQL, the seek actually happens on the next access
73 $this->mSeekTo = $row;
74 return true;
75 }
76 }