|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Soluble Japha |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://github.com/belgattitude/soluble-japha |
|
9
|
|
|
* @copyright Copyright (c) 2013-2020 Vanvelthem Sébastien |
|
10
|
|
|
* @license MIT License https://github.com/belgattitude/soluble-japha/blob/master/LICENSE.md |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Soluble\Japha\Db; |
|
14
|
|
|
|
|
15
|
|
|
use Soluble\Japha\Bridge; |
|
16
|
|
|
use Soluble\Japha\Bridge\Exception; |
|
17
|
|
|
use Soluble\Japha\Interfaces; |
|
18
|
|
|
|
|
19
|
|
|
class DriverManager |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Interfaces\JavaObject DriverManager object ('java.sql.DriverManager') |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $driverManager; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Bridge\Adapter |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $ba; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Bridge\Adapter $ba |
|
33
|
|
|
*/ |
|
34
|
11 |
|
public function __construct(Bridge\Adapter $ba) |
|
35
|
|
|
{ |
|
36
|
11 |
|
$this->ba = $ba; |
|
37
|
11 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create an sql connection to database. |
|
41
|
|
|
* |
|
42
|
|
|
* |
|
43
|
|
|
* @throws Exception\JavaException |
|
44
|
|
|
* @throws Exception\ClassNotFoundException |
|
45
|
|
|
* @throws Exception\InvalidArgumentException |
|
46
|
|
|
* @throws Exception\BrokenConnectionException |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $dsn |
|
49
|
|
|
* @param string $driverClass |
|
50
|
|
|
* |
|
51
|
|
|
* @return Interfaces\JavaObject Java('java.sql.Connection') |
|
52
|
|
|
*/ |
|
53
|
8 |
|
public function createConnection(string $dsn, string $driverClass = 'com.mysql.jdbc.Driver'): Interfaces\JavaObject |
|
54
|
|
|
{ |
|
55
|
8 |
|
if (!is_string($dsn) || trim($dsn) == '') { |
|
|
|
|
|
|
56
|
1 |
|
$message = 'DSN param must be a valid (on-empty) string'; |
|
57
|
1 |
|
throw new Exception\InvalidArgumentException(__METHOD__.' '.$message); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
7 |
|
$class = $this->ba->javaClass('java.lang.Class'); |
|
61
|
|
|
try { |
|
62
|
7 |
|
$class->forName($driverClass); |
|
|
|
|
|
|
63
|
1 |
|
} catch (Exception\JavaException $e) { |
|
64
|
1 |
|
throw $e; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
6 |
|
$conn = $this->getDriverManager()->getConnection($dsn); |
|
|
|
|
|
|
69
|
1 |
|
} catch (Exception\JavaExceptionInterface $e) { |
|
70
|
1 |
|
throw $e; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
return $conn; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Return underlying java driver manager. |
|
78
|
|
|
* |
|
79
|
|
|
* @return Interfaces\JavaObject Java('java.sql.DriverManager') |
|
80
|
|
|
*/ |
|
81
|
7 |
|
public function getDriverManager(): Interfaces\JavaObject |
|
82
|
|
|
{ |
|
83
|
7 |
|
if ($this->driverManager === null) { |
|
84
|
7 |
|
$this->driverManager = $this->ba->javaClass('java.sql.DriverManager'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
7 |
|
return $this->driverManager; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Return a JDBC DSN formatted string from options. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $driver driver name (mysql/mariadb/oracle/postgres...) |
|
94
|
|
|
* @param string $db database name |
|
95
|
|
|
* @param string $host server ip or name |
|
96
|
|
|
* @param string $user username to connect |
|
97
|
|
|
* @param string $password password to connect |
|
98
|
|
|
* @param array $options extra options as an associative array |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
2 |
|
public static function getJdbcDsn(string $driver, string $db, string $host, string $user, string $password, array $options = []): string |
|
103
|
|
|
{ |
|
104
|
2 |
|
$extras = ''; |
|
105
|
2 |
|
if (count($options) > 0) { |
|
106
|
1 |
|
$tmp = []; |
|
107
|
1 |
|
foreach ($options as $key => $value) { |
|
108
|
1 |
|
$tmp[] = urlencode($key).'='.urlencode($value); |
|
109
|
|
|
} |
|
110
|
1 |
|
$extras = '&'.implode('&', $tmp); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
return "jdbc:$driver://$host/$db?user=$user&password=$password".$extras; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|