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
20 namespace Wikimedia\Rdbms
;
23 * Lazy-loaded wrapper for simplification and scrubbing of SQL queries for profiling
29 class GeneralizedSql
{
35 /** @var string|null */
39 * @param string $rawSql
40 * @param string $prefix
42 public function __construct( $rawSql, $prefix ) {
43 $this->rawSql
= $rawSql;
44 $this->prefix
= $prefix;
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() {
67 * @param string $prefix
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;