Re: [PATCH] DB_DataObject support for PostgreSQL casts

From: Date: Fri, 03 Oct 2003 07:39:23 +0000
Subject: Re: [PATCH] DB_DataObject support for PostgreSQL casts
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-22340@lists.php.net to get a copy of this message
Could you have a look at supporting this by a) generator writes: fieldname = bytea (eg. the field type.) b) DataObjects doing $suffix = ''; if (is_string($v)) { $suffix='::'.$v; $k = DB_DATAOBJECT_STR; } Regards Alan Ian Eure wrote:
On Thursday October 2 2003 12:16 pm, Ian Eure wrote:
Enclosed is a patch to allow casting fields for PostgreSQL. In order to handle bytea fields (akin to MySQL's BLOB type) in PostgreSQL 7.2 and 7.2.1, insert/updates must have '::bytea' appended to the value. This adds a class var, $_casts, which contains the casts on a per-field basis and an addCast() method. Patch is attached.
Fixed patch is attached, array_key_exists() was called before checking if $this->_cast was an array. ------------------------------------------------------------------------ --- DataObject.php.orig Thu Oct 2 12:00:11 2003 +++ DataObject.php Thu Oct 2 15:10:03 2003 @@ -721,7 +721,11 @@
             }
              if ($v & DB_DATAOBJECT_STR) {
-                $rightq .= $__DB->quote($this->$k) . " ";
+                $rightq .= $__DB->quote($this->$k);
+                if (is_array($this->_cast) && array_key_exists($k, $this->_cast)) {
+                    $rightq .= '::'.$this->_cast[$k];
+                }
+                $rightq .=  " ";
                 continue;
             }
             if (is_numeric($this->$k)) {
@@ -833,7 +837,11 @@
             }
              if ($v & DB_DATAOBJECT_STR) {
-                $settings .= $k .' = '. $__DB->quote($this->$k) . ' ';
+                $settings .= $k .' = '. $__DB->quote($this->$k);
+                if (is_array($this->_cast) && array_key_exists($k, $this->_cast)) {
+                    $settings .= '::'.$this->_cast[$k];
+                }
+                $settings .=  ' ';
                 continue;
             }
             if (is_numeric($this->$k)) {
@@ -1069,6 +1077,21 @@
         return substr($__DB->quote($string),1,-1);
     }
 +    /**
+     * Add a cast type for PostgreSQL
+     *
+     * The field $field will get '::{$type}' appended to it on insert/update
+     *
+     * @param  string  $field The field to cast
+     * @param  string  $type The type to cast $field as
+     * @access public
+     * @return void
+     */
+    function addCast($field, $type)
+    {
+        $this->_cast[$field] = $type;
+    }
+
     /* ==================================================== */
     /*        Major Private Vars                            */
     /* ==================================================== */
@@ -1153,6 +1176,14 @@
      * @var     integer
      */
     var $_DB_resultid; // database result object
+
+    /**
+     * PostgreSQL casts
+     *
+     * @access  private
+     * @var     array
+     */
+    var $_cast;
       /* =========================================================== */
-- Can you help out? Need Consulting Services or Know of a Job? http://www.akbkhome.com

« previous php.pear.dev (#22340) next »