-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
109 lines (80 loc) · 3.38 KB
/
Copy pathexample.php
File metadata and controls
109 lines (80 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* This file is part of the sshilko/php-sql-mydb package.
*
* (c) Sergei Shilko <contact@sshilko.com>
*
* MIT License
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @license https://opensource.org/licenses/mit-license.php MIT
*/
// @codeCoverageIgnoreStart
declare(strict_types = 1);
use MydbRepository\UserRepository;
use sql\MydbCredentials;
use sql\MydbFactory;
use sql\MydbLogger;
use sql\MydbOptions;
use sql\MydbRegistry;
include_once __DIR__ . '/../vendor/autoload.php';
include_once __DIR__ . '/MydbRepository/UserRepository.php';
$registry = new MydbRegistry();
$mylogger = new MydbLogger();
$sqlHost = getenv('MYSQL_HOST') ?: 'mysql';
$auth = new MydbCredentials($sqlHost, 'root', 'root', 'mydb', 3306);
$factory = new MydbFactory();
$opts = $factory->createOptions();
$opts->setTransactionIsolationLevel(MydbOptions::TRANSACTION_ISOLATION_LEVEL_READ_COMMITTED);
$mydb = $factory->create($auth, $mylogger, $opts);
$mydb->beginTransaction();
$array1 = $mydb->select("SELECT 123");
$array2 = $mydb->query("SELECT 123");
assert($array1 === $array2);
$created = $mydb->command('CREATE TEMPORARY TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`myenum` enum ("e1","e2") NOT NULL DEFAULT "e1",
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4');
assert(true === $created);
$insertedStringId = $mydb->insert("INSERT INTO users (id, name) VALUES (10, 'user10')");
assert('10' === $insertedStringId);
$insertedStringId = $mydb->insertOne(['id' => 20, 'name' => 'user20'], 'users');
assert('20' === $insertedStringId);
$statement = $mydb->prepare('INSERT INTO users (id, name) VALUES (?, ?)');
$statement->bind([25, 'user25']);
assert(true === $statement->execute());
assert('25' === (string) $statement->getInsertId());
$statement->close();
assert([['id' => 25, 'name' => 'user25']] === $mydb->execute('SELECT id, name FROM users WHERE id = ?', [25]));
$mydb->insertMany([[30, 'user30'], [40, 'user40']], ['id', 'name'], 'users');
assert(['10', '20', '25', '30', '40'] === array_column($mydb->query("SELECT id, name FROM users ORDER BY id ASC"), 'id'));
$deletedRowsCount = $mydb->delete('DELETE FROM users WHERE id = 40');
assert(1 === $deletedRowsCount);
assert(['10', '20', '25', '30'] === array_column($mydb->select("SELECT id, name FROM users ORDER BY id ASC"), 'id'));
$enum = $mydb->getEnumValues('users', 'myenum');
assert(['e1', 'e2'] === $enum);
$prim = $mydb->getPrimaryKeys('users');
assert(['id'] === $prim);
$deletedRowsCount = $mydb->deleteWhere(['id' => '30'], 'users');
assert(1 === $deletedRowsCount);
assert(['10', '20', '25'] === array_column($mydb->select("SELECT id, name FROM users ORDER BY id ASC"), 'id'));
assert(1 === $mydb->updateWhere(['id' => 99], ['id' => 10], 'users'));
assert(['20', '25', '99'] === array_column($mydb->select("SELECT id, name FROM users ORDER BY id ASC"), 'id'));
$db10 = $factory->create($auth, $mylogger, $opts);
$db10->open();
$db20 = $factory->create($auth, $mylogger, $opts);
$registry['db1'] = $mydb;
$registry['db2'] = $db10;
$registry['db3'] = $db20;
$db10->close();
$db20->close();
$userRepo = new UserRepository($registry);
$user20 = $userRepo->findById(20);
assert('user20' === $user20[0]['name']);
$mydb->rollbackTransaction();
echo 'OK' . PHP_EOL;
exit(0);
// @codeCoverageIgnoreEnd