Skip to content

Commit 8130c8f

Browse files
committed
update README
1 parent c45a2ba commit 8130c8f

File tree

1 file changed

+85
-4
lines changed

1 file changed

+85
-4
lines changed

README.md

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ This is a small easy-to-use PHP component for working with a database by PDO. It
1010

1111
**PAY ATTENTION! v0.2 and v0.3+ are incompatible.**
1212

13+
## Contributing
14+
15+
Bug reports and/or pull requests are welcome
16+
17+
## License
18+
19+
The package is available as open source under the terms of the [MIT license](https://github.com/co0lc0der/simple-query-builder-php/blob/main/LICENSE.md)
20+
1321
## Installation
1422
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
1523

@@ -21,15 +29,15 @@ or add
2129
```json
2230
"co0lc0der/simple-query-builder": "*"
2331
```
24-
to the require section of your `composer.json` file.
32+
to the `require section` of your `composer.json` file.
2533

2634
## How to use
2735
### Main public methods
2836
- `getSql()` returns SQL query string which will be executed
2937
- `getParams()` returns an array of parameters for a query
3038
- `getResult()` returns query's results
3139
- `getCount()` returns results' rows count
32-
- `getError()` returns `true` if an error is had
40+
- `hasError()` returns `true` if an error is had
3341
- `getErrorMessage()` returns an error message if an error is had
3442
- `setError($message)` sets `$error` to `true` and `$errorMessage`
3543
- `getFirst()` returns the first item of results
@@ -38,8 +46,10 @@ to the require section of your `composer.json` file.
3846
- `all()` executes SQL query and return all rows of result (`fetchAll()`)
3947
- `one()` executes SQL query and return the first row of result (`fetch()`)
4048
- `column()` executes SQL query and return the first column of result (`fetchColumn()`)
49+
- `pluck($key_index, $col_index)` executes SQL query and returns an array (the key (usually ID) and the needed column of result), `key_index` is `0` and `col_index` is `1` by default
4150
- `go()` this method is for non `SELECT` queries. it executes SQL query and return nothing (but returns the last inserted row ID for `INSERT` method)
4251
- `count()` prepares a query with SQL `COUNT()` function
52+
- `exists()` returns `true` if SQL query result has a row
4353
- `query($sql, $params[], $fetch_type)` executes prepared `$sql` with `$params`. it can be used for custom queries
4454
- 'SQL' methods are presented in [Usage section](#usage-examples)
4555

@@ -69,6 +79,8 @@ SELECT * FROM `users`;
6979
- Select a row with a condition
7080
```php
7181
$results = $query->select('users')->where([['id', '=', 10]])->one();
82+
// or since 0.3.4
83+
$results = $query->select('users')->where([['id', 10]])->one();
7284
```
7385
```sql
7486
SELECT * FROM `users` WHERE `id` = 10;
@@ -80,6 +92,12 @@ $results = $query->select('users')->where([
8092
'and',
8193
['group_id', '=', 2],
8294
])->all();
95+
// or since 0.3.4
96+
$results = $query->select('users')->where([
97+
['id', '>', 1],
98+
'and',
99+
['group_id', 2],
100+
])->all();
83101
```
84102
```sql
85103
SELECT * FROM `users` WHERE (`id` > 1) AND (`group_id` = 2);
@@ -108,6 +126,12 @@ $results = $query->select('posts')
108126
->offset(14)
109127
->limit(7)
110128
->all();
129+
// or since 0.3.4
130+
$results = $query->select('posts')
131+
->where([['user_id', 3]])
132+
->offset(14)
133+
->limit(7)
134+
->all();
111135
```
112136
```sql
113137
SELECT * FROM `posts` WHERE (`user_id` = 3) OFFSET 14 LIMIT 7;
@@ -128,6 +152,11 @@ $results = $query->select(['b' => 'branches'], ['b.id', 'b.name'])
128152
->where([['b.id', '>', 1], 'and', ['b.parent_id', '=', 1]])
129153
->orderBy('b.id', 'desc')
130154
->all();
155+
// or since 0.3.4
156+
$results = $query->select(['b' => 'branches'], ['b.id', 'b.name'])
157+
->where([['b.id', '>', 1], 'and', ['b.parent_id', 1]])
158+
->orderBy('b.id desc')
159+
->all();
131160
```
132161
```sql
133162
SELECT `b`.`id`, `b`.`name` FROM `branches` AS `b`
@@ -149,13 +178,19 @@ WHERE (`views` >= 1000) GROUP BY `category`;
149178
$groups = $query->select('orders', ['month_num' => 'MONTH(`created_at`)', 'total' => 'SUM(`total`)'])
150179
->where([['YEAR(`created_at`)', '=', 2020]])
151180
->groupBy('month_num')
152-
->having([['total', '>', 20000]])
181+
->having([['total', '=', 20000]])
182+
->all();
183+
// or since 0.3.4
184+
$groups = $query->select('orders', ['month_num' => 'MONTH(`created_at`)', 'total' => 'SUM(`total`)'])
185+
->where([['YEAR(`created_at`)', 2020]])
186+
->groupBy('month_num')
187+
->having([['total', 20000]])
153188
->all();
154189
```
155190
```sql
156191
SELECT MONTH(`created_at`) AS `month_num`, SUM(`total`) AS `total`
157192
FROM `orders` WHERE (YEAR(`created_at`) = 2020)
158-
GROUP BY `month_num` HAVING (`total` > 20000);
193+
GROUP BY `month_num` HAVING (`total` = 20000);
159194
```
160195
4. `JOIN`. Supports `INNER`, `LEFT OUTER`, `RIGHT OUTER`, `FULL OUTER` and `CROSS` joins (`INNER` is by default)
161196
```php
@@ -200,6 +235,35 @@ INNER JOIN `printer_models` AS `p` ON `cp`.`printer_id` = `p`.`id`
200235
INNER JOIN `cartridge_types` AS `c` ON p.cartridge_id=c.id
201236
WHERE (`cp`.`cab_id` IN (11,12,13)) OR (`cp`.`cab_id` = 5) AND (`p`.`id` > `c`.`id`)
202237
```
238+
```php
239+
// or since 0.3.4
240+
$results = $query->select(['cp' => 'cabs_printers'], [
241+
'cp.id',
242+
'cp.cab_id',
243+
'cab_name' => 'cb.name',
244+
'cp.printer_id',
245+
'cartridge_id' => 'c.id',
246+
'printer_name' => 'p.name',
247+
'cartridge_type' => 'c.name',
248+
'cp.comment'
249+
])
250+
->join(['cb' => 'cabs'], ['cp.cab_id', 'cb.id'])
251+
->join(['p' => 'printer_models'], ['cp.printer_id', 'p.id'])
252+
->join(['c' => 'cartridge_types'], ['p.cartridge_id', 'c.id'])
253+
->groupBy(['cp.printer_id', 'cartridge_id'])
254+
->orderBy(['cp.cab_id', 'cp.printer_id desc'])
255+
->all();
256+
```
257+
```sql
258+
SELECT `cp`.`id`, `cp`.`cab_id`, `cb`.`name` AS `cab_name`, `cp`.`printer_id`, `c`.`id` AS `cartridge_id`,
259+
`p`.`name` AS `printer_name`, `c`.`name` AS `cartridge_type`, `cp`.`comment`
260+
FROM `cabs_printers` AS `cp`
261+
INNER JOIN `cabs` AS `cb` ON `cp`.`cab_id` = `cb`.`id`
262+
INNER JOIN `printer_models` AS `p` ON `cp`.`printer_id` = `p`.`id`
263+
INNER JOIN `cartridge_types` AS `c` ON `p`.`cartridge_id` = `c`.`id`
264+
GROUP BY `cp`.`printer_id`, `cartridge_id`
265+
ORDER BY `cp`.`cab_id` ASC, `cp`.`printer_id` DESC;
266+
```
203267
- Insert a row
204268
```php
205269
$new_id = $query->insert('groups', [
@@ -236,6 +300,14 @@ $query->update('users', [
236300
->where([['id', '=', 7]])
237301
->limit()
238302
->go();
303+
// or since 0.3.4
304+
$query->update('users', [
305+
'username' => 'John Doe',
306+
'status' => 'new status'
307+
])
308+
->where([['id', 7]])
309+
->limit()
310+
->go();
239311
```
240312
```sql
241313
UPDATE `users` SET `username` = 'John Doe', `status` = 'new status'
@@ -257,6 +329,11 @@ $query->delete('users')
257329
->where([['name', '=', 'John']])
258330
->limit()
259331
->go();
332+
// or since 0.3.4
333+
$query->delete('users')
334+
->where([['name', 'John']])
335+
->limit()
336+
->go();
260337
```
261338
```sql
262339
DELETE FROM `users` WHERE `name` = 'John' LIMIT 1;
@@ -266,6 +343,10 @@ DELETE FROM `users` WHERE `name` = 'John' LIMIT 1;
266343
$query->delete('comments')
267344
->where([['user_id', '=', 10]])
268345
->go();
346+
// or since 0.3.4
347+
$query->delete('comments')
348+
->where([['user_id', 10]])
349+
->go();
269350
```
270351
```sql
271352
DELETE FROM `comments` WHERE `user_id` = 10;

0 commit comments

Comments
 (0)