Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix for #2678 (Zend\Db's Sqlsrv Driver) #2749

Closed
wants to merge 2 commits into from

2 participants

@ralphschindler
Collaborator

This is a fix for issue #2678

ralphschindler added some commits
@ralphschindler ralphschindler Zend\Db
* Added some error checking to Sqlsrv driver main class and statement class
9c9e481
@ralphschindler ralphschindler ZendTest\Db
* Added integration test for Sqlsrv's createStatement()
bc4c0e3
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#2749] CS fixes
- trailing whitespace
0560a16
@weierophinney weierophinney closed this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2749'
Close #2749
Fixes #2678
8a17e03
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#2749] CS fixes
- trailing whitespace
0536f56
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2749'
Close #2749
Fixes #2678
091b568
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2749' into develop
Forward port #2749
54ff18c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 12, 2012
  1. @ralphschindler

    Zend\Db

    ralphschindler authored
    * Added some error checking to Sqlsrv driver main class and statement class
  2. @ralphschindler

    ZendTest\Db

    ralphschindler authored
    * Added integration test for Sqlsrv's createStatement()
This page is out of date. Refresh to see the latest.
View
4 library/Zend/Db/Adapter/Driver/Sqlsrv/Sqlsrv.php
@@ -140,7 +140,9 @@ public function createStatement($sqlOrResource = null)
}
$statement->initialize($this->connection->getResource());
} elseif (is_resource($sqlOrResource)) {
- $statement->setResource($sqlOrResource);
+ $statement->initialize($sqlOrResource); // will check the resource type
+ } else {
+ throw new Exception\InvalidArgumentException('createStatement() only accepts an SQL string or a Sqlsrv resource');
}
return $statement;
}
View
9 library/Zend/Db/Adapter/Driver/Sqlsrv/Statement.php
@@ -89,11 +89,16 @@ public function setDriver(Sqlsrv $driver)
public function initialize($resource)
{
$resourceType = get_resource_type($resource);
- if ($resourceType != 'SQL Server Connection' && $resourceType != 'SQL Server Statement') {
+
+ if ($resourceType == 'SQL Server Connection') {
+ $this->sqlsrv = $resource;
+ } elseif ($resourceType == 'SQL Server Statement') {
+ $this->resource = $resource;
+ $this->isPrepared = true;
+ } else {
throw new Exception\InvalidArgumentException('Invalid resource provided to ' . __CLASS__);
}
- $this->sqlsrv = $resource;
return $this;
}
View
30 tests/ZendTest/Db/Adapter/Driver/Sqlsrv/AbstractIntegrationTest.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace ZendTest\Db\Adapter\Driver\Sqlsrv;
+
+abstract class AbstractIntegrationTest extends \PHPUnit_Framework_TestCase
+{
+ protected $variables = array(
+ 'hostname' => 'ZEND_DB_ADAPTER_DRIVER_SQLSRV_HOSTNAME',
+ 'username' => 'ZEND_DB_ADAPTER_DRIVER_SQLSRV_USERNAME',
+ 'password' => 'ZEND_DB_ADAPTER_DRIVER_SQLSRV_PASSWORD',
+ );
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ */
+ protected function setUp()
+ {
+ foreach ($this->variables as $name => $value) {
+ if (!isset($GLOBALS[$value])) {
+ $this->fail('Missing required variable ' . $value . ' from phpunit.xml for this integration test');
+ }
+ $this->variables[$name] = $GLOBALS[$value];
+ }
+
+ if (!extension_loaded('sqlsrv')) {
+ $this->fail('The phpunit group integration-sqlsrv was enabled, but the extension is not loaded.');
+ }
+ }
+}
View
26 tests/ZendTest/Db/Adapter/Driver/Sqlsrv/ConnectionIntegrationTest.php 100644 → 100755
@@ -8,32 +8,8 @@
* @group integration
* @group integration-sqlserver
*/
-class ConnectionIntegrationTest extends \PHPUnit_Framework_TestCase
+class ConnectionIntegrationTest extends AbstractIntegrationTest
{
- protected $variables = array(
- 'hostname' => 'ZEND_DB_ADAPTER_DRIVER_SQLSRV_HOSTNAME',
- 'username' => 'ZEND_DB_ADAPTER_DRIVER_SQLSRV_USERNAME',
- 'password' => 'ZEND_DB_ADAPTER_DRIVER_SQLSRV_PASSWORD',
- );
-
- /**
- * Sets up the fixture, for example, opens a network connection.
- * This method is called before a test is executed.
- */
- protected function setUp()
- {
- foreach ($this->variables as $name => $value) {
- if (!isset($GLOBALS[$value])) {
- $this->fail('Missing required variable ' . $value . ' from phpunit.xml for this integration test');
- }
- $this->variables[$name] = $GLOBALS[$value];
- }
-
- if (!extension_loaded('sqlsrv')) {
- $this->fail('The phpunit group integration-sqlsrv was enabled, but the extension is not loaded.');
- }
- }
-
/**
* @covers Zend\Db\Adapter\Driver\Sqlsrv\Connection::getCurrentSchema
*/
View
36 tests/ZendTest/Db/Adapter/Driver/Sqlsrv/SqlSrvIntegrationTest.php 100644 → 100755
@@ -7,20 +7,8 @@
* @group integration
* @group integration-sqlsrv
*/
-class SqlsrvIntegrationTest extends \PHPUnit_Framework_TestCase
+class SqlsrvIntegrationTest extends AbstractIntegrationTest
{
-
- /**
- * Sets up the fixture, for example, opens a network connection.
- * This method is called before a test is executed.
- */
- protected function setUp()
- {
- if (!extension_loaded('sqlsrv')) {
- $this->fail('The phpunit group integration-sqlsrv was enabled, but the extension is not loaded.');
- }
- }
-
/**
* @group integration-sqlserver
* @covers Zend\Db\Adapter\Driver\Sqlsrv\Sqlsrv::checkEnvironment
@@ -31,4 +19,26 @@ public function testCheckEnvironment()
$this->assertNull($sqlserver->checkEnvironment());
}
+ public function testCreateStatement()
+ {
+ $driver = new Sqlsrv(array());
+
+ $resource = sqlsrv_connect(
+ $this->variables['hostname'], array(
+ 'UID' => $this->variables['username'],
+ 'PWD' => $this->variables['password']
+ )
+ );
+
+ $driver->getConnection()->setResource($resource);
+
+ $stmt = $driver->createStatement('SELECT 1');
+ $this->assertInstanceOf('Zend\Db\Adapter\Driver\Sqlsrv\Statement', $stmt);
+ $stmt = $driver->createStatement($resource);
+ $this->assertInstanceOf('Zend\Db\Adapter\Driver\Sqlsrv\Statement', $stmt);
+
+ $this->setExpectedException('Zend\Db\Adapter\Exception\InvalidArgumentException', 'only accepts an SQL string or a Sqlsrv resource');
+ $driver->createStatement(new \stdClass);
+ }
+
}
Something went wrong with that request. Please try again.