Example #1
0
 /**
  * Given one XMLDB Statement, build the needed SQL insert sentences to execute it.
  *
  * @param string $statement SQL statement.
  * @return array Array of sentences in the SQL statement.
  */
 function getExecuteInsertSQL($statement)
 {
     $results = array();
     //Array where all the sentences will be stored
     if ($sentences = $statement->getSentences()) {
         foreach ($sentences as $sentence) {
             /// Get the list of fields
             $fields = $statement->getFieldsFromInsertSentence($sentence);
             /// Get the values of fields
             $values = $statement->getValuesFromInsertSentence($sentence);
             /// Look if we have some CONCAT value and transform it dynamically
             foreach ($values as $key => $value) {
                 /// Trim single quotes
                 $value = trim($value, "'");
                 if (stristr($value, 'CONCAT') !== false) {
                     /// Look for data between parenthesis
                     preg_match("/CONCAT\\s*\\((.*)\\)\$/is", trim($value), $matches);
                     if (isset($matches[1])) {
                         $part = $matches[1];
                         /// Convert the comma separated string to an array
                         $arr = xmldb_object::comma2array($part);
                         if ($arr) {
                             $value = $this->getConcatSQL($arr);
                         }
                     }
                 }
                 /// Values to be sent to DB must be properly escaped
                 $value = $this->addslashes($value);
                 /// Back trimmed quotes
                 $value = "'" . $value . "'";
                 /// Back to the array
                 $values[$key] = $value;
             }
             /// Iterate over fields, escaping them if necessary
             foreach ($fields as $key => $field) {
                 $fields[$key] = $this->getEncQuoted($field);
             }
             /// Build the final SQL sentence and add it to the array of results
             $sql = 'INSERT INTO ' . $this->getEncQuoted($this->prefix . $statement->getTable()) . '(' . implode(', ', $fields) . ') ' . 'VALUES (' . implode(', ', $values) . ')';
             $results[] = $sql;
         }
     }
     return $results;
 }