@@ -52,22 +52,22 @@ discard block |
||
52 | 52 | * @param int $fetchMode |
53 | 53 | * @return array|int|null |
54 | 54 | */ |
55 | - public function query( $query, $params = null, $fetchMode = \PDO::FETCH_ASSOC ) |
|
55 | + public function query($query, $params = null, $fetchMode = \PDO::FETCH_ASSOC) |
|
56 | 56 | { |
57 | 57 | |
58 | - $query = trim( str_replace( "\r", " ", $query ) ); |
|
59 | - $statement = self::getQueryStatement( $query ); |
|
58 | + $query = trim(str_replace("\r", " ", $query)); |
|
59 | + $statement = self::getQueryStatement($query); |
|
60 | 60 | |
61 | - $this->queryInit( $query, $params ); |
|
61 | + $this->queryInit($query, $params); |
|
62 | 62 | |
63 | - if ( $statement === self::QUERY_TYPE_SELECT || |
|
63 | + if ($statement === self::QUERY_TYPE_SELECT || |
|
64 | 64 | $statement === self::QUERY_TYPE_SHOW || |
65 | 65 | $statement === self::QUERY_TYPE_DESC || |
66 | 66 | $statement === self::QUERY_TYPE_EXPLAIN |
67 | 67 | ) { |
68 | - return $this->sQuery->fetchAll( $fetchMode ); |
|
68 | + return $this->sQuery->fetchAll($fetchMode); |
|
69 | 69 | } |
70 | - elseif ( $statement === self::QUERY_TYPE_INSERT || |
|
70 | + elseif ($statement === self::QUERY_TYPE_INSERT || |
|
71 | 71 | $statement === self::QUERY_TYPE_UPDATE || |
72 | 72 | $statement === self::QUERY_TYPE_DELETE |
73 | 73 | ) { |
@@ -84,46 +84,46 @@ discard block |
||
84 | 84 | * @param null $params |
85 | 85 | * @return array|null |
86 | 86 | */ |
87 | - public function column( $query, $params = null ) |
|
87 | + public function column($query, $params = null) |
|
88 | 88 | { |
89 | - $this->queryInit( $query, $params ); |
|
89 | + $this->queryInit($query, $params); |
|
90 | 90 | |
91 | - $query = trim( str_replace( "\r", " ", $query ) ); |
|
92 | - $statement = self::getQueryStatement( $query ); |
|
91 | + $query = trim(str_replace("\r", " ", $query)); |
|
92 | + $statement = self::getQueryStatement($query); |
|
93 | 93 | |
94 | - if ( $statement === self::QUERY_TYPE_EXPLAIN ) |
|
95 | - return $this->sQuery->fetchAll( \PDO::FETCH_ASSOC ); |
|
94 | + if ($statement === self::QUERY_TYPE_EXPLAIN) |
|
95 | + return $this->sQuery->fetchAll(\PDO::FETCH_ASSOC); |
|
96 | 96 | |
97 | - $Columns = $this->sQuery->fetchAll( \PDO::FETCH_NUM ); |
|
97 | + $Columns = $this->sQuery->fetchAll(\PDO::FETCH_NUM); |
|
98 | 98 | |
99 | 99 | $column = null; |
100 | 100 | |
101 | - foreach ( $Columns as $cells ) { |
|
101 | + foreach ($Columns as $cells) { |
|
102 | 102 | $column[] = $cells[0]; |
103 | 103 | } |
104 | 104 | |
105 | 105 | return $column; |
106 | 106 | } |
107 | 107 | |
108 | - public function row( $query, $params = null, $fetchmode = \PDO::FETCH_ASSOC ) |
|
108 | + public function row($query, $params = null, $fetchmode = \PDO::FETCH_ASSOC) |
|
109 | 109 | { |
110 | - $this->queryInit( $query, $params ); |
|
110 | + $this->queryInit($query, $params); |
|
111 | 111 | |
112 | - $query = trim( str_replace( "\r", " ", $query ) ); |
|
113 | - $statement = self::getQueryStatement( $query ); |
|
112 | + $query = trim(str_replace("\r", " ", $query)); |
|
113 | + $statement = self::getQueryStatement($query); |
|
114 | 114 | |
115 | - if ( $statement === self::QUERY_TYPE_EXPLAIN ) |
|
116 | - return $this->sQuery->fetchAll( \PDO::FETCH_ASSOC ); |
|
115 | + if ($statement === self::QUERY_TYPE_EXPLAIN) |
|
116 | + return $this->sQuery->fetchAll(\PDO::FETCH_ASSOC); |
|
117 | 117 | |
118 | - $result = $this->sQuery->fetch( $fetchmode ); |
|
118 | + $result = $this->sQuery->fetch($fetchmode); |
|
119 | 119 | $this->sQuery->closeCursor(); // Frees up the connection to the server so that other SQL statements may be issued, |
120 | 120 | |
121 | 121 | return $result; |
122 | 122 | } |
123 | 123 | |
124 | - public function single( $query, $params = null ) |
|
124 | + public function single($query, $params = null) |
|
125 | 125 | { |
126 | - $this->queryInit( $query, $params ); |
|
126 | + $this->queryInit($query, $params); |
|
127 | 127 | $result = $this->sQuery->fetchColumn(); |
128 | 128 | $this->sQuery->closeCursor(); // Frees up the connection to the server so that other SQL statements may be issued |
129 | 129 | |
@@ -135,57 +135,57 @@ discard block |
||
135 | 135 | * @param string $query |
136 | 136 | * @param array $parameters |
137 | 137 | */ |
138 | - private function queryInit( $query, $parameters = [] ) |
|
138 | + private function queryInit($query, $parameters = []) |
|
139 | 139 | { |
140 | - $this->pdo = DbConnect::getInstance()->getConnection( self::getQueryStatement( $query ) ); |
|
141 | - $startQueryTime = microtime( true ); |
|
140 | + $this->pdo = DbConnect::getInstance()->getConnection(self::getQueryStatement($query)); |
|
141 | + $startQueryTime = microtime(true); |
|
142 | 142 | |
143 | 143 | try { |
144 | 144 | |
145 | 145 | /** |
146 | 146 | * Prepare query |
147 | 147 | */ |
148 | - $this->sQuery = $this->pdo->prepare( $query ); |
|
148 | + $this->sQuery = $this->pdo->prepare($query); |
|
149 | 149 | |
150 | 150 | /** |
151 | 151 | * Add parameters to the parameter array |
152 | 152 | */ |
153 | - if ( self::isArrayAssoc( $parameters ) ) |
|
154 | - $this->bindMore( $parameters ); |
|
153 | + if (self::isArrayAssoc($parameters)) |
|
154 | + $this->bindMore($parameters); |
|
155 | 155 | else |
156 | - foreach ( $parameters as $key => $val ) |
|
157 | - $this->parameters[] = array( $key + 1, $val ); |
|
156 | + foreach ($parameters as $key => $val) |
|
157 | + $this->parameters[] = array($key + 1, $val); |
|
158 | 158 | |
159 | - if ( count( $this->parameters ) ) { |
|
160 | - foreach ( $this->parameters as $param => $value ) { |
|
161 | - if ( is_int( $value[1] ) ) { |
|
159 | + if (count($this->parameters)) { |
|
160 | + foreach ($this->parameters as $param => $value) { |
|
161 | + if (is_int($value[1])) { |
|
162 | 162 | $type = \PDO::PARAM_INT; |
163 | 163 | } |
164 | - elseif ( is_bool( $value[1] ) ) { |
|
164 | + elseif (is_bool($value[1])) { |
|
165 | 165 | $type = \PDO::PARAM_BOOL; |
166 | 166 | } |
167 | - elseif ( is_null( $value[1] ) ) { |
|
167 | + elseif (is_null($value[1])) { |
|
168 | 168 | $type = \PDO::PARAM_NULL; |
169 | 169 | } |
170 | 170 | else { |
171 | 171 | $type = \PDO::PARAM_STR; |
172 | 172 | } |
173 | - $this->sQuery->bindValue( $value[0], $value[1], $type ); |
|
173 | + $this->sQuery->bindValue($value[0], $value[1], $type); |
|
174 | 174 | } |
175 | 175 | } |
176 | 176 | |
177 | 177 | $this->sQuery->execute(); |
178 | 178 | |
179 | - if ( DbConfig::getInstance()->isEnableLogQueryDuration() ) { |
|
180 | - $duration = microtime( true ) - $startQueryTime; |
|
181 | - DbLog::getInstance()->writeQueryDuration( $query, $duration ); |
|
179 | + if (DbConfig::getInstance()->isEnableLogQueryDuration()) { |
|
180 | + $duration = microtime(true) - $startQueryTime; |
|
181 | + DbLog::getInstance()->writeQueryDuration($query, $duration); |
|
182 | 182 | } |
183 | 183 | |
184 | - } catch ( \PDOException $e ) { |
|
185 | - if ( DbConfig::getInstance()->isEnableLogErrors() ) { |
|
186 | - DbLog::getInstance()->writeQueryErros( $query, $e->getCode(), $e->getMessage() ); |
|
184 | + } catch (\PDOException $e) { |
|
185 | + if (DbConfig::getInstance()->isEnableLogErrors()) { |
|
186 | + DbLog::getInstance()->writeQueryErros($query, $e->getCode(), $e->getMessage()); |
|
187 | 187 | } |
188 | - throw new \PDOException( $e->getMessage(), $e->getCode() ); |
|
188 | + throw new \PDOException($e->getMessage(), $e->getCode()); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | /** |
@@ -195,19 +195,19 @@ discard block |
||
195 | 195 | } |
196 | 196 | |
197 | 197 | |
198 | - public function bindMore( $parray ) |
|
198 | + public function bindMore($parray) |
|
199 | 199 | { |
200 | - if ( !count( $this->parameters ) && is_array( $parray ) ) { |
|
201 | - $columns = array_keys( $parray ); |
|
202 | - foreach ( $columns as $i => &$column ) { |
|
203 | - $this->bind( $column, $parray[ $column ] ); |
|
200 | + if (!count($this->parameters) && is_array($parray)) { |
|
201 | + $columns = array_keys($parray); |
|
202 | + foreach ($columns as $i => &$column) { |
|
203 | + $this->bind($column, $parray[$column]); |
|
204 | 204 | } |
205 | 205 | } |
206 | 206 | } |
207 | 207 | |
208 | - public function bind( $para, $value ) |
|
208 | + public function bind($para, $value) |
|
209 | 209 | { |
210 | - $this->parameters[ sizeof( $this->parameters ) ] = [ ":" . $para, $value ]; |
|
210 | + $this->parameters[sizeof($this->parameters)] = [":" . $para, $value]; |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | |
@@ -221,16 +221,16 @@ discard block |
||
221 | 221 | * @param $queryString |
222 | 222 | * @return string |
223 | 223 | */ |
224 | - public static function getQueryStatement( $queryString ) |
|
224 | + public static function getQueryStatement($queryString) |
|
225 | 225 | { |
226 | - $queryString = trim( $queryString ); |
|
226 | + $queryString = trim($queryString); |
|
227 | 227 | |
228 | - if ( $queryString === '' ) { |
|
228 | + if ($queryString === '') { |
|
229 | 229 | return self::QUERY_TYPE_EMPTY; |
230 | 230 | } |
231 | 231 | |
232 | - if ( preg_match( '/^(select|insert|update|delete|replace|show|desc|explain)[\s]+/i', $queryString, $matches ) ) { |
|
233 | - switch ( strtolower( $matches[1] ) ) { |
|
232 | + if (preg_match('/^(select|insert|update|delete|replace|show|desc|explain)[\s]+/i', $queryString, $matches)) { |
|
233 | + switch (strtolower($matches[1])) { |
|
234 | 234 | case 'select': |
235 | 235 | return self::QUERY_TYPE_SELECT; |
236 | 236 | break; |
@@ -263,11 +263,11 @@ discard block |
||
263 | 263 | * @param array $arr |
264 | 264 | * @return bool |
265 | 265 | */ |
266 | - public static function isArrayAssoc( array $arr ) |
|
266 | + public static function isArrayAssoc(array $arr) |
|
267 | 267 | { |
268 | - if ( array() === $arr ) return false; |
|
268 | + if (array() === $arr) return false; |
|
269 | 269 | |
270 | - return array_keys( $arr ) !== range( 0, count( $arr ) - 1 ); |
|
270 | + return array_keys($arr) !== range(0, count($arr) - 1); |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | |
@@ -276,7 +276,7 @@ discard block |
||
276 | 276 | */ |
277 | 277 | public static function getInstance() |
278 | 278 | { |
279 | - if ( null === self::$instance ) { |
|
279 | + if (null === self::$instance) { |
|
280 | 280 | self::$instance = new self(); |
281 | 281 | } |
282 | 282 |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | */ |
31 | 31 | private function __construct() |
32 | 32 | { |
33 | - $this->dateTimeZone = new \DateTimeZone( 'Europe/Bucharest' ); |
|
33 | + $this->dateTimeZone = new \DateTimeZone('Europe/Bucharest'); |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | |
@@ -38,61 +38,61 @@ discard block |
||
38 | 38 | * @param $query |
39 | 39 | * @param $duration |
40 | 40 | */ |
41 | - public function writeQueryDuration( $query, $duration ) |
|
41 | + public function writeQueryDuration($query, $duration) |
|
42 | 42 | { |
43 | - $backtrace = end( debug_backtrace() ); |
|
43 | + $backtrace = end(debug_backtrace()); |
|
44 | 44 | $location = $backtrace['file'] . " Line: " . $backtrace['line']; |
45 | 45 | |
46 | 46 | $this->path = DbConfig::getInstance()->getLogPathQueryDuration(); //my comments |
47 | - $message = "Duration: " . round( $duration, 5 ) . "\r\n"; |
|
47 | + $message = "Duration: " . round($duration, 5) . "\r\n"; |
|
48 | 48 | $message .= "Location: $location\r\n"; |
49 | 49 | $message .= "Query: $query"; |
50 | - $this->write( $message ); |
|
50 | + $this->write($message); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | /** |
54 | 54 | * @param $query |
55 | 55 | * @param $duration |
56 | 56 | */ |
57 | - public function writeQueryErros( $query, $code, $error ) |
|
57 | + public function writeQueryErros($query, $code, $error) |
|
58 | 58 | { |
59 | - $backtrace = end( debug_backtrace() ); |
|
59 | + $backtrace = end(debug_backtrace()); |
|
60 | 60 | $location = $backtrace['file'] . " Line: " . $backtrace['line']; |
61 | 61 | |
62 | 62 | $this->path = DbConfig::getInstance()->getLogPathErrors(); //my comments |
63 | 63 | $message = "Query: $query" . "\r\n"; |
64 | 64 | $message .= "Location: $location\r\n"; |
65 | 65 | $message .= "Error code : " . $code . "\r\n"; |
66 | - $message .= "" . $error ; |
|
66 | + $message .= "" . $error; |
|
67 | 67 | |
68 | - $this->write( $message ); |
|
68 | + $this->write($message); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | |
72 | - public function write( $message ) |
|
72 | + public function write($message) |
|
73 | 73 | { |
74 | 74 | |
75 | 75 | $date = new \DateTime(); |
76 | - $date->setTimezone( $this->dateTimeZone ); |
|
76 | + $date->setTimezone($this->dateTimeZone); |
|
77 | 77 | |
78 | - $log = $this->path . $date->format( 'Y-m-d' ) . ".txt"; |
|
79 | - $time = $date->format( 'H:i:s' ); |
|
78 | + $log = $this->path . $date->format('Y-m-d') . ".txt"; |
|
79 | + $time = $date->format('H:i:s'); |
|
80 | 80 | |
81 | 81 | $messageFormat = "[$time]\r\n$message\r\n\r\n"; |
82 | 82 | |
83 | - if ( is_dir( $this->path ) ) { |
|
84 | - if ( !file_exists( $log ) ) { |
|
85 | - $fh = fopen( $log, 'a+' ) or die( "Fatal Error !" ); |
|
86 | - fwrite( $fh, $messageFormat ); |
|
87 | - fclose( $fh ); |
|
83 | + if (is_dir($this->path)) { |
|
84 | + if (!file_exists($log)) { |
|
85 | + $fh = fopen($log, 'a+') or die("Fatal Error !"); |
|
86 | + fwrite($fh, $messageFormat); |
|
87 | + fclose($fh); |
|
88 | 88 | } |
89 | 89 | else { |
90 | - $this->edit( $log, $date, $messageFormat ); |
|
90 | + $this->edit($log, $date, $messageFormat); |
|
91 | 91 | } |
92 | 92 | } |
93 | 93 | else { |
94 | - if ( mkdir( $this->path, 0777 ) === true ) { |
|
95 | - $this->write( $message ); |
|
94 | + if (mkdir($this->path, 0777) === true) { |
|
95 | + $this->write($message); |
|
96 | 96 | } |
97 | 97 | } |
98 | 98 | } |
@@ -103,9 +103,9 @@ discard block |
||
103 | 103 | * @param \DateTime $date |
104 | 104 | * @param string $message |
105 | 105 | */ |
106 | - private function edit( $log, \DateTime $date, $message ) |
|
106 | + private function edit($log, \DateTime $date, $message) |
|
107 | 107 | { |
108 | - file_put_contents( $log, $message, FILE_APPEND ); |
|
108 | + file_put_contents($log, $message, FILE_APPEND); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | */ |
115 | 115 | public static function getInstance() |
116 | 116 | { |
117 | - if ( null === self::$instance ) { |
|
117 | + if (null === self::$instance) { |
|
118 | 118 | self::$instance = new self(); |
119 | 119 | } |
120 | 120 |
@@ -42,8 +42,8 @@ discard block |
||
42 | 42 | */ |
43 | 43 | public function getMasterConnection() |
44 | 44 | { |
45 | - if ( !is_a( $this->pdoMaster, \PDO::class ) ) |
|
46 | - $this->pdoMaster = $this->connect( $this->config->getMasterDataConnect() ); |
|
45 | + if (!is_a($this->pdoMaster, \PDO::class)) |
|
46 | + $this->pdoMaster = $this->connect($this->config->getMasterDataConnect()); |
|
47 | 47 | |
48 | 48 | return $this->pdoMaster; |
49 | 49 | } |
@@ -53,11 +53,11 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function getSlaveConnection() |
55 | 55 | { |
56 | - if ( !$this->config->getReplicationEnable() ) |
|
56 | + if (!$this->config->getReplicationEnable()) |
|
57 | 57 | return $this->getMasterConnection(); |
58 | 58 | |
59 | - if ( !is_a( $this->pdoSlave, \PDO::class ) ) |
|
60 | - $this->pdoSlave = $this->connect( $this->config->getSlaveDataConnect() ); |
|
59 | + if (!is_a($this->pdoSlave, \PDO::class)) |
|
60 | + $this->pdoSlave = $this->connect($this->config->getSlaveDataConnect()); |
|
61 | 61 | |
62 | 62 | return $this->pdoSlave; |
63 | 63 | } |
@@ -66,11 +66,11 @@ discard block |
||
66 | 66 | * @param $statement |
67 | 67 | * @return \PDO |
68 | 68 | */ |
69 | - public function getConnection( $statement ) |
|
69 | + public function getConnection($statement) |
|
70 | 70 | { |
71 | - $statement = trim( strtolower( $statement ) ); |
|
71 | + $statement = trim(strtolower($statement)); |
|
72 | 72 | |
73 | - if ( $statement === DbService::QUERY_TYPE_SELECT ) |
|
73 | + if ($statement === DbService::QUERY_TYPE_SELECT) |
|
74 | 74 | return $this->getSlaveConnection(); |
75 | 75 | |
76 | 76 | return $this->getMasterConnection(); |
@@ -80,9 +80,9 @@ discard block |
||
80 | 80 | * @param $string |
81 | 81 | * @return string |
82 | 82 | */ |
83 | - public function quote( $string ) |
|
83 | + public function quote($string) |
|
84 | 84 | { |
85 | - return $this->getMasterConnection()->quote( $string ); |
|
85 | + return $this->getMasterConnection()->quote($string); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
@@ -97,22 +97,22 @@ discard block |
||
97 | 97 | * @param $dataConnect |
98 | 98 | * @return \PDO |
99 | 99 | */ |
100 | - private function connect( $dataConnect ) |
|
100 | + private function connect($dataConnect) |
|
101 | 101 | { |
102 | 102 | $dsn = 'mysql:dbname=' . $dataConnect["dbname"] . ';host=' . $dataConnect["host"] . ''; |
103 | 103 | try { |
104 | 104 | |
105 | - $pdo = new \PDO( $dsn, $dataConnect["user"], $dataConnect["password"], |
|
106 | - array( \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" ) |
|
105 | + $pdo = new \PDO($dsn, $dataConnect["user"], $dataConnect["password"], |
|
106 | + array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") |
|
107 | 107 | ); |
108 | 108 | |
109 | - $pdo->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION ); |
|
110 | - $pdo->setAttribute( \PDO::ATTR_EMULATE_PREPARES, false ); |
|
111 | - } catch ( \PDOException $e ) { |
|
112 | - if ( DbConfig::getInstance()->isEnableLogErrors() ) { |
|
113 | - DbLog::getInstance()->writeQueryErros( 'Connection fail!', $e->getCode(), $e->getMessage() ); |
|
109 | + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
110 | + $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); |
|
111 | + } catch (\PDOException $e) { |
|
112 | + if (DbConfig::getInstance()->isEnableLogErrors()) { |
|
113 | + DbLog::getInstance()->writeQueryErros('Connection fail!', $e->getCode(), $e->getMessage()); |
|
114 | 114 | } |
115 | - throw new \PDOException( $e->getMessage(), $e->getCode() ); |
|
115 | + throw new \PDOException($e->getMessage(), $e->getCode()); |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | return $pdo; |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | */ |
125 | 125 | public static function getInstance() |
126 | 126 | { |
127 | - if ( null === self::$instance ) { |
|
127 | + if (null === self::$instance) { |
|
128 | 128 | self::$instance = new self(); |
129 | 129 | } |
130 | 130 |