From 897ec14ec217b91252aac57d9b6d59986aa792b8 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 15 May 2015 09:26:23 +0200 Subject: [PATCH] Move to PSR-1 and PSR-2. --- Dal.php | 238 +++++++++++++++++------------------- DalStatement.php | 119 ++++++++---------- Exception.php | 10 +- IDal/Wrapper.php | 91 ++++++-------- IDal/WrapperStatement.php | 72 +++++------ Layer/Pdo/Pdo.php | 193 +++++++++++++++-------------- Layer/Pdo/Statement.php | 131 +++++++++----------- Query/Delete.php | 22 ++-- Query/Dml.php | 13 +- Query/Insert.php | 113 ++++++++--------- Query/Join.php | 41 +++---- Query/Query.php | 75 +++++------- Query/Select.php | 83 ++++++------- Query/SelectCore.php | 171 ++++++++++++-------------- Query/Update.php | 67 +++++----- Query/Where.php | 56 ++++----- Test/Unit/Query/Example.php | 52 ++++---- 17 files changed, 718 insertions(+), 829 deletions(-) diff --git a/Dal.php b/Dal.php index 145d308..9f85c1e 100644 --- a/Dal.php +++ b/Dal.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -43,14 +43,11 @@ * * The heigher class of the Database Abstract Layer. It wrappes all DAL. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Dal implements Core\Parameter\Parameterizable, - Core\Event\Source { - +class Dal implements Core\Parameter\Parameterizable, Core\Event\Source +{ /** * Abstract layer: DBA. * @@ -82,35 +79,35 @@ class Dal implements Core\Parameter\Parameterizable, /** * Multiton. * - * @var \Hoa\Database\Dal array + * @var array */ private static $_instance = []; /** * Current singleton ID. * - * @var \Hoa\Database\Dal string + * @var string */ private static $_id = null; /** * Current ID. * - * @var \Hoa\Database\Dal string + * @var string */ protected $__id = null; /** * The layer instance. * - * @var \Hoa\Database\IDal\Wrapper object + * @var \Hoa\Database\IDal\Wrapper */ protected $_layer = null; /** * Parameter of \Hoa\Database\Dal. * - * @var \Hoa\Core\Parameter object + * @var \Hoa\Core\Parameter */ protected static $_parameters = null; @@ -120,21 +117,25 @@ class Dal implements Core\Parameter\Parameterizable, * Create a DAL instance, representing a connection to a database. * The constructor is private to make a multiton. * - * @access private * @param string $dalName The database abstract layer name. * @param string $dsn The DSN of database. * @param string $username The username to connect to database. * @param string $password The password to connect to database. * @param array $driverOptions The driver options. * @return void - * @throw \Hoa\Database\Exception - */ - private function __construct ( $dalName, $dsn, $username, $password, - Array $driverOptions = [] ) { - + * @throws \Hoa\Database\Exception + */ + private function __construct( + $dalName, + $dsn, + $username, + $password, + Array $driverOptions = [] + ) { // Please see https://bugs.php.net/55154. - if(0 !== preg_match('#^sqlite:(.+)$#i', $dsn, $matches)) + if (0 !== preg_match('#^sqlite:(.+)$#i', $dsn, $matches)) { $dsn = 'sqlite:' . resolve($matches[1]); + } $id = $this->__id = self::$_id; $event = 'hoa://Event/Database/' . $id; @@ -164,12 +165,11 @@ private function __construct ( $dalName, $dsn, $username, $password, /** * Initialize parameters. * - * @access public * @param array $parameters Parameters. * @return void */ - public static function initializeParameters ( Array $parameters = [] ) { - + public static function initializeParameters(Array $parameters = []) + { self::$_parameters = new Core\Parameter( __CLASS__, [], @@ -194,7 +194,6 @@ public static function initializeParameters ( Array $parameters = [] ) { /** * Make a multiton on the $id. * - * @access public * @param string $id The instance ID. * @param string $dalName The database abstract layer name. * @param string $dsn The DSN of database. @@ -202,33 +201,40 @@ public static function initializeParameters ( Array $parameters = [] ) { * @param string $password The password to connect to database. * @param array $driverOptions The driver options. * @return \Hoa\Database\Dal - * @throw \Hoa\Database\Exception - */ - public static function getInstance ( $id, - $dalName = null, $dsn = null, - $username = null, $password = null, - Array $driverOptions = [] ) { - - if(null === self::$_parameters) + * @throws \Hoa\Database\Exception + */ + public static function getInstance( + $id, + $dalName = null, + $dsn = null, + $username = null, + $password = null, + Array $driverOptions = [] + ) { + if (null === self::$_parameters) { self::initializeParameters(); + } self::$_id = $id; - if(isset(self::$_instance[$id])) + if (isset(self::$_instance[$id])) { return self::$_instance[$id]; + } - if( null === $dalName - && null === $dsn - && null === $username - && null === $password - && empty($driverOptions)) { - + if (null === $dalName && + null === $dsn && + null === $username && + null === $password && + empty($driverOptions)) { $list = self::$_parameters->unlinearizeBranche('connection.list'); - if(!isset($list[$id])) + if (!isset($list[$id])) { throw new Exception( 'Connection ID %s does not exist in the connection list.', - 0, $id); + 0, + $id + ); + } $handle = $list[$id]; $dalName = @$handle['dal'] ?: 'Undefined'; @@ -252,28 +258,31 @@ public static function getInstance ( $id, * If no instance was set but if the connection.autoload parameter is set, * then we auto-connect (autoload) a connection. * - * @access public * @return \Hoa\Database\IDal\Wrapper - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public static function getLastInstance ( ) { - - if(null === self::$_parameters) + public static function getLastInstance() + { + if (null === self::$_parameters) { self::initializeParameters(); + } - if(null === self::$_id) { - + if (null === self::$_id) { $autoload = self::$_parameters->getFormattedParameter( 'connection.autoload' ); - if(null !== $autoload) + if (null !== $autoload) { self::getInstance($autoload); + } } - if(null === self::$_id) + if (null === self::$_id) { throw new Exception( - 'No instance was set, cannot return the last instance.', 0); + 'No instance was set, cannot return the last instance.', + 1 + ); + } return self::$_instance[self::$_id]; } @@ -281,22 +290,20 @@ public static function getLastInstance ( ) { /** * Get parameters. * - * @access public * @return \Hoa\Core\Parameter */ - public function getParameters ( ) { - + public function getParameters() + { return self::$_parameters; } /** * Close connection to the database. * - * @access public * @return bool */ - public function close ( ) { - + public function close() + { $id = $this->getId(); $event = 'hoa://Event/Database/' . $id; @@ -319,12 +326,11 @@ public function close ( ) { /** * Set database abstract layer instance. * - * @access protected * @param \Hoa\Database\IDal\Wrapper $dal The DAL instance. * @return \Hoa\Database\IDal\Wrapper */ - protected function setDal ( IDal\Wrapper $dal ) { - + protected function setDal(IDal\Wrapper $dal) + { $old = $this->_layer; $this->_layer = $dal; @@ -334,63 +340,59 @@ protected function setDal ( IDal\Wrapper $dal ) { /** * Get the database abstract layer instance. * - * @access protected * @return \Hoa\Database\IDal\Wrapper */ - protected function getDal ( ) { - + protected function getDal() + { return $this->_layer; } /** * Initiate a transaction. * - * @access public * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function beginTransaction ( ) { - + public function beginTransaction() + { return $this->getDal()->beginTransaction(); } /** * Commit a transaction. * - * @access public * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function commit ( ) { - + public function commit() + { return $this->getDal()->commit(); } /** * Roll back a transaction. * - * @access public * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function rollBack ( ) { - + public function rollBack() + { return $this->getDal()->rollBack(); } /** * Return the ID of the last inserted row or sequence value. * - * @access public * @param string $name Name of sequence object (needed for some * driver). * @return string - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function lastInsertId ( $name = null ) { - - if(null === $name) + public function lastInsertId($name = null) + { + if (null === $name) { return $this->getDal()->lastInsertId(); + } return $this->getDal()->lastInsertId($name); } @@ -398,16 +400,15 @@ public function lastInsertId ( $name = null ) { /** * Prepare a statement for execution and returns a statement object. * - * @access public * @param string $statement This must be a valid SQL statement for the * target database server. * @param array $options Options to set attributes values for the * layer statement. * @return \Hoa\Database\DalStatement - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function prepare ( $statement, Array $options = [] ) { - + public function prepare($statement, Array $options = []) + { return new DalStatement( $this->getDal()->prepare( $statement, $options @@ -418,17 +419,17 @@ public function prepare ( $statement, Array $options = [] ) { /** * Quote a string for use in a query. * - * @access public * @param string $string The string to be quoted. * @param int $type Provide a data type hint for drivers that * have alternate quoting styles. * @return string - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function quote ( $string = null, $type = -1 ) { - - if($type < 0) + public function quote($string = null, $type = -1) + { + if ($type < 0) { return $this->getDal()->quote($string); + } return $this->getDal()->quote($string, $type); } @@ -437,13 +438,12 @@ public function quote ( $string = null, $type = -1 ) { * Execute an SQL statement, returning a result set as a * \Hoa\Database\DalStatement object. * - * @access public * @param string $statement The SQL statement to prepare and execute. * @return \Hoa\Database\DalStatement - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function query ( $statement ) { - + public function query($statement) + { return new DalStatement( $this->getDal()->query($statement) ); @@ -453,12 +453,11 @@ public function query ( $statement ) { * Fetch the SQLSTATE associated with the last operation on the database * handle. * - * @access public * @return string - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function errorCode ( ) { - + public function errorCode() + { return $this->getDal()->errorCode(); } @@ -466,87 +465,80 @@ public function errorCode ( ) { * Fetch extends error information associated with the last operation on the * database handle. * - * @access public * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function errorInfo ( ) { - + public function errorInfo() + { return $this->getDal()->errorInfo(); } /** * Return an array of available drivers. * - * @access public * @return array - * @throw \Hoa\Datatase\Exception + * @throws \Hoa\Datatase\Exception */ - public function getAvailableDrivers ( ) { - + public function getAvailableDrivers() + { return $this->getDal()->getAvailableDrivers(); } /** * Set attributes. * - * @access public * @param array $attributes Attributes values. * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function setAttributes ( Array $attributes ) { - + public function setAttributes(Array $attributes) + { return $this->getDal()->setAttributes($attributes); } /** * Set a specific attribute. * - * @access public * @param mixed $attribute Attribute name. * @param mixed $value Attribute value. * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function setAttribute ( $attribute, $value ) { - + public function setAttribute($attribute, $value) + { return $this->getDal()->setAtribute($attribute, $value); } /** * Retrieve all database connection attributes. * - * @access public * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function getAttributes ( ) { - + public function getAttributes() + { return $this->getDal()->getAttributes(); } /** * Retrieve a database connection attribute. * - * @access public * @param string $attribute Attribute name. * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function getAttribute ( $attribute ) { - + public function getAttribute($attribute) + { return $this->getDal()->getAttribute($attribute); } /** * Get current ID. * - * @access public * @return string */ - public function getId ( ) { - + public function getId() + { return $this->__id; } } diff --git a/DalStatement.php b/DalStatement.php index 7bc18e9..77600f4 100644 --- a/DalStatement.php +++ b/DalStatement.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -41,18 +41,15 @@ * * The heigher class that represents a DAL statement. * - * @author Ivan Enderlin - * @author Raphaël Emourgeon - * @copyright Copyright © 2007-2015 Ivan Enderlin, Raphaël Emourgeon. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class DalStatement { - +class DalStatement +{ /** * The statement instance. * - * @var \Hoa\Database\IDal\WrapperStatement object + * @var \Hoa\Database\IDal\WrapperStatement */ protected $statement = null; @@ -61,28 +58,26 @@ class DalStatement { /** * Create a statement instance. * - * @access public * @param \Hoa\Database\IDal\WrapperStatement $statement The * statement * instance. * @return void */ - public function __construct ( IDal\WrapperStatement $statement ) { - + public function __construct(IDal\WrapperStatement $statement) + { $this->setStatement($statement); } /** * Set the statement instance. * - * @access protected * @param \Hoa\Database\IDal\WrapperStatement $statement The * statement * instance. * @return \Hoa\Database\IDal\WrapperStatement */ - protected function setStatement ( IDal\WrapperStatement $statement ) { - + protected function setStatement(IDal\WrapperStatement $statement) + { $old = $this->statement; $this->statement = $statement; @@ -92,27 +87,26 @@ protected function setStatement ( IDal\WrapperStatement $statement ) { /** * Get the statement instance. * - * @access protected * @return \Hoa\Database\IDal\WrapperStatement */ - protected function getStatement ( ) { - + protected function getStatement() + { return $this->statement; } /** * Execute a prepared statement. * - * @access public * @param array $bindParameters Bind parameters values if bindParam is * not called. * @return \Hoa\Database\DalStatement - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function execute ( Array $bindParameters = [] ) { - - if(empty($bindParameters)) + public function execute(Array $bindParameters = []) + { + if (empty($bindParameters)) { return $this->getStatement()->execute(); + } $this->getStatement()->execute($bindParameters); @@ -122,26 +116,30 @@ public function execute ( Array $bindParameters = [] ) { /** * Bind a parameter to te specified variable name. * - * @access public * @param mixed $parameter Parameter name. * @param mixed $value Parameter value. * @param int $type Type of value. * @param int $length Length of data type. * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function bindParameter ( $parameter, &$value, $type = null, - $length = null) { - - if(null === $type) + public function bindParameter( + $parameter, + &$value, + $type = null, + $length = null + ) { + if (null === $type) { return $this->getStatement()->bindParameter($parameter, $value); + } - if(null === $length) + if (null === $length) { return $this->getStatement()->bindParameter( $parameter, $value, $type ); + } return $this->getStatement()->bindParameter( $parameter, @@ -154,60 +152,55 @@ public function bindParameter ( $parameter, &$value, $type = null, /** * Return an array containing all of the result set rows. * - * @access public * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchAll ( ) { - + public function fetchAll() + { return $this->getStatement()->fetchAll(); } /** * Fetch the first row in the result set. * - * @access public * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchFirst ( ) { - + public function fetchFirst() + { return $this->getStatement()->fetchFirst(); } /** * Fetch the last row in the result set. * - * @access public * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchLast ( ) { - + public function fetchLast() + { return $this->getStatement()->fetchLast(); } /** * Fetch the next row in the result set. * - * @access public * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchNext ( ) { - + public function fetchNext() + { return $this->getStatement()->fetchNext(); } /** * Fetch the previous row in the result set. * - * @access public * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchPrior ( ) { - + public function fetchPrior() + { return $this->getStatement()->fetchPrior(); } @@ -215,25 +208,23 @@ public function fetchPrior ( ) { * Return a single column from the next row of the result set or false if * there is no more row. * - * @access public * @param int $column Column index. * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchColumn ( $column = 0 ) { - + public function fetchColumn($column = 0) + { return $this->getStatement()->fetchColumn($column); } /** * Close the cursor, enabling the statement to be executed again. * - * @access public * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function closeCursor ( ) { - + public function closeCursor() + { return $this->getStatement()->closeCursor(); } @@ -241,12 +232,11 @@ public function closeCursor ( ) { * Fetch the SQLSTATE associated with the last operation on the statement * handle. * - * @access public * @return string - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function errorCode ( ) { - + public function errorCode() + { return $this->getStatement()->errorCode(); } @@ -254,12 +244,11 @@ public function errorCode ( ) { * Fetch extends error information associated with the last operation on the * statement handle. * - * @access public * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function errorInfo ( ) { - + public function errorInfo() + { return $this->getStatement()->errorInfo(); } } diff --git a/Exception.php b/Exception.php index 5475a83..9f10488 100644 --- a/Exception.php +++ b/Exception.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -43,9 +43,9 @@ * * Extending the \Hoa\Core\Exception class. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Exception extends Core\Exception { } +class Exception extends Core\Exception +{ +} diff --git a/IDal/Wrapper.php b/IDal/Wrapper.php index 104530b..5e58ed9 100644 --- a/IDal/Wrapper.php +++ b/IDal/Wrapper.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -41,167 +41,154 @@ * * Interface of a DAL wrapper. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -interface Wrapper { - +interface Wrapper +{ /** * Create a DAL instance, representing a connection to a database. * - * @access public * @param string $dsn The DSN of database. * @param string $username The username to connect to database. * @param string $password The password to connect to database. * @param array $driverOptions The driver options. * @return void - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function __construct ( $dsn, $username, $password, - Array $driverOption = [] ); + public function __construct( + $dsn, + $username, + $password, + Array $driverOption = [] + ); /** * Initiate a transaction. * - * @access public * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function beginTransaction ( ); + public function beginTransaction(); /** * Commit a transaction. * - * @access public * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function commit ( ); + public function commit(); /** * Roll back a transaction. * - * @access public * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function rollBack ( ); + public function rollBack(); /** * Return the ID of the last inserted row or sequence value. * - * @access public * @param string $name Name of sequence object (needed for some * driver). * @return string - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function lastInsertId ( $name = null ); + public function lastInsertId($name = null); /** * Prepare a statement for execution and returns a statement object. * - * @access public * @param string $statement This must be a valid SQL statement for the * target database server. * @param array $options Options to set attributes values for the * AbstractLayer Statement. * @return \Hoa\Database\IDal\WrapperStatement - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function prepare ( $statement, Array $options = [] ); + public function prepare($statement, Array $options = []); /** * Quote a string for use in a query. * - * @access public * @param string $string The string to be quoted. * @param int $type Provide a data type hint for drivers that * have alternate quoting styles. * @return string - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function quote ( $string = null, $type = -1 ); + public function quote($string = null, $type = -1); /** * Execute an SQL statement, returning a result set as a * \Hoa\Database\IDal\WrapperStatement object. * - * @access public * @param string $statement The SQL statement to prepare and execute. * @return \Hoa\Database\IDal\WrapperStatement - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function query ( $statement ); + public function query($statement); /** * Fetch the SQLSTATE associated with the last operation on the database * handle. * - * @access public * @return string - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function errorCode ( ); + public function errorCode(); /** * Fetch extends error information associated with the last operation on the * database handle. * - * @access public * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function errorInfo ( ); + public function errorInfo(); /** * Return an array of available drivers. * - * @access public * @return array - * @throw \Hoa\Datatase\Exception + * @throws \Hoa\Datatase\Exception */ - public function getAvailableDrivers ( ); + public function getAvailableDrivers(); /** * Set attributes. * - * @access public * @param array $attributes Attributes values. * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function setAttributes ( Array $attributes ); + public function setAttributes(Array $attributes); /** * Set a specific attribute. * - * @access public * @param mixed $attribute Attribute name. * @param mixed $value Attribute value. * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function setAttribute ( $attribute, $value ); + public function setAttribute($attribute, $value); /** * Retrieve all database connection attributes. * - * @access public * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function getAttributes ( ); + public function getAttributes(); /** * Retrieve a database connection attribute. * - * @access public * @param string $attribute Attribute name. * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function getAttribute ( $attribute ); + public function getAttribute($attribute); } diff --git a/IDal/WrapperStatement.php b/IDal/WrapperStatement.php index ee7d2eb..cbf99c4 100644 --- a/IDal/WrapperStatement.php +++ b/IDal/WrapperStatement.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -41,121 +41,111 @@ * * Interface of a DAL statement wrapper. * - * @author Ivan Enderlin - * @author Raphaël Emourgeon - * @copyright Copyright © 2007-2015 Ivan Enderlin, Raphaël Emourgeon. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -interface WrapperStatement { - +interface WrapperStatement +{ /** * Execute a prepared statement. * - * @access public * @param array $bindParameters Bind parameters values if * bindParameter() is not called. * @return \Hoa\Database\IDal\WrapperStatement - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function execute ( Array $bindParameters = [] ); + public function execute(Array $bindParameters = []); /** * Bind a parameter to te specified variable name. * - * @access public * @param mixed $parameter Parameter name. * @param mixed $value Parameter value. * @param int $type Type of value. * @param int $length Length of data type. * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function bindParameter ( $parameter, &$value, $type = null, - $length = null); + public function bindParameter( + $parameter, + &$value, + $type = null, + $length = null + ); /** * Return an array containing all of the result set rows. * - * @access public * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchAll ( ); + public function fetchAll(); /** * Fetch the first row in the result set. * - * @access public * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchFirst ( ); + public function fetchFirst(); /** * Fetch the last row in the result set. * - * @access public * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchLast ( ); + public function fetchLast(); /** * Fetch the next row in the result set. * - * @access public * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchNext ( ); + public function fetchNext(); /** * Fetch the previous row in the result set. * - * @access public * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchPrior ( ); + public function fetchPrior(); /** * Return a single column from the next row of the result set or false if * there is no more row. * - * @access public * @param int $column Column index. * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchColumn ( $column = 0 ); + public function fetchColumn($column = 0); /** * Close the cursor, enabling the statement to be executed again. * - * @access public * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function closeCursor ( ); + public function closeCursor(); /** * Fetch the SQLSTATE associated with the last operation on the statement * handle. * - * @access public * @return string - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function errorCode ( ); + public function errorCode(); /** * Fetch extends error information associated with the last operation on the * statement handle. * - * @access public * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function errorInfo ( ); + public function errorInfo(); } diff --git a/Layer/Pdo/Pdo.php b/Layer/Pdo/Pdo.php index dbf071c..4cc2c8b 100644 --- a/Layer/Pdo/Pdo.php +++ b/Layer/Pdo/Pdo.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -44,17 +44,15 @@ * * Wrap PDO. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Pdo implements Database\IDal\Wrapper { - +class Pdo implements Database\IDal\Wrapper +{ /** * Connection to database. * - * @var \PDO object + * @var \PDO */ protected $_connection = null; @@ -63,31 +61,37 @@ class Pdo implements Database\IDal\Wrapper { /** * Create a DAL instance, representing a connection to a database. * - * @access public * @param string $dsn The DSN of database. * @param string $username The username to connect to database. * @param string $password The password to connect to database. * @param array $driverOptions The driver options. * @return void - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function __construct ( $dsn, $username, $password, - Array $driverOptions = [] ) { - - if(false === extension_loaded('pdo')) + public function __construct( + $dsn, + $username, + $password, + Array $driverOptions = [] + ) { + if (false === extension_loaded('pdo')) { throw new Database\Exception( - 'The module PDO is not enabled.', 0); + 'The module PDO is not enabled.', + 0 + ); + } $connection = null; try { - $connection = new \PDO($dsn, $username, $password, $driverOptions); - } - catch ( \PDOException $e ) { - + } catch (\PDOException $e) { throw new Database\Exception( - $e->getMessage(), $e->getCode(), null, $e); + $e->getMessage(), + $e->getCode(), + null, + $e + ); } $this->setConnection($connection); @@ -98,12 +102,11 @@ public function __construct ( $dsn, $username, $password, /** * Set the connection. * - * @access protected * @param \PDO $connection The PDO instance. * @return \PDO */ - protected function setConnection ( \PDO $connection ) { - + protected function setConnection(\PDO $connection) + { $old = $this->_connection; $this->_connection = $connection; @@ -113,15 +116,17 @@ protected function setConnection ( \PDO $connection ) { /** * Get the connection instance. * - * @access protected * @return PDO - * @throw \Hoa\Database\Dal\Exception + * @throws \Hoa\Database\Dal\Exception */ - protected function getConnection ( ) { - - if(null === $this->_connection) + protected function getConnection() + { + if (null === $this->_connection) { throw new Database\Exception( - 'Cannot return a null connection.', 1); + 'Cannot return a null connection.', + 1 + ); + } return $this->_connection; } @@ -129,52 +134,49 @@ protected function getConnection ( ) { /** * Initiate a transaction. * - * @access public * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function beginTransaction ( ) { - + public function beginTransaction() + { return $this->getConnection()->beginTransaction(); } /** * Commit a transaction. * - * @access public * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function commit ( ) { - + public function commit() + { return $this->getConnection()->commit(); } /** * Roll back a transaction. * - * @access public * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function rollBack ( ) { - + public function rollBack() + { return $this->getConnection()->rollBack(); } /** * Return the ID of the last inserted row or sequence value. * - * @access public * @param string $name Name of sequence object (needed for some * driver). * @return string - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function lastInsertId ( $name = null ) { - - if(null === $name) + public function lastInsertId($name = null) + { + if (null === $name) { return $this->getConnection()->lastInsertId(); + } return $this->getConnection()->lastInsertId($name); } @@ -182,21 +184,24 @@ public function lastInsertId ( $name = null ) { /** * Prepare a statement for execution and returns a statement object. * - * @access public * @param string $statement This must be a valid SQL statement for the * target database server. * @param array $options Options to set attributes values for the * Layer Statement. * @return \Hoa\Database\Layer\Pdo\Statement - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function prepare ( $statement, Array $options = [] ) { - + public function prepare($statement, Array $options = []) + { $handle = $this->getConnection()->prepare($statement); - if(!($handle instanceof \PDOStatement)) + if (!($handle instanceof \PDOStatement)) { throw new Database\Exception( - '%3$s (%1$s/%2$d).', 2, $this->errorInfo()); + '%3$s (%1$s/%2$d).', + 2, + $this->errorInfo() + ); + } return new Statement($handle); } @@ -204,17 +209,17 @@ public function prepare ( $statement, Array $options = [] ) { /** * Quote a sting for use in a query. * - * @access public * @param string $string The string to be quoted. * @param int $type Provide a data type hint for drivers that * have alternate quoting styles. * @return string - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function quote ( $string = null, $type = -1 ) { - - if($type < 0) + public function quote($string = null, $type = -1) + { + if ($type < 0) { return $this->getConnection()->quote($string); + } return $this->getConnection()->quote($string, $type); } @@ -223,18 +228,21 @@ public function quote ( $string = null, $type = -1 ) { * Execute an SQL statement, returning a result set as a * \Hoa\Database\Layer\Pdo\Statement object. * - * @access public * @param string $statement The SQL statement to prepare and execute. * @return \Hoa\Database\Layer\Pdo\Statement - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function query ( $statement ) { - + public function query($statement) + { $handle = $this->getConnection()->query($statement); - if(!($handle instanceof \PDOStatement)) + if (!($handle instanceof \PDOStatement)) { throw new Database\Exception( - '%3$s (%1$s/%2$d).', 3, $this->errorInfo()); + '%3$s (%1$s/%2$d).', + 3, + $this->errorInfo() + ); + } return new Statement($handle); } @@ -243,12 +251,11 @@ public function query ( $statement ) { * Fetch the SQLSTATE associated with the last operation on the database * handle. * - * @access public * @return string - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function errorCode ( ) { - + public function errorCode() + { return $this->getConnection()->errorCode(); } @@ -256,41 +263,39 @@ public function errorCode ( ) { * Fetch extends error information associated with the last operation on the * database handle. * - * @access public * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function errorInfo ( ) { - + public function errorInfo() + { return $this->getConnection()->errorInfo(); } /** * Return an array of available drivers. * - * @access public * @return array - * @throw \Hoa\Datatase\Exception + * @throws \Hoa\Datatase\Exception */ - public function getAvailableDrivers ( ) { - + public function getAvailableDrivers() + { return $this->getConnection()->getAvailableDrivers(); } /** * Set attributes. * - * @access public * @param array $attributes Attributes values. * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function setAttributes ( Array $attributes ) { - + public function setAttributes(Array $attributes) + { $out = true; - foreach($attributes as $attribute => $value) + foreach ($attributes as $attribute => $value) { $out &= $this->setAttribute($attribute, $value); + } return (bool) $out; } @@ -298,26 +303,24 @@ public function setAttributes ( Array $attributes ) { /** * Set a specific attribute. * - * @access public * @param mixed $attribute Attribute name. * @param mixed $value Attribute value. * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function setAttribute ( $attribute, $value ) { - + public function setAttribute($attribute, $value) + { return $this->getConnection()->setAttribute($attribute, $value); } /** * Retrieve all database connection attributes. * - * @access public * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function getAttributes ( ) { - + public function getAttributes() + { $out = []; $attributes = [ 0 => 'AUTOCOMMIT', @@ -334,8 +337,9 @@ public function getAttributes ( ) { 11 => 'TIMEOUT' ]; - foreach($attributes as $attribute) + foreach ($attributes as $attribute) { $out[$attribute] = $this->getAttribute($attribute); + } return $out; } @@ -343,15 +347,16 @@ public function getAttributes ( ) { /** * Retrieve a database connection attribute. * - * @access public * @param string $attribute Attribute name. * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function getAttribute ( $attribute ) { - - return $this->getConnection() - ->getAttribute(constant('\PDO::ATTR_' . $attribute )); + public function getAttribute($attribute) + { + return + $this + ->getConnection() + ->getAttribute(constant('\PDO::ATTR_' . $attribute)); } } diff --git a/Layer/Pdo/Statement.php b/Layer/Pdo/Statement.php index 2df6ef1..46be2f0 100644 --- a/Layer/Pdo/Statement.php +++ b/Layer/Pdo/Statement.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -43,18 +43,15 @@ * * Wrap PDOStatement. * - * @author Ivan Enderlin - * @author Raphaël Emourgeon - * @copyright Copyright © 2007-2015 Ivan Enderlin, Raphaël Emourgeon. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Statement implements Database\IDal\WrapperStatement { - +class Statement implements Database\IDal\WrapperStatement +{ /** * The statement instance. * - * @var \PDOStatement object + * @var \PDOStatement */ protected $_statement = null; @@ -63,12 +60,11 @@ class Statement implements Database\IDal\WrapperStatement { /** * Create a statement instance. * - * @access public * @param \PDOStatement $statement The PDOStatement instance. * @return void */ - public function __construct ( \PDOStatement $statement ) { - + public function __construct(\PDOStatement $statement) + { $this->setStatement($statement); return; @@ -77,12 +73,11 @@ public function __construct ( \PDOStatement $statement ) { /** * Set the statement instance. * - * @access protected * @param \PDOStatement $statement The PDOStatement instance. * @return \PDOStatement */ - protected function setStatement ( \PDOStatement $statement ) { - + protected function setStatement(\PDOStatement $statement) + { $old = $this->_statement; $this->_statement = $statement; @@ -92,28 +87,30 @@ protected function setStatement ( \PDOStatement $statement ) { /** * Get the statement instance. * - * @access protected * @return \PDOStatement */ - protected function getStatement ( ) { - + protected function getStatement() + { return $this->_statement; } /** * Execute a prepared statement. * - * @access public * @param array $bindParameters Bind parameters values if bindParam * is not called. * @return \Hoa\Database\Pdo\Statement - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function execute ( Array $bindParameters = null ) { - - if(false === $this->getStatement()->execute($bindParameters)) + public function execute(Array $bindParameters = null) + { + if (false === $this->getStatement()->execute($bindParameters)) { throw new Database\Exception( - '%3$s (%1$s/%2$d)', 0, $this->errorInfo()); + '%3$s (%1$s/%2$d)', + 0, + $this->errorInfo() + ); + } return $this; } @@ -121,22 +118,26 @@ public function execute ( Array $bindParameters = null ) { /** * Bind a parameter to te specified variable name. * - * @access public * @param mixed $parameter Parameter name. * @param mixed $value Parameter value. * @param int $type Type of value. * @param int $length Length of data type. * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function bindParameter ( $parameter, &$value, $type = null, - $length = null ) { - - if(null === $type) + public function bindParameter( + $parameter, + &$value, + $type = null, + $length = null + ) { + if (null === $type) { return $this->getStatement()->bindParam($parameter, $value); + } - if(null === $length) + if (null === $length) { return $this->getStatement()->bindParam($parameter, $value, $type); + } return $this->getStatement()->bindParam($parameter, $value, $type, $length); } @@ -144,26 +145,24 @@ public function bindParameter ( $parameter, &$value, $type = null, /** * Return an array containing all of the result set rows. * - * @access public * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchAll ( ) { - + public function fetchAll() + { return $this->getStatement()->fetchAll(\PDO::FETCH_ASSOC); } /** * Fetch the next row in the result set. * - * @access protected * @param int $orientation Must be one of the \PDO::FETCH_ORI_* * constants. * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - protected function fetch ( $orientation = \PDO::FETCH_ORI_NEXT ) { - + protected function fetch($orientation = \PDO::FETCH_ORI_NEXT) + { return $this->getStatement()->fetch( \PDO::FETCH_ASSOC, $orientation @@ -173,48 +172,44 @@ protected function fetch ( $orientation = \PDO::FETCH_ORI_NEXT ) { /** * Fetch the first row in the result set. * - * @access public * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchFirst ( ) { - + public function fetchFirst() + { return $this->fetch(\PDO::FETCH_ORI_FIRST); } /** * Fetch the last row in the result set. * - * @access public * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchLast ( ) { - + public function fetchLast() + { return $this->fetch(\PDO::FETCH_ORI_LAST); } /** * Fetch the next row in the result set. * - * @access public * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchNext ( ) { - + public function fetchNext() + { return $this->fetch(\PDO::FETCH_ORI_NEXT); } /** * Fetch the previous row in the result set. * - * @access public * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchPrior ( ) { - + public function fetchPrior() + { return $this->fetch(\PDO::FETCH_ORI_PRIOR); } @@ -222,25 +217,23 @@ public function fetchPrior ( ) { * Return a single column from the next row of the result set or false if * there is no more row. * - * @access public * @param int $column Column index. * @return mixed - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function fetchColumn ( $column = 0 ) { - + public function fetchColumn($column = 0) + { return $this->getStatement()->fetchColumn($column); } /** * Close the cursor, enabling the statement to be executed again. * - * @access public * @return bool - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function closeCursor ( ) { - + public function closeCursor() + { return $this->getStatement()->closeCursor(); } @@ -248,12 +241,11 @@ public function closeCursor ( ) { * Fetch the SQLSTATE associated with the last operation on the statement * handle. * - * @access public * @return string - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function errorCode ( ) { - + public function errorCode() + { return $this->getStatement()->errorCode(); } @@ -261,12 +253,11 @@ public function errorCode ( ) { * Fetch extends error information associated with the last operation on the * statement handle. * - * @access public * @return array - * @throw \Hoa\Database\Exception + * @throws \Hoa\Database\Exception */ - public function errorInfo ( ) { - + public function errorInfo() + { return $this->getStatement()->errorInfo(); } } diff --git a/Query/Delete.php b/Query/Delete.php index 9f63450..4301765 100644 --- a/Query/Delete.php +++ b/Query/Delete.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -41,17 +41,15 @@ * * Build a DELETE query. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Delete extends Where implements Dml { - +class Delete extends Where implements Dml +{ /** * Table name. * - * @var \Hoa\Database\Query\Delete string + * @var string */ protected $_from = null; @@ -60,12 +58,11 @@ class Delete extends Where implements Dml { /** * Set the table name. * - * @access public * @param string $source Table name. * @return \Hoa\Database\Query\Delete */ - public function from ( $source ) { - + public function from($source) + { $this->_from = $source; return $this; @@ -74,11 +71,10 @@ public function from ( $source ) { /** * Generate the query. * - * @access public * @return string */ - public function __toString ( ) { - + public function __toString() + { return 'DELETE FROM ' . $this->_from . parent::__toString(); } } diff --git a/Query/Dml.php b/Query/Dml.php index 469b3ac..c71f280 100644 --- a/Query/Dml.php +++ b/Query/Dml.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -41,18 +41,15 @@ * * Represent Data Manipulation Language queries. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -interface Dml { - +interface Dml +{ /** * Generate the query. * - * @access public * @return string */ - public function __toString ( ); + public function __toString(); } diff --git a/Query/Insert.php b/Query/Insert.php index b0dd037..7149d5f 100644 --- a/Query/Insert.php +++ b/Query/Insert.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -41,45 +41,43 @@ * * Build an INSERT query. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Insert implements Dml { - +class Insert implements Dml +{ /** * Source. * - * @var \Hoa\Database\Query\Insert string + * @var string */ protected $_into = null; /** * Alternative to INSERT. * - * @var \Hoa\Database\Query\Insert string + * @var string */ protected $_or = null; /** * Columns. * - * @var \Hoa\Database\Query\Insert array + * @var array */ protected $_columns = []; /** * Values (tuples). * - * @var \Hoa\Database\Query\Insert array + * @var array */ protected $_values = []; /** * Whether we should use default values or not. * - * @var \Hoa\Database\Query\Insert bool + * @var bool */ protected $_defaultValues = false; @@ -88,12 +86,11 @@ class Insert implements Dml { /** * Set source. * - * @access public * @param string $name Name. * @return \Hoa\Database\Query\Insert */ - public function into ( $name ) { - + public function into($name) + { $this->_into = $name; return $this; @@ -102,67 +99,61 @@ public function into ( $name ) { /** * Insert or rollback. * - * @access public * @return \Hoa\Database\Query\Insert */ - public function rollback ( ) { - + public function rollback() + { return $this->_or('ROLLBACK'); } /** * Insert or abort. * - * @access public * @return \Hoa\Database\Query\Insert */ - public function abort ( ) { - + public function abort() + { return $this->_or('ABORT'); } /** * Insert or replace. * - * @access public * @return \Hoa\Database\Query\Insert */ - public function replace ( ) { - + public function replace() + { return $this->_or('REPLACE'); } /** * Insert or fail. * - * @access public * @return \Hoa\Database\Query\Insert */ - public function fail ( ) { - + public function fail() + { return $this->_or('FAIL'); } /** * Insert or ignore. * - * @access public * @return \Hoa\Database\Query\Insert */ - public function ignore ( ) { - + public function ignore() + { return $this->_or('IGNORE'); } /** * Declare an alternative to “INSERT”. * - * @access protected * @param string $or Alternative. * @return \Hoa\Database\Query\Insert */ - protected function _or ( $or ) { - + protected function _or($or) + { $this->_or = $or; return $this; @@ -171,15 +162,15 @@ protected function _or ( $or ) { /** * Set columns. * - * @access public * @param string $column Column name. * @param ... ... * @return \Hoa\Database\Query\Insert */ - public function on ( $column ) { - - foreach(func_get_args() as $column) + public function on($column) + { + foreach (func_get_args() as $column) { $this->_columns[] = $column; + } return $this; } @@ -188,25 +179,25 @@ public function on ( $column ) { * Set values (on call per tuple). * Expression can be: a regular value or a SELECT query. * - * @access public * @param mixed $expression Expression. * @param ... ... * @return \Hoa\Database\Query\Insert */ - public function values ( $expression ) { - - if($expression instanceof Select) + public function values($expression) + { + if ($expression instanceof Select) { $this->_values = (string) $expression; - else { - - if(is_string($this->_values)) + } else { + if (is_string($this->_values)) { $this->_values = []; + } $values = &$this->_values[]; $values = []; - foreach(func_get_args() as $expression) + foreach (func_get_args() as $expression) { $values[] = $expression; + } } return $this; @@ -215,11 +206,10 @@ public function values ( $expression ) { /** * Use default values. * - * @access public * @return \Hoa\Database\Query\Insert */ - public function defaultValues ( ) { - + public function defaultValues() + { $this->_defaultValues = true; return $this; @@ -228,17 +218,14 @@ public function defaultValues ( ) { /** * Allow to use the “or” attribute to chain method calls. * - * @access public * @param string $name Name. * @return mixed */ - public function __get ( $name ) { - - switch(strtolower($name)) { - + public function __get($name) + { + switch (strtolower($name)) { case 'or': return $this; - break; default: return $this->$name; @@ -248,31 +235,35 @@ public function __get ( $name ) { /** * Generate the query. * - * @access public * @return string */ - public function __toString ( ) { - + public function __toString() + { $out = 'INSERT'; - if(null !== $this->_or) + if (null !== $this->_or) { $out .= ' OR ' . $this->_or; + } $out .= ' INTO ' . $this->_into; - if(true === $this->_defaultValues) + if (true === $this->_defaultValues) { return $out . ' DEFAULT VALUES'; + } - if(!empty($this->_columns)) + if (!empty($this->_columns)) { $out .= ' (' . implode(', ', $this->_columns) . ')'; + } - if(is_string($this->_values)) + if (is_string($this->_values)) { return $out . ' ' . $this->_values; + } $tuples = []; - foreach($this->_values as $tuple) + foreach ($this->_values as $tuple) { $tuples[] = '(' . implode(', ', $tuple) . ')'; + } return $out . ' VALUES ' . implode(', ', $tuples); } diff --git a/Query/Join.php b/Query/Join.php index 604c19b..ad0f961 100644 --- a/Query/Join.php +++ b/Query/Join.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -41,24 +41,22 @@ * * Build a JOIN clause. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Join { - +class Join +{ /** * Parent query. * - * @var \Hoa\Database\Query\Select object + * @var \Hoa\Database\Query\Select */ protected $_parent = null; /** * Reference the FROM entry of the parent (simulate “friends”). * - * @var \Hoa\Database\Query\Select string + * @var string */ protected $_from = null; @@ -67,13 +65,12 @@ class Join { /** * Constructor. * - * @access public * @param \Hoa\Database\Query\Select $parent Parent query. * @param string $from FROM entry (“friends”). * @return void */ - public function __construct ( Select $parent, Array &$from ) { - + public function __construct(Select $parent, Array &$from) + { $this->_parent = $parent; $this->_from = &$from; end($this->_from); @@ -84,14 +81,14 @@ public function __construct ( Select $parent, Array &$from ) { /** * Declare the JOIN constraint ON. * - * @access public * @param string $expression Expression. * @return \Hoa\Database\Query\Select */ - public function on ( $expression ) { - - $this->_from[key($this->_from)] = current($this->_from) . - ' ON ' . $expression; + public function on($expression) + { + $this->_from[key($this->_from)] = + current($this->_from) . + ' ON ' . $expression; return $this->_parent; } @@ -99,16 +96,16 @@ public function on ( $expression ) { /** * Declare the JOIN constraint USING. * - * @access public * @param string $expression Expression. * @param ... ... * @return \Hoa\Database\Query\Select */ - public function using ( $expression ) { - - $this->_from[key($this->_from)] = current($this->_from) . - ' USING (' . - implode(', ', func_get_args()) . ')'; + public function using($expression) + { + $this->_from[key($this->_from)] = + current($this->_from) . + ' USING (' . + implode(', ', func_get_args()) . ')'; return $this->_parent; } diff --git a/Query/Query.php b/Query/Query.php index 1c8c44b..c267e4b 100644 --- a/Query/Query.php +++ b/Query/Query.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -43,24 +43,22 @@ * * Multiton of queries. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Query { - +class Query +{ /** * Multiton of queries. * - * @var \Hoa\Database\Query array + * @var array */ protected static $_queries = []; /** * Current instance ID. * - * @var \Hoa\Database\Query string + * @var string */ protected $_id = null; @@ -69,12 +67,11 @@ class Query { /** * Set current instance ID. * - * @access public * @param string $id ID. * @return \Hoa\Database\Query */ - public function setId ( $id ) { - + public function setId($id) + { $this->_id = $id; return $this; @@ -83,69 +80,63 @@ public function setId ( $id ) { /** * Get current instance ID. * - * @access public * @return string */ - public function getId ( ) { - + public function getId() + { return $this->_id; } /** * Start a START query. * - * @access public * @param string $column Column. * @param ... ... * @return \Hoa\Database\Query\Select */ - public function select ( $column = null ) { - + public function select($column = null) + { return $this->store(new Select(func_get_args())); } /** * Start an INSERT query. * - * @access public * @return \Hoa\Database\Query\Insert */ - public function insert ( ) { - + public function insert() + { return $this->store(new Insert()); } /** * Start an UPDATE query. * - * @access public * @return \Hoa\Database\Query\Update */ - public function update ( ) { - + public function update() + { return $this->store(new Update()); } /** * Start a DELETE query. * - * @access public * @return \Hoa\Database\Query\Delete */ - public function delete ( ) { - + public function delete() + { return $this->store(new Delete()); } /** * Start a WHERE clause. * - * @access public * @param string $expression Expression. * @return \Hoa\Database\Query\Where */ - public function where ( $expression ) { - + public function where($expression) + { $where = new Where(); return $this->store($where->where($expression)); @@ -154,16 +145,16 @@ public function where ( $expression ) { /** * Store the current instance if necessary. * - * @access protected * @param \Hoa\Database\Query\Dml $object Object. * @return \Hoa\Database\Query\Dml */ - protected function store ( $object ) { - - if(null === $id = $this->getId()) + protected function store($object) + { + if (null === $id = $this->getId()) { $out = $object; - else + } else { $out = static::$_queries[$id] = $object; + } $this->_id = null; @@ -173,14 +164,14 @@ protected function store ( $object ) { /** * Get a query (a clone of it). * - * @access public * @param string $id ID. * @return \Hoa\Database\Query\Dml */ - public static function get ( $id ) { - - if(null === $out = static::getReference($id)) + public static function get($id) + { + if (null === $out = static::getReference($id)) { return null; + } return clone $out; } @@ -188,14 +179,14 @@ public static function get ( $id ) { /** * Get a query (not a clone of it). * - * @access public * @param string $id ID. * @return \Hoa\Database\Query\Dml */ - public static function getReference ( $id ) { - - if(false === array_key_exists($id, static::$_queries)) + public static function getReference($id) + { + if (false === array_key_exists($id, static::$_queries)) { return null; + } return static::$_queries[$id]; } diff --git a/Query/Select.php b/Query/Select.php index cf07ec9..80bb0bf 100644 --- a/Query/Select.php +++ b/Query/Select.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -41,38 +41,36 @@ * * Build a SELECT query. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Select extends SelectCore implements Dml { - +class Select extends SelectCore implements Dml +{ /** * “Core” selects (whether we have union, unionAll, intersect or except). * - * @var \Hoa\Database\Query\Select array + * @var array */ protected $_select = []; /** * Ordering terms. * - * @var \Hoa\Database\Query\Select array + * @var array */ protected $_orderBy = []; /** * Limit expressions. * - * @var \Hoa\Database\Query\Select array + * @var array */ protected $_limit = []; /** * Offset expression. * - * @var \Hoa\Database\Query\Select string + * @var string */ protected $_offset = null; @@ -81,56 +79,51 @@ class Select extends SelectCore implements Dml { /** * Start a new SELECT query which is an union of the previous one. * - * @access public * @return \Hoa\Database\Query\Select */ - public function union ( ) { - + public function union() + { return $this->compose('UNION'); } /** * Start a new SELECT query which is an unionAll of the previous one. * - * @access public * @return \Hoa\Database\Query\Select */ - public function unionAll ( ) { - + public function unionAll() + { return $this->compose('UNION ALL'); } /** * Start a new SELECT query which is an intersection of the previous one. * - * @access public * @return \Hoa\Database\Query\Select */ - public function intersect ( ) { - + public function intersect() + { return $this->compose('INTERSECT'); } /** * Start a new SELECT query which is an exception of the previous one. * - * @access public * @return \Hoa\Database\Query\Select */ - public function except ( ) { - + public function except() + { return $this->compose('EXCEPT'); } /** * Compose SELECT queries. * - * @access protected * @param string $operator Composition operator. * @return \Hoa\Database\Query\Select */ - protected function compose ( $operator ) { - + protected function compose($operator) + { $this->_select[] = parent::__toString() . ' ' . $operator; $this->reset(); @@ -140,15 +133,15 @@ protected function compose ( $operator ) { /** * Add ordering terms. * - * @access public * @param string $term Term. * @param ... ... * @return \Hoa\Database\Query\Select */ - public function orderBy ( $term ) { - - foreach(func_get_args() as $term) + public function orderBy($term) + { + foreach (func_get_args() as $term) { $this->_orderBy[] = $term; + } return $this; } @@ -156,15 +149,15 @@ public function orderBy ( $term ) { /** * Add limit expressions. * - * @access public * @param string $expression Expression. * @param ... ... * @return \Hoa\Database\Query\Select */ - public function limit ( $expression ) { - - foreach(func_get_args() as $expression) + public function limit($expression) + { + foreach (func_get_args() as $expression) { $this->_limit[] = $expression; + } return $this; } @@ -172,12 +165,11 @@ public function limit ( $expression ) { /** * Add offset expression. * - * @access public * @param string $expression Expression. * @return \Hoa\Database\Query\Select */ - public function offset ( $expression ) { - + public function offset($expression) + { $this->_offset = $expression; return $this; @@ -186,31 +178,32 @@ public function offset ( $expression ) { /** * Generate the query. * - * @access public * @return string */ - public function __toString ( ) { - + public function __toString() + { $out = null; $select = implode(' ', $this->_select); - if(!empty($select)) + if (!empty($select)) { $out .= $select . ' '; + } $out .= parent::__toString(); - if(!empty($this->_orderBy)) + if (!empty($this->_orderBy)) { $out .= ' ORDER BY ' . implode(', ', $this->_orderBy); + } - if(!empty($this->_limit)) { - + if (!empty($this->_limit)) { $out .= ' LIMIT'; - if(null !== $this->_offset) + if (null !== $this->_offset) { $out .= ' ' . $this->_limit[0] . ' OFFSET ' . $this->_offset; - else + } else { $out .= ' ' . implode(', ', $this->_limit); + } } return $out; diff --git a/Query/SelectCore.php b/Query/SelectCore.php index f0f451d..737edf4 100644 --- a/Query/SelectCore.php +++ b/Query/SelectCore.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -41,45 +41,43 @@ * * Core of the SELECT query. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -abstract class SelectCore extends Where { - +abstract class SelectCore extends Where +{ /** * Columns. * - * @var \Hoa\Database\Query\SelectCore array + * @var array */ protected $_columns = null; /** * SELECT DISTINCT or SELECT ALL. * - * @var \Hoa\Database\Query\SelectCore string + * @var string */ protected $_distinctOrAll = null; /** * Sources. * - * @var \Hoa\Database\Query\SelectCore array + * @var array */ protected $_from = []; /** * Group by expressions. * - * @var \Hoa\Database\Query\SelectCore array + * @var array */ protected $_groupBy = []; /** * Having expression. * - * @var \Hoa\Database\Query\SelectCore string + * @var string */ protected $_having = null; @@ -88,12 +86,11 @@ abstract class SelectCore extends Where { /** * Set columns. * - * @access public * @param array $columns Columns. * @return void */ - public function __construct ( Array $columns = [] ) { - + public function __construct(Array $columns = []) + { $this->_columns = $columns; return; @@ -102,11 +99,10 @@ public function __construct ( Array $columns = [] ) { /** * Make a SELECT DISTINCT. * - * @access public * @return \Hoa\Database\Query\SelectCore */ - public function distinct ( ) { - + public function distinct() + { $this->_distinctOrAll = 'DISTINCT'; return $this; @@ -115,11 +111,10 @@ public function distinct ( ) { /** * Make a SELECT ALL. * - * @access public * @return \Hoa\Database\Query\SelectCore */ - public function all ( ) { - + public function all() + { $this->_distinctOrAll = 'ALL'; return $this; @@ -128,15 +123,15 @@ public function all ( ) { /** * Select a column. * - * @access public * @param string $column Column. * @param ... ... * @return \Hoa\Database\Query\SelectCore */ - public function select ( $column ) { - - foreach(func_get_args() as $column) + public function select($column) + { + foreach (func_get_args() as $column) { $this->_columns[] = $column; + } return $this; } @@ -144,15 +139,15 @@ public function select ( $column ) { /** * Group by expression. * - * @access public * @param string $expression Expression. * @param ... ... * @return \Hoa\Database\Query\SelectCore */ - public function groupBy ( $expression ) { - - foreach(func_get_args() as $expression) + public function groupBy($expression) + { + foreach (func_get_args() as $expression) { $this->_groupBy[] = $expression; + } return $this; } @@ -160,12 +155,11 @@ public function groupBy ( $expression ) { /** * Having expression. * - * @access public * @param string $expression Expression. * @return \Hoa\Database\Query\SelectCore */ - public function having ( $expression ) { - + public function having($expression) + { $this->_having = $expression; return $this; @@ -174,17 +168,16 @@ public function having ( $expression ) { /** * Set source (regular or a SELECT query). * - * @access public * @param mixed $source Source. * @param ... ... * @return \Hoa\Database\Query\SelectCore */ - public function from ( $source ) { - - foreach(func_get_args() as $source) { - - if($source instanceof self) + public function from($source) + { + foreach (func_get_args() as $source) { + if ($source instanceof self) { $source = '(' . $source . ')'; + } $this->_from[] = $source; } @@ -195,14 +188,14 @@ public function from ( $source ) { /** * Alias the last declared source. * - * @access public * @param string $alias Alias. * @return \Hoa\Database\Query\SelectCore */ - public function _as ( $alias ) { - - if(empty($this->_from)) + public function _as($alias) + { + if (empty($this->_from)) { return $this; + } $this->_from[$alias] = array_pop($this->_from); @@ -212,138 +205,129 @@ public function _as ( $alias ) { /** * Join a source (regular of a SELECT query). * - * @access public * @param mixed $source Source. * @return \Hoa\Database\Query\Join */ - public function join ( $source ) { - + public function join($source) + { return $this->_join('JOIN', $source); } /** * Natural join a source (regular of a SELECT query). * - * @access public * @param mixed $source Source. * @return \Hoa\Database\Query\Join */ - public function naturalJoin ( $source ) { - + public function naturalJoin($source) + { return $this->_join('NATURAL JOIN', $source); } /** * Left join a source (regular of a SELECT query). * - * @access public * @param mixed $source Source. * @return \Hoa\Database\Query\Join */ - public function leftJoin ( $source ) { - + public function leftJoin($source) + { return $this->_join('LEFT JOIN', $source); } /** * Natural left join a source (regular of a SELECT query). * - * @access public * @param mixed $source Source. * @return \Hoa\Database\Query\Join */ - public function naturalLeftJoin ( $source ) { - + public function naturalLeftJoin($source) + { return $this->_join('NATURAL LEFT JOIN', $source); } /** * Left outer join a source (regular of a SELECT query). * - * @access public * @param mixed $source Source. * @return \Hoa\Database\Query\Join */ - public function leftOuterJoin ( $source ) { - + public function leftOuterJoin($source) + { return $this->_join('LEFT OUTER JOIN', $source); } /** * Natural left outer join a source (regular of a SELECT query). * - * @access public * @param mixed $source Source. * @return \Hoa\Database\Query\Join */ - public function naturalLeftOuterJoin ( $source ) { - + public function naturalLeftOuterJoin($source) + { return $this->_join('NATURAL LEFT OUTER JOIN', $source); } /** * Inner join a source (regular of a SELECT query). * - * @access public * @param mixed $source Source. * @return \Hoa\Database\Query\Join */ - public function innerJoin ( $source ) { - + public function innerJoin($source) + { return $this->_join('INNER JOIN', $source); } /** * Natural inner join a source (regular of a SELECT query). * - * @access public * @param mixed $source Source. * @return \Hoa\Database\Query\Join */ - public function naturalInnerJoin ( $source ) { - + public function naturalInnerJoin($source) + { return $this->_join('NATURAL INNER JOIN', $source); } /** * Cross join a source (regular of a SELECT query). * - * @access public * @param mixed $source Source. * @return \Hoa\Database\Query\Join */ - public function crossJoin ( $source ) { - + public function crossJoin($source) + { return $this->_join('CROSS JOIN', $source); } /** * Natural cross join a source (regular of a SELECT query). * - * @access public * @param mixed $source Source. * @return \Hoa\Database\Query\Join */ - public function naturalCrossJoin ( $source ) { - + public function naturalCrossJoin($source) + { return $this->_join('NATURAL CROSS JOIN', $source); } /** * Make a join. * - * @access protected * @param string $type Type. * @param mixed $source Source. * @return \Hoa\Database\Query\Join */ - protected function _join ( $type, $source ) { - - if(empty($this->_from)) + protected function _join($type, $source) + { + if (empty($this->_from)) { return $this; + } - if($source instanceof self) + if ($source instanceof self) { $source = '(' . $source . ')'; + } end($this->_from); $key = key($this->_from); @@ -356,11 +340,10 @@ protected function _join ( $type, $source ) { /** * Reset some properties. * - * @access public * @return \Hoa\Database\Query\SelectCore */ - public function reset ( ) { - + public function reset() + { parent::reset(); $this->_columns = []; $this->_distinctOrAll = null; @@ -374,43 +357,45 @@ public function reset ( ) { /** * Generate the query. * - * @access public * @return string */ - public function __toString ( ) { - + public function __toString() + { $out = 'SELECT'; - if(null !== $this->_distinctOrAll) + if (null !== $this->_distinctOrAll) { $out .= ' ' . $this->_distinctOrAll; + } - if(!empty($this->_columns)) + if (!empty($this->_columns)) { $out .= ' ' . implode(', ', $this->_columns); - else + } else { $out .= ' *'; + } - if(!empty($this->_from)) { - + if (!empty($this->_from)) { $out .= ' FROM '; $handle = []; - foreach($this->_from as $alias => $from) - if(is_int($alias)) + foreach ($this->_from as $alias => $from) { + if (is_int($alias)) { $handle[] = $from; - else + } else { $handle[] = $from . ' AS ' . $alias; + } + } $out .= implode(', ', $handle); } $out .= parent::__toString(); - if(!empty($this->_groupBy)) { - + if (!empty($this->_groupBy)) { $out .= ' GROUP BY ' . implode(', ', $this->_groupBy); - if(!empty($this->_having)) + if (!empty($this->_having)) { $out .= ' HAVING ' . $this->_having; + } } return $out; diff --git a/Query/Update.php b/Query/Update.php index 0ff2a06..9b452cd 100644 --- a/Query/Update.php +++ b/Query/Update.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -41,31 +41,29 @@ * * Build an UPDATE query. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Update extends Where implements Dml { - +class Update extends Where implements Dml +{ /** * Table. * - * @var \Hoa\Database\Query\Update string + * @var string */ protected $_table = null; /** * Alternative to UPDATE. * - * @var \Hoa\Database\Query\Update string + * @var string */ protected $_or = null; /** * Pairs to update. * - * @var \Hoa\Database\Query\Update string + * @var string */ protected $_set = []; @@ -74,67 +72,61 @@ class Update extends Where implements Dml { /** * Update or rollback. * - * @access public * @return \Hoa\Database\Query\Update */ - public function rollback ( ) { - + public function rollback() + { return $this->_or('ROLLBACK'); } /** * Update or abort. * - * @access public * @return \Hoa\Database\Query\Update */ - public function abort ( ) { - + public function abort() + { return $this->_or('ABORT'); } /** * Update or replace. * - * @access public * @return \Hoa\Database\Query\Update */ - public function replace ( ) { - + public function replace() + { return $this->_or('REPLACE'); } /** * Update or fail. * - * @access public * @return \Hoa\Database\Query\Update */ - public function fail ( ) { - + public function fail() + { return $this->_or('FAIL'); } /** * Update or ignore. * - * @access public * @return \Hoa\Database\Query\Update */ - public function ignore ( ) { - + public function ignore() + { return $this->_or('IGNORE'); } /** * Declare an alternative to “INSERT”. * - * @access protected * @param string $or Alternative. * @return \Hoa\Database\Query\Update */ - protected function _or ( $or ) { - + protected function _or($or) + { $this->_or = $or; return $this; @@ -143,12 +135,11 @@ protected function _or ( $or ) { /** * Set the table. * - * @access public * @param string $table Table. * @return \Hoa\Database\Query\Update */ - public function table ( $table ) { - + public function table($table) + { $this->_table = $table; return $this; @@ -157,13 +148,12 @@ public function table ( $table ) { /** * Set a pair. * - * @access public * @param string $name Name. * @param mixed $value Value. * @return \Hoa\Database\Query\Update */ - public function set ( $name, $value ) { - + public function set($name, $value) + { $this->_set[$name] = $value; return $this; @@ -172,21 +162,22 @@ public function set ( $name, $value ) { /** * Generate the query. * - * @access public * @return string */ - public function __toString ( ) { - + public function __toString() + { $out = 'UPDATE'; - if(null !== $this->_or) + if (null !== $this->_or) { $out .= ' OR ' . $this->_or; + } $out .= ' ' . $this->_table; $set = []; - foreach($this->_set as $name => $value) + foreach ($this->_set as $name => $value) { $set[] = $name . ' = ' . $value; + } $out .= ' SET ' . implode(', ', $set); diff --git a/Query/Where.php b/Query/Where.php index 35058bf..b6816d1 100644 --- a/Query/Where.php +++ b/Query/Where.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -41,24 +41,22 @@ * * Build a WHERE clause. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Where { - +class Where +{ /** * Expressions. * - * @var \Hoa\Database\Query\Where array + * @var array */ protected $_where = []; /** * Current logic operator. * - * @var \Hoa\Database\Query\Where string + * @var string */ protected $_logicOperator = null; @@ -67,19 +65,20 @@ class Where { /** * Add an expression (regular string or a WHERE clause). * - * @access public * @param mixed $expression Expression. * @return \Hoa\Database\Query\Where */ - public function where ( $expression ) { - + public function where($expression) + { $where = null; - if(!empty($this->_where)) + if (!empty($this->_where)) { $where = ($this->_logicOperator ?: 'AND') . ' '; + } - if($expression instanceof self) + if ($expression instanceof self) { $expression = '(' . substr($expression, 7) . ')'; + } $this->_where[] = $where . $expression; $this->_logicOperator = null; @@ -90,31 +89,29 @@ public function where ( $expression ) { /** * Redirect undefined calls to _calls. * - * @access public * @param string $name Name. * @param array $values Values. * @return \Hoa\Database\Query\Where */ - public function __call ( $name, Array $values ) { - - return call_user_func_array(array($this, '_' . $name), $values); + public function __call($name, Array $values) + { + return call_user_func_array([$this, '_' . $name], $values); } /** * Set the current logic operator. * - * @access public * @param string $name Name. * @return \Hoa\Database\Query\Where */ - public function __get ( $name ) { - - switch(strtolower($name)) { - + public function __get($name) + { + switch (strtolower($name)) { case 'and': case 'or': $this->_logicOperator = strtoupper($name); - break; + + break; default: return $this->$name; @@ -126,11 +123,10 @@ public function __get ( $name ) { /** * Reset. * - * @access public * @return \Hoa\Database\Query\Where */ - public function reset ( ) { - + public function reset() + { $this->_where = []; return $this; @@ -139,13 +135,13 @@ public function reset ( ) { /** * Generate the query. * - * @access public * @return string */ - public function __toString ( ) { - - if(empty($this->_where)) + public function __toString() + { + if (empty($this->_where)) { return null; + } return ' WHERE ' . implode(' ', $this->_where); } diff --git a/Test/Unit/Query/Example.php b/Test/Unit/Query/Example.php index 5630b9e..176c0de 100644 --- a/Test/Unit/Query/Example.php +++ b/Test/Unit/Query/Example.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -36,23 +36,21 @@ namespace Hoa\Database\Test\Unit\Query; -use Hoa\Test; use Hoa\Database\Query as CUT; +use Hoa\Test; /** * Class \Hoa\Database\Test\Unit\Query\Example. * * Test suite of some examples. * - * @author Ivan Enderlin - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Example extends Test\Unit\Suite { - - public function case_basic_one ( ) { - +class Example extends Test\Unit\Suite +{ + public function case_basic_one() + { $this ->given($q = new CUT()) ->when( @@ -63,8 +61,8 @@ public function case_basic_one ( ) { ->isEqualTo('SELECT a FROM foo'); } - public function case_reuse ( ) { - + public function case_reuse() + { $this ->given( $q = new CUT(), @@ -78,8 +76,8 @@ public function case_reuse ( ) { ->isEqualTo('SELECT a FROM foo WHERE i > 3'); } - public function case_bigger_one ( ) { - + public function case_bigger_one() + { $this ->given($q = new CUT()) ->when( @@ -105,8 +103,8 @@ public function case_bigger_one ( ) { ); } - public function case_sub_selects ( ) { - + public function case_sub_selects() + { $this ->given($q = new CUT()) ->when( @@ -131,8 +129,8 @@ public function case_sub_selects ( ) { ); } - public function case_sub_wheres ( ) { - + public function case_sub_wheres() + { $this ->given($q = new CUT()) ->when( @@ -163,8 +161,8 @@ public function case_sub_wheres ( ) { ); } - public function case_default_insert ( ) { - + public function case_default_insert() + { $this ->given($q = new CUT()) ->when( @@ -179,8 +177,8 @@ public function case_default_insert ( ) { ); } - public function case_basic_insert ( ) { - + public function case_basic_insert() + { $this ->given($q = new CUT()) ->when( @@ -198,8 +196,8 @@ public function case_basic_insert ( ) { ); } - public function case_insert_select ( ) { - + public function case_insert_select() + { $this ->given($q = new CUT()) ->when( @@ -222,8 +220,8 @@ public function case_insert_select ( ) { ); } - public function case_basic_update ( ) { - + public function case_basic_update() + { $this ->given($q = new CUT()) ->when( @@ -249,8 +247,8 @@ public function case_basic_update ( ) { ); } - public function case_basic_delete ( ) { - + public function case_basic_delete() + { $this ->given($q = new CUT()) ->when(