示例#1
1
 /**
  *
  * @param PWECore $PWE
  * @param bool $forceNewConnection
  * @param string $alias
  * @return Connection
  * @throws DBALException
  */
 public static function getConnection(PWECore $PWE, $forceNewConnection = false, $alias = null)
 {
     if (!$forceNewConnection && self::$connection[$alias]) {
         PWELogger::debug('Used cached connection');
         return self::$connection[$alias];
     }
     $settings = $PWE->getModulesManager()->getModuleSettings(self::getClass());
     $connections = $settings['!c']['connection'];
     $ix = PWEXMLFunctions::findNodeWithAttributeValue($connections, 'alias', $alias);
     if ($ix < 0) {
         throw new \InvalidArgumentException("Alias {$alias} not found in database configs");
     }
     $params = $connections[$ix]['!a'];
     $config = new Configuration();
     $config->setSQLLogger(new PWEDoctrineLogger($alias ? $alias : ''));
     PWELogger::debug("Getting connection: %s", $params);
     self::$connection[$alias] = DriverManager::getConnection($params, $config);
     return self::$connection[$alias];
 }
 public function register(Container $app)
 {
     $app['dbal.parameters'] = function ($app) {
         $parameters = array('host' => 'localhost', 'port' => null, 'dbname' => null, 'user' => null, 'password' => null, 'driver' => 'pdo_mysql', 'unix_socket' => null, 'charset' => 'utf8');
         foreach ($parameters as $key => $value) {
             if (isset($app['dbal.' . $key])) {
                 $parameters[$key] = $app['dbal.' . $key];
             }
         }
         return $parameters;
     };
     $app['dbal.event_manager'] = function ($app) {
         return new EventManager();
     };
     $app['dbal.logger'] = function ($app) {
         return new DBALLogger($app['logger']);
     };
     $app['dbal.configuration'] = function ($app) {
         $cfg = new Configuration();
         $cfg->setSqlLogger($app['dbal.logger']);
         return $cfg;
     };
     $app['dbal'] = function ($app) {
         return DriverManager::getConnection($app['dbal.parameters'], $app['dbal.configuration'], $app['dbal.event_manager']);
     };
 }
示例#3
0
 /**
  * Get an instance of a DBAL Connection
  *
  * @param sting $name the connection name
  * @return Doctrine\DBAL\Connection
  */
 public function newConnection()
 {
     $configuration = new DBAL\Configuration();
     $logger = new MSQLLogger($this);
     $configuration->setSQLLogger($logger);
     return DBAL\DriverManager::getConnection($this->config, $configuration);
 }
示例#4
0
 /**
  * Tests that the default auto-commit mode for connections can be set in the configuration container.
  *
  * @group DBAL-81
  */
 public function testSetsDefaultConnectionAutoCommitMode()
 {
     $this->config->setAutoCommit(false);
     $this->assertFalse($this->config->getAutoCommit());
     $this->config->setAutoCommit(0);
     $this->assertFalse($this->config->getAutoCommit());
 }
示例#5
0
 /**
  * setup configuration for Doctrine Dbal.
  * 
  * @param array $db_config array config for override the default configuration.
  */
 public function setupConfigurationDbal(array $db_config = [])
 {
     $dbal_config = new Configuration();
     if (empty($db_config)) {
         //setup connection configuration.
         $config = new SystemConfig();
         $config->load('db');
         $db_params = $config->get('ALL', 'db');
         unset($config, $db_params['table_prefix']);
     } else {
         $db_params = $db_config;
         unset($db_params['table_prefix']);
     }
     $dbal_config->setSQLLogger(new \System\Libraries\Db\Logger());
     try {
         $this->Conn = DriverManager::getConnection($db_params, $dbal_config);
         $this->Conn->connect();
     } catch (\Doctrine\DBAL\DBALException $e) {
         http_response_code(500);
         echo $e->getMessage();
         exit;
     }
     $this->Conn->setFetchMode(\PDO::FETCH_OBJ);
     unset($dbal_config, $db_params);
 }
 /**
  * @return \Doctrine\DBAL\Connection
  * @throws \Doctrine\DBAL\DBALException
  */
 private static function create()
 {
     $configuration = new Configuration();
     $configuration->getSQLLogger(new EchoSQLLogger());
     $parameters = ['dbname' => $_ENV['testsuite_db_name'], 'user' => $_ENV['testsuite_db_user'], 'password' => $_ENV['testsuite_db_password'], 'host' => $_ENV['testsuite_db_host'], 'driver' => 'pdo_mysql'];
     $connection = DriverManager::getConnection($parameters, $configuration);
     return $connection;
 }
示例#7
0
 public function register()
 {
     $this->container->add('service.dbal.connection', function () {
         $connectionParams = $this->container->get('config')['dbal']['connection']['params'];
         $config = new Configuration();
         $config->setSQLLogger(new DebugStack());
         return DriverManager::getConnection($connectionParams, $config);
     });
 }
示例#8
0
文件: db.php 项目: 453111208/bbc
 protected static function getFacadeAccessor()
 {
     if (!static::$__db) {
         $configuration = new Configuration();
         $logger = new base_database_logger();
         $configuration->setSQLLogger($logger);
         static::$__db = new base_database_manager($configuration);
     }
     return static::$__db;
 }
 public function getConn()
 {
     if (isset(static::$connection)) {
         return static::$connection;
     }
     //set up a special logger that counts queries here
     $configuration = new Configuration();
     $configuration->setSQLLogger($this->getSQLLogger());
     static::$connection = DriverManager::getConnection($this->getConnectionParams(), $configuration);
     return static::$connection;
 }
 /**
  * Get DBAL configuration object
  * @param boolean $devMode Development mode enables a query logger
  * @return Configuration
  */
 protected function getConfig($devMode)
 {
     $config = new Configuration();
     if ($devMode) {
         //Create logger
         $debugStack = new DebugStack();
         $config->setSQLLogger($debugStack);
         $this->app->instance('dbal.debug', $debugStack);
     }
     return $config;
 }
 /**
  * @group DBAL-474
  */
 public function testFiltersSequences()
 {
     $configuration = new Configuration();
     $configuration->setFilterSchemaAssetsExpression('/^schema/');
     $sequences = array(array('relname' => 'foo', 'schemaname' => 'schema'), array('relname' => 'bar', 'schemaname' => 'schema'), array('relname' => 'baz', 'schemaname' => ''), array('relname' => 'bloo', 'schemaname' => 'bloo_schema'));
     $this->connection->expects($this->any())->method('getConfiguration')->will($this->returnValue($configuration));
     $this->connection->expects($this->at(0))->method('fetchAll')->will($this->returnValue($sequences));
     $this->connection->expects($this->at(1))->method('fetchAll')->will($this->returnValue(array(array('min_value' => 1, 'increment_by' => 1))));
     $this->connection->expects($this->at(2))->method('fetchAll')->will($this->returnValue(array(array('min_value' => 2, 'increment_by' => 2))));
     $this->connection->expects($this->exactly(3))->method('fetchAll');
     $this->assertEquals(array(new Sequence('schema.foo', 2, 2), new Sequence('schema.bar', 1, 1)), $this->schemaManager->listSequences('database'));
 }
 /**
  * @expectedException \Doctrine\DBAL\DBALException
  */
 public function testExecuteCallsLoggerStopQueryOnException()
 {
     $logger = $this->getMock('\\Doctrine\\DBAL\\Logging\\SQLLogger');
     $this->configuration->expects($this->once())->method('getSQLLogger')->will($this->returnValue($logger));
     // Needed to satisfy construction of DBALException
     $this->conn->expects($this->any())->method('resolveParams')->will($this->returnValue(array()));
     $logger->expects($this->once())->method('startQuery');
     $logger->expects($this->once())->method('stopQuery');
     $this->pdoStatement->expects($this->once())->method('execute')->will($this->throwException(new \Exception("Mock test exception")));
     $statement = new Statement("", $this->conn);
     $statement->execute();
 }
示例#13
0
 public function register()
 {
     $this->container['dbal_connection_params'] = function () {
         return $this->container->get('config')['dbal']['connection']['params'];
     };
     $this->container['db_connection'] = function () {
         $connectionParams = $this->container->get('dbal_connection_params');
         $debugStack = new DebugStack();
         $config = new Configuration();
         $config->setSQLLogger($debugStack);
         return DriverManager::getConnection($connectionParams, $config);
     };
 }
 /**
  * @param ServiceLocatorInterface $serviceLocator
  * @param Configuration           $config
  */
 public function setupDBALConfiguration(ServiceLocatorInterface $serviceLocator, Configuration $config)
 {
     $options = $this->getOptions($serviceLocator);
     $config->setResultCacheImpl($serviceLocator->get($options->resultCache));
     $config->setSQLLogger($options->sqlLogger);
     foreach ($options->types as $name => $class) {
         if (Type::hasType($name)) {
             Type::overrideType($name, $class);
         } else {
             Type::addType($name, $class);
         }
     }
 }
示例#15
0
 public function testExecuteCallsLoggerStartQueryWithParametersWhenParamsPassedToExecute()
 {
     $name = 'foo';
     $var = 'bar';
     $values = array($name => $var);
     $types = array();
     $sql = '';
     $logger = $this->getMock('\\Doctrine\\DBAL\\Logging\\SQLLogger');
     $logger->expects($this->once())->method('startQuery')->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types));
     $this->configuration->expects($this->once())->method('getSQLLogger')->will($this->returnValue($logger));
     $statement = new Statement($sql, $this->conn);
     $statement->execute($values);
 }
 /**
  * Creates a RepositoryFactory
  *
  * @param   Container $container The container
  *
  * @return  RepositoryFactory
  */
 public function createRepositoryFactory(Container $container)
 {
     $config = parse_ini_file(JPATH_ROOT . '/config/database.ini', true);
     $configuration = new Configuration();
     // Add logger
     $logger = new DebugStack();
     $configuration->setSQLLogger($logger);
     $connection = DriverManager::getConnection(['url' => $config['databaseUrl']], $configuration);
     $transactor = new DoctrineTransactor($connection);
     $repositoryFactory = new RepositoryFactory($config, $connection, $transactor);
     if ($container->has('dispatcher')) {
         $repositoryFactory->setDispatcher($container->get('dispatcher'));
     }
     return $repositoryFactory;
 }
示例#17
0
 /**
  * Cancel any database changes done during the current transaction.
  *
  * this method can be listened with onPreTransactionRollback and onTransactionRollback
  * eventlistener methods
  *
  * @throws ConnectionException If the rollback operation failed.
  */
 public function rollBack()
 {
     if ($this->_transactionNestingLevel == 0) {
         throw ConnectionException::noActiveTransaction();
     }
     $this->connect();
     $logger = $this->_config->getSQLLogger();
     if ($this->_transactionNestingLevel == 1) {
         if ($logger) {
             $logger->startQuery('"ROLLBACK"');
         }
         $this->_transactionNestingLevel = 0;
         $this->_conn->rollback();
         $this->_isRollbackOnly = false;
         if ($logger) {
             $logger->stopQuery();
         }
     } else {
         if ($this->_nestTransactionsWithSavepoints) {
             if ($logger) {
                 $logger->startQuery('"ROLLBACK TO SAVEPOINT"');
             }
             $this->rollbackSavepoint($this->_getNestedTransactionSavePointName());
             --$this->_transactionNestingLevel;
             if ($logger) {
                 $logger->stopQuery();
             }
         } else {
             $this->_isRollbackOnly = true;
             --$this->_transactionNestingLevel;
         }
     }
 }
示例#18
0
 /**
  * Creates a new connection to the database
  *
  * @param Context $context
  * @param array|Config $db_data
  */
 public function __construct(Context $context, $db_data = [])
 {
     parent::__construct($context);
     // load the defaults if the config object has been passed
     if ($db_data instanceof Config) {
         $db_data = $db_data->get('foolz/foolframe', 'db', 'default');
     }
     $config = new Configuration();
     $config->setSQLLogger(new DoctrineLogger($context));
     $data = ['dbname' => $db_data['dbname'], 'user' => $db_data['user'], 'password' => $db_data['password'], 'host' => $db_data['host'], 'driver' => $db_data['driver']];
     if ($db_data['driver'] == 'pdo_mysql') {
         $data['charset'] = $db_data['charset'];
     }
     $this->prefix = $db_data['prefix'];
     $this->connection = DriverManager::getConnection($data, $config);
 }
 /**
  * @param ContainerInterface $container
  * @param Configuration      $config
  */
 public function setupDBALConfiguration(ContainerInterface $container, Configuration $config)
 {
     $options = $this->getOptions($container);
     $config->setResultCacheImpl($container->get($options->resultCache));
     $sqlLogger = $options->sqlLogger;
     if (is_string($sqlLogger) and $container->has($sqlLogger)) {
         $sqlLogger = $container->get($sqlLogger);
     }
     $config->setSQLLogger($sqlLogger);
     foreach ($options->types as $name => $class) {
         if (Type::hasType($name)) {
             Type::overrideType($name, $class);
         } else {
             Type::addType($name, $class);
         }
     }
 }
示例#20
0
 public function register(Neptune $neptune)
 {
     $neptune['db.config'] = function ($neptune) {
         $config = $neptune['config']->get('neptune.database', []);
         if (empty($config)) {
             throw new ConfigKeyException('Database configuration is empty');
         }
         return $config;
     };
     $neptune['dbs'] = function ($neptune) {
         $dbs = new Container();
         $config = $neptune['db.config'];
         //register types
         if (isset($config['_types'])) {
             foreach ($config['_types'] as $name => $classname) {
                 Type::addType($name, $classname);
             }
             unset($config['_types']);
         }
         foreach ($config as $name => $config) {
             $dbs[$name] = function ($dbs) use($config, $neptune) {
                 $configuration = new Configuration();
                 if (isset($config['logger'])) {
                     $configuration->setSQLLogger(new PsrSqlLogger($neptune[$config['logger']]));
                 }
                 return DriverManager::getConnection($config, $configuration);
             };
         }
         return $dbs;
     };
     //shortcut for the first database
     $neptune['db'] = function ($neptune) {
         $config = $neptune['db.config'];
         reset($config);
         $default = key($config);
         return $neptune['dbs'][$default];
     };
 }
示例#21
0
 /**
  * @param array
  * @param \Doctrine\Common\EventManager|NULL
  * @return \Doctrine\DBAL\Connection
  */
 public static function createConnection(array $params, EventManager $evm)
 {
     $panel = NULL;
     $config = new Configuration();
     if (isset($params['debugger']) && $params['debugger'] === TRUE) {
         $panel = new ConnectionPanel();
         if (Debugger::$bar) {
             Debugger::$bar->addPanel($panel);
         }
         Debugger::$blueScreen->addPanel(array($panel, 'renderException'));
         $config->setSQLLogger($panel);
     } else {
         Debugger::$blueScreen->addPanel('Nette\\Database\\Diagnostics\\ConnectionPanel::renderException');
     }
     $cfg = $params['connection'];
     $connection = DriverManager::getConnection($cfg, $config, $evm);
     if ($connection->getDatabasePlatform()->getName() == 'mysql' && isset($cfg['charset'])) {
         $evm->addEventSubscriber(new MysqlSessionInit($cfg['charset'], $cfg['collation']));
     }
     if ($panel && $panel->doExplains) {
         $panel->setConnection($connection);
     }
     return $connection;
 }
示例#22
0
 public function setSqlLogger(Doctrine\DBAL\Logging\SQLLogger $sqlLogger)
 {
     $this->entityManagerConfiguration->setSQLLogger($sqlLogger);
 }
示例#23
0
文件: Connection.php 项目: jay45/porn
 /**
  * Initializes a new instance of the Connection class.
  *
  * @param array                              $params       The connection parameters.
  * @param \Doctrine\DBAL\Driver              $driver       The driver to use.
  * @param \Doctrine\DBAL\Configuration|null  $config       The configuration, optional.
  * @param \Doctrine\Common\EventManager|null $eventManager The event manager, optional.
  *
  * @throws \Doctrine\DBAL\DBALException
  */
 public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null)
 {
     $this->_driver = $driver;
     $this->_params = $params;
     if (isset($params['pdo'])) {
         $this->_conn = $params['pdo'];
         $this->_isConnected = true;
     }
     // Create default config and event manager if none given
     if (!$config) {
         $config = new Configuration();
     }
     if (!$eventManager) {
         $eventManager = new EventManager();
     }
     $this->_config = $config;
     $this->_eventManager = $eventManager;
     $this->_expr = new Query\Expression\ExpressionBuilder($this);
     if (!isset($params['platform'])) {
         $this->_platform = $driver->getDatabasePlatform();
     } elseif ($params['platform'] instanceof Platforms\AbstractPlatform) {
         $this->_platform = $params['platform'];
     } else {
         throw DBALException::invalidPlatformSpecified();
     }
     $this->_platform->setEventManager($eventManager);
     $this->_transactionIsolationLevel = $this->_platform->getDefaultTransactionIsolationLevel();
     $this->autoCommit = $config->getAutoCommit();
 }
示例#24
0
 /**
  * Initialize the panel (set a SQL logger)
  *
  * @param Configuration $doctrineConfiguration The doctrine configuration
  * @param string        $name                  The name of the panel (Useful if you watch multiple Doctrine instance)
  */
 public function __construct(Configuration $doctrineConfiguration, $name = '')
 {
     $doctrineConfiguration->setSQLLogger(new DebugStack());
     $this->doctrineConfiguration = $doctrineConfiguration;
     $this->name = $name;
 }
 /**
  * Initializes a new instance of the Connection class.
  *
  * @param array                              $params       The connection parameters.
  * @param \Doctrine\DBAL\Driver              $driver       The driver to use.
  * @param \Doctrine\DBAL\Configuration|null  $config       The configuration, optional.
  * @param \Doctrine\Common\EventManager|null $eventManager The event manager, optional.
  *
  * @throws \Doctrine\DBAL\DBALException
  */
 public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null)
 {
     $this->_driver = $driver;
     $this->_params = $params;
     if (isset($params['pdo'])) {
         $this->_conn = $params['pdo'];
         $this->_isConnected = true;
         unset($this->_params['pdo']);
     }
     // Create default config and event manager if none given
     if (!$config) {
         $config = new Configuration();
     }
     if (!$eventManager) {
         $eventManager = new EventManager();
     }
     $this->_config = $config;
     $this->_eventManager = $eventManager;
     $this->_expr = new Query\Expression\ExpressionBuilder($this);
     $this->autoCommit = $config->getAutoCommit();
 }
示例#26
0
 /**
  * Initializes a new instance of the Connection class.
  *
  * @param array $params  The connection parameters.
  * @param Driver $driver
  * @param Configuration $config
  * @param EventManager $eventManager
  */
 public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null)
 {
     $this->_driver = $driver;
     $this->_params = $params;
     if (isset($params['pdo'])) {
         $this->_conn = $params['pdo'];
         $this->_isConnected = true;
     }
     // Create default config and event manager if none given
     if (!$config) {
         $config = new Configuration();
     }
     if (!$eventManager) {
         $eventManager = new EventManager();
     }
     $this->_config = $config;
     $this->_eventManager = $eventManager;
     $this->_platform = $driver->getDatabasePlatform();
     $this->_transactionIsolationLevel = $this->_platform->getDefaultTransactionIsolationLevel();
     $this->_quoteIdentifiers = $config->getQuoteIdentifiers();
     $this->_platform->setQuoteIdentifiers($this->_quoteIdentifiers);
 }
示例#27
0
 public function addMysqlFunctions()
 {
     $this->config->addCustomNumericFunction('RAND', DoctrineFunctions\Mysql\Rand::class);
     $this->config->addCustomNumericFunction('DISTANCE', DoctrineFunctions\Mysql\Distance::class);
 }
示例#28
0
 /**
  * Creates a new configuration that can be used for Doctrine.
  */
 public function __construct()
 {
     parent::__construct();
     $this->_attributes = array_merge($this->_attributes, array('resultCacheImpl' => null, 'queryCacheImpl' => null, 'metadataCacheImpl' => null, 'metadataDriverImpl' => null, 'proxyDir' => null, 'useCExtension' => false, 'autoGenerateProxyClasses' => true, 'proxyNamespace' => null));
 }
示例#29
0
文件: Connection.php 项目: acp3/core
 /**
  * @throws \Doctrine\DBAL\DBALException
  */
 protected function connect()
 {
     $config = new DBAL\Configuration();
     if ($this->appMode === ApplicationMode::DEVELOPMENT) {
         $config->setSQLLogger(new SQLLogger($this->logger));
     }
     $config->setResultCacheImpl($this->cacheDriverFactory->create('db-queries'));
     return DBAL\DriverManager::getConnection($this->connectionParams, $config);
 }
示例#30
0
 /**
  * Executes an SQL statement, returning a result set as a Statement object.
  *
  * @return \Doctrine\DBAL\Driver\Statement
  *
  * @throws \Doctrine\DBAL\DBALException
  */
 public function query()
 {
     $this->connect();
     $args = func_get_args();
     $logger = $this->_config->getSQLLogger();
     if ($logger) {
         $logger->startQuery($args[0]);
     }
     try {
         switch (func_num_args()) {
             case 1:
                 $statement = $this->_conn->query($args[0]);
                 break;
             case 2:
                 $statement = $this->_conn->query($args[0], $args[1]);
                 break;
             default:
                 $statement = call_user_func_array(array($this->_conn, 'query'), $args);
                 break;
         }
     } catch (\Exception $ex) {
         throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $args[0]);
     }
     $statement->setFetchMode($this->defaultFetchMode);
     if ($logger) {
         $logger->stopQuery();
     }
     return $statement;
 }