Skip to content

Commit

Permalink
Merge pull request #4089 from najdanovicivan/nosql/db-connection-auto…
Browse files Browse the repository at this point in the history
…-query-class

BaseConnection - Added automatic handling of query class for 3rd party drivers
  • Loading branch information
MGatner authored Jan 11, 2021
2 parents b8ca07c + 3545ca9 commit 9201f01
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
20 changes: 19 additions & 1 deletion system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ abstract class BaseConnection implements ConnectionInterface
*/
protected $aliasedTables = [];

/**
* Query Class
*
* @var string
*/
protected $queryClass = 'CodeIgniter\\Database\\Query';

//--------------------------------------------------------------------

/**
Expand All @@ -302,6 +309,13 @@ public function __construct(array $params)
{
$this->$key = $value;
}

$queryClass = str_replace('Connection', 'Query', static::class);

if (class_exists($queryClass))
{
$this->queryClass = $queryClass;
}
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -594,9 +608,13 @@ abstract protected function execute(string $sql);
* @param string $queryClass
*
* @return BaseResult|Query|false
*
* @todo BC set $queryClass default as null in 4.1
*/
public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = 'CodeIgniter\\Database\\Query')
public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = '')
{
$queryClass = $queryClass ?: $this->queryClass;

if (empty($this->connID))
{
$this->initialize();
Expand Down
6 changes: 4 additions & 2 deletions system/Test/Mock/MockConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ public function shouldReturn(string $method, $return)
* @param string $queryClass
*
* @return BaseResult|Query|false
*
* @todo BC set $queryClass default as null in 4.1
*/
public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = 'CodeIgniter\\Database\\Query')
public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = '')
{
$queryClass = str_replace('Connection', 'Query', get_class($this));
$queryClass = str_replace('Connection', 'Query', static::class);

$query = new $queryClass($this);

Expand Down

0 comments on commit 9201f01

Please sign in to comment.