* @copyright 2010-2014 Justin Swanhart and André Rothe * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) * @version SVN: $Id: SelectBuilder.php 903 2014-01-06 11:29:19Z phosco@gmx.de $ * */ require_once dirname(__FILE__) . '/../exceptions/UnableToCreateSQLException.php'; require_once dirname(__FILE__) . '/ConstantBuilder.php'; require_once dirname(__FILE__) . '/FunctionBuilder.php'; require_once dirname(__FILE__) . '/SelectExpressionBuilder.php'; require_once dirname(__FILE__) . '/SelectBracketExpressionBuilder.php'; require_once dirname(__FILE__) . '/ColumnReferenceBuilder.php'; /** * This class implements the builder for the [SELECT] field. You can overwrite * all functions to achieve another handling. * * @author André Rothe * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) * */ class SelectBuilder { protected function buildConstant($parsed) { $builder = new ConstantBuilder(); return $builder->build($parsed); } protected function buildFunction($parsed) { $builder = new FunctionBuilder(); return $builder->build($parsed); } protected function buildSelectExpression($parsed) { $builder = new SelectExpressionBuilder(); return $builder->build($parsed); } protected function buildSelectBracketExpression($parsed) { $builder = new SelectBracketExpressionBuilder(); return $builder->build($parsed); } protected function buildColRef($parsed) { $builder = new ColumnReferenceBuilder(); return $builder->build($parsed); } protected function buildReserved($parsed) { $builder = new ReservedBuilder(); return $builder->build($parsed); } /** * Returns a well-formatted delimiter string. If you don't need nice SQL, * you could simply return $parsed['delim']. * * @param array $parsed The part of the output array, which contains the current expression. * @return a string, which is added right after the expression */ protected function getDelimiter($parsed) { return ($parsed['delim'] === false ? '' : (trim($parsed['delim']) . ' ')); } public function build($parsed) { $sql = ""; foreach ($parsed as $k => $v) { $len = strlen($sql); $sql .= $this->buildColRef($v); $sql .= $this->buildSelectBracketExpression($v); $sql .= $this->buildSelectExpression($v); $sql .= $this->buildFunction($v); $sql .= $this->buildConstant($v); $sql .= $this->buildReserved($v); if ($len == strlen($sql)) { throw new UnableToCreateSQLException('SELECT', $k, $v, 'expr_type'); } $sql .= $this->getDelimiter($v); } return "SELECT " . $sql; } } ?>