From 42d8f3f0a117dce29ab3fe30ef46fafb419224e3 Mon Sep 17 00:00:00 2001 From: ikkez Date: Tue, 15 May 2018 15:03:21 +0200 Subject: [PATCH] add failsafe for index keys longer than 64 chars --- lib/db/sql/schema.php | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/db/sql/schema.php b/lib/db/sql/schema.php index 2e1f91f..96a7818 100644 --- a/lib/db/sql/schema.php +++ b/lib/db/sql/schema.php @@ -18,8 +18,8 @@ * https://github.com/ikkez/F3-Sugar/ * * @package DB - * @version 2.2.2 - * @date 06.03.2018 + * @version 2.2.3 + * @date 15.05.2018 **/ @@ -375,7 +375,8 @@ protected function _addIndex($index_cols, $search_cols, $unique, $length) if(strtoupper($search_cols[$index_cols[$i]]['type']) == 'TEXT') $col.='('.$length.')'; $cols = implode(',', $quotedCols); - $name = $this->db->quotekey($this->name.'___'.implode('__', $index_cols)); + $name = $this->assembleIndexKey($index_cols,$this->name); + $name = $this->db->quotekey($name); $table = $this->db->quotekey($this->name); $index = $unique ? 'UNIQUE INDEX' : 'INDEX'; $cmd = array( @@ -388,6 +389,22 @@ protected function _addIndex($index_cols, $search_cols, $unique, $length) $this->queries[] = $query; } + /** + * create index name from one or more given column names, max. 64 char lengths + * @param string|array $index_cols + * @return string + */ + protected function assembleIndexKey($index_cols,$table_name) { + if (!is_array($index_cols)) + $index_cols = array($index_cols); + $name = $table_name.'___'.implode('__', $index_cols); + if (strlen($name)>64) + $name=$table_name.'___'.\Base::instance()->hash(implode('__', $index_cols)); + if (strlen($name)>64) + $name='___'.\Base::instance()->hash($table_name.'___'.implode('__', $index_cols)); + return $name; + } + /** * set primary / composite key to table * @param string|array $pkeys @@ -651,9 +668,6 @@ protected function _sqlite_rebuild($exec=true) unset($existing_columns[$name]); // drop index foreach (array_keys($indexes) as $col) { - // for backward compatibility - if ($col == $name) - unset($indexes[$name]); // new index names if ($col == $this->name.'___'.$name) unset($indexes[$this->name.'___'.$name]); @@ -663,11 +677,10 @@ protected function _sqlite_rebuild($exec=true) $col = explode('___', $col); $ci = explode('__', $col[1]); $col = implode('___',$col); - } else // for backward compatibility - $ci = explode('__', $col); - // drop combined index - if (in_array($name, $ci)) - unset($indexes[$col]); + // drop combined index + if (in_array($name, $ci)) + unset($indexes[$col]); + } } } }