Skip to content
Draft
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
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"test:integration:required": "phpunit -c phpunit-integration.xml --colors=never --fail-on-skipped"
},
"repositories": [
{
"type": "git",
"url": "https://github.com/phpnomad/db.git"
},
{
"type": "vcs",
"url": "https://github.com/woocommerce/action-scheduler"
Expand Down Expand Up @@ -43,7 +47,7 @@
"php": ">=8.0",
"phpnomad/auth": "^1.0",
"phpnomad/asset": "^1.0",
"phpnomad/db": "^2.0",
"phpnomad/db": "dev-release/2.2 as 2.2.x-dev",
"phpnomad/datastore": "^2.0",
"phpnomad/event": "^1.0",
"phpnomad/email": "^1.0",
Expand Down
43 changes: 26 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions lib/Database/ClauseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPNomad\Database\Interfaces\ClauseBuilder as ClauseBuilderInterface;
use PHPNomad\Database\Traits\WithPrependedFields;
use PHPNomad\Integrations\WordPress\Traits\CanGetDataFormats;
use wpdb;

class ClauseBuilder implements ClauseBuilderInterface
{
Expand All @@ -19,6 +20,13 @@ class ClauseBuilder implements ClauseBuilderInterface
'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'IS NULL', 'IS NOT NULL',
];

private ?wpdb $database = null;

public function __construct(?wpdb $database = null)
{
$this->database = $database;
}

/** @inheritDoc */
public function where($field, string $operator, ...$values)
{
Expand Down Expand Up @@ -149,8 +157,7 @@ protected function addCondition($field, string $operator, array $values, ?string
}

if ($preparedValues !== []) {
global $wpdb;
$condition = $wpdb->prepare($condition, ...$preparedValues);
$condition = $this->wpdb()->prepare($condition, ...$preparedValues);
if (!is_string($condition) || $condition === '') {
throw new QueryBuilderException('WordPress could not prepare a condition.');
}
Expand Down Expand Up @@ -313,4 +320,16 @@ private function validateGroup(string $logic, array $clauses): string

return $logic;
}

/** @return wpdb */
private function wpdb()
{
if ($this->database !== null) {
return $this->database;
}

global $wpdb;

return $wpdb;
}
}
47 changes: 46 additions & 1 deletion lib/Database/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use PHPNomad\Database\Exceptions\QueryBuilderException;
use PHPNomad\Database\Interfaces\ClauseBuilder;
use PHPNomad\Database\Interfaces\HasQueryTables;
use PHPNomad\Database\Interfaces\QueryBuilder as QueryBuilderInterface;
use PHPNomad\Database\Interfaces\Table;
use PHPNomad\Database\Traits\WithPrependedFields;
use PHPNomad\Integrations\WordPress\Traits\CanGetDataFormats;
use PHPNomad\Utils\Helpers\Arr;
use wpdb;

class QueryBuilder implements QueryBuilderInterface
class QueryBuilder implements QueryBuilderInterface, HasQueryTables
{
use CanGetDataFormats;
use WithPrependedFields;
Expand All @@ -20,6 +21,8 @@ class QueryBuilder implements QueryBuilderInterface

protected array $from = [];

protected array $join = [];

protected array $sql = [];

private array $preparedValues = [];
Expand All @@ -41,6 +44,18 @@ class QueryBuilder implements QueryBuilderInterface
protected ?ClauseBuilder $clauseBuilder = null;
protected array $groupBy = [];

private ?Table $rootTable = null;

/** @var list<Table> */
private array $joinTables = [];

private ?wpdb $database = null;

public function __construct(?wpdb $database = null)
{
$this->database = $database;
}

/** @inheritDoc */
public function select(string $field, string ...$fields)
{
Expand All @@ -64,6 +79,7 @@ public function select(string $field, string ...$fields)
public function from(Table $table)
{
$this->useTable($table);
$this->rootTable = $table;
$this->from = ['FROM', $table->getName(), 'AS', $table->getAlias()];

return $this;
Expand Down Expand Up @@ -99,6 +115,8 @@ public function leftJoin(Table $table, string $column, string $onColumn)
$this->join = $join;
}

$this->joinTables[] = $table;

return $this;
}

Expand All @@ -123,6 +141,8 @@ public function rightJoin(Table $table, string $column, string $onColumn)
$this->join = $join;
}

$this->joinTables[] = $table;

return $this;
}

Expand Down Expand Up @@ -274,12 +294,23 @@ public function build(): string
return $sql;
}

/** @inheritDoc */
public function getReferencedTables(): array
{
if ($this->rootTable === null) {
return [];
}

return array_merge([$this->rootTable], $this->joinTables);
}

/** @inheritDoc */
public function reset()
{
$this->select = [];
$this->clauseBuilder = null;
$this->from = [];
$this->join = [];
$this->sql = [];
$this->preparedValues = [];
$this->prepare = [];
Expand All @@ -290,6 +321,8 @@ public function reset()
$this->offset = [];
$this->orderBy = [];
$this->groupBy = [];
$this->rootTable = null;
$this->joinTables = [];

return $this;
}
Expand All @@ -303,6 +336,14 @@ public function resetClauses(string $clause, string ...$clauses)
if (isset($this->$clauseToReset)) {
$this->$clauseToReset = [];
}

if ($clauseToReset === 'from') {
$this->rootTable = null;
}

if ($clauseToReset === 'join') {
$this->joinTables = [];
}
}

return $this;
Expand Down Expand Up @@ -347,6 +388,10 @@ private function maybeAppend(string $key)
*/
private function wpdb(): wpdb
{
if ($this->database !== null) {
return $this->database;
}

global $wpdb;

return $wpdb;
Expand Down
Loading
Loading