Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Zend\Db transaction api unification #5001

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions library/Zend/Db/Adapter/Driver/AbstractConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Db\Adapter\Driver;

use Zend\Db\Adapter\Profiler;

abstract class AbstractConnection implements ConnectionInterface, Profiler\ProfilerAwareInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure there will be developers that will disagree with me and who's frame of reference is to always use inheritance even for trivial things, but this abstraction seems unnecessary to me. The guts of this class do nothing algorithmically interesting and since that is the case, I see little need to introduce a base type that all driver connection classes inherit from.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I told before, this abstract was a basis for another PR, so, I'm going to push it either in order to show you my final goal

{
/**
* @var array
*/
protected $connectionParameters = array();

/**
* @var string
*/
protected $driverName = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find any use of this attribute. What is his purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Maks3w $connectionParameters => used in connect()
$driverName => used in PDO adapter (So, I moved it in Abstract in order for other adapter to be able to be updated this way)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am with @Maks3w, if you never intend to use that property, or that property has no context in the current abstract, then it should not be in here.


/**
* @var boolean
*/
protected $inTransaction = false;

/**
* @var Profiler\ProfilerInterface
*/
protected $profiler = null;

/**
* @var resource
*/
protected $resource = null;

/**
* Disconnect
*
* @return self
*/
public function disconnect()
{
if ($this->isConnected()) {
$this->resource = null;
}

return $this;
}

/**
* Get connection parameters
*
* @return array
*/
public function getConnectionParameters()
{
return $this->connectionParameters;
}

/**
* Get driver name
*
* @return null|string
*/
public function getDriverName()
{
return $this->driverName;
}

/**
* @return null|Profiler\ProfilerInterface
*/
public function getProfiler()
{
return $this->profiler;
}

/**
* Get resource
*
* @return resource
*/
public function getResource()
{
if (!$this->isConnected()) {
$this->connect();
}

return $this->resource;
}

/**
* Checks whether the connection is in transaction state.
*
* @return boolean
*/
public function inTransaction()
{
return $this->inTransaction;
}

/**
* @param array $connectionParameters
* @return self
*/
public function setConnectionParameters(array $connectionParameters)
{
$this->connectionParameters = $connectionParameters;

return $this;
}

/**
* @param Profiler\ProfilerInterface $profiler
* @return self
*/
public function setProfiler(Profiler\ProfilerInterface $profiler)
{
$this->profiler = $profiler;

return $this;
}
}
107 changes: 18 additions & 89 deletions library/Zend/Db/Adapter/Driver/IbmDb2/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,17 @@

namespace Zend\Db\Adapter\Driver\IbmDb2;

use Zend\Db\Adapter\Driver\AbstractConnection;
use Zend\Db\Adapter\Driver\ConnectionInterface;
use Zend\Db\Adapter\Exception;
use Zend\Db\Adapter\Profiler;

class Connection implements ConnectionInterface, Profiler\ProfilerAwareInterface
class Connection extends AbstractConnection
{
/**
* @var IbmDb2
* @var IbmDb2
*/
protected $driver = null;

/**
* @var array
*/
protected $connectionParameters = null;

/**
* @var resource
*/
protected $resource = null;

/**
* @var Profiler\ProfilerInterface
*/
protected $profiler = null;

/**
* In transaction
*
* @var bool
*/
protected $inTransaction = false;

/**
* i5 OS
*
Expand Down Expand Up @@ -78,61 +56,27 @@ public function __construct($connectionParameters = null)
/**
* Set driver
*
* @param IbmDb2 $driver
* @return Connection
* @param IbmDb2 $driver
* @return self
*/
public function setDriver(IbmDb2 $driver)
{
$this->driver = $driver;
return $this;
}

/**
* @param Profiler\ProfilerInterface $profiler
* @return Connection
*/
public function setProfiler(Profiler\ProfilerInterface $profiler)
{
$this->profiler = $profiler;
return $this;
}

/**
* @return null|Profiler\ProfilerInterface
*/
public function getProfiler()
{
return $this->profiler;
}

/**
* @param array $connectionParameters
* @return Connection
*/
public function setConnectionParameters(array $connectionParameters)
{
$this->connectionParameters = $connectionParameters;
return $this;
}

/**
* @return array
*/
public function getConnectionParameters()
{
return $this->connectionParameters;
}

/**
* @param resource $resource DB2 resource
* @return Connection
* @param resource $resource DB2 resource
* @return self
*/
public function setResource($resource)
{
if (!is_resource($resource) || get_resource_type($resource) !== 'DB2 Connection') {
throw new Exception\InvalidArgumentException('The resource provided must be of type "DB2 Connection"');
}
$this->resource = $resource;

return $this;
}

Expand All @@ -148,17 +92,8 @@ public function getCurrentSchema()
}

$info = db2_server_info($this->resource);
return (isset($info->DB_NAME) ? $info->DB_NAME : '');
}

/**
* Get resource
*
* @return mixed
*/
public function getResource()
{
return $this->resource;
return (isset($info->DB_NAME) ? $info->DB_NAME : '');
}

/**
Expand Down Expand Up @@ -233,7 +168,7 @@ public function disconnect()
/**
* Begin transaction
*
* @return ConnectionInterface
* @return self
*/
public function beginTransaction()
{
Expand All @@ -250,23 +185,14 @@ public function beginTransaction()
$this->prevAutocommit = db2_autocommit($this->resource);
db2_autocommit($this->resource, DB2_AUTOCOMMIT_OFF);
$this->inTransaction = true;
return $this;
}

/**
* In transaction
*
* @return bool
*/
public function inTransaction()
{
return $this->inTransaction;
return $this;
}

/**
* Commit
*
* @return ConnectionInterface
* @return self
*/
public function commit()
{
Expand All @@ -283,21 +209,22 @@ public function commit()
}

$this->inTransaction = false;

return $this;
}

/**
* Rollback
*
* @return ConnectionInterface
* @return self
*/
public function rollback()
{
if (!$this->resource) {
if (!$this->isConnected()) {
throw new Exception\RuntimeException('Must be connected before you can rollback.');
}

if (!$this->inTransaction) {
if (!$this->inTransaction()) {
throw new Exception\RuntimeException('Must call beginTransaction() before you can rollback.');
}

Expand All @@ -310,6 +237,7 @@ public function rollback()
}

$this->inTransaction = false;

return $this;
}

Expand Down Expand Up @@ -368,6 +296,7 @@ protected function isI5()
}

$this->i5 = php_uname('s') == 'OS400' ? true : false;

return $this->i5;
}
}
Loading