3 // creates schema file output
6 error_reporting(E_ALL ^ E_NOTICE
);
8 define('ISSO', dirname(dirname(getcwd())));
9 require_once ISSO
. '/App.php';
10 require_once ISSO
. '/DbMySqlI.php';
11 require_once ISSO
. '/Input.php';
13 $db = BSApp
::$db = new BSDbMySqlI();
14 $input = BSApp
::$input = new BSInput();
16 if (empty($_REQUEST['submit']))
20 <form action
="create_schema.php" method
="post" name
="params">
22 <div
>Database
: <input name
="database" type
="text" size
="35" /></div
>
23 <div
>Username
: <input name
="username" type
="text" size
="35" value
="mysql" /></div
>
24 <div
>Password
: <input name
="password" type
="password" size
="35" /></div
>
25 <div
>Server
: <input name
="server" type
="text" size
="35" value
="localhost" /></div
>
29 <div
>Database engine
: <input name
="engine" type
="radio" value
="mysql" checked
="checked" /> MySQL
<input name
="engine" type
="radio" value
="postgresql" /> PostgreSQL
</div
>
30 <div
>Array variable
: <input name
="variable" type
="text" size
="35" value
="schema" /></div
>
31 <div
>Encase names with ticks
: <input name
="encase" type
="checkbox" value
="1" /> Yes
</div
>
32 <div
>Add TABLE_PREFIX
: <input name
="prefix" type
="checkbox" checked
="checked" value
="1" /> Yes
</div
>
34 <input name
="submit" type
="submit" value
="Create Schema" />
42 $db->connect($input->in
['server'], $input->in
['username'], $input->in
['password'], $input->in
['database']);
44 $t = ($input->in
['encase'] ?
'`' : '');
45 $prefix = ($input->in
['prefix'] ?
'" . TABLE_PREFIX . "' : '');
47 $tables = $db->query("SHOW TABLES");
48 while ($table = $tables->fetchArray(false
))
54 $build = "CREATE TABLE $t$prefix$table$t\n(\n";
56 $indexes = $db->query("SHOW INDEX FROM $table");
57 while ($index = $indexes->fetchArray())
59 array_walk($index, 'trim');
61 if ($index['Sub_part'] && $input->in
['engine'] == 'postgresql')
63 $subpart = " ($index[Sub_part])";
66 if ($index['Key_name'] == 'PRIMARY')
68 $keys['primary'][] = "$t$index[Column_name]$t$subpart";
70 else if ($index['Index_type'] == 'FULLTEXT')
72 $keys['fulltext']["$index[Key_name]"][] = "$t$index[Column_name]$t$subpart";
74 else if ($index['Index_type'] == 'BTREE' && $index['Non_unique'] == 0)
76 $keys['unique']["$index[Key_name]"][] = "$t$index[Column_name]$t$subpart";
78 else if ($index['Index_type'] == 'BTREE' && $index['Non_unique'] == 1)
80 $keys['std']["$index[Key_name]"][] = "$t$index[Column_name]$t$subpart";
82 // we should never get here :-)
90 $fields = $db->query("DESCRIBE $table");
91 while ($field = $fields->fetchArray())
93 array_walk($field, 'trim');
95 if (preg_match('#^(tinyint|smallint|bigint|int)\(([0-9]+)\)( unsigned)?#i', $field['Type'], $matches))
99 $field['Type'] = 'bool';
101 else if ($matches[2] < 10)
103 $field['Type'] = 'smallint';
105 else if ($matches[2] > 12)
107 $field['Type'] = 'bigint';
111 $field['Type'] = 'int';
114 if ($input->in
['engine'] == 'mysql' && $field['Type'] != 'bool')
116 $field['Type'] .= $matches[3];
118 else if ($input->in
['engine'] == 'postgresql')
120 if (preg_match('#AUTO_INCREMENT#i', $field['Extra']))
122 $field['Type'] = 'SERIAL';
123 $field['Extra'] = '';
127 else if (preg_match('#^mediumtext|longtext$#i', $field['Type']))
129 $field['Type'] = 'text';
131 else if (preg_match('#.+?blob$#i', $field['Type']))
133 $field['Type'] = 'blob';
134 if ($input->in
['engine'] == 'postgresql')
136 $field['Type'] = 'bytea';
140 // quote default values where appropriate
141 if (!empty($field['Default']))
143 if ($field['Default'] != 'CURRENT_TIMESTAMP')
145 $field['Default'] = "'$field[Default]'";
149 $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]" : "");
152 $build .= implode(",\n", $list);
154 if ($keys['primary'])
156 $build .= ",\n\tPRIMARY KEY (" . implode(', ', $keys['primary']) . ")";
158 if ($keys['fulltext'])
160 foreach ($keys['fulltext'] as $name => $columns)
162 $build .= ",\n\tFULLTEXT KEY $t$name$t (" . implode(', ', $columns) . ")";
167 foreach ($keys['unique'] as $name => $columns)
169 $build .= ",\n\tUNIQUE KEY $t$name$t (" . implode(', ', $columns) . ")";
174 foreach ($keys['std'] as $name => $columns)
176 $build .= ",\n\tKEY $t$name$t (" . implode(', ', $columns) . ")";
182 $queries["$table"] = $build;
185 if ($input->in
['variable'] != '')
187 foreach ($queries as $table => $query)
189 $output[] = "\${$input->in['variable']}['$table'] = \"\n$query\";";
197 $output = implode("\n\n", $output);
201 <textarea name
="output" rows
="50" cols
="60" style
="width: 100%"><?
= $output ?
></textarea
>