|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mindplay\sql\mysql; |
|
4
|
|
|
|
|
5
|
|
|
use mindplay\sql\model\Database; |
|
6
|
|
|
use mindplay\sql\model\DatabaseContainerFactory; |
|
7
|
|
|
use mindplay\sql\model\Driver; |
|
8
|
|
|
use mindplay\sql\model\query\DeleteQuery; |
|
9
|
|
|
use mindplay\sql\model\query\InsertQuery; |
|
10
|
|
|
use mindplay\sql\model\query\UpdateQuery; |
|
11
|
|
|
use mindplay\sql\model\schema\Table; |
|
12
|
|
|
use mindplay\sql\model\types\BoolType; |
|
13
|
|
|
use mindplay\sql\model\types\FloatType; |
|
14
|
|
|
use PDO; |
|
15
|
|
|
|
|
16
|
|
|
class MySQLDatabase extends Database implements Driver |
|
17
|
|
|
{ |
|
18
|
1 |
|
protected function bootstrap(DatabaseContainerFactory $factory): void |
|
19
|
|
|
{ |
|
20
|
1 |
|
$factory->set(Driver::class, $this); |
|
21
|
|
|
|
|
22
|
1 |
|
$factory->register(BoolType::class, function () { |
|
23
|
1 |
|
return BoolType::get(1, 0); |
|
24
|
1 |
|
}); |
|
25
|
|
|
|
|
26
|
1 |
|
$factory->alias("scalar.boolean", BoolType::class); |
|
27
|
|
|
|
|
28
|
1 |
|
$factory->register("scalar.double", FloatType::class); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function createConnection(PDO $pdo): MySQLConnection |
|
32
|
|
|
{ |
|
33
|
1 |
|
return $this->container->create(MySQLConnection::class, ['pdo' => $pdo]); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritdoc |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function quoteName(string $name): string |
|
40
|
|
|
{ |
|
41
|
1 |
|
return '`' . $name . '`'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritdoc |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function quoteTableName(string|null $schema, string $table): string |
|
48
|
|
|
{ |
|
49
|
1 |
|
return $schema |
|
50
|
1 |
|
? "`{$schema}_{$table}`" |
|
51
|
1 |
|
: "`{$table}`"; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
public function select(Table $from): MySQLSelectQuery |
|
55
|
|
|
{ |
|
56
|
1 |
|
return $this->container->create(MySQLSelectQuery::class, [Table::class => $from]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function insert(Table $into): InsertQuery |
|
60
|
|
|
{ |
|
61
|
1 |
|
return $this->container->create(InsertQuery::class, [Table::class => $into]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
public function update(Table $table): UpdateQuery |
|
65
|
|
|
{ |
|
66
|
1 |
|
return $this->container->create(MySQLUpdateQuery::class, [Table::class => $table]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
public function delete(Table $table): DeleteQuery |
|
70
|
|
|
{ |
|
71
|
1 |
|
return $this->container->create(MySQLDeleteQuery::class, [Table::class => $table]); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|