Ejemplo n.º 1
0
 /**
  * @return	null
  */
 public function setUp()
 {
     $this->runDbStartupTask();
     $params = DbRegistry::getConnectionParams('af-tester');
     $this->params = $params->get('conn-params');
     $this->conn = new MysqliConn($this->params);
 }
Ejemplo n.º 2
0
 /**
  * @return	null
  */
 public function setUp()
 {
     $this->runDbStartupTask();
     $connector = DbRegistry::getConnector('af-tester');
     $this->conn = $connector->getConnection();
     $this->sql = "SELECT param_2, result \n\t\t\t\t\t  FROM   test_queries \n\t\t\t\t\t  WHERE  query_id IN(1,2,3)";
     $this->conn->connect();
 }
Ejemplo n.º 3
0
 /**
  * @return	null
  */
 public function setUp()
 {
     $this->runDbStartupTask();
     $connector = DbRegistry::getConnector('af-tester');
     $this->conn = $connector->getConnection();
     $this->sql = "SELECT param_2, result \n\t\t\t\t\t  FROM   test_queries \n\t\t\t\t\t  WHERE  query_id = 1;\n\n\t\t\t\t\t  SELECT param_2, result \n\t\t\t\t\t  FROM   test_queries \n\t\t\t\t\t  WHERE  query_id = 2;\n\t\t\t\t\t\n\t\t\t\t\t  SELECT param_2, result \n\t\t\t\t\t  FROM   test_queries \n\t\t\t\t\t  WHERE  query_id = 3";
     $this->conn->connect();
     $this->adapter = new MultiQueryAdapter();
 }
Ejemplo n.º 4
0
 /**
  * @return null
  */
 public function setUp()
 {
     $this->runDbStartupTask();
     $connector = DbRegistry::getConnector('af-tester');
     $this->conn = $connector->getConnection();
     $this->conn->connect();
     $driver = $this->conn->getDriver();
     $this->stmtDriver = $driver->stmt_init();
     $this->stmt = new PreparedStmt($this->stmtDriver);
 }
Ejemplo n.º 5
0
 /**
  * @param	string	$key
  * @return	DbConnectorInterface | false 
  */
 public function getConnector($key = null)
 {
     if (null === $key) {
         $key = DbRegistry::getDefaultConnectorKey();
     }
     return DbRegistry::getConnector($key);
 }
Ejemplo n.º 6
0
 public function testClear()
 {
     $list = array('key1' => $this->getMockConnector(), 'key2' => $this->getMockConnector(), 'key3' => $this->getMockConnector());
     DbRegistry::setConnectors($list);
     $list2 = array('param1' => $this->getMockDictionary(), 'param2' => $this->getMockDictionary(), 'param3' => $this->getMockDictionary());
     DbRegistry::setConnectionParams($list2);
     $this->assertNull(DbRegistry::clear());
     $this->assertEquals(array(), DbRegistry::getAllConnectors());
     $this->assertEquals(array(), DbRegistry::getAllConnectionParams());
 }
Ejemplo n.º 7
0
 /**
  * @param	array	$params		config params 
  * @return	null
  */
 public function execute(array $params = null)
 {
     if (empty($params)) {
         $err = 'database startup requires config params which are empty';
         throw new InvalidArgumentException($err);
     }
     if (!isset($params['db']) || !is_array($params['db'])) {
         $err = 'database config is not set or not an array';
         throw new InvalidArgumentException($err);
     }
     $db = $params['db'];
     if (!is_array($db) || $db === array_values($db)) {
         $err = 'database config must be an associative array of ';
         $err .= 'connector-key=>connector-config pairs';
         throw new InvalidArgumentException($err);
     }
     if (!isset($db['connectors']) || !is_array($db['connectors'])) {
         $err = 'db connectors have been set but are not an array';
         throw new InvalidArgumentException($err);
     }
     $connectors = $db['connectors'];
     if (isset($db['default-connector'])) {
         DbRegistry::setDefaultConnectorKey($db['default-connector']);
     }
     /*
      * used when many database connectors are defined in the
      * common section of a config but you want to confine your app to
      * a subset of those connectors
      */
     if (isset($db['use'])) {
         $scope = $db['use'];
         if (is_string($scope) && isset($connectors[$scope])) {
             $connectors = array($scope => $connectors[$scope]);
         } else {
             if (is_array($scope)) {
                 if ($scope === array_values($scope)) {
                     $tmp = array();
                     foreach ($connectors as $key => $data) {
                         if (in_array($key, $scope)) {
                             $tmp[$key] = $data;
                         }
                     }
                     $connectors = $tmp;
                 } else {
                     if (isset($scope) && is_array($scope)) {
                         $tmp = array();
                         foreach ($connectors as $key => $data) {
                             if (in_array($key, $scope)) {
                                 $tmp[$key] = $data;
                             }
                         }
                         $connectors = $tmp;
                     }
                 }
             }
         }
     }
     /**
      * For every connector we need to determine the adapter which 
      * is a vendor specific connection class, and if that connector
      * is using replication and finally if their is a custom connector
      * class to be used. With that we create a series of connectors and
      * add them to the db registry to be used
      */
     $factory = $this->getFactory();
     foreach ($connectors as $key => $config) {
         if (!is_string($key) || empty($key)) {
             $err = 'connector key must be a non empty string';
             throw new InvalidArgumentException($err);
         }
         if (!isset($config['adapter'])) {
             $err = 'missing config setting -(adapter)';
             throw new InvalidArgumentException($err);
         }
         /* save the connection parameters separately in the registry
          * so that connections setting can be reused without having to 
          * go back to the config file
          */
         DbRegistry::addConnectionParams($key, $config);
         $slave = null;
         $adapterClass = $config['adapter'];
         $connectorClass = null;
         if (isset($config['connector-class'])) {
             $connectorClass = $config['connector'];
             if (!is_string($connectorClass) || empty($connectorClass)) {
                 $err = 'connector class must be a non empty string';
                 throw new InvalidArgumentException($err);
             }
         }
         if (isset($config['master-params'])) {
             $master = $factory->createConnection($adapterClass, $config['master-params']);
             if (isset($config['slave-params'])) {
                 $slave = $factory->createConnection($adapterClass, $config['slave-params']);
             }
         } else {
             if (isset($config['conn-params'])) {
                 $master = $factory->createConnection($adapterClass, $config['conn-params']);
             } else {
                 $err = 'could not find any connection parameters, looking ';
                 $err .= 'for: replication -(master-params|[slave-params]) or ';
                 $err .= 'non-replication -(conn-params)';
                 throw new InvalidArgumentException($err);
             }
         }
         /* when connector class is null the factory will create a default
          * connector, for appfuel that is DbConnector
          */
         $connector = $factory->createConnector($master, $slave, $connectorClass);
         /*
          * The framework relies on the fact that we build and add db 
          * connectors to the registry. Datasources have no idea how to 
          * create connectors. 
          */
         DbRegistry::addConnector($key, $connector);
     }
 }