Used sqlsrv_query() instead

This commit is contained in:
Jenny Tam 2017-11-10 14:23:38 -08:00
parent 1daa4f0623
commit 094e32dc19
7 changed files with 75 additions and 39 deletions

View file

@ -372,7 +372,6 @@ function connect($options = array(), $disableCE = false)
$connectionOptions = array_merge($connectionOptions, array("ColumnEncryption" => "Enabled")); $connectionOptions = array_merge($connectionOptions, array("ColumnEncryption" => "Enabled"));
} }
if ($keystore == "ksp") { if ($keystore == "ksp") {
// require('AE_Ksp.inc');
$ksp_path = getKSPPath(); $ksp_path = getKSPPath();
$ksp_options = array("CEKeystoreProvider"=>$ksp_path, $ksp_options = array("CEKeystoreProvider"=>$ksp_path,
"CEKeystoreName"=>KSP_NAME, "CEKeystoreName"=>KSP_NAME,
@ -481,7 +480,7 @@ function selectFromTable($conn, $tbname, $conds = null, $values = null)
* @param resource $conn : connection resource * @param resource $conn : connection resource
* @param string $sql : T-SQL query * @param string $sql : T-SQL query
* @param string $conds : string of condition(s) possibly with placeholders, null by default * @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 * @param array $options : array of query options, null by default
* @return resource sqlsrv statement upon success or false otherwise * @return resource sqlsrv statement upon success or false otherwise
*/ */

View file

@ -20,7 +20,7 @@ $stmt = AE\insertRow($conn, $tableName, array('ID' => 'Aå_Ð×Æ×Ø_B'));
// Fetch data // Fetch data
$query = "SELECT * FROM $tableName"; $query = "SELECT * FROM $tableName";
$stmt = AE\executeQueryEx($conn, $query, array("Scrollable"=>"buffered")); $stmt = sqlsrv_query($conn, $query, [], array("Scrollable"=>"buffered"));
// Fetch // Fetch
$row = sqlsrv_fetch_array($stmt); $row = sqlsrv_fetch_array($stmt);

View file

@ -91,21 +91,51 @@ $columns = array(new AE\ColumnMeta('CHAR(4)', 'ID'),
AE\createTable($conn, $tableName1, $columns); AE\createTable($conn, $tableName1, $columns);
// Insert data // Insert data
$params = array('P001', 'Pencil 2B', '102', '24', '0.24', '2016-02-01', 'Red'); if (AE\isColEncrypted()) {
$data = getInputData1($params); $sql = "INSERT INTO $tableName1 VALUES
AE\insertRow($conn, $tableName1, $data); (?, ?, ?, ?, ?, ?, ?),
(?, ?, ?, ?, ?, ?, ?),
$params = array('P002', 'Notepad', '102', '12', '3.87', '2016-02-21', null); (?, ?, ?, ?, ?, ?, ?),
$data = getInputData1($params); (?, ?, ?, ?, ?, ?, ?)";
AE\insertRow($conn, $tableName1, $data); $stmt = sqlsrv_query($conn, $sql, array(array('P001', null, null, SQLSRV_SQLTYPE_CHAR(4)),
array('Pencil 2B', null, null, SQLSRV_SQLTYPE_VARCHAR(128)),
$params = array('P001', 'Mirror 2\"', '652', '3', '15.99', '2016-02-01', null); array('102', null, null, SQLSRV_SQLTYPE_SMALLINT),
$data = getInputData1($params); array('24', null, null, SQLSRV_SQLTYPE_INT),
AE\insertRow($conn, $tableName1, $data); array('0.24', null, null, SQLSRV_SQLTYPE_FLOAT),
array('2016-02-01', null, null, SQLSRV_SQLTYPE_DATETIME),
$params = array('P003', 'USB connector', '1652', '31', '9.99', '2016-02-01', null); array('Red', null, null, SQLSRV_SQLTYPE_VARCHAR(20)),
$data = getInputData1($params); array('P002', null, null, SQLSRV_SQLTYPE_CHAR(4)),
AE\insertRow($conn, $tableName1, $data); array('Notepad', null, null, SQLSRV_SQLTYPE_VARCHAR(128)),
array('102', null, null, SQLSRV_SQLTYPE_SMALLINT),
array('12', null, null, SQLSRV_SQLTYPE_INT),
array('3.87', null, null, SQLSRV_SQLTYPE_FLOAT),
array('2016-02-21', null, null, SQLSRV_SQLTYPE_DATETIME),
array(null, null, null, SQLSRV_SQLTYPE_VARCHAR(20)),
array('P001', null, null, SQLSRV_SQLTYPE_CHAR(4)),
array('Mirror 2\"', null, null, SQLSRV_SQLTYPE_VARCHAR(128)),
array('652', null, null, SQLSRV_SQLTYPE_SMALLINT),
array('3', null, null, SQLSRV_SQLTYPE_INT),
array('15.99', null, null, SQLSRV_SQLTYPE_FLOAT),
array('2016-02-01', null, null, SQLSRV_SQLTYPE_DATETIME),
array(null, null, null, SQLSRV_SQLTYPE_VARCHAR(20)),
array('P003', null, null, SQLSRV_SQLTYPE_CHAR(4)),
array('USB connector', null, null, SQLSRV_SQLTYPE_VARCHAR(128)),
array('1652', null, null, SQLSRV_SQLTYPE_SMALLINT),
array('31', null, null, SQLSRV_SQLTYPE_INT),
array('9.99', null, null, SQLSRV_SQLTYPE_FLOAT),
array('2016-02-01', null, null, SQLSRV_SQLTYPE_DATETIME),
array(null, null, null, SQLSRV_SQLTYPE_VARCHAR(20))));
} else {
$sql = "INSERT INTO $tableName1 VALUES
('P001', 'Pencil 2B', '102', '24', '0.24', '2016-02-01', 'Red'),
('P002', 'Notepad', '102', '12', '3.87', '2016-02-21', Null),
('P001', 'Mirror 2\"', '652', '3', '15.99', '2016-02-01', NULL),
('P003', 'USB connector', '1652', '31', '9.99', '2016-02-01', NULL)";
$stmt = sqlsrv_query($conn, $sql);
}
if (!$stmt) {
fatalError("Failed to insert test data into $tableName1\n");
}
// Create table Country // Create table Country
$columns = array(new AE\ColumnMeta('CHAR(4)', 'SerialNumber'), $columns = array(new AE\ColumnMeta('CHAR(4)', 'SerialNumber'),
@ -113,17 +143,21 @@ $columns = array(new AE\ColumnMeta('CHAR(4)', 'SerialNumber'),
AE\createTable($conn, $tableName2, $columns); AE\createTable($conn, $tableName2, $columns);
// Insert data // Insert data
$params = array('P001', 'FR'); if (AE\isColEncrypted()) {
$data = getInputData2($params); $sql = "INSERT INTO $tableName2 VALUES (?, ?), (?, ?), (?, ?)";
AE\insertRow($conn, $tableName2, $data); $stmt = sqlsrv_query($conn, $sql, array(array('P001', null, null, SQLSRV_SQLTYPE_CHAR(4)),
array('FR', null, null, SQLSRV_SQLTYPE_VARCHAR(2)),
$params = array('P002', 'UK'); array('P002', null, null, SQLSRV_SQLTYPE_CHAR(4)),
$data = getInputData2($params); array('UK', null, null, SQLSRV_SQLTYPE_VARCHAR(2)),
AE\insertRow($conn, $tableName2, $data); array('P003', null, null, SQLSRV_SQLTYPE_CHAR(4)),
array('DE', null, null, SQLSRV_SQLTYPE_VARCHAR(2))));
$params = array('P003', 'DE'); } else {
$data = getInputData2($params); $sql = "INSERT INTO $tableName2 VALUES ('P001', 'FR'), ('P002', 'UK'), ('P003', 'DE')";
AE\insertRow($conn, $tableName2, $data); $stmt = sqlsrv_query($conn, $sql);
}
if (!$stmt) {
fatalError("Failed to insert test data into $tableName2\n");
}
// With AE enabled, we cannot do comparisons with encrypted columns // With AE enabled, we cannot do comparisons with encrypted columns
// Also, only forward cursor or client buffer is supported // Also, only forward cursor or client buffer is supported

View file

@ -9,7 +9,7 @@ Test sqlsrv_num_rows method.
require_once('MsCommon.inc'); require_once('MsCommon.inc');
$conn = AE\connect(); $conn = AE\connect();
$tableName = 'utf16invalid'; $tableName = 'testNumRows';
$columns = array(new AE\ColumnMeta('int', 'id', 'identity'), $columns = array(new AE\ColumnMeta('int', 'id', 'identity'),
new AE\ColumnMeta('nvarchar(100)', 'c1')); new AE\ColumnMeta('nvarchar(100)', 'c1'));
@ -24,7 +24,7 @@ Test sqlsrv_num_rows method.
} else { } else {
$options = array('Scrollable' => SQLSRV_CURSOR_KEYSET); $options = array('Scrollable' => SQLSRV_CURSOR_KEYSET);
} }
$stmt = AE\executeQueryEx($conn, "SELECT * FROM $tableName", $options); $stmt = sqlsrv_query($conn, "SELECT * FROM $tableName", array(), $options);
$row_nums = sqlsrv_num_rows($stmt); $row_nums = sqlsrv_num_rows($stmt);
echo $row_nums; echo $row_nums;

View file

@ -40,7 +40,7 @@ if (sqlsrv_has_rows($stmt)) {
} }
// Fetch data using a scrollable cursor // Fetch data using a scrollable cursor
$stmt = AE\executeQueryEx($conn, $query, array("Scrollable"=>"buffered")); $stmt = sqlsrv_query($conn, $query, [], array("Scrollable"=>"buffered"));
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n"; echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n"; echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";

View file

@ -60,7 +60,7 @@ if (!$stmt) {
} }
$query = "SELECT TOP 2 * FROM $tableName"; $query = "SELECT TOP 2 * FROM $tableName";
$stmt = AE\executeQueryEx($conn, $query, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED)); $stmt = sqlsrv_query($conn, $query, array(), array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED));
$array = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC); $array = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC);
var_dump($array); var_dump($array);

View file

@ -73,14 +73,15 @@ for ($i = 1; $i <= $numRows; $i++) {
// https://github.com/Microsoft/msphpsql/wiki/Features#aelimitation // https://github.com/Microsoft/msphpsql/wiki/Features#aelimitation
$query = "SELECT * FROM $tableName"; $query = "SELECT * FROM $tableName";
$options = array('Scrollable' => SQLSRV_CURSOR_FORWARD); $options = array('Scrollable' => SQLSRV_CURSOR_FORWARD);
$stmt = AE\executeQueryEx($conn, $query, $options); $stmt = sqlsrv_query($conn, $query, array(), $options);
hasRows($stmt, false); hasRows($stmt, false);
countRows($stmt, $numRows, 'forward only'); countRows($stmt, $numRows, 'forward only');
sqlsrv_free_stmt($stmt); sqlsrv_free_stmt($stmt);
if (! AE\isColEncrypted()) { if (! AE\isColEncrypted()) {
$options = array('Scrollable' => 'static'); $options = array('Scrollable' => 'static');
$stmt = AE\executeQueryEx($conn, $query, $options); $stmt = sqlsrv_query($conn, $query, array(), $options);
$result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_ABSOLUTE, 4); $result = sqlsrv_fetch($stmt, SQLSRV_SCROLL_ABSOLUTE, 4);
if($result !== null) { if($result !== null) {
@ -128,13 +129,14 @@ if (! AE\isColEncrypted()) {
sqlsrv_free_stmt($stmt); sqlsrv_free_stmt($stmt);
$options = array('Scrollable' => 'static'); $options = array('Scrollable' => 'static');
$stmt = AE\executeQueryEx($conn, $query, $options); $stmt = sqlsrv_query($conn, $query, array(), $options);
hasRows($stmt, false); hasRows($stmt, false);
countRows($stmt, $numRows, 'static'); countRows($stmt, $numRows, 'static');
sqlsrv_free_stmt($stmt); sqlsrv_free_stmt($stmt);
$options = array('Scrollable' => 'dynamic'); $options = array('Scrollable' => 'dynamic');
$stmt = AE\executeQueryEx($conn, $query, $options); $stmt = sqlsrv_query($conn, $query, array(), $options);
sqlsrv_fetch($stmt); sqlsrv_fetch($stmt);
sqlsrv_fetch($stmt); sqlsrv_fetch($stmt);
@ -148,7 +150,8 @@ if (! AE\isColEncrypted()) {
sqlsrv_free_stmt($stmt); sqlsrv_free_stmt($stmt);
$options = array('Scrollable' => SQLSRV_CURSOR_STATIC); $options = array('Scrollable' => SQLSRV_CURSOR_STATIC);
$stmt = AE\executeQueryEx($conn, $query, $options); $stmt = sqlsrv_query($conn, $query, array(), $options);
$row_count = sqlsrv_num_rows($stmt); $row_count = sqlsrv_num_rows($stmt);
if($row_count != $numRows) { if($row_count != $numRows) {
die("sqlsrv_num_rows should have returned 6 rows in the static cursor\n"); die("sqlsrv_num_rows should have returned 6 rows in the static cursor\n");
@ -164,7 +167,7 @@ if (! AE\isColEncrypted()) {
} }
$options = array('Scrollable' => SQLSRV_CURSOR_DYNAMIC); $options = array('Scrollable' => SQLSRV_CURSOR_DYNAMIC);
$stmt = AE\executeQueryEx($conn, $query, $options); $stmt = sqlsrv_query($conn, $query, array(), $options);
$result = sqlsrv_num_rows($stmt); $result = sqlsrv_num_rows($stmt);
if($result !== false) { if($result !== false) {