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\DBALException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ClientServerDatabaseConnector |
12
|
|
|
* @package Coco\SourceWatcher\Core\Database\Connections |
13
|
|
|
*/ |
14
|
|
|
abstract class ClientServerDatabaseConnector extends Connector |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected string $host; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected int $port; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected string $dbName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getHost () : string |
35
|
|
|
{ |
36
|
|
|
return $this->host; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $host |
41
|
|
|
*/ |
42
|
|
|
public function setHost ( string $host ) : void |
43
|
|
|
{ |
44
|
|
|
$this->host = $host; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
|
|
public function getPort () : int |
51
|
|
|
{ |
52
|
|
|
return $this->port; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param int $port |
57
|
|
|
*/ |
58
|
|
|
public function setPort ( int $port ) : void |
59
|
|
|
{ |
60
|
|
|
$this->port = $port; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getDbName () : string |
67
|
|
|
{ |
68
|
|
|
return $this->dbName; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $dbName |
73
|
|
|
*/ |
74
|
|
|
public function setDbName ( string $dbName ) : void |
75
|
|
|
{ |
76
|
|
|
$this->dbName = $dbName; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Row $row |
81
|
|
|
* @return int |
82
|
|
|
* @throws SourceWatcherException |
83
|
|
|
*/ |
84
|
|
|
public function insert ( Row $row ) : int |
85
|
|
|
{ |
86
|
|
|
if ( $this->tableName == null || $this->tableName == "" ) { |
87
|
|
|
throw new SourceWatcherException( i18n::getInstance()->getText( ClientServerDatabaseConnector::class, "No_Table_Name_Found" ) ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$connection = $this->connect(); |
91
|
|
|
|
92
|
|
|
if ( !$connection->isConnected() ) { |
93
|
|
|
throw new SourceWatcherException( i18n::getInstance()->getText( ClientServerDatabaseConnector::class, "Connection_Object_Not_Connected_Cannot_Insert" ) ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
try { |
97
|
|
|
$numberOfAffectedRows = $connection->insert( $this->tableName, $row->getAttributes() ); |
98
|
|
|
|
99
|
|
|
$connection->close(); |
100
|
|
|
} catch ( DBALException $dbalException ) { |
101
|
|
|
$errorMessage = sprintf( "Something went wrong while trying to insert the row: %s", $dbalException->getMessage() ); |
102
|
|
|
throw new SourceWatcherException( $errorMessage ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $numberOfAffectedRows; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|