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
109 public function table( $table ) {
110 $this->table = $table;
111 return $this;
112 }
113
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
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().
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36
getType()
Get the RDBMS type of the server (e.g.