function create(&$xmls)
{
$sql = array();
// drop any existing indexes
if (is_array($legacy_indexes = $xmls->dict->MetaIndexes($this->name))) {
foreach ($legacy_indexes as $index => $index_details) {
$sql[] = $xmls->dict->DropIndexSQL($index, $this->name);
}
}
// remove fields to be dropped from table object
foreach ($this->drop_field as $field) {
unset($this->fields[$field]);
}
// if table exists
if (is_array($legacy_fields = $xmls->dict->MetaColumns($this->name))) {
// drop table
if ($this->drop_table) {
$sql[] = $xmls->dict->DropTableSQL($this->name);
return $sql;
}
// drop any existing fields not in schema
foreach ($legacy_fields as $field_id => $field) {
if (!isset($this->fields[$field_id])) {
$sql[] = $xmls->dict->DropColumnSQL($this->name, '`' . $field->name . '`');
}
}
// if table doesn't exist
} else {
if ($this->drop_table) {
return $sql;
}
$legacy_fields = array();
}
// Loop through the field specifier array, building the associative array for the field options
$fldarray = array();
foreach ($this->fields as $field_id => $finfo) {
// Set an empty size if it isn't supplied
if (!isset($finfo['SIZE'])) {
$finfo['SIZE'] = '';
}
// Initialize the field array with the type and size
$fldarray[$field_id] = array('NAME' => $finfo['NAME'], 'TYPE' => $finfo['TYPE'], 'SIZE' => $finfo['SIZE']);
// Loop through the options array and add the field options.
if (isset($finfo['OPTS'])) {
foreach ($finfo['OPTS'] as $opt) {
// Option has an argument.
if (is_array($opt)) {
$key = key($opt);
$value = $opt[key($opt)];
@($fldarray[$field_id][$key] .= $value);
// Option doesn't have arguments
} else {
$fldarray[$field_id][$opt] = $opt;
}
}
}
}
if (empty($legacy_fields)) {
// Create the new table
$sql[] = $xmls->dict->CreateTableSQL($this->name, $fldarray, $this->opts);
logMsg(end($sql), 'Generated CreateTableSQL');
} else {
// Upgrade an existing table
logMsg("Upgrading {$this->name} using '{$xmls->upgrade}'");
switch ($xmls->upgrade) {
// Use ChangeTableSQL
case 'ALTER':
logMsg('Generated ChangeTableSQL (ALTERing table)');
$sql[] = $xmls->dict->ChangeTableSQL($this->name, $fldarray, $this->opts);
break;
case 'REPLACE':
logMsg('Doing upgrade REPLACE (testing)');
$sql[] = $xmls->dict->DropTableSQL($this->name);
$sql[] = $xmls->dict->CreateTableSQL($this->name, $fldarray, $this->opts);
break;
// ignore table
// ignore table
// ignore table
// ignore table
default:
return array();
}
}
foreach ($this->indexes as $index) {
$sql[] = $index->create($xmls);
}
if (isset($this->data)) {
$sql[] = $this->data->create($xmls);
}
return $sql;
}