|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\MSSQL; |
|
4
|
|
|
|
|
5
|
|
|
use DateTime; |
|
6
|
|
|
use SilverStripe\ORM\Connect\Query; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A result-set from a MSSQL database. |
|
10
|
|
|
*/ |
|
11
|
|
|
class SQLServerQuery extends Query |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The SQLServerConnector object that created this result set. |
|
16
|
|
|
* |
|
17
|
|
|
* @var SQLServerConnector |
|
18
|
|
|
*/ |
|
19
|
|
|
private $connector; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The internal MSSQL handle that points to the result set. |
|
23
|
|
|
* |
|
24
|
|
|
* @var resource |
|
25
|
|
|
*/ |
|
26
|
|
|
private $handle; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Hook the result-set given into a Query class, suitable for use by sapphire. |
|
30
|
|
|
* @param SQLServerConnector $connector The database object that created this query. |
|
31
|
|
|
* @param resource $handle the internal mssql handle that is points to the resultset. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(SQLServerConnector $connector, $handle) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->connector = $connector; |
|
36
|
|
|
$this->handle = $handle; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function __destruct() |
|
40
|
|
|
{ |
|
41
|
|
|
if (is_resource($this->handle)) { |
|
42
|
|
|
sqlsrv_free_stmt($this->handle); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getIterator() |
|
47
|
|
|
{ |
|
48
|
|
|
if (is_resource($this->handle)) { |
|
49
|
|
|
while ($data = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_ASSOC)) { |
|
50
|
|
|
// special case for sqlsrv - date values are DateTime coming out of the sqlsrv drivers, |
|
51
|
|
|
// so we convert to the usual Y-m-d H:i:s value! |
|
52
|
|
|
foreach ($data as $name => $value) { |
|
53
|
|
|
if ($value instanceof DateTime) { |
|
54
|
|
|
$data[$name] = $value->format('Y-m-d H:i:s'); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
yield $data; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function numRecords() |
|
64
|
|
|
{ |
|
65
|
|
|
if (!is_resource($this->handle)) { |
|
66
|
|
|
return false; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// WARNING: This will only work if the cursor type is scrollable! |
|
70
|
|
|
if (function_exists('sqlsrv_num_rows')) { |
|
71
|
|
|
return sqlsrv_num_rows($this->handle); |
|
72
|
|
|
} else { |
|
73
|
|
|
user_error('MSSQLQuery::numRecords() not supported in this version of sqlsrv', E_USER_WARNING); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|