Skip to content

Commit 4c9e201

Browse files
committed
Merge pull request #88 from anisimovt/outerjoin
Outerjoin ability added.
2 parents 768f0a3 + f0d3ae1 commit 4c9e201

File tree

5 files changed

+132
-1
lines changed

5 files changed

+132
-1
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/SQLFullOuterJoin.class.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/***************************************************************************
3+
* Copyright (C) 2012 by Timofey A. Anisimov *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify *
6+
* it under the terms of the GNU Lesser General Public License as *
7+
* published by the Free Software Foundation; either version 3 of the *
8+
* License, or (at your option) any later version. *
9+
* *
10+
***************************************************************************/
11+
12+
/**
13+
* @ingroup OSQL
14+
**/
15+
final class SQLFullOuterJoin extends SQLBaseJoin
16+
{
17+
/**
18+
* @param Dialect $dialect
19+
* @return string
20+
*/
21+
public function toDialectString(Dialect $dialect)
22+
{
23+
return parent::baseToString($dialect, 'FULL OUTER ');
24+
}
25+
26+
}

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

doc/ChangeLog

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
2012-04-22 Georgiy T. Kutsurua
2-
2+
33
* core/OSQL/DBColumn.class.php: Fix DBColumn
44

5+
2012-04-20 Timofey A. Anisimov
6+
7+
* test/main/OsqlSelectTest.class.php,
8+
core/SQLFullOuterJoin.class.php,
9+
core/Joiner.class.php
10+
core/SelectQuery.class.php:
11+
Full outer join capability in select query
12+
513
2012-04-12 Evgeny V. Kokovikhin
614

715
* test/main/Utils/PinbaTest.class.php, main/Monitoring/PinbaClient.class.php:

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)