Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue#237 - Symbol "#" is valid for identifiers for Oracle platform #238

Open
wants to merge 2 commits into
base: 2.14.x
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions src/Adapter/Platform/Oracle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@

class Oracle extends AbstractPlatform
{
/**
* Override default pattern '/([^0-9,a-z,A-Z$_:])/i'
*
* @var string $quoteIdentifierFragmentPattern
*/
protected $quoteIdentifierFragmentPattern = '/([^0-9,a-z,A-Z$_:#])/i';

/** @var null|Pdo|Oci8|\PDO */
protected $resource;

Expand Down
131 changes: 131 additions & 0 deletions test/integration/Adapter/Driver/Oci8/CharsetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace LaminasIntegrationTest\Db\Adapter\Driver\Oci8;

use Laminas\Db\Adapter\Adapter;
use Laminas\Db\Adapter\Driver\ResultInterface;
use Laminas\Db\Sql\Select;
use Laminas\Db\TableGateway\TableGateway;
use PHPUnit\Framework\TestCase;

use function sprintf;

class CharsetTest extends TestCase
{
use TraitSetup;

/**
* @covers \Laminas\Db\TableGateway\TableGateway::__construct
*/
public function testConstructor()
{
$adapter = $this->createAdapterWithQuoteIdentifiers();
$tableGateway = new TableGateway('test_charset', $adapter);
$this->assertInstanceOf(TableGateway::class, $tableGateway);
}

public function testSelectWithAdapterDirectSql()
{
$adapter = $this->createAdapterWithQuoteIdentifiers();

$expectedSql = 'select id, field$, field_, field# from test_charset';

$statement = $adapter->createStatement($expectedSql);

$actualSql = $statement->getSql();
$this->assertEquals($expectedSql, $actualSql);

$result = $statement->execute();
$this->assertInstanceOf(ResultInterface::class, $result);
}

public function testSelectQuoteFieldName()
{
$adapter = $this->createAdapterWithQuoteIdentifiers();
$select = new Select('TEST_CHARSET');
$select->columns([
'FIELD$',
'FIELD_',
'FIELD#',
]);
$plarform = $adapter->getPlatform();
$sqlString = $select->getSqlString($plarform);
$statement = $adapter->createStatement($sqlString);
$result = $statement->execute();
$this->assertInstanceOf(ResultInterface::class, $result);
}

/**
* @see https://github.com/zendframework/zend-db/issues/35
* @see https://github.com/zendframework/zend-db/pull/178
*
* @return mixed
*/
public function testInsertWithTableGateway()
{
$adapter = $this->createAdapterWithQuoteIdentifiers();
$tableGateway = new TableGateway('TEST_CHARSET', $adapter);

$affectedRows = $tableGateway->insert([
'FIELD$' => 'field$value',
'FIELD_' => 'field_value',
'FIELD#' => 'field#value',
]);
$this->assertEquals(1, $affectedRows);

//$result = $tableGateway->getLastInsertValue(); // Is not avaliable.
return $this->getMaxIdFrom($adapter, 'TEST_CHARSET');
}

/**
* @param string $idFieldName
* @return mixed
*/
public function getMaxIdFrom(
Adapter $adapter,
string $tableName,
$idFieldName = 'ID'
) {
/**
* @warning: "$adapter->query" does not work in the same transaction.
*/
//$resultSet = $adapter->query($sql, Adapter::QUERY_MODE_EXECUTE);
//$resultArr = $resultSet->toArray();
//$result = $resultArr[0]['LAST_ID'];

$connection = $adapter->getDriver()->getConnection();
$sql = sprintf(
'select max(%s) as last_id from %s',
$idFieldName,
$tableName
);
$resultSet = $connection->execute($sql);
$row = $resultSet->current();
return $row['LAST_ID'];
}

/**
* @depends testInsertWithExtendedCharsetFieldName
* @param mixed $id
*/
public function testUpdateWithTableGateway($id)
{
$adapter = $this->createAdapterWithQuoteIdentifiers();
$tableGateway = new TableGateway('test_charset', $adapter);

$data = [
'FIELD$' => 'test$alue3',
'FIELD_' => 'test_value4',
'FIELD#' => 'test#value4',
];
$affectedRows = $tableGateway->update($data, ['id' => $id]);
$this->assertEquals(1, $affectedRows);

$rowSet = $tableGateway->select(['id' => $id]);
$row = $rowSet->current();

foreach ($data as $key => $value) {
$this->assertEquals($row->$key, $value);
}
}
}
25 changes: 25 additions & 0 deletions test/integration/Adapter/Driver/Oci8/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace LaminasIntegrationTest\Db\Adapter\Driver\Oci8;

use PHPUnit\Framework\TestCase;

/**
* @group integration
* @group integration-oracle
*/
class ConnectionTest extends TestCase
{
use TraitSetup;

public function testConnectionOk()
{
$connection = $this->createConnection();
$connection->connect();

$result = $connection->isConnected();
self::assertTrue($result);

$connection->disconnect();
}
}
119 changes: 119 additions & 0 deletions test/integration/Adapter/Driver/Oci8/TraitSetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace LaminasIntegrationTest\Db\Adapter\Driver\Oci8;

use Laminas\Db\Adapter\Adapter;
use Laminas\Db\Adapter\Driver\Oci8\Connection;
use Laminas\Db\Adapter\Platform;
use Laminas\Db\Adapter\Profiler;
use Laminas\Db\ResultSet;

use function array_merge;
use function extension_loaded;
use function getenv;
use function sprintf;

// phpcs:ignore WebimpressCodingStandard.NamingConventions.Trait.Suffix
trait TraitSetup
{
/**
* Options for adapter from "phpunit.xml".
*
* @var array $variables
*/
protected $variables = [
'hostname' => 'TESTS_LAMINAS_DB_ADAPTER_DRIVER_OCI8_HOSTNAME',
'username' => 'TESTS_LAMINAS_DB_ADAPTER_DRIVER_OCI8_USERNAME',
'password' => 'TESTS_LAMINAS_DB_ADAPTER_DRIVER_OCI8_PASSWORD',
'database' => 'TESTS_LAMINAS_DB_ADAPTER_DRIVER_OCI8_DATABASE',
];

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp(): void
{
if (! getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_OCI8')) {
$this->markTestSkipped('OCI8 integration test disabled');
}

if (! extension_loaded('oci8')) {
$this->fail('The phpunit group integration-oci8 was enabled, but the extension "oci8" is not loaded.');
}

foreach ($this->variables as $name => $value) {
if (! getenv($value)) {
$this->markTestSkipped(sprintf(
'Missing required variable %s from phpunit.xml for this integration test',
$value
));
}
$this->variables[$name] = getenv($value);
}
$this->variables['hostname'] .= '/' . $this->variables['database'];
unset($this->variables['database']);
}

protected function createConnection(): Connection
{
return new Connection($this->variables);
}

/**
* @return array
*/
protected function getDriverConfig()
{
return [
'driver' => 'Oci8',
'hostname' => $this->variables['hostname'],
'username' => $this->variables['username'],
'password' => $this->variables['password'],
'charset' => 'AL32UTF8',
// 'platform_options' => [
// 'quote_identifiers' => true
// ],
];
}

protected function createAdapter(
?Platform\PlatformInterface $platform = null,
?ResultSet\ResultSetInterface $queryResultPrototype = null,
?Profiler\ProfilerInterface $profiler = null
): Adapter {
$driverConfig = $this->getDriverConfig();
return new Adapter($driverConfig, $platform, $queryResultPrototype, $profiler);
}

protected function createAdapterWithQuoteIdentifiers(
?Platform\PlatformInterface $platform = null,
?ResultSet\ResultSetInterface $queryResultPrototype = null,
?Profiler\ProfilerInterface $profiler = null
): Adapter {
$driverConfig = $this->getDriverConfig();
$driverConfig = array_merge([
'platform_options' => [],
], $driverConfig);
$driverConfig['platform_options']['quote_identifiers'] = true;
return new Adapter($driverConfig, $platform, $queryResultPrototype, $profiler);
}

protected function createAdapterWithoutQuoteIdentifiers(
?Platform\PlatformInterface $platform = null,
?ResultSet\ResultSetInterface $queryResultPrototype = null,
?Profiler\ProfilerInterface $profiler = null
): Adapter {
/**
* @see \Laminas\Db\Adapter::createDriver ['oci8', 'pdo']
*
* @var $driverConfig - a config for oracle 'oci8' driver.
*/
$driverConfig = $this->getDriverConfig();
$driverConfig = array_merge([
'platform_options' => [],
], $driverConfig);
$driverConfig['platform_options']['quote_identifiers'] = false;
return new Adapter($driverConfig, $platform, $queryResultPrototype, $profiler);
}
}
5 changes: 5 additions & 0 deletions test/integration/IntegrationTestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use LaminasIntegrationTest\Db\Platform\FixtureLoader;
use LaminasIntegrationTest\Db\Platform\MysqlFixtureLoader;
use LaminasIntegrationTest\Db\Platform\OracleFixtureLoader;
use LaminasIntegrationTest\Db\Platform\PgsqlFixtureLoader;
use LaminasIntegrationTest\Db\Platform\SqlServerFixtureLoader;
use PHPUnit\Framework\TestListener;
Expand Down Expand Up @@ -39,6 +40,10 @@ public function startTestSuite(TestSuite $suite): void
$this->fixtureLoaders[] = new SqlServerFixtureLoader();
}

if (getenv('TESTS_LAMINAS_DB_ADAPTER_DRIVER_OCI8')) {
$this->fixtureLoaders[] = new OracleFixtureLoader();
}

if (empty($this->fixtureLoaders)) {
return;
}
Expand Down
Loading