It's been a while since I sent my original message, and nobody has
replied, so I'm going to assume that I was simply too brief in my
explanation (which, looking back, seems quite likely).

Basically, I have a library, written in C, that uses a MySQL 5+
database. I've already written an extension for PHP to interface with my
library, but I would like to have the ability to share a connection with
mysqli. Here is a short snippet to show my extension (with this patch)
works in PHP:

$db = new mysqli('localhost', 'username', 'password', 'rdf');
$ts3db = new tstore($db);
$query = 'SELECT ?person1, ?person2 WHERE { ?person1 ' .
    '<http://xmlns.com/foaf/0.1/knows> ?person2 }';

if ( ($res = $ts3db->query($query)) ) {
  // A successful result is a DOMDocument.
  echo $res->saveXML();
}

Since now headers are installed from the mysqli extension, I currently
have no way of getting a pointer to the MYSQL struct from the mysqli
object passed to the tstore construct without copying code from the
mysqli extension to my own, which obviously wouldn't be very robust to
changes in the mysqli extension, or asking the user to point to the
mysqli header file, which would only be available if they are compiling
from source. My solution is to add a function to the mysqli extension
which will accept a mysqli object zval and return a pointer to the MYSQL
connection:

MYSQL * mysqli_get_connection(zval *mysql_link,
    INTERNAL_FUNCTION_PARAMETERS);

Once this function exists in the mysqli extension, I can copy the
function prototype into my extension. I am confident that the prototype
won't have to be changed in the future, mainly because of how simple it is.

In order to properly detect failures, I've had to make a small
modification outside of this new function which should have no impact on
anything else, as well as a few minor changes to my function. These
changes were not in my original patch, and the updated version is attached.

If anyone has any questions, please let me know.

-Evan Nemerson



Evan Nemerson wrote:
> Hi everyone,
> 
> I'm working on an extension for an RDF triple store that is built on top
> of MySQL 5+, and I'd like to be able to share the MySQL connection with
> mysqli. Unfortunately, since the mysqli header isn't installed, I can't
> seem to find a way that could withstand changes in mysqli.
> 
> What I've come up with is to create a trivial function which will take a
> (zval *) and return a (MYSQL *). I know it's a bit of a hack, but I was
> trying to be as unintrusive as possible.
> 
> Patch is attached.
> 
> -Evan Nemerson
Index: php_mysqli.h
===================================================================
RCS file: /repository/php-src/ext/mysqli/php_mysqli.h,v
retrieving revision 1.60
diff -u -r1.60 php_mysqli.h
--- php_mysqli.h	9 May 2006 11:27:20 -0000	1.60
+++ php_mysqli.h	11 Sep 2006 19:54:27 -0000
@@ -221,11 +221,11 @@
   		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't fetch %s", intern->zo.ce->name);\
   		RETURN_NULL();\
   	}\
-	__ptr = (__type)my_res->ptr; \
 	if (__check && my_res->status < __check) { \
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid object or resource %s\n", intern->zo.ce->name); \
 		RETURN_NULL();\
 	}\
+	__ptr = (__type)my_res->ptr; \
 }
 
 #define MYSQLI_SET_STATUS(__id, __value) \
Index: mysqli.c
===================================================================
RCS file: /repository/php-src/ext/mysqli/mysqli.c,v
retrieving revision 1.97
diff -u -r1.97 mysqli.c
--- mysqli.c	27 Jul 2006 10:53:03 -0000	1.97
+++ mysqli.c	11 Sep 2006 19:54:29 -0000
@@ -331,6 +331,16 @@
 }
 /* }}} */
 
+/* {{{ MYSQL * mysqli_get_connection(zval *mysql_link TSRMLS_DC) */
+MYSQL * mysqli_get_connection(zval *mysql_link, INTERNAL_FUNCTION_PARAMETERS) {
+	MY_MYSQL *mysql = NULL;
+
+	MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
+
+	return mysql ? mysql->mysql : NULL;
+}
+/* }}} */
+
 static union _zend_function *php_mysqli_constructor_get(zval *object TSRMLS_DC)
 {
 	mysqli_object *obj = (mysqli_object *)zend_objects_get_address(object TSRMLS_CC);

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to