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
ed584565
Commit
ed584565
authored
Nov 10, 2011
by
Kim Hemsø Rasmussen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added mysqli driver.
parent
7101ecd7
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
553 additions
and
0 deletions
+553
-0
Driver.php
lib/Doctrine/DBAL/Driver/Mysqli/Driver.php
+56
-0
MysqliConnection.php
lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
+104
-0
MysqliException.php
lib/Doctrine/DBAL/Driver/Mysqli/MysqliException.php
+28
-0
MysqliStatement.php
lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
+341
-0
DriverManager.php
lib/Doctrine/DBAL/DriverManager.php
+1
-0
DataAccessTest.php
tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
+15
-0
TestUtil.php
tests/Doctrine/Tests/TestUtil.php
+8
-0
No files found.
lib/Doctrine/DBAL/Driver/Mysqli/Driver.php
0 → 100644
View file @
ed584565
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Driver\Mysqli
;
use
Doctrine\DBAL\Driver
as
DriverInterface
;
/**
* @author Kim Hensø Rasmussen <kimhemsoe@gmail.com>
*/
class
Driver
implements
DriverInterface
{
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
array
())
{
return
new
MysqliConnection
(
$params
,
$username
,
$password
,
$driverOptions
);
}
public
function
getName
()
{
return
'mysqli'
;
}
public
function
getSchemaManager
(
\Doctrine\DBAL\Connection
$conn
)
{
return
new
\Doctrine\DBAL\Schema\MySqlSchemaManager
(
$conn
);
}
public
function
getDatabasePlatform
()
{
return
new
\Doctrine\DBAL\Platforms\MySqlPlatform
();
}
public
function
getDatabase
(
\Doctrine\DBAL\Connection
$conn
)
{
$params
=
$conn
->
getParams
();
return
$params
[
'dbname'
];
}
}
lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
0 → 100644
View file @
ed584565
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Driver\Mysqli
;
use
Doctrine\DBAL\Driver\Connection
as
ConnectionInterface
;
/**
* @author Kim Hensø Rasmussen <kimhemsoe@gmail.com>
*/
class
MysqliConnection
implements
ConnectionInterface
{
/**
* @var \mysqli
*/
private
$_conn
;
public
function
__construct
(
array
$params
,
$username
,
$password
,
array
$driverOptions
=
array
())
{
$port
=
isset
(
$params
[
'port'
])
?
$params
[
'port'
]
:
ini_get
(
'mysqli.default_port'
);
$socket
=
isset
(
$params
[
'unix_socket'
])
?
$params
[
'unix_socket'
]
:
ini_get
(
'mysqli.default_socket'
);
$this
->
_conn
=
new
\mysqli
(
$params
[
'host'
],
$username
,
$password
,
$params
[
'dbname'
],
$port
,
$socket
);
if
(
isset
(
$params
[
'charset'
]))
{
$this
->
_conn
->
set_charset
(
$params
[
'charset'
]);
}
}
public
function
prepare
(
$prepareString
)
{
return
new
MysqliStatement
(
$this
->
_conn
,
$prepareString
);
}
public
function
query
()
{
$args
=
func_get_args
();
$sql
=
$args
[
0
];
$stmt
=
$this
->
prepare
(
$sql
);
$stmt
->
execute
();
return
$stmt
;
}
public
function
quote
(
$input
,
$type
=
\PDO
::
PARAM_STR
)
{
return
"'"
.
$this
->
_conn
->
escape_string
(
$input
)
.
"'"
;
}
public
function
exec
(
$statement
)
{
$this
->
_conn
->
query
(
$statement
);
return
$this
->
_conn
->
affected_rows
;
}
public
function
lastInsertId
(
$name
=
null
)
{
return
$this
->
_conn
->
insert_id
;
}
public
function
beginTransaction
()
{
$this
->
_conn
->
query
(
'START TRANSACTION'
);
return
true
;
}
public
function
commit
()
{
return
$this
->
_conn
->
commit
();
}
public
function
rollBack
()
{
return
$this
->
_conn
->
rollback
();
}
public
function
errorCode
()
{
return
$this
->
_conn
->
errno
;
}
public
function
errorInfo
()
{
return
$this
->
_conn
->
error
;
}
}
lib/Doctrine/DBAL/Driver/Mysqli/MysqliException.php
0 → 100644
View file @
ed584565
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Driver\Mysqli
;
/**
* @author Kim Hensø Rasmussen <kimhemsoe@gmail.com>
*/
class
MysqliException
extends
\Exception
{}
lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
0 → 100644
View file @
ed584565
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Driver\Mysqli
;
use
Doctrine\DBAL\Driver\Statement
as
StatementInterface
;
use
PDO
;
/**
* @author Kim Hensø Rasmussen <kimhemsoe@gmail.com>
*/
class
MysqliStatement
implements
\IteratorAggregate
,
StatementInterface
{
private
static
$_paramTypeMap
=
array
(
PDO
::
PARAM_STR
=>
's'
,
PDO
::
PARAM_BOOL
=>
'i'
,
PDO
::
PARAM_NULL
=>
's'
,
PDO
::
PARAM_INT
=>
'i'
,
PDO
::
PARAM_LOB
=>
's'
// TODO Support LOB bigger then max package size.
);
private
$_conn
;
private
$_stmt
;
/**
* @var null|false|array
*/
private
$_columnNames
;
/**
* @var null|array
*/
private
$_rowBindedValues
;
/**
* @var array
*/
private
$_bindedValues
;
/**
* Contains ref values for bindValue()
*
* @var array
*/
private
$_values
=
array
();
private
$_defaultFetchStyle
=
PDO
::
FETCH_BOTH
;
public
function
__construct
(
\mysqli
$conn
,
$prepareString
)
{
$this
->
_conn
=
$conn
;
$this
->
_stmt
=
$conn
->
prepare
(
$prepareString
);
if
(
false
===
$this
->
_stmt
)
{
throw
new
MysqliException
(
$this
->
_conn
->
error
,
$this
->
_conn
->
errno
);
}
$paramCount
=
$this
->
_stmt
->
param_count
;
if
(
0
<
$paramCount
)
{
// Index 0 is types
// Need to init the string else php think we are trying to access it as a array. Better solution for this ?
$bindedValues
=
array
(
0
=>
str_repeat
(
's'
,
$paramCount
));
$null
=
null
;
for
(
$i
=
1
;
$i
<
$paramCount
;
$i
++
)
{
$bindedValues
[]
=&
$null
;
}
$this
->
_bindedValues
=
$bindedValues
;
}
}
/**
* {@inheritdoc}
*/
public
function
bindParam
(
$column
,
&
$variable
,
$type
=
null
)
{
if
(
null
===
$type
)
{
$type
=
's'
;
}
else
{
if
(
isset
(
self
::
$_paramTypeMap
[
$type
]))
{
$type
=
self
::
$_paramTypeMap
[
$type
];
}
else
{
throw
new
MysqliException
(
"Unkown type: '
{
$type
}
'"
);
}
}
$this
->
_bindedValues
[
$column
]
=&
$variable
;
$this
->
_bindedValues
[
0
][
$column
-
1
]
=
's'
;
return
true
;
}
/**
* {@inheritdoc}
*/
public
function
bindValue
(
$param
,
$value
,
$type
=
null
)
{
if
(
null
===
$type
)
{
$type
=
's'
;
}
else
{
if
(
isset
(
self
::
$_paramTypeMap
[
$type
]))
{
$type
=
self
::
$_paramTypeMap
[
$type
];
}
else
{
throw
new
MysqliException
(
"Unknown type: '
{
$type
}
'"
);
}
}
$this
->
_values
[
$param
]
=
$value
;
$this
->
_bindedValues
[
$param
]
=&
$this
->
_values
[
$param
];
$this
->
_bindedValues
[
0
][
$param
-
1
]
=
's'
;
return
true
;
}
/**
* {@inheritdoc}
*/
public
function
execute
(
$params
=
null
)
{
if
(
null
!==
$this
->
_bindedValues
)
{
if
(
null
!==
$params
)
{
if
(
!
$this
->
_bindValues
(
$params
))
{
return
false
;
}
}
else
{
if
(
!
call_user_func_array
(
array
(
$this
->
_stmt
,
'bind_param'
),
$this
->
_bindedValues
))
{
return
false
;
}
}
}
if
(
!
$this
->
_stmt
->
execute
())
{
throw
new
MysqliException
(
$this
->
_stmt
->
error
,
$this
->
_stmt
->
errno
);
}
if
(
null
===
$this
->
_columnNames
)
{
$meta
=
$this
->
_stmt
->
result_metadata
();
if
(
false
!==
$meta
)
{
$columnNames
=
array
();
foreach
(
$meta
->
fetch_fields
()
as
$col
)
{
$columnNames
[]
=
$col
->
name
;
}
$meta
->
free
();
$this
->
_columnNames
=
$columnNames
;
$this
->
_rowBindedValues
=
array_fill
(
0
,
count
(
$columnNames
),
NULL
);
$refs
=
array
();
foreach
(
$this
->
_rowBindedValues
as
$key
=>
&
$value
)
{
$refs
[
$key
]
=&
$value
;
}
if
(
!
call_user_func_array
(
array
(
$this
->
_stmt
,
'bind_result'
),
$refs
))
{
throw
new
MysqliException
(
$this
->
_stmt
->
error
,
$this
->
_stmt
->
errno
);
}
}
else
{
$this
->
_columnNames
=
false
;
}
}
// We have a result.
if
(
false
!==
$this
->
_columnNames
)
{
$this
->
_stmt
->
store_result
();
}
return
true
;
}
/**
* Bind a array of values to bound parameters
*
* @param array $values
* @return boolean
*/
private
function
_bindValues
(
$values
)
{
$params
=
array
();
$types
=
str_repeat
(
's'
,
count
(
$values
));
$params
[
0
]
=
$types
;
foreach
(
$values
as
&
$v
)
{
$params
[]
=&
$v
;
}
return
call_user_func_array
(
array
(
$this
->
_stmt
,
'bind_param'
),
$params
);
}
/**
* @return null|false|array
*/
private
function
_fetch
()
{
$ret
=
$this
->
_stmt
->
fetch
();
if
(
true
===
$ret
)
{
$values
=
array
();
// Aint there another way to copy values ?
foreach
(
$this
->
_rowBindedValues
as
$v
)
{
// Mysqli converts them to a scalar type it can fit in. Tests dont expect that.
$values
[]
=
null
===
$v
?
null
:
(
string
)
$v
;
}
return
$values
;
}
else
{
return
$ret
;
}
}
/**
* {@inheritdoc}
*/
public
function
fetch
(
$fetchStyle
=
null
)
{
$values
=
$this
->
_fetch
();
if
(
!
$values
)
{
return
$values
;
// false or null
}
$fetchStyle
=
$fetchStyle
?:
$this
->
_defaultFetchStyle
;
switch
(
$fetchStyle
)
{
case
PDO
::
FETCH_NUM
:
return
$values
;
case
PDO
::
FETCH_ASSOC
:
return
array_combine
(
$this
->
_columnNames
,
$values
);
case
PDO
::
FETCH_BOTH
:
$ret
=
array_combine
(
$this
->
_columnNames
,
$values
);
$ret
+=
$values
;
return
$ret
;
default
:
throw
new
MysqliException
(
sprintf
(
"Unknown fetch type '%s'"
,
$fetchStyle
));
}
}
/**
* {@inheritdoc}
*/
public
function
fetchAll
(
$fetchStyle
=
\PDO
::
FETCH_BOTH
)
{
$a
=
array
();
while
((
$row
=
$this
->
fetch
(
$fetchStyle
))
!=
null
)
{
$a
[]
=
$row
;
}
return
$a
;
}
/**
* {@inheritdoc}
*/
public
function
fetchColumn
(
$columnIndex
=
0
)
{
$values
=
$this
->
_fetch
();
if
(
!
$values
)
{
return
false
;
}
return
$values
[
$columnIndex
];
}
/**
* {@inheritdoc}
*/
public
function
errorCode
()
{
return
$this
->
_stmt
->
errno
;
}
/**
* {@inheritdoc}
*/
public
function
errorInfo
()
{
return
$this
->
_stmt
->
error
;
}
/**
* {@inheritdoc}
*/
public
function
closeCursor
()
{
$this
->
_stmt
->
free_result
();
return
true
;
}
/**
* {@inheritdoc}
*/
public
function
rowCount
()
{
if
(
false
===
$this
->
_columnNames
)
{
return
$this
->
_stmt
->
affected_rows
;
}
else
{
return
$this
->
_stmt
->
num_rows
;
}
}
/**
* {@inheritdoc}
*/
public
function
columnCount
()
{
return
$this
->
_stmt
->
field_count
;
}
/**
* {@inheritdoc}
*/
public
function
setFetchMode
(
$fetchMode
=
PDO
::
FETCH_BOTH
)
{
$this
->
_defaultFetchStyle
=
$fetchMode
;
}
/**
* {@inheritdoc}
*/
public
function
getIterator
()
{
$data
=
$this
->
fetchAll
(
$this
->
_defaultFetchStyle
);
return
new
\ArrayIterator
(
$data
);
}
}
lib/Doctrine/DBAL/DriverManager.php
View file @
ed584565
...
@@ -44,6 +44,7 @@ final class DriverManager
...
@@ -44,6 +44,7 @@ final class DriverManager
'ibm_db2'
=>
'Doctrine\DBAL\Driver\IBMDB2\DB2Driver'
,
'ibm_db2'
=>
'Doctrine\DBAL\Driver\IBMDB2\DB2Driver'
,
'pdo_ibm'
=>
'Doctrine\DBAL\Driver\PDOIbm\Driver'
,
'pdo_ibm'
=>
'Doctrine\DBAL\Driver\PDOIbm\Driver'
,
'pdo_sqlsrv'
=>
'Doctrine\DBAL\Driver\PDOSqlsrv\Driver'
,
'pdo_sqlsrv'
=>
'Doctrine\DBAL\Driver\PDOSqlsrv\Driver'
,
'mysqli'
=>
'Doctrine\DBAL\Driver\Mysqli\Driver'
,
);
);
/** Private constructor. This class cannot be instantiated. */
/** Private constructor. This class cannot be instantiated. */
...
...
tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
View file @
ed584565
...
@@ -164,6 +164,21 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
...
@@ -164,6 +164,21 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this
->
assertEquals
(
'foo'
,
$row
[
'test_string'
]);
$this
->
assertEquals
(
'foo'
,
$row
[
'test_string'
]);
}
}
public
function
testFetchBoth
()
{
$sql
=
"SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"
;
$row
=
$this
->
_conn
->
executeQuery
(
$sql
,
array
(
1
,
'foo'
))
->
fetch
();
$this
->
assertTrue
(
$row
!==
false
);
$row
=
array_change_key_case
(
$row
,
\CASE_LOWER
);
$this
->
assertEquals
(
1
,
$row
[
'test_int'
]);
$this
->
assertEquals
(
'foo'
,
$row
[
'test_string'
]);
$this
->
assertEquals
(
1
,
$row
[
0
]);
$this
->
assertEquals
(
'foo'
,
$row
[
1
]);
}
public
function
testFetchRow
()
public
function
testFetchRow
()
{
{
$sql
=
"SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"
;
$sql
=
"SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?"
;
...
...
tests/Doctrine/Tests/TestUtil.php
View file @
ed584565
...
@@ -53,6 +53,14 @@ class TestUtil
...
@@ -53,6 +53,14 @@ class TestUtil
'port'
=>
$GLOBALS
[
'tmpdb_port'
]
'port'
=>
$GLOBALS
[
'tmpdb_port'
]
);
);
if
(
isset
(
$GLOBALS
[
'db_unix_socket'
]))
{
$realDbParams
[
'unix_socket'
]
=
$GLOBALS
[
'db_unix_socket'
];
}
if
(
isset
(
$GLOBALS
[
'tmpdb_unix_socket'
]))
{
$tmpDbParams
[
'unix_socket'
]
=
$GLOBALS
[
'tmpdb_unix_socket'
];
}
$realConn
=
\Doctrine\DBAL\DriverManager
::
getConnection
(
$realDbParams
);
$realConn
=
\Doctrine\DBAL\DriverManager
::
getConnection
(
$realDbParams
);
$platform
=
$realConn
->
getDatabasePlatform
();
$platform
=
$realConn
->
getDatabasePlatform
();
...
...
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