2017-03-30 20:59:39 +02:00
--TEST--
Test various Katmai types, like geography, geometry, hierarchy, sparse, etc. and fetch them back as strings
--FILE--
<?php
2017-10-04 23:22:56 +02:00
require_once('MsCommon.inc');
2017-03-30 20:59:39 +02:00
function Katmai_Basic_Types($conn)
{
$tableName = GetTempTableName();
2017-10-04 23:22:56 +02:00
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_time] time, [c2_date] date, [c3_datetimeoffset] datetimeoffset, [c4_geography] geography, [c5_geometry] geometry, [c6_hierarchyid] hierarchyid, [c7_uniqueidentifier] uniqueidentifier ROWGUIDCOL NOT NULL UNIQUE DEFAULT NEWID())");
sqlsrv_free_stmt($stmt);
2017-03-30 20:59:39 +02:00
$query = "INSERT INTO $tableName ([c1_time], [c2_date], [c3_datetimeoffset], [c4_geography], [c5_geometry], [c6_hierarchyid], [c7_uniqueidentifier]) VALUES ((''), ('0001-01-01'), (null), ('POINT(6.60 52.15)'), ('POLYGON((25.68 55.08, 26.03 55.99, 26.13 56.54, 25.68 55.08))'), ('/'), ('b644da01-2a9a-43b9-b98c-27c25a28cc5e'))";
$stmt = sqlsrv_query($conn, $query);
2017-10-04 23:22:56 +02:00
sqlsrv_free_stmt($stmt);
2017-03-30 20:59:39 +02:00
echo "\nShowing results of Katmai basic fields\n";
2017-10-04 23:22:56 +02:00
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName");
$numFields = sqlsrv_num_fields($stmt);
$result = sqlsrv_fetch($stmt);
for ($i = 0; $i < $numFields; $i++) {
if ($i < 3) {
$value = sqlsrv_get_field($stmt, $i, SQLSRV_PHPTYPE_DATETIME);
if ($value == null) {
if ($i != 2) { // only this field is null
2017-04-01 02:33:47 +02:00
echo "$value is unexpected for field " . ($i+1) . "\n";
2017-10-04 23:22:56 +02:00
}
} else {
if ($i == 0) {
2017-04-01 02:33:47 +02:00
$value = date('Y-m-d', date_format($value, "U"));
2017-10-04 23:22:56 +02:00
$today = date('Y-m-d');
if ($value !== $today) {
2017-04-01 02:33:47 +02:00
echo "$value is unexpected for field " . ($i+1) . "\n";
2017-10-04 23:22:56 +02:00
}
} else {
$value = date_format($value, 'Y-m-d');
if ($value !== '0001-01-01') {
2017-04-01 02:33:47 +02:00
echo "$value is unexpected for field " . ($i+1) . "\n";
2017-10-04 23:22:56 +02:00
}
2017-04-01 02:33:47 +02:00
}
}
2017-10-04 23:22:56 +02:00
} else {
$value = sqlsrv_get_field($stmt, $i, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
var_dump($value);
2017-03-30 20:59:39 +02:00
}
2017-10-04 23:22:56 +02:00
}
sqlsrv_free_stmt($stmt);
2017-03-30 20:59:39 +02:00
}
function Katmai_SparseChar($conn)
{
$tableName = GetTempTableName();
2017-10-04 23:22:56 +02:00
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName (c1 int, c2 char(512) SPARSE NULL, c3 varchar(512) SPARSE NULL, c4 varchar(max) SPARSE NULL, c5 nchar(512) SPARSE NULL, c6 nvarchar(512) SPARSE NULL, c7 nvarchar(max) SPARSE NULL)");
sqlsrv_free_stmt($stmt);
2017-03-30 20:59:39 +02:00
$input = "The quick brown fox jumps over the lazy dog";
2017-10-04 23:22:56 +02:00
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1, c2, c5) VALUES(1, 'The quick brown fox jumps over the lazy dog', 'The quick brown fox jumps over the lazy dog')");
sqlsrv_free_stmt($stmt);
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1, c3, c6) VALUES(2, 'The quick brown fox jumps over the lazy dog', 'The quick brown fox jumps over the lazy dog')");
sqlsrv_free_stmt($stmt);
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1, c4, c7) VALUES(3, 'The quick brown fox jumps over the lazy dog', 'The quick brown fox jumps over the lazy dog')");
sqlsrv_free_stmt($stmt);
2017-03-30 20:59:39 +02:00
echo "\nComparing results of Katmai sparse fields\n";
2017-10-04 23:22:56 +02:00
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName");
while ($result = sqlsrv_fetch($stmt)) {
$c1 = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
2017-03-30 20:59:39 +02:00
$fld1 = $c1;
$fld2 = $fld1 + 3;
2017-10-04 23:22:56 +02:00
2017-03-30 20:59:39 +02:00
$value1 = sqlsrv_get_field($stmt, $fld1, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
$value2 = sqlsrv_get_field($stmt, $fld2, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
2017-10-04 23:22:56 +02:00
if ($input !== trim($value1)) {
2017-03-30 20:59:39 +02:00
echo "The value is unexpected!\n";
}
2017-10-04 23:22:56 +02:00
if ($value1 !== $value2) {
2017-03-30 20:59:39 +02:00
echo "The values don't match!\n";
}
}
2017-10-04 23:22:56 +02:00
sqlsrv_free_stmt($stmt);
2017-03-30 20:59:39 +02:00
}
function Katmai_SparseNumeric($conn)
{
$tableName = GetTempTableName();
2017-10-04 23:22:56 +02:00
2017-03-30 20:59:39 +02:00
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName (c1 int, c2 int SPARSE NULL, c3 tinyint SPARSE NULL, c4 smallint SPARSE NULL, c5 bigint SPARSE NULL, c6 bit SPARSE NULL, c7 float SPARSE NULL, c8 real SPARSE NULL, c9 decimal(28,4) SPARSE NULL, c10 numeric(32,4) SPARSE NULL)");
2017-10-04 23:22:56 +02:00
$stmt = sqlsrv_query($conn, "INSERT INTO $tableName (c1, c2, c3, c4, c5, c6, c7, c8, c9, c10) VALUES(1, '1', '1', '1', '1', '1', '1', '1', '1', '1')");
2017-03-30 20:59:39 +02:00
sqlsrv_free_stmt($stmt);
2017-10-04 23:22:56 +02:00
2017-03-30 20:59:39 +02:00
echo "\nShowing results of Katmai sparse numeric fields\n";
2017-10-04 23:22:56 +02:00
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName");
$numFields = sqlsrv_num_fields($stmt);
$result = sqlsrv_fetch($stmt);
for ($i = 1; $i < $numFields; $i++) {
$value = sqlsrv_get_field($stmt, $i, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
2017-03-30 20:59:39 +02:00
var_dump($value);
2017-10-04 23:22:56 +02:00
}
2017-03-30 20:59:39 +02:00
}
//--------------------------------------------------------------------
2017-04-01 02:33:47 +02:00
// RunTest
2017-03-30 20:59:39 +02:00
//
//--------------------------------------------------------------------
2017-04-01 02:33:47 +02:00
function RunTest()
2017-03-30 20:59:39 +02:00
{
2017-10-04 23:22:56 +02:00
startTest("sqlsrv_katmai_special_types");
2017-05-02 02:01:30 +02:00
echo "\nTest begins...\n";
2017-10-04 23:22:56 +02:00
try {
set_time_limit(0);
sqlsrv_configure('WarningsReturnAsErrors', 1);
2017-03-30 20:59:39 +02:00
// Connect
2017-05-02 02:01:30 +02:00
$conn = Connect();
2017-10-04 23:22:56 +02:00
if (!$conn) {
fatalError("Could not connect.\n");
}
2017-03-30 20:59:39 +02:00
Katmai_Basic_Types($conn);
Katmai_SparseChar($conn);
Katmai_SparseNumeric($conn);
2017-10-04 23:22:56 +02:00
sqlsrv_close($conn);
} catch (Exception $e) {
2017-03-30 20:59:39 +02:00
echo $e->getMessage();
}
echo "\nDone\n";
2017-10-04 23:22:56 +02:00
endTest("sqlsrv_katmai_special_types");
2017-03-30 20:59:39 +02:00
}
2017-04-01 02:33:47 +02:00
RunTest();
2017-03-30 20:59:39 +02:00
?>
--EXPECT--
2017-05-02 02:01:30 +02:00
Test begins...
2017-03-30 20:59:39 +02:00
Showing results of Katmai basic fields
string(44) "E6100000010C3333333333134A406666666666661A40"
string(192) "00000000010404000000AE47E17A14AE39400AD7A3703D8A4B4048E17A14AE073A401F85EB51B8FE4B40E17A14AE47213A4085EB51B81E454C40AE47E17A14AE39400AD7A3703D8A4B4001000000020000000001000000FFFFFFFF0000000003"
string(0) ""
string(36) "B644DA01-2A9A-43B9-B98C-27C25A28CC5E"
Comparing results of Katmai sparse fields
Showing results of Katmai sparse numeric fields
string(1) "1"
string(1) "1"
string(1) "1"
string(1) "1"
string(1) "1"
string(3) "1.0"
string(3) "1.0"
string(6) "1.0000"
string(6) "1.0000"
Done
2017-10-04 23:22:56 +02:00
Test "sqlsrv_katmai_special_types" completed successfully.