93 lines
1.8 KiB
PHP
93 lines
1.8 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Nest\Database\Query;
|
||
|
|
||
|
use Nest\Database\Database;
|
||
|
|
||
|
/**
|
||
|
* Database query builder.
|
||
|
*/
|
||
|
class QueryBuilder
|
||
|
{
|
||
|
/**
|
||
|
* Base table of the query.
|
||
|
* @var string
|
||
|
*/
|
||
|
protected string $table;
|
||
|
|
||
|
/**
|
||
|
* Build a new query builder.
|
||
|
* @param Database $database The database on which to execute the query.
|
||
|
* @param string|null $table Base table of the query.
|
||
|
*/
|
||
|
public function __construct(protected Database $database, ?string $table = null)
|
||
|
{
|
||
|
if (!empty($table)) $this->setTable($table);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the database on which to execute the query.
|
||
|
* @return Database
|
||
|
*/
|
||
|
public function getDatabase(): Database
|
||
|
{
|
||
|
return $this->database;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Set the table to use in the query.
|
||
|
* @param string $table The table to use in the query.
|
||
|
* @return $this
|
||
|
*/
|
||
|
public function setTable(string $table): static
|
||
|
{
|
||
|
$this->table = $table;
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the table to use in the query.
|
||
|
* @return string
|
||
|
*/
|
||
|
public function getTable(): string
|
||
|
{
|
||
|
return $this->table;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a new SELECT query.
|
||
|
* @return SelectQuery A select query.
|
||
|
*/
|
||
|
public function newSelect(): SelectQuery
|
||
|
{
|
||
|
return new SelectQuery($this->getDatabase(), $this->getTable());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a new INSERT query.
|
||
|
* @return InsertQuery An INSERT query.
|
||
|
*/
|
||
|
public function newInsert(): InsertQuery
|
||
|
{
|
||
|
return new InsertQuery($this->getDatabase(), $this->getTable());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a new UPDATE query.
|
||
|
* @return UpdateQuery An UPDATE query.
|
||
|
*/
|
||
|
public function newUpdate(): UpdateQuery
|
||
|
{
|
||
|
return new UpdateQuery($this->getDatabase(), $this->getTable());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a new DELETE query.
|
||
|
* @return DeleteQuery A DELETE query.
|
||
|
*/
|
||
|
public function newDelete(): DeleteQuery
|
||
|
{
|
||
|
return new DeleteQuery($this->getDatabase(), $this->getTable());
|
||
|
}
|
||
|
}
|