Merged ksp inc into MsHelper and refactored KSP tests
This commit is contained in:
parent
47c5512cf1
commit
41eec918c9
|
@ -1,25 +0,0 @@
|
|||
<?php
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
$ksp_name = 'MyCustomKSPName';
|
||||
$encrypt_key = 'LPKCWVD07N3RG98J0MBLG4H2';
|
||||
$ksp_test_table = 'CustomKSPTestTable';
|
||||
|
||||
?>
|
|
@ -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
|
||||
|
@ -210,6 +213,27 @@ 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
|
||||
*/
|
||||
|
@ -346,11 +370,11 @@ function connect($options = array(), $disableCE = false)
|
|||
$connectionOptions = array_merge($connectionOptions, array("ColumnEncryption" => "Enabled"));
|
||||
}
|
||||
if ($keystore == "ksp") {
|
||||
require('AE_Ksp.inc');
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,21 +3,15 @@
|
|||
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." );
|
||||
}
|
||||
|
||||
?>
|
|
@ -4,46 +4,31 @@ Fetch data from a prepopulated test table given a custom keystore provider
|
|||
<?php require('skipif_not_ksp.inc'); ?>
|
||||
--FILE--
|
||||
<?php
|
||||
sqlsrv_configure( 'WarningsReturnAsErrors', 1 );
|
||||
sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL );
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
sqlsrv_configure('LogSeverity', SQLSRV_LOG_SEVERITY_ALL);
|
||||
|
||||
require( 'MsSetup.inc' );
|
||||
require( 'AE_Ksp.inc' );
|
||||
|
||||
$ksp_path = getKSPpath();
|
||||
|
||||
$connectionInfo = array( "Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>$ksp_path,
|
||||
"CEKeystoreName"=>$ksp_name,
|
||||
"CEKeystoreEncryptKey"=>$encrypt_key,
|
||||
'ReturnDatesAsStrings'=>true);
|
||||
|
||||
$conn = sqlsrv_connect( $server, $connectionInfo );
|
||||
if( $conn === false )
|
||||
{
|
||||
require_once('MsHelper.inc');
|
||||
$conn = AE\connect(array('ReturnDatesAsStrings'=>true));
|
||||
if($conn === false) {
|
||||
echo "Failed to connect.\n";
|
||||
print_r( sqlsrv_errors() );
|
||||
}
|
||||
else
|
||||
{
|
||||
print_r(sqlsrv_errors());
|
||||
} else {
|
||||
echo "Connected successfully with ColumnEncryption enabled.\n";
|
||||
}
|
||||
|
||||
$ksp_test_table = AE\KSP_TEST_TABLE;
|
||||
$tsql = "SELECT * FROM $ksp_test_table";
|
||||
$stmt = sqlsrv_prepare($conn, $tsql);
|
||||
if (! sqlsrv_execute($stmt) )
|
||||
{
|
||||
if (! sqlsrv_execute($stmt)) {
|
||||
echo "Failed to fetch data.\n";
|
||||
print_r( sqlsrv_errors() );
|
||||
print_r(sqlsrv_errors());
|
||||
}
|
||||
|
||||
// fetch data
|
||||
while ($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC ))
|
||||
{
|
||||
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) {
|
||||
echo "c1=" . $row[0] . "\tc2=" . $row[1] . "\tc3=" . $row[2] . "\tc4=" . $row[3] . "\n" ;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
sqlsrv_free_stmt($stmt);
|
||||
sqlsrv_close($conn);
|
||||
|
||||
|
@ -61,4 +46,4 @@ c1=67 c2=Sample data 6 for column 2 c3=ghi c4=2017-08-16
|
|||
c1=78 c2=Sample data 7 for column 2 c3=hij c4=2017-08-17
|
||||
c1=89 c2=Sample data 8 for column 2 c3=ijk c4=2017-08-18
|
||||
c1=100 c2=Sample data 9 for column 2 c3=jkl c4=2017-08-19
|
||||
Done
|
||||
Done
|
||||
|
|
|
@ -4,50 +4,34 @@ Fetch encrypted data from a prepopulated test table given a custom keystore prov
|
|||
<?php require('skipif_not_ksp.inc'); ?>
|
||||
--FILE--
|
||||
<?php
|
||||
sqlsrv_configure( 'WarningsReturnAsErrors', 1 );
|
||||
sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL );
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
sqlsrv_configure('LogSeverity', SQLSRV_LOG_SEVERITY_ALL);
|
||||
|
||||
require( 'MsSetup.inc' );
|
||||
require( 'AE_Ksp.inc' );
|
||||
|
||||
$ksp_path = getKSPpath();
|
||||
|
||||
$connectionInfo = array( "Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"CEKeystoreProvider"=>$ksp_path,
|
||||
"CEKeystoreName"=>$ksp_name,
|
||||
"CEKeystoreEncryptKey"=>$encrypt_key,
|
||||
'ReturnDatesAsStrings'=>true);
|
||||
|
||||
$conn = sqlsrv_connect( $server, $connectionInfo );
|
||||
if( $conn === false )
|
||||
{
|
||||
echo "Failed to connect.\n";
|
||||
print_r( sqlsrv_errors() );
|
||||
}
|
||||
else
|
||||
{
|
||||
require_once('MsHelper.inc');
|
||||
$conn = AE\connect(array('ReturnDatesAsStrings'=>true));
|
||||
if($conn === false) {
|
||||
fatalError("Failed to connect.\n");
|
||||
} else {
|
||||
echo "Connected successfully with ColumnEncryption disabled.\n";
|
||||
}
|
||||
|
||||
$ksp_test_table = AE\KSP_TEST_TABLE;
|
||||
$tsql = "SELECT * FROM $ksp_test_table";
|
||||
$stmt = sqlsrv_prepare($conn, $tsql);
|
||||
if (! sqlsrv_execute($stmt) )
|
||||
{
|
||||
echo "Failed to fetch data.\n";
|
||||
print_r( sqlsrv_errors() );
|
||||
if (!sqlsrv_execute($stmt)) {
|
||||
fatalError("Failed to fetch data.\n");
|
||||
}
|
||||
|
||||
// fetch data
|
||||
while ($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC ))
|
||||
{
|
||||
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) {
|
||||
// all columns should return binary data except the first column
|
||||
echo "c1=" . $row[0];
|
||||
echo "\tc2=" . bin2hex($row[1]);
|
||||
echo "\tc3=" . bin2hex($row[2]);
|
||||
echo "\tc3=" . bin2hex($row[2]);
|
||||
echo "\tc4=" . bin2hex($row[3]);
|
||||
echo "\n" ;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
sqlsrv_free_stmt($stmt);
|
||||
sqlsrv_close($conn);
|
||||
|
||||
|
|
|
@ -5,91 +5,88 @@ Connect using a custom keystore provider with some required inputs missing
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
function connect( $server, $connectionInfo )
|
||||
function connect($server, $connectionInfo)
|
||||
{
|
||||
$conn = sqlsrv_connect( $server, $connectionInfo );
|
||||
if( $conn === false )
|
||||
{
|
||||
$conn = sqlsrv_connect($server, $connectionInfo);
|
||||
if($conn === false) {
|
||||
echo "Failed to connect.\n";
|
||||
$errors = sqlsrv_errors();
|
||||
foreach ( $errors[0] as $key => $error )
|
||||
{
|
||||
if( is_string( $key ) )
|
||||
foreach ($errors[0] as $key => $error) {
|
||||
if(is_string($key)) {
|
||||
echo "[$key] => $error\n";
|
||||
}
|
||||
}
|
||||
echo "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
echo "Connected successfully with ColumnEncryption enabled.\n";
|
||||
}
|
||||
|
||||
|
||||
return $conn;
|
||||
}
|
||||
|
||||
sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL );
|
||||
|
||||
require( 'MsSetup.inc' );
|
||||
require( 'AE_Ksp.inc' );
|
||||
|
||||
$ksp_path = getKSPpath();
|
||||
sqlsrv_configure('LogSeverity', SQLSRV_LOG_SEVERITY_ALL);
|
||||
|
||||
require_once('MsHelper.inc');
|
||||
$ksp_path = AE\getKSPpath();
|
||||
$ksp_name = AE\KSP_NAME;
|
||||
$encrypt_key = AE\ENCRYPT_KEY;
|
||||
|
||||
echo("Connecting... with column encryption\n");
|
||||
$connectionInfo = array( "Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled");
|
||||
$connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled");
|
||||
|
||||
connect($server, $connectionInfo);
|
||||
|
||||
connect( $server, $connectionInfo );
|
||||
|
||||
echo("Connecting... with an invalid input to CEKeystoreProvider\n");
|
||||
$connectionInfo = array( "Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>1);
|
||||
$connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>1);
|
||||
|
||||
connect( $server, $connectionInfo );
|
||||
connect($server, $connectionInfo);
|
||||
|
||||
echo("Connecting... with an empty path\n");
|
||||
$connectionInfo = array( "Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>"",
|
||||
"CEKeystoreName"=>$ksp_name,
|
||||
"CEKeystoreEncryptKey"=>$encrypt_key);
|
||||
$connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>"",
|
||||
"CEKeystoreName"=>$ksp_name,
|
||||
"CEKeystoreEncryptKey"=>$encrypt_key);
|
||||
|
||||
connect($server, $connectionInfo);
|
||||
|
||||
connect( $server, $connectionInfo );
|
||||
|
||||
echo("Connecting... without a name\n");
|
||||
$connectionInfo = array( "Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>$ksp_path,
|
||||
"CEKeystoreEncryptKey"=>$encrypt_key);
|
||||
$connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>$ksp_path,
|
||||
"CEKeystoreEncryptKey"=>$encrypt_key);
|
||||
|
||||
connect( $server, $connectionInfo );
|
||||
connect($server, $connectionInfo);
|
||||
|
||||
echo("Connecting... with an empty name\n");
|
||||
$connectionInfo = array( "Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>$ksp_path,
|
||||
"CEKeystoreName"=>"",
|
||||
"CEKeystoreEncryptKey"=>$encrypt_key);
|
||||
$connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>$ksp_path,
|
||||
"CEKeystoreName"=>"",
|
||||
"CEKeystoreEncryptKey"=>$encrypt_key);
|
||||
|
||||
connect( $server, $connectionInfo );
|
||||
connect($server, $connectionInfo);
|
||||
|
||||
echo("Connecting... without a key\n");
|
||||
$connectionInfo = array( "Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>$ksp_path,
|
||||
"CEKeystoreName"=>$ksp_name);
|
||||
|
||||
connect( $server, $connectionInfo );
|
||||
$connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>$ksp_path,
|
||||
"CEKeystoreName"=>$ksp_name);
|
||||
|
||||
connect($server, $connectionInfo);
|
||||
|
||||
echo("Connecting... with all required inputs\n");
|
||||
$connectionInfo = array( "Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>$ksp_path,
|
||||
"CEKeystoreName"=>$ksp_name,
|
||||
"CEKeystoreEncryptKey"=>$encrypt_key);
|
||||
|
||||
connect( $server, $connectionInfo );
|
||||
|
||||
$connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ColumnEncryption"=>"enabled",
|
||||
"CEKeystoreProvider"=>$ksp_path,
|
||||
"CEKeystoreName"=>$ksp_name,
|
||||
"CEKeystoreEncryptKey"=>$encrypt_key);
|
||||
|
||||
connect($server, $connectionInfo);
|
||||
|
||||
echo "Done\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
|
|
|
@ -7,202 +7,165 @@ Test simple insert, fetch and update with ColumnEncryption enabled and a custome
|
|||
function CreatePatientsTable()
|
||||
{
|
||||
global $conn;
|
||||
$tablename = 'Patients';
|
||||
|
||||
$stmt = sqlsrv_query( $conn, "IF OBJECT_ID('$tablename', 'U') IS NOT NULL DROP TABLE $tablename" );
|
||||
sqlsrv_free_stmt( $stmt );
|
||||
$tableName = 'Patients';
|
||||
|
||||
$tsql = "CREATE TABLE $tablename (
|
||||
[PatientId] [int] IDENTITY(1,1) NOT NULL,
|
||||
[SSN] [char](11) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = CustomCEK, ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NOT NULL,
|
||||
[FirstName] [nvarchar](50) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = CustomCEK, ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NULL,
|
||||
[LastName] [nvarchar](50) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = CustomCEK, ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NULL,
|
||||
[BirthDate] [date] ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = CustomCEK, ENCRYPTION_TYPE = Randomized, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NOT NULL)";
|
||||
|
||||
$stmt = sqlsrv_query( $conn, $tsql );
|
||||
if (! $stmt )
|
||||
{
|
||||
echo "Failed to create test table!\n";
|
||||
die( print_r( sqlsrv_errors(), true ));
|
||||
$columns = array(new AE\ColumnMeta('int', 'PatientId', 'IDENTITY(1,1) NOT NULL'),
|
||||
new AE\ColumnMeta('char(11)', 'SSN'),
|
||||
new AE\ColumnMeta('nvarchar(50)', 'FirstName'),
|
||||
new AE\ColumnMeta('nvarchar(50)', 'LastName'),
|
||||
new AE\ColumnMeta('date', 'BirthDate'));
|
||||
$stmt = AE\createTable($conn, $tableName, $columns);
|
||||
if (!$stmt) {
|
||||
fatalError("Failed to create test table!\n");
|
||||
}
|
||||
|
||||
return $tablename;
|
||||
return $tableName;
|
||||
}
|
||||
|
||||
function InsertData($ssn, $fname, $lname, $date)
|
||||
|
||||
function insertData($ssn, $fname, $lname, $date)
|
||||
{
|
||||
global $conn, $tablename;
|
||||
|
||||
global $conn, $tableName;
|
||||
$params = array(
|
||||
array($ssn, null, null, SQLSRV_SQLTYPE_CHAR(11)), array($fname, null, null, SQLSRV_SQLTYPE_NVARCHAR(50)), array($lname, null, null, SQLSRV_SQLTYPE_NVARCHAR(50)), array($date, null, null, SQLSRV_SQLTYPE_DATE)
|
||||
);
|
||||
|
||||
$tsql = "INSERT INTO $tablename (SSN, FirstName, LastName, BirthDate) VALUES (?, ?, ?, ?)";
|
||||
if (! $stmt = sqlsrv_prepare($conn, $tsql, $params))
|
||||
{
|
||||
echo "Failed to prepare statement.\n";
|
||||
print_r( sqlsrv_errors() );
|
||||
array($ssn, null, null, SQLSRV_SQLTYPE_CHAR(11)), array($fname, null, null, SQLSRV_SQLTYPE_NVARCHAR(50)), array($lname, null, null, SQLSRV_SQLTYPE_NVARCHAR(50)), array($date, null, null, SQLSRV_SQLTYPE_DATE)
|
||||
);
|
||||
|
||||
$tsql = "INSERT INTO $tableName (SSN, FirstName, LastName, BirthDate) VALUES (?, ?, ?, ?)";
|
||||
if (! $stmt = sqlsrv_prepare($conn, $tsql, $params)) {
|
||||
fatalError("Failed to prepare statement.\n");
|
||||
}
|
||||
|
||||
if (! sqlsrv_execute($stmt))
|
||||
{
|
||||
echo "Failed to insert a new record.\n";
|
||||
print_r( sqlsrv_errors() );
|
||||
if (! sqlsrv_execute($stmt)) {
|
||||
fatalError("Failed to insert a new record.\n");
|
||||
}
|
||||
}
|
||||
|
||||
function SelectData()
|
||||
{
|
||||
global $conn, $tablename;
|
||||
|
||||
$stmt = sqlsrv_query($conn, "SELECT * FROM $tablename");
|
||||
|
||||
while ($obj = sqlsrv_fetch_object( $stmt ))
|
||||
{
|
||||
function selectData()
|
||||
{
|
||||
global $conn, $tableName;
|
||||
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName");
|
||||
while ($obj = sqlsrv_fetch_object($stmt)) {
|
||||
echo $obj->PatientId . "\n";
|
||||
echo $obj->SSN . "\n";
|
||||
echo $obj->FirstName . "\n";
|
||||
echo $obj->LastName . "\n";
|
||||
echo $obj->BirthDate . "\n\n";
|
||||
}
|
||||
echo $obj->BirthDate . "\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
function SelectDataBuffered()
|
||||
function selectDataBuffered()
|
||||
{
|
||||
global $conn, $tablename;
|
||||
|
||||
$stmt = sqlsrv_query($conn, "SELECT * FROM $tablename", array(), array("Scrollable"=>"buffered"));
|
||||
global $conn, $tableName;
|
||||
|
||||
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName", array(), array("Scrollable"=>"buffered"));
|
||||
|
||||
$row_count = sqlsrv_num_rows($stmt);
|
||||
echo "\nRow count for result set is $row_count\n";
|
||||
|
||||
|
||||
echo "First record=>\t";
|
||||
$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST);
|
||||
$SSN = sqlsrv_get_field( $stmt, 1);
|
||||
echo "SSN = $SSN \n";
|
||||
$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_FIRST);
|
||||
$SSN = sqlsrv_get_field($stmt, 1);
|
||||
echo "SSN = $SSN\n";
|
||||
|
||||
echo "Next record=>\t";
|
||||
$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_NEXT);
|
||||
$BirthDate = sqlsrv_get_field( $stmt, 4);
|
||||
echo "BirthDate = $BirthDate \n";
|
||||
|
||||
$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_NEXT);
|
||||
$BirthDate = sqlsrv_get_field($stmt, 4);
|
||||
echo "BirthDate = $BirthDate\n";
|
||||
|
||||
echo "Last record=>\t";
|
||||
$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_LAST);
|
||||
$LastName = sqlsrv_get_field( $stmt, 3);
|
||||
echo "LastName = $LastName \n";
|
||||
$row = sqlsrv_fetch($stmt, SQLSRV_SCROLL_LAST);
|
||||
$LastName = sqlsrv_get_field($stmt, 3);
|
||||
echo "LastName = $LastName\n";
|
||||
}
|
||||
|
||||
sqlsrv_configure( 'WarningsReturnAsErrors', 1 );
|
||||
sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL );
|
||||
|
||||
require_once( 'MsSetup.inc' );
|
||||
require_once( 'AE_Ksp.inc' );
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
sqlsrv_configure('LogSeverity', SQLSRV_LOG_SEVERITY_ALL);
|
||||
|
||||
$ksp_path = getKSPpath();
|
||||
|
||||
$connectionInfo = array( "Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd,
|
||||
"ReturnDatesAsStrings"=>true, "ColumnEncryption"=>'Enabled',
|
||||
"CEKeystoreProvider"=>$ksp_path,
|
||||
"CEKeystoreName"=>$ksp_name,
|
||||
"CEKeystoreEncryptKey"=>$encrypt_key);
|
||||
|
||||
$conn = sqlsrv_connect( $server, $connectionInfo );
|
||||
if( $conn === false )
|
||||
{
|
||||
echo "Failed to connect.\n";
|
||||
print_r( sqlsrv_errors() );
|
||||
}
|
||||
else
|
||||
{
|
||||
require_once('MsHelper.inc');
|
||||
$conn = AE\connect(array('ReturnDatesAsStrings'=>true));
|
||||
if($conn === false) {
|
||||
fatalError( "Failed to connect.\n");
|
||||
} else {
|
||||
echo "Connected successfully with ColumnEncryption enabled.\n";
|
||||
}
|
||||
|
||||
$tablename = CreatePatientsTable();
|
||||
|
||||
InsertData('748-68-0245', 'Jeannette', 'McDonald', '2002-11-28');
|
||||
InsertData('795-73-9838', 'John', 'Doe', '2001-05-29');
|
||||
InsertData('456-12-5486', 'Jonathan', 'Wong', '1999-12-20');
|
||||
InsertData('156-45-5486', 'Marianne', 'Smith', '1997-03-04');
|
||||
|
||||
SelectData();
|
||||
|
||||
$tableName = CreatePatientsTable();
|
||||
|
||||
insertData('748-68-0245', 'Jeannette', 'McDonald', '2002-11-28');
|
||||
insertData('795-73-9838', 'John', 'Doe', '2001-05-29');
|
||||
insertData('456-12-5486', 'Jonathan', 'Wong', '1999-12-20');
|
||||
insertData('156-45-5486', 'Marianne', 'Smith', '1997-03-04');
|
||||
|
||||
selectData();
|
||||
|
||||
///////////////////////////////////////////
|
||||
echo "Update Patient Jonathan Wong...\n";
|
||||
$params = array(array('1999-12-31', null, null, SQLSRV_SQLTYPE_DATE), array('Chang', null, null, SQLSRV_SQLTYPE_NVARCHAR(50)), array('456-12-5486', null, null, SQLSRV_SQLTYPE_CHAR(11)));
|
||||
|
||||
$params = array(array('1999-12-31', null, null, SQLSRV_SQLTYPE_DATE),
|
||||
array('Chang', null, null, SQLSRV_SQLTYPE_NVARCHAR(50)),
|
||||
array('456-12-5486', null, null, SQLSRV_SQLTYPE_CHAR(11)));
|
||||
|
||||
$tsql = "UPDATE Patients SET BirthDate = ?, LastName = ? WHERE SSN = ?";
|
||||
$stmt = sqlsrv_query($conn, $tsql, $params);
|
||||
|
||||
if (! $stmt)
|
||||
{
|
||||
echo "Failed to update record\n";
|
||||
print_r( sqlsrv_errors() );
|
||||
if (!$stmt) {
|
||||
fatalError("Failed to update record\n");
|
||||
}
|
||||
|
||||
|
||||
echo "Update his birthdate too...\n";
|
||||
$params = array(array('456-12-5486', null, null, SQLSRV_SQLTYPE_CHAR(11)));
|
||||
$params = array(array('456-12-5486', null, null, SQLSRV_SQLTYPE_CHAR(11)));
|
||||
$tsql = "SELECT SSN, FirstName, LastName, BirthDate FROM Patients WHERE SSN = ?";
|
||||
$stmt = sqlsrv_query($conn, $tsql, $params);
|
||||
if (! $stmt)
|
||||
{
|
||||
echo "Failed to select with a WHERE clause\n";
|
||||
print_r( sqlsrv_errors() );
|
||||
}
|
||||
else
|
||||
{
|
||||
$obj = sqlsrv_fetch_object( $stmt );
|
||||
|
||||
echo "BirthDate updated for $obj->FirstName:\n";
|
||||
if (!$stmt) {
|
||||
fatalError("Failed to select with a WHERE clause\n");
|
||||
} else {
|
||||
$obj = sqlsrv_fetch_object($stmt);
|
||||
echo "BirthDate updated for $obj->FirstName:\n";
|
||||
echo $obj->SSN . "\n";
|
||||
echo $obj->FirstName . "\n";
|
||||
echo $obj->LastName . "\n";
|
||||
echo $obj->BirthDate . "\n\n";
|
||||
echo $obj->BirthDate . "\n\n";
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////
|
||||
$procName = '#phpAEProc1';
|
||||
$spArgs = "@p1 INT, @p2 DATE OUTPUT";
|
||||
$spCode = "SET @p2 = ( SELECT [BirthDate] FROM Patients WHERE [PatientId] = @p1 )";
|
||||
$spCode = "SET @p2 = (SELECT [BirthDate] FROM Patients WHERE [PatientId] = @p1)";
|
||||
$stmt = sqlsrv_query($conn, "CREATE PROC [$procName] ($spArgs) AS BEGIN $spCode END");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
$callResult = '1900-01-01';
|
||||
|
||||
$callResult = '1900-01-01';
|
||||
//when binding parameter using sqlsrv_query in a column encryption enabled connection, need to provide the sql_type in all parameters
|
||||
$params = array( array( 1, SQLSRV_PARAM_IN, null, SQLSRV_SQLTYPE_INT ), array( &$callResult, SQLSRV_PARAM_OUT, null, SQLSRV_SQLTYPE_DATE));
|
||||
$params = array(array(1, SQLSRV_PARAM_IN, null, SQLSRV_SQLTYPE_INT),
|
||||
array(&$callResult, SQLSRV_PARAM_OUT, null, SQLSRV_SQLTYPE_DATE));
|
||||
$callArgs = "?, ?";
|
||||
$stmt = sqlsrv_query($conn, "{ CALL [$procName] ($callArgs)}", $params);
|
||||
if (! $stmt )
|
||||
{
|
||||
print_r( sqlsrv_errors() );
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!$stmt) {
|
||||
print_r(sqlsrv_errors());
|
||||
} else {
|
||||
echo "BirthDate for the first record is: $callResult\n";
|
||||
}
|
||||
|
||||
///////////////////////////////////////////
|
||||
///////////////////////////////////////////
|
||||
$procName = '#phpAEProc2';
|
||||
$spArgs = "@p1 INT, @p2 CHAR(11) OUTPUT";
|
||||
$spCode = "SET @p2 = ( SELECT [SSN] FROM Patients WHERE [PatientId] = @p1 )";
|
||||
$spCode = "SET @p2 = (SELECT [SSN] FROM Patients WHERE [PatientId] = @p1)";
|
||||
$stmt = sqlsrv_query($conn, "CREATE PROC [$procName] ($spArgs) AS BEGIN $spCode END");
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
|
||||
$callResult = '000-00-0000';
|
||||
//when binding parameter using sqlsrv_query in a column encryption enabled connection, need to provide the sql_type in all parameters
|
||||
$params = array( array( 1, SQLSRV_PARAM_IN, null, SQLSRV_SQLTYPE_INT ), array( &$callResult, SQLSRV_PARAM_OUT, null, SQLSRV_SQLTYPE_CHAR(11)));
|
||||
// when binding parameter using sqlsrv_query in a column encryption enabled connection,
|
||||
// need to provide the sql_type in all parameters
|
||||
$params = array(array(1, SQLSRV_PARAM_IN, null, SQLSRV_SQLTYPE_INT),
|
||||
array(&$callResult, SQLSRV_PARAM_OUT, null, SQLSRV_SQLTYPE_CHAR(11)));
|
||||
$callArgs = "?, ?";
|
||||
$stmt = sqlsrv_query($conn, "{ CALL [$procName] ($callArgs)}", $params);
|
||||
if (! $stmt )
|
||||
{
|
||||
print_r( sqlsrv_errors() );
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!$stmt) {
|
||||
print_r(sqlsrv_errors());
|
||||
} else {
|
||||
echo "SSN for the first record is: $callResult\n";
|
||||
}
|
||||
|
||||
SelectDataBuffered();
|
||||
|
||||
|
||||
selectDataBuffered();
|
||||
|
||||
echo "\nDone\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
|
@ -243,8 +206,8 @@ BirthDate for the first record is: 2002-11-28
|
|||
SSN for the first record is: 748-68-0245
|
||||
|
||||
Row count for result set is 4
|
||||
First record=> SSN = 748-68-0245
|
||||
Next record=> BirthDate = 2001-05-29
|
||||
Last record=> LastName = Smith
|
||||
First record=> SSN = 748-68-0245
|
||||
Next record=> BirthDate = 2001-05-29
|
||||
Last record=> LastName = Smith
|
||||
|
||||
Done
|
Loading…
Reference in a new issue