Skip to content

Commit

Permalink
Query Builder Extends Pdo Query
Browse files Browse the repository at this point in the history
  • Loading branch information
roboxon committed Nov 18, 2023
1 parent c8fd8e0 commit bc07f55
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 52 deletions.
26 changes: 11 additions & 15 deletions src/database/QueryBuilder.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace GemLibrary\Database;

use GemLibrary\Database\PdoQuery;
use GemLibrary\Database\Query\Delete;
use GemLibrary\Database\Query\Insert;
use GemLibrary\Database\Query\Select;
use GemLibrary\Database\Query\Update;

class QueryBuilder
class QueryBuilder extends PdoQuery
{
public static function select(string ...$select): Select
public function __construct()
{
parent::__construct();
}

public function select(string ...$select): Select
{
return new Select($select);
}

/**
* @param string $intoTable
*/
public static function insert(string $intoTable): Insert
public function insert(string $intoTable): Insert
{
return new Insert($intoTable);
}

/**
* @param string $table
*/
public static function update(string $table): Update
public function update(string $table): Update
{
return new Update($table);
}
Expand All @@ -44,7 +40,7 @@ public static function update(string $table): Update
* @param string $table
* Delete from table
*/
public static function delete(string $table): Delete
public function delete(string $table): Delete
{
return new Delete($table);
}
Expand Down
2 changes: 1 addition & 1 deletion src/database/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@

interface QueryBuilderInterface
{
public function run(PdoQuery $pdoQuery): mixed;
public function run(): mixed;
}
10 changes: 5 additions & 5 deletions src/database/query/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

namespace GemLibrary\Database\Query;

use GemLibrary\Database\PdoQuery as DatabasePdoQuery;
use GemLibrary\Database\QueryBuilderInterface;

use GemLibrary\Database\QueryBuilderInterface;
use GemLibrary\Database\QueryBuilder;

class Delete implements QueryBuilderInterface
class Delete extends QueryBuilder implements QueryBuilderInterface
{
use WhereTrait;

Expand Down Expand Up @@ -50,9 +50,9 @@ public function __toString(): string
return $this->_query;
}

public function run(DatabasePdoQuery $queryProvider): self
public function run(): self
{
$this->result = $queryProvider->deleteQuery($this->_query, $this->arrayBindValues);
$this->result = $this->deleteQuery($this->_query, $this->arrayBindValues);

return $this;
}
Expand Down
8 changes: 4 additions & 4 deletions src/database/query/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

namespace GemLibrary\Database\Query;

use GemLibrary\Database\PdoQuery;
use GemLibrary\Database\QueryBuilderInterface;
use GemLibrary\Database\QueryBuilder;

class Insert implements QueryBuilderInterface
class Insert extends QueryBuilder implements QueryBuilderInterface
{
/**
* @var null|int
Expand Down Expand Up @@ -84,9 +84,9 @@ public function values(mixed ...$values): self
return $this;
}

public function run(PdoQuery $queryProvider): self
public function run(): self
{
$this->result = $queryProvider->insertQuery($this->_query, $this->keyValue);
$this->result = $this->insertQuery($this->_query, $this->keyValue);

return $this;
}
Expand Down
41 changes: 19 additions & 22 deletions src/database/query/Select.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace GemLibrary\Database\Query;

use GemLibrary\Database\PdoQuery;
use GemLibrary\Database\QueryBuilder;
use GemLibrary\Database\QueryBuilderInterface;
use GemLibrary\Database\QueryProvider;

class Select implements QueryBuilderInterface
class Select extends QueryBuilder implements QueryBuilderInterface
{
use LimitTrait;
use WhereTrait;
Expand Down Expand Up @@ -71,6 +59,8 @@ class Select implements QueryBuilderInterface

private ?int $offset = null;

//private QueryBuilder $queryBuilder;

/**
* @param array<mixed> $select
*/
Expand All @@ -79,6 +69,13 @@ public function __construct(array $select)
$this->fields = $select;
}

// public function setQueryBuilder(QueryBuilder $queryBuilder): self
// {
// $this->queryBuilder = $queryBuilder;

// return $this;
// }

public function __toString(): string
{
$this->query = $this->selectMaker() . implode(', ', $this->from)
Expand Down Expand Up @@ -141,24 +138,24 @@ public function leftJoin(string ...$join): self
/**
* @param QueryProvider $queryProvider
*/
public function run(PdoQuery $queryProvider): self
public function run(): self
{
$this->result = $queryProvider->selectQuery($this->query, $this->arrayBindValues);
$this->result = $this->selectQuery($this->query, $this->arrayBindValues);

return $this;
}

public function count(PdoQuery $queryProvider): self
public function count(): self
{
$this->result = $queryProvider->countQuery($this->query, $this->arrayBindValues);
$this->result = $this->countQuery($this->query, $this->arrayBindValues);

return $this;
}

public function json(PdoQuery $queryProvider): self
public function json(): self
{
$array = [];
$result = $queryProvider->selectQuery($this->query, $this->arrayBindValues);
$result = $this->selectQuery($this->query, $this->arrayBindValues);
if (\is_array($result)) {
foreach ($result as $item) {
$encoded = json_encode($item, JSON_PRETTY_PRINT);
Expand All @@ -179,10 +176,10 @@ public function json(PdoQuery $queryProvider): self
* @param object $object
* retrun array of Objects
*/
public function object(PdoQuery $queryProvider, object $object): self
public function object(object $object): self
{
$class = $object::class;
$result = $queryProvider->selectQuery($this->query, $this->arrayBindValues);
$result = $this->selectQuery($this->query, $this->arrayBindValues);
if (\is_array($result)) {
foreach ($result as $item) {
if (\is_array($item)) {
Expand Down
9 changes: 4 additions & 5 deletions src/database/query/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
*/

namespace GemLibrary\Database\Query;
use GemLibrary\Database\QueryBuilder;

use GemLibrary\Database\PdoQuery;
use GemLibrary\Database\QueryBuilderInterface;
use GemLibrary\Database\QueryProvider;

class Update implements QueryBuilderInterface
class Update extends QueryBuilder implements QueryBuilderInterface
{
use WhereTrait;

Expand Down Expand Up @@ -68,9 +67,9 @@ public function set(string $column, mixed $value): self
return $this;
}

public function run(PdoQuery $queryProvider): self
public function run(): self
{
$this->result = $queryProvider->updateQuery($this->_query, $this->arrayBindValues);
$this->result = $this->updateQuery($this->_query, $this->arrayBindValues);
return $this;
}
}

0 comments on commit bc07f55

Please sign in to comment.