Skip to content

Instantly share code, notes, and snippets.

@pyldin601
Created March 25, 2016 14:54
Show Gist options
  • Save pyldin601/2d1ec228304ae6b7a180 to your computer and use it in GitHub Desktop.
Save pyldin601/2d1ec228304ae6b7a180 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Roman Gemini <[email protected]>
* @date 25.03.2016
* @time 15:54
*/
namespace Slang\Database\Fluorite\Concrete;
use Slang\Database\Fluorite\Expressions\Column;
use Slang\Database\Fluorite\Expressions\Table;
use Slang\Database\Fluorite\Statements\SelectStatement;
use Slang\Database\Fluorite\Statements\StatementVisitor;
class MySqlStatementVisitor extends StatementVisitor
{
public function visitSelectStatement(SelectStatement $statement)
{
$expressionVisitor = new MySqlExpressionVisitor();
$sql = "SELECT ";
$sql .= $this->compileColumns(
array_map(
function (Column $column) use ($expressionVisitor) {
return $expressionVisitor->visitColumn($column);
},
$statement->getColumns()
)
);
$sql .= " FROM ";
$sql .= $this->compileTables(
array_map(
function (Table $table) use ($expressionVisitor) {
return $expressionVisitor->visitTable($table);
},
$statement->getTables()
)
);
$sql .= " WHERE ";
$sql .= $expressionVisitor->visitCriteria($statement->getCriteria());
return $sql;
}
public function visitUpdateStatement($statement)
{
// TODO: Implement visitUpdateStatement() method.
}
/**
* @param array $columns
* @return string
*/
private function compileColumns(array $columns)
{
if (count($columns) == 0) {
throw new \InvalidArgumentException("Must be at least one column in statement.");
}
return implode(',', $columns);
}
/**
* @param array $tables
* @return mixed
*/
private function compileTables(array $tables)
{
if (count($tables) == 0) {
throw new \InvalidArgumentException("Must be at least one table in statement.");
}
return implode(',', $tables);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment