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; + } +} 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 @@ +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'; } 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;