|
9 | 9 |
|
10 | 10 | namespace chillerlan\DatabaseExample; |
11 | 11 |
|
12 | | -use chillerlan\Database\Database; |
| 12 | +use chillerlan\Database\DatabaseOptions; |
| 13 | +use chillerlan\Database\Drivers\MySQLiDrv; |
| 14 | +use chillerlan\DotEnv\DotEnv; |
| 15 | +use chillerlan\SimpleCache\MemoryCache; |
13 | 16 |
|
14 | | -/** @var \chillerlan\Database\DatabaseOptions $options */ |
15 | | -$options = null; |
| 17 | +require_once __DIR__.'/../vendor/autoload.php'; |
16 | 18 |
|
17 | | -/** @var \Psr\SimpleCache\CacheInterface $cache */ |
18 | | -$cache = null; |
| 19 | +$env = (new DotEnv(__DIR__.'/../.config', '.env'))->load(); |
19 | 20 |
|
20 | | -require_once __DIR__.'/common.php'; |
| 21 | +$options = new DatabaseOptions([ |
| 22 | + 'host' => $env->get('DB_MYSQLI_HOST'), |
| 23 | + 'port' => (int)$env->get('DB_MYSQLI_PORT'), |
| 24 | + 'socket' => $env->get('DB_MYSQLI_SOCKET'), |
| 25 | + 'database' => $env->get('DB_MYSQLI_DATABASE'), |
| 26 | + 'username' => $env->get('DB_MYSQLI_USERNAME'), |
| 27 | + 'password' => $env->get('DB_MYSQLI_PASSWORD'), |
| 28 | +]); |
21 | 29 |
|
22 | | -$db = new Database($options, $cache); |
| 30 | +$db = new MySQLiDrv($options, new MemoryCache); |
23 | 31 | $db->connect(); |
24 | 32 |
|
| 33 | +// named parameters for context |
25 | 34 | $db->create |
26 | | - ->table('products') |
| 35 | + ->table(tablename: 'products') |
27 | 36 | ->ifNotExists() |
28 | | - ->int('id',10, null, false, 'UNSIGNED AUTO_INCREMENT') |
29 | | - ->tinytext('name', null, false) |
30 | | - ->varchar('type', 20) |
31 | | - ->decimal('price', '9,2', 0) |
32 | | - ->decimal('weight', '8,3') |
33 | | - ->int('added', 10, 0, null, 'UNSIGNED') |
34 | | - ->primaryKey('id') |
35 | | - ->query(); |
36 | | - |
37 | | -$db->truncate->table('products')->query(); |
| 37 | + ->int(name: 'id', length: 10, isNull: false, attribute: 'UNSIGNED AUTO_INCREMENT') |
| 38 | + ->tinytext(name: 'name', isNull: false) |
| 39 | + ->varchar(name: 'type', length: 20) |
| 40 | + ->decimal(name: 'price', length: '9,2', defaultValue: 0) |
| 41 | + ->decimal(name: 'weight', length: '8,3') |
| 42 | + ->int(name: 'added', length: 10, defaultValue: 0, attribute: 'UNSIGNED') |
| 43 | + ->primaryKey(field: 'id') |
| 44 | + ->executeQuery(); |
| 45 | + |
| 46 | +$db->truncate |
| 47 | + ->table('products') |
| 48 | + ->executeQuery(); |
38 | 49 |
|
39 | 50 |
|
40 | 51 | // single row insert |
41 | 52 | $db->insert |
42 | 53 | ->into('products') |
43 | 54 | ->values(['name' => 'product1', 'type' => 'a', 'price' => 3.99, 'weight' => 0.1, 'added' => time()]) |
44 | | - ->query(); |
| 55 | + ->executeQuery(); |
45 | 56 |
|
46 | 57 |
|
47 | 58 | // multi insert |
|
53 | 64 | $db->insert |
54 | 65 | ->into('products') |
55 | 66 | ->values($values) |
56 | | - ->multi(); |
| 67 | + ->executeMultiQuery(); |
57 | 68 |
|
58 | 69 |
|
59 | 70 | // multi insert with callback |
|
68 | 79 | ->values([ |
69 | 80 | ['name' => '?', 'type' => '?', 'price' => '?', 'weight' => '?', 'added' => '?'] |
70 | 81 | ]) |
71 | | - ->callback($values, function($row){ |
| 82 | + ->executeMultiQuery($values, function(array $row):array{ |
72 | 83 | return [ |
73 | 84 | $row[0], |
74 | 85 | $row[1], |
|
90 | 101 | ->from(['t1' => 'products']) |
91 | 102 | ->where('t1.type', 'a') |
92 | 103 | ->orderBy(['t1.price' => 'asc']) |
93 | | - ->query('uid') |
| 104 | + ->executeQuery('uid') |
94 | 105 | ->toArray(); |
95 | 106 |
|
96 | 107 | var_dump($result); |
|
0 commit comments