Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Klasa PHP5] Modyfikacja PDO oraz PDOStatement, MyPDO, MyPDOStatement -> statystyki oraz licznik
greycoffey
post 4.12.2010, 18:28:33
Post #1





Grupa: Zarejestrowani
Postów: 320
Pomógł: 29
Dołączył: 3.04.2010

Ostrzeżenie: (20%)
X----


Witam wszystkich, jeśli kiedykolwiek używaliście PDO do obsługi baz danych, na pewno napotkaliście pewien błahy problem, mianowicie liczenie zapytań oraz tworzenie statystyk czasowych. Po jakimś czasie może zrezygnowaliście, lub stworzyliście upragnioną klasę. la tych co zrezygnowali, oto kod z małą dokumentacją:

  1. <?php
  2.  
  3. /* Autor: GreyCoffey
  4.  * Email: greycoffey@gmail.com
  5.  * GG: 790560
  6.  * ===
  7.  * Opis:
  8.  * Plik zawiera klasy rozszerzające możliwości klas PDO oraz PDOStatement.
  9.  * Podstawowe funkcje: statystyki dot. czasu wykonania zapytań oraz
  10.  * dotyczące liczby zapytań. Proszę o uwagi.
  11.  * ===
  12.  * Użycie:
  13.  * Podczas tworzenia obiektu PDO, zamieniamy nazwę na MyPDO.
  14.  * ===
  15.  * Dodatkowe metody (statyczne) w MyPDO:
  16.  * getType($zmienna) - określa typ zmiennej i podaje odpowiednią stałą PDO, przydatne przy filtrowaniu.
  17.  * getStats() - zwraca tablicę z wykonanymi zapytaniami. Indeks 'sql' - zapytanie, 'time' - czas wykonania.
  18.  * getCounter() - zwraca ilość wykonanych zapytań.
  19.  */
  20.  
  21. class MyPDO extends PDO
  22. {
  23. private static $stats;
  24. private static $counter=0;
  25. private static $start;
  26.  
  27. public static function getType($var)
  28. {
  29. if (is_int($var))
  30. return PDO::PARAM_INT;
  31. else if (is_bool($var))
  32. return PDO::PARAM_BOOL;
  33. else if (is_null($var))
  34. return PDO::PARAM_NULL;
  35.  
  36. return PDO::PARAM_STR;
  37. }
  38.  
  39. public static function startTime()
  40. {
  41. self::$start = microtime(true);
  42. }
  43.  
  44. public static function endTime($sql)
  45. {
  46. self::$stats[] = array('sql'=>$sql,'time'=>(microtime(true)-self::$start));
  47. }
  48.  
  49. public static function getStats()
  50. {
  51. return self::$stats;
  52. }
  53.  
  54. public static function getCounter()
  55. {
  56. return self::$counter;
  57. }
  58.  
  59. public static function incCounter()
  60. {
  61. self::$counter++;
  62. }
  63.  
  64. public function exec($sql) {
  65. $args = func_get_args();
  66.  
  67. if (Config::get('mypdostats')=='on')
  68. self::startTime();
  69.  
  70. $result = call_user_func_array('parent::exec', $args);
  71.  
  72. if (Config::get('mypdostats')=='on')
  73. self::endTime($sql);
  74.  
  75. self::incCounter();
  76. return $result;
  77. }
  78.  
  79. public function prepare($sql)
  80. {
  81. $args = func_get_args();
  82. $result = call_user_func_array('parent::prepare', $args);
  83. $res = new MyPDOStatement($result);
  84. return $res;
  85.  
  86. }
  87.  
  88. public function query($sql) {
  89. $args = func_get_args();
  90.  
  91. if (Config::get('mypdostats')=='on')
  92. self::startTime();
  93.  
  94. $result = call_user_func_array('parent::query', $args);
  95. $res = new MyPDOStatement($result);
  96.  
  97. if (Config::get('mypdostats')=='on')
  98. self::endTime($sql);
  99.  
  100. self::incCounter();
  101. return $res;
  102. }
  103. }
  104.  
  105. class MyPDOStatement
  106. {
  107. private $_object;
  108.  
  109. public function __construct($result)
  110. {
  111. $this->_object = $result;
  112. }
  113.  
  114. public function execute()
  115. {
  116. $args = func_get_args();
  117.  
  118. if (Config::get('mypdostats')=='on')
  119. MyPDO::startTime();
  120.  
  121. $a = $this->_object;
  122. $result = call_user_func_array(array(&$a, 'execute'), $args);
  123.  
  124. if (Config::get('mypdostats')=='on')
  125. MyPDO::endTime($this->_object->queryString);
  126.  
  127. MyPDO::incCounter();
  128. return $result;
  129. }
  130.  
  131. public function __call($method, $args)
  132. {
  133. $a = $this->_object;
  134. if(is_callable(array(&$a, $method)))
  135. {
  136. $result = call_user_func_array(array(&$a, $method), $args);
  137. }
  138. else
  139. $result = false;
  140.  
  141. return $result;
  142. }
  143.  
  144. }
  145.  
  146. ?>


Config jest moją klasą, jeśli ktoś chce kod, proszę:

  1. <?php
  2.  
  3. /* Autor: GreyCoffey
  4.  * Email: greycoffey@gmail.com
  5.  * GG: 790560
  6.  * Config czyta zmienne z pliku ini, i udostępnia je innym klasom.
  7.  */
  8.  
  9. class Config
  10. {
  11. private static $_loaded=FALSE;
  12. private static $_config;
  13. private static $_defaultinifile='config.ini';
  14.  
  15. public static function load($file)
  16. {
  17. if (!file_exists($file)) {
  18. throw new Exception("Plik '$file' nie stnieje.");
  19. }
  20.  
  21. if (self::$_loaded==FALSE) {
  22. self::$_config=parse_ini_file($file);
  23. self::$_loaded = TRUE; return TRUE;
  24. }
  25.  
  26. return FALSE;
  27. }
  28.  
  29. public static function get($name)
  30. {
  31. if (self::$_loaded==FALSE) {
  32. self::load(self::$_defaultinifile);
  33. }
  34.  
  35. if (isset(self::$_config[$name])) {
  36. return self::$_config[$name];
  37. }
  38.  
  39. return NULL;
  40. }
  41. }
  42.  
  43. ?>


Licencja? Możecie rozpowszechniać, modyfikować, sprzedawać, nagrywać, usuwać itp., ale nie mozecie usuwać informacji o autorze (dokumentację tak ;P).
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 16.04.2024 - 20:17