Skip to content

Commit

Permalink
Allow query to be imported or exported to array
Browse files Browse the repository at this point in the history
  • Loading branch information
tuananhnghiem committed Sep 9, 2019
1 parent 850474d commit ecc79a4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
3. Add options to set quotes for identifiers
4. Make default not use quotes for identifiers
5. SQLServer: Generate correct query for limit and offset
6. Query: Adding groupByRaw() method
6. Query: Add groupByRaw() method
7. Query: Add create() static function to create query from array
8. Query: Adding toArray() function to export query to array format
9. Query: Add fill() method to quickly fill query with array

## Version 1.5.0

Expand Down
58 changes: 56 additions & 2 deletions Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class Query
public $joins;
public $distinct = false;
public $unions;

public $values;

public $lock = null;

public function __construct()
Expand Down Expand Up @@ -656,4 +654,60 @@ public function __toString()
{
return $this->toSQL();
}


/**
* Check if there is sub serialized query
* and convert it to query
*/
protected function rebuildSubQueries($arr)
{
if(!gettype($arr)=="array") {
return $arr;
}

foreach($arr as $key=>$value)
{
if( gettype($value)=="array"){

if ( isset($value["type"])
&& isset($value["tables"])
&& isset($value["columns"])
&& isset($value["conditions"])
&& isset($value["groups"])
&& isset($value["orders"])
) {
$class = get_class($this);
$arr[$key] = $class::create($value);
} else {
$arr[$key] = $this->rebuildSubQueries($value);
}
}
return $arr;
}
}

public function fill($arr)
{
if ($arr!==null) {
$arr = $this->rebuildSubQueries($arr);
foreach ($arr as $key=>$value) {
if (isset($this->$key)) {
$this->$key = $value;
}
}
}
}

public static function create($arr)
{
$query = new Query;
$query->fill($arr);
return $query;
}

public function toArray()
{
return get_object_vars($this);
}
}
22 changes: 21 additions & 1 deletion tests/unit/koolreport/querybuilder/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class QueryTest extends \Codeception\Test\Unit
public function testSelectRaw()
{
$sql = DB::table('orders')->selectRaw('price * ? as price_with_tax', [1.0825])->toMySQL();
$this->assertEquals($sql,"SELECT price * 1.0825 as price_with_tax FROM orders");
$this->assertEquals($sql, "SELECT price * 1.0825 as price_with_tax FROM orders");
}

public function testCoverIdentity()
Expand Down Expand Up @@ -61,4 +61,24 @@ public function testGroupBy()
->groupByRaw('DAY(created_time)')->toMySQL(true);
$this->assertEquals($sql3, "SELECT * FROM `orders` GROUP BY DAY(created_time)");
}

public function testCreate()
{
$query = Query::create([
"type"=>"select",
"tables"=>["orders"],
]);
$sql = $query->toMySQL();
$this->assertEquals($sql, "SELECT * FROM orders");
}

public function testSerialize()
{
$st = '{"type":"select","tables":["orders",[{"type":"select","tables":["orderdetails"],"columns":[["amount"]],"conditions":[],"orders":[],"groups":[],"having":null,"limit":null,"offset":null,"joins":[],"distinct":false,"unions":[],"values":[],"lock":null},"t"]],"columns":[["name","firstName"]],"conditions":[],"orders":[],"groups":["name"],"having":null,"limit":null,"offset":null,"joins":[["JOIN","customers",{"type":"select","tables":[],"columns":[],"conditions":[["customerId","=","[{colName}]id"]],"orders":[],"groups":[],"having":null,"limit":null,"offset":null,"joins":[],"distinct":false,"unions":[],"values":[],"lock":null}]],"distinct":false,"unions":[],"values":[],"lock":null}';

$query = Query::create(json_decode($st,true));
$serial = json_encode($query->toArray());
$this->assertEquals($serial, $st);
}

}

0 comments on commit ecc79a4

Please sign in to comment.