1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\System\Db;
9:
10: /**
11: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
12: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
13: * @copyright Copyright (c) 2019, Afterlogic Corp.
14: *
15: * @package Api
16: * @subpackage Db
17: */
18: class Key
19: {
20: public const TYPE_KEY = 0;
21: public const TYPE_UNIQUE_KEY = 1;
22: public const TYPE_PRIMARY_KEY = 2;
23: public const TYPE_INDEX = 3;
24: public const TYPE_FULLTEXT = 4;
25:
26: /**
27: * @var int
28: */
29: protected $iType;
30:
31: /**
32: * @var array
33: */
34: protected $aFields;
35:
36: /**
37: * @param string $sName
38: * @param int $iType
39: * @param array $aFields
40: */
41: public function __construct($iType, array $aFields)
42: {
43: $this->iType = $iType;
44: $this->aFields = $aFields;
45: }
46:
47: /**
48: * @return int
49: */
50: public function GetType()
51: {
52: return $this->iType;
53: }
54:
55: /**
56: * @param string $sTableName
57: * @return string
58: */
59: public function GetName($sTableName)
60: {
61: $aList = $this->aFields;
62: sort($aList);
63: return strtoupper($sTableName.'_'.implode('_', $aList).'_INDEX');
64: }
65:
66: /**
67: * @return array
68: */
69: public function GetIndexesFields()
70: {
71: return $this->aFields;
72: }
73:
74: /**
75: * @return string
76: */
77: public function ToString($oHelper, $sTableName)
78: {
79: $sResult = '';
80: if (0 < count($this->aFields)) {
81: switch ($this->iType) {
82: case Key::TYPE_PRIMARY_KEY:
83: $sResult .= 'PRIMARY KEY';
84: break;
85: case Key::TYPE_UNIQUE_KEY:
86: $sResult .= 'UNIQUE '.$oHelper->EscapeColumn($this->GetName($sTableName));
87: break;
88: case Key::TYPE_INDEX:
89: $sResult .= 'INDEX '.$oHelper->EscapeColumn($this->GetName($sTableName));
90: break;
91: // case Key::TYPE_FULLTEXT:
92: // $sResult .= 'FULLTEXT '.$oHelper->EscapeColumn($this->GetName($sTableName));
93: // break;
94: }
95:
96: $aValues = array_map(array(&$oHelper, 'EscapeColumn'), $this->aFields);
97: $sResult .= ' ('.implode(', ', $aValues).')';
98: }
99:
100: return trim($sResult);
101: }
102:
103: /**
104: * @return string
105: */
106: public function ToSingleIndexRequest($oHelper, $sTableName)
107: {
108: return $oHelper->CreateIndexRequest($this->iType, $sTableName, $this->GetName($sTableName), $this->aFields);
109: }
110: }
111: