1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\MSSQL; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\Connect\DatabaseException; |
6
|
|
|
use SilverStripe\ORM\Connect\Query; |
7
|
|
|
use function sqlsrv_fetch_array; |
8
|
|
|
use function sqlsrv_num_rows; |
9
|
|
|
use function sqlsrv_free_stmt; |
10
|
|
|
use const SQLSRV_SCROLL_ABSOLUTE; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A result-set from a MSSQL database. |
14
|
|
|
*/ |
15
|
|
|
class SQLServerQuery extends Query |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The SQLServerConnector object that created this result set. |
20
|
|
|
* |
21
|
|
|
* @var SQLServerConnector |
22
|
|
|
*/ |
23
|
|
|
protected $connector; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The internal MSSQL handle that points to the result set. |
27
|
|
|
* |
28
|
|
|
* @var resource |
29
|
|
|
*/ |
30
|
|
|
protected $handle; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Hook the result-set given into a Query class, suitable for use by sapphire. |
34
|
|
|
* @param SQLServerConnector $connector The database object that created this query. |
35
|
|
|
* @param resource $handle the internal mssql handle that is points to the resultset. |
36
|
|
|
*/ |
37
|
|
|
public function __construct(SQLServerConnector $connector, $handle) |
38
|
|
|
{ |
39
|
|
|
$this->connector = $connector; |
40
|
|
|
$this->handle = $handle; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function __destruct() |
44
|
|
|
{ |
45
|
|
|
if (is_resource($this->handle)) { |
46
|
|
|
sqlsrv_free_stmt($this->handle); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function numRecords() |
51
|
|
|
{ |
52
|
|
|
if (!is_resource($this->handle)) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// WARNING: This will only work if the cursor type is scrollable! |
57
|
|
|
if (function_exists('sqlsrv_num_rows')) { |
58
|
|
|
return sqlsrv_num_rows($this->handle); |
59
|
|
|
} else { |
60
|
|
|
user_error('MSSQLQuery::numRecords() not supported in this version of sqlsrv', E_USER_WARNING); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function nextRecord() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if (is_resource($this->handle)) { |
67
|
|
|
$result = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_ASSOC); |
68
|
|
|
|
69
|
|
|
if ($result && !empty($result)) { |
70
|
|
|
return $result; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return false; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function seek($row) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
if (is_resource($this->handle)) { |
80
|
|
|
$result = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_ABSOLUTE, $row); |
81
|
|
|
|
82
|
|
|
if ($result && !empty($result)) { |
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return false; |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.