[Patch]
| From: | Craig Larmer | Date: | Thu, 17 Jul 2003 21:09:42 +0000 |
| Subject: | [Patch] | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-18355@lists.php.net to get a copy of this message | ||
Hi,
Here is a little patch I'm using for the DB package that has proved very
handy for me and may be useful to others out there.
The patch allows you to prefix table_fields with one of the !, & or ?
placeholder characters when creating arrays for use with autoPrepare and
autoExecute. This gives these functions a lot more flexibility, in
particular the ! placeholder can prefix field_names where you want to
use built-in db functions (such as sysdate on Oracle or now() on
MySQL).
I've snipped a bit of code from my project that you can read over which
shows the now() function of mysql being used by autoExecute.
I'm pretty sure this little change won't break anything as it will
default to a ? if there's no prefix. I'm assuming no databases allow
punctuation in a valid field_name because this patch would cause
problems then. I've only tested it on Oracle and MySQL and have had no
problems.
If you need some more info then drop me a line at craig@lapooh.plus.com.
Cheers,
CL
Craig Larmer
craig@lapooh.plus.com
? DB.diff Index: DB/common.php =================================================================== RCS file: /repository/pear/DB/DB/common.php,v retrieving revision 1.21 diff -u -r1.21 common.php --- DB/common.php 20 Jun 2003 20:41:30 -0000 1.21 +++ DB/common.php 17 Jul 2003 20:47:41 -0000 @@ -510,8 +510,15 @@ $names .= ','; $values .= ','; } - $names .= $value; - $values .= '?'; + //if first character in field_name is a special placeholder then use that as substitute in SQL query + //this allows greater flexibility for the autoPrepare/autoExecute functions + if (substr($value,0,1)=='!' || substr($value,0,1)=='?' || substr($value,0,1)=='&') { + $names .= substr($value,1); + $values .= substr($value,0,1); + } else { + $names .= $value; + $values .= '?'; + } } return "INSERT INTO $table ($names) VALUES ($values)"; break; @@ -523,7 +530,13 @@ } else { $set .= ','; } - $set .= "$value = ?"; + //if first character in field_name is a special placeholder then use that as substitute in SQL query + //this allows greater flexibility for the autoPrepare/autoExecute functions + if (substr($value,0,1)=='!' || substr($value,0,1)=='?' || substr($value,0,1)=='&') { + $set .= substr($value,1).' = '.substr($value,0,1); + } else { + $set .= "$value = ?"; + } } $sql = "UPDATE $table SET $set"; if ($where) { Attachment: [application/x-php] code_example.php
? DB.diff Index: DB/common.php =================================================================== RCS file: /repository/pear/DB/DB/common.php,v retrieving revision 1.21 diff -u -r1.21 common.php --- DB/common.php 20 Jun 2003 20:41:30 -0000 1.21 +++ DB/common.php 17 Jul 2003 20:47:41 -0000 @@ -510,8 +510,15 @@ $names .= ','; $values .= ','; } - $names .= $value; - $values .= '?'; + //if first character in field_name is a special placeholder then use that as substitute in SQL query + //this allows greater flexibility for the autoPrepare/autoExecute functions + if (substr($value,0,1)=='!' || substr($value,0,1)=='?' || substr($value,0,1)=='&') { + $names .= substr($value,1); + $values .= substr($value,0,1); + } else { + $names .= $value; + $values .= '?'; + } } return "INSERT INTO $table ($names) VALUES ($values)"; break; @@ -523,7 +530,13 @@ } else { $set .= ','; } - $set .= "$value = ?"; + //if first character in field_name is a special placeholder then use that as substitute in SQL query + //this allows greater flexibility for the autoPrepare/autoExecute functions + if (substr($value,0,1)=='!' || substr($value,0,1)=='?' || substr($value,0,1)=='&') { + $set .= substr($value,1).' = '.substr($value,0,1); + } else { + $set .= "$value = ?"; + } } $sql = "UPDATE $table SET $set"; if ($where) { Attachment: [application/x-php] code_example.php