-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connect.php
180 lines (149 loc) · 4.34 KB
/
Connect.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
<?php
namespace dc\yukon;
require_once(dirname(__FILE__).'\config.php');
// Database connection object.
interface iConnect
{
function get_config();
function get_connection(); // Return database connection resource.
function set_config(ConnectConfig $value); // Set config object.
function close_connection(); // Close current connection.
function open_connection(); // Attempt database connection.
}
// Database host connection manager.
class Connect implements iConnect
{
private
$connect = NULL, // Database connection resource.
$config = NULL; // Connection parameters object.
public function __construct(ConnectConfig $connect = NULL)
{
// Set connection parameters member. If no argument
// is provided, then create a blank connection
// parameter instance.
if($connect)
{
$this->set_config($connect);
}
else
{
$this->set_config(new ConnectConfig);
}
// Connect to database server.
$this->open_connection();
}
public function __destruct()
{
// Close DB connection.
// $this->close_connection();
}
// Accessors.
public function get_config()
{
return $this->config;
}
public function get_connection()
{
return $this->connect;
}
// Mutators
public function set_config(ConnectConfig $value)
{
$this->config = $value;
}
// Connect to database host. Returns connection.
public function open_connection()
{
$connect = NULL; // Database connection reference.
$db_cred = NULL; // Credentials array.
$config = $this->config;
$error = $config->get_error();
// Set up credential array.
$db_cred = array('Database' => $config->get_name(),
'UID' => $config->get_user(),
'PWD' => $config->get_password(),
'CharacterSet' => $config->get_charset());
try
{
// Can't connect if there's no host.
if(!$config->get_host())
{
$msg = EXCEPTION_MSG::CONNECT_OPEN_HOST;
$msg .= ', Host: '.$config->get_host();
$msg .= ', DB: '.$config->get_name();
$error->exception_throw(new Exception($msg, EXCEPTION_CODE::CONNECT_OPEN_HOST));
}
// Establish database connection.
//$connect = sqlsrv_connect($config->get_host(), $db_cred);
// PDO requires a single concatenated string
// containing the type (MYSQL, MSSQL, etc.),
// hostname, and database name.
$dsn = 'sqlsrv:Server='.$config->get_host().';Database='.$config->get_name();
$connect = new \PDO($dsn, $config->get_user(), $config->get_password());
// False returned. Database connection has failed.
if(!$connect)
{
$error->exception_throw(new Exception(EXCEPTION_MSG::CONNECT_OPEN_FAIL, EXCEPTION_CODE::CONNECT_OPEN_FAIL));
}
}
catch (\PDOException $pdo_exception)
{
// PDO always throws an exception on connect fail.
// Let's just catch it here since sending it
// on to our custom exception handler isn't really
// tennable at the moment. Hopefully fix that soon. :)
echo('<br />');
echo('<b>'.LIBRARY::NAME.' error code '.EXCEPTION_CODE::CONNECT_OPEN_FAIL.": </b>");
echo(EXCEPTION_MSG::CONNECT_OPEN_FAIL);
echo('<br />');
error_log($pdo_exception->getMessage());
}
catch (Exception $exception)
{
// Send to catch.
$error->exception_catch();
}
// Set connect data
$this->connect = $connect;
return $connect;
}
// Close database connection and returns TRUE, or
// return FALSE if connection does not exist.
public function close_connection()
{
/*
$result = FALSE; // Connection present and closed?
$connect = $this->connect; // Database connection.
$config = $this->config;
$error = $config->get_error();
try
{
// Can't close if there is no connection.
if(!$connect)
{
$error->exception_throw(new Exception(EXCEPTION_MSG::CONNECT_CLOSE_CONNECTION, EXCEPTION_CODE::CONNECT_CLOSE_CONNECTION));
}
// Close database connection.
$result = sqlsrv_close($connect);
// Verify we were able to disconnect, else throw exception.
if($result)
{
// Clean connection member.
$this->connect = NULL;
}
else
{
$error->exception_throw(new Exception(EXCEPTION_MSG::CONNECT_CLOSE_FAIL, EXCEPTION_CODE::CONNECT_CLOSE_FAIL));
}
}
catch (Exception $exception)
{
// Send to application catch.
$error->exception_catch();
}
return $result;
*/
return TRUE;
}
}
?>