1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core\Database\Connections; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\Row; |
6
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
7
|
|
|
use Coco\SourceWatcher\Utils\i18n; |
8
|
|
|
use Doctrine\DBAL\Connection; |
9
|
|
|
use Doctrine\DBAL\DBALException; |
10
|
|
|
use Doctrine\DBAL\DriverManager; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Connector |
14
|
|
|
* @package Coco\SourceWatcher\Core\Database\Connections |
15
|
|
|
*/ |
16
|
|
|
abstract class Connector |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected string $driver = ""; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected array $connectionParameters = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected string $user = ""; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected string $password = ""; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected string $tableName = ""; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getDriver () : string |
47
|
|
|
{ |
48
|
|
|
return $this->driver; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
protected abstract function getConnectionParameters () : array; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getUser () : string |
60
|
|
|
{ |
61
|
|
|
return $this->user; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $user |
66
|
|
|
*/ |
67
|
|
|
public function setUser ( string $user ) : void |
68
|
|
|
{ |
69
|
|
|
$this->user = $user; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getPassword () : string |
76
|
|
|
{ |
77
|
|
|
return $this->password; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $password |
82
|
|
|
*/ |
83
|
|
|
public function setPassword ( string $password ) : void |
84
|
|
|
{ |
85
|
|
|
$this->password = $password; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getTableName () : string |
92
|
|
|
{ |
93
|
|
|
return $this->tableName; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $tableName |
98
|
|
|
*/ |
99
|
|
|
public function setTableName ( string $tableName ) : void |
100
|
|
|
{ |
101
|
|
|
$this->tableName = $tableName; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Connection |
106
|
|
|
* @throws SourceWatcherException |
107
|
|
|
*/ |
108
|
|
|
public function getConnection () : Connection |
109
|
|
|
{ |
110
|
|
|
try { |
111
|
|
|
return DriverManager::getConnection( $this->getConnectionParameters() ); |
112
|
|
|
} catch ( DBALException $dbalException ) { |
113
|
|
|
throw new SourceWatcherException( "Something went wrong trying to get a connection: ", 0, $dbalException->getMessage() ); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Row $row |
119
|
|
|
* @return int |
120
|
|
|
* @throws SourceWatcherException |
121
|
|
|
*/ |
122
|
|
|
public function insert ( Row $row ) : int |
123
|
|
|
{ |
124
|
|
|
if ( $this->tableName == null || $this->tableName == "" ) { |
125
|
|
|
throw new SourceWatcherException( i18n::getInstance()->getText( Connector::class, "No_Table_Name_Found" ) ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$connection = $this->getConnection(); |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
if ( !$connection->isConnected() ) { |
132
|
|
|
$connection->connect(); |
133
|
|
|
} |
134
|
|
|
} catch ( DBALException $dbalException ) { |
135
|
|
|
throw new SourceWatcherException( i18n::getInstance()->getText( Connector::class, "Connection_Object_Not_Connected_Cannot_Insert" ), 0, $dbalException ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
try { |
139
|
|
|
$numberOfAffectedRows = $connection->insert( $this->tableName, $row->getAttributes() ); |
140
|
|
|
|
141
|
|
|
$connection->close(); |
142
|
|
|
} catch ( DBALException $dbalException ) { |
143
|
|
|
throw new SourceWatcherException( i18n::getInstance()->getText( Connector::class, "Unexpected_Error" ), 0, $dbalException ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $numberOfAffectedRows; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|