Skip to content

Instantly share code, notes, and snippets.

@dgellow
Last active December 12, 2015 04:28
Show Gist options
  • Save dgellow/4714007 to your computer and use it in GitHub Desktop.
Save dgellow/4714007 to your computer and use it in GitHub Desktop.
QueryEngine - Module 151
<?php
/**
* Created by JetBrains PhpStorm.
* User: Samuel El-Borai, Thomas Jolain, Etienne Ischer
* Date: 21/01/13
* Time: 09:37.
*/
require_once("Query.php");
require_once("Database.php");
class QueryEngine
{
private $statement;
private $tempKeyArray;
private $tempValueArray;
public function read(Query $query, $returnNull = false) {
$this->tempKeyArray = array();
$this->tempValueArray = array();
try {
$connection = $this->createConnection($query->database);
$this->statement = $connection->prepare($query->SQL);
$this->bindParamValue($query);
if ($this->statement->execute()) {
if (!$returnNull) {
$query->resultArray = $this->statement->fetchAll();
}
} else
{
throw new PDOException("Error during query execution. ");
}
$this->statement->closeCursor();
unset($this->statement);
unset($this->tempKeyArray);
unset($this->tempValueArray);
} catch (PDOException $e)
{
handleException("Error during query processing.", $e);
var_dump($e);
die();
}
}
private function createConnection($database) {
try {
if ($database === Database::Access) {
$connectionString = "odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Program Files (x86)\\UwAmp\\www\\sql\\UserDatabase.mdb";
$username = "estore";
$password = "";
} else if ($database === Database::MySQL) {
$connectionString = "mysql:dbname=bd_articles;host=microlulz.ch;port=3306";
$username = "estore";
$password = "";
} else {
throw new Exception("query->Database isn't correctly set");
}
$pdo = new PDO($connectionString, $username, $password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e) {
handleException("Error during database connection creation.", $e);
die();
}
return $pdo;
}
private function bindParamValue($query) {
try {
if (isset($query->dataArray) && count($query->dataArray) > 0) {
$count = 0;
foreach ($query->dataArray as $k => $v) {
array_push($this->tempKeyArray, $k);
array_push($this->tempValueArray, $v);
$this->statement->bindParam($this->tempKeyArray[$count], $this->tempValueArray[$count], $query->typeArray[$k]);
$count++;
}
}
} catch (Exception $e) {
handleException("Error during param binding.", $e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment