@@ -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,55 +135,55 @@ 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 | 188 | throw new DbException($e->getMessage(), DbException::DB_QUERY_ERROR); |
| 189 | 189 | } |
@@ -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 | case 'insert': |
@@ -256,11 +256,11 @@ discard block |
||
| 256 | 256 | * @param array $arr |
| 257 | 257 | * @return bool |
| 258 | 258 | */ |
| 259 | - public static function isArrayAssoc( array $arr ) |
|
| 259 | + public static function isArrayAssoc(array $arr) |
|
| 260 | 260 | { |
| 261 | - if ( array() === $arr ) return false; |
|
| 261 | + if (array() === $arr) return false; |
|
| 262 | 262 | |
| 263 | - return array_keys( $arr ) !== range( 0, count( $arr ) - 1 ); |
|
| 263 | + return array_keys($arr) !== range(0, count($arr) - 1); |
|
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | |
@@ -269,7 +269,7 @@ discard block |
||
| 269 | 269 | */ |
| 270 | 270 | public static function getInstance() |
| 271 | 271 | { |
| 272 | - if ( null === self::$instance ) { |
|
| 272 | + if (null === self::$instance) { |
|
| 273 | 273 | self::$instance = new self(); |
| 274 | 274 | } |
| 275 | 275 | |
@@ -11,13 +11,13 @@ |
||
| 11 | 11 | include_once $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php'; |
| 12 | 12 | |
| 13 | 13 | |
| 14 | -$query = QueryBuild::select( 'employees22' ) |
|
| 14 | +$query = QueryBuild::select('employees22') |
|
| 15 | 15 | ->fields('lastName, jobTitle, officeCode') |
| 16 | - ->whereEqual( 'jobTitle', "Sales Rep" ) |
|
| 17 | - ->whereIn( 'officeCode', [ 2, 3, 4 ] ); |
|
| 16 | + ->whereEqual('jobTitle', "Sales Rep") |
|
| 17 | + ->whereIn('officeCode', [2, 3, 4]); |
|
| 18 | 18 | |
| 19 | 19 | |
| 20 | -echo "<pre>" . print_r( $query->getSyntax(), 1 ) . "</pre>"; |
|
| 21 | -echo "<pre>" . print_r( $query->getBindParams(), 1 ) . "</pre>"; |
|
| 22 | -echo "<pre>" . print_r( $query->getSyntax( 1 ), 1 ) . "</pre>"; |
|
| 23 | -echo "<pre>" . print_r( $query->execute(), 1 ) . "</pre>"; |
|
| 24 | 20 | \ No newline at end of file |
| 21 | +echo "<pre>" . print_r($query->getSyntax(), 1) . "</pre>"; |
|
| 22 | +echo "<pre>" . print_r($query->getBindParams(), 1) . "</pre>"; |
|
| 23 | +echo "<pre>" . print_r($query->getSyntax(1), 1) . "</pre>"; |
|
| 24 | +echo "<pre>" . print_r($query->execute(), 1) . "</pre>"; |
|
| 25 | 25 | \ No newline at end of file |