diff --git a/README.md b/README.md index 1d5995ab..f86235d6 100644 --- a/README.md +++ b/README.md @@ -88,16 +88,16 @@ create('profile', --- innerJoin(string $leftTable = null, string $rightTable = null, - string $leftColumn = null, string $rightColumn = null, $condition = EQ); + string $leftColumn = null, string $rightColumn = null, string $tableAs = null, $condition = EQ); leftJoin(string $leftTable = null, string $rightTable = null, - string $leftColumn = null, string $rightColumn = null, $condition = EQ); + string $leftColumn = null, string $rightColumn = null, string $tableAs = null, $condition = EQ); rightJoin(string $leftTable = null, string $rightTable = null, - string $leftColumn = null, string $rightColumn = null, $condition = EQ); + string $leftColumn = null, string $rightColumn = null, string $tableAs = null, $condition = EQ); fullJoin(string $leftTable = null, string $rightTable = null, - string $leftColumn = null, string $rightColumn = null, $condition = EQ); + string $leftColumn = null, string $rightColumn = null, string $tableAs = null, $condition = EQ); --- ```php @@ -179,7 +179,7 @@ foreach ($result as $row) { $result = $db->selecting('profile', 'name, email', // Conditionals can also be called, stacked with other functions like: // innerJoin(), leftJoin(), rightJoin(), fullJoin() - // as (leftTable, rightTable, leftColumn, rightColumn, equal condition), + // as (leftTable, rightTable, leftColumn, rightColumn, tableAs, equal condition), // where( eq( columns, values, _AND ), like( columns, _d ) ), // groupBy( columns ), // having( between( columns, values1, values2 ) ), diff --git a/lib/ezFunctions.php b/lib/ezFunctions.php index 2fcc98c2..3c335ded 100644 --- a/lib/ezFunctions.php +++ b/lib/ezFunctions.php @@ -370,11 +370,12 @@ function innerJoin( $rightTable = '', $leftColumn = null, $rightColumn = null, + $tableAs = null, $condition = \EQ ) { $ezQuery = \getInstance(); - return ($ezQuery instanceof DatabaseInterface) - ? $ezQuery->innerJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $condition) + return ($ezQuery instanceOf DatabaseInterface) + ? $ezQuery->innerJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition) : false; } @@ -383,11 +384,12 @@ function leftJoin( $rightTable = '', $leftColumn = null, $rightColumn = null, + $tableAs = null, $condition = \EQ ) { $ezQuery = \getInstance(); - return ($ezQuery instanceof DatabaseInterface) - ? $ezQuery->leftJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $condition) + return ($ezQuery instanceOf DatabaseInterface) + ? $ezQuery->leftJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition) : false; } @@ -396,11 +398,12 @@ function rightJoin( $rightTable = '', $leftColumn = null, $rightColumn = null, + $tableAs = null, $condition = \EQ ) { $ezQuery = \getInstance(); - return ($ezQuery instanceof DatabaseInterface) - ? $ezQuery->rightJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $condition) + return ($ezQuery instanceOf DatabaseInterface) + ? $ezQuery->rightJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition) : false; } @@ -409,11 +412,12 @@ function fullJoin( $rightTable = '', $leftColumn = null, $rightColumn = null, + $tableAs = null, $condition = \EQ ) { $ezQuery = \getInstance(); - return ($ezQuery instanceof DatabaseInterface) - ? $ezQuery->fullJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $condition) + return ($ezQuery instanceOf DatabaseInterface) + ? $ezQuery->fullJoin($leftTable, $rightTable, $leftColumn, $rightColumn, $tableAs, $condition) : false; } diff --git a/lib/ezQuery.php b/lib/ezQuery.php index c037fe7d..d0a36611 100644 --- a/lib/ezQuery.php +++ b/lib/ezQuery.php @@ -186,6 +186,7 @@ public function innerJoin( string $rightTable = null, string $leftColumn = null, string $rightColumn = null, + string $tableAs = null, $condition = \EQ ) { return $this->joining( @@ -194,6 +195,7 @@ public function innerJoin( $rightTable, $leftColumn, $rightColumn, + $tableAs, $condition ); } @@ -203,6 +205,7 @@ public function leftJoin( string $rightTable = null, string $leftColumn = null, string $rightColumn = null, + string $tableAs = null, $condition = \EQ ) { return $this->joining( @@ -211,6 +214,7 @@ public function leftJoin( $rightTable, $leftColumn, $rightColumn, + $tableAs, $condition ); } @@ -220,6 +224,7 @@ public function rightJoin( string $rightTable = null, string $leftColumn = null, string $rightColumn = null, + string $tableAs = null, $condition = \EQ ) { return $this->joining( @@ -228,6 +233,7 @@ public function rightJoin( $rightTable, $leftColumn, $rightColumn, + $tableAs, $condition ); } @@ -237,14 +243,16 @@ public function fullJoin( string $rightTable = null, string $leftColumn = null, string $rightColumn = null, + string $tableAs = null, $condition = \EQ ) { return $this->joining( 'FULL', $leftTable, $rightTable, - $$leftColumn, + $leftColumn, $rightColumn, + $tableAs, $condition ); } @@ -269,36 +277,42 @@ public function fullJoin( * * @param string $leftColumn - * @param string $rightColumn - + * @param string $tableAs - * * @param string $condition - * * @return bool|string JOIN sql statement, false for error - */ + */ private function joining( String $type = \_INNER, string $leftTable = null, string $rightTable = null, string $leftColumn = null, string $rightColumn = null, + string $tableAs = null, $condition = \EQ ) { if ( !\in_array($type, \_JOINERS) || !\in_array($condition, \_BOOLEAN) || empty($leftTable) - || empty($rightTable) || empty($columnFields) || empty($leftColumn) + || empty($rightTable) + || empty($leftColumn) ) { return false; } + if (empty($tableAs)) + $tableAs = $rightTable; + if (\is_string($leftColumn) && empty($rightColumn)) - $onCondition = ' ON ' . $leftTable . $leftColumn . ' = ' . $rightTable . $leftColumn; + $onCondition = ' ON ' . $leftTable . '.' . $leftColumn . ' = ' . $tableAs . '.' . $leftColumn; elseif ($condition !== \EQ) - $onCondition = ' ON ' . $leftTable . $leftColumn . " $condition " . $rightTable . $rightColumn; + $onCondition = ' ON ' . $leftTable . '.' . $leftColumn . ' ' . $condition . ' ' . $tableAs . '.' . $rightColumn; else - $onCondition = ' ON ' . $leftTable . $leftColumn . ' = ' . $rightTable . $rightColumn; + $onCondition = ' ON ' . $leftTable . '.' . $leftColumn . ' = ' . $tableAs . '.' . $rightColumn; - return ' ' . $type . ' JOIN ' . $rightTable . $onCondition; + return ' ' . $type . ' JOIN ' . $rightTable . ' AS ' . $tableAs . ' ' . $onCondition; } public function orderBy($orderBy, $order) diff --git a/lib/ezQueryInterface.php b/lib/ezQueryInterface.php index d0b6bc5b..08bfe5f0 100644 --- a/lib/ezQueryInterface.php +++ b/lib/ezQueryInterface.php @@ -107,6 +107,7 @@ public function having(...$having); * @param string $rightTable - * @param string $leftColumn - * @param string $rightColumn - + * @param string $tableAs - * @param string $condition - * * @return bool|string JOIN sql statement, false for error @@ -116,6 +117,7 @@ public function innerJoin( string $rightTable = null, string $leftColumn = null, string $rightColumn = null, + string $tableAs = null, $condition = \EQ ); @@ -139,6 +141,7 @@ public function innerJoin( * @param string $rightTable - * @param string $leftColumn - * @param string $rightColumn - + * @param string $tableAs - * @param string $condition - * * @return bool|string JOIN sql statement, false for error @@ -148,6 +151,7 @@ public function leftJoin( string $rightTable = null, string $leftColumn = null, string $rightColumn = null, + string $tableAs = null, $condition = \EQ ); @@ -171,6 +175,7 @@ public function leftJoin( * @param string $rightTable - * @param string $leftColumn - * @param string $rightColumn - + * @param string $tableAs - * @param string $condition - * * @return bool|string JOIN sql statement, false for error @@ -180,6 +185,7 @@ public function rightJoin( string $rightTable = null, string $leftColumn = null, string $rightColumn = null, + string $tableAs = null, $condition = \EQ ); @@ -202,6 +208,7 @@ public function rightJoin( * @param string $rightTable - * @param string $leftColumn - * @param string $rightColumn - + * @param string $tableAs - * @param string $condition - * * @return bool|string JOIN sql statement, false for error @@ -211,6 +218,7 @@ public function fullJoin( string $rightTable = null, string $leftColumn = null, string $rightColumn = null, + string $tableAs = null, $condition = \EQ ); diff --git a/tests/ezFunctionsTest.php b/tests/ezFunctionsTest.php index c0bb9fe7..e080bb21 100644 --- a/tests/ezFunctionsTest.php +++ b/tests/ezFunctionsTest.php @@ -7,7 +7,7 @@ class ezFunctionsTest extends EZTestCase { protected function setUp(): void - { + { \clearInstance(); } @@ -58,146 +58,159 @@ public function testDropColumn() public function testEq() { - $this->assertInternalType('array', eq('field', 'data')); + $this->assertIsArray(eq('field', 'data')); $this->assertArraySubset([1 => EQ], eq('field', 'data')); } public function testNeq() { - $this->assertInternalType('array', neq('field', 'data')); + $this->assertIsArray(neq('field', 'data')); $this->assertArraySubset([3 => _AND], neq('field', 'data', _AND)); } public function testNe() { - $this->assertInternalType('array', ne('field', 'data')); + $this->assertIsArray(ne('field', 'data')); $this->assertArraySubset([4 => 'extra'], ne('field', 'data', _AND, 'extra')); } public function testLt() { - $this->assertInternalType('array', lt('field', 'data')); + $this->assertIsArray(lt('field', 'data')); $this->assertArraySubset([2 => 'data'], lt('field', 'data')); } public function testLte() { - $this->assertInternalType('array', lte('field', 'data')); + $this->assertIsArray(lte('field', 'data')); $this->assertArraySubset([0 => 'field'], lte('field', 'data')); } public function testGt() { - $this->assertInternalType('array', gt('field', 'data')); + $this->assertIsArray(gt('field', 'data')); $this->assertArraySubset([0 => 'field'], gt('field', 'data')); } public function testGte() { - $this->assertInternalType('array', gte('field', 'data')); + $this->assertIsArray(gte('field', 'data')); $this->assertArraySubset([0 => 'field'], gte('field', 'data')); } public function testIsNull() { - $this->assertInternalType('array', isNull('field')); + $this->assertIsArray(isNull('field')); $this->assertArraySubset([2 => 'null'], isNull('field')); } public function testIsNotNull() { - $this->assertInternalType('array', isNotNull('field')); + $this->assertIsArray(isNotNull('field')); $this->assertArraySubset([2 => 'null'], isNotNull('field')); } public function testLike() { - $this->assertInternalType('array', like('field', 'data')); + $this->assertIsArray(like('field', 'data')); $this->assertArraySubset([2 => 'data'], like('field', 'data')); } public function testNotLike() { - $this->assertInternalType('array', notLike('field', 'data')); + $this->assertIsArray(notLike('field', 'data')); $this->assertArraySubset([2 => 'data'], notLike('field', 'data')); } public function testIn() { - $this->assertInternalType('array', in('field', 'data')); + $this->assertIsArray(in('field', 'data')); $this->assertArraySubset([8 => 'data6'], in('field', 'data', 'data1', 'data2', 'data3', 'data4', 'data5', 'data6')); } public function testNotIn() { - $this->assertInternalType('array', notIn('field', 'data')); + $this->assertIsArray(notIn('field', 'data')); $this->assertArraySubset([5 => 'data3'], notIn('field', 'data', 'data1', 'data2', 'data3', 'data4', 'data5', 'data6')); } public function testBetween() { - $this->assertInternalType('array', between('field', 'data', 'data2')); + $this->assertIsArray(between('field', 'data', 'data2')); $this->assertArraySubset([1 => _BETWEEN], between('field', 'data', 'data2')); } public function testNotBetween() { - $this->assertInternalType('array', notBetween('field', 'data', 'data2')); + $this->assertIsArray(notBetween('field', 'data', 'data2')); $this->assertArraySubset([3 => 'data2'], notBetween('field', 'data', 'data2')); } - public function testSetInstance() { + public function testSetInstance() + { $this->assertFalse(\setInstance()); $this->assertFalse(\setInstance($this)); } - public function testSelect() { + public function testSelect() + { $this->assertFalse(select('')); } - public function testSelect_into() { + public function testSelect_into() + { $this->assertFalse(select_into('field', 'data', 'data2')); } - public function testInsert_select() { + public function testInsert_select() + { $this->assertFalse(insert_select('field', 'data', 'data2')); } - public function testCreate_select() { + public function testCreate_select() + { $this->assertFalse(create_select('field', 'data', 'data2')); } - public function testWhere() { + public function testWhere() + { $this->assertFalse(where('field', 'data', 'data2')); } - public function testGroupBy() { + public function testGroupBy() + { $this->assertFalse(groupBy('')); $this->assertNotNull(groupBy('field')); } - public function testHaving() { + public function testHaving() + { $this->assertFalse(having('field', 'data', 'data2')); } - public function testOrderBy() { + public function testOrderBy() + { $this->assertFalse(orderBy('', 'data')); $this->assertNotNull(orderBy('field', 'data')); } - public function testInsert() { + public function testInsert() + { $this->assertFalse(insert('field', ['data' => 'data2'])); } - public function testUpdate() { + public function testUpdate() + { $this->assertFalse(update('field', 'data', 'data2')); } - public function testDeleting() { + public function testDeleting() + { $this->assertFalse(deleting('field', 'data', 'data2')); } - public function testReplace() { + public function testReplace() + { $this->assertFalse(replace('field', ['data' => 'data2'])); } } diff --git a/tests/ezQueryTest.php b/tests/ezQueryTest.php index 381356d1..e77ec67d 100644 --- a/tests/ezQueryTest.php +++ b/tests/ezQueryTest.php @@ -31,7 +31,7 @@ public function testHaving() $expect = $this->object->having(in('other_test', 'testing 1', 'testing 2', 'testing 3', 'testing 4', 'testing 5')); - $this->assertContains('HAVING', $expect); + $this->assertStringContainsString('HAVING', $expect); } public function testWhere() @@ -41,14 +41,14 @@ public function testWhere() $expect = $this->object->where(in('where_test', 'testing 1', 'testing 2', 'testing 3', 'testing 4', 'testing 5')); - $this->assertContains('WHERE', $expect); - $this->assertContains('IN', $expect); - $this->assertContains('(', $expect); - $this->assertContains('testing 2\'', $expect); - $this->assertContains('testing 5', $expect); - $this->assertContains(')', $expect); + $this->assertStringContainsString('WHERE', $expect); + $this->assertStringContainsString('IN', $expect); + $this->assertStringContainsString('(', $expect); + $this->assertStringContainsString('testing 2\'', $expect); + $this->assertStringContainsString('testing 5', $expect); + $this->assertStringContainsString(')', $expect); - $this->assertContains( + $this->assertStringContainsString( 'AND', $this->object->where( array('where_test', '=', 'testing 1'), @@ -57,7 +57,7 @@ public function testWhere() ); $this->object->prepareOn(); - $this->assertContains('__ez__', $this->object->where(eq('where_test', 'testing 1'))); + $this->assertStringContainsString('__ez__', $this->object->where(eq('where_test', 'testing 1'))); $this->assertFalse($this->object->where(like('where_test', 'fail'))); } diff --git a/tests/mysqli/mysqliTest.php b/tests/mysqli/mysqliTest.php index cd3daad6..87618173 100644 --- a/tests/mysqli/mysqliTest.php +++ b/tests/mysqli/mysqliTest.php @@ -545,7 +545,7 @@ public function testWhere() ) ); - $this->assertContains('WHERE where_test BETWEEN \'testing 1\' AND \'testing 2\' AND test_null IS NULL', $expect); + $this->assertStringContainsString('WHERE where_test BETWEEN \'testing 1\' AND \'testing 2\' AND test_null IS NULL', $expect); $this->assertFalse(where( array('where_test', 'bad', 'testing 1', 'or'), @@ -558,7 +558,7 @@ public function testWhere() like('test_null', 'null') ); - $this->assertContains('WHERE where_test BETWEEN ' . _TAG . ' AND ' . _TAG . ' AND test_null IS NULL', $expect); + $this->assertStringContainsString('WHERE where_test BETWEEN ' . _TAG . ' AND ' . _TAG . ' AND test_null IS NULL', $expect); } public function testQuery_prepared() diff --git a/tests/pdo/pdo_mysqlTest.php b/tests/pdo/pdo_mysqlTest.php index 21a2e88b..dc36492c 100644 --- a/tests/pdo/pdo_mysqlTest.php +++ b/tests/pdo/pdo_mysqlTest.php @@ -265,6 +265,41 @@ public function testSelecting() $this->assertEquals(0, $this->object->query('DROP TABLE unit_test')); } + public function testJoins() + { + $this->assertTrue($this->object->connect('mysql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD)); + $this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), PRIMARY KEY (ID))'); + $this->object->insert('unit_test', array('id' => '1', 'test_key' => 'testing 1')); + $this->object->insert('unit_test', array('id' => '2', 'test_key' => 'testing 2')); + $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3')); + $this->object->query('CREATE TABLE unit_test_child(child_id integer, child_test_key varchar(50), parent_id integer, PRIMARY KEY (child_id))'); + $this->object->insert('unit_test', array('child_id' => '1', 'child_test_key' => 'testing child 1', 'parent_id' => '3')); + $this->object->insert('unit_test', array('child_id' => '2', 'child_test_key' => 'testing child 2', 'parent_id' => '2')); + $this->object->insert('unit_test', array('child_id' => '3', 'child_test_key' => 'testing child 3', 'parent_id' => '1')); + + $result = $this->object->selecting('unit_test_child', '*', leftJoin('unit_test_child', 'unit_test', 'parent_id', 'id')); + $i = 1; + $o = 3; + foreach ($result as $row) { + $this->assertEquals($i, $row->child_id); + $this->assertEquals('testing child ' . $i, $row->child_test_key); + $this->assertEquals($o, $row->id); + $this->assertEquals('testing ' . $o, $row->test_key); + ++$i; + --$o; + } + + $result = $this->object->selecting('unit_test_child', 'child.parent_id', leftJoin('unit_test_child', 'unit_test', 'parent_id', 'id', 'child')); + $o = 3; + foreach ($result as $row) { + $this->assertEquals($o, $row->parent_id); + --$o; + } + + $this->assertEquals(0, $this->object->query('DROP TABLE unit_test')); + $this->assertEquals(0, $this->object->query('DROP TABLE unit_test_child')); + } + public function testBeginTransactionCommit() { $this->object->connect(); diff --git a/tests/pdo/pdo_pgsqlTest.php b/tests/pdo/pdo_pgsqlTest.php index add4d5c1..414b6f06 100644 --- a/tests/pdo/pdo_pgsqlTest.php +++ b/tests/pdo/pdo_pgsqlTest.php @@ -189,6 +189,38 @@ public function testSelecting() } } + public function testJoins() + { + $this->assertTrue($this->object->connect('pgsql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD)); + $this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), PRIMARY KEY (ID))'); + $this->object->insert('unit_test', array('id' => '1', 'test_key' => 'testing 1')); + $this->object->insert('unit_test', array('id' => '2', 'test_key' => 'testing 2')); + $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3')); + $this->object->query('CREATE TABLE unit_test_child(child_id integer, child_test_key varchar(50), parent_id integer, PRIMARY KEY (child_id))'); + $this->object->insert('unit_test', array('child_id' => '1', 'child_test_key' => 'testing child 1', 'parent_id' => '3')); + $this->object->insert('unit_test', array('child_id' => '2', 'child_test_key' => 'testing child 2', 'parent_id' => '2')); + $this->object->insert('unit_test', array('child_id' => '3', 'child_test_key' => 'testing child 3', 'parent_id' => '1')); + + $result = $this->object->selecting('unit_test_child', '*', leftJoin('unit_test_child', 'unit_test', 'parent_id', 'id')); + $i = 1; + $o = 3; + foreach ($result as $row) { + $this->assertEquals($i, $row->child_id); + $this->assertEquals('testing child ' . $i, $row->child_test_key); + $this->assertEquals($o, $row->id); + $this->assertEquals('testing ' . $o, $row->test_key); + ++$i; + --$o; + } + + $result = $this->object->selecting('unit_test_child', 'child.parent_id', leftJoin('unit_test_child', 'unit_test', 'parent_id', 'id', 'child')); + $o = 3; + foreach ($result as $row) { + $this->assertEquals($o, $row->parent_id); + --$o; + } + } + public function testPosgreSQLDisconnect() { $this->assertTrue($this->object->connect('pgsql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD)); diff --git a/tests/pdo/pdo_sqliteTest.php b/tests/pdo/pdo_sqliteTest.php index 90c805ac..ea639af2 100644 --- a/tests/pdo/pdo_sqliteTest.php +++ b/tests/pdo/pdo_sqliteTest.php @@ -216,6 +216,41 @@ public function testSelecting() $this->assertEquals(1, $this->object->query('DROP TABLE unit_test')); } + public function testJoins() + { + $this->assertTrue($this->object->connect('sqlite:' . self::TEST_SQLITE_DB, '', '', array(), true)); + $this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), PRIMARY KEY (ID))'); + $this->object->insert('unit_test', array('id' => '1', 'test_key' => 'testing 1')); + $this->object->insert('unit_test', array('id' => '2', 'test_key' => 'testing 2')); + $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3')); + $this->object->query('CREATE TABLE unit_test_child(child_id integer, child_test_key varchar(50), parent_id integer, PRIMARY KEY (child_id))'); + $this->object->insert('unit_test', array('child_id' => '1', 'child_test_key' => 'testing child 1', 'parent_id' => '3')); + $this->object->insert('unit_test', array('child_id' => '2', 'child_test_key' => 'testing child 2', 'parent_id' => '2')); + $this->object->insert('unit_test', array('child_id' => '3', 'child_test_key' => 'testing child 3', 'parent_id' => '1')); + + $result = $this->object->selecting('unit_test_child', '*', leftJoin('unit_test_child', 'unit_test', 'parent_id', 'id')); + $i = 1; + $o = 3; + foreach ($result as $row) { + $this->assertEquals($i, $row->child_id); + $this->assertEquals('testing child ' . $i, $row->child_test_key); + $this->assertEquals($o, $row->id); + $this->assertEquals('testing ' . $o, $row->test_key); + ++$i; + --$o; + } + + $result = $this->object->selecting('unit_test_child', 'child.parent_id', leftJoin('unit_test_child', 'unit_test', 'parent_id', 'id', 'child')); + $o = 3; + foreach ($result as $row) { + $this->assertEquals($o, $row->parent_id); + --$o; + } + + $this->assertEquals(1, $this->object->query('DROP TABLE unit_test')); + $this->assertEquals(1, $this->object->query('DROP TABLE unit_test_child')); + } + public function testSQLiteDisconnect() { $this->assertTrue($this->object->connect()); diff --git a/tests/pdo/pdo_sqlsrvTest.php b/tests/pdo/pdo_sqlsrvTest.php index 26c022db..f3ed02b5 100644 --- a/tests/pdo/pdo_sqlsrvTest.php +++ b/tests/pdo/pdo_sqlsrvTest.php @@ -193,6 +193,38 @@ public function testSelecting() } } + public function testJoins() + { + $this->assertTrue($this->object->connect('sqlsrv:Server=' . self::TEST_DB_HOST . ';Database=' . self::TEST_DB_NAME, self::TEST_DB_USER, self::TEST_DB_PASSWORD)); + $this->object->query('CREATE TABLE unit_test(id integer, test_key varchar(50), PRIMARY KEY (ID))'); + $this->object->insert('unit_test', array('id' => '1', 'test_key' => 'testing 1')); + $this->object->insert('unit_test', array('id' => '2', 'test_key' => 'testing 2')); + $this->object->insert('unit_test', array('id' => '3', 'test_key' => 'testing 3')); + $this->object->query('CREATE TABLE unit_test_child(child_id integer, child_test_key varchar(50), parent_id integer, PRIMARY KEY (child_id))'); + $this->object->insert('unit_test', array('child_id' => '1', 'child_test_key' => 'testing child 1', 'parent_id' => '3')); + $this->object->insert('unit_test', array('child_id' => '2', 'child_test_key' => 'testing child 2', 'parent_id' => '2')); + $this->object->insert('unit_test', array('child_id' => '3', 'child_test_key' => 'testing child 3', 'parent_id' => '1')); + + $result = $this->object->selecting('unit_test_child', '*', leftJoin('unit_test_child', 'unit_test', 'parent_id', 'id')); + $i = 1; + $o = 3; + foreach ($result as $row) { + $this->assertEquals($i, $row->child_id); + $this->assertEquals('testing child ' . $i, $row->child_test_key); + $this->assertEquals($o, $row->id); + $this->assertEquals('testing ' . $o, $row->test_key); + ++$i; + --$o; + } + + $result = $this->object->selecting('unit_test_child', 'child.parent_id', leftJoin('unit_test_child', 'unit_test', 'parent_id', 'id', 'child')); + $o = 3; + foreach ($result as $row) { + $this->assertEquals($o, $row->parent_id); + --$o; + } + } + public function testSQLsrvDisconnect() { $this->assertTrue($this->object->connect('sqlsrv:Server=' . self::TEST_DB_HOST . ';Database=' . self::TEST_DB_NAME, self::TEST_DB_USER, self::TEST_DB_PASSWORD));