Example #1
0
 /**
  * This function returns an instance of the appropriate pre-compiler for the
  * specified data source/config.
  *
  * @access public
  * @static
  * @param mixed $config                         the data source configurations
  * @return DB_SQL_Precompiler                   an instance of the pre-compiler
  */
 public static function precompiler($config = 'default')
 {
     $data_source = DB_DataSource::instance($config);
     $precompiler = 'DB_' . $data_source->dialect . '_Precompiler';
     $object = new $precompiler($data_source);
     return $object;
 }
Example #2
0
 /**
  * This function tests DB_DataSource::__construct().
  *
  * @access public
  * @param mixed $test_data                          the test data
  * @param string $expected                          the expected values
  *
  * @dataProvider provider_constructor
  */
 public function test_constructor($test_data, $expected)
 {
     // Initialization
     $data_source = new DB_DataSource($test_data);
     // Assertions
     $this->assertRegExp('/^(database|unique_id)\\.[a-zA-Z0-9_]+$/', $data_source->id, 'Failed when testing "id" property.');
     $this->assertSame($expected['type'], $data_source->type, 'Failed when testing "type" property.');
     $this->assertSame($expected['dialect'], $data_source->dialect, 'Failed when testing "dialect" property.');
     $this->assertSame($expected['driver'], $data_source->driver, 'Failed when testing "driver" property.');
     $this->assertSame($expected['connection']['persistent'], $data_source->is_persistent(), 'Failed when testing is_persistent().');
     $this->assertSame($expected['connection']['hostname'], $data_source->host, 'Failed when testing "host" property.');
     $this->assertSame($expected['connection']['port'], $data_source->port, 'Failed when testing "port" property.');
     $this->assertSame($expected['connection']['database'], $data_source->database, 'Failed when testing "database" property.');
     $this->assertSame($expected['connection']['username'], $data_source->username, 'Failed when testing "username" property.');
     $this->assertSame($expected['connection']['password'], $data_source->password, 'Failed when testing "password" property.');
     $this->assertSame($expected['connection']['role'], $data_source->role, 'Failed when testing "role" property.');
     $this->assertSame($expected['charset'], $data_source->charset, 'Failed when testing "charset" property.');
 }
Example #3
0
 /**
  * This constructor instantiates this class using the specified model's name.
  *
  * @access public
  * @param string $model                             the model's name
  */
 public function __construct($model)
 {
     $name = $model;
     $model = DB_ORM_Model::model_name($name);
     $this->data_source = DB_DataSource::instance($model::data_source(DB_DataSource::MASTER_INSTANCE));
     $builder = 'DB_' . $this->data_source->dialect . '_Insert_Builder';
     $this->builder = new $builder($this->data_source);
     $extension = DB_ORM_Model::builder_name($name);
     if (class_exists($extension)) {
         $this->extension = new $extension($this->builder);
     }
     $table = $model::table();
     $this->builder->into($table);
     $this->model = $model;
 }
Example #4
0
 /**
  * This function returns a connection to the appropriate database based
  * on the specified configurations.
  *
  * @access public
  * @static
  * @param mixed $config                         the data source configurations
  * @return DB_Connection_Driver                 the database connection
  */
 public static function factory($config = 'default')
 {
     $data_source = DB_DataSource::instance($config);
     $driver = 'DB_' . $data_source->dialect . '_Connection_' . $data_source->driver;
     $connection = new $driver($data_source);
     return $connection;
 }
Example #5
0
 /**
  * This constructor instantiates this class using the specified data source.
  *
  * @access public
  * @param mixed $config                             the data source configurations
  */
 public function __construct($config)
 {
     $this->data_source = DB_DataSource::instance($config);
     $builder = 'DB_' . $this->data_source->dialect . '_Insert_Builder';
     $this->builder = new $builder($this->data_source);
 }
Example #6
0
 /**
  * This constructor instantiates this class using the specified data source.
  *
  * @access public
  * @param mixed $config                  the data source configurations
  */
 public function __construct($config)
 {
     $this->data_source = DB_DataSource::instance($config);
     $precompiler = 'DB_' . $this->data_source->dialect . '_Precompiler';
     $this->precompiler = new $precompiler();
 }
Example #7
0
 /**
  * This constructor instantiates this class using the specified model's name.
  *
  * @access public
  * @param string $model                             the model's name
  * @param array $columns                            the columns to be selected
  */
 public function __construct($model, array $columns = array())
 {
     $name = $model;
     $model = DB_ORM_Model::model_name($name);
     $this->data_source = DB_DataSource::instance($model::data_source(DB_DataSource::SLAVE_INSTANCE));
     $builder = 'DB_' . $this->data_source->dialect . '_Select_Builder';
     $this->table = $model::table();
     $this->builder = new $builder($this->data_source, $columns);
     if (empty($columns)) {
         $this->builder->all("{$this->table}.*");
     }
     $this->builder->from($this->table);
     $extension = DB_ORM_Model::builder_name($name);
     if (class_exists($extension)) {
         $this->extension = new $extension($this->builder);
     }
     $this->model = $model;
 }
Example #8
0
 /**
  * This function returns the appropriate connection from the pool. When there are
  * multiple connections created from the same data source, the last opened connection
  * will be returned when $new is set to "FALSE."
  *
  * @access public
  * @param mixed $config                         the data source configurations
  * @param boolean $new                          whether to create a new connection
  * @return DB_Connection_Driver                 the appropriate connection
  * @throws Throwable_Database_Exception         indicates that no new connections
  *                                              can be added
  */
 public function get_connection($config = 'default', $new = FALSE)
 {
     $data_source = DB_DataSource::instance($config);
     if (isset($this->pool[$data_source->id]) and !empty($this->pool[$data_source->id])) {
         if ($new) {
             foreach ($this->pool[$data_source->id] as $connection) {
                 if (!$connection->is_connected()) {
                     $connection->open();
                     return $connection;
                 }
             }
         } else {
             $connection = end($this->pool[$data_source->id]);
             do {
                 if ($connection->is_connected()) {
                     reset($this->pool[$data_source->id]);
                     return $connection;
                 }
             } while ($connection = prev($this->pool[$data_source->id]));
             $connection = end($this->pool[$data_source->id]);
             reset($this->pool[$data_source->id]);
             $connection->open();
             return $connection;
         }
     }
     if ($this->count() >= $this->settings['max_size']) {
         throw new Throwable_Database_Exception('Message: Failed to create new connection. Reason: Exceeded maximum number of connections that may be held in the pool.', array(':source' => $data_source, ':new' => $new));
     }
     $connection = DB_Connection_Driver::factory($data_source);
     $connection->open();
     $connection_id = $connection->__hashCode();
     $this->pool[$data_source->id][$connection_id] = $connection;
     $this->lookup[$connection_id] = $data_source->id;
     return $connection;
 }
Example #9
0
 /**
  * This constructor instantiates this class using the specified data source.
  *
  * @access public
  * @param mixed $config                         the data source configurations
  * @param array $columns                        the columns to be selected
  */
 public function __construct($config, array $columns = array())
 {
     $this->data_source = DB_DataSource::instance($config);
     $builder = 'DB_' . $this->data_source->dialect . '_Select_Builder';
     $this->builder = new $builder($this->data_source, $columns);
 }