Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/Elgg/Application/Database.php (1 issue)

1
<?php
2
namespace Elgg\Application;
3
4
use Elgg\Database as ElggDb;
5
6
/**
7
 * Elgg 3.0 public database API
8
 *
9
 * This is returned by elgg()->getDb() or Application::start()->getDb().
10
 *
11
 * @see \Elgg\Application::getDb for more details.
12
 *
13
 * @property-read string $prefix Elgg table prefix (read only)
14
 */
15
class Database {
16
17
	/**
18
	 * The "real" database instance
19
	 *
20
	 * @var ElggDb
21
	 */
22
	private $db;
23
24
	/**
25
	 * Constructor
26
	 *
27
	 * @param ElggDb $db The Elgg database
28
	 * @access private
29
	 */
30 14
	public function __construct(ElggDb $db) {
31 14
		$this->db = $db;
32 14
	}
33
34
	/**
35
	 * Retrieve rows from the database.
36
	 *
37
	 * Queries are executed with {@link \Elgg\Database::executeQuery()} and results
38
	 * are retrieved with {@link \PDO::fetchObject()}.  If a callback
39
	 * function $callback is defined, each row will be passed as a single
40
	 * argument to $callback.  If no callback function is defined, the
41
	 * entire result set is returned as an array.
42
	 *
43
	 * @param string   $query    The query being passed.
44
	 * @param callable $callback Optionally, the name of a function to call back to on each row
45
	 * @param array    $params   Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
46
	 *
47
	 * @return array An array of database result objects or callback function results. If the query
48
	 *               returned nothing, an empty array.
49
	 * @throws \DatabaseException
50
	 */
51
	public function getData($query, $callback = '', array $params = []) {
52
		return $this->db->getData($query, $callback, $params);
53
	}
54
55
	/**
56
	 * Retrieve a single row from the database.
57
	 *
58
	 * Similar to {@link \Elgg\Database::getData()} but returns only the first row
59
	 * matched.  If a callback function $callback is specified, the row will be passed
60
	 * as the only argument to $callback.
61
	 *
62
	 * @param string   $query    The query to execute.
63
	 * @param callable $callback A callback function to apply to the row
64
	 * @param array    $params   Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
65
	 *
66
	 * @return mixed A single database result object or the result of the callback function.
67
	 * @throws \DatabaseException
68
	 */
69
	public function getDataRow($query, $callback = '', array $params = []) {
70
		return $this->db->getDataRow($query, $callback, $params);
71
	}
72
73
	/**
74
	 * Insert a row into the database.
75
	 *
76
	 * @note Altering the DB invalidates all queries in the query cache.
77
	 *
78
	 * @param string $query  The query to execute.
79
	 * @param array  $params Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
80
	 *
81
	 * @return int|false The database id of the inserted row if a AUTO_INCREMENT field is
82
	 *                   defined, 0 if not, and false on failure.
83
	 * @throws \DatabaseException
84
	 */
85
	public function insertData($query, array $params = []) {
86
		return $this->db->insertData($query, $params);
87
	}
88
89
	/**
90
	 * Update the database.
91
	 *
92
	 * @note Altering the DB invalidates all queries in the query cache.
93
	 *
94
	 * @note WARNING! update_data() has the 2nd and 3rd arguments reversed.
95
	 *
96
	 * @param string $query      The query to run.
97
	 * @param bool   $getNumRows Return the number of rows affected (default: false).
98
	 * @param array  $params     Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
99
	 *
100
	 * @return bool|int
101
	 * @throws \DatabaseException
102
	 */
103
	public function updateData($query, $getNumRows = false, array $params = []) {
104
		return $this->db->updateData($query, $getNumRows, $params);
105
	}
106
107
	/**
108
	 * Delete data from the database
109
	 *
110
	 * @note Altering the DB invalidates all queries in query cache.
111
	 *
112
	 * @param string $query  The SQL query to run
113
	 * @param array  $params Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
114
	 *
115
	 * @return int The number of affected rows
116
	 * @throws \DatabaseException
117
	 */
118
	public function deleteData($query, array $params = []) {
119
		return $this->db->deleteData($query, $params);
120
	}
121
122
	/**
123
	 * Sanitizes an integer value for use in a query
124
	 *
125
	 * @param int  $value  Value to sanitize
126
	 * @param bool $signed Whether negative values are allowed (default: true)
127
	 * @return int
128
	 * @deprecated Use query parameters where possible
129
	 */
130
	public function sanitizeInt($value, $signed = true) {
131
		return $this->db->sanitizeInt($value, $signed);
132
	}
133
134
	/**
135
	 * Sanitizes a string for use in a query
136
	 *
137
	 * @param string $value Value to escape
138
	 * @return string
139
	 * @throws \DatabaseException
140
	 * @deprecated Use query parameters where possible
141
	 */
142
	public function sanitizeString($value) {
143
		return $this->db->sanitizeString($value);
144
	}
145
146
	/**
147
	 * Gets (if required, also creates) a DB connection.
148
	 *
149
	 * @param string $type The type of link we want: "read", "write" or "readwrite".
150
	 *
151
	 * @return Connection
152
	 * @throws \DatabaseException
153
	 * @access private
154
	 */
155 13
	public function getConnection($type) {
156 13
		return $this->db->getConnection($type);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->db->getConnection($type) returns the type Doctrine\DBAL\Connection which is incompatible with the documented return type Elgg\Application\Connection.
Loading history...
157
	}
158
159
	/**
160
	 * Handle magic property reads
161
	 *
162
	 * @param string $name Property name
163
	 * @return mixed
164
	 */
165
	public function __get($name) {
166
		return $this->db->{$name};
167
	}
168
169
	/**
170
	 * Handle magic property writes
171
	 *
172
	 * @param string $name  Property name
173
	 * @param mixed  $value Value
174
	 * @return void
175
	 */
176
	public function __set($name, $value) {
177
		$this->db->{$name} = $value;
178
	}
179
}
180