Skip to content

Commit 362c3ec

Browse files
committed
:octocat:
1 parent e242dce commit 362c3ec

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

examples/querybuilder.php

+33-22
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,50 @@
99

1010
namespace chillerlan\DatabaseExample;
1111

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;
1316

14-
/** @var \chillerlan\Database\DatabaseOptions $options */
15-
$options = null;
17+
require_once __DIR__.'/../vendor/autoload.php';
1618

17-
/** @var \Psr\SimpleCache\CacheInterface $cache */
18-
$cache = null;
19+
$env = (new DotEnv(__DIR__.'/../.config', '.env'))->load();
1920

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+
]);
2129

22-
$db = new Database($options, $cache);
30+
$db = new MySQLiDrv($options, new MemoryCache);
2331
$db->connect();
2432

33+
// named parameters for context
2534
$db->create
26-
->table('products')
35+
->table(tablename: 'products')
2736
->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();
3849

3950

4051
// single row insert
4152
$db->insert
4253
->into('products')
4354
->values(['name' => 'product1', 'type' => 'a', 'price' => 3.99, 'weight' => 0.1, 'added' => time()])
44-
->query();
55+
->executeQuery();
4556

4657

4758
// multi insert
@@ -53,7 +64,7 @@
5364
$db->insert
5465
->into('products')
5566
->values($values)
56-
->multi();
67+
->executeMultiQuery();
5768

5869

5970
// multi insert with callback
@@ -68,7 +79,7 @@
6879
->values([
6980
['name' => '?', 'type' => '?', 'price' => '?', 'weight' => '?', 'added' => '?']
7081
])
71-
->callback($values, function($row){
82+
->executeMultiQuery($values, function(array $row):array{
7283
return [
7384
$row[0],
7485
$row[1],
@@ -90,7 +101,7 @@
90101
->from(['t1' => 'products'])
91102
->where('t1.type', 'a')
92103
->orderBy(['t1.price' => 'asc'])
93-
->query('uid')
104+
->executeQuery('uid')
94105
->toArray();
95106

96107
var_dump($result);

0 commit comments

Comments
 (0)