Use constants plus changes to BindParamOption and insertRow

This commit is contained in:
Jenny Tam 2017-10-04 16:51:15 -07:00
parent a7e0baf897
commit cf31031b53
10 changed files with 60 additions and 63 deletions

View file

@ -12,25 +12,37 @@ namespace AE {
require_once('MsSetup.inc'); require_once('MsSetup.inc');
const KEYSTORE_NONE = 'none';
const KEYSTORE_WIN = 'win';
const KEYSTORE_KSP = 'ksp';
const KEYSTORE_AKV = 'akv';
const INSERT_QUERY = 1;
const INSERT_PREPARE = 2;
const INSERT_QUERY_PARAMS = 3;
const INSERT_PREPARE_PARAMS = 4;
/** /**
* class for encapsulating column metadata needed for creating a table * class for encapsulating column metadata needed for creating a table
*/ */
class ColumnMeta class ColumnMeta
{ {
public $colName; public $colName;
public $dataType; //a string that includes the size of the type if necessary (e.g., decimal(10,5)) public $dataType; // a string that includes the size of the type if necessary (e.g., decimal(10,5))
public $encType; //randomized or deterministic; default is deterministic public $encType; // randomized or deterministic; default is deterministic
public $options; //a string that is null by default (e.g. NOT NULL Identity (1,1) ) public $options; // a string that is null by default (e.g. NOT NULL Identity (1,1) )
public function __construct($dataType, $colName = null, $options = null, $encType = "deterministic") public function __construct($dataType, $colName = null, $options = null, $deterministic = true)
{ {
if (is_null($colName)) { if (is_null($colName)) {
$this->colName = getDefaultColname($dataType); $this->colName = getDefaultColname($dataType);
} else { } else {
$this->colName = $colName; $this->colName = $colName;
} }
$this->encType = ($deterministic ? "deterministic" : "randomized");
$this->dataType = $dataType; $this->dataType = $dataType;
$this->encType = $encType;
$this->options = $options; $this->options = $options;
} }
/** /**
@ -41,7 +53,6 @@ class ColumnMeta
$append = " "; $append = " ";
// an identity column is not encrypted because a select query with identity column as the where clause is often run and the user want to have to bind parameter every time // an identity column is not encrypted because a select query with identity column as the where clause is often run and the user want to have to bind parameter every time
// if ( $keystore != "none" && stripos( $this->options, "identity" ) === false )
if (isColEncrypted() && stripos($this->options, "identity") === false) { if (isColEncrypted() && stripos($this->options, "identity") === false) {
$cekName = getCekName(); $cekName = getCekName();
if (stripos($this->dataType, "char") !== false) { if (stripos($this->dataType, "char") !== false) {
@ -60,14 +71,14 @@ class ColumnMeta
*/ */
class BindParamOption class BindParamOption
{ {
public $index; //1-based index of the parameter public $value; // the param value
public $direction; //SQLSRV_PARAM_ constant indicating the parameter direction public $direction; // SQLSRV_PARAM_ constant indicating the parameter direction
public $phpType; //SQLSRV_PHPTYPE_ constant specifying the php type of the return values public $phpType; // SQLSRV_PHPTYPE_ constant specifying the php type of the return values
public $sqlType; //SQLSRV_SQLTYPE_ constant specifying the SQL type of the input public $sqlType; // SQLSRV_SQLTYPE_ constant specifying the SQL type of the input
public function __construct($index, $direction = null, $phpType = null, $sqlType = null) public function __construct($value, $direction = null, $phpType = null, $sqlType = null)
{ {
$this->index = $index; $this->value = $value;
$this->direction = $direction; $this->direction = $direction;
$this->phpType = $phpType; $this->phpType = $phpType;
$this->sqlType = $sqlType; $this->sqlType = $sqlType;
@ -114,7 +125,7 @@ class BindParamOption
$sqlType = constant($type); $sqlType = constant($type);
} }
} }
return array( $var, $direction, $phpType, $sqlType ); return array($var, $direction, $phpType, $sqlType);
} }
} }
@ -127,16 +138,16 @@ function getCekName()
$cekName = ''; $cekName = '';
switch ($keystore) { switch ($keystore) {
case "none": case KEYSTORE_NONE:
$cekName = ''; $cekName = '';
break; break;
case "win": case KEYSTORE_WIN:
$cekName = 'AEColumnKey'; $cekName = 'AEColumnKey';
break; break;
case "ksp": case KEYSTORE_KSP:
$cekName = 'CustomCEK'; $cekName = 'CustomCEK';
break; break;
case "akv": case KEYSTORE_AKV:
$cekName = 'AKVColumnKey'; $cekName = 'AKVColumnKey';
break; break;
default: default:
@ -240,7 +251,7 @@ function getSeqPlaceholders($num)
function isColEncrypted() function isColEncrypted()
{ {
global $keystore, $dataEncrypted; global $keystore, $dataEncrypted;
if ($keystore == "none" || !$dataEncrypted) { if ($keystore == KEYSTORE_NONE || !$dataEncrypted) {
return false; return false;
} else { } else {
return true; return true;
@ -318,25 +329,25 @@ function createTable($conn, $tbname, $columnMetaArr)
* Insert a row into a table * Insert a row into a table
* @param object $conn : sqlsrv connection object * @param object $conn : sqlsrv connection object
* @param string $tbname : name of the table for the row to be inserted * @param string $tbname : name of the table for the row to be inserted
* @param array $inputs : an associative array column name and its value * @param array $inputs : an associative array column name and its value, which may be a
* literal or a BindParamOption object
* @param bool $r : true if the row was successfully inserted, otherwise false. Default value is null to make this parameter optional. * @param bool $r : true if the row was successfully inserted, otherwise false. Default value is null to make this parameter optional.
* $param string $api : SQLSRV API used for executing the insert query * $param string $api : SQLSRV API used for executing the insert query
* accepted values: "query", "queryParamsOp", "prepare", "prepareParamsOp" * accepted values: INSERT_QUERY, INSERT_PREPARE, INSERT_QUERY_PARAMS, INSERT_PREPARE_PARAMS
* @param array $paramOption : an array of bindParamOptions. Should only be provided if $api is "perpareWithParamOp", if provided, has to be in the same order as $inputs * @return object sqlsrv statement object of the insert statement
* @return object PDOStatement object of the insert statement
*/ */
function insertRow($conn, $tbname, $inputs, &$r = null, $api = "query", $paramOption = array()) function insertRow($conn, $tbname, $inputs, &$r = null, $api = INSERT_QUERY)
{ {
require 'MsSetup.inc'; require 'MsSetup.inc';
$stmt = null; $stmt = null;
if ($keystore == "none" && $api != "queryParamsOp" && $api != "prepareParamsOp") { if (!isColEncrypted() && $api < INSERT_QUERY_PARAMS) {
$insertSql = getInsertSqlComplete($tbname, $inputs); $insertSql = getInsertSqlComplete($tbname, $inputs);
switch ($api) { switch ($api) {
case "query": case INSERT_QUERY:
$stmt = sqlsrv_query($conn, $insertSql); $stmt = sqlsrv_query($conn, $insertSql);
break; break;
case "prepare": case INSERT_PREPARE:
$stmt = sqlsrv_prepare($conn, $insertSql); $stmt = sqlsrv_prepare($conn, $insertSql);
$r = sqlsrv_execute($stmt); $r = sqlsrv_execute($stmt);
break; break;
@ -345,29 +356,15 @@ function insertRow($conn, $tbname, $inputs, &$r = null, $api = "query", $paramOp
// if AE is on, must bind param // if AE is on, must bind param
$insertSql = getInsertSqlPlaceholders($tbname, $inputs); $insertSql = getInsertSqlPlaceholders($tbname, $inputs);
$params = array(); $params = array();
if (empty($paramOption)) { foreach ($inputs as $key => $input) {
foreach ($inputs as $key => $value) { if (!is_object($input)) {
array_push($params, $input->bindParamArr($inputs[$key]));
} else {
array_push($params, $inputs[$key]); array_push($params, $inputs[$key]);
} }
} else {
$i = 1;
foreach ($inputs as $key => $value) {
$nooption = true;
foreach ($paramOption as $op) {
if ($op->index == $i) {
array_push($params, $op->bindParamArr($inputs[$key]));
$nooption = false;
$i++;
break;
}
}
if ($nooption) {
array_push($params, $inputs[$key]);
$i++;
}
}
} }
if ($keystore != "none" || $api == "prepareParamsOp") {
if ($api == INSERT_PREPARE_PARAMS) {
$stmt = sqlsrv_prepare($conn, $insertSql, $params); $stmt = sqlsrv_prepare($conn, $insertSql, $params);
$r = sqlsrv_execute($stmt); $r = sqlsrv_execute($stmt);
} else { } else {

View file

@ -18,7 +18,7 @@ foreach ($dataTypes as $dataType) {
// create table // create table
$tbname = GetTempTableName("", false); $tbname = GetTempTableName("", false);
$colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, "randomized")); $colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr); AE\createTable($conn, $tbname, $colMetaArr);
// insert a row // insert a row

View file

@ -20,7 +20,7 @@ foreach ($dataTypes as $dataType) {
// create table // create table
$tbname = GetTempTableName("", false); $tbname = GetTempTableName("", false);
$colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, "randomized")); $colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr); AE\createTable($conn, $tbname, $colMetaArr);
// insert a row // insert a row

View file

@ -17,7 +17,7 @@ foreach ($dataTypes as $dataType) {
// create table // create table
$tbname = GetTempTableName("", false); $tbname = GetTempTableName("", false);
$colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, "randomized")); $colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr); AE\createTable($conn, $tbname, $colMetaArr);
// insert a row // insert a row

View file

@ -17,7 +17,7 @@ $colMetaArr = array( new AE\ColumnMeta("int", "PatientId", "IDENTITY(1,1)"),
new AE\ColumnMeta("char(11)", "SSN"), new AE\ColumnMeta("char(11)", "SSN"),
new AE\ColumnMeta("nvarchar(50)", "FirstName", "NULL"), new AE\ColumnMeta("nvarchar(50)", "FirstName", "NULL"),
new AE\ColumnMeta("nvarchar(50)", "LastName", "NULL"), new AE\ColumnMeta("nvarchar(50)", "LastName", "NULL"),
new AE\ColumnMeta("date", "BirthDate", null, "randomized")); new AE\ColumnMeta("date", "BirthDate", null, false));
AE\createTable($conn, $tbname, $colMetaArr); AE\createTable($conn, $tbname, $colMetaArr);
// insert a row // insert a row

View file

@ -28,7 +28,7 @@ foreach ($dataTypes as $dataType) {
// create table // create table
$tbname = GetTempTableName("", false); $tbname = GetTempTableName("", false);
$colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, "randomized")); $colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr); AE\createTable($conn, $tbname, $colMetaArr);
// test each SQLSRV_SQLTYPE_ constants // test each SQLSRV_SQLTYPE_ constants
@ -36,9 +36,9 @@ foreach ($dataTypes as $dataType) {
// insert a row // insert a row
$inputValues = array_slice(${explode("(", $dataType)[0] . "_params"}, 1, 2); $inputValues = array_slice(${explode("(", $dataType)[0] . "_params"}, 1, 2);
$sqlType = get_default_size_prec($sqlType); $sqlType = get_default_size_prec($sqlType);
$paramOp = array( new AE\BindParamOption(1, null, null, $sqlType), new AE\BindParamOption(2, null, null, $sqlType)); $inputs = array(new AE\BindParamOption($inputValues[0], null, null, $sqlType), new AE\BindParamOption($inputValues[1], null, null, $sqlType));
$r; $r;
$stmt = AE\insertRow($conn, $tbname, array( $colMetaArr[0]->colName => $inputValues[0], $colMetaArr[1]->colName => $inputValues[1] ), $r, "prepareParamsOp", $paramOp); $stmt = AE\insertRow($conn, $tbname, array( $colMetaArr[0]->colName => $inputs[0], $colMetaArr[1]->colName => $inputs[1] ), $r, AE\INSERT_PREPARE_PARAMS);
if (!AE\isColEncrypted()) { if (!AE\isColEncrypted()) {
if ($r === false) { if ($r === false) {

View file

@ -23,7 +23,7 @@ foreach ($dataTypes as $dataType) {
// create table // create table
$tbname = GetTempTableName("", false); $tbname = GetTempTableName("", false);
$colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, "randomized")); $colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr); AE\createTable($conn, $tbname, $colMetaArr);
// test each SQLSRV_SQLTYPE_ constants // test each SQLSRV_SQLTYPE_ constants
@ -31,9 +31,9 @@ foreach ($dataTypes as $dataType) {
// insert a row // insert a row
$inputValues = array_slice(${explode("(", $dataType)[0] . "_params"}, 1, 2); $inputValues = array_slice(${explode("(", $dataType)[0] . "_params"}, 1, 2);
$sqlType = get_default_size_prec($sqlType); $sqlType = get_default_size_prec($sqlType);
$paramOp = array( new AE\BindParamOption(1, null, null, $sqlType), new AE\BindParamOption(2, null, null, $sqlType)); $inputs = array(new AE\BindParamOption($inputValues[0], null, null, $sqlType), new AE\BindParamOption($inputValues[1], null, null, $sqlType));
$r; $r;
$stmt = AE\insertRow($conn, $tbname, array( $colMetaArr[0]->colName => $inputValues[0], $colMetaArr[1]->colName => $inputValues[1] ), $r, "prepareParamsOp", $paramOp); $stmt = AE\insertRow($conn, $tbname, array($colMetaArr[0]->colName => $inputs[0], $colMetaArr[1]->colName => $inputs[1]), $r, AE\INSERT_PREPARE_PARAMS);
if (!AE\isColEncrypted()) { if (!AE\isColEncrypted()) {
if ($r === false) { if ($r === false) {

View file

@ -31,7 +31,7 @@ foreach ($dataTypes as $dataType) {
// create table // create table
$tbname = GetTempTableName("", false); $tbname = GetTempTableName("", false);
$colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, "randomized")); $colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr); AE\createTable($conn, $tbname, $colMetaArr);
// test each SQLSRV_SQLTYPE_ constants // test each SQLSRV_SQLTYPE_ constants
@ -39,9 +39,9 @@ foreach ($dataTypes as $dataType) {
// insert a row // insert a row
$inputValues = array_slice(${explode("(", $dataType)[0] . "_params"}, 1, 2); $inputValues = array_slice(${explode("(", $dataType)[0] . "_params"}, 1, 2);
$sqlType = get_default_size_prec($sqlType); $sqlType = get_default_size_prec($sqlType);
$paramOp = array( new AE\BindParamOption(1, null, null, $sqlType), new AE\BindParamOption(2, null, null, $sqlType)); $inputs = array(new AE\BindParamOption($inputValues[0], null, null, $sqlType), new AE\BindParamOption($inputValues[1], null, null, $sqlType));
$r; $r;
$stmt = AE\insertRow($conn, $tbname, array( $colMetaArr[0]->colName => $inputValues[0], $colMetaArr[1]->colName => $inputValues[1] ), $r, "prepareParamsOp", $paramOp); $stmt = AE\insertRow($conn, $tbname, array( $colMetaArr[0]->colName => $inputs[0], $colMetaArr[1]->colName => $inputs[1] ), $r, AE\INSERT_PREPARE_PARAMS);
if (!AE\isColEncrypted()) { if (!AE\isColEncrypted()) {
if ($r === false) { if ($r === false) {

View file

@ -25,7 +25,7 @@ foreach ($dataTypes as $dataType) {
// create table // create table
$tbname = GetTempTableName("", false); $tbname = GetTempTableName("", false);
$colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, "randomized")); $colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr); AE\createTable($conn, $tbname, $colMetaArr);
// test each SQLSRV_SQLTYPE_ constants // test each SQLSRV_SQLTYPE_ constants
@ -33,9 +33,9 @@ foreach ($dataTypes as $dataType) {
// insert a row // insert a row
$inputValues = array_slice(${explode("(", $dataType)[0] . "_params"}, 1, 2); $inputValues = array_slice(${explode("(", $dataType)[0] . "_params"}, 1, 2);
$sqlType = get_default_size_prec($sqlType); $sqlType = get_default_size_prec($sqlType);
$paramOp = array( new AE\BindParamOption(1, null, null, $sqlType), new AE\BindParamOption(2, null, null, $sqlType)); $inputs = array(new AE\BindParamOption($inputValues[0], null, null, $sqlType), new AE\BindParamOption($inputValues[1], null, null, $sqlType));
$r; $r;
$stmt = AE\insertRow($conn, $tbname, array( $colMetaArr[0]->colName => $inputValues[0], $colMetaArr[1]->colName => $inputValues[1] ), $r, "prepareParamsOp", $paramOp); $stmt = AE\insertRow($conn, $tbname, array( $colMetaArr[0]->colName => $inputs[0], $colMetaArr[1]->colName => $inputs[1] ), $r, AE\INSERT_PREPARE_PARAMS);
if ($r === false) { if ($r === false) {
if (!AE\isColEncrypted()) { if (!AE\isColEncrypted()) {

View file

@ -17,7 +17,7 @@ foreach ($dataTypes as $dataType) {
// create table // create table
$tbname = GetTempTableName("", false); $tbname = GetTempTableName("", false);
$colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, "randomized")); $colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr); AE\createTable($conn, $tbname, $colMetaArr);
// insert a row // insert a row