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
43 protected $db;
44
49 public function __construct( IDatabase $db ) {
50 $this->db = $db;
51 }
52
60 public function connection( IDatabase $db ) {
61 if ( $this->db->getType() !== $db->getType() ) {
62 throw new InvalidArgumentException(
63 __METHOD__ . ' cannot switch to a database of a different type.'
64 );
65 }
66 $this->db = $db;
67 return $this;
68 }
69
85 public function queryInfo( $info ) {
86 if ( isset( $info['table'] ) ) {
87 $this->table( $info['table'] );
88 }
89 if ( isset( $info['rows'] ) ) {
90 $this->rows( $info['rows'] );
91 }
92 if ( isset( $info['uniqueIndexFields'] ) ) {
93 $this->uniqueIndexFields( (array)$info['uniqueIndexFields'] );
94 }
95 if ( isset( $info['caller'] ) ) {
96 $this->caller( $info['caller'] );
97 }
98 return $this;
99 }
100
108 public function table( $table ) {
109 $this->table = $table;
110 return $this;
111 }
112
120 public function replaceInto( string $table ) {
121 return $this->table( $table );
122 }
123
134 public function rows( array $rows ) {
135 $this->rows = array_merge( $this->rows, $rows );
136 return $this;
137 }
138
148 public function row( array $row ) {
149 $this->rows[] = $row;
150 return $this;
151 }
152
159 public function uniqueIndexFields( $uniqueIndexFields ) {
160 if ( is_string( $uniqueIndexFields ) ) {
161 $uniqueIndexFields = [ $uniqueIndexFields ];
162 }
163 $this->uniqueIndexFields = $uniqueIndexFields;
164 return $this;
165 }
166
174 public function caller( $fname ) {
175 $this->caller = $fname;
176 return $this;
177 }
178
182 public function execute() {
183 if ( !$this->rows ) {
184 throw new UnexpectedValueException(
185 __METHOD__ . ' can\'t have empty $rows value' );
186 }
187 if ( $this->table === '' ) {
188 throw new UnexpectedValueException(
189 __METHOD__ . ' expects table not to be empty' );
190 }
191 if ( !$this->uniqueIndexFields ) {
192 throw new UnexpectedValueException(
193 __METHOD__ . ' expects unique key to be provided' );
194 }
195 $this->db->replace( $this->table, [ $this->uniqueIndexFields ], $this->rows, $this->caller );
196 }
197
208 public function getQueryInfo() {
209 $info = [
210 'table' => $this->table,
211 'rows' => $this->rows,
212 'uniqueIndexFields' => $this->uniqueIndexFields,
213 ];
214 if ( $this->caller !== __CLASS__ ) {
215 $info['caller'] = $this->caller;
216 }
217 return $info;
218 }
219}
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.