Builds query.

@since 1.0.0

@access protected
@param string $table The table name.
@param array|string $columns The array of columns to select.
@param array $criteria The array of conditions.
@return string The query string.
15583
protected function _prepareQuery( $table, $columns, $criteria ) {
		$where = '1 = 1';
		$params = array();
		
		foreach ( $criteria as $column => $value ) {
			$pattern = '%s';
			if ( is_null( $value ) ) {
				$pattern = '%s AND `%s` IS NULL';
			} elseif ( is_numeric( $value ) ) {
				$pattern = '%s AND `%s` = %%d';
				$params[] = $value;
			} else {
				$pattern = '%s AND `%s` = %%s';
				$params[] = $value;
			}

			$where = sprintf( $pattern, $where, $column );
		}

		if ( is_array( $columns ) ) {
			$columns = implode( ', ', $columns );
		}

		$query = sprintf( 'SELECT %s FROM %s WHERE %s', $columns, $table, $where );
		if ( ! empty( $params ) ) {
			$query = $this->_wpdb->prepare( $query, $params );
		}

		return $query;
	}
1055581
public function select(string $column, string ...$columns)
    {
        $instance = $this->selectQueryInstance();

        $instance->selectQuery()->columns($column, ...$columns);

        return $instance;
    }
463104
protected function update(array $data = array(), array $criteria)
    {
        $set = array();
        foreach ($data as $columnName => $value) {
            $set[] = "$columnName = ?";
        }
        
        $params = array_merge(array_values($data), array_values($criteria));
        
        $sql = "UPDATE $this->_table SET " . implode(', ', $set)
                . " WHERE " . implode(' = ? AND ', array_keys($criteria)) . " = ?";
        
        $pdos = $this->_pdo->prepare($sql);
        
        $pdos->execute($params);
        
        return $pdos->statement()->rowCount();
    }
947587
public function from($table, array $columns = null)
    {
        $this->q['from']['table'] = $table;

        if (isset($columns)) {
            $this->q['from']['columns'] = $columns;
        }

        return $this;
    }
907707
protected function getCreateColumnCommentsSQLQueries(array $columns, $table)
    {
        $queries = array();

        foreach ($columns as $column) {
            if ($this->hasCustomType($column->getType()->getName()) || ($column->getComment() !== null)) {
                $queries[] = $this->getCreateColumnCommentSQLQuery($column, $table);
            }
        }

        return $queries;
    }
20577
public function search(string $table, string $columns = "*", string $condition = "1",
                           string $key = "", string $appendix = "", string $appendixkey = "") : ?array {
        $this -> normalizeSelectVals($columns, $condition);

        $key = substr($this -> quote("%".$key."%"), 1, -1);
        $appendixkey = substr($this -> quote($appendixkey), 1, -1);

        // Find number of keys
        $knum = substr_count($condition, "?");
        // Find number of appendix keys
        $aknum = substr_count($appendix, "?");

        $values = [];
        for ($i = 0; $i < $knum; $i++)
            $values[] = $key;
        for ($i = 0; $i < $aknum; $i++)
            $values[] = $appendixkey;

        return $this -> preparedStatement(
            "SELECT $columns FROM $table WHERE $condition $appendix",
            $values,
            'DB::search'
        );

    }
706752
private function getConditions($columns)
    {
        return array_filter($this->conditions, function ($condition) use ($columns) {
            return in_array($condition['column'], $columns);
        });
    }
220597
public function select($column, ...$columns) {
        $this->params = [];
        if (count($columns)) {
            array_unshift($columns, $column);
            $columnStr = implode('`,`', $columns);
            $this->sql = "SELECT `{$columnStr}` ";
        } else {
            $this->sql = "SELECT {$column} ";
        }
        return $this;
    }
132760
public function findForeignKeys ($table, $column = null)
		{
			$query = ( new  Query )
				->select('CONSTRAINT_NAME')
				->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
				->where([
					'TABLE_SCHEMA' => $this->getDbName(),
					'TABLE_NAME'   => $table
				]);
			if (!is_null($column))
				$query->andWhere([ 'COLUMN_NAME' => $column ]);

			return $query->column();
		}
1234563
public function columnsFrom($table, string $column, string ...$columns)
    {
        $this->from($table);

        list($tableAlias, $tableName) = $this->dialect()->parseTable($table);

        if (!$tableAlias) {
            $tableAlias = $tableName;
        }

        array_unshift($columns, $column);

        foreach ($columns as &$column) {
            $column = $tableAlias . '.' . $column;
        }
        unset($column);

        $this->columns(...$columns);

        return $this;
    }
525878
public function updateInDatabase($table, array $data, array $criteria = [])
    {
        $query = $this->_getDriver()->update($table, $data, $criteria);
        $parameters = array_merge(array_values($data), array_values($criteria));
        $this->debugSection('Query', $query);
        if (!empty($parameters)) {
            $this->debugSection('Parameters', $parameters);
        }
        $this->_getDriver()->executeQuery($query, $parameters);
    }
1326628
public function select($column)
    {
        $builder         = new Select($this->driver);
        $this->lastQuery = $builder;

        $columns = is_array($column) ? $column : func_get_args();

        return $builder->select($columns);
    }
1075752
public function count($column = '*', $criteria = array(),
            $return = Table::RETURN_DEFAULT) {
        $this->query = 'SELECT COUNT(' . Util::camelTo_($column) . ') as rows FROM `' . $this->name . '`';
        $this->where($criteria);

        $this->setExpectedResult($return, true);
        if ($ret = $this->execute()) {
            return ($ret) ? $ret[0]['rows'] : 0;
        }
        return 0;
    }
618059
public function buildSelect($columns, &$params, $distinct = false, $selectOption = null, $query)
    {
        $select = $distinct ? 'SELECT DISTINCT' : 'SELECT';

        if ($selectOption !== null) {
            $select .= ' ' . $selectOption;
        }

        if (empty($columns)) {
            return $select . ' *';
        }

        $columns = is_array($columns) ? $columns : [$columns];

        foreach ($columns as $i => $column) {
            if ($column instanceof Expression) {
                if (is_int($i)) {
                    $columns[$i] = $column->expression;
                }
                else {
                    $columns[$i] = $column->expression . ' AS ' . $this->db->quoteColumnName($i);
                }

                $params = array_merge($params, $column->params);
            }
            elseif ($column instanceof Query) {
                list($sql, $params) = $this->build($column, $params);

                $columns[$i] = "($sql) AS " . $this->db->quoteColumnName($i);
            }
            elseif (is_string($i)) {
                if (strpos($column, '(') === false) {
                    $column = $this->db->quoteColumnName($column);
                }

                $columns[$i] = "$column AS " . $this->db->quoteColumnName($i);
            }
            elseif (strpos($column, '(') === false) {
                if (preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_\.]+)$/', $column, $matches)) {
                    $columns[$i] = $this->db->quoteColumnName($matches[1]) . ' AS ' . $this->db->quoteColumnName($matches[2]);
                }
                else {
                    $columns[$i] = $this->db->quoteColumnName($column);
                }
            }
        }

        return $select . ' ' . implode(', ', $columns);
    }
1208000
public static function select(
        $table, $fields = array(),
        $condition = null,
        $statement = null)
    {
        $query = array('SELECT ');
        array_push($query, (empty($fields)?'*':implode(', ', $fields)));
        array_push($query, " FROM $table");
        array_push($query, (isset($condition))?" WHERE $condition":'');
        array_push($query, (isset($statement))?" $statement":'');
        return implode($query);
    }
555694
protected function select(array $columns = [])
    {
        $columns = !is_array($columns) ? func_get_args() : $columns;

        if (empty($columns)) {
            $columns = ['*'];
        }

        return static::db()->select($columns)->from($this->table());
    }
1047638
protected function compileColumns($columns, $clause)
    {
        $list = [];
        foreach ((array)$columns as $alias => $column) {
            if (is_string($column) && preg_match('/^(?:([0-9a-zA-Z$_]+)\.)?([0-9a-zA-Z$_]+)$/s', trim($column), $match)) { // for maximum performance, first check the most common case..
                // yes, the expression is just a column name: "col1" or "t1.col1"!
                $column = (!empty($match[1]) ? $this->db->quoteName($match[1]) . '.' : '') . $this->db->quoteName($match[2]);
            }
            else {
                if (is_callable($column)) {
                    $column = $column(new static($this->db));
                }

                if ($column instanceof BuilderContract) {
                    $this->putBindings($clause, $column->bindings());
                    $column = $column->toSql();
                }

                $column = $this->compileExpression($column);
            }

            if (is_string($alias)) {
                if (strncasecmp($column, 'SELECT ', 7) === 0) {
                    $column = "($column)";
                }
                $column .= ' AS ' . $this->db->quoteName($alias);
            }

            $list[] = $column;
        }

        return $list;
    }