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
55 public function connection( IDatabase $db ) {
56 if ( $this->db->getType() !== $db->getType() ) {
57 throw new InvalidArgumentException(
58 __METHOD__ . ' cannot switch to a database of a different type.'
59 );
60 }
61 $this->db = $db;
62 return $this;
63 }
64
68 public function queryInfo( $info ) {
69 if ( isset( $info['table'] ) ) {
70 $this->table( $info['table'] );
71 }
72 if ( isset( $info['rows'] ) ) {
73 $this->rows( $info['rows'] );
74 }
75 if ( isset( $info['uniqueIndexFields'] ) ) {
76 $this->uniqueIndexFields( (array)$info['uniqueIndexFields'] );
77 }
78 if ( isset( $info['caller'] ) ) {
79 $this->caller( $info['caller'] );
80 }
81 return $this;
82 }
83
91 public function table( $table ) {
92 $this->table = $table;
93 return $this;
94 }
95
103 public function replaceInto( string $table ) {
104 return $this->table( $table );
105 }
106
117 public function rows( array $rows ) {
118 $this->rows = array_merge( $this->rows, $rows );
119 return $this;
120 }
121
131 public function row( array $row ) {
132 $this->rows[] = $row;
133 return $this;
134 }
135
142 public function uniqueIndexFields( $uniqueIndexFields ) {
143 if ( is_string( $uniqueIndexFields ) ) {
144 $uniqueIndexFields = [ $uniqueIndexFields ];
145 }
146 $this->uniqueIndexFields = $uniqueIndexFields;
147 return $this;
148 }
149
153 public function caller( $fname ) {
154 $this->caller = $fname;
155 return $this;
156 }
157
161 public function execute(): void {
162 if ( !$this->rows ) {
163 throw new UnexpectedValueException(
164 __METHOD__ . ' can\'t have empty $rows value' );
165 }
166 if ( $this->table === '' ) {
167 throw new UnexpectedValueException(
168 __METHOD__ . ' expects table not to be empty' );
169 }
170 if ( !$this->uniqueIndexFields ) {
171 throw new UnexpectedValueException(
172 __METHOD__ . ' expects unique key to be provided' );
173 }
174 $this->db->replace( $this->table, [ $this->uniqueIndexFields ], $this->rows, $this->caller );
175 }
176
180 public function getQueryInfo() {
181 $info = [
182 'table' => $this->table,
183 'rows' => $this->rows,
184 'uniqueIndexFields' => $this->uniqueIndexFields,
185 ];
186 if ( $this->caller !== __CLASS__ ) {
187 $info['caller'] = $this->caller;
188 }
189 return $info;
190 }
191}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
Build REPLACE queries with a fluent interface.
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.$this
connection(IDatabase $db)
Change the IDatabase object the query builder is bound to.The specified IDatabase will subsequently b...
__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 the counter-part meth...
Interface to a relational database.
Definition IDatabase.php:31
getType()
Get the RDBMS type of the server (e.g.
Shared interface of all write query builders.