Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
10e7d416
Commit
10e7d416
authored
Sep 22, 2006
by
zYne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Doctrine_DB_TestCase, Doctrine_DB_Exception added, enhanced parseDSN method
parent
09d249e5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
121 additions
and
179 deletions
+121
-179
Exception.php
Doctrine/DB/Exception.php
+2
-0
DB.php
draft/DB.php
+49
-179
DBTestCase.php
tests/DBTestCase.php
+70
-0
No files found.
Doctrine/DB/Exception.php
0 → 100644
View file @
10e7d416
<?php
class
Doctrine_DB_Exception
extends
Doctrine_Exception
{
}
draft/DB.php
View file @
10e7d416
...
@@ -27,7 +27,7 @@
...
@@ -27,7 +27,7 @@
* @license LGPL
* @license LGPL
* @package Doctrine
* @package Doctrine
*/
*/
class
Doctrine_DB
implements
Countable
,
IteratorAggregate
{
class
Doctrine_DB
2
implements
Countable
,
IteratorAggregate
{
/**
/**
* default DSN
* default DSN
*/
*/
...
@@ -126,7 +126,7 @@ class Doctrine_DB implements Countable, IteratorAggregate {
...
@@ -126,7 +126,7 @@ class Doctrine_DB implements Countable, IteratorAggregate {
$md5
=
md5
(
$dsn
);
$md5
=
md5
(
$dsn
);
if
(
isset
(
$username
))
{
if
(
isset
(
$username
))
{
self
::
$instances
[
$md5
]
=
new
Doctrine_DB
(
$dsn
,
$username
,
$password
);
self
::
$instances
[
$md5
]
=
new
self
(
$dsn
,
$username
,
$password
);
}
}
if
(
!
isset
(
self
::
$instances
[
$md5
]))
{
if
(
!
isset
(
self
::
$instances
[
$md5
]))
{
...
@@ -135,13 +135,9 @@ class Doctrine_DB implements Countable, IteratorAggregate {
...
@@ -135,13 +135,9 @@ class Doctrine_DB implements Countable, IteratorAggregate {
}
else
{
}
else
{
$a
=
self
::
parseDSN
(
$dsn
);
$a
=
self
::
parseDSN
(
$dsn
);
}
}
$e
=
array
(
);
extract
(
$a
);
$dsn
=
$a
[
"phptype"
]
.
":host="
.
$a
[
"hostspec"
]
.
";dbname="
.
$a
[
"database"
];
self
::
$instances
[
$md5
]
=
new
self
(
$dsn
,
$user
,
$pass
);
$username
=
isset
(
$a
[
"username"
])
?
$a
[
'username'
]
:
null
;
$password
=
isset
(
$a
[
"password"
])
?
$a
[
'password'
]
:
null
;
self
::
$instances
[
$md5
]
=
new
Doctrine_DB
(
$dsn
,
$username
,
$password
);
}
}
return
self
::
$instances
[
$md5
];
return
self
::
$instances
[
$md5
];
}
}
...
@@ -285,7 +281,7 @@ class Doctrine_DB implements Countable, IteratorAggregate {
...
@@ -285,7 +281,7 @@ class Doctrine_DB implements Countable, IteratorAggregate {
* returns an array of available PDO drivers
* returns an array of available PDO drivers
*/
*/
public
static
function
getAvailableDrivers
()
{
public
static
function
getAvailableDrivers
()
{
return
PDO
::
getAvail
i
bleDrivers
();
return
PDO
::
getAvail
a
bleDrivers
();
}
}
/**
/**
* setAttribute
* setAttribute
...
@@ -339,185 +335,59 @@ class Doctrine_DB implements Countable, IteratorAggregate {
...
@@ -339,185 +335,59 @@ class Doctrine_DB implements Countable, IteratorAggregate {
}
}
/**
/**
* parseDSN
* parseDSN
* Parse a data source name.
*
* Additional keys can be added by appending a URI query string to the
* end of the DSN.
*
* The format of the supplied DSN is in its fullest form:
* <code>
* phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true
* </code>
*
* Most variations are allowed:
* <code>
* phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644
* phptype://username:password@hostspec/database_name
* phptype://username:password@hostspec
* phptype://username@hostspec
* phptype://hostspec/database
* phptype://hostspec
* phptype(dbsyntax)
* phptype
* </code>
*
* @param string Data Source Name to be parsed
*
*
* @return array an associative array with the following keys:
* @param string $dsn
* + phptype: Database backend used in PHP (mysql, odbc etc.)
* @return array Parsed contents of DSN
* + dbsyntax: Database used with regards to SQL syntax etc.
*/
* + protocol: Communication protocol to use (tcp, unix etc.)
function
parseDSN
(
$dsn
)
{
* + hostspec: Host specification (hostname[:port])
$parts
=
@
parse_url
(
$dsn
);
* + database: Database to use on the DBMS server
* + username: User name for login
$names
=
array
(
'scheme'
,
'host'
,
'port'
,
'user'
,
'pass'
,
'path'
,
'query'
,
'fragment'
);
* + password: Password for login
*
foreach
(
$names
as
$name
)
{
* @access public
if
(
!
isset
(
$parts
[
$name
]))
* @author Tomas V.V.Cox <cox@idecnet.com>
$parts
[
$name
]
=
null
;
*/
public
static
function
parseDSN
(
$dsn
)
{
// Find phptype and dbsyntax
if
((
$pos
=
strpos
(
$dsn
,
'://'
))
!==
false
)
{
$str
=
substr
(
$dsn
,
0
,
$pos
);
$dsn
=
substr
(
$dsn
,
$pos
+
3
);
}
else
{
$str
=
$dsn
;
$dsn
=
null
;
}
}
// Get phptype and dbsyntax
if
(
count
(
$parts
)
==
0
||
!
isset
(
$parts
[
'scheme'
]))
// $str => phptype(dbsyntax)
throw
new
Doctrine_DB_Exception
(
'Empty data source name'
);
if
(
preg_match
(
'|^(.+?)\((.*?)\)$|'
,
$str
,
$arr
))
{
$parsed
[
'phptype'
]
=
$arr
[
1
];
$parsed
[
'dbsyntax'
]
=
!
$arr
[
2
]
?
$arr
[
1
]
:
$arr
[
2
];
}
else
{
$parsed
[
'phptype'
]
=
$str
;
$parsed
[
'dbsyntax'
]
=
$str
;
}
if
(
!
count
(
$dsn
))
{
$drivers
=
self
::
getAvailableDrivers
();
return
$parsed
;
}
if
(
!
in_array
(
$parts
[
'scheme'
],
$drivers
))
throw
new
Doctrine_DB_Exception
(
'Driver '
.
$parts
[
'scheme'
]
.
' not availible or extension not loaded'
);
// Get (if found): username and password
switch
(
$parts
[
'scheme'
])
{
// $dsn => username:password@protocol+hostspec/database
case
'sqlite'
:
if
((
$at
=
strrpos
(
$dsn
,
'@'
))
!==
false
)
{
if
(
isset
(
$parts
[
'host'
])
&&
$parts
[
'host'
]
==
':memory'
)
{
$str
=
substr
(
$dsn
,
0
,
$at
);
$parts
[
'database'
]
=
':memory:'
;
$dsn
=
substr
(
$dsn
,
$at
+
1
);
$parts
[
'dsn'
]
=
'sqlite::memory:'
;
if
((
$pos
=
strpos
(
$str
,
':'
))
!==
false
)
{
}
$parsed
[
'username'
]
=
rawurldecode
(
substr
(
$str
,
0
,
$pos
));
$parsed
[
'password'
]
=
rawurldecode
(
substr
(
$str
,
$pos
+
1
));
}
else
{
$parsed
[
'username'
]
=
rawurldecode
(
$str
);
}
}
// Find protocol and hostspec
break
;
case
'mysql'
:
case
'informix'
:
case
'oci8'
:
case
'mssql'
:
case
'firebird'
:
case
'pgsql'
:
case
'odbc'
:
if
(
!
isset
(
$parts
[
'path'
])
||
$parts
[
'path'
]
==
'/'
)
throw
new
Doctrine_DB_Exception
(
'No database availible in data source name'
);
// $dsn => proto(proto_opts)/database
if
(
isset
(
$parts
[
'path'
]))
if
(
preg_match
(
'|^([^(]+)\((.*?)\)/?(.*?)$|'
,
$dsn
,
$match
))
{
$parts
[
'database'
]
=
substr
(
$parts
[
'path'
],
1
);
$proto
=
$match
[
1
];
$proto_opts
=
$match
[
2
]
?
$match
[
2
]
:
false
;
if
(
!
isset
(
$parts
[
'host'
]))
$dsn
=
$match
[
3
]
;
throw
new
Doctrine_DB_Exception
(
'No hostname set in data source name'
)
;
// $dsn => protocol+hostspec/database (old format)
$parts
[
'dsn'
]
=
$parts
[
"scheme"
]
.
":host="
.
$parts
[
"host"
]
.
";dbname="
.
$parts
[
"database"
];
}
else
{
break
;
if
(
strpos
(
$dsn
,
'+'
)
!==
false
)
{
default
:
list
(
$proto
,
$dsn
)
=
explode
(
'+'
,
$dsn
,
2
);
throw
new
Doctrine_DB_Exception
(
'Unknown driver '
.
$parts
[
'scheme'
]);
}
}
if
(
strpos
(
$dsn
,
'/'
)
!==
false
)
{
list
(
$proto_opts
,
$dsn
)
=
explode
(
'/'
,
$dsn
,
2
);
}
else
{
$proto_opts
=
$dsn
;
$dsn
=
null
;
}
}
// process the different protocol options
$parsed
[
'protocol'
]
=
(
!
empty
(
$proto
))
?
$proto
:
'tcp'
;
$proto_opts
=
rawurldecode
(
$proto_opts
);
if
(
strpos
(
$proto_opts
,
':'
)
!==
false
)
{
list
(
$proto_opts
,
$parsed
[
'port'
])
=
explode
(
':'
,
$proto_opts
);
}
if
(
$parsed
[
'protocol'
]
==
'tcp'
)
{
$parsed
[
'hostspec'
]
=
$proto_opts
;
}
elseif
(
$parsed
[
'protocol'
]
==
'unix'
)
{
$parsed
[
'socket'
]
=
$proto_opts
;
}
// Get dabase if any
// $dsn => database
if
(
$dsn
)
{
// /database
if
((
$pos
=
strpos
(
$dsn
,
'?'
))
===
false
)
{
$parsed
[
'database'
]
=
$dsn
;
// /database?param1=value1¶m2=value2
}
else
{
$parsed
[
'database'
]
=
substr
(
$dsn
,
0
,
$pos
);
$dsn
=
substr
(
$dsn
,
$pos
+
1
);
if
(
strpos
(
$dsn
,
'&'
)
!==
false
)
{
$opts
=
explode
(
'&'
,
$dsn
);
}
else
{
// database?param1=value1
$opts
=
array
(
$dsn
);
}
foreach
(
$opts
as
$opt
)
{
list
(
$key
,
$value
)
=
explode
(
'='
,
$opt
);
if
(
!
isset
(
$parsed
[
$key
]))
{
// don't allow params overwrite
$parsed
[
$key
]
=
rawurldecode
(
$value
);
}
}
}
}
return
$parsed
;
return
$parts
;
}
/**
* Here is my version of parseDSN. It is a bit leaner than the one above, but you can choose either one.
* This one relies on the built in functionality more than replicating it in userland code so it should
* be more efficient. Not completely compatible with the parser above, but it is easy to add in
* the phptype/dbsyntax and protocol/hostspec parts if need be.
*
* @author Elliot Anderson <elliot.a@gmail.com>
*
* @param string $dsn
* @return array Parsed contents of DSN
*/
function
parseDSNnew
(
$dsn
)
{
$parts
=
parse_url
(
$dsn
);
$parsed
=
array
(
);
if
(
count
(
$parts
)
==
0
)
return
false
;
if
(
isset
(
$parts
[
'scheme'
]
)
)
$parsed
[
'phptype'
]
=
$parsed
[
'dbsyntax'
]
=
$parts
[
'scheme'
];
if
(
isset
(
$parts
[
'host'
]
)
)
{
if
(
strpos
(
$parts
[
'host'
],
'+'
)
)
{
$tmp
=
explode
(
'+'
,
$parts
[
'host'
]
);
$parsed
[
'protocol'
]
=
$tmp
[
0
];
$parsed
[
'hostspec'
]
=
$tmp
[
1
];
}
else
{
$parsed
[
'hostspec'
]
=
$parts
[
'host'
];
}
}
if
(
isset
(
$parts
[
'path'
]
)
)
$parsed
[
'database'
]
=
substr
(
$parts
[
'path'
],
1
);
if
(
isset
(
$parts
[
'user'
]
)
)
$parsed
[
'username'
]
=
$parts
[
'user'
];
if
(
isset
(
$parts
[
'pass'
]
)
)
$parsed
[
'password'
]
=
$parts
[
'pass'
];
if
(
isset
(
$parts
[
'query'
]
)
)
parse_str
(
$parts
[
'query'
],
$parsed
[
'options'
]
);
return
$parsed
;
}
}
}
}
tests/DBTestCase.php
0 → 100644
View file @
10e7d416
<?php
require_once
(
"../draft/DB.php"
);
class
Doctrine_DB_TestCase
extends
Doctrine_UnitTestCase
{
public
function
prepareData
()
{
}
public
function
prepareTables
()
{
}
public
function
init
()
{
}
public
function
testInvalidDSN
()
{
try
{
$conn
=
Doctrine_DB2
::
getConnection
(
''
);
$this
->
fail
();
}
catch
(
Doctrine_DB_Exception
$e
)
{
$this
->
pass
();
}
try
{
$conn
=
Doctrine_DB2
::
getConnection
(
'unknown'
);
$this
->
fail
();
}
catch
(
Doctrine_DB_Exception
$e
)
{
$this
->
pass
();
}
try
{
$conn
=
Doctrine_DB2
::
getConnection
(
0
);
$this
->
fail
();
}
catch
(
Doctrine_DB_Exception
$e
)
{
$this
->
pass
();
}
}
public
function
testInvalidScheme
()
{
try
{
$conn
=
Doctrine_DB2
::
getConnection
(
'unknown://:memory:'
);
$this
->
fail
();
}
catch
(
Doctrine_DB_Exception
$e
)
{
$this
->
pass
();
}
}
public
function
testInvalidHost
()
{
try
{
$conn
=
Doctrine_DB2
::
getConnection
(
'mysql://user:password@'
);
$this
->
fail
();
}
catch
(
Doctrine_DB_Exception
$e
)
{
$this
->
pass
();
}
}
public
function
testInvalidDatabase
()
{
try
{
$conn
=
Doctrine_DB2
::
getConnection
(
'mysql://user:password@host/'
);
$this
->
fail
();
}
catch
(
Doctrine_DB_Exception
$e
)
{
$this
->
pass
();
}
}
public
function
testGetConnection
()
{
$conn
=
Doctrine_DB2
::
getConnection
(
'mysql://zYne:password@localhost/test'
);
$this
->
assertEqual
(
$conn
->
getDSN
(),
'mysql:host=localhost;dbname=test'
);
$this
->
assertEqual
(
$conn
->
getUsername
(),
'zYne'
);
$this
->
assertEqual
(
$conn
->
getPassword
(),
'password'
);
$conn
=
Doctrine_DB2
::
getConnection
(
'sqlite://:memory:'
);
$this
->
assertEqual
(
$conn
->
getDSN
(),
'sqlite::memory:'
);
$this
->
assertEqual
(
$conn
->
getUsername
(),
null
);
$this
->
assertEqual
(
$conn
->
getPassword
(),
null
);
}
}
?>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment