Commit 88d16346 authored by Kim Hemsø Rasmussen's avatar Kim Hemsø Rasmussen

Fixed some of the things Stof pointed out.

parent ed584565
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
...@@ -24,7 +22,7 @@ namespace Doctrine\DBAL\Driver\Mysqli; ...@@ -24,7 +22,7 @@ namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver as DriverInterface; use Doctrine\DBAL\Driver as DriverInterface;
/** /**
* @author Kim Hensø Rasmussen <kimhemsoe@gmail.com> * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
*/ */
class Driver implements DriverInterface class Driver implements DriverInterface
{ {
......
...@@ -24,7 +24,7 @@ namespace Doctrine\DBAL\Driver\Mysqli; ...@@ -24,7 +24,7 @@ namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver\Connection as ConnectionInterface; use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
/** /**
* @author Kim Hensø Rasmussen <kimhemsoe@gmail.com> * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
*/ */
class MysqliConnection implements ConnectionInterface class MysqliConnection implements ConnectionInterface
{ {
...@@ -40,8 +40,7 @@ class MysqliConnection implements ConnectionInterface ...@@ -40,8 +40,7 @@ class MysqliConnection implements ConnectionInterface
$this->_conn = new \mysqli($params['host'], $username, $password, $params['dbname'], $port, $socket); $this->_conn = new \mysqli($params['host'], $username, $password, $params['dbname'], $port, $socket);
if (isset($params['charset'])) if (isset($params['charset'])) {
{
$this->_conn->set_charset($params['charset']); $this->_conn->set_charset($params['charset']);
} }
} }
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
/** /**
* @author Kim Hensø Rasmussen <kimhemsoe@gmail.com> * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
*/ */
class MysqliException extends \Exception class MysqliException extends \Exception
{} {}
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
...@@ -21,13 +19,12 @@ ...@@ -21,13 +19,12 @@
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use PDO; use PDO;
/** /**
* @author Kim Hensø Rasmussen <kimhemsoe@gmail.com> * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
*/ */
class MysqliStatement implements \IteratorAggregate, StatementInterface class MysqliStatement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement
{ {
private static $_paramTypeMap = array( private static $_paramTypeMap = array(
PDO::PARAM_STR => 's', PDO::PARAM_STR => 's',
...@@ -149,8 +146,7 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface ...@@ -149,8 +146,7 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface
if (null === $this->_columnNames) { if (null === $this->_columnNames) {
$meta = $this->_stmt->result_metadata(); $meta = $this->_stmt->result_metadata();
if (false !== $meta) if (false !== $meta) {
{
$columnNames = array(); $columnNames = array();
foreach ($meta->fetch_fields() as $col) { foreach ($meta->fetch_fields() as $col) {
$columnNames[] = $col->name; $columnNames[] = $col->name;
...@@ -161,25 +157,20 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface ...@@ -161,25 +157,20 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface
$this->_rowBindedValues = array_fill(0, count($columnNames), NULL); $this->_rowBindedValues = array_fill(0, count($columnNames), NULL);
$refs = array(); $refs = array();
foreach ($this->_rowBindedValues as $key => &$value) foreach ($this->_rowBindedValues as $key => &$value) {
{
$refs[$key] =& $value; $refs[$key] =& $value;
} }
if (!call_user_func_array(array($this->_stmt, 'bind_result'), $refs)) if (!call_user_func_array(array($this->_stmt, 'bind_result'), $refs)) {
{
throw new MysqliException($this->_stmt->error, $this->_stmt->errno); throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
} }
} } else {
else
{
$this->_columnNames = false; $this->_columnNames = false;
} }
} }
// We have a result. // We have a result.
if (false !== $this->_columnNames) if (false !== $this->_columnNames) {
{
$this->_stmt->store_result(); $this->_stmt->store_result();
} }
return true; return true;
...@@ -212,15 +203,13 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface ...@@ -212,15 +203,13 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface
if (true === $ret) { if (true === $ret) {
$values = array(); $values = array();
// Aint there another way to copy values ?
foreach ($this->_rowBindedValues as $v) { foreach ($this->_rowBindedValues as $v) {
// Mysqli converts them to a scalar type it can fit in. Tests dont expect that. // Mysqli converts them to a scalar type it can fit in. Tests dont expect that.
$values[] = null === $v ? null : (string)$v; $values[] = null === $v ? null : (string)$v;
} }
return $values; return $values;
} else {
return $ret;
} }
return $ret;
} }
/** /**
...@@ -229,8 +218,10 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface ...@@ -229,8 +218,10 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface
public function fetch($fetchStyle = null) public function fetch($fetchStyle = null)
{ {
$values = $this->_fetch(); $values = $this->_fetch();
if (!$values) { if (null === $values) {
return $values; // false or null return null;
} elseif (false === $values) {
throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
} }
$fetchStyle = $fetchStyle ?: $this->_defaultFetchStyle; $fetchStyle = $fetchStyle ?: $this->_defaultFetchStyle;
...@@ -249,7 +240,7 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface ...@@ -249,7 +240,7 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface
return $ret; return $ret;
default: default:
throw new MysqliException(sprintf("Unknown fetch type '%s'", $fetchStyle)); throw new MysqliException("Unknown fetch type '{$fetchStyle}'");
} }
} }
...@@ -259,7 +250,7 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface ...@@ -259,7 +250,7 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface
public function fetchAll($fetchStyle = \PDO::FETCH_BOTH) public function fetchAll($fetchStyle = \PDO::FETCH_BOTH)
{ {
$a = array(); $a = array();
while (($row = $this->fetch($fetchStyle)) != null) { while (($row = $this->fetch($fetchStyle)) !== null) {
$a[] = $row; $a[] = $row;
} }
return $a; return $a;
...@@ -271,8 +262,10 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface ...@@ -271,8 +262,10 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface
public function fetchColumn($columnIndex = 0) public function fetchColumn($columnIndex = 0)
{ {
$values = $this->_fetch(); $values = $this->_fetch();
if (!$values) { if (null === $values) {
return false; return null;
} elseif (false === $values) {
throw new MysqliException($this->_stmt->error, $this->_stmt->errno);
} }
return $values[$columnIndex]; return $values[$columnIndex];
} }
...@@ -309,9 +302,8 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface ...@@ -309,9 +302,8 @@ class MysqliStatement implements \IteratorAggregate, StatementInterface
{ {
if(false === $this->_columnNames) { if(false === $this->_columnNames) {
return $this->_stmt->affected_rows; return $this->_stmt->affected_rows;
} else {
return $this->_stmt->num_rows;
} }
return $this->_stmt->num_rows;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment