Skip to content

Commit

Permalink
1. Updated queries to properly escape values.
Browse files Browse the repository at this point in the history
2. Updated documentation to show more examples of Query Builders.
  • Loading branch information
Nathan Fiscaletti authored and nathan-fiscaletti committed Nov 2, 2019
1 parent b024254 commit facc44e
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 24 deletions.
188 changes: 177 additions & 11 deletions docs/queries.md
Expand Up @@ -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
Expand All @@ -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).
> 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);`
2 changes: 1 addition & 1 deletion src/MySqliWrapper/Functions.php
Expand Up @@ -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;
Expand Down
24 changes: 12 additions & 12 deletions src/MySqliWrapper/QueryBuilder.php
Expand Up @@ -48,8 +48,8 @@ public function insert($data)
$valResult = '(';
foreach ($data as $property => $value) {
$propResult .= ($propResult == '(')
? "$property"
: ",$property";
? "`$property`"
: ",`$property`";

$valResult .= ($valResult == '(')
? '?'
Expand Down Expand Up @@ -88,8 +88,8 @@ public function update($data)
$result = '';
foreach ($data as $property => $value) {
$result .= ($result == '')
? "$property = ?"
: ",$property = ?";
? "`$property` = ?"
: ",`$property` = ?";

$this->withQueryParameter($value);
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit facc44e

Please sign in to comment.