Commit 3978ebba02c69fa227423d89f19df737b40083d7

  • avatar
  • p4bl0 <pablo.rauzy @gm…l.com>
  • Fri Mar 06 15:14:47 CET 2009
Adding the possibility to use persistent connexion to the database (MySQL or SQLite)
Db.php
(13 / 7)
  
1212
1313 protected
1414 $_connection,
15 $_persistent,
1516 $_host,
1617 $_user,
1718 $_pass,
2525 public function __construct ($db_params)
2626 {
2727 $this->_connection = null;
28 $this->_host = null;
29 $this->_user = null;
30 $this->_pass = null;
31 $this->_base = null;
28 if (isset($db_params['persistent']))
29 $this->_persistent = $db_params['persistent'];
30 if (isset($db_params['host'])) $this->_host = $db_params['host'];
31 if (isset($db_params['user'])) $this->_user = $db_params['user'];
32 if (isset($db_params['pass'])) $this->_pass = $db_params['pass'];
33 if (isset($db_params['base'])) $this->_base = $db_params['base'];
3234 $this->_query = null;
3335 $this->_numberOfQueries = 0;
3436 $this->_results = null;
3537 $this->_rows = array();
3638 }
39
40 abstract function connect ();
3741
38 abstract function reconnect ();
42 public function reconnect ()
43 {
44 if (!$this->isConnected()) $this->connect();
45 }
3946
4047 public function isConnected ()
4148 {
119119 {
120120 return $this->_numberOfQueries;
121121 }
122
123 abstract function freeResults ();
124122
125123}
Db/MySQL.php
(16 / 16)
  
1313 public function __construct ($db_params)
1414 {
1515 parent::__construct($db_params);
16 $this->_host = $db_params['host'];
17 $this->_user = $db_params['user'];
18 $this->_pass = $db_params['pass'];
19 $this->_base = $db_params['base'];
20
21 $this->_connection = mysql_connect($this->_host, $this->_user, $this->_pass);
22 mysql_select_db($this->_base, $this->_connection);
16 $this->connect();
2317 }
24
25 public function reconnect ()
18
19 public function connect ()
2620 {
27 if (!$this->isConnected()) {
28 $this->_connection = mysql_connect($this->_host, $this->_user, $this->_pass);
29 mysql_select_db($this->_base, $this->_connection);
30 }
21 if ($this->_persistent) $this->_connection = mysql_pconnect($this->_host,
22 $this->_user,
23 $this->_pass);
24 else $this->_connection = mysql_connect($this->_host,
25 $this->_user,
26 $this->_pass);
27 mysql_select_db($this->_base, $this->_connection);
3128 }
3229
3330 public function execute ()
7575
7676 public function numberOfRows ()
7777 {
78 if (strtolower(substr($this->_query, 0, 6)) == 'select' && $this->_results != null)
78 if (strtolower(substr($this->_query, 0, 6)) == 'select' &&
79 $this->_results != null)
7980 return mysql_num_rows($this->_results);
8081 else if ($this->_results === true)
8182 return $this->affectedRows();
8585
8686 public function affectedRows ()
8787 {
88 if (in_array(strtolower(substr($this->_query, 0, 7)), array('insert ', 'update ', 'replace', 'delete ')))
88 if (in_array(strtolower(substr($this->_query, 0, 7)),
89 array('insert ', 'update ', 'replace', 'delete ')))
8990 return mysql_affected_rows($this->_connection);
9091 else return null;
9192 }
9293
9394 public function freeResults ()
9495 {
95 if (substr($this->_query, 0, 6) == 'SELECT' && $this->_results != null)
96 if (strtolower(substr($this->_query, 0, 6)) == 'select' &&
97 $this->_results != null)
9698 mysql_free_result($this->_results);
9799 }
98100
Db/SQLite.php
(10 / 18)
  
1313 public function __construct ($db_params)
1414 {
1515 parent::__construct($db_params);
16 $this->_base = $db_params['base'];
1716 if (isset($db_params['dir']))
18 $this->_base = $db_params['dir'].$db_params['base'];
19 else
20 $this->_base = $db_params['base'];
21
22 $this->_connection = sqlite_open($this->_base);
17 $this->_base = $db_params['dir'].$this->_base;
18 $this->connect();
2319 }
24
25 public function reconnect ()
20
21 public function connect ()
2622 {
27 if (!$this->isConnected())
28 $this->_connection = sqlite_open($this->_base);
23 if ($this->_persistent) $this->_connection = sqlite_popen($this->_base);
24 else $this->_connection = sqlite_open($this->_base);
2925 }
3026
3127 public function execute ()
7272
7373 public function numberOfRows ()
7474 {
75 if (strtolower(substr($this->_query, 0, 6)) == 'select' && $this->_results != null)
75 if (strtolower(substr($this->_query, 0, 6)) == 'select' &&
76 $this->_results != null)
7677 return sqlite_num_rows($this->_results);
7778 else if ($this->_results === true)
7879 return $this->affectedRows();
8282
8383 public function affectedRows ()
8484 {
85 if (in_array(strtolower(substr($this->_query, 0, 7)), array('insert ', 'update ', 'replace', 'delete ')))
85 if (in_array(strtolower(substr($this->_query, 0, 7)),
86 array('insert ', 'update ', 'replace', 'delete ')))
8687 return sqlite_changes($this->_connection);
8788 else return null;
88 }
89
90 public function freeResults ()
91 {
92 if (substr($this->_query, 0, 6) == 'SELECT' && $this->_results != null)
93 mysql_free_result($this->_results);
9489 }
9590
9691}