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\Pdo\MySql;
9:
10: use Aurora\System\Db\Field;
11:
12: /**
13: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
14: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
15: * @copyright Copyright (c) 2019, Afterlogic Corp.
16: *
17: * @package Api
18: * @subpackage Db
19: */
20: class Helper implements \Aurora\System\Db\IHelper
21: {
22: /**
23: * @param string $sValue
24: * @param bool $bWithOutQuote = false
25: * @param bool $bSearch = false
26: * @return string
27: */
28: public function EscapeString($sValue, $bWithOutQuote = false, $bSearch = false)
29: {
30: $sResult = '';
31: if ($bWithOutQuote) {
32: $sResult = addslashes($sValue);
33: } else {
34: $sResult = 0 === strlen($sValue) ? '\'\'' : '\''.addslashes($sValue).'\'';
35: }
36:
37: if ($bSearch) {
38: $sResult = str_replace(array("%", "_"), array("\\%", "\\_"), $sResult);
39: }
40:
41: return $sResult;
42: }
43:
44: /**
45: * @param string $sValue
46: * @return string
47: */
48: public function EscapeColumn($sValue)
49: {
50: return 0 === strlen($sValue) ? $sValue : '`'.$sValue.'`';
51: }
52:
53: /**
54: * @param int $iTimeStamp
55: * @param bool $bAsInsert = false
56: * @return string
57: */
58: public function TimeStampToDateFormat($iTimeStamp, $bAsInsert = false)
59: {
60: $sResult = (string) gmdate('Y-m-d H:i:s', $iTimeStamp);
61: return ($bAsInsert) ? $this->UpdateDateFormat($sResult) : $sResult;
62: }
63:
64: /**
65: * @param string $sFieldName
66: * @return string
67: */
68: public function GetDateFormat($sFieldName)
69: {
70: return 'DATE_FORMAT('.$sFieldName.', "%Y-%m-%d %T")';
71: }
72:
73: /**
74: * @param string $sFieldName
75: * @return string
76: */
77: public function UpdateDateFormat($sFieldName)
78: {
79: return $this->EscapeString($sFieldName);
80: }
81:
82: /**
83: * @return bool
84: */
85: public function UseSingleIndexRequest()
86: {
87: return false;
88: }
89:
90: /**
91: * @return string
92: */
93: public function CreateIndexRequest($iIndexType, $sTableName, $sIndexName, $aFields)
94: {
95: return '';
96: }
97:
98: /**
99: * @return string
100: */
101: public function DropIndexRequest($sIndexesName, $sTableName)
102: {
103: return sprintf('DROP INDEX %s ON %s', $sIndexesName, $sTableName);
104: }
105:
106: /**
107: * @param string $sName
108: * @param int $iFieldType
109: * @return string
110: */
111: public function FieldToString($sName, $iFieldType, $mDefault = null, $iCustomLen = null, $bNotNullWithOutDefault = false)
112: {
113: $sResult = $this->EscapeColumn($sName).' ';
114: switch ($iFieldType) {
115: case Field::AUTO_INT:
116: $sResult .= 'int(11) NOT NULL auto_increment';
117: break;
118: case Field::AUTO_INT_BIG:
119: $sResult .= 'bigint(20) NOT NULL auto_increment';
120: break;
121: case Field::AUTO_INT_UNSIGNED:
122: $sResult .= 'int(11) unsigned NOT NULL auto_increment';
123: break;
124: case Field::AUTO_INT_BIG_UNSIGNED:
125: $sResult .= 'bigint(20) unsigned NOT NULL auto_increment';
126: break;
127:
128: case Field::BIT:
129: $sResult .= 'tinyint(1)';
130: break;
131: case Field::INT:
132: $sResult .= 'int(11)';
133: break;
134: case Field::INT_UNSIGNED:
135: $sResult .= 'int(11) unsigned';
136: break;
137: case Field::INT_SHORT:
138: $sResult .= 'tinyint(4)';
139: break;
140: case Field::INT_SHORT_SMALL:
141: $sResult .= 'tinyint(2)';
142: break;
143: case Field::INT_SMALL:
144: $sResult .= 'smallint(6)';
145: break;
146: case Field::INT_BIG:
147: $sResult .= 'bigint(20)';
148: break;
149: case Field::INT_UNSIGNED:
150: $sResult .= 'int(11) UNSIGNED';
151: break;
152: case Field::INT_BIG_UNSIGNED:
153: $sResult .= 'bigint UNSIGNED';
154: break;
155:
156: case Field::CHAR:
157: $sResult .= 'varchar(1)';
158: break;
159: case Field::VAR_CHAR:
160: $sResult .= (null === $iCustomLen)
161: ? 'varchar(255)' : 'varchar('.((int) $iCustomLen).')';
162: break;
163: case Field::TEXT:
164: $sResult .= 'text';
165: break;
166: case Field::TEXT_LONG:
167: $sResult .= 'longtext';
168: break;
169: case Field::TEXT_MEDIUM:
170: $sResult .= 'mediumtext';
171: break;
172: case Field::BLOB:
173: $sResult .= 'blob';
174: break;
175: case Field::BLOB_LONG:
176: $sResult .= 'longblob';
177: break;
178:
179: case Field::DATETIME:
180: $sResult .= 'datetime';
181: break;
182: }
183:
184: if (in_array($iFieldType, array(Field::AUTO_INT, Field::AUTO_INT_BIG,
185: Field::AUTO_INT_UNSIGNED, Field::AUTO_INT_BIG_UNSIGNED,
186: Field::TEXT, Field::TEXT_LONG, Field::BLOB, Field::BLOB_LONG))) {
187: // no need default
188: } elseif (null !== $mDefault) {
189: $sResult .= ' NOT NULL default ';
190: if (is_string($mDefault)) {
191: $sResult .= $this->EscapeString($mDefault);
192: } elseif (is_numeric($mDefault)) {
193: $sResult .= (string) $mDefault;
194: }
195: } else {
196: $sResult .= $bNotNullWithOutDefault ? ' NOT NULL' : ' default NULL';
197: }
198:
199: return trim($sResult);
200: }
201:
202: /**
203: * @return string
204: */
205: public function CreateTableLastLine()
206: {
207: return '/*!40101 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci */';
208: }
209:
210: /**
211: * @return string
212: */
213: public function GenerateLastIdSeq($sTableName, $sFiledName)
214: {
215: return null;
216: }
217: }
218: