← Back to team overview

anewt-developers team mailing list archive

[Branch ~uws/anewt/anewt.uws] Rev 1697: [database] Add debug stuff and improve MySQL error handling

 

------------------------------------------------------------
revno: 1697
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Mon 2009-07-20 22:41:56 +0200
message:
  [database] Add debug stuff and improve MySQL error handling
  
  Added AnewtDatabaseConnection::$n_queries_executed and
  AnewtDatabaseResultSet::$queries_executed for debugging
  purposes.
  
  Also update MySQL backend to throw better errors when the
  configured encoding is invalid.
modified:
  database/backend-mysql-old.lib.php
  database/backend-mysql.lib.php
  database/backend-postgresql.lib.php
  database/backend-sqlite.lib.php
  database/connection.lib.php
  database/prepared-query.lib.php

=== modified file 'database/backend-mysql-old.lib.php'
--- database/backend-mysql-old.lib.php	2009-07-18 12:42:07 +0000
+++ database/backend-mysql-old.lib.php	2009-07-20 20:41:56 +0000
@@ -89,7 +89,7 @@
 		return mysql_insert_id($this->connection_handle);
 	}
 
-	function _result_set_for_query($sql)
+	function real_execute_sql($sql)
 	{
 		$result_set_handle = mysql_query($sql, $this->connection_handle);
 

=== modified file 'database/backend-mysql.lib.php'
--- database/backend-mysql.lib.php	2009-07-20 20:01:43 +0000
+++ database/backend-mysql.lib.php	2009-07-20 20:41:56 +0000
@@ -67,12 +67,22 @@
 		try
 		{
 			$this->connection_handle = @new MySQLi($hostname, $username, $password, $database);
+		}
+		catch (Exception $e)
+		{
+			throw new AnewtDatabaseConnectionException('Could not connect to MySQL database: %s', $e->getMessage());
+		}
 
-			if ($encoding)
+		if ($encoding)
+		{
+			try
+			{
 				$this->prepare_execute('SET NAMES ?str?', $encoding);
-		}
-		catch (Exception $e) {
-			throw new AnewtDatabaseConnectionException('Could not connect to MySQL database: %s', $e->getMessage());
+			}
+			catch (Exception $e)
+			{
+				throw new AnewtDatabaseConnectionException('Could not set MySQL encoding to "%s": %s', $encoding, $e->getMessage());
+			}
 		}
 
 	}
@@ -93,7 +103,7 @@
 		return $this->connection_handle->insert_id;
 	}
 
-	function _result_set_for_query($sql)
+	function real_execute_sql($sql)
 	{
 		$result_set_handle = $this->connection_handle->query($sql);
 

=== modified file 'database/backend-postgresql.lib.php'
--- database/backend-postgresql.lib.php	2009-07-18 16:51:09 +0000
+++ database/backend-postgresql.lib.php	2009-07-20 20:41:56 +0000
@@ -103,7 +103,7 @@
 		return $row['id'];
 	}
 
-	function _result_set_for_query($sql)
+	function real_execute_sql($sql)
 	{
 		$result_set_handle = pg_query($this->connection_handle, $sql);
 

=== modified file 'database/backend-sqlite.lib.php'
--- database/backend-sqlite.lib.php	2009-07-18 12:42:07 +0000
+++ database/backend-sqlite.lib.php	2009-07-20 20:41:56 +0000
@@ -67,7 +67,7 @@
 		return sqlite_last_insert_rowid($this->connection_handle);
 	}
 
-	public function _result_set_for_query($sql)
+	public function real_execute_sql($sql)
 	{
 		$result_set_handle = sqlite_query($this->connection_handle, $sql);
 

=== modified file 'database/connection.lib.php'
--- database/connection.lib.php	2009-07-18 12:42:07 +0000
+++ database/connection.lib.php	2009-07-20 20:41:56 +0000
@@ -44,6 +44,22 @@
 	protected $connection_handle;
 
 	/**
+	 * The number of executed queries.
+	 *
+	 * This is mostly useful for debugging.
+	 */
+	public $n_queries_executed;
+
+	/**
+	 * List of executed queries.
+	 *
+	 * This is mostly useful for debugging. Note that only a certain number of
+	 * queries are kept here before the first query is no longer kept around
+	 * (ring buffer), hence the list may not be complete.
+	 */
+	public $queries_executed;
+
+	/**
 	 * \private
 	 *
 	 * Create a new connection instance (internal use only).
@@ -59,6 +75,9 @@
 	{
 		assert('is_assoc_array($settings);');
 
+		$this->n_queries_executed = 0;
+		$this->queries_executed = array();
+
 		$default_settings = array(
 			'persistent' => true,
 		);
@@ -332,9 +351,10 @@
 	/**
 	 * \private
 	 *
-	 * Create an AnewtDatabaseResultSet (subclass) for a query.
+	 * Execute SQL and create an AnewtDatabaseResultSet.
 	 *
-	 * This method is for internal use only.
+	 * This method is for internal use only. Use one of the prepare() methods to
+	 * prepare and execute queries.
 	 *
 	 * \param $sql
 	 *   The SQL query to execute.
@@ -342,7 +362,26 @@
 	 * \return
 	 *   A new AnewtDatabaseResultSet instance.
 	 */
-	abstract function _result_set_for_query($sql);
+	function execute_sql($sql)
+	{
+		$this->n_queries_executed ++;
+
+		/* There is a maximum on the number of queries kept in the list to avoid
+		 * memory issues with long running scripts that execute many queries. */
+		if (count($this->queries_executed) >= 100)
+			array_shift($this->queries_executed);
+
+		$this->queries_executed[] = $sql;
+
+		return $this->real_execute_sql($sql);
+	}
+
+	/**
+	 * Execute SQL and create an AnewtDatabaseResultSet for a query.
+	 * 
+	 * This method is for internal use only and is backend-specific.
+	 */
+	abstract protected function real_execute_sql($sql);
 
 	/** \} */
 

=== modified file 'database/prepared-query.lib.php'
--- database/prepared-query.lib.php	2009-03-29 15:47:50 +0000
+++ database/prepared-query.lib.php	2009-07-20 20:41:56 +0000
@@ -100,7 +100,7 @@
 	{
 		$this->connection->connect();
 		$sql = $this->sql_template->fillv($values);
-		$result_set = $this->connection->_result_set_for_query($sql);
+		$result_set = $this->connection->execute_sql($sql);
 		return $result_set;
 	}
 }



--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws

Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription.