diff --git a/autoload.php b/autoload.php index 5d8f021..9465614 100644 --- a/autoload.php +++ b/autoload.php @@ -26,9 +26,7 @@ function do_include($path) } // Load base classes -require_once(BASE_PATH.'db_pdo.php'); -require_once(BASE_PATH.'db_sql.php'); -require_once(BASE_PATH.'query_builder.php'); +array_map('do_include', glob(BASE_PATH.'classes/*.php')); // Load PDO Drivers foreach(pdo_drivers() as $d) diff --git a/db_pdo.php b/classes/db_pdo.php similarity index 100% rename from db_pdo.php rename to classes/db_pdo.php diff --git a/classes/db_reg.php b/classes/db_reg.php new file mode 100644 index 0000000..8456370 --- /dev/null +++ b/classes/db_reg.php @@ -0,0 +1,83 @@ +get_db($key); + + // Set the current key in the registry + self::$instance[$key] = new Query_Builder($db_params); + } + + // -------------------------------------------------------------------------- + + /** + * Return exiting connections + * + * @return array + */ + public static function get_connections() + { + return array_keys(self::$instance); + } + + // -------------------------------------------------------------------------- + + /** + * Remove a database connection + * + * @param string $key + * @return void + */ + public static function remove_db($key) + { + unset(self::$instance[$key]); + } +} +// End of dbreg.php \ No newline at end of file diff --git a/db_sql.php b/classes/db_sql.php similarity index 100% rename from db_sql.php rename to classes/db_sql.php diff --git a/query_builder.php b/classes/query_builder.php similarity index 98% rename from query_builder.php rename to classes/query_builder.php index 1e19d2c..3b8ed3a 100644 --- a/query_builder.php +++ b/classes/query_builder.php @@ -53,6 +53,9 @@ class Query_Builder { // ) private $query_map; + // Convenience property for connection management + public $conn_name = ""; + /** * Constructor * @@ -72,6 +75,11 @@ class Query_Builder { $params = $p; } + + // Let the connection work with 'conn_db' or 'database' + $params->conn_db = ( ! isset($params->database)) + ? @$params->conn_db + : @$params->database; $params->type = strtolower($params->type); $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; @@ -80,7 +88,7 @@ class Query_Builder { switch($dbtype) { default: - $dsn = "dbname={$params->database}"; + $dsn = "dbname={$params->conn_db}"; if ( ! empty($params->host)) { @@ -91,6 +99,7 @@ class Query_Builder { { $dsn .= ";port={$params->port}"; } + break; case "sqlite": @@ -102,6 +111,9 @@ class Query_Builder { break; } + // Set the charset + //$dsn .= ";charset=utf-8"; + // Create the database connection if ( ! empty($params->user)) { @@ -112,6 +124,12 @@ class Query_Builder { $this->db = new $dbtype($dsn); } + if (isset($params->name)) + { + $this->conn_name = $params->name; + } + + // Make things just slightly shorter $this->sql =& $this->db->sql; } diff --git a/drivers/firebird/firebird_driver.php b/drivers/firebird/firebird_driver.php index e454fff..f46899a 100644 --- a/drivers/firebird/firebird_driver.php +++ b/drivers/firebird/firebird_driver.php @@ -45,17 +45,6 @@ class firebird extends DB_PDO { // -------------------------------------------------------------------------- - /** - * Close the link to the database and any existing results - */ - public function __destruct() - { - @fbird_close(); - @fbird_free_result($this->statement); - } - - // -------------------------------------------------------------------------- - /** * Doesn't apply to Firebird */ diff --git a/drivers/firebird/firebird_result.php b/drivers/firebird/firebird_result.php index fe6994b..3d08fa4 100644 --- a/drivers/firebird/firebird_result.php +++ b/drivers/firebird/firebird_result.php @@ -85,6 +85,20 @@ class Firebird_Result { return $all; } + + // -------------------------------------------------------------------------- + + /** + * Emulate PDOStatement::fetchColumn + * + * @param int $colum_num + * @return mixed + */ + public function fetchColumn($column_num=0) + { + $row = $this->fetch(PDO::FETCH_NUM); + return $row[$column_num]; + } // -------------------------------------------------------------------------- diff --git a/drivers/firebird/firebird_sql.php b/drivers/firebird/firebird_sql.php index e632f17..66491a1 100644 --- a/drivers/firebird/firebird_sql.php +++ b/drivers/firebird/firebird_sql.php @@ -232,10 +232,9 @@ class Firebird_SQL extends DB_SQL { public function table_list() { return << "SET NAMES UTF-8 COLLATE 'UTF-8'", + )); + parent::__construct("mysql:$dsn", $username, $password, $options); $class = __CLASS__.'_sql'; diff --git a/drivers/pgsql/pgsql_sql.php b/drivers/pgsql/pgsql_sql.php index 4553c9c..79becfd 100644 --- a/drivers/pgsql/pgsql_sql.php +++ b/drivers/pgsql/pgsql_sql.php @@ -277,6 +277,29 @@ SQL; ORDER BY "relname" ASC SQL; } - + + // -------------------------------------------------------------------------- + + /** + * Return sql to list columns of the specified table + * + * @param string $table + * @return string + */ + public function column_list($table) + { + return <<