Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saved php types with metadata when fetching #1049

Merged
merged 4 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions source/pdo_sqlsrv/pdo_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,14 +782,11 @@ int pdo_sqlsrv_stmt_get_col_data( _Inout_ pdo_stmt_t *stmt, _In_ int colno,

// set the encoding if the user specified one via bindColumn, otherwise use the statement's encoding
// save the php type for next use
if (driver_stmt->current_meta_data[colno]->sqlsrv_php_type.typeinfo.type != SQLSRV_PHPTYPE_INVALID) {
sqlsrv_php_type = driver_stmt->current_meta_data[colno]->sqlsrv_php_type;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this cause problems with the new tests?

Copy link
Contributor Author

@yitam yitam Nov 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see in the pdo test, the user may choose different encoding or type for every single fetch of each row

else {
sqlsrv_php_type = driver_stmt->sql_type_to_php_type(static_cast<SQLUINTEGER>(driver_stmt->current_meta_data[colno]->field_type),
static_cast<SQLUINTEGER>( driver_stmt->current_meta_data[colno]->field_size ), true );
driver_stmt->current_meta_data[colno]->sqlsrv_php_type = sqlsrv_php_type;
}
sqlsrv_php_type = driver_stmt->sql_type_to_php_type(
static_cast<SQLUINTEGER>(driver_stmt->current_meta_data[colno]->field_type),
static_cast<SQLUINTEGER>( driver_stmt->current_meta_data[colno]->field_size ),
true );
driver_stmt->current_meta_data[colno]->sqlsrv_php_type = sqlsrv_php_type;

// if a column is bound to a type different than the column type, figure out a way to convert it to the
// type they want
Expand Down
81 changes: 81 additions & 0 deletions test/functional/pdo_sqlsrv/pdo_569_query_varcharmax_ae.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
--TEST--
GitHub issue #569 - direct query on varchar max fields results in function sequence error (Always Encrypted)
--DESCRIPTION--
This is similar to pdo_569_query_varcharmax.phpt but is not limited to testing the Always Encrypted feature in Windows only.
--ENV--
PHPT_EXEC=true
--SKIPIF--
<?php require('skipif_mid-refactor.inc'); ?>
--FILE--
<?php
require_once("MsCommon_mid-refactor.inc");

try {
$conn = connect();
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$tableName = 'pdoTestTable_569_ae';
createTable($conn, $tableName, array(new ColumnMeta("int", "id", "IDENTITY"), "c1" => "nvarchar(max)"));

$input = array();

$input[0] = 'some very large string';
$input[1] = '1234567890.1234';
$input[2] = 'über über';

$numRows = 3;
$tsql = "INSERT INTO $tableName (c1) VALUES (?)";

$stmt = $conn->prepare($tsql);
for ($i = 0; $i < $numRows; $i++) {
$stmt->bindParam(1, $input[$i]);
$stmt->execute();
}

$tsql = "SELECT id, c1 FROM $tableName ORDER BY id";
$stmt = $conn->prepare($tsql);
$stmt->execute();

// Fetch one row each time with different pdo type and/or encoding
$result = $stmt->fetch(PDO::FETCH_NUM);
if ($result[1] !== $input[0]) {
echo "Expected $input[0] but got: ";
var_dump($result[0]);
}

$stmt->bindColumn(2, $value, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_SYSTEM);
$result = $stmt->fetch(PDO::FETCH_BOUND);
if (!$result || $value !== $input[1]) {
echo "Expected $input[1] but got: ";
var_dump($result);
}

$stmt->bindColumn(2, $value, PDO::PARAM_STR);
$result = $stmt->fetch(PDO::FETCH_BOUND);
if (!$result || $value !== $input[2]) {
echo "Expected $input[2] but got: ";
var_dump($value);
}

// Fetch again but all at once
$stmt->execute();
$rows = $stmt->fetchall(PDO::FETCH_ASSOC);
for ($i = 0; $i < $numRows; $i++) {
$i = $rows[$i]['id'] - 1;
if ($rows[$i]['c1'] !== $input[$i]) {
echo "Expected $input[$i] but got: ";
var_dump($rows[$i]['c1']);
}
}

unset($stmt);
unset($conn);
} catch (PDOException $e) {
echo $e->getMessage();
}

echo "Done\n";

?>
--EXPECT--
Done
97 changes: 97 additions & 0 deletions test/functional/sqlsrv/srv_569_query_varcharmax_ae.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
--TEST--
GitHub issue #569 - sqlsrv_query on varchar max fields results in function sequence error
--DESCRIPTION--
This is similar to srv_569_query_varcharmax.phpt but is not limited to testing the Always Encrypted feature in Windows only.
--ENV--
PHPT_EXEC=true
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php
require_once('MsCommon.inc');

$conn = AE\connect(array('CharacterSet'=>'UTF-8'));

$tableName = 'srvTestTable_569_ae';

$columns = array(new AE\ColumnMeta('int', 'id', 'identity'),
new AE\ColumnMeta('nvarchar(max)', 'c1'));
AE\createTable($conn, $tableName, $columns);

$input = array();

$input[0] = 'some very large string';
$input[1] = '1234567890.1234';
$input[2] = 'über über';

$numRows = 3;
$isql = "INSERT INTO $tableName (c1) VALUES (?)";
for ($i = 0; $i < $numRows; $i++) {
$stmt = sqlsrv_prepare($conn, $isql, array($input[$i]));
$result = sqlsrv_execute($stmt);
if (!$result) {
fatalError("Failed to insert row $i into $tableName");
}
}

// Select all from test table
$tsql = "SELECT id, c1 FROM $tableName ORDER BY id";
$stmt = sqlsrv_prepare($conn, $tsql);
if (!$stmt) {
fatalError("Failed to read from $tableName");
}
$result = sqlsrv_execute($stmt);
if (!$result) {
fatalError("Failed to select data from $tableName");
}

readline("Press ENTER to continue: ");
// Fetch each row as an array
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$i = $row['id'] - 1;
if ($row['c1'] !== $input[$i]) {
echo "Expected $input[$i] but got: ";
var_dump($fieldVal);
}
}

// Fetch again, one field each time
sqlsrv_execute($stmt);

$i = 0;
while ($i < $numRows) {
sqlsrv_fetch($stmt);

switch ($i) {
case 0:
$fieldVal = sqlsrv_get_field($stmt, 1, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
break;
case 1:
$stream = sqlsrv_get_field($stmt, 1);
while (!feof( $stream)) {
$fieldVal = fread($stream, 50);
}
break;
default:
$fieldVal = sqlsrv_get_field($stmt, 1, SQLSRV_PHPTYPE_STRING('utf-8'));
break;
}

if ($fieldVal !== $input[$i]) {
echo 'Expected $input[$i] but got: ';
var_dump($fieldVal);
}

$i++;
}

dropTable($conn, $tableName);

echo "Done\n";

sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);

?>
--EXPECT--
Done