- {
- var $db_dsnw; // DSN for write operations
- var $db_dsnr; // DSN for read operations
- var $db_connected = false; // Already connected ?
- var $db_mode = ''; // Connection mode
- var $db_handle = 0; // Connection handle
- var $db_error = false;
- var $db_error_msg = '';
- var $debug_mode = false;
-
- var $a_query_results = array('dummy');
- var $last_res_id = 0;
-
-
- /**
- * Object constructor
- *
- * @param string DSN for read/write operations
- * @param string Optional DSN for read only operations
- */
- function __construct($db_dsnw, $db_dsnr='', $pconn=false)
- {
- if ($db_dsnr=='')
- $db_dsnr=$db_dsnw;
-
- $this->db_dsnw = $db_dsnw;
- $this->db_dsnr = $db_dsnr;
- $this->db_pconn = $pconn;
-
- $dsn_array = MDB2::parseDSN($db_dsnw);
- $this->db_provider = $dsn_array['phptype'];
- }
-
-
- /**
- * Connect to specific database
- *
- * @param string DSN for DB connections
- * @return object PEAR database handle
- * @access private
- */
- function dsn_connect($dsn)
- {
- // Use persistent connections if available
- $db_options = array(
- 'persistent' => $this->db_pconn,
- 'emulate_prepared' => $this->debug_mode,
- 'debug' => $this->debug_mode,
- 'debug_handler' => 'mdb2_debug_handler',
- 'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL);
-
- if ($this->db_provider == 'pgsql') {
- $db_options['disable_smart_seqname'] = true;
- $db_options['seqname_format'] = '%s';
- }
-
- $dbh = MDB2::connect($dsn, $db_options);
-
- if (MDB2::isError($dbh))
- {
- $this->db_error = TRUE;
- $this->db_error_msg = $dbh->getMessage();
-
- raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__,
- 'file' => __FILE__, 'message' => $dbh->getUserInfo()), TRUE, FALSE);
- }
- else if ($this->db_provider=='sqlite')
- {
- $dsn_array = MDB2::parseDSN($dsn);
- if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials))
- $this->_sqlite_create_database($dbh, $this->sqlite_initials);
- }
- else
- $dbh->setCharset('utf8');
-
- return $dbh;
- }
-
-
- /**
- * Connect to appropiate databse
- * depending on the operation
- *
- * @param string Connection mode (r|w)
- * @access public
- */
- function db_connect($mode)
- {
- $this->db_mode = $mode;
-
- // Already connected
- if ($this->db_connected)
- {
- // no replication, current connection is ok
- if ($this->db_dsnw==$this->db_dsnr)
- return;
-
- // connected to master, current connection is ok
- if ($this->db_mode=='w')
- return;
-
- // Same mode, current connection is ok
- if ($this->db_mode==$mode)
- return;
- }
-
- if ($mode=='r')
- $dsn = $this->db_dsnr;
- else
- $dsn = $this->db_dsnw;
-
- $this->db_handle = $this->dsn_connect($dsn);
- $this->db_connected = true;
- }
-
-
- /**
- * Activate/deactivate debug mode
- *
- * @param boolean True if SQL queries should be logged
- */
- function set_debug($dbg = true)
- {
- $this->debug_mode = $dbg;
- if ($this->db_connected)
- {
- $this->db_handle->setOption('debug', $dbg);
- $this->db_handle->setOption('emulate_prepared', $dbg);
- }
- }
-
-
- /**
- * Getter for error state
- *
- * @param boolean True on error
- */
- function is_error()
- {
- return $this->db_error ? $this->db_error_msg : FALSE;
- }
-
-
- /**
- * Execute a SQL query
- *
- * @param string SQL query to execute
- * @param mixed Values to be inserted in query
- * @return number Query handle identifier
- * @access public
- */
- function query()
- {
- $params = func_get_args();
- $query = array_shift($params);
-
- return $this->_query($query, 0, 0, $params);
- }
-
-
- /**
- * Execute a SQL query with limits
- *
- * @param string SQL query to execute
- * @param number Offset for LIMIT statement
- * @param number Number of rows for LIMIT statement
- * @param mixed Values to be inserted in query
- * @return number Query handle identifier
- * @access public
- */
- function limitquery()
- {
- $params = func_get_args();
- $query = array_shift($params);
- $offset = array_shift($params);
- $numrows = array_shift($params);
-
- return $this->_query($query, $offset, $numrows, $params);
- }
-
-
- /**
- * Execute a SQL query with limits
- *
- * @param string SQL query to execute
- * @param number Offset for LIMIT statement
- * @param number Number of rows for LIMIT statement
- * @param array Values to be inserted in query
- * @return number Query handle identifier
- * @access private
- */
- function _query($query, $offset, $numrows, $params)
- {
- // Read or write ?
- if (strtolower(trim(substr($query,0,6)))=='select')
- $mode='r';
- else
- $mode='w';
-
- $this->db_connect($mode);
-
- if ($this->db_provider == 'sqlite')
- $this->_sqlite_prepare();
-
- if ($numrows || $offset)
- $result = $this->db_handle->setLimit($numrows,$offset);
-
- if (empty($params))
- $result = $this->db_handle->query($query);
- else
- {
- $params = (array)$params;
- $q = $this->db_handle->prepare($query);
- if ($this->db_handle->isError($q))
- {
- $this->db_error = TRUE;
- $this->db_error_msg = $q->userinfo;
-
- raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__,
- 'message' => $this->db_error_msg), TRUE, TRUE);
+{
+ public $db_dsnw; // DSN for write operations
+ public $db_dsnr; // DSN for read operations
+ public $db_connected = false; // Already connected ?
+ public $db_mode = ''; // Connection mode
+ public $db_handle = 0; // Connection handle
+ public $db_error = false;
+ public $db_error_msg = '';
+
+ private $debug_mode = false;
+ private $conn_failure = false;
+ private $a_query_results = array('dummy');
+ private $last_res_id = 0;
+ private $tables;
+
+
+ /**
+ * Object constructor
+ *
+ * @param string $db_dsnw DSN for read/write operations
+ * @param string $db_dsnr Optional DSN for read only operations
+ */
+ function __construct($db_dsnw, $db_dsnr='', $pconn=false)
+ {
+ if (empty($db_dsnr))
+ $db_dsnr = $db_dsnw;
+
+ $this->db_dsnw = $db_dsnw;
+ $this->db_dsnr = $db_dsnr;
+ $this->db_pconn = $pconn;
+
+ $dsn_array = MDB2::parseDSN($db_dsnw);
+ $this->db_provider = $dsn_array['phptype'];
+ }
+
+
+ /**
+ * Connect to specific database
+ *
+ * @param string $dsn DSN for DB connections
+ * @return MDB2 PEAR database handle
+ * @access private
+ */
+ private function dsn_connect($dsn)
+ {
+ // Use persistent connections if available
+ $db_options = array(
+ 'persistent' => $this->db_pconn,
+ 'emulate_prepared' => $this->debug_mode,
+ 'debug' => $this->debug_mode,
+ 'debug_handler' => array($this, 'debug_handler'),
+ 'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL);
+
+ if ($this->db_provider == 'pgsql') {
+ $db_options['disable_smart_seqname'] = true;
+ $db_options['seqname_format'] = '%s';
+ }
+ $this->db_error = false;
+ $this->db_error_msg = null;
+
+ $dbh = MDB2::connect($dsn, $db_options);
+
+ if (MDB2::isError($dbh)) {
+ $this->db_error = true;
+ $this->db_error_msg = $dbh->getMessage();
+
+ raise_error(array('code' => 500, 'type' => 'db',
+ 'line' => __LINE__, 'file' => __FILE__,
+ 'message' => $dbh->getUserInfo()), true, false);
+ }
+ else if ($this->db_provider == 'sqlite') {
+ $dsn_array = MDB2::parseDSN($dsn);
+ if (!filesize($dsn_array['database']) && !empty($this->sqlite_initials))
+ $this->_sqlite_create_database($dbh, $this->sqlite_initials);
+ }
+ else if ($this->db_provider!='mssql' && $this->db_provider!='sqlsrv')
+ $dbh->setCharset('utf8');
+
+ return $dbh;
+ }
+
+
+ /**
+ * Connect to appropiate database depending on the operation
+ *
+ * @param string $mode Connection mode (r|w)
+ * @access public
+ */
+ function db_connect($mode)
+ {
+ // previous connection failed, don't attempt to connect again
+ if ($this->conn_failure) {
+ return;