Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa] DB manager
ARJ
post
Post #1





Grupa: Zarejestrowani
Postów: 453
Pomógł: 22
Dołączył: 20.09.2004
Skąd: Kraków - NH -

Ostrzeżenie: (0%)
-----


witam.
wystartowałem z projektem frameworka i póki co ciężko mi idzie. postanowiłem napisać najpierw DB manager i dopiero wgłębiać się dalej.
proszę o ocenę kodu i wszelkie sugestię. PDO dopiero poznaje jak i możliwości php5 dlatego podejrzewam, że mogłem coś namieszać (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) zastosowania nie podaję bo to chyba oczywiste.

config
  1. <?php
  2. define('DB_HOST','localhost');
  3. define('DB_USER','arj');
  4. define('DB_PASS','');
  5. define('DB_NAME','test');
  6. define('DB_KIND','mysql');
  7. define('DB_CHARSET','utf8');
  8. define('DB_COLCON','utf8_general_ci');
  9. ?>


dbmanager.class.php
  1. <?php
  2. /**
  3.  * Database Manager
  4.  * 
  5.  * @package Darion
  6.  * @author Arkadiusz 'ARJ' Jasak
  7.  * @copyright Copyright (c) 2006 Arkadiusz 'ARJ' Jasak (ajasak[at]gmail.com)
  8.  * @version 0.2
  9.  */
  10. class DBmanager{
  11. /**
  12.  * Object instance
  13.  *
  14.  * @var object
  15.  * @access private
  16.  */
  17. static private $thisInstance = null;
  18.  
  19. /**
  20.  * Query results
  21.  *
  22.  * @var array
  23.  * @access private
  24.  */
  25. private $result = array();
  26.  
  27. /**
  28.  * PDO instance
  29.  *
  30.  * @var object
  31.  * @access private
  32.  */
  33. private $dbh;
  34.  
  35. /**
  36.  * Number of executes
  37.  *
  38.  * @access private
  39.  * @var int
  40.  */
  41. private $numExecutes = 0;
  42.  
  43. /**
  44.  * Constructor
  45.  * Connect to database and set character encoding
  46.  * @access public
  47.  */
  48. public function __construct(){
  49. try{
  50. $this->dbh = new PDO(DB_KIND.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
  51. $this->dbh->exec('SET CHARACTER SET '.DB_CHARSET);
  52. $this->dbh->exec('SET collation_connection = '.DB_COLCON.';');
  53. }catch (PDOException $e){
  54. echo 'Error!: ' . $e->getMessage() . '<br/>';
  55. }
  56. }
  57.  
  58. /**
  59.  * Query and fetch
  60.  *
  61.  * @param string $_query
  62.  * @param string $_mode
  63.  * @param int $_param
  64.  * @access public
  65.  * @return array
  66.  */
  67. public function queryandfetch($_query, $_mode, $_param = NULL){
  68. $this->numExecutes++;
  69. $this->result = null;
  70. $this->result = $this->dbh->query($_query);
  71.  
  72. switch ($_mode){
  73. case 'assoc':
  74. return $this->result->fetchAll(PDO::FETCH_ASSOC);
  75. break;
  76. case 'both':
  77. return $this->result->fetchAll(PDO::FETCH_BOTH);
  78. break;
  79. case 'column':
  80. return $this->result->fetchAll(PDO::FETCH_COLUMN, $_param);
  81. break;
  82. default:
  83. return $this->result->fetchAll(PDO::FETCH_ASSOC);
  84. }
  85. }
  86.  
  87. /**
  88.  * Exec: insert, update, delete
  89.  *
  90.  * @param string $_exec
  91.  * @access public
  92.  * @return int or false
  93.  */
  94. public function exec($_exec){
  95. $this->numExecutes++;
  96. $this->result = $this->dbh->exec($_exec);
  97. if($this->result == false){
  98. return false;
  99. }else{
  100. return $this->result;
  101. }
  102. }
  103.  
  104. /**
  105.  * Count rows number
  106.  *
  107.  * @param string $_table
  108.  * @param string $_where
  109.  * @return int
  110.  */
  111. public function numRows($_table, $_where){
  112. $this->numExecutes++;
  113. $this->result = $this->dbh->query("SELECT COUNT(*) FROM ".$_table." WHERE ".$_where);
  114. return $this->result->fetchColumn();
  115. }
  116.  
  117. /**
  118.  * Return number of executes
  119.  *
  120.  * @access public
  121.  * @return int
  122.  */
  123. public function numExecutes(){
  124. return $this->numExecutes;
  125. }
  126.  
  127. /**
  128.  * Singleton
  129.  *
  130.  * @access public
  131.  * @return object
  132.  */
  133. static public function getInstance() {
  134. if(self::$thisInstance == null)
  135. {
  136. self::$thisInstance = new DBmanager();
  137. }
  138. return self::$thisInstance;
  139. }
  140.  
  141. /**
  142.  * Destructor
  143.  * Close connection with database
  144.  * @access public
  145.  */
  146. public function __destruct(){
  147. $this->dbh = null;
  148. }
  149. }
  150. ?>


Ten post edytował ARJ 18.09.2006, 18:29:30
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
ARJ
post
Post #2





Grupa: Zarejestrowani
Postów: 453
Pomógł: 22
Dołączył: 20.09.2004
Skąd: Kraków - NH -

Ostrzeżenie: (0%)
-----


w pierwszym poście przedstawiam DB manager ver 0.2
Wprowadziłem następujące zmiany:
- przerobiona metoda queryandfetch (można wybrać co ma zostać zwrócone)
- nowa metoda numRows
- nowa zmienna i metoda numExecutes zwracająca ilość zapytań wysłanych do bazy

ToDo:
- napisać obsługę Exceptions
- wprowadzić wszystkie poprawki i naprawić błędy które tu zgłosicie
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 9.06.2026 - 09:31