Example #1
0
 /**
  * Sets up the database configuration, loads the database driver
  *
  * @param array $config
  */
 public function __construct($config = array())
 {
     if (empty($config)) {
         // Load the default config
         $config = AetherDatabaseConfig::retrieve('database.default');
     } elseif (is_array($config) && count($config) > 0) {
         if (!array_key_exists('connection', $config)) {
             $config = array('connection' => $config);
         }
     } elseif (is_string($config)) {
         // The config is a DSN string
         if (strpos($config, '://') !== false) {
             $config = array('connection' => $config);
         } else {
             $name = $config;
             // Test the config name
             if (($config = AetherDatabaseConfig::retrieve('database.' . $config)) === null) {
                 throw new DatabaseException("undefined config {$name}");
             }
         }
     }
     // Merge the default config with the passed config
     $this->config = array_merge($this->config, $config);
     // If the connection is a DSN string
     if (is_string($this->config['connection'])) {
         // Make sure the connection is valid
         if (strpos($this->config['connection'], '://') === false) {
             throw new DatabaseException('invalid_dsn ' . $this->config['connection']);
         }
         // Parse the DSN, creating an array to hold the connection parameters
         $db = array('type' => FALSE, 'user' => FALSE, 'pass' => FALSE, 'host' => FALSE, 'port' => FALSE, 'socket' => FALSE, 'database' => FALSE);
         // Get the protocol and arguments
         list($db['type'], $connection) = explode('://', $this->config['connection'], 2);
         if (strpos($connection, '@') !== false) {
             // Get the username and password
             list($db['pass'], $connection) = explode('@', $connection, 2);
             // Check if a password is supplied
             $logindata = explode(':', $db['pass'], 2);
             $db['pass'] = count($logindata) > 1 ? $logindata[1] : '';
             $db['user'] = $logindata[0];
             // Prepare for finding the database
             $connection = explode('/', $connection);
             // Find the database name
             $db['database'] = array_pop($connection);
             // Reset connection string
             $connection = implode('/', $connection);
             // Find the socket
             if (preg_match('/^unix\\([^)]++\\)/', $connection)) {
                 // This one is a little hairy: we explode based on the end of
                 // the socket, removing the 'unix(' from the connection string
                 list($db['socket'], $connection) = explode(')', substr($connection, 5), 2);
             } elseif (strpos($connection, ':') !== false) {
                 // Fetch the host and port name
                 list($db['host'], $db['port']) = explode(':', $connection, 2);
             } else {
                 $db['host'] = $connection;
             }
         } else {
             // File connection
             $connection = explode('/', $connection);
             // Find database file name
             $db['database'] = array_pop($connection);
             // Find database directory name
             $db['socket'] = implode('/', $connection) . '/';
         }
         // Reset the connection array to the database config
         $this->config['connection'] = $db;
     }
     // Set the driver name
     $driver = 'AetherDatabase' . ucfirst($this->config['connection']['type']) . 'Driver';
     // Load the driver
     if (!AetherDatabaseConfig::autoLoad($driver)) {
         throw new DatabaseException('driver_not_found ' . $this->config['connection']['type']);
     }
     // Initialize the driver
     $this->driver = new $driver($this->config);
     // Validate the driver
     if (!$this->driver instanceof AetherDatabaseDriver) {
         throw new DatabaseException('driver ' . $this->config['connection']['type'] . ' does not implement DatabaseDriver');
     }
     // ::log('debug', 'Database library initialized');
 }
Example #2
0
 public function resultArray($object = null, $type = PGSQL_ASSOC)
 {
     $rows = array();
     if (is_string($object)) {
         $fetch = $object;
     } elseif (is_bool($object)) {
         if ($object === true) {
             $fetch = 'pg_fetch_object';
             // NOTE - The class set by $type must be defined before fetching the result,
             // autoloading is disabled to save a lot of stupid overhead.
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         } else {
             $fetch = 'pg_fetch_array';
         }
     } else {
         // Use the default config values
         $fetch = $this->fetchType;
         if ($fetch == 'pg_fetch_object') {
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         }
     }
     if ($this->totalRows) {
         pg_result_seek($this->result, 0);
         while ($row = $fetch($this->result, NULL, $type)) {
             $rows[] = $row;
         }
     }
     return $rows;
 }
Example #3
0
 public function resultArray($object = NULL, $type = MYSQL_ASSOC)
 {
     $rows = array();
     if (is_string($object)) {
         $fetch = $object;
     } elseif (is_bool($object)) {
         if ($object === true) {
             $fetch = 'mysql_fetch_object';
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         } else {
             $fetch = 'mysql_fetch_array';
         }
     } else {
         // Use the default config values
         $fetch = $this->fetchType;
         if ($fetch == 'mysql_fetch_object') {
             if (is_string($this->returnType) && AetherDatabaseConfig::autoLoad($this->returnType)) {
                 $type = $this->returnType;
             } else {
                 $type = 'stdClass';
             }
         }
     }
     if (mysql_num_rows($this->result)) {
         // Reset the pointer location to make sure things work properly
         mysql_data_seek($this->result, 0);
         while ($row = $fetch($this->result, $type)) {
             $rows[] = $row;
         }
     }
     return isset($rows) ? $rows : array();
 }
Example #4
0
 public function resultArray($object = NULL, $type = PDO::FETCH_ASSOC)
 {
     $rows = array();
     if (is_string($object)) {
         $fetch = $object;
     } elseif (is_bool($object)) {
         if ($object === true) {
             $fetch = PDO::FETCH_OBJ;
             // NOTE - The class set by $type must be defined before fetching the result,
             // autoloading is disabled to save a lot of stupid overhead.
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         } else {
             $fetch = PDO::FETCH_OBJ;
         }
     } else {
         // Use the default config values
         $fetch = $this->fetchType;
         if ($fetch == PDO::FETCH_OBJ) {
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         }
     }
     try {
         while ($row = $this->result->fetch($fetch)) {
             $rows[] = $row;
         }
     } catch (PDOException $e) {
         throw new DatabaseException('database error: ' . $e->getMessage());
         return false;
     }
     return $rows;
 }
Example #5
0
 public function resultArray($object = NULL, $type = MYSQLI_ASSOC)
 {
     $rows = array();
     if (is_string($object)) {
         $fetch = $object;
     } elseif (is_bool($object)) {
         if ($object === true) {
             $fetch = 'fetch_object';
             // NOTE - The class set by $type must be defined before fetching the result,
             // autoloading is disabled to save a lot of stupid overhead.
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         } else {
             $fetch = 'fetch_array';
         }
     } else {
         // Use the default config values
         $fetch = $this->fetchType;
         if ($fetch == 'fetch_object') {
             $type = is_string($type) && AetherDatabaseConfig::autoLoad($type) ? $type : 'stdClass';
         }
     }
     if ($this->result->num_rows) {
         // Reset the pointer location to make sure things work properly
         $this->result->data_seek(0);
         while ($row = $this->result->{$fetch}($type)) {
             $rows[] = $row;
         }
     }
     return isset($rows) ? $rows : array();
 }