Advertisement
Guest User

Untitled

a guest
Apr 11th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.84 KB | None | 0 0
  1. <?php
  2. /**
  3.  * <!--
  4.  * This file is part of the adventure php framework (APF) published under
  5.  * http://adventure-php-framework.org.
  6.  *
  7.  * The APF is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU Lesser General Public License as published
  9.  * by the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * The APF is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15.  * GNU Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public License
  18.  * along with the APF. If not, see http://www.gnu.org/licenses/lgpl-3.0.txt.
  19.  * -->
  20.  */
  21. import('core::database', 'AbstractDatabaseHandler');
  22. import('core::database', 'DatabaseHandlerException');
  23. /**
  24.  * @package core::database
  25.  * @class PDOHandler
  26.  *
  27.  * This class implements a connection handler for the ConnectionManager
  28.  * to use with pdo interface.
  29.  *
  30.  * @author Tobias Lückel (megger)
  31.  * @version
  32.  * Version 0.1, 11.04.2012<br />
  33.  */
  34. class PDOHandler extends AbstractDatabaseHandler {
  35.     /**
  36.      * @public
  37.      *
  38.      * @author Tobias Lückel (megger)
  39.      * @version
  40.      * Version 0.1, 11.04.2012<br />
  41.      */
  42.     public function __construct() {
  43.         $this->__dbLogFileName = 'pdo';
  44.     }
  45.  
  46.     /**
  47.      * @protected
  48.      *
  49.      * Provides internal service to open a database connection.
  50.      *
  51.      * @author Tobias Lückel (megger)
  52.      * @version
  53.      * Version 0.1, 11.04.2012<br />
  54.      */
  55.     protected function __connect()
  56.     {
  57.         // get dsn based on the configuration
  58.         $dsn = $this->getDSN();
  59.  
  60.         // log dsn if debugging is active
  61.         if ($this->__dbDebug === true) {
  62.             $this->__dbLog->logEntry($this->__dbLogFileName,
  63.                 '[PDOHandler::__connect()] Current DSN: ' . $dsn,
  64.                 'DEBUG');
  65.         }
  66.  
  67.         // connect to database
  68.         $this->__dbConn = new PDO($dsn, $this->__dbUser, $this->__dbPass);
  69.  
  70.         // switch errormode of PDO to exceptions
  71.         $this->__dbConn->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  72.  
  73.         // configure client connection
  74.         $this->initCharsetAndCollation();
  75.     }
  76.  
  77.     /**
  78.      * @protected
  79.      *
  80.      * Provides internal service to close a database connection.
  81.      *
  82.      * @author Tobias Lückel (megger)
  83.      * @version
  84.      * Version 0.1, 11.04.2012<br />
  85.      */
  86.     protected function __close()
  87.     {
  88.         $this->__dbConn = null;
  89.     }
  90.  
  91.     /**
  92.      * @public
  93.      *
  94.      * Turns off autocommit mode! Changes to the database via PDO are not
  95.      * committed until calling commit().
  96.      * rollBack() will roll back all changes and turns on the autocommit mode!
  97.      *
  98.      * @return boolean
  99.      *
  100.      * @author Tobias Lückel (megger)
  101.      * @version
  102.      * Version 0.1, 11.04.2012<br />
  103.      */
  104.     public function beginTransaction() {
  105.         return $this->__dbConn->beginTransaction();
  106.     }
  107.  
  108.     /**
  109.      * @public
  110.      *
  111.      * Commits a transaction and turns on the autocommit mode!
  112.      *
  113.      * @return boolean
  114.      *
  115.      * @author Tobias Lückel (megger)
  116.      * @version
  117.      * Version 0.1, 11.04.2012<br />
  118.      */
  119.     public function commit() {
  120.         return $this->__dbConn->commit();
  121.     }
  122.  
  123.     /**
  124.      * @public
  125.      *
  126.      * Rolls back the current transaction
  127.      *
  128.      * @return boolean
  129.      *
  130.      * @author Tobias Lückel (megger)
  131.      * @version
  132.      * Version 0.1, 11.04.2012<br />
  133.      */
  134.     public function rollBack() {
  135.         return $this->__dbConn->rollBack();
  136.     }
  137.  
  138.     /**
  139.      * @public
  140.      *
  141.      * Checks if a transaction is active
  142.      *
  143.      * @return boolean
  144.      *
  145.      * @author Tobias Lückel (megger)
  146.      * @version
  147.      * Version 0.1, 11.04.2012<br />
  148.      */
  149.     public function inTransaction() {
  150.         return $this->__dbConn->inTransaction();
  151.     }
  152.  
  153.     /**
  154.      * @public
  155.      *
  156.      * Prepares a statement for execution and returns a PDOStatement object
  157.      *
  158.      * @param $statement The statement string
  159.      * @return PDOStatement A PDOStatement object to work with
  160.      *
  161.      * @author Tobias Lückel (megger)
  162.      * @version
  163.      * Version 0.1, 11.04.2012<br />
  164.      */
  165.     public function prepareStatement($statement) {
  166.         return $this->__dbConn->prepare($statement);
  167.     }
  168.  
  169.     /**
  170.      * @public
  171.      *
  172.      * Executes a statement, located within a statement file. The place holders contained in the
  173.      * file are replaced by the given values.
  174.      *
  175.      * @param string $namespace Namespace of the statement file.
  176.      * @param string $statementName Name of the statement file (filebody!).
  177.      * @param string[] $params A list of statement parameters.
  178.      * @param bool $logStatement Indicates, if the statement is logged for debug purposes.
  179.      * @return PDOStatement A PDOStatement object to work with.
  180.      *
  181.      * @author Tobias Lückel (megger)
  182.      * @version
  183.      * Version 0.1, 11.04.2012<br />
  184.      */
  185.     public function executeStatement($namespace, $statementFile, array $params = array(), $logStatement = false)
  186.     {
  187.         // load statement file content
  188.         $statement = $this->getPreparedStatement($namespace, $statementFile, $params);
  189.  
  190.         // log statements in debug mode or when requested explicitly
  191.         if ($this->__dbDebug == true || $logStatement == true) {
  192.             $this->__dbLog->logEntry($this->__dbLogFileName,
  193.                 '[PDOHandler::executeStatement()] Current statement: ' . $statement,
  194.                 'DEBUG');
  195.         }
  196.  
  197.         // prepare statement for execution
  198.         $pdoStatement = $this->__dbConn->prepare($statement);
  199.  
  200.         // check if the statement was executed
  201.         if (!$pdoStatement) {
  202.             $errorInfo = $this->__dbConn->errorInfo();
  203.             $message = '(' . $errorInfo[0] . '/'.$errorInfo[1].') ' . $errorInfo[2] . ' (Statement: ' . $statement . ')';
  204.             $this->__dbLog->logEntry($this->__dbLogFileName, $message, 'ERROR');
  205.             throw new DatabaseHandlerException('[PDOHandler::executeStatement()] ' . $message);
  206.         }
  207.  
  208.         // track $__lastInsertID for further usage
  209.         $this->__lastInsertID = $this->__dbConn->lastInsertId();
  210.  
  211.         return $pdoStatement;
  212.     }
  213.  
  214.     /**
  215.      * @public
  216.      *
  217.      * Executes a statement applied as a string to the method and returns the
  218.      * result pointer.
  219.      *
  220.      * @param string $statement The statement string.
  221.      * @param boolean $logStatement Inidcates, whether the given statement should be
  222.      *                              logged for debug purposes.
  223.      * @return PDOStatement A PDOStatement object to work with.
  224.      *
  225.      * @author Tobias Lückel (megger)
  226.      * @version
  227.      * Version 0.1, 11.04.2012<br />
  228.      */
  229.     public function executeTextStatement($statement, $logStatement = false)
  230.     {
  231.         // log statements in debug mode or when requested explicitly
  232.         if ($this->__dbDebug == true || $logStatement == true) {
  233.             $this->__dbLog->logEntry($this->__dbLogFileName,
  234.                 '[PDOHandler::executeTextStatement()] Current statement: ' . $statement,
  235.                 'DEBUG');
  236.         }
  237.  
  238.         // prepare statement for execution
  239.         $pdoStatement = $this->__dbConn->prepare($statement);
  240.  
  241.         // check if statement was executed
  242.         if (!$pdoStatement) {
  243.             $errorInfo = $this->__dbConn->errorInfo();
  244.             $message = '(' . $errorInfo[0] . '/'.$errorInfo[1].') ' . $errorInfo[2] . ' (Statement: ' . $statement . ')';
  245.             $this->__dbLog->logEntry($this->__dbLogFileName, $message, 'ERROR');
  246.             throw new DatabaseHandlerException('[PDOHandler::executeStatement()] ' . $message);
  247.         }
  248.  
  249.         // track $__lastInsertID for further usage
  250.         $this->__lastInsertID = $this->__dbConn->lastInsertId();
  251.  
  252.         return $pdoStatement;
  253.     }
  254.  
  255.     /**
  256.      * @public
  257.      *
  258.      * Fetches a record from the database using the given PDOStatement.
  259.      *
  260.      * @param PDOStatement $pdoStatement The PDOStatement returned by executeStatement() or executeTextStatement().
  261.      * @param int $type The type the returned data should have. Use the static *_FETCH_MODE constants.
  262.      * @return string[] The associative result array. Returns false if no row was found.
  263.      *
  264.      * @author Tobias Lückel (megger)
  265.      * @version
  266.      * Version 0.1, 11.04.2012<br />
  267.      */
  268.     public function fetchData($pdoStatement, $type = self::ASSOC_FETCH_MODE)
  269.     {
  270.         $return = null;
  271.         switch ($type) {
  272.             case self::ASSOC_FETCH_MODE:
  273.                 $return = $pdoStatement->fetch(PDO::FETCH_ASSOC);
  274.                 break;
  275.             case self::OBJECT_FETCH_MODE:
  276.                 $return = $pdoStatement->fetch(PDO::FETCH_OBJ);
  277.                 break;
  278.             case self::NUMERIC_FETCH_MODE:
  279.                 $return = $pdoStatement->fetch(PDO::FETCH_NUM);
  280.                 break;
  281.         }
  282.         if ($return == null) {
  283.             return false;
  284.         }
  285.         return $return;
  286.     }
  287.  
  288.     /**
  289.      * @public
  290.      *
  291.      * Escapes given values to be SQL injection save.
  292.      *
  293.      * @param string $value The unescaped value.
  294.      * @return string The escapted string.
  295.      *
  296.      * @author Tobias Lückel (megger)
  297.      * @version
  298.      * Version 0.1, 11.04.2012<br />
  299.      */
  300.     public function escapeValue($value)
  301.     {
  302.         return $this->__dbConn->quote($value);
  303.     }
  304.  
  305.     /**
  306.      * @public
  307.      *
  308.      * Returns the amount of rows, that are affected by a previous update or delete call.
  309.      *
  310.      * @param PDOStatement $pdoStatement The PDOStatement.
  311.      * @return int The number of affected rows.
  312.      *
  313.      * @author Tobias Lückel (megger)
  314.      * @version
  315.      * Version 0.1, 11.04.2012<br />
  316.      */
  317.     public function getAffectedRows($pdoStatement)
  318.     {
  319.         return $pdoStatement->rowCount();
  320.     }
  321.  
  322.     /**
  323.      * @public
  324.      *
  325.      * Returns the number of selected rows by the given PDOStatement.
  326.      * Some databases may return the number of rows returned by a select statement.
  327.      * However, this behaviour is not guaranteed for all databases and
  328.      * should not be relied on for portable applications.
  329.      *
  330.      * @param PDOStatement $pdoStatement The PDOStatement.
  331.      * @return int The number of selected rows.
  332.      *
  333.      * @author Tobias Lückel (megger)
  334.      * @version
  335.      * Version 0.1, 11.04.2012<br />
  336.      */
  337.     public function getNumRows($pdoStatement)
  338.     {
  339.         return $pdoStatement->rowCount();
  340.     }
  341.  
  342.     /**
  343.      * @private
  344.      *
  345.      * Returns the data source name (DSN) for the database connection.
  346.      * The string is build bases on the configuration parameter 'db.PDO'
  347.      * Actual following db drivers are supported:
  348.      *  - mysql(i)
  349.      *
  350.      * @return string
  351.      * @author Tobias Lückel (megger)
  352.      * @version
  353.      * Version 0.1, 11.04.2012<br />
  354.      */
  355.     private function getDSN() {
  356.         $dsn = '';
  357.         switch (strtolower($this->__dbPDO)) {
  358.             case 'mysql':
  359.             case 'mysqli':
  360.                 if (isset($this->__dbSocket) && $this->__dbSocket != '') {
  361.                     $dsn = 'mysql:unix_socket='.$this->__dbSocket;
  362.                 } else {
  363.                     $dsn = 'mysql:host=';
  364.                     if (isset($this->__dbHost)) {
  365.                         $dsn .= $this->__dbHost;
  366.                     } else {
  367.                         $dsn .= 'localhost';
  368.                     }
  369.                     if (isset($this->__dbPort)) {
  370.                         $dsn .= ';port='.$this->__dbPort;
  371.                     }
  372.                 }
  373.                 $dsn .= ';dbname='.$this->__dbName;
  374.                 break;
  375.         }
  376.         return $dsn;
  377.     }
  378. }
  379.  
  380. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement