Re: example and the patch file
| From: | Wez Furlong | Date: | Mon, 24 Oct 2005 15:01:07 +0000 |
| Subject: | Re: example and the patch file | ||
| References: | 1 2 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-3143@lists.php.net to get a copy of this message | ||
It's a pretty small patch, but I'm hesitant to add this, because XML
can mean different things to different people. For example, you can
guarantee that not everyone will want their rows encapsulated as <row>
elements. The next logical step would be to make that configurable,
and then PDO is suddenly turned into a string building class.
Generally speaking, the PHP way is to push that kind of string
building out to the PHP script, where it's much less error prone and
much simpler to accomplish.
Performance here isn't likely to be an issue; the cost of building the
string in the PHP script is going to be small compared to the cost of
fetching the data and then running it through XSLT. I don't think the
perf gain is going to be sufficient to warrant increased maintenance
costs at the extension level.
--Wez.
On 10/24/05, Justin Wu <ezdevelop@gmail.com> wrote:
> Here is the example:
> ---------------------
> <?php
> $stmt = $x->query("SELECT id, title FROM news");
> echo $stmt->fetchAll(PDO_FETCH_XML);
> ?>
> Here is the output:
> -------------------
> <resultset queryString="SELECT id, title FROM news">
> <row>
> <id>1</id>
> <title>hello world</title>
> </row>
> <row>
> <id>2</id>
> <title> foo bar </title>
> </row>
> ......
> </resultset>
>
> Here is the patch file (against PDO-1.0RC1):
> ---------------------
> diff -Nur PDO-1.0RC1/pdo.c pdo/pdo.c
> --- PDO-1.0RC1/pdo.c Sun Sep 11 13:27:30 2005
> +++ pdo/pdo.c Mon Oct 24 19:18:38 2005
> @@ -320,6 +320,7 @@
> REGISTER_LONG_CONSTANT("PDO_FETCH_SERIALIZE",(long)PDO_FETCH_SERIALIZE,
> CONST_CS|CONST_PERSISTENT);
> #endif
> REGISTER_LONG_CONSTANT("PDO_FETCH_NAMED",(long)PDO_FETCH_NAMED,
> CONST_CS|CONST_PERSISTENT);
> + REGISTER_LONG_CONSTANT("PDO_FETCH_XML", (long)PDO_FETCH_XML,
> CONST_CS|CONST_PERSISTENT);
>
> REGISTER_LONG_CONSTANT("PDO_ATTR_AUTOCOMMIT", (long)PDO_ATTR_AUTOCOMMIT,
> CONST_CS|CONST_PERSISTENT);
> REGISTER_LONG_CONSTANT("PDO_ATTR_PREFETCH", (long)PDO_ATTR_PREFETCH,
> CONST_CS|CONST_PERSISTENT);
> diff -Nur PDO-1.0RC1/pdo_stmt.c pdo/pdo_stmt.c
> --- PDO-1.0RC1/pdo_stmt.c Sun Sep 11 03:32:16 2005
> +++ pdo/pdo_stmt.c Mon Oct 24 19:19:39 2005
> @@ -36,6 +36,7 @@
> #include "zend_exceptions.h"
> #include "zend_interfaces.h"
> #include "php_memory_streams.h"
> +#include "ext/standard/php_smart_str.h"
>
> #if COMPILE_DL_PDO
> /* {{{ content from zend_arg_defs.c:
> @@ -743,6 +744,7 @@
> int flags = how & PDO_FETCH_FLAGS, idx, old_arg_count;
> zend_class_entry * ce, * old_ce;
> zval grp_val, *grp, **pgrp, *retval, *old_ctor_args;
> + smart_str xmlstr = {0};
>
> how = how & ~PDO_FETCH_FLAGS;
> if (how == PDO_FETCH_USE_DEFAULT) {
> @@ -846,6 +848,11 @@
> }
> }
> break;
> +
> + case PDO_FETCH_XML:
> + convert_to_string(return_value);
> + smart_str_appends(&xmlstr, "<row>");
> + break;
>
>
> default:
> @@ -969,6 +976,16 @@
> stmt->fetch.func.values[idx] = val;
> stmt->fetch.cls.fci.params[idx] = &stmt->fetch.func.values[idx];
> break;
> +
> + case PDO_FETCH_XML:
> + smart_str_appends(&xmlstr, "<");
> + smart_str_appendl(&xmlstr, stmt->columns[i].name,
> stmt->columns[i].namelen);
> + smart_str_appends(&xmlstr, ">");
> + smart_str_appendl(&xmlstr, Z_STRVAL_P(val), Z_STRLEN_P(val));
> + smart_str_appends(&xmlstr, "</");
> + smart_str_appendl(&xmlstr, stmt->columns[i].name,
> stmt->columns[i].namelen);
> + smart_str_appends(&xmlstr, ">");
> + break;
>
> default:
> zval_ptr_dtor(&val);
> @@ -1020,6 +1037,13 @@
> zval_ptr_dtor(&stmt->fetch.func.values[idx]);
> }
> break;
> +
> + case PDO_FETCH_XML:
> + smart_str_appends(&xmlstr, "</row>");
> + smart_str_0(&xmlstr);
> + ZVAL_STRINGL(return_value, xmlstr.c, xmlstr.len, 1);
> + smart_str_free(&xmlstr);
> + break;
>
> default:
> break;
> @@ -1231,6 +1255,7 @@
> zend_class_entry *old_ce;
> zval *old_ctor_args, *ctor_args = NULL;
> int error = 0, old_arg_count;
> + smart_str xmlstr = {0};
>
> if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lzz",
> &how, &arg2, &ctor_args)) {
> RETURN_FALSE;
> @@ -1318,7 +1343,6 @@
> error = 1;
> }
> }
> -
> if (!error) {
> PDO_STMT_CLEAR_ERR();
> MAKE_STD_ZVAL(data);
> @@ -1334,7 +1358,21 @@
> }
> }
> if (!error) {
> - if ((how & PDO_FETCH_GROUP)) {
> + if ((how & PDO_FETCH_XML)) {
> + convert_to_string(return_value);
> + smart_str_appends(&xmlstr, "<resultset queryString=\"");
> + smart_str_appendl(&xmlstr, stmt->query_string, stmt->query_stringlen);
> + smart_str_appends(&xmlstr, "\">");
> + do {
> + smart_str_appends(&xmlstr, Z_STRVAL_P(data));
> + MAKE_STD_ZVAL(data);
> + } while (do_fetch(stmt, TRUE, data, how, PDO_FETCH_ORI_NEXT, 0, 0
> TSRMLS_CC));
> +
> + smart_str_appends(&xmlstr, "</resultset>");
> + smart_str_0(&xmlstr);
> + ZVAL_STRINGL(return_value, xmlstr.c, xmlstr.len, 1);
> + smart_str_free(&xmlstr);
> + } else if ((how & PDO_FETCH_GROUP)) {
> do {
> MAKE_STD_ZVAL(data);
> } while (do_fetch(stmt, TRUE, data, how, PDO_FETCH_ORI_NEXT, 0,
> return_all TSRMLS_CC));
> diff -Nur PDO-1.0RC1/php_pdo_driver.h pdo/php_pdo_driver.h
> --- PDO-1.0RC1/php_pdo_driver.h Wed Jul 20 11:38:34 2005
> +++ pdo/php_pdo_driver.h Mon Oct 24 19:18:29 2005
> @@ -90,6 +90,7 @@
> PDO_FETCH_INTO, /* fetch row into an existing object */
> PDO_FETCH_FUNC, /* fetch into function and return its result */
> PDO_FETCH_NAMED, /* like PDO_FETCH_ASSOC, but can handle duplicate
> names */
> + PDO_FETCH_XML, /* fetch row into xml format */
> PDO_FETCH__MAX /* must be last */
> };
>
>
> --
> PECL development discussion Mailing List (http://pecl.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>