From 99e5032a8af32dfa60cd116139959f01e2f5cd4e Mon Sep 17 00:00:00 2001 From: Fabian de Laender Date: Sat, 25 Jul 2015 19:08:15 +0200 Subject: [PATCH] Release version 1.7.0 --- .gitignore | 54 +++ CHANGELOG.md | 70 ++++ LICENSE | 3 +- README.md | 82 ++++ composer.json | 29 ++ docs/AGGREGATES.md | 31 ++ docs/Clause/GROUP_BY.md | 14 + docs/Clause/HAVING.md | 33 ++ docs/Clause/JOIN.md | 27 ++ docs/Clause/LIMIT.md | 14 + docs/Clause/OFFSET.md | 14 + docs/Clause/ORDER_BY.md | 18 + docs/Clause/WHERE.md | 55 +++ docs/README.md | 22 ++ docs/Statement/DELETE.md | 22 ++ docs/Statement/INSERT.md | 17 + docs/Statement/SELECT.md | 52 +++ docs/Statement/UPDATE.md | 22 ++ src/PDO/Clause/ClauseContainer.php | 20 + src/PDO/Clause/GroupClause.php | 36 ++ src/PDO/Clause/HavingClause.php | 109 ++++++ src/PDO/Clause/JoinClause.php | 80 ++++ src/PDO/Clause/LimitClause.php | 44 +++ src/PDO/Clause/OffsetClause.php | 44 +++ src/PDO/Clause/OrderClause.php | 37 ++ src/PDO/Clause/WhereClause.php | 226 +++++++++++ src/PDO/Database.php | 78 ++++ src/PDO/Statement.php | 30 ++ src/PDO/Statement/DeleteStatement.php | 64 ++++ src/PDO/Statement/InsertStatement.php | 99 +++++ src/PDO/Statement/SelectStatement.php | 428 +++++++++++++++++++++ src/PDO/Statement/StatementContainer.php | 467 +++++++++++++++++++++++ src/PDO/Statement/UpdateStatement.php | 85 +++++ 33 files changed, 2424 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 composer.json create mode 100644 docs/AGGREGATES.md create mode 100644 docs/Clause/GROUP_BY.md create mode 100644 docs/Clause/HAVING.md create mode 100644 docs/Clause/JOIN.md create mode 100644 docs/Clause/LIMIT.md create mode 100644 docs/Clause/OFFSET.md create mode 100644 docs/Clause/ORDER_BY.md create mode 100644 docs/Clause/WHERE.md create mode 100644 docs/README.md create mode 100644 docs/Statement/DELETE.md create mode 100644 docs/Statement/INSERT.md create mode 100644 docs/Statement/SELECT.md create mode 100644 docs/Statement/UPDATE.md create mode 100644 src/PDO/Clause/ClauseContainer.php create mode 100644 src/PDO/Clause/GroupClause.php create mode 100644 src/PDO/Clause/HavingClause.php create mode 100644 src/PDO/Clause/JoinClause.php create mode 100644 src/PDO/Clause/LimitClause.php create mode 100644 src/PDO/Clause/OffsetClause.php create mode 100644 src/PDO/Clause/OrderClause.php create mode 100644 src/PDO/Clause/WhereClause.php create mode 100644 src/PDO/Database.php create mode 100644 src/PDO/Statement.php create mode 100644 src/PDO/Statement/DeleteStatement.php create mode 100644 src/PDO/Statement/InsertStatement.php create mode 100644 src/PDO/Statement/SelectStatement.php create mode 100644 src/PDO/Statement/StatementContainer.php create mode 100644 src/PDO/Statement/UpdateStatement.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40ff20b --- /dev/null +++ b/.gitignore @@ -0,0 +1,54 @@ +# --------------------------- +# --- OSX Generated Files --- +# --------------------------- + +.DS_Store +.AppleDouble +.LSOverride + +# --- Icon Must End With Two \r --- +Icon + + +# --- Thumbnails --- +._* + +# --- Files That Might Appear On External Disk --- +.Spotlight-V100 +.Trashes + +# --- Directories Potentially Created On Remote AFP Share --- +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# --------------------------- +# --- WIN Generated Files --- +# --------------------------- + +# --- Windows Image File Caches --- +Thumbs.db +ehthumbs.db + +# --- Folder Config File --- +Desktop.ini + +# --- Recycle Bin Used On File Shares --- +$RECYCLE.BIN/ + +# --- Windows Installer Files --- +*.cab +*.msi +*.msm +*.msp + +# --------------------------- +# ---- Development Files ---- +# --------------------------- + +composer.lock +composer.phar +vendor/ +.idea/ \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..f862de3 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,70 @@ +### Changelog + +##### v1.6.1 ++ Updated documentation, see [docs](https://github.com/FaaPz/Slim-PDO/blob/master/docs) directory ++ Updated query builder with: + - Added `havingCount()` method + - Added `havingMax()` method + - Added `havingMin()` method + - Added `havingAvg()` method + - Added `havingSum()` method + +##### v1.6.0 ++ Added documentation, see [docs](https://github.com/FaaPz/Slim-PDO/blob/master/docs) directory ++ Removed `AggregateClause` class (stupid mistake, aggregates are not clauses) ++ Updated query builder with: + - Added `whereLike()` method + - Added `orWhereLike()` method + - Added `whereNotLike()` method + - Added `orWhereNotLike()` method ++ Updated `SelectStatement` class with: + - Added aggregate methods `count()`, `distinctCount()`, `max()`, `min()`, `avg()` and `sum()` + +##### v1.5.0 ++ Updated query builder with: + - Added `having()` method + - Added `orHaving()` method + - Added `distinct()` method + - Added `count()` method + - Added `distinctCount()` method + - Added `max()` method + - Added `min()` method + - Added `avg()` method + - Added `sum()` method + +##### v1.4.0 ++ Updated query builder with: + - Added `whereBetween()` method + - Added `orWhereBetween()` method + - Added `whereNotBetween()` method + - Added `orWhereNotBetween()` method + - Added `whereIn()` method + - Added `orWhereIn()` method + - Added `whereNotIn()` method + - Added `orWhereNotIn()` method + - Added `whereNull()` method + - Added `orWhereNull()` method + - Added `whereNotNull()` method + - Added `orWhereNotNull()` method + +##### v1.3.0 ++ Updated query builder with: + - Added `where()` method + - Added `orWhere()` method + - Added `join()` method + - Added `leftJoin()` method + - Added `rightJoin()` method + - Added `fullJoin()` method + +##### v1.2.0 ++ Updated query builder with: + - Added `groupBy()` method + - Added `orderBy()` method + - Added `limit()` method + - Added `offset()` method + +##### v1.1.0 ++ Added query builder + +##### v1.0.0 ++ First release version \ No newline at end of file diff --git a/LICENSE b/LICENSE index 16b0436..185a313 100644 --- a/LICENSE +++ b/LICENSE @@ -18,5 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 68252ec..a09315e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,84 @@ # Slim-PDO + PDO database library for Slim Framework + +### Installation + +Use [Composer](https://getcomposer.org/) + +```json +"require": { + "slim/pdo": "~1.7" +} +``` + +### Usage + +Simple example selecting all data from `users` table. + +```php +require_once('vendor/autoload.php'); + +$dsn = 'mysql:host=your_db_host;dbname=your_db_name;charset=utf8'; +$usr = 'your_db_username'; +$pwd = 'your_db_password'; + +$pdo = new \Slim\PDO\Database( $dsn , $usr , $pwd ); + +$qry = $pdo->prepare("SELECT * FROM users"); +$qry->execute(); + +try +{ + var_dump( $qry->fetchAll() ); +} +catch( \PDOException $e ) +{ + exit( $e->getMessage() ); +} +``` + +Examples selecting, inserting, updating and deleting data from or into `users` table. + +```php +// SELECT * FROM users WHERE id = ? +$selectStatement = $pdo->select() + ->from('users') + ->where('id', '=', 1234); + +$stmt = $selectStatement->execute(); +$data = $stmt->fetchAll(); + +// INSERT INTO users ( id , usr , pwd ) VALUES ( ? , ? , ? ) +$insertStatement = $pdo->insert(array('id', 'usr', 'pwd')) + ->into('users') + ->values(array(1234, 'your_username', 'your_password')); + +$insertId = $insertStatement->execute(); + +// UPDATE users SET pwd = ? WHERE id = ? +$updateStatement = $pdo->update(array('pwd' => 'your_new_password')) + ->table('users') + ->where('id', '=', 1234); + +$affectedRows = $updateStatement->execute(); + +// DELETE FROM users WHERE id = ? +$deleteStatement = $pdo->delete() + ->from('users') + ->where('id', '=', 1234); + +$affectedRows = $deleteStatement->execute(); +``` + +### Documentation (WIP) + +See [DOCUMENTATION](https://github.com/FaaPz/Slim-PDO/blob/master/docs/README.md) + +### Changelog + +See [CHANGELOG](https://github.com/FaaPz/Slim-PDO/blob/master/CHANGELOG.md) + +### License + +See [LICENSE](https://github.com/FaaPz/Slim-PDO/blob/master/LICENSE) \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..cd3d459 --- /dev/null +++ b/composer.json @@ -0,0 +1,29 @@ +{ + "name": "slim/pdo", + "description": "PDO database library for Slim Framework", + "version": "1.7.0", + "type": "library", + "keywords": ["pdo", "database", "slim", "framework"], + "homepage": "https://github.com/FaaPz/Slim-PDO", + "license": "MIT", + "authors": [ + { + "name": "Fabian de Laender", + "email": "fabian@faapz.nl", + "homepage": "http://faapz.nl", + "role": "Developer" + } + ], + "support": { + "issues": "https://github.com/FaaPz/Slim-PDO/issues" + }, + "require": { + "php": ">=5.3.0", + "ext-pdo": "*" + }, + "autoload": { + "psr-4": { + "Slim\\PDO\\": "src/PDO/" + } + } +} diff --git a/docs/AGGREGATES.md b/docs/AGGREGATES.md new file mode 100644 index 0000000..28f2a33 --- /dev/null +++ b/docs/AGGREGATES.md @@ -0,0 +1,31 @@ +### Aggregates + +##### Methods + ++ `count()` ++ `distinctCount()` ++ `max()` ++ `min()` ++ `avg()` ++ `sum()` + +> Used only in [SELECT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/SELECT.md) statements. + +##### Examples + +```php +// ... COUNT( * ) +$selectStatement->count(); + +// ... COUNT( votes ) AS all_votes +$selectStatement->count('votes', 'all_votes'); + +// ... COUNT( DISTINCT customer_id ) +$selectStatement->distinctCount('customer_id'); + +// ... MIN|MAX( salary ) , AVG( price ) , SUM( votes ) +$selectStatement->min('salary'); +$selectStatement->max('salary'); +$selectStatement->avg('price'); +$selectStatement->sum('votes'); +``` \ No newline at end of file diff --git a/docs/Clause/GROUP_BY.md b/docs/Clause/GROUP_BY.md new file mode 100644 index 0000000..872551b --- /dev/null +++ b/docs/Clause/GROUP_BY.md @@ -0,0 +1,14 @@ +### GROUP BY clause + +##### Methods + ++ `groupBy()` + +> Used only in [SELECT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/SELECT.md) statements. + +##### Examples of grouping + +```php +// ... GROUP BY f_name +$selectStatement->groupBy('f_name'); +``` \ No newline at end of file diff --git a/docs/Clause/HAVING.md b/docs/Clause/HAVING.md new file mode 100644 index 0000000..f400d8e --- /dev/null +++ b/docs/Clause/HAVING.md @@ -0,0 +1,33 @@ +### HAVING clause + +##### Methods + ++ `having()` ++ `orHaving()` ++ `havingCount()` ++ `havingMax()` ++ `havingMin()` ++ `havingAvg()` ++ `havingSum()` + +> Used only in [SELECT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/SELECT.md) statements. + +##### Examples + +```php +// ... HAVING MIN( price ) > ? OR MAX( price ) < ? +$selectStatement->having('MIN( price )', '>', 125)->orHaving('MAX( price )', '<', 250); + +// ... HAVING COUNT( * ) > ? +$selectStatement->havingCount('*', '>', 1234); + +// ... HAVING MIN|MAX( salary ) > ? +$selectStatement->havingMin('salary', '>', 25000); +$selectStatement->havingMax('salary', '<', 50000); + +// ... HAVING AVG( price ) < ? +$selectStatement->havingAvg('price', '<', 12.5); + +// ... HAVING SUM( votes ) > ? +$selectStatement->havingSum('votes', '>', 25); +``` \ No newline at end of file diff --git a/docs/Clause/JOIN.md b/docs/Clause/JOIN.md new file mode 100644 index 0000000..189dcde --- /dev/null +++ b/docs/Clause/JOIN.md @@ -0,0 +1,27 @@ +### JOIN clause + +##### Methods + ++ `join()` ++ `leftJoin()` ++ `rightJoin()` ++ `fullJoin()` + +> Used only in [SELECT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/SELECT.md) statements. + +##### Examples + +```php +// ... INNER JOIN orders ON customers.id = orders.customer_id +$selectStatement->join('orders', 'customers.id', '=', 'orders.customer_id'); +$selectStatement->join('orders', 'customers.id', '=', 'orders.customer_id', 'INNER'); + +// ... LEFT OUTER JOIN orders ON customers.id = orders.customer_id +$selectStatement->leftJoin('orders', 'customers.id', '=', 'orders.customer_id'); + +// ... RIGHT OUTER JOIN orders ON customers.id = orders.customer_id +$selectStatement->rightJoin('orders', 'customers.id', '=', 'orders.customer_id'); + +// ... FULL OUTER JOIN orders ON customers.id = orders.customer_id +$selectStatement->fullJoin('orders', 'customers.id', '=', 'orders.customer_id'); +``` \ No newline at end of file diff --git a/docs/Clause/LIMIT.md b/docs/Clause/LIMIT.md new file mode 100644 index 0000000..0288a3d --- /dev/null +++ b/docs/Clause/LIMIT.md @@ -0,0 +1,14 @@ +### LIMIT clause + +##### Methods + ++ `limit()` + +> Used in [SELECT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/SELECT.md), [UPDATE](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/UPDATE.md) and [DELETE](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/DELETE.md) statements. + +##### Examples of limiting + +```php +// ... LIMIT 10 +$statement->limit(10); +``` \ No newline at end of file diff --git a/docs/Clause/OFFSET.md b/docs/Clause/OFFSET.md new file mode 100644 index 0000000..b1ea5f8 --- /dev/null +++ b/docs/Clause/OFFSET.md @@ -0,0 +1,14 @@ +### OFFSET clause + +##### Methods + ++ `offset()` + +> Used only in [SELECT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/SELECT.md) statements. + +##### Examples of offsetting + +```php +// ... OFFSET 20 +$selectStatement->offset(20); +``` \ No newline at end of file diff --git a/docs/Clause/ORDER_BY.md b/docs/Clause/ORDER_BY.md new file mode 100644 index 0000000..1fa42be --- /dev/null +++ b/docs/Clause/ORDER_BY.md @@ -0,0 +1,18 @@ +### ORDER BY clause + +##### Methods + ++ `orderBy()` + +> Used in [SELECT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/SELECT.md), [UPDATE](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/UPDATE.md) and [DELETE](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/DELETE.md) statements. + +##### Examples of ordering + +```php +// ... ORDER BY l_name ASC +$statement->orderBy('l_name'); +$statement->orderBy('l_name', 'ASC'); + +// ... ORDER BY l_name DESC +$statement->orderBy('l_name', 'DESC'); +``` \ No newline at end of file diff --git a/docs/Clause/WHERE.md b/docs/Clause/WHERE.md new file mode 100644 index 0000000..a3bbbe4 --- /dev/null +++ b/docs/Clause/WHERE.md @@ -0,0 +1,55 @@ +### WHERE clause + +##### Methods + ++ `where()` ++ `orWhere()` ++ `whereBetween()` ++ `orWhereBetween()` ++ `whereNotBetween()` ++ `orWhereNotBetween()` ++ `whereIn()` ++ `orWhereIn()` ++ `whereNotIn()` ++ `orWhereNotIn()` ++ `whereLike()` ++ `orWhereLike()` ++ `whereNotLike()` ++ `orWhereNotLike()` ++ `whereNull()` ++ `orWhereNull()` ++ `whereNotNull()` ++ `orWhereNotNull()` + +> Used in [SELECT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/SELECT.md), [UPDATE](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/UPDATE.md) and [DELETE](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/DELETE.md) statements. + +##### Examples + +```php +// ... WHERE usr = ? OR f_name = ? +$statement->where('usr', '=', 'FaaPz')->orWhere('f_name', '=', 'Fabian'); + +// ... WHERE customer_id BETWEEN ? AND ? +$statement->whereBetween('customer_id', array( 110, 220 )); + +// ... WHERE customer_id NOT BETWEEN ? AND ? +$statement->whereNotBetween('customer_id', array( 330, 440 )); + +// ... WHERE customer_id IN ( ?, ?, ?, ? ) +$statement->whereIn('customer_id', array( 110, 120, 130, 140 )); + +// ... WHERE customer_id NOT IN ( ?, ?, ?, ? ) +$statement->whereNotIn('customer_id', array( 112, 124, 136, 148 )); + +// ... WHERE f_name LIKE ? +$statement->whereLike('f_name', 'Fab___'); + +// ... WHERE l_name NOT LIKE ? +$statement->whereNotLike('l_name', 'Lae%'); + +// ... WHERE f_name IS NULL +$statement->whereNull('f_name'); + +// ... WHERE l_name IS NOT NULL +$statement->whereNotNull('l_name'); +``` \ No newline at end of file diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..1f551dd --- /dev/null +++ b/docs/README.md @@ -0,0 +1,22 @@ +### Documentation + +##### Statements + ++ [SELECT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/SELECT.md) ++ [INSERT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/INSERT.md) ++ [UPDATE](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/UPDATE.md) ++ [DELETE](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Statement/DELETE.md) + +##### Clauses + ++ [JOIN](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/JOIN.md) ++ [WHERE](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/WHERE.md) ++ [GROUP BY](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/GROUP_BY.md) ++ [HAVING](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/HAVING.md) ++ [ORDER BY](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/ORDER_BY.md) ++ [LIMIT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/LIMIT.md) ++ [OFFSET](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/OFFSET.md) + +##### Functions + ++ [AGGREGATES](https://github.com/FaaPz/Slim-PDO/blob/master/docs/AGGREGATES.md) \ No newline at end of file diff --git a/docs/Statement/DELETE.md b/docs/Statement/DELETE.md new file mode 100644 index 0000000..b1174df --- /dev/null +++ b/docs/Statement/DELETE.md @@ -0,0 +1,22 @@ +### DELETE statement + +##### Methods + ++ `from()` + +##### Clauses + ++ [WHERE](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/WHERE.md) ++ [ORDER BY](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/ORDER_BY.md) ++ [LIMIT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/LIMIT.md) + +##### Examples + +```php +// DELETE FROM users WHERE id = ? +$deleteStatement = $slimPdo->delete() + ->from('users') + ->where('id', '=', 1234); + +$affectedRows = $deleteStatement->execute(); +``` \ No newline at end of file diff --git a/docs/Statement/INSERT.md b/docs/Statement/INSERT.md new file mode 100644 index 0000000..bbbd1d4 --- /dev/null +++ b/docs/Statement/INSERT.md @@ -0,0 +1,17 @@ +### INSERT statement + +##### Methods + ++ `into()` ++ `values()` + +##### Examples + +```php +// INSERT INTO users ( id , usr , pwd ) VALUES ( ? , ? , ? ) +$insertStatement = $slimPdo->insert(array('id', 'usr', 'pwd')) + ->into('users') + ->values(array(1234, 'your_username', 'your_password')); + +$insertId = $insertStatement->execute(); +``` \ No newline at end of file diff --git a/docs/Statement/SELECT.md b/docs/Statement/SELECT.md new file mode 100644 index 0000000..d82b754 --- /dev/null +++ b/docs/Statement/SELECT.md @@ -0,0 +1,52 @@ +### SELECT statement + +##### Methods + ++ `distinct()` ++ `from()` ++ `join()` ++ `leftJoin()` ++ `rightJoin()` ++ `fullJoin()` ++ `groupBy()` ++ `having()` ++ `orHaving()` ++ `havingCount()` ++ `havingMax()` ++ `havingMin()` ++ `havingAvg()` ++ `havingSum()` ++ `offset()` + +##### Aggregate methods + ++ `count` ++ `distinctCount()` ++ `max()` ++ `min()` ++ `avg()` ++ `sum()` + +> Documentation? Click [here](https://github.com/FaaPz/Slim-PDO/blob/master/docs/AGGREGATES.md)! + +##### Clauses + ++ [JOIN](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/JOIN.md) ++ [WHERE](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/WHERE.md) ++ [GROUP BY](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/GROUP_BY.md) ++ [HAVING](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/HAVING.md) ++ [ORDER BY](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/ORDER_BY.md) ++ [LIMIT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/LIMIT.md) ++ [OFFSET](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/OFFSET.md) + +##### Examples + +```php +// SELECT * FROM users WHERE id = ? +$selectStatement = $slimPdo->select() + ->from('users') + ->where('id', '=', 1234); + +$stmt = $selectStatement->execute(); +$data = $stmt->fetchAll(); +``` \ No newline at end of file diff --git a/docs/Statement/UPDATE.md b/docs/Statement/UPDATE.md new file mode 100644 index 0000000..740b16c --- /dev/null +++ b/docs/Statement/UPDATE.md @@ -0,0 +1,22 @@ +### UPDATE statement + +##### Methods + ++ `table()` + +##### Clauses + ++ [WHERE](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/WHERE.md) ++ [ORDER BY](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/ORDER_BY.md) ++ [LIMIT](https://github.com/FaaPz/Slim-PDO/blob/master/docs/Clause/LIMIT.md) + +##### Examples + +```php +// UPDATE users SET pwd = ? WHERE id = ? +$updateStatement = $slimPdo->update(array('pwd' => 'your_new_password')) + ->table('users') + ->where('id', '=', 1234); + +$affectedRows = $updateStatement->execute(); +``` \ No newline at end of file diff --git a/src/PDO/Clause/ClauseContainer.php b/src/PDO/Clause/ClauseContainer.php new file mode 100644 index 0000000..425d4bf --- /dev/null +++ b/src/PDO/Clause/ClauseContainer.php @@ -0,0 +1,20 @@ + + */ +abstract class ClauseContainer +{ + /** + * @var array + */ + protected $container = array(); +} diff --git a/src/PDO/Clause/GroupClause.php b/src/PDO/Clause/GroupClause.php new file mode 100644 index 0000000..84ad11f --- /dev/null +++ b/src/PDO/Clause/GroupClause.php @@ -0,0 +1,36 @@ + + */ +class GroupClause extends ClauseContainer +{ + /** + * @param $statement + */ + public function groupBy( $statement ) + { + $this->container[] = $statement; + } + + /** + * @return string + */ + public function __toString() + { + if( empty( $this->container ) ) + { + return ''; + } + + return ' GROUP BY ' . implode( ' , ' , $this->container ); + } +} diff --git a/src/PDO/Clause/HavingClause.php b/src/PDO/Clause/HavingClause.php new file mode 100644 index 0000000..0351dea --- /dev/null +++ b/src/PDO/Clause/HavingClause.php @@ -0,0 +1,109 @@ + + */ +class HavingClause extends ClauseContainer +{ + /** + * @param $column + * @param null $operator + * @param string $rule + */ + public function having( $column , $operator = null , $rule = 'AND' ) + { + $this->container[] = ' ' . $rule . ' ' . $column . ' ' . $operator . ' ?'; + } + + /** + * @param $column + * @param null $operator + */ + public function orHaving( $column , $operator = null ) + { + $this->having( $column , $operator , 'OR' ); + } + + /** + * @param $column + * @param null $operator + */ + public function havingCount( $column , $operator = null ) + { + $column = 'COUNT( ' . $column . ' )'; + + $this->having( $column , $operator ); + } + + /** + * @param $column + * @param null $operator + */ + public function havingMax( $column , $operator = null ) + { + $column = 'MAX( ' . $column . ' )'; + + $this->having( $column , $operator ); + } + + /** + * @param $column + * @param null $operator + */ + public function havingMin( $column , $operator = null ) + { + $column = 'MIN( ' . $column . ' )'; + + $this->having( $column , $operator ); + } + + /** + * @param $column + * @param null $operator + */ + public function havingAvg( $column , $operator = null ) + { + $column = 'AVG( ' . $column . ' )'; + + $this->having( $column , $operator ); + } + + /** + * @param $column + * @param null $operator + */ + public function havingSum( $column , $operator = null ) + { + $column = 'SUM( ' . $column . ' )'; + + $this->having( $column , $operator ); + } + + /** + * @return string + */ + public function __toString() + { + if( empty( $this->container ) ) + { + return ''; + } + + $args = array(); + + foreach( $this->container as $having ) + { + $args[] = $having; + } + + return ' HAVING ' . ltrim( implode( '' , $args ) , 'AND ' ); + } +} diff --git a/src/PDO/Clause/JoinClause.php b/src/PDO/Clause/JoinClause.php new file mode 100644 index 0000000..e8551e0 --- /dev/null +++ b/src/PDO/Clause/JoinClause.php @@ -0,0 +1,80 @@ + + */ +class JoinClause extends ClauseContainer +{ + /** + * @param $table + * @param $first + * @param null $operator + * @param null $second + * @param string $type + */ + public function join( $table , $first , $operator = null , $second = null , $type = 'INNER' ) + { + $this->container[] = ' ' . $type . ' JOIN ' . $table . ' ON ' . $first . ' ' . $operator . ' ' . $second; + } + + /** + * @param $table + * @param $first + * @param null $operator + * @param null $second + */ + public function leftJoin( $table , $first , $operator = null , $second = null ) + { + $this->join( $table , $first , $operator , $second , 'LEFT OUTER' ); + } + + /** + * @param $table + * @param $first + * @param null $operator + * @param null $second + */ + public function rightJoin( $table , $first , $operator = null , $second = null ) + { + $this->join( $table , $first , $operator , $second , 'RIGHT OUTER' ); + } + + /** + * @param $table + * @param $first + * @param null $operator + * @param null $second + */ + public function fullJoin( $table , $first , $operator = null , $second = null ) + { + $this->join( $table , $first , $operator , $second , 'FULL OUTER' ); + } + + /** + * @return string + */ + public function __toString() + { + if( empty( $this->container ) ) + { + return ''; + } + + $args = array(); + + foreach( $this->container as $join ) + { + $args[] = $join; + } + + return implode( '' , $args ); + } +} diff --git a/src/PDO/Clause/LimitClause.php b/src/PDO/Clause/LimitClause.php new file mode 100644 index 0000000..4077909 --- /dev/null +++ b/src/PDO/Clause/LimitClause.php @@ -0,0 +1,44 @@ + + */ +class LimitClause extends ClauseContainer +{ + /** + * @var null + */ + private $limit = null; + + /** + * @param $number + */ + public function limit( $number ) + { + if( is_int( $number ) && $number >= 0 ) + { + $this->limit = intval( $number ); + } + } + + /** + * @return string + */ + public function __toString() + { + if( $this->limit === null ) + { + return ''; + } + + return ' LIMIT ' . $this->limit; + } +} diff --git a/src/PDO/Clause/OffsetClause.php b/src/PDO/Clause/OffsetClause.php new file mode 100644 index 0000000..2cf5efa --- /dev/null +++ b/src/PDO/Clause/OffsetClause.php @@ -0,0 +1,44 @@ + + */ +class OffsetClause extends ClauseContainer +{ + /** + * @var null + */ + private $offset = null; + + /** + * @param $number + */ + public function offset( $number ) + { + if( is_int( $number ) && $number >= 0 ) + { + $this->offset = intval( $number ); + } + } + + /** + * @return string + */ + public function __toString() + { + if( $this->offset === null ) + { + return ''; + } + + return ' OFFSET ' . $this->offset; + } +} diff --git a/src/PDO/Clause/OrderClause.php b/src/PDO/Clause/OrderClause.php new file mode 100644 index 0000000..f0c1b32 --- /dev/null +++ b/src/PDO/Clause/OrderClause.php @@ -0,0 +1,37 @@ + + */ +class OrderClause extends ClauseContainer +{ + /** + * @param $statement + * @param string $order + */ + public function orderBy( $statement , $order = 'ASC' ) + { + $this->container[] = $statement . ' ' . strtoupper( $order ); + } + + /** + * @return string + */ + public function __toString() + { + if( empty( $this->container ) ) + { + return ''; + } + + return ' ORDER BY ' . implode( ' , ' , $this->container ); + } +} diff --git a/src/PDO/Clause/WhereClause.php b/src/PDO/Clause/WhereClause.php new file mode 100644 index 0000000..257b3c9 --- /dev/null +++ b/src/PDO/Clause/WhereClause.php @@ -0,0 +1,226 @@ + + */ +class WhereClause extends ClauseContainer +{ + /** + * @param $column + * @param null $operator + * @param string $rule + */ + public function where( $column , $operator = null , $rule = 'AND' ) + { + $this->container[] = ' ' . $rule . ' ' . $column . ' ' . $operator . ' ?'; + } + + /** + * @param $column + * @param null $operator + */ + public function orWhere( $column , $operator = null ) + { + $this->where( $column , $operator , 'OR' ); + } + + /** + * @param $column + * @param string $rule + * @param bool $not + */ + public function whereBetween( $column , $rule = 'AND' , $not = false ) + { + $syntax = 'BETWEEN'; + + if( $not ) + { + $syntax = 'NOT BETWEEN'; + } + + $this->container[] = ' ' . $rule . ' ' . $column . ' ' . $syntax . ' ? AND ?'; + } + + /** + * @param $column + */ + public function orWhereBetween( $column ) + { + $this->whereBetween( $column , 'OR' ); + } + + /** + * @param $column + * @param string $rule + */ + public function whereNotBetween( $column , $rule = 'AND' ) + { + $this->whereBetween( $column , $rule , true ); + } + + /** + * @param $column + */ + public function orWhereNotBetween( $column ) + { + $this->whereNotBetween( $column , 'OR' ); + } + + /** + * @param $column + * @param $placeholders + * @param string $rule + * @param bool $not + */ + public function whereIn( $column , $placeholders , $rule = 'AND' , $not = false ) + { + $syntax = 'IN'; + + if( $not ) + { + $syntax = 'NOT IN'; + } + + $this->container[] = ' ' . $rule . ' ' . $column . ' ' . $syntax . ' ' . $placeholders; + } + + /** + * @param $column + * @param $placeholders + */ + public function orWhereIn( $column , $placeholders ) + { + $this->whereIn( $column , $placeholders , 'OR' ); + } + + /** + * @param $column + * @param $placeholders + * @param string $rule + */ + public function whereNotIn( $column , $placeholders , $rule = 'AND' ) + { + $this->whereIn( $column , $placeholders , $rule , true ); + } + + /** + * @param $column + * @param $placeholders + */ + public function orWhereNotIn( $column , $placeholders ) + { + $this->whereNotIn( $column , $placeholders , 'OR' ); + } + + /** + * @param $column + * @param string $rule + * @param bool $not + */ + public function whereLike( $column , $rule = 'AND' , $not = false ) + { + $syntax = 'LIKE'; + + if( $not ) + { + $syntax = 'NOT LIKE'; + } + + $this->container[] = ' ' . $rule . ' ' . $column . ' ' . $syntax . ' ?'; + } + + /** + * @param $column + */ + public function orWhereLike( $column ) + { + $this->whereLike( $column ); + } + + /** + * @param $column + * @param string $rule + */ + public function whereNotLike( $column , $rule = 'AND' ) + { + $this->whereLike( $column , $rule , true ); + } + + /** + * @param $column + */ + public function orWhereNotLike( $column ) + { + $this->whereNotLike( $column , 'OR' ); + } + + /** + * @param $column + * @param string $rule + * @param bool $not + */ + public function whereNull( $column , $rule = 'AND' , $not = false ) + { + $syntax = 'NULL'; + + if( $not ) + { + $syntax = 'NOT NULL'; + } + + $this->container[] = ' ' . $rule . ' ' . $column . ' IS ' . $syntax; + } + + /** + * @param $column + */ + public function orWhereNull( $column ) + { + $this->whereNull( $column , 'OR' ); + } + + /** + * @param $column + * @param string $rule + */ + public function whereNotNull( $column , $rule = 'AND' ) + { + $this->whereNull( $column , $rule , true ); + } + + /** + * @param $column + */ + public function orWhereNotNull( $column ) + { + $this->whereNotNull( $column , 'OR' ); + } + + /** + * @return string + */ + public function __toString() + { + if( empty( $this->container ) ) + { + return ''; + } + + $args = array(); + + foreach( $this->container as $where ) + { + $args[] = $where; + } + + return ' WHERE ' . ltrim( implode( '' , $args ) , 'AND ' ); + } +} diff --git a/src/PDO/Database.php b/src/PDO/Database.php new file mode 100644 index 0000000..ce10033 --- /dev/null +++ b/src/PDO/Database.php @@ -0,0 +1,78 @@ + + */ +class Database extends \PDO +{ + /** + * Constructor + * + * @param $dsn + * @param null $usr + * @param null $pwd + * @param array $options + */ + public function __construct( $dsn , $usr = null , $pwd = null , array $options = array() ) + { + $options = array( + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, + \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, + \PDO::ATTR_EMULATE_PREPARES => false, + \PDO::ATTR_STATEMENT_CLASS => array( + 'Slim\\PDO\\Statement', + array( $this ) + ) + ) + $options; + + @parent::__construct( $dsn , $usr , $pwd , $options ); + } + + /** + * @param array $columns + * @return SelectStatement + */ + public function select( array $columns ) + { + return new SelectStatement( $this , $columns ); + } + + /** + * @param array $columns + * @return InsertStatement + */ + public function insert( array $columns ) + { + return new InsertStatement( $this , $columns ); + } + + /** + * @param array $pairs + * @return UpdateStatement + */ + public function update( array $pairs ) + { + return new UpdateStatement( $this , $pairs ); + } + + /** + * @return DeleteStatement + */ + public function delete() + { + return new DeleteStatement( $this ); + } +} diff --git a/src/PDO/Statement.php b/src/PDO/Statement.php new file mode 100644 index 0000000..f42669a --- /dev/null +++ b/src/PDO/Statement.php @@ -0,0 +1,30 @@ + + */ +class Statement extends \PDOStatement +{ + /** + * @var Database + */ + protected $dbh; + + /** + * Constructor + * + * @param Database $dbh + */ + protected function __construct( Database $dbh ) + { + $this->dbh = $dbh; + } +} diff --git a/src/PDO/Statement/DeleteStatement.php b/src/PDO/Statement/DeleteStatement.php new file mode 100644 index 0000000..551ce12 --- /dev/null +++ b/src/PDO/Statement/DeleteStatement.php @@ -0,0 +1,64 @@ + + */ +class DeleteStatement extends StatementContainer +{ + /** + * Constructor + * + * @param Database $dbh + */ + public function __construct( Database $dbh ) + { + parent::__construct( $dbh ); + } + + /** + * @param $table + * @return $this + */ + public function from( $table ) + { + $this->setTable( $table ); + + return $this; + } + + /** + * @return string + */ + public function __toString() + { + if( empty( $this->table ) ) + { + trigger_error( 'No table is set for deletion' , E_USER_ERROR ); + } + + $sql = 'DELETE FROM ' . $this->table; + $sql .= $this->whereClause; + $sql .= $this->orderClause; + $sql .= $this->limitClause; + + return $sql; + } + + /** + * @return int + */ + public function execute() + { + return parent::execute()->rowCount(); + } +} diff --git a/src/PDO/Statement/InsertStatement.php b/src/PDO/Statement/InsertStatement.php new file mode 100644 index 0000000..7d4e33c --- /dev/null +++ b/src/PDO/Statement/InsertStatement.php @@ -0,0 +1,99 @@ + + */ +class InsertStatement extends StatementContainer +{ + /** + * Constructor + * + * @param Database $dbh + * @param array $columns + */ + public function __construct( Database $dbh , array $columns ) + { + parent::__construct( $dbh ); + + $this->setColumns( $columns ); + } + + /** + * @param $table + * @return $this + */ + public function into( $table ) + { + $this->setTable( $table ); + + return $this; + } + + /** + * @param array $values + * @return $this + */ + public function values( array $values ) + { + $this->setValues( $values ); + + $this->setPlaceholders( $values ); + + return $this; + } + + /** + * @return string + */ + public function __toString() + { + if( empty( $this->table ) ) + { + trigger_error( 'No table is set for insertion' , E_USER_ERROR ); + } + + if( empty( $this->columns ) ) + { + trigger_error( 'Missing columns for insertion' , E_USER_ERROR ); + } + + if( empty( $this->values ) ) + { + trigger_error( 'Missing values for insertion' , E_USER_ERROR ); + } + + $sql = 'INSERT INTO ' . $this->table; + $sql .= ' ' . $this->getColumns(); + $sql .= ' VALUES ' . $this->getPlaceholders(); + + return $sql; + } + + /** + * @return string + */ + public function execute() + { + parent::execute(); + + return $this->dbh->lastInsertId(); + } + + /** + * @return string + */ + private function getColumns() + { + return '( ' . implode( ' , ' , $this->columns ) . ' )'; + } +} diff --git a/src/PDO/Statement/SelectStatement.php b/src/PDO/Statement/SelectStatement.php new file mode 100644 index 0000000..9d09564 --- /dev/null +++ b/src/PDO/Statement/SelectStatement.php @@ -0,0 +1,428 @@ + + */ +class SelectStatement extends StatementContainer +{ + /** + * @var bool + */ + private $distinct = false; + + /** + * @var JoinClause + */ + private $joinClause; + + /** + * @var GroupClause + */ + private $groupClause; + + /** + * @var HavingClause + */ + private $havingClause; + + /** + * @var OffsetClause + */ + private $offsetClause; + + /** + * Constructor + * + * @param Database $dbh + * @param array $columns + */ + public function __construct( Database $dbh , array $columns ) + { + parent::__construct( $dbh ); + + if( empty( $columns ) ) + { + $columns = array('*'); + } + + $this->setColumns( $columns ); + + $this->joinClause = new JoinClause(); + $this->groupClause = new GroupClause(); + $this->havingClause = new HavingClause(); + $this->offsetClause = new OffsetClause(); + } + + /** + * @return $this + */ + public function distinct() + { + $this->distinct = true; + + return $this; + } + + /** + * @param string $column + * @param null $as + * @param bool $distinct + * @return $this + */ + public function count( $column = '*' , $as = null , $distinct = false ) + { + $this->columns[] = $this->setDistinct( $distinct ) . ' ' . $column . ' )' . $this->setAs( $as ); + + return $this; + } + + /** + * @param string $column + * @param null $as + * @return $this + */ + public function distinctCount( $column = '*' , $as = null ) + { + $this->count( $column , $as , true ); + + return $this; + } + + /** + * @param $column + * @param null $as + * @return $this + */ + public function max( $column , $as = null ) + { + $this->columns[] = 'MAX( ' . $column . ' )' . $this->setAs( $as ); + + return $this; + } + + /** + * @param $column + * @param null $as + * @return $this + */ + public function min( $column , $as = null ) + { + $this->columns[] = 'MIN( ' . $column . ' )' . $this->setAs( $as ); + + return $this; + } + + /** + * @param $column + * @param null $as + * @return $this + */ + public function avg( $column , $as = null ) + { + $this->columns[] = 'AVG( ' . $column . ' )' . $this->setAs( $as ); + + return $this; + } + + /** + * @param $column + * @param null $as + * @return $this + */ + public function sum( $column , $as = null ) + { + $this->columns[] = 'SUM( ' . $column . ' )' . $this->setAs( $as ); + + return $this; + } + + /** + * @param $table + * @return $this + */ + public function from( $table ) + { + $this->setTable( $table ); + + return $this; + } + + /** + * @param $table + * @param $first + * @param null $operator + * @param null $second + * @param string $type + * @return $this + */ + public function join( $table , $first , $operator = null , $second = null , $type = 'INNER' ) + { + $this->joinClause->join( $table , $first , $operator , $second , $type ); + + return $this; + } + + /** + * @param $table + * @param $first + * @param null $operator + * @param null $second + * @return $this + */ + public function leftJoin( $table , $first , $operator = null , $second = null ) + { + $this->joinClause->leftJoin( $table , $first , $operator , $second ); + + return $this; + } + + /** + * @param $table + * @param $first + * @param null $operator + * @param null $second + * @return $this + */ + public function rightJoin( $table , $first , $operator = null , $second = null ) + { + $this->joinClause->rightJoin( $table , $first , $operator , $second ); + + return $this; + } + + /** + * @param $table + * @param $first + * @param null $operator + * @param null $second + * @return $this + */ + public function fullJoin( $table , $first , $operator = null , $second = null ) + { + $this->joinClause->fullJoin( $table , $first , $operator , $second ); + + return $this; + } + + /** + * @param $statement + * @return $this + */ + public function groupBy( $statement ) + { + $this->groupClause->groupBy( $statement ); + + return $this; + } + + /** + * @param $column + * @param null $operator + * @param null $value + * @param string $rule + * @return $this + */ + public function having( $column , $operator = null , $value = null , $rule = 'AND' ) + { + $this->values[] = $value; + + $this->havingClause->having( $column , $operator , $rule ); + + return $this; + } + + /** + * @param $column + * @param null $operator + * @param null $value + * @return $this + */ + public function orHaving( $column , $operator = null , $value = null ) + { + $this->values[] = $value; + + $this->havingClause->orHaving( $column , $operator ); + + return $this; + } + + /** + * @param $column + * @param null $operator + * @param null $value + * @return $this + */ + public function havingCount( $column , $operator = null , $value = null ) + { + $this->values[] = $value; + + $this->havingClause->havingCount( $column , $operator ); + + return $this; + } + + /** + * @param $column + * @param null $operator + * @param null $value + * @return $this + */ + public function havingMax( $column , $operator = null , $value = null ) + { + $this->values[] = $value; + + $this->havingClause->havingMax( $column , $operator ); + + return $this; + } + + /** + * @param $column + * @param null $operator + * @param null $value + * @return $this + */ + public function havingMin( $column , $operator = null , $value = null ) + { + $this->values[] = $value; + + $this->havingClause->havingMin( $column , $operator ); + + return $this; + } + + /** + * @param $column + * @param null $operator + * @param null $value + * @return $this + */ + public function havingAvg( $column , $operator = null , $value = null ) + { + $this->values[] = $value; + + $this->havingClause->havingAvg( $column , $operator ); + + return $this; + } + + /** + * @param $column + * @param null $operator + * @param null $value + * @return $this + */ + public function havingSum( $column , $operator = null , $value = null ) + { + $this->values[] = $value; + + $this->havingClause->havingSum( $column , $operator ); + + return $this; + } + + /** + * @param $number + * @return $this + */ + public function offset( $number ) + { + $this->offsetClause->offset( $number ); + + return $this; + } + + /** + * @return string + */ + public function __toString() + { + if( empty( $this->table ) ) + { + trigger_error( 'No table is set for selection' , E_USER_ERROR ); + } + + $sql = $this->getSelect() . ' ' . $this->getColumns(); + $sql .= ' FROM ' . $this->table; + $sql .= $this->joinClause; + $sql .= $this->whereClause; + $sql .= $this->groupClause; + $sql .= $this->havingClause; + $sql .= $this->orderClause; + $sql .= $this->limitClause; + $sql .= $this->offsetClause; + + return $sql; + } + + /** + * @return \PDOStatement + */ + public function execute() + { + return parent::execute(); + } + + /** + * @return string + */ + private function getSelect() + { + if( $this->distinct ) + { + return 'SELECT DISTINCT'; + } + + return 'SELECT'; + } + + /** + * @return string + */ + private function getColumns() + { + return implode( ' , ' , $this->columns ); + } + + /** + * @param $distinct + * @return string + */ + private function setDistinct( $distinct ) + { + if( $distinct ) + { + return 'COUNT( DISTINCT'; + } + + return 'COUNT('; + } + + /** + * @param $as + * @return string + */ + private function setAs( $as ) + { + if( empty( $as ) ) + { + return ''; + } + + return ' AS ' . $as; + } +} diff --git a/src/PDO/Statement/StatementContainer.php b/src/PDO/Statement/StatementContainer.php new file mode 100644 index 0000000..9826a80 --- /dev/null +++ b/src/PDO/Statement/StatementContainer.php @@ -0,0 +1,467 @@ + + */ +abstract class StatementContainer +{ + /** + * @var Database + */ + protected $dbh; + + /** + * @var array + */ + protected $columns = array(); + + /** + * @var array + */ + protected $values = array(); + + /** + * @var array + */ + protected $placeholders = array(); + + /** + * @var + */ + protected $table; + + /** + * @var WhereClause + */ + protected $whereClause; + + /** + * @var OrderClause + */ + protected $orderClause; + + /** + * @var LimitClause + */ + protected $limitClause; + + /** + * Constructor + * + * @param Database $dbh + */ + public function __construct( Database $dbh ) + { + $this->dbh = $dbh; + + $this->whereClause = new WhereClause(); + $this->orderClause = new OrderClause(); + $this->limitClause = new LimitClause(); + } + + /** + * @param $column + * @param null $operator + * @param null $value + * @param string $rule + * @return $this + */ + public function where( $column , $operator = null , $value = null , $rule = 'AND' ) + { + $this->values[] = $value; + + $this->whereClause->where( $column , $operator , $rule ); + + return $this; + } + + /** + * @param $column + * @param null $operator + * @param null $value + * @return $this + */ + public function orWhere( $column , $operator = null , $value = null ) + { + $this->values[] = $value; + + $this->whereClause->orWhere( $column , $operator ); + + return $this; + } + + /** + * @param $column + * @param array $values + * @param string $rule + * @return $this + */ + public function whereBetween( $column , array $values , $rule = 'AND' ) + { + $this->setValues( $values ); + + $this->whereClause->whereBetween( $column , $rule ); + + return $this; + } + + /** + * @param $column + * @param array $values + * @return $this + */ + public function orWhereBetween( $column , array $values ) + { + $this->setValues( $values ); + + $this->whereClause->orWhereBetween( $column ); + + return $this; + } + + /** + * @param $column + * @param array $values + * @param string $rule + * @return $this + */ + public function whereNotBetween( $column , array $values , $rule = 'AND' ) + { + $this->setValues( $values ); + + $this->whereClause->whereNotBetween( $column , $rule ); + + return $this; + } + + /** + * @param $column + * @param array $values + * @return $this + */ + public function orWhereNotBetween( $column , array $values ) + { + $this->setValues( $values ); + + $this->whereClause->orWhereNotBetween( $column ); + + return $this; + } + + /** + * @param $column + * @param array $values + * @param string $rule + * @return $this + */ + public function whereIn( $column , array $values , $rule = 'AND' ) + { + $this->setValues( $values ); + + $this->setPlaceholders( $values ); + + $this->whereClause->whereIn( $column , $this->getPlaceholders() , $rule ); + + return $this; + } + + /** + * @param $column + * @param array $values + * @return $this + */ + public function orWhereIn( $column , array $values ) + { + $this->setValues( $values ); + + $this->setPlaceholders( $values ); + + $this->whereClause->orWhereIn( $column , $this->getPlaceholders() ); + + return $this; + } + + /** + * @param $column + * @param array $values + * @param string $rule + * @return $this + */ + public function whereNotIn( $column , array $values , $rule = 'AND' ) + { + $this->setValues( $values ); + + $this->setPlaceholders( $values ); + + $this->whereClause->whereNotIn( $column , $this->getPlaceholders() , $rule ); + + return $this; + } + + /** + * @param $column + * @param array $values + * @return $this + */ + public function orWhereNotIn( $column , array $values ) + { + $this->setValues( $values ); + + $this->setPlaceholders( $values ); + + $this->whereClause->orWhereNotIn( $column , $this->getPlaceholders() ); + + return $this; + } + + /** + * @param $column + * @param null $value + * @param string $rule + * @return $this + */ + public function whereLike( $column , $value = null , $rule = 'AND' ) + { + $this->values[] = $value; + + $this->whereClause->whereLike( $column , $rule ); + + return $this; + } + + /** + * @param $column + * @param null $value + * @return $this + */ + public function orWhereLike( $column , $value = null ) + { + $this->values[] = $value; + + $this->whereClause->orWhereLike( $column ); + + return $this; + } + + /** + * @param $column + * @param null $value + * @param string $rule + * @return $this + */ + public function whereNotLike( $column , $value = null , $rule = 'AND' ) + { + $this->values[] = $value; + + $this->whereClause->whereNotLike( $column , $rule ); + + return $this; + } + + /** + * @param $column + * @param null $value + * @return $this + */ + public function orWhereNotLike( $column , $value = null ) + { + $this->values[] = $value; + + $this->whereClause->orWhereNotLike( $column ); + + return $this; + } + + /** + * @param $column + * @param string $rule + * @return $this + */ + public function whereNull( $column , $rule = 'AND' ) + { + $this->whereClause->whereNull( $column , $rule ); + + return $this; + } + + /** + * @param $column + * @return $this + */ + public function orWhereNull( $column ) + { + $this->whereClause->orWhereNull( $column ); + + return $this; + } + + /** + * @param $column + * @param string $rule + * @return $this + */ + public function whereNotNull( $column , $rule = 'AND' ) + { + $this->whereClause->whereNotNull( $column , $rule ); + + return $this; + } + + /** + * @param $column + * @return $this + */ + public function orWhereNotNull( $column ) + { + $this->whereClause->orWhereNotNull( $column ); + + return $this; + } + + /** + * @param $statement + * @param string $order + * @return $this + */ + public function orderBy( $statement , $order = 'ASC' ) + { + $this->orderClause->orderBy( $statement , $order ); + + return $this; + } + + /** + * @param $number + * @return $this + */ + public function limit( $number ) + { + $this->limitClause->limit( $number ); + + return $this; + } + + /** + * @return mixed + */ + abstract public function __toString(); + + /** + * @return \PDOStatement + */ + public function execute() + { + $stmt = $this->getStatement(); + $stmt->execute( $this->values ); + + return $stmt; + } + + /** + * @return string + */ + public function compile() + { + return $this->getStatement()->queryString; + } + + /** + * @param $table + * @return $this + */ + protected function setTable( $table ) + { + $this->table = $table; + + return $this; + } + + /** + * @param array $columns + * @return $this + */ + protected function setColumns( array $columns ) + { + $this->columns = array_merge( $this->columns , $columns ); + + return $this; + } + + /** + * @param array $values + * @return $this + */ + protected function setValues( array $values ) + { + $this->values = array_merge( $this->values , $values ); + + return $this; + } + + /** + * @return string + */ + protected function getPlaceholders() + { + $placeholders = $this->placeholders; + + unset( $this->placeholders ); + + return '( ' . implode( ' , ' , $placeholders ) . ' )'; + } + + /** + * @param array $values + */ + protected function setPlaceholders( array $values ) + { + foreach( $values as $value ) + { + $this->placeholders[] = $this->setPlaceholder( '?' , sizeof( $value ) ); + } + } + + /** + * @return \PDOStatement + */ + private function getStatement() + { + $sql = $this; + + return $this->dbh->prepare( $sql ); + } + + /** + * @param $text + * @param int $count + * @param string $separator + * @return string + */ + private function setPlaceholder( $text , $count = 0 , $separator = ' , ' ) + { + $result = array(); + + if( $count > 0 ) + { + for( $x = 0; $x < $count; $x++ ) + { + $result[] = $text; + } + } + + return implode( $separator , $result ); + } +} diff --git a/src/PDO/Statement/UpdateStatement.php b/src/PDO/Statement/UpdateStatement.php new file mode 100644 index 0000000..fdfedc9 --- /dev/null +++ b/src/PDO/Statement/UpdateStatement.php @@ -0,0 +1,85 @@ + + */ +class UpdateStatement extends StatementContainer +{ + /** + * Constructor + * + * @param Database $dbh + * @param array $pairs + */ + public function __construct( Database $dbh , array $pairs ) + { + parent::__construct( $dbh ); + + foreach( $pairs as $column => $value ) + { + $this->columns[] = $column . ' = ?'; + $this->values[] = $value; + } + } + + /** + * @param $table + * @return $this + */ + public function table( $table ) + { + $this->setTable( $table ); + + return $this; + } + + /** + * @return string + */ + public function __toString() + { + if( empty( $this->table ) ) + { + trigger_error( 'No table is set for update' , E_USER_ERROR ); + } + + if( empty( $this->columns ) && empty( $this->values ) ) + { + trigger_error( 'Missing columns and values for update' , E_USER_ERROR ); + } + + $sql = 'UPDATE ' . $this->table; + $sql .= ' SET ' . $this->getColumns(); + $sql .= $this->whereClause; + $sql .= $this->orderClause; + $sql .= $this->limitClause; + + return $sql; + } + + /** + * @return int + */ + public function execute() + { + return parent::execute()->rowCount(); + } + + /** + * @return string + */ + private function getColumns() + { + return implode( ' , ' , $this->columns ); + } +}