# how to make nasty code less nasty (obtaining sqlite db handle)

- Thread: https://externals.io/message/14597
- Original archive: https://news-web.php.net/php.internals/14597
- Started by Derrell.Lipman@UnwiredUniverse.com on 2005-02-03T22:03:45+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #14597 — Derrell.Lipman@UnwiredUniverse.com — 2005-02-03T22:03:45+00:00

URL: https://externals.io/message/14597#14597

I've written an extension which needs to accept a Resource which is an SQLite
database handle, and call a C function which will be using that db handle to
issue queries.  What I've done works, but is kinda nasty because there doesn't
appear to be a clean way to obtain that database handle nor, from what I can
tell, determine the resource type to expect.  Is there a nicer way of doing
this?

PHP_FUNCTION(my_PHP_function)
{
    int             id;
    int             dataLen;
    int             resourceType;
    char *          pData;
    zval *          zdb;
    void *          hDB;
    void **         phDB;

    /* Ensure we got the correct number of parameters */
    if (ZEND_NUM_ARGS() != 2)
    {
        WRONG_PARAM_COUNT;
    }

    /* Retrieve the parameters */
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                              "rs",
                              &zdb, &pData, &dataLen) == FAILURE)
    {
        return;
    }

    /*
     * Voodoo to retrieve the sqlite database handle from the resource.
     * How do I validate that resourceType is reasonable?
     */
    id = zdb->value.lval;
    if ((phDB = zend_list_find(id, &resourceType)) == NULL)
    {
        return;
    }

    /*
     * This is nasty too.  We "know" that the first field in the private
     * structure is the database handle.  Just extract it.
     */
    hDB = *phDB;

    RETURN_STRING(my_C_function(hDB, pData), TRUE);
}


Thanks,

Derrell

---

## #14628 — Wez Furlong — 2005-02-04T01:36:58+00:00

URL: https://externals.io/message/14597#14628 · In reply to: #14597

There is no official way to do this kind of thing between arbitrary extensions.
When extensions that are written to share this information, they
typically export an XXX_API function that you can link against to
fetch the data.

What you've done works, so that's fine, although there is a risk of
crashing if someone passes the wrong type of resource in.  If you're
thinking of distributing your extension, we could add a more official
API for you to use, although that doesn't exist in older versions of
the extension, etc. etc.

Some time back, I proposed a framework (and even a patch IIRC) that
allowed this kind of thing at run-time; despite "we the extension
developers" liking the idea, there was some negative feedback from
andi/zeev so it never went further.  Search the php-dev archives for
my name and "interfaces" for more on that if you're interesting in
this seeing light in PHP 5.2.

--Wez.

On Thu, 03 Feb 2005 17:03:45 -0500, Derrell.Lipman@unwireduniverse.com
<Derrell.Lipman@unwireduniverse.com> wrote:
> I've written an extension which needs to accept a Resource which is an SQLite
> database handle, and call a C function which will be using that db handle to
> issue queries.  What I've done works, but is kinda nasty because there doesn't
> appear to be a clean way to obtain that database handle nor, from what I can
> tell, determine the resource type to expect.  Is there a nicer way of doing
> this?
> 
> PHP_FUNCTION(my_PHP_function)
> {
>     int             id;
>     int             dataLen;
>     int             resourceType;
>     char *          pData;
>     zval *          zdb;
>     void *          hDB;
>     void **         phDB;
> 
>     /* Ensure we got the correct number of parameters */
>     if (ZEND_NUM_ARGS() != 2)
>     {
>         WRONG_PARAM_COUNT;
>     }
> 
>     /* Retrieve the parameters */
>     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
>                               "rs",
>                               &zdb, &pData, &dataLen) == FAILURE)
>     {
>         return;
>     }
> 
>     /*
>      * Voodoo to retrieve the sqlite database handle from the resource.
>      * How do I validate that resourceType is reasonable?
>      */
>     id = zdb->value.lval;
>     if ((phDB = zend_list_find(id, &resourceType)) == NULL)
>     {
>         return;
>     }
> 
>     /*
>      * This is nasty too.  We "know" that the first field in the private
>      * structure is the database handle.  Just extract it.
>      */
>     hDB = *phDB;
> 
>     RETURN_STRING(my_C_function(hDB, pData), TRUE);
> }
> 
> Thanks,
> 
> Derrell

---

## #14636 — Derrell.Lipman@UnwiredUniverse.com — 2005-02-04T03:56:04+00:00

URL: https://externals.io/message/14597#14636 · In reply to: #14628

Wez Furlong <kingwez@gmail.com> writes:

> There is no official way to do this kind of thing between arbitrary
> extensions.  When extensions that are written to share this information,
> they typically export an XXX_API function that you can link against to fetch
> the data.
>
> What you've done works, so that's fine, although there is a risk of
> crashing if someone passes the wrong type of resource in.  If you're
> thinking of distributing your extension, we could add a more official
> API for you to use, although that doesn't exist in older versions of
> the extension, etc. etc.

Thanks, Wez.  My extension will likely never be used other than with
locally-written code.  My biggest concern is making use of the incestual
knowledge of the private php_sqlite_db structure and that the database handle
is the first field in it.  That structure may change in the future.

My use of this is as a function used in a query -- sqlite_create_function() is
used to allow a call to a php function which passes the database handle to the
extension function.  Prior to today, my extension had its own private database
handle that it initialized on the MINIT function, but I was causing a deadlock
by calling this function from within a query in PHP which called the extension
which tried to issue additional queries using my private database handle in
the extension.  Passing in the php-derived handle is the only way of
eliminating that deadlock.

I wonder whether I'm really alone in wanting access to a database handle for
extensions.  It would seem appropriate to provide access to the C-level
database handle given a PHP resources.  That would eliminate the need to base
an extenion on insestual knowledge of the database-specific extension.

Thanks again,

Derrell

---

## #14645 — Andrei Zmievski — 2005-02-04T21:39:49+00:00

URL: https://externals.io/message/14597#14645 · In reply to: #14628

> Some time back, I proposed a framework (and even a patch IIRC) that
> allowed this kind of thing at run-time; despite "we the extension
> developers" liking the idea, there was some negative feedback from
> andi/zeev so it never went further.  Search the php-dev archives for
> my name and "interfaces" for more on that if you're interesting in
> this seeing light in PHP 5.2.

I'd definitely be interested in seeing the extension dependency
framework in 5.2.

- Andrei

---

## #14646 — Rasmus Lerdorf — 2005-02-04T21:46:47+00:00

URL: https://externals.io/message/14597#14646 · In reply to: #14645

Andrei Zmievski wrote:
> 
> 
> > […]
> 
> 
> I'd definitely be interested in seeing the extension dependency
> framework in 5.2.

We need it for the ext/libxml mess, for example.

-Rasmus

---

## #14647 — Wez Furlong — 2005-02-04T21:51:38+00:00

URL: https://externals.io/message/14597#14647 · In reply to: #14645

The extension deps framework (that orders the initialization done in
internal_functions.c) is already in HEAD; I was talking about the
extension "interfaces" idea I proposed a fairly long time ago now
(long enough that I can't remember how long ago). I remember you
showing interest in that at that time.

--Wez.

> I'd definitely be interested in seeing the extension dependency
> framework in 5.2.
> 
> - Andrei

---

## #14648 — l0t3k — 2005-02-04T23:12:27+00:00

URL: https://externals.io/message/14597#14648 · In reply to: #14647

i found this

http://www.zend.com/lists/php-dev/200204/msg01083.html



"Wez Furlong" <kingwez@gmail.com> wrote in message 
news:4e89b42605020413515e6448e6@mail.gmail.com...
> The extension deps framework (that orders the initialization done in
> internal_functions.c) is already in HEAD; I was talking about the
> extension "interfaces" idea I proposed a fairly long time ago now
> (long enough that I can't remember how long ago). I remember you
> showing interest in that at that time.
>
> --Wez.
