1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-library package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Library\Core\Database\Connector; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use PDO; |
16
|
|
|
use WBW\Library\Core\Security\Authenticator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Microsoft Access database connector. |
20
|
|
|
* |
21
|
|
|
* @author webeweb <https://github.com/webeweb/> |
22
|
|
|
* @package WBW\Library\Core\Database\Connector |
23
|
|
|
*/ |
24
|
|
|
class MicrosoftAccessDatabaseConnector extends AbstractDatabaseConnector { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Microsoft Access DSN. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
const DEFAULT_DSN = "odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=%DBQ%; UID=%UID%; PWD=%PWD%"; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
* |
36
|
|
|
* @param Authenticator $authenticator The authenticator. |
37
|
|
|
* @param string $database The database. |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Authenticator $authenticator, $database) { |
40
|
|
|
parent::__construct($authenticator); |
41
|
|
|
|
42
|
|
|
$this->setDatabase($database); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
protected function connect() { |
49
|
|
|
|
50
|
|
|
if (false === file_exists($this->getDatabase())) { |
51
|
|
|
throw new InvalidArgumentException(sprintf("The database \"%s\" was not found", $this->getDatabase())); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$searches = ["%DBQ%", "%UID%", "%PWD%"]; |
55
|
|
|
$replaces = [$this->getDatabase(), $this->getAuthenticator()->getPasswordAuthentication()->getUsername(), $this->getAuthenticator()->getPasswordAuthentication()->getPassword()]; |
56
|
|
|
|
57
|
|
|
$dsn = str_replace($searches, $replaces, self::DEFAULT_DSN); |
58
|
|
|
|
59
|
|
|
return new PDO($dsn); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|