MediaWiki master
ReplaceQueryBuilder.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\Rdbms;
4
5use InvalidArgumentException;
6use UnexpectedValueException;
7
25 private $table = '';
26
30 private $rows = [];
31
35 private $caller = __CLASS__;
36
40 private $uniqueIndexFields = [];
41
42 protected IDatabase $db;
43
48 public function __construct( IDatabase $db ) {
49 $this->db = $db;
50 }
51
59 public function connection( IDatabase $db ) {
60 if ( $this->db->getType() !== $db->getType() ) {
61 throw new InvalidArgumentException(
62 __METHOD__ . ' cannot switch to a database of a different type.'
63 );
64 }
65 $this->db = $db;
66 return $this;
67 }
68
84 public function queryInfo( $info ) {
85 if ( isset( $info['table'] ) ) {
86 $this->table( $info['table'] );
87 }
88 if ( isset( $info['rows'] ) ) {
89 $this->rows( $info['rows'] );
90 }
91 if ( isset( $info['uniqueIndexFields'] ) ) {
92 $this->uniqueIndexFields( (array)$info['uniqueIndexFields'] );
93 }
94 if ( isset( $info['caller'] ) ) {
95 $this->caller( $info['caller'] );
96 }
97 return $this;
98 }
99
107 public function table( $table ) {
108 $this->table = $table;
109 return $this;
110 }
111
119 public function replaceInto( string $table ) {
120 return $this->table( $table );
121 }
122
133 public function rows( array $rows ) {
134 $this->rows = array_merge( $this->rows, $rows );
135 return $this;
136 }
137
147 public function row( array $row ) {
148 $this->rows[] = $row;
149 return $this;
150 }
151
158 public function uniqueIndexFields( $uniqueIndexFields ) {
159 if ( is_string( $uniqueIndexFields ) ) {
160 $uniqueIndexFields = [ $uniqueIndexFields ];
161 }
162 $this->uniqueIndexFields = $uniqueIndexFields;
163 return $this;
164 }
165
173 public function caller( $fname ) {
174 $this->caller = $fname;
175 return $this;
176 }
177
181 public function execute() {
182 if ( !$this->rows ) {
183 throw new UnexpectedValueException(
184 __METHOD__ . ' can\'t have empty $rows value' );
185 }
186 if ( $this->table === '' ) {
187 throw new UnexpectedValueException(
188 __METHOD__ . ' expects table not to be empty' );
189 }
190 if ( !$this->uniqueIndexFields ) {
191 throw new UnexpectedValueException(
192 __METHOD__ . ' expects unique key to be provided' );
193 }
194 $this->db->replace( $this->table, [ $this->uniqueIndexFields ], $this->rows, $this->caller );
195 }
196
207 public function getQueryInfo() {
208 $info = [
209 'table' => $this->table,
210 'rows' => $this->rows,
211 'uniqueIndexFields' => $this->uniqueIndexFields,
212 ];
213 if ( $this->caller !== __CLASS__ ) {
214 $info['caller'] = $this->caller;
215 }
216 return $info;
217 }
218}
Build REPLACE queries with a fluent interface.
execute()
Run the constructed REPLACE query and return the result.
uniqueIndexFields( $uniqueIndexFields)
Set the unique index fields.
replaceInto(string $table)
Set table for the query.
table( $table)
Manually set the table name to be passed to IDatabase::replace()
caller( $fname)
Set the method name to be included in an SQL comment.
connection(IDatabase $db)
Change the IDatabase object the query builder is bound to.
__construct(IDatabase $db)
Only for use in subclasses.
rows(array $rows)
Add rows to be inserted.
row(array $row)
Add one row to be inserted.
queryInfo( $info)
Set the query parameters to the given values, appending to the values which were already set.
getQueryInfo()
Get an associative array describing the query in terms of its raw parameters to Database::replace().
Interface to a relational database.
Definition IDatabase.php:45
getType()
Get the RDBMS type of the server (e.g.