public function setUp() { if (!TESTS_ZEND_DB_ADAPTER_PDO_MYSQL_ENABLED) { $this->markTestSkipped('Database tests are not enabled.'); return; } if (!extension_loaded('pdo')) { $this->markTestSkipped('PDO is required for this test.'); return; } if (!in_array('mysql', \PDO::getAvailableDrivers())) { $this->markTestSkipped('Mysql is not included in PDO in this PHP installation.'); return; } $params = array( 'host' => TESTS_ZEND_DB_ADAPTER_MYSQL_HOSTNAME, 'username' => TESTS_ZEND_DB_ADAPTER_MYSQL_USERNAME, 'password' => TESTS_ZEND_DB_ADAPTER_MYSQL_PASSWORD, 'dbname' => TESTS_ZEND_DB_ADAPTER_MYSQL_DATABASE, ); $this->dbAdapter = \Zend\Db\Db::factory('PdoMysql', $params); $this->dbAdapter->query("DROP TABLE IF EXISTS foo"); $this->dbAdapter->query("DROP TABLE IF EXISTS bar"); $this->dbAdapter->query( 'CREATE TABLE foo (id INT(10) AUTO_INCREMENT PRIMARY KEY, foo VARCHAR(255), bar VARCHAR(255), baz VARCHAR(255)) AUTO_INCREMENT=1' ); $this->dbAdapter->query( 'CREATE TABLE bar (id INT(10) AUTO_INCREMENT PRIMARY KEY, foo VARCHAR(255), bar VARCHAR(255), baz VARCHAR(255)) AUTO_INCREMENT=1' ); }
/** * Test AUTO_QUOTE_IDENTIFIERS option * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true * * SQLite actually allows delimited identifiers to remain * case-insensitive, so this test overrides its parent. */ public function testAdapterAutoQuoteIdentifiersTrue() { $params = $this->_util->getParams(); $params['options'] = array( Db\Db::AUTO_QUOTE_IDENTIFIERS => true ); $db = Db\Db::factory($this->getDriver(), $params); $db->getConnection(); $select = $this->_db->select(); $select->from('zfproducts'); $stmt = $this->_db->query($select); $result1 = $stmt->fetchAll(); $this->assertEquals(1, $result1[0]['product_id']); $select = $this->_db->select(); $select->from('ZFPRODUCTS'); try { $stmt = $this->_db->query($select); $result2 = $stmt->fetchAll(); } catch (\Exception $e) { $this->assertType('Zend_Db_Statement_Exception', $e, 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e)); $this->fail('Unexpected exception '.get_class($e).' received: '.$e->getMessage()); } $this->assertEquals($result1, $result2); }
protected function _initDb() { if (!extension_loaded('pdo') || !in_array('sqlite', \PDO::getAvailableDrivers())) { $this->markTestSkipped('Test only with pdo_sqlite'); } $db = \Zend\Db\Db::factory('Pdo\\Sqlite', array('dbname' => ':memory:')); \Zend\Db\Table\AbstractTable::setDefaultAdapter($db); $this->_createTable(); }
public function setUp() { if (!extension_loaded('pdo')) { $this->markTestSkipped('PDO is required for this test.'); } if (!in_array('sqlite', \PDO::getAvailableDrivers())) { $this->markTestSkipped('SqLite is not included in PDO in this PHP installation.'); } $this->dbAdapter = \Zend\Db\Db::factory('Pdo\\Sqlite', array('dbname' => ':memory:')); $this->dbAdapter->query('CREATE TABLE "foo" (id INTEGER PRIMARY KEY AUTOINCREMENT, foo VARCHAR, bar VARCHAR, baz VARCHAR)'); $this->dbAdapter->query('CREATE TABLE "bar" (id INTEGER PRIMARY KEY AUTOINCREMENT, foo VARCHAR, bar VARCHAR, baz VARCHAR)'); }
/** * Test AUTO_QUOTE_IDENTIFIERS option * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true */ public function testAdapterAutoQuoteIdentifiersTrue() { $params = $this->_util->getParams(); $params['options'] = array(Db\Db::AUTO_QUOTE_IDENTIFIERS => true); $db = Db\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']); }
/** * Sets up the database connection and creates the table for session data * * @param array $primary * @return void */ protected function _setupDb(array $primary) { if (!extension_loaded('pdo_sqlite')) { $this->markTestSkipped('The pdo_sqlite extension must be available and enabled for this test'); } $this->_db = Db::factory('Pdo\\Sqlite', array('dbname' => ':memory:')); AbstractTable::setDefaultAdapter($this->_db); $query = array(); $query[] = 'CREATE TABLE `Sessions` ( '; $query[] = '`id` varchar(32) NOT NULL, '; if (in_array('save_path', $primary)) { $query[] = '`save_path` varchar(32) NOT NULL, '; } if (in_array('name', $primary)) { $query[] = '`name` varchar(32) NOT NULL, '; } $query[] = '`modified` int(11) default NULL, '; $query[] = '`lifetime` int(11) default NULL, '; $query[] = '`data` text, '; $query[] = 'PRIMARY KEY (' . implode(', ', $primary) . ') '; $query[] = ');'; $this->_db->query(implode("\n", $query)); }
/** * @group ZF-1541 */ public function testCharacterSetUtf8() { // Create a new adapter $params = $this->_util->getParams(); $params['charset'] = 'utf8'; $db = Db\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'); $table = $db->quoteIdentifier('charsetutf8'); $db->query("SET IDENTITY_INSERT $table ON"); // 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(Db\Db::FETCH_NUM); $a = array( 0 => array(0 => 1, 1 => 'äöüß') ); $this->assertEquals($a, $fetched, 'result of query not as expected'); $db->query("SET IDENTITY_INSERT $table OFF"); // clean up unset($stmt); $util->dropTable($tableName); }
/** * @group ZF-7924 */ public function testDbFactoryWillLoadCaseInsensitiveAdapterName() { $this->markTestSkipped('Invalid due to autoload requirement.'); try { $adapter = Db\Db::factory( 'DB_ADAPTER', array('dbname' => 'dummy', 'adapterNamespace' => '\ZendTest\Db\Adapter\TestAsset\Test\MyCompany1') ); } catch (\Exception $e) { $this->fail('Could not load file for reason: ' . $e->getMessage()); } $this->assertEquals('\ZendTest\Db\Adapter\TestAsset\Test\MyCompany1\Db\Adapter', get_class($adapter)); }
/** * Retrieve initialized DB connection * * @return null|Zend_Db_Adapter_Interface */ public function getDbAdapter() { if (null === $this->_db && null !== ($adapter = $this->getAdapter())) { $this->_db = \Zend\Db\Db::factory($adapter, $this->getParams()); } return $this->_db; }
/** * @group ZF-5146 */ public function testAdapterLobAsStringFromDriverOptions() { $params = $this->_util->getParams(); $params['driver_options'] = array( 'lob_as_string' => true ); $db = Db\Db::factory($this->getDriver(), $params); $this->assertTrue($db->getLobAsString()); }
/** * Ensures that the PDO Buffered Query does not throw the error * 2014 General error * * @link http://framework.zend.com/issues/browse/ZF-2101 * @return void */ public function testZF2101() { $params = $this->_util->getParams(); $params['driver_options'] = array(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true); $db = Db\Db::factory($this->getDriver(), $params); // Set default bound value $customerId = 1; // Stored procedure returns a single row $stmt = $db->prepare('CALL zf_test_procedure(:customerId)'); $stmt->bindParam('customerId', $customerId, \PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(); $this->assertEquals(1, $result[0]['product_id']); // Reset statement $stmt->closeCursor(); // Stored procedure returns a single row $stmt = $db->prepare('CALL zf_test_procedure(:customerId)'); $stmt->bindParam('customerId', $customerId, \PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(); $this->assertEquals(1, $result[0]['product_id']); }
public function testMySqliInitCommand() { $params = $this->_util->getParams(); $params['driver_options'] = array('mysqli_init_command' => 'SET AUTOCOMMIT=0;'); $db = Db\Db::factory($this->getDriver(), $params); $sql = 'SELECT @@AUTOCOMMIT as autocommit'; $row = $db->fetchRow($sql); $this->assertEquals(0, $row['autocommit']); }
/** * Retrieve initialized DB connection * * @return \Zend\Db\Adapter\AbstractAdapter */ protected function _getDbAdapter() { if (null === $this->_db) { if ($this->_config->resources->db) { $dbConfig = $this->_config->resources->db; $this->_db = \Zend\Db\Db::factory($dbConfig->adapter, $dbConfig->params); } elseif ($this->_config->resources->multidb) { foreach ($this->_config->resources->multidb as $db) { if ($db->default) { $this->_db = \Zend\Db\Db::factory($db->adapter, $db); } } } if ($this->_db instanceof \Zend\Db\Adapter\AbstractAdapter) { throw new Akrabat\Db\Schema\Exception('Database was not initialized'); } } return $this->_db; }
/** * Initialize DB adapter using 'driverOptions' section of the _options array * * Throws an exception if the adapter cannot connect to DB. * * @return \Zend\Db\Adapter\AbstractAdapter * @throws \Zend\Queue\Exception */ protected function _initDBAdapter() { $options = &$this->_options['driverOptions']; if (!array_key_exists('type', $options)) { throw new QueueException("Configuration array must have a key for 'type' for the database type to use"); } if (!array_key_exists('host', $options)) { throw new QueueException("Configuration array must have a key for 'host' for the host to use"); } if (!array_key_exists('username', $options)) { throw new QueueException("Configuration array must have a key for 'username' for the username to use"); } if (!array_key_exists('password', $options)) { throw new QueueException("Configuration array must have a key for 'password' for the password to use"); } if (!array_key_exists('dbname', $options)) { throw new QueueException("Configuration array must have a key for 'dbname' for the database to use"); } $type = $options['type']; unset($options['type']); try { $db = DB_ns\Db::factory($type, $options); } catch (DB_ns\Exception $e) { throw new QueueException('Error connecting to database: ' . $e->getMessage(), $e->getCode(), $e); } return $db; }
/** * Initialize the Database Connections (instances of Zend_Db_Table_Abstract) * * @return \Zend\Application\Resource\Multidb */ public function init() { $options = $this->getOptions(); if (isset($options['defaultMetadataCache'])) { $this->_setDefaultMetadataCache($options['defaultMetadataCache']); unset($options['defaultMetadataCache']); } foreach ($options as $id => $params) { $adapter = $params['adapter']; $default = (int) ( isset($params['isDefaultTableAdapter']) && $params['isDefaultTableAdapter'] || isset($params['default']) && $params['default'] ); unset( $params['adapter'], $params['default'], $params['isDefaultTableAdapter'] ); $this->_dbs[$id] = \Zend\Db\Db::factory($adapter, $params); if ($default) { $this->_setDefault($this->_dbs[$id]); } } return $this; }
/** * This is "related" to the issue. It appears the fix for * describeTable is relatively untestable due to the fact that * its primary focus is to reduce the query time, not the result * set. * * @group ZF-5169 */ public function testAdapterSchemaOptionInListTables() { $params = $this->_util->getParams(); unset($params['schema']); $connection = Db\Db::factory($this->getDriver(), $params); $tableCountNoSchema = count($connection->listTables()); $dbConfig = $this->_db->getConfig(); if ($this->_db->isI5()) { if (isset($dbConfig['driver_options']['i5_lib'])) { $schema = $dbConfig['driver_options']['i5_lib']; } } elseif (!$this->_db->isI5()) { $schema = $this->_util->getSchema(); } else { $this->markTestSkipped('No valid schema to test against.'); return; } $params = $this->_util->getParams(); $params['schema'] = $schema; $connection = Db\Db::factory($this->getDriver(), $params); $tableCountSchema = count($connection->listTables()); $this->assertGreaterThan(0, $tableCountNoSchema, 'Adapter without schema should produce large result'); $this->assertGreaterThan(0, $tableCountSchema, 'Adapter with schema should produce large result'); $this->assertTrue($tableCountNoSchema > $tableCountSchema, 'Table count with schema provided should be less than without.'); }