Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This class creates database connections using PDO as a backend.
- * Upon construction PDO::ATTR_ERRMODE is set to PDO::ERRMODE_EXCEPTION.
- * Upon connection failure, PDOException is thrown and execution is stopped.
- * @param string $dsn
- * @param string $user
- * @param string $pass
- * @param array $options [optional]
- * @version v 1.0 2012/05/05 09:00:00
- * @author Dustin David Novo
- */
- class PDOManager {
- /** Member Variables */
- /**
- * PDO database connection handle.
- * @var PDO
- */
- private $connection;
- /**
- * PDOStatement handle
- * @var PDOStatement
- */
- private $sth;
- /**
- * Holds the column count of the most recent query.
- * @var int
- */
- private $columnCount;
- /**
- * Holds the insert ID of the last inserted object.
- * @var int
- */
- private $lastInsertId;
- /**
- * Holds the number of affected rows from the last query.
- * @var int
- */
- private $affectedRowCount;
- /**
- * Holds the most recent database query.
- * @var string
- */
- private $lastQuery;
- /** Methods */
- /**
- * Constructor calls PDOManager::openConnection()
- * @see PDOManager::openConnection()
- * @param string $dsn - Host and database to connect to.
- * @param string $user - Username to connect with database. [optional]
- * @param string $pass - Password to connect with database. [optional]
- * @param array $options - Driver options to use when connecting. [optional]
- */
- function __construct($dsn, $user = NULL, $pass = NULL, $options = NULL) {
- $this->openConnection($dsn, $user, $pass, $options);
- }
- /**
- * Creates new PDO and assigns it to $this->connection.
- * Sets PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION.
- * Upon connection failure PDOException is thrown and
- * execution is halted.
- *
- * This function uses references passed by the constructor.
- * @param string $dsn - Host and database to connect to.
- * @param string $user - Username to connect with database.
- * @param string $pass - Password to connect with database.
- * @param array $options - Driver options to use when connecting. [optional]
- */
- public function openConnection(&$dsn, &$user, &$pass, &$options = NULL) {
- try {
- $this->connection = new PDO($dsn, $user, $pass, $options);
- $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch (PDOException $exc) {
- echo "Connecting to database failed: " . $exc->getMessage();
- }
- }
- /**
- * Closes connection by setting $this->connection = NULL.
- * Returns true if connection has been destroyed.
- * Returns false if there was no connection to destroy.
- * @return boolean
- */
- public function closeConnection() {
- if (isset($this->connection)) {
- $this->connection = NULL;
- return TRUE;
- }
- else {
- return FALSE;
- }
- }
- /**
- * Queries the current connection and returns mixed results
- * depending on $fetch_style used. If only $boundParams is to be used
- * simply pass NULL for $fetch_style and $fetch_class parameters.
- *
- * Function also sets the lastInsertId, affectedRowCount, columnCount,
- * and lastQuery variable values for use if needed.
- *
- * @param string $sql - The SQL query to execute.
- * @param int $fetch_style - The PDO::FETCH_* attribute to use. [optional]
- * @param string $fetch_class - Used if PDO::FETCH_CLASS is chosen. [optional]
- * @param array $boundParams - Array of parameters to bind. [optional]
- * @return mixed <br/>If no $fetch_style is defined, function will return
- * the affected row count as INT.
- *
- * If a $fetch_style is defined the function will return
- * an array of all results (fetchAll) using the given $fetch_style.
- *
- * NOTE: If PDO::FETCH_CLASS is chosen function will expect $fetch_class
- * to exist as well. However, if $fetch_class is left NULL the function
- * will still return results as if PDO::FETCH_OBJ was the defined
- * $fetch_style.
- * @throws PDOException
- */
- public function query($sql, $fetch_style = NULL, $fetch_class = NULL, $boundParams = NULL) {
- $this->lastQuery = $sql;
- $waiting = true;
- while ($waiting) {
- try {
- $this->sth = $this->connection->prepare($sql);
- $this->sth->closeCursor();
- $this->connection->beginTransaction();
- if ($boundParams == NULL) {
- $this->sth->execute();
- }
- else {
- $this->sth->execute($boundParams);
- }
- $this->lastInsertId = $this->connection->lastInsertId();
- $this->columnCount = $this->sth->columnCount();
- $this->affectedRowCount = $this->sth->rowCount();
- sleep(1);
- $this->connection->commit();
- $waiting = false;
- if ($fetch_style !== NULL) {
- if ($fetch_style === PDO::FETCH_CLASS && is_string($fetch_class) && $fetch_class != "") {
- return $this->sth->fetchAll($fetch_style, $fetch_class);
- }
- else {
- return $this->sth->fetchAll($fetch_style);
- }
- }
- else {
- return $this->getAffectedRowCount();
- }
- }
- catch (PDOException $exc) {
- if (stripos($exc->getMessage(), 'DATABASE IS LOCKED') !== FALSE) {
- // commit the transaction after waiting
- // also make it sleep for a bit
- usleep(500000);
- //$this->connection->commit();
- }
- else {
- $this->connection->rollBack();
- throw $exc;
- }
- }
- }
- }
- /**
- * Returns the last inserted row's id by calling PDO::lastInsertId().
- *
- * BEWARE: When using transactions with certain
- * databases, be sure to call this function BEFORE committing.
- * If called after the commit(), the function will simply return 0
- * rather than the insert id.
- * @see PDO::lastInsertId()
- * @return int
- */
- public function getLastInsertId() {
- return $this->lastInsertId;
- }
- /**
- * Returns the number of rows affected by the most recent query
- * by calling PDOStatement::rowCount().
- *
- * BEWARE: When using transaction with certain
- * databases, be sure to call this function BEFORE committing.
- * @see PDOStatement::rowCount()
- * @return int
- */
- public function getAffectedRowCount() {
- return $this->affectedRowCount;
- }
- /**
- * Returns the number of columns in the last result set by calling
- * PDOStatement::columnCount().
- * @return int
- */
- public function getColumnCount() {
- return $this->columnCount;
- }
- /**
- * Returns the last query ran whether it succeeded or not.
- * @return string
- */
- public function getLastQuery() {
- return $this->lastQuery;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment