}
if ( isset( $options['HAVING'] ) ) {
$having = is_array( $options['HAVING'] )
- ? $this->makeList( $options['HAVING'], LIST_AND )
+ ? $this->makeList( $options['HAVING'], self::LIST_AND )
: $options['HAVING'];
$sql .= ' HAVING ' . $having;
}
if ( !empty( $conds ) ) {
if ( is_array( $conds ) ) {
- $conds = $this->makeList( $conds, LIST_AND );
+ $conds = $this->makeList( $conds, self::LIST_AND );
}
$sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex WHERE $conds $preLimitTail";
} else {
function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
$table = $this->tableName( $table );
$opts = $this->makeUpdateOptions( $options );
- $sql = "UPDATE $opts $table SET " . $this->makeList( $values, LIST_SET );
+ $sql = "UPDATE $opts $table SET " . $this->makeList( $values, self::LIST_SET );
if ( $conds !== [] && $conds !== '*' ) {
- $sql .= " WHERE " . $this->makeList( $conds, LIST_AND );
+ $sql .= " WHERE " . $this->makeList( $conds, self::LIST_AND );
}
return $this->query( $sql, $fname );
}
- public function makeList( $a, $mode = LIST_COMMA ) {
+ public function makeList( $a, $mode = self::LIST_COMMA ) {
if ( !is_array( $a ) ) {
throw new DBUnexpectedError( $this, __METHOD__ . ' called with incorrect parameters' );
}
foreach ( $a as $field => $value ) {
if ( !$first ) {
- if ( $mode == LIST_AND ) {
+ if ( $mode == self::LIST_AND ) {
$list .= ' AND ';
- } elseif ( $mode == LIST_OR ) {
+ } elseif ( $mode == self::LIST_OR ) {
$list .= ' OR ';
} else {
$list .= ',';
$first = false;
}
- if ( ( $mode == LIST_AND || $mode == LIST_OR ) && is_numeric( $field ) ) {
+ if ( ( $mode == self::LIST_AND || $mode == self::LIST_OR ) && is_numeric( $field ) ) {
$list .= "($value)";
- } elseif ( ( $mode == LIST_SET ) && is_numeric( $field ) ) {
+ } elseif ( $mode == self::LIST_SET && is_numeric( $field ) ) {
$list .= "$value";
- } elseif ( ( $mode == LIST_AND || $mode == LIST_OR ) && is_array( $value ) ) {
+ } elseif (
+ ( $mode == self::LIST_AND || $mode == self::LIST_OR ) && is_array( $value )
+ ) {
// Remove null from array to be handled separately if found
$includeNull = false;
foreach ( array_keys( $value, null, true ) as $nullKey ) {
unset( $value[$nullKey] );
}
if ( count( $value ) == 0 && !$includeNull ) {
- throw new InvalidArgumentException( __METHOD__ . ": empty input for field $field" );
+ throw new InvalidArgumentException(
+ __METHOD__ . ": empty input for field $field" );
} elseif ( count( $value ) == 0 ) {
// only check if $field is null
$list .= "$field IS NULL";
}
}
} elseif ( $value === null ) {
- if ( $mode == LIST_AND || $mode == LIST_OR ) {
+ if ( $mode == self::LIST_AND || $mode == self::LIST_OR ) {
$list .= "$field IS ";
- } elseif ( $mode == LIST_SET ) {
+ } elseif ( $mode == self::LIST_SET ) {
$list .= "$field = ";
}
$list .= 'NULL';
} else {
- if ( $mode == LIST_AND || $mode == LIST_OR || $mode == LIST_SET ) {
+ if (
+ $mode == self::LIST_AND || $mode == self::LIST_OR || $mode == self::LIST_SET
+ ) {
$list .= "$field = ";
}
- $list .= $mode == LIST_NAMES ? $value : $this->addQuotes( $value );
+ $list .= $mode == self::LIST_NAMES ? $value : $this->addQuotes( $value );
}
}
if ( count( $sub ) ) {
$conds[] = $this->makeList(
[ $baseKey => $base, $subKey => array_keys( $sub ) ],
- LIST_AND );
+ self::LIST_AND );
}
}
if ( $conds ) {
- return $this->makeList( $conds, LIST_OR );
+ return $this->makeList( $conds, self::LIST_OR );
} else {
// Nothing to search for...
return false;
$tableClause .= ' ' . $ignore;
}
}
- $on = $this->makeList( (array)$conds, LIST_AND );
+ $on = $this->makeList( (array)$conds, self::LIST_AND );
if ( $on != '' ) {
$tableClause .= ' ON (' . $on . ')';
}
foreach ( $index as $column ) {
$rowKey[$column] = $row[$column];
}
- $clauses[] = $this->makeList( $rowKey, LIST_AND );
+ $clauses[] = $this->makeList( $rowKey, self::LIST_AND );
}
}
- $where = [ $this->makeList( $clauses, LIST_OR ) ];
+ $where = [ $this->makeList( $clauses, self::LIST_OR ) ];
} else {
$where = false;
}
$joinTable = $this->tableName( $joinTable );
$sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
if ( $conds != '*' ) {
- $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND );
+ $sql .= 'WHERE ' . $this->makeList( $conds, self::LIST_AND );
}
$sql .= ')';
if ( $conds != '*' ) {
if ( is_array( $conds ) ) {
- $conds = $this->makeList( $conds, LIST_AND );
+ $conds = $this->makeList( $conds, self::LIST_AND );
}
$sql .= ' WHERE ' . $conds;
}
if ( $conds != '*' ) {
if ( is_array( $conds ) ) {
- $conds = $this->makeList( $conds, LIST_AND );
+ $conds = $this->makeList( $conds, self::LIST_AND );
}
$sql .= " WHERE $conds";
}
public function conditional( $cond, $trueVal, $falseVal ) {
if ( is_array( $cond ) ) {
- $cond = $this->makeList( $cond, LIST_AND );
+ $cond = $this->makeList( $cond, self::LIST_AND );
}
return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
* @see https://www.percona.com/doc/percona-toolkit/2.1/pt-heartbeat.html
*/
protected function getHeartbeatData( array $conds ) {
- $whereSQL = $this->makeList( $conds, LIST_AND );
+ $whereSQL = $this->makeList( $conds, self::LIST_AND );
// Use ORDER BY for channel based queries since that field might not be UNIQUE.
// Note: this would use "TIMESTAMPDIFF(MICROSECOND,ts,UTC_TIMESTAMP(6))" but the
// percision field is not supported in MySQL <= 5.5.
$sql = "DELETE $delTable FROM $delTable, $joinTable WHERE $delVar=$joinVar ";
if ( $conds != '*' ) {
- $sql .= ' AND ' . $this->makeList( $conds, LIST_AND );
+ $sql .= ' AND ' . $this->makeList( $conds, self::LIST_AND );
}
return $this->query( $sql, $fname );
$rowTuples[] = '(' . $this->makeList( $row ) . ')';
}
$sql .= implode( ',', $rowTuples );
- $sql .= " ON DUPLICATE KEY UPDATE " . $this->makeList( $set, LIST_SET );
+ $sql .= " ON DUPLICATE KEY UPDATE " . $this->makeList( $set, self::LIST_SET );
return (bool)$this->query( $sql, $fname );
}
/** @var string Estimate time to apply (scanning, applying) */
const ESTIMATE_DB_APPLY = 'apply';
+ /** @var int Combine list with comma delimeters */
+ const LIST_COMMA = 0;
+ /** @var int Combine list with AND clauses */
+ const LIST_AND = 1;
+ /** @var int Convert map into a SET clause */
+ const LIST_SET = 2;
+ /** @var int Treat as field name and do not apply value escaping */
+ const LIST_NAMES = 3;
+ /** @var int Combine list with OR clauses */
+ const LIST_OR = 4;
+
/**
* A string describing the current software version, and possibly
* other details in a user-friendly way. Will be listed on Special:Version, etc.
/**
* Makes an encoded list of strings from an array
*
+ * These can be used to make conjunctions or disjunctions on SQL condition strings
+ * derived from an array (see IDatabase::select() $conds documentation).
+ *
+ * Example usage:
+ * @code
+ * $sql = $db->makeList( [
+ * 'rev_user' => $id,
+ * $db->makeList( [ 'rev_minor' => 1, 'rev_len' < 500 ], $db::LIST_OR ] )
+ * ], $db::LIST_AND );
+ * @endcode
+ * This would set $sql to "rev_user = '$id' AND (rev_minor = '1' OR rev_len < '500')"
+ *
* @param array $a Containing the data
- * @param int $mode Constant
- * - LIST_COMMA: Comma separated, no field names
- * - LIST_AND: ANDed WHERE clause (without the WHERE). See the
- * documentation for $conds in IDatabase::select().
- * - LIST_OR: ORed WHERE clause (without the WHERE)
- * - LIST_SET: Comma separated with field names, like a SET clause
- * - LIST_NAMES: Comma separated field names
+ * @param int $mode IDatabase class constant:
+ * - IDatabase::LIST_COMMA: Comma separated, no field names
+ * - IDatabase::LIST_AND: ANDed WHERE clause (without the WHERE).
+ * - IDatabase::LIST_OR: ORed WHERE clause (without the WHERE)
+ * - IDatabase::LIST_SET: Comma separated with field names, like a SET clause
+ * - IDatabase::LIST_NAMES: Comma separated field names
* @throws DBError
* @return string
*/
- public function makeList( $a, $mode = LIST_COMMA );
+ public function makeList( $a, $mode = self::LIST_COMMA );
/**
* Build a partial where clause from a 2-d array such as used for LinkBatch.