1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin; |
6
|
|
|
|
7
|
|
|
use function define; |
8
|
|
|
use function defined; |
9
|
|
|
|
10
|
|
|
use const MYSQLI_BLOB_FLAG; |
11
|
|
|
use const MYSQLI_ENUM_FLAG; |
12
|
|
|
use const MYSQLI_MULTIPLE_KEY_FLAG; |
13
|
|
|
use const MYSQLI_NOT_NULL_FLAG; |
14
|
|
|
use const MYSQLI_PRI_KEY_FLAG; |
15
|
|
|
use const MYSQLI_SET_FLAG; |
16
|
|
|
use const MYSQLI_TYPE_BIT; |
17
|
|
|
use const MYSQLI_TYPE_BLOB; |
18
|
|
|
use const MYSQLI_TYPE_DATE; |
19
|
|
|
use const MYSQLI_TYPE_DATETIME; |
20
|
|
|
use const MYSQLI_TYPE_DECIMAL; |
21
|
|
|
use const MYSQLI_TYPE_DOUBLE; |
22
|
|
|
use const MYSQLI_TYPE_ENUM; |
23
|
|
|
use const MYSQLI_TYPE_FLOAT; |
24
|
|
|
use const MYSQLI_TYPE_GEOMETRY; |
25
|
|
|
use const MYSQLI_TYPE_INT24; |
26
|
|
|
use const MYSQLI_TYPE_JSON; |
27
|
|
|
use const MYSQLI_TYPE_LONG; |
28
|
|
|
use const MYSQLI_TYPE_LONG_BLOB; |
29
|
|
|
use const MYSQLI_TYPE_LONGLONG; |
30
|
|
|
use const MYSQLI_TYPE_MEDIUM_BLOB; |
31
|
|
|
use const MYSQLI_TYPE_NEWDATE; |
32
|
|
|
use const MYSQLI_TYPE_NEWDECIMAL; |
33
|
|
|
use const MYSQLI_TYPE_NULL; |
34
|
|
|
use const MYSQLI_TYPE_SET; |
35
|
|
|
use const MYSQLI_TYPE_SHORT; |
36
|
|
|
use const MYSQLI_TYPE_STRING; |
37
|
|
|
use const MYSQLI_TYPE_TIME; |
38
|
|
|
use const MYSQLI_TYPE_TIMESTAMP; |
39
|
|
|
use const MYSQLI_TYPE_TINY; |
40
|
|
|
use const MYSQLI_TYPE_TINY_BLOB; |
41
|
|
|
use const MYSQLI_TYPE_VAR_STRING; |
42
|
|
|
use const MYSQLI_TYPE_YEAR; |
43
|
|
|
use const MYSQLI_UNIQUE_KEY_FLAG; |
44
|
|
|
use const MYSQLI_UNSIGNED_FLAG; |
45
|
|
|
use const MYSQLI_ZEROFILL_FLAG; |
46
|
|
|
|
47
|
|
|
// Issue #16043 - client API mysqlnd seem not to have MYSQLI_TYPE_JSON defined |
48
|
|
|
if (! defined('MYSQLI_TYPE_JSON')) { |
49
|
|
|
define('MYSQLI_TYPE_JSON', 245); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Handles fields Metadata |
54
|
|
|
* |
55
|
|
|
* NOTE: Getters are not used in all implementations due to the important cost of getters calls |
56
|
|
|
*/ |
57
|
|
|
final class FieldMetadata |
58
|
|
|
{ |
59
|
|
|
public const TYPE_GEOMETRY = 1; |
60
|
|
|
public const TYPE_BIT = 2; |
61
|
|
|
public const TYPE_JSON = 3; |
62
|
|
|
public const TYPE_REAL = 4; |
63
|
|
|
public const TYPE_INT = 5; |
64
|
|
|
public const TYPE_BLOB = 6; |
65
|
|
|
public const TYPE_UNKNOWN = -1; |
66
|
|
|
public const TYPE_NULL = 7; |
67
|
|
|
public const TYPE_STRING = 8; |
68
|
|
|
public const TYPE_DATE = 9; |
69
|
|
|
public const TYPE_TIME = 10; |
70
|
|
|
public const TYPE_TIMESTAMP = 11; |
71
|
|
|
public const TYPE_DATETIME = 12; |
72
|
|
|
public const TYPE_YEAR = 13; |
73
|
|
|
|
74
|
|
|
/** @readonly */ |
75
|
|
|
public bool $isMultipleKey; |
76
|
|
|
|
77
|
|
|
/** @readonly */ |
78
|
|
|
public bool $isPrimaryKey; |
79
|
|
|
|
80
|
|
|
/** @readonly */ |
81
|
|
|
public bool $isUniqueKey; |
82
|
|
|
|
83
|
|
|
/** @readonly */ |
84
|
|
|
public bool $isNotNull; |
85
|
|
|
|
86
|
|
|
/** @readonly */ |
87
|
|
|
public bool $isUnsigned; |
88
|
|
|
|
89
|
|
|
/** @readonly */ |
90
|
|
|
public bool $isZerofill; |
91
|
|
|
|
92
|
|
|
/** @readonly */ |
93
|
|
|
public bool $isNumeric; |
94
|
|
|
|
95
|
|
|
/** @readonly */ |
96
|
|
|
public bool $isBlob; |
97
|
|
|
|
98
|
|
|
/** @readonly */ |
99
|
|
|
public bool $isBinary; |
100
|
|
|
|
101
|
|
|
/** @readonly */ |
102
|
|
|
public bool $isEnum; |
103
|
|
|
|
104
|
|
|
/** @readonly */ |
105
|
|
|
public bool $isSet; |
106
|
|
|
|
107
|
|
|
private int|null $mappedType; |
108
|
|
|
|
109
|
|
|
/** @readonly */ |
110
|
|
|
public bool $isMappedTypeBit; |
111
|
|
|
|
112
|
|
|
/** @readonly */ |
113
|
|
|
public bool $isMappedTypeGeometry; |
114
|
|
|
|
115
|
|
|
/** @readonly */ |
116
|
|
|
public bool $isMappedTypeTimestamp; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* The column name |
120
|
|
|
*/ |
121
|
|
|
public string $name; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* The original column name if an alias did exist |
125
|
|
|
*/ |
126
|
|
|
public string $orgname; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* The table name |
130
|
|
|
*/ |
131
|
|
|
public string $table; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* The original table name |
135
|
|
|
*/ |
136
|
|
|
public string $orgtable; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* The charset number |
140
|
|
|
* |
141
|
|
|
* @readonly |
142
|
|
|
*/ |
143
|
|
|
public int $charsetnr; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* The number of decimals used (for integer fields) |
147
|
|
|
* |
148
|
|
|
* @readonly |
149
|
|
|
*/ |
150
|
|
|
public int $decimals; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* The width of the field, as specified in the table definition. |
154
|
|
|
* |
155
|
|
|
* @readonly |
156
|
|
|
*/ |
157
|
|
|
public int $length; |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* A field only used by the Results class |
161
|
|
|
*/ |
162
|
|
|
public string|null $internalMediaType = null; |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @psalm-param object{ |
166
|
|
|
* name: string, |
167
|
|
|
* orgname: string, |
168
|
|
|
* table: string, |
169
|
|
|
* orgtable: string, |
170
|
|
|
* max_length: int, |
171
|
|
|
* length: int, |
172
|
|
|
* charsetnr: int, |
173
|
|
|
* flags: int, |
174
|
|
|
* type: int, |
175
|
|
|
* decimals: int, |
176
|
|
|
* db: string, |
177
|
|
|
* def: string, |
178
|
|
|
* catalog: string, |
179
|
|
|
* } $field |
180
|
|
|
*/ |
181
|
20 |
|
public function __construct(object $field) |
182
|
|
|
{ |
183
|
20 |
|
$type = $field->type; |
184
|
20 |
|
$this->mappedType = $this->getMappedInternalType($type); |
185
|
|
|
|
186
|
20 |
|
$flags = $field->flags; |
187
|
20 |
|
$this->isMultipleKey = (bool) ($flags & MYSQLI_MULTIPLE_KEY_FLAG); |
188
|
20 |
|
$this->isPrimaryKey = (bool) ($flags & MYSQLI_PRI_KEY_FLAG); |
189
|
20 |
|
$this->isUniqueKey = (bool) ($flags & MYSQLI_UNIQUE_KEY_FLAG); |
190
|
20 |
|
$this->isNotNull = (bool) ($flags & MYSQLI_NOT_NULL_FLAG); |
191
|
20 |
|
$this->isUnsigned = (bool) ($flags & MYSQLI_UNSIGNED_FLAG); |
192
|
20 |
|
$this->isZerofill = (bool) ($flags & MYSQLI_ZEROFILL_FLAG); |
193
|
20 |
|
$this->isBlob = (bool) ($flags & MYSQLI_BLOB_FLAG); |
194
|
20 |
|
$this->isEnum = (bool) ($flags & MYSQLI_ENUM_FLAG); |
195
|
20 |
|
$this->isSet = (bool) ($flags & MYSQLI_SET_FLAG); |
196
|
|
|
|
197
|
|
|
// as flags 32768 can be NUM_FLAG or GROUP_FLAG |
198
|
|
|
// reference: https://www.php.net/manual/en/mysqli-result.fetch-fields.php |
199
|
|
|
// so check field type instead of flags |
200
|
20 |
|
$this->isNumeric = $this->isType(self::TYPE_INT) || $this->isType(self::TYPE_REAL); |
201
|
|
|
|
202
|
|
|
// MYSQLI_PART_KEY_FLAG => 'part_key', |
203
|
|
|
// MYSQLI_TIMESTAMP_FLAG => 'timestamp', |
204
|
|
|
// MYSQLI_AUTO_INCREMENT_FLAG => 'auto_increment', |
205
|
|
|
|
206
|
20 |
|
$this->isMappedTypeBit = $this->isType(self::TYPE_BIT); |
207
|
20 |
|
$this->isMappedTypeGeometry = $this->isType(self::TYPE_GEOMETRY); |
208
|
20 |
|
$this->isMappedTypeTimestamp = $this->isType(self::TYPE_TIMESTAMP); |
209
|
|
|
|
210
|
20 |
|
$this->name = $field->name; |
211
|
20 |
|
$this->orgname = $field->orgname; |
212
|
20 |
|
$this->table = $field->table; |
213
|
20 |
|
$this->orgtable = $field->orgtable; |
214
|
20 |
|
$this->charsetnr = $field->charsetnr; |
215
|
20 |
|
$this->decimals = $field->decimals; |
216
|
20 |
|
$this->length = $field->length; |
217
|
|
|
|
218
|
|
|
// 63 is the number for the MySQL charset "binary" |
219
|
20 |
|
$this->isBinary = ( |
220
|
20 |
|
$type === MYSQLI_TYPE_TINY_BLOB || |
221
|
20 |
|
$type === MYSQLI_TYPE_BLOB || |
222
|
20 |
|
$type === MYSQLI_TYPE_MEDIUM_BLOB || |
223
|
20 |
|
$type === MYSQLI_TYPE_LONG_BLOB || |
224
|
20 |
|
$type === MYSQLI_TYPE_VAR_STRING || |
225
|
20 |
|
$type === MYSQLI_TYPE_STRING |
226
|
20 |
|
) && $this->charsetnr == 63; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @see https://dev.mysql.com/doc/connectors/en/apis-php-mysqli.constants.html |
231
|
|
|
* |
232
|
|
|
* @psalm-return self::TYPE_*|null |
233
|
|
|
*/ |
234
|
20 |
|
private function getMappedInternalType(int $type): int|null |
235
|
|
|
{ |
236
|
20 |
|
return match ($type) { |
237
|
|
|
MYSQLI_TYPE_DECIMAL => self::TYPE_REAL, |
238
|
|
|
MYSQLI_TYPE_NEWDECIMAL => self::TYPE_REAL, |
239
|
|
|
MYSQLI_TYPE_TINY => self::TYPE_INT, |
240
|
|
|
MYSQLI_TYPE_SHORT => self::TYPE_INT, |
241
|
|
|
MYSQLI_TYPE_LONG => self::TYPE_INT, |
242
|
4 |
|
MYSQLI_TYPE_FLOAT => self::TYPE_REAL, |
243
|
|
|
MYSQLI_TYPE_DOUBLE => self::TYPE_REAL, |
244
|
|
|
MYSQLI_TYPE_NULL => self::TYPE_NULL, |
245
|
|
|
MYSQLI_TYPE_TIMESTAMP => self::TYPE_TIMESTAMP, |
246
|
|
|
MYSQLI_TYPE_LONGLONG => self::TYPE_INT, |
247
|
4 |
|
MYSQLI_TYPE_INT24 => self::TYPE_INT, |
248
|
|
|
MYSQLI_TYPE_DATE => self::TYPE_DATE, |
249
|
|
|
MYSQLI_TYPE_TIME => self::TYPE_TIME, |
250
|
|
|
MYSQLI_TYPE_DATETIME => self::TYPE_DATETIME, |
251
|
|
|
MYSQLI_TYPE_YEAR => self::TYPE_YEAR, |
252
|
|
|
MYSQLI_TYPE_NEWDATE => self::TYPE_DATE, |
253
|
|
|
MYSQLI_TYPE_ENUM => self::TYPE_UNKNOWN, |
254
|
|
|
MYSQLI_TYPE_SET => self::TYPE_UNKNOWN, |
255
|
|
|
MYSQLI_TYPE_TINY_BLOB => self::TYPE_BLOB, |
256
|
|
|
MYSQLI_TYPE_MEDIUM_BLOB => self::TYPE_BLOB, |
257
|
|
|
MYSQLI_TYPE_LONG_BLOB => self::TYPE_BLOB, |
258
|
|
|
MYSQLI_TYPE_BLOB => self::TYPE_BLOB, |
259
|
|
|
MYSQLI_TYPE_VAR_STRING => self::TYPE_STRING, |
260
|
4 |
|
MYSQLI_TYPE_STRING => self::TYPE_STRING, |
261
|
|
|
// MySQL returns MYSQLI_TYPE_STRING for CHAR |
262
|
|
|
// and MYSQLI_TYPE_CHAR === MYSQLI_TYPE_TINY |
263
|
|
|
// so this would override TINYINT and mark all TINYINT as string |
264
|
|
|
// see https://github.com/phpmyadmin/phpmyadmin/issues/8569 |
265
|
|
|
//$typeAr[MYSQLI_TYPE_CHAR] = self::TYPE_STRING; |
266
|
|
|
MYSQLI_TYPE_GEOMETRY => self::TYPE_GEOMETRY, |
267
|
|
|
MYSQLI_TYPE_BIT => self::TYPE_BIT, |
268
|
|
|
MYSQLI_TYPE_JSON => self::TYPE_JSON, |
269
|
20 |
|
default => null, |
270
|
20 |
|
}; |
271
|
|
|
} |
272
|
|
|
|
273
|
20 |
|
public function isNotNull(): bool |
274
|
|
|
{ |
275
|
20 |
|
return $this->isNotNull; |
276
|
|
|
} |
277
|
|
|
|
278
|
8 |
|
public function isNumeric(): bool |
279
|
|
|
{ |
280
|
8 |
|
return $this->isNumeric; |
281
|
|
|
} |
282
|
|
|
|
283
|
20 |
|
public function isBinary(): bool |
284
|
|
|
{ |
285
|
20 |
|
return $this->isBinary; |
286
|
|
|
} |
287
|
|
|
|
288
|
20 |
|
public function isBlob(): bool |
289
|
|
|
{ |
290
|
20 |
|
return $this->isBlob; |
291
|
|
|
} |
292
|
|
|
|
293
|
20 |
|
public function isPrimaryKey(): bool |
294
|
|
|
{ |
295
|
20 |
|
return $this->isPrimaryKey; |
296
|
|
|
} |
297
|
|
|
|
298
|
20 |
|
public function isUniqueKey(): bool |
299
|
|
|
{ |
300
|
20 |
|
return $this->isUniqueKey; |
301
|
|
|
} |
302
|
|
|
|
303
|
20 |
|
public function isMultipleKey(): bool |
304
|
|
|
{ |
305
|
20 |
|
return $this->isMultipleKey; |
306
|
|
|
} |
307
|
|
|
|
308
|
20 |
|
public function isUnsigned(): bool |
309
|
|
|
{ |
310
|
20 |
|
return $this->isUnsigned; |
311
|
|
|
} |
312
|
|
|
|
313
|
20 |
|
public function isZerofill(): bool |
314
|
|
|
{ |
315
|
20 |
|
return $this->isZerofill; |
316
|
|
|
} |
317
|
|
|
|
318
|
20 |
|
public function isEnum(): bool |
319
|
|
|
{ |
320
|
20 |
|
return $this->isEnum; |
321
|
|
|
} |
322
|
|
|
|
323
|
20 |
|
public function isSet(): bool |
324
|
|
|
{ |
325
|
20 |
|
return $this->isSet; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Checks that it is type DATE/TIME/DATETIME |
330
|
|
|
*/ |
331
|
|
|
public function isDateTimeType(): bool |
332
|
|
|
{ |
333
|
|
|
return $this->isType(self::TYPE_DATE) |
334
|
|
|
|| $this->isType(self::TYPE_TIME) |
335
|
|
|
|| $this->isType(self::TYPE_DATETIME) |
336
|
|
|
|| $this->isType(self::TYPE_TIMESTAMP); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Checks that it contains time |
341
|
|
|
* A "DATE" field returns false for example |
342
|
|
|
*/ |
343
|
|
|
public function isTimeType(): bool |
344
|
|
|
{ |
345
|
|
|
return $this->isType(self::TYPE_TIME) |
346
|
|
|
|| $this->isType(self::TYPE_TIMESTAMP) |
347
|
|
|
|| $this->isType(self::TYPE_DATETIME); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Get the mapped type as a string |
352
|
|
|
* |
353
|
|
|
* @return string Empty when nothing could be matched |
354
|
|
|
*/ |
355
|
16 |
|
public function getMappedType(): string |
356
|
|
|
{ |
357
|
16 |
|
return match ($this->mappedType) { |
358
|
|
|
self::TYPE_GEOMETRY => 'geometry', |
359
|
|
|
self::TYPE_BIT => 'bit', |
360
|
|
|
self::TYPE_JSON => 'json', |
361
|
4 |
|
self::TYPE_REAL => 'real', |
362
|
4 |
|
self::TYPE_INT => 'int', |
363
|
|
|
self::TYPE_BLOB => 'blob', |
364
|
|
|
self::TYPE_UNKNOWN => 'unknown', |
365
|
|
|
self::TYPE_NULL => 'null', |
366
|
|
|
self::TYPE_STRING => 'string', |
367
|
|
|
self::TYPE_DATE => 'date', |
368
|
|
|
self::TYPE_TIME => 'time', |
369
|
|
|
self::TYPE_TIMESTAMP => 'timestamp', |
370
|
|
|
self::TYPE_DATETIME => 'datetime', |
371
|
|
|
self::TYPE_YEAR => 'year', |
372
|
16 |
|
default => '', |
373
|
16 |
|
}; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Check if it is the mapped type |
378
|
|
|
* |
379
|
|
|
* @phpstan-param self::TYPE_* $type |
380
|
|
|
*/ |
381
|
20 |
|
public function isType(int $type): bool |
382
|
|
|
{ |
383
|
20 |
|
return $this->mappedType === $type; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Check if it is NOT the mapped type |
388
|
|
|
* |
389
|
|
|
* @phpstan-param self::TYPE_* $type |
390
|
|
|
*/ |
391
|
|
|
public function isNotType(int $type): bool |
392
|
|
|
{ |
393
|
|
|
return $this->mappedType !== $type; |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|