-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSN.class.php
More file actions
274 lines (243 loc) · 6.42 KB
/
DSN.class.php
File metadata and controls
274 lines (243 loc) · 6.42 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
require_once( dirname( __FILE__ ) . "/../funcset/DSN.class.php" );
if ( 0 ) {
/**
* Parse DSN (Data source name) and provice methods for reading received DSN
*
* DSN object provides universal way to describe connect string for connecting to data source.
*
* for client-server DBMS DSN uses as :
* dbmsengine://server/database - connect to 'database' w/o username and password
* dbmsengine://username:password@server/database - connect to 'database' on server with specified usernmae and password
* dbmsengine://server/database/table - connect to 'database' and select some table (URI to concretee table)
*
* for non-server uses as :
* dbmsengine://localhost/?database=local/path/to/database.db
* - open 'database.db' located in 'local/path/to/'
*
* dbmsengine://localhost/?database=local/path/to/database.db&table=t_table
* - open database.db,
* - located in local/path/to/
* - select concrete table 't_table'
*
*
* The main idea in presentation DSN as URI ,
* you can use URI notation for locating any database object in your system,
* with using additional arguments for DBMS you can extends syntax.
*
* dbms://server/database/table/primary_key/pkvalue&connectparam=value
*
* Notice: server query argument names: 'database', 'table'
*
* Examples:
*
* Task: URI to signle object by primary key (in table 't_table' select object with id=1):
*
* mysql://localhost/database/t_table?select='id=1'
* - URL via query
*
* mysql://localhost/database/t_table/id/1
* - URI as hierachy
*
* sqlite://localhost/?database=../local/database.db&table=t_table&&select='id=1'
* - DSN for non-server DBMS can't accept only URI beacause need describe path to DB file on filesystem.
*
* sqlite://localhost//t_table/id/1&database=../local/database.db
* - combined way: URI as hierarhy and database file name in query
*
*/
class DSN
{
var $params = array();
var $dsn = null;
var $isValid = false;
/**
* construct object
*
* @param string $dsn methods://user:password@server:port/database/table[query]
* @return DSN
*/
function DSN( $dsn)
{
$this->dsn = $dsn;
$this->isValid = $this->_parse( $dsn );
}
/**
* returns methos or null
*
* @return string
*/
function getMethod()
{
if ( !array_key_exists( "scheme", $this->params ) ) return null;
return $this->params[ "scheme"];
}
/**
* returns username or null
*
* @return string
*/
function getUsername()
{
if ( !array_key_exists( "user", $this->params ) ) return null;
return $this->params[ "user"];
}
/**
* returns password or null
*
* @return string
*/
function getPassword()
{
if ( !array_key_exists( "pass", $this->params ) ) return null;
if ( !$this->params['pass'] ) return null;
return $this->params[ "pass"];
}
/**
* returns database name or null
*
* @return string
*/
function getDatabase()
{
if ( !array_key_exists( "database", $this->params ) ) return null;
return $this->params[ "database"];
}
/**
* returns table name or null
*
* @return unknown
*/
function getTable()
{
if ( !array_key_exists( "table", $this->params ) ) return null;
return $this->params[ "table"];
}
/**
* returns host or null
*
* @return string
*/
function getHost()
{
if ( !array_key_exists( "host", $this->params ) ) return null;
return $this->params[ "host"];
}
/**
* returns port number or null
*
* @return integer
*/
function getPort()
{
if ( !array_key_exists( "port", $this->params ) ) return null;
if ( is_null( $this->params[ "port"])) return null;
return intval($this->params[ "port"]);
}
/**
* Return TRUE if 'port' was specified in DSN
*
* @return bool
*/
function isPortSpecified()
{
return $this->getPort() != null;
}
/**
* Gets specified params.
*
* @return array array of params specivied via query part of URI
*/
function getParams()
{
if (!array_key_exists('params', $this->params)) return array();
return $this->params;
}
/**
* Retrun 'true' if item selector specified.
*
* @return bool
*/
function isItemSelectorSpecified()
{
return array_key_exists('column_name', $this->params);
}
/**
* Gets item selctor if specified.
*
* returns array with keys for creating select query:
*
* \li table - table name where need select item
* \li column - primary key column name
* \li value - primary key column value
*
* @return array accosiative array
*/
function getItemSelector()
{
if ( !$this->isItemSelectorSpecified()) return array();
return array(
'table' => $this->getTable(),
'column' => $this->params['column_name'],
'value' => $this->params['column_value']
);
}
/**
* true if received DSN is valid
*
* @return bool
*/
function isValid()
{
return $this->isValid;
}
function _convertURI()
{
return true;
}
/**
@return bool sign of valid url
*/
function _parse( $url )
{
$parts = explode( "//", $url );
if ( count( $parts ) == 1 ) return false;
if ( $parts[1] == "" ) return false;
$this->params = parse_url( $url );
if ( !array_key_exists( "path", $this->params ) ) return false;
$parts = explode( "/", $this->params["path"] );
if ( !array_key_exists( 1, $parts ) ) return false;
if ( !array_key_exists( "pass",$this->params ) ) {
$this->params["pass"] = "";
}
$this->params["database"] = $parts[1];
if ( count( $parts ) > 2 ) {
$this->params["table"] = $parts[2];
if (count( $parts ) == 5 ) {
//primary key and value
$this->params["column_name"] = $parts[3];
$this->params["column_value"] = $parts[4];
}
}
else {
$this->params["table"] = "";
}
if ( !array_key_exists( "port", $this->params ) ) {
$this->params["port"]=null;
}
//for non server databases
if (array_key_exists('query', $this->params)) {
$args = array();
parse_str($this->params['query'], $args);
$this->params['params'] = $args;
if ( array_key_exists('database', $args)) {
$this->params['database'] = $args['database'];
}
if ( array_key_exists('table', $args)) {
$this->params['table'] = $args['table'];
}
}
return true;
}
}
}