51 if ( $this->db->getType() !==
$db->
getType() ) {
52 throw new \InvalidArgumentException( __METHOD__ .
53 ' cannot switch to a database of a different type.' );
77 if ( isset( $info[
'tables'] ) ) {
80 if ( isset( $info[
'fields'] ) ) {
81 $this->
fields( $info[
'fields'] );
83 if ( isset( $info[
'conds'] ) ) {
84 $this->
where( $info[
'conds'] );
86 if ( isset( $info[
'options'] ) ) {
87 $this->
options( (array)$info[
'options'] );
89 if ( isset( $info[
'join_conds'] ) ) {
90 $this->
joinConds( (array)$info[
'join_conds'] );
110 } elseif ( is_string(
$tables ) ) {
113 throw new \InvalidArgumentException( __METHOD__ .
114 ': $tables must be a string or array' );
135 public function from( $table, $alias =
null ) {
136 return $this->
table( $table, $alias );
146 foreach (
$tables as $alias => $table ) {
147 if ( is_string( $alias ) ) {
148 $this->
table( $table, $alias );
150 $this->
table( $table );
193 public function field( $field, $alias =
null ) {
194 if ( $alias ===
null ) {
197 $this->
fields[$alias] = $field;
239 if ( is_array(
$conds ) ) {
286 return 'sqb' . ( $this->nextAutoAlias++ );
310 $this->
options[
'OFFSET'] = $offset;
325 $this->
options[
'LIMIT'] = $limit;
337 $this->
options[] =
'LOCK IN SHARE MODE';
349 $this->
options[] =
'FOR UPDATE';
370 $this->
options[
'MAX_EXECUTION_TIME'] = $time;
414 if ( $direction ===
null ) {
416 } elseif ( is_array(
$fields ) ) {
417 $fieldsWithDirection = [];
418 foreach (
$fields as $field ) {
419 $fieldsWithDirection[] =
"$field $direction";
421 $this->
mergeOption(
'ORDER BY', $fieldsWithDirection );
423 $this->
mergeOption(
'ORDER BY',
"$fields $direction" );
435 $value = isset( $this->
options[$name] )
436 ? (array)$this->
options[$name] : [];
437 if ( is_array( $newArrayOrValue ) ) {
438 $value = array_merge( $value, $newArrayOrValue );
440 $value[] = $newArrayOrValue;
442 $this->
options[$name] = $value;
488 } elseif ( !is_array( $this->
options[$type] ) ) {
489 throw new \UnexpectedValueException(
490 __METHOD__ .
": The $type option cannot be appended to " .
491 'because it is not an array. This may have been caused by a prior ' .
492 'call to option() or options().' );
494 if ( is_array( $value ) ) {
496 } elseif ( $this->lastAlias ===
null ) {
497 throw new \UnexpectedValueException(
498 __METHOD__ .
': Cannot append index value since there is no' .
511 $this->
options[
'EXPLAIN'] =
true;
521 $this->
options[] =
'STRAIGHT_JOIN';
531 $this->
options[] =
'SQL_BIG_RESULT';
541 $this->
options[] =
'SQL_BUFFER_RESULT';
551 $this->
options[] =
'SQL_SMALL_RESULT';
561 $this->
options[] =
'SQL_CALC_FOUND_ROWS';
573 public function option( $name, $value =
null ) {
574 if ( $value ===
null ) {
577 $this->
options[$name] = $value;
623 if ( count( $this->
fields ) !== 1 ) {
624 throw new \UnexpectedValueException(
625 __METHOD__ .
' expects the query to have only one field' );
627 $field = reset( $this->
fields );
628 return $this->db->selectField( $this->
tables, $field, $this->
conds, $this->
caller,
640 if ( count( $this->
fields ) !== 1 ) {
641 throw new \UnexpectedValueException(
642 __METHOD__ .
' expects the query to have only one field' );
644 $field = reset( $this->
fields );
645 return $this->db->selectFieldValues( $this->
tables, $field, $this->
conds, $this->
caller,
694 if ( count( $this->
fields ) === 0 ) {
696 } elseif ( count( $this->
fields ) === 1 ) {
697 return reset( $this->
fields );
699 throw new \UnexpectedValueException(
700 __METHOD__ .
' expects the query to have at most one field' );
726 if ( count( $this->
fields ) !== 1 ) {
727 throw new \UnexpectedValueException(
728 __METHOD__ .
' expects the query to have only one field' );
730 $field = reset( $this->
fields );
731 return $this->db->buildGroupConcatField( $delim, $this->
tables, $field,