public function compile($db = NULL)
 {
     if (!is_object($db)) {
         $db = Database::instance($db);
     }
     $query = 'INSERT INTO ' . $db->quote_table($this->_table);
     $query .= ' (' . implode(', ', array_map(array($db, 'quote_column'), $this->_columns)) . ') ';
     if (is_array($this->_values)) {
         $quote = array($db, 'quote');
         $groups = array();
         foreach ($this->_values as $group) {
             foreach ($group as $offset => $value) {
                 if ((is_string($value) and array_key_exists($value, $this->_parameters)) === FALSE) {
                     $group[$offset] = $db->quote($value);
                 }
             }
             $groups[] = '(' . implode(', ', $group) . ')';
         }
         $query .= 'VALUES ' . implode(', ', $groups);
     } else {
         $query .= (string) $this->_values;
     }
     $this->_sql = $query;
     return parent::compile($db);
 }
Beispiel #2
0
 /**
  * Compile the SQL query and return it.
  *
  * @param   mixed  $db  Database instance or name of instance
  * @return  string
  */
 public function compile($db = NULL)
 {
     if (!is_object($db)) {
         // Get the database instance
         $db = Database::instance($db);
     }
     // Start an insertion query
     $query = 'INSERT INTO ' . $db->quote_table($this->_table);
     // Add the column names
     $query .= ' (' . implode(', ', array_map(array($db, 'quote_column'), $this->_columns)) . ') ';
     if (is_array($this->_values)) {
         // Callback for quoting values
         $quote = array($db, 'quote');
         $groups = array();
         foreach ($this->_values as $group) {
             foreach ($group as $offset => $value) {
                 if ((is_string($value) and array_key_exists($value, $this->_parameters)) === FALSE) {
                     // Quote the value, it is not a parameter
                     $group[$offset] = $db->quote($value);
                 }
             }
             $groups[] = '(' . implode(', ', $group) . ')';
         }
         // Add the values
         $query .= 'VALUES ' . implode(', ', $groups);
     } else {
         // Add the sub-query
         $query .= (string) $this->_values;
     }
     $this->_sql = $query;
     return parent::compile($db);
 }