@@ -23,67 +23,67 @@ |
||
| 23 | 23 | class QueryInsertMultiple extends QueryStatement implements QueryStatementInterface |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - use InsertMultiple, Replacement, Ignore, DefaultPriority, LowPriority, HighPriority, Utilities; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var string |
|
| 30 | - */ |
|
| 31 | - protected $statement = self::QUERY_STATEMENT_INSERT; |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * QueryInsert constructor. |
|
| 36 | - * @param QueryBuild $queryBuild |
|
| 37 | - * @param string $table |
|
| 38 | - */ |
|
| 39 | - public function __construct(QueryBuild $queryBuild, $table = null) |
|
| 40 | - { |
|
| 41 | - parent::__construct($queryBuild, $table); |
|
| 42 | - $this->queryStructure->setElement(QueryStructure::FIELDS, array()); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function getSyntax($replacement = self::REPLACEMENT_NONE) |
|
| 46 | - { |
|
| 47 | - $syntax = array(); |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Explain |
|
| 51 | - */ |
|
| 52 | - $syntax[] = $this->getExplainSyntax(); |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * UPDATE statement |
|
| 56 | - */ |
|
| 57 | - $syntax[] = $this->statement; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * PRIORITY |
|
| 61 | - */ |
|
| 62 | - $syntax[] = $this->queryStructure->getElement(QueryStructure::PRIORITY); |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * IGNORE clause |
|
| 66 | - */ |
|
| 67 | - $syntax[] = $this->queryStructure->getElement(QueryStructure::IGNORE) ? 'IGNORE' : ''; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * INTO table |
|
| 71 | - */ |
|
| 72 | - $syntax[] = 'INTO ' . $this->queryStructure->getElement(QueryStructure::TABLE); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * FIELDS update |
|
| 76 | - */ |
|
| 77 | - $syntax[] = $this->getInsertMultipleRowsSyntax(); |
|
| 78 | - |
|
| 79 | - $syntax = implode(' ', $syntax); |
|
| 80 | - |
|
| 81 | - return $this->getSyntaxReplace($syntax, $replacement); |
|
| 82 | - |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - public function execute() |
|
| 86 | - { |
|
| 87 | - return DbService::getInstance()->query($this->getSyntax(), $this->queryStructure->getElement(QueryStructure::BIND_PARAMS)); |
|
| 88 | - } |
|
| 26 | + use InsertMultiple, Replacement, Ignore, DefaultPriority, LowPriority, HighPriority, Utilities; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var string |
|
| 30 | + */ |
|
| 31 | + protected $statement = self::QUERY_STATEMENT_INSERT; |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * QueryInsert constructor. |
|
| 36 | + * @param QueryBuild $queryBuild |
|
| 37 | + * @param string $table |
|
| 38 | + */ |
|
| 39 | + public function __construct(QueryBuild $queryBuild, $table = null) |
|
| 40 | + { |
|
| 41 | + parent::__construct($queryBuild, $table); |
|
| 42 | + $this->queryStructure->setElement(QueryStructure::FIELDS, array()); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function getSyntax($replacement = self::REPLACEMENT_NONE) |
|
| 46 | + { |
|
| 47 | + $syntax = array(); |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Explain |
|
| 51 | + */ |
|
| 52 | + $syntax[] = $this->getExplainSyntax(); |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * UPDATE statement |
|
| 56 | + */ |
|
| 57 | + $syntax[] = $this->statement; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * PRIORITY |
|
| 61 | + */ |
|
| 62 | + $syntax[] = $this->queryStructure->getElement(QueryStructure::PRIORITY); |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * IGNORE clause |
|
| 66 | + */ |
|
| 67 | + $syntax[] = $this->queryStructure->getElement(QueryStructure::IGNORE) ? 'IGNORE' : ''; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * INTO table |
|
| 71 | + */ |
|
| 72 | + $syntax[] = 'INTO ' . $this->queryStructure->getElement(QueryStructure::TABLE); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * FIELDS update |
|
| 76 | + */ |
|
| 77 | + $syntax[] = $this->getInsertMultipleRowsSyntax(); |
|
| 78 | + |
|
| 79 | + $syntax = implode(' ', $syntax); |
|
| 80 | + |
|
| 81 | + return $this->getSyntaxReplace($syntax, $replacement); |
|
| 82 | + |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + public function execute() |
|
| 86 | + { |
|
| 87 | + return DbService::getInstance()->query($this->getSyntax(), $this->queryStructure->getElement(QueryStructure::BIND_PARAMS)); |
|
| 88 | + } |
|
| 89 | 89 | } |
| 90 | 90 | \ No newline at end of file |
@@ -18,71 +18,71 @@ |
||
| 18 | 18 | abstract class QueryStatement |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - use Utilities, TableValidation, ColumnValidation; |
|
| 22 | - |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Statements |
|
| 26 | - */ |
|
| 27 | - const QUERY_STATEMENT_SELECT = 'SELECT'; |
|
| 28 | - const QUERY_STATEMENT_UPDATE = 'UPDATE'; |
|
| 29 | - const QUERY_STATEMENT_DELETE = 'DELETE'; |
|
| 30 | - const QUERY_STATEMENT_INSERT = 'INSERT'; |
|
| 31 | - const QUERY_STATEMENT_CUSTOM = 'CUSTOM'; |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - protected $statement; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @var QueryBuild |
|
| 41 | - */ |
|
| 42 | - protected $queryBuild; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @var QueryStructure |
|
| 46 | - */ |
|
| 47 | - protected $queryStructure; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @var array |
|
| 51 | - */ |
|
| 52 | - protected $usedInstanceIds = []; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - protected $tablePrefix; |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * QueryStatement constructor. |
|
| 62 | - * @param QueryBuild $queryBuild |
|
| 63 | - * @param string|QuerySelect $table |
|
| 64 | - * @throws QueryException |
|
| 65 | - */ |
|
| 66 | - public function __construct(QueryBuild $queryBuild, $table = '') |
|
| 67 | - { |
|
| 68 | - |
|
| 69 | - $table = $this->validateTable($table); |
|
| 70 | - |
|
| 71 | - $this->queryBuild = $queryBuild; |
|
| 72 | - $this->queryStructure = new QueryStructure(); |
|
| 73 | - $this->queryStructure->setElement(QueryStructure::TABLE, $table); |
|
| 74 | - $this->queryStructure->setElement(QueryStructure::STATEMENT, $this->statement); |
|
| 75 | - $this->queryStructure->setElement(QueryStructure::QUERY_TYPE, $this->queryBuild->getType()); |
|
| 76 | - |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @return mixed |
|
| 81 | - */ |
|
| 82 | - public function getBindParams() |
|
| 83 | - { |
|
| 84 | - return $this->queryStructure->getElement(QueryStructure::BIND_PARAMS); |
|
| 85 | - } |
|
| 21 | + use Utilities, TableValidation, ColumnValidation; |
|
| 22 | + |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Statements |
|
| 26 | + */ |
|
| 27 | + const QUERY_STATEMENT_SELECT = 'SELECT'; |
|
| 28 | + const QUERY_STATEMENT_UPDATE = 'UPDATE'; |
|
| 29 | + const QUERY_STATEMENT_DELETE = 'DELETE'; |
|
| 30 | + const QUERY_STATEMENT_INSERT = 'INSERT'; |
|
| 31 | + const QUERY_STATEMENT_CUSTOM = 'CUSTOM'; |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + protected $statement; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @var QueryBuild |
|
| 41 | + */ |
|
| 42 | + protected $queryBuild; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @var QueryStructure |
|
| 46 | + */ |
|
| 47 | + protected $queryStructure; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @var array |
|
| 51 | + */ |
|
| 52 | + protected $usedInstanceIds = []; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + protected $tablePrefix; |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * QueryStatement constructor. |
|
| 62 | + * @param QueryBuild $queryBuild |
|
| 63 | + * @param string|QuerySelect $table |
|
| 64 | + * @throws QueryException |
|
| 65 | + */ |
|
| 66 | + public function __construct(QueryBuild $queryBuild, $table = '') |
|
| 67 | + { |
|
| 68 | + |
|
| 69 | + $table = $this->validateTable($table); |
|
| 70 | + |
|
| 71 | + $this->queryBuild = $queryBuild; |
|
| 72 | + $this->queryStructure = new QueryStructure(); |
|
| 73 | + $this->queryStructure->setElement(QueryStructure::TABLE, $table); |
|
| 74 | + $this->queryStructure->setElement(QueryStructure::STATEMENT, $this->statement); |
|
| 75 | + $this->queryStructure->setElement(QueryStructure::QUERY_TYPE, $this->queryBuild->getType()); |
|
| 76 | + |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @return mixed |
|
| 81 | + */ |
|
| 82 | + public function getBindParams() |
|
| 83 | + { |
|
| 84 | + return $this->queryStructure->getElement(QueryStructure::BIND_PARAMS); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | 87 | |
| 88 | 88 | } |
| 89 | 89 | \ No newline at end of file |
@@ -11,17 +11,17 @@ |
||
| 11 | 11 | interface QueryStatementInterface |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - const REPLACEMENT_NONE = 0; |
|
| 15 | - const REPLACEMENT_VALUES = 1; |
|
| 14 | + const REPLACEMENT_NONE = 0; |
|
| 15 | + const REPLACEMENT_VALUES = 1; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @param bool $replacement |
|
| 19 | - * @return string |
|
| 20 | - */ |
|
| 21 | - public function getSyntax($replacement = self::REPLACEMENT_NONE); |
|
| 17 | + /** |
|
| 18 | + * @param bool $replacement |
|
| 19 | + * @return string |
|
| 20 | + */ |
|
| 21 | + public function getSyntax($replacement = self::REPLACEMENT_NONE); |
|
| 22 | 22 | |
| 23 | - public function execute(); |
|
| 23 | + public function execute(); |
|
| 24 | 24 | |
| 25 | - public function getBindParams(); |
|
| 25 | + public function getBindParams(); |
|
| 26 | 26 | |
| 27 | 27 | } |
| 28 | 28 | \ No newline at end of file |
@@ -27,81 +27,81 @@ |
||
| 27 | 27 | class QueryDelete extends QueryStatement implements QueryStatementInterface |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - use Limit, Where, WhereAndHavingBuilder, Replacement, OrderBy, SetFields, Ignore, DefaultPriority, LowPriority, Utilities; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - protected $statement = self::QUERY_STATEMENT_DELETE; |
|
| 36 | - |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * QueryDelete constructor. |
|
| 40 | - * @param QueryBuild $queryBuild |
|
| 41 | - * @param string $table |
|
| 42 | - */ |
|
| 43 | - public function __construct(QueryBuild $queryBuild, $table = null) |
|
| 44 | - { |
|
| 45 | - parent::__construct($queryBuild, $table); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - public function getSyntax($replacement = self::REPLACEMENT_NONE) |
|
| 49 | - { |
|
| 50 | - $syntax = array(); |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Explain |
|
| 54 | - */ |
|
| 55 | - $syntax[] = $this->getExplainSyntax(); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * UPDATE statement |
|
| 59 | - */ |
|
| 60 | - $syntax[] = $this->statement; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * PRIORITY |
|
| 64 | - */ |
|
| 65 | - $syntax[] = $this->queryStructure->getElement(QueryStructure::PRIORITY); |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * TABLE update |
|
| 69 | - */ |
|
| 70 | - $syntax[] = 'FROM ' . $this->queryStructure->getElement(QueryStructure::TABLE); |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * WHERE clause |
|
| 74 | - */ |
|
| 75 | - $syntax[] = $this->getWhereSyntax(); |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * ORDER BY clause |
|
| 79 | - */ |
|
| 80 | - $syntax[] = $this->getOrderBySyntax(); |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * LIMIT clause |
|
| 84 | - */ |
|
| 85 | - $syntax[] = $this->getLimitSyntax(); |
|
| 86 | - |
|
| 87 | - $syntax = implode(' ', $syntax); |
|
| 88 | - |
|
| 89 | - return $this->getSyntaxReplace($syntax, $replacement); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @return array|int|null |
|
| 94 | - * @throws QueryException |
|
| 95 | - */ |
|
| 96 | - public function execute() |
|
| 97 | - { |
|
| 98 | - |
|
| 99 | - if ($this->queryStructure->getElement((QueryStructure::WHERE_TRIGGER)) && !count($this->queryStructure->getElement(QueryStructure::WHERE))) |
|
| 100 | - throw new QueryException('Where or Having clause is required for this statement!', QueryException::QUERY_ERROR_DELETE_NOT_FILTER); |
|
| 101 | - |
|
| 102 | - return DbService::getInstance()->query($this->getSyntax(), $this->queryStructure->getElement(QueryStructure::BIND_PARAMS)); |
|
| 103 | - |
|
| 104 | - } |
|
| 30 | + use Limit, Where, WhereAndHavingBuilder, Replacement, OrderBy, SetFields, Ignore, DefaultPriority, LowPriority, Utilities; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + protected $statement = self::QUERY_STATEMENT_DELETE; |
|
| 36 | + |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * QueryDelete constructor. |
|
| 40 | + * @param QueryBuild $queryBuild |
|
| 41 | + * @param string $table |
|
| 42 | + */ |
|
| 43 | + public function __construct(QueryBuild $queryBuild, $table = null) |
|
| 44 | + { |
|
| 45 | + parent::__construct($queryBuild, $table); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + public function getSyntax($replacement = self::REPLACEMENT_NONE) |
|
| 49 | + { |
|
| 50 | + $syntax = array(); |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Explain |
|
| 54 | + */ |
|
| 55 | + $syntax[] = $this->getExplainSyntax(); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * UPDATE statement |
|
| 59 | + */ |
|
| 60 | + $syntax[] = $this->statement; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * PRIORITY |
|
| 64 | + */ |
|
| 65 | + $syntax[] = $this->queryStructure->getElement(QueryStructure::PRIORITY); |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * TABLE update |
|
| 69 | + */ |
|
| 70 | + $syntax[] = 'FROM ' . $this->queryStructure->getElement(QueryStructure::TABLE); |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * WHERE clause |
|
| 74 | + */ |
|
| 75 | + $syntax[] = $this->getWhereSyntax(); |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * ORDER BY clause |
|
| 79 | + */ |
|
| 80 | + $syntax[] = $this->getOrderBySyntax(); |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * LIMIT clause |
|
| 84 | + */ |
|
| 85 | + $syntax[] = $this->getLimitSyntax(); |
|
| 86 | + |
|
| 87 | + $syntax = implode(' ', $syntax); |
|
| 88 | + |
|
| 89 | + return $this->getSyntaxReplace($syntax, $replacement); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @return array|int|null |
|
| 94 | + * @throws QueryException |
|
| 95 | + */ |
|
| 96 | + public function execute() |
|
| 97 | + { |
|
| 98 | + |
|
| 99 | + if ($this->queryStructure->getElement((QueryStructure::WHERE_TRIGGER)) && !count($this->queryStructure->getElement(QueryStructure::WHERE))) |
|
| 100 | + throw new QueryException('Where or Having clause is required for this statement!', QueryException::QUERY_ERROR_DELETE_NOT_FILTER); |
|
| 101 | + |
|
| 102 | + return DbService::getInstance()->query($this->getSyntax(), $this->queryStructure->getElement(QueryStructure::BIND_PARAMS)); |
|
| 103 | + |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | 106 | |
| 107 | 107 | } |
| 108 | 108 | \ No newline at end of file |
@@ -11,111 +11,111 @@ |
||
| 11 | 11 | class DbLog |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - private static $instance; |
|
| 15 | - |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - private $path; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var \DateTimeZone |
|
| 24 | - */ |
|
| 25 | - private $dateTimeZone; |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * DbLog constructor. |
|
| 30 | - */ |
|
| 31 | - private function __construct() |
|
| 32 | - { |
|
| 33 | - $this->dateTimeZone = new \DateTimeZone('Europe/Bucharest'); |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param $query |
|
| 39 | - * @param $duration |
|
| 40 | - */ |
|
| 41 | - public function writeQueryDuration($query, $duration) |
|
| 42 | - { |
|
| 43 | - $backtrace = end(debug_backtrace()); |
|
| 44 | - $location = $backtrace['file'] . " Line: " . $backtrace['line']; |
|
| 45 | - |
|
| 46 | - $this->path = DbConfig::getInstance()->getLogPathQueryDuration(); //my comments |
|
| 47 | - $message = "Duration: " . round($duration, 5) . "\r\n"; |
|
| 48 | - $message .= "Location: $location\r\n"; |
|
| 49 | - $message .= "Query: $query"; |
|
| 50 | - $this->write($message); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param $query |
|
| 55 | - * @param $duration |
|
| 56 | - */ |
|
| 57 | - public function writeQueryErros($query, $code, $error) |
|
| 58 | - { |
|
| 59 | - $backtrace = end(debug_backtrace()); |
|
| 60 | - $location = $backtrace['file'] . " Line: " . $backtrace['line']; |
|
| 61 | - |
|
| 62 | - $this->path = DbConfig::getInstance()->getLogPathErrors(); //my comments |
|
| 63 | - $message = "Query: $query" . "\r\n"; |
|
| 64 | - $message .= "Location: $location\r\n"; |
|
| 65 | - $message .= "Error code : " . $code . "\r\n"; |
|
| 66 | - $message .= "" . $error; |
|
| 67 | - |
|
| 68 | - $this->write($message); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - |
|
| 72 | - public function write($message) |
|
| 73 | - { |
|
| 74 | - |
|
| 75 | - $date = new \DateTime(); |
|
| 76 | - $date->setTimezone($this->dateTimeZone); |
|
| 77 | - |
|
| 78 | - $log = $this->path . $date->format('Y-m-d') . ".txt"; |
|
| 79 | - $time = $date->format('H:i:s'); |
|
| 80 | - |
|
| 81 | - $messageFormat = "[$time]\r\n$message\r\n\r\n"; |
|
| 82 | - |
|
| 83 | - if (is_dir($this->path)) { |
|
| 84 | - if (!file_exists($log)) { |
|
| 85 | - $fh = fopen($log, 'a+'); |
|
| 86 | - fwrite($fh, $messageFormat); |
|
| 87 | - fclose($fh); |
|
| 88 | - } else { |
|
| 89 | - $this->edit($log, $messageFormat); |
|
| 90 | - } |
|
| 91 | - } else { |
|
| 92 | - if (mkdir($this->path, 0777) === true) { |
|
| 93 | - $this->write($message); |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @param string $log |
|
| 101 | - * @param string $message |
|
| 102 | - */ |
|
| 103 | - private function edit($log, $message) |
|
| 104 | - { |
|
| 105 | - file_put_contents($log, $message, FILE_APPEND); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @return DbLog |
|
| 111 | - */ |
|
| 112 | - public static function getInstance() |
|
| 113 | - { |
|
| 114 | - if (null === self::$instance) { |
|
| 115 | - self::$instance = new self(); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - return self::$instance; |
|
| 119 | - } |
|
| 14 | + private static $instance; |
|
| 15 | + |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + private $path; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var \DateTimeZone |
|
| 24 | + */ |
|
| 25 | + private $dateTimeZone; |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * DbLog constructor. |
|
| 30 | + */ |
|
| 31 | + private function __construct() |
|
| 32 | + { |
|
| 33 | + $this->dateTimeZone = new \DateTimeZone('Europe/Bucharest'); |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param $query |
|
| 39 | + * @param $duration |
|
| 40 | + */ |
|
| 41 | + public function writeQueryDuration($query, $duration) |
|
| 42 | + { |
|
| 43 | + $backtrace = end(debug_backtrace()); |
|
| 44 | + $location = $backtrace['file'] . " Line: " . $backtrace['line']; |
|
| 45 | + |
|
| 46 | + $this->path = DbConfig::getInstance()->getLogPathQueryDuration(); //my comments |
|
| 47 | + $message = "Duration: " . round($duration, 5) . "\r\n"; |
|
| 48 | + $message .= "Location: $location\r\n"; |
|
| 49 | + $message .= "Query: $query"; |
|
| 50 | + $this->write($message); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param $query |
|
| 55 | + * @param $duration |
|
| 56 | + */ |
|
| 57 | + public function writeQueryErros($query, $code, $error) |
|
| 58 | + { |
|
| 59 | + $backtrace = end(debug_backtrace()); |
|
| 60 | + $location = $backtrace['file'] . " Line: " . $backtrace['line']; |
|
| 61 | + |
|
| 62 | + $this->path = DbConfig::getInstance()->getLogPathErrors(); //my comments |
|
| 63 | + $message = "Query: $query" . "\r\n"; |
|
| 64 | + $message .= "Location: $location\r\n"; |
|
| 65 | + $message .= "Error code : " . $code . "\r\n"; |
|
| 66 | + $message .= "" . $error; |
|
| 67 | + |
|
| 68 | + $this->write($message); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + |
|
| 72 | + public function write($message) |
|
| 73 | + { |
|
| 74 | + |
|
| 75 | + $date = new \DateTime(); |
|
| 76 | + $date->setTimezone($this->dateTimeZone); |
|
| 77 | + |
|
| 78 | + $log = $this->path . $date->format('Y-m-d') . ".txt"; |
|
| 79 | + $time = $date->format('H:i:s'); |
|
| 80 | + |
|
| 81 | + $messageFormat = "[$time]\r\n$message\r\n\r\n"; |
|
| 82 | + |
|
| 83 | + if (is_dir($this->path)) { |
|
| 84 | + if (!file_exists($log)) { |
|
| 85 | + $fh = fopen($log, 'a+'); |
|
| 86 | + fwrite($fh, $messageFormat); |
|
| 87 | + fclose($fh); |
|
| 88 | + } else { |
|
| 89 | + $this->edit($log, $messageFormat); |
|
| 90 | + } |
|
| 91 | + } else { |
|
| 92 | + if (mkdir($this->path, 0777) === true) { |
|
| 93 | + $this->write($message); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @param string $log |
|
| 101 | + * @param string $message |
|
| 102 | + */ |
|
| 103 | + private function edit($log, $message) |
|
| 104 | + { |
|
| 105 | + file_put_contents($log, $message, FILE_APPEND); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @return DbLog |
|
| 111 | + */ |
|
| 112 | + public static function getInstance() |
|
| 113 | + { |
|
| 114 | + if (null === self::$instance) { |
|
| 115 | + self::$instance = new self(); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return self::$instance; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | } |
| 122 | 122 | \ No newline at end of file |
@@ -11,251 +11,251 @@ |
||
| 11 | 11 | class DbService |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - const QUERY_TYPE_INSERT = 'INSERT'; |
|
| 15 | - const QUERY_TYPE_DELETE = 'DELETE'; |
|
| 16 | - const QUERY_TYPE_UPDATE = 'UPDATE'; |
|
| 17 | - const QUERY_TYPE_SELECT = 'SELECT'; |
|
| 18 | - const QUERY_TYPE_REPLACE = 'REPLACE'; |
|
| 19 | - const QUERY_TYPE_SHOW = 'SHOW'; |
|
| 20 | - const QUERY_TYPE_DESC = 'DESC'; |
|
| 21 | - const QUERY_TYPE_EMPTY = 'EMPTY'; |
|
| 22 | - const QUERY_TYPE_OTHER = 'OTHER'; |
|
| 23 | - const QUERY_TYPE_EXPLAIN = 'EXPLAIN'; |
|
| 24 | - |
|
| 25 | - const ON_ERROR_THROW_EXCEPTION = 1; |
|
| 26 | - const ON_ERROR_RETURN_ERROR = 2; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var DbService |
|
| 30 | - */ |
|
| 31 | - private static $instance; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var \PDO |
|
| 35 | - */ |
|
| 36 | - private $pdo; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var \PDOStatement |
|
| 40 | - */ |
|
| 41 | - private $sQuery; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var array |
|
| 45 | - */ |
|
| 46 | - private $parameters = []; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @return DbService |
|
| 50 | - */ |
|
| 51 | - public static function getInstance() |
|
| 52 | - { |
|
| 53 | - if (null === self::$instance) { |
|
| 54 | - self::$instance = new self(); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - return self::$instance; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @param string $query |
|
| 62 | - * @param array $params |
|
| 63 | - * @param int $fetchMode |
|
| 64 | - * @return array|int|null |
|
| 65 | - */ |
|
| 66 | - public function query($query, $params = null, $fetchMode = \PDO::FETCH_ASSOC) |
|
| 67 | - { |
|
| 68 | - |
|
| 69 | - $query = trim(str_replace("\r", " ", $query)); |
|
| 70 | - $statement = self::getQueryStatement($query); |
|
| 71 | - |
|
| 72 | - $this->queryInit($query, $params); |
|
| 73 | - |
|
| 74 | - if ($statement === self::QUERY_TYPE_SELECT || $statement === self::QUERY_TYPE_SHOW || $statement === self::QUERY_TYPE_DESC || $statement === self::QUERY_TYPE_EXPLAIN) { |
|
| 75 | - return $this->sQuery->fetchAll($fetchMode); |
|
| 76 | - } elseif ($statement === self::QUERY_TYPE_INSERT || $statement === self::QUERY_TYPE_UPDATE || $statement === self::QUERY_TYPE_DELETE) { |
|
| 77 | - return $this->sQuery->rowCount(); |
|
| 78 | - } else { |
|
| 79 | - |
|
| 80 | - return NULL; |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param $queryString |
|
| 86 | - * @return string |
|
| 87 | - */ |
|
| 88 | - public static function getQueryStatement($queryString) |
|
| 89 | - { |
|
| 90 | - $queryString = trim($queryString); |
|
| 91 | - |
|
| 92 | - if ($queryString === '') { |
|
| 93 | - return self::QUERY_TYPE_EMPTY; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - if (preg_match('/^(select|insert|update|delete|replace|show|desc|explain)[\s]+/i', $queryString, $matches)) { |
|
| 97 | - switch (strtolower($matches[1])) { |
|
| 98 | - case 'select': |
|
| 99 | - return self::QUERY_TYPE_SELECT; |
|
| 100 | - case 'insert': |
|
| 101 | - return self::QUERY_TYPE_INSERT; |
|
| 102 | - case 'update': |
|
| 103 | - return self::QUERY_TYPE_UPDATE; |
|
| 104 | - case 'delete': |
|
| 105 | - return self::QUERY_TYPE_DELETE; |
|
| 106 | - case 'replace': |
|
| 107 | - return self::QUERY_TYPE_REPLACE; |
|
| 108 | - case 'explain': |
|
| 109 | - return self::QUERY_TYPE_EXPLAIN; |
|
| 110 | - default: |
|
| 111 | - return self::QUERY_TYPE_OTHER; |
|
| 112 | - } |
|
| 113 | - } else { |
|
| 114 | - return self::QUERY_TYPE_OTHER; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @param $query |
|
| 120 | - * @param array $parameters |
|
| 121 | - * @throws DbException |
|
| 122 | - */ |
|
| 123 | - private function queryInit($query, $parameters = []) |
|
| 124 | - { |
|
| 125 | - $this->pdo = DbConnect::getInstance()->getConnection(self::getQueryStatement($query)); |
|
| 126 | - $startQueryTime = microtime(true); |
|
| 127 | - |
|
| 128 | - try { |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Prepare query |
|
| 132 | - */ |
|
| 133 | - $this->sQuery = $this->pdo->prepare($query); |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Add parameters to the parameter array |
|
| 137 | - */ |
|
| 138 | - if (self::isArrayAssoc($parameters)) |
|
| 139 | - $this->bindMore($parameters); else |
|
| 140 | - foreach ($parameters as $key => $val) |
|
| 141 | - $this->parameters[] = array($key + 1, $val); |
|
| 142 | - |
|
| 143 | - if (count($this->parameters)) { |
|
| 144 | - foreach ($this->parameters as $param => $value) { |
|
| 145 | - if (is_int($value[1])) { |
|
| 146 | - $type = \PDO::PARAM_INT; |
|
| 147 | - } elseif (is_bool($value[1])) { |
|
| 148 | - $type = \PDO::PARAM_BOOL; |
|
| 149 | - } elseif (is_null($value[1])) { |
|
| 150 | - $type = \PDO::PARAM_NULL; |
|
| 151 | - } else { |
|
| 152 | - $type = \PDO::PARAM_STR; |
|
| 153 | - } |
|
| 154 | - $this->sQuery->bindValue($value[0], $value[1], $type); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - $this->sQuery->execute(); |
|
| 159 | - |
|
| 160 | - if (DbConfig::getInstance()->isEnableLogQueryDuration()) { |
|
| 161 | - $duration = microtime(true) - $startQueryTime; |
|
| 162 | - DbLog::getInstance()->writeQueryDuration($query, $duration); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - } catch (\PDOException $e) { |
|
| 166 | - if (DbConfig::getInstance()->isEnableLogErrors()) { |
|
| 167 | - DbLog::getInstance()->writeQueryErros($query, $e->getCode(), $e->getMessage()); |
|
| 168 | - } |
|
| 169 | - throw new DbException('Database error!', DbException::DB_QUERY_ERROR); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Reset the parameters |
|
| 174 | - */ |
|
| 175 | - $this->parameters = array(); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @param array $arr |
|
| 180 | - * @return bool |
|
| 181 | - */ |
|
| 182 | - public static function isArrayAssoc(array $arr) |
|
| 183 | - { |
|
| 184 | - if (array() === $arr) |
|
| 185 | - return false; |
|
| 186 | - |
|
| 187 | - return array_keys($arr) !== range(0, count($arr) - 1); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - public function bindMore($parray) |
|
| 191 | - { |
|
| 192 | - if (!count($this->parameters) && is_array($parray)) { |
|
| 193 | - $columns = array_keys($parray); |
|
| 194 | - foreach ($columns as $i => &$column) { |
|
| 195 | - $this->bind($column, $parray[ $column ]); |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - public function bind($para, $value) |
|
| 201 | - { |
|
| 202 | - $this->parameters[ sizeof($this->parameters) ] = [":" . $para, $value]; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @param $query |
|
| 207 | - * @param array $params |
|
| 208 | - * @return array|null |
|
| 209 | - */ |
|
| 210 | - public function column($query, $params = null) |
|
| 211 | - { |
|
| 212 | - $this->queryInit($query, $params); |
|
| 213 | - |
|
| 214 | - $query = trim(str_replace("\r", " ", $query)); |
|
| 215 | - $statement = self::getQueryStatement($query); |
|
| 216 | - |
|
| 217 | - if ($statement === self::QUERY_TYPE_EXPLAIN) |
|
| 218 | - return $this->sQuery->fetchAll(\PDO::FETCH_ASSOC); |
|
| 219 | - |
|
| 220 | - $Columns = $this->sQuery->fetchAll(\PDO::FETCH_NUM); |
|
| 221 | - |
|
| 222 | - $column = null; |
|
| 223 | - |
|
| 224 | - foreach ($Columns as $cells) { |
|
| 225 | - $column[] = $cells[0]; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - return $column; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - public function row($query, $params = null, $fetchmode = \PDO::FETCH_ASSOC) |
|
| 232 | - { |
|
| 233 | - $this->queryInit($query, $params); |
|
| 234 | - |
|
| 235 | - $query = trim(str_replace("\r", " ", $query)); |
|
| 236 | - $statement = self::getQueryStatement($query); |
|
| 237 | - |
|
| 238 | - if ($statement === self::QUERY_TYPE_EXPLAIN) |
|
| 239 | - return $this->sQuery->fetchAll(\PDO::FETCH_ASSOC); |
|
| 240 | - |
|
| 241 | - $result = $this->sQuery->fetch($fetchmode); |
|
| 242 | - $this->sQuery->closeCursor(); // Frees up the connection to the server so that other SQL statements may be issued, |
|
| 243 | - |
|
| 244 | - return $result; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - public function single($query, $params = null) |
|
| 248 | - { |
|
| 249 | - $this->queryInit($query, $params); |
|
| 250 | - $result = $this->sQuery->fetchColumn(); |
|
| 251 | - $this->sQuery->closeCursor(); // Frees up the connection to the server so that other SQL statements may be issued |
|
| 252 | - |
|
| 253 | - return $result; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - public function CloseConnection() |
|
| 257 | - { |
|
| 258 | - $this->pdo = null; |
|
| 259 | - } |
|
| 14 | + const QUERY_TYPE_INSERT = 'INSERT'; |
|
| 15 | + const QUERY_TYPE_DELETE = 'DELETE'; |
|
| 16 | + const QUERY_TYPE_UPDATE = 'UPDATE'; |
|
| 17 | + const QUERY_TYPE_SELECT = 'SELECT'; |
|
| 18 | + const QUERY_TYPE_REPLACE = 'REPLACE'; |
|
| 19 | + const QUERY_TYPE_SHOW = 'SHOW'; |
|
| 20 | + const QUERY_TYPE_DESC = 'DESC'; |
|
| 21 | + const QUERY_TYPE_EMPTY = 'EMPTY'; |
|
| 22 | + const QUERY_TYPE_OTHER = 'OTHER'; |
|
| 23 | + const QUERY_TYPE_EXPLAIN = 'EXPLAIN'; |
|
| 24 | + |
|
| 25 | + const ON_ERROR_THROW_EXCEPTION = 1; |
|
| 26 | + const ON_ERROR_RETURN_ERROR = 2; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var DbService |
|
| 30 | + */ |
|
| 31 | + private static $instance; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var \PDO |
|
| 35 | + */ |
|
| 36 | + private $pdo; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var \PDOStatement |
|
| 40 | + */ |
|
| 41 | + private $sQuery; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var array |
|
| 45 | + */ |
|
| 46 | + private $parameters = []; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @return DbService |
|
| 50 | + */ |
|
| 51 | + public static function getInstance() |
|
| 52 | + { |
|
| 53 | + if (null === self::$instance) { |
|
| 54 | + self::$instance = new self(); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + return self::$instance; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @param string $query |
|
| 62 | + * @param array $params |
|
| 63 | + * @param int $fetchMode |
|
| 64 | + * @return array|int|null |
|
| 65 | + */ |
|
| 66 | + public function query($query, $params = null, $fetchMode = \PDO::FETCH_ASSOC) |
|
| 67 | + { |
|
| 68 | + |
|
| 69 | + $query = trim(str_replace("\r", " ", $query)); |
|
| 70 | + $statement = self::getQueryStatement($query); |
|
| 71 | + |
|
| 72 | + $this->queryInit($query, $params); |
|
| 73 | + |
|
| 74 | + if ($statement === self::QUERY_TYPE_SELECT || $statement === self::QUERY_TYPE_SHOW || $statement === self::QUERY_TYPE_DESC || $statement === self::QUERY_TYPE_EXPLAIN) { |
|
| 75 | + return $this->sQuery->fetchAll($fetchMode); |
|
| 76 | + } elseif ($statement === self::QUERY_TYPE_INSERT || $statement === self::QUERY_TYPE_UPDATE || $statement === self::QUERY_TYPE_DELETE) { |
|
| 77 | + return $this->sQuery->rowCount(); |
|
| 78 | + } else { |
|
| 79 | + |
|
| 80 | + return NULL; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param $queryString |
|
| 86 | + * @return string |
|
| 87 | + */ |
|
| 88 | + public static function getQueryStatement($queryString) |
|
| 89 | + { |
|
| 90 | + $queryString = trim($queryString); |
|
| 91 | + |
|
| 92 | + if ($queryString === '') { |
|
| 93 | + return self::QUERY_TYPE_EMPTY; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + if (preg_match('/^(select|insert|update|delete|replace|show|desc|explain)[\s]+/i', $queryString, $matches)) { |
|
| 97 | + switch (strtolower($matches[1])) { |
|
| 98 | + case 'select': |
|
| 99 | + return self::QUERY_TYPE_SELECT; |
|
| 100 | + case 'insert': |
|
| 101 | + return self::QUERY_TYPE_INSERT; |
|
| 102 | + case 'update': |
|
| 103 | + return self::QUERY_TYPE_UPDATE; |
|
| 104 | + case 'delete': |
|
| 105 | + return self::QUERY_TYPE_DELETE; |
|
| 106 | + case 'replace': |
|
| 107 | + return self::QUERY_TYPE_REPLACE; |
|
| 108 | + case 'explain': |
|
| 109 | + return self::QUERY_TYPE_EXPLAIN; |
|
| 110 | + default: |
|
| 111 | + return self::QUERY_TYPE_OTHER; |
|
| 112 | + } |
|
| 113 | + } else { |
|
| 114 | + return self::QUERY_TYPE_OTHER; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @param $query |
|
| 120 | + * @param array $parameters |
|
| 121 | + * @throws DbException |
|
| 122 | + */ |
|
| 123 | + private function queryInit($query, $parameters = []) |
|
| 124 | + { |
|
| 125 | + $this->pdo = DbConnect::getInstance()->getConnection(self::getQueryStatement($query)); |
|
| 126 | + $startQueryTime = microtime(true); |
|
| 127 | + |
|
| 128 | + try { |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Prepare query |
|
| 132 | + */ |
|
| 133 | + $this->sQuery = $this->pdo->prepare($query); |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Add parameters to the parameter array |
|
| 137 | + */ |
|
| 138 | + if (self::isArrayAssoc($parameters)) |
|
| 139 | + $this->bindMore($parameters); else |
|
| 140 | + foreach ($parameters as $key => $val) |
|
| 141 | + $this->parameters[] = array($key + 1, $val); |
|
| 142 | + |
|
| 143 | + if (count($this->parameters)) { |
|
| 144 | + foreach ($this->parameters as $param => $value) { |
|
| 145 | + if (is_int($value[1])) { |
|
| 146 | + $type = \PDO::PARAM_INT; |
|
| 147 | + } elseif (is_bool($value[1])) { |
|
| 148 | + $type = \PDO::PARAM_BOOL; |
|
| 149 | + } elseif (is_null($value[1])) { |
|
| 150 | + $type = \PDO::PARAM_NULL; |
|
| 151 | + } else { |
|
| 152 | + $type = \PDO::PARAM_STR; |
|
| 153 | + } |
|
| 154 | + $this->sQuery->bindValue($value[0], $value[1], $type); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + $this->sQuery->execute(); |
|
| 159 | + |
|
| 160 | + if (DbConfig::getInstance()->isEnableLogQueryDuration()) { |
|
| 161 | + $duration = microtime(true) - $startQueryTime; |
|
| 162 | + DbLog::getInstance()->writeQueryDuration($query, $duration); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + } catch (\PDOException $e) { |
|
| 166 | + if (DbConfig::getInstance()->isEnableLogErrors()) { |
|
| 167 | + DbLog::getInstance()->writeQueryErros($query, $e->getCode(), $e->getMessage()); |
|
| 168 | + } |
|
| 169 | + throw new DbException('Database error!', DbException::DB_QUERY_ERROR); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Reset the parameters |
|
| 174 | + */ |
|
| 175 | + $this->parameters = array(); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @param array $arr |
|
| 180 | + * @return bool |
|
| 181 | + */ |
|
| 182 | + public static function isArrayAssoc(array $arr) |
|
| 183 | + { |
|
| 184 | + if (array() === $arr) |
|
| 185 | + return false; |
|
| 186 | + |
|
| 187 | + return array_keys($arr) !== range(0, count($arr) - 1); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + public function bindMore($parray) |
|
| 191 | + { |
|
| 192 | + if (!count($this->parameters) && is_array($parray)) { |
|
| 193 | + $columns = array_keys($parray); |
|
| 194 | + foreach ($columns as $i => &$column) { |
|
| 195 | + $this->bind($column, $parray[ $column ]); |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + public function bind($para, $value) |
|
| 201 | + { |
|
| 202 | + $this->parameters[ sizeof($this->parameters) ] = [":" . $para, $value]; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @param $query |
|
| 207 | + * @param array $params |
|
| 208 | + * @return array|null |
|
| 209 | + */ |
|
| 210 | + public function column($query, $params = null) |
|
| 211 | + { |
|
| 212 | + $this->queryInit($query, $params); |
|
| 213 | + |
|
| 214 | + $query = trim(str_replace("\r", " ", $query)); |
|
| 215 | + $statement = self::getQueryStatement($query); |
|
| 216 | + |
|
| 217 | + if ($statement === self::QUERY_TYPE_EXPLAIN) |
|
| 218 | + return $this->sQuery->fetchAll(\PDO::FETCH_ASSOC); |
|
| 219 | + |
|
| 220 | + $Columns = $this->sQuery->fetchAll(\PDO::FETCH_NUM); |
|
| 221 | + |
|
| 222 | + $column = null; |
|
| 223 | + |
|
| 224 | + foreach ($Columns as $cells) { |
|
| 225 | + $column[] = $cells[0]; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + return $column; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + public function row($query, $params = null, $fetchmode = \PDO::FETCH_ASSOC) |
|
| 232 | + { |
|
| 233 | + $this->queryInit($query, $params); |
|
| 234 | + |
|
| 235 | + $query = trim(str_replace("\r", " ", $query)); |
|
| 236 | + $statement = self::getQueryStatement($query); |
|
| 237 | + |
|
| 238 | + if ($statement === self::QUERY_TYPE_EXPLAIN) |
|
| 239 | + return $this->sQuery->fetchAll(\PDO::FETCH_ASSOC); |
|
| 240 | + |
|
| 241 | + $result = $this->sQuery->fetch($fetchmode); |
|
| 242 | + $this->sQuery->closeCursor(); // Frees up the connection to the server so that other SQL statements may be issued, |
|
| 243 | + |
|
| 244 | + return $result; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + public function single($query, $params = null) |
|
| 248 | + { |
|
| 249 | + $this->queryInit($query, $params); |
|
| 250 | + $result = $this->sQuery->fetchColumn(); |
|
| 251 | + $this->sQuery->closeCursor(); // Frees up the connection to the server so that other SQL statements may be issued |
|
| 252 | + |
|
| 253 | + return $result; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + public function CloseConnection() |
|
| 257 | + { |
|
| 258 | + $this->pdo = null; |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | 261 | } |
| 262 | 262 | \ No newline at end of file |
@@ -11,9 +11,9 @@ |
||
| 11 | 11 | class DbException extends \Exception |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - const DB_CONNECTION_ERROR = 1; |
|
| 15 | - const DB_QUERY_ERROR = 2; |
|
| 16 | - const DB_ERROR_MASTER_DATA_CONNECTION_MISSING = 3; |
|
| 14 | + const DB_CONNECTION_ERROR = 1; |
|
| 15 | + const DB_QUERY_ERROR = 2; |
|
| 16 | + const DB_ERROR_MASTER_DATA_CONNECTION_MISSING = 3; |
|
| 17 | 17 | |
| 18 | 18 | |
| 19 | 19 | } |
| 20 | 20 | \ No newline at end of file |
@@ -11,122 +11,122 @@ |
||
| 11 | 11 | class DbConnect |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - private static $instance; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @var \PDO |
|
| 18 | - */ |
|
| 19 | - private $pdoMaster; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var \PDO |
|
| 23 | - */ |
|
| 24 | - private $pdoSlave; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var DbConfig |
|
| 28 | - */ |
|
| 29 | - private $config; |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * DbConnect constructor. |
|
| 34 | - */ |
|
| 35 | - private function __construct() |
|
| 36 | - { |
|
| 37 | - $this->config = DbConfig::getInstance(); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @return \PDO |
|
| 42 | - */ |
|
| 43 | - public function getMasterConnection() |
|
| 44 | - { |
|
| 45 | - if (!is_a($this->pdoMaster, \PDO::class)) |
|
| 46 | - $this->pdoMaster = $this->connect($this->config->getMasterDataConnect()); |
|
| 47 | - |
|
| 48 | - return $this->pdoMaster; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @return \PDO |
|
| 53 | - */ |
|
| 54 | - public function getSlaveConnection() |
|
| 55 | - { |
|
| 56 | - if (!$this->config->getReplicationEnable()) |
|
| 57 | - return $this->getMasterConnection(); |
|
| 58 | - |
|
| 59 | - if (!is_a($this->pdoSlave, \PDO::class)) |
|
| 60 | - $this->pdoSlave = $this->connect($this->config->getSlaveDataConnect()); |
|
| 61 | - |
|
| 62 | - return $this->pdoSlave; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param $statement |
|
| 67 | - * @return \PDO |
|
| 68 | - */ |
|
| 69 | - public function getConnection($statement) |
|
| 70 | - { |
|
| 71 | - $statement = trim(strtolower($statement)); |
|
| 72 | - |
|
| 73 | - if ($statement === DbService::QUERY_TYPE_SELECT) |
|
| 74 | - return $this->getSlaveConnection(); |
|
| 75 | - |
|
| 76 | - return $this->getMasterConnection(); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param $string |
|
| 81 | - * @return string |
|
| 82 | - */ |
|
| 83 | - public function quote($string) |
|
| 84 | - { |
|
| 85 | - return $this->getMasterConnection()->quote($string); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public function lastInsertId() |
|
| 92 | - { |
|
| 93 | - return $this->getMasterConnection()->lastInsertId(); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @param $dataConnect |
|
| 98 | - * @return \PDO |
|
| 99 | - */ |
|
| 100 | - private function connect($dataConnect) |
|
| 101 | - { |
|
| 102 | - $dsn = 'mysql:dbname=' . $dataConnect["dbname"] . ';host=' . $dataConnect["host"] . ''; |
|
| 103 | - try { |
|
| 104 | - |
|
| 105 | - $pdo = new \PDO($dsn, $dataConnect["user"], $dataConnect["password"], array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); |
|
| 106 | - |
|
| 107 | - $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
| 108 | - $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); |
|
| 109 | - } catch (\PDOException $e) { |
|
| 110 | - if (DbConfig::getInstance()->isEnableLogErrors()) { |
|
| 111 | - DbLog::getInstance()->writeQueryErros('Connection fail!', $e->getCode(), $e->getMessage()); |
|
| 112 | - } |
|
| 113 | - throw new DbException('Database connection error!', DbException::DB_CONNECTION_ERROR); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - return $pdo; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @return DbConnect |
|
| 122 | - */ |
|
| 123 | - public static function getInstance() |
|
| 124 | - { |
|
| 125 | - if (null === self::$instance) { |
|
| 126 | - self::$instance = new self(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - return self::$instance; |
|
| 130 | - } |
|
| 14 | + private static $instance; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @var \PDO |
|
| 18 | + */ |
|
| 19 | + private $pdoMaster; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var \PDO |
|
| 23 | + */ |
|
| 24 | + private $pdoSlave; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var DbConfig |
|
| 28 | + */ |
|
| 29 | + private $config; |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * DbConnect constructor. |
|
| 34 | + */ |
|
| 35 | + private function __construct() |
|
| 36 | + { |
|
| 37 | + $this->config = DbConfig::getInstance(); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @return \PDO |
|
| 42 | + */ |
|
| 43 | + public function getMasterConnection() |
|
| 44 | + { |
|
| 45 | + if (!is_a($this->pdoMaster, \PDO::class)) |
|
| 46 | + $this->pdoMaster = $this->connect($this->config->getMasterDataConnect()); |
|
| 47 | + |
|
| 48 | + return $this->pdoMaster; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @return \PDO |
|
| 53 | + */ |
|
| 54 | + public function getSlaveConnection() |
|
| 55 | + { |
|
| 56 | + if (!$this->config->getReplicationEnable()) |
|
| 57 | + return $this->getMasterConnection(); |
|
| 58 | + |
|
| 59 | + if (!is_a($this->pdoSlave, \PDO::class)) |
|
| 60 | + $this->pdoSlave = $this->connect($this->config->getSlaveDataConnect()); |
|
| 61 | + |
|
| 62 | + return $this->pdoSlave; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param $statement |
|
| 67 | + * @return \PDO |
|
| 68 | + */ |
|
| 69 | + public function getConnection($statement) |
|
| 70 | + { |
|
| 71 | + $statement = trim(strtolower($statement)); |
|
| 72 | + |
|
| 73 | + if ($statement === DbService::QUERY_TYPE_SELECT) |
|
| 74 | + return $this->getSlaveConnection(); |
|
| 75 | + |
|
| 76 | + return $this->getMasterConnection(); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param $string |
|
| 81 | + * @return string |
|
| 82 | + */ |
|
| 83 | + public function quote($string) |
|
| 84 | + { |
|
| 85 | + return $this->getMasterConnection()->quote($string); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public function lastInsertId() |
|
| 92 | + { |
|
| 93 | + return $this->getMasterConnection()->lastInsertId(); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @param $dataConnect |
|
| 98 | + * @return \PDO |
|
| 99 | + */ |
|
| 100 | + private function connect($dataConnect) |
|
| 101 | + { |
|
| 102 | + $dsn = 'mysql:dbname=' . $dataConnect["dbname"] . ';host=' . $dataConnect["host"] . ''; |
|
| 103 | + try { |
|
| 104 | + |
|
| 105 | + $pdo = new \PDO($dsn, $dataConnect["user"], $dataConnect["password"], array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); |
|
| 106 | + |
|
| 107 | + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
| 108 | + $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); |
|
| 109 | + } catch (\PDOException $e) { |
|
| 110 | + if (DbConfig::getInstance()->isEnableLogErrors()) { |
|
| 111 | + DbLog::getInstance()->writeQueryErros('Connection fail!', $e->getCode(), $e->getMessage()); |
|
| 112 | + } |
|
| 113 | + throw new DbException('Database connection error!', DbException::DB_CONNECTION_ERROR); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + return $pdo; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @return DbConnect |
|
| 122 | + */ |
|
| 123 | + public static function getInstance() |
|
| 124 | + { |
|
| 125 | + if (null === self::$instance) { |
|
| 126 | + self::$instance = new self(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + return self::$instance; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | 132 | } |
| 133 | 133 | \ No newline at end of file |
@@ -11,225 +11,225 @@ |
||
| 11 | 11 | class DbConfig |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - private static $instance; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - private $dbConfig; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var bool |
|
| 23 | - */ |
|
| 24 | - private $replicationEnable; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - private $masterDataConnect; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var array |
|
| 33 | - */ |
|
| 34 | - private $slaveDataConnect; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var bool |
|
| 38 | - */ |
|
| 39 | - private $enableLogErrors = false; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var bool |
|
| 43 | - */ |
|
| 44 | - private $enableLogQueryDuration = false; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - private $logPathErrors; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - private $logPathQueryDuration; |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * DbConfig constructor. |
|
| 59 | - */ |
|
| 60 | - private function __construct() |
|
| 61 | - { |
|
| 62 | - $vendorCfg = __DIR__ . '/../../../../../vendor-cfg/qpdb_db_config.php'; |
|
| 63 | - if (file_exists($vendorCfg)) |
|
| 64 | - $this->dbConfig = require $vendorCfg; else |
|
| 65 | - $this->dbConfig = require __DIR__ . '/../../config/qpdb_db_config.php'; |
|
| 66 | - |
|
| 67 | - $this->buildConfig(); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @param string $fileConfig |
|
| 72 | - * @return $this |
|
| 73 | - */ |
|
| 74 | - public function withFileConfig($fileConfig) |
|
| 75 | - { |
|
| 76 | - $this->dbConfig = require $fileConfig; |
|
| 77 | - $this->buildConfig(); |
|
| 78 | - |
|
| 79 | - return $this; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @return bool |
|
| 84 | - */ |
|
| 85 | - public function getReplicationEnable() |
|
| 86 | - { |
|
| 87 | - return $this->replicationEnable; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @return array |
|
| 92 | - */ |
|
| 93 | - public function getMasterDataConnect() |
|
| 94 | - { |
|
| 95 | - return $this->masterDataConnect; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @return array |
|
| 100 | - */ |
|
| 101 | - public function getSlaveDataConnect() |
|
| 102 | - { |
|
| 103 | - return $this->slaveDataConnect; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @return array |
|
| 108 | - */ |
|
| 109 | - public function getLogConfig() |
|
| 110 | - { |
|
| 111 | - return $this->dbConfig['db_log']; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @return bool |
|
| 116 | - */ |
|
| 117 | - public function isEnableLogErrors() |
|
| 118 | - { |
|
| 119 | - return $this->enableLogErrors; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @return bool |
|
| 124 | - */ |
|
| 125 | - public function isEnableLogQueryDuration() |
|
| 126 | - { |
|
| 127 | - return $this->enableLogQueryDuration; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @return string |
|
| 132 | - */ |
|
| 133 | - public function getLogPathErrors() |
|
| 134 | - { |
|
| 135 | - return $this->logPathErrors; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @return string |
|
| 140 | - */ |
|
| 141 | - public function getLogPathQueryDuration() |
|
| 142 | - { |
|
| 143 | - return $this->logPathQueryDuration; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - public function useTablePrefix() |
|
| 147 | - { |
|
| 148 | - if (!empty($this->dbConfig['use_table_prefix'])) |
|
| 149 | - return $this->dbConfig['use_table_prefix']; |
|
| 150 | - |
|
| 151 | - return false; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - public function getTablePrefix() |
|
| 155 | - { |
|
| 156 | - return $this->dbConfig['table_prefix']; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - |
|
| 160 | - private function buildConfig() |
|
| 161 | - { |
|
| 162 | - $this->replicationEnable = $this->dbConfig['replicationEnable']; |
|
| 163 | - $this->readMasterDataConnect(); |
|
| 164 | - $this->readSlaveDataConnect(); |
|
| 165 | - $this->configLogger(); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - private function configLogger() |
|
| 169 | - { |
|
| 170 | - $this->enableLogErrors = $this->dbConfig['db_log']['enable_log_errors']; |
|
| 171 | - $this->enableLogQueryDuration = $this->dbConfig['db_log']['enable_log_query_duration']; |
|
| 172 | - $this->logPathErrors = $this->dbConfig['db_log']['log_path_errors']; |
|
| 173 | - $this->logPathQueryDuration = $this->dbConfig['db_log']['log_path_query_duration']; |
|
| 174 | - |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * @return bool |
|
| 179 | - * @throws DbException |
|
| 180 | - */ |
|
| 181 | - private function readMasterDataConnect() |
|
| 182 | - { |
|
| 183 | - |
|
| 184 | - if (!isset($this->dbConfig['master_data_connect'][0])) |
|
| 185 | - throw new DbException('Master data connect is missing', DbException::DB_ERROR_MASTER_DATA_CONNECTION_MISSING); |
|
| 186 | - |
|
| 187 | - $dataConnection = $this->dbConfig['master_data_connect']; |
|
| 188 | - |
|
| 189 | - if (!$this->replicationEnable || count($dataConnection) == 1) { |
|
| 190 | - $this->masterDataConnect = $dataConnection[0]; |
|
| 191 | - |
|
| 192 | - return true; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - shuffle($dataConnection); |
|
| 196 | - $this->masterDataConnect = $dataConnection[0]; |
|
| 197 | - |
|
| 198 | - return true; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * @return bool |
|
| 204 | - */ |
|
| 205 | - private function readSlaveDataConnect() |
|
| 206 | - { |
|
| 207 | - |
|
| 208 | - if (!isset($this->dbConfig['slave_data_connect'][0])) { |
|
| 209 | - $this->slaveDataConnect = $this->masterDataConnect; |
|
| 210 | - |
|
| 211 | - return true; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $dataConnection = $this->dbConfig['slave_data_connect']; |
|
| 215 | - |
|
| 216 | - shuffle($dataConnection); |
|
| 217 | - $this->slaveDataConnect = $dataConnection[0]; |
|
| 218 | - |
|
| 219 | - return true; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * @return DbConfig |
|
| 224 | - */ |
|
| 225 | - public static function getInstance() |
|
| 226 | - { |
|
| 227 | - if (null === self::$instance) { |
|
| 228 | - self::$instance = new self(); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - return self::$instance; |
|
| 232 | - } |
|
| 14 | + private static $instance; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + private $dbConfig; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var bool |
|
| 23 | + */ |
|
| 24 | + private $replicationEnable; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + private $masterDataConnect; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var array |
|
| 33 | + */ |
|
| 34 | + private $slaveDataConnect; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var bool |
|
| 38 | + */ |
|
| 39 | + private $enableLogErrors = false; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var bool |
|
| 43 | + */ |
|
| 44 | + private $enableLogQueryDuration = false; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + private $logPathErrors; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + private $logPathQueryDuration; |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * DbConfig constructor. |
|
| 59 | + */ |
|
| 60 | + private function __construct() |
|
| 61 | + { |
|
| 62 | + $vendorCfg = __DIR__ . '/../../../../../vendor-cfg/qpdb_db_config.php'; |
|
| 63 | + if (file_exists($vendorCfg)) |
|
| 64 | + $this->dbConfig = require $vendorCfg; else |
|
| 65 | + $this->dbConfig = require __DIR__ . '/../../config/qpdb_db_config.php'; |
|
| 66 | + |
|
| 67 | + $this->buildConfig(); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @param string $fileConfig |
|
| 72 | + * @return $this |
|
| 73 | + */ |
|
| 74 | + public function withFileConfig($fileConfig) |
|
| 75 | + { |
|
| 76 | + $this->dbConfig = require $fileConfig; |
|
| 77 | + $this->buildConfig(); |
|
| 78 | + |
|
| 79 | + return $this; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @return bool |
|
| 84 | + */ |
|
| 85 | + public function getReplicationEnable() |
|
| 86 | + { |
|
| 87 | + return $this->replicationEnable; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @return array |
|
| 92 | + */ |
|
| 93 | + public function getMasterDataConnect() |
|
| 94 | + { |
|
| 95 | + return $this->masterDataConnect; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @return array |
|
| 100 | + */ |
|
| 101 | + public function getSlaveDataConnect() |
|
| 102 | + { |
|
| 103 | + return $this->slaveDataConnect; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @return array |
|
| 108 | + */ |
|
| 109 | + public function getLogConfig() |
|
| 110 | + { |
|
| 111 | + return $this->dbConfig['db_log']; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @return bool |
|
| 116 | + */ |
|
| 117 | + public function isEnableLogErrors() |
|
| 118 | + { |
|
| 119 | + return $this->enableLogErrors; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @return bool |
|
| 124 | + */ |
|
| 125 | + public function isEnableLogQueryDuration() |
|
| 126 | + { |
|
| 127 | + return $this->enableLogQueryDuration; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @return string |
|
| 132 | + */ |
|
| 133 | + public function getLogPathErrors() |
|
| 134 | + { |
|
| 135 | + return $this->logPathErrors; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @return string |
|
| 140 | + */ |
|
| 141 | + public function getLogPathQueryDuration() |
|
| 142 | + { |
|
| 143 | + return $this->logPathQueryDuration; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + public function useTablePrefix() |
|
| 147 | + { |
|
| 148 | + if (!empty($this->dbConfig['use_table_prefix'])) |
|
| 149 | + return $this->dbConfig['use_table_prefix']; |
|
| 150 | + |
|
| 151 | + return false; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + public function getTablePrefix() |
|
| 155 | + { |
|
| 156 | + return $this->dbConfig['table_prefix']; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + |
|
| 160 | + private function buildConfig() |
|
| 161 | + { |
|
| 162 | + $this->replicationEnable = $this->dbConfig['replicationEnable']; |
|
| 163 | + $this->readMasterDataConnect(); |
|
| 164 | + $this->readSlaveDataConnect(); |
|
| 165 | + $this->configLogger(); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + private function configLogger() |
|
| 169 | + { |
|
| 170 | + $this->enableLogErrors = $this->dbConfig['db_log']['enable_log_errors']; |
|
| 171 | + $this->enableLogQueryDuration = $this->dbConfig['db_log']['enable_log_query_duration']; |
|
| 172 | + $this->logPathErrors = $this->dbConfig['db_log']['log_path_errors']; |
|
| 173 | + $this->logPathQueryDuration = $this->dbConfig['db_log']['log_path_query_duration']; |
|
| 174 | + |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * @return bool |
|
| 179 | + * @throws DbException |
|
| 180 | + */ |
|
| 181 | + private function readMasterDataConnect() |
|
| 182 | + { |
|
| 183 | + |
|
| 184 | + if (!isset($this->dbConfig['master_data_connect'][0])) |
|
| 185 | + throw new DbException('Master data connect is missing', DbException::DB_ERROR_MASTER_DATA_CONNECTION_MISSING); |
|
| 186 | + |
|
| 187 | + $dataConnection = $this->dbConfig['master_data_connect']; |
|
| 188 | + |
|
| 189 | + if (!$this->replicationEnable || count($dataConnection) == 1) { |
|
| 190 | + $this->masterDataConnect = $dataConnection[0]; |
|
| 191 | + |
|
| 192 | + return true; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + shuffle($dataConnection); |
|
| 196 | + $this->masterDataConnect = $dataConnection[0]; |
|
| 197 | + |
|
| 198 | + return true; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * @return bool |
|
| 204 | + */ |
|
| 205 | + private function readSlaveDataConnect() |
|
| 206 | + { |
|
| 207 | + |
|
| 208 | + if (!isset($this->dbConfig['slave_data_connect'][0])) { |
|
| 209 | + $this->slaveDataConnect = $this->masterDataConnect; |
|
| 210 | + |
|
| 211 | + return true; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $dataConnection = $this->dbConfig['slave_data_connect']; |
|
| 215 | + |
|
| 216 | + shuffle($dataConnection); |
|
| 217 | + $this->slaveDataConnect = $dataConnection[0]; |
|
| 218 | + |
|
| 219 | + return true; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * @return DbConfig |
|
| 224 | + */ |
|
| 225 | + public static function getInstance() |
|
| 226 | + { |
|
| 227 | + if (null === self::$instance) { |
|
| 228 | + self::$instance = new self(); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + return self::$instance; |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | 234 | |
| 235 | 235 | } |
| 236 | 236 | \ No newline at end of file |