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 Storage
19: {
20: /**
21: * @var string
22: */
23: protected $sPrefix;
24:
25: /**
26: * @var Sql
27: */
28: protected $oConnector;
29:
30: /**
31: * @var Sql
32: */
33: protected $oSlaveConnector;
34:
35: /**
36: * @var \Aurora\System\Exceptions\DbException
37: */
38: protected $oLastException;
39:
40: /**
41: * @var \Aurora\System\Settings
42: */
43: protected $oSettings;
44:
45: /**
46: * @param \Aurora\System\Settings $oSettings
47: */
48: public function __construct(\Aurora\System\AbstractSettings &$oSettings)
49: {
50: $aConnections =& Creator::CreateConnector($oSettings);
51:
52: $this->oSettings = $oSettings;
53: $this->sPrefix = $this->oSettings->DBPrefix;
54: $this->oConnector = null;
55: $this->oSlaveConnector = null;
56: $this->oLastException = null;
57:
58: if (is_array($aConnections) && 2 === count($aConnections)) {
59: $this->oConnector =& $aConnections[0];
60: if (null !== $aConnections[1]) {
61: $this->oSlaveConnector =& $aConnections[1];
62: }
63: }
64: }
65:
66: /**
67: * @return &Sql
68: */
69: public function &GetConnector()
70: {
71: return $this->oConnector;
72: }
73:
74: /**
75: * @return &Sql
76: */
77: public function &GetSlaveConnector()
78: {
79: return $this->oSlaveConnector;
80: }
81:
82: /**
83: * @return bool
84: */
85: public function IsConnected()
86: {
87: return $this->oConnector->IsConnected();
88: }
89:
90: /**
91: * @return bool
92: */
93: public function Connect()
94: {
95: if (!isset($this->oConnector)) {
96: return false;
97: }
98:
99: if ($this->oConnector->IsConnected()) {
100: return true;
101: }
102:
103: $this->oConnector->ReInitIfNotConnected(
104: $this->oSettings->DBHost,
105: $this->oSettings->DBLogin,
106: $this->oSettings->DBPassword,
107: $this->oSettings->DBName
108: );
109:
110: return $this->oConnector->Connect();
111: }
112:
113: /**
114: * @return bool
115: */
116: public function ConnectSlave()
117: {
118: if ($this->oSlaveConnector->IsConnected()) {
119: return true;
120: }
121:
122: $this->oSlaveConnector->ReInitIfNotConnected(
123: $this->oSettings->DBSlaveHost,
124: $this->oSettings->DBSlaveLogin,
125: $this->oSettings->DBSlavePassword,
126: $this->oSettings->DBSlaveName
127: );
128:
129: return $this->oSlaveConnector->Connect(true, true);
130: }
131:
132: /**
133: * @return bool
134: */
135: public function ConnectNoSelect()
136: {
137: if ($this->oConnector->IsConnected()) {
138: return true;
139: }
140: return $this->oConnector->ConnectNoSelect();
141: }
142:
143: /**
144: * @return bool
145: */
146: public function Disconnect()
147: {
148: $this->oConnector->Disconnect();
149: if ($this->oSlaveConnector) {
150: $this->oSlaveConnector->Disconnect();
151: }
152: return true;
153: }
154:
155: /**
156: * @return bool
157: */
158: public function Select()
159: {
160: return $this->oConnector->Select();
161: }
162:
163: /**
164: * @return bool
165: */
166: public function Execute($sSql)
167: {
168: $bResult = false;
169: if (!empty($sSql)) {
170: if ($this->oSlaveConnector && $this->isSlaveSql($sSql)) {
171: if ($this->ConnectSlave()) {
172: $bResult = $this->oSlaveConnector->Execute($sSql, true);
173: }
174: } else {
175: if ($this->Connect()) {
176: $bResult = $this->oConnector->Execute($sSql);
177: }
178: }
179: }
180:
181: return $bResult;
182: }
183:
184: /**
185: * @param bool $bAutoFree = true
186: * @return bool
187: */
188: public function GetNextArrayRecord($bAutoFree = true)
189: {
190: if ($this->oSlaveConnector) {
191: return $this->oSlaveConnector->GetNextArrayRecord($bAutoFree);
192: }
193: return $this->oConnector->GetNextArrayRecord($bAutoFree);
194: }
195:
196: /**
197: * @param bool $bAutoFree = true
198: * @return bool
199: */
200: public function GetNextRecord($bAutoFree = true)
201: {
202: if ($this->oSlaveConnector) {
203: return $this->oSlaveConnector->GetNextRecord($bAutoFree);
204: }
205:
206: return $this->oConnector->GetNextRecord($bAutoFree);
207: }
208:
209: /**
210: * @return bool
211: */
212: public function FreeResult()
213: {
214: if ($this->oSlaveConnector) {
215: return $this->oSlaveConnector->FreeResult();
216: }
217:
218: if ($this->oConnector) {
219: return $this->oConnector->FreeResult();
220: }
221: }
222:
223: /**
224: * @return array|bool [object]
225: */
226: public function GetResultAsObjects()
227: {
228: $aResult = array();
229: while (false !== ($oRow = $this->GetNextRecord())) {
230: $aResult[] = $oRow;
231: }
232: return $aResult;
233: }
234:
235: /**
236: * @return array|bool [array]
237: */
238: public function GetResultAsAssocArrays()
239: {
240: $aResult = array();
241: while (false !== ($aRow = $this->GetNextArrayRecord())) {
242: $aResult[] = $aRow;
243: }
244: return $aResult;
245: }
246:
247: /**
248: * @param string $sTableName = null
249: * @param string $sFieldName = null
250: * @return int
251: */
252: public function GetLastInsertId($sTableName = null, $sFieldName = null)
253: {
254: return $this->oConnector->GetLastInsertId($sTableName, $sFieldName);
255: }
256:
257: /**
258: * @return int
259: */
260: public function ResultCount()
261: {
262: if ($this->oSlaveConnector) {
263: return $this->oSlaveConnector->ResultCount();
264: }
265: return $this->oConnector->ResultCount();
266: }
267:
268: /**
269: * @return array
270: */
271: public function GetTableNames()
272: {
273: $aResult = false;
274: if ($this->Connect()) {
275: $aResult = $this->oConnector->GetTableNames();
276: }
277: return $aResult;
278: }
279:
280: /**
281: * @param string $sTableName
282: * @return array
283: */
284: public function GetTableFields($sTableName)
285: {
286: $aResult = false;
287: if ($this->Connect()) {
288: $aResult = $this->oConnector->GetTableFields($sTableName);
289: }
290: return $aResult;
291: }
292:
293: /**
294: * @param string $sTableName
295: * @return array
296: */
297: public function GetTableIndexes($sTableName)
298: {
299: $aResult = false;
300: if ($this->Connect()) {
301: $aResult = $this->oConnector->GetTableIndexes($sTableName);
302: }
303: return $aResult;
304: }
305:
306: /**
307: * @return string
308: */
309: public function prefix()
310: {
311: return $this->sPrefix;
312: }
313:
314: /**
315: * @return string
316: */
317: public function GetError()
318: {
319: return '#'.$this->oConnector->ErrorCode.': '.$this->oConnector->ErrorDesc;
320: }
321:
322: /**
323: * @return \Aurora\System\Exceptions\DbException
324: */
325: public function GetException()
326: {
327: return $this->oLastException;
328: }
329:
330: /**
331: * @param \Aurora\System\Exceptions\DbException $oException
332: */
333: public function SetException($oException)
334: {
335: $this->oLastException = $oException;
336: }
337:
338: /**
339: * @param string $sSql
340: * @return bool
341: */
342: protected function isSlaveSql($sSql)
343: {
344: return in_array(strtoupper(substr(trim($sSql), 0, 6)), array('SELECT')) || in_array(strtoupper(substr(trim($sSql), 0, 7)), array('(SELECT'));
345: }
346: }
347: