|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Jasper report integration for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://github.com/belgattitude/soluble-jasper |
|
9
|
|
|
* @author Vanvelthem Sébastien |
|
10
|
|
|
* @copyright Copyright (c) 2017-2019 Vanvelthem Sébastien |
|
11
|
|
|
* @license MIT |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Soluble\Jasper\DataSource\Util; |
|
15
|
|
|
|
|
16
|
|
|
use Soluble\Jasper\Exception\InvalidArgumentException; |
|
17
|
|
|
|
|
18
|
|
|
class JdbcDsnFactory |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Return a JDBC DSN formatted string from options. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $driver driver name (mysql/oracle/postgres...) |
|
24
|
|
|
* @param string $db database name |
|
25
|
|
|
* @param string $host server ip or hostname (i.e localhost) |
|
26
|
|
|
* @param string $user username to connect |
|
27
|
|
|
* @param string $password password to connect |
|
28
|
|
|
* @param string[] $driverOptions extra options as an associative array (i.e serverTimezone...) |
|
29
|
|
|
* |
|
30
|
|
|
* @return string i.e: "jdbc:[driver]://localhost/[database]?user=[user]&password=[password]&serverTimezone=UTC"; |
|
31
|
|
|
*/ |
|
32
|
3 |
|
public static function createDsn( |
|
33
|
|
|
string $driver, |
|
34
|
|
|
string $db, |
|
35
|
|
|
string $host, |
|
36
|
|
|
string $user, |
|
37
|
|
|
string $password, |
|
38
|
|
|
array $driverOptions = [] |
|
39
|
|
|
): string { |
|
40
|
3 |
|
$extras = ''; |
|
41
|
3 |
|
if (count($driverOptions) > 0) { |
|
42
|
2 |
|
$tmp = []; |
|
43
|
2 |
|
foreach ($driverOptions as $key => $value) { |
|
44
|
2 |
|
$tmp[] = urlencode($key) . '=' . urlencode($value); |
|
45
|
|
|
} |
|
46
|
2 |
|
$extras = '&' . implode('&', $tmp); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
return sprintf( |
|
50
|
3 |
|
'jdbc:%s://%s/%s?user=%s&password=%s%s', |
|
51
|
|
|
$driver, |
|
52
|
|
|
$host, |
|
53
|
|
|
$db, |
|
54
|
|
|
$user, |
|
55
|
|
|
$password, |
|
56
|
|
|
$extras |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Return a JDBC DSN formatted string from options. |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $params associative array with ['driver', 'db', 'host', 'user', 'password'] and optionally ['driverOptions'] as array |
|
64
|
|
|
* |
|
65
|
|
|
* @return string i.e: "jdbc:[driver]://localhost/[database]?user=[user]&password=[password]&serverTimezone=UTC"; |
|
66
|
|
|
*/ |
|
67
|
5 |
|
public static function createDsnFromParams(array $params): string |
|
68
|
|
|
{ |
|
69
|
5 |
|
$driver = $params['driver'] ?? null; |
|
70
|
5 |
|
if ($driver === null) { |
|
71
|
1 |
|
throw new InvalidArgumentException('Missing required "driver" option.'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
4 |
|
$db = $params['db'] ?? null; |
|
75
|
4 |
|
if ($db === null) { |
|
76
|
1 |
|
throw new InvalidArgumentException('Missing required "db" option.'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
3 |
|
$host = $params['host'] ?? null; |
|
80
|
3 |
|
if ($host === null) { |
|
81
|
1 |
|
throw new InvalidArgumentException('Missing required "host" option.'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// use and password can be optional for sqlite |
|
85
|
2 |
|
$user = $params['user'] ?? ''; |
|
86
|
2 |
|
$password = $params['password'] ?? ''; // can be cleartext |
|
87
|
|
|
|
|
88
|
2 |
|
$driverOptions = $params['driverOptions'] ?? []; |
|
89
|
|
|
|
|
90
|
2 |
|
if (!is_array($driverOptions)) { |
|
91
|
1 |
|
throw new InvalidArgumentException('Invalid type, "driverOptions" must be an array.'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
return self::createDsn($driver, $db, $host, $user, $password, $driverOptions); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|