Database/src/Query/QueryBuilder.php

93 lines
1.8 KiB
PHP
Raw Normal View History

2024-11-08 16:33:44 +01:00
<?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());
}
}