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\MySql;
9:
10: use Aurora\System\Db;
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: * @var bool
24: */
25: protected $bNotNullWithOutDefault;
26:
27: /**
28: * @param string $sValue
29: * @param bool $bWithOutQuote = false
30: * @param bool $bSearch = false
31: * @return string
32: */
33: public function EscapeString($sValue, $bWithOutQuote = false, $bSearch = false)
34: {
35: $sResult = '';
36: if ($bWithOutQuote) {
37: $sResult = addslashes($sValue);
38: } else {
39: $sResult = 0 === strlen($sValue) ? '\'\'' : '\''.addslashes($sValue).'\'';
40: }
41:
42: if ($bSearch) {
43: $sResult = str_replace(array("%", "_"), array("\\%", "\\_"), $sResult);
44: }
45:
46: return $sResult;
47: }
48:
49: /**
50: * @param string $sValue
51: * @return string
52: */
53: public function EscapeColumn($sValue)
54: {
55: return 0 === strlen($sValue) ? $sValue : '`'.$sValue.'`';
56: }
57:
58: /**
59: * @param int $iTimeStamp
60: * @param bool $bAsInsert = false
61: * @return string
62: */
63: public function TimeStampToDateFormat($iTimeStamp, $bAsInsert = false)
64: {
65: $sResult = (string) gmdate('Y-m-d H:i:s', $iTimeStamp);
66: return ($bAsInsert) ? $this->UpdateDateFormat($sResult) : $sResult;
67: }
68:
69: /**
70: * @param string $sFieldName
71: * @return string
72: */
73: public function GetDateFormat($sFieldName)
74: {
75: return 'DATE_FORMAT('.$sFieldName.', "%Y-%m-%d %T")';
76: }
77:
78: /**
79: * @param string $sFieldName
80: * @return string
81: */
82: public function UpdateDateFormat($sFieldName)
83: {
84: return $this->EscapeString($sFieldName);
85: }
86:
87: /**
88: * @param string $sName
89: * @param int $iFieldType
90: * @return string
91: */
92: public function FieldToString($sName, $iFieldType, $mDefault = null, $iCustomLen = null, $bNotNullWithOutDefault = false)
93: {
94: $sResult = $this->EscapeColumn($sName).' ';
95: switch ($iFieldType) {
96: case Db\Field::AUTO_INT:
97: $sResult .= 'int(11) NOT NULL auto_increment';
98: break;
99: case Db\Field::AUTO_INT_BIG:
100: $sResult .= 'bigint(20) NOT NULL auto_increment';
101: break;
102: case Db\Field::AUTO_INT_UNSIGNED:
103: $sResult .= 'int(11) unsigned NOT NULL auto_increment';
104: break;
105: case Db\Field::AUTO_INT_BIG_UNSIGNED:
106: $sResult .= 'bigint(20) unsigned NOT NULL auto_increment';
107: break;
108:
109: case Db\Field::BIT:
110: $sResult .= 'tinyint(1)';
111: break;
112: case Db\Field::INT:
113: $sResult .= 'int(11)';
114: break;
115: case Db\Field::INT_UNSIGNED:
116: $sResult .= 'int(11) unsigned';
117: break;
118: case Db\Field::INT_SHORT:
119: $sResult .= 'tinyint(4)';
120: break;
121: case Db\Field::INT_SHORT_SMALL:
122: $sResult .= 'tinyint(2)';
123: break;
124: case Db\Field::INT_SMALL:
125: $sResult .= 'smallint(6)';
126: break;
127: case Db\Field::INT_BIG:
128: $sResult .= 'bigint(20)';
129: break;
130: case Db\Field::INT_UNSIGNED:
131: $sResult .= 'int(11) UNSIGNED';
132: break;
133: case Db\Field::INT_BIG_UNSIGNED:
134: $sResult .= 'bigint UNSIGNED';
135: break;
136:
137: case Db\Field::CHAR:
138: $sResult .= 'varchar(1)';
139: break;
140: case Db\Field::VAR_CHAR:
141: $sResult .= (null === $iCustomLen)
142: ? 'varchar(255)' : 'varchar('.((int) $iCustomLen).')';
143: break;
144: case Db\Field::TEXT:
145: $sResult .= 'text';
146: break;
147: case Db\Field::TEXT_LONG:
148: $sResult .= 'longtext';
149: break;
150: case Db\Field::TEXT_MEDIUM:
151: $sResult .= 'mediumtext';
152: break;
153: case Db\Field::BLOB:
154: $sResult .= 'blob';
155: break;
156: case Db\Field::BLOB_LONG:
157: $sResult .= 'longblob';
158: break;
159:
160: case Db\Field::DATETIME:
161: $sResult .= 'datetime';
162: break;
163: }
164:
165: if (in_array($iFieldType, array(Db\Field::AUTO_INT, Db\Field::AUTO_INT_BIG,
166: Db\Field::AUTO_INT_UNSIGNED, Db\Field::AUTO_INT_BIG_UNSIGNED,
167: Db\Field::TEXT, Db\Field::TEXT_LONG, Db\Field::BLOB, Db\Field::BLOB_LONG))) {
168: // no need default
169: } elseif (null !== $mDefault) {
170: $sResult .= ' NOT NULL default ';
171: if (is_string($mDefault)) {
172: $sResult .= $this->EscapeString($mDefault);
173: } elseif (is_numeric($mDefault)) {
174: $sResult .= (string) $mDefault;
175: }
176: } else {
177: $sResult .= $this->bNotNullWithOutDefault ? ' NOT NULL' : ' default NULL';
178: }
179:
180: return trim($sResult);
181: }
182:
183: /**
184: * @return string
185: */
186: public function CreateTableLastLine()
187: {
188: return '/*!40101 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci */';
189: }
190: }
191: