Skip to content

Commit 6b9e03e

Browse files
committed
- Some minor improvements
1 parent d4511bc commit 6b9e03e

16 files changed

+395
-145
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ indent_size = 4
55
charset = utf-8
66
trim_trailing_whitespace = true
77
insert_final_newline = true
8+
9+
[*.md]
10+
end_of_line = lf
11+
indent_style = tab
12+
indent_size = 4
13+
charset = utf-8
14+
trim_trailing_whitespace = true
15+
insert_final_newline = true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
!/doc
23
!/src
34
!/tests
45
!/.editorconfig

README.md

Lines changed: 15 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -16,161 +16,33 @@ Here a few things to keep in mind:
1616
* The charset is up to you. No special binding to UTF8, although UTF8 is the default.
1717
* The order of method-calls of each statement-builder is irrelevant. The resulting query will always render the right order.
1818
* No animals were harmed due to the production of this library.
19+
* The order of method-calls doesn't matter.
1920

2021
## Some examples
2122

22-
### Initialization
23-
24-
```PHP
25-
$pdo = new PDO('mysql:host=127.0.0.1;dbname=test;charset=utf8', 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
26-
```
27-
28-
```PHP
29-
$mysql = new MySQL($pdo);
30-
$mysql->getAliasRegistry()->add('t', 'testdb.test__');
31-
```
23+
[Initialization](doc/initialization.md)
3224

3325
### Select
3426

35-
```PHP
36-
$subSelect = function ($id) use ($mysql) {
37-
return $mysql->select()
38-
->field('t.id')
39-
->from('t', 'table')
40-
->where('t.foreign_id=?', $id);
41-
};
42-
43-
$select = $mysql->select()
44-
->field('COUNT(*)', 'customer_count')
45-
->from('t1', 't#test1')
46-
->joinInner('t2', 't#test2', 't2.test_id = t1.id AND t2.field1 = ?', 123)
47-
->joinLeft('t3', 't#test3', 't3.test_id = t1.id')
48-
->joinRight('t4', 't#test4', 't4.test_id = t1.id')
49-
->joinRight('t5', $subSelect(10), 't5.test_id = t1.id')
50-
->orderBy('t1.field1')
51-
->orderBy('t1.field2', 'DESC')
52-
->limit(100)
53-
->offset(50);
54-
```
55-
56-
```PHP
57-
if($contition === true) {
58-
$select->where('t1.somefield = ?', $someValue);
59-
}
60-
```
61-
62-
```PHP
63-
$rows = $select->fetchRows();
64-
foreach($rows as $row) {
65-
print_r($row);
66-
}
67-
```
68-
69-
* The order of method-calls doesn't matter.
27+
[Simple select](doc/simple-select.md)
28+
[Complex select](doc/complex-select.md)
29+
[Optional conditions](doc/optional-conditions.md)
7030

7131
### Insert
7232

73-
You can insert key-value-arrays with `addAll`, `updateAll`, `addOrUpdateAll`. As the second parameter you can provide an array to specify the only fields to consider.
74-
75-
```PHP
76-
$id = $mysql->insert()
77-
->into('test')
78-
->addOrUpdateAll($data, ['field1', 'field2', 'field3'])
79-
->add('created_by', $userId)
80-
->addOrUpdate('updated_by', $userId)
81-
->addExpr('created_at=NOW()')
82-
->addOrUpdateExpr('updated_at=NOW()')
83-
->run();
84-
```
85-
86-
* `insert()` alwasy returns an id, no matter if a dataset was actually inserted or updated.
87-
* You can mass-insert by using `insert()->...->insertRows(array $rows)`.
88-
89-
There is also an option to build an `INSERT INTO ... SELECT ... FROM ... ON DUPLICATE KEY UPDATE ...`:
90-
91-
```PHP
92-
$id = $mysql->insert()
93-
->into('test')
94-
->addExpr('field1=:field1')
95-
->addOrUpdateExpr('field2=:field2')
96-
->addExpr('field3=NOW()')
97-
->from(
98-
$mysql->select()
99-
->field('a.myfield1', 'field1')
100-
->field('a.myfield2', 'field2')
101-
->from('a', 'mytable')
102-
->where('field=?', 1)
103-
)->run();
104-
```
33+
[Insert](doc/insert.md)
10534

10635
### Update
10736

108-
```PHP
109-
$mysql->update()
110-
->table('t1', 'test1')
111-
->joinLeft('t2', 'test2', 't1.id = t2.test_id')
112-
->setAll($data)
113-
->where("t1.field1 = ? OR t2.field2 > ?", 1, 10)
114-
->where("field IN (?)", [1, 2, 3, 4, 5, 6])
115-
->run();
116-
```
37+
[Insert](doc/update.md)
11738

11839
### Delete
11940

120-
You can use joins in delete-statements. But only the rows of tables specified in `from` will be modified (deleted).
121-
122-
```PHP
123-
$mysql->delete()
124-
->from('t1', 'test1')
125-
->joinLeft('t2', 'test2', 't1.id=t2.test_id')
126-
->where('t1.field1=? AND t2.field2 > ?', 1, 10)
127-
->run();
128-
```
129-
130-
### True nested transactions
131-
132-
```php
133-
$mysql = new \Kir\MySQL\Databases\MySQL($pdo);
134-
135-
$mysql->delete()->from('test')->run();
136-
137-
$test = function () use ($mysql) {
138-
$name = $mysql->select()
139-
->field('t.name')
140-
->from('t', 'test')
141-
->where('t.id=?', 1)
142-
->fetchValue();
143-
printf("Current name is %s\n", $name);
144-
};
145-
146-
$setName = function ($name) use ($mysql) {
147-
$mysql->insert()
148-
->into('test')
149-
->add('id', 1)
150-
->addOrUpdate('name', $name)
151-
->run();
152-
};
153-
154-
$setName('Peter');
155-
$test();
156-
157-
$mysql->transaction(function () use ($mysql, $setName, $test) {
158-
$setName('Paul');
159-
$test();
160-
161-
// $mysql->transaction or...
162-
$mysql->dryRun(function () use ($mysql, $setName, $test) {
163-
$setName('Bert');
164-
$test();
165-
});
166-
});
167-
168-
$test();
169-
```
170-
171-
```
172-
Current name is Peter
173-
Current name is Paul
174-
Current name is Bert
175-
Current name is Paul
176-
```
41+
[Insert](doc/delete.md)
42+
43+
### Misc
44+
45+
[Nested transactions](doc/nested-transactions.md)
46+
47+
48+

doc/complex-select.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Complex select
2+
3+
```php
4+
$dateStart = '2016-05-01';
5+
$dateEnd = '2016-05-31';
6+
7+
$tableA = $db->select()
8+
->field('a.field1')
9+
->field('a.field2')
10+
->from('a', 'table_a')
11+
->where('a.date BETWEEN ? AND ?', $dateStart, $dateEnd);
12+
13+
$tableB = $db->select()
14+
->field('b.field1')
15+
->field('b.field2')
16+
->from('b', 'table_b')
17+
->where('b.date BETWEEN ? AND ?', $dateStart, $dateEnd);
18+
19+
$tableC = $db->select()
20+
->field('t.field1')
21+
->field('t.field2')
22+
->from('t', 'table_c')
23+
->where('t.date BETWEEN ? AND ?', $dateStart, $dateEnd);
24+
25+
echo $db->select()
26+
->from('t',
27+
$db->select()
28+
->from('a', $tableA)
29+
->joinLeft('b', $tableB, 'b.id=a.id')
30+
->where('NOT ISNULL(a.field1)')
31+
->union($tableC)
32+
);
33+
```
34+
35+
[Back](../README.md)

doc/delete.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Delete
2+
3+
You can use joins in delete-statements. But only the rows of tables specified in `from` will be modified (deleted).
4+
5+
```PHP
6+
$mysql->delete()
7+
->from('t1', 'test1')
8+
->joinLeft('t2', 'test2', 't1.id=t2.test_id')
9+
->where('t1.field1=? AND t2.field2 > ?', 1, 10)
10+
->run();
11+
```
12+
13+
[Back](../README.md)

doc/initialization.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Initialization
2+
3+
```PHP
4+
$pdo = new PDO('mysql:host=127.0.0.1;dbname=test;charset=utf8', 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
5+
```
6+
7+
```PHP
8+
$mysql = new MySQL($pdo);
9+
$mysql->getAliasRegistry()->add('t', 'testdb.test__');
10+
```
11+
12+
[Back](../README.md)

doc/insert.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Insert
2+
3+
You can insert key-value-arrays with `addAll`, `updateAll`, `addOrUpdateAll`. As the second parameter you can provide an array to specify the only fields to consider.
4+
5+
```PHP
6+
$id = $mysql->insert()
7+
->into('test')
8+
->addOrUpdateAll($data, ['field1', 'field2', 'field3'])
9+
->add('created_by', $userId)
10+
->addOrUpdate('updated_by', $userId)
11+
->addExpr('created_at=NOW()')
12+
->addOrUpdateExpr('updated_at=NOW()')
13+
->run();
14+
```
15+
16+
* `insert()` alwasy returns an id, no matter if a dataset was actually inserted or updated.
17+
* You can mass-insert by using `insert()->...->insertRows(array $rows)`.
18+
19+
There is also an option to build an `INSERT INTO ... SELECT ... FROM ... ON DUPLICATE KEY UPDATE ...`:
20+
21+
```PHP
22+
$id = $mysql->insert()
23+
->into('test')
24+
->addExpr('field1=:field1')
25+
->addOrUpdateExpr('field2=:field2')
26+
->addExpr('field3=NOW()')
27+
->from(
28+
$mysql->select()
29+
->field('a.myfield1', 'field1')
30+
->field('a.myfield2', 'field2')
31+
->from('a', 'mytable')
32+
->where('field=?', 1)
33+
)->run();
34+
```
35+
36+
[Back](../README.md)

doc/nested-transactions.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Nested transactions
2+
3+
```php
4+
$mysql = new \Kir\MySQL\Databases\MySQL($pdo);
5+
6+
$mysql->delete()->from('test')->run();
7+
8+
$test = function () use ($mysql) {
9+
$name = $mysql->select()
10+
->field('t.name')
11+
->from('t', 'test')
12+
->where('t.id=?', 1)
13+
->fetchValue();
14+
printf("Current name is %s\n", $name);
15+
};
16+
17+
$setName = function ($name) use ($mysql) {
18+
$mysql->insert()
19+
->into('test')
20+
->add('id', 1)
21+
->addOrUpdate('name', $name)
22+
->run();
23+
};
24+
25+
$setName('Peter');
26+
$test();
27+
28+
$mysql->transaction(function () use ($mysql, $setName, $test) {
29+
$setName('Paul');
30+
$test();
31+
32+
// $mysql->transaction or...
33+
$mysql->dryRun(function () use ($mysql, $setName, $test) {
34+
$setName('Bert');
35+
$test();
36+
});
37+
});
38+
39+
$test();
40+
```
41+
42+
```
43+
Current name is Peter
44+
Current name is Paul
45+
Current name is Bert
46+
Current name is Paul
47+
```
48+
49+
[Back](../README.md)

doc/optional-conditions.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Optional conditions
2+
3+
```PHP
4+
$filter = [
5+
'name' => 'Peter',
6+
'date' => [
7+
'start' => '2016-05-01',
8+
'end' => '2016-05-31',
9+
],
10+
];
11+
12+
$query = $db
13+
->from('t', 'test')
14+
->where(new OptionalDBFilterMap('t.name=?', $filter, 'name'))
15+
->where(new OptionalDBFilterMap('t.date >= ?', $filter, 'date.start')) // Key in dot-notation
16+
->where(new OptionalDBFilterMap('t.date <= ?', $filter, ['date', 'end'])) // Array-Key
17+
```
18+
19+
[Back](../README.md)

0 commit comments

Comments
 (0)