/*
Description:
-> Base general purpose PDO MySQL database connection instantiation class.
-> contains set of common generic database interaction methods.
-> helper modules can be created for simplifying use of its generic methods,
and "plugged in" using the load_helper_module() method.
*/
class db_connection
{
/* property: connection object instance */
private $connection;
/* properties: connection variables */
private $dbhost; // database host.
private $dbname; // database name.
private $dbuser; // database username.
private $dbpass; // database password.
/* property: HM -> stores array of helper module objects */
public $HM;
public function __construct()
{
/* set connection properties */
$this->dbhost = DB_HOST;
$this->dbname = DB_NAME;
$this->dbuser = DB_USER;
$this->dbpass = DB_PASS;
/* instantiate connection */
try
{
$this->connection = new PDO(
'mysql:host=' . $this->dbhost . ';dbname=' . $this->dbname,
$this->dbuser,
$this->dbpass
);
$this->connection->setAttribute(PDO::ATTR_PERSISTENT, true);
$this->connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
exit;
}
/* prep $this->HM for storing helper modules */
$this->HM = array();
}
public function __destruct()
{
$this->HM = null;
$this->connection = null;
}
// method: register_helper()
// -> creates a new helper module object with a preferred specified name.
// -> $name is the desired name of the helper, $helper is the name of helper class.
// -> helper modules contain "wrapper" methods to ease working with the generic
// methods defined here (db_connection)
public function register_helper($name, $helper)
{
if(!isset($this->HM[$name]))
{
$this->HM[$name] = new $helper($this);
}
}
/**********************************/
/* TABLE CREATION and REPORTING */
/**********************************/
public function construct_table($table, $fields, $primary)
{
$sql = 'CREATE TABLE IF NOT EXISTS `' . $table . '` ( ';
for($i = 0; $i < count($fields); $i++)
{
$sql .= '`' . $fields[$i][0] . '` ' . $fields[$i][1] . ', ';
}
$sql .= 'PRIMARY KEY (`' . $primary . '`) ';
$sql .= ' )';
$sql .= 'ENGINE=InnoDB CHARACTER SET=utf8';
try
{
$this->connection->exec($sql);
}
catch(PDOException $e)
{
jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
}
}
public function report_table($table)
{
$tables_array = array();
try
{ // get table names and push to $tables_array
$tablequery = $this->connection->query("
SELECT
TABLE_NAME
FROM
information_schema.TABLES
WHERE
TABLE_TYPE = 'BASE TABLE'
AND
TABLE_SCHEMA = '$this->dbname'
");
while($fetch = $tablequery->fetch())
{
array_push($tables_array, $fetch['TABLE_NAME']);
}
}
catch(PDOException $e)
{
jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
}
if(in_array($table, $tables_array))
{
return true;
}
return false;
}
public function drop_table($table)
{
if($this->report_table($table)){
try
{
$this->connection->exec("DROP TABLE `$table`");
}
catch(PDOException $e)
{
jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
}
}
}
/********************/
/* SELECT METHODS */
/********************/
// method: get_row_count -> returns the number of rows based on primary key entries.
public function fetch_row_count($table, $key)
{
if($this->report_table($table)){
try{
$statement = $this->connection->query("
SELECT COUNT(`$key`)
AS `num`
FROM `$table`
");
$fetch = $statement->fetch();
return $fetch['num'];
}
catch(PDOException $e){
jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
return null;
}
}
return null;
}
public function check_duplicate($table, $collumn, $item)
{
try
{
$statement = $this->connection->prepare("
SELECT COUNT(`$collumn`)
AS `dup`
FROM `$table`
WHERE `$collumn` = :item
");
$statement->bindParam(':item', $item);
$statement->execute();
$fetch = $statement->fetch();
if($fetch['dup'] > 0)
{ // if we have a find..
return true;
}
return false;
}
catch(PDOException $e)
{
jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
return null;
}
}
// public method: fetch_items() -> [ table name | items to get | [ from where | equals this ] ]
public function fetch_items($table, $items, $where, $values)
{
$sql = 'SELECT ';
// where are we selecting from?
if(is_array($items))
{ // are we dealing with an array?
for($i = 0; $i < count($items); $i++)
{
if($i !== (count($items) - 1))
{
$sql .= '`' . $items[$i] . '`, ';
}
else
{
$sql .= '`' . $items[$i] . '` ';
}
}
}
else
{ // if not an array
$sql .= '`' . $items . '` ';
}
// from which table?
$sql .= 'FROM `' . $table . '` ';
// where?
if(is_array($where))
{
for($i = 0; $i < count($where); $i++)
{
if($i == 0)
{
$sql .= 'WHERE `' . $where[$i] . '` = :item' . $i . ' ';
}
else
{
$sql .= 'AND `' . $where[$i] . '` = :item' . $i . ' ';
}
}
}
else
{
$sql .= 'WHERE `' . $where . '` = :item ';
}
try
{
$statement = $this->connection->prepare($sql);
if(is_array($where))
{
for($i = 0; $i < count($where); $i++)
{
$statement->bindParam(':item' . $i, $values[$i]);
}
}
else
{
$statement->bindParam(':item', $values);
}
$statement->execute();
$fetch = $statement->fetch();
if(is_array($fetch))
{
return $fetch;
}
return null;
}
catch(PDOException $e)
{
jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
}
}
/********************/
/* UPDATE METHODS */
/********************/
public function update_timestamp($table, $update, $where, $values)
{
$sql = 'UPDATE `' . $table . '` ';
$sql .= 'SET `' . $update . '` = NOW() ';
$sql .= 'WHERE `' . $where . '` = :item ';
try
{
$statement = $this->connection->prepare($sql);
$statement->bindParam(':item', $values);
$statement->execute();
}
catch(PDOException $e)
{
jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
}
}
public function update_fields($table, $updatecols, $updatevals, $wherecols, $wherevals)
{
$sql = 'UPDATE `' . $table . '` SET ';
if(is_array($updatecols))
{
for($i = 0; $i < count($updatecols); $i++)
{
if($i !== (count($updatecols) - 1))
{
$sql .= '`' . $updatecols[$i] . '` = :update' . $i . ', ';
}
else
{
$sql .= '`' . $updatecols[$i] . '` = :update' . $i . ' ';
}
}
}
else
{
$sql .= '`' . $updatecols . '` = :update ';
}
if(is_array($wherecols))
{
for($i = 0; $i < count($wherecols); $i++)
{
if($i == 0)
{
$sql .= 'WHERE `' . $wherecols[$i] . '` = :where' . $i . ' ';
}
else
{
$sql .= 'AND `' . $wherecols[$i] . '` = :where' . $i . ' ';
}
}
}
else
{
$sql .= 'WHERE `' . $wherecols . '` = :where ';
}
try
{
$statement = $this->connection->prepare($sql);
if(is_array($updatecols))
{
for($i = 0; $i < count($updatecols); $i++)
{
$statement->bindParam(':update' . $i, $updatevals[$i]);
}
}
else
{
$statement->bindParam(':update', $updatevals);
}
if(is_array($wherecols))
{
for($i = 0; $i < count($wherecols); $i++)
{
$statement->bindParam(':where' . $i, $wherevals[$i]);
}
}
else
{
$statement->bindParam(':where', $wherevals);
}
$statement->execute();
}
catch(PDOException $e)
{
jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
}
}
/********************/
/* INSERT METHODS */
/********************/
public function insert_into_table($table, $collumns, $values)
{
$sql = 'INSERT INTO `' . $table . '` (';
for($i = 0; $i < count($collumns); $i++)
{
if($i !== (count($collumns) - 1))
{
$sql .= '`' . $collumns[$i] . '`, ';
}
else
{
$sql .= '`' . $collumns[$i] . '`';
}
}
$sql .= ') VALUES (';
for($i = 0; $i < count($values); $i++)
{
if($i !== (count($values) - 1))
{
$sql .= ':item' . $i . ', ';
}
else
{
$sql .= ':item' . $i;
}
}
$sql .= ')';
try
{
$statement = $this->connection->prepare($sql);
for($i = 0; $i < count($values); $i++)
{
$statement->bindParam(':item' . $i, $values[$i]);
}
$statement->execute();
}
catch(PDOException $e)
{
jerk_debug('Error In: ' . __METHOD__ , $e->getMessage());
return false;
}
return true;
}
}