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
cbc157fb
Commit
cbc157fb
authored
Nov 11, 2006
by
zYne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
replaced the old Doctrine_Db with new one
parent
cd80b593
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
466 additions
and
69 deletions
+466
-69
Db.php
lib/Doctrine/Db.php
+465
-68
UnitTestCase.php
tests/UnitTestCase.php
+1
-1
No files found.
lib/Doctrine/Db.php
View file @
cbc157fb
...
@@ -18,120 +18,517 @@
...
@@ -18,120 +18,517 @@
* and is licensed under the LGPL. For more information, see
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
* <http://www.phpdoctrine.com>.
*/
*/
class
Doctrine_Db
extends
PDO
implements
Countable
,
IteratorAggregate
{
/**
* Doctrine_Db
* A thin wrapper layer on top of PDO / Doctrine_Adapter
*
* The purpose of this class is to provide an easy to use, pluggable eventlistener
* architecture to database handler object
*
* Aspects such as logging, query profiling and caching can be easily implemented through
* the use of these listeners
*
* @author Konsta Vesterinen
* @license LGPL
* @package Doctrine
*/
class
Doctrine_Db
implements
Countable
,
IteratorAggregate
,
Doctrine_Adapter_Interface
{
/**
* error constants
*/
const
ERR
=
-
1
;
const
ERR_SYNTAX
=
-
2
;
const
ERR_CONSTRAINT
=
-
3
;
const
ERR_NOT_FOUND
=
-
4
;
const
ERR_ALREADY_EXISTS
=
-
5
;
const
ERR_UNSUPPORTED
=
-
6
;
const
ERR_MISMATCH
=
-
7
;
const
ERR_INVALID
=
-
8
;
const
ERR_NOT_CAPABLE
=
-
9
;
const
ERR_TRUNCATED
=
-
10
;
const
ERR_INVALID_NUMBER
=
-
11
;
const
ERR_INVALID_DATE
=
-
12
;
const
ERR_DIVZERO
=
-
13
;
const
ERR_NODBSELECTED
=
-
14
;
const
ERR_CANNOT_CREATE
=
-
15
;
const
ERR_CANNOT_DELETE
=
-
16
;
const
ERR_CANNOT_DROP
=
-
17
;
const
ERR_NOSUCHTABLE
=
-
18
;
const
ERR_NOSUCHFIELD
=
-
19
;
const
ERR_NEED_MORE_DATA
=
-
20
;
const
ERR_NOT_LOCKED
=
-
21
;
const
ERR_VALUE_COUNT_ON_ROW
=
-
22
;
const
ERR_INVALID_DSN
=
-
23
;
const
ERR_CONNECT_FAILED
=
-
24
;
const
ERR_EXTENSION_NOT_FOUND
=
-
25
;
const
ERR_NOSUCHDB
=
-
26
;
const
ERR_ACCESS_VIOLATION
=
-
27
;
const
ERR_CANNOT_REPLACE
=
-
28
;
const
ERR_CONSTRAINT_NOT_NULL
=
-
29
;
const
ERR_DEADLOCK
=
-
30
;
const
ERR_CANNOT_ALTER
=
-
31
;
const
ERR_MANAGER
=
-
32
;
const
ERR_MANAGER_PARSE
=
-
33
;
const
ERR_LOADMODULE
=
-
34
;
const
ERR_INSUFFICIENT_DATA
=
-
35
;
/**
* @var array $instances all the instances of this class
*/
protected
static
$instances
=
array
();
/**
/**
*
default DSN
*
@var array $isConnected whether or not a connection has been established
*/
*/
const
DSN
=
"mysql://root:dc34@localhost/test"
;
protected
$isConnected
=
false
;
/**
/**
*
executed queries
*
@var PDO $dbh the database handler
*/
*/
pr
ivate
$queries
=
array
()
;
pr
otected
$dbh
;
/**
/**
*
execution times of the executed querie
s
*
@var array $option
s
*/
*/
private
$exectimes
=
array
();
protected
$options
=
array
(
'dsn'
=>
null
,
'username'
=>
null
,
'password'
=>
null
,
);
/**
* @var Doctrine_Db_EventListener_Interface|Doctrine_Overloadable $listener
* listener for listening events
*/
protected
$listener
;
/**
* @var integer $querySequence
*/
protected
$querySequence
=
0
;
private
static
$driverMap
=
array
(
'oracle'
=>
'oci8'
,
'postgres'
=>
'pgsql'
,
'oci'
=>
'oci8'
,
'sqlite2'
=>
'sqlite'
,
'sqlite3'
=>
'sqlite'
);
/**
/**
* constructor
* constructor
* @param string $dsn
*
* @param string $username
* @param string $dsn data source name
* @param string $password
* @param string $user database username
* @param string $pass database password
*/
*/
public
function
__construct
(
$dsn
,
$username
,
$password
)
{
public
function
__construct
(
$dsn
,
$user
,
$pass
)
{
parent
::
__construct
(
$dsn
,
$username
,
$password
);
if
(
!
isset
(
$user
))
{
$this
->
setAttribute
(
PDO
::
ATTR_ERRMODE
,
PDO
::
ERRMODE_EXCEPTION
);
$a
=
self
::
parseDSN
(
$dsn
);
$this
->
setAttribute
(
PDO
::
ATTR_STATEMENT_CLASS
,
array
(
"Doctrine_DbStatement"
,
array
(
$this
)));
extract
(
$a
);
}
$this
->
options
[
'dsn'
]
=
$dsn
;
$this
->
options
[
'username'
]
=
$user
;
$this
->
options
[
'password'
]
=
$pass
;
$this
->
listener
=
new
Doctrine_Db_EventListener
();
}
}
public
static
function
getConn
(
$dsn
,
$username
=
null
,
$password
=
null
)
{
public
function
nextQuerySequence
()
{
static
$instance
;
return
++
$this
->
querySequence
;
}
/**
* getQuerySequence
*/
public
function
getQuerySequence
()
{
return
$this
->
querySequence
;
}
/**
* getDBH
*/
public
function
getDBH
()
{
return
$this
->
dbh
;
}
public
function
getOption
(
$name
)
{
if
(
!
array_key_exists
(
$name
,
$this
->
options
))
throw
new
Doctrine_Db_Exception
(
'Unknown option '
.
$name
);
if
(
!
isset
(
$instance
))
{
return
$this
->
options
[
$name
];
$instance
=
new
Doctrine_Db
(
$dsn
,
$username
,
$password
);
}
return
$instance
;
}
}
/**
* addListener
*
* @param Doctrine_Db_EventListener_Interface|Doctrine_Overloadable $listener
* @return Doctrine_Db
*/
public
function
addListener
(
$listener
,
$name
=
null
)
{
if
(
!
(
$this
->
listener
instanceof
Doctrine_Db_EventListener_Chain
))
$this
->
listener
=
new
Doctrine_Db_EventListener_Chain
();
$this
->
listener
->
add
(
$listener
,
$name
);
return
$this
;
}
/**
* getListener
*
* @return Doctrine_Db_EventListener_Interface|Doctrine_Overloadable
*/
public
function
getListener
()
{
return
$this
->
listener
;
}
/**
/**
* @param string $dsn PEAR::DB like DSN
* setListener
* format: schema://user:password@address/dbname
*
* @param Doctrine_Db_EventListener_Interface|Doctrine_Overloadable $listener
* @return Doctrine_Db
*/
*/
public
static
function
getConnection
(
$dsn
=
null
)
{
public
function
setListener
(
$listener
)
{
static
$instance
=
array
();
if
(
!
(
$listener
instanceof
Doctrine_Db_EventListener_Interface
)
&&
$md5
=
md5
(
$dsn
);
!
(
$listener
instanceof
Doctrine_Overloadable
))
throw
new
Doctrine_Db_Exception
(
"Couldn't set eventlistener for database handler. EventListeners should implement either Doctrine_Db_EventListener_Interface or Doctrine_Overloadable"
);
if
(
!
isset
(
$instance
[
$md5
]))
{
$this
->
listener
=
$listener
;
if
(
!
isset
(
$dsn
))
{
$a
=
parse_url
(
self
::
DSN
);
}
else
{
$a
=
parse_url
(
$dsn
);
}
$e
=
array
();
$e
[
0
]
=
$a
[
"scheme"
]
.
":host="
.
$a
[
"host"
]
.
";dbname="
.
substr
(
$a
[
"path"
],
1
);
return
$this
;
$e
[
1
]
=
$a
[
"user"
];
}
$e
[
2
]
=
$a
[
"pass"
];
/**
* connect
* connects into database
*
* @return boolean
*/
public
function
connect
()
{
if
(
$this
->
isConnected
)
return
false
;
$this
->
dbh
=
new
PDO
(
$this
->
options
[
'dsn'
],
$this
->
options
[
'username'
],
$this
->
options
[
'password'
]);
$this
->
dbh
->
setAttribute
(
PDO
::
ATTR_ERRMODE
,
PDO
::
ERRMODE_EXCEPTION
);
$this
->
dbh
->
setAttribute
(
PDO
::
ATTR_STATEMENT_CLASS
,
array
(
"Doctrine_Db_Statement"
,
array
(
$this
)));
$this
->
isConnected
=
true
;
return
true
;
}
/**
* getConnection
*
* @param string $dsn PEAR::DB like DSN or PDO like DSN
* format for PEAR::DB like DSN: schema://user:password@address/dbname
*
* @return
*/
public
static
function
getConnection
(
$dsn
=
null
,
$username
=
null
,
$password
=
null
)
{
return
new
self
(
$dsn
,
$username
,
$password
);
}
/**
* driverName
* converts a driver name like (oracle) to appropriate PDO
* driver name (oci8 in the case of oracle)
*
* @param string $name
* @return string
*/
public
static
function
driverName
(
$name
)
{
if
(
isset
(
self
::
$driverMap
[
$name
]))
return
self
::
$driverMap
[
$name
];
$instance
[
$md5
]
=
new
Doctrine_Db
(
$e
[
0
],
$e
[
1
],
$e
[
2
]);
return
$name
;
}
/**
* parseDSN
*
* @param string $dsn
* @return array Parsed contents of DSN
*/
function
parseDSN
(
$dsn
)
{
// silence any warnings
$parts
=
@
parse_url
(
$dsn
);
$names
=
array
(
'scheme'
,
'host'
,
'port'
,
'user'
,
'pass'
,
'path'
,
'query'
,
'fragment'
);
foreach
(
$names
as
$name
)
{
if
(
!
isset
(
$parts
[
$name
]))
$parts
[
$name
]
=
null
;
}
}
return
$instance
[
$md5
];
if
(
count
(
$parts
)
==
0
||
!
isset
(
$parts
[
'scheme'
]))
throw
new
Doctrine_Db_Exception
(
'Empty data source name'
);
$drivers
=
self
::
getAvailableDrivers
();
$parts
[
'scheme'
]
=
self
::
driverName
(
$parts
[
'scheme'
]);
if
(
!
in_array
(
$parts
[
'scheme'
],
$drivers
))
throw
new
Doctrine_Db_Exception
(
'Driver '
.
$parts
[
'scheme'
]
.
' not availible or extension not loaded'
);
switch
(
$parts
[
'scheme'
])
{
case
'sqlite'
:
if
(
isset
(
$parts
[
'host'
])
&&
$parts
[
'host'
]
==
':memory'
)
{
$parts
[
'database'
]
=
':memory:'
;
$parts
[
'dsn'
]
=
'sqlite::memory:'
;
}
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'
);
if
(
isset
(
$parts
[
'path'
]))
$parts
[
'database'
]
=
substr
(
$parts
[
'path'
],
1
);
if
(
!
isset
(
$parts
[
'host'
]))
throw
new
Doctrine_Db_Exception
(
'No hostname set in data source name'
);
$parts
[
'dsn'
]
=
$parts
[
"scheme"
]
.
":host="
.
$parts
[
"host"
]
.
";dbname="
.
$parts
[
"database"
];
break
;
default
:
throw
new
Doctrine_Db_Exception
(
'Unknown driver '
.
$parts
[
'scheme'
]);
}
return
$parts
;
}
/**
* clear
* clears all instances from the memory
*
* @return void
*/
public
static
function
clear
()
{
self
::
$instances
=
array
();
}
/**
* errorCode
* Fetch the SQLSTATE associated with the last operation on the database handle
*
* @return integer
*/
public
function
errorCode
()
{
return
$this
->
dbh
->
errorCode
();
}
}
/**
/**
* @param string $query query to be executed
* errorInfo
* Fetch extended error information associated with the last operation on the database handle
*
* @return array
*/
*/
public
function
query
(
$query
)
{
public
function
errorInfo
()
{
try
{
return
$this
->
dbh
->
errorInfo
();
$this
->
queries
[]
=
$query
;
}
$time
=
microtime
();
/**
* prepare
*
* @param string $statement
*/
public
function
prepare
(
$statement
)
{
$this
->
connect
();
$stmt
=
parent
::
query
(
$query
);
$event
=
new
Doctrine_Db_Event
(
$this
,
Doctrine_Db_Event
::
PREPARE
,
$statement
);
$this
->
exectimes
[]
=
(
microtime
()
-
$time
);
$this
->
listener
->
onPrePrepare
(
$event
);
return
$stmt
;
}
catch
(
PDOException
$e
)
{
$stmt
=
$this
->
dbh
->
prepare
(
$statement
);
throw
$e
;
}
$this
->
listener
->
onPrepare
(
$event
);
$this
->
querySequence
++
;
return
$stmt
;
}
/**
* query
*
* @param string $statement
* @param array $params
* @return Doctrine_Db_Statement|boolean
*/
public
function
query
(
$statement
,
array
$params
=
array
())
{
$this
->
connect
();
$event
=
new
Doctrine_Db_Event
(
$this
,
Doctrine_Db_Event
::
QUERY
,
$statement
);
$this
->
listener
->
onPreQuery
(
$event
);
if
(
!
empty
(
$params
))
$stmt
=
$this
->
dbh
->
query
(
$statement
)
->
execute
(
$params
);
else
$stmt
=
$this
->
dbh
->
query
(
$statement
);
$this
->
listener
->
onQuery
(
$event
);
$this
->
querySequence
++
;
return
$stmt
;
}
}
/**
/**
* @param string $query query to be prepared
* quote
* quotes a string for use in a query
*
* @param string $input
* @return string
*/
*/
public
function
prepare
(
$query
)
{
public
function
quote
(
$input
)
{
$this
->
queries
[]
=
$query
;
$this
->
connect
()
;
return
parent
::
prepare
(
$query
);
return
$this
->
dbh
->
quote
(
$input
);
}
}
/**
/**
* @param string $time exectime of the last executed query
* exec
* @return void
* executes an SQL statement and returns the number of affected rows
*
* @param string $statement
* @return integer
*/
public
function
exec
(
$statement
)
{
$this
->
connect
();
$args
=
func_get_args
();
$event
=
new
Doctrine_Db_Event
(
$this
,
Doctrine_Db_Event
::
EXEC
,
$statement
);
$this
->
listener
->
onPreExec
(
$event
);
$rows
=
$this
->
dbh
->
exec
(
$statement
);
$this
->
listener
->
onExec
(
$event
);
return
$rows
;
}
/**
* fetchAll
*
* @return array
*/
public
function
fetchAll
(
$statement
,
array
$params
=
array
())
{
return
$this
->
query
(
$statement
,
$params
)
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
}
public
function
fetchOne
(
$statement
,
array
$params
=
array
())
{
return
current
(
$this
->
query
(
$statement
,
$params
)
->
fetch
(
PDO
::
FETCH_NUM
));
}
public
function
fetchRow
(
$statement
,
array
$params
=
array
())
{
return
$this
->
query
(
$statement
,
$params
)
->
fetch
(
PDO
::
FETCH_ASSOC
);
}
public
function
fetchArray
(
$statement
,
array
$params
=
array
())
{
return
$this
->
query
(
$statement
,
$params
)
->
fetch
(
PDO
::
FETCH_NUM
);
}
public
function
fetchColumn
(
$statement
,
array
$params
=
array
())
{
return
$this
->
query
(
$statement
,
$params
)
->
fetchAll
(
PDO
::
FETCH_COLUMN
);
}
public
function
fetchAssoc
(
$statement
,
array
$params
=
array
())
{
return
$this
->
query
(
$statement
,
$params
)
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
}
public
function
fetchBoth
(
$statement
,
array
$params
=
array
())
{
return
$this
->
query
(
$statement
,
$params
)
->
fetchAll
(
PDO
::
FETCH_BOTH
);
}
/**
* lastInsertId
*
* @return integer
*/
*/
public
function
addExecTime
(
$time
)
{
public
function
lastInsertId
()
{
$this
->
exectimes
[]
=
$time
;
$this
->
connect
();
return
$this
->
dbh
->
lastInsertId
();
}
}
/**
* begins a transaction
*
* @return boolean
*/
public
function
beginTransaction
()
{
$event
=
new
Doctrine_Db_Event
(
$this
,
Doctrine_Db_Event
::
BEGIN
);
$this
->
listener
->
onPreBeginTransaction
(
$event
);
$return
=
$this
->
dbh
->
beginTransaction
();
$this
->
listener
->
onBeginTransaction
(
$event
);
public
function
getExecTimes
()
{
return
$return
;
return
$this
->
exectimes
;
}
}
/**
/**
* @return array an array of executed queries
* commits a transaction
*
* @return boolean
*/
*/
public
function
getQueries
()
{
public
function
commit
()
{
return
$this
->
queries
;
$event
=
new
Doctrine_Db_Event
(
$this
,
Doctrine_Db_Event
::
COMMIT
);
$this
->
listener
->
onPreCommit
(
$event
);
$return
=
$this
->
dbh
->
commit
();
$this
->
listener
->
onCommit
(
$event
);
return
$return
;
}
}
/**
/**
* rollBack
*
* @return boolean
*/
public
function
rollBack
()
{
$this
->
connect
();
$event
=
new
Doctrine_Db_Event
(
$this
,
Doctrine_Db_Event
::
ROLLBACK
);
$this
->
listener
->
onPreRollback
(
$event
);
$this
->
dbh
->
rollBack
();
$this
->
listener
->
onRollback
(
$event
);
}
/**
* getAttribute
* retrieves a database connection attribute
*
* @param integer $attribute
* @return mixed
*/
public
function
getAttribute
(
$attribute
)
{
$this
->
connect
();
return
$this
->
dbh
->
getAttribute
(
$attribute
);
}
/**
* returns an array of available PDO drivers
*/
public
static
function
getAvailableDrivers
()
{
return
PDO
::
getAvailableDrivers
();
}
/**
* setAttribute
* sets an attribute
*
* @param integer $attribute
* @param mixed $value
* @return boolean
*/
public
function
setAttribute
(
$attribute
,
$value
)
{
$this
->
connect
();
$this
->
dbh
->
setAttribute
(
$attribute
,
$value
);
}
/**
* getIterator
*
* @return ArrayIterator
* @return ArrayIterator
*/
*/
public
function
getIterator
()
{
public
function
getIterator
()
{
return
new
ArrayIterator
(
$this
->
queries
);
if
(
$this
->
listener
instanceof
Doctrine_Db_Profiler
)
return
$this
->
listener
;
}
}
/**
/**
* count
* returns the number of executed queries
* returns the number of executed queries
*
* @return integer
* @return integer
*/
*/
public
function
count
()
{
public
function
count
()
{
return
count
(
$this
->
queries
);
return
$this
->
querySequence
;
}
}
}
}
tests/UnitTestCase.php
View file @
cbc157fb
...
@@ -71,7 +71,7 @@ class Doctrine_UnitTestCase extends UnitTestCase {
...
@@ -71,7 +71,7 @@ class Doctrine_UnitTestCase extends UnitTestCase {
$this
->
manager
->
setAttribute
(
Doctrine
::
ATTR_LISTENER
,
$this
->
listener
);
$this
->
manager
->
setAttribute
(
Doctrine
::
ATTR_LISTENER
,
$this
->
listener
);
}
else
{
}
else
{
//$this->dbh = Doctrine_Db::getConnection();
//$this->dbh = Doctrine_Db::getConnection();
$this
->
dbh
=
Doctrine_Db
2
::
getConnection
(
"sqlite::memory:"
);
$this
->
dbh
=
Doctrine_Db
::
getConnection
(
"sqlite::memory:"
);
//$this->dbh = new PDO("sqlite::memory:");
//$this->dbh = new PDO("sqlite::memory:");
$this
->
connection
=
$this
->
manager
->
openConnection
(
$this
->
dbh
);
$this
->
connection
=
$this
->
manager
->
openConnection
(
$this
->
dbh
);
...
...
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