forked from myfarms/phppgadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colproperties.php
346 lines (311 loc) · 11.9 KB
/
colproperties.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/**
* List Columns properties in tables
*
* $Id: colproperties.php
*/
// Include application functions
include_once('./libraries/lib.inc.php');
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
if (isset($_REQUEST['table']))
$tableName =& $_REQUEST['table'];
elseif (isset($_REQUEST['view']))
$tableName =& $_REQUEST['view'];
else
die($lang['strnotableprovided']);
/**
* Displays a screen where they can alter a column
*/
function doAlter($msg = '') {
global $data, $misc, $_reload_browser;
global $lang;
if (!isset($_REQUEST['stage'])) $_REQUEST['stage'] = 1;
switch ($_REQUEST['stage']) {
case 1:
$misc->printTrail('column');
$misc->printTitle($lang['stralter'], 'pg.column.alter');
$misc->printMsg($msg);
echo "<script src=\"tables.js\" type=\"text/javascript\"></script>";
echo "<form action=\"colproperties.php\" method=\"post\">\n";
// Output table header
echo "<table>\n";
echo "<tr><th class=\"data required\">{$lang['strname']}</th>\n";
if ($data->hasAlterColumnType()) {
echo "<th class=\"data required\" colspan=\"2\">{$lang['strtype']}</th>\n";
echo "<th class=\"data\">{$lang['strlength']}</th>\n";
}
else {
echo "<th class=\"data required\">{$lang['strtype']}</th>\n";
}
echo "<th class=\"data\">{$lang['strnotnull']}</th>\n<th class=\"data\">{$lang['strdefault']}</th>\n<th class=\"data\">{$lang['strcomment']}</th></tr>\n";
$column = $data->getTableAttributes($_REQUEST['table'], $_REQUEST['column']);
$column->fields['attnotnull'] = $data->phpBool($column->fields['attnotnull']);
// Upon first drawing the screen, load the existing column information
// from the database.
if (!isset($_REQUEST['default'])) {
$_REQUEST['field'] = $column->fields['attname'];
$_REQUEST['type'] = $column->fields['base_type'];
// Check to see if its' an array type...
// XXX: HACKY
if (substr($column->fields['base_type'], strlen($column->fields['base_type']) - 2) == '[]') {
$_REQUEST['type'] = substr($column->fields['base_type'], 0, strlen($column->fields['base_type']) - 2);
$_REQUEST['array'] = '[]';
}
else {
$_REQUEST['type'] = $column->fields['base_type'];
$_REQUEST['array'] = '';
}
// To figure out the length, look in the brackets :(
// XXX: HACKY
if ($column->fields['type'] != $column->fields['base_type'] && preg_match('/\\(([0-9, ]*)\\)/', $column->fields['type'], $bits)) {
$_REQUEST['length'] = $bits[1];
}
else
$_REQUEST['length'] = '';
$_REQUEST['default'] = $_REQUEST['olddefault'] = $column->fields['adsrc'];
if ($column->fields['attnotnull']) $_REQUEST['notnull'] = 'YES';
$_REQUEST['comment'] = $column->fields['comment'];
}
// Column name
echo "<tr><td><input name=\"field\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
htmlspecialchars($_REQUEST['field']), "\" /></td>\n";
// Column type
$escaped_predef_types = array(); // the JS escaped array elements
if ($data->hasAlterColumnType()) {
// Fetch all available types
$types = $data->getTypes(true, false, true);
$types_for_js = array();
echo "<td><select name=\"type\" id=\"type\" onchange=\"checkLengths(document.getElementById('type').value,'');\">\n";
while (!$types->EOF) {
$typname = $types->fields['typname'];
$types_for_js[] = $typname;
echo "\t<option value=\"", htmlspecialchars($typname), "\"", ($typname == $_REQUEST['type']) ? ' selected="selected"' : '', ">",
$misc->printVal($typname), "</option>\n";
$types->moveNext();
}
echo "</select></td>\n";
// Output array type selector
echo "<td><select name=\"array\">\n";
echo "\t<option value=\"\"", ($_REQUEST['array'] == '') ? ' selected="selected"' : '', "></option>\n";
echo "\t<option value=\"[]\"", ($_REQUEST['array'] == '[]') ? ' selected="selected"' : '', ">[ ]</option>\n";
echo "</select></td>\n";
$predefined_size_types = array_intersect($data->predefined_size_types, $types_for_js);
foreach($predefined_size_types as $value) {
$escaped_predef_types[] = "'{$value}'";
}
echo "<td><input name=\"length\" id=\"lengths\" size=\"8\" value=\"",
htmlspecialchars($_REQUEST['length']), "\" /></td>\n";
} else {
// Otherwise draw the read-only type name
echo "<td>", $misc->printVal($data->formatType($column->fields['type'], $column->fields['atttypmod'])), "</td>\n";
}
echo "<td><input type=\"checkbox\" name=\"notnull\"", (isset($_REQUEST['notnull'])) ? ' checked="checked"' : '', " /></td>\n";
echo "<td><input name=\"default\" size=\"20\" value=\"",
htmlspecialchars($_REQUEST['default']), "\" /></td>\n";
echo "<td><input name=\"comment\" size=\"40\" value=\"",
htmlspecialchars($_REQUEST['comment']), "\" /></td></tr>\n";
echo "</table>\n";
echo "<p><input type=\"hidden\" name=\"action\" value=\"properties\" />\n";
echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
echo $misc->form;
echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
echo "<input type=\"hidden\" name=\"column\" value=\"", htmlspecialchars($_REQUEST['column']), "\" />\n";
echo "<input type=\"hidden\" name=\"olddefault\" value=\"", htmlspecialchars($_REQUEST['olddefault']), "\" />\n";
if ($column->fields['attnotnull']) echo "<input type=\"hidden\" name=\"oldnotnull\" value=\"on\" />\n";
echo "<input type=\"hidden\" name=\"oldtype\" value=\"", htmlspecialchars($data->formatType($column->fields['type'], $column->fields['atttypmod'])), "\" />\n";
// Add hidden variables to suppress error notices if we don't support altering column type
if (!$data->hasAlterColumnType()) {
echo "<input type=\"hidden\" name=\"type\" value=\"", htmlspecialchars($_REQUEST['type']), "\" />\n";
echo "<input type=\"hidden\" name=\"length\" value=\"", htmlspecialchars($_REQUEST['length']), "\" />\n";
echo "<input type=\"hidden\" name=\"array\" value=\"", htmlspecialchars($_REQUEST['array']), "\" />\n";
}
echo "<input type=\"submit\" value=\"{$lang['stralter']}\" />\n";
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
echo "</form>\n";
echo "<script type=\"text/javascript\">predefined_lengths = new Array(". implode(",",$escaped_predef_types) .");checkLengths(document.getElementById('type').value,'');</script>\n";
break;
case 2:
// Check inputs
if (trim($_REQUEST['field']) == '') {
$_REQUEST['stage'] = 1;
doAlter($lang['strcolneedsname']);
return;
}
if (!isset($_REQUEST['length'])) $_REQUEST['length'] = '';
$status = $data->alterColumn($_REQUEST['table'], $_REQUEST['column'], $_REQUEST['field'],
isset($_REQUEST['notnull']), isset($_REQUEST['oldnotnull']),
$_REQUEST['default'], $_REQUEST['olddefault'],
$_REQUEST['type'], $_REQUEST['length'], $_REQUEST['array'], $_REQUEST['oldtype'],
$_REQUEST['comment']);
if ($status == 0) {
if ($_REQUEST['column'] != $_REQUEST['field']) {
$_REQUEST['column'] = $_REQUEST['field'];
$_reload_browser = true;
}
doDefault($lang['strcolumnaltered']);
}
else {
$_REQUEST['stage'] = 1;
doAlter($lang['strcolumnalteredbad']);
return;
}
break;
default:
echo "<p>{$lang['strinvalidparam']}</p>\n";
}
}
/**
* Show default list of columns in the table
*/
function doDefault($msg = '', $isTable = true) {
global $data, $conf, $misc, $tableName;
global $lang;
function attPre(&$rowdata) {
global $data;
$rowdata->fields['+type'] = $data->formatType($rowdata->fields['type'], $rowdata->fields['atttypmod']);
}
if (empty($_REQUEST['column']))
$msg.= "<br/>{$lang['strnoobjects']}";
$misc->printTrail('column');
//$misc->printTitle($lang['strcolprop']);
$misc->printTabs('column','properties');
$misc->printMsg($msg);
if (! empty($_REQUEST['column'])) {
// Get table
$tdata = $data->getTable($tableName);
// Get columns
$attrs = $data->getTableAttributes($tableName, $_REQUEST['column']);
// Show comment if any
if ($attrs->fields['comment'] !== null)
echo "<p class=\"comment\">", $misc->printVal($attrs->fields['comment']), "</p>\n";
$column = array(
'column' => array(
'title' => $lang['strcolumn'],
'field' => field('attname'),
),
'type' => array(
'title' => $lang['strtype'],
'field' => field('+type'),
)
);
if ($isTable) {
$column['notnull'] = array(
'title' => $lang['strnotnull'],
'field' => field('attnotnull'),
'type' => 'bool',
'params'=> array('true' => 'NOT NULL', 'false' => '')
);
$column['default'] = array(
'title' => $lang['strdefault'],
'field' => field('adsrc'),
);
}
$actions=array();
$misc->printTable($attrs, $column, $actions, 'colproperties-colproperties', null, 'attPre');
echo "<br />\n";
$f_attname = $_REQUEST['column'];
$f_table = $tableName;
$f_schema = $data->_schema;
$data->fieldClean($f_attname);
$data->fieldClean($f_table);
$data->fieldClean($f_schema);
$query = "SELECT \"{$f_attname}\", count(*) AS \"count\" FROM \"{$f_schema}\".\"{$f_table}\" GROUP BY \"{$f_attname}\" ORDER BY \"{$f_attname}\"";
if ($isTable) {
/* Browse link */
/* FIXME browsing a col should somehow be a action so we don't
* send an ugly SQL in the URL */
$navlinks = array (
'browse' => array (
'attr'=> array (
'href' => array (
'url' => 'display.php',
'urlvars' => array (
'subject' => 'column',
'server' => $_REQUEST['server'],
'database' => $_REQUEST['database'],
'schema' => $_REQUEST['schema'],
'table' => $tableName,
'column' => $_REQUEST['column'],
'return' => 'column',
'query' => $query
)
)
),
'content' => $lang['strbrowse'],
),
'alter' => array (
'attr'=> array (
'href' => array (
'url' => 'colproperties.php',
'urlvars' => array (
'action' => 'properties',
'server' => $_REQUEST['server'],
'database' => $_REQUEST['database'],
'schema' => $_REQUEST['schema'],
'table' => $tableName,
'column' => $_REQUEST['column'],
)
)
),
'content' => $lang['stralter'],
),
'drop' => array (
'attr'=> array (
'href' => array (
'url' => 'tblproperties.php',
'urlvars' => array (
'action' => 'confirm_drop',
'server' => $_REQUEST['server'],
'database' => $_REQUEST['database'],
'schema' => $_REQUEST['schema'],
'table' => $tableName,
'column' => $_REQUEST['column'],
)
)
),
'content' => $lang['strdrop'],
)
);
}
else {
/* Browse link */
$navlinks = array (
'browse' => array (
'attr'=> array (
'href' => array (
'url' => 'display.php',
'urlvars' => array (
'subject' => 'column',
'server' => $_REQUEST['server'],
'database' => $_REQUEST['database'],
'schema' => $_REQUEST['schema'],
'view' => $tableName,
'column' => $_REQUEST['column'],
'return' => 'column',
'query' => $query
)
)
),
'content' => $lang['strbrowse']
)
);
}
$misc->printNavLinks($navlinks, 'colproperties-colproperties', get_defined_vars());
}
}
$misc->printHeader($lang['strtables'] . ' - ' . $tableName);
$misc->printBody();
if (isset($_REQUEST['view']))
doDefault(null, false);
else
switch ($action) {
case 'properties':
if (isset($_POST['cancel'])) doDefault();
else doAlter();
break;
default:
doDefault();
break;
}
$misc->printFooter();
?>