MediaWiki/newSelectQueryBuilder

From CODECS Dev

Covered here :

Building blocks

table() / from()

From JoinGroupBase :

/**
	 * Add a single table or a single parenthesized group.
	 *
	 * @param string|JoinGroup|SelectQueryBuilder $table The unqualified name of a table,
	 *   a table name of the form "information_schema.<unquoted identifier>", a JoinGroup
	 *   containing multiple tables, or a SelectQueryBuilder representing a subquery.
	 * @param-taint $table exec_sql
	 * @param string|null $alias The table alias, or null for no alias
	 * @param-taint $alias exec_sql
	 * @return $this
	 */
	public function table( $table, $alias = null ) {
	// ...
	}

from() is just an alias.

/**
	 * Add a single table to the SELECT query. Alias for table().
	 *
	 * @param string $table The table name
	 * @param string|null $alias The table alias, or null for no alias
	 * @return $this
	 */
	public function from( $table, $alias = null ) {
		return $this->table( $table, $alias );
	}

tables()

Alternative for multiple tables.

/**
	 * Add multiple tables. It's recommended to use join() and leftJoin() instead in new code.
	 *
	 * @param string[] $tables
	 * @return $this
	 */
	public function tables( $tables ) {
		foreach ( $tables as $alias => $table ) {
			if ( is_string( $alias ) ) {
				$this->table( $table, $alias );
			} else {
				$this->table( $table );
			}
		}
		return $this;
	}

fields

select() is an alias for fields()

/**
	 * Add a field or an array of fields to the query. Each field is an SQL
	 * fragment. If the array key is non-numeric, the key is taken to be an
	 * alias for the field.
	 *
	 * @see IDatabase::select()
	 *
	 * @param string|string[] $fields
	 * @return $this
	 */
	public function fields( $fields ) {
		if ( is_array( $fields ) ) {
			$this->fields = array_merge( $this->fields, $fields );
		} else {
			$this->fields[] = $fields;
		}
		return $this;
	}

field()

single field

	/**
	 * Add a single field to the query, optionally with an alias. The field is
	 * an SQL fragment. It is unsafe to pass user input to this function.
	 *
	 * @param string $field
	 * @param string|null $alias
	 * @return $this
	 */
	public function field( $field, $alias = null ) {
		if ( $alias === null ) {
			$this->fields[] = $field;
		} else {
			$this->fields[$alias] = $field;
		}
		return $this;
	}

where()

conditions

/**
	 * Add conditions to the query. The supplied conditions will be appended
	 * to the existing conditions, separated by AND.
	 *
	 * @param string|array $conds
	 *
	 * May be either a string containing a single condition, or an array of
	 * conditions. If an array is given, the conditions constructed from each
	 * element are combined with AND.
	 *
	 * Array elements may take one of two forms:
	 *
	 *   - Elements with a numeric key are interpreted as raw SQL fragments.
	 *   - Elements with a string key are interpreted as equality conditions,
	 *     where the key is the field name.
	 *     - If the value of such an array element is a scalar (such as a
	 *       string), it will be treated as data and thus quoted appropriately.
	 *       If it is null, an IS NULL clause will be added.
	 *     - If the value is an array, an IN (...) clause will be constructed
	 *       from its non-null elements, and an IS NULL clause will be added
	 *       if null is present, such that the field may match any of the
	 *       elements in the array. The non-null elements will be quoted.
	 *
	 * Note that expressions are often DBMS-dependent in their syntax.
	 * DBMS-independent wrappers are provided for constructing several types of
	 * expression commonly used in condition queries. See:
	 *    - IDatabase::buildLike()
	 *    - IDatabase::conditional()
	 *
	 * Untrusted user input is safe in the values of string keys, however untrusted
	 * input must not be used in the array key names or in the values of numeric keys.
	 * Escaping of untrusted input used in values of numeric keys should be done via
	 * IDatabase::addQuotes()
	 *
	 * @return $this
	 */
	public function where( $conds ) {
		if ( is_array( $conds ) ) {
			foreach ( $conds as $key => $cond ) {
				if ( is_int( $key ) ) {
					$this->conds[] = $cond;
				} elseif ( isset( $this->conds[$key] ) ) {
					// @phan-suppress-previous-line PhanTypeMismatchDimFetch
					// T288882
					$this->conds[] = $this->db->makeList(
						[ $key => $cond ], IDatabase::LIST_AND );
				} else {
					$this->conds[$key] = $cond;
				}
			}
		} else {
			$this->conds[] = $conds;
		}
		return $this;
	}

join()

Same as inner join. From JoinGroupBase

public function join( $table, $alias = null, $conds = [] ) {
		$this->addJoin( 'JOIN', $table, $alias, $conds );
		return $this;
	}

leftJoin()

From JoinGroupBase

/**
	 * Left join a table or group of tables. This should be called after table().
	 *
	 * @param string|JoinGroup|SelectQueryBuilder $table The unqualified name of a table,
	 *   a table name of the form "information_schema.<unquoted identifier>", a JoinGroup
	 *   containing multiple tables, or a SelectQueryBuilder representing a subquery.
	 * @param string|null $alias The alias name, or null to automatically
	 *   generate an alias which will be unique to this builder
	 * @param string|array $conds The conditions for the ON clause
	 * @return $this
	 */
public function leftJoin( $table, $alias = null, $conds = [] ) {
		$this->addJoin( 'LEFT JOIN', $table, $alias, $conds );
		return $this;
	}

straightJoin()

orderBy()

options()

public function options( array $options ) {
		$this->options = array_merge( $this->options, $options );
		return $this;
	}

caller()

fetchResultSet()