Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Support specifying the database key length (see #7771).
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Apr 20, 2015
1 parent 91c526a commit 6570be8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Resources/contao/dca/tl_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'id' => 'primary',
'pid' => 'index',
'uuid' => 'unique',
'path' => 'index', // not unique (see #7725)
'path' => 'index(333)', // not unique (see #7725)
'extension' => 'index'
)
)
Expand Down
26 changes: 20 additions & 6 deletions src/Resources/contao/library/Contao/Database/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ public function getFromDb()
}

$return = array();
$quote = function ($item) { return '`' . $item . '`'; };

foreach ($tables as $table)
{
Expand All @@ -439,7 +440,7 @@ public function getFromDb()
foreach ($fields as $field)
{
$name = $field['name'];
$field['name'] = '`'.$field['name'].'`';
$field['name'] = $quote($field['name']);

if ($field['type'] != 'index')
{
Expand Down Expand Up @@ -487,27 +488,40 @@ public function getFromDb()
// Indices
if (isset($field['index']) && $field['index_fields'])
{
$index_fields = implode('`, `', $field['index_fields']);
// Quote the field names
$index_fields = implode(', ', array_map
(
function ($item) use ($quote) {
if (strpos($item, '(') === false) {
return $quote($item);
}

list($name, $length) = explode('(', rtrim($item, ')'));

return $quote($name) . '(' . $length . ')';
},
$field['index_fields'])
);

switch ($field['index'])
{
case 'UNIQUE':
if ($name == 'PRIMARY')
{
$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'PRIMARY KEY (`'.$index_fields.'`)';
$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'PRIMARY KEY ('.$index_fields.')';
}
else
{
$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'UNIQUE KEY `'.$name.'` (`'.$index_fields.'`)';
$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'UNIQUE KEY `'.$name.'` ('.$index_fields.')';
}
break;

case 'FULLTEXT':
$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'FULLTEXT KEY `'.$name.'` (`'.$index_fields.'`)';
$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'FULLTEXT KEY `'.$name.'` ('.$index_fields.')';
break;

default:
$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'KEY `'.$name.'` (`'.$index_fields.'`)';
$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'KEY `'.$name.'` ('.$index_fields.')';
break;
}

Expand Down
9 changes: 8 additions & 1 deletion src/Resources/contao/library/Contao/Database/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,16 @@ protected function list_fields($strTable)

while ($objIndex->next())
{
$strColumnName = $objIndex->Column_name;

if ($objIndex->Sub_part != '')
{
$strColumnName .= '(' . $objIndex->Sub_part . ')';
}

$arrReturn[$objIndex->Key_name]['name'] = $objIndex->Key_name;
$arrReturn[$objIndex->Key_name]['type'] = 'index';
$arrReturn[$objIndex->Key_name]['index_fields'][] = $objIndex->Column_name;
$arrReturn[$objIndex->Key_name]['index_fields'][] = $strColumnName;
$arrReturn[$objIndex->Key_name]['index'] = (($objIndex->Non_unique == 0) ? 'UNIQUE' : 'KEY');
}

Expand Down
9 changes: 8 additions & 1 deletion src/Resources/contao/library/Contao/Database/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,16 @@ protected function list_fields($strTable)

while ($objIndex->next())
{
$strColumnName = $objIndex->Column_name;

if ($objIndex->Sub_part != '')
{
$strColumnName .= '(' . $objIndex->Sub_part . ')';
}

$arrReturn[$objIndex->Key_name]['name'] = $objIndex->Key_name;
$arrReturn[$objIndex->Key_name]['type'] = 'index';
$arrReturn[$objIndex->Key_name]['index_fields'][] = $objIndex->Column_name;
$arrReturn[$objIndex->Key_name]['index_fields'][] = $strColumnName;
$arrReturn[$objIndex->Key_name]['index'] = (($objIndex->Non_unique == 0) ? 'UNIQUE' : 'KEY');
}

Expand Down
19 changes: 14 additions & 5 deletions src/Resources/contao/library/Contao/DcaExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,32 +306,41 @@ public function getDbInstallerArray()
$return['TABLE_FIELDS'][$k] = '`' . $k . '` ' . $v;
}

$quote = function ($item) { return '`' . $item . '`'; };

// Keys
foreach ($this->arrKeys as $k=>$v)
{
// Handle multi-column indexes (see #5556)
if (strpos($k, ',') !== false)
{
$f = trimsplit(',', $k);
$f = array_map($quote, trimsplit(',', $k));
$k = str_replace(',', '_', $k);
}
else
{
$f = array($k);
$f = array($quote($k));
}

// Handle key lengths (see #221)
if (preg_match('/\([0-9]+\)/', $v))
{
list($v, $length) = explode('(', rtrim($v, ')'));
$f = array($quote($k) . '(' . $length . ')');
}

if ($v == 'primary')
{
$k = 'PRIMARY';
$v = 'PRIMARY KEY (`' . implode('`, `', $f) . '`)';
$v = 'PRIMARY KEY (' . implode(', ', $f) . ')';
}
elseif ($v == 'index')
{
$v = 'KEY `' . $k . '` (`' . implode('`, `', $f) . '`)';
$v = 'KEY `' . $k . '` (' . implode(', ', $f) . ')';
}
else
{
$v = strtoupper($v) . ' KEY `' . $k . '` (`' . implode('`, `', $f) . '`)';
$v = strtoupper($v) . ' KEY `' . $k . '` (' . implode(', ', $f) . ')';
}

$return['TABLE_CREATE_DEFINITIONS'][$k] = $v;
Expand Down

0 comments on commit 6570be8

Please sign in to comment.