Skip to content

Commit

Permalink
Split off db_util classes for each driver
Browse files Browse the repository at this point in the history
  • Loading branch information
timw4mail committed Apr 18, 2012
1 parent c12fcd9 commit 12f53c5
Show file tree
Hide file tree
Showing 23 changed files with 802 additions and 634 deletions.
16 changes: 15 additions & 1 deletion classes/db_pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@
*/
abstract class DB_PDO extends PDO {

public $manip;
// Reference to last query
protected $statement;

// Character to escape identifiers
protected $escape_char = '"';

// References to sub-classes
public $sql,
$util;

/**
* PDO constructor wrapper
Expand All @@ -36,6 +42,14 @@ abstract class DB_PDO extends PDO {
public function __construct($dsn, $username=NULL, $password=NULL, $driver_options=array())
{
parent::__construct($dsn, $username, $password, $driver_options);

// Load the sql class for the driver
$class = get_class($this)."_sql";
$this->sql = new $class();

// Load the util class for the driver
$class = get_class($this)."_util";
$this->util = new $class($this);

$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
Expand Down
37 changes: 0 additions & 37 deletions classes/db_sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,6 @@ public function sum()
// ! Abstract Methods
// --------------------------------------------------------------------------

/**
* Get database-specific sql to create a new table
*
* @abstract
* @param string $name
* @param array $columns
* @param array $constraints
* @param array $indexes
* @return string
*/
abstract public function create_table($name, $columns, array $constraints=array(), array $indexes=array());

/**
* Get database-specific sql to drop a table
*
* @abstract
* @param string $name
* @return string
*/
abstract public function delete_table($name);

/**
* Get database specific sql for limit clause
*
Expand All @@ -122,22 +101,6 @@ abstract public function limit($sql, $limit, $offset=FALSE);
* @return string
*/
abstract public function random();

/**
* Return an SQL file with the database table structure
*
* @abstract
* @return string
*/
abstract public function backup_structure();

/**
* Return an SQL file with the database data as insert statements
*
* @abstract
* @return string
*/
abstract public function backup_data();

/**
* Returns sql to list other databases
Expand Down
71 changes: 71 additions & 0 deletions classes/db_util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Query
*
* Free Query Builder / Database Abstraction Layer
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/Query
* @license http://philsturgeon.co.uk/code/dbad-license
*/

// --------------------------------------------------------------------------

/**
* Abstract class defining database / table creation methods
*/
abstract class DB_Util {

private $conn;

/**
* Save a reference to the connection object for later use
*/
public function __construct(&$conn)
{
$this->conn =& $conn;
}

// --------------------------------------------------------------------------
// ! Abstract Methods
// --------------------------------------------------------------------------

/**
* Get database-specific sql to create a new table
*
* @abstract
* @param string $name
* @param array $columns
* @param array $constraints
* @param array $indexes
* @return string
*/
abstract public function create_table($name, $columns, array $constraints=array(), array $indexes=array());

/**
* Get database-specific sql to drop a table
*
* @abstract
* @param string $name
* @return string
*/
abstract public function delete_table($name);

/**
* Return an SQL file with the database table structure
*
* @abstract
* @return string
*/
abstract public function backup_structure();

/**
* Return an SQL file with the database data as insert statements
*
* @abstract
* @return string
*/
abstract public function backup_data();

}
2 changes: 2 additions & 0 deletions classes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public static function &get_instance()
*/
private function __construct()
{
// For testing and use outside of OpenSQLManager,
// define a different SETTINGS_DIR
if ( ! defined('SETTINGS_DIR'))
{
define('SETTINGS_DIR', '.');
Expand Down
14 changes: 12 additions & 2 deletions drivers/firebird/firebird_driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,19 @@ public function __construct($dbpath, $user='sysdba', $pass='masterkey')
throw new PDOException(fbird_errmsg());
die();
}


// Load these classes here because this
// driver does not call the constructor
// of DB_PDO, which defines these two
// class variables for the other drivers

// Load the sql class
$class = __CLASS__."_sql";
$this->sql = new $class;
$this->sql = new $class();

// Load the util class
$class = __CLASS__."_util";
$this->util = new $class($this);
}

// --------------------------------------------------------------------------
Expand Down
157 changes: 1 addition & 156 deletions drivers/firebird/firebird_sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,79 +17,6 @@
*/
class Firebird_SQL extends DB_SQL {

/**
* Convienience public function to generate sql for creating a db table
*
* @param string $name
* @param array $fields
* @param array $constraints=array()
* @param array $indexes=array()
*
* @return string
*/
public function create_table($name, $fields, array $constraints=array(), array $indexes=array())
{
$column_array = array();

// Reorganize into an array indexed with column information
// Eg $column_array[$colname] = array(
// 'type' => ...,
// 'constraint' => ...,
// 'index' => ...,
// )
foreach($fields as $colname => $type)
{
if(is_numeric($colname))
{
$colname = $type;
}

$column_array[$colname] = array();
$column_array[$colname]['type'] = ($type !== $colname) ? $type : '';
}

if( ! empty($constraints))
{
foreach($constraints as $col => $const)
{
$column_array[$col]['constraint'] = $const;
}
}

// Join column definitons together
$columns = array();
foreach($column_array as $n => $props)
{
$str = '"'.$n.'" ';
$str .= (isset($props['type'])) ? "{$props['type']} " : "";
$str .= (isset($props['constraint'])) ? "{$props['constraint']} " : "";

$columns[] = $str;
}

// Generate the sql for the creation of the table
$sql = 'CREATE TABLE "'.$name.'" (';
$sql .= implode(',', $columns);
$sql .= ')';

return $sql;
}

// --------------------------------------------------------------------------

/**
* Drop the selected table
*
* @param string $name
* @return string
*/
public function delete_table($name)
{
return 'DROP TABLE "'.$name.'"';
}

// --------------------------------------------------------------------------

/**
* Limit clause
*
Expand Down Expand Up @@ -127,89 +54,7 @@ public function random()
return FALSE;
}

// --------------------------------------------------------------------------

/**
* Create an SQL backup file for the current database's structure
*
* @return string
*/
public function backup_structure()
{
// @todo Implement Backup structure function
return '';
}

// --------------------------------------------------------------------------

/**
* Create an SQL backup file for the current database's data
*
* @param array $exclude
* @param bool $system_tables
* @return string
*/
public function backup_data($exclude=array(), $system_tables=FALSE)
{
// Determine which tables to use
if($system_tables == TRUE)
{
$tables = array_merge($this->get_system_tables(), $this->get_tables());
}
else
{
$tables = $this->get_tables();
}

// Filter out the tables you don't want
if( ! empty($exclude))
{
$tables = array_diff($tables, $exclude);
}

$output_sql = '';

// Get the data for each object
foreach($tables as $t)
{
$sql = 'SELECT * FROM "'.trim($t).'"';
$res = $this->query($sql);
$obj_res = $this->fetchAll(PDO::FETCH_ASSOC);

unset($res);

// Nab the column names by getting the keys of the first row
$columns = @array_keys($obj_res[0]);

$insert_rows = array();

// Create the insert statements
foreach($obj_res as $row)
{
$row = array_values($row);

// Quote values as needed by type
if(stripos($t, 'RDB$') === FALSE)
{
$row = array_map(array(&$this, 'quote'), $row);
$row = array_map('trim', $row);
}

$row_string = 'INSERT INTO "'.trim($t).'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');';

unset($row);

$insert_rows[] = $row_string;
}

unset($obj_res);

$output_sql .= "\n\nSET TRANSACTION;\n".implode("\n", $insert_rows)."\nCOMMIT;";
}

return $output_sql;
}


// --------------------------------------------------------------------------

/**
Expand Down
Loading

0 comments on commit 12f53c5

Please sign in to comment.