コード例 #1
0
 /**
  * Proxy the database connector setQuery() method
  */
 public function setQuery($sql, $offset = 0, $limit = 0, $prefix = '#__')
 {
     $result = false;
     //Convert any linebreaks to br tags, added to solve a bug with Virtuemart 1.1.2
     $sql = str_replace('\\r\\n', '<br />', $sql);
     $operation = preg_split('/\\s/', trim($sql), 2, PREG_SPLIT_NO_EMPTY);
     switch (strtoupper($operation[0])) {
         case 'INSERT':
             $parser = new KDatabaseQueryParser();
             if (!($query = $parser->parse($this->replaceTablePrefix($sql, '', $prefix)))) {
                 $this->select($sql);
                 break;
             }
             //Remove prefix from the table name
             $table = str_replace($this->getPrefix(), '', $query['table_names'][0]);
             if (!isset($query['column_names'])) {
                 // the column names weren't specified, get them from the table's metadata
                 $fields = $this->getTableFields($table);
                 $query['column_names'] = array_keys($fields[$table]);
             }
             // Make a list of field names and their values
             $data = array();
             foreach ($query['column_names'] as $key => $column_name) {
                 $data[$column_name] = $query['values'][$key]['value'];
             }
             $this->insert($table, $data);
             break;
         case 'UPDATE':
             //Make sure the where statement is uppercase
             $sql = str_replace('where', 'WHERE', $sql);
             //Split the sql string
             $where = substr($sql, strpos($sql, 'WHERE'));
             $query = substr_replace($sql, 'WHERE 1 = 1', strpos($sql, 'WHERE'));
             $parser = new KDatabaseQueryParser();
             if (!($query = $parser->parse($this->replaceTablePrefix($query, '', $prefix)))) {
                 $this->select($sql);
                 break;
             }
             //Remove prefix from the table name
             $table = str_replace($this->getPrefix(), '', $query['table_names'][0]);
             $data = array();
             foreach ($query['column_names'] as $key => $column_name) {
                 $data[$column_name] = $query['values'][$key]['value'];
             }
             $this->update($table, $data, $where);
             break;
         case 'DELETE':
             //Make sure the where statement is uppercase
             $sql = str_replace('where', 'WHERE', $sql);
             //Split the sql string
             $where = substr($sql, strpos($sql, 'WHERE'));
             $query = substr_replace($sql, 'WHERE 1 = 1', strpos($sql, 'WHERE'));
             $parser = new KDatabaseQueryParser();
             if (!($query = $parser->parse($this->replaceTablePrefix($query, '', $prefix)))) {
                 $this->select($sql);
                 break;
             }
             //Remove prefix from the table name
             $table = str_replace($this->getPrefix(), '', $query['table_names'][0]);
             $this->delete($table, $where);
             break;
         default:
             $this->select($sql, $offset, $limit);
     }
 }
コード例 #2
0
 protected function _findPkInWhere($where)
 {
     $parser = new KDatabaseQueryParser(str_replace('WHERE', '', $where));
     $sql = $parser->parseSearchClause();
     // Assumptions:
     // Depth 1: "WHERE primary_key = n"
     // Depth 2: "WHERE primary_key = n AND foo = bar [AND ....]"
     $depth = array_key_exists('arg_1', $sql['arg_1']) ? 2 : 1;
     switch ($depth) {
         case 1:
             $id = $sql['arg_2']['value'];
             break;
         case 2:
             $id = $sql['arg_1']['arg_2']['value'];
             break;
         default:
             $id = false;
     }
     // extra failsafe
     $id = is_numeric($id) ? $id : false;
     return $id;
 }