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
4a5be979
Commit
4a5be979
authored
Nov 13, 2013
by
Benjamin Eberlei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Exception support for MySQLi
parent
0f654115
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
73 additions
and
5 deletions
+73
-5
Driver.php
lib/Doctrine/DBAL/Driver/Mysqli/Driver.php
+55
-1
MysqliConnection.php
lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
+8
-0
MysqliException.php
lib/Doctrine/DBAL/Driver/Mysqli/MysqliException.php
+3
-1
DataAccessTest.php
tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
+6
-2
ExceptionTest.php
tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php
+1
-1
No files found.
lib/Doctrine/DBAL/Driver/Mysqli/Driver.php
View file @
4a5be979
...
@@ -20,6 +20,8 @@
...
@@ -20,6 +20,8 @@
namespace
Doctrine\DBAL\Driver\Mysqli
;
namespace
Doctrine\DBAL\Driver\Mysqli
;
use
Doctrine\DBAL\Driver
as
DriverInterface
;
use
Doctrine\DBAL\Driver
as
DriverInterface
;
use
Doctrine\DBAL\Driver\Mysqli\MysqliException
;
use
Doctrine\DBAL\DBALException
;
/**
/**
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
...
@@ -31,7 +33,11 @@ class Driver implements DriverInterface
...
@@ -31,7 +33,11 @@ class Driver implements DriverInterface
*/
*/
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
array
())
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
array
())
{
{
return
new
MysqliConnection
(
$params
,
$username
,
$password
,
$driverOptions
);
try
{
return
new
MysqliConnection
(
$params
,
$username
,
$password
,
$driverOptions
);
}
catch
(
MysqliException
$e
)
{
throw
DBALException
::
driverException
(
$this
,
$e
);
}
}
}
/**
/**
...
@@ -73,6 +79,54 @@ class Driver implements DriverInterface
...
@@ -73,6 +79,54 @@ class Driver implements DriverInterface
*/
*/
public
function
convertExceptionCode
(
\Exception
$exception
)
public
function
convertExceptionCode
(
\Exception
$exception
)
{
{
if
(
strpos
(
$exception
->
getMessage
(),
'Table'
)
===
0
)
{
if
(
strpos
(
$exception
->
getMessage
(),
'doesn\'t exist'
)
!==
false
)
{
return
DBALException
::
ERROR_UNKNOWN_TABLE
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'already exists'
)
!==
false
)
{
return
DBALException
::
ERROR_TABLE_ALREADY_EXISTS
;
}
}
if
(
strpos
(
$exception
->
getMessage
(),
'Unknown column'
)
===
0
)
{
return
DBALException
::
ERROR_BAD_FIELD_NAME
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'Cannot delete or update a parent row: a foreign key constraint fails'
)
!==
false
)
{
return
DBALException
::
ERROR_FOREIGN_KEY_CONSTRAINT
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'Duplicate entry'
)
!==
false
)
{
return
DBALException
::
ERROR_DUPLICATE_KEY
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'Column not found: 1054 Unknown column'
)
!==
false
)
{
return
DBALException
::
ERROR_BAD_FIELD_NAME
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'in field list is ambiguous'
)
!==
falsE
)
{
return
DBALException
::
ERROR_NON_UNIQUE_FIELD_NAME
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'You have an error in your SQL syntax; check the manual'
)
!==
false
)
{
return
DBALException
::
ERROR_SYNTAX
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'Access denied for user'
)
!==
false
)
{
return
DBALException
::
ERROR_ACCESS_DENIED
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'getaddrinfo failed: Name or service not known'
)
!==
false
)
{
return
DBALException
::
ERROR_ACCESS_DENIED
;
}
if
(
strpos
(
$exception
->
getMessage
(),
' cannot be null'
))
{
return
DBALException
::
ERROR_NOT_NULL
;
}
var_dump
(
$exception
->
geTcode
());
var_dump
(
$exception
->
getMEssage
());
return
0
;
return
0
;
}
}
}
}
lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
View file @
4a5be979
...
@@ -45,10 +45,18 @@ class MysqliConnection implements Connection
...
@@ -45,10 +45,18 @@ class MysqliConnection implements Connection
$socket
=
isset
(
$params
[
'unix_socket'
])
?
$params
[
'unix_socket'
]
:
ini_get
(
'mysqli.default_socket'
);
$socket
=
isset
(
$params
[
'unix_socket'
])
?
$params
[
'unix_socket'
]
:
ini_get
(
'mysqli.default_socket'
);
$this
->
_conn
=
mysqli_init
();
$this
->
_conn
=
mysqli_init
();
$previousHandler
=
set_error_handler
(
function
()
{
});
if
(
!
$this
->
_conn
->
real_connect
(
$params
[
'host'
],
$username
,
$password
,
$params
[
'dbname'
],
$port
,
$socket
))
{
if
(
!
$this
->
_conn
->
real_connect
(
$params
[
'host'
],
$username
,
$password
,
$params
[
'dbname'
],
$port
,
$socket
))
{
set_error_handler
(
$previousHandler
);
throw
new
MysqliException
(
$this
->
_conn
->
connect_error
,
$this
->
_conn
->
connect_errno
);
throw
new
MysqliException
(
$this
->
_conn
->
connect_error
,
$this
->
_conn
->
connect_errno
);
}
}
set_error_handler
(
$previousHandler
);
if
(
isset
(
$params
[
'charset'
]))
{
if
(
isset
(
$params
[
'charset'
]))
{
$this
->
_conn
->
set_charset
(
$params
[
'charset'
]);
$this
->
_conn
->
set_charset
(
$params
[
'charset'
]);
}
}
...
...
lib/Doctrine/DBAL/Driver/Mysqli/MysqliException.php
View file @
4a5be979
...
@@ -19,9 +19,11 @@
...
@@ -19,9 +19,11 @@
namespace
Doctrine\DBAL\Driver\Mysqli
;
namespace
Doctrine\DBAL\Driver\Mysqli
;
use
Doctrine\DBAL\DBALException
;
/**
/**
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
*/
*/
class
MysqliException
extends
\
Exception
class
MysqliException
extends
DBAL
Exception
{
{
}
}
tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
View file @
4a5be979
...
@@ -203,15 +203,19 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
...
@@ -203,15 +203,19 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
$row
=
array_change_key_case
(
$row
,
\CASE_LOWER
);
$row
=
array_change_key_case
(
$row
,
\CASE_LOWER
);
$this
->
assertEquals
(
1
,
$row
[
'test_int'
]);
$this
->
assertEquals
(
1
,
$row
[
'test_int'
]);
$this
->
assertEquals
(
$datetimeString
,
$row
[
'test_datetime'
]);
$this
->
assertEquals
(
$datetimeString
,
$row
[
'test_datetime'
]);
}
}
/**
/**
* @group DBAL-209
* @group DBAL-209
* @expectedException \Doctrine\DBAL\DBALException
* @expectedException \Doctrine\DBAL\DBALException
*/
*/
public
function
testFetchAllWithMissingTypes
()
public
function
testFetchAllWithMissingTypes
()
{
{
if
(
$this
->
_conn
->
getDriver
()
instanceof
\Doctrine\DBAL\Driver\Mysqli\Driver
)
{
$this
->
markTestSkipped
(
'mysqli actually supports this'
);
}
$datetimeString
=
'2010-01-01 10:10:10'
;
$datetimeString
=
'2010-01-01 10:10:10'
;
$datetime
=
new
\DateTime
(
$datetimeString
);
$datetime
=
new
\DateTime
(
$datetimeString
);
$sql
=
"SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"
;
$sql
=
"SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"
;
...
...
tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php
View file @
4a5be979
...
@@ -9,7 +9,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
...
@@ -9,7 +9,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
{
parent
::
setUp
();
parent
::
setUp
();
$supportExceptions
=
array
(
'pdo_sqlite'
,
'pdo_mysql'
,
'pdo_pgsql'
);
$supportExceptions
=
array
(
'pdo_sqlite'
,
'pdo_mysql'
,
'pdo_pgsql'
,
'mysqli'
);
$params
=
$this
->
_conn
->
getParams
();
$params
=
$this
->
_conn
->
getParams
();
if
(
!
in_array
(
$params
[
'driver'
],
$supportExceptions
))
{
if
(
!
in_array
(
$params
[
'driver'
],
$supportExceptions
))
{
...
...
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