|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
4
|
|
|
* @author Bart Visscher <[email protected]> |
|
5
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
|
6
|
|
|
* @author Morris Jobke <[email protected]> |
|
7
|
|
|
* @author Robin Appelman <[email protected]> |
|
8
|
|
|
* @author Robin McCorkell <[email protected]> |
|
9
|
|
|
* @author Thomas Müller <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright Copyright (c) 2017, ownCloud GmbH |
|
12
|
|
|
* @license AGPL-3.0 |
|
13
|
|
|
* |
|
14
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
15
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
16
|
|
|
* as published by the Free Software Foundation. |
|
17
|
|
|
* |
|
18
|
|
|
* This program is distributed in the hope that it will be useful, |
|
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21
|
|
|
* GNU Affero General Public License for more details. |
|
22
|
|
|
* |
|
23
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* small wrapper around \Doctrine\DBAL\Driver\Statement to make it behave, more like an MDB2 Statement |
|
30
|
|
|
* |
|
31
|
|
|
* @method boolean bindValue(mixed $param, mixed $value, integer $type = null); |
|
32
|
|
|
* @method string errorCode(); |
|
33
|
|
|
* @method array errorInfo(); |
|
34
|
|
|
* @method integer rowCount(); |
|
35
|
|
|
* @method array fetchAll(integer $fetchMode = null); |
|
36
|
|
|
*/ |
|
37
|
|
|
class OC_DB_StatementWrapper { |
|
38
|
|
|
/** |
|
39
|
|
|
* @var \Doctrine\DBAL\Driver\Statement |
|
40
|
|
|
*/ |
|
41
|
|
|
private $statement = null; |
|
42
|
|
|
private $isManipulation = false; |
|
43
|
|
|
private $lastArguments = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param boolean $isManipulation |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($statement, $isManipulation) { |
|
49
|
|
|
$this->statement = $statement; |
|
50
|
|
|
$this->isManipulation = $isManipulation; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* pass all other function directly to the \Doctrine\DBAL\Driver\Statement |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __call($name,$arguments) { |
|
57
|
|
|
return call_user_func_array([$this->statement,$name], $arguments); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* make execute return the result instead of a bool |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $input |
|
64
|
|
|
* @return \OC_DB_StatementWrapper|int |
|
65
|
|
|
*/ |
|
66
|
|
|
public function execute($input= []) { |
|
67
|
|
|
$this->lastArguments = $input; |
|
68
|
|
|
if (count($input) > 0) { |
|
69
|
|
|
$result = $this->statement->execute($input); |
|
70
|
|
|
} else { |
|
71
|
|
|
$result = $this->statement->execute(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($result === false) { |
|
75
|
|
|
return false; |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
if ($this->isManipulation) { |
|
78
|
|
|
$count = $this->statement->rowCount(); |
|
79
|
|
|
return $count; |
|
80
|
|
|
} else { |
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* provide an alias for fetch |
|
87
|
|
|
* |
|
88
|
|
|
* @return mixed |
|
89
|
|
|
*/ |
|
90
|
|
|
public function fetchRow() { |
|
91
|
|
|
return $this->statement->fetch(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Provide a simple fetchOne. |
|
96
|
|
|
* |
|
97
|
|
|
* fetch single column from the next row |
|
98
|
|
|
* @param int $column the column number to fetch |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function fetchOne($column = 0) { |
|
102
|
|
|
return $this->statement->fetchColumn($column); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Binds a PHP variable to a corresponding named or question mark placeholder in the |
|
107
|
|
|
* SQL statement that was use to prepare the statement. |
|
108
|
|
|
* |
|
109
|
|
|
* @param mixed $column Either the placeholder name or the 1-indexed placeholder index |
|
110
|
|
|
* @param mixed $variable The variable to bind |
|
111
|
|
|
* @param integer|null $type one of the PDO::PARAM_* constants |
|
112
|
|
|
* @param integer|null $length max length when using an OUT bind |
|
113
|
|
|
* @return boolean |
|
114
|
|
|
*/ |
|
115
|
|
|
public function bindParam($column, &$variable, $type = null, $length = null){ |
|
116
|
|
|
return $this->statement->bindParam($column, $variable, $type, $length); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
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.