2017-05-15 23:34:43 +02:00
|
|
|
|
--TEST--
|
|
|
|
|
Test simple insert and update sql_variants using parameters of some different data types
|
|
|
|
|
--DESCRIPTION--
|
|
|
|
|
ORDER BY should work with sql_variants
|
2017-11-29 23:40:47 +01:00
|
|
|
|
--SKIPIF--
|
|
|
|
|
<?php require('skipif_versions_old.inc'); ?>
|
2017-05-15 23:34:43 +02:00
|
|
|
|
--FILE--
|
|
|
|
|
<?php
|
2017-10-04 23:22:56 +02:00
|
|
|
|
require_once('MsCommon.inc');
|
2017-05-15 23:34:43 +02:00
|
|
|
|
|
|
|
|
|
class Country
|
|
|
|
|
{
|
|
|
|
|
public $id;
|
|
|
|
|
public $country;
|
|
|
|
|
public $continent;
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
public function getCountry()
|
2017-05-15 23:34:43 +02:00
|
|
|
|
{
|
|
|
|
|
return $this->country;
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
public function getContinent()
|
2017-05-15 23:34:43 +02:00
|
|
|
|
{
|
|
|
|
|
return $this->continent;
|
2017-10-04 23:22:56 +02:00
|
|
|
|
}
|
2017-05-15 23:34:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
function createVariantTable($conn, $tableName)
|
2017-05-15 23:34:43 +02:00
|
|
|
|
{
|
2017-10-04 23:22:56 +02:00
|
|
|
|
// create a table for testing
|
2017-11-29 23:40:47 +01:00
|
|
|
|
$columns = array(new AE\ColumnMeta('sql_variant', 'id'),
|
|
|
|
|
new AE\ColumnMeta('sql_variant', 'country'),
|
|
|
|
|
new AE\ColumnMeta('sql_variant', 'continent'));
|
|
|
|
|
$stmt = AE\createTable($conn, $tableName, $columns);
|
|
|
|
|
if (!$stmt) {
|
|
|
|
|
fatalError("Failed to create table $tableName\n");
|
|
|
|
|
}
|
2017-05-15 23:34:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
function addCountry($conn, $tableName, $id, $country, $continent)
|
2017-05-15 23:34:43 +02:00
|
|
|
|
{
|
|
|
|
|
$query = "INSERT $tableName ([id], [country], [continent]) VALUES (?, ?, ?)";
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-05-15 23:34:43 +02:00
|
|
|
|
// set parameters
|
|
|
|
|
$params = array($id, $country, $continent);
|
2017-11-29 23:40:47 +01:00
|
|
|
|
$stmt = AE\executeQueryParams($conn, $query, $params);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
if ($stmt) {
|
|
|
|
|
echo "\nAdded $country in $continent with ID $id.";
|
|
|
|
|
} else {
|
|
|
|
|
fatalError("Failed to insert country $country.\n");
|
|
|
|
|
}
|
2017-05-15 23:34:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
function updateID($conn, $tableName, $id, $country, $continent)
|
2017-05-15 23:34:43 +02:00
|
|
|
|
{
|
|
|
|
|
$query = "UPDATE $tableName SET id = ? WHERE country = ? AND continent = ?";
|
2017-10-04 23:22:56 +02:00
|
|
|
|
$param1 = $id;
|
|
|
|
|
$param2 = $country;
|
2017-05-15 23:34:43 +02:00
|
|
|
|
$param3 = $continent;
|
2017-10-04 23:22:56 +02:00
|
|
|
|
$params = array( &$param1, &$param2, &$param3);
|
|
|
|
|
|
|
|
|
|
if ($stmt = sqlsrv_prepare($conn, $query, $params)) {
|
|
|
|
|
if (sqlsrv_execute($stmt)) {
|
2017-05-15 23:34:43 +02:00
|
|
|
|
echo "\nCountry $country now updated with new id $id.";
|
2017-10-04 23:22:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sqlsrv_free_stmt($stmt);
|
|
|
|
|
} else {
|
|
|
|
|
fatalError("Failed to update ID.\n");
|
2017-05-15 23:34:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
function updateCountry($conn, $tableName, $id, $country, $continent)
|
2017-05-15 23:34:43 +02:00
|
|
|
|
{
|
|
|
|
|
$query = "UPDATE $tableName SET country = ? WHERE id = ? AND continent = ?";
|
2017-10-04 23:22:56 +02:00
|
|
|
|
$param1 = $country;
|
|
|
|
|
$param2 = $id;
|
2017-05-15 23:34:43 +02:00
|
|
|
|
$param3 = $continent;
|
2017-10-04 23:22:56 +02:00
|
|
|
|
$params = array( &$param1, &$param2, &$param3);
|
|
|
|
|
|
|
|
|
|
if ($stmt = sqlsrv_prepare($conn, $query, $params)) {
|
|
|
|
|
if (sqlsrv_execute($stmt)) {
|
2017-05-15 23:34:43 +02:00
|
|
|
|
echo "\nThe country in $continent is now $country.";
|
2017-10-04 23:22:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sqlsrv_free_stmt($stmt);
|
|
|
|
|
} else {
|
|
|
|
|
fatalError("Failed to update country.\n");
|
2017-05-15 23:34:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-22 17:01:59 +01:00
|
|
|
|
function fetch($conn, $tableName, $buffered = false)
|
2017-05-15 23:34:43 +02:00
|
|
|
|
{
|
|
|
|
|
$select = "SELECT * FROM $tableName ORDER BY id";
|
2020-01-22 17:01:59 +01:00
|
|
|
|
|
|
|
|
|
if ($buffered) {
|
|
|
|
|
$stmt = sqlsrv_query($conn, $select, array(), array("Scrollable"=>"buffered"));
|
|
|
|
|
} else {
|
|
|
|
|
$stmt = sqlsrv_query($conn, $select);
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
while ($country = sqlsrv_fetch_object($stmt, "Country")) {
|
|
|
|
|
echo "\nID: " . $country->id . " ";
|
2017-05-15 23:34:43 +02:00
|
|
|
|
echo $country->getCountry() . ", ";
|
|
|
|
|
echo $country->getContinent();
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
sqlsrv_free_stmt($stmt);
|
2017-05-15 23:34:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
try {
|
|
|
|
|
setup();
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
// connect
|
|
|
|
|
$conn = AE\connect();
|
2017-05-15 23:34:43 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
// Create a temp table that will be automatically dropped once the connection is closed
|
|
|
|
|
$tableName = 'simple_update_variants';
|
|
|
|
|
createVariantTable($conn, $tableName);
|
2017-05-15 23:34:43 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
// Add three countries
|
|
|
|
|
addCountry($conn, $tableName, 1, 'Canada', 'North America');
|
|
|
|
|
addCountry($conn, $tableName, 3, 'France', 'Europe');
|
|
|
|
|
addCountry($conn, $tableName, 5, 'Australia', 'Australia');
|
2017-05-15 23:34:43 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
// Read data
|
|
|
|
|
fetch($conn, $tableName);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
// Update id
|
|
|
|
|
updateID($conn, $tableName, 4, 'Canada', 'North America');
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
// Read data
|
2020-01-22 17:01:59 +01:00
|
|
|
|
fetch($conn, $tableName, true);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
// Update country
|
|
|
|
|
updateCountry($conn, $tableName, 4, 'Mexico', 'North America');
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
// Read data
|
2020-01-22 17:01:59 +01:00
|
|
|
|
fetch($conn, $tableName, true);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
// Add two more countries
|
|
|
|
|
addCountry($conn, $tableName, 6, 'Brazil', 'South America');
|
|
|
|
|
addCountry($conn, $tableName, 2, 'Egypt', 'Africa');
|
2017-05-15 23:34:43 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
// Read data
|
|
|
|
|
fetch($conn, $tableName);
|
|
|
|
|
|
|
|
|
|
dropTable($conn, $tableName);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-11-29 23:40:47 +01:00
|
|
|
|
sqlsrv_close($conn);
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
echo $e->getMessage();
|
2017-05-15 23:34:43 +02:00
|
|
|
|
}
|
2017-11-29 23:40:47 +01:00
|
|
|
|
echo "\nDone\n";
|
2017-05-15 23:34:43 +02:00
|
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
--EXPECT--
|
|
|
|
|
|
|
|
|
|
Added Canada in North America with ID 1.
|
|
|
|
|
Added France in Europe with ID 3.
|
|
|
|
|
Added Australia in Australia with ID 5.
|
|
|
|
|
ID: 1 Canada, North America
|
|
|
|
|
ID: 3 France, Europe
|
|
|
|
|
ID: 5 Australia, Australia
|
|
|
|
|
Country Canada now updated with new id 4.
|
|
|
|
|
ID: 3 France, Europe
|
|
|
|
|
ID: 4 Canada, North America
|
|
|
|
|
ID: 5 Australia, Australia
|
|
|
|
|
The country in North America is now Mexico.
|
|
|
|
|
ID: 3 France, Europe
|
|
|
|
|
ID: 4 Mexico, North America
|
|
|
|
|
ID: 5 Australia, Australia
|
|
|
|
|
Added Brazil in South America with ID 6.
|
|
|
|
|
Added Egypt in Africa with ID 2.
|
|
|
|
|
ID: 2 Egypt, Africa
|
|
|
|
|
ID: 3 France, Europe
|
|
|
|
|
ID: 4 Mexico, North America
|
|
|
|
|
ID: 5 Australia, Australia
|
|
|
|
|
ID: 6 Brazil, South America
|
|
|
|
|
Done
|