-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbconnect.impl.php
203 lines (151 loc) · 5.73 KB
/
dbconnect.impl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
include_once('dbconnect.const.php');
class DbConnectImpl {
private $servername;
private $username;
private $password;
private $dbname;
private $port;
private $charset;
private $querySql;
private $engine;
private $isUseSSL;
public function getQuerySql(){
return $this->querySql;
}
public function setQuerySql($querySql){
$this->querySql = $querySql;
}
public function IsUseSSL(){
return (bool) $this->isUseSSL;
}
public function setIsUseSSL($isUseSSL){
$this->isUseSSL = $isUseSSL;
}
public function getServername(){
return $this->servername;
}
public function setServername($servername){
$this->servername = $servername;
}
public function getUsername(){
return $this->username;
}
public function setUsername($username){
$this->username = $username;
}
public function getPassword(){
return $this->password;
}
public function setPassword($password){
$this->password = $password;
}
public function getDbname(){
return $this->dbname;
}
public function setDbname($dbname){
$this->dbname = $dbname;
}
public function getPort(){
return $this->port;
}
public function setPort($port){
$this->port = $port;
}
public function getEngine(){
return $this->engine;
}
public function setEngine($engine){
$this->engine = strtoupper($engine);
}
public function showResultQuerySql(){
try{
$resultInJson = DbConnectImpl::executeQuerySql();
/*Check for errors*/
if($resultInJson === FALSE){
echo 'JSON error! Error code.: ' .json_last_error(). '; Error Message.: "' .json_last_error_msg(). '".';
die();
}
print("<h2 style='text-align: center;'>:.showResultQuerySql().:</h2>");
print("<p><h3>Query Executed.:</h3> "."<h2>".$this->getQuerySql()."</h2></p>");
echo "<pre>";
print_r($resultInJson);
echo "</pre>";
}catch(PDOException $e){
echo "<h3 style='color: red;'>Show result query SQL failed: " . $e->getMessage()."</h3>";
die();
}
}
private function dbConnection() {
try {
$dbConnection = $this->validIsUseSSL();
// set the PDO error mode to exception
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConnection;
}catch(PDOException $e){
echo "<br><br>";
echo "<h3 style='color: red;'>Connection failed: " . $e->getMessage()."</h3>";
die();
}
}
private function validIsUseSSL(){
if($this->IsUseSSL() !== FALSE){
$dbConnection = new PDO(DbConnectImpl::getDns(), $this->getUsername(), $this->getPassword(), DbConnectImpl::getOptions());
}else{
$dbConnection = new PDO(DbConnectImpl::getDns(), $this->getUsername(), $this->getPassword());
}
return $dbConnection;
}
private function getOptions(){
if($this->getEngine() === DbConnectConst::ENGINE_MYSQL){
$options = array(
//PDO::MYSQL_ATTR_SSL_CA => '/var/www/html/test-connection/ca.pem',
PDO::MYSQL_ATTR_SSL_CA => DbConnectConst::SSL_CA_MYSQL,
//PDO::MYSQL_ATTR_SSL_CERT => '/var/www/html/test-connection/client-cert.pem',
//PDO::MYSQL_ATTR_SSL_KEY => '/var/www/html/test-connection/client-key.pem',
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
);
}else if($this->getEngine() === DbConnectConst::ENGINE_ORACLE){
$options = array();
}else if($this->getEngine() === DbConnectConst::ENGINE_PGSQL){
$options = array();
}else{
throw new PDOException(DbConnectConst::MESSAGE_ERROR_ENGINE . $this->getEngine());
}
return $options;
}
private function getDns(){
if($this->getEngine() === DbConnectConst::ENGINE_MYSQL){
$this->charset = DbConnectConst::CHARSET_MYSQL;
return "mysql:host=".$this->getServername().";port=".$this->getPort().";charset=".$this->charset.";dbname=".$this->getDbname();
}else if($this->getEngine() === DbConnectConst::ENGINE_ORACLE){
$tns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = ".$this->getServername().")(PORT = ".$this->getPort().")) (CONNECT_DATA = (SERVICE_NAME = ORCL) (SID = ORCL)))";
return "oci:dbname=".$tns;
}else if($this->getEngine() === DbConnectConst::ENGINE_PGSQL){
$this->charset = "--client_encoding=".DbConnectConst::CHARSET;
return "pgsql:host=".$this->getServername().";port=".$this->getPort().";options=".$this->charset.";dbname=".$this->getDbname();
}else{
throw new PDOException(DbConnectConst::MESSAGE_ERROR_ENGINE . $this->getEngine());
}
}
private function executeQuerySql(){
$sth = DbConnectImpl::dbConnection()->prepare($this->getQuerySql());
$sth->execute();
$result = DbConnectImpl::getResult();
$quantityObject=0;
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$quantityObject = $quantityObject+1;
$resultIndex = count($result);
$result[$resultIndex] = array(
$row
);
}
echo "<b>Quantitys Object.: ".$quantityObject."</b>";
$sth->closeCursor();
return json_encode($result, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
}
private function getResult(){
return array();
}
}
?>