@@ -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+
0 commit comments