From 09dbbd96c6d9e4c62ce8ef0ee083855fb5513836 Mon Sep 17 00:00:00 2001 From: Golgote Date: Sun, 2 May 2021 11:16:06 +0200 Subject: [PATCH 1/2] Add lateral joins for postgresql --- src/Pgsql/Select.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Pgsql/Select.php b/src/Pgsql/Select.php index 4da0891..1f67afc 100644 --- a/src/Pgsql/Select.php +++ b/src/Pgsql/Select.php @@ -19,4 +19,37 @@ */ class Select extends Common\Select { + /** + * + * Adds a LATERAL JOIN to an aliased subselect and columns to the query. + * + * @param string $join The join type: inner, left, natural, etc. + * + * @param string|Select $spec If a Select + * object, use as the sub-select; if a string, the sub-select + * command string. + * + * @param string $name The alias name for the sub-select. + * + * @param string $cond Join on this condition. + * + * @param array $bind Values to bind to ?-placeholders in the condition. + * + * @return $this + * + * @throws Exception + * + */ + public function lateralJoinSubSelect($join, $spec, $name, $cond = null, array $bind = array()) + { + $join = strtoupper(ltrim("$join JOIN LATERAL")); + $this->addTableRef("$join (SELECT ...) AS", $name); + + $spec = $this->subSelect($spec, ' '); + $name = $this->quoter->quoteName($name); + $cond = $this->fixJoinCondition($cond, $bind); + + $text = rtrim("$join ($spec ) AS $name $cond"); + return $this->addJoin(' ' . $text); + } } From 6156272d02331c56e437826acf99b7d5cdf3e75d Mon Sep 17 00:00:00 2001 From: Golgote Date: Sun, 2 May 2021 12:07:31 +0200 Subject: [PATCH 2/2] No need for AS keyword --- src/Pgsql/Select.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Pgsql/Select.php b/src/Pgsql/Select.php index 1f67afc..574b9a0 100644 --- a/src/Pgsql/Select.php +++ b/src/Pgsql/Select.php @@ -49,7 +49,7 @@ public function lateralJoinSubSelect($join, $spec, $name, $cond = null, array $b $name = $this->quoter->quoteName($name); $cond = $this->fixJoinCondition($cond, $bind); - $text = rtrim("$join ($spec ) AS $name $cond"); + $text = rtrim("$join ($spec ) $name $cond"); return $this->addJoin(' ' . $text); } }