Skip to content

Instantly share code, notes, and snippets.

@pyldin601
Created March 25, 2016 14:54
Show Gist options
  • Save pyldin601/ae1a39727781f71505fd to your computer and use it in GitHub Desktop.
Save pyldin601/ae1a39727781f71505fd to your computer and use it in GitHub Desktop.
<?php
/**
* @author Roman Gemini <[email protected]>
* @date 25.03.2016
* @time 15:56
*/
namespace Slang\Database\Fluorite\Concrete;
use Slang\Database\Fluorite\Expressions\Column;
use Slang\Database\Fluorite\Expressions\Comparison;
use Slang\Database\Fluorite\Expressions\CompositeExpression;
use Slang\Database\Fluorite\Expressions\Criteria;
use Slang\Database\Fluorite\Expressions\ExpressionVisitor;
use Slang\Database\Fluorite\Expressions\Table;
use Slang\Database\Fluorite\Expressions\Value;
class MySqlExpressionVisitor extends ExpressionVisitor
{
/**
* @param Column $column
* @return string
*/
public function visitColumn(Column $column)
{
$sql = "";
if ($column->getDatabase()) {
$sql .= "`{$column->getDatabase()}`.";
}
if ($column->getTable()) {
$sql .= "`{$column->getTable()}`";
}
$sql .= "`{$column->getColumn()}`";
if ($column->getAlias()) {
$sql .= " AS `{$column->getAlias()}`";
}
return $sql;
}
/**
* @param Comparison $comparison
* @return string
*/
public function visitComparison(Comparison $comparison)
{
$visitedLeft = $this->visit($comparison->getLeft());
$visitedRight = $this->visit($comparison->getRight());
$type = $comparison->getOperator();
return $visitedLeft . $type . $visitedRight;
}
/**
* @param CompositeExpression $compositeExpression
* @return mixed
*/
public function visitCompositeExpression(CompositeExpression $compositeExpression)
{
$visitedExpressions = [];
foreach ($compositeExpression->getExpressionList() as $expression) {
$visitedExpressions[] = $this->visit($expression);
}
return '(' . implode(
str_pad($compositeExpression->getConjunctionType(), 1, ' ', STR_PAD_BOTH),
$visitedExpressions
) . ')';
}
/**
* @param Table $table
* @return string
*/
public function visitTable(Table $table)
{
$sql = "";
if ($table->getDatabase()) {
$sql .= "`{$table->getDatabase()}`.";
}
if ($table->getTable()) {
$sql .= "`{$table->getTable()}`";
}
if ($table->getAlias()) {
$sql .= " `{$table->getAlias()}`";
}
return $sql;
}
/**
* @param Value $value
* @return mixed
*/
public function visitValue(Value $value)
{
return $value->getValue();
}
/**
* @param Criteria $criteria
* @return mixed
*/
public function visitCriteria(Criteria $criteria)
{
return $this->visit($criteria->getExpression());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment