private function __construct() { //for Oracle $tns = "\r\n\t (DESCRIPTION =\r\n\t (ADDRESS_LIST = (\r\n\t ADDRESS =\r\n\t (PROTOCOL = TCP)\r\n\t (HOST = " . Constants::$databaseHost . ")\r\n\t (PORT = 1521)\r\n\t )\r\n\t )\r\n\t (CONNECT_DATA = (\r\n\t SERVICE_NAME = " . Constants::$databaseName . ")\r\n\t )\r\n\t )"; try { switch (Constants::$databaseDriver) { case "SqlLite": $connectionTyp = "sqlite:" . $_SERVER["DOCUMENT_ROOT"] . "/db/database.db"; break; case "MySql": $connectionTyp = "mysql:host=" . Constants::$databaseHost . ";dbname=" . Constants::$databaseName . ";charset=utf8"; break; case "MSSql": $connectionTyp = "sqlsrv:Server=" . Constants::$databaseHost . ";Database=" . Constants::$databaseName . ";charset=utf8"; break; case "PgSql": $connectionTyp = "pgsql:host=" . Constants::$databaseHost . ";dbname=" . Constants::$databaseName . ";charset=utf8"; break; case "Oracle": $connectionTyp = "oci:dbname=" . $this->{$tns}; break; default: echo "Unsuportted DB Driver! Check the configuration in Constants.php"; exit(1); } self::$connection = new PDO($connectionTyp, Constants::$databaseUser, Constants::$databasePass, $this->SQLoverSSL()); self::$connection->setAttribute = array(PDO::ATTR_PERSISTENT => ture, PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); die; } }
public function GenericEntityManager(GenericEntity $entity) { $this->Updater = new Updater(); if ($this->Updater->ExistEntry($entity)) { $this->entityToManage = $entity; } else { echo "entity not found."; die; } $this->PDO = ConnectionProvider::getConnection(); $this->findAll .= Constants::$databaseName . "." . $entity->getTablename(); $this->findById .= Constants::$databaseName . "." . $entity->getTablename() . " where " . $this->entityToManage->getIdcolumn() . " = '"; $this->lastId .= Constants::$databaseName . "." . $entity->getTablename(); $this->countAll .= Constants::$databaseName . "." . $entity->getTablename(); }
public static function getConnection() { $hostname = "localhost"; $user = "******"; $password = ""; $database = "user_data"; if (self::$connection == null) { try { self::$connection = new PDO('mysql:host=' . $hostname . ';dbname=' . $database, $user, $password); } catch (Exception $ex) { print $ex; } } return self::$connection; }
public function Updater() { $this->PDO = ConnectionProvider::getConnection(); $this->GenericEntityManager = new GenericEntityManager(); }
public function readAll() { $sql = "select * from comment where post_id='" . $_REQUEST['post_id'] . "' order by created desc;"; $comments = array(); foreach (ConnectionProvider::getConnection()->query($sql) as $row) { $comment = new Comment(); $comment->id = $row['id']; $comment->text = $row['text']; $comment->post_id = $row['post_id']; $comment->created = $row['created']; $comment->author_name = $row['author_name']; $comments[] = $comment; } return $comments; }