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\Internationalization; |
8
|
|
|
use Doctrine\DBAL\Connection; |
9
|
|
|
use Doctrine\DBAL\DriverManager; |
10
|
|
|
use Exception; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Connector |
14
|
|
|
* |
15
|
|
|
* @package Coco\SourceWatcher\Core\Database\Connections |
16
|
|
|
*/ |
17
|
|
|
abstract class Connector |
18
|
|
|
{ |
19
|
|
|
protected string $driver = ""; |
20
|
|
|
|
21
|
|
|
protected array $connectionParameters = []; |
22
|
|
|
|
23
|
|
|
protected string $user = ""; |
24
|
|
|
|
25
|
|
|
protected string $password = ""; |
26
|
|
|
|
27
|
|
|
protected string $tableName = ""; |
28
|
|
|
|
29
|
|
|
public function getDriver () : string |
30
|
|
|
{ |
31
|
|
|
return $this->driver; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected abstract function getConnectionParameters () : array; |
35
|
|
|
|
36
|
|
|
public function getUser () : string |
37
|
|
|
{ |
38
|
|
|
return $this->user; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setUser ( string $user ) : void |
42
|
|
|
{ |
43
|
|
|
$this->user = $user; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getPassword () : string |
47
|
|
|
{ |
48
|
|
|
return $this->password; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setPassword ( string $password ) : void |
52
|
|
|
{ |
53
|
|
|
$this->password = $password; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getTableName () : string |
57
|
|
|
{ |
58
|
|
|
return $this->tableName; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setTableName ( string $tableName ) : void |
62
|
|
|
{ |
63
|
|
|
$this->tableName = $tableName; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return Connection |
68
|
|
|
* @throws Exception |
69
|
|
|
*/ |
70
|
|
|
public function getConnection () : Connection |
71
|
|
|
{ |
72
|
|
|
return DriverManager::getConnection( $this->getConnectionParameters() ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Row $row |
77
|
|
|
* @return int |
78
|
|
|
* @throws SourceWatcherException |
79
|
|
|
*/ |
80
|
|
|
public function insert ( Row $row ) : int |
81
|
|
|
{ |
82
|
|
|
if ( $this->tableName == null || $this->tableName == "" ) { |
83
|
|
|
throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class, |
84
|
|
|
"No_Table_Name_Found" ) ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
try { |
88
|
|
|
$connection = $this->getConnection(); |
89
|
|
|
|
90
|
|
|
if ( !$connection->isConnected() ) { |
91
|
|
|
$connection->connect(); |
92
|
|
|
} |
93
|
|
|
} catch ( Exception $exception ) { |
94
|
|
|
throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class, |
95
|
|
|
"Connection_Object_Not_Connected_Cannot_Insert" ), 0, $exception ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
try { |
99
|
|
|
$numberOfAffectedRows = $connection->insert( $this->tableName, $row->getAttributes() ); |
100
|
|
|
|
101
|
|
|
$connection->close(); |
102
|
|
|
} catch ( Exception $exception ) { |
103
|
|
|
throw new SourceWatcherException( Internationalization::getInstance()->getText( Connector::class, |
104
|
|
|
"Unexpected_Error" ), 0, $exception ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $numberOfAffectedRows; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $query |
112
|
|
|
* @return array |
113
|
|
|
* @throws SourceWatcherException |
114
|
|
|
*/ |
115
|
|
|
public function executePlainQuery ( string $query ) : array |
116
|
|
|
{ |
117
|
|
|
try { |
118
|
|
|
$connection = $this->getConnection(); |
119
|
|
|
|
120
|
|
|
if ( !$connection->isConnected() ) { |
121
|
|
|
$connection->connect(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$statement = $connection->executeQuery( $query ); |
125
|
|
|
|
126
|
|
|
return $statement->fetchAllAssociative(); |
|
|
|
|
127
|
|
|
} catch ( \Doctrine\DBAL\Driver\Exception $e ) { |
128
|
|
|
throw new SourceWatcherException( "Something went wrong: " . $e->getMessage(), 0, $e ); |
129
|
|
|
} catch ( Exception $e ) { |
130
|
|
|
throw new SourceWatcherException( "Something unexpected went wrong: " . $e->getMessage(), 0, $e ); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.