connect($input->in['server'], $input->in['username'], $input->in['password'], $input->in['database']);
$t = ($input->in['encase'] ? '`' : '');
$prefix = ($input->in['prefix'] ? '" . TABLE_PREFIX . "' : '');
$tables = $db->query("SHOW TABLES");
while ($table = $tables->fetchArray(false))
{
$table = $table[0];
$list = array();
$keys = array();
$build = "CREATE TABLE $t$prefix$table$t\n(\n";
$indexes = $db->query("SHOW INDEX FROM $table");
while ($index = $indexes->fetchArray())
{
array_walk($index, 'trim');
if ($index['Sub_part'] && $input->in['engine'] == 'postgresql')
{
$subpart = " ($index[Sub_part])";
}
if ($index['Key_name'] == 'PRIMARY')
{
$keys['primary'][] = "$t$index[Column_name]$t$subpart";
}
else if ($index['Index_type'] == 'FULLTEXT')
{
$keys['fulltext']["$index[Key_name]"][] = "$t$index[Column_name]$t$subpart";
}
else if ($index['Index_type'] == 'BTREE' && $index['Non_unique'] == 0)
{
$keys['unique']["$index[Key_name]"][] = "$t$index[Column_name]$t$subpart";
}
else if ($index['Index_type'] == 'BTREE' && $index['Non_unique'] == 1)
{
$keys['std']["$index[Key_name]"][] = "$t$index[Column_name]$t$subpart";
}
// we should never get here :-)
else
{
print_r($index);
exit;
}
}
$fields = $db->query("DESCRIBE $table");
while ($field = $fields->fetchArray())
{
array_walk($field, 'trim');
if (preg_match('#^(tinyint|smallint|bigint|int)\(([0-9]+)\)( unsigned)?#i', $field['Type'], $matches))
{
if ($matches[2] == 1)
{
$field['Type'] = 'bool';
}
else if ($matches[2] < 10)
{
$field['Type'] = 'smallint';
}
else if ($matches[2] > 12)
{
$field['Type'] = 'bigint';
}
else
{
$field['Type'] = 'int';
}
if ($input->in['engine'] == 'mysql' && $field['Type'] != 'bool')
{
$field['Type'] .= $matches[3];
}
else if ($input->in['engine'] == 'postgresql')
{
if (preg_match('#AUTO_INCREMENT#i', $field['Extra']))
{
$field['Type'] = 'SERIAL';
$field['Extra'] = '';
}
}
}
else if (preg_match('#^mediumtext|longtext$#i', $field['Type']))
{
$field['Type'] = 'text';
}
else if (preg_match('#.+?blob$#i', $field['Type']))
{
$field['Type'] = 'blob';
if ($input->in['engine'] == 'postgresql')
{
$field['Type'] = 'bytea';
}
}
// quote default values where appropriate
if (!empty($field['Default']))
{
if ($field['Default'] != 'CURRENT_TIMESTAMP')
{
$field['Default'] = "'$field[Default]'";
}
}
$list[] = "\t$t$field[Field]$t " . $field['Type'] . " " . ($field['Null'] == 'YES' ? "NULL" : "NOT NULL") . ($field['Extra'] != '' ? " " . strtoupper($field['Extra']) : "") . (!empty($field['Default']) ? " DEFAULT $field[Default]" : "");
}
$build .= implode(",\n", $list);
if ($keys['primary'])
{
$build .= ",\n\tPRIMARY KEY (" . implode(', ', $keys['primary']) . ")";
}
if ($keys['fulltext'])
{
foreach ($keys['fulltext'] as $name => $columns)
{
$build .= ",\n\tFULLTEXT KEY $t$name$t (" . implode(', ', $columns) . ")";
}
}
if ($keys['unique'])
{
foreach ($keys['unique'] as $name => $columns)
{
$build .= ",\n\tUNIQUE KEY $t$name$t (" . implode(', ', $columns) . ")";
}
}
if ($keys['std'])
{
foreach ($keys['std'] as $name => $columns)
{
$build .= ",\n\tKEY $t$name$t (" . implode(', ', $columns) . ")";
}
}
$build .= "\n);";
$queries["$table"] = $build;
}
if ($input->in['variable'] != '')
{
foreach ($queries as $table => $query)
{
$output[] = "\${$input->in['variable']}['$table'] = \"\n$query\";";
}
}
else
{
$output = $queries;
}
$output = implode("\n\n", $output);
?>