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 Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\DBALException; |
9
|
|
|
use Doctrine\DBAL\DriverManager; |
10
|
|
|
use Exception; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class SqliteConnector |
14
|
|
|
* @package Coco\SourceWatcher\Core\Database\Connections |
15
|
|
|
* |
16
|
|
|
* Following the definition from: https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#pdo-sqlite |
17
|
|
|
*/ |
18
|
|
|
class SqliteConnector extends EmbeddedDatabaseConnector |
19
|
|
|
{ |
20
|
|
|
protected string $path; |
21
|
|
|
protected bool $memory; |
22
|
|
|
|
23
|
|
|
public function __construct () |
24
|
|
|
{ |
25
|
|
|
$this->driver = "pdo_sqlite"; |
26
|
|
|
|
27
|
|
|
$this->memory = false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getPath () : string |
34
|
|
|
{ |
35
|
|
|
return $this->path; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $path |
40
|
|
|
*/ |
41
|
|
|
public function setPath ( string $path ) : void |
42
|
|
|
{ |
43
|
|
|
$this->path = $path; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function isMemory () : bool |
50
|
|
|
{ |
51
|
|
|
return $this->memory; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param bool $memory |
56
|
|
|
*/ |
57
|
|
|
public function setMemory ( bool $memory ) : void |
58
|
|
|
{ |
59
|
|
|
$this->memory = $memory; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function connect () : Connection |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
|
|
return DriverManager::getConnection( $this->getConnectionParameters() ); |
66
|
|
|
} catch ( DBALException $dbalException ) { |
67
|
|
|
throw new SourceWatcherException( "Something went wrong trying to get a connection: " . $dbalException->getMessage() ); |
68
|
|
|
} catch ( Exception $exception ) { |
69
|
|
|
throw new SourceWatcherException( "Something unexpected went wrong trying to get a connection: " . $exception->getMessage() ); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function getConnectionParameters () : array |
74
|
|
|
{ |
75
|
|
|
$this->connectionParameters = array(); |
76
|
|
|
|
77
|
|
|
$this->connectionParameters["driver"] = $this->driver; |
78
|
|
|
$this->connectionParameters["user"] = $this->user; |
79
|
|
|
$this->connectionParameters["password"] = $this->password; |
80
|
|
|
|
81
|
|
|
if ( isset( $this->path ) && $this->path !== "" ) { |
82
|
|
|
$this->connectionParameters["path"] = $this->path; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->connectionParameters["memory"] = $this->memory; |
86
|
|
|
|
87
|
|
|
return $this->connectionParameters; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function insert ( Row $row ) : void |
91
|
|
|
{ |
92
|
|
|
if ( $this->tableName == null || $this->tableName == "" ) { |
93
|
|
|
throw new SourceWatcherException( "No table name found." ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$connection = $this->connect(); |
97
|
|
|
|
98
|
|
|
$numberOfAffectedRows = $connection->insert( $this->tableName, $row->getAttributes() ); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|