|
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
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.