| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124 |
- <?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';
- /**
- * @see Zend_Loader
- */
- require_once 'Zend/Loader.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_Adapter_TestCommon extends Zend_Db_TestSetup
- {
- /**
- * Test AUTO_QUOTE_IDENTIFIERS option
- * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true
- */
- public function testAdapterZendConfig()
- {
- Zend_Loader::loadClass('Zend_Config');
- $params = new Zend_Config($this->_util->getParams());
- $db = Zend_Db::factory($this->getDriver(), $params);
- $db->getConnection();
- }
- /**
- * Test empty adapterNamespace issue
- * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true
- */
- public function testAdapterZendConfigEmptyNamespace()
- {
- Zend_Loader::loadClass('Zend_Config');
- $params = $this->_util->getParams();
- $params['adapterNamespace'] = '';
- $params = new Zend_Config($params);
- $db = Zend_Db::factory($this->getDriver(), $params);
- $db->getConnection();
- }
- /**
- * Test empty driver_options issue
- * Case: $config['driver_options'] == ''
- */
- public function testAdapterZendConfigEmptyDriverOptions()
- {
- Zend_Loader::loadClass('Zend_Config');
- $params = $this->_util->getParams();
- $params['driver_options'] = '';
- $params = new Zend_Config($params);
- $db = Zend_Db::factory($this->getDriver(), $params);
- $db->getConnection();
- $config = $db->getConfig();
- $this->assertEquals(array(), $config['driver_options']);
- }
- /**
- * Test AUTO_QUOTE_IDENTIFIERS option
- * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true
- */
- public function testAdapterAutoQuoteIdentifiersTrue()
- {
- $params = $this->_util->getParams();
- $params['options'] = array(
- Zend_Db::AUTO_QUOTE_IDENTIFIERS => true
- );
- $db = Zend_Db::factory($this->getDriver(), $params);
- $db->getConnection();
- $select = $this->_db->select();
- $select->from('zfproducts');
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(3, count($result), 'Expected 3 rows in first query result');
- $this->assertEquals(1, $result[0]['product_id']);
- $select = $this->_db->select();
- $select->from('ZFPRODUCTS');
- try {
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->fail('Expected exception not thrown');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Statement_Exception,
- 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
- }
- }
- /**
- * Test AUTO_QUOTE_IDENTIFIERS option
- * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = false
- */
- public function testAdapterAutoQuoteIdentifiersFalse()
- {
- $params = $this->_util->getParams();
- $params['options'] = array(
- Zend_Db::AUTO_QUOTE_IDENTIFIERS => false
- );
- $db = Zend_Db::factory($this->getDriver(), $params);
- $db->getConnection();
- // create a new util object, with the new db adapter
- $driver = $this->getDriver();
- $utilClass = "Zend_Db_TestUtil_{$driver}";
- $util = new $utilClass();
- $util->setAdapter($db);
- // create test table using no identifier quoting
- $util->createTable('noquote', array(
- 'id' => 'INT NOT NULL PRIMARY KEY',
- 'stuff' => 'CHAR(10)'
- ));
- $tableName = $this->_util->getTableName('noquote');
- // insert into the table
- $numRows = $db->insert($tableName, array(
- 'id' => 1,
- 'stuff' => 'no quote 1'
- ));
- $this->assertEquals(1, $numRows,
- 'number of rows in first insert not as expected');
- // check if the row was inserted as expected
- $sql = "SELECT id, stuff FROM $tableName ORDER BY id";
- $stmt = $db->query($sql);
- $fetched = $stmt->fetchAll(Zend_Db::FETCH_NUM);
- $a = array(
- 0 => array(0 => 1, 1 => 'no quote 1')
- );
- $this->assertEquals($a, $fetched,
- 'result of first query not as expected');
- // insert into the table using other case
- $numRows = $db->insert($tableName, array(
- 'ID' => 2,
- 'STUFF' => 'no quote 2'
- ));
- $this->assertEquals(1, $numRows,
- 'number of rows in second insert not as expected');
- // check if the row was inserted as expected
- $sql = 'SELECT ID, STUFF FROM ' . $tableName . ' ORDER BY ID';
- $stmt = $db->query($sql);
- $fetched = $stmt->fetchAll(Zend_Db::FETCH_NUM);
- $a = array(
- 0 => array(0 => 1, 1 => 'no quote 1'),
- 1 => array(0 => 2, 1 => 'no quote 2'),
- );
- $this->assertEquals($a, $fetched,
- 'result of second query not as expected');
- // clean up
- unset($stmt);
- $util->dropTable($tableName);
- }
- protected function _testAdapterConstructInvalidParam($param, $adapterClass = null)
- {
- $exceptionClass = 'Zend_Db_Adapter_Exception';
- if ($adapterClass === null) {
- $adapterClass = 'Zend_Db_Adapter_' . $this->getDriver();
- }
- $params = $this->_util->getParams();
- unset($params[$param]);
- Zend_Loader::loadClass($adapterClass);
- Zend_Loader::loadClass($exceptionClass);
- try {
- $db = new $adapterClass($params);
- $db->getConnection(); // force a connection
- $this->fail("Expected to catch $exceptionClass");
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof $exceptionClass, "Expected to catch $exceptionClass, got ".get_class($e));
- $this->assertContains("Configuration array must have a key for '$param'", $e->getMessage());
- }
- }
- public function testAdapterConstructInvalidParamDbnameException()
- {
- $this->_testAdapterConstructInvalidParam('dbname');
- }
- public function testAdapterConstructInvalidParamUsernameException()
- {
- $this->_testAdapterConstructInvalidParam('username');
- }
- public function testAdapterConstructInvalidParamPasswordException()
- {
- $this->_testAdapterConstructInvalidParam('password');
- }
- /**
- * Test Adapter's delete() method.
- * Delete one row from test table, and verify it was deleted.
- * Then try to delete a row that doesn't exist, and verify it had no effect.
- */
- public function testAdapterDelete()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- $select = $this->_db->select()->from('zfproducts')->order('product_id ASC');
- $result = $this->_db->fetchAll($select);
- $this->assertEquals(3, count($result), 'Expected count of result to be 2');
- $this->assertEquals(1, $result[0]['product_id'], 'Expecting product_id of 0th row to be 1');
- $rowsAffected = $this->_db->delete('zfproducts', "$product_id = 2");
- $this->assertEquals(1, $rowsAffected, 'Expecting rows affected to be 1');
- $select = $this->_db->select()->from('zfproducts')->order('product_id ASC');
- $result = $this->_db->fetchAll($select);
- $this->assertEquals(2, count($result), 'Expected count of result to be 2');
- $this->assertEquals(1, $result[0]['product_id'], 'Expecting product_id of 0th row to be 1');
- $rowsAffected = $this->_db->delete('zfproducts', "$product_id = 327");
- $this->assertEquals(0, $rowsAffected, 'Expected rows affected to return 0');
- }
- public function testAdapterDeleteWhereArray()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $product_name = $this->_db->quoteIdentifier('product_name');
- $ids = $this->_db->fetchCol("SELECT $product_id FROM $products ORDER BY $product_id");
- $this->assertEquals(array(1, 2, 3), $ids);
- $rowsAffected = $this->_db->delete(
- 'zfproducts',
- array("$product_id = 1", "$product_name = 'Windows'")
- );
- $this->assertEquals(1, $rowsAffected);
- $ids = $this->_db->fetchCol("SELECT $product_id FROM $products ORDER BY $product_id");
- $this->assertEquals(array(2, 3), $ids);
- }
- /**
- * @group ZF-1726
- */
- public function testAdapterDeleteWhereArrayWithVariable()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $product_name = $this->_db->quoteIdentifier('product_name');
- $ids = $this->_db->fetchCol("SELECT $product_id FROM $products ORDER BY $product_id");
- $this->assertEquals(array(1, 2, 3), $ids);
- $rowsAffected = $this->_db->delete(
- 'zfproducts',
- array("$product_id = ?" => 1, "$product_name = ?" => 'Windows')
- );
- $this->assertEquals(1, $rowsAffected);
- $ids = $this->_db->fetchCol("SELECT $product_id FROM $products ORDER BY $product_id");
- $this->assertEquals(array(2, 3), $ids);
- }
- public function testAdapterDeleteWhereDbExpr()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $expr = new Zend_Db_Expr("$product_id = 1");
- $ids = $this->_db->fetchCol("SELECT $product_id FROM $products ORDER BY $product_id");
- $this->assertEquals(array(1, 2, 3), $ids);
- $rowsAffected = $this->_db->delete(
- 'zfproducts',
- $expr
- );
- $this->assertEquals(1, $rowsAffected);
- $ids = $this->_db->fetchCol("SELECT $product_id FROM $products ORDER BY $product_id");
- $this->assertEquals(array(2, 3), $ids);
- }
- public function testAdapterDeleteEmptyWhere()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
- $this->assertEquals(4, $count);
- $rowsAffected = $this->_db->delete(
- 'zfbugs'
- // intentionally no where clause
- );
- $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
- $this->assertEquals(0, $count);
- }
- public function testAdapterDescribeTableAttributeColumn()
- {
- $desc = $this->_db->describeTable('zfproducts');
- $this->assertEquals('zfproducts', $desc['product_name']['TABLE_NAME'], 'Expected table name to be zfproducts');
- $this->assertEquals('product_name', $desc['product_name']['COLUMN_NAME'], 'Expected column name to be product_name');
- $this->assertEquals(2, $desc['product_name']['COLUMN_POSITION'], 'Expected column position to be 2');
- $this->assertRegExp('/varchar/i', $desc['product_name']['DATA_TYPE'], 'Expected data type to be VARCHAR');
- $this->assertEquals('', $desc['product_name']['DEFAULT'], 'Expected default to be empty string');
- $this->assertTrue( $desc['product_name']['NULLABLE'], 'Expected product_name to be nullable');
- $this->assertNull( $desc['product_name']['SCALE'], 'Expected scale to be null');
- $this->assertNull( $desc['product_name']['PRECISION'], 'Expected precision to be null');
- $this->assertFalse( $desc['product_name']['PRIMARY'], 'Expected product_name not to be a primary key');
- $this->assertNull( $desc['product_name']['PRIMARY_POSITION'], 'Expected product_name to return null for PRIMARY_POSITION');
- $this->assertFalse( $desc['product_name']['IDENTITY'], 'Expected product_name to return false for IDENTITY');
- }
- /**
- * Test Adapter's describeTable() method.
- * Retrieve the adapter's description of the test table and examine it.
- */
- public function testAdapterDescribeTableHasColumns()
- {
- $desc = $this->_db->describeTable('zfproducts');
- $cols = array(
- 'product_id',
- 'product_name'
- );
- $this->assertEquals($cols, array_keys($desc));
- }
- public function testAdapterDescribeTableMetadataFields()
- {
- $desc = $this->_db->describeTable('zfproducts');
- $keys = array(
- 'SCHEMA_NAME',
- 'TABLE_NAME',
- 'COLUMN_NAME',
- 'COLUMN_POSITION',
- 'DATA_TYPE',
- 'DEFAULT',
- 'NULLABLE',
- 'LENGTH',
- 'SCALE',
- 'PRECISION',
- 'UNSIGNED',
- 'PRIMARY',
- 'PRIMARY_POSITION',
- 'IDENTITY'
- );
- $this->assertEquals($keys, array_keys($desc['product_name']));
- }
- /**
- * Test that an auto-increment key reports itself.
- * This is not supported in all RDBMS brands, so we test
- * it separately from the full describe tests above.
- */
- public function testAdapterDescribeTablePrimaryAuto()
- {
- $desc = $this->_db->describeTable('zfproducts');
- $auto = $desc['product_id']['IDENTITY'];
- if ($auto === null) {
- $this->markTestIncomplete($this->getDriver() . ' needs to learn how to discover auto-increment keys');
- }
- $this->assertTrue($desc['product_id']['IDENTITY']);
- }
- public function testAdapterDescribeTablePrimaryKeyColumn()
- {
- $desc = $this->_db->describeTable('zfproducts');
- $this->assertEquals('zfproducts', $desc['product_id']['TABLE_NAME'], 'Expected table name to be zfproducts');
- $this->assertEquals('product_id', $desc['product_id']['COLUMN_NAME'], 'Expected column name to be product_id');
- $this->assertEquals(1, $desc['product_id']['COLUMN_POSITION'], 'Expected column position to be 1');
- $this->assertEquals('', $desc['product_id']['DEFAULT'], 'Expected default to be empty string');
- $this->assertFalse( $desc['product_id']['NULLABLE'], 'Expected product_id not to be nullable');
- $this->assertNull( $desc['product_id']['SCALE'], 'Expected scale to be null');
- $this->assertNull( $desc['product_id']['PRECISION'], 'Expected precision to be null');
- $this->assertTrue( $desc['product_id']['PRIMARY'], 'Expected product_id to be a primary key');
- $this->assertEquals(1, $desc['product_id']['PRIMARY_POSITION']);
- }
- /**
- * @group ZF-2927
- */
- public function testAdapterDescribeView()
- {
- $describe = $this->_db->describeTable('temp_view');
- $this->assertEquals(8, count($describe));
- }
- /**
- * Test the Adapter's fetchAll() method.
- */
- public function testAdapterFetchAll()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $result = $this->_db->fetchAll("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1);
- $this->assertEquals(2, count($result));
- $this->assertEquals('2', $result[0]['product_id']);
- }
- /**
- * Test that fetchAssoc() still fetched an associative array
- * after the adapter's default fetch mode is set to something else.
- */
- public function testAdapterFetchAllOverrideFetchMode()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $col_name = $this->_db->foldCase('product_id');
- $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
- // Test associative array
- $result = $this->_db->fetchAll("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1, Zend_Db::FETCH_ASSOC);
- $this->assertEquals(2, count($result));
- $this->assertTrue(is_array($result[0]));
- $this->assertEquals(2, count($result[0])); // count columns
- $this->assertEquals(2, $result[0][$col_name]);
- // Test numeric and associative array
- $result = $this->_db->fetchAll("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1, Zend_Db::FETCH_BOTH);
- $this->assertEquals(2, count($result));
- $this->assertTrue(is_array($result[0]));
- $this->assertEquals(4, count($result[0])); // count columns
- $this->assertEquals(2, $result[0][$col_name]);
- $this->assertEquals(2, $result[0][0]);
- // Ensure original fetch mode has been retained
- $result = $this->_db->fetchAll("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id", 1);
- $this->assertEquals(2, count($result));
- $this->assertTrue(is_object($result[0]));
- $this->assertEquals(2, $result[0]->$col_name);
- }
- /**
- * Test the Adapter's fetchAssoc() method.
- */
- public function testAdapterFetchAssoc()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $result = $this->_db->fetchAssoc("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id DESC", 1);
- foreach ($result as $idKey => $row) {
- $this->assertEquals($idKey, $row['product_id']);
- }
- }
- /**
- * Test that fetchAssoc() still fetched an associative array
- * after the adapter's default fetch mode is set to something else.
- */
- public function testAdapterFetchAssocAfterSetFetchMode()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
- $result = $this->_db->fetchAssoc("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id DESC", 1);
- $this->assertTrue(is_array($result));
- $this->assertEquals(array('product_id', 'product_name'), array_keys(current($result)));
- }
- /**
- * Test the Adapter's fetchCol() method.
- */
- public function testAdapterFetchCol()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $result = $this->_db->fetchCol("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1);
- $this->assertTrue(is_array($result));
- $this->assertEquals(2, count($result)); // count rows
- $this->assertEquals(2, $result[0]);
- $this->assertEquals(3, $result[1]);
- }
- /**
- * Test that fetchCol() still fetched an associative array
- * after the adapter's default fetch mode is set to something else.
- */
- public function testAdapterFetchColAfterSetFetchMode()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
- $result = $this->_db->fetchCol("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1);
- $this->assertTrue(is_array($result));
- $this->assertEquals(2, count($result)); // count rows
- $this->assertEquals(2, $result[0]);
- $this->assertEquals(3, $result[1]);
- }
- /**
- * Test the Adapter's fetchOne() method.
- */
- public function testAdapterFetchOne()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $product_name = $this->_db->quoteIdentifier('product_name');
- $prod = 'Linux';
- $result = $this->_db->fetchOne("SELECT $product_name FROM $products WHERE $product_id > ? ORDER BY $product_id", 1);
- $this->assertEquals($prod, $result);
- }
- /**
- * Test that fetchCol() still fetched an associative array
- * after the adapter's default fetch mode is set to something else.
- */
- public function testAdapterFetchOneAfterSetFetchMode()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $product_name = $this->_db->quoteIdentifier('product_name');
- $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
- $prod = 'Linux';
- $result = $this->_db->fetchOne("SELECT $product_name FROM $products WHERE $product_id > ? ORDER BY $product_id", 1);
- $this->assertTrue(is_string($result));
- $this->assertEquals($prod, $result);
- }
- /**
- * Test the Adapter's fetchPairs() method.
- */
- public function testAdapterFetchPairs()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $product_name = $this->_db->quoteIdentifier('product_name');
- $prod = 'Linux';
- $result = $this->_db->fetchPairs("SELECT $product_id, $product_name FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1);
- $this->assertEquals(2, count($result)); // count rows
- $this->assertEquals($prod, $result[2]);
- }
- /**
- * Test the Adapter's fetchPairs() method.
- */
- public function testAdapterFetchPairsAfterSetFetchMode()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $product_name = $this->_db->quoteIdentifier('product_name');
- $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
- $prod = 'Linux';
- $result = $this->_db->fetchPairs("SELECT $product_id, $product_name FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1);
- $this->assertTrue(is_array($result));
- $this->assertEquals(2, count($result)); // count rows
- $this->assertEquals($prod, $result[2]);
- }
- /**
- * Test the Adapter's fetchRow() method.
- */
- public function testAdapterFetchRow()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $result = $this->_db->fetchRow("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id", 1);
- $this->assertEquals(2, count($result)); // count columns
- $this->assertEquals(2, $result['product_id']);
- }
- /**
- * Test that fetchAssoc() still fetched an associative array
- * after the adapter's default fetch mode is set to something else.
- */
- public function testAdapterFetchRowOverrideFetchMode()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $col_name = $this->_db->foldCase('product_id');
- $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
- // Test associative array
- $result = $this->_db->fetchRow("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id", 1, Zend_Db::FETCH_ASSOC);
- $this->assertTrue(is_array($result));
- $this->assertEquals(2, count($result)); // count columns
- $this->assertEquals(2, $result['product_id']);
- // Test numeric and associative array
- $result = $this->_db->fetchRow("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id", 1, Zend_Db::FETCH_BOTH);
- $this->assertTrue(is_array($result));
- $this->assertEquals(4, count($result)); // count columns
- $this->assertEquals(2, $result['product_id']);
- $this->assertEquals(2, $result[0]);
- // Ensure original fetch mode has been retained
- $result = $this->_db->fetchRow("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id", 1);
- $this->assertTrue(is_object($result));
- $this->assertEquals(2, $result->$col_name);
- }
- /**
- * Test the Adapter's insert() method.
- * This requires providing an associative array of column=>value pairs.
- */
- public function testAdapterInsert()
- {
- $row = array (
- 'bug_description' => 'New bug',
- 'bug_status' => 'NEW',
- 'created_on' => '2007-04-02',
- 'updated_on' => '2007-04-02',
- 'reported_by' => 'micky',
- 'assigned_to' => 'goofy',
- 'verified_by' => 'dduck'
- );
- $rowsAffected = $this->_db->insert('zfbugs', $row);
- $this->assertEquals(1, $rowsAffected);
- $lastInsertId = $this->_db->lastInsertId();
- $this->assertTrue(is_string($lastInsertId));
- $this->assertEquals('5', (string) $lastInsertId,
- 'Expected new id to be 5');
- }
- public function testAdapterInsertSequence()
- {
- $row = array (
- 'product_id' => $this->_db->nextSequenceId('zfproducts_seq'),
- 'product_name' => 'Solaris',
- );
- $rowsAffected = $this->_db->insert('zfproducts', $row);
- $this->assertEquals(1, $rowsAffected);
- $lastInsertId = $this->_db->lastInsertId('zfproducts');
- $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
- $this->assertTrue(is_string($lastInsertId));
- $this->assertTrue(is_string($lastSequenceId));
- $this->assertEquals('4', (string) $lastInsertId, 'Expected new id to be 4');
- }
- public function testAdapterInsertDbExpr()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- $expr = new Zend_Db_Expr('2+3');
- $row = array (
- 'bug_id' => $expr,
- 'bug_description' => 'New bug',
- 'bug_status' => 'NEW',
- 'created_on' => '2007-04-02',
- 'updated_on' => '2007-04-02',
- 'reported_by' => 'micky',
- 'assigned_to' => 'goofy',
- 'verified_by' => 'dduck'
- );
- $rowsAffected = $this->_db->insert('zfbugs', $row);
- $this->assertEquals(1, $rowsAffected);
- $value = $this->_db->fetchOne("SELECT $bug_id FROM $bugs WHERE $bug_id = 5");
- $this->assertEquals(5, $value);
- }
- /**
- * Test the Adapter's limit() method.
- * Fetch 1 row. Then fetch 1 row offset by 1 row.
- * @group ZF-4246
- */
- public function testAdapterLimit()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $sql = $this->_db->limit("SELECT * FROM $products ORDER BY $product_id", 1);
- $stmt = $this->_db->query($sql);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result),
- 'Expecting row count to be 1');
- $this->assertEquals(2, count($result[0]),
- 'Expecting column count to be 2');
- $this->assertEquals(1, $result[0]['product_id'],
- 'Expecting to get product_id 1');
- // Check that extra field ZEND_DB_ROWNUM isn't present
- // (particulary with Db2 & Oracle)
- $this->assertArrayNotHasKey('zend_db_rownum', $result[0]);
- $this->assertArrayNotHasKey('ZEND_DB_ROWNUM', $result[0]);
- }
- /**
- * @group ZF-4246
- */
- public function testAdapterLimitOffset()
- {
- $products = $this->_db->quoteIdentifier('zfproducts');
- $product_id = $this->_db->quoteIdentifier('product_id');
- $sql = $this->_db->limit("SELECT * FROM $products ORDER BY $product_id", 1, 1);
- $stmt = $this->_db->query($sql);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, count($result),
- 'Expecting row count to be 1');
- $this->assertEquals(2, count($result[0]),
- 'Expecting column count to be 2');
- $this->assertEquals(2, $result[0]['product_id'],
- 'Expecting to get product_id 2');
- // Check that extra field ZEND_DB_ROWNUM isn't present
- // (particulary with Db2 & Oracle)
- $this->assertArrayNotHasKey('zend_db_rownum', $result[0]);
- $this->assertArrayNotHasKey('ZEND_DB_ROWNUM', $result[0]);
- }
- public function testAdapterLimitInvalidArgumentException()
- {
- try {
- $sql = $this->_db->limit('SELECT * FROM zfproducts', 0);
- $this->fail('Expected to catch Zend_Db_Adapter_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Adapter_Exception,
- 'Expecting object of type Zend_Db_Adapter_Exception, got '.get_class($e));
- }
- try {
- $sql = $this->_db->limit('SELECT * FROM zfproducts', 1, -1);
- $this->fail('Expected to catch Zend_Db_Adapter_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Adapter_Exception,
- 'Expecting object of type Zend_Db_Adapter_Exception, got '.get_class($e));
- }
- }
- /**
- * Test the Adapter's listTables() method.
- * Fetch the list of tables and verify that the test table exists in
- * the list.
- */
- public function testAdapterListTables()
- {
- $tables = $this->_db->listTables();
- $this->assertContains('zfproducts', $tables);
- }
- /**
- * Used by _testAdapterOptionCaseFoldingNatural()
- * DB2 and Oracle return identifiers in uppercase naturally,
- * so those test suites will override this method.
- */
- protected function _testAdapterOptionCaseFoldingNaturalIdentifier()
- {
- return 'case_folded_identifier';
- }
- /**
- * Used by _testAdapterOptionCaseFoldingNatural()
- * SQLite needs to do metadata setup,
- * because it uses the in-memory database,
- * so that test suite will override this method.
- */
- protected function _testAdapterOptionCaseFoldingSetup(Zend_Db_Adapter_Abstract $db)
- {
- $db->getConnection();
- }
- /**
- * Used by:
- * - testAdapterOptionCaseFoldingNatural()
- * - testAdapterOptionCaseFoldingUpper()
- * - testAdapterOptionCaseFoldingLower()
- *
- * @param int $case
- * @return string
- */
- public function _testAdapterOptionCaseFoldingCommon($case)
- {
- $params = $this->_util->getParams();
- $params['options'] = array(
- Zend_Db::CASE_FOLDING => $case
- );
- $db = Zend_Db::factory($this->getDriver(), $params);
- $this->_testAdapterOptionCaseFoldingSetup($db);
- $products = $db->quoteIdentifier('zfproducts');
- $product_name = $db->quoteIdentifier('product_name');
- /*
- * it is important not to delimit the pname identifier
- * in the following query
- */
- $sql = "SELECT $product_name AS case_folded_identifier FROM $products";
- $stmt = $db->query($sql);
- $result = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
- $keys = array_keys($result[0]);
- return $keys[0];
- }
- /**
- * Test the connection's CASE_FOLDING option.
- * Case: Zend_Db::CASE_NATURAL
- */
- public function testAdapterOptionCaseFoldingNatural()
- {
- $natural = $this->_testAdapterOptionCaseFoldingCommon(Zend_Db::CASE_NATURAL);
- $expected = $this->_testAdapterOptionCaseFoldingNaturalIdentifier();
- $this->assertEquals($natural, $expected, 'Natural case does not match');
- }
- /**
- * Test the connection's CASE_FOLDING option.
- * Case: Zend_Db::CASE_UPPER
- */
- public function testAdapterOptionCaseFoldingUpper()
- {
- $upper = $this->_testAdapterOptionCaseFoldingCommon(Zend_Db::CASE_UPPER);
- $expected = strtoupper($this->_testAdapterOptionCaseFoldingNaturalIdentifier());
- $this->assertEquals($upper, $expected, 'Upper case does not match');
- }
- /**
- * Test the connection's CASE_FOLDING option.
- * Case: Zend_Db::CASE_LOWER
- */
- public function testAdapterOptionCaseFoldingLower()
- {
- $lower = $this->_testAdapterOptionCaseFoldingCommon(Zend_Db::CASE_LOWER);
- $expected = strtolower($this->_testAdapterOptionCaseFoldingNaturalIdentifier());
- $this->assertEquals($lower, $expected, 'Lower case does not match');
- }
- /**
- * Test the connection's CASE_FOLDING option.
- * Case: invalid value throws exception
- */
- public function testAdapterOptionCaseFoldingInvalidException()
- {
- try {
- $lower = $this->_testAdapterOptionCaseFoldingCommon(-999);
- $this->fail('Expected exception not thrown');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Adapter_Exception,
- 'Expecting object of type Zend_Db_Adapter_Exception, got '.get_class($e));
- $this->assertEquals("Case must be one of the following constants: Zend_Db::CASE_NATURAL, Zend_Db::CASE_LOWER, Zend_Db::CASE_UPPER", $e->getMessage());
- }
- }
- /**
- * Test that we can query twice in a row.
- * That is, a query doesn't leave the adapter in an unworking state.
- * See bug ZF-1778.
- */
- public function testAdapterQueryAfterUnclosedQuery()
- {
- $select = $this->_db->select()
- ->from('zfproducts')
- ->order('product_id');
- $stmt1 = $this->_db->query($select);
- $result1 = $stmt1->fetchAll(Zend_Db::FETCH_NUM);
- $stmt1 = $this->_db->query($select);
- $result2 = $stmt1->fetchAll(Zend_Db::FETCH_NUM);
- $this->assertEquals($result1, $result2);
- }
- /**
- * Ensures that query() throws an exception when given a bogus query
- *
- * @return void
- */
- public function testAdapterQueryBogus()
- {
- try {
- $this->_db->query('Bogus query');
- $this->fail('Expected exception not thrown');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Statement_Exception,
- 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
- }
- }
- /**
- * Ensures that query() throws an exception when given a bogus table
- *
- * @return void
- */
- public function testAdapterQueryBogusTable()
- {
- try {
- $this->_db->query('SELECT * FROM BogusTable');
- $this->fail('Expected exception not thrown');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Statement_Exception,
- 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
- }
- }
- /**
- * Ensures that query() provides expected behavior when returning no results
- *
- * @return void
- */
- public function testAdapterQueryResultsNone()
- {
- $stmt = $this->_db->query('SELECT * FROM ' . $this->_db->quoteIdentifier('zfbugs') . ' WHERE '
- . $this->_db->quoteIdentifier('bug_id') . ' = -1');
- $this->assertTrue(is_object($stmt),
- 'Expected query() to return object; got ' . gettype($stmt));
- $this->assertTrue($stmt instanceof Zend_Db_Statement_Interface,
- 'Expected query() to return Zend_Db_Statement or PDOStatement; got ' . get_class($stmt));
- $this->assertEquals(0, $count = count($stmt->fetchAll()),
- "Expected fetchAll() to return zero rows; got $count");
- }
- /**
- * Test that quote() accepts a string and returns
- * a quoted string.
- */
- public function testAdapterQuote()
- {
- $string = 'String without quotes';
- $value = $this->_db->quote($string);
- $this->assertEquals("'String without quotes'", $value);
- }
- /**
- * Test that quote() takes an array and returns
- * an imploded string of comma-separated, quoted elements.
- */
- public function testAdapterQuoteArray()
- {
- $array = array("it's", 'all', 'right!');
- $value = $this->_db->quote($array);
- $this->assertEquals("'it\\'s', 'all', 'right!'", $value);
- }
- /**
- * test that quote() accepts a Zend_Db_Expr
- * and returns the string representation,
- * with no quoting applied.
- */
- public function testAdapterQuoteDbExpr()
- {
- $string = 'String with "`\' quotes';
- $expr = new Zend_Db_Expr($string);
- $value = $this->_db->quote($expr);
- $this->assertEquals($string, $value);
- }
- /**
- * test that quote() accepts a string containing
- * digits and returns an unquoted string.
- */
- public function testAdapterQuoteDigitString()
- {
- $string = '123';
- $value = $this->_db->quote($string);
- $this->assertEquals("'123'", $value);
- }
- /**
- * test that quote() escapes a double-quote
- * character in a string.
- */
- public function testAdapterQuoteDoubleQuote()
- {
- $string = 'St John"s Wort';
- $value = $this->_db->quote($string);
- $this->assertEquals("'St John\\\"s Wort'", $value);
- }
- /**
- * test that quote() accepts an integer and
- * returns an unquoted integer.
- */
- public function testAdapterQuoteInteger()
- {
- $int = 123;
- $value = $this->_db->quote($int);
- $this->assertEquals(123, $value);
- }
- /**
- * test that quote() accepts an array and returns
- * an imploded string of unquoted elements
- */
- public function testAdapterQuoteIntegerArray()
- {
- $array = array(1,'2',3);
- $value = $this->_db->quote($array);
- $this->assertEquals("1, '2', 3", $value);
- }
- /**
- * test that quote() escapes a single-quote
- * character in a string.
- */
- public function testAdapterQuoteSingleQuote()
- {
- $string = "St John's Wort";
- $value = $this->_db->quote($string);
- $this->assertEquals("'St John\'s Wort'", $value);
- }
- /**
- * test that quoteColumnAs() accepts a string
- * and an alias, and returns each as delimited
- * identifiers, with 'AS' in between.
- */
- public function testAdapterQuoteColumnAs()
- {
- $string = "foo";
- $alias = "bar";
- $value = $this->_db->quoteColumnAs($string, $alias);
- $this->assertEquals('"foo" AS "bar"', $value);
- }
- /**
- * test that quoteColumnAs() accepts a string
- * and an alias, but ignores the alias if it is
- * the same as the base identifier in the string.
- */
- public function testAdapterQuoteColumnAsSameString()
- {
- $string = 'foo.bar';
- $alias = 'bar';
- $value = $this->_db->quoteColumnAs($string, $alias);
- $this->assertEquals('"foo"."bar"', $value);
- }
- /**
- * test that quoteIdentifier() accepts a string
- * and returns a delimited identifier.
- */
- public function testAdapterQuoteIdentifier()
- {
- $string = 'table_name';
- $value = $this->_db->quoteIdentifier($string);
- $this->assertEquals('"table_name"', $value);
- }
- /**
- * test that quoteIdentifier() accepts an array
- * and returns a qualified delimited identifier.
- */
- public function testAdapterQuoteIdentifierArray()
- {
- $array = array('foo', 'bar');
- $value = $this->_db->quoteIdentifier($array);
- $this->assertEquals('"foo"."bar"', $value);
- }
- /**
- * test that quoteIdentifier() accepts an array
- * containing a Zend_Db_Expr, and returns strings
- * as delimited identifiers, and Exprs as unquoted.
- */
- public function testAdapterQuoteIdentifierArrayDbExpr()
- {
- $expr = new Zend_Db_Expr('*');
- $array = array('foo', $expr);
- $value = $this->_db->quoteIdentifier($array);
- $this->assertEquals('"foo".*', $value);
- }
- /**
- * test that quoteIdentifier() accepts a Zend_Db_Expr
- * and returns the string representation,
- * with no quoting applied.
- */
- public function testAdapterQuoteIdentifierDbExpr()
- {
- $string = 'String with "`\' quotes';
- $expr = new Zend_Db_Expr($string);
- $value = $this->_db->quoteIdentifier($expr);
- $this->assertEquals($string, $value);
- }
- /**
- * test that quoteIdentifer() escapes a double-quote
- * character in a string.
- */
- public function testAdapterQuoteIdentifierDoubleQuote()
- {
- $string = 'table_"_name';
- $value = $this->_db->quoteIdentifier($string);
- $this->assertEquals('"table_""_name"', $value);
- }
- /**
- * test that quoteIdentifer() accepts an integer
- * and returns a delimited identifier as with a string.
- */
- public function testAdapterQuoteIdentifierInteger()
- {
- $int = 123;
- $value = $this->_db->quoteIdentifier($int);
- $this->assertEquals('"123"', $value);
- }
- /**
- * test that quoteIdentifier() accepts a string
- * containing a dot (".") character, splits the
- * string, quotes each segment individually as
- * delimited identifers, and returns the imploded
- * string.
- */
- public function testAdapterQuoteIdentifierQualified()
- {
- $string = 'table.column';
- $value = $this->_db->quoteIdentifier($string);
- $this->assertEquals('"table"."column"', $value);
- }
- /**
- * test that quoteIdentifer() escapes a single-quote
- * character in a string.
- */
- public function testAdapterQuoteIdentifierSingleQuote()
- {
- $string = "table_'_name";
- $value = $this->_db->quoteIdentifier($string);
- $this->assertEquals('"table_\'_name"', $value);
- }
- /**
- * test that quoteTableAs() accepts a string and an alias,
- * and returns each as delimited identifiers.
- * Most RDBMS want an 'AS' in between.
- */
- public function testAdapterQuoteTableAs()
- {
- $string = "foo";
- $alias = "bar";
- $value = $this->_db->quoteTableAs($string, $alias);
- $this->assertEquals('"foo" AS "bar"', $value);
- }
- /**
- * test that quoteInto() accepts a Zend_Db_Expr
- * and returns the string representation,
- * with no quoting applied.
- */
- public function testAdapterQuoteIntoDbExpr()
- {
- $string = 'id=?';
- $expr = new Zend_Db_Expr('CURDATE()');
- $value = $this->_db->quoteInto($string, $expr);
- $this->assertEquals("id=CURDATE()", $value);
- }
- /**
- * test that quoteInto() escapes a double-quote
- * character in a string.
- */
- public function testAdapterQuoteIntoDoubleQuote()
- {
- $string = 'id=?';
- $param = 'St John"s Wort';
- $value = $this->_db->quoteInto($string, $param);
- $this->assertEquals("id='St John\\\"s Wort'", $value);
- }
- /**
- * test that quoteInto() escapes a single-quote
- * character in a string.
- */
- public function testAdapterQuoteIntoSingleQuote()
- {
- $string = 'id = ?';
- $param = 'St John\'s Wort';
- $value = $this->_db->quoteInto($string, $param);
- $this->assertEquals("id = 'St John\\'s Wort'", $value);
- }
- protected $_numericDataTypes = array(
- Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
- Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
- Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE
- );
- public function testAdapterQuoteIntoType()
- {
- $value = $this->_db->quoteInto('foo = ?', 1234, Zend_Db::INT_TYPE);
- $this->assertTrue(is_string($value));
- $this->assertEquals('foo = 1234', $value,
- 'Incorrect quoteInto() result for INT_TYPE');
- $value = $this->_db->quoteInto('foo = ?', 1234, Zend_Db::BIGINT_TYPE);
- $this->assertTrue(is_string($value));
- $this->assertEquals('foo = 1234', $value,
- 'Incorrect quoteInto() result for BIGINT_TYPE');
- $value = $this->_db->quoteInto('foo = ?', 12.34, Zend_Db::FLOAT_TYPE);
- $this->assertTrue(is_string($value));
- $this->assertEquals('foo = 12.340000', $value,
- 'Incorrect quoteInto() result for FLOAT_TYPE');
- $value = $this->_db->quoteInto('foo = ?', 1234, 'CHAR');
- $this->assertTrue(is_string($value));
- $this->assertEquals("foo = 1234", $value,
- 'Incorrect quoteInto() result for CHAR');
- $value = $this->_db->quoteInto('foo = ?', '1234', 'CHAR');
- $this->assertTrue(is_string($value));
- $this->assertEquals("foo = '1234'", $value,
- 'Incorrect quoteInto() result for CHAR');
- }
- public function testAdapterQuoteIntoCount()
- {
- $value = $this->_db->quoteInto('foo = ? and bar = ?', 1234, null, 1);
- $this->assertTrue(is_string($value));
- $this->assertEquals('foo = 1234 and bar = ?', $value,
- 'Incorrect quoteInto() result for count');
- }
- public function testAdapterQuoteIntoCountAndQuestionMark()
- {
- $string = 'foo = ? OR moo = ? OR boo = ?';
- $param = 'What?';
- $value = $this->_db->quoteInto($string, $param, null, 2);
- $this->assertTrue(is_string($value));
- $this->assertEquals("foo = 'What?' OR moo = 'What?' OR boo = ?", $value,
- 'Incorrect quoteInto() result for count and question mark in value');
- }
- public function testAdapterQuoteTypeInt()
- {
- foreach ($this->_numericDataTypes as $typeName => $type) {
- if ($type != 0) {
- continue;
- }
- // test int value
- $value = $this->_db->quote(1234, $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('1234', $value,
- "Incorrect quote() INT_TYPE result for int");
- // test int string
- $value = $this->_db->quote('1234', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('1234', $value,
- "Incorrect quote() INT_TYPE result for int string");
- // test int string with + sign
- $value = $this->_db->quote('+1234', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('1234', $value,
- "Incorrect quote() INT_TYPE result for int string with + sign");
- // test int string with - sign
- $value = $this->_db->quote('-1234', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('-1234', $value,
- "Incorrect quote() INT_TYPE result for int string with - sign");
- // test int string with non-digit text
- $value = $this->_db->quote('1234abcd', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('1234', $value,
- "Incorrect quote() INT_TYPE result for int string with non-digit text");
- // test non-digit test; it should return 0
- $value = $this->_db->quote('abcd', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('0', $value,
- "Incorrect quote() INT_TYPE result for non-digit string");
- // test Zend_Db_Expr is passed through literally
- $value = $this->_db->quote(new Zend_Db_Expr('1+2+3'), $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals("1+2+3", $value,
- "Incorrect quote() INT_TYPE result for Zend_Db_Expr");
- }
- }
- public function testAdapterQuoteTypeBigInt()
- {
- foreach ($this->_numericDataTypes as $typeName => $type) {
- if ($type != 1) {
- continue;
- }
- // test int value
- $value = $this->_db->quote(1234, $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals(1234, $value,
- "Incorrect quote() BIGINT_TYPE result for int");
- // test int string
- $value = $this->_db->quote('1234', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('1234', $value,
- "Incorrect quote() BIGINT_TYPE result for int string");
- // test int string with + sign
- $value = $this->_db->quote('+1234', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('+1234', $value,
- "Incorrect quote() BIGINT_TYPE result for int string");
- // test int string with - sign
- $value = $this->_db->quote('-1234', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('-1234', $value,
- "Incorrect quote() BIGINT_TYPE result for int string");
- // test int string with positive exponent
- $value = $this->_db->quote('1234e15', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('1234e15', $value,
- "Incorrect quote() BIGINT_TYPE result for int string");
- $value = $this->_db->quote('1234e+15', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('1234e+15', $value,
- "Incorrect quote() BIGINT_TYPE result for int string");
- // test int value with negative exponent
- $value = $this->_db->quote('1234e-15', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('1234e-15', $value,
- "Incorrect quote() BIGINT_TYPE result for int string");
- // test bigint value
- $value = $this->_db->quote('2200000000', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('2200000000', $value,
- "Incorrect quote() BIGINT_TYPE result for big int string");
- // test octal value with leading zeroes
- $value = $this->_db->quote('020310253000', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('020310253000', $value,
- "Incorrect quote() BIGINT_TYPE result for big octal int string with leading zeroes");
- // test decimal value with leading zeroes
- $value = $this->_db->quote('09091', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('09091', $value,
- "Incorrect quote() BIGINT_TYPE result for decimal int string with leading zeroes");
- // test hex value with ODBC-style notation
- $value = $this->_db->quote('0x83215600', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('0x83215600', $value,
- "Incorrect quote() BIGINT_TYPE result for big hex int string");
- // test non-numeric value; it should be zero
- $value = $this->_db->quote('abcd', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('0', $value,
- "Incorrect quote() BIGINT_TYPE result for non-digt string");
- // test Zend_Db_Expr is passed through literally
- $value = $this->_db->quote(new Zend_Db_Expr('1+2+3'), $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals("1+2+3", $value,
- "Incorrect quote() BIGINT_TYPE result for Zend_Db_Expr");
- }
- }
- public function testAdapterQuoteTypeFloat()
- {
- foreach ($this->_numericDataTypes as $typeName => $type) {
- if ($type != 2) {
- continue;
- }
- $value = $this->_db->quote(12.34, $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('12.34', $value,
- "Incorrect quote() FLOAT_TYPE result");
- $value = $this->_db->quote('12.34', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('12.34', $value,
- "Incorrect quote() FLOAT_TYPE result");
- $value = $this->_db->quote('+12.34', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('12.34', $value,
- "Incorrect quote() FLOAT_TYPE result");
- $value = $this->_db->quote('-12.34', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('-12.34', $value,
- "Incorrect quote() FLOAT_TYPE result");
- $value = $this->_db->quote('12.34abcd', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('12.34', $value,
- "Incorrect quote() FLOAT_TYPE result");
- $value = $this->_db->quote('abcd', $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals('0', $value,
- "Incorrect quote() FLOAT_TYPE result");
- $value = $this->_db->quote(new Zend_Db_Expr('1+2+3'), $typeName);
- $this->assertTrue(is_string($value));
- $this->assertEquals("1+2+3", $value,
- "Incorrect quote() FLOAT_TYPE result");
- }
- }
- public function testAdapterQuoteTypeNonNumeric()
- {
- $value = $this->_db->quote(1234, 'CHAR');
- $this->assertTrue(is_int($value));
- $this->assertEquals(1234, $value,
- "Incorrect quote() CHAR type result");
- $value = $this->_db->quote('1234', 'CHAR');
- $this->assertTrue(is_string($value));
- $this->assertEquals("'1234'", $value,
- "Incorrect quote() CHAR type result");
- $value = $this->_db->quote('1234abcd', 'CHAR');
- $this->assertTrue(is_string($value));
- $this->assertEquals("'1234abcd'", $value,
- "Incorrect quote() CHAR type result");
- $value = $this->_db->quote('1234abcd56', 'CHAR');
- $this->assertTrue(is_string($value));
- $this->assertEquals("'1234abcd56'", $value,
- "Incorrect quote() CHAR type result");
- // test Zend_Db_Expr is passed through literally
- $value = $this->_db->quote(new Zend_Db_Expr('1+2+3'), 'CHAR');
- $this->assertTrue(is_string($value));
- $this->assertEquals("1+2+3", $value,
- "Incorrect quote() CHAR type result for Zend_Db_Expr");
- }
- /**
- * Ensures that the character sequence ":0'" is handled properly
- *
- * @link http://framework.zend.com/issues/browse/ZF-2059
- * @return void
- */
- public function testZF2059()
- {
- $value = ':0\'';
- $valueQuoted = $this->_db->quote($value);
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- $bug_status = $this->_db->quoteIdentifier('bug_status');
- $sql = "SELECT $bug_id FROM $bugs WHERE $bug_status != " . $valueQuoted;
- $results = $this->_db->fetchAll($sql);
- $this->assertEquals(4, count($results));
- }
- public function testAdapterSetFetchMode()
- {
- $modes = array(
- Zend_Db::FETCH_ASSOC,
- Zend_Db::FETCH_BOTH,
- Zend_Db::FETCH_NUM,
- Zend_Db::FETCH_OBJ
- );
- foreach ($modes as $mode) {
- $this->_db->setFetchMode($mode);
- $this->assertEquals($mode, $this->_db->getFetchMode());
- }
- }
- public function testAdapterSetFetchModeInvalidException()
- {
- try {
- $this->_db->setFetchMode(-999);
- $this->fail('Expected exception not thrown');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Adapter_Exception,
- 'Expecting object of type Zend_Db_Adapter_Exception, got '.get_class($e));
- $this->assertEquals("Invalid fetch mode '-999' specified", $e->getMessage());
- }
- }
- public function testAdapterTransactionAutoCommit()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- // use our default connection as the Connection1
- $dbConnection1 = $this->_db;
- // create a second connection to the same database
- $dbConnection2 = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
- $dbConnection2->getConnection();
- // notice the number of rows in connection 2
- $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
- $this->assertEquals(4, $count);
- // delete a row in connection 1
- $rowsAffected = $dbConnection1->delete(
- 'zfbugs',
- "$bug_id = 1"
- );
- $this->assertEquals(1, $rowsAffected);
- // we should see the results in connection 2 immediately
- // after the DELETE executes, because it's autocommit
- $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
- $this->assertEquals(3, $count);
- }
- public function testAdapterTransactionCommit()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- // use our default connection as the Connection1
- $dbConnection1 = $this->_db;
- // create a second connection to the same database
- $dbConnection2 = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
- $dbConnection2->getConnection();
- // notice the number of rows in connection 2
- $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
- $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
- // start an explicit transaction in connection 1
- $dbConnection1->beginTransaction();
- // delete a row in connection 1
- $rowsAffected = $dbConnection1->delete(
- 'zfbugs',
- "$bug_id = 1"
- );
- $this->assertEquals(1, $rowsAffected);
- // we should still see all rows in connection 2
- // because the DELETE has not been committed yet
- $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
- $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
- // commit the DELETE
- $dbConnection1->commit();
- // now we should see one fewer rows in connection 2
- $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
- $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 3)');
- // delete another row in connection 1
- $rowsAffected = $dbConnection1->delete(
- 'zfbugs',
- "$bug_id = 2"
- );
- $this->assertEquals(1, $rowsAffected);
- // we should see results immediately, because
- // the db connection returns to auto-commit mode
- $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
- $this->assertEquals(2, $count);
- }
- public function testAdapterTransactionRollback()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- // use our default connection as the Connection1
- $dbConnection1 = $this->_db;
- // create a second connection to the same database
- $dbConnection2 = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
- $dbConnection2->getConnection();
- // notice the number of rows in connection 2
- $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
- $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
- // start an explicit transaction in connection 1
- $dbConnection1->beginTransaction();
- // delete a row in connection 1
- $rowsAffected = $dbConnection1->delete(
- 'zfbugs',
- "$bug_id = 1"
- );
- $this->assertEquals(1, $rowsAffected);
- // we should still see all rows in connection 2
- // because the DELETE has not been committed yet
- $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
- $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
- // rollback the DELETE
- $dbConnection1->rollback();
- // now we should see the same number of rows
- // because the DELETE was rolled back
- $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
- $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table after DELETE is rolled back (step 3)');
- // delete another row in connection 1
- $rowsAffected = $dbConnection1->delete(
- 'zfbugs',
- "$bug_id = 2"
- );
- $this->assertEquals(1, $rowsAffected);
- // we should see results immediately, because
- // the db connection returns to auto-commit mode
- $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
- $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 4)');
- }
- /**
- * Test the Adapter's update() method.
- * Update a single row and verify that the change was made.
- * Attempt to update a row that does not exist, and verify
- * that no change was made.
- */
- public function testAdapterUpdate()
- {
- $product_id = $this->_db->quoteIdentifier('product_id');
- // Test that we can change the values in
- // an existing row.
- $rowsAffected = $this->_db->update(
- 'zfproducts',
- array('product_name' => 'Vista'),
- "$product_id = 1"
- );
- $this->assertEquals(1, $rowsAffected);
- // Query the row to see if we have the new values.
- $select = $this->_db->select();
- $select->from('zfproducts');
- $select->where("$product_id = 1");
- $stmt = $this->_db->query($select);
- $result = $stmt->fetchAll();
- $this->assertEquals(1, $result[0]['product_id']);
- $this->assertEquals('Vista', $result[0]['product_name']);
- // Test that update affects no rows if the WHERE
- // clause matches none.
- $rowsAffected = $this->_db->update(
- 'zfproducts',
- array('product_name' => 'Vista'),
- "$product_id = 327"
- );
- $this->assertEquals(0, $rowsAffected);
- }
- public function testAdapterUpdateSetDbExpr()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- $bug_status = $this->_db->quoteIdentifier('bug_status');
- $expr = new Zend_Db_Expr("UPPER('setExpr')");
- $rowsAffected = $this->_db->update(
- 'zfbugs',
- array('bug_status' => $expr),
- "$bug_id = 1"
- );
- $this->assertEquals(1, $rowsAffected);
- $value = $this->_db->fetchOne("SELECT $bug_status FROM $bugs WHERE $bug_id = 1");
- $this->assertEquals('SETEXPR', $value);
- }
- public function testAdapterUpdateWhereArray()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- $bug_status = $this->_db->quoteIdentifier('bug_status');
- $rowsAffected = $this->_db->update(
- 'zfbugs',
- array('bug_status' => 'ARRAY'),
- array("$bug_id = 1", "$bug_status = 'NEW'")
- );
- $this->assertEquals(1, $rowsAffected);
- $value = $this->_db->fetchOne("SELECT $bug_status FROM $bugs WHERE $bug_id = 1");
- $this->assertEquals('ARRAY', $value);
- }
- /**
- * @group ZF-1726
- */
- public function testAdapterUpdateWhereArrayWithVariable()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- $bug_status = $this->_db->quoteIdentifier('bug_status');
- $rowsAffected = $this->_db->update(
- 'zfbugs',
- array('bug_status' => 'ARRAY'),
- array("$bug_id = ?" => 1, "$bug_status = ?" => 'NEW')
- );
- $this->assertEquals(1, $rowsAffected);
- $value = $this->_db->fetchOne("SELECT $bug_status FROM $bugs WHERE $bug_id = 1");
- $this->assertEquals('ARRAY', $value);
- }
- public function testAdapterUpdateWhereDbExpr()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bug_id = $this->_db->quoteIdentifier('bug_id');
- $bug_status = $this->_db->quoteIdentifier('bug_status');
- $whereExpr = new Zend_Db_Expr("$bug_id = 1");
- $rowsAffected = $this->_db->update(
- 'zfbugs',
- array('bug_status' => 'DBEXPR'),
- $whereExpr
- );
- $this->assertEquals(1, $rowsAffected);
- $value = $this->_db->fetchOne("SELECT $bug_status FROM $bugs WHERE $bug_id = 1");
- $this->assertEquals('DBEXPR', $value);
- }
- public function testAdapterUpdateEmptyWhere()
- {
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $bug_status = $this->_db->quoteIdentifier('bug_status');
- $rowsAffected = $this->_db->update(
- 'zfbugs',
- array('bug_status' => 'EMPTY')
- // intentionally no where clause
- );
- $this->assertEquals(4, $rowsAffected);
- $value = $this->_db->fetchCol("SELECT $bug_status FROM $bugs");
- $this->assertEquals(array_fill(0, 4, 'EMPTY'), $value);
- }
- /**
- * @group ZF-8597
- * Oracle is limited to 30 characters for an identifier
- */
- public function testAdapterUpdateWithLongColumnIdentifier()
- {
- // create test table using no identifier quoting
- $this->_util->createTable('zf_longidentifier', array(
- 'id' => 'INTEGER NOT NULL',
- 'veryveryveryverylongidentifier' => 'INTEGER NOT NULL'
- ));
- $tableName = $this->_util->getTableName('zf_longidentifier');
- // insert into the table
- $this->_db->insert($tableName, array(
- 'id' => 1,
- 'veryveryveryverylongidentifier' => 2
- ));
- //try to update
- $this->_db->update($tableName,
- array('veryveryveryverylongidentifier' => 3),
- array($this->_db->quoteIdentifier('id') . ' = 1'));
- // check if the row was inserted as expected
- $select = $this->_db->select()->from($tableName, array('id', 'veryveryveryverylongidentifier'));
- $stmt = $this->_db->query($select);
- $fetched = $stmt->fetchAll(Zend_Db::FETCH_NUM);
- $a = array(
- 0 => array(0 => 1, 1 => 3)
- );
- $this->assertEquals($a, $fetched,
- 'result of query not as expected');
- // clean up
- unset($stmt);
- $this->_util->dropTable($tableName);
- }
- protected function _testAdapterAlternateStatement($stmtClass)
- {
- $ip = get_include_path();
- $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files';
- $newIp = $dir . PATH_SEPARATOR . $ip;
- set_include_path($newIp);
- $params = $this->_util->getParams();
- $params['options'] = array(
- Zend_Db::AUTO_QUOTE_IDENTIFIERS => false
- );
- $db = Zend_Db::factory($this->getDriver(), $params);
- $db->getConnection();
- $db->setStatementClass($stmtClass);
- $currentStmtClass = $db->getStatementClass();
- $this->assertEquals($stmtClass, $currentStmtClass);
- $bugs = $this->_db->quoteIdentifier('zfbugs');
- $stmt = $db->prepare("SELECT COUNT(*) FROM $bugs");
- $this->assertTrue($stmt instanceof $stmtClass,
- 'Expecting object of type ' . $stmtClass . ', got ' . get_class($stmt));
- }
- public function testAdapterSetCustomProfiler()
- {
- $profilerOptions = array(
- 'enabled' => true,
- 'class' => 'stdClass',
- 'instance' => new stdClass()
- );
- try {
- $this->_db->setProfiler($profilerOptions);
- $this->fail('Expected to catch Zend_Db_Profiler_Exception');
- } catch (Zend_Exception $e) {
- $this->assertTrue($e instanceof Zend_Db_Profiler_Exception);
- $this->assertEquals('Class stdClass does not extend Zend_Db_Profiler', $e->getMessage());
- }
- }
- /**
- * @group ZF-5099
- */
- public function testAdapterGetServerVersion()
- {
- $version = $this->_db->getServerVersion();
- $this->assertNotNull($version);
- $this->assertTrue(version_compare($version, '1.0.0', '>'));
- $this->assertTrue(version_compare($version, '99.0.0', '<'));
- }
- public function testAdapterGetConnection()
- {
- $this->_db->getConnection();
- $this->assertTrue($this->_db->isConnected());
- }
- /**
- * @group ZF-5050
- */
- public function testAdapterCloseConnection()
- {
- $this->_db->closeConnection();
- $this->assertFalse($this->_db->isConnected());
- // Double closing must be without any errors
- $this->_db->closeConnection();
- $this->assertFalse($this->_db->isConnected());
- }
- /**
- * @group ZF-5146
- */
- public function testAdapterReadClobFetchAll()
- {
- $documents = $this->_db->quoteIdentifier('zfdocuments');
- $document_id = $this->_db->quoteIdentifier('doc_id');
- $value = $this->_db->fetchAll("SELECT * FROM $documents WHERE $document_id = 1");
- $expected = 'this is the clob that never ends...'.
- 'this is the clob that never ends...'.
- 'this is the clob that never ends...';
- $this->assertEquals($expected, $value[0]['doc_clob']);
- }
- /**
- * @group ZF-5146
- */
- public function testAdapterReadClobFetchRow()
- {
- $documents = $this->_db->quoteIdentifier('zfdocuments');
- $document_id = $this->_db->quoteIdentifier('doc_id');
- $value = $this->_db->fetchRow("SELECT * FROM $documents WHERE $document_id = 1");
- $expected = 'this is the clob that never ends...'.
- 'this is the clob that never ends...'.
- 'this is the clob that never ends...';
- $this->assertEquals($expected, $value['doc_clob']);
- }
- /**
- * @group ZF-5146
- */
- public function testAdapterReadClobFetchAssoc()
- {
- $documents = $this->_db->quoteIdentifier('zfdocuments');
- $document_id = $this->_db->quoteIdentifier('doc_id');
- $value = $this->_db->fetchAssoc("SELECT * FROM $documents WHERE $document_id = 1");
- $expected = 'this is the clob that never ends...'.
- 'this is the clob that never ends...'.
- 'this is the clob that never ends...';
- $this->assertEquals($expected, $value[1]['doc_clob']);
- }
- /**
- * @group ZF-5146
- */
- public function testAdapterReadClobFetchCol()
- {
- $documents = $this->_db->quoteIdentifier('zfdocuments');
- $document_id = $this->_db->quoteIdentifier('doc_id');
- $document_clob = $this->_db->quoteIdentifier('doc_clob');
- $value = $this->_db->fetchCol("SELECT $document_clob FROM $documents WHERE $document_id = 1");
- $expected = 'this is the clob that never ends...'.
- 'this is the clob that never ends...'.
- 'this is the clob that never ends...';
- $this->assertEquals($expected, $value[0]);
- }
- /**
- * @group ZF-5146
- */
- public function testAdapterReadClobFetchOne()
- {
- $documents = $this->_db->quoteIdentifier('zfdocuments');
- $document_id = $this->_db->quoteIdentifier('doc_id');
- $document_clob = $this->_db->quoteIdentifier('doc_clob');
- $value = $this->_db->fetchOne("SELECT $document_clob FROM $documents WHERE $document_id = 1");
- $expected = 'this is the clob that never ends...'.
- 'this is the clob that never ends...'.
- 'this is the clob that never ends...';
- $this->assertEquals($expected, $value);
- }
- public function testAdapterSerializationIsOkByDefault()
- {
- $serialized = serialize($this->_db);
- $this->assertTrue(is_string($serialized));
- $this->assertThat(unserialize($serialized), new PHPUnit_Framework_Constraint_IsInstanceOf('Zend_Db_Adapter_Abstract'));
- }
- public function testAdapterSerializationFailsWhenNotAllowedToBeSerialized()
- {
- $params = $this->_util->getParams();
- $params['options'] = array(
- Zend_Db::ALLOW_SERIALIZATION => false
- );
- $db = Zend_Db::factory($this->getDriver(), $params);
- $this->setExpectedException('Zend_Db_Adapter_Exception');
- $serialized = serialize($db);
- }
- public function testAdapterUnSerializationAutoReconnection()
- {
- $serialized = serialize($this->_db);
- $db = unserialize($serialized);
- $this->assertFalse($db->isConnected());
- $params = $this->_util->getParams();
- $params['options'] = array(
- Zend_Db::AUTO_RECONNECT_ON_UNSERIALIZE => true
- );
- $db = Zend_Db::factory($this->getDriver(), $params);
- $db = unserialize(serialize($db));
- $this->assertTrue($db->isConnected());
- }
- /**
- * @group ZF-1541
- */
- public function testCharacterSetUtf8()
- {
- // Create a new adapter
- $params = $this->_util->getParams();
- $params['charset'] = 'utf8';
- $db = Zend_Db::factory($this->getDriver(), $params);
- // create a new util object, with the new db adapter
- $driver = $this->getDriver();
- $utilClass = "Zend_Db_TestUtil_{$driver}";
- $util = new $utilClass();
- $util->setAdapter($db);
- // create test table using no identifier quoting
- $util->createTable('charsetutf8', array(
- 'id' => 'IDENTITY',
- 'stuff' => 'VARCHAR(32)'
- ));
- $tableName = $this->_util->getTableName('charsetutf8');
- // insert into the table
- $numRows = $db->insert($tableName, array(
- 'id' => 1,
- 'stuff' => 'äöüß'
- ));
- // check if the row was inserted as expected
- $select = $db->select()->from($tableName, array('id', 'stuff'));
- $stmt = $db->query($select);
- $fetched = $stmt->fetchAll(Zend_Db::FETCH_NUM);
- $a = array(
- 0 => array(0 => 1, 1 => 'äöüß')
- );
- $this->assertEquals($a, $fetched,
- 'result of query not as expected');
- // clean up
- unset($stmt);
- $util->dropTable($tableName);
- }
- /**
- * @group ZF-7737
- */
- public function testQuoteIntoReplacesPlaceholderAtFirstCharacterWhenCountIsNotNull()
- {
- $quotedString = $this->_db->quoteInto('? = bar', 'foo', NULL, 1);
- $this->assertEquals("'foo' = bar", $quotedString);
- }
- /**
- * @group ZF-8399
- */
- public function testLongQueryWithTextField()
- {
- // create test table using no identifier quoting
- $this->_util->createTable('zf_longquery', array(
- 'id' => 'INTEGER NOT NULL',
- 'stuff' => 'TEXT NOT NULL'
- ));
- $tableName = $this->_util->getTableName('zf_longquery');
- // insert into the table
- $longValue = str_repeat('x', 4000);
- $numRows = $this->_db->insert($tableName, array(
- 'id' => 1,
- 'stuff' => $longValue
- ));
- $quotedTableName = $this->_db->quoteIdentifier('zf_longquery');
- $sql = "INSERT INTO $quotedTableName VALUES (2, '$longValue')";
- $this->_db->query($sql);
- // check if the row was inserted as expected
- $select = $this->_db->select()->from($tableName, array('id', 'stuff'));
- $stmt = $this->_db->query($select);
- $fetched = $stmt->fetchAll(Zend_Db::FETCH_NUM);
- $a = array(
- 0 => array(0 => 1, 1 => $longValue),
- 1 => array(0 => 2, 1 => $longValue)
- );
- $this->assertEquals($a, $fetched,
- 'result of query not as expected');
- // clean up
- unset($stmt);
- $this->_util->dropTable($tableName);
- }
- /**
- * @group ZF-6620
- */
- public function testAdapterOptionFetchMode()
- {
- $params = $this->_util->getParams();
- $params['options'] = array(
- Zend_Db::FETCH_MODE => 'obj'
- );
- $db = Zend_Db::factory($this->getDriver(), $params);
- $select = $db->select()->from('zfproducts');
- $row = $db->fetchRow($select);
- $this->assertTrue($row instanceof stdClass);
- }
- }
|