| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770 |
- <?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-2014 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-2014 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->assertTrue($select instanceof Zend_Db_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->assertTrue($select instanceof Zend_Db_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->assertTrue($select instanceof Zend_Db_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->assertTrue($e instanceof Zend_Db_Select_Exception,
- '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->assertTrue($e instanceof Zend_Db_Select_Exception,
- '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->assertTrue($e instanceof Zend_Db_Select_Exception,
- '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->assertTrue($e instanceof Zend_Db_Select_Exception,
- '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, 0);
- $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->assertTrue(is_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());
- }
- /**
- * @group ZF-5953
- */
- public function testJoinUsingAllowsSpecifyingMultipleColumnsViaAnArray()
- {
- $table_A = $this->_db->quoteTableAs('A');
- $table_B = $this->_db->quoteTableAs('B');
- $colOne = $this->_db->quoteIdentifier('colOne');
- $colTwo = $this->_db->quoteIdentifier('colTwo');
- $s = $this->_db->select()->from('A')->joinUsing('B', array($colOne,$colTwo));
- $this->assertContains(
- "JOIN {$table_B} ON {$table_B}.{$colOne} = {$table_A}.{$colOne}"
- . " AND {$table_B}.{$colTwo} = {$table_A}.{$colTwo}",
- $s->assemble()
- );
- }
- /**
- * @group ZF-3309
- */
- public function testJoinUsingUsesTableNameOfTableBeingJoinedWhenAliasNotDefined()
- {
- $table1 = $this->_db->quoteTableAs('table1');
- $table2 = $this->_db->quoteTableAs('table2');
- $colname = $this->_db->quoteIdentifier('column1');
- $select = $this->_db->select();
- $select->from('table1')->joinUsing('table2', $colname);
- $this->assertRegexp("/ON {$table2}.{$colname}/s", $select->assemble());
- }
- /**
- * @group ZF-3309
- */
- public function testJoinUsingUsesAliasOfTableBeingJoinedWhenAliasIsDefined()
- {
- $table1 = $this->_db->quoteTableAs('table1');
- $table2_alias = $this->_db->quoteTableAs('t2');
- $colname = $this->_db->quoteIdentifier('column1');
- $select = $this->_db->select();
- $select->from('table1')->joinUsing(array('t2'=>'table2'), $colname);
- $this->assertRegexp("/ON {$table2_alias}.{$colname}/s", $select->assemble());
- }
- public function testSqlInjectionWithOrder()
- {
- $select = $this->_db->select();
- $select->from(array('p' => 'products'))->order('MD5(1);select');
- $this->assertEquals($select, 'SELECT "p".* FROM "products" AS "p" ORDER BY "MD5(1);select" ASC');
- $select = $this->_db->select();
- $select->from(array('p' => 'products'))->order('name;select;MD5(1)');
- $this->assertEquals($select, 'SELECT "p".* FROM "products" AS "p" ORDER BY "name;select;MD5(1)" ASC');
- }
- }
|