| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Db
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @see Zend_Db_TestSetup
- */
- require_once 'Zend/Db/TestSetup.php';
- /**
- * @category Zend
- * @package Zend_Db
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- abstract class Zend_Db_Select_TestCommon extends Zend_Db_TestSetup
- {
- /**
- * Test basic use of the Zend_Db_Select class.
- *
- * @return Zend_Db_Select
- */
- protected function _select()
- {
- $select = $this->_db->select();
- $select->from('zfproducts');
- return $select;
- }
- public function testSelect()
- {
- $select = $this->_select();
- $this->assertType('Zend_Db_Select', $select,
- 'Expecting object of type Zend_Db_Select, got '.get_class($select));
- $stmt = $this->_db->query($select);
- $row = $stmt->fetch();
- $stmt->closeCursor();
- $this->assertEquals(2, count($row)); // correct number of fields
- $this->assertEquals(1, $row['product_id']); // correct data
- }
- public function testSelectToString()
- {
- $select = $this->_select();
- $this->assertEquals($select->__toString(), $select->assemble()); // correct data
- }
- /**
- * Test basic use of the Zend_Db_Select class.
- */
- public function testSelectQuery()
- {
- $select = $this->_select();
- $this->assertType('Zend_Db_Select', $select,
- 'Expecting object of type Zend_Db_Select, got '.get_class($select));
- $stmt = $select->query();
- $row = $stmt->fetch();
- $stmt->closeCursor();
- $this->assertEquals(2, count($row)); // correct number of fields
- $this->assertEquals(1, $row['product_id']); // correct data
- }
- /**
- * ZF-2017: Test bind use of the Zend_Db_Select class.
- * @group ZF-2017
- */
- public function testSelectQueryWithBinds()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_select()->where("$product_id = :product_id")
- ->bind(array(':product_id' => 1));
- $this->assertType('Zend_Db_Select', $select,
- 'Expecting object of type Zend_Db_Select, got '.get_class($select));
- $stmt = $select->query();
- $row = $stmt->fetch();
- $stmt->closeCursor();
- $this->assertEquals(2, count($row)); // correct number of fields
- $this->assertEquals(1, $row['product_id']); // correct data
- }
- /**
- * Test Zend_Db_Select specifying columns
- */
- protected function _selectColumnsScalar()
- {
- $select = $this->_db->select()
- ->from('zfproducts', 'product_name'); // scalar
- return $select;
- }
- public function testSelectColumnsScalar()
- {
- $select = $this->_selectColumnsScalar();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result), 'Expected count of result set to be 2');
- $this->assertEquals(1, count($result[0]), 'Expected column count of result set to be 1');
- $this->assertThat($result[0], $this->arrayHasKey('product_name'));
- }
- protected function _selectColumnsArray()
- {
- $select = $this->_db->select()
- ->from('zfproducts', array('product_id', 'product_name')); // array
- return $select;
- }
- public function testSelectColumnsArray()
- {
- $select = $this->_selectColumnsArray();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result), 'Expected count of result set to be 2');
- $this->assertEquals(2, count($result[0]), 'Expected column count of result set to be 2');
- $this->assertThat($result[0], $this->arrayHasKey('product_id'));
- $this->assertThat($result[0], $this->arrayHasKey('product_name'));
- }
- /**
- * Test support for column aliases.
- * e.g. from('table', array('alias' => 'col1')).
- */
- protected function _selectColumnsAliases()
- {
- $select = $this->_db->select()
- ->from('zfproducts', array('alias' => 'product_name'));
- return $select;
- }
- public function testSelectColumnsAliases()
- {
- $select = $this->_selectColumnsAliases();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result), 'Expected count of result set to be 2');
- $this->assertThat($result[0], $this->arrayHasKey('alias'));
- $this->assertThat($result[0], $this->logicalNot($this->arrayHasKey('product_name')));
- }
- /**
- * Test syntax to support qualified column names,
- * e.g. from('table', array('table.col1', 'table.col2')).
- */
- protected function _selectColumnsQualified()
- {
- $select = $this->_db->select()
- ->from('zfproducts', "zfproducts.product_name");
- return $select;
- }
- public function testSelectColumnsQualified()
- {
- $select = $this->_selectColumnsQualified();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertThat($result[0], $this->arrayHasKey('product_name'));
- }
- /**
- * Test support for columns defined by Zend_Db_Expr.
- */
- protected function _selectColumnsExpr()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_name = $this->_db->quoteIdentifier('product_name');
- $select = $this->_db->select()
- ->from('zfproducts', new Zend_Db_Expr($products.'.'.$product_name));
- return $select;
- }
- public function testSelectColumnsExpr()
- {
- $select = $this->_selectColumnsExpr();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertThat($result[0], $this->arrayHasKey('product_name'));
- }
- /**
- * Test support for automatic conversion of SQL functions to
- * Zend_Db_Expr, e.g. from('table', array('COUNT(*)'))
- * should generate the same result as
- * from('table', array(new Zend_Db_Expr('COUNT(*)')))
- */
- protected function _selectColumnsAutoExpr()
- {
- $select = $this->_db->select()
- ->from('zfproducts', array('count' => 'COUNT(*)'));
- return $select;
- }
- public function testSelectColumnsAutoExpr()
- {
- $select = $this->_selectColumnsAutoExpr();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertThat($result[0], $this->arrayHasKey('count'));
- $this->assertEquals(3, $result[0]['count']);
- }
- /**
- * Test adding the DISTINCT query modifier to a Zend_Db_Select object.
- */
- protected function _selectDistinctModifier()
- {
- $select = $this->_db->select()
- ->distinct()
- ->from('zfproducts', new Zend_Db_Expr(327));
- return $select;
- }
- public function testSelectDistinctModifier()
- {
- $select = $this->_selectDistinctModifier();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result));
- }
- /**
- * Test adding the FOR UPDATE query modifier to a Zend_Db_Select object.
- *
- public function testSelectForUpdateModifier()
- {
- }
- */
- /**
- * Test support for schema-qualified table names in from()
- * e.g. from('schema.table').
- */
- protected function _selectFromQualified()
- {
- $schema = $this->_util->getSchema();
- $select = $this->_db->select()
- ->from("$schema.zfproducts");
- return $select;
- }
- public function testSelectFromQualified()
- {
- $select = $this->_selectFromQualified();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result));
- }
- /**
- * Test support for nested select in from()
- */
- protected function _selectFromSelectObject()
- {
- $subquery = $this->_db->select()
- ->from('subqueryTable');
- $select = $this->_db->select()
- ->from($subquery);
- return $select;
- }
- public function testSelectFromSelectObject()
- {
- $select = $this->_selectFromSelectObject();
- $query = $select->assemble();
- $cmp = 'SELECT ' . $this->_db->quoteIdentifier('t') . '.* FROM (SELECT '
- . $this->_db->quoteIdentifier('subqueryTable') . '.* FROM '
- . $this->_db->quoteIdentifier('subqueryTable') . ') AS '
- . $this->_db->quoteIdentifier('t');
- $this->assertEquals($query, $cmp);
- }
- /**
- * Test support for nested select in from()
- */
- protected function _selectColumnsReset()
- {
- $select = $this->_db->select()
- ->from(array('p' => 'zfproducts'), array('product_id', 'product_name'));
- return $select;
- }
- public function testSelectColumnsReset()
- {
- $select = $this->_selectColumnsReset()
- ->reset(Zend_Db_Select::COLUMNS)
- ->columns('product_name');
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertContains('product_name', array_keys($result[0]));
- $this->assertNotContains('product_id', array_keys($result[0]));
- $select = $this->_selectColumnsReset()
- ->reset(Zend_Db_Select::COLUMNS)
- ->columns('p.product_name');
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertContains('product_name', array_keys($result[0]));
- $this->assertNotContains('product_id', array_keys($result[0]));
- $select = $this->_selectColumnsReset()
- ->reset(Zend_Db_Select::COLUMNS)
- ->columns('product_name', 'p');
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertContains('product_name', array_keys($result[0]));
- $this->assertNotContains('product_id', array_keys($result[0]));
- }
- public function testSelectColumnsResetBeforeFrom()
- {
- $select = $this->_selectColumnsReset();
- try {
- $select->reset(Zend_Db_Select::COLUMNS)
- ->reset(Zend_Db_Select::FROM)
- ->columns('product_id');
- $this->fail('Expected exception of type "Zend_Db_Select_Exception"');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Select_Exception', $e,
- 'Expected exception of type "Zend_Db_Select_Exception", got ' . get_class($e));
- $this->assertEquals("No table has been specified for the FROM clause", $e->getMessage());
- }
- }
- protected function _selectColumnWithColonQuotedParameter()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->where($product_id . ' = ?', "as'as:x");
- return $select;
- }
- public function testSelectColumnWithColonQuotedParameter()
- {
- $stmt = $select = $this->_selectColumnWithColonQuotedParameter()
- ->query();
- $result = $stmt->fetchAll();
- $this->assertEquals(0, count($result));
- }
- /**
- * Test support for FOR UPDATE
- * e.g. from('schema.table').
- */
- public function testSelectFromForUpdate()
- {
- $select = $this->_db->select()
- ->from("zfproducts")
- ->forUpdate();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result));
- }
- /**
- * Test adding a JOIN to a Zend_Db_Select object.
- */
- protected function _selectJoin()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->join('zfbugs_products', "$products.$product_id = $bugs_products.$product_id");
- return $select;
- }
- public function testSelectJoin()
- {
- $select = $this->_selectJoin();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(6, count($result));
- $this->assertEquals(3, count($result[0]));
- }
- /**
- * Test adding an INNER JOIN to a Zend_Db_Select object.
- * This should be exactly the same as the plain JOIN clause.
- */
- protected function _selectJoinWithCorrelationName()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $xyz1 = $this->_db->quoteIdentifier('xyz1');
- $xyz2 = $this->_db->quoteIdentifier('xyz2');
- $select = $this->_db->select()
- ->from( array('xyz1' => 'zfproducts') )
- ->join( array('xyz2' => 'zfbugs_products'), "$xyz1.$product_id = $xyz2.$product_id")
- ->where("$xyz1.$product_id = 1");
- return $select;
- }
- public function testSelectJoinWithCorrelationName()
- {
- $select = $this->_selectJoinWithCorrelationName();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(3, count($result[0]));
- }
- /**
- * Test adding an INNER JOIN to a Zend_Db_Select object.
- * This should be exactly the same as the plain JOIN clause.
- */
- protected function _selectJoinInner()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->joinInner('zfbugs_products', "$products.$product_id = $bugs_products.$product_id");
- return $select;
- }
- public function testSelectJoinInner()
- {
- $select = $this->_selectJoinInner();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(6, count($result));
- $this->assertEquals(3, count($result[0]));
- }
- /**
- * Test adding a JOIN to a Zend_Db_Select object.
- */
- protected function _selectJoinWithNocolumns()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->join('zfbugs', "$bugs.$bug_id = 1", array())
- ->join('zfbugs_products', "$products.$product_id = $bugs_products.$product_id AND $bugs_products.$bug_id = $bugs.$bug_id", null);
- return $select;
- }
- public function testSelectJoinWithNocolumns()
- {
- $select = $this->_selectJoinWithNocolumns();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result));
- $this->assertEquals(2, count($result[0]));
- }
- /**
- * Test adding an outer join to a Zend_Db_Select object.
- */
- protected function _selectJoinLeft()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- $select = $this->_db->select()
- ->from('zfbugs')
- ->joinLeft('zfbugs_products', "$bugs.$bug_id = $bugs_products.$bug_id");
- return $select;
- }
- public function testSelectJoinLeft()
- {
- $select = $this->_selectJoinLeft();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(7, count($result));
- $this->assertEquals(9, count($result[0]));
- $this->assertEquals(3, $result[3]['product_id']);
- $this->assertNull($result[6]['product_id']);
- }
- /**
- * Returns a select object that uses table aliases and specifies a mixed ordering of columns,
- * for testing whether the user-specified ordering is preserved.
- *
- * @return Zend_Db_Select
- */
- protected function _selectJoinLeftTableAliasesColumnOrderPreserve()
- {
- $bugsBugId = $this->_db->quoteIdentifier('b.bug_id');
- $bugsProductBugId = $this->_db->quoteIdentifier('bp.bug_id');
- $select = $this->_db->select()
- ->from(array('b' => 'zfbugs'), array('b.bug_id', 'bp.product_id', 'b.bug_description'))
- ->joinLeft(array('bp' => 'zfbugs_products'), "$bugsBugId = $bugsProductBugId", array());
- return $select;
- }
- /**
- * Ensures that when table aliases are used with a mixed ordering of columns, the user-specified
- * column ordering is preserved.
- *
- * @return void
- */
- public function testJoinLeftTableAliasesColumnOrderPreserve()
- {
- $select = $this->_selectJoinLeftTableAliasesColumnOrderPreserve();
- $this->assertRegExp('/^.*b.*bug_id.*,.*bp.*product_id.*,.*b.*bug_description.*$/s', $select->assemble());
- }
- /**
- * Test adding an outer join to a Zend_Db_Select object.
- */
- protected function _selectJoinRight()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- $select = $this->_db->select()
- ->from('zfbugs_products')
- ->joinRight('zfbugs', "$bugs_products.$bug_id = $bugs.$bug_id");
- return $select;
- }
- public function testSelectJoinRight()
- {
- $select = $this->_selectJoinRight();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(7, count($result));
- $this->assertEquals(9, count($result[0]));
- $this->assertEquals(3, $result[3]['product_id']);
- $this->assertNull($result[6]['product_id']);
- }
- /**
- * Test adding a cross join to a Zend_Db_Select object.
- */
- protected function _selectJoinCross()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->joinCross('zfbugs_products');
- return $select;
- }
- public function testSelectJoinCross()
- {
- $select = $this->_selectJoinCross();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(18, count($result));
- $this->assertEquals(3, count($result[0]));
- }
- /**
- * Test support for schema-qualified table names in join(),
- * e.g. join('schema.table', 'condition')
- */
- protected function _selectJoinQualified()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $schema = $this->_util->getSchema();
- $select = $this->_db->select()
- ->from('zfproducts')
- ->join("$schema.zfbugs_products", "$products.$product_id = $bugs_products.$product_id");
- return $select;
- }
- public function testSelectJoinQualified()
- {
- $select = $this->_selectJoinQualified();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(6, count($result));
- $this->assertEquals(3, count($result[0]));
- }
- protected function _selectJoinUsing()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->joinUsing("zfbugs_products", "$product_id")
- ->where("$bugs_products.$product_id < ?", 3);
- return $select;
- }
- public function testSelectMagicMethod()
- {
- $select = $this->_selectJoinUsing();
- try {
- $select->foo();
- $this->fail('Expected exception of type "Zend_Db_Select_Exception"');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Select_Exception', $e,
- 'Expected exception of type "Zend_Db_Select_Exception", got ' . get_class($e));
- $this->assertEquals("Unrecognized method 'foo()'", $e->getMessage());
- }
- }
- public function testSelectJoinUsing()
- {
- $select = $this->_selectJoinUsing();
- $sql = preg_replace('/\\s+/', ' ', $select->assemble());
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result));
- $this->assertEquals(1, $result[0]['product_id']);
- }
- protected function _selectJoinInnerUsing()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->joinInnerUsing("zfbugs_products", "$product_id")
- ->where("$bugs_products.$product_id < ?", 3);
- return $select;
- }
- public function testSelectJoinInnerUsing()
- {
- $select = $this->_selectJoinInnerUsing();
- $sql = preg_replace('/\\s+/', ' ', $select->assemble());
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result));
- $this->assertEquals(1, $result[0]['product_id']);
- }
- public function testSelectJoinInnerUsingException()
- {
- $select = $this->_selectJoinInnerUsing();
- try {
- $select->joinFooUsing();
- $this->fail('Expected exception of type "Zend_Db_Select_Exception"');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Select_Exception', $e,
- 'Expected exception of type "Zend_Db_Select_Exception", got ' . get_class($e));
- $this->assertEquals("Unrecognized method 'joinFooUsing()'", $e->getMessage());
- }
- }
- protected function _selectJoinCrossUsing()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->where("$bugs_products.$product_id < ?", 3);
- return $select;
- }
- public function testSelectJoinCrossUsing()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_selectJoinCrossUsing();
- try {
- $select->joinCrossUsing("zfbugs_products", "$product_id");
- $this->fail('Expected exception of type "Zend_Db_Select_Exception"');
- } catch (Zend_Exception $e) {
- $this->assertType('Zend_Db_Select_Exception', $e,
- 'Expected exception of type "Zend_Db_Select_Exception", got ' . get_class($e));
- $this->assertEquals("Cannot perform a joinUsing with method 'joinCrossUsing()'", $e->getMessage());
- }
- }
- /**
- * Test adding a WHERE clause to a Zend_Db_Select object.
- */
- protected function _selectWhere()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->where("$product_id = 2");
- return $select;
- }
- public function testSelectWhere()
- {
- $select = $this->_selectWhere();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(2, $result[0]['product_id']);
- }
- /**
- * Test support for nested select in from()
- */
- protected function _selectWhereSelectObject()
- {
- $subquery = $this->_db->select()
- ->from('subqueryTable');
- $select = $this->_db->select()
- ->from('table')
- ->where('foo IN ?', $subquery);
- return $select;
- }
- public function testSelectWhereSelectObject()
- {
- $select = $this->_selectWhereSelectObject();
- $query = $select->assemble();
- $cmp = 'SELECT ' . $this->_db->quoteIdentifier('table') . '.* FROM '
- . $this->_db->quoteIdentifier('table') . ' WHERE (foo IN (SELECT '
- . $this->_db->quoteIdentifier('subqueryTable') . '.* FROM '
- . $this->_db->quoteIdentifier('subqueryTable') . '))';
- $this->assertEquals($query, $cmp);
- }
- protected function _selectWhereArray()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->where("$product_id IN (?)", array(1, 2, 3));
- return $select;
- }
- public function testSelectWhereArray()
- {
- $select = $this->_selectWhereArray();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result));
- }
- /**
- * test adding more WHERE conditions,
- * which should be combined with AND by default.
- */
- protected function _selectWhereAnd()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->where("$product_id = 2")
- ->where("$product_id = 1");
- return $select;
- }
- public function testSelectWhereAnd()
- {
- $select = $this->_selectWhereAnd();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(0, count($result));
- }
- /**
- * Test support for where() with a parameter,
- * e.g. where('id = ?', 1).
- */
- protected function _selectWhereWithParameter()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->where("$product_id = ?", 2);
- return $select;
- }
- public function testSelectWhereWithParameter()
- {
- $select = $this->_selectWhereWithParameter();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(2, $result[0]['product_id']);
- }
- /**
- * Test support for where() with a specified type,
- * e.g. where('id = ?', 1, 'int').
- */
- protected function _selectWhereWithType()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->where("$product_id = ?", 2, 'int');
- return $select;
- }
- public function testSelectWhereWithType()
- {
- $select = $this->_selectWhereWithType();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(2, $result[0]['product_id']);
- }
- /**
- * Test support for where() with a specified type,
- * e.g. where('id = ?', 1, 'int').
- */
- protected function _selectWhereWithTypeFloat()
- {
- $price_total = $this->_db->quoteIdentifier('price_total');
- $select = $this->_db->select()
- ->from('zfprice')
- ->where("$price_total = ?", 200.45, Zend_Db::FLOAT_TYPE);
- return $select;
- }
- public function testSelectWhereWithTypeFloat()
- {
- $locale = setlocale(LC_ALL, null);
- $select = $this->_selectWhereWithTypeFloat();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(200.45, $result[0]['price_total']);
- try {
- setlocale(LC_ALL, 'fr_BE.UTF-8');
- $select = $this->_selectWhereWithTypeFloat();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(200.45, $result[0]['price_total']);
- } catch (Zend_Exception $e) {
- setlocale(LC_ALL, $locale);
- throw $e;
- }
- setlocale(LC_ALL, $locale);
- }
- /**
- * Test adding an OR WHERE clause to a Zend_Db_Select object.
- */
- protected function _selectWhereOr()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->orWhere("$product_id = 1")
- ->orWhere("$product_id = 2");
- return $select;
- }
- public function testSelectWhereOr()
- {
- $select = $this->_selectWhereOr();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(2, count($result));
- $this->assertEquals(1, $result[0]['product_id']);
- $this->assertEquals(2, $result[1]['product_id']);
- }
- /**
- * Test support for where() with a parameter,
- * e.g. orWhere('id = ?', 2).
- */
- protected function _selectWhereOrWithParameter()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->orWhere("$product_id = ?", 1)
- ->orWhere("$product_id = ?", 2);
- return $select;
- }
- public function testSelectWhereOrWithParameter()
- {
- $select = $this->_selectWhereOrWithParameter();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(2, count($result));
- $this->assertEquals(1, $result[0]['product_id']);
- $this->assertEquals(2, $result[1]['product_id']);
- }
- /**
- * Test adding a GROUP BY clause to a Zend_Db_Select object.
- */
- protected function _selectGroupBy()
- {
- $thecount = $this->_db->quoteIdentifier('thecount');
- $select = $this->_db->select()
- ->from('zfbugs_products', array('bug_id', new Zend_Db_Expr("COUNT(*) AS $thecount")))
- ->group('bug_id')
- ->order('bug_id');
- return $select;
- }
- public function testSelectGroupBy()
- {
- $select = $this->_selectGroupBy();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result),
- 'Expected count of first result set to be 2');
- $this->assertEquals(1, $result[0]['bug_id']);
- $this->assertEquals(3, $result[0]['thecount'],
- 'Expected count(*) of first result set to be 2');
- $this->assertEquals(2, $result[1]['bug_id']);
- $this->assertEquals(1, $result[1]['thecount']);
- }
- /**
- * Test support for qualified table in group(),
- * e.g. group('schema.table').
- */
- protected function _selectGroupByQualified()
- {
- $thecount = $this->_db->quoteIdentifier('thecount');
- $select = $this->_db->select()
- ->from('zfbugs_products', array('bug_id', new Zend_Db_Expr("COUNT(*) AS $thecount")))
- ->group("zfbugs_products.bug_id")
- ->order('bug_id');
- return $select;
- }
- public function testSelectGroupByQualified()
- {
- $select = $this->_selectGroupByQualified();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result),
- 'Expected count of first result set to be 2');
- $this->assertEquals(1, $result[0]['bug_id']);
- $this->assertEquals(3, $result[0]['thecount'],
- 'Expected count(*) of first result set to be 2');
- $this->assertEquals(2, $result[1]['bug_id']);
- $this->assertEquals(1, $result[1]['thecount']);
- }
- /**
- * Test support for Zend_Db_Expr in group(),
- * e.g. group(new Zend_Db_Expr('id+1'))
- */
- protected function _selectGroupByExpr()
- {
- $thecount = $this->_db->quoteIdentifier('thecount');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- $select = $this->_db->select()
- ->from('zfbugs_products', array('bug_id'=>new Zend_Db_Expr("$bug_id+1"), new Zend_Db_Expr("COUNT(*) AS $thecount")))
- ->group(new Zend_Db_Expr("$bug_id+1"))
- ->order(new Zend_Db_Expr("$bug_id+1"));
- return $select;
- }
- public function testSelectGroupByExpr()
- {
- $select = $this->_selectGroupByExpr();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result),
- 'Expected count of first result set to be 2');
- $this->assertEquals(2, $result[0]['bug_id'],
- 'Expected first bug_id to be 2');
- $this->assertEquals(3, $result[0]['thecount'],
- 'Expected count(*) of first group to be 2');
- $this->assertEquals(3, $result[1]['bug_id'],
- 'Expected second bug_id to be 3');
- $this->assertEquals(1, $result[1]['thecount'],
- 'Expected count(*) of second group to be 1');
- }
- /**
- * Test support for automatic conversion of a SQL
- * function to a Zend_Db_Expr in group(),
- * e.g. group('LOWER(title)') should give the same
- * result as group(new Zend_Db_Expr('LOWER(title)')).
- */
- protected function _selectGroupByAutoExpr()
- {
- $thecount = $this->_db->quoteIdentifier('thecount');
- $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- $select = $this->_db->select()
- ->from('zfbugs_products', array('bug_id'=>"ABS($bugs_products.$bug_id)", new Zend_Db_Expr("COUNT(*) AS $thecount")))
- ->group("ABS($bugs_products.$bug_id)")
- ->order("ABS($bugs_products.$bug_id)");
- return $select;
- }
- public function testSelectGroupByAutoExpr()
- {
- $select = $this->_selectGroupByAutoExpr();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result), 'Expected count of first result set to be 2');
- $this->assertEquals(1, $result[0]['bug_id']);
- $this->assertEquals(3, $result[0]['thecount'], 'Expected count(*) of first result set to be 2');
- $this->assertEquals(2, $result[1]['bug_id']);
- $this->assertEquals(1, $result[1]['thecount']);
- }
- /**
- * Test adding a HAVING clause to a Zend_Db_Select object.
- */
- protected function _selectHaving()
- {
- $select = $this->_db->select()
- ->from('zfbugs_products', array('bug_id', 'COUNT(*) AS thecount'))
- ->group('bug_id')
- ->having('COUNT(*) > 1')
- ->order('bug_id');
- return $select;
- }
- public function testSelectHaving()
- {
- $select = $this->_selectHaving();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(2, count($result));
- $this->assertEquals(1, $result[0]['bug_id']);
- $this->assertEquals(3, $result[0]['thecount']);
- }
- protected function _selectHavingAnd()
- {
- $select = $this->_db->select()
- ->from('zfbugs_products', array('bug_id', 'COUNT(*) AS thecount'))
- ->group('bug_id')
- ->having('COUNT(*) > 1')
- ->having('COUNT(*) = 1')
- ->order('bug_id');
- return $select;
- }
- public function testSelectHavingAnd()
- {
- $select = $this->_selectHavingAnd();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(0, count($result));
- }
- /**
- * Test support for parameter in having(),
- * e.g. having('count(*) > ?', 1).
- */
- protected function _selectHavingWithParameter()
- {
- $select = $this->_db->select()
- ->from('zfbugs_products', array('bug_id', 'COUNT(*) AS thecount'))
- ->group('bug_id')
- ->having('COUNT(*) > ?', 1)
- ->order('bug_id');
- return $select;
- }
- public function testSelectHavingWithParameter()
- {
- $select = $this->_selectHavingWithParameter();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(2, count($result));
- $this->assertEquals(1, $result[0]['bug_id']);
- $this->assertEquals(3, $result[0]['thecount']);
- }
- /**
- * Test adding a HAVING clause to a Zend_Db_Select object.
- */
- protected function _selectHavingOr()
- {
- $select = $this->_db->select()
- ->from('zfbugs_products', array('bug_id', 'COUNT(*) AS thecount'))
- ->group('bug_id')
- ->orHaving('COUNT(*) > 1')
- ->orHaving('COUNT(*) = 1')
- ->order('bug_id');
- return $select;
- }
- public function testSelectHavingOr()
- {
- $select = $this->_selectHavingOr();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result));
- $this->assertEquals(1, $result[0]['bug_id']);
- $this->assertEquals(3, $result[0]['thecount']);
- $this->assertEquals(2, $result[1]['bug_id']);
- $this->assertEquals(1, $result[1]['thecount']);
- }
- /**
- * Test support for parameter in orHaving(),
- * e.g. orHaving('count(*) > ?', 1).
- */
- protected function _selectHavingOrWithParameter()
- {
- $select = $this->_db->select()
- ->from('zfbugs_products', array('bug_id', 'COUNT(*) AS thecount'))
- ->group('bug_id')
- ->orHaving('COUNT(*) > ?', 1)
- ->orHaving('COUNT(*) = ?', 1)
- ->order('bug_id');
- return $select;
- }
- public function testSelectHavingOrWithParameter()
- {
- $select = $this->_selectHavingOrWithParameter();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result));
- $this->assertEquals(1, $result[0]['bug_id']);
- $this->assertEquals(3, $result[0]['thecount']);
- $this->assertEquals(2, $result[1]['bug_id']);
- $this->assertEquals(1, $result[1]['thecount']);
- }
- /**
- * Test adding an ORDER BY clause to a Zend_Db_Select object.
- */
- protected function _selectOrderBy()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order('product_id');
- return $select;
- }
- public function testSelectOrderBy()
- {
- $select = $this->_selectOrderBy();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, $result[0]['product_id']);
- }
- protected function _selectOrderByArray()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order(array('product_name', 'product_id'));
- return $select;
- }
- public function testSelectOrderByArray()
- {
- $select = $this->_selectOrderByArray();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result),
- 'Expected count of result set to be 3');
- $this->assertEquals('Linux', $result[0]['product_name']);
- $this->assertEquals(2, $result[0]['product_id']);
- }
- protected function _selectOrderByAsc()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order("product_id ASC");
- return $select;
- }
- public function testSelectOrderByAsc()
- {
- $select = $this->_selectOrderByAsc();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result),
- 'Expected count of result set to be 2');
- $this->assertEquals(1, $result[0]['product_id']);
- }
- protected function _selectOrderByPosition()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order('2');
- return $select;
- }
- public function testSelectOrderByPosition()
- {
- $select = $this->_selectOrderByPosition();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(2, $result[0]['product_id']);
- $this->assertEquals(3, $result[1]['product_id']);
- $this->assertEquals(1, $result[2]['product_id']);
- }
- protected function _selectOrderByPositionAsc()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order('2 ASC');
- return $select;
- }
- public function testSelectOrderByPositionAsc()
- {
- $select = $this->_selectOrderByPositionAsc();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(2, $result[0]['product_id']);
- $this->assertEquals(3, $result[1]['product_id']);
- $this->assertEquals(1, $result[2]['product_id']);
- }
- protected function _selectOrderByPositionDesc()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order('2 DESC');
- return $select;
- }
- public function testSelectOrderByPositionDesc()
- {
- $select = $this->_selectOrderByPositionDesc();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, $result[0]['product_id']);
- $this->assertEquals(3, $result[1]['product_id']);
- $this->assertEquals(2, $result[2]['product_id']);
- }
- protected function _selectOrderByMultiplePositions()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order(array('2 DESC', '1 DESC'));
- return $select;
- }
- public function testSelectOrderByMultiplePositions()
- {
- $select = $this->_selectOrderByMultiplePositions();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, $result[0]['product_id']);
- $this->assertEquals(3, $result[1]['product_id']);
- $this->assertEquals(2, $result[2]['product_id']);
- }
- protected function _selectOrderByDesc()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order("product_id DESC");
- return $select;
- }
- public function testSelectOrderByDesc()
- {
- $select = $this->_selectOrderByDesc();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result),
- 'Expected count of result set to be 2');
- $this->assertEquals(3, $result[0]['product_id']);
- }
- /**
- * Test support for qualified table in order(),
- * e.g. order('schema.table').
- */
- protected function _selectOrderByQualified()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order("zfproducts.product_id");
- return $select;
- }
- public function testSelectOrderByQualified()
- {
- $select = $this->_selectOrderByQualified();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, $result[0]['product_id']);
- }
- /**
- * Test support for Zend_Db_Expr in order(),
- * e.g. order(new Zend_Db_Expr('id+1')).
- */
- protected function _selectOrderByExpr()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order(new Zend_Db_Expr("1"));
- return $select;
- }
- public function testSelectOrderByExpr()
- {
- $select = $this->_selectOrderByExpr();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, $result[0]['product_id']);
- }
- /**
- * Test automatic conversion of SQL functions to
- * Zend_Db_Expr, e.g. order('LOWER(title)')
- * should give the same result as
- * order(new Zend_Db_Expr('LOWER(title)')).
- */
- protected function _selectOrderByAutoExpr()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order("ABS($products.$product_id)");
- return $select;
- }
- public function testSelectOrderByAutoExpr()
- {
- $select = $this->_selectOrderByAutoExpr();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, $result[0]['product_id']);
- }
- /**
- * Test ORDER BY clause that contains multiple lines.
- * See ZF-1822, which says that the regexp matching
- * ASC|DESC fails when string is multi-line.
- */
- protected function _selectOrderByMultiLine()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order("product_id\nDESC");
- return $select;
- }
- public function testSelectOrderByMultiLine()
- {
- $select = $this->_selectOrderByMultiLine();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, $result[0]['product_id']);
- }
- /**
- * @group ZF-4246
- */
- protected function _checkExtraField($result)
- {
- // Check that extra field ZEND_DB_ROWNUM isn't present
- // (particulary with Db2 & Oracle)
- $this->assertArrayNotHasKey('zend_db_rownum', $result);
- $this->assertArrayNotHasKey('ZEND_DB_ROWNUM', $result);
- }
- /**
- * Test adding a LIMIT clause to a Zend_Db_Select object.
- */
- protected function _selectLimit()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order('product_id')
- ->limit(1);
- return $select;
- }
- /**
- * @group ZF-4246
- */
- public function testSelectLimit()
- {
- $select = $this->_selectLimit();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(1, $result[0]['product_id']);
- $this->_checkExtraField($result[0]);
- }
- /**
- * @group ZF-5263
- * @group ZF-4246
- */
- public function testSelectLimitFetchCol()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()
- ->from('zfproducts', 'product_name')
- ->where($product_id . ' = ?', 3)
- ->limit(1);
- $result = $this->_db->fetchCol($select);
- $this->assertEquals(1, count($result));
- $this->assertEquals('OS X', $result[0]);
- $this->_checkExtraField($result);
- }
- protected function _selectLimitNone()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order('product_id')
- ->limit(); // no limit
- return $select;
- }
- /**
- * @group ZF-4246
- */
- public function testSelectLimitNone()
- {
- $select = $this->_selectLimitNone();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result));
- $this->_checkExtraField($result[0]);
- }
- protected function _selectLimitOffset()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order('product_id')
- ->limit(1, 1);
- return $select;
- }
- /**
- * @group ZF-4246
- */
- public function testSelectLimitOffset()
- {
- $select = $this->_selectLimitOffset();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(2, $result[0]['product_id']);
- $this->_checkExtraField($result[0]);
- }
- /**
- * Test the limitPage() method of a Zend_Db_Select object.
- */
- protected function _selectLimitPageOne()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order('product_id')
- ->limitPage(1, 1); // first page, length 1
- return $select;
- }
- /**
- * @group ZF-4246
- */
- public function testSelectLimitPageOne()
- {
- $select = $this->_selectLimitPageOne();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(1, $result[0]['product_id']);
- $this->_checkExtraField($result[0]);
- }
- protected function _selectLimitPageTwo()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order('product_id')
- ->limitPage(2, 1); // second page, length 1
- return $select;
- }
- /**
- * @group ZF-4246
- */
- public function testSelectLimitPageTwo()
- {
- $select = $this->_selectLimitPageTwo();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result));
- $this->assertEquals(2, $result[0]['product_id']);
- $this->_checkExtraField($result[0]);
- }
- /**
- * Test the getPart() and reset() methods of a Zend_Db_Select object.
- */
- public function testSelectGetPartAndReset()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->limit(1);
- $count = $select->getPart(Zend_Db_Select::LIMIT_COUNT);
- $this->assertEquals(1, $count);
- $select->reset(Zend_Db_Select::LIMIT_COUNT);
- $count = $select->getPart(Zend_Db_Select::LIMIT_COUNT);
- $this->assertNull($count);
- $select->reset(); // reset the whole object
- $from = $select->getPart(Zend_Db_Select::FROM);
- $this->assertTrue(empty($from));
- }
- /**
- * Test the UNION statement for a Zend_Db_Select object.
- */
- protected function _selectUnionString()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- $bug_status = $this->_db->quoteIdentifier('bug_status');
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $product_name = $this->_db->quoteIdentifier('product_name');
- $id = $this->_db->quoteIdentifier('id');
- $name = $this->_db->quoteIdentifier('name');
- $sql1 = "SELECT $bug_id AS $id, $bug_status AS $name FROM $bugs";
- $sql2 = "SELECT $product_id AS $id, $product_name AS $name FROM $products";
- $select = $this->_db->select()
- ->union(array($sql1, $sql2))
- ->order('id');
- return $select;
- }
- public function testSelectUnionString()
- {
- $select = $this->_selectUnionString();
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(7, count($result));
- $this->assertEquals(1, $result[0]['id']);
- }
- /**
- * @group ZF-4772
- * @expectedException Zend_Db_Select_Exception
- */
- public function testSelectUnionNoArrayThrowsException()
- {
- $this->_db->select()->union('string');
- }
- /**
- * @group ZF-4772
- * @expectedException Zend_Db_Select_Exception
- */
- public function testSelectUnionInvalidUnionTypeThrowsException()
- {
- $this->_db->select()->union(array(), 'foo');
- }
- /**
- * @group ZF-6653
- */
- public function testSelectIsTheSameWhenCallingFromAndJoinInDifferentOrders()
- {
- $selectFromThenJoin = $this->_db->select();
- $selectFromThenJoin->from(array('f' => 'foo'), array('columnfoo'))
- ->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'));
- $selectJoinThenFrom = $this->_db->select();
- $selectJoinThenFrom->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'))
- ->from(array('f' => 'foo'), array('columnfoo'));
- $sqlSelectFromThenJoin = $selectFromThenJoin->assemble();
- $sqlSelectJoinThenFrom = $selectJoinThenFrom->assemble();
- $this->assertEquals($sqlSelectFromThenJoin, $sqlSelectJoinThenFrom);
- }
- /**
- * @group ZF-6653
- */
- public function testSelectIsTheSameWhenCallingMultipleFromsAfterJoin()
- {
- $selectFromThenJoin = $this->_db->select();
- $selectFromThenJoin->from(array('f' => 'foo'), array('columnfoo'))
- ->from(array('d' => 'doo'), array('columndoo'))
- ->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'));
- $selectJoinThenFrom = $this->_db->select();
- $selectJoinThenFrom->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'))
- ->from(array('f' => 'foo'), array('columnfoo'))
- ->from(array('d' => 'doo'), array('columndoo'));
- $sqlSelectFromThenJoin = $selectFromThenJoin->assemble();
- $sqlSelectJoinThenFrom = $selectJoinThenFrom->assemble();
- $this->assertEquals($sqlSelectFromThenJoin, $sqlSelectJoinThenFrom);
- }
- /**
- * @group ZF-6653
- */
- public function testSelectWithMultipleFromsAfterAJoinWillProperlyOrderColumns()
- {
- $select = $this->_selectWithMultipleFromsAfterAJoinWillProperlyOrderColumns();
- $quote = $this->_db->getQuoteIdentifierSymbol();
- $target = 'SELECT `f`.`columnfoo`, `d`.`columndoo`, `b`.`barcolumn` AS `baralias` FROM ' . $this->_db->quoteTableAs('foo', 'f')
- . "\n" . ' INNER JOIN ' . $this->_db->quoteTableAs('doo', 'd')
- . "\n" . ' LEFT JOIN ' . $this->_db->quoteTableAs('bar', 'b') . ' ON f.columnfoo2 = b.barcolumn2';
- if ($quote != '`') {
- $target = str_replace('`', $quote, $target);
- }
- $this->assertEquals($target, $select->assemble());
- }
- protected function _selectWithMultipleFromsAfterAJoinWillProperlyOrderColumns()
- {
- $selectJoinThenFrom = $this->_db->select();
- $selectJoinThenFrom->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'))
- ->from(array('f' => 'foo'), array('columnfoo'))
- ->from(array('d' => 'doo'), array('columndoo'));
- return $selectJoinThenFrom;
- }
- public function testSerializeSelect()
- {
- /* checks if the adapter has effectively gotten serialized,
- no exceptions are thrown here, so it's all right */
- $serialize = serialize($this->_select());
- $this->assertType('string',$serialize);
- }
-
- /**
- * @group ZF-3792
- */
- public function testJoinUsingActuallyGeneratesAnInnerJoinOnForCompatibilityReasons()
- {
- $table_A = $this->_db->quoteTableAs('A');
- $table_B = $this->_db->quoteTableAs('B');
- $colname = $this->_db->quoteIdentifier('colname');
-
- $s = $this->_db->select()->from('A')->joinUsing('B', $colname);
- $this->assertContains("JOIN {$table_B} ON {$table_B}.{$colname} = {$table_A}.{$colname}", $s->assemble());
- }
- }
|