Various DB changes

This commit is contained in:
Timothy Warren 2012-02-01 21:02:11 -05:00
parent a7de6f6e54
commit 23f5d7ef35
3 changed files with 126 additions and 3 deletions

117
src/databases/db_pdo.php Normal file
View File

@ -0,0 +1,117 @@
<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Base Database class
*
* Extends PDO to simplify cross-database issues
*/
class DB_PDO extends PDO {
protected $statement;
function __construct($dsn, $username=NULL, $password=NULL, $driver_options=array())
{
parent::__construct($dsn, $username, $password, $driver_options);
}
// -------------------------------------------------------------------------
/**
* Simplifies prepared statements for database queries
*
* @param string $sql
* @param array $data
* @return mixed PDOStatement / FALSE
*/
function prepare_query($sql, $data)
{
// Prepare the sql
$query = $this->prepare($sql);
if( ! is_like_array($query))
{
$this->get_last_error();
return FALSE;
}
// Set the statement in the class variable for easy later access
$this->statement =& $query;
if( ! is_like_array($data))
{
trigger_error("Invalid data argument");
return FALSE;
}
// Bind the parameters
foreach($data as $k => $value)
{
$res = $query->bindValue($k, $value);
if( ! $res)
{
trigger_error("Parameter not successfully bound");
return FALSE;
}
}
return $query;
}
// -------------------------------------------------------------------------
/**
* Retreives the data from a select query
*
* @param PDOStatement $statement
* @return array
*/
function get_query_data($statement)
{
// Execute the query
$statement->execute();
// Return the data array fetched
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
// -------------------------------------------------------------------------
/**
* Returns number of rows affected by an INSERT, UPDATE, DELETE type query
*
* @param PDOStatement $statement
* @return int
*/
function affected_rows($statement)
{
// Execute the query
$statement->execute();
// Return number of rows affected
return $statement->rowCount();
}
// -------------------------------------------------------------------------
abstract function create_database($name){}
}
// End of db_pdo.php

View File

@ -21,9 +21,12 @@ class MySQL extends DB_PDO {
function __construct($dsn, $username=null, $password=null, $options=array())
{
$options = array_merge(array(
),
$options);
parent::__construct("mysql:$dsn", $username, $password, $options);
}
/**
@ -33,7 +36,9 @@ class MySQL extends DB_PDO {
*/
function truncate($table)
{
$sql = "TRUNCATE `{$table}`";
$this->query($sql);
}
}

View File

@ -119,6 +119,7 @@ class Add_DB extends GtkWindow {
// Replace default capitalization with something that looks better.
$d = str_replace("sql", "SQL", $d);
$d = str_ireplace("pg", "Postgre", $d);
$d = str_ireplace("odbc", "ODBC", $d);
$d = ucfirst($d);
$drivers[] = $d;