Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Created : 2007-08-31 - Art-Coding - Corentin Larose <dev@art-coding.fr>
- // Updated : 2008-03-10 - Art-Coding - Corentin Larose <dev@art-coding.fr>
- /**
- * @package AcPDO
- * @author Art-Coding - Corentin Larose <dev@art-coding.fr>
- */
- class AcPDO extends PDO {
- // Constants ===================================================================================
- // =============================================================================================
- // Public properties ===========================================================================
- public $mLastQueryTimer;
- public $mGlobalTimer;
- public $mRawQueries = array();
- public $mRawSuccessQueries = array();
- public $mQueries = array();
- public $mSuccessQueries = array();
- public $mNumQueries;
- public $mDebug = false;
- // =============================================================================================
- // Protected properties ========================================================================
- // =============================================================================================
- // Private properties ==========================================================================
- private static $mSingleton = null;
- // =============================================================================================
- // Public methods ==============================================================================
- /**
- * Execute an SQL statement and return the number of affected rows
- *
- * @param string $sql The SQL statement to prepare and execute.
- * @return int
- */
- public function exec($sql) {
- if ($this->mDebug) {
- $this->mNumQueries++;
- $this->mQueries[] = $sql;
- $start = microtime(true);
- $ret = parent::exec($sql);
- $end = microtime(true);
- $this->mLastQueryTimer = $end - $start;
- $this->mGlobalTimer += $end - $start;
- $this->mSuccessQueries[] = '# ' . $this->mLastQueryTimer . "\n" . $sql;
- return $ret;
- } // if
- return parent::exec($sql);
- } // method
- /**
- * Executes an SQL statement, returning a result set as a PDOStatement object
- * Although this function is only documented as having a single parameter, you may pass additional arguments to this function.
- * They will be treated as though you called PDOStatement->setFetchMode() on the resultant statement object.
- *
- * @param string $sql The SQL statement to prepare and execute.
- * @param int $arg1
- * @param mixed $arg2
- * @param array $arg3
- * @return PDOStatement
- */
- public function query($sql, $arg1 = null, $arg2 = null, $arg3 = null) {
- if ($this->mDebug) {
- $this->mNumQueries++;
- $this->mQueries[] = $sql;
- $start = microtime(true);
- $ret = parent::query($sql, $arg1, $arg2, $arg3);
- $end = microtime(true);
- $this->mLastQueryTimer = $end - $start;
- $this->mGlobalTimer += $end - $start;
- $this->mSuccessQueries[] = '# ' . $this->mLastQueryTimer . "\n" . $sql;
- return $ret;
- } // if
- return parent::query($sql, $arg1, $arg2, $arg3);
- } // method
- /**
- * Enable debugging / logging of queries.
- *
- * @param boolean $state True=enabled, false=disabled.
- * @return void
- */
- public function ToggleDebug($state = true) {
- $this->mDebug = $state;
- } // method
- /**
- * Returns total time used for sql queries execution.
- *
- * @return float
- */
- public function GetGlobalTimer() {
- return $this->mGlobalTimer;
- } // method
- /**
- * Returns time used for last sql query execution.
- *
- * @return float
- */
- public function GetLastQueryTimer() {
- return $this->mLastQueryTimer;
- } // method
- /**
- * Returns time used for last sql query execution.
- *
- * @return float
- */
- public function GetNumQueries() {
- return $this->mNumQueries;
- } // method
- /**
- * Returns raw queries.
- *
- * @return float
- */
- public function GetRawQueries() {
- return $this->mRawQueries;
- } // method
- /**
- * Returns raw success queries.
- *
- * @return float
- */
- public function GetRawSuccessQueries() {
- return $this->mRawSuccessQueries;
- } // method
- /**
- * Returns queries.
- *
- * @return float
- */
- public function GetQueries() {
- return $this->mQueries;
- } // method
- /**
- * Returns success queries.
- *
- * @return float
- */
- public function GetSuccessQueries() {
- return $this->mSuccessQueries;
- } // method
- /**
- * Singleton function Creates once an extended PDO instance representing a connection to a database.
- *
- * @param string $dsn The Data Source Name, or DSN, contains the information required to connect to the database.
- * @param string $username The user name for the DSN string. This parameter is optional for some PDO drivers.
- * @param string $password The password for the DSN string. This parameter is optional for some PDO drivers.
- * @param array $driverOptions A key=>value array of driver-specific connection options.
- * @return AcPDO
- */
- public static function GetInstance($dsn = null, $username = null, $password = null, $driverOptions = null) {
- if (is_null(self::$mSingleton)) {
- self::$mSingleton = new AcPDO($dsn, $username, $password, $driverOptions);
- } // if
- return self::$mSingleton;
- } // method
- /**
- * Singleton function Creates once an extended PDO instance representing a connection to a database.
- *
- * @param string $dsn The Data Source Name, or DSN, contains the information required to connect to the database.
- * @param string $username The user name for the DSN string. This parameter is optional for some PDO drivers.
- * @param string $password The password for the DSN string. This parameter is optional for some PDO drivers.
- * @param array $driverOptions A key=>value array of driver-specific connection options.
- * @return AcPDO
- */
- public static function Singleton($dsn = null, $username = null, $password = null, $driverOptions = null) {
- return self::GetInstance($dsn, $username, $password, $driverOptions);
- } // method
- // =============================================================================================
- // Protected methods ===========================================================================
- // =============================================================================================
- // Private methods =============================================================================
- // =============================================================================================
- // Magic methods ===============================================================================
- /**
- * Constructor Creates an extended PDO instance representing a connection to a database.
- *
- * @param string $dsn
- * @param string $username
- * @param string $password
- * @param array $driverOptions
- * @return void
- */
- public function __construct($dsn, $username = null, $password = null, $driverOptions = null) {
- parent::__construct($dsn, $username, $password, $driverOptions);
- $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('AcPDOStatement', array($this)));
- } // method
- // =============================================================================================
- } // class
Add Comment
Please, Sign In to add comment