Skip to content

Commit

Permalink
Merge pull request #590 from yitam/sqlsrvBuffer
Browse files Browse the repository at this point in the history
Refactor sqlsrv tests with buffer or cursor
  • Loading branch information
yitam authored Nov 15, 2017
2 parents bb2d1d7 + 094e32d commit 04a1637
Show file tree
Hide file tree
Showing 27 changed files with 1,048 additions and 1,190 deletions.
41 changes: 23 additions & 18 deletions test/functional/sqlsrv/0052.phpt
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
--TEST--
scrollable results with no rows.
--DESCRIPTION--
this test is very similar to test_scrollable.phpt... might consider removing this test as a duplicate
--SKIPIF--
<?php require('skipif.inc'); ?>
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php
sqlsrv_configure('WarningsReturnAsErrors', 0);
sqlsrv_configure('LogSeverity', SQLSRV_LOG_SEVERITY_ALL);

require_once('MsCommon.inc');

$conn = connect();
if ($conn === false) {
die(print_r(sqlsrv_errors(), true));
}
$conn = AE\connect();
$tableName = 'ScrollTest';

$stmt = sqlsrv_query($conn, "IF OBJECT_ID('ScrollTest', 'U') IS NOT NULL DROP TABLE ScrollTest");
if ($stmt !== false) {
sqlsrv_free_stmt($stmt);
}

$stmt = sqlsrv_query($conn, "CREATE TABLE ScrollTest (id int, value char(10))");
if ($stmt === false) {
die(print_r(sqlsrv_errors(), true));
$columns = array(new AE\ColumnMeta('int', 'id'),
new AE\ColumnMeta('char(10)', 'value'));
$stmt = AE\createTable($conn, $tableName, $columns);
if (!$stmt) {
fatalError("Failed to create table for the test]n");
}
sqlsrv_free_stmt($stmt);

// Always Encrypted feature only supports SQLSRV_CURSOR_FORWARD or
// SQLSRV_CURSOR_CLIENT_BUFFERED
$query = "SELECT * FROM $tableName";
if (AE\isColEncrypted()) {
$options = array('Scrollable' => SQLSRV_CURSOR_FORWARD);
} else {
$options = array('Scrollable' => 'static');
}

$stmt = sqlsrv_query($conn, "SELECT * FROM ScrollTest", array(), array( "Scrollable" => 'static' ));
$stmt = sqlsrv_query($conn, $query, array(), $options);
$rows = sqlsrv_has_rows($stmt);
if ($rows != false) {
fatalError("Should be no rows present");
Expand All @@ -39,8 +45,8 @@ scrollable results with no rows.
if ($row === false) {
print_r(sqlsrv_errors(), true);
}

$stmt = sqlsrv_query($conn, "SELECT * FROM ScrollTest");
$stmt = sqlsrv_query($conn, $query);
$rows = sqlsrv_has_rows($stmt);
if ($rows != false) {
fatalError("Should be no rows present");
Expand All @@ -55,8 +61,7 @@ scrollable results with no rows.
print_r(sqlsrv_errors(), true);
}

$stmt = sqlsrv_query($conn, "DROP TABLE ScrollTest");

dropTable($conn, $tableName);
echo "Test succeeded.\n";

?>
Expand Down
25 changes: 0 additions & 25 deletions test/functional/sqlsrv/AE_Ksp.inc

This file was deleted.

68 changes: 48 additions & 20 deletions test/functional/sqlsrv/MsHelper.inc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const INSERT_PREPARE = 2;
const INSERT_QUERY_PARAMS = 3;
const INSERT_PREPARE_PARAMS = 4;

const KSP_NAME = 'MyCustomKSPName';
const ENCRYPT_KEY = 'LPKCWVD07N3RG98J0MBLG4H2';
const KSP_TEST_TABLE = 'CustomKSPTestTable';

/**
* class for encapsulating column metadata needed for creating a table
Expand Down Expand Up @@ -71,10 +74,10 @@ class ColumnMeta
// https://docs.microsoft.com/en-us/sql/relational-databases/security/encryption/always-encrypted-database-engine

$unsupported = array("xml", "timestamp", "image", "ntext", "text", "sql_variant", "hierarchyid", "geography", "geometry", "alias");

if (stripos($this->options, "identity") !== false) {
$this->encryptable = false;
} elseif (in_array($this->dataType, $unsupported)) {
} elseif (in_array(strtolower($this->dataType), $unsupported)) {
$this->encryptable = false;
} else {
$this->encryptable = true;
Expand Down Expand Up @@ -210,6 +213,29 @@ function getCekName()
return $cekName;
}

/**
* @return the path to the KSP dll/so file
*/
function getKSPpath()
{
$name = 'myKSP';

$dir_name = realpath(dirname(__FILE__));
$ksp = $dir_name . DIRECTORY_SEPARATOR . $name;
if (strtoupper(substr(php_uname('s'), 0, 3)) == 'WIN') {
$arch = 'x64';
if (PHP_INT_SIZE == 4) {
// running 32 bit
$arch = '';
}
$ksp .= $arch . '.dll';
} else {
$ksp .= '.so';
}

return $ksp;
}

/**
* @return string default column name when a name is not provided in the ColumnMeta class
*/
Expand Down Expand Up @@ -346,11 +372,10 @@ function connect($options = array(), $disableCE = false)
$connectionOptions = array_merge($connectionOptions, array("ColumnEncryption" => "Enabled"));
}
if ($keystore == "ksp") {
require('AE_Ksp.inc');
$ksp_path = getKSPPath();
$ksp_options = array("CEKeystoreProvider"=>$ksp_path,
"CEKeystoreName"=>$ksp_name,
"CEKeystoreEncryptKey"=>$encrypt_key);
"CEKeystoreName"=>KSP_NAME,
"CEKeystoreEncryptKey"=>ENCRYPT_KEY);
$connectionOptions = array_merge($connectionOptions, $ksp_options);
}
}
Expand Down Expand Up @@ -419,19 +444,20 @@ function insertRow($conn, $tbname, $inputs, &$r = null, $api = INSERT_QUERY)
array_push($params, $inputs[$key]);
}
}

// use prepare for inserts when AE is enabled
if (isColEncrypted() || $api == INSERT_PREPARE_PARAMS) {
$stmt = sqlsrv_prepare($conn, $insertSql, $params);
if ($stmt) {
$r = sqlsrv_execute($stmt);
} else {
fatalError("insertRow: failed to prepare insert query!");
fatalError("insertRow: failed to prepare insert query!");
}
} else {
$stmt = sqlsrv_query($conn, $insertSql, $params);
}
}

return $stmt;
}

Expand All @@ -454,10 +480,11 @@ function selectFromTable($conn, $tbname, $conds = null, $values = null)
* @param resource $conn : connection resource
* @param string $sql : T-SQL query
* @param string $conds : string of condition(s) possibly with placeholders, null by default
* @param array $values : array of parameters, null by default
* @param array $values : array of parameters for placeholders in $conds, null by default
* @param array $options : array of query options, null by default
* @return resource sqlsrv statement upon success or false otherwise
*/
function executeQuery($conn, $sql, $conds = null, $values = null)
function executeQuery($conn, $sql, $conds = null, $values = null, $options = null)
{
if (!isColEncrypted()) {
// if not encrypted replace placeholders ('?') with values, if array not empty
Expand All @@ -475,17 +502,19 @@ function executeQuery($conn, $sql, $conds = null, $values = null)
$sql = $sql . " WHERE $clause ";
} elseif (!empty($conds)) {
$sql = $sql . " WHERE $conds ";
}
$stmt = sqlsrv_query($conn, $sql);
}

$stmt = sqlsrv_query($conn, $sql, null, $options);
} else {
// with AE enabled, use sqlsrv_prepare() in case there are
// with AE enabled, use sqlsrv_prepare() in case there are
// fields with unlimited size
if (empty($conds) || empty($values)) {
$stmt = sqlsrv_prepare($conn, $sql);
if (empty($conds)) {
$stmt = sqlsrv_prepare($conn, $sql, null, $options);
} else {
$sql = $sql . " WHERE $conds ";
$stmt = sqlsrv_prepare($conn, $sql, $values);
}
// pass $values to sqlsrv_prepare whether the array is null, empty or filled
$stmt = sqlsrv_prepare($conn, $sql, $values, $options);
}
if ($stmt) {
$r = sqlsrv_execute($stmt);
if (!$r) {
Expand Down Expand Up @@ -649,13 +678,12 @@ function insertTestRow($conn, $tbname, $index)
echo("Invalid row index $index for test data!\n");
return false;
}

// get array of input values
$inputArray = getInsertArray($index);
if (empty($inputArray)) {
fatalError("getInsertSqlComplete: inputs for inserting a row cannot be empty");
}

$result = null;
if (isColEncrypted()) {
$stmt = insertRow($conn, $tbname, $inputArray, $result);
Expand All @@ -667,14 +695,14 @@ function insertTestRow($conn, $tbname, $index)
$col = 1;
foreach ($inputArray as $key => $value) {
$colStr .= $key . ", ";

if (is_array($value)) {
$value = $value[0];
// this might be an input to a decimal, a numeric or a binary field
if (isBinary($col)) {
$value = "0x" . $value; // annotate the input string as a hex string
}
}
}
if (is_null($value)) {
$valStr .= "null, ";
} elseif (is_string($value) && !isBinary($col)) {
Expand Down
26 changes: 10 additions & 16 deletions test/functional/sqlsrv/skipif_not_ksp.inc
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
<?php

if(! extension_loaded( "sqlsrv" ) )
die( "skip extension not loaded" );
if (!extension_loaded("sqlsrv"))
die("skip extension not loaded");

require_once( 'MsSetup.inc' );
require_once('MsCommon.inc');

if ($keystore != 'ksp')
die ( 'skip - this test requires a custom keystore provider.' );
if ($keystore != AE\KEYSTORE_KSP)
die('skip - this test requires a custom keystore provider.');

require_once( 'MsCommon.inc' );

$conn = Connect();
if( ! $conn )
{
echo( "Error: could not connect during SKIPIF!" );
$conn = AE\connect();
if (! $conn) {
echo("Error: could not connect during SKIPIF!");
} elseif (AE\isColEncrypted() && !AE\isQualified($conn)) {
die("skip - AE feature not supported in the current environment.");
}
else if(! IsAEQualified( $conn ) )
{
die( "skip - AE feature not supported in the current environment." );
}

?>
Loading

0 comments on commit 04a1637

Please sign in to comment.