Beispiel #1
0
 function connect()
 {
     global $user;
     global $db_include_path;
     if ($this->pear_type == 'MDB2') {
         require_once $db_include_path . 'MDB2.php';
         $this->mode = 'MDB2';
         $this->fetch_mode = MDB2_FETCHMODE_ASSOC;
     } else {
         require_once $db_include_path . 'DB.php';
         $this->mode = 'DB';
         $this->fetch_mode = DB_FETCHMODE_ASSOC;
     }
     if ($this->db_split) {
         $this->debug[] = 'configuration: db_split is set to true, both read and write servers required';
     } else {
         $this->debug[] = 'configuration: db_split is set to false, only one server for read/write required';
     }
     if ($this->random_connect_read) {
         $this->debug[] = 'configuration: Read server order being randomized';
     } else {
         $this->debug[] = 'configuration: Maintaining read server order';
     }
     if ($this->random_connect_write) {
         $this->debug[] = 'configuration: Write server order being randomized';
     } else {
         $this->debug[] = 'configuration: Maintaining write server order';
     }
     if (empty($this->read_servers)) {
         $this->debug[] = 'connecting: No sql read servers defined';
     } else {
         if ($this->random_connect_read) {
             shuffle($this->read_servers);
         }
         foreach ($this->read_servers as $v) {
             $dsn = $this->db_type . '://' . $v[0] . ':' . $v[1] . '@' . $v[2] . '/' . $v[3];
             if ($this->pear_type == 'MDB2') {
                 $db_read =& MDB2::Connect($dsn, array('persistent' => $this->persistent));
             } else {
                 $db_read = DB::Connect($dsn, $this->persistent);
             }
             $this->db_read = $db_read;
             if (PEAR::isError($this->db_read) || !$this->db_read) {
                 $this->debug[] = 'connecting: UNABLE TO CONNECT to read DB: ' . $v[2] . ' (' . $this->db_type . ') as ' . $v[0] . ' using PEAR ' . $this->pear_type;
             } else {
                 $this->debug[] = 'connecting: Connected to read DB: ' . $v[2] . ' (' . $this->db_type . ') as ' . $v[0] . ' using PEAR ' . $this->pear_type;
                 $this->current_read = $v;
                 break;
             }
         }
         if (!$this->db_read || PEAR::isError($this->db_read)) {
             $this->debug[] = 'connecting: Could not connect to any configured read servers';
         } else {
             if ($this->db_split) {
                 if (empty($this->write_servers)) {
                     $this->debug[] = 'connecting: No sql write servers defined';
                 } else {
                     if ($this->random_connect_write) {
                         shuffle($this->write_servers);
                     }
                     foreach ($this->write_servers as $v) {
                         $dsn = $this->db_type . '://' . $v[0] . ':' . $v[1] . '@' . $v[2] . '/' . $v[3];
                         if ($this->pear_type == 'MDB2') {
                             $this->db_write =& MDB2::Connect($dsn, true);
                         } else {
                             $this->db_write =& DB::Connect($dsn, true);
                         }
                         if (PEAR::isError($this->db_write) || !$this->db_write) {
                             $this->debug[] = 'connecting: Unable to connect to write DB: ' . $v[2] . ' as ' . $v[0];
                         } else {
                             $this->debug[] = 'connecting: Connected to write DB: ' . $v[2] . ' (' . $this->db_type . ') as ' . $v[0] . ' using PEAR ' . $this->pear_type;
                             $this->current_write = $v;
                             break;
                         }
                     }
                     if (!$this->db_write || PEAR::isError($this->db_write)) {
                         $this->debug[] = 'connecting: Could not connect to any configured write servers';
                     }
                 }
             } else {
                 $this->db_write = $this->db_read;
                 $this->debug[] = 'connection: db_split is set to false, using current read server as read/write';
                 $this->debug[] = 'connecting: Successfully connnected to required sql servers';
                 return true;
             }
         }
     }
     $user->notices[] = 'Could not establish a connection to the database';
 }
Beispiel #2
0
 /**
  *  Open a database connection if one is not currently open
  *
  *  The name of the database normally comes from
  *  $database_settings which is set in {@link
  *  environment.php} by reading file config/database.ini. The
  *  database name may be overridden by assigning a different name
  *  to {@link $database_name}. 
  *  
  *  If there is a connection now open, as indicated by the saved
  *  value of a MDB2 object in $active_connections[$connection_name], and
  *  {@link force_reconnect} is not true, then set the database
  *  fetch mode and return.
  *
  *  If there is no connection, open one and save a reference to
  *  it in $active_connections[$connection_name].
  *
  *  @uses $db
  *  @uses $database_name
  *  @uses $force_reconnect
  *  @uses $active_connections
  *  @uses is_error()
  *  @throws {@link ActiveRecordError}
  */
 function establish_connection()
 {
     $connection =& self::$active_connections[$this->connection_name];
     if (!is_object($connection) || $this->force_reconnect) {
         $connection_settings = array();
         $connection_options = array();
         if (array_key_exists($this->connection_name, self::$database_settings)) {
             # Use a different custom sections settings ?
             if (array_key_exists("use", self::$database_settings[$this->connection_name])) {
                 $connection_settings = self::$database_settings[self::$database_settings[$this->connection_name]['use']];
             } else {
                 # Custom defined db settings in database.ini
                 $connection_settings = self::$database_settings[$this->connection_name];
             }
         } else {
             # Just use the current TRAX_ENV's environment db settings
             # $this->connection_name's default value is TRAX_ENV so
             # if should never really get here unless override $this->connection_name
             # and you define a custom db section in database.ini and it can't find it.
             $connection_settings = self::$database_settings[TRAX_ENV];
         }
         # Override database name if param is set
         if ($this->database_name) {
             $connection_settings['database'] = $this->database_name;
         }
         # Set optional Pear parameters
         if (isset($connection_settings['persistent'])) {
             $connection_options['persistent'] = $connection_settings['persistent'];
         }
         # Connect to the database and throw an error if the connect fails.
         //print("MDB2::Connect($connection_settings, $connection_options) \n");
         $connection =& MDB2::Connect($connection_settings, $connection_options);
         //var_dump($connection_settings);
         //static $connect_cnt;  $connect_cnt++; error_log("connection #".$connect_cnt);
         # For Postgres schemas (http://www.postgresql.org/docs/8.0/interactive/ddl-schemas.html)
         if (isset($connection_settings['schema_search_path'])) {
             if (!$this->is_error($connection)) {
                 # Set the schema search path to a string of comma-separated schema names.
                 # First strip out all the whitespace
                 $connection->query('SET search_path TO ' . preg_replace('/\\s+/', '', $connection_settings['schema_search_path']));
             }
         }
     }
     if (!$this->is_error($connection)) {
         self::$active_connections[$this->connection_name] =& $connection;
         self::$db =& $connection;
         self::$db->setFetchMode($this->fetch_mode);
     } else {
         $this->raise($connection->getMessage());
     }
     return self::$db;
 }
Beispiel #3
0
 /**
  *  Open a database connection if one is not currently open
  *
  *  The name of the database normally comes from
  *  $database_settings which is set in {@link
  *  environment.php} by reading file config/database.ini. The
  *  database name may be overridden by assigning a different name
  *  to {@link $database_name}.
  *
  *  If there is a connection now open, as indicated by the saved
  *  value of a MDB2 object in $connection_pool[$connection_name], and
  *  {@link force_reconnect} is not true, then set the database
  *  fetch mode and return.
  *
  *  If there is no connection, open one and save a reference to
  *  it in $connection_pool[$connection_name].
  *
  *  @uses $db
  *  @uses $database_name
  *  @uses $force_reconnect
  *  @uses $connection_pool
  *  @uses is_error()
  *  @throws {@link ActiveRecordError}
  */
 function establish_connection($connection_name = null, $read_only = false)
 {
     #error_log("trying connection name:$connection_name");
     $connection_name = $this->get_connection_name($connection_name);
     #error_log("got connection name:$connection_name read only:".($read_only ? 'true' : 'false'));
     if ($read_only) {
         $connection =& self::$connection_pool_read_only[$connection_name];
     } else {
         $connection =& self::$connection_pool[$connection_name];
     }
     if (!is_object($connection) || $this->force_reconnect) {
         $connection_settings = array();
         $connection_options = array();
         if (array_key_exists($connection_name, self::$database_settings)) {
             # Use a different custom sections settings ?
             if (array_key_exists("use", self::$database_settings[$connection_name])) {
                 $connection_settings = self::$database_settings[self::$database_settings[$connection_name]['use']];
             } else {
                 # Custom defined db settings in database.ini
                 $connection_settings = self::$database_settings[$connection_name];
             }
         } else {
             # Just use the current environment's db settings
             # $connection_name's default value is 'development' so
             # it should never really get here unless you override $this->connection_name
             # and you define a custom db section in database.ini and it can't find it.
             $connection_settings = self::$database_settings[$connection_name];
         }
         # Override database name if param is set
         if ($this->database_name) {
             $connection_settings['database'] = $this->database_name;
         }
         # Set optional Pear parameters
         if (isset($connection_settings['persistent'])) {
             $connection_options['persistent'] = $connection_settings['persistent'];
         }
         # Connect to the database and throw an error if the connect fails.
         $connection =& MDB2::Connect($connection_settings, $connection_options);
         #static $connect_cnt;  $connect_cnt++; error_log("establish_connection($connection_name, $read_only) #".$connect_cnt);
         # For Postgres schemas (http://www.postgresql.org/docs/8.0/interactive/ddl-schemas.html)
         if (isset($connection_settings['schema_search_path'])) {
             if (!$this->is_error($connection)) {
                 # Set the schema search path to a string of comma-separated schema names.
                 # First strip out all the whitespace
                 $connection->query('SET search_path TO ' . preg_replace('/\\s+/', '', $connection_settings['schema_search_path']));
             }
         }
     }
     if (!$this->is_error($connection)) {
         $connection->setFetchMode($this->fetch_mode);
         if ($read_only) {
             self::$connection_pool_read_only[$connection_name] =& $connection;
             $pool_size = count(self::$connection_pool_read_only);
             $this->read_only_connection_name = $connection_name;
         } else {
             self::$connection_pool[$connection_name] =& $connection;
             $pool_size = count(self::$connection_pool);
             $this->connection_name = $connection_name;
         }
         if ($pool_size > 1 || $this->database_name != '') {
             $dsn = $connection->getDSN('array', true);
             #error_log("dsn:".print_r($dsn, true));
             if ($this->database_name != '') {
                 #$type = "database defined";
                 $database_name = $this->database_name;
             } elseif ($dsn['database'] != '') {
                 #$type = "dsn";
                 $database_name = $dsn['database'];
             }
             if ($database_name) {
                 #error_log("connect $type switch database to {$database_name}");
                 $connection->setDatabase($database_name);
             }
         }
     } else {
         $this->raise($connection->getMessage());
     }
     return $connection;
 }