Skip to content

Commit

Permalink
sqlsrv: platform renamed to SqlServerPlatform
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 24, 2017
1 parent bddd753 commit 111be6a
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 8 deletions.
39 changes: 39 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
build: off
cache:
- c:\php -> appveyor.yml
- '%LOCALAPPDATA%\Composer\files -> appveyor.yml'

clone_folder: c:\projects\dbal

services:
- mssql2016

init:
- SET PATH=c:\php;%PATH%
- SET PHP=1
- SET ANSICON=121x90 (121x90)

install:
# Install PHP
- IF EXIST c:\php (SET PHP=0) ELSE (mkdir c:\php)
- IF %PHP%==1 cd c:\php
- IF %PHP%==1 appveyor DownloadFile http://windows.php.net/downloads/releases/archives/php-7.1.0-Win32-VC14-x64.zip
- IF %PHP%==1 7z x php-7.1.0-Win32-VC14-x64.zip >nul
- IF %PHP%==1 echo extension_dir=ext >> php.ini
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
- IF %PHP%==1 echo extension=php_sqlsrv_ts.dll >> php.ini
- IF %PHP%==1 appveyor DownloadFile https://github.com/Microsoft/msphpsql/releases/download/4.1.4-Windows/7.1.zip
- IF %PHP%==1 7z x 7.1.zip >nul
- IF %PHP%==1 copy 7.1\x64\php_sqlsrv_71_ts.dll ext\php_sqlsrv_ts.dll
- IF %PHP%==1 del /Q *.zip
- cd c:\projects\dbal

# Install Nette Tester
- appveyor DownloadFile https://getcomposer.org/composer.phar
- php composer.phar install --prefer-dist --no-interaction --no-progress

# Create databases.ini
- copy tests\databases.appveyor.ini tests\databases.ini

test_script:
- vendor\bin\tester -p php -c tests\php.win-sample.ini --setup tests\inc\setup.php tests\cases
4 changes: 2 additions & 2 deletions src/Drivers/Sqlsrv/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Nextras\Dbal\NotNullConstraintViolationException;
use Nextras\Dbal\NotSupportedException;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\Platforms\SqlsrvSqlPlatform;
use Nextras\Dbal\Platforms\SqlServerPlatform;
use Nextras\Dbal\QueryException;
use Nextras\Dbal\Result\Result;
use Nextras\Dbal\UniqueConstraintViolationException;
Expand Down Expand Up @@ -175,7 +175,7 @@ public function getQueryElapsedTime(): float

public function createPlatform(Connection $connection): IPlatform
{
return new SqlsrvSqlPlatform($connection);
return new SqlServerPlatform($connection);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Nextras\Dbal\Connection;


class SqlsrvSqlPlatform implements IPlatform
class SqlServerPlatform implements IPlatform
{
/** @var Connection */
private $connection;
Expand All @@ -25,7 +25,7 @@ public function __construct(Connection $connection)

public function getName(): string
{
return 'sqlsrv';
return 'mssql';
}


Expand Down
10 changes: 8 additions & 2 deletions tests/cases/integration/result.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace NextrasTests\Dbal;

use Nextras\Dbal\InvalidStateException;
use Nextras\Dbal\Platforms\SqlServerPlatform;
use Nextras\Dbal\Utils\DateTimeImmutable;
use Tester\Assert;

Expand Down Expand Up @@ -43,8 +44,13 @@ class ResultIntegrationTest extends IntegrationTestCase
$result->setValueNormalization(FALSE);
$follower = $result->fetch();

Assert::same('2', $follower->tag_id);
Assert::same('2', $follower->author_id);
if ($this->connection->getPlatform() instanceof SqlServerPlatform) {
Assert::same(2, $follower->tag_id);
Assert::same(2, $follower->author_id);
} else {
Assert::same('2', $follower->tag_id);
Assert::same('2', $follower->author_id);
}
Assert::type('string', $follower->created_at);
}

Expand Down
47 changes: 47 additions & 0 deletions tests/data/sqlsrv-data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
DELETE FROM books_x_tags DBCC CHECKIDENT ('books_x_tags', RESEED, 0);
DELETE FROM books DBCC CHECKIDENT ('books', RESEED, 0);
DELETE FROM tags DBCC CHECKIDENT ('tags', RESEED, 0);
DELETE FROM authors DBCC CHECKIDENT ('authors', RESEED, 0);
DELETE FROM publishers DBCC CHECKIDENT ('publishers', RESEED, 0);
DELETE FROM tag_followers DBCC CHECKIDENT ('tag_followers', RESEED, 0);


SET IDENTITY_INSERT authors ON
INSERT INTO authors (id, name, web, born) VALUES (1, 'Writer 1', 'http://example.com/1', NULL);
SET IDENTITY_INSERT authors ON
INSERT INTO authors (id, name, web, born) VALUES (2, 'Writer 2', 'http://example.com/2', NULL);


SET IDENTITY_INSERT publishers ON
INSERT INTO publishers (id, name) VALUES (1, 'Nextras publisher');


SET IDENTITY_INSERT tags ON
INSERT INTO tags (id, name) VALUES (1, 'Tag 1');
SET IDENTITY_INSERT tags ON
INSERT INTO tags (id, name) VALUES (2, 'Tag 2');
SET IDENTITY_INSERT tags ON
INSERT INTO tags (id, name) VALUES (3, 'Tag 3');


SET IDENTITY_INSERT books ON
INSERT INTO books (id, author_id, translator_id, title, publisher_id) VALUES (1, 1, 1, 'Book 1', 1);
SET IDENTITY_INSERT books ON
INSERT INTO books (id, author_id, translator_id, title, publisher_id) VALUES (2, 1, NULL, 'Book 2', 1);
SET IDENTITY_INSERT books ON
INSERT INTO books (id, author_id, translator_id, title, publisher_id) VALUES (3, 2, 2, 'Book 3', 1);
SET IDENTITY_INSERT books ON
INSERT INTO books (id, author_id, translator_id, title, publisher_id) VALUES (4, 2, 2, 'Book 4', 1);


INSERT INTO books_x_tags (book_id, tag_id) VALUES (1, 1);
INSERT INTO books_x_tags (book_id, tag_id) VALUES (1, 2);
INSERT INTO books_x_tags (book_id, tag_id) VALUES (2, 2);
INSERT INTO books_x_tags (book_id, tag_id) VALUES (2, 3);
INSERT INTO books_x_tags (book_id, tag_id) VALUES (3, 3);


-- there is nothing as connection timezone, so we have to explictly express the timezone
INSERT INTO tag_followers (tag_id, author_id, created_at) VALUES (1, 1, '2014-01-01 00:10:00+01:00');
INSERT INTO tag_followers (tag_id, author_id, created_at) VALUES (3, 1, '2014-01-01 00:10:00+01:00');
INSERT INTO tag_followers (tag_id, author_id, created_at) VALUES (2, 2, '2014-01-01 00:10:00+01:00');
67 changes: 67 additions & 0 deletions tests/data/sqlsrv-init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
CREATE TABLE authors (
id int NOT NULL IDENTITY(11,1),
name varchar(50) NOT NULL,
web varchar(100) NOT NULL,
born date DEFAULT NULL,
PRIMARY KEY(id)
);


CREATE TABLE publishers (
id int NOT NULL IDENTITY(1,1),
name varchar(50) NOT NULL,
PRIMARY KEY(id)
);

CREATE UNIQUE INDEX idx_unique_publishers_name ON publishers(name);

CREATE TABLE tags (
id int NOT NULL IDENTITY(1,1),
name varchar(50) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE eans (
id int NOT NULL IDENTITY(1,1),
code varchar(50) NOT NULL,
PRIMARY KEY(id)
);

CREATE TABLE books (
id int NOT NULL IDENTITY(1,1),
author_id int NOT NULL,
translator_id int,
title varchar(50) NOT NULL,
publisher_id int NOT NULL,
ean_id int,
PRIMARY KEY (id),
CONSTRAINT books_authors FOREIGN KEY (author_id) REFERENCES authors (id),
CONSTRAINT books_translator FOREIGN KEY (translator_id) REFERENCES authors (id),
CONSTRAINT books_publisher FOREIGN KEY (publisher_id) REFERENCES publishers (id),
CONSTRAINT books_ean FOREIGN KEY (ean_id) REFERENCES eans (id)
);

CREATE INDEX book_title ON books (title);

CREATE VIEW my_books AS SELECT * FROM books WHERE author_id = 1;

CREATE TABLE books_x_tags (
book_id int NOT NULL,
tag_id int NOT NULL,
PRIMARY KEY (book_id, tag_id),
CONSTRAINT books_x_tags_tag FOREIGN KEY (tag_id) REFERENCES tags (id),
CONSTRAINT books_x_tags_book FOREIGN KEY (book_id) REFERENCES books (id) ON DELETE CASCADE
);

CREATE TABLE tag_followers (
tag_id int NOT NULL,
author_id int NOT NULL,
created_at datetimeoffset NOT NULL,
PRIMARY KEY (tag_id, author_id),
CONSTRAINT tag_followers_tag FOREIGN KEY (tag_id) REFERENCES tags (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT tag_followers_author FOREIGN KEY (author_id) REFERENCES authors (id) ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE TABLE table_with_defaults (
name VARCHAR(255) DEFAULT 'Jon Snow'
);
9 changes: 9 additions & 0 deletions tests/data/sqlsrv-reset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Nextras\Dbal\Connection;

return function (Connection $connection, $dbname) {
$connection->query('DROP DATABASE IF EXISTS %table', $dbname);
$connection->query('CREATE DATABASE IF NOT EXISTS %table', $dbname);
$connection->query('USE %table', $dbname);
};
6 changes: 6 additions & 0 deletions tests/databases.appveyor.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[sqlsrv]
driver = sqlsrv
host = "(local)\SQL2016"
username = sa
password = "Password12!"
database = "master"
12 changes: 10 additions & 2 deletions tests/inc/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use Mockery;
use Nextras\Dbal\Connection;
use Nextras\Dbal\InvalidArgumentException;
use Nextras\Dbal\InvalidStateException;
use Nextras\Dbal\Platforms\MySqlPlatform;
use Nextras\Dbal\Platforms\PostgreSqlPlatform;
use Nextras\Dbal\Platforms\SqlServerPlatform;
use Nextras\Dbal\Utils\FileImporter;
use Tester\Environment;

Expand All @@ -22,10 +25,15 @@ class IntegrationTestCase extends TestCase
public function initData(Connection $connection)
{
$this->lockConnection($connection);
if ($connection->getPlatform() instanceof PostgreSqlPlatform) {
$platform = $connection->getPlatform();
if ($platform instanceof PostgreSqlPlatform) {
FileImporter::executeFile($connection, __DIR__ . '/../data/pgsql-data.sql');
} else {
} elseif ($platform instanceof SqlServerPlatform) {
FileImporter::executeFile($connection, __DIR__ . '/../data/sqlsrv-data.sql');
} elseif ($platform instanceof MySqlPlatform) {
FileImporter::executeFile($connection, __DIR__ . '/../data/mysql-data.sql');
} else {
throw new InvalidStateException();
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/php.win-sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
extension_dir = "./ext"
extension=php_mysqli.dll
extension=php_pgsql.dll
extension=php_sqlsrv_ts.dll

0 comments on commit 111be6a

Please sign in to comment.