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

[2.0] Adding support for PDO::ATTR_STATEMENT_CLASS #70

Merged
merged 5 commits into from
Sep 4, 2019
Merged
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
21 changes: 12 additions & 9 deletions src/Pdo/Oci8.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,22 @@ public function prepare($statement, $options = null)
$this->table = $matches[1];
}

// Prepare the statement
$sth = @oci_parse($this->dbh, $statement);

if (! $sth) {
$e = oci_error($this->dbh);
throw new Oci8Exception($e['message']);
}

if (! is_array($options)) {
$options = [];
}

return new Statement($sth, $this, $options);
// Prepare the statement
if ($objClass = $this->getAttribute(PDO::ATTR_STATEMENT_CLASS)) {
$sth = new $objClass[0](...$objClass[1]);
if (! $sth instanceof Statement) {
throw new Oci8Exception("Class '$stmClass' must be an instance of ".Statement::class);
}
} else {
$sth = new Statement($this, $options);
}
$sth->parse($this->dbh, $statement);

return $sth;
}

/**
Expand Down
35 changes: 25 additions & 10 deletions src/Pdo/Oci8/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,12 @@ class Statement extends PDOStatement
/**
* Constructor.
*
* @param resource $sth Statement handle created with oci_parse()
* @param Oci8 $connection The Pdo_Oci8 object for this statement
* @param array $options Options for the statement handle
* @throws Oci8Exception
*/
public function __construct($sth, Oci8 $connection, array $options = [])
public function __construct(Oci8 $connection, array $options = [])
{
if (strtolower(get_resource_type($sth)) != 'oci8 statement') {
throw new Oci8Exception(
'Resource expected of type oci8 statement; '
. (string) get_resource_type($sth) . ' received instead'
);
}

$this->sth = $sth;
$this->connection = $connection;
$this->options = $options;

Expand All @@ -133,6 +124,30 @@ public function __construct($sth, Oci8 $connection, array $options = [])
}
}

/**
* Prepare the statement.
*
* @param resource $dbh
* @param string $statement
* @throws Oci8Exception
*/
public function parse($dbh, $statement)
{
$resourceType = strtolower(get_resource_type($dbh));
if (! in_array($resourceType, ['oci8 connection', 'oci8 persistent connection'])) {
throw new Oci8Exception(
'Resource expected of type "oci8 connection" or "oci8 persistent connection" but got "'
. (string) get_resource_type($dbh) . '" instead.'
);
}

$this->sth = oci_parse($dbh, $statement);
if (! $this->sth) {
$e = oci_error($dbh);
throw new Oci8Exception($e['message']);
}
}

/**
* Executes a prepared statement.
*
Expand Down
43 changes: 34 additions & 9 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

class ConnectionTest extends TestCase
{
const DEFAULT_USER = 'system';
const DEFAULT_PWD = 'oracle';
const DEFAULT_DSN = 'oci:dbname=127.0.0.1:49161/xe';

/**
* @var Oci8
*/
protected $con = null;

/**
Expand All @@ -14,9 +21,9 @@ class ConnectionTest extends TestCase
*/
public function setUp()
{
$user = getenv('OCI_USER') ?: 'system';
$pwd = getenv('OCI_PWD') ?: 'oracle';
$dsn = getenv('OCI_DSN') ?: 'oci:dbname=127.0.0.1:49161/xe';
$user = getenv('OCI_USER') ?: self::DEFAULT_USER;
$pwd = getenv('OCI_PWD') ?: self::DEFAULT_PWD;
$dsn = getenv('OCI_DSN') ?: self::DEFAULT_DSN;
$this->con = new Oci8($dsn, $user, $pwd);
}

Expand All @@ -37,9 +44,9 @@ public function testObject()
*/
public function testPersistentConnection()
{
$user = getenv('OCI_USER') ?: 'system';
$pwd = getenv('OCI_PWD') ?: 'oracle';
$dsn = getenv('OCI_DSN') ?: 'oci:dbname=127.0.0.1:49161/xe';
$user = getenv('OCI_USER') ?: self::DEFAULT_USER;
$pwd = getenv('OCI_PWD') ?: self::DEFAULT_PWD;
$dsn = getenv('OCI_DSN') ?: self::DEFAULT_DSN;
$con = new Oci8($dsn, $user, $pwd, [\PDO::ATTR_PERSISTENT => true]);
$this->assertNotNull($con);
}
Expand All @@ -51,9 +58,9 @@ public function testPersistentConnection()
*/
public function testConnectionWithParameters()
{
$user = getenv('OCI_USER') ?: 'system';
$pwd = getenv('OCI_PWD') ?: 'oracle';
$dsn = getenv('OCI_DSN') ?: 'oci:dbname=127.0.0.1:49161/xe';
$user = getenv('OCI_USER') ?: self::DEFAULT_USER;
$pwd = getenv('OCI_PWD') ?: self::DEFAULT_PWD;
$dsn = getenv('OCI_DSN') ?: self::DEFAULT_DSN;
$con = new Oci8("$dsn;charset=utf8", $user, $pwd);
$this->assertNotNull($con);
}
Expand Down Expand Up @@ -185,6 +192,13 @@ public function testClose()
$this->assertEquals(['00000', null, null], $this->con->errorInfo());
}

public function testOtherStatementClass()
{
$this->con->setAttribute(\PDO::ATTR_STATEMENT_CLASS, [TestStatement::class, [$this->con]]);
$stmt = $this->con->prepare('INSERT INTO person, email (name) VALUES (:person, :email)');
$this->assertEquals(1, TestStatement::$called);
}

public function testBindParamSingle()
{
$stmt = $this->con->prepare('INSERT INTO person (name) VALUES (?)');
Expand All @@ -201,3 +215,14 @@ public function testBindParamMultiple()
$this->assertTrue($stmt->bindParam(':email', $email, PDO::PARAM_STR));
}
}

class TestStatement extends Oci8\Statement
{
public static $called = 0;

public function parse($dbh, $statement)
{
self::$called++;
parent::parse($dbh, $statement);
}
}