Skip to content

Commit

Permalink
Implemetation of raw_join
Browse files Browse the repository at this point in the history
  • Loading branch information
moiseevigor authored and treffynnon committed Apr 26, 2014
1 parent de29638 commit b2420cb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -949,13 +949,15 @@ protected function _add_join_source($join_operator, $table, $constraint, $table_
/**
* Add a RAW JOIN source to the query
*/
public function raw_join($table, $constraint, $table_alias=null) {
public function raw_join($table, $constraint, $table_alias, $parameters = array()) {
// Add table alias if present
if (!is_null($table_alias)) {
$table_alias = $this->_quote_identifier($table_alias);
$table .= " {$table_alias}";
}

$this->_values = $parameters;

// Build the constraint
if (is_array($constraint)) {
list($first_column, $operator, $second_column) = $constraint;
Expand Down
12 changes: 12 additions & 0 deletions test/QueryBuilderPsr1Test53.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,18 @@ public function testJoinWithStringConstraint() {
$this->assertEquals($expected, ORM::getLastQuery());
}

public function testRawJoin() {
ORM::forTable('widget')->rawJoin('INNER JOIN ( SELECT * FROM `widget_handle` )', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle')->findMany();
$expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` ) `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
$this->assertEquals($expected, ORM::getLastQuery());
}

public function testRawJoinWithParameters() {
ORM::forTable('widget')->rawJoin('INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE ? AND `widget_handle`.category = ?)', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle', array('%button%', 2))->findMany();
$expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE '%button%' AND `widget_handle`.category = '2') `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
$this->assertEquals($expected, ORM::getLastQuery());
}

public function testSelectWithDistinct() {
ORM::forTable('widget')->distinct()->select('name')->findMany();
$expected = "SELECT DISTINCT `name` FROM `widget`";
Expand Down
12 changes: 12 additions & 0 deletions test/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,18 @@ public function testJoinWithStringConstraint() {
$this->assertEquals($expected, ORM::get_last_query());
}

public function testRawJoin() {
ORM::for_table('widget')->raw_join('INNER JOIN ( SELECT * FROM `widget_handle` )', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle')->find_many();
$expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` ) `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
$this->assertEquals($expected, ORM::get_last_query());
}

public function testRawJoinWithParameters() {
ORM::for_table('widget')->raw_join('INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE ? AND `widget_handle`.category = ?)', array('widget_handle.widget_id', '=', 'widget.id'), 'widget_handle', array('%button%', 2))->find_many();
$expected = "SELECT * FROM `widget` INNER JOIN ( SELECT * FROM `widget_handle` WHERE `widget_handle`.name LIKE '%button%' AND `widget_handle`.category = '2') `widget_handle` ON `widget_handle`.`widget_id` = `widget`.`id`";
$this->assertEquals($expected, ORM::get_last_query());
}

public function testSelectWithDistinct() {
ORM::for_table('widget')->distinct()->select('name')->find_many();
$expected = "SELECT DISTINCT `name` FROM `widget`";
Expand Down

0 comments on commit b2420cb

Please sign in to comment.