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

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Dec 6, 2017
2 parents ab1f2e8 + 83dbe94 commit 6113b68
Show file tree
Hide file tree
Showing 20 changed files with 468 additions and 33 deletions.
40 changes: 40 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,46 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.9.0 - TBD

### Added

- [#216](https://github.com/zendframework/zend-db/pull/216) added AFTER support
in ALTER TABLE syntax for MySQL
- [#223](https://github.com/zendframework/zend-db/pull/223) added support for
empty values set with IN predicate
- [#271](https://github.com/zendframework/zend-db/pull/271) added support for
dash character on MySQL identifier
- [#273](https://github.com/zendframework/zend-db/pull/273) added support for
implementing an error handler for db2_prepare
- [#275](https://github.com/zendframework/zend-db/pull/275) added support for
LIMIT OFFSET for db2
- [#280](https://github.com/zendframework/zend-db/pull/280) added version dsn
parameter for pdo_dblib

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- [#205](https://github.com/zendframework/zend-db/pull/205) fixes the spaces in
ORDER BY syntax
- [#229](https://github.com/zendframework/zend-db/pull/229) fixes the support
of SSL for mysqli
- [#255](https://github.com/zendframework/zend-db/pull/255) fixes ResultSet with
array values
- [#261](https://github.com/zendframework/zend-db/pull/261) fixes Exception in
Firebird driver doesn't support lastInsertId
- [#276](https://github.com/zendframework/zend-db/pull/276) fixes the support
of PHP 7.2
- [#287](https://github.com/zendframework/zend-db/pull/287) fixes the usage of
count() with PHP 7.2

## 2.8.3 - TBD

### Added
Expand Down
11 changes: 10 additions & 1 deletion docs/book/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,16 @@ $select->columns(['foo', 'bar']);

// As an associative array with aliases as the keys
// (produces 'bar' AS 'foo', 'bax' AS 'baz')
$select->columns(['foo' => 'bar', 'baz' => 'bax']);
$select->columns([
'foo' => 'bar',
'baz' => 'bax'
]);

// Sql function call on the column
// (produces CONCAT_WS('/', 'bar', 'bax') AS 'foo')
$select->columns([
'foo' => new \Zend\Db\Sql\Expression("CONCAT_WS('/', 'bar', 'bax')")
]);
```

### join()
Expand Down
5 changes: 4 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0"?>
<ruleset name="Zend Framework coding standard">
<rule ref="./vendor/zendframework/zend-coding-standard/ruleset.xml"/>
<rule ref="./vendor/zendframework/zend-coding-standard/ruleset.xml" />
<rule ref="PSR1.Files.SideEffects">
<exclude-pattern>*/test/Adapter/Driver/IbmDb2/StatementTest.php</exclude-pattern>
</rule>

<!-- Paths to check -->
<file>src</file>
Expand Down
37 changes: 36 additions & 1 deletion src/Adapter/Driver/IbmDb2/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Zend\Db\Adapter\Driver\IbmDb2;

use ErrorException;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Adapter\Exception;
use Zend\Db\Adapter\ParameterContainer;
Expand Down Expand Up @@ -172,7 +173,14 @@ public function prepare($sql = null)
$sql = $this->sql;
}

$this->resource = db2_prepare($this->db2, $sql);
try {
set_error_handler($this->createErrorHandler());
$this->resource = db2_prepare($this->db2, $sql);
} catch (ErrorException $e) {
throw new Exception\RuntimeException($e->getMessage() . '. ' . db2_stmt_errormsg(), db2_stmt_error(), $e);
} finally {
restore_error_handler();
}

if ($this->resource === false) {
throw new Exception\RuntimeException(db2_stmt_errormsg(), db2_stmt_error());
Expand Down Expand Up @@ -239,4 +247,31 @@ public function execute($parameters = null)
$result = $this->driver->createResult($this->resource);
return $result;
}

/**
* Creates and returns a callable error handler that raises exceptions.
*
* Only raises exceptions for errors that are within the error_reporting mask.
*
* @return callable
*/
private function createErrorHandler()
{
/**
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return void
* @throws ErrorException if error is not within the error_reporting mask.
*/
return function ($errno, $errstr, $errfile, $errline) {
if (! (error_reporting() & $errno)) {
// error_reporting does not include this error
return;
}

throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
};
}
}
7 changes: 3 additions & 4 deletions src/Adapter/Driver/Mysqli/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,10 @@ public function key()
*/
public function rewind()
{
if ($this->position !== 0) {
if ($this->isBuffered === false) {
throw new Exception\RuntimeException('Unbuffered results cannot be rewound for multiple iterations');
}
if (0 !== $this->position && false === $this->isBuffered) {
throw new Exception\RuntimeException('Unbuffered results cannot be rewound for multiple iterations');
}

$this->resource->data_seek(0); // works for both mysqli_result & mysqli_stmt
$this->currentComplete = false;
$this->position = 0;
Expand Down
6 changes: 6 additions & 0 deletions src/Adapter/Driver/Pdo/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ public function connect()
case 'unix_socket':
$unix_socket = (string) $value;
break;
case 'version':
$version = (string) $value;
break;
case 'driver_options':
case 'options':
$value = (array) $value;
Expand Down Expand Up @@ -250,6 +253,9 @@ public function connect()
if (isset($unix_socket)) {
$dsn[] = "unix_socket={$unix_socket}";
}
if (isset($version)) {
$dsn[] = "version={$version}";
}
break;
}
$dsn = $pdoDriver . ':' . implode(';', $dsn);
Expand Down
7 changes: 6 additions & 1 deletion src/Adapter/Platform/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ abstract class AbstractPlatform implements PlatformInterface
*/
protected $quoteIdentifiers = true;

/**
* @var string
*/
protected $quoteIdentifierFragmentPattern = '/([^0-9,a-z,A-Z$_:])/i';

/**
* {@inheritDoc}
*/
Expand All @@ -42,7 +47,7 @@ public function quoteIdentifierInFragment($identifier, array $safeWords = [])
}

$parts = preg_split(
'/([^0-9,a-z,A-Z$_:])/i',
$this->quoteIdentifierFragmentPattern,
$identifier,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
Expand Down
7 changes: 7 additions & 0 deletions src/Adapter/Platform/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class Mysql extends AbstractPlatform
*/
protected $resource = null;

/**
* NOTE: Include dashes for MySQL only, need tests for others platforms
*
* @var string
*/
protected $quoteIdentifierFragmentPattern = '/([^0-9,a-z,A-Z$_\-:])/i';

/**
* @param null|\Zend\Db\Adapter\Driver\Mysqli\Mysqli|\Zend\Db\Adapter\Driver\Pdo\Pdo|\mysqli|\PDO $driver
*/
Expand Down
41 changes: 40 additions & 1 deletion src/Sql/Platform/IbmDb2/SelectDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ class SelectDecorator extends Select implements PlatformDecoratorInterface
*/
protected $subject = null;

/**
/**
* @var bool
*/
protected $supportsLimitOffset = false;


/**
* @return bool
*/
public function getIsSelectContainDistinct()
Expand All @@ -51,6 +57,22 @@ public function setSubject($select)
$this->subject = $select;
}

/**
* @return bool
*/
public function getSupportsLimitOffset()
{
return $this->supportsLimitOffset;
}

/**
* @param bool $supportsLimitOffset
*/
public function setSupportsLimitOffset($supportsLimitOffset)
{
$this->supportsLimitOffset = $supportsLimitOffset;
}

/**
* @see Select::renderTable
*/
Expand Down Expand Up @@ -87,6 +109,23 @@ protected function processLimitOffset(
return;
}

if ($this->supportsLimitOffset) {
// Note: db2_prepare/db2_execute fails with positional parameters, for LIMIT & OFFSET
$limit = (int) $this->limit;
if (! $limit) {
return;
}

$offset = (int) $this->offset;
if ($offset) {
array_push($sqls, sprintf("LIMIT %s OFFSET %s", $limit, $offset));
return;
}

array_push($sqls, sprintf("LIMIT %s", $limit));
return;
}

$selectParameters = $parameters[self::SELECT];

$starSuffix = $platform->getIdentifierSeparator() . self::SQL_STAR;
Expand Down
4 changes: 4 additions & 0 deletions src/Sql/Platform/Mysql/Ddl/AlterTableDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AlterTableDecorator extends AlterTable implements PlatformDecoratorInterfa
'columnformat' => 4,
'format' => 4,
'storage' => 5,
'after' => 6
];

/**
Expand Down Expand Up @@ -130,6 +131,9 @@ protected function processAddColumns(PlatformInterface $adapterPlatform = null)
$insert = ' STORAGE ' . strtoupper($coValue);
$j = 2;
break;
case 'after':
$insert = ' AFTER ' . $adapterPlatform->quoteIdentifier($coValue);
$j = 2;
}

if ($insert) {
Expand Down
5 changes: 3 additions & 2 deletions src/Sql/Predicate/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct($identifier = null, $valueSet = null)
if ($identifier) {
$this->setIdentifier($identifier);
}
if ($valueSet) {
if ($valueSet !== null) {
$this->setValueSet($valueSet);
}
}
Expand Down Expand Up @@ -122,9 +122,10 @@ public function getExpressionData()
foreach ($values as $argument) {
list($replacements[], $types[]) = $this->normalizeArgument($argument, self::TYPE_VALUE);
}
$valuePlaceholders = count($values) > 0 ? array_fill(0, count($values), '%s') : [];
$specification = vsprintf(
$this->specification,
[$identifierSpecFragment, '(' . implode(', ', array_fill(0, count($values), '%s')) . ')']
[$identifierSpecFragment, '(' . implode(', ', $valuePlaceholders) . ')']
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/TableGateway/AbstractTableGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function select($where = null)

/**
* @param Select $select
* @return null|ResultSetInterface
* @return ResultSetInterface
* @throws \RuntimeException
*/
public function selectWith(Select $select)
Expand Down
21 changes: 19 additions & 2 deletions test/Adapter/Driver/IbmDb2/StatementIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\TestCase;
use Zend\Db\Adapter\Driver\IbmDb2\IbmDb2;
use Zend\Db\Adapter\Driver\IbmDb2\Statement;
use Zend\Db\Adapter\Exception\RuntimeException;

/**
* @group integration
Expand Down Expand Up @@ -74,7 +75,7 @@ public function testGetResource()

$statement = new Statement;
$statement->initialize($db2Resource);
$statement->prepare("SELECT 'foo'");
$statement->prepare("SELECT 'foo' FROM sysibm.sysdummy1");
$resource = $statement->getResource();
self::assertEquals('DB2 Statement', get_resource_type($resource));
unset($resource, $db2Resource);
Expand All @@ -101,7 +102,23 @@ public function testPrepare()
}

/**
* @covers \Zend\Db\Adapter\Driver\IbmDb2\Statement::execute
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::prepare
*/
public function testPrepareThrowsAnExceptionOnFailure()
{
$db2Resource = db2_connect(
$this->variables['database'],
$this->variables['username'],
$this->variables['password']
);
$statement = new Statement;
$statement->initialize($db2Resource);
$this->setExpectedException(RuntimeException::class);
$statement->prepare("SELECT");
}

/**
* @covers Zend\Db\Adapter\Driver\IbmDb2\Statement::execute
*/
public function testExecute()
{
Expand Down
Loading

0 comments on commit 6113b68

Please sign in to comment.