1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mindplay\sql\mysql; |
4
|
|
|
|
5
|
|
|
use mindplay\sql\model\Database; |
6
|
|
|
use mindplay\sql\model\DatabaseContainer; |
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(DatabaseContainer $container) |
19
|
|
|
{ |
20
|
1 |
|
$container->set(Driver::class, $this); |
21
|
|
|
|
22
|
1 |
|
$container->register(BoolType::class, function () { |
23
|
1 |
|
return BoolType::get(1, 0); |
24
|
1 |
|
}); |
25
|
|
|
|
26
|
1 |
|
$container->alias("scalar.boolean", BoolType::class); |
27
|
|
|
|
28
|
1 |
|
$container->register("scalar.double", FloatType::class); |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param PDO $pdo |
33
|
|
|
* |
34
|
|
|
* @return MySQLConnection |
35
|
|
|
*/ |
36
|
1 |
|
public function createConnection(PDO $pdo) |
37
|
|
|
{ |
38
|
1 |
|
return $this->container->create(MySQLConnection::class, ['pdo' => $pdo]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
1 |
|
public function quoteName($name) |
45
|
|
|
{ |
46
|
1 |
|
return '`' . $name . '`'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Table $from |
51
|
|
|
* |
52
|
|
|
* @return MySQLSelectQuery |
53
|
|
|
*/ |
54
|
1 |
|
public function select(Table $from) |
55
|
|
|
{ |
56
|
1 |
|
return $this->container->create(MySQLSelectQuery::class, ['root' => $from]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Table $into |
61
|
|
|
* |
62
|
|
|
* @return InsertQuery |
63
|
|
|
*/ |
64
|
1 |
|
public function insert(Table $into) |
65
|
|
|
{ |
66
|
1 |
|
return $this->container->create(InsertQuery::class, ['table' => $into]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Table $table |
71
|
|
|
* |
72
|
|
|
* @return UpdateQuery |
73
|
|
|
*/ |
74
|
1 |
|
public function update(Table $table) |
75
|
|
|
{ |
76
|
1 |
|
return $this->container->create(UpdateQuery::class, ['root' => $table]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Table $table |
81
|
|
|
* |
82
|
|
|
* @return DeleteQuery |
83
|
|
|
*/ |
84
|
1 |
|
public function delete(Table $table) |
85
|
|
|
{ |
86
|
1 |
|
return $this->container->create(DeleteQuery::class, ['root' => $table]); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|