2017-03-09 23:12:53 +01:00
|
|
|
|
--TEST--
|
|
|
|
|
Test insert various numeric data types and fetch them back as strings
|
|
|
|
|
--FILE--
|
|
|
|
|
<?php
|
2017-10-04 23:22:56 +02:00
|
|
|
|
require_once('MsCommon.inc');
|
2017-03-09 23:12:53 +01:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
function execDataValue($conn, $numRows, $phpType = SQLSRV_PHPTYPE_NULL)
|
2017-03-09 23:12:53 +01:00
|
|
|
|
{
|
2017-11-29 23:40:47 +01:00
|
|
|
|
$tableName = 'param_query_value';
|
|
|
|
|
$columns = array(new AE\ColumnMeta('int', 'c1_int'),
|
|
|
|
|
new AE\ColumnMeta('smallint', 'c2_smallint'));
|
|
|
|
|
$stmt = AE\createTable($conn, $tableName, $columns);
|
|
|
|
|
if (!$stmt) {
|
|
|
|
|
fatalError("failed to create table $tableName\n");
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
if ($phpType == SQLSRV_PHPTYPE_NULL) {
|
2017-03-09 23:12:53 +01:00
|
|
|
|
echo "Insert integers without PHP type\n";
|
|
|
|
|
$stmt = sqlsrv_prepare($conn, "INSERT INTO [$tableName] (c1_int, c2_smallint) VALUES (?, ?)", array(array(&$v1), array(&$v2)));
|
2017-10-04 23:22:56 +02:00
|
|
|
|
} else { // SQLSRV_PHPTYPE_INT
|
2017-03-09 23:12:53 +01:00
|
|
|
|
echo "Insert integers as SQLSRV_PHPTYPE_INT\n";
|
|
|
|
|
$stmt = sqlsrv_prepare($conn, "INSERT INTO [$tableName] (c1_int, c2_smallint) VALUES (?, ?)", array(array(&$v1, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_INT), array(&$v2, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_INT)));
|
|
|
|
|
}
|
2017-11-29 23:40:47 +01:00
|
|
|
|
if (!$stmt) {
|
|
|
|
|
fatalError("execDataValue: failed to prepare statement!");
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
$value = 1;
|
|
|
|
|
for ($i = 0; $i < $numRows; $i++) {
|
2017-03-09 23:12:53 +01:00
|
|
|
|
$v1 = $value;
|
|
|
|
|
$v2 = $v1 + 1;
|
2017-11-29 23:40:47 +01:00
|
|
|
|
$res = sqlsrv_execute($stmt);
|
|
|
|
|
if (!$res) {
|
|
|
|
|
fatalError("execDataValue: failed to insert $v1, $v2");
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-03-09 23:12:53 +01:00
|
|
|
|
$value += 10;
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
sqlsrv_free_stmt($stmt);
|
|
|
|
|
|
2017-03-09 23:12:53 +01:00
|
|
|
|
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName");
|
2017-11-29 23:40:47 +01:00
|
|
|
|
fetchData($stmt, $numRows);
|
2017-03-09 23:12:53 +01:00
|
|
|
|
|
2017-10-04 23:22:56 +02:00
|
|
|
|
sqlsrv_free_stmt($stmt);
|
2017-11-29 23:40:47 +01:00
|
|
|
|
|
|
|
|
|
dropTable($conn, $tableName);
|
2017-03-09 23:12:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
function execDataParam($conn, $numRows, $withParam = false)
|
2017-03-09 23:12:53 +01:00
|
|
|
|
{
|
2017-11-29 23:40:47 +01:00
|
|
|
|
$tableName = 'param_query_param';
|
|
|
|
|
$columns = array(new AE\ColumnMeta('float', 'c1_float'),
|
|
|
|
|
new AE\ColumnMeta('real', 'c2_real'));
|
|
|
|
|
$stmt = AE\createTable($conn, $tableName, $columns);
|
|
|
|
|
if (!$stmt) {
|
|
|
|
|
fatalError("failed to create table $tableName\n");
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
if ($withParam) {
|
2017-03-09 23:12:53 +01:00
|
|
|
|
echo "Insert floats with direction specified\n";
|
|
|
|
|
$stmt = sqlsrv_prepare($conn, "INSERT INTO [$tableName] (c1_float, c2_real) VALUES (?, ?)", array(array(&$v1, SQLSRV_PARAM_IN), array(&$v2, SQLSRV_PARAM_IN)));
|
2017-10-04 23:22:56 +02:00
|
|
|
|
} else { // no param
|
2017-03-09 23:12:53 +01:00
|
|
|
|
echo "Insert floats without direction\n";
|
|
|
|
|
$stmt = sqlsrv_prepare($conn, "INSERT INTO [$tableName] (c1_float, c2_real) VALUES (?, ?)", array(&$v1, &$v2));
|
|
|
|
|
}
|
2017-11-29 23:40:47 +01:00
|
|
|
|
if (!$stmt) {
|
|
|
|
|
fatalError("execDataParam: failed to prepare statement!");
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
$value = 1.0;
|
|
|
|
|
for ($i = 0; $i < $numRows; $i++) {
|
2017-03-09 23:12:53 +01:00
|
|
|
|
$v1 = $value;
|
|
|
|
|
$v2 = $v1 + 1.0;
|
2017-11-29 23:40:47 +01:00
|
|
|
|
$res = sqlsrv_execute($stmt);
|
|
|
|
|
if (!$res) {
|
|
|
|
|
fatalError("execDataParam: failed to insert $v1, $v2");
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-03-09 23:12:53 +01:00
|
|
|
|
$value += 10;
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
sqlsrv_free_stmt($stmt);
|
|
|
|
|
|
2017-03-09 23:12:53 +01:00
|
|
|
|
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName");
|
2017-11-29 23:40:47 +01:00
|
|
|
|
fetchData($stmt, $numRows);
|
2017-03-09 23:12:53 +01:00
|
|
|
|
|
2017-10-04 23:22:56 +02:00
|
|
|
|
sqlsrv_free_stmt($stmt);
|
2017-11-29 23:40:47 +01:00
|
|
|
|
|
|
|
|
|
dropTable($conn, $tableName);
|
2017-03-09 23:12:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
function fetchData($stmt, $numRows)
|
2017-03-09 23:12:53 +01:00
|
|
|
|
{
|
2017-10-04 23:22:56 +02:00
|
|
|
|
for ($i = 0; $i < $numRows; $i++) {
|
2017-03-09 23:12:53 +01:00
|
|
|
|
sqlsrv_fetch($stmt);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-03-09 23:12:53 +01:00
|
|
|
|
$value = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
|
|
|
|
|
echo "$value, ";
|
|
|
|
|
|
|
|
|
|
$value = sqlsrv_get_field($stmt, 1, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
|
2017-10-04 23:22:56 +02:00
|
|
|
|
echo "$value\n";
|
|
|
|
|
}
|
2017-03-09 23:12:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
echo "\nTest begins...\n";
|
|
|
|
|
try {
|
|
|
|
|
set_time_limit(0);
|
|
|
|
|
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
// connect
|
|
|
|
|
$conn = AE\connect();
|
2017-03-09 23:12:53 +01:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
$numRows = 5;
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
execDataValue($conn, $numRows);
|
|
|
|
|
execDataValue($conn, $numRows, SQLSRV_PHPTYPE_INT);
|
|
|
|
|
execDataParam($conn, $numRows, true);
|
|
|
|
|
execDataParam($conn, $numRows);
|
2017-03-09 23:12:53 +01:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
sqlsrv_close($conn);
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
echo $e->getMessage();
|
|
|
|
|
}
|
|
|
|
|
echo "\nDone\n";
|
2017-03-09 23:12:53 +01:00
|
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
--EXPECT--
|
|
|
|
|
|
2017-05-02 02:01:30 +02:00
|
|
|
|
Test begins...
|
2017-03-09 23:12:53 +01:00
|
|
|
|
Insert integers without PHP type
|
|
|
|
|
1, 2
|
|
|
|
|
11, 12
|
|
|
|
|
21, 22
|
|
|
|
|
31, 32
|
|
|
|
|
41, 42
|
|
|
|
|
Insert integers as SQLSRV_PHPTYPE_INT
|
|
|
|
|
1, 2
|
|
|
|
|
11, 12
|
|
|
|
|
21, 22
|
|
|
|
|
31, 32
|
|
|
|
|
41, 42
|
|
|
|
|
Insert floats with direction specified
|
|
|
|
|
1.0, 2.0
|
|
|
|
|
11.0, 12.0
|
|
|
|
|
21.0, 22.0
|
|
|
|
|
31.0, 32.0
|
|
|
|
|
41.0, 42.0
|
|
|
|
|
Insert floats without direction
|
|
|
|
|
1.0, 2.0
|
|
|
|
|
11.0, 12.0
|
|
|
|
|
21.0, 22.0
|
|
|
|
|
31.0, 32.0
|
|
|
|
|
41.0, 42.0
|
|
|
|
|
|
|
|
|
|
Done
|