Skip to content

Commit

Permalink
1. 'Date/year/month/day/time' functions were added.
Browse files Browse the repository at this point in the history
2. 'Advanced Joins' implemented (
which involves the solution for the #2 issue )

3. Licence file added

4. Some work on Aliased Tables (on from and joins)
  • Loading branch information
rexshijaku committed Jun 3, 2021
1 parent c680a83 commit ce4ce91
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 54 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Rexhep Shijaku

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
50 changes: 50 additions & 0 deletions examples/data_time.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use RexShijaku\SQLToLaravelBuilder\SQLToLaravelBuilder;

require_once dirname(__FILE__) . '/../vendor/autoload.php';

//==========================================================
$converter = new SQLToLaravelBuilder();

$sql = 'SELECT * FROM members WHERE DATE(created_at) = "2021-03-31" ';
echo $converter->convert($sql);
// prints
// DB::table('members')
// ->whereDate('created_at', '=', '2021-03-31')
// ->get();

//==========================================================

$sql = 'SELECT * FROM members WHERE YEAR(created_at) = 1991';
echo $converter->convert($sql);
// prints
// DB::table('members')
// ->whereYear('created_at', '=', 1991)
// ->get();

//==========================================================

$sql = 'SELECT * FROM members WHERE MONTH(created_at) = 12 ';
echo $converter->convert($sql);
// DB::table('members')
// ->whereMonth('created_at', '=', 12)
// ->get();

//==========================================================

$sql = 'SELECT * FROM members WHERE DAY(created_at) = 15 ';
echo $converter->convert($sql);
// prints
// DB::table('members')
// ->whereDay('created_at', '=', 15)
// ->get();

//==========================================================

$sql = 'SELECT * FROM members WHERE TIME(created_at) = "11:20:45" ';
echo $converter->convert($sql);
// prints
// DB::table('members')
// ->whereTime('created_at', '=', '11:20:45')
// ->get();
24 changes: 23 additions & 1 deletion examples/join.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,26 @@
// ->crossJoin('details')
// ->get();

//==========================================================
//===================Advanced Join Clauses========================

$sql = 'SELECT * FROM members JOIN details
ON members.id = details.members_id
AND age > 10 AND age NOT BETWEEN 10 AND 20
AND title IS NOT NULL AND NOT age > 10 AND NAME LIKE "%Jo%"
AND age NOT IN (10,20,30)
LEFT JOIN further_details fd
ON details.id = fd.details_id';
echo $converter->convert($sql);
// prints
// DB::table('members')
// ->join('details', function ($join) {
// $join->on('members.id', '=', 'details.members_id')
// ->where('age', '>', 10)
// ->whereNotBetween('age', [10, 20])
// ->whereNotNull('title')
// ->whereRaw(' NOT age > ? ', [10])
// ->where('NAME', 'LIKE', '%Jo%')
// ->whereNotIn('age', [10, 20, 30]);
// })
// ->leftJoin(DB::raw('further_details fd'), 'details.id', '=', 'fd.details_id')
// ->get();
2 changes: 2 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Options
'facade' => 'DB::',
'group' => true
);
private $supporting_fn = array('date', 'month', 'year' ,'day', 'time');

public function __construct($options)
{
Expand All @@ -31,6 +32,7 @@ public function set(): void

unset($this->options['settings']); // unset reserved
$this->options['settings']['agg'] = $this->aggregate_fn;
$this->options['settings']['fns'] = $this->supporting_fn;
}

public function get()
Expand Down
7 changes: 7 additions & 0 deletions src/builders/CriterionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ public function build(array $parts, array &$skip_bag = array())
$fn = $this->getValue($part['sep']) == 'or' ? 'orWhereRaw' : 'whereRaw';
$query_val .= '->' . $fn . '(' . $this->quote($part['field'] . ' AGAINST ' . $part['value']) . ')';
break;
case CriterionTypes::Function:
$fn = $this->getValue($part['sep']) == 'or' ? 'orWhere' : 'where';
$fn = $this->fnMerger(array($fn, $part['fn']));
$op = $part['operator'];
$inner = $this->quote($part['field']) . ',' . $this->quote(strtoupper($op)) . ',' . $this->wrapValue($part['value']['value']);
$query_val .= '->' . $fn . '(' . $inner . ')';
break;
default:
break;
}
Expand Down
51 changes: 45 additions & 6 deletions src/builders/JoinBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,56 @@ public function build(array $parts, array &$skip_bag = array())

foreach ($parts as $join) {

$condition = implode('', $join['condition_separators']);
if ($this->getValue($join['type']) !== 'join') { // left,right,cross etc
$fn = $this->fnMerger(array(strtolower($join['type']), 'join'));
} else
$fn = $this->fnMerger(array('join'));

$qb .= "->" . $fn . "(" . $this->quote($join['table']);
if (!empty($join['condition_fields'])) { // in cross e.g are empty
$qb .= "," . $this->quote($join['condition_fields'][0])
. "," . $this->quote($condition)
. "," . $this->quote($join['condition_fields'][1]);
$qb .= "->" . $fn . "(" . $this->buildRawable($join['table'], $join['table_is_raw']);
if (isset($join['on_clause']) && count($join['on_clause']) > 0) // in cross join no on_clause!
{
// everything except columns are raw !
if (count($join['on_clause']) == 1
&& $join['on_clause'][0]['type'] !== 'between'
&& $join['on_clause'][0]['raw_field'] === false
&& $join['on_clause'][0]['raw_value'] === false) {

$on_clause = $join['on_clause'][0];
$qb .= "," . $this->quote($on_clause['field'])
. "," . $this->quote(implode(' ', $on_clause['operators']))
. "," . $this->quote($on_clause['value']);
} else {

$qb .= ',' . 'function($join) {';
$qb .= '$join';

foreach ($join['on_clause'] as $on_clause) {

if ($on_clause['type'] == 'between' || $on_clause['raw_field'] || $on_clause['raw_value']) {
if (isset($on_clause['const_value']))
$on_clause['raw_value'] = !$on_clause['const_value'];
$builder = new CriterionBuilder($this->options);
$q = $builder->build(array($on_clause));
$qb .= $q;
} else {
// no raw found and not between
$operators = implode(' ', $on_clause['operators']);
$fn_parts = $on_clause['sep'] == 'and' ? array('on') : array('or', 'on');

$qb .= '->';
$qb .= $this->fnMerger($fn_parts);
$qb .= '(';

$qb .= $this->quote($on_clause['field'], $on_clause['raw_field'])
. "," . $this->quote($operators)
. "," . $this->quote($on_clause['value'],
!$on_clause['const_value'] && $on_clause['raw_value']);

$qb .= ')';
}
}
$qb .= '; }';
}
}
$qb .= ")";
}
Expand Down
10 changes: 7 additions & 3 deletions src/extractors/AbstractExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function mergeExpressionParts($parts)
return (implode('', $parts));
}

protected function getWithAlias($val)
protected function getWithAlias($val, &$is_raw)
{
if ($val['expr_type'] === 'table')
$return = $val['table']; // no alias here, if any, it will be added at the end
Expand All @@ -130,8 +130,12 @@ protected function getWithAlias($val)
$return = $val['base_expr'];
}
}
if ($this->hasAlias($val))
$return .= ' ' . $val['alias']['base_expr'];
if ($this->hasAlias($val)) {
$return .= ' ';
if ($val['alias']['as'] === false) // because Laravel escapes 'table t' expressions entirely!
$is_raw = true;
$return .= $val['alias']['base_expr'];
}
return $return;
}

Expand Down
76 changes: 65 additions & 11 deletions src/extractors/CriterionExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,16 @@ function getCriteriaParts($value, &$parts = array(), $context = CriterionContext
'value' => $res_value['value'],
'raw_field' => $res_field['is_raw'],
'raw_value' => $res_value['is_raw'],
'sep' => $logical_operator
'sep' => $logical_operator,
'const_value' => $res_value['is_const']
);
break;
case 'is':

$this->handle_outer_negation = true;

$res_field = $this->getLeft($index, $value);
$res_value = $this->getRight($index, $value, $curr_index);
$res_value = $this->getRight($index, $value, $curr_index, $context);

$operators_ = array('is');
if ($res_value['has_negation'])
Expand All @@ -102,7 +103,9 @@ function getCriteriaParts($value, &$parts = array(), $context = CriterionContext
'value' => $res_value['value'],
'raw_field' => $res_field['is_raw'],
'raw_value' => $res_value['is_raw'],
'sep' => $logical_operator); // now combine fields + operators
'sep' => $logical_operator,
'const_value' => $res_value['is_const']
); // now combine fields + operators
break;
case "between":
$btw_operators = array();
Expand Down Expand Up @@ -131,7 +134,7 @@ function getCriteriaParts($value, &$parts = array(), $context = CriterionContext
$like_operators[] = 'like';

$res_field = $this->getLeft($index, $value);
$res_val = $this->getRight($index, $value, $curr_index);
$res_val = $this->getRight($index, $value, $curr_index, $context);


$parts[] = array(
Expand All @@ -141,7 +144,8 @@ function getCriteriaParts($value, &$parts = array(), $context = CriterionContext
'value' => $res_val['value'],
'raw_field' => $res_field['is_raw'],
'raw_value' => $res_val['is_raw'],
'sep' => $logical_operator);
'sep' => $logical_operator,
'const_value' => $res_val['is_const']);
break;
case "in":

Expand All @@ -161,8 +165,8 @@ function getCriteriaParts($value, &$parts = array(), $context = CriterionContext
'raw_field' => $res_field['is_raw'],
'raw_value' => $res_val['is_raw'],
'sep' => $logical_operator,
'as_php_arr' => $res_val['value_type'] == 'in-list'
);
'as_php_arr' => $res_val['value_type'] == 'in-list',
'const_value' => $res_val['is_const']);

break;
case "not":
Expand Down Expand Up @@ -196,6 +200,37 @@ function getCriteriaParts($value, &$parts = array(), $context = CriterionContext
'sep' => $logical_operator
);

} else if (CriterionContext::Where == $context) {
$fn = $this->getValue($val['base_expr']);

if (in_array($fn, $this->options['settings']['fns'])) {

if($val['sub_tree'] !== false && $this->isRaw($val['sub_tree'][0]))
continue;

$params = ''; // params is field in this context
$this->getFnParams($val, $params);

$temp_index = $curr_index;
$curr_index = $index = ($index + 1); // move to operator
$sep = $this->getValue($value[$curr_index]['base_expr']);
$res_val = $this->getRight($index, $value, $curr_index, $context);

if ($res_val['is_raw']) {
$curr_index = $index = $temp_index;
continue;
}


$parts[] = array(
'type' => CriterionTypes::Function,
'fn' => $fn,
'field' => $params,
'value' => $res_val,
'operator' => $sep,
'sep' => $logical_operator
);
}
}

}
Expand Down Expand Up @@ -307,6 +342,8 @@ function getRight($index, $value, &$curr_index, $context = CriterionContext::Whe
$right_operator = '';
$is_raw = false;

$is_const = null;

while (!$this->isLogicalOperator($right_operator)) { // x > 2 and (until you find first logical operator keep looping)
$right_ind++;
if ($right_ind < count($value)) {
Expand All @@ -317,7 +354,10 @@ function getRight($index, $value, &$curr_index, $context = CriterionContext::Whe
$right_operator = $this->getValue($value[$right_ind]['base_expr']);
else {
$value_ .= $value[$right_ind]['base_expr'];
$is_raw = true; // if some operation is happening then the expression should not be escaped
if ($context === CriterionContext::Join) // because on x=x+5, x+5 is escaped entirely !
$is_const = false;
else
$is_raw = true; // if some operation is happening then the expression should not be escaped
}

if ($right_operator == 'not')
Expand All @@ -329,8 +369,21 @@ function getRight($index, $value, &$curr_index, $context = CriterionContext::Whe
break;
} else {
$value_type = $value[$right_ind]['expr_type'];
if ($value[$right_ind]['expr_type'] != 'const')
$is_raw = true;
if ($context === CriterionContext::Join) { // on x = y (both x,y must be column)
if ($value[$right_ind]['expr_type'] != 'colref')
$is_raw = true;

if (!isset($is_const)) {
if ($value[$right_ind]['expr_type'] == 'const')
$is_const = true;
else
$is_const = false;
}

} else {
if ($value[$right_ind]['expr_type'] != 'const')
$is_raw = true;
}

if ($value_type == 'subquery')
$value_type = 'field_only';
Expand All @@ -342,7 +395,8 @@ function getRight($index, $value, &$curr_index, $context = CriterionContext::Whe
break;
}
$curr_index = $right_ind;
return array('value' => $value_, 'has_negation' => $has_negation, 'is_raw' => $is_raw, 'value_type' => $value_type);
return array('value' => $value_, 'has_negation' => $has_negation,
'is_raw' => $is_raw, 'value_type' => $value_type, 'is_const' => $is_const);
}

private function getBetweenValue($index, $value, &$curr_index)
Expand Down
9 changes: 3 additions & 6 deletions src/extractors/FromExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ public function extract(array $value, array $parsed = array())

function extractSingle($value)
{
return array('table' => $this->getTable($value), 'is_raw' => $value[0]['expr_type'] != 'table');
}

private function getTable($value)
{
return $this->getWithAlias($value[0]);
$is_raw = $value[0]['expr_type'] != 'table';
$table = $this->getWithAlias($value[0], $is_raw);
return array('table' => $table, 'is_raw' => $is_raw);
}

}
Loading

0 comments on commit ce4ce91

Please sign in to comment.