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: * @static
16: * @package Api
17: * @subpackage Db
18: */
19: class Creator
20: {
21: /**
22: * @var DbMySql;
23: */
24: public static $oDbConnector;
25:
26: /**
27: * @var DbMySql;
28: */
29: public static $oSlaveDbConnector;
30:
31: /**
32: * @var object;
33: */
34: public static $oCommandCreatorHelper;
35:
36: private function __construct()
37: {
38: }
39:
40: /**
41: * @return void
42: */
43: public static function ClearStatic()
44: {
45: self::$oDbConnector = null;
46: self::$oSlaveDbConnector = null;
47: }
48:
49: /**
50: * @param array $aData
51: * @return Sql
52: */
53: public static function ConnectorFabric($aData)
54: {
55: $oConnector = null;
56: if (isset($aData['Type'])) {
57: $iDbType = $aData['Type'];
58:
59: if (isset($aData['DBHost'], $aData['DBLogin'], $aData['DBPassword'], $aData['DBName'], $aData['DBTablePrefix'])) {
60: if (\Aurora\System\Enums\DbType::PostgreSQL === $iDbType) {
61: $oConnector = new Pdo\Postgres($aData['DBHost'], $aData['DBLogin'], $aData['DBPassword'], $aData['DBName'], $aData['DBTablePrefix']);
62: } else {
63: $oConnector = new Pdo\MySql($aData['DBHost'], $aData['DBLogin'], $aData['DBPassword'], $aData['DBName'], $aData['DBTablePrefix']);
64: }
65: }
66: }
67:
68: return $oConnector;
69: }
70:
71: /**
72: * @param int $iDbType = \Aurora\System\Enums\DbType::MySQL
73: * @return IDbHelper
74: */
75: public static function CommandCreatorHelperFabric($iDbType = \Aurora\System\Enums\DbType::MySQL)
76: {
77: $oHelper = null;
78: if (\Aurora\System\Enums\DbType::PostgreSQL === $iDbType) {
79: $oHelper = new Pdo\Postgres\Helper();
80: } else {
81: $oHelper = new Pdo\MySql\Helper();
82: }
83:
84: return $oHelper;
85: }
86:
87: /**
88: * @param \Aurora\System\Settingss $oSettings
89: * @return &MySql
90: */
91: public static function &CreateConnector(\Aurora\System\AbstractSettings $oSettings)
92: {
93: $aResult = array();
94: if (!is_object(self::$oDbConnector)) {
95: Creator::$oDbConnector = Creator::ConnectorFabric(array(
96: 'Type' => $oSettings->DBType,
97: 'DBHost' => $oSettings->DBHost,
98: 'DBLogin' => $oSettings->DBLogin,
99: 'DBPassword' => $oSettings->DBPassword,
100: 'DBName' => $oSettings->DBName,
101: 'DBTablePrefix' => $oSettings->DBPrefix
102: ));
103:
104: if ($oSettings->UseSlaveConnection) {
105: Creator::$oSlaveDbConnector = Creator::ConnectorFabric(array(
106: 'Type' => $oSettings->DBType,
107: 'DBHost' => $oSettings->DBSlaveHost,
108: 'DBLogin' => $oSettings->DBSlaveLogin,
109: 'DBPassword' => $oSettings->DBSlavePassword,
110: 'DBName' => $oSettings->DBSlaveName,
111: 'DBTablePrefix' => $oSettings->DBPrefix
112: ));
113: }
114: }
115:
116: $aResult = array(&Creator::$oDbConnector, &Creator::$oSlaveDbConnector);
117: return $aResult;
118: }
119:
120: /**
121: * @param \Aurora\System\AbstractSettings $oSettings
122: * @return &IDbHelper
123: */
124: public static function &CreateCommandCreatorHelper(\Aurora\System\AbstractSettings $oSettings)
125: {
126: if (is_object(Creator::$oCommandCreatorHelper)) {
127: return Creator::$oCommandCreatorHelper;
128: }
129:
130: Creator::$oCommandCreatorHelper = Creator::CommandCreatorHelperFabric(
131: $oSettings->DBType
132: );
133:
134: return Creator::$oCommandCreatorHelper;
135: }
136: }
137: