Passed
Push — master ( 37473d...30ff7d )
by Maurício
11:30 queued 14s
created

libraries/classes/Dbal/DbalInterface.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Dbal;
6
7
use PhpMyAdmin\Config\Settings\Server;
8
use PhpMyAdmin\ConfigStorage\Relation;
9
use PhpMyAdmin\DatabaseInterface;
10
use PhpMyAdmin\FieldMetadata;
11
use PhpMyAdmin\SystemDatabase;
12
use PhpMyAdmin\Table;
13
14
/**
15
 * Main interface for database interactions
16
 *
17
 * @psalm-import-type ConnectionType from Connection
18
 */
19
interface DbalInterface
20
{
21
    public const FETCH_NUM = 'NUM';
22
    public const FETCH_ASSOC = 'ASSOC';
23
24
    /**
25
     * runs a query
26
     *
27
     * @param string $query             SQL query to execute
28
     * @param int    $options           optional query options
29
     * @param bool   $cacheAffectedRows whether to cache affected rows
30
     * @psalm-param ConnectionType $connectionType
31
     */
32
    public function query(
33
        string $query,
34
        int $connectionType = Connection::TYPE_USER,
35
        int $options = 0,
36
        bool $cacheAffectedRows = true,
37
    ): ResultInterface;
38
39
    /**
40
     * runs a query and returns the result
41
     *
42
     * @param string $query             query to run
43
     * @param int    $options           query options
44
     * @param bool   $cacheAffectedRows whether to cache affected row
45
     * @psalm-param ConnectionType $connectionType
46
     */
47
    public function tryQuery(
48
        string $query,
49
        int $connectionType = Connection::TYPE_USER,
50
        int $options = 0,
51
        bool $cacheAffectedRows = true,
52
    ): mixed;
53
54
    /**
55
     * Send multiple SQL queries to the database server and execute the first one
56
     *
57
     * @param string $multiQuery multi query statement to execute
58
     * @psalm-param ConnectionType $connectionType
59
     */
60
    public function tryMultiQuery(
61
        string $multiQuery = '',
62
        int $connectionType = Connection::TYPE_USER,
63
    ): bool;
64
65
    /**
66
     * returns array with table names for given db
67
     *
68
     * @param string $database name of database
69
     * @psalm-param ConnectionType $connectionType
70
     *
71
     * @return array<int, string>   tables names
72
     */
73
    public function getTables(string $database, int $connectionType = Connection::TYPE_USER): array;
74
75
    /**
76
     * returns array of all tables in given db or dbs
77
     * this function expects unquoted names:
78
     * RIGHT: my_database
79
     * WRONG: `my_database`
80
     * WRONG: my\_database
81
     * if $tbl_is_group is true, $table is used as filter for table names
82
     *
83
     * <code>
84
     * $dbi->getTablesFull('my_database');
85
     * $dbi->getTablesFull('my_database', 'my_table'));
86
     * $dbi->getTablesFull('my_database', 'my_tables_', true));
87
     * </code>
88
     *
89
     * @param string         $database     database
90
     * @param string|mixed[] $table        table name(s)
91
     * @param bool           $tableIsGroup $table is a table group
92
     * @param int            $limitOffset  zero-based offset for the count
93
     * @param bool|int       $limitCount   number of tables to return
94
     * @param string         $sortBy       table attribute to sort by
95
     * @param string         $sortOrder    direction to sort (ASC or DESC)
96
     * @param string|null    $tableType    whether table or view
97
     * @psalm-param ConnectionType $connectionType
98
     *
99
     * @return mixed[]           list of tables in given db(s)
100
     *
101
     * @todo    move into Table
102
     */
103
    public function getTablesFull(
104
        string $database,
105
        string|array $table = '',
106
        bool $tableIsGroup = false,
107
        int $limitOffset = 0,
108
        bool|int $limitCount = false,
109
        string $sortBy = 'Name',
110
        string $sortOrder = 'ASC',
111
        string|null $tableType = null,
112
        int $connectionType = Connection::TYPE_USER,
113
    ): array;
114
115
    /**
116
     * Get VIEWs in a particular database
117
     *
118
     * @param string $db Database name to look in
119
     *
120
     * @return Table[] Set of VIEWs inside the database
121
     */
122
    public function getVirtualTables(string $db): array;
123
124
    /**
125
     * returns array with databases containing extended infos about them
126
     *
127
     * @param string|null $database    database
128
     * @param bool        $forceStats  retrieve stats also for MySQL < 5
129
     * @param string      $sortBy      column to order by
130
     * @param string      $sortOrder   ASC or DESC
131
     * @param int         $limitOffset starting offset for LIMIT
132
     * @param bool|int    $limitCount  row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']
133
     * @psalm-param ConnectionType $connectionType
134
     *
135
     * @return mixed[]
136
     *
137
     * @todo    move into ListDatabase?
138
     */
139
    public function getDatabasesFull(
140
        string|null $database = null,
141
        bool $forceStats = false,
142
        int $connectionType = Connection::TYPE_USER,
143
        string $sortBy = 'SCHEMA_NAME',
144
        string $sortOrder = 'ASC',
145
        int $limitOffset = 0,
146
        bool|int $limitCount = false,
147
    ): array;
148
149
    /**
150
     * returns detailed array with all columns for given table in database,
151
     * or all tables/databases
152
     *
153
     * @param string|null $database name of database
154
     * @param string|null $table    name of table to retrieve columns from
155
     * @param string|null $column   name of specific column
156
     * @psalm-param ConnectionType $connectionType
157
     *
158
     * @return mixed[]
159
     */
160
    public function getColumnsFull(
161
        string|null $database = null,
162
        string|null $table = null,
163
        string|null $column = null,
164
        int $connectionType = Connection::TYPE_USER,
165
    ): array;
166
167
    /**
168
     * Returns description of a $column in given table
169
     *
170
     * @param string $database name of database
171
     * @param string $table    name of table to retrieve columns from
172
     * @param string $column   name of column
173
     * @param bool   $full     whether to return full info or only column names
174
     * @psalm-param ConnectionType $connectionType
175
     *
176
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
177
     *  Field: string,
178
     *  Type: string,
179
     *  Collation?: string|null,
180
     *  Null:'YES'|'NO',
181
     *  Key: string,
182
     *  Default: string|null,
183
     *  Extra: string,
184
     *  Privileges?: string,
185
     *  Comment?: string
186
     * }|null
187
     */
188
    public function getColumn(
189
        string $database,
190
        string $table,
191
        string $column,
192
        bool $full = false,
193
        int $connectionType = Connection::TYPE_USER,
194
    ): array|null;
195
196
    /**
197
     * Returns descriptions of columns in given table
198
     *
199
     * @param string $database name of database
200
     * @param string $table    name of table to retrieve columns from
201
     * @param bool   $full     whether to return full info or only column names
202
     * @psalm-param ConnectionType $connectionType
203
     *
204
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
205
     *  Field: string,
206
     *  Type: string,
207
     *  Collation?: string|null,
208
     *  Null:'YES'|'NO',
209
     *  Key: string,
210
     *  Default: string|null,
211
     *  Extra: string,
212
     *  Privileges?: string,
213
     *  Comment?: string
214
     * }[] array indexed by column names
215
     */
216
    public function getColumns(
217
        string $database,
218
        string $table,
219
        bool $full = false,
220
        int $connectionType = Connection::TYPE_USER,
221
    ): array;
222
223
    /**
224
     * Returns all column names in given table
225
     *
226
     * @param string $database name of database
227
     * @param string $table    name of table to retrieve columns from
228
     * @psalm-param ConnectionType $connectionType
229
     *
230
     * @return string[]
231
     */
232
    public function getColumnNames(
233
        string $database,
234
        string $table,
235
        int $connectionType = Connection::TYPE_USER,
236
    ): array;
237
238
    /**
239
     * Returns indexes of a table
240
     *
241
     * @param string $database name of database
242
     * @param string $table    name of the table whose indexes are to be retrieved
243
     * @psalm-param ConnectionType $connectionType
244
     *
245
     * @return array<int, array<string, string|null>>
246
     * @psalm-return array<int, array{
247
     *   Table: string,
248
     *   Non_unique: '0'|'1',
249
     *   Key_name: string,
250
     *   Seq_in_index: string,
251
     *   Column_name: string|null,
252
     *   Collation: 'A'|'D'|null,
253
     *   Cardinality: string,
254
     *   Sub_part: string|null,
255
     *   Packed: string|null,
256
     *   Null: string|null,
257
     *   Index_type: 'BTREE'|'FULLTEXT'|'HASH'|'RTREE',
258
     *   Comment: string,
259
     *   Index_comment: string,
260
     *   Ignored?: string,
261
     *   Visible?: string,
262
     *   Expression?: string|null
263
     * }>
264
     */
265
    public function getTableIndexes(
266
        string $database,
267
        string $table,
268
        int $connectionType = Connection::TYPE_USER,
269
    ): array;
270
271
    /**
272
     * returns value of given mysql server variable
273
     *
274
     * @param string $var  mysql server variable name
275
     * @param int    $type DatabaseInterface::GETVAR_SESSION | DatabaseInterface::GETVAR_GLOBAL
276
     * @psalm-param ConnectionType $connectionType
277
     *
278
     * @return false|string|null value for mysql server variable
279
     */
280
    public function getVariable(
281
        string $var,
282
        int $type = DatabaseInterface::GETVAR_SESSION,
283
        int $connectionType = Connection::TYPE_USER,
284
    ): false|string|null;
285
286
    /**
287
     * Sets new value for a variable if it is different from the current value
288
     *
289
     * @param string $var   variable name
290
     * @param string $value value to set
291
     * @psalm-param ConnectionType $connectionType
292
     */
293
    public function setVariable(
294
        string $var,
295
        string $value,
296
        int $connectionType = Connection::TYPE_USER,
297
    ): bool;
298
299
    /**
300
     * Function called just after a connection to the MySQL database server has
301
     * been established. It sets the connection collation, and determines the
302
     * version of MySQL which is running.
303
     */
304
    public function postConnect(Server $currentServer): void;
305
306
    /**
307
     * Sets collation connection for user link
308
     *
309
     * @param string $collation collation to set
310
     */
311
    public function setCollation(string $collation): void;
312
313
    /**
314
     * Function called just after a connection to the MySQL database server has
315
     * been established. It sets the connection collation, and determines the
316
     * version of MySQL which is running.
317
     */
318
    public function postConnectControl(Relation $relation): void;
319
320
    /**
321
     * returns a single value from the given result or query,
322
     * if the query or the result has more than one row or field
323
     * the first field of the first row is returned
324
     *
325
     * <code>
326
     * $sql = 'SELECT `name` FROM `user` WHERE `id` = 123';
327
     * $user_name = $dbi->fetchValue($sql);
328
     * // produces
329
     * // $user_name = 'John Doe'
330
     * </code>
331
     *
332
     * @param string     $query The query to execute
333
     * @param int|string $field field to fetch the value from, starting at 0, with 0 being default
334
     * @psalm-param ConnectionType $connectionType
335
     *
336
     * @return string|false|null value of first field in first row from result
337
     *               or false if not found
338
     */
339
    public function fetchValue(
340
        string $query,
341
        int|string $field = 0,
342
        int $connectionType = Connection::TYPE_USER,
343
    ): string|false|null;
344
345
    /**
346
     * Returns only the first row from the result or null if result is empty.
347
     *
348
     * <code>
349
     * $sql = 'SELECT * FROM `user` WHERE `id` = 123';
350
     * $user = $dbi->fetchSingleRow($sql);
351
     * // produces
352
     * // $user = array('id' => 123, 'name' => 'John Doe')
353
     * </code>
354
     *
355
     * @param string $query The query to execute
356
     * @param string $type  NUM|ASSOC returned array should either numeric associative or both
357
     * @psalm-param self::FETCH_NUM|self::FETCH_ASSOC $type
358
     * @psalm-param ConnectionType $connectionType
359
     */
360
    public function fetchSingleRow(
361
        string $query,
362
        string $type = DbalInterface::FETCH_ASSOC,
363
        int $connectionType = Connection::TYPE_USER,
364
    ): array|null;
365
366
    /**
367
     * returns all rows in the resultset in one array
368
     *
369
     * <code>
370
     * $sql = 'SELECT * FROM `user`';
371
     * $users = $dbi->fetchResult($sql);
372
     * // produces
373
     * // $users[] = array('id' => 123, 'name' => 'John Doe')
374
     *
375
     * $sql = 'SELECT `id`, `name` FROM `user`';
376
     * $users = $dbi->fetchResult($sql, 'id');
377
     * // produces
378
     * // $users['123'] = array('id' => 123, 'name' => 'John Doe')
379
     *
380
     * $sql = 'SELECT `id`, `name` FROM `user`';
381
     * $users = $dbi->fetchResult($sql, 0);
382
     * // produces
383
     * // $users['123'] = array(0 => 123, 1 => 'John Doe')
384
     *
385
     * $sql = 'SELECT `id`, `name` FROM `user`';
386
     * $users = $dbi->fetchResult($sql, 'id', 'name');
387
     * // or
388
     * $users = $dbi->fetchResult($sql, 0, 1);
389
     * // produces
390
     * // $users['123'] = 'John Doe'
391
     *
392
     * $sql = 'SELECT `name` FROM `user`';
393
     * $users = $dbi->fetchResult($sql);
394
     * // produces
395
     * // $users[] = 'John Doe'
396
     *
397
     * $sql = 'SELECT `group`, `name` FROM `user`'
398
     * $users = $dbi->fetchResult($sql, array('group', null), 'name');
399
     * // produces
400
     * // $users['admin'][] = 'John Doe'
401
     *
402
     * $sql = 'SELECT `group`, `name` FROM `user`'
403
     * $users = $dbi->fetchResult($sql, array('group', 'name'), 'id');
404
     * // produces
405
     * // $users['admin']['John Doe'] = '123'
406
     * </code>
407
     *
408
     * @param string                  $query query to execute
409
     * @param string|int|mixed[]|null $key   field-name or offset used as key for array or array of those
410
     * @param string|int|null         $value value-name or offset used as value for array
411
     * @psalm-param ConnectionType $connectionType
412
     *
413
     * @return mixed[] resultrows or values indexed by $key
414
     */
415
    public function fetchResult(
416
        string $query,
417
        string|int|array|null $key = null,
418
        string|int|null $value = null,
419
        int $connectionType = Connection::TYPE_USER,
420
    ): array;
421
422
    /**
423
     * Get supported SQL compatibility modes
424
     *
425
     * @return mixed[] supported SQL compatibility modes
426
     */
427
    public function getCompatibilities(): array;
428
429
    /**
430
     * returns warnings for last query
431
     *
432
     * @psalm-param ConnectionType $connectionType
433
     *
434
     * @return mixed[] warnings
435
     */
436
    public function getWarnings(int $connectionType = Connection::TYPE_USER): array;
437
438
    /**
439
     * gets the current user with host
440
     *
441
     * @return string the current user i.e. user@host
442
     */
443
    public function getCurrentUser(): string;
444
445
    /**
446
     * Checks if current user is superuser
447
     */
448
    public function isSuperUser(): bool;
449
450
    public function isGrantUser(): bool;
451
452
    public function isCreateUser(): bool;
453
454
    public function isConnected(): bool;
455
456
    /**
457
     * Get the current user and host
458
     *
459
     * @return array<int, string> array of username and hostname
460
     */
461
    public function getCurrentUserAndHost(): array;
462
463
    /**
464
     * Returns value for lower_case_table_names variable
465
     *
466
     * @see https://mariadb.com/kb/en/server-system-variables/#lower_case_table_names
467
     * @see https://dev.mysql.com/doc/refman/en/server-system-variables.html#sysvar_lower_case_table_names
468
     *
469
     * @psalm-return 0|1|2
470
     */
471
    public function getLowerCaseNames(): int;
472
473
    /**
474
     * Connects to the database server.
475
     *
476
     * @param int|null $target How to store connection link, defaults to $mode
477
     * @psalm-param ConnectionType $connectionType
478
     * @psalm-param ConnectionType|null $target
479
     */
480
    public function connect(Server $currentServer, int $connectionType, int|null $target = null): Connection|null;
481
482
    /**
483
     * selects given database
484
     *
485
     * @param string|DatabaseName $dbname database name to select
486
     * @psalm-param ConnectionType $connectionType
487
     */
488
    public function selectDb(string|DatabaseName $dbname, int $connectionType = Connection::TYPE_USER): bool;
489
490
    /**
491
     * Check if there are any more query results from a multi query
492
     *
493
     * @psalm-param ConnectionType $connectionType
494
     */
495
    public function moreResults(int $connectionType = Connection::TYPE_USER): bool;
496
497
    /**
498
     * Prepare next result from multi_query
499
     *
500
     * @psalm-param ConnectionType $connectionType
501
     */
502
    public function nextResult(int $connectionType = Connection::TYPE_USER): bool;
503
504
    /**
505
     * Store the result returned from multi query
506
     *
507
     * @psalm-param ConnectionType $connectionType
508
     *
509
     * @return mixed false when empty results / result set when not empty
510
     */
511
    public function storeResult(int $connectionType = Connection::TYPE_USER): mixed;
512
513
    /**
514
     * Returns a string representing the type of connection used
515
     *
516
     * @psalm-param ConnectionType $connectionType
517
     *
518
     * @return string|bool type of connection used
519
     */
520
    public function getHostInfo(int $connectionType = Connection::TYPE_USER): string|bool;
521
522
    /**
523
     * Returns the version of the MySQL protocol used
524
     *
525
     * @psalm-param ConnectionType $connectionType
526
     *
527
     * @return int|bool version of the MySQL protocol used
528
     */
529
    public function getProtoInfo(int $connectionType = Connection::TYPE_USER): int|bool;
530
531
    /**
532
     * returns a string that represents the client library version
533
     *
534
     * @return string MySQL client library version
535
     */
536
    public function getClientInfo(): string;
537
538
    /**
539
     * Returns last error message or an empty string if no errors occurred.
540
     *
541
     * @psalm-param ConnectionType $connectionType
542
     */
543
    public function getError(int $connectionType = Connection::TYPE_USER): string;
544
545
    /**
546
     * returns the number of rows returned by last query
547
     * used with tryQuery as it accepts false
548
     *
549
     * @param string $query query to run
550
     *
551
     * @psalm-return int|numeric-string
552
     */
553
    public function queryAndGetNumRows(string $query): string|int;
554
555
    /**
556
     * returns last inserted auto_increment id for given $link
557
     * or $GLOBALS['userlink']
558
     *
559
     * @psalm-param ConnectionType $connectionType
560
     */
561
    public function insertId(int $connectionType = Connection::TYPE_USER): int;
562
563
    /**
564
     * returns the number of rows affected by last query
565
     *
566
     * @param bool $getFromCache whether to retrieve from cache
567
     * @psalm-param ConnectionType $connectionType
568
     *
569
     * @psalm-return int|numeric-string
570
     */
571
    public function affectedRows(int $connectionType = Connection::TYPE_USER, bool $getFromCache = true): int|string;
572
573
    /**
574
     * returns metainfo for fields in $result
575
     *
576
     * @param ResultInterface $result result set identifier
577
     *
578
     * @return FieldMetadata[] meta info for fields in $result
579
     */
580
    public function getFieldsMeta(ResultInterface $result): array;
581
582
    /**
583
     * Returns properly quoted string for use in MySQL queries.
584
     *
585
     * @param string $str string to be quoted
586
     * @psalm-param ConnectionType $connectionType
587
     *
588
     * @psalm-return non-empty-string
589
     *
590
     * @psalm-taint-escape sql
591
     */
592
    public function quoteString(string $str, int $connectionType = Connection::TYPE_USER): string;
593
594
    /**
595
     * returns properly escaped string for use in MySQL queries
596
     *
597
     * @deprecated Use {@see quoteString()} instead.
598
     *
599
     * @param string $str string to be escaped
600
     * @psalm-param ConnectionType $connectionType
601
     *
602
     * @return string a MySQL escaped string
603
     */
604
    public function escapeString(string $str, int $connectionType = Connection::TYPE_USER): string;
605
606
    /**
607
     * Returns properly escaped string for use in MySQL LIKE clauses.
608
     * This method escapes only _, %, and /. It does not escape quotes or any other characters.
609
     *
610
     * @param string $str string to be escaped
611
     *
612
     * @return string a MySQL escaped LIKE string
613
     */
614
    public function escapeMysqlWildcards(string $str): string;
615
616
    /**
617
     * Checks if this database server is running on Amazon RDS.
618
     */
619
    public function isAmazonRds(): bool;
620
621
    /**
622
     * Gets SQL for killing a process.
623
     *
624
     * @param int $process Process ID
625
     */
626
    public function getKillQuery(int $process): string;
627
628
    /**
629
     * Get the phpmyadmin database manager
630
     */
631
    public function getSystemDatabase(): SystemDatabase;
632
633
    /**
634
     * Get a table with database name and table name
635
     *
636
     * @param string $dbName    DB name
637
     * @param string $tableName Table name
638
     */
639
    public function getTable(string $dbName, string $tableName): Table;
640
641
    /**
642
     * returns collation of given db
643
     *
644
     * @param string $db name of db
645
     *
646
     * @return string  collation of $db
647
     */
648
    public function getDbCollation(string $db): string;
649
650
    /**
651
     * returns default server collation from show variables
652
     */
653
    public function getServerCollation(): string;
654
655
    /**
656
     * Server version as number
657
     */
658
    public function getVersion(): int;
659
660
    /**
661
     * Server version
662
     */
663
    public function getVersionString(): string;
664
665
    /**
666
     * Server version comment
667
     */
668
    public function getVersionComment(): string;
669
670
    /**
671
     * Whether connection is MariaDB
672
     */
673
    public function isMariaDB(): bool;
674
675
    /**
676
     * Whether connection is Percona
677
     */
678
    public function isPercona(): bool;
679
680
    /**
681
     * Prepare an SQL statement for execution.
682
     *
683
     * @param string $query The query, as a string.
684
     * @psalm-param ConnectionType $connectionType
685
     */
686
    public function prepare(string $query, int $connectionType = Connection::TYPE_USER): Statement|null;
687
}
688