From 6eaa432788051962453a3c04f547bfbc5e3f3d37 Mon Sep 17 00:00:00 2001 From: "Paul M. Jones" Date: Sun, 19 Mar 2017 10:19:28 -0500 Subject: [PATCH 1/5] put quoter into common --- src/AbstractQuery.php | 1 + src/{ => Common}/Quoter.php | 4 +-- src/QueryFactory.php | 2 +- tests/QuoterTest.php | 50 ------------------------------------- 4 files changed, 4 insertions(+), 53 deletions(-) rename src/{ => Common}/Quoter.php (99%) delete mode 100644 tests/QuoterTest.php diff --git a/src/AbstractQuery.php b/src/AbstractQuery.php index 7d3cdad..6e4691c 100644 --- a/src/AbstractQuery.php +++ b/src/AbstractQuery.php @@ -9,6 +9,7 @@ namespace Aura\SqlQuery; use Aura\SqlQuery\Common\SubselectInterface; +use Aura\SqlQuery\Common\Quoter; /** * diff --git a/src/Quoter.php b/src/Common/Quoter.php similarity index 99% rename from src/Quoter.php rename to src/Common/Quoter.php index f50b3e5..d1d2a30 100644 --- a/src/Quoter.php +++ b/src/Common/Quoter.php @@ -6,7 +6,7 @@ * @license http://opensource.org/licenses/bsd-license.php BSD * */ -namespace Aura\SqlQuery; +namespace Aura\SqlQuery\Common; /** * @@ -15,7 +15,7 @@ * @package Aura.SqlQuery * */ -class Quoter +class Quoter implements QuoterInterface { /** * diff --git a/src/QueryFactory.php b/src/QueryFactory.php index 2819698..69339b2 100644 --- a/src/QueryFactory.php +++ b/src/QueryFactory.php @@ -220,7 +220,7 @@ protected function newInstance($query) protected function getQuoter() { if (! $this->quoter) { - $this->quoter = new Quoter( + $this->quoter = new Common\Quoter( $this->quote_name_prefix, $this->quote_name_suffix ); diff --git a/tests/QuoterTest.php b/tests/QuoterTest.php deleted file mode 100644 index a963ccc..0000000 --- a/tests/QuoterTest.php +++ /dev/null @@ -1,50 +0,0 @@ -quoter = new Quoter('`', '`'); - } - - public function testQuoteName() - { - // table AS alias - $actual = $this->quoter->quoteName('table AS alias'); - $this->assertSame('`table` AS `alias`', $actual); - - // table.col AS alias - $actual = $this->quoter->quoteName('table.col AS alias'); - $this->assertSame('`table`.`col` AS `alias`', $actual); - - // table alias - $actual = $this->quoter->quoteName('table alias'); - $this->assertSame('`table` `alias`', $actual); - - // table.col alias - $actual = $this->quoter->quoteName('table.col alias'); - $this->assertSame('`table`.`col` `alias`', $actual); - - // plain old identifier - $actual = $this->quoter->quoteName('table'); - $this->assertSame('`table`', $actual); - - // star - $actual = $this->quoter->quoteName('*'); - $this->assertSame('*', $actual); - - // star dot star - $actual = $this->quoter->quoteName('*.*'); - $this->assertSame('*.*', $actual); - } - - public function testQuoteNamesIn() - { - $sql = "*, *.*, f.bar, foo.bar, CONCAT('foo.bar', \"baz.dib\") AS zim"; - $actual = $this->quoter->quoteNamesIn($sql); - $expect = "*, *.*, `f`.`bar`, `foo`.`bar`, CONCAT('foo.bar', \"baz.dib\") AS `zim`"; - $this->assertSame($expect, $actual); - } -} From 6ae2cb07b1594fc227544898d8ece198a98db931 Mon Sep 17 00:00:00 2001 From: "Paul M. Jones" Date: Sun, 19 Mar 2017 10:21:38 -0500 Subject: [PATCH 2/5] actually add needed files --- src/Common/QuoterInterface.php | 76 ++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/Common/QuoterInterface.php diff --git a/src/Common/QuoterInterface.php b/src/Common/QuoterInterface.php new file mode 100644 index 0000000..55c8abb --- /dev/null +++ b/src/Common/QuoterInterface.php @@ -0,0 +1,76 @@ + Date: Sun, 19 Mar 2017 10:22:23 -0500 Subject: [PATCH 3/5] add test back --- tests/Common/QuoterTest.php | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/Common/QuoterTest.php diff --git a/tests/Common/QuoterTest.php b/tests/Common/QuoterTest.php new file mode 100644 index 0000000..c298d01 --- /dev/null +++ b/tests/Common/QuoterTest.php @@ -0,0 +1,50 @@ +quoter = new Quoter('`', '`'); + } + + public function testQuoteName() + { + // table AS alias + $actual = $this->quoter->quoteName('table AS alias'); + $this->assertSame('`table` AS `alias`', $actual); + + // table.col AS alias + $actual = $this->quoter->quoteName('table.col AS alias'); + $this->assertSame('`table`.`col` AS `alias`', $actual); + + // table alias + $actual = $this->quoter->quoteName('table alias'); + $this->assertSame('`table` `alias`', $actual); + + // table.col alias + $actual = $this->quoter->quoteName('table.col alias'); + $this->assertSame('`table`.`col` `alias`', $actual); + + // plain old identifier + $actual = $this->quoter->quoteName('table'); + $this->assertSame('`table`', $actual); + + // star + $actual = $this->quoter->quoteName('*'); + $this->assertSame('*', $actual); + + // star dot star + $actual = $this->quoter->quoteName('*.*'); + $this->assertSame('*.*', $actual); + } + + public function testQuoteNamesIn() + { + $sql = "*, *.*, f.bar, foo.bar, CONCAT('foo.bar', \"baz.dib\") AS zim"; + $actual = $this->quoter->quoteNamesIn($sql); + $expect = "*, *.*, `f`.`bar`, `foo`.`bar`, CONCAT('foo.bar', \"baz.dib\") AS `zim`"; + $this->assertSame($expect, $actual); + } +} From 2b5e8aabdcb13fd7e686c66a9afc3fc8aaee993a Mon Sep 17 00:00:00 2001 From: "Paul M. Jones" Date: Sun, 19 Mar 2017 10:31:47 -0500 Subject: [PATCH 4/5] split quoters among DBs --- src/AbstractQuery.php | 4 +-- src/Common/Quoter.php | 17 ---------- src/Mysql/Quoter.php | 25 ++++++++++++++ src/QueryFactory.php | 68 ++++++++++++------------------------- src/Sqlsrv/Quoter.php | 25 ++++++++++++++ tests/Common/QuoterTest.php | 15 ++++---- 6 files changed, 81 insertions(+), 73 deletions(-) create mode 100644 src/Mysql/Quoter.php create mode 100644 src/Sqlsrv/Quoter.php diff --git a/src/AbstractQuery.php b/src/AbstractQuery.php index 6e4691c..f404b95 100644 --- a/src/AbstractQuery.php +++ b/src/AbstractQuery.php @@ -9,7 +9,7 @@ namespace Aura\SqlQuery; use Aura\SqlQuery\Common\SubselectInterface; -use Aura\SqlQuery\Common\Quoter; +use Aura\SqlQuery\Common\QuoterInterface; /** * @@ -85,7 +85,7 @@ abstract class AbstractQuery * placeholders (@see getSeqPlaceholder()). * */ - public function __construct(Quoter $quoter, $builder, $seq_bind_prefix = '') + public function __construct(QuoterInterface $quoter, $builder, $seq_bind_prefix = '') { $this->quoter = $quoter; $this->builder = $builder; diff --git a/src/Common/Quoter.php b/src/Common/Quoter.php index d1d2a30..1c26edc 100644 --- a/src/Common/Quoter.php +++ b/src/Common/Quoter.php @@ -35,23 +35,6 @@ class Quoter implements QuoterInterface */ protected $quote_name_suffix = '"'; - /** - * - * Constructor. - * - * @param string $quote_name_prefix The prefix to use when quoting - * identifier names. - * - * @param string $quote_name_suffix The suffix to use when quoting - * identifier names. - * - */ - public function __construct($quote_name_prefix, $quote_name_suffix) - { - $this->quote_name_prefix = $quote_name_prefix; - $this->quote_name_suffix = $quote_name_suffix; - } - /** * * Returns the prefix to use when quoting identifier names. diff --git a/src/Mysql/Quoter.php b/src/Mysql/Quoter.php new file mode 100644 index 0000000..db511ca --- /dev/null +++ b/src/Mysql/Quoter.php @@ -0,0 +1,25 @@ + array('"', '"'), - 'Mysql' => array('`', '`'), - 'Pgsql' => array('"', '"'), - 'Sqlite' => array('"', '"'), - 'Sqlsrv' => array('[', ']'), - ); - - /** - * - * The quote name prefix extracted from `$quotes`. - * - * @var string - * - */ - protected $quote_name_prefix; - - /** - * - * The quote name suffix extracted from `$quotes`. - * - * @var string - * - */ - protected $quote_name_suffix; - /** * * A map of `table.col` names to last-insert-id names. @@ -83,7 +50,7 @@ class QueryFactory * * A Quoter for identifiers. * - * @param Quoter + * @param QuoterInterface * */ protected $quoter; @@ -107,14 +74,10 @@ class QueryFactory * query objects instead of db-specific ones. * */ - public function __construct( - $db, - $common = null - ) { + public function __construct($db, $common = null) + { $this->db = ucfirst(strtolower($db)); $this->common = ($common === self::COMMON); - $this->quote_name_prefix = $this->quotes[$this->db][0]; - $this->quote_name_suffix = $this->quotes[$this->db][1]; } /** @@ -205,11 +168,20 @@ protected function newInstance($query) return new $queryClass( $this->getQuoter(), - new $builderClass(), + $this->newBuilder($query), $this->newSeqBindPrefix() ); } + protected function newBuilder($query) + { + $builderClass = "Aura\SqlQuery\\{$this->db}\\{$query}Builder"; + if ($this->common || ! class_exists($builderClass)) { + $builderClass = "Aura\SqlQuery\Common\\{$query}Builder"; + } + return new $builderClass(); + } + /** * * Returns the Quoter object for queries; creates one if needed. @@ -220,15 +192,19 @@ protected function newInstance($query) protected function getQuoter() { if (! $this->quoter) { - $this->quoter = new Common\Quoter( - $this->quote_name_prefix, - $this->quote_name_suffix - ); + $this->quoter = $this->newQuoter(); } - return $this->quoter; } + protected function newQuoter() + { + $quoterClass = "Aura\SqlQuery\\{$this->db}\Quoter"; + if ($this->common || ! class_exists($quoterClass)) { + $quoterClass = "Aura\SqlQuery\Common\Quoter"; + } + return new $quoterClass(); + } /** * * Returns a new sequential-placeholder prefix for a query object. diff --git a/src/Sqlsrv/Quoter.php b/src/Sqlsrv/Quoter.php new file mode 100644 index 0000000..9fa3080 --- /dev/null +++ b/src/Sqlsrv/Quoter.php @@ -0,0 +1,25 @@ +quoter = new Quoter('`', '`'); + $this->quoter = new Quoter(); } public function testQuoteName() { // table AS alias $actual = $this->quoter->quoteName('table AS alias'); - $this->assertSame('`table` AS `alias`', $actual); + $this->assertSame('"table" AS "alias"', $actual); // table.col AS alias $actual = $this->quoter->quoteName('table.col AS alias'); - $this->assertSame('`table`.`col` AS `alias`', $actual); + $this->assertSame('"table"."col" AS "alias"', $actual); // table alias $actual = $this->quoter->quoteName('table alias'); - $this->assertSame('`table` `alias`', $actual); + $this->assertSame('"table" "alias"', $actual); // table.col alias $actual = $this->quoter->quoteName('table.col alias'); - $this->assertSame('`table`.`col` `alias`', $actual); + $this->assertSame('"table"."col" "alias"', $actual); // plain old identifier $actual = $this->quoter->quoteName('table'); - $this->assertSame('`table`', $actual); + $this->assertSame('"table"', $actual); // star $actual = $this->quoter->quoteName('*'); @@ -44,7 +43,7 @@ public function testQuoteNamesIn() { $sql = "*, *.*, f.bar, foo.bar, CONCAT('foo.bar', \"baz.dib\") AS zim"; $actual = $this->quoter->quoteNamesIn($sql); - $expect = "*, *.*, `f`.`bar`, `foo`.`bar`, CONCAT('foo.bar', \"baz.dib\") AS `zim`"; + $expect = "*, *.*, \"f\".\"bar\", \"foo\".\"bar\", CONCAT('foo.bar', \"baz.dib\") AS \"zim\""; $this->assertSame($expect, $actual); } } From 0cae715662948b41373521fe5a1136bd72111078 Mon Sep 17 00:00:00 2001 From: "Paul M. Jones" Date: Sun, 19 Mar 2017 10:32:10 -0500 Subject: [PATCH 5/5] whitespace --- src/QueryFactory.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/QueryFactory.php b/src/QueryFactory.php index 38731bd..b71ef53 100644 --- a/src/QueryFactory.php +++ b/src/QueryFactory.php @@ -205,6 +205,7 @@ protected function newQuoter() } return new $quoterClass(); } + /** * * Returns a new sequential-placeholder prefix for a query object.