changeset 8:dd84b57c63f2

Layout.
author Sjoerd Mullender <sjoerd@acm.org>
date Tue, 20 Sep 2016 11:08:39 +0200 (2016-09-20)
parents db64265f237d
children 32bbba9f1fff
files lib/php_mapi.inc lib/php_monetdb.php
diffstat 2 files changed, 274 insertions(+), 233 deletions(-) [+]
line wrap: on
line diff
--- a/lib/php_mapi.inc
+++ b/lib/php_mapi.inc
@@ -1,23 +1,23 @@
 <?php
 /**
-   *	Implementation of the MAPI protocol (v9).
-   *
-   *
-   * 	Provides:
-   * -  mapi_query($data) {
-   * -  mapi_store($data) {
-   * -  php_parse_tuples($rows) {
-   * -  mapi_connect() {
-   * -  mapi_authenticate($user, $passwd, $hash, $salt, $dbname) {
-   * -  mapi_read() {
-   * -  mapi_write($msg) {
-   * -  set_timezone() {
-   * -  format_command($cmd) {
-   * -  mapi_connect_proxy() {
-   * -  mapi_open() {
-   * - mapi_close() {
-   *
-**/
+ *	Implementation of the MAPI protocol (v9).
+ *
+ *
+ * 	Provides:
+ * -  mapi_query($data) {
+ * -  mapi_store($data) {
+ * -  php_parse_tuples($rows) {
+ * -  mapi_connect() {
+ * -  mapi_authenticate($user, $passwd, $hash, $salt, $dbname) {
+ * -  mapi_read() {
+ * -  mapi_write($msg) {
+ * -  set_timezone() {
+ * -  format_command($cmd) {
+ * -  mapi_connect_proxy() {
+ * -  mapi_open() {
+ * -  mapi_close() {
+ *
+ **/
 
 	define("MAX_PACKET_SIZE", 8190); // Maximum packet size
 
@@ -91,7 +91,7 @@
 			$handle = array("conn" => "", "header" => array(), "query" => "", "record_set" => array(), "operation" => -1, "last_row" => 0);
 			$handle["conn"] = $conn["id"];
 
-			if ( ($operation = mapi_store($data, $handle)) == FALSE) {
+			if (($operation = mapi_store($data, $handle)) == FALSE) {
 				return FALSE;
 			}
 
@@ -127,7 +127,7 @@
 			mapi_write($socket, format_command("export " . $handle["query"]["id"] . " " . $handle["query"]["index"] . " " . $exp_size));
 			$data = mapi_read($socket);
 
-			if ( ($operation = mapi_store($data, $handle)) == FALSE ) {
+			if (($operation = mapi_store($data, $handle)) == FALSE) {
 				return FALSE;
 			}
 
@@ -150,14 +150,14 @@
 		$rows = ""; // stores (partially) retrieved rows
 		foreach ($data as $row) {
 			/*
-			 	PHP5.2 complains when $row[0] is accessed with:
-
-				Notice: Uninitialized string offset: 0
-
-				In order to avoid the E_NOTICE error substr($row, 0, 1) is used
-				to access the first character of a string
-
-			*/
+			 * PHP5.2 complains when $row[0] is accessed with:
+			 *
+			 * Notice: Uninitialized string offset: 0
+			 *
+			 * In order to avoid the E_NOTICE error
+			 * substr($row, 0, 1) is used to access the
+			 * first character of a string
+			 */
 			if (substr($row, 0, 1) == MSG_QUERY) {
 				if ($row[1] == Q_TABLE) {
 					$operation = Q_TABLE;
@@ -197,10 +197,9 @@
 		}*/
 
 		if ($header != "") {
-
 			$handle["header"] = php_parse_header($header);
 			/* Store the number of fields returned by the dataset */
-		   	if ($operation == Q_TABLE || $operation == Q_BLOCK ) {
+			if ($operation == Q_TABLE || $operation == Q_BLOCK) {
 				$handle["query"]["fields"] = count($handle["header"]["fields"]);
 			}
 		}
@@ -227,7 +226,7 @@
 		foreach ($rows as &$row) {
 
 			$row = ltrim($row, "[ ");
-			$row = explode(",\t",  $row);
+			$row = explode(",\t", $row);
 
 			foreach ($row as &$field) {
 				$field = stripslashes($field);
@@ -243,12 +242,11 @@
 
 	function php_parse_row($row) {
 		$row = ltrim($row, "[ ");
-		$row = explode(",\t",  $row);
+		$row = explode(",\t", $row);
 		foreach ($row as &$field) {
 			if ($field == "NULL") {
 				$field = NULL;
-			}
-			else {
+			} else {
 
 				$field = stripslashes($field);
 				// strip left/right \" chars and right ','
@@ -307,9 +305,9 @@
 		/* No merovingian redirect. Perform an actual connection. */
 		if ($merovingian_iter == NULL) {
 			if (socket_connect($socket, $host, $port) == FALSE) {
-            	$last_error = socket_strerror(socket_last_error());
-              	throw new Exception('Cannot connect to server: ' . $last_error);
-            }
+				$last_error = socket_strerror(socket_last_error());
+				throw new Exception('Cannot connect to server: ' . $last_error);
+			}
 		}
 
 
@@ -349,9 +347,9 @@
 		// not ready to authenticate yet
 			if ($response[0] == MSG_REDIRECT) {
 				$redirects = explode("\n", $response);
-	                 	/* Follow the first redirect */
+				/* Follow the first redirect */
 
-				if ( ($redirects[0] == "") || (substr($redirects[0], 0, 6) != "^mapi:") ) {
+				if (($redirects[0] == "") || (substr($redirects[0], 0, 6) != "^mapi:")) {
 					print "Invalid redirect " . $redirects[0] . "\n";
 					return FALSE;
 				}
@@ -370,10 +368,10 @@
 						return FALSE;
 					}
 				}
-			 	else if($redirect_to['scheme'] == MONETDB ) {
+			 	else if($redirect_to['scheme'] == MONETDB) {
 
 					$options['host'] = $redirect_to['host'];
-					$options['port']     = $redirect_to['port'];
+					$options['port'] = $redirect_to['port'];
 					$options['database'] = ltrim($redirect_to['path'], '/');
 
 					socket_close($socket);
@@ -382,11 +380,10 @@
 						return FALSE;
 					}
 
-			    } else {
+				} else {
 					print $response;
 					return FALSE;
 				}
-
 			} else if ($response[0] == MSG_INFO) {
 				$last_error = $response;
 				return FALSE;
@@ -399,7 +396,7 @@
 	/* Hash function names have to be uppercase */
 	function mapi_authenticate_v9($socket, $user, $passwd, $hash, $algos, $salt, $dbname, $pwhash) {
 		$auth_string = "";
-		if ( (is_array($algos) && (! in_array(strtoupper($hash), $algos)) )  ) 			 {
+		if (is_array($algos) && !in_array(strtoupper($hash), $algos)) {
 			$last_error = "Hash function " . $hash . " not supported";
 			return FALSE;
 		}
@@ -415,7 +412,7 @@
 	// decode the header and get the requested amount of data
 	function mapi_read($socket=NULL) {
 		# get the first 2 bytes
-		if ( ($header = socket_read($socket, 2)) == FALSE) {
+		if (($header = socket_read($socket, 2)) == FALSE) {
 			$last_error = socket_strerror(socket_last_error());
 			throw new Exception('Lost connection with the server: ' . $last_error);
 		}
@@ -428,17 +425,17 @@
 		}
 
 
-		while ((ord($header[0]) & 1) == 0 ) {
-			if ( ($header = socket_read($socket, 2)) == FALSE) {
+		while ((ord($header[0]) & 1) == 0) {
+			if (($header = socket_read($socket, 2)) == FALSE) {
 				$last_error = socket_strerror(socket_last_error());
 				throw new Exception('Lost connection with the server: ' . $last_error);
 			}
 
-			$chunk_size = ( ((ord($header[1]))  << 7) | (ord($header[0]) >> 1) );
+			$chunk_size = ((ord($header[1])) << 7) | (ord($header[0]) >> 1);
 
 			$block = "";
 			while (strlen($block) < $chunk_size) {
-				if ( ($block .= socket_read($socket, $chunk_size - strlen($block))) == FALSE) {
+				if (($block .= socket_read($socket, $chunk_size - strlen($block))) == FALSE) {
 					$last_error = socket_strerror(socket_last_error());
 					throw new Exception('Lost connection with the server: ' . $last_error);
 				}
@@ -470,11 +467,11 @@
 
 		$is_final = FALSE;
 		while (! $is_final) {
-			$data  = substr($msg, $pos, min(MAX_PACKET_SIZE, (strlen($msg) - $pos))  );
+			$data = substr($msg, $pos, min(MAX_PACKET_SIZE, strlen($msg) - $pos));
 			$pos += strlen($data);
 
 			$end = 0; // more packets will follow
-			if ( (strlen($msg) - $pos) == 0) {
+			if ((strlen($msg) - $pos) == 0) {
 				$is_final = TRUE;
 				$end = 1;
 			}
@@ -482,11 +479,12 @@
 			$fb = (strlen($data) << 1) | $end;
 
 			/**
-			  * socket_write() does not guarantee all data to be transmitted.
-			  * Make sure that the buffer is flushed.
-			*/
+			 * socket_write() does not guarantee all data
+			 * to be transmitted.  Make sure that the
+			 * buffer is flushed.
+			 */
 
-			if ( ($buf = socket_flush($socket, pack("v", $fb) . $data)) == FALSE) {
+			if (($buf = socket_flush($socket, pack("v", $fb) . $data)) == FALSE) {
 				$last_error = socket_strerror(socket_last_error());
 				return -1;
 			}
@@ -528,15 +526,18 @@
 		$merovingian_iter = 0;
 
 		/**
-		  * When connecting, the function would first try to find a (persistent) link that's already
-		  * open with the same host, username and password. If one is found, an identifier for it will be returned
-		  * instead of opening a new connection.
-		  * TODO: move this check to mapi_connect() to deal with options arrays rewritten by redirects.
-		*/
+		 * When connecting, the function would first try to
+		 * find a (persistent) link that's already open with
+		 * the same host, username and password. If one is
+		 * found, an identifier for it will be returned
+		 * instead of opening a new connection.  TODO: move
+		 * this check to mapi_connect() to deal with options
+		 * arrays rewritten by redirects.
+		 */
 		if ($options['persistent'] == TRUE) {
 			if (count($connection_pool) > 0) {
 				foreach ($connection_pool as $conn) {
-					if ( ($conn["persistent"] == TRUE) && ($conn["dbname"] == $options['database']) && ($conn['username'] == $options['username']) && ($conn["password"] == hash($options['hashfunc'], $options['password']) && ($conn['host']) == $options['host']) && ( $conn['port'] == $options['port'] ) ) {
+					if (($conn["persistent"] == TRUE) && ($conn["dbname"] == $options['database']) && ($conn['username'] == $options['username']) && ($conn["password"] == hash($options['hashfunc'], $options['password']) && ($conn['host']) == $options['host']) && ($conn['port'] == $options['port'])) {
 						return $conn;
 					}
 				}
@@ -545,30 +546,37 @@
 
 		$socket	= mapi_open();
 
-		if ( mapi_connect($socket, $options, $merovingian_iter) == TRUE ) {
+		if (mapi_connect($socket, $options, $merovingian_iter) == TRUE) {
 			/* Connected */
 
 			// Create a new connection instance and insert an entry in the connections table
 			$id = mapi_generate_id();
 			if ($options['lang'] == LANG_SQL) {
-
-
 				/*
-					PHP requires a timezone to be specified in the configuration environment; if not specified
-					either in php.ini or via date_default_timezone_set(), a default is set 'Europe/Berlin'.
-
-					PHP complains in case we query the OS to get system's timezone (E_STRICT error).
-
-					To avoid unexpected behaviours and warnings at execution time we set a timezone on mserver
-					only if PHP interpreter is aware of it (php.ini contains a date.timezone entry).
-				*/
+				 * PHP requires a timezone to be
+				 * specified in the configuration
+				 * environment; if not specified
+				 * either in php.ini or via
+				 * date_default_timezone_set(), a
+				 * default is set 'Europe/Berlin'.
+				 *
+				 * PHP complains in case we query the
+				 * OS to get system's timezone
+				 * (E_STRICT error).
+				 *
+				 * To avoid unexpected behaviours and
+				 * warnings at execution time we set a
+				 * timezone on mserver only if PHP
+				 * interpreter is aware of it (php.ini
+				 * contains a date.timezone entry).
+				 */
 				if (ini_get("date.timezone")) {
 					set_timezone($socket); // set the timezone according to the system's configuration
 				}
 
 				// export the reply size (max number of tuples returned at query executions)
 				mapi_write($socket, format_command("reply_size " . REPLY_SIZE));
-				if (strlen($response = mapi_read($socket)) > 0 ) {
+				if (strlen($response = mapi_read($socket)) > 0) {
 					// something went wrong
 					$last_error = $response;
 					return FALSE;
@@ -610,9 +618,8 @@
 			}
 			if ($res != NULL) {
 				if (!is_array($res['query']) ||
-					!array_key_exists('id', $res['query']) ||
-					mapi_free_result($conn['id'], $res['query']['id']))
-			   	{
+				    !array_key_exists('id', $res['query']) ||
+				    mapi_free_result($conn['id'], $res['query']['id'])) {
 					return TRUE;
 				}
 			}
@@ -666,10 +673,12 @@
 			socket_close($socket);
 
 			/* remove the $conn from the pool */
-			// Create anonymous callback function to filter results for connection.
-			$function_body = 'return ( $input[\'id\'] != "'.$conn["id"].'" );';
+			// Create anonymous callback function to
+			// filter results for connection.
+			$function_body = 'return ($input[\'id\'] != "'.$conn["id"].'");';
 			$function_name = create_function('$input', $function_body);
-			// Filter the results array using the anonymous callback function.
+			// Filter the results array using the
+			// anonymous callback function.
 			$connection_pool = array_filter($connection_pool, $function_name);
 
 			if (isset($conn)) {
@@ -725,14 +734,14 @@
 
 		$connections = array();
 		if ($connection_pool !== null) {
-		    foreach($connection_pool as $conn) {
-			$connections[] = $conn["id"];
-		    }
+			foreach($connection_pool as $conn) {
+				$connections[] = $conn["id"];
+			}
 		}
 
 		$id = hash("sha1", time());
 		if (count($connections) > 0) {
-			while (in_array($id, $connections) ) {
+			while (in_array($id, $connections)) {
 				$id = hash("sha1", time());
 			}
 		}
@@ -749,7 +758,8 @@
 		return FALSE;
 	}
 
-	/* Write data through a socket; make sure that the buffer is actually flushed */
+	/* Write data through a socket; make sure that the buffer is
+	 * actually flushed */
 	function socket_flush($socket, $data) {
 		$buf = 0;
 		$bytes = strlen($data);
@@ -758,7 +768,7 @@
 			return FALSE;
 		}
 
-		while ( ($bytes - $buf) > 0 ) {
+		while (($bytes - $buf) > 0) {
 			$buf += socket_write($socket, substr($data, $buf, $bytes), $bytes - $buf);
 			//print "Buf: " . $buf . "\n";
 
@@ -776,50 +786,52 @@
 		$index = 0; // current position in the original string
 		$t = 0; // current position in the quoted string
 
-		/* Parse the original string character by character and copy it in a new buffer escaping characters */
-        while ($size < 0 ? $quoted_string[$t] : $size > 0) {
-                if ($size > 0)
-                        $size--;
-                switch ($string[$index]) {
-                	case '\n':
-                        $quoted_string[$t++] = '\\';
-                        $quoted_string[$t++] = 'n';
-                        break;
-                	case '\t':
-                        $quoted_string[$t++] = '\\';
-                        $quoted_string[$t++] = 't';
-                        break;
-                	case PLACEHOLDER:
-                        $quoted_string[$t++] = '\\';
-                        $quoted_string[$t++] = PLACEHOLDER;
-                        break;
-                	case '\\':
-                        $quoted_string[$t++] = '\\';
-                        $quoted_string[$t++] = '\\';
-                        break;
-                	case '\'':
-                        $quoted_string[$t++] = '\'';
-                       break;
-                	case '\"':
-                        $quoted_string[$t++] = '\\';
-                        $quoted_string[$t++] = '"';
-                        break;
-                	case '\0':
-                        $quoted_string[$t++] = '\\';
-                        $quoted_string[$t++] = '0';
-                        break;
-                default:
-                        $quoted_string[$t++] = $string[$index];
-                        break;
-                }
-                $index++;
-                /* also deal with binaries */
-        }
+		/* Parse the original string character by character
+		 * and copy it in a new buffer escaping characters */
+
+		while ($size < 0 ? $quoted_string[$t] : $size > 0) {
+			if ($size > 0)
+				$size--;
+			switch ($string[$index]) {
+			case '\n':
+				$quoted_string[$t++] = '\\';
+				$quoted_string[$t++] = 'n';
+				break;
+			case '\t':
+				$quoted_string[$t++] = '\\';
+				$quoted_string[$t++] = 't';
+				break;
+			case PLACEHOLDER:
+				$quoted_string[$t++] = '\\';
+				$quoted_string[$t++] = PLACEHOLDER;
+				break;
+			case '\\':
+				$quoted_string[$t++] = '\\';
+				$quoted_string[$t++] = '\\';
+				break;
+			case '\'':
+				$quoted_string[$t++] = '\'';
+				break;
+			case '\"':
+				$quoted_string[$t++] = '\\';
+				$quoted_string[$t++] = '"';
+				break;
+			case '\0':
+				$quoted_string[$t++] = '\\';
+				$quoted_string[$t++] = '0';
+				break;
+			default:
+				$quoted_string[$t++] = $string[$index];
+				break;
+			}
+			$index++;
+			/* also deal with binaries */
+		}
 
 		if (is_array($quoted_string)) {
 			$quoted_string = implode($quoted_string);
 		}
-        return $quoted_string;
+		return $quoted_string;
 	}
 
 ?>
--- a/lib/php_monetdb.php
+++ b/lib/php_monetdb.php
@@ -13,14 +13,14 @@
 	*
 	* This library relies on the native PHP mapi implementation.
 	*
-	* This file should be included by all pages that want to make use of a
- 	* database connection.
+	* This file should be included by all pages that want to make
+ 	* use of a database connection.
 	*
 	* Synopsis of the provided functions:
 	*
 	* function monetdb_connect() *
 	* function monetdb_disconnect()  *
-    * function monetdb_connected() *
+	* function monetdb_connected() *
 	* function monetdb_query($query)  *
 	* function monetdb_fetch_assoc($hdl) *
 	* function monetdb_fetch_object($hdl)
@@ -33,12 +33,14 @@
 	**/
 
 	/**
-	* php_mapi.inc is a native (socket based) php implementation of the MAPI communication protocol.
+	* php_mapi.inc is a native (socket based) php implementation
+	* of the MAPI communication protocol.
 	*/
 	require 'php_mapi.inc';
 
 	/**
-	* register a 'monetdb' extension to retain compatibility with wht Cimpl based scripts.
+	* register a 'monetdb' extension to retain compatibility with
+	* wht Cimpl based scripts.
 	*/
 
 	/**
@@ -50,11 +52,18 @@
 	 * @param string username (default is monetdb)
 	 * @param string password (default is monetdb)
 	 * @param string database to use (default is demo)
-	 * @param string hash function to use during authentication (defaults to SHA1)
+	 * @param string hash function to use during authentication
+	 *        (defaults to SHA1)
 	 * @return bool TRUE on success or FALSE on failure
 	 */
 
-	function monetdb_connect($lang = "sql", $host = "127.0.0.1", $port = 50000, $username = "monetdb", $password = "monetdb", $database = "demo", $hashfunc = "") {
+	function monetdb_connect($lang = "sql",
+		 		 $host = "127.0.0.1",
+				 $port = 50000,
+				 $username = "monetdb",
+				 $password = "monetdb",
+				 $database = "demo",
+				 $hashfunc = "") {
 	 	$options["host"] = $host;
 		$options["port"] = $port;
 
@@ -65,27 +74,32 @@
 		$options["persistent"] = FALSE;
 
 
-	    if ($hashfunc == "") {
-		    $hashfunc = "sha1";
-	    }
+		if ($hashfunc == "") {
+			$hashfunc = "sha1";
+		}
 
-	    if ($lang == "") {
-	        $lang = "sql";
-	    }
+		if ($lang == "") {
+			$lang = "sql";
+		}
 
-	    $options["hashfunc"] = $hashfunc;
-        $options["lang"]     = $lang;
+		$options["hashfunc"] = $hashfunc;
+		$options["lang"]     = $lang;
 
 		return mapi_connect_proxy($options);
 	}
 
 	/**
 	 * Opens a persistent connection to a MonetDB server.
-	 * First, when connecting, the function would first try to find a (persistent) link that's already open with the same host,
-	 * username and password. If one is found, an identifier for it will be returned instead of opening a new connection.
+	 * First, when connecting, the function would first try to
+	 * find a (persistent) link that's already open with the same
+	 * host, username and password. If one is found, an identifier
+	 * for it will be returned instead of opening a new
+	 * connection.
 	 *
-	 * Second, the connection to the SQL server will not be closed when the execution of the script ends.
-	 * Instead, the link will remain open for future use (monetdb_close() will not close links established by monetdb_pconnect()).
+	 * Second, the connection to the SQL server will not be closed
+	 * when the execution of the script ends.  Instead, the link
+	 * will remain open for future use (monetdb_close() will not
+	 * close links established by monetdb_pconnect()).
 	 *
 	 * This type of link is therefore called 'persistent'.
 	 *
@@ -95,12 +109,18 @@
 	 * @param string username (default is monetdb)
 	 * @param string password (default is monetdb)
 	 * @param string database to use (default is demo)
-	 * @param string hash function to use during authentication (defaults to SHA1)
+	 * @param string hash function to use during authentication
+	 *        (defaults to SHA1)
 	 * @return bool TRUE on success or FALSE on failure
 	 */
 
-  function monetdb_pconnect($lang = "sql", $host = "127.0.0.1", $port = 500000, $username = "monetdb", $password = "monetdb", $database = "demo", $hashfunc = "") {
-
+	function monetdb_pconnect($lang = "sql",
+		 		  $host = "127.0.0.1",
+				  $port = 500000,
+				  $username = "monetdb",
+				  $password = "monetdb",
+				  $database = "demo",
+				  $hashfunc = "") {
 	 	$options["host"] = $host;
 		$options["port"] = $port;
 
@@ -110,17 +130,17 @@
 		$options["persistent"] = TRUE;
 
 		if ($hashfunc == "") {
-		    $hashfunc = "sha1";
-	    }
+			$hashfunc = "sha1";
+		}
 
-	    if ($lang == "") {
-	        $lang = "sql";
+		if ($lang == "") {
+			$lang = "sql";
 		} else if (strstr($lang, "sql") == $lang) {
 			$lang = "sql";
 		}
 
-	    $options["hashfunc"] = $hashfunc;
-        $options["lang"]     = $lang;
+		$options["hashfunc"] = $hashfunc;
+		$options["lang"]     = $lang;
 
 		return mapi_connect_proxy($options);
 	}
@@ -130,7 +150,7 @@
 	 *
 	 * @param resource connection instance
 	 */
-	function monetdb_disconnect($conn=NULL) {
+	function monetdb_disconnect($conn = NULL) {
 		$num_args = func_num_args();
 
 		if ($num_args == 0) {
@@ -142,15 +162,15 @@
 	}
 
 	/**
-	 * Returns whether a connection to the database has been made, and has
-	 * not been closed yet.  Note that this function doesn't guarantee that
-	 * the connection is alive or usable.
+	 * Returns whether a connection to the database has been made,
+	 * and has not been closed yet.  Note that this function
+	 * doesn't guarantee that the connection is alive or usable.
 	 *
 	 * @param resource connection instance
 	 * @return bool TRUE if there is a connection, FALSE otherwise
 	 *
 	 */
-	function monetdb_connected($connection=NULL) {
+	function monetdb_connected($connection = NULL) {
 		$num_args = func_num_args();
 
 		if ($num_args == 0){
@@ -167,7 +187,7 @@
 	 * @param string the SQL query to execute
 	 * @return resource a query handle or FALSE on failure
 	 */
-	function monetdb_query($connection=NULL, $query="") {
+	function monetdb_query($connection = NULL, $query = "") {
 		$num_args = func_num_args();
 
 		if ($num_args == 1){
@@ -190,10 +210,11 @@
 	 * Returns the number of rows in the query result.
 	 *
 	 * @param resouce the query resource
-	 * @return int the number of rows in the result; FALSE if the query did not return any result set
+	 * @return int the number of rows in the result; FALSE if the
+	 *         query did not return any result set
 	 */
 	function monetdb_num_rows($hdl) {
-		if ($hdl["operation"] == Q_TABLE || $hdl["operation"] == Q_BLOCK ) {
+		if ($hdl["operation"] == Q_TABLE || $hdl["operation"] == Q_BLOCK) {
 			return $hdl["query"]["rows"];
 		} else {
 			return FALSE;
@@ -204,75 +225,74 @@
 	 * Returns the number of fields in the query result.
 	 *
 	 * @param resouce the query resource
-	 * @return int the number of fields in the result; FALSE if the query did not return any result set
+	 * @return int the number of fields in the result; FALSE if
+	 *         the query did not return any result set
 	 */
 	function monetdb_num_fields($hdl) {
-    	if ($hdl["operation"] == Q_TABLE || $hdl["operation"] == Q_BLOCK ) {
-  			return $hdl["query"]["fields"];
-  		} else {
-  			return FALSE;
-  		}
+		if ($hdl["operation"] == Q_TABLE || $hdl["operation"] == Q_BLOCK) {
+			return $hdl["query"]["fields"];
+		} else {
+			return FALSE;
+		}
 	}
 
 	/**
 	 * Returns an array containing column values as value.
-	 * For efficiency reasons the array pointer is not reset when calling monetdb_fetch_row
-	 * specifying a row value.
+	 * For efficiency reasons the array pointer is not reset when
+	 * calling monetdb_fetch_row specifying a row value.
 	 *
 	 * @param resource the query handle
 	 * @param int the position of the row to retrieve
-	 * @return array the next row in the query result as associative array or
-	 *         FALSE if no more rows exist
+	 * @return array the next row in the query result as
+	 *         associative array or FALSE if no more rows exist
 	 */
-	function monetdb_fetch_row(&$hdl, $row=-1) {
+	function monetdb_fetch_row(&$hdl, $row = -1) {
 		global $last_error;
 
-		if ($hdl["operation"] != Q_TABLE && $hdl["operation"] != Q_BLOCK ) {
+		if ($hdl["operation"] != Q_TABLE && $hdl["operation"] != Q_BLOCK) {
 			return FALSE;
 		}
 
 		if ($row == -1){
-		  // Parse the tuple and present it to the user
+			// Parse the tuple and present it to the user
 			$entry = current($hdl["record_set"]);
 
 			//advance the array of one position
 			next($hdl["record_set"]);
-
 		} else {
 			if ($row < $hdl["query"]["rows"]) {
-			  /* Parse the tuple and present it to the user*/
+				// Parse the tuple and present it to the user
 				$entry = $hdl["record_set"][$row-1];
-			}
-			else {
+			} else {
 				$last_error = "Index out of bound\n";
 				return FALSE;
 			}
 		}
 
-    if ($entry) {
-		  return php_parse_row($entry);
-	  }
+		if ($entry) {
+			return php_parse_row($entry);
+		}
 
-	  return $entry;
+		return $entry;
 	}
 
 
 	/**
-	 * Returns an associative array containing the column names as keys, and
-	 * column values as value.
+	 * Returns an associative array containing the column names as
+	 * keys, and column values as value.
 	 *
 	 * @param resource the query handle
 	 * @param int the position of the row to retrieve
-	 * @return array the next row in the query result as associative array or
-	 *         FALSE if no more rows exist
+	 * @return array the next row in the query result as
+	 *         associative array or FALSE if no more rows exist
 	 */
-	function monetdb_fetch_assoc(&$hdl, $row=-1) {
-		if ($hdl["operation"] != Q_TABLE && $hdl["operation"] != Q_BLOCK ) {
+	function monetdb_fetch_assoc(&$hdl, $row = -1) {
+		if ($hdl["operation"] != Q_TABLE && $hdl["operation"] != Q_BLOCK) {
 			return FALSE;
 		}
 
 		// first retrieve the row as an array
-		$fetched_row =  monetdb_fetch_row($hdl, $row);
+		$fetched_row = monetdb_fetch_row($hdl, $row);
 
 		if ($fetched_row == FALSE) {
 			return FALSE;
@@ -293,33 +313,33 @@
 
 
 	/**
-	 * Returns the result in the given query resource as object one row at a time.  Column
-	 * names become members of the object through which the column values
-	 * can be retrieved.
+	 * Returns the result in the given query resource as object
+	 * one row at a time.  Column names become members of the
+	 * object through which the column values can be retrieved.
 	 *
 	 * @param resource the query handle
 	 * @param int the position of the row to retrieve
-	 * @return the query result as object or FALSE if there are no more rows
+	 * @return the query result as object or FALSE if there are no
+	 *         more rows
 	 */
-	function monetdb_fetch_object(&$hdl, $row=-1)  {
-
-		if ($hdl["operation"] != Q_TABLE && $hdl["operation"] != Q_BLOCK ) {
+	function monetdb_fetch_object(&$hdl, $row = -1) {
+		if ($hdl["operation"] != Q_TABLE && $hdl["operation"] != Q_BLOCK) {
 			return FALSE;
 		}
 
-		if (($row_array =  monetdb_fetch_assoc($hdl, $row)) == FALSE) {
+		if (($row_array = monetdb_fetch_assoc($hdl, $row)) == FALSE) {
 			return FALSE;
 		}
 
 		$row_object = new stdClass();
 		if (is_array($row_array) && count($row_array) > 0) {
-			foreach ($row_array as $name=>$value) {
-		   		$name = strtolower(trim($name));
-		        if (!empty($name)) {
-		        	$row_object->$name = $value;
-		        }
-		     }
-		 }
+			foreach ($row_array as $name => $value) {
+				$name = strtolower(trim($name));
+				if (!empty($name)) {
+					$row_object->$name = $value;
+				}
+			}
+		}
 
 		return $row_object;
 	}
@@ -332,14 +352,14 @@
 	* @return string the field name, FALSE is an error occured.
 	*/
 	function monetdb_field_name(&$hdl, $field) {
-	    if (is_array($hdl) && $field >= 0) {
-            if ($hdl["operation"] == Q_TABLE || $hdl["operation"] == Q_BLOCK ) {
-                if ($hdl["header"]["fields"] != "" ) {
-                    return $hdl["header"]["fields"][$field];
-                }
-		    }
-    	}
-    	return FALSE;
+		if (is_array($hdl) && $field >= 0) {
+			if ($hdl["operation"] == Q_TABLE || $hdl["operation"] == Q_BLOCK) {
+				if ($hdl["header"]["fields"] != "") {
+					return $hdl["header"]["fields"][$field];
+				}
+			}
+		}
+		return FALSE;
 	}
 
 	/**
@@ -348,7 +368,8 @@
 	 * queries.
 	 *
 	 * @param resource the query handle
-	 * @return int the number of affected rows, FALSE if the last executed query did not affect any row.
+	 * @return int the number of affected rows, FALSE if the last
+	 *         executed query did not affect any row.
 	 */
 	function monetdb_affected_rows($hdl) {
 		if ($hdl["operation"] != Q_UPDATE) {
@@ -360,17 +381,18 @@
 
 
 	/**
-	* Check if the query handle contains a result set. Note: the result set may be empty.
+	* Check if the query handle contains a result set. Note: the
+	* result set may be empty.
 	*
 	* @param resource the query handle
 	* @return bool TRUE if the query contains a result set, FALSE otherwise.
 	*/
 	function monetdb_has_result($hdl) {
-	    if (($hdl["operation"] == Q_TABLE) || ($hdl["operation"] == Q_BLOCK)) {
-	        return TRUE;
-	    }
+		if (($hdl["operation"] == Q_TABLE) || ($hdl["operation"] == Q_BLOCK)) {
+			return TRUE;
+		}
 
-	    return FALSE;
+		return FALSE;
 	}
 
 	/**
@@ -389,9 +411,10 @@
 	 * @param resource connection instance
 	 * @param seq sequence whose next
 	 * value we want to retrieve
-	 * @return string the ID of the last tuple inserted. FALSE if an error occurs
+	 * @return string the ID of the last tuple inserted. FALSE if
+	 *         an error occurs
 	 */
-	function monetdb_insert_id($connection = NULL, $seq)  {
+	function monetdb_insert_id($connection = NULL, $seq) {
 		$num_args = func_num_args();
 
 		if ($num_args == 1) {
@@ -403,19 +426,20 @@
 			$query = "SELECT NEXT VALUE FOR ".monetdb_quote_ident($seq)."";
 			$res = monetdb_query($connection, $query);
 			$row = monetdb_fetch_assoc($result);
-            return($row[$seq]);
+			return($row[$seq]);
 		}
 
 		return FALSE;
 	}
 
 	/**
-	 * Returns a 'quoted identifier' suitable for MonetDB.
-	 * This utility function can be used in queries to for instance quote
-	 * names of tables of columns that otherwise would be a mistaken for a
-	 * keyword.
-	 * NOTE: the given string is currently not checked for validity, hence
-	 *       the output of this function may be an invalid identifier.
+	 * Returns a 'quoted identifier' suitable for MonetDB.  This
+	 * utility function can be used in queries to for instance
+	 * quote names of tables of columns that otherwise would be a
+	 * mistaken for a keyword.
+	 * NOTE: the given string is currently not checked for
+	 *       validity, hence the output of this function may be an
+	 *       invalid identifier.
 	 *
 	 * @param string the identifier to quote
 	 * @return string the quoted identifier
@@ -468,13 +492,15 @@
 
 
 	/*
-	 * These functions are not present in the original Cimpl implementation
+	 * These functions are not present in the original Cimpl
+	 * implementation
 	 */
 
 	/**
 	* Create a new savepoint ID
 	* @param resource connection instance
-	* @return bool TRUE if the ID has been correctly generated, FALSE otherwise.
+	* @return bool TRUE if the ID has been correctly generated,
+	*         FALSE otherwise.
 	*/
 	function create_savepoint(&$conn) {
 		if ($conn != NULL) {
@@ -492,7 +518,8 @@
 	/**
 	* Release a savepoint ID.
 	* @param resource connection instance
-	* @return bool TRUE if the ID has been correctly released, FALSE otherwise.
+	* @return bool TRUE if the ID has been correctly released,
+	*         FALSE otherwise.
 	*/
 	function release_savepoint(&$conn) {
 		if ($conn != NULL) {
@@ -506,7 +533,8 @@
 	/**
 	* Return the current (last generated) savepoint ID.
 	* @param resource connection instance
-	* @return string savepoint ID. I no savepoints are available, FALSE is returned.
+	* @return string savepoint ID. I no savepoints are available,
+	*         FALSE is returned.
 	*/
 	function get_savepoint(&$conn) {
 		if (count($conn["transactions"]) == 0) {
@@ -521,9 +549,10 @@
 	* Sets auto commit mode on/off
 	* @param resource connection instance
 	* @param bool TRUE to turn auto commit on, FALSE to turn it off.
-	* @return bool TRUE is auto commit mode was correctly set. FALSE otherwise.
+	* @return bool TRUE is auto commit mode was correctly
+	*         set. FALSE otherwise.
 	*/
-	function auto_commit($conn, $flag=TRUE) {
+	function auto_commit($conn, $flag = TRUE) {
 		if ($conn["socket"] != NULL) {
 			$cmd = "auto_commit " . (int) $flag;
 			mapi_write($conn["socket"], format_command($cmd));