Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/refactor query builder #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 5 additions & 96 deletions src/Rxn/Orm/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rxn\Orm;

use Rxn\Orm\Builder\Command;
use Rxn\Orm\Builder\QueryParser;

abstract class Builder
Expand All @@ -21,17 +22,6 @@ abstract class Builder
*/
public $table_aliases = [];

/**
* @param string $reference
*
* @return string
*/
protected function cleanReference(string $reference): string
{
$filtered_reference = $this->filterReference($reference);
return $this->escapeReference($filtered_reference);
}

protected function cleanValue(string $value): string
{
return "'$value'";
Expand All @@ -47,67 +37,18 @@ function changeKey(&$array, $old_key, $new_key)
return array_combine($keys, $array);
}

/**
* @param string $operand
*
* @return string
*/
protected function filterReference(string $operand): string
{
$operand = preg_replace('#[\`\s]#', '', $operand);
preg_match('#[\p{L}\_\.\-\`0-9]+#', $operand, $matches);
if (isset($matches[0])) {
return $matches[0];
}
return '';
}

/**
* @param string $operand
*
* @return string
*/
protected function escapeReference(string $operand): string
{
$exploded_operand = explode('.', $operand);
if (count($exploded_operand) === 2) {
return "`{$exploded_operand[0]}`.`{$exploded_operand[1]}`";
}
return "`$operand`";
}


protected function isAssociative(array $array)
{
if ([] === $array) {
return false;
}
ksort($array);
return array_keys($array) !== range(0, count($array) - 1);
}

protected function addCommandWithModifiers($command, $modifiers, $key)
{
$this->commands[$command][$key] = $modifiers;
}

protected function addCommand($command, $value)
{
$this->commands[$command][] = $value;
}

protected function loadCommands(Builder $builder)
{
$this->commands = array_merge_recursive((array)$this->commands, (array)$builder->commands);
}

protected function loadGroupCommands(Builder $builder, $type) {
$this->commands[$type][] = $builder->commands;
protected function loadGroupCommands(Command $command, $type) {
$this->commands[$type][] = $command;
}

protected function loadBindings(Builder $builder)
protected function loadBindings(Command $command)
{
$this->bindings = array_merge((array)$this->bindings, (array)$builder->bindings);
$this->bindings = array_merge((array)$this->bindings, (array)$command->bindings);
}

protected function loadTableAliases(Builder $builder)
Expand All @@ -120,38 +61,6 @@ protected function getCommands()
return $this->commands;
}

protected function addBindings($key_values)
{
if (empty($key_values)) {
return null;
}
foreach ($key_values as $value) {
$this->addBinding($value);
}
}

protected function addBinding($value)
{
$this->bindings[] = $value;
}

protected function getOperandBindings($operand): array
{
if (is_array($operand)) {
$bindings = [];
$parsed_array = [];
if (empty($bindings)) {
foreach ($operand as $value) {
$parsed_array[] = '?';
$bindings[] = $value;
}
return ['(' . implode(",", $parsed_array) . ')', $bindings];
}
}

return ['?', [$operand]];
}

public function build() {
$parser = new QueryParser($this);
$this->rawSql = $parser->getSql();
Expand Down
22 changes: 22 additions & 0 deletions src/Rxn/Orm/Builder/Column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace Rxn\Orm\Builder;

final class Column
{
/**
* @var string
*/
public $column;

/**
* @var string|null
*/
public $alias;

public function __construct($column, $alias = null)
{
$this->column = $column;
$this->alias = $alias;
}
}
152 changes: 152 additions & 0 deletions src/Rxn/Orm/Builder/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php declare(strict_types=1);

namespace Rxn\Orm\Builder;

use Rxn\Orm\Builder\Query\On;

abstract class Command
{
/**
* @var string
*/
protected $command;

/**
* @var array
*/
protected $tables = [];

/**
* @var array
*/
protected $bindings = [];

protected function isAssociative(array $array)
{
if ($array === []) {
return false;
}
ksort($array);
return array_keys($array) !== range(0, count($array) - 1);
}

/**
* @param string $reference
*
* @return string
*/
protected function cleanReference(string $reference): string
{
$filtered_reference = $this->filterReference($reference);
//return $this->escapeReference($filtered_reference);
return $filtered_reference;
}

protected function addColumn($column, $alias = null)
{
list($column, $table, $database) = $this->listColumnTableDatabase($column);

$key = null;
if (!empty($column)) {
$key = $column;
if (!empty($alias)) {
$key = $alias;
}
}

$table = $this->addTable($table, null, $database);

$table->columns[$key] = new Column($column, $alias);
}

protected function addTable($table, $alias = null, $database = null)
{
$this->tables[$table] = new Table($table, $alias, $database);
return $this->tables[$table];
}

protected function listColumnTableDatabase($reference)
{
preg_match('#\`?(\p{L}+)\`?\.\`?(\p{L}+)\`?#', $reference, $matches);
if (isset($matches[1]) && isset($matches[2]) && !isset($matches[3])) {
$table = $matches[1];
$column = $matches[2];
return [$column, $table, null];
} elseif (isset($matches[3])) {
$database = $matches[1];
$table = $matches[2];
$column = $matches[3];
return [$column, $table, $database];
}
$column = $reference;
return [$column, null, null];
}

/**
* @param string $operand
*
* @return string
*/
protected function escapeReference(string $operand): string
{
$exploded_operands = explode('.', $operand);

$operands = [];
foreach ($exploded_operands as $exploded_operand) {
$operands[] = "`$exploded_operand`";
}
return implode(".", $operands);
}

/**
* @param string $operand
*
* @return string
*/
protected function filterReference(string $operand): string
{
$operand = preg_replace('#[\`\s]#', '', $operand);
preg_match('#[\p{L}\_\.\-\`0-9]+#', $operand, $matches);
if (isset($matches[0])) {
return $matches[0];
}
return '';
}

/**
* @param $operand
*
* @return array
*/
protected function getOperandBindings($operand): array
{
if (is_array($operand)) {
$bindings = [];
$parsed_array = [];
if (empty($bindings)) {
foreach ($operand as $value) {
$parsed_array[] = '?';
$bindings[] = $value;
}
return ['(' . implode(",", $parsed_array) . ')', $bindings];
}
}

return ['?', [$operand]];
}

protected function addBindings($key_values)
{
if (empty($key_values)) {
return null;
}
foreach ($key_values as $value) {
$this->addBinding($value);
}
}

protected function addBinding($value)
{
$this->bindings[] = $value;
}
}
Loading