From 389ae61b844d3c03bfd023f0c6078895d2f771db Mon Sep 17 00:00:00 2001 From: Edwin Pranatha Date: Fri, 28 Jun 2024 18:01:03 +0800 Subject: [PATCH] EP - update to 1.2.0 support PHP8.3 --- src/.DS_Store | Bin 0 -> 6148 bytes src/Connection.php | 243 ++++++++++++++------------- src/Connectors/ConnectionFactory.php | 106 ++++++------ src/Connectors/SQLiteConnector.php | 21 ++- src/DB.php | 48 ++++-- src/Exception/QueryException.php | 1 + 6 files changed, 230 insertions(+), 189 deletions(-) create mode 100644 src/.DS_Store diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9c0ec106351db86ead02007fdae0e64e9af9844c GIT binary patch literal 6148 zcmeHKJ5EC}5S%3`f@o4w`U>2@ijos>0hELyQj~`TNR+SQTpW$rPeJsc3r#dDt;b&P z*zy!_-vY4J`~Cq~0$9);@#(|VeBXUyHx)4=ooBq@4F?Q3?O!L^zXzOqiw!=o!}Cx6 z?zkO>qi@TkfE17dQa}nwfeR{7#dUFa!83J~6p#Yfp@4rM8r`uMj*0Q<;1DeUan5iU z=g~_Ln+J%!a7<)`W=SO`)vCp?q%+xmkrfD|}a zV3y0J*Z&RukN*Fhq?Htq0#~Jg&DW3X6`xeKb@Di`wT=En_na@f8|Ojc5apN{<(LaE e$M=zxdClkC?}cMx&>0UpQ9lE&i%bgqw*ueB`xTl1 literal 0 HcmV?d00001 diff --git a/src/Connection.php b/src/Connection.php index a6a53ba..57646bc 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -81,7 +81,7 @@ class Connection implements ConnectionInterface * @param ExceptionHandlerInterface $exceptionHandler * @param string $tablePrefix */ - public function __construct(PDO $pdo = null, Grammar $queryGrammar = null, ExceptionHandlerInterface $exceptionHandler = null, $tablePrefix = '') + public function __construct (PDO $pdo = null, Grammar $queryGrammar = null, ExceptionHandlerInterface $exceptionHandler = null, $tablePrefix = '') { $this->pdo = $pdo; @@ -92,7 +92,8 @@ public function __construct(PDO $pdo = null, Grammar $queryGrammar = null, Excep $this->tablePrefix = $tablePrefix; } - public function newQuery() { + public function newQuery () + { return new Builder( $this, $this->getQueryGrammar() ); } @@ -102,7 +103,7 @@ public function newQuery() { * @param string $table * @return \Neko\Database\Query\Builder */ - public function table($table) + public function table ($table) { //$query = new Query\Builder($this, $this->getQueryGrammar()); //return $query->from($table); @@ -114,9 +115,9 @@ public function table($table) * @param $values * @return \PDOStatement */ - public function insert($table, array $values) + public function insert ($table, array $values) { - return $this->table($table)->insert($values); + return $this->table( $table )->insert( $values ); } /** @@ -124,9 +125,9 @@ public function insert($table, array $values) * @param $values * @return \PDOStatement */ - public function insertIgnore($table, array $values) + public function insertIgnore ($table, array $values) { - return $this->table($table)->insertIgnore($values); + return $this->table( $table )->insertIgnore( $values ); } /** @@ -134,9 +135,9 @@ public function insertIgnore($table, array $values) * @param $values * @return \PDOStatement */ - public function replace($table, array $values) + public function replace ($table, array $values) { - return $this->table($table)->replace($values); + return $this->table( $table )->replace( $values ); } /** @@ -145,9 +146,9 @@ public function replace($table, array $values) * @param $updateValues * @return \PDOStatement */ - public function insertUpdate($table, array $values, array $updateValues) + public function insertUpdate ($table, array $values, array $updateValues) { - return $this->table($table)->insertUpdate($values, $updateValues); + return $this->table( $table )->insertUpdate( $values, $updateValues ); } /** @@ -156,9 +157,9 @@ public function insertUpdate($table, array $values, array $updateValues) * @param $bindings * @return \PDOStatement */ - public function delete($table, $where, array $bindings = array()) + public function delete ($table, $where, array $bindings = array()) { - return $this->table($table)->whereRaw($where, $bindings)->delete(); + return $this->table( $table )->whereRaw( $where, $bindings )->delete(); } /** @@ -168,9 +169,9 @@ public function delete($table, $where, array $bindings = array()) * @param $bindings * @return \PDOStatement */ - public function update($table, $values, $where, array $bindings = array()) + public function update ($table, $values, $where, array $bindings = array()) { - return $this->table($table)->whereRaw($where, $bindings)->update($values); + return $this->table( $table )->whereRaw( $where, $bindings )->update( $values ); } /** @@ -179,9 +180,9 @@ public function update($table, $values, $where, array $bindings = array()) * @param mixed $value * @return \Neko\Database\Query\Expression */ - public function raw($value) + public function raw ($value) { - return new Query\Expression($value); + return new Query\Expression( $value ); } /** @@ -192,9 +193,9 @@ public function raw($value) * @param bool $useReadPdo * @return mixed */ - public function fetchOne($query, array $bindings = array(), $useReadPdo = true) + public function fetchOne ($query, array $bindings = array(), $useReadPdo = true) { - return $this->run($query, $bindings, $useReadPdo)->fetchColumn(); + return $this->run( $query, $bindings, $useReadPdo )->fetchColumn(); } /** @@ -205,9 +206,9 @@ public function fetchOne($query, array $bindings = array(), $useReadPdo = true) * @param bool $useReadPdo * @return array */ - public function fetch($query, array $bindings = array(), $useReadPdo = true) + public function fetch ($query, array $bindings = array(), $useReadPdo = true) { - return $this->run($query, $bindings, $useReadPdo)->fetch($this->getFetchMode()); + return $this->run( $query, $bindings, $useReadPdo )->fetch( $this->getFetchMode() ); } /** @@ -218,9 +219,9 @@ public function fetch($query, array $bindings = array(), $useReadPdo = true) * @param bool $useReadPdo * @return array */ - public function fetchNumeric($query, array $bindings = array(), $useReadPdo = true) + public function fetchNumeric ($query, array $bindings = array(), $useReadPdo = true) { - return $this->run($query, $bindings, $useReadPdo)->fetch(PDO::FETCH_NUM); + return $this->run( $query, $bindings, $useReadPdo )->fetch( PDO::FETCH_NUM ); } /** @@ -231,9 +232,9 @@ public function fetchNumeric($query, array $bindings = array(), $useReadPdo = tr * @param bool $useReadPdo * @return array */ - public function fetchAll($query, array $bindings = array(), $useReadPdo = true) + public function fetchAll ($query, array $bindings = array(), $useReadPdo = true) { - return $this->run($query, $bindings, $useReadPdo)->fetchAll($this->getFetchMode()); + return $this->run( $query, $bindings, $useReadPdo )->fetchAll( $this->getFetchMode() ); } /** @@ -241,9 +242,9 @@ public function fetchAll($query, array $bindings = array(), $useReadPdo = true) * @param array $bindings * @return \PDOStatement */ - public function query($query, array $bindings = array()) + public function query ($query, array $bindings = array()) { - return $this->run($query, $bindings); + return $this->run( $query, $bindings ); } /** @@ -252,14 +253,14 @@ public function query($query, array $bindings = array()) * @param \Closure $callback * @return array */ - public function pretend(Closure $callback) + public function pretend (Closure $callback) { $this->pretending = true; // Basically to make the database connection "pretend", we will just return // the default values for all the query methods, then we will return an // array of queries that were "executed" within the Closure callback. - $callback($this); + $callback( $this ); $this->pretending = false; @@ -270,9 +271,9 @@ public function pretend(Closure $callback) * @param null $name * @return mixed|string */ - public function lastInsertId($name = null) + public function lastInsertId ($name = null) { - return $this->pdo->lastInsertId($name); + return $this->pdo->lastInsertId( $name ); } /** @@ -281,13 +282,14 @@ public function lastInsertId($name = null) * @param $string * @return array|string */ - public function quote($string) + public function quote ($string) { - if (is_array($string)) { - return array_map(array($this->pdo, 'quote'), $string); + if (is_array( $string )) + { + return array_map( array( $this->pdo, 'quote' ), $string ); } - return $this->pdo->quote($string); + return $this->pdo->quote( $string ); } /** @@ -297,12 +299,13 @@ public function quote($string) * @param array $bind * @return mixed */ - public function quoteInto($sql, array $bind = array()) + public function quoteInto ($sql, array $bind = array()) { - foreach ($bind as $key => $value) { - $replace = (is_numeric($key) ? '?' : ':' . $key); + foreach ( $bind as $key => $value ) + { + $replace = (is_numeric( $key ) ? '?' : ':' . $key); - $sql = substr_replace($sql, $this->quote($value), strpos($sql, $replace), strlen($replace)); + $sql = substr_replace( $sql, $this->quote( $value ), strpos( $sql, $replace ), strlen( $replace ) ); } return $sql; @@ -318,18 +321,29 @@ public function quoteInto($sql, array $bind = array()) * * @throws \Exception */ - protected function run($query, $bindings, $useReadPdo = false) + protected function run ($query, $bindings, $useReadPdo = false) { global $app; $this->reconnectIfMissingConnection(); // We can calculate the time it takes to execute the query and log the SQL, bindings and time against our logger. - $start = microtime(true); + $start = microtime( true ); - $statement = $this->execute($query, $bindings, $useReadPdo); + $statement = $this->execute( $query, $bindings, $useReadPdo ); - $app->hook->apply('database_exec', [$query, $bindings, $start]); - $this->logQuery($query, $bindings, $start); + if (php_sapi_name() !== 'cli') + { + $app->hook->apply( 'database_exec', [ $query, $bindings, $start ] ); + } + + if (isset($_ENV['LOG_LEVEL']) && strtolower( $_ENV['LOG_LEVEL'] ) == "debug") + { + Log::write( "debug", array( + 'query' => $query, + 'bindings' => $bindings, + 'time' => $start + ), true ); + } return $statement; } @@ -341,27 +355,29 @@ protected function run($query, $bindings, $useReadPdo = false) * @return \PDOStatement * @throws \Exception */ - private function execute($query, $bindings, $useReadPdo) + private function execute ($query, $bindings, $useReadPdo) { if ($this->pretending()) return new \PDOStatement(); $pdo = $useReadPdo ? $this->getReadPdo() : $this->getPdo(); - try { + try + { // For update or delete statements, we want to get the number of rows affected // by the statement and return that back to the developer. We'll first need // to execute the statement and then we'll use PDO to fetch the affected. - $statement = $pdo->prepare($query); + $statement = $pdo->prepare( $query ); - $statement->execute($this->prepareBindings($bindings)); + $statement->execute( $this->prepareBindings( $bindings ) ); } - // If an exception occurs when attempting to run a query, we'll call the exception handler - // if there is one, or throw the exception if not - catch (\Exception $e) { + // If an exception occurs when attempting to run a query, we'll call the exception handler + // if there is one, or throw the exception if not + catch ( \Exception $e ) + { - if($this->exceptionHandler) + if ($this->exceptionHandler) { - $this->exceptionHandler->handle($query, $this->prepareBindings($bindings), $e); + $this->exceptionHandler->handle( $query, $this->prepareBindings( $bindings ), $e ); } throw $e; @@ -376,17 +392,20 @@ private function execute($query, $bindings, $useReadPdo) * @param array $bindings * @return array */ - public function prepareBindings(array $bindings) + public function prepareBindings (array $bindings) { $grammar = $this->getQueryGrammar(); - foreach ($bindings as $key => $value) { + foreach ( $bindings as $key => $value ) + { // We need to transform all instances of the DateTime class into an actual // date string. Each query grammar maintains its own date string format // so we'll just ask the grammar for the format to get from the date. - if ($value instanceof DateTime) { - $bindings[$key] = $value->format($grammar->getDateFormat()); - } elseif ($value === false) { + if ($value instanceof DateTime) + { + $bindings[$key] = $value->format( $grammar->getDateFormat() ); + } elseif ($value === false) + { $bindings[$key] = 0; } } @@ -402,23 +421,25 @@ public function prepareBindings(array $bindings) * * @throws \Exception */ - public function transaction(Closure $callback) + public function transaction (Closure $callback) { $this->beginTransaction(); // We'll simply execute the given callback within a try / catch block // and if we catch any exception we can rollback the transaction // so that none of the changes are persisted to the database. - try { - $result = $callback($this); + try + { + $result = $callback( $this ); $this->commit(); } - // If we catch an exception, we will roll back so nothing gets messed - // up in the database. Then we'll re-throw the exception so it can - // be handled how the developer sees fit for their applications. - catch (\Exception $e) { + // If we catch an exception, we will roll back so nothing gets messed + // up in the database. Then we'll re-throw the exception so it can + // be handled how the developer sees fit for their applications. + catch ( \Exception $e ) + { $this->rollBack(); throw $e; @@ -432,10 +453,10 @@ public function transaction(Closure $callback) * * @return $this */ - public function beginTransaction() + public function beginTransaction () { $this->reconnectIfMissingConnection(); - + $this->pdo->beginTransaction(); return $this; @@ -446,7 +467,7 @@ public function beginTransaction() * * @return $this */ - public function commit() + public function commit () { $this->pdo->commit(); @@ -456,7 +477,7 @@ public function commit() /** * @return $this */ - public function rollBack() + public function rollBack () { $this->pdo->rollBack(); @@ -466,7 +487,7 @@ public function rollBack() /** * @return bool */ - public function inTransaction() + public function inTransaction () { return $this->pdo->inTransaction(); } @@ -476,9 +497,9 @@ public function inTransaction() * * @return $this */ - public function disconnect() + public function disconnect () { - $this->setPdo(null)->setReadPdo(null); + $this->setPdo( null )->setReadPdo( null ); return $this; } @@ -486,7 +507,7 @@ public function disconnect() /** * */ - public function connect() + public function connect () { $this->reconnectIfMissingConnection(); @@ -500,21 +521,21 @@ public function connect() * * @throws \LogicException */ - public function reconnect() + public function reconnect () { - if (is_callable($this->reconnector)) { + if (is_callable( $this->reconnector )) + { try { - return call_user_func($this->reconnector, $this); - } - catch(\PDOException $e) + return call_user_func( $this->reconnector, $this ); + } catch ( \PDOException $e ) { - $this->exceptionHandler->handle("Connection attempt", array(), $e); + $this->exceptionHandler->handle( "Connection attempt", array(), $e ); } } - throw new \LogicException("Lost connection and no reconnector available."); + throw new \LogicException( "Lost connection and no reconnector available." ); } /** @@ -522,9 +543,10 @@ public function reconnect() * * @return void */ - protected function reconnectIfMissingConnection() + protected function reconnectIfMissingConnection () { - if (is_null($this->getPdo()) || is_null($this->getReadPdo())) { + if (is_null( $this->getPdo() ) || is_null( $this->getReadPdo() )) + { $this->reconnect(); } } @@ -537,16 +559,10 @@ protected function reconnectIfMissingConnection() * @param float $start * @return void */ - protected function logQuery($query, $bindings, $start = null) + protected function logQuery ($query, $bindings, $start = null) { - if ($this->loggingQueries==false) return; - $time = $start ? round((microtime(true) - $start) * 1000, 2) : null; - - Log::write("debug",array( - 'query' => $query, - 'bindings' => $bindings, - 'time' => $time - ),true); + if ($this->loggingQueries == false) return; + $time = $start ? round( (microtime( true ) - $start) * 1000, 2 ) : null; } /** @@ -554,7 +570,7 @@ protected function logQuery($query, $bindings, $start = null) * * @return \PDO */ - public function getPdo() + public function getPdo () { return $this->pdo; } @@ -564,7 +580,7 @@ public function getPdo() * * @return \PDO */ - public function getReadPdo() + public function getReadPdo () { if (!$this->readPdo || $this->pdo->inTransaction()) { @@ -580,7 +596,7 @@ public function getReadPdo() * @param \PDO|null $pdo * @return $this */ - public function setPdo($pdo) + public function setPdo ($pdo) { $this->pdo = $pdo; @@ -593,7 +609,7 @@ public function setPdo($pdo) * @param \PDO|null $pdo * @return $this */ - public function setReadPdo($pdo) + public function setReadPdo ($pdo) { $this->readPdo = $pdo; @@ -606,7 +622,7 @@ public function setReadPdo($pdo) * @param callable $reconnector * @return $this */ - public function setReconnector(callable $reconnector) + public function setReconnector (callable $reconnector) { $this->reconnector = $reconnector; @@ -618,17 +634,17 @@ public function setReconnector(callable $reconnector) * * @return string */ - public function getDriverName() + public function getDriverName () { - return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); + return $this->pdo->getAttribute( \PDO::ATTR_DRIVER_NAME ); } - + /** * Get the query grammar used by the connection. * * @return \Neko\Database\Query\Grammars\Grammar */ - public function getQueryGrammar() + public function getQueryGrammar () { return $this->queryGrammar; } @@ -639,7 +655,7 @@ public function getQueryGrammar() * @param \Neko\Database\Query\Grammars\Grammar * @return $this */ - public function setQueryGrammar(Query\Grammars\Grammar $grammar) + public function setQueryGrammar (Query\Grammars\Grammar $grammar) { $this->queryGrammar = $grammar; @@ -651,7 +667,7 @@ public function setQueryGrammar(Query\Grammars\Grammar $grammar) * * @return bool */ - public function pretending() + public function pretending () { return $this->pretending === true; } @@ -661,7 +677,7 @@ public function pretending() * * @return int */ - public function getFetchMode() + public function getFetchMode () { return $this->fetchMode; } @@ -672,7 +688,7 @@ public function getFetchMode() * @param int $fetchMode * @return int */ - public function setFetchMode($fetchMode) + public function setFetchMode ($fetchMode) { $this->fetchMode = $fetchMode; @@ -684,7 +700,7 @@ public function setFetchMode($fetchMode) * * @return $this */ - public function enableQueryLog($bool=false) + public function enableQueryLog ($bool = false) { $this->loggingQueries = $bool; return $this; @@ -695,7 +711,7 @@ public function enableQueryLog($bool=false) * * @return bool */ - public function logging() + public function logging () { return $this->loggingQueries; } @@ -705,7 +721,7 @@ public function logging() * * @return string */ - public function getTablePrefix() + public function getTablePrefix () { return $this->tablePrefix; } @@ -716,11 +732,11 @@ public function getTablePrefix() * @param string $prefix * @return $this */ - public function setTablePrefix($prefix) + public function setTablePrefix ($prefix) { $this->tablePrefix = $prefix; - $this->getQueryGrammar()->setTablePrefix($prefix); + $this->getQueryGrammar()->setTablePrefix( $prefix ); return $this; } @@ -730,7 +746,7 @@ public function setTablePrefix($prefix) * * @return LoggerInterface $logger */ - public function setLogger($bool=false) + public function setLogger ($bool = false) { return $bool; } @@ -739,7 +755,7 @@ public function setLogger($bool=false) * @param ExceptionHandlerInterface $exceptionHandler * @return $this */ - public function setExceptionHandler(ExceptionHandlerInterface $exceptionHandler) + public function setExceptionHandler (ExceptionHandlerInterface $exceptionHandler) { $this->exceptionHandler = $exceptionHandler; @@ -764,9 +780,10 @@ public function setExceptionHandler(ExceptionHandlerInterface $exceptionHandler) * * @return object */ - public function first($sql, $bindings = []) + public function first ($sql, $bindings = []) { - if (count($results = $this->query($sql, $bindings)->fetchAll($this->getFetchMode())) > 0) { + if (count( $results = $this->query( $sql, $bindings )->fetchAll( $this->getFetchMode() ) ) > 0) + { return $results[0]; } } diff --git a/src/Connectors/ConnectionFactory.php b/src/Connectors/ConnectionFactory.php index a4249b6..bd8553b 100644 --- a/src/Connectors/ConnectionFactory.php +++ b/src/Connectors/ConnectionFactory.php @@ -1,4 +1,5 @@ -connectionClassName = $connectionClassName; } } @@ -64,17 +66,19 @@ public function __construct($connectionClassName = null) * @param array $config * @return \Neko\Database\Connection */ - public function make(array $config) + public function make (array $config) { - if (!isset($config['driver'])) { - throw new \InvalidArgumentException("A driver must be specified."); + if (!isset($config['driver'])) + { + throw new \InvalidArgumentException( "A driver must be specified." ); } - return $this->makeConnection($config, !empty($config['lazy']))->setReconnector(function (Connection $connection) use ($config) { - $fresh = $this->makeConnection($config, false); + return $this->makeConnection( $config, !empty($config['lazy']) )->setReconnector( function (Connection $connection) use ($config) + { + $fresh = $this->makeConnection( $config, false ); - return $connection->setPdo($fresh->getPdo())->setReadPdo($fresh->getReadPdo()); - }); + return $connection->setPdo( $fresh->getPdo() )->setReadPdo( $fresh->getReadPdo() ); + } ); } /** @@ -84,13 +88,14 @@ public function make(array $config) * @param bool $lazy * @return \Neko\Database\Connection */ - protected function makeConnection(array $config, $lazy) + protected function makeConnection (array $config, $lazy) { - if (isset($config['read'])) { - return $this->createReadWriteConnection($config, $lazy); + if (isset($config['read'])) + { + return $this->createReadWriteConnection( $config, $lazy ); } - return $this->createSingleConnection($config, $lazy); + return $this->createSingleConnection( $config, $lazy ); } /** @@ -100,18 +105,18 @@ protected function makeConnection(array $config, $lazy) * @param bool $lazy * @return \Neko\Database\Connection */ - protected function createSingleConnection(array $config, $lazy) + protected function createSingleConnection (array $config, $lazy) { $connection = $this->createConnection(); $connection - ->setExceptionHandler($this->createExceptionHandler($config)) - ->setQueryGrammar($this->createQueryGrammar($config['driver'])) - ->setTablePrefix(isset($config['prefix']) ? $config['prefix'] : ''); + ->setExceptionHandler( $this->createExceptionHandler( $config ) ) + ->setQueryGrammar( $this->createQueryGrammar( $config['driver'] ) ) + ->setTablePrefix( isset($config['prefix']) ? $config['prefix'] : '' ); - if(!$lazy) + if (!$lazy) { - $connection->setPdo($this->createConnector($config['driver'])->connect($config)); + $connection->setPdo( $this->createConnector( $config['driver'] )->connect( $config ) ); } return $connection; @@ -121,7 +126,7 @@ protected function createSingleConnection(array $config, $lazy) /** * @return \Neko\Database\Connection */ - protected function createConnection() + protected function createConnection () { return new $this->connectionClassName; } @@ -132,13 +137,13 @@ protected function createConnection() * @param array $config * @return \Neko\Database\Connection */ - protected function createReadWriteConnection(array $config, $lazy) + protected function createReadWriteConnection (array $config, $lazy) { - $connection = $this->createSingleConnection($this->getWriteConfig($config), $lazy); + $connection = $this->createSingleConnection( $this->getWriteConfig( $config ), $lazy ); - if(!$lazy) + if (!$lazy) { - $connection->setReadPdo($this->createReadPdo($config)); + $connection->setReadPdo( $this->createReadPdo( $config ) ); } return $connection; @@ -150,11 +155,11 @@ protected function createReadWriteConnection(array $config, $lazy) * @param array $config * @return \PDO */ - protected function createReadPdo(array $config) + protected function createReadPdo (array $config) { - $readConfig = $this->getReadConfig($config); + $readConfig = $this->getReadConfig( $config ); - return $this->createConnector($readConfig['driver'])->connect($readConfig); + return $this->createConnector( $readConfig['driver'] )->connect( $readConfig ); } /** @@ -163,11 +168,11 @@ protected function createReadPdo(array $config) * @param array $config * @return array */ - protected function getReadConfig(array $config) + protected function getReadConfig (array $config) { - $readConfig = $this->getReadWriteConfig($config, 'read'); + $readConfig = $this->getReadWriteConfig( $config, 'read' ); - return $this->mergeReadWriteConfig($config, $readConfig); + return $this->mergeReadWriteConfig( $config, $readConfig ); } /** @@ -176,11 +181,11 @@ protected function getReadConfig(array $config) * @param array $config * @return array */ - protected function getWriteConfig(array $config) + protected function getWriteConfig (array $config) { - $writeConfig = $this->getReadWriteConfig($config, 'write'); + $writeConfig = $this->getReadWriteConfig( $config, 'write' ); - return $this->mergeReadWriteConfig($config, $writeConfig); + return $this->mergeReadWriteConfig( $config, $writeConfig ); } /** @@ -190,10 +195,11 @@ protected function getWriteConfig(array $config) * @param string $type * @return array */ - protected function getReadWriteConfig(array $config, $type) + protected function getReadWriteConfig (array $config, $type) { - if (isset($config[$type][0])) { - return $config[$type][array_rand($config[$type])]; + if (isset($config[$type][0])) + { + return $config[$type][array_rand( $config[$type] )]; } return $config[$type]; @@ -206,9 +212,9 @@ protected function getReadWriteConfig(array $config, $type) * @param array $merge * @return array */ - protected function mergeReadWriteConfig(array $config, array $merge) + protected function mergeReadWriteConfig (array $config, array $merge) { - return array_diff_key(array_merge($config, $merge), array_flip(array('read', 'write'))); + return array_diff_key( array_merge( $config, $merge ), array_flip( array( 'read', 'write' ) ) ); } /** @@ -219,9 +225,10 @@ protected function mergeReadWriteConfig(array $config, array $merge) * * @throws \InvalidArgumentException */ - public function createConnector($driver) + public function createConnector ($driver) { - switch ($driver) { + switch ($driver) + { case 'mysql': return new MySqlConnector; @@ -235,7 +242,7 @@ public function createConnector($driver) return new SqlServerConnector; } - throw new \InvalidArgumentException("Unsupported driver [$driver]"); + throw new \InvalidArgumentException( "Unsupported driver [$driver]" ); } /** @@ -246,9 +253,10 @@ public function createConnector($driver) * * @throws \InvalidArgumentException */ - protected function createQueryGrammar($driver) + protected function createQueryGrammar ($driver) { - switch ($driver) { + switch ($driver) + { case 'mysql': return new MySqlGrammar(); break; @@ -266,13 +274,13 @@ protected function createQueryGrammar($driver) break; } - throw new \InvalidArgumentException("Unsupported driver [$driver]"); + throw new \InvalidArgumentException( "Unsupported driver [$driver]" ); } - protected function createExceptionHandler(array $config) + protected function createExceptionHandler (array $config) { - $logSafeParams = array_diff_key($config, array_flip($this->excludedLogParams)); + $logSafeParams = array_diff_key( $config, array_flip( $this->excludedLogParams ) ); - return new ExceptionHandler($logSafeParams); + return new ExceptionHandler( $logSafeParams ); } } diff --git a/src/Connectors/SQLiteConnector.php b/src/Connectors/SQLiteConnector.php index 35c5a77..856fb1d 100644 --- a/src/Connectors/SQLiteConnector.php +++ b/src/Connectors/SQLiteConnector.php @@ -1,4 +1,5 @@ -getOptions($config); + $options = $this->getOptions( $config ); // SQLite supports "in-memory" databases that only last as long as the owning // connection does. These are useful for tests or for short lifetime store // querying. In-memory databases may only have a single open connection. - if ($config['database'] == ':memory:') { - return $this->createConnection('sqlite::memory:', $config, $options); + if ($config['database'] == ':memory:') + { + return $this->createConnection( 'sqlite::memory:', $config, $options ); } - $path = realpath($config['database']); + $path = realpath( $config['database'] ); // Here we'll verify that the SQLite database exists before going any further // as the developer probably wants to know if the database exists and this // SQLite driver will not throw any exception if it does not by default. - if ($path === false) { - throw new \InvalidArgumentException("Database file does not exist path = '".$path."'"); + if ($path === false) + { + throw new \InvalidArgumentException( "Database file does not exist path = '" . $path . "'" ); } - return $this->createConnection("sqlite:{$path}", $config, $options); + return $this->createConnection( "sqlite:{$path}", $config, $options ); } } diff --git a/src/DB.php b/src/DB.php index e64dd52..04adc3d 100644 --- a/src/DB.php +++ b/src/DB.php @@ -2,6 +2,7 @@ namespace Neko\Database; use Neko\Database\Connectors\ConnectionFactory; + /** * Main database class. * @@ -20,14 +21,15 @@ * * @package \Neko\Database */ -class DB { +class DB +{ /** * The database connection. * * @var \Neko\Database\Connection */ protected static $connection; - + /** * Begin a fluent query against a database table. @@ -36,7 +38,8 @@ class DB { * * @return \Neko\Database\Builder */ - public static function table( $table ) { + public static function table ($table) + { return static::getConnection()->table( $table ); } @@ -47,21 +50,24 @@ public static function table( $table ) { * @param array $bindings * @return array */ - public static function select( $query, array $bindings = [] ) { + public static function select ($query, array $bindings = []) + { return static::getConnection()->fetchAll( $query, $bindings ); } - public static function connection($con_string) { + public static function connection ($con_string) + { global $app; - if($con_string == null) + if ($con_string == null) { $con_string = $app->config['db']["default"]; - }else{ + } else + { $con_string = $app->config['db'][$con_string]; } $factory = new ConnectionFactory(); - $con = $factory->make($con_string); + $con = $factory->make( $con_string ); return $con; } @@ -70,9 +76,11 @@ public static function connection($con_string) { * * @return \Database\ConnectionInterface|\Neko\Database\Connection */ - public static function getConnection() { + public static function getConnection () + { global $pdo; - if ( is_null( static::$connection ) ) { + if (is_null( static::$connection )) + { static::$connection = new Connection( $pdo ); } @@ -84,20 +92,22 @@ public static function getConnection() { * * @param \Database\ConnectionInterface $connection */ - public static function setConnection($con_string=null) { + public static function setConnection ($con_string = null) + { global $app; $factory = new ConnectionFactory(); - if($con_string == null) + if ($con_string == null) { $con_string = $app->config['db'][$app->config['db_connection']]; - }else{ + } else + { $con_string = $app->config['db'][$con_string]; } - $con = $factory->make($con_string); - $logging = (isset($con_string['logging']) && $con_string['logging']!==false) ? true : false; - $con->enableQueryLog($logging); + $con = $factory->make( $con_string ); + $logging = (isset($con_string['logging']) && $con_string['logging'] !== false) ? true : false; + $con->enableQueryLog( $logging ); static::$connection = $con; } @@ -109,8 +119,10 @@ public static function setConnection($con_string=null) { * * @return mixed */ - public static function __callStatic( $name, $arguments ) { - if ( method_exists( $connection = static::getConnection(), $name ) ) { + public static function __callStatic ($name, $arguments) + { + if (method_exists( $connection = static::getConnection(), $name )) + { return $connection->{$name}( ...$arguments ); } diff --git a/src/Exception/QueryException.php b/src/Exception/QueryException.php index dd18a30..c38a52e 100644 --- a/src/Exception/QueryException.php +++ b/src/Exception/QueryException.php @@ -17,6 +17,7 @@ class QueryException extends \RuntimeException { * @var array */ protected $bindings; + protected $previous; /** * Create a new query exception instance.