1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Abstract base class for XOOPS Database access classes |
4
|
|
|
* |
5
|
|
|
* You may not change or alter any portion of this comment or credits |
6
|
|
|
* of supporting developers from this source code or any supporting source code |
7
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
8
|
|
|
* This program is distributed in the hope that it will be useful, |
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11
|
|
|
* |
12
|
|
|
* @copyright (c) 2000-2025 XOOPS Project (https://xoops.org) |
13
|
|
|
* @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html) |
14
|
|
|
* @package kernel |
15
|
|
|
* @subpackage database |
16
|
|
|
* @since 1.0.0 |
17
|
|
|
* @author Kazumi Ono <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
defined('XOOPS_ROOT_PATH') || exit('Restricted access'); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* make sure this is only included once! |
24
|
|
|
*/ |
25
|
|
|
if (defined('XOOPS_C_DATABASE_INCLUDED')) { |
26
|
|
|
return null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
define('XOOPS_C_DATABASE_INCLUDED', 1); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Abstract base class for Database access classes |
33
|
|
|
* |
34
|
|
|
* @abstract |
35
|
|
|
* @author Kazumi Ono <[email protected]> |
36
|
|
|
* @package kernel |
37
|
|
|
* @subpackage database |
38
|
|
|
*/ |
39
|
|
|
abstract class XoopsDatabase |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Prefix for tables in the database |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public $prefix = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* reference to a {@link XoopsLogger} object |
50
|
|
|
* |
51
|
|
|
* @see XoopsLogger |
52
|
|
|
* @var object XoopsLogger |
53
|
|
|
*/ |
54
|
|
|
public $logger; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* If statements that modify the database are selected |
58
|
|
|
* |
59
|
|
|
* @var boolean |
60
|
|
|
*/ |
61
|
|
|
public $allowWebChanges = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* XoopsDatabase constructor. |
65
|
|
|
*/ |
66
|
|
|
public function __construct() |
67
|
|
|
{ |
68
|
|
|
// exit('Cannot instantiate this class directly'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* assign a {@link XoopsLogger} object to the database |
73
|
|
|
* |
74
|
|
|
* @see XoopsLogger |
75
|
|
|
* @param XoopsLogger $logger reference to a {@link XoopsLogger} object |
76
|
|
|
*/ |
77
|
|
|
|
78
|
|
|
public function setLogger(XoopsLogger $logger) |
79
|
|
|
{ |
80
|
|
|
$this->logger = &$logger; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* set the prefix for tables in the database |
85
|
|
|
* |
86
|
|
|
* @param string $value table prefix |
87
|
|
|
*/ |
88
|
|
|
public function setPrefix($value) |
89
|
|
|
{ |
90
|
|
|
$this->prefix = $value; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* attach the prefix.'_' to a given tablename |
95
|
|
|
* |
96
|
|
|
* if tablename is empty, only prefix will be returned |
97
|
|
|
* |
98
|
|
|
* @param string $tablename tablename |
99
|
|
|
* @return string prefixed tablename, just prefix if tablename is empty |
100
|
|
|
*/ |
101
|
|
|
public function prefix($tablename = '') |
102
|
|
|
{ |
103
|
|
|
if ($tablename != '') { |
104
|
|
|
return $this->prefix . '_' . $tablename; |
105
|
|
|
} else { |
106
|
|
|
return $this->prefix; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Test the passed result to determine if it is a valid result set |
112
|
|
|
* |
113
|
|
|
* @param mixed $result value to test |
114
|
|
|
* |
115
|
|
|
* @return bool true if $result is a database result set, otherwise false |
116
|
|
|
*/ |
117
|
|
|
abstract public function isResultSet($result); |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return a human-readable description of the last DB error. |
121
|
|
|
* Subclasses must override to provide engine-specific details. |
122
|
|
|
* |
123
|
|
|
* @return string Error message or empty string if no error. |
124
|
|
|
*/ |
125
|
|
|
abstract public function error(); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return an engine-specific error code for the last DB error. |
129
|
|
|
* Subclasses must override to provide engine-specific details. |
130
|
|
|
* |
131
|
|
|
* @return int Error code (e.g., MySQL errno) or 0 if no error. |
132
|
|
|
*/ |
133
|
|
|
abstract public function errno(); |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Executes a SELECT-like statement and returns a result handle. |
137
|
|
|
* If $limit is null, no LIMIT/OFFSET is applied. |
138
|
|
|
* If $limit is not null and $start is null, $start defaults to 0. |
139
|
|
|
* |
140
|
|
|
* @param string $sql |
141
|
|
|
* @param int|null $limit |
142
|
|
|
* @param int|null $start |
143
|
|
|
* @return mixed driver result/resource|false |
144
|
|
|
*/ |
145
|
|
|
abstract public function query(string $sql, ?int $limit = null, ?int $start = null); |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Executes a mutating statement (INSERT/UPDATE/DELETE/DDL). |
149
|
|
|
* New code should prefer exec(). |
150
|
|
|
*/ |
151
|
|
|
abstract public function exec(string $sql); |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Legacy alias for writes; kept for BC. |
155
|
|
|
*/ |
156
|
|
|
public function queryF(string $sql) |
157
|
|
|
{ |
158
|
|
|
// Deprecated: delegate to exec(). |
159
|
|
|
// Optional: behind a dev flag, emit a deprecation warning. |
160
|
|
|
// if (is_object($GLOBALS['xoopsLogger'])) { |
161
|
|
|
// $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . " is deprecated since XOOPS 2.5.12, please use 'exec()' instead."); |
162
|
|
|
// } |
163
|
|
|
|
164
|
|
|
return $this->exec($sql); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Canonical quoting API — returns a single-quoted SQL literal. |
170
|
|
|
* MUST include surrounding single quotes and perform driver-appropriate escaping. |
171
|
|
|
* |
172
|
|
|
* @param string $str Raw, unescaped string |
173
|
|
|
* @return string Quoted SQL literal, e.g. 'O''Reilly' for input "O'Reilly" |
174
|
|
|
*/ |
175
|
|
|
|
176
|
|
|
abstract public function quote(string $str); |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
Legacy alias. Returns a single-quoted SQL literal. |
180
|
|
|
* @deprecated Use quote() |
181
|
|
|
*/ |
182
|
|
|
public function quoteString(string $str) |
183
|
|
|
{ |
184
|
|
|
if (is_object($GLOBALS['xoopsLogger'])) { |
185
|
|
|
$GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ ." is deprecated since XOOPS 2.5.12, use quote() instead."); |
186
|
|
|
} |
187
|
|
|
return $this->quote($str); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Helper: normalize pagination semantics for drivers. |
192
|
|
|
* - limit=null => [null, null] (no pagination) |
193
|
|
|
* - limit>=0 => [limit, max(0, start??0)] |
194
|
|
|
*/ |
195
|
|
|
final protected function normalizeLimitStart(?int $limit, ?int $start): array |
196
|
|
|
{ |
197
|
|
|
if ($limit === null) { |
198
|
|
|
return [null, null]; |
199
|
|
|
} |
200
|
|
|
$limit = max(0, $limit); |
201
|
|
|
$start = max(0, $start ?? 0); |
202
|
|
|
return [$limit, $start]; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Only for backward compatibility |
208
|
|
|
* |
209
|
|
|
* @deprecated |
210
|
|
|
*/ |
211
|
|
|
class Database |
212
|
|
|
{ |
213
|
|
|
/** |
214
|
|
|
* @return object |
215
|
|
|
*/ |
216
|
|
|
public function getInstance() |
217
|
|
|
{ |
218
|
|
|
if (is_object($GLOBALS['xoopsLogger'])) { |
219
|
|
|
$GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . " is deprecated since XOOPS 2.5.4, please use 'XoopsDatabaseFactory::getDatabaseConnection();' instead."); |
220
|
|
|
} |
221
|
|
|
$inst = XoopsDatabaseFactory::getDatabaseConnection(); |
222
|
|
|
|
223
|
|
|
return $inst; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|