1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Fakerino package. |
4
|
|
|
* |
5
|
|
|
* (c) Nicola Pietroluongo <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fakerino\Core\Database; |
12
|
|
|
|
13
|
|
|
use Doctrine\DBAL\ConnectionException; |
14
|
|
|
use Doctrine\DBAL\Types\Type; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class DoctrineLayer, |
18
|
|
|
* provides an interface for the Doctrine DBAL. |
19
|
|
|
* |
20
|
|
|
* @author Nicola Pietroluongo <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class DoctrineLayer implements DbInterface |
23
|
|
|
{ |
24
|
|
|
/* @var \Doctrine\DBAL\DriverManager */ |
25
|
|
|
public static $conn; |
26
|
|
|
|
27
|
|
|
private $tableName; |
28
|
|
|
private $columns; |
29
|
|
|
private $totalFields; |
30
|
|
|
private $databaseConfig; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array $databaseConfig |
34
|
|
|
*/ |
35
|
70 |
|
public function __construct($databaseConfig) |
36
|
|
|
{ |
37
|
70 |
|
$this->databaseConfig = $databaseConfig; |
38
|
70 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
25 |
|
public function connect() |
44
|
|
|
{ |
45
|
25 |
|
$config = new \Doctrine\DBAL\Configuration(); |
46
|
25 |
|
self::$conn = \Doctrine\DBAL\DriverManager::getConnection($this->databaseConfig, $config); |
|
|
|
|
47
|
|
|
|
48
|
25 |
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
25 |
|
public function setTable($tableName) |
55
|
|
|
{ |
56
|
25 |
|
$this->tableName = $tableName; |
57
|
25 |
|
$schemaManager = self::$conn->getSchemaManager(); |
|
|
|
|
58
|
25 |
|
self::$conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); |
|
|
|
|
59
|
|
|
|
60
|
25 |
|
$tableSchema = $schemaManager->listTableColumns($tableName); |
61
|
25 |
|
foreach ($tableSchema as $column) { |
62
|
25 |
|
$this->columns[] = $column; |
63
|
25 |
|
} |
64
|
25 |
|
$this->totalFields = count($this->columns); |
65
|
25 |
|
if (empty($tableSchema)) { |
66
|
1 |
|
throw new ConnectionException(sprintf('Error in database connection, please check the configuration parameter and the table name "%s"', $tableName)); |
67
|
|
|
} |
68
|
25 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
2 |
|
public function getTotalColumns() |
74
|
|
|
{ |
75
|
2 |
|
return $this->totalFields; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
2 |
|
public function getColumnType($num) |
82
|
|
|
{ |
83
|
2 |
|
$doctrineType = $this->columns[$num]->getType()->getName(); |
84
|
|
|
|
85
|
2 |
|
return $this->getFakeType($doctrineType); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
2 |
|
public function getColumnName($num) |
92
|
|
|
{ |
93
|
2 |
|
return $this->columns[$num]->getName(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
1 |
|
public function getColumnLength($num) |
100
|
|
|
{ |
101
|
1 |
|
return $this->columns[$num]->getLength(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
1 |
|
public function isColumnAutoincrement($num) |
108
|
|
|
{ |
109
|
1 |
|
return $this->columns[$num]->getAutoincrement(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
5 |
|
public function insert(DbRowEntity $rows) |
116
|
|
|
{ |
117
|
5 |
|
$queryBuilder = self::$conn->createQueryBuilder(); |
|
|
|
|
118
|
5 |
|
$sql = $queryBuilder->insert($this->tableName); |
119
|
5 |
|
$values = array(); |
120
|
5 |
|
$types = array(); |
121
|
5 |
|
$rowsElement = $rows->getFields(); |
122
|
5 |
|
foreach ($rowsElement as $field) { |
123
|
5 |
|
$sql->setValue($field->getName(), '?'); |
124
|
5 |
|
if ($this->isDate($field->getType())) { |
125
|
2 |
|
$values[] = new \DateTime($field->getValue()); |
126
|
2 |
|
} else { |
127
|
3 |
|
$values[] = $field->getValue(); |
128
|
|
|
} |
129
|
5 |
|
$types[] = $field->getType(); |
130
|
5 |
|
} |
131
|
5 |
|
self::$conn->executeQuery($sql, $values, $types); |
|
|
|
|
132
|
|
|
|
133
|
5 |
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $columnType |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
5 |
|
private function isDate($columnType) |
141
|
1 |
|
{ |
142
|
5 |
|
return in_array($columnType, array(Type::DATETIME, Type::DATETIMETZ, Type::DATE, Type::TIME)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
18 |
|
public function getFakeType($columnType) |
149
|
|
|
{ |
150
|
|
|
$fakeType = array( |
151
|
18 |
|
Type::BIGINT => 'integer', |
152
|
18 |
|
Type::BOOLEAN => 'boolean', |
153
|
18 |
|
Type::DATETIME => 'datetime', |
154
|
18 |
|
Type::DATETIMETZ => 'datetimetz', |
155
|
18 |
|
Type::DATE => 'date', |
156
|
18 |
|
Type::TIME => 'time', |
157
|
18 |
|
Type::DECIMAL => 'integer', |
158
|
18 |
|
Type::INTEGER => 'integer', |
159
|
18 |
|
Type::SMALLINT => 'integer', |
160
|
18 |
|
Type::STRING => 'string', |
161
|
18 |
|
Type::TEXT => 'text', |
162
|
18 |
|
Type::BLOB => 'text', |
163
|
18 |
|
Type::FLOAT => 'integer', |
164
|
18 |
|
Type::GUID => 'integer', |
165
|
18 |
|
Type::SIMPLE_ARRAY => 'string', |
166
|
18 |
|
Type::BINARY => 'string', |
167
|
18 |
|
); |
168
|
|
|
|
169
|
18 |
|
return $fakeType[strtolower($columnType)]; |
170
|
|
|
} |
171
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..