From 3cc056a8a81920b34f090c5379daa23ae6794cbb Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 20 Feb 2020 16:04:46 +0100 Subject: [PATCH 1/4] Introduce interface `CommentsInterface` --- src/CommentsInterface.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/CommentsInterface.php diff --git a/src/CommentsInterface.php b/src/CommentsInterface.php new file mode 100644 index 0000000..8740587 --- /dev/null +++ b/src/CommentsInterface.php @@ -0,0 +1,38 @@ + Date: Thu, 20 Feb 2020 16:05:01 +0100 Subject: [PATCH 2/4] Introduce trait `Comments` --- src/Comments.php | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/Comments.php diff --git a/src/Comments.php b/src/Comments.php new file mode 100644 index 0000000..4b196aa --- /dev/null +++ b/src/Comments.php @@ -0,0 +1,44 @@ +comments[] = $comment; + + return $this; + } + + public function getComments() + { + return $this->comments; + } + + public function addHint($hint) + { + $this->hints[] = $hint; + + return $this; + } + + public function getHints() + { + return $this->hints; + } +} From afbfdd097e2fbb650f20f71b30f0e43da102ea87 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 20 Feb 2020 16:05:21 +0100 Subject: [PATCH 3/4] Select: Implement interface `CommentsInterface` --- src/Select.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Select.php b/src/Select.php index d09897b..34e6874 100644 --- a/src/Select.php +++ b/src/Select.php @@ -5,10 +5,11 @@ /** * SQL SELECT query */ -class Select implements CommonTableExpressionInterface, LimitOffsetInterface, OrderByInterface, WhereInterface +class Select implements CommonTableExpressionInterface, LimitOffsetInterface, OrderByInterface, WhereInterface, CommentsInterface { use CommonTableExpression; use LimitOffset; + use Comments; use OrderBy; use Where; From eb887d36ecc224c3d35bb2d7e26cda0f301ae6b1 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 20 Feb 2020 16:05:59 +0100 Subject: [PATCH 4/4] QueryBuilder: Render comments and optimizer hints for selects --- src/QueryBuilder.php | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 7ca147a..72442ff 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -107,7 +107,12 @@ public function assembleSelect(Select $select, array &$values = []) { $sql = array_filter([ $this->buildWith($select->getWith(), $values), - $this->buildSelect($select->getColumns(), $select->getDistinct(), $values), + $this->buildSelect( + $this->buildComments($select->getComments(), $select->getHints()), + $select->getColumns(), + $select->getDistinct(), + $values + ), $this->buildFrom($select->getFrom(), $values), $this->buildJoin($select->getJoin(), $values), $this->buildWhere($select->getWhere(), $values), @@ -199,6 +204,25 @@ public function buildWith(array $with, array &$values) return ($hasRecursive ? 'WITH RECURSIVE ' : 'WITH ') . implode(', ', $ctes); } + public function buildComments(array $comments, array $hints) + { + $result = ''; + + if (! empty($comments)) { + $result = '/* ' . join(', ', $comments) . ' */'; + } + + if (! empty($hints)) { + if ($result) { + $result .= ' '; + } + + $result .= '/*+ ' . join(' ', $hints) . ' */'; + } + + return $result; + } + /** * Build the DELETE FROM part of a query * @@ -418,7 +442,7 @@ public function buildInsertColumnsAndValues(array $columns, array $insertValues, * * @return string The SELECT part of the query */ - public function buildSelect(array $columns, $distinct, array &$values) + public function buildSelect($comments, array $columns, $distinct, array &$values) { if (empty($columns)) { return ''; @@ -426,6 +450,10 @@ public function buildSelect(array $columns, $distinct, array &$values) $select = 'SELECT'; + if ($comments) { + $select .= " $comments"; + } + if ($distinct) { $select .= ' DISTINCT'; }