1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Gerrie package. |
4
|
|
|
* |
5
|
|
|
* (c) Andreas Grunwald <[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 Gerrie\Service; |
12
|
|
|
|
13
|
|
|
use Gerrie\Component\Database\Database; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
class DatabaseService |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
21
|
|
|
*/ |
22
|
|
|
protected $logger = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Gerrie\Component\Database\Database |
26
|
|
|
*/ |
27
|
|
|
protected $database = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor |
31
|
|
|
* |
32
|
|
|
* @param Database $database |
33
|
|
|
* @param OutputInterface $output |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Database $database, OutputInterface $output) |
36
|
|
|
{ |
37
|
|
|
$this->database = $database; |
38
|
|
|
$this->logger = $output; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Creates the necessary database tables, if they are not exists. |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
|
|
public function setupDatabaseTables() |
48
|
|
|
{ |
49
|
|
|
$logger = $this->getLogger(); |
50
|
|
|
$database = $this->getDatabase(); |
51
|
|
|
|
52
|
|
|
$databaseHandle = $database->getDatabaseConnection(); |
53
|
|
|
$tableDefinition = $database->getTableDefinition(); |
54
|
|
|
|
55
|
|
|
$tables = array_keys($tableDefinition); |
56
|
|
|
foreach ($tables as $tableName) { |
57
|
|
|
$logger->writeln('<info>Table "' . $tableName . '"</info>'); |
58
|
|
|
|
59
|
|
|
$statement = $databaseHandle->prepare('SHOW TABLES LIKE :table'); |
60
|
|
|
$statement->bindParam(':table', $tableName, \PDO::PARAM_STR); |
61
|
|
|
$statement->execute(); |
62
|
|
|
|
63
|
|
|
if ($statement->rowCount() == 1) { |
64
|
|
|
$logger->writeln('<info>=> Exists. Skip it</info>'); |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Table does not exists. Try to create it |
69
|
|
|
$createTableResult = $databaseHandle->query($tableDefinition[$tableName]); |
70
|
|
|
|
71
|
|
|
if ($createTableResult === false) { |
72
|
|
|
$databaseError = $databaseHandle->errorInfo(); |
73
|
|
|
$message = 'Table "%s" could not be created. %s (%s)'; |
74
|
|
|
$message = sprintf($message, $tableName, $databaseError[2], $databaseError[1]); |
75
|
|
|
throw new \Exception($message, 1398100879); |
76
|
|
|
|
77
|
|
|
} else { |
78
|
|
|
$logger->writeln('<info>Not exists. Created</info>'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the database object |
87
|
|
|
* |
88
|
|
|
* @return Database |
89
|
|
|
*/ |
90
|
|
|
public function getDatabase() |
91
|
|
|
{ |
92
|
|
|
return $this->database; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the logger object |
97
|
|
|
* |
98
|
|
|
* @return OutputInterface |
99
|
|
|
*/ |
100
|
|
|
public function getLogger() |
101
|
|
|
{ |
102
|
|
|
return $this->logger; |
103
|
|
|
} |
104
|
|
|
} |