php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9541 New Feature Patch
Submitted: 2001-03-04 01:16 UTC Modified: 2001-05-05 21:37 UTC
From: dirk at rackspace dot com Assigned:
Status: Closed Package: PostgreSQL related
PHP Version: 4.0.4pl1 OS: RedHat 6.2
Private report: No CVE-ID: None
 [2001-03-04 01:16 UTC] dirk at rackspace dot com
Hi - I ran into the problem that the notices that are sent from the postgres backend are being ignored by php. Most of the time this doesn't matter, but it would be nice to be able to profile an application by running explains (which show sql cost) on every  sql statement that is executed that way you can find where your problems are as you develop the software.  Below is a patch to add this php4 (Although I don't have a cvs I account - I did built it against the 200103031945 snap shot so it should be easy to integrate into the source tree) Can you please add this before the next release :)

new php function - I wasn't sure how to document this in the code so here is is in the bug report

pg_last_notice(int connection)
returns either an empty string or the contents of the last notice sent  out by the database.
This can be useful to get the output from the explain command.

see patch below


--- php4-200103031945/ext/pgsql/pgsql.c	Mon Feb 26 00:45:27 2001
+++ php4-200103031945/ext/pgsql/pgsql.c.notice	Sat Mar  3 23:40:55 2001
@@ -46,6 +46,7 @@
 	PHP_FE(pg_pconnect,		NULL)
 	PHP_FE(pg_close,		NULL)
 	PHP_FE(pg_cmdtuples,	NULL)
+	PHP_FE(pg_last_notice,	NULL)
 	PHP_FE(pg_dbname,		NULL)
 	PHP_FE(pg_errormessage,	NULL)
 	PHP_FE(pg_trace,		NULL)
@@ -146,12 +147,31 @@
 	PQfinish(link);
 	PGG(num_persistent)--;
 	PGG(num_links)--;
+	if (PGG(last_notice) != NULL) 
+	{
+		efree(PGG(last_notice));
+	}
 }
 
+/* This function is used to silence the notice messages sent by the postgres back end. This can be useful when you know you are going to trigger a notice and don't care 
+ */
 static void _be_quiet(void * arg, const char * message)
 {
 }
 
+/*This function is used to store the last notice for later retreval
+ */
+static void _store_notice(void * arg, const char * message)
+{
+	char *copy_of_message = NULL;
+
+	if (PGG(last_notice) != NULL) 
+	{
+		efree(PGG(last_notice));
+	}
+	PGG(last_notice) = estrdup(message);
+}
+
 static int _rollback_transactions(zend_rsrc_list_entry *rsrc)
 {
 	PGconn *link = (PGconn *)rsrc->ptr;
@@ -194,6 +214,7 @@
 static void php_pgsql_init_globals(PGLS_D)
 {
 	PGG(num_persistent) = 0;
+	PGG(last_notice) = NULL;
 }
 
 PHP_MINIT_FUNCTION(pgsql)
@@ -465,6 +486,9 @@
 	}
 	efree(hashed_details);
 	php_pgsql_set_default_link(return_value->value.lval);
+	/* Set the notice handler so we can keep notices for later*/
+	PQsetNoticeProcessor(pgsql, _store_notice, NULL);
+
 }
 
 
@@ -854,6 +878,20 @@
 }
 /* }}} */
 
+/* {{{ proto int pg_last_notice(int connection)
+   Returns the last notice set by the backend */
+PHP_FUNCTION(pg_last_notice)
+{
+	if (PGG(last_notice) == NULL) 
+	{
+		RETURN_FALSE;
+	}
+	else 
+	{	
+		RETURN_STRING(PGG(last_notice),0);
+	}
+}
+/* }}} */
 
 char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list)
 {
--- php4-200103031945/ext/pgsql/php_pgsql.h	Mon Feb 26 00:45:27 2001
+++ php4-200103031945/ext/pgsql/php_pgsql.h.notice	Sat Mar  3 23:40:55 2001
@@ -64,6 +64,7 @@
 PHP_FUNCTION(pg_numrows);
 PHP_FUNCTION(pg_numfields);
 PHP_FUNCTION(pg_cmdtuples);
+PHP_FUNCTION(pg_last_notice);
 PHP_FUNCTION(pg_fieldname);
 PHP_FUNCTION(pg_fieldsize);
 PHP_FUNCTION(pg_fieldtype);
@@ -119,6 +120,7 @@
 	long max_links,max_persistent;
 	long allow_persistent;
 	int le_lofp,le_string;
+	char *last_notice;
 } php_pgsql_globals;
 
 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-17 14:18 UTC] dirk at rackspace dot com
Is this going to make it into 4.0.5?
 [2001-05-05 21:36 UTC] rasmus@php.net
It is now in CVS.  Well, not exactly your patch, but there is a pg_last_notice() function.  Probably won't be in 4.0.6 either, but definitely in 4.0.7.
 [2001-05-05 21:37 UTC] rasmus@php.net
forgot to close
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 22:01:30 2024 UTC