Merge "Use gendered pronouns in emailuserfooter"
[mediawiki.git] / includes / libs / rdbms / database / utils / GeneralizedSql.php
blob16ee7441c5361235adaf7c90c40e9002a33793dd
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
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. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
20 namespace Wikimedia\Rdbms;
22 /**
23 * Lazy-loaded wrapper for simplification and scrubbing of SQL queries for profiling
25 * @since 1.34
26 * @ingroup Profiler
27 * @ingroup Database
29 class GeneralizedSql {
30 /** @var string */
31 private $rawSql;
32 /** @var string */
33 private $prefix;
35 /** @var string|null */
36 private $genericSql;
38 /**
39 * @param string $rawSql
40 * @param string $prefix
42 public function __construct( $rawSql, $prefix ) {
43 $this->rawSql = $rawSql;
44 $this->prefix = $prefix;
47 /**
48 * @return string
50 public function stringify() {
51 if ( $this->genericSql !== null ) {
52 return $this->genericSql;
55 $this->genericSql = $this->prefix .
56 substr( QueryBuilderFromRawSql::generalizeSQL( $this->rawSql ), 0, 255 );
58 return $this->genericSql;
61 public function getRawSql() {
62 return $this->rawSql;
65 /**
66 * @param Query $query
67 * @param string $prefix
68 * @return self
70 public static function newFromQuery( Query $query, $prefix ) {
71 $generalizedSql = new self( $query->getSQL(), $prefix );
73 $cleanedSql = $query->getCleanedSql();
74 if ( $cleanedSql != '' ) {
75 // Generalized SQL already provided; no need to use regexes
76 $generalizedSql->genericSql = $prefix . $cleanedSql;
79 return $generalizedSql;