Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Query
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 8
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteQuery
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
56
 getVerb
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 fieldHasBit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSQL
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFlags
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getWriteTable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCleanedSql
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6namespace Wikimedia\Rdbms;
7
8use Wikimedia\Rdbms\Platform\SQLPlatform;
9
10/**
11 * Holds information on Query to be executed
12 *
13 * @ingroup Database
14 * @internal
15 * @since 1.41
16 */
17class Query {
18
19    /**
20     * The possible multi-word values for getVerb().
21     * @internal For use by Query and QueryBuilderFromRawSQL only.
22     */
23    public const MULTIWORD_VERBS = [
24        'RELEASE SAVEPOINT',
25        'ROLLBACK TO SAVEPOINT',
26        'CREATE TEMPORARY',
27        'CREATE INDEX',
28        'DROP INDEX',
29        'CREATE DATABASE',
30        'ALTER DATABASE',
31        'DROP DATABASE',
32    ];
33
34    private string $sql;
35    private int $flags;
36    private string $queryVerb;
37    private ?string $writeTable;
38    private string $cleanedSql;
39
40    /**
41     * @param string $sql SQL statement text
42     * @param int $flags Bit field of ISQLPlatform::QUERY_CHANGE_* constants
43     * @param string $queryVerb The first words of the SQL statement that convey what kind of
44     *  database/table/column/index command was specified. Except for the cases listed in
45     *  {@see MULTIWORD_VERBS}, this will be the first word of the SQL statement.
46     * @param string|null $writeTable The table targeted for writes, if any. Can be omitted if
47     *   it would be hard to identify the table (e.g. when parsing an arbitrary SQL string).
48     * @param string $cleanedSql Sanitized/simplified SQL statement text for logging.
49     *   Typically, this means replacing variables / parameter values with placeholders.
50     *   Can be omitted, in which case the code using the Query is responsible for sanitizing.
51     */
52    public function __construct(
53        string $sql,
54        $flags,
55        $queryVerb,
56        ?string $writeTable = null,
57        $cleanedSql = ''
58    ) {
59        $this->sql = $sql;
60        $this->flags = $flags;
61        $this->queryVerb = $queryVerb;
62        $this->writeTable = $writeTable;
63        $this->cleanedSql = substr( $cleanedSql, 0, 255 );
64    }
65
66    public function isWriteQuery(): bool {
67        // Check if a SQL wrapper method already flagged the query as a non-write
68        if (
69            $this->fieldHasBit( $this->flags, SQLPlatform::QUERY_CHANGE_NONE ) ||
70            $this->fieldHasBit( $this->flags, SQLPlatform::QUERY_CHANGE_TRX ) ||
71            $this->fieldHasBit( $this->flags, SQLPlatform::QUERY_CHANGE_LOCKS )
72        ) {
73            return false;
74        }
75        // Check if a SQL wrapper method already flagged the query as a write
76        if (
77            $this->fieldHasBit( $this->flags, SQLPlatform::QUERY_CHANGE_ROWS ) ||
78            $this->fieldHasBit( $this->flags, SQLPlatform::QUERY_CHANGE_SCHEMA ) ||
79            $this->fieldHasBit( $this->flags, SQLPlatform::QUERY_PSEUDO_PERMANENT )
80        ) {
81            return true;
82        }
83
84        throw new DBLanguageError( __METHOD__ . ' called with incorrect flags parameter' );
85    }
86
87    public function getVerb(): string {
88        return $this->queryVerb;
89    }
90
91    private function fieldHasBit( int $flags, int $bit ): bool {
92        return ( ( $flags & $bit ) === $bit );
93    }
94
95    public function getSQL(): string {
96        return $this->sql;
97    }
98
99    public function getFlags(): int {
100        // The whole concept of flags is terrible. This should be deprecated.
101        return $this->flags;
102    }
103
104    /**
105     * Get the table which is being written to, or null for a read query or if
106     * the destination is unknown.
107     */
108    public function getWriteTable(): ?string {
109        return $this->writeTable;
110    }
111
112    /**
113     * Get the cleaned/sanitized SQL statement text for logging.
114     * Might return an empty string, which means sanitization is the caller's responsibility.
115     */
116    public function getCleanedSql(): string {
117        return $this->cleanedSql;
118    }
119}