diff --git a/docs/queries.md b/docs/queries.md index 23ce2e1..0724380 100644 --- a/docs/queries.md +++ b/docs/queries.md @@ -52,6 +52,8 @@ $connection->execute($query, true); With a QueryBuilder you still have access to all of the same features as a Query, however you are now provided with functions for generating the query itself. +You can retrieve a query builder using `$connection->table('sometable')`. You can then start building your query with this object. + The following code demonstrates a simple example of the same query as above, but using a QueryBuilder instead. ```php @@ -61,26 +63,190 @@ $connection->table('users') ->fetchFirst(); ``` -> See [QueryBuilder](../src/MySqliWrapper/QueryBuilder.php) for a list of all functions provided by the QueryBuilder. - **When using a QueryBuilder, there are two more functions added for retrieving the result.** They can be used as follows: 1. Retrieve an instance of [\MySqliWrapper\Model](../src/MySqliWrapper/Model.php) for the Table. ```php -$connection->table('users') - ->select('*') - ->where('user_id', 3) - ->first(); +$queryBuilder->first(); ``` 2. Retrieve a list of instances of [\MySqliWrapper\Model](../src/MySqliWrapper/Model.php) for the Table. ```php -$connection->table('users') - ->select('*') - ->where('user_id', 3) - ->get(); +$queryBuilder->get(); ``` -> See [Models](./models.md). \ No newline at end of file +> You can also still use all of the functions that come from the `Query` class. + +## Using a QueryBuilder + +For each of the following examples; assume we have a Query Builder in the variable `$qb` that was created with the following code: + +```php +$qb = $connection->table('people'); +``` + +After you have finished building a Query using the Query Builder, you can execute it using one of the functions documented earlier on this page. + +> Including what's listed here, there are a lot of other use cases for the Query Builder. Look at the [`\MySqliWrapper\QueryBuilder`](../src/MySqliWrapper/QueryBuilder.php) class for a full list of these functions. + +### Inserting Data + +- PHP + + ```php + $qb->insert([ + 'name' => 'nathan', + 'age' => 24 + ]); + + echo $qb->getRawQuery(); + ``` + +- Results In + + ```sql + INSERT INTO `people` + (`name`,`age`) + VALUES (?,?) + ``` + +### Deleting Data + +- PHP + + ```php + $qb->delete() + ->where('name', '=', 'nathan') + ->orWhere('age', '>', 5); + + echo $qb->getRawQuery(); + ``` + +- Results In + + ```sql + DELETE FROM `people` + WHERE `name` = ? + OR `age` > ? + ``` + +### Updating Data + +- PHP + + ```php + $qb->update([ + 'age' => 25, + 'phone' => '1234567890', + ])->where('name', '=', 'John'); + + echo $qb->getRawQuery(); + ``` + +- Results In + + ```sql + UPDATE `people` SET + `age` = ?,`phone` = ? + WHERE `name` = ? + ``` + +### Incrementing a Column + +- PHP + + ```php + $qb->increment('friends') + ->where('name', '=', 'john'); + + echo $qb->getRawQuery(); + ``` + +- Results In + + ```sql + UPDATE `people` SET + `friends` = `friends` + ? + WHERE `name` = ? + ``` + +- You can also + + * Provide a number to increment by, by default this function will increment the number by `1`. + + ```php + $qb->increment('friends', 10); + ``` + + * Provider aditional `update` data + + ```php + $qb->increment('friends', 10, ['age' => 25]); + ``` + +### Retrieving Data + +- PHP + + ```php + $qb->select(['name', 'age']); + + echo $qb->getRawQuery(); + ``` + +- Results In + + ```sql + SELECT + `name`, `age` + FROM `people` + ``` + +- You can also + + * Provide `'*'` instead of an array of elements to select. + + ```php + $qb->select('*'); + ``` + + * Provide a custom select string + + ```php + $qb->select('`name`, `people.age` as years_old'); + ``` + +### Joining other Tables + +- PHP + + ```php + $qb->select(['people.*', 'games.name']) + ->leftJoin('games', function($join) { + $join->on('games.id', '=', '`people.game_id`'); + }); + + echo $qb->getRawQuery(); + ``` + +- Results In + + ```sql + SELECT + `people.*`, `games.name` + FROM `people` + LEFT JOIN `games` + ON `games.id` = `people.game_id` + ``` + +- You can also + + * Normal `join` with `$qb->join($table, $join);` + * Outer `join` with `$qb->outerJoin($table, $join);` + * Inner `join` with `$qb->innerJoin($table, $join);` + * Left Outer `join` with `$qb->leftOuterJoin($table, $join);` + * Right `join` with `$qb->rightJoin($table, $join);` + * Right Outer `join` with `$qb->rightOuterJoin($table, $join);` + * Cross `join` with `$qb->crossJoin($table);` diff --git a/src/MySqliWrapper/Functions.php b/src/MySqliWrapper/Functions.php index d60f6d3..3e52c28 100644 --- a/src/MySqliWrapper/Functions.php +++ b/src/MySqliWrapper/Functions.php @@ -38,7 +38,7 @@ function mysqliwrapper__asQueryBindType($value) function mysqliwrapper__selectableToString($what) { if (is_array($what)) { - return implode(','.PHP_EOL, $what); + return '`'.implode('`, `', $what).'`'; } return $what; diff --git a/src/MySqliWrapper/QueryBuilder.php b/src/MySqliWrapper/QueryBuilder.php index bdd9f4d..2aefd59 100644 --- a/src/MySqliWrapper/QueryBuilder.php +++ b/src/MySqliWrapper/QueryBuilder.php @@ -48,8 +48,8 @@ public function insert($data) $valResult = '('; foreach ($data as $property => $value) { $propResult .= ($propResult == '(') - ? "$property" - : ",$property"; + ? "`$property`" + : ",`$property`"; $valResult .= ($valResult == '(') ? '?' @@ -88,8 +88,8 @@ public function update($data) $result = ''; foreach ($data as $property => $value) { $result .= ($result == '') - ? "$property = ?" - : ",$property = ?"; + ? "`$property` = ?" + : ",`$property` = ?"; $this->withQueryParameter($value); } @@ -111,11 +111,11 @@ public function update($data) */ public function increment($column, $amount = 1, $update = []) { - $query .= "UPDATE `$this->table` SET".PHP_EOL; - $result = "$column = $column + ?"; + $query = "UPDATE `$this->table` SET".PHP_EOL; + $result = "`$column` = `$column` + ?"; $this->withQueryParameter($amount); foreach ($update as $property => $value) { - $result .= ",$property = ?"; + $result .= ",`$property` = ?"; $this->withQueryParameter($value); } $query .= $result.PHP_EOL; @@ -170,7 +170,7 @@ public function where($property, $operator, $value) { $this->withQueryParameter($value); - return $this->raw("WHERE $property $operator ?".PHP_EOL); + return $this->raw("WHERE `$property` $operator ?".PHP_EOL); } /** @@ -186,7 +186,7 @@ public function orWhere($property, $operator, $value) { $this->withQueryParameter($value); - return $this->raw("OR $property $operator ?".PHP_EOL); + return $this->raw("OR `$property` $operator ?".PHP_EOL); } /** @@ -202,7 +202,7 @@ public function andWhere($property, $operator, $value) { $this->withQueryParameter($value); - return $this->raw("AND $property $operator ?".PHP_EOL); + return $this->raw("AND `$property` $operator ?".PHP_EOL); } /** @@ -403,7 +403,7 @@ public function when($value, $true, $false = null) */ public function on($property, $operator, $value) { - return $this->raw("ON $property $operator $value".PHP_EOL); + return $this->raw("ON `$property` $operator $value".PHP_EOL); } /** @@ -416,7 +416,7 @@ public function on($property, $operator, $value) */ public function orderBy($column, $direction = 'ASC') { - return $this->raw("ORDER BY $column $direction".PHP_EOL); + return $this->raw("ORDER BY `$column` $direction".PHP_EOL); } /**