|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mindplay\sql\framework; |
|
4
|
|
|
|
|
5
|
|
|
use mindplay\sql\model\DeleteQuery; |
|
6
|
|
|
use mindplay\sql\model\InsertQuery; |
|
7
|
|
|
use mindplay\sql\model\Schema; |
|
8
|
|
|
use mindplay\sql\model\SelectQuery; |
|
9
|
|
|
use mindplay\sql\model\SQLQuery; |
|
10
|
|
|
use mindplay\sql\model\Table; |
|
11
|
|
|
use mindplay\sql\model\UpdateQuery; |
|
12
|
|
|
use UnexpectedValueException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This class implements the primary public API of the database model. |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class Database |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var DatabaseContainer |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $container; |
|
23
|
|
|
|
|
24
|
1 |
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
1 |
|
$this->container = new DatabaseContainer(); |
|
27
|
|
|
|
|
28
|
1 |
|
$this->container->set(Driver::class, $this->createDriver()); |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return Driver |
|
33
|
|
|
*/ |
|
34
|
|
|
abstract protected function createDriver(); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string Schema class-name |
|
38
|
|
|
* |
|
39
|
|
|
* @return Schema |
|
40
|
|
|
*/ |
|
41
|
1 |
View Code Duplication |
public function getSchema($schema) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
1 |
|
if (! $this->container->has($schema)) { |
|
44
|
1 |
|
$this->container->register($schema); // auto-wiring (for Schema with no special constructor dependencies) |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
$schema = $this->container->get($schema); |
|
48
|
|
|
|
|
49
|
1 |
|
if (! $schema instanceof Schema) { |
|
50
|
|
|
$class_name = get_class($schema); |
|
51
|
|
|
|
|
52
|
|
|
throw new UnexpectedValueException("{$class_name} does not extend the Schema class"); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
return $schema; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Table $from |
|
60
|
|
|
* |
|
61
|
|
|
* @return SelectQuery |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function select(Table $from) |
|
64
|
|
|
{ |
|
65
|
1 |
|
return $this->container->create(SelectQuery::class, ['root' => $from]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param Table $into |
|
70
|
|
|
* |
|
71
|
|
|
* @return InsertQuery |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function insert(Table $into) |
|
74
|
|
|
{ |
|
75
|
1 |
|
return $this->container->create(InsertQuery::class, ['table' => $into]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param Table $table |
|
80
|
|
|
* |
|
81
|
|
|
* @return UpdateQuery |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function update(Table $table) |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->container->create(UpdateQuery::class, ['root' => $table]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param Table $table |
|
90
|
|
|
* |
|
91
|
|
|
* @return DeleteQuery |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function delete(Table $table) |
|
94
|
|
|
{ |
|
95
|
1 |
|
return $this->container->create(DeleteQuery::class, ['root' => $table]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $sql |
|
100
|
|
|
* |
|
101
|
|
|
* @return SQLQuery |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function sql($sql) |
|
104
|
|
|
{ |
|
105
|
1 |
|
return $this->container->create(SQLQuery::class, ['sql' => $sql]); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.