From ee7865c7ae4e90f9e60e7014221b3c09f70001b4 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Thu, 7 Mar 2024 23:07:00 -0300 Subject: [PATCH] MagiQL AbstractBuilder and fix sqlsrv driver --- system/MagiQL/Builder/AbstractBuilder.php | 90 +++++++++++++++++++ .../Syntax/Adapt/SQLite3/UpdateWriter.php | 36 ++++++++ system/MagiQL/Builder/Syntax/ColumnWriter.php | 52 +---------- .../MagiQL/Builder/Syntax/WriterFactory.php | 5 +- system/MagiQL/MagiQL.php | 12 +-- system/database/Databases/Driver/SQLSRV.php | 2 +- system/database/Databases/Object/Result.php | 3 + .../Databases/Object/ResultCacheIterator.php | 3 + 8 files changed, 143 insertions(+), 60 deletions(-) create mode 100644 system/MagiQL/Builder/AbstractBuilder.php create mode 100644 system/MagiQL/Builder/Syntax/Adapt/SQLite3/UpdateWriter.php diff --git a/system/MagiQL/Builder/AbstractBuilder.php b/system/MagiQL/Builder/AbstractBuilder.php new file mode 100644 index 0000000..0a37337 --- /dev/null +++ b/system/MagiQL/Builder/AbstractBuilder.php @@ -0,0 +1,90 @@ +getWriteObj() ? $this->getWriteObj()->writeColumnName($column) : parent::writeColumnName($column); + } + + /** + * {@inheritdoc} + */ + public function writeTableName(Table $table) + { + return $this->getWriteObj() ? $this->getWriteObj()->writeTableName($table) : parent::writeTableName($table); + } + + /** + * {@inheritdoc} + */ + public function writeTableAlias($alias) + { + return $this->getWriteObj() ? $this->getWriteObj()->writeTableAlias($alias) : parent::writeTableAlias($alias); + } + + /** + * {@inheritdoc} + */ + public function writeColumnAlias($alias) + { + return $this->getWriteObj() ? $this->getWriteObj()->writeColumnAlias($alias) : parent::writeColumnAlias($alias); + } + + /** + * + * @return \Phacil\Framework\MagiQL\Api\BuilderInterface + */ + protected function getWriteObj() + { + if ($this->writerAlternative) return $this->writerAlternative; + + $writerDbType = null; + if (method_exists($this, 'getDb')) { + $writerDbType = $this->getDb()->getDBTypeId(); + } + switch ($writerDbType) { + case DatabaseDriverInterface::LIST_DB_TYPE_ID['MYSQL']: + $this->writerAlternative = new \Phacil\Framework\MagiQL\Builder\Syntax\Adapt\MySQL\ColumnTable(); + break; + + case DatabaseDriverInterface::LIST_DB_TYPE_ID['MSSQL']: + $this->writerAlternative = new \Phacil\Framework\MagiQL\Builder\Syntax\Adapt\MSSQL\ColumnTable(); + break; + + case DatabaseDriverInterface::LIST_DB_TYPE_ID['POSTGRE']: + case DatabaseDriverInterface::LIST_DB_TYPE_ID['SQLLITE3']: + $this->writerAlternative = new \Phacil\Framework\MagiQL\Builder\Syntax\Adapt\PostgreSQL\ColumnTable(); + break; + + case DatabaseDriverInterface::LIST_DB_TYPE_ID['ORACLE']: + $this->writerAlternative = new \Phacil\Framework\MagiQL\Builder\Syntax\Adapt\Oracle\ColumnTable(); + break; + + default: + return false; + break; + } + + return $this->writerAlternative; + } +} \ No newline at end of file diff --git a/system/MagiQL/Builder/Syntax/Adapt/SQLite3/UpdateWriter.php b/system/MagiQL/Builder/Syntax/Adapt/SQLite3/UpdateWriter.php new file mode 100644 index 0000000..d3d26c0 --- /dev/null +++ b/system/MagiQL/Builder/Syntax/Adapt/SQLite3/UpdateWriter.php @@ -0,0 +1,36 @@ +getValues() as $column => $value) { + $newColumn = array($column); + $column = $this->columnWriter->writeColumn(SyntaxFactory::createColumn($newColumn, null)); + + $value = $this->writer->writePlaceholderValue($value); + + $assigns[] = "$column = $value"; + } + + return \implode(', ', $assigns); + } +} diff --git a/system/MagiQL/Builder/Syntax/ColumnWriter.php b/system/MagiQL/Builder/Syntax/ColumnWriter.php index df2ab62..fe7d0a0 100644 --- a/system/MagiQL/Builder/Syntax/ColumnWriter.php +++ b/system/MagiQL/Builder/Syntax/ColumnWriter.php @@ -12,7 +12,6 @@ use Phacil\Framework\MagiQL\Api\BuilderInterface; use Phacil\Framework\MagiQL\Manipulation\Select; use Phacil\Framework\MagiQL\Syntax\Column; use Phacil\Framework\MagiQL\Syntax\SyntaxFactory; -use Phacil\Framework\Databases\Api\DriverInterface as DatabaseDriverInterface; /** * Class ColumnWriter. @@ -24,8 +23,6 @@ class ColumnWriter */ protected $writer; - private $writerAlternative; - /** * @var PlaceholderWriter */ @@ -77,7 +74,7 @@ class ColumnWriter if (\is_numeric($key)) { /* @var Column $value */ - $key = $this->getWriteObj()->writeTableName($value->getTable()); + $key = $this->writer->writeTableName($value->getTable()); } $column = $selectWriter->selectToColumn($key, $value); } @@ -139,7 +136,7 @@ class ColumnWriter public function writeColumnWithAlias(Column $column) { if (($alias = $column->getAlias()) && !$column->isAll()) { - return $this->writeColumn($column).' AS '.$this->getWriteObj()->writeColumnAlias($alias); + return $this->writeColumn($column).' AS '.$this->writer->writeColumnAlias($alias); } return $this->writeColumn($column); @@ -153,52 +150,11 @@ class ColumnWriter public function writeColumn(Column $column) { $alias = $column->getTable()->getAlias(); - $table = ($alias) ? $this->getWriteObj()->writeTableAlias($alias) : $this->getWriteObj()->writeTable($column->getTable()); + $table = ($alias) ? $this->writer->writeTableAlias($alias) : $this->writer->writeTable($column->getTable()); $columnString = (empty($table)) ? '' : "{$table}."; - $columnString .= $this->getWriteObj()->writeColumnName($column); + $columnString .= $this->writer->writeColumnName($column); return $columnString; } - - /** - * - * @return \Phacil\Framework\MagiQL\Api\BuilderInterface - */ - protected function getWriteObj() - { - if($this->writerAlternative) return $this->writerAlternative; - - $writerDbType = null; - if (method_exists($this->writer, 'getDb')) { - /** @var \Phacil\Framework\MagiQL */ - $writer = $this->writer; - $writerDbType = $writer->getDb()->getDBTypeId(); - unset($writer); - } - switch ($writerDbType) { - case DatabaseDriverInterface::LIST_DB_TYPE_ID['MYSQL']: - $this->writerAlternative = new \Phacil\Framework\MagiQL\Builder\Syntax\Adapt\MySQL\ColumnTable(); - break; - - case DatabaseDriverInterface::LIST_DB_TYPE_ID['MSSQL']: - $this->writerAlternative = new \Phacil\Framework\MagiQL\Builder\Syntax\Adapt\MSSQL\ColumnTable(); - break; - - case DatabaseDriverInterface::LIST_DB_TYPE_ID['POSTGRE']: - case DatabaseDriverInterface::LIST_DB_TYPE_ID['SQLLITE3']: - $this->writerAlternative = new \Phacil\Framework\MagiQL\Builder\Syntax\Adapt\PostgreSQL\ColumnTable(); - break; - - case DatabaseDriverInterface::LIST_DB_TYPE_ID['ORACLE']: - $this->writerAlternative = new \Phacil\Framework\MagiQL\Builder\Syntax\Adapt\Oracle\ColumnTable(); - break; - - default: - $this->writerAlternative = $this->writer; - break; - } - - return $this->writerAlternative; - } } diff --git a/system/MagiQL/Builder/Syntax/WriterFactory.php b/system/MagiQL/Builder/Syntax/WriterFactory.php index c398009..3a16db5 100644 --- a/system/MagiQL/Builder/Syntax/WriterFactory.php +++ b/system/MagiQL/Builder/Syntax/WriterFactory.php @@ -96,7 +96,6 @@ final class WriterFactory /** @var \Phacil\Framework\MagiQL $writer */ switch ($writer->getDb()->getDBTypeId()) { case DatabaseDriverInterface::LIST_DB_TYPE_ID['MYSQL']: - case DatabaseDriverInterface::LIST_DB_TYPE_ID['SQLLITE3']: case DatabaseDriverInterface::LIST_DB_TYPE_ID['MSSQL']: case DatabaseDriverInterface::LIST_DB_TYPE_ID['ORACLE']: return new UpdateWriter($writer, $placeholderWriter); @@ -106,6 +105,10 @@ final class WriterFactory return new \Phacil\Framework\MagiQL\Builder\Syntax\Adapt\PostgreSQL\UpdateWriter($writer, $placeholderWriter); break; + case DatabaseDriverInterface::LIST_DB_TYPE_ID['SQLLITE3']: + return new \Phacil\Framework\MagiQL\Builder\Syntax\Adapt\SQLite3\UpdateWriter($writer, $placeholderWriter); + break; + default: return new UpdateWriter($writer, $placeholderWriter); break; diff --git a/system/MagiQL/MagiQL.php b/system/MagiQL/MagiQL.php index 3324dbc..6169664 100644 --- a/system/MagiQL/MagiQL.php +++ b/system/MagiQL/MagiQL.php @@ -8,10 +8,10 @@ namespace Phacil\Framework; -use Phacil\Framework\MagiQL\Builder; use Phacil\Framework\Databases\Api\DriverInterface as DatabaseDriverInterface; +use Phacil\Framework\MagiQL\Builder\AbstractBuilder; -class MagiQL extends Builder { +class MagiQL extends AbstractBuilder { const SELECT_KEY = 'SELECT'; const FROM_KEY = 'FROM'; @@ -26,8 +26,6 @@ class MagiQL extends Builder { */ private $db; - private $queryObj; - /** * * @var array @@ -37,7 +35,6 @@ class MagiQL extends Builder { public function __construct(\Phacil\Framework\Api\Database $db) { $this->db = $db; - $this->queryObj = new Builder(); parent::__construct(); } @@ -101,9 +98,4 @@ class MagiQL extends Builder { return false; } - public function __call($name, $arguments = array()){ - - return call_user_func_array([$this->queryObj, $name], $arguments); - } - } \ No newline at end of file diff --git a/system/database/Databases/Driver/SQLSRV.php b/system/database/Databases/Driver/SQLSRV.php index 9e409cf..c581c6e 100644 --- a/system/database/Databases/Driver/SQLSRV.php +++ b/system/database/Databases/Driver/SQLSRV.php @@ -158,7 +158,7 @@ class SQLSRV implements DriverInterface { /** @var \Phacil\Framework\Databases\Object\ResultInterface */ $resultObj = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]); - $resultObj->setNumRows(\sqlsrv_num_rows($stmt)); + $resultObj->setNumRows(count($data)); return $resultObj; } diff --git a/system/database/Databases/Object/Result.php b/system/database/Databases/Object/Result.php index e84b663..cb229b4 100644 --- a/system/database/Databases/Object/Result.php +++ b/system/database/Databases/Object/Result.php @@ -123,6 +123,9 @@ class Result extends \ArrayIterator implements ResultInterface { * {@inheritdoc} */ public function getNumRows(){ + if(!$this->num_rows) + $this->num_rows = $this->count(); + return $this->num_rows; } diff --git a/system/database/Databases/Object/ResultCacheIterator.php b/system/database/Databases/Object/ResultCacheIterator.php index 3db3eca..db942e5 100644 --- a/system/database/Databases/Object/ResultCacheIterator.php +++ b/system/database/Databases/Object/ResultCacheIterator.php @@ -109,6 +109,9 @@ class ResultCacheIterator extends \CachingIterator implements ResultInterface { */ public function getRows() { + if (!$this->num_rows) + $this->num_rows = $this->Iterator->count(); + return $this->rows; }