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