/** * 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'); }
/** * Makes a singular word plural. * * @param string $str word to pluralize * @param int $count * @return string */ public static function plural($str, $count = NULL) { // Remove garbage $str = strtolower(trim($str)); if (is_string($count)) { // Convert to integer when using a digit string $count = (int) $count; } // Do nothing with singular if ($count === 1) { return $str; } // Cache key name $key = 'plural_' . $str . $count; if (isset(self::$cache[$key])) { return self::$cache[$key]; } if (self::uncountable($str)) { return self::$cache[$key] = $str; } if (empty(self::$irregular)) { // Cache irregular words self::$irregular = AetherDatabaseConfig::retrieve('inflector.irregular'); } if (isset(self::$irregular[$str])) { $str = self::$irregular[$str]; } elseif (preg_match('/[sxz]$/', $str) || preg_match('/[^aeioudgkprt]h$/', $str)) { $str .= 'es'; } elseif (preg_match('/[^aeiou]y$/', $str)) { // Change "y" to "ies" $str = substr_replace($str, 'ies', -1); } else { $str .= 's'; } // Set the cache and return return self::$cache[$key] = $str; }
/** * Fetches SQL type information about a field, in a generic format. * * @param string $str field datatype * @return array */ protected function sqlType($str) { static $sqlTypes; if ($sqlTypes === NULL) { // Load SQL data types $sqlTypes = AetherDatabaseConfig::retrieve('sql_types'); } $str = strtolower(trim($str)); if (($open = strpos($str, '(')) !== false) { // Find closing bracket $close = strpos($str, ')', $open) - 1; // Find the type without the size $type = substr($str, 0, $open); } else { // No length $type = $str; } if (empty($sqlTypes[$type])) { exit('Unknown field type: ' . $type . '. ' . 'Please report this to: drift@hardware.no!'); } // Fetch the field definition $field = $sqlTypes[$type]; switch ($field['type']) { case 'string': case 'float': if (isset($close)) { // Add the length to the field info $field['length'] = substr($str, $open + 1, $close - $open); } break; case 'int': // Add unsigned value $field['unsigned'] = strpos($str, 'unsigned') !== false; break; } return $field; }