Created
March 25, 2016 14:54
-
-
Save pyldin601/2d1ec228304ae6b7a180 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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