[Solar-svn] Revision 3296

pmjones at solarphp.com pmjones at solarphp.com
Thu Jul 31 17:25:52 CDT 2008


Solar_Sql_Adapter: [ADD] Method _setup() for follow-on to the constructor in extended classes.

Solar_Sql_Adapter_MysqlReplicated: [ADD] Method _setup() and property $_slaves to hold the merged connection information for slave servers.


Modified: trunk/Solar/Sql/Adapter/MysqlReplicated.php
===================================================================
--- trunk/Solar/Sql/Adapter/MysqlReplicated.php	2008-07-31 22:10:52 UTC (rev 3295)
+++ trunk/Solar/Sql/Adapter/MysqlReplicated.php	2008-07-31 22:25:51 UTC (rev 3296)
@@ -87,12 +87,21 @@
     
     /**
      * 
+     * Array of slave connection parameters for DSNs.
+     * 
+     * @var array
+     * 
+     */
+    protected $_slaves;
+    
+    /**
+     * 
      * Which slave key the [[$_dsn]] property was built from.
      * 
      * @var mixed
      * 
      */
-    protected $_dsn_key;
+    protected $_slave_key;
     
     /**
      * 
@@ -120,6 +129,36 @@
     
     /**
      * 
+     * Follow-on setup for the constructor to build the $_slaves array.
+     * 
+     * @return void
+     * 
+     * @see $_slaves
+     * 
+     */
+    protected function _setup()
+    {
+        // build up the $_slaves array info, using some of the values from
+        // the master as defaults
+        $base = array(
+            'host' => null,
+            'port' => $this->_config['port'],
+            'sock' => null,
+            'user' => $this->_config['user'],
+            'pass' => $this->_config['pass'],
+            'name' => $this->_config['name'],
+        );
+        
+        foreach ($this->_config['slaves'] as $key => $val) {
+            $this->_slaves[$key] = array_merge($base, $val);
+        }
+        
+        // done, on to the main setup
+        parent::_setup();
+    }
+    
+    /**
+     * 
      * Sets the connection-specific cache key prefix.
      * 
      * @param string $prefix The cache-key prefix.  When null, defaults to
@@ -167,7 +206,7 @@
      * 
      * @see $_dsn
      * 
-     * @see $_dsn_key
+     * @see $_slave_key
      * 
      * @see $_dsn_master
      * 
@@ -175,19 +214,13 @@
     protected function _setDsn()
     {
         // pick a random slave key
-        $this->_dsn_key = array_rand(array_keys($this->_config['slaves']));
+        $this->_slave_key = array_rand(
+            array_keys($this->_slaves)
+        );
         
         // get the slave info
-        $slave = $this->_config['slaves'][$this->_dsn_key];
+        $slave = $this->_slaves[$this->_slave_key];
         
-        // set missing information from the master
-        $list = array('port', 'user', 'pass', 'name');
-        foreach ($list as $item) {
-            if (empty($slave[$item])) {
-                $slave[$item] = $this->_config[$item];
-            }
-        }
-        
         // set DSN for slave
         $this->_dsn = $this->_buildDsn($slave);
         
@@ -213,7 +246,7 @@
         
         // which slave dsn key was used?
         // need this so we have the right credentials.
-        $key = $this->_dsn_key;
+        $key = $this->_slave_key;
         
         // start profile time
         $time = microtime(true);
@@ -221,8 +254,8 @@
         // attempt the connection
         $this->_pdo = new PDO(
             $this->_dsn,
-            $this->_config['slaves'][$key]['user'],
-            $this->_config['slaves'][$key]['pass']
+            $this->_slaves[$key]['user'],
+            $this->_slaves[$key]['pass']
         );
         
         // post-connection tasks
@@ -325,8 +358,8 @@
             if ($is_select) {
                 // slave
                 $this->connect();
-                $key = $this->_dsn_key;
-                $config = $this->_config['slaves'][$key];
+                $key = $this->_slave_key;
+                $config = $this->_slaves[$key];
                 $prep = $this->_pdo->prepare($stmt);
             } else {
                 // master

Modified: trunk/Solar/Sql/Adapter.php
===================================================================
--- trunk/Solar/Sql/Adapter.php	2008-07-31 22:10:52 UTC (rev 3295)
+++ trunk/Solar/Sql/Adapter.php	2008-07-31 22:25:51 UTC (rev 3296)
@@ -273,9 +273,6 @@
     {
         parent::__construct($config);
         
-        // set the DSN from the config info
-        $this->_setDsn();
-        
         // turn on profiling?
         $this->setProfiling($this->_config['profiling']);
         
@@ -285,6 +282,22 @@
             $this->_config['cache']
         );
         
+        // follow-on setup
+        $this->_setup();
+    }
+    
+    /**
+     * 
+     * Follow-on setup from the constructor; useful for extended classes.
+     * 
+     * @return void
+     * 
+     */
+    protected function _setup()
+    {
+        // set the DSN from the config info
+        $this->_setDsn();
+        
         // set the cache-key prefix
         $this->setCacheKeyPrefix();
     }




More information about the Solar-svn mailing list