Changeset 2419

Show
Ignore:
Timestamp:
04/04/2008 11:32:17 AM (8 months ago)
Author:
Shadowhand
Message:

Updated pdomo:

  • Added PDO_Model::cached_result()
  • Added PDODB::where(), PDODB::join(), and PDODB::test_operator()
  • Changed PDO_Model::on_find() to on_load()
  • Added PDO_Model->select_sql
  • Added a second parameter, $replace, to PDO_Model::load()
  • Added PDO_Model::load_query()
Location:
trunk/modules/pdomo
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/pdomo/i18n/en_US/pdo.php

    r2399 r2419  
    99        'invalid_database' => 'Invalid database used to construct %s (all databases must be instances of PDO)', 
    1010        'invalid_get' => 'Requested key %s does not exist in %s', 
    11         'invalid_operation' => 'Invalid operation %s used in %s::find()', 
     11        'invalid_operation' => 'Invalid operation "%s" was attempted in PDODB', 
    1212); 
  • trunk/modules/pdomo/libraries/PDODB.php

    r2405 r2419  
    4343                // Load the driver 
    4444                $this->driver = call_user_func(array($driver, 'instance')); 
     45        } 
     46 
     47        /** 
     48         * Returns a database-specific JOIN statement 
     49         * 
     50         * @param   string  table to join 
     51         * @param   string  column to join on 
     52         * @param   string  column to join on 
     53         * @param   string  join type 
     54         * @return  string 
     55         */ 
     56        public function join($table, $col1, $col2, $type = 'LEFT') 
     57        { 
     58                // Return a JOIN clause 
     59                return $type.' JOIN '.$this->quote_identifier($table).' ON '.$this->quote_identifier($col1).' = '.$this->quote_identifier($col2); 
     60        } 
     61 
     62        /** 
     63         * Returns a database-specific WHERE statement. 
     64         * 
     65         * @param   string  column name 
     66         * @param   string  operation 
     67         * @param   string  column value 
     68         * @return  string 
     69         */ 
     70        public function where($key, $op, $value = NULL) 
     71        { 
     72                if ($value === NULL) 
     73                { 
     74                        // Use the operator as the value 
     75                        $value = $op; 
     76 
     77                        // Use equals for the operator 
     78                        $op = '='; 
     79                } 
     80 
     81                // Return a WHERE clause 
     82                return $this->quote_identifier($key).' '.$this->test_operator($op).' '.$this->quote($value); 
    4583        } 
    4684 
     
    106144        } 
    107145 
     146        /** 
     147         * Tests the validity of an operator. 
     148         * 
     149         * @throws  Kohana_Exception 
     150         * @param   string  operator to test 
     151         * @return  string 
     152         */ 
     153        public function test_operator($op) 
     154        { 
     155                if ( ! preg_match('/^=|[!<>]=?|(?:NOT\s+)?(LIKE|REGEXP?|IN)$/i', $op)) 
     156                        throw new Kohana_Exception('pdo.invalid_operation', $op); 
     157 
     158                return strtoupper($op); 
     159        } 
     160 
    108161} // End PDODB 
  • trunk/modules/pdomo/models/pdo.php

    r2405 r2419  
    2424        protected $primary_key = 'id'; 
    2525 
     26        // SQL statement to select rows 
     27        protected $select_sql; 
     28 
    2629        // Object data status 
    2730        protected $loaded = FALSE; 
     
    6265                $this->db = $db; 
    6366 
     67                if (empty($this->select_sql)) 
     68                { 
     69                        // Set the default SELECT statement 
     70                        $table = $this->db->quote_identifier($this->table); 
     71                        $this->select_sql = 'SELECT '.$table.'.* FROM '.$table; 
     72                } 
     73 
    6474                // Empty the data 
    6575                $this->__empty_data(); 
     
    179189 
    180190        /** 
    181          * Called as the last step of find(), before a return. 
    182          * 
    183          * @return  void 
    184          */ 
    185         protected function __on_find() 
     191         * Called whenever the internal data is changed, before a return. 
     192         * 
     193         * @return  void 
     194         */ 
     195        protected function __on_load() 
    186196        { 
    187197                // No default action 
     
    245255         * 
    246256         * @chainable 
    247          * @param   array   key/value array 
     257         * @param   array    key/value array 
     258         * @param   boolean  replace the current data 
    248259         * @return  object 
    249260         */ 
    250         public function load(array $data) 
    251         { 
    252                 foreach ($data as $key => $val) 
    253                 { 
    254                         // Set each value separately 
    255                         $this->__set($key, $val); 
    256                 } 
     261        public function load($data, $replace = FALSE) 
     262        { 
     263                // Force the data to be an array 
     264                $data = (array) $data; 
     265 
     266                if ($replace === TRUE) 
     267                { 
     268                        // Empty the data 
     269                        $this->__empty_data(); 
     270 
     271                        // Replace the existing data with the loaded data 
     272                        $this->data = $data; 
     273 
     274                        // Set types 
     275                        $this->__set_types(); 
     276 
     277                        // Object is loaded and saved 
     278                        $this->loaded = $this->saved = TRUE; 
     279                } 
     280                else 
     281                { 
     282                        foreach ($data as $key => $val) 
     283                        { 
     284                                // Set each value separately 
     285                                $this->__set($key, $val); 
     286                        } 
     287                } 
     288 
     289                return $this; 
     290        } 
     291 
     292        /** 
     293         * Loads the first result of a statement as a new data set. 
     294         * 
     295         * @param   object  PDOStatement to execute 
     296         * @return  void 
     297         */ 
     298        protected function __load_query(PDOStatement $query) 
     299        { 
     300                // Empty the data 
     301                $this->__empty_data(); 
     302 
     303                if ($query->execute() AND $query->rowCount() > 0) 
     304                { 
     305                        // Load the data of the object 
     306                        $this->data = $query->fetch(PDO::FETCH_ASSOC); 
     307 
     308                        // No data has been changed 
     309                        $this->changed = array(); 
     310 
     311                        // Data has been loaded and is saved 
     312                        $this->loaded = $this->saved = TRUE; 
     313 
     314                        // Reset the types of loaded data 
     315                        $this->__set_types(); 
     316                } 
     317 
     318                // Execute the on_load event 
     319                $this->__on_load(); 
    257320        } 
    258321 
     
    270333         * @return  object 
    271334         */ 
    272         public function find($key, $op = '=', $value = NULL) 
    273         { 
    274                 if (($num_args = func_num_args()) < 3) 
    275                 { 
    276                         if ($num_args === 2) 
     335        public function find($key, $op = '######', $value = '######') 
     336        { 
     337                if ($value === '######') 
     338                { 
     339                        if ($op === '######') 
     340                        { 
     341                                // Use the key as the value 
     342                                $value = $key; 
     343 
     344                                // Use the primary key 
     345                                $key = $this->primary_key; 
     346                        } 
     347                        else 
    277348                        { 
    278349                                // Use the operator as the value 
    279350                                $value = $op; 
    280351                        } 
    281                         else 
    282                         { 
    283                                 // Use the key as the value 
    284                                 $value = $key; 
    285  
    286                                 // Use the primary key 
    287                                 $key = $this->primary_key; 
    288                         } 
    289352 
    290353                        // Use equals for the operator 
     
    304367 
    305368                // Quote all values 
    306                 $table = $this->db->quote_identifier($this->table); 
    307                 $key   = $this->db->quote_identifier($key); 
     369                $key   = $this->db->quote_identifier("{$this->table}.$key"); 
    308370                $value = $this->db->quote($value); 
    309371 
    310372                // Find a single row matching the criteria 
    311                 $query = $this->db->prepare('SELECT '.$table.'.* FROM '.$table.' WHERE '.$key.' '.$op.' '.$value.' '.$this->db->limit(1)); 
    312  
    313                 if ($query->execute() AND $query->rowCount() > 0) 
    314                 { 
    315                         // Load the data of the object 
    316                         $this->data = $query->fetch(PDO::FETCH_ASSOC); 
     373                $this->__load_query($this->db->prepare($this->select_sql.' WHERE '.$key.' '.$op.' '.$value.' '.$this->db->limit(1))); 
     374 
     375                return $this; 
     376        } 
     377 
     378        /** 
     379         * Saves the current object back into the database. 
     380         * 
     381         * @return  boolean 
     382         */ 
     383        public function save() 
     384        { 
     385                if ($this->saved === TRUE) 
     386                        return TRUE; 
     387 
     388                if (is_array($errors = $this->__validate())) 
     389                        return $errors; 
     390 
     391                if ($this->loaded === TRUE) 
     392                { 
     393                        // Perform an UPDATE 
     394                        $insert = FALSE; 
     395 
     396                        // Create the SQL 
     397                        $sql = 'UPDATE '.$this->db->quote_identifier($this->table).' SET '; 
     398 
     399                        $set = array(); 
     400                        foreach ($this->changed as $key) 
     401                        { 
     402                                // Add the new data 
     403                                $set[] = $this->db->quote_identifier($key).' = '.$this->db->quote($this->data[$key]); 
     404                        } 
     405 
     406                        // Add the WHERE 
     407                        $sql .= implode(', ', $set).' WHERE '.$this->db->quote_identifier($this->primary_key).' = '.$this->db->quote($this->data[$this->primary_key]); 
     408                } 
     409                else 
     410                { 
     411                        // Perform an INSERT 
     412                        $insert = TRUE; 
     413 
     414                        $data = array(); 
     415                        foreach ($this->changed as $key) 
     416                        { 
     417                                // Load the changed data 
     418                                $data[$this->db->quote_identifier($key)] = $this->db->quote($this->data[$key]); 
     419                        } 
     420 
     421                        if ($this->auto_increment === TRUE) 
     422                        { 
     423                                // Remove the primary key from the insert 
     424                                unset($data[$this->primary_key]); 
     425                        } 
     426 
     427                        // Create the SQL statement 
     428                        $sql = 'INSERT INTO '.$this->db->quote_identifier($this->table).' ('.implode(', ', array_keys($data)).') VALUES ('.implode(', ', $data).')'; 
     429                } 
     430 
     431                if ($count = $this->db->exec($sql)) 
     432                { 
     433                        if ($insert === TRUE AND $this->auto_increment === TRUE) 
     434                        { 
     435                                // Get and assign the insert ID 
     436                                $this->data[$this->primary_key] = $this->db->lastInsertId(); 
     437                        } 
    317438 
    318439                        // No data has been changed 
    319440                        $this->changed = array(); 
    320441 
    321                         // Data has been loaded and is saved 
    322                         $this->loaded = $this->saved = TRUE; 
    323  
    324                         // Reset the types of loaded data 
    325                         $this->__set_types(); 
    326                 } 
    327  
    328                 // Execute the on_find event 
    329                 $this->__on_find(); 
    330  
    331                 return $this; 
    332         } 
    333  
    334         /** 
    335          * Saves the current object back into the database. 
    336          * 
    337          * @return  boolean 
    338          */ 
    339         public function save() 
    340         { 
    341                 if ($this->saved === TRUE) 
    342                         return TRUE; 
    343  
    344                 if (is_array($errors = $this->__validate())) 
    345                         return $errors; 
    346  
    347                 if ($this->loaded === TRUE) 
    348                 { 
    349                         // Perform an UPDATE 
    350                         $insert = FALSE; 
    351  
    352                         // Create the SQL 
    353                         $sql = 'UPDATE '.$this->db->quote_identifier($this->table).' SET '; 
    354  
    355                         $set = array(); 
    356                         foreach ($this->changed as $key) 
    357                         { 
    358                                 // Add the new data 
    359                                 $set[] = $this->db->quote_identifier($key).' = '.$this->db->quote($this->data[$key]); 
    360                         } 
    361  
    362                         // Add the WHERE 
    363                         $sql .= implode(', ', $set).' WHERE '.$this->db->quote_identifier($this->primary_key).' = '.$this->db->quote($this->data[$this->primary_key]); 
    364                 } 
    365                 else 
    366                 { 
    367                         // Perform an INSERT 
    368                         $insert = TRUE; 
    369  
    370                         $data = array(); 
    371                         foreach ($this->changed as $key) 
    372                         { 
    373                                 // Load the changed data 
    374                                 $data[$this->db->quote_identifier($key)] = $this->db->quote($this->data[$key]); 
    375                         } 
    376  
    377                         if ($this->auto_increment === TRUE) 
    378                         { 
    379                                 // Remove the primary key from the insert 
    380                                 unset($data[$this->primary_key]); 
    381                         } 
    382  
    383                         // Create the SQL statement 
    384                         $sql = 'INSERT INTO '.$this->db->quote_identifier($this->table).' ('.implode(', ', array_keys($data)).') VALUES ('.implode(', ', $data).')'; 
    385                 } 
    386  
    387                 if ($count = $this->db->exec($sql)) 
    388                 { 
    389                         if ($insert === TRUE AND $this->auto_increment === TRUE) 
    390                         { 
    391                                 // Get and assign the insert ID 
    392                                 $this->data[$this->primary_key] = $this->db->lastInsertId(); 
    393                         } 
    394  
    395                         // No data has been changed 
    396                         $this->changed = array(); 
    397  
    398442                        // Object is loaded and saved 
    399443                        $this->saved = $this->loaded = TRUE; 
     
    443487        } 
    444488 
     489        /** 
     490         * Return the cached result of a query. If the result does not exist, it 
     491         * will be created. The result will always be an array of objects. 
     492         * 
     493         * @param   string   cache key name 
     494         * @param   string   SQL statement 
     495         * @return  array 
     496         */ 
     497        protected function cached_result($key, $sql) 
     498        { 
     499                // Cache key 
     500                $key .= '-'.sha1($sql); 
     501 
     502                // Retrieve the cached result 
     503                $result = Cache::instance()->get($key); 
     504 
     505                if ( ! is_array($result)) 
     506                { 
     507                        // Prepare the query 
     508                        $query = $this->db->prepare($sql); 
     509 
     510                        // Execute the query 
     511                        $query->execute(); 
     512 
     513                        // Return all the rows 
     514                        $result = $query->fetchAll(PDO::FETCH_OBJ); 
     515 
     516                        // Set the cache 
     517                        Cache::instance()->set($key, $result, array('database', 'result'), strtotime('now +1 hour')); 
     518                } 
     519 
     520                // Return the cached result 
     521                return $result; 
     522        } 
     523 
    445524} // End PDO_Model 
  • trunk/modules/pdomo/models/user_token.php

    r2399 r2419  
    3535        } 
    3636 
    37         protected function __on_find() 
     37        protected function __on_load() 
    3838        { 
    3939                if ($this->loaded AND $this->expires < time())