]>
src.bluestatic.org Git - isso.git/blob - tools/create_schema.php
3 // creates schema file output
6 ini_set('include_path', ini_get('include_path') . ':' . dirname(dirname(getcwd())));
7 require_once('ISSO/Register.php');
9 $db = BSApp
::LoadModule('DbMySQL');
10 $input = BSApp
::LoadModule('Input');
12 if (empty($_REQUEST['submit']))
16 <form action
="create_schema.php" method
="post" name
="params">
18 <div
>Database
: <input name
="database" type
="text" size
="35" /></div
>
19 <div
>Username
: <input name
="username" type
="text" size
="35" value
="mysql" /></div
>
20 <div
>Password
: <input name
="password" type
="password" size
="35" /></div
>
21 <div
>Server
: <input name
="server" type
="text" size
="35" value
="localhost" /></div
>
25 <div
>Database engine
: <input name
="engine" type
="radio" value
="mysql" checked
="checked" /> MySQL
<input name
="engine" type
="radio" value
="postgresql" /> PostgreSQL
</div
>
26 <div
>Array variable
: <input name
="variable" type
="text" size
="35" value
="schema" /></div
>
27 <div
>Encase names with ticks
: <input name
="encase" type
="checkbox" value
="1" /> Yes
</div
>
28 <div
>Add TABLE_PREFIX
: <input name
="prefix" type
="checkbox" checked
="checked" value
="1" /> Yes
</div
>
30 <input name
="submit" type
="submit" value
="Create Schema" />
38 $db->connect($input->in
['server'], $input->in
['username'], $input->in
['password'], $input->in
['database'], false);
40 $t = ($input->in
['encase'] ? '`' : '');
41 $prefix = ($input->in
['prefix'] ? '" . TABLE_PREFIX . "' : '');
43 $tables = $db->query("SHOW TABLES");
44 while ($table = $db->fetchArray($tables, false))
50 $build = "CREATE TABLE $t$prefix$table$t\n(\n";
52 $indexes = $db->query("SHOW INDEX FROM $table");
53 while ($index = $db->fetchArray($indexes))
55 array_walk($index, 'trim');
57 if ($index['Sub_part'] AND $input->in['engine'] == 'postgresql')
59 $subpart = " ($index[Sub_part
])";
62 if ($index['Key_name'] == 'PRIMARY')
64 $keys['primary'][] = "$t$index[Column_name
]$t$subpart";
66 else if ($index['Index_type'] == 'FULLTEXT')
68 $keys['fulltext']["$index[Key_name]"][] = "$t$index[Column_name]$t$subpart";
70 else if ($index['Index_type'] == 'BTREE' AND $index['Non_unique'] == 0)
72 $keys['unique']["$index[Key_name
]"][] = "$t$index[Column_name
]$t$subpart";
74 else if ($index['Index_type'] == 'BTREE' AND $index['Non_unique'] == 1)
76 $keys['std']["$index[Key_name]"][] = "$t$index[Column_name]$t$subpart";
78 // we should never get here :-)
86 $fields = $db->query("DESCRIBE
$table");
87 while ($field = $db->fetchArray($fields))
89 array_walk($field, 'trim');
91 if (preg_match('#^(tinyint|smallint|bigint|int)\(([0-9]+)\)( unsigned)?#i', $field['Type'], $matches))
95 $field['Type'] = 'bool';
97 else if ($matches[2] < 10)
99 $field['Type'] = 'smallint';
101 else if ($matches[2] > 12)
103 $field['Type'] = 'bigint';
107 $field['Type'] = 'int';
110 if ($input->in
['engine'] == 'mysql' AND $field['Type'] != 'bool')
112 $field['Type'] .= $matches[3];
114 else if ($input->in
['engine'] == 'postgresql')
116 if (preg_match('#AUTO_INCREMENT#i', $field['Extra']))
118 $field['Type'] = 'SERIAL';
119 $field['Extra'] = '';
123 else if (preg_match('#^mediumtext|longtext$#i', $field['Type']))
125 $field['Type'] = 'text';
127 else if (preg_match('#.+?blob$#i', $field['Type']))
129 $field['Type'] = 'blob';
130 if ($input->in
['engine'] == 'postgresql')
132 $field['Type'] = 'bytea';
136 // quote default values where appropriate
137 if (!empty($field['Default']))
139 if ($field['Default'] != 'CURRENT_TIMESTAMP')
141 $field['Default'] = "'$field[Default]'";
145 $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]" : "");
148 $build .= implode(",\n", $list);
150 if ($keys['primary'])
152 $build .= ",\n\tPRIMARY KEY (" . implode(', ', $keys['primary']) . ")";
154 if ($keys['fulltext'])
156 foreach ($keys['fulltext'] AS $name => $columns)
158 $build .= ",\n\tFULLTEXT KEY $t$name$t (" . implode(', ', $columns) . ")";
163 foreach ($keys['unique'] AS $name => $columns)
165 $build .= ",\n\tUNIQUE KEY $t$name$t (" . implode(', ', $columns) . ")";
170 foreach ($keys['std'] AS $name => $columns)
172 $build .= ",\n\tKEY $t$name$t (" . implode(', ', $columns) . ")";
178 $queries["$table"] = $build;
181 if ($input->in['variable'] != '')
183 foreach ($queries AS $table => $query)
185 $output[] = "\$
{$input
->in
['variable']}['$table'] = \"\n$query\";";
193 $output = implode("\n\n", $output);
197 <textarea name="output" rows="50" cols="60" style="width: 100%"><?= $output ?></textarea>