[Solar-svn] Revision 3284

pmjones at solarphp.com pmjones at solarphp.com
Thu Jul 31 10:25:45 CDT 2008


Solar_Sql_Adapter: [REF] Refactor prepare-and-bind to their own methods.


Modified: trunk/Solar/Sql/Adapter.php
===================================================================
--- trunk/Solar/Sql/Adapter.php	2008-07-30 15:00:08 UTC (rev 3283)
+++ trunk/Solar/Sql/Adapter.php	2008-07-31 15:25:45 UTC (rev 3284)
@@ -540,12 +540,16 @@
         // begin the profile time
         $time = microtime(true);
         
-        // prepare the statment
+        // prepre the statement and bind data to it
+        $prep = $this->_prepare($stmt);
+        $this->_bind($prep, $data);
+        
+        // now try to execute
         try {
-            $obj = $this->_pdo->prepare($stmt);
+            $prep->execute();
         } catch (PDOException $e) {
             throw $this->_exception(
-                'ERR_PREPARE_FAILED',
+                'ERR_QUERY_FAILED',
                 array(
                     'pdo_code'  => $e->getCode(),
                     'pdo_text'  => $e->getMessage(),
@@ -560,53 +564,31 @@
             );
         }
         
-        // was data passed for binding?
-        if ($data) {
-            
-            // find all :placeholder matches.  note that this is a little
-            // brain-dead; it will find placeholders in literal text, which
-            // will cause errors later.  so in general, you should *either*
-            // bind at query time *or* bind as you go, not both.
-            preg_match_all(
-                "/\W:([a-zA-Z_][a-zA-Z0-9_]+?)\W/m",
-                $stmt . "\n",
-                $matches
-            );
-            
-            // bind values to placeholders, repeating as needed
-            $repeat = array();
-            foreach ($matches[1] as $key) {
-                
-                // only attempt to bind if the data key exists.
-                // this allows for nulls and empty strings.
-                if (! array_key_exists($key, $data)) {
-                    // skip it
-                    continue;
-                }
-            
-                // what does PDO expect as the placeholder name?
-                if (empty($repeat[$key])) {
-                    // first time is ":foo"
-                    $repeat[$key] = 1;
-                    $name = $key;
-                } else {
-                    // repeated times of ":foo" are treated by PDO as
-                    // ":foo2", ":foo3", etc.
-                    $repeat[$key] ++;
-                    $name = $key . $repeat[$key];
-                }
-                
-                // bind the value to the placeholder name
-                $obj->bindValue($name, $data[$key]);
-            }
-        }
+        // retain the profile data?
+        $this->_addProfile($time, $prep->queryString, $data);
         
-        // now try to execute
+        // done!
+        return $prep;
+    }
+    
+    /**
+     * 
+     * Prepares an SQL query as a PDOStatement object.
+     * 
+     * @param string $stmt The text of the SQL statement, optionally with
+     * named placeholders.
+     * 
+     * @return PDOStatement
+     * 
+     */
+    protected function _prepare($stmt)
+    {
+        // prepare the statment
         try {
-            $obj->execute();
+            $prep = $this->_pdo->prepare($stmt);
         } catch (PDOException $e) {
             throw $this->_exception(
-                'ERR_QUERY_FAILED',
+                'ERR_PREPARE_FAILED',
                 array(
                     'pdo_code'  => $e->getCode(),
                     'pdo_text'  => $e->getMessage(),
@@ -621,11 +603,61 @@
             );
         }
         
-        // retain the profile data?
-        $this->_addProfile($time, $obj->queryString, $data);
+        return $prep;
+    }
+    
+    /**
+     * 
+     * Binds data as values into a prepared PDOStatment.
+     * 
+     * @param PDOStatement $prep The prepared PDOStatement.
+     * 
+     * @return void
+     * 
+     */
+    protected function _bind($prep, $data)
+    {
+        // was data passed for binding?
+        if (! $data) {
+            return;
+        }
+            
+        // find all :placeholder matches.  note that this is a little
+        // brain-dead; it will find placeholders in literal text, which
+        // will cause errors later.  so in general, you should *either*
+        // bind at query time *or* bind as you go, not both.
+        preg_match_all(
+            "/\W:([a-zA-Z_][a-zA-Z0-9_]+?)\W/m",
+            $stmt . "\n",
+            $matches
+        );
         
-        // done!
-        return $obj;
+        // bind values to placeholders, repeating as needed
+        $repeat = array();
+        foreach ($matches[1] as $key) {
+            
+            // only attempt to bind if the data key exists.
+            // this allows for nulls and empty strings.
+            if (! array_key_exists($key, $data)) {
+                // skip it
+                continue;
+            }
+        
+            // what does PDO expect as the placeholder name?
+            if (empty($repeat[$key])) {
+                // first time is ":foo"
+                $repeat[$key] = 1;
+                $name = $key;
+            } else {
+                // repeated times of ":foo" are treated by PDO as
+                // ":foo2", ":foo3", etc.
+                $repeat[$key] ++;
+                $name = $key . $repeat[$key];
+            }
+            
+            // bind the value to the placeholder name
+            $prep->bindValue($name, $data[$key]);
+        }
     }
     
     /**




More information about the Solar-svn mailing list