function build_insert_query(&$schema, &$sql_data, $current_table)
{
global $db, $user;
global $convert;
// Can we use IGNORE with this DBMS?
$sql_ignore = strpos($db->get_sql_layer(), 'mysql') === 0 && !defined('DEBUG') ? 'IGNORE ' : '';
$insert_query = 'INSERT ' . $sql_ignore . 'INTO ' . $schema['target'] . ' (';
$aliases = array();
$sql_data = array('source_fields' => array(), 'target_fields' => array(), 'source_tables' => array(), 'select_fields' => array());
foreach ($schema as $key => $val) {
// Example: array('group_name', 'extension_groups.group_name', 'htmlspecialchars'),
if (is_int($key)) {
if (!empty($val[0])) {
// Target fields
$sql_data['target_fields'][$val[0]] = $key;
$insert_query .= $val[0] . ', ';
}
if (!is_array($val[1])) {
$val[1] = array($val[1]);
}
foreach ($val[1] as $valkey => $value_1) {
// This should cover about any case:
//
// table.field => SELECT table.field FROM table
// table.field AS alias => SELECT table.field AS alias FROM table
// table.field AS table2.alias => SELECT table2.field AS alias FROM table table2
// table.field AS table2.field => SELECT table2.field FROM table table2
//
if (preg_match('/^([a-z0-9_]+)\\.([a-z0-9_]+)( +AS +(([a-z0-9_]+?)\\.)?([a-z0-9_]+))?$/i', $value_1, $m)) {
// There is 'AS ...' in the field names
if (!empty($m[3])) {
$value_1 = $m[2] == $m[6] ? $m[1] . '.' . $m[2] : $m[1] . '.' . $m[2] . ' AS ' . $m[6];
// Table alias: store it then replace the source table with it
if (!empty($m[5]) && $m[5] != $m[1]) {
$aliases[$m[5]] = $m[1];
$value_1 = str_replace($m[1] . '.' . $m[2], $m[5] . '.' . $m[2], $value_1);
}
} else {
// No table alias
$sql_data['source_tables'][$m[1]] = empty($convert->src_table_prefix) ? $m[1] : $convert->src_table_prefix . $m[1] . ' ' . $m[1];
}
$sql_data['select_fields'][$value_1] = $value_1;
$sql_data['source_fields'][$key][$valkey] = !empty($m[6]) ? $m[6] : $m[2];
}
}
} else {
if ($key == 'where' || $key == 'group_by' || $key == 'order_by' || $key == 'having') {
if (@preg_match_all('/([a-z0-9_]+)\\.([a-z0-9_]+)/i', $val, $m)) {
foreach ($m[1] as $value) {
$sql_data['source_tables'][$value] = empty($convert->src_table_prefix) ? $value : $convert->src_table_prefix . $value . ' ' . $value;
}
}
}
}
}
// Add the aliases to the list of tables
foreach ($aliases as $alias => $table) {
$sql_data['source_tables'][$alias] = $convert->src_table_prefix . $table . ' ' . $alias;
}
// 'left_join' => 'forums LEFT JOIN forum_prune ON forums.forum_id = forum_prune.forum_id',
if (!empty($schema['left_join'])) {
if (!is_array($schema['left_join'])) {
$schema['left_join'] = array($schema['left_join']);
}
foreach ($schema['left_join'] as $left_join) {
// This won't handle concatened LEFT JOINs
if (!preg_match('/([a-z0-9_]+) LEFT JOIN ([a-z0-9_]+) A?S? ?([a-z0-9_]*?) ?(ON|USING)(.*)/i', $left_join, $m)) {
$this->error(sprintf($user->lang['NOT_UNDERSTAND'], 'LEFT JOIN', $left_join, $current_table, $schema['target']), __LINE__, __FILE__);
}
if (!empty($aliases[$m[2]])) {
if (!empty($m[3])) {
$this->error(sprintf($user->lang['NAMING_CONFLICT'], $m[2], $m[3], $schema['left_join']), __LINE__, __FILE__);
}
$m[2] = $aliases[$m[2]];
$m[3] = $m[2];
}
$right_table = $convert->src_table_prefix . $m[2];
if (!empty($m[3])) {
unset($sql_data['source_tables'][$m[3]]);
} else {
if ($m[2] != $m[1]) {
unset($sql_data['source_tables'][$m[2]]);
}
}
if (strpos($sql_data['source_tables'][$m[1]], "\nLEFT JOIN") !== false) {
$sql_data['source_tables'][$m[1]] = '(' . $sql_data['source_tables'][$m[1]] . ")\nLEFT JOIN {$right_table}";
} else {
$sql_data['source_tables'][$m[1]] .= "\nLEFT JOIN {$right_table}";
}
if (!empty($m[3])) {
unset($sql_data['source_tables'][$m[3]]);
$sql_data['source_tables'][$m[1]] .= ' AS ' . $m[3];
} else {
if (!empty($convert->src_table_prefix)) {
$sql_data['source_tables'][$m[1]] .= ' AS ' . $m[2];
}
}
$sql_data['source_tables'][$m[1]] .= ' ' . $m[4] . $m[5];
}
}
// Remove ", " from the end of the insert query
$insert_query = substr($insert_query, 0, -2) . ') VALUES ';
return $insert_query;
}