Quotes fields, also handles aliased fields.

@see lithium\data\source\Database::fields()
@param string $alias
@param string $field
@return string The quoted field.
489064
protected function _fieldsQuote($alias, $field) {
		list($open, $close) = $this->_quotes;
		$aliasing = preg_split("/\s+as\s+/i", $field);

		if (isset($aliasing[1])) {
			list($aliasname, $fieldname) = $this->_splitFieldname($aliasing[0]);
			$alias = $aliasname ? : $alias;
			return "{$open}{$alias}{$close}.{$open}{$fieldname}{$close} as {$aliasing[1]}";
		} elseif ($alias) {
			return "{$open}{$alias}{$close}.{$open}{$field}{$close}";
		} else {
			return "{$open}{$field}{$close}";
		}
	}
879302
public function aliasFields($fields, $defaultAlias = null)
    {
        $aliased = [];
        foreach ($fields as $alias => $field) {
            if (is_numeric($alias) && is_string($field)) {
                $aliased += $this->aliasField($field, $defaultAlias);
                continue;
            }
            $aliased[$alias] = $field;
        }

        return $aliased;
    }
596645
function dateFormat($field, $format, $alias = null) {
		$alias = $alias ? (' AS "' . $this->quoteIdentifier($alias, true) . '"') : '';

		// todo: use strtok() to parse $format
		$parts = array();
		foreach (explode('-', $format) as $part) {
			$expr = false;
			switch (strtolower($part)) {
				case 'yyyy': case 'yy': case '%y':
					$expr = "DATEPART(YY, {$field})";
					break;
				case '%x':
					$expr = "(CASE WHEN DATEPART(ISOWK, {$field}) - DATEPART(WW, {$field}) > 49 THEN -1 ELSE 0 END)+DATEPART(YY, {$field})";
					break;
				case 'ww': case 'w': case '%v':
					$expr = "DATEPART(ISOWK, {$field})";
					break;
				case 'mm': case 'm': case '%m':
					$expr = "DATEPART(MM, {$field})";
					break;
				case 'dd': case 'd': case '%d':
					$expr = "DATEPART(DD, {$field})";
					break;
				default:
					$expr = "DATEPART({$part}, {$field})";
					break;
			}
			if ($expr) {
				$expr = "CAST({$expr} AS VARCHAR)";
				$length = false;

				switch ($part) {
					case 'YYYY': case 'yyyy': case '%Y':
						$length = 4;
						break;
					case 'YY': case 'yy': case '%y':
					case '%d': case 'DD': case 'dd':
					case '%m': case 'MM': case 'mm':
						$length = 2;
						break;
				}

				if ($length) {
					$expr = "RIGHT('" . str_repeat('0', $length) . "' + {$expr}, {$length})";
				}

				$parts[] = $expr;
			}
		}

		foreach ($parts as &$v)
			$v = "CAST({$v} AS VARCHAR)";
		return join("+ '-' +", $parts) . $alias;
	}
750004
public function field($field, $except = false, $tableName = '', $prefix = '', $alias = '')
    {
        if (empty($field)) {
            return $this;
        } elseif ($field instanceof Expression) {
            $this->options['field'][] = $field;
            return $this;
        }

        if (is_string($field)) {
            if (preg_match('/[\<\'\"\(]/', $field)) {
                return $this->fieldRaw($field);
            }

            $field = array_map('trim', explode(',', $field));
        }

        $projection = [];
        foreach ($field as $key => $val) {
            if (is_numeric($key)) {
                $projection[$val] = $except ? 0 : 1;
            } else {
                $projection[$key] = $val;
            }
        }

        $this->options['projection'] = $projection;

        return $this;
    }
1209847
public function andWhere($field, $operation, $value = null, $alias = self::DEFAULT_TABLE_ALIAS)
    {
        if (strpos($field, '.') > 0) {
            //monta os joins
            $this->fieldValue = [
                'value'     => $value,
                'operation' => $operation,
            ];
            $newAliasField = $this->associationQueryFields($field);
            $alias = $newAliasField['alias'];
            $field = $newAliasField['field'];
        }
        $this->queryBuilder->andWhere($this->makeExpression($field, $operation, $value, $alias));

        return $this;
    }
728855
public function fields($fields, $context) {
		$type = $context->type();
		$schema = $context->schema()->fields();
		$alias = $context->alias();

		if (!is_array($fields)) {
			return $this->_fieldsReturn($type, $context, $fields, $schema);
		}

		$context->applyStrategy($this);
		$fields = $this->_fields($fields ? : $context->fields(), $context);
		$context->map($this->_schema($context, $fields));
		$toMerge = [];

		if (isset($fields[0])) {
			foreach ($fields[0] as $val) {
				$toMerge[] = (is_object($val) && isset($val->scalar)) ? $val->scalar : $val;
			}
			unset($fields[0]);
		}

		$fields = isset($fields[$alias]) ? [$alias => $fields[$alias]] + $fields : $fields;

		foreach ($fields as $field => $value) {
			if (is_array($value)) {
				if (isset($value['*'])) {
					$toMerge[] = $this->name($field) . '.*';
					continue;
				}
				foreach ($value as $fieldname => $mode) {
					$toMerge[] = $this->_fieldsQuote($field, $fieldname);
				}
			}
		}

		return $this->_fieldsReturn($type, $context, $toMerge, $schema);
	}
453798
public function quote_identifier($field) {
		$field = str_replace('`', '``', $field);
		$parts = explode('.', $field);

		foreach (array_keys($parts) as $k) {
			$parts[$k] = '`' . $parts[$k] . '`';
		}

		return implode('.', $parts);
	}
1307310
protected function getSelectColumnAssociationSQL($field, $assoc, ClassMetadata $class, $alias = 'r')
    {
        if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) ) {
            return '';
        }

        $columnList  = array();
        $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);

        foreach ($assoc['joinColumns'] as $joinColumn) {
            $type             = null;
            $isIdentifier     = isset($assoc['id']) && $assoc['id'] === true;
            $quotedColumn     = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform);
            $resultColumnName = $this->getSQLColumnAlias($joinColumn['name']);
            $columnList[]     = $this->getSQLTableAlias($class->name, ($alias == 'r' ? '' : $alias) )
                                . '.' . $quotedColumn . ' AS ' . $resultColumnName;
            $type             = PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em);

            $this->currentPersisterContext->rsm->addMetaResult($alias, $resultColumnName, $quotedColumn, $isIdentifier, $type);
        }

        return implode(', ', $columnList);
    }
523585
public function field($field)
    {
        return $this->fieldFactory->createFromField(
            $this->getFieldData($field),
            $this->getFieldValue($field)
        );
    }
1247887
public function toString($schemas = [], $aliases = [])
    {
        $fields = $this->dialect()->names($this->_parts['fields']);
        $sql = 'SELECT' .
            $this->_buildFlags($this->_parts['flags']) .
            $this->_buildChunk($fields ?: '*') .
            $this->_buildClause('FROM', $this->dialect()->names($this->_parts['from'])) .
            $this->_buildJoins($schemas, $aliases) .
            $this->_buildClause('WHERE', $this->dialect()->conditions($this->_parts['where'], compact('schemas', 'aliases'))) .
            $this->_buildClause('GROUP BY', $this->_group()) .
            $this->_buildClause('HAVING', $this->dialect()->conditions($this->_parts['having'], compact('schemas', 'aliases'))) .
            $this->_buildOrder($aliases) .
            $this->_buildClause('LIMIT', $this->_parts['limit']);

        if ($this->_parts['lock']) {
            $sql .= $this->_buildFlag($this->_parts['lock'], $this->_parts['lock']) .
                    $this->_buildFlag('NOWAIT', $this->_parts['noWait']);
        }

        return $this->_alias ? "({$sql}) AS " . $this->dialect()->name($this->_alias) : $sql;
    }
63961
public function count(string $column = '*', string $alias = null): string
    {
        if ($alias) {
            $alias = $this->quoteName($alias);
        }

        return 'COUNT(' . $this->quoteName($column) . ')' . ($alias ? ' AS ' . $alias : '');
    }
690317
protected function quoteFields(array $fields): array
    {
        if ('"' !== $this->sEnclosure) {
            return $fields;
        }

        return array_map(function ($element) {
            return \str_replace('"', '""', $element);
        }, $fields);
    }
534126
private function createJoin(string $alias, string $field, string $joinType): string
    {
        $relationship = $alias . '.' . $field;
        $key = $relationship . '.' . $joinType;

        if (!isset($this->uniqueJoins[$key])) {
            $joinedAlias = $this->uniqueNameFactory->createAliasName($field);

            if ($joinType === 'innerJoin') {
                $this->queryBuilder->innerJoin($relationship, $joinedAlias);
            } else {
                $this->queryBuilder->leftJoin($relationship, $joinedAlias);
            }

            // TODO: For now we assume the query will always access some field on the relation, so we optimize SQL by
            // fetching those objects in a single SQL query. But this should be revisited by either exposing an option
            // to the API so the client could decide to select or not the relations, or even better to detect in GraphQL
            // query if it's actually used or not.
            $this->queryBuilder->addSelect($joinedAlias);

            $this->uniqueJoins[$key] = $joinedAlias;
        }

        return $this->uniqueJoins[$key];
    }
510964
public function getFieldByAlias($alias)
    {
        foreach ($this->_fields as $column => $fields)
        {
            foreach ($fields as $field)
            {
                if ($field->getAlias() === $alias)
                {
                    return $field;
                }
            }
        }
        return null;
    }
357808
public function from(string $table, string $alias = null, $field = null): Select
    {
        $this->from[] = [
          'table' => $table,
          'alias' => $alias
        ];

        if ($field !== null) {
            if (is_array($field)) {
                foreach ($field as $fld) {
                    $this->field(($alias ?? $table) . '.' . $fld);
                }
            } else {
                $this->field(($alias ?? $table) . '.' . $field);
            }
        }

        return $this;
    }
1071202
public function addField($field, $alias = null)
    {
        if (isset($this->addFieldStrategies[$field])) {
            $this->addFieldStrategies[$field]->addField($this->getCollection(), $field, $alias);

            return ;
        }
        parent::addField($field, $alias);
    }
855545
private function getFieldName(string $field): string
    {
        if (preg_match('/^:/', $field)) {
            return str_replace(':', '', $field);
        }

        if (false === strpos($field, '.')) {
            return $this->tableAlias . '.' . $field;
        }

        return $field;
    }