setActive(true); $tables_format = $this->getTablesFormat($connection); $is_connection = (is_numeric($tables_format) === true && $tables_format > 0); } catch (TDbException $e) { throw new BCatalogException( DatabaseError::MSG_ERROR_DB_CONNECTION_PROBLEM . ' ' . $e->getErrorMessage(), DatabaseError::ERROR_DB_CONNECTION_PROBLEM ); } if(array_key_exists('password', $db_params)) { // mask database password. $db_params['password'] = preg_replace('/.{1}/', '*', $db_params['password']); } $logmsg = 'DBParams=%s, Connection=%s, TablesFormat=%s'; $msg = sprintf($logmsg, print_r($db_params, true), var_export($is_connection, true), var_export($tables_format, true)); $this->getModule('logging')->log( __FUNCTION__, $msg, Logging::CATEGORY_APPLICATION, __FILE__, __LINE__ ); return $is_connection; } /** * Test connection to the Catalog using params from config. * * @access public * @return bool true if connection established, otherwise false */ public function testCatalog() { $result = false; $api_config = $this->getModule('api_config')->getConfig(); if (array_key_exists('db', $api_config)) { $result = $this->testDbConnection($api_config['db']); } else { throw new BCatalogException( DatabaseError::MSG_ERROR_DATABASE_ACCESS_NOT_SUPPORTED, DatabaseError::ERROR_DATABASE_ACCESS_NOT_SUPPORTED ); } return $result; } /** * Get Catalog database tables format * * @access private * @param TDBConnection $connection handler to database connection * @return mixed Catalog database tables format or null */ private function getTablesFormat(TDBConnection $connection) { $query = 'SELECT versionid FROM Version'; $command = $connection->createCommand($query); $row = $command->queryRow(); $tables_format = array_key_exists('versionid', $row) ? $row['versionid'] : null; return $tables_format; } public function getDatabaseSize() { $db_params = $this->getModule('api_config')->getConfig('db'); $connection = APIDbModule::getAPIDbConnection($db_params); $connection->setActive(true); $pdo = $connection->getPdoInstance(); $size = 0; switch ($db_params['type']) { case self::PGSQL_TYPE: { $sql = "SELECT pg_database_size('{$db_params['name']}') AS dbsize"; $result = $pdo->query($sql); $row = $result->fetch(); $size = $row['dbsize']; break; } case self::MYSQL_TYPE: { $sql = "SELECT Sum(data_length + index_length) AS dbsize FROM information_schema.tables"; $result = $pdo->query($sql); $row = $result->fetch(); $size = $row['dbsize']; break; } case self::SQLITE_TYPE: { $sql = "PRAGMA page_count"; $result = $pdo->query($sql); $page_count = $result->fetch(); $sql = "PRAGMA page_size"; $result = $pdo->query($sql); $page_size = $result->fetch(); $size = ($page_count['page_count'] * $page_size['page_size']); break; } } $dbsize = array('dbsize' => $size, 'dbtype' => $db_params['type']); $pdo = null; return $dbsize; } } ?>