|
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; |
|
15
|
|
|
|
|
16
|
|
|
use Soluble\Japha\Bridge\Adapter as BridgeAdapter; |
|
17
|
|
|
use Soluble\Japha\Bridge\Exception\JavaException; |
|
18
|
|
|
use Soluble\Japha\Db\DriverManager; |
|
19
|
|
|
use Soluble\Japha\Interfaces\JavaObject; |
|
20
|
|
|
use Soluble\Jasper\DataSource\Contract\JavaSqlConnectionInterface; |
|
21
|
|
|
use Soluble\Jasper\Exception\JavaProxiedException; |
|
22
|
|
|
|
|
23
|
|
|
class JavaSqlConnection implements JavaSqlConnectionInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $dsn; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $driverClass; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* JDBCDataSource constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $jdbcDsn JDBC DSN, i.e.: "jdbc:mysql://localhost/database?user=X&password=Y&serverTimezone=UTC"; |
|
39
|
|
|
* @param string $driverClass Java driver class, i.e.: 'com.mysql.jdbc.Driver' (must be in classpath on the jvm side) |
|
40
|
|
|
*/ |
|
41
|
3 |
|
public function __construct(string $jdbcDsn, string $driverClass) |
|
42
|
|
|
{ |
|
43
|
3 |
|
$this->dsn = $jdbcDsn; |
|
44
|
3 |
|
$this->driverClass = $driverClass; |
|
45
|
3 |
|
} |
|
46
|
|
|
|
|
47
|
3 |
|
public function getDriverClass(): string |
|
48
|
|
|
{ |
|
49
|
3 |
|
return $this->driverClass; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
public function getJdbcDsn(): string |
|
53
|
|
|
{ |
|
54
|
3 |
|
return $this->dsn; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return JavaObject Java('java.sql.Connection') |
|
59
|
|
|
* |
|
60
|
|
|
* @throws JavaProxiedException when the sql connection cannot be initialized |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function getJasperReportSqlConnection(BridgeAdapter $bridgeAdapter): JavaObject |
|
63
|
|
|
{ |
|
64
|
|
|
try { |
|
65
|
2 |
|
$connection = (new DriverManager($bridgeAdapter))->createConnection( |
|
66
|
2 |
|
$this->getJdbcDsn(), |
|
67
|
2 |
|
$this->getDriverClass() |
|
68
|
|
|
); |
|
69
|
1 |
|
} catch (JavaException $e) { |
|
70
|
1 |
|
throw new JavaProxiedException( |
|
71
|
1 |
|
$e, |
|
72
|
1 |
|
sprintf( |
|
73
|
1 |
|
'Error getting sql connection: %s', |
|
74
|
1 |
|
$e->getMessage() |
|
75
|
|
|
) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
return $connection; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|