diff --git a/etemplate/inc/class.db_tools.inc.php b/etemplate/inc/class.db_tools.inc.php index cac27e8c18..5b86305b61 100644 --- a/etemplate/inc/class.db_tools.inc.php +++ b/etemplate/inc/class.db_tools.inc.php @@ -44,13 +44,13 @@ var $setup_header = 'app = $GLOBALS['HTTP_GET_VARS']['app']; + $this->app = $_GET['app']; } if (is_array($content)) { @@ -192,6 +194,8 @@ ExecMethod('etemplate.editor.edit'); return; } + $add_index = isset($content['add_index']); + // from here on, filling new content for eTemplate $content = array( 'msg' => $msg, @@ -208,7 +212,7 @@ ); if ($this->table != '' && isset($this->data[$this->table])) { - $content += $this->table2content($this->data[$this->table]); + $content += $this->table2content($this->data[$this->table],$sel_options['Index'],$add_index); } $no_button = array( ); if (!$this->app || !$this->table) @@ -315,6 +319,35 @@ return True; // dont continue in edit } + /*! + @function has_single_index + @syntax has_single_index( $col,$index ) + @author ralfbecker + @abstract checks if there is an index (only) on $col (not a multiple index incl. $col) + @param $col column name + @param $index ix or uc array of table-defintion + @result True if $col has a single index + */ + function has_single_index($col,$index,&$options) + { + foreach($index as $in) + { + if ($in == $col || is_array($in) && $in[0] == $col && !isset($in[1])) + { + if ($in != $col && isset($in['options'])) + { + foreach($in['options'] as $db => $opts) + { + $options[] = $db.'('.(is_array($opts)?implode(',',$opts):$opts).')'; + } + $options = implode(', ',$options); + } + return True; + } + } + return False; + } + /*! @function table2content @syntax table2content( $table ) @@ -323,15 +356,15 @@ @param $table table-definition, eg. $phpgw_baseline[$table_name] @result content-array */ - function table2content($table) + function table2content($table,&$columns,$extra_index=False) { - $content = array(); + $content = $columns = array(); for ($n = 1; list($col_name,$col_defs) = each($table['fd']); ++$n) { $col_defs['name'] = $col_name; $col_defs['pk'] = in_array($col_name,$table['pk']); - $col_defs['uc'] = in_array($col_name,$table['uc']); - $col_defs['ix'] = in_array($col_name,$table['ix']); + $col_defs['uc'] = $this->has_single_index($col_name,$table['uc'],$col_defs['options']); + $col_defs['ix'] = $this->has_single_index($col_name,$table['ix'],$col_defs['options']); $col_defs['fk'] = $table['fk'][$col_name]; if (isset($col_defs['default']) && $col_defs['default'] == '') { @@ -342,10 +375,34 @@ $col_defs['n'] = $n; $content["Row$n"] = $col_defs; + + $columns[$n] = $col_name; + } + $n = 2; + foreach(array('uc','ix') as $type) + { + foreach($table[$type] as $index) + { + if (is_array($index) && isset($index[1])) // multicolum index + { + $content['Index'][$n]['unique'] = $type == 'uc'; + $content['Index'][$n]['n'] = $n - 1; + foreach($index as $col) + { + $content['Index'][$n][] = array_search($col,$columns); + } + ++$n; + } + } + } + if ($extra_index) + { + $content['Index'][$n]['n'] = $n-1; } if ($this->debug >= 3) { - echo "
table2content: content ="; _debug_array($content); + echo "
table2content(,,'$extra_index'): content ="; _debug_array($content); + echo "
columns ="; _debug_array($columns);
}
return $content;
}
@@ -392,7 +449,7 @@
{
switch ($col['type']) // set some defaults for precision, else setup fails
{
- case 'float':
+ case 'float':
case 'int': $col['precision'] = 4; break;
case 'char': $col['precision'] = 1; break;
case 'varchar': $col['precision'] = 255; break;
@@ -423,7 +480,23 @@
case 'ix':
if ($val)
{
- $table[$prop][] = $name;
+ if ($col['options'])
+ {
+ $opts = array();
+ foreach(explode(',',$col['options']) as $opt)
+ {
+ list($db,$opt) = split('[(:)]',$opt);
+ $opts[$db] = is_numeric($opt) ? intval($opt) : $opt;
+ }
+ $table[$prop][] = array(
+ $name,
+ 'options' => $opts
+ );
+ }
+ else
+ {
+ $table[$prop][] = $name;
+ }
}
break;
case 'fk':
@@ -434,6 +507,29 @@
break;
}
}
+ $num2col[$n] = $col['name'];
+ }
+ }
+ foreach($content['Index'] as $n => $index)
+ {
+ $idx_arr = array();
+ foreach($index as $key => $num)
+ {
+ if (is_numeric($key) && $num && @$num2col[$num])
+ {
+ $idx_arr[] = $num2col[$num];
+ }
+ }
+ if (count($idx_arr) && !isset($content['delete_index'][$n]))
+ {
+ if ($index['unique'])
+ {
+ $table['uc'][] = $idx_arr;
+ }
+ else
+ {
+ $table['ix'][] = $idx_arr;
+ }
}
}
if ($this->debug >= 2)
@@ -480,10 +576,6 @@
if (in_array($parent,array('pk','fk','ix','uc')))
{
$depth = 0;
- if ($parent != 'fk')
- {
- $only_vals = True;
- }
}
if ($depth)
{
@@ -499,17 +591,17 @@
$n = 0;
foreach($arr as $key => $val)
{
- if (!$only_vals)
+ if (!is_int($key))
{
$def .= "'$key' => ";
}
if (is_array($val))
{
- $def .= $this->write_array($val,$parent == 'fd' ? 0 : $depth,$key,$only_vals);
+ $def .= $this->write_array($val,$parent == 'fd' ? 0 : $depth,$key);
}
else
{
- if (!$only_vals && $key == 'nullable')
+ if (!$only_vals && $key === 'nullable')
{
$def .= $val ? 'True' : 'False';
}
@@ -857,6 +949,37 @@
return $update;
}
+ /*!
+ @function normalize_index
+ @abstract orders the single-colum-indices after the columns and the multicolunm ones bedind
+ @syntax normalize_index( $index,$cols )
+ @param index array with indices
+ @param cols array with column-defs (col-name in key)
+ @author ralfbecker
+ @result the new array
+ */
+ function normalize_index($index,$cols)
+ {
+ $normalized = array();
+ foreach($cols as $col => $data)
+ {
+ foreach($index as $n => $idx)
+ {
+ if ($idx == $col || is_array($idx) && $idx[0] == $col && !isset($idx[1]))
+ {
+ $normalized[] = isset($idx['options']) ? $idx : $col;
+ unset($index[$n]);
+ break;
+ }
+ }
+ }
+ foreach($index as $idx)
+ {
+ $normalized[] = $idx;
+ }
+ return $normalized;
+ }
+
/*!
@function normalize
@syntax normalize( $table )
@@ -882,8 +1005,8 @@
'fd' => $table['fd'],
'pk' => $table['pk'],
'fk' => $table['fk'],
- 'ix' => $table['ix'],
- 'uc' => $table['uc']
+ 'ix' => $this->normalize_index($table['ix'],$table['fd']),
+ 'uc' => $this->normalize_index($table['uc'],$table['fd'])
);
}
diff --git a/etemplate/setup/etemplates.inc.php b/etemplate/setup/etemplates.inc.php
index a933a783a5..2e7fd937c6 100644
--- a/etemplate/setup/etemplates.inc.php
+++ b/etemplate/setup/etemplates.inc.php
@@ -1,5 +1,5 @@
'etemplate.nextmatch_widget.nm_row','template' =
$templ_data[] = array('name' => 'etemplate.db-tools.cols','template' => '','lang' => '','group' => '0','version' => '0.9.15.002','data' => 'a:3:{i:0;a:2:{s:2:\"c1\";s:3:\"nmh\";s:2:\"c2\";s:3:\"nmr\";}i:1;a:12:{s:1:\"A\";a:4:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:1:\"#\";s:7:\"no_lang\";s:1:\"1\";s:5:\"align\";s:6:\"center\";}s:1:\"B\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:10:\"ColumnName\";}s:1:\"C\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:4:\"Type\";}s:1:\"D\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:9:\"Precision\";}s:1:\"E\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:5:\"Scale\";}s:1:\"F\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:8:\"NOT NULL\";}s:1:\"G\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:6:\"Unique\";}s:1:\"H\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:11:\"Primary Key\";}s:1:\"I\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:7:\"Indexed\";}s:1:\"J\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:11:\"Foreign Key\";}s:1:\"K\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:7:\"Default\";}s:1:\"L\";a:5:{s:4:\"type\";s:6:\"button\";s:5:\"label\";s:10:\"Add Column\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:10:\"add_column\";s:4:\"help\";s:42:\"Add a new column (after the existing ones)\";}}i:2;a:12:{s:1:\"A\";a:4:{s:4:\"type\";s:5:\"label\";s:7:\"no_lang\";s:1:\"1\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:12:\"Row${row}[n]\";}s:1:\"B\";a:5:{s:4:\"type\";s:4:\"text\";s:4:\"size\";s:2:\"20\";s:7:\"no_lang\";s:1:\"1\";s:4:\"name\";s:15:\"Row${row}[name]\";s:4:\"help\";s:127:\"need to be unique in the table and no reseved word from SQL, best prefix all with a common 2-digit short for the app, eg. \'et_\'\";}s:1:\"C\";a:4:{s:4:\"type\";s:6:\"select\";s:7:\"no_lang\";s:1:\"1\";s:4:\"name\";s:15:\"Row${row}[type]\";s:4:\"help\";s:18:\"type of the column\";}s:1:\"D\";a:4:{s:4:\"type\";s:3:\"int\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:20:\"Row${row}[precision]\";s:4:\"help\";s:64:\"length for char+varchar, precisions int: 2, 4, 8 and float: 4, 8\";}s:1:\"E\";a:3:{s:4:\"type\";s:3:\"int\";s:4:\"name\";s:16:\"Row${row}[scale]\";s:4:\"help\";s:15:\"scale for float\";}s:1:\"F\";a:4:{s:4:\"type\";s:8:\"checkbox\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:18:\"Row${row}[notnull]\";s:4:\"help\";s:35:\"can not have special SQL-value NULL\";}s:1:\"G\";a:4:{s:4:\"type\";s:8:\"checkbox\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:13:\"Row${row}[uc]\";s:4:\"help\";s:59:\"DB ensures that every row has a unique value in that column\";}s:1:\"H\";a:4:{s:4:\"type\";s:8:\"checkbox\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:13:\"Row${row}[pk]\";s:4:\"help\";s:52:\"Primary key for the table, gets automaticaly indexed\";}s:1:\"I\";a:4:{s:4:\"type\";s:8:\"checkbox\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:13:\"Row${row}[ix]\";s:4:\"help\";s:81:\"an indexed column speeds up querys using that column (cost space on the disk !!!)\";}s:1:\"J\";a:5:{s:4:\"type\";s:4:\"text\";s:4:\"size\";s:2:\"20\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:13:\"Row${row}[fk]\";s:4:\"help\";s:46:\"name of other table where column is a key from\";}s:1:\"K\";a:4:{s:4:\"type\";s:4:\"text\";s:4:\"size\";s:2:\"20\";s:4:\"name\";s:18:\"Row${row}[default]\";s:4:\"help\";s:54:\"enter \'\' for an empty default, nothing mean no default\";}s:1:\"L\";a:5:{s:4:\"type\";s:6:\"button\";s:5:\"label\";s:13:\"Delete Column\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:12:\"delete[$row]\";s:4:\"help\";s:19:\"Deletes this column\";}}}','size' => '','style' => '','modified' => '1064452548',);
+$templ_data[] = array('name' => 'etemplate.db-tools.cols','template' => '','lang' => '','group' => '0','version' => '0.9.15.003','data' => 'a:3:{i:0;a:2:{s:2:\"c1\";s:3:\"nmh\";s:2:\"c2\";s:3:\"nmr\";}i:1;a:13:{s:1:\"A\";a:4:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:1:\"#\";s:7:\"no_lang\";s:1:\"1\";s:5:\"align\";s:6:\"center\";}s:1:\"B\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:10:\"ColumnName\";}s:1:\"C\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:4:\"Type\";}s:1:\"D\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:9:\"Precision\";}s:1:\"E\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:5:\"Scale\";}s:1:\"F\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:8:\"NOT NULL\";}s:1:\"G\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:11:\"Primary Key\";}s:1:\"H\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:6:\"Unique\";}s:1:\"I\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:7:\"Indexed\";}s:1:\"J\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:12:\"Indexoptions\";}s:1:\"K\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:11:\"Foreign Key\";}s:1:\"L\";a:2:{s:4:\"type\";s:5:\"label\";s:5:\"label\";s:7:\"Default\";}s:1:\"M\";a:5:{s:4:\"type\";s:6:\"button\";s:5:\"label\";s:10:\"Add Column\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:10:\"add_column\";s:4:\"help\";s:42:\"Add a new column (after the existing ones)\";}}i:2;a:13:{s:1:\"A\";a:4:{s:4:\"type\";s:5:\"label\";s:7:\"no_lang\";s:1:\"1\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:12:\"Row${row}[n]\";}s:1:\"B\";a:5:{s:4:\"type\";s:4:\"text\";s:4:\"size\";s:2:\"15\";s:7:\"no_lang\";s:1:\"1\";s:4:\"name\";s:15:\"Row${row}[name]\";s:4:\"help\";s:127:\"need to be unique in the table and no reseved word from SQL, best prefix all with a common 2-digit short for the app, eg. \'et_\'\";}s:1:\"C\";a:4:{s:4:\"type\";s:6:\"select\";s:7:\"no_lang\";s:1:\"1\";s:4:\"name\";s:15:\"Row${row}[type]\";s:4:\"help\";s:18:\"type of the column\";}s:1:\"D\";a:4:{s:4:\"type\";s:3:\"int\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:20:\"Row${row}[precision]\";s:4:\"help\";s:64:\"length for char+varchar, precisions int: 2, 4, 8 and float: 4, 8\";}s:1:\"E\";a:3:{s:4:\"type\";s:3:\"int\";s:4:\"name\";s:16:\"Row${row}[scale]\";s:4:\"help\";s:15:\"scale for float\";}s:1:\"F\";a:4:{s:4:\"type\";s:8:\"checkbox\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:18:\"Row${row}[notnull]\";s:4:\"help\";s:35:\"can not have special SQL-value NULL\";}s:1:\"G\";a:4:{s:4:\"type\";s:8:\"checkbox\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:13:\"Row${row}[pk]\";s:4:\"help\";s:52:\"Primary key for the table, gets automaticaly indexed\";}s:1:\"H\";a:4:{s:4:\"type\";s:8:\"checkbox\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:13:\"Row${row}[uc]\";s:4:\"help\";s:59:\"DB ensures that every row has a unique value in that column\";}s:1:\"I\";a:4:{s:4:\"type\";s:8:\"checkbox\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:13:\"Row${row}[ix]\";s:4:\"help\";s:81:\"an indexed column speeds up querys using that column (cost space on the disk !!!)\";}s:1:\"J\";a:4:{s:4:\"type\";s:4:\"text\";s:4:\"size\";s:2:\"15\";s:4:\"name\";s:18:\"Row${row}[options]\";s:4:\"help\";s:105:\"DB-specific index options (comma-sep.), eg. mysql(FULLTEXT) or mysql(100) for the indexed length of a col\";}s:1:\"K\";a:5:{s:4:\"type\";s:4:\"text\";s:4:\"size\";s:2:\"20\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:13:\"Row${row}[fk]\";s:4:\"help\";s:46:\"name of other table where column is a key from\";}s:1:\"L\";a:4:{s:4:\"type\";s:4:\"text\";s:4:\"size\";s:1:\"8\";s:4:\"name\";s:18:\"Row${row}[default]\";s:4:\"help\";s:54:\"enter \'\' for an empty default, nothing mean no default\";}s:1:\"M\";a:5:{s:4:\"type\";s:6:\"button\";s:5:\"label\";s:13:\"Delete Column\";s:5:\"align\";s:6:\"center\";s:4:\"name\";s:12:\"delete[$row]\";s:4:\"help\";s:19:\"Deletes this column\";}}}','size' => '','style' => '','modified' => '1067163210',);
+
+$templ_data[] = array('name' => 'etemplate.db-tools.edit','template' => '','lang' => '','group' => '0','version' => '0.9.15.002','data' => 'a:4:{i:0;a:1:{s:1:\"D\";s:2:\"1%\";}i:1;a:7:{s:1:\"A\";a:7:{s:4:\"type\";s:10:\"select-app\";s:4:\"size\";s:19:\"Select one ...,,all\";s:5:\"label\";s:11:\"Application\";s:7:\"no_lang\";s:1:\"1\";s:4:\"name\";s:3:\"app\";s:8:\"onchange\";s:1:\"1\";s:4:\"help\";s:40:\"Select an application, (*) = uninstalled\";}s:1:\"B\";a:6:{s:4:\"type\";s:6:\"select\";s:5:\"label\";s:9:\"TableName\";s:7:\"no_lang\";s:1:\"1\";s:4:\"name\";s:10:\"table_name\";s:8:\"onchange\";s:1:\"1\";s:4:\"help\";s:34:\"Select an table of the application\";}s:1:\"C\";a:5:{s:4:\"type\";s:4:\"text\";s:4:\"size\";s:2:\"20\";s:5:\"align\";s:5:\"right\";s:4:\"name\";s:14:\"new_table_name\";s:4:\"help\";s:20:\"Name of table to add\";}s:1:\"D\";a:4:{s:4:\"type\";s:6:\"button\";s:5:\"label\";s:9:\"Add Table\";s:4:\"name\";s:9:\"add_table\";s:4:\"help\";s:38:\"Create a new table for the application\";}s:1:\"E\";a:4:{s:4:\"type\";s:6:\"button\";s:5:\"label\";s:6:\"Import\";s:4:\"name\";s:6:\"import\";s:4:\"help\";s:47:\"Import table-definitions from existing db-table\";}s:1:\"F\";a:5:{s:4:\"type\";s:6:\"button\";s:5:\"label\";s:10:\"Drop Table\";s:4:\"name\";s:10:\"drop_table\";s:8:\"disabled\";s:1:\"1\";s:4:\"help\";s:37:\"Drop a table - this can NOT be undone\";}s:1:\"G\";a:4:{s:4:\"type\";s:6:\"button\";s:5:\"label\";s:12:\"Write Tables\";s:4:\"name\";s:12:\"write_tables\";s:4:\"help\";s:40:\"Write