1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package toolkit |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
trait DatabaseKeyDefinition |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @internal |
11
|
|
|
* Given a field name valid field $k, this methods build a key definition |
12
|
|
|
* SQL part from an array of options. It will use the array $options to generate |
13
|
|
|
* the a complete SQL definition part, with all its possible properties. |
14
|
|
|
* |
15
|
|
|
* @param string $k |
16
|
|
|
* The name of the key |
17
|
|
|
* @param string|array $options |
18
|
|
|
* All the options needed to properly create the key. |
19
|
|
|
* When the value is a string, it is considered as the key's type. |
20
|
|
|
* @param string $options.type |
21
|
|
|
* The SQL type of the key. |
22
|
|
|
* Valid values are: 'key', 'unique', 'primary', 'fulltext', 'index' |
23
|
|
|
* @param string|array $options.cols |
24
|
|
|
* The list of columns to be included in the key. |
25
|
|
|
* If omitted, the name of the key be added as the only column in the key. |
26
|
|
|
* @return string |
27
|
|
|
* The SQL part containing the key definition. |
28
|
|
|
* @throws DatabaseStatementException |
29
|
|
|
*/ |
30
|
|
|
public function buildKeyDefinitionFromArray($k, $options) |
31
|
|
|
{ |
32
|
|
|
if (is_string($options)) { |
33
|
|
|
$options = ['type' => $options]; |
34
|
|
|
} elseif (!is_array($options)) { |
|
|
|
|
35
|
|
|
throw new DatabaseStatementException('Key value can only be a string or an array'); |
36
|
|
|
} elseif (!isset($options['type'])) { |
37
|
|
|
throw new DatabaseStatementException('Key type must be defined.'); |
38
|
|
|
} |
39
|
|
|
$type = strtolower($options['type']); |
40
|
|
|
$k = $this->asTickedString($k); |
|
|
|
|
41
|
|
|
$cols = isset($options['cols']) ? $options['cols'] : $k; |
42
|
|
|
$typeIndex = in_array($type, [ |
43
|
|
|
'key', 'unique', 'primary', 'fulltext', 'index' |
44
|
|
|
]); |
45
|
|
|
if ($typeIndex === false) { |
46
|
|
|
throw new DatabaseStatementException("Key of type `$type` is not valid"); |
47
|
|
|
} |
48
|
|
|
switch ($type) { |
49
|
|
|
case 'unique': |
50
|
|
|
$type = strtoupper($type) . ' KEY'; |
51
|
|
|
break; |
52
|
|
|
case 'primary': |
53
|
|
|
// Use the key name as the KEY keyword |
54
|
|
|
// since the primary key does not have a name |
55
|
|
|
$k = 'KEY'; |
56
|
|
|
// fall through |
57
|
|
|
default: |
58
|
|
|
$type = strtoupper($type); |
59
|
|
|
break; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Format columns including size |
63
|
|
|
$colsFormatted = []; |
64
|
|
|
|
65
|
|
|
if (!is_array($cols)) { |
66
|
|
|
$cols = [$cols]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($cols as $key => $value) { |
70
|
|
|
// No Size |
71
|
|
|
if (General::intval($key) !== -1) { |
72
|
|
|
$colsFormatted[] = $this->asTickedString($value); |
73
|
|
|
// Size |
74
|
|
|
} else { |
75
|
|
|
$colsFormatted[] = $this->asTickedString($key) . '(' . $value . ')'; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
$colsFormatted = implode(self::LIST_DELIMITER, $colsFormatted); |
|
|
|
|
79
|
|
|
|
80
|
|
|
return "$type $k ($colsFormatted)"; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|