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
51 public function __construct( IDatabase $db ) {
52 $this->db = $db;
53 }
54
62 public function connection( IDatabase $db ) {
63 if ( $this->db->getType() !== $db->getType() ) {
64 throw new InvalidArgumentException(
65 __METHOD__ . ' cannot switch to a database of a different type.'
66 );
67 }
68 $this->db = $db;
69 return $this;
70 }
71
87 public function queryInfo( $info ) {
88 if ( isset( $info['table'] ) ) {
89 $this->table( $info['table'] );
90 }
91 if ( isset( $info['rows'] ) ) {
92 $this->rows( $info['rows'] );
93 }
94 if ( isset( $info['uniqueIndexFields'] ) ) {
95 $this->uniqueIndexFields( (array)$info['uniqueIndexFields'] );
96 }
97 if ( isset( $info['caller'] ) ) {
98 $this->caller( $info['caller'] );
99 }
100 return $this;
101 }
102
110 public function table( $table ) {
111 $this->table = $table;
112 return $this;
113 }
114
122 public function replaceInto( string $table ) {
123 return $this->table( $table );
124 }
125
136 public function rows( array $rows ) {
137 $this->rows = array_merge( $this->rows, $rows );
138 return $this;
139 }
140
150 public function row( array $row ) {
151 $this->rows[] = $row;
152 return $this;
153 }
154
161 public function uniqueIndexFields( $uniqueIndexFields ) {
162 if ( is_string( $uniqueIndexFields ) ) {
163 $uniqueIndexFields = [ $uniqueIndexFields ];
164 }
165 $this->uniqueIndexFields = $uniqueIndexFields;
166 return $this;
167 }
168
176 public function caller( $fname ) {
177 $this->caller = $fname;
178 return $this;
179 }
180
184 public function execute() {
185 if ( !$this->rows ) {
186 throw new UnexpectedValueException(
187 __METHOD__ . ' can\'t have empty $rows value' );
188 }
189 if ( $this->table === '' ) {
190 throw new UnexpectedValueException(
191 __METHOD__ . ' expects table not to be empty' );
192 }
193 if ( !$this->uniqueIndexFields ) {
194 throw new UnexpectedValueException(
195 __METHOD__ . ' expects unique key to be provided' );
196 }
197 $this->db->replace( $this->table, [ $this->uniqueIndexFields ], $this->rows, $this->caller );
198 }
199
210 public function getQueryInfo() {
211 $info = [
212 'table' => $this->table,
213 'rows' => $this->rows,
214 'uniqueIndexFields' => $this->uniqueIndexFields,
215 ];
216 if ( $this->caller !== __CLASS__ ) {
217 $info['caller'] = $this->caller;
218 }
219 return $info;
220 }
221}
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:48
getType()
Get the RDBMS type of the server (e.g.