Guest User

Untitled

a guest
Jul 15th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. <?php
  2. // Created : 2007-08-31 - Art-Coding - Corentin Larose <dev@art-coding.fr>
  3. // Updated : 2008-03-10 - Art-Coding - Corentin Larose <dev@art-coding.fr>
  4. /**
  5. * @package AcPDO
  6. * @author Art-Coding - Corentin Larose <dev@art-coding.fr>
  7. */
  8. class AcPDO extends PDO {
  9. // Constants ===================================================================================
  10. // =============================================================================================
  11. // Public properties ===========================================================================
  12. public $mLastQueryTimer;
  13. public $mGlobalTimer;
  14. public $mRawQueries = array();
  15. public $mRawSuccessQueries = array();
  16. public $mQueries = array();
  17. public $mSuccessQueries = array();
  18. public $mNumQueries;
  19. public $mDebug = false;
  20. // =============================================================================================
  21. // Protected properties ========================================================================
  22. // =============================================================================================
  23. // Private properties ==========================================================================
  24. private static $mSingleton = null;
  25. // =============================================================================================
  26. // Public methods ==============================================================================
  27. /**
  28. * Execute an SQL statement and return the number of affected rows
  29. *
  30. * @param string $sql The SQL statement to prepare and execute.
  31. * @return int
  32. */
  33. public function exec($sql) {
  34. if ($this->mDebug) {
  35. $this->mNumQueries++;
  36. $this->mQueries[] = $sql;
  37. $start = microtime(true);
  38. $ret = parent::exec($sql);
  39. $end = microtime(true);
  40. $this->mLastQueryTimer = $end - $start;
  41. $this->mGlobalTimer += $end - $start;
  42. $this->mSuccessQueries[] = '# ' . $this->mLastQueryTimer . "\n" . $sql;
  43. return $ret;
  44. } // if
  45. return parent::exec($sql);
  46. } // method
  47. /**
  48. * Executes an SQL statement, returning a result set as a PDOStatement object
  49. * Although this function is only documented as having a single parameter, you may pass additional arguments to this function.
  50. * They will be treated as though you called PDOStatement->setFetchMode() on the resultant statement object.
  51. *
  52. * @param string $sql The SQL statement to prepare and execute.
  53. * @param int $arg1
  54. * @param mixed $arg2
  55. * @param array $arg3
  56. * @return PDOStatement
  57. */
  58. public function query($sql, $arg1 = null, $arg2 = null, $arg3 = null) {
  59. if ($this->mDebug) {
  60. $this->mNumQueries++;
  61. $this->mQueries[] = $sql;
  62. $start = microtime(true);
  63. $ret = parent::query($sql, $arg1, $arg2, $arg3);
  64. $end = microtime(true);
  65. $this->mLastQueryTimer = $end - $start;
  66. $this->mGlobalTimer += $end - $start;
  67. $this->mSuccessQueries[] = '# ' . $this->mLastQueryTimer . "\n" . $sql;
  68. return $ret;
  69. } // if
  70. return parent::query($sql, $arg1, $arg2, $arg3);
  71. } // method
  72. /**
  73. * Enable debugging / logging of queries.
  74. *
  75. * @param boolean $state True=enabled, false=disabled.
  76. * @return void
  77. */
  78. public function ToggleDebug($state = true) {
  79. $this->mDebug = $state;
  80. } // method
  81. /**
  82. * Returns total time used for sql queries execution.
  83. *
  84. * @return float
  85. */
  86. public function GetGlobalTimer() {
  87. return $this->mGlobalTimer;
  88. } // method
  89. /**
  90. * Returns time used for last sql query execution.
  91. *
  92. * @return float
  93. */
  94. public function GetLastQueryTimer() {
  95. return $this->mLastQueryTimer;
  96. } // method
  97. /**
  98. * Returns time used for last sql query execution.
  99. *
  100. * @return float
  101. */
  102. public function GetNumQueries() {
  103. return $this->mNumQueries;
  104. } // method
  105. /**
  106. * Returns raw queries.
  107. *
  108. * @return float
  109. */
  110. public function GetRawQueries() {
  111. return $this->mRawQueries;
  112. } // method
  113. /**
  114. * Returns raw success queries.
  115. *
  116. * @return float
  117. */
  118. public function GetRawSuccessQueries() {
  119. return $this->mRawSuccessQueries;
  120. } // method
  121. /**
  122. * Returns queries.
  123. *
  124. * @return float
  125. */
  126. public function GetQueries() {
  127. return $this->mQueries;
  128. } // method
  129. /**
  130. * Returns success queries.
  131. *
  132. * @return float
  133. */
  134. public function GetSuccessQueries() {
  135. return $this->mSuccessQueries;
  136. } // method
  137. /**
  138. * Singleton function Creates once an extended PDO instance representing a connection to a database.
  139. *
  140. * @param string $dsn The Data Source Name, or DSN, contains the information required to connect to the database.
  141. * @param string $username The user name for the DSN string. This parameter is optional for some PDO drivers.
  142. * @param string $password The password for the DSN string. This parameter is optional for some PDO drivers.
  143. * @param array $driverOptions A key=>value array of driver-specific connection options.
  144. * @return AcPDO
  145. */
  146. public static function GetInstance($dsn = null, $username = null, $password = null, $driverOptions = null) {
  147. if (is_null(self::$mSingleton)) {
  148. self::$mSingleton = new AcPDO($dsn, $username, $password, $driverOptions);
  149. } // if
  150. return self::$mSingleton;
  151. } // method
  152. /**
  153. * Singleton function Creates once an extended PDO instance representing a connection to a database.
  154. *
  155. * @param string $dsn The Data Source Name, or DSN, contains the information required to connect to the database.
  156. * @param string $username The user name for the DSN string. This parameter is optional for some PDO drivers.
  157. * @param string $password The password for the DSN string. This parameter is optional for some PDO drivers.
  158. * @param array $driverOptions A key=>value array of driver-specific connection options.
  159. * @return AcPDO
  160. */
  161. public static function Singleton($dsn = null, $username = null, $password = null, $driverOptions = null) {
  162. return self::GetInstance($dsn, $username, $password, $driverOptions);
  163. } // method
  164. // =============================================================================================
  165. // Protected methods ===========================================================================
  166. // =============================================================================================
  167. // Private methods =============================================================================
  168. // =============================================================================================
  169. // Magic methods ===============================================================================
  170. /**
  171. * Constructor Creates an extended PDO instance representing a connection to a database.
  172. *
  173. * @param string $dsn
  174. * @param string $username
  175. * @param string $password
  176. * @param array $driverOptions
  177. * @return void
  178. */
  179. public function __construct($dsn, $username = null, $password = null, $driverOptions = null) {
  180. parent::__construct($dsn, $username, $password, $driverOptions);
  181. $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('AcPDOStatement', array($this)));
  182. } // method
  183. // =============================================================================================
  184. } // class
Add Comment
Please, Sign In to add comment