Skip to content

Commit c77a90c

Browse files
committed
full outer join in select query and test
1 parent c77fe16 commit c77a90c

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

core/OSQL/Joiner.class.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ public function rightJoin(SQLRightJoin $join)
7070

7171
return $this;
7272
}
73+
74+
/**
75+
* @param SQLFullOuterJoin $join
76+
* @return Joiner
77+
*/
78+
public function fullOuterJoin(SQLFullOuterJoin $join)
79+
{
80+
$this->from[] = $join;
81+
$this->tables[$join->getTable()] = true;
82+
83+
return $this;
84+
}
7385

7486
public function getFirstTable()
7587
{

core/OSQL/SelectQuery.class.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ public function rightJoin($table, LogicalObject $logic, $alias = null)
131131

132132
return $this;
133133
}
134+
135+
/**
136+
* @param $table
137+
* @param LogicalObject $logic
138+
* @param null $alias
139+
* @return SelectQuery
140+
*/
141+
public function fullOuterJoin($table, LogicalObject $logic, $alias = null)
142+
{
143+
$this->joiner->fullOuterJoin(
144+
new SQLFullOuterJoin($table, $logic, $alias)
145+
);
146+
147+
$this->aliases[$alias] = true;
148+
149+
return $this;
150+
}
134151

135152
/**
136153
* @return SelectQuery

test/main/OsqlSelectTest.class.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,73 @@ public function testSelectSubqueryGet()
5353
.'FROM "test_table"'
5454
);
5555
}
56+
57+
public function testSelectJoin()
58+
{
59+
$dialect = PostgresDialect::me();
60+
61+
$joinTypeList = array(
62+
'JOIN ' => 'join',
63+
'LEFT JOIN ' => 'leftJoin',
64+
'RIGHT JOIN ' => 'rightJoin',
65+
'FULL OUTER JOIN ' => 'fullOuterJoin'
66+
);
67+
68+
$joinExpression =
69+
Expression::eq(
70+
DBField::create('joinField', 'table1'),
71+
DBField::create('joinField', 'table2')
72+
);
73+
74+
$baseRawQuery =
75+
'SELECT '
76+
.'"table1"."field1", '
77+
.'"table2"."field2" '
78+
.'FROM "table1" ';
79+
80+
81+
foreach ($joinTypeList as $sqlJoin => $method) {
82+
$query =
83+
$this->getBaseJoinSelect()->{$method}('table2', $joinExpression);
84+
85+
$rawQuery =
86+
$baseRawQuery
87+
.$sqlJoin
88+
.'"table2" ON ("table1"."joinField" = "table2"."joinField")';
89+
90+
$this->assertEquals(
91+
$rawQuery,
92+
$query->toDialectString($dialect)
93+
);
94+
95+
$query =
96+
$this->getBaseJoinSelect()->{$method}(
97+
'table2',
98+
$joinExpression,
99+
'table2'
100+
);
101+
102+
$rawQuery =
103+
$baseRawQuery
104+
.$sqlJoin
105+
.'"table2" AS "table2" '
106+
.'ON ("table1"."joinField" = "table2"."joinField")';
107+
108+
$this->assertEquals(
109+
$rawQuery,
110+
$query->toDialectString($dialect)
111+
);
112+
}
113+
}
114+
115+
private function getBaseJoinSelect()
116+
{
117+
return
118+
OSQL::select()->
119+
from('table1')->
120+
get(DBField::create('field1', 'table1'))->
121+
get(DBField::create('field2', 'table2'));
122+
}
123+
56124
}
57125
?>

0 commit comments

Comments
 (0)