14 public const SORT_ASC =
'ASC';
17 public const SORT_DESC =
'DESC';
64 if ( $this->db->getType() !==
$db->
getType() ) {
65 throw new \InvalidArgumentException( __METHOD__ .
66 ' cannot switch to a database of a different type.' );
92 if ( isset( $info[
'tables'] ) ) {
95 if ( isset( $info[
'fields'] ) ) {
96 $this->
fields( $info[
'fields'] );
98 if ( isset( $info[
'conds'] ) ) {
99 $this->
where( $info[
'conds'] );
101 if ( isset( $info[
'options'] ) ) {
102 $this->
options( (array)$info[
'options'] );
104 if ( isset( $info[
'join_conds'] ) ) {
105 $this->
joinConds( (array)$info[
'join_conds'] );
107 if ( isset( $info[
'joins'] ) ) {
108 $this->
joinConds( (array)$info[
'joins'] );
128 } elseif ( is_string(
$tables ) ) {
131 throw new \InvalidArgumentException( __METHOD__ .
132 ': $tables must be a string or array' );
153 public function from( $table, $alias =
null ) {
154 return $this->
table( $table, $alias );
164 foreach (
$tables as $alias => $table ) {
165 if ( is_string( $alias ) ) {
166 $this->
table( $table, $alias );
168 $this->
table( $table );
211 public function field( $field, $alias =
null ) {
212 if ( $alias ===
null ) {
215 $this->
fields[$alias] = $field;
257 if ( is_array(
$conds ) ) {
258 foreach (
$conds as $key => $cond ) {
259 if ( is_int( $key ) ) {
260 $this->
conds[] = $cond;
261 } elseif ( isset( $this->
conds[$key] ) ) {
263 $this->
conds[] = $this->db->makeList(
264 [ $key => $cond ], IDatabase::LIST_AND );
266 $this->
conds[$key] = $cond;
314 return 'sqb' . ( $this->nextAutoAlias++ );
338 $this->
options[
'OFFSET'] = $offset;
353 $this->
options[
'LIMIT'] = $limit;
365 $this->
options[] =
'LOCK IN SHARE MODE';
377 $this->
options[] =
'FOR UPDATE';
398 $this->
options[
'MAX_EXECUTION_TIME'] = $time;
443 if ( $direction ===
null ) {
445 } elseif ( is_array(
$fields ) ) {
446 $fieldsWithDirection = [];
447 foreach (
$fields as $field ) {
448 $fieldsWithDirection[] =
"$field $direction";
450 $this->
mergeOption(
'ORDER BY', $fieldsWithDirection );
452 $this->
mergeOption(
'ORDER BY',
"$fields $direction" );
464 $value = isset( $this->
options[$name] )
465 ? (array)$this->
options[$name] : [];
466 if ( is_array( $newArrayOrValue ) ) {
467 $value = array_merge( $value, $newArrayOrValue );
469 $value[] = $newArrayOrValue;
471 $this->
options[$name] = $value;
517 } elseif ( !is_array( $this->
options[$type] ) ) {
518 throw new \UnexpectedValueException(
519 __METHOD__ .
": The $type option cannot be appended to " .
520 'because it is not an array. This may have been caused by a prior ' .
521 'call to option() or options().' );
523 if ( is_array( $value ) ) {
525 } elseif ( $this->lastAlias ===
null ) {
526 throw new \UnexpectedValueException(
527 __METHOD__ .
': Cannot append index value since there is no' .
540 $this->
options[
'EXPLAIN'] =
true;
550 $this->
options[] =
'STRAIGHT_JOIN';
560 $this->
options[] =
'SQL_BIG_RESULT';
570 $this->
options[] =
'SQL_BUFFER_RESULT';
580 $this->
options[] =
'SQL_SMALL_RESULT';
590 $this->
options[] =
'SQL_CALC_FOUND_ROWS';
602 public function option( $name, $value =
null ) {
603 if ( $value ===
null ) {
606 $this->
options[$name] = $value;
652 if ( count( $this->
fields ) !== 1 ) {
653 throw new \UnexpectedValueException(
654 __METHOD__ .
' expects the query to have only one field' );
656 $field = reset( $this->
fields );
657 return $this->db->selectField( $this->
tables, $field, $this->
conds, $this->
caller,
669 if ( count( $this->
fields ) !== 1 ) {
670 throw new \UnexpectedValueException(
671 __METHOD__ .
' expects the query to have only one field' );
673 $field = reset( $this->
fields );
674 return $this->db->selectFieldValues( $this->
tables, $field, $this->
conds, $this->
caller,
723 if ( count( $this->
fields ) === 0 ) {
725 } elseif ( count( $this->
fields ) === 1 ) {
726 return reset( $this->
fields );
728 throw new \UnexpectedValueException(
729 __METHOD__ .
' expects the query to have at most one field' );
755 if ( count( $this->
fields ) !== 1 ) {
756 throw new \UnexpectedValueException(
757 __METHOD__ .
' expects the query to have only one field' );
759 $field = reset( $this->
fields );
760 return $this->db->buildGroupConcatField( $delim, $this->
tables, $field,