/** * @static * @param array $database_settings * @return AkDbAdapter */ function &getInstance($database_specifications = AK_DEFAULT_DATABASE_PROFILE, $auto_connect = true) { static $connections; $settings_hash = is_string($database_specifications) ? $database_specifications : AkDbAdapter::_hash($database_specifications); if (empty($connections[$settings_hash])) { global $database_settings; if (is_string($database_specifications)) { if (!empty($database_settings[$database_specifications])) { $database_specifications = $database_settings[$database_specifications]; } elseif (strstr($database_specifications, '://')) { $database_specifications = AkDbAdapter::_getDbSettingsFromDsn($database_specifications); $settings_hash = AK_ENVIRONMENT; } else { trigger_error(Ak::t("Could not find the database profile '%profile_name' in config/config.php.", array('%profile_name' => $database_specifications)), E_USER_ERROR); $return = false; return $return; } } elseif (!empty($database_settings[$settings_hash])) { $database_specifications = $database_settings[$settings_hash]; } $available_adapters = Ak::toArray(AK_AVAILABLE_DATABASES); $class_name = 'AkDbAdapter'; $designated_database = strtolower($database_specifications['type']); if (in_array($designated_database, $available_adapters)) { $class_name = 'Ak' . ucfirst($designated_database) . 'DbAdapter'; require_once AK_LIB_DIR . DS . 'AkActiveRecord' . DS . 'AkDbAdapters' . DS . $class_name . '.php'; } $connections[$settings_hash] =& new $class_name($database_specifications, $auto_connect); } return $connections[$settings_hash]; }
function test_should_establish_a_connection() { $this->installAndIncludeModels(array('DummyModel'=>'id')); $Model =& $this->DummyModel; $default_connection =& AkDbAdapter::getInstance(); $available_tables_on_default = $default_connection->availableTables(); unset ($Model->_db); $this->assertReference($Model->establishConnection(),$default_connection); $development_connection =& $Model->establishConnection('development'); $available_tables_on_development = $development_connection->availableTables(); $this->assertFalse($development_connection===$default_connection); $this->assertFalse($Model->establishConnection('not_specified_profile')); $this->assertError("The environment not_specified_profile is not allowed. Allowed environments: setup,testing,development,production"); $this->assertError("Could not find the database profile 'not_specified_profile' in config/database.yml."); $check_default_connection =& AkDbAdapter::getInstance(); $this->assertReference($default_connection,$check_default_connection); $this->assertReference($default_connection->connection,$check_default_connection->connection); //because we dont get two different connections at the same time on PHP if user and password is identical //thus: !$this->assertEqual($available_tables_on_default,$check_default_connection->availableTables()); //we have to: $check_default_connection->connect(); //now we get: $this->assertEqual($available_tables_on_default,$check_default_connection->availableTables()); //BUT again: !! //$this->assertNotEqual($available_tables_on_development,$development_connection->availableTables()); //$this->assertEqual($available_tables_on_default,$development_connection->availableTables()); }
/** * Gets an instance of AkDbAdapter * * Whenever a database connection is required you can get a * reference to the default database connection by doing: * * $db =& Ak::db(); // get an adodb instance * * AdoDB manual can be found at http://phplens.com/adodb/ * * @access public * @param string $dns A string containing Data Source Name (information * regarding database connection) * http://phplens.com/adodb/code.initialization.html#dsnsupport * @static * @return resource Php AdoDb instance. */ function &db($dsn = null) { require_once AK_LIB_DIR . DS . 'AkActiveRecord' . DS . 'AkDbAdapter.php'; if (empty($dsn) || !is_array($dsn)) { return AkDbAdapter::getInstance(); } return AkDbAdapter::getInstance($dsn); }
public function test_should_rename_columns_for_postgre() { $db =& AkDbAdapter::getInstance(); if ($db->type() !== 'postgre') { return; } $this->installAndIncludeModels(array('RenameColumn' => "id,namen string(55),postcunt int not null default 0,help string default 'none'")); $table_name = 'rename_columns'; $db->renameColumn($table_name, 'namen', 'name'); }
function testSetUp() { $this->assertThat($this->User->getTableName(), $this->equalTo('my_users')); $this->assertThat($this->Customer->getTableName(), $this->equalTo('my_customers')); $this->assertThat($this->Client->getTableName(), $this->equalTo('my_clients')); $this->assertArrayHasKey('clients', $this->User->getAssociated('hasAndBelongsToMany')); $this->assertContains('my_users', AkDbAdapter::getInstance()->availableTables()); $this->assertContains('my_customers', AkDbAdapter::getInstance()->availableTables()); $this->assertContains('my_clients', AkDbAdapter::getInstance()->availableTables()); }
function AkInstaller($db_connection = null) { if (empty($db_connection)) { $this->db =& AkDbAdapter::getInstance(); } else { $this->db =& $db_connection; } $this->data_dictionary =& $this->db->getDictionary(); $this->available_tables = $this->getAvailableTables(); }
public function connect($die_on_error = true) { parent::connect($die_on_error); if (defined('AK_SET_UTF8_ON_MYSQL_CONNECT') && AK_SET_UTF8_ON_MYSQL_CONNECT) { if (isset($this->connection->_connectionID)) { if (function_exists('mysql_set_charset')) { mysql_set_charset('utf8', $this->connection->_connectionID); } else { mysql_query('SET CHARACTER SET "utf8"', $this->connection->_connectionID); } } } }
/** * @static * @param array $database_settings * @return AkDbAdapter */ static function &getInstance($database_specifications = AK_DEFAULT_DATABASE_PROFILE, $auto_connect = true, $namespace = null) { $settings_hash = is_string($database_specifications) ? $database_specifications : AkDbAdapter::hash($database_specifications); $static_var_name = 'AkDbAdapter_getInstance_' . $settings_hash; if (!($Connection = Ak::getStaticVar($static_var_name))) { defined('AK_DATABASE_SETTINGS_NAMESPACE') || define('AK_DATABASE_SETTINGS_NAMESPACE', 'database'); $namespace = empty($namespace) ? AK_DATABASE_SETTINGS_NAMESPACE : $namespace; if (empty($database_specifications)) { $database_specifications = Ak::getSettings($namespace, false, $settings_hash); } elseif (is_string($database_specifications)) { $environment_settings = Ak::getSettings($namespace, false, $database_specifications); if (!empty($environment_settings)) { $database_specifications = $environment_settings; } elseif (strstr($database_specifications, '://')) { $database_specifications = AkDbAdapter::getDbSettingsFromDsn($database_specifications); $settings_hash = AK_ENVIRONMENT; } else { global $database_settings; if (isset($database_settings) && !file_exists(AkConfig::getDir('config') . DS . $namespace . '.yml')) { trigger_error(Ak::t("You are still using the old config/config.php database configuration. Please upgrade to use the config/database.yml configuration."), E_USER_NOTICE); } if (!file_exists(AkConfig::getDir('config') . DS . $namespace . '.yml')) { trigger_error(Ak::t("Could not find the database configuration file in %dbconfig.", array('%dbconfig' => AkConfig::getDir('config') . DS . $namespace . '.yml')), E_USER_ERROR); } else { trigger_error(Ak::t("Could not find the database profile '%profile_name' in config/%dbfile.yml.", array('%profile_name' => $database_specifications, '%dbfile' => $namespace)), E_USER_ERROR); } $return = false; return $return; } } elseif (!empty($database_settings[$settings_hash])) { $database_specifications = $database_settings[$settings_hash]; } if (empty($database_specifications)) { trigger_error(Ak::t('Could not find database settings for %namespace.yml/%settings_hash', array('%namespace' => $namespace, '%settings_hash' => $settings_hash)), E_USER_ERROR); } // Compatibility with RoR database.yml format $database_specifications['user'] = isset($database_specifications['user']) ? $database_specifications['user'] : (isset($database_specifications['username']) ? $database_specifications['username'] : ''); $database_specifications['type'] = isset($database_specifications['type']) ? $database_specifications['type'] : $database_specifications['adapter']; $database_specifications['database_name'] = isset($database_specifications['database_name']) ? $database_specifications['database_name'] : (isset($database_specifications['database']) ? $database_specifications['database'] : ''); $class_name = 'Ak' . AkInflector::camelize($database_specifications['type']) . 'DbAdapter'; $adapter_class_file = AK_ACTIVE_RECORD_DIR . DS . 'adapters' . DS . AkInflector::underscore($database_specifications['type']) . '.php'; if (!@(include_once $adapter_class_file)) { trigger_error(Ak::t('Could not find database adapter file on %path', array('%path' => $adapter_class_file)), E_USER_ERROR); } $Connection = new $class_name($database_specifications, $auto_connect); Ak::setStaticVar($static_var_name, $Connection); } return $Connection; }
public function init($db_connection = null, $reinit = false) { if (!$this->_inited || $reinit) { // Install scripts might use more RAM than normal requests. @ini_set('memory_limit', -1); if (empty($db_connection)) { $this->db = AkDbAdapter::getInstance(); } else { $this->db = $db_connection; } $this->ensureAkelosMigrationsModelIsAvailable(); AkDbSchemaCache::clearAll(); $this->data_dictionary = $this->db->getDictionary(); $this->available_tables = $this->getAvailableTables(); $this->_inited = true; } }
public function test_should_establish_a_connection() { $this->installAndIncludeModels(array('DummyModel' => 'id')); $Model = $this->DummyModel; $default_connection = AkDbAdapter::getInstance(); $available_tables_on_default = $default_connection->getAvailableTables(); unset($Model->_db); $this->assertReference($Model->establishConnection(), $default_connection); $development_connection = $Model->establishConnection('development', true); $available_tables_on_development = $development_connection->getAvailableTables(); $this->assertFalse($development_connection === $default_connection); $this->assertUpcomingError("Could not find the"); $this->assertFalse($Model->establishConnection('not_specified_profile', true)); $check_default_connection = AkDbAdapter::getInstance(); $this->assertReference($default_connection, $check_default_connection); $this->assertReference($default_connection->connection, $check_default_connection->connection); }
/** * @static * @param array $database_settings * @return AkDbAdapter */ public static function &getInstance($database_specifications = AK_DEFAULT_DATABASE_PROFILE, $auto_connect = true) { static $connections; $settings_hash = is_string($database_specifications) ? $database_specifications : AkDbAdapter::_hash($database_specifications); if (empty($connections[$settings_hash])) { if (empty($database_specifications)) { $settings_hash = AK_ENVIRONMENT; $database_specifications = Ak::getSettings('database', false, $settings_hash); } else { if (is_string($database_specifications)) { $environment_settings = Ak::getSettings('database', false, $database_specifications); if (!empty($environment_settings)) { $database_specifications = $environment_settings; } elseif (strstr($database_specifications, '://')) { $database_specifications = AkDbAdapter::_getDbSettingsFromDsn($database_specifications); $settings_hash = AK_ENVIRONMENT; } else { global $database_settings; if (isset($database_settings) && !file_exists(AK_CONFIG_DIR . DS . 'database.yml')) { trigger_error(Ak::t("You are still using the old config/config.php database configuration. Please upgrade to use the config/database.yml configuration."), E_USER_NOTICE); } if (!file_exists(AK_CONFIG_DIR . DS . 'database.yml')) { trigger_error(Ak::t("Could not find the database configuration file in %dbconfig.", array('%dbconfig' => AK_CONFIG_DIR . DS . 'database.yml')), E_USER_ERROR); } else { trigger_error(Ak::t("Could not find the database profile '%profile_name' in config/database.yml.", array('%profile_name' => $database_specifications)), E_USER_ERROR); } $return = false; return $return; } } elseif (!empty($database_settings[$settings_hash])) { $database_specifications = $database_settings[$settings_hash]; } } $available_adapters = Ak::toArray(AK_AVAILABLE_DATABASES); $class_name = 'AkDbAdapter'; $designated_database = strtolower($database_specifications['type']); if (in_array($designated_database, $available_adapters)) { $class_name = 'Ak' . ucfirst($designated_database) . 'DbAdapter'; require_once AK_LIB_DIR . DS . 'AkActiveRecord' . DS . 'AkDbAdapters' . DS . $class_name . '.php'; } $connections[$settings_hash] = new $class_name($database_specifications, $auto_connect); } return $connections[$settings_hash]; }
public function __construct() { $adapter = AkDbAdapter::getInstance('sqlite_databases', true, 'database'); parent::__construct($adapter); }
function assertTableHasColumns($table_name, $columns) { $column_details = AkDbAdapter::getInstance()->getColumnDetails($table_name); self::assertEquals($columns, array_map('strtolower', array_keys($column_details))); }
function __construct() { $adapter =& AkDbAdapter::getInstance('sqlite_databases'); parent::AkInstaller($adapter); }
function _checkDbConfig($type, $settings = array()) { $this->debug('Checking if database config for "' . $type . '" is available'); require_once AK_BASE_DIR . DS . 'lib' . DS . 'AkActiveRecord' . DS . 'AkDbAdapter.php'; require_once AK_LIB_DIR . DS . 'Ak.php'; if (empty($settings)) { require_once AK_BASE_DIR . DS . 'lib' . DS . 'AkConfig.php'; $config = new AkConfig(); $settings = $config->get($type, 'testing'); } $db = new AkDbAdapter($settings); $db->connect(false); if (!($res = $db->connected())) { $this->error("[{$type}] " . 'Cannot connect to DB'); } else { $this->info("[{$type}] " . 'Successfully connected to database ' . ($settings['type'] == 'sqlite' ? $settings['database_file'] : $settings['database_name'])); $createRes = $db->execute('CREATE table ci_test_dummy(id integer)'); if (!$createRes) { $res = false; $this->error("[{$type}] " . 'Could not create test table: ci_test_dummy'); } else { $res = true; $this->info("[{$type}] " . 'Successfully created test table: ci_test_dummy'); $db->execute('DROP table ci_test_dummy'); } } return $res; }
/** * Gets an instance of AkDbAdapter * * Whenever a database connection is required you can get a * reference to the default database connection by doing: * * $db =& Ak::db(); // get an adodb instance * * AdoDB manual can be found at http://phplens.com/adodb/ * * @access public * @param string $dns A string containing Data Source Name (information * regarding database connection) * http://phplens.com/adodb/code.initialization.html#dsnsupport * @static * @return resource Php AdoDb instance. */ function &db($dsn = null) { require_once AK_LIB_DIR . DS . 'AkActiveRecord' . DS . 'AkDbAdapter.php'; return AkDbAdapter::getInstance($dsn); }
/** * Gets an instance of AkDbAdapter * * Whenever a database connection is required you can get a * reference to the default database connection by doing: * * $db = Ak::db(); // get an adodb instance * * AdoDB manual can be found at http://phplens.com/adodb/ * * @access public * @param string $dns A string containing Data Source Name (information * regarding database connection) * http://phplens.com/adodb/code.initialization.html#dsnsupport * @static * @return resource Php AdoDb instance. */ static function &db($dsn = null) { return AkDbAdapter::getInstance($dsn); }
function _test_investigate_DBTimeStamp() { $db =& AkDbAdapter::getInstance(); var_dump($db->DBTimeStamp('2007.11.17')); var_dump($db->DBTimeStamp('2007-11-17')); var_dump($db->DBTimeStamp('2007-11-17 17:40:23')); var_dump($db->DBTimeStamp('2007-11-17 8:40:23')); var_dump($db->DBTimeStamp('17-11-2007')); var_dump($db->DBTimeStamp(time())); }
/** * Sets the connection for the class. */ function &setConnection($db_adapter = null) { if (is_null($db_adapter)){ $db_adapter =& AkDbAdapter::getInstance(); } return $this->_db =& $db_adapter; }
/** * Sets the connection for the class. */ public function &setConnection($db_adapter = null) { if (is_null($db_adapter)) { $db_adapter = AkDbAdapter::getInstance(); } $this->_db = $db_adapter; return $db_adapter; }
function runFrameworkInstaller() { static $unique_dsn = array(); require_once AK_LIB_DIR . DS . 'AkInstaller.php'; require_once AK_APP_DIR . DS . 'installers' . DS . 'framework_installer.php'; foreach (array('production', 'development') as $mode) { $dsn = $this->_getDsn($mode); if (!isset($unique_dsn[$dsn])) { $DbInstance =& AkDbAdapter::getInstance(array('type' => $this->getDatabaseType($mode), 'file' => AK_CONFIG_DIR . DS . $this->getDatabaseName($mode) . '-' . $this->random . '.sqlite', 'user' => $this->getDatabaseUser($mode), 'password' => $this->getDatabasePassword($mode), 'host' => $this->getDatabaseHost($mode), 'database_name' => $this->getDatabaseName($mode))); $installer =& new FrameworkInstaller($DbInstance); $installer->install(null, array('mode' => $mode)); $unique_dsn[$dsn] = true; } } return true; }