another set of PDO tests

This commit is contained in:
yitam 2017-05-03 14:33:19 -07:00
parent 586b2b3e4e
commit ca50a21bd8
14 changed files with 2224 additions and 0 deletions

View file

@ -0,0 +1,186 @@
--TEST--
Github 138. Test for Unicode Column Metadata.
--SKIPIF--
<?php require('skipif.inc'); ?>
--FILE--
<?php
class MyStatement extends PDOStatement {
public function BindValues(array &$values, array &$blobs, $placeholder_prefix, $columnInformation, &$max_placeholder = NULL, $blob_suffix = NULL) {
if (empty($max_placeholder)) {
$max_placeholder = 0;
}
foreach ($values as $field_name => &$field_value) {
$placeholder = $placeholder_prefix . $max_placeholder++;
$blob_key = $placeholder . $blob_suffix;
if (isset($columnInformation['blobs'][$field_name])) {
$blobs[$blob_key] = fopen('php://memory', 'a');
fwrite($blobs[$blob_key], $field_value);
rewind($blobs[$blob_key]);
$this->bindParam($placeholder, $blobs[$blob_key], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
}
else {
// Even though not a blob, make sure we retain a copy of these values.
$blobs[$blob_key] = $field_value;
$this->bindParam($placeholder, $blobs[$blob_key], PDO::PARAM_STR);
}
}
}
}
/**
*
* @param string $connection_id
*
* @return PDO
*/
function connection($connection_id) {
include 'MsSetup.inc';
$host = $server;
$database = $databaseName;
$username = $uid;
$password = $pwd;
static $connections = array();
if (!isset($connections[$connection_id])) {
$connection_options['pdo'] = array();
$connection_options['pdo'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$cnn = new PDO("sqlsrv:server=$host;Database=$database", $username, $password, $connection_options['pdo']);
$cnn->setAttribute(PDO::ATTR_STATEMENT_CLASS, [MyStatement::class]);
$connections[$connection_id] = $cnn;
}
return $connections[$connection_id];
}
/**
* Summary of prepare
*
* @param mixed $connection
* @param mixed $query
* @return PDOStatement
*/
function prepare($connection, $query) {
$pdo_options = array();
$pdo_options[PDO::ATTR_EMULATE_PREPARES] = TRUE;
$pdo_options[PDO::SQLSRV_ATTR_DIRECT_QUERY] = TRUE;
$pdo_options[PDO::ATTR_CURSOR] = PDO::CURSOR_SCROLL;
$pdo_options[PDO::SQLSRV_ATTR_CURSOR_SCROLL_TYPE] = PDO::SQLSRV_CURSOR_BUFFERED;
return $connection->prepare($query, $pdo_options);
}
/**
* Summary of execute
*
* @param PDO $connection
* @param string $query
*
* @param PDOStatement;
*/
function execute($connection, $query, array $args = array()) {
$st = prepare($connection, $query);
foreach ($args as $key => $value) {
if (is_numeric($value)) {
$st->bindValue($key, $value, PDO::PARAM_INT);
}
else {
$st->bindValue($key, $value, PDO::PARAM_STR);
}
}
$st->execute();
// Bind column types properly.
$null = array();
$st->columnNames = array();
for ($i = 0; $i < $st->columnCount(); $i++) {
$meta = $st->getColumnMeta($i);
$st->columnNames[]= $meta['name'];
$sqlsrv_type = $meta['sqlsrv:decl_type'];
$parts = explode(' ', $sqlsrv_type);
$type = reset($parts);
switch($type) {
case 'varbinary':
$null[$i] = NULL;
$st->bindColumn($i + 1, $null[$i], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
break;
case 'int':
case 'bit':
case 'smallint':
case 'tinyint':
case 'bigint':
$null[$i] = NULL;
$st->bindColumn($i + 1, $null[$i], PDO::PARAM_INT);
break;
}
}
return $st;
}
//*******************************************************
// TEST BEGIN
//*******************************************************
$connection = connection('default');
// Drop
try {
execute($connection, 'DROP TABLE [mytáble]');
}
catch(Exception $e) {}
$tablescript = <<<EOF
CREATE TABLE [dbo].[mytáble](
[id] [nchar](10) NULL,
[väriable] [nchar](10) NULL,
[tésting] [nchar](10) NULL
) ON [PRIMARY]
EOF;
// Recreate
execute($connection, $tablescript);
$query = <<<EOF
INSERT INTO [mytáble] (id, tésting, väriable) VALUES (:db_insert0, :db_insert1, :db_insert2)
EOF;
$blobs = [];
/** @var MyStatement */
$st = prepare($connection, $query);
$st->bindValue(':db_insert0', 'a', PDO::PARAM_STR);
$st->bindValue(':db_insert1', 'b', PDO::PARAM_STR);
$st->bindValue(':db_insert2', 'c', PDO::PARAM_STR);
$st->execute();
$st = prepare($connection, 'SELECT * FROM [mytáble]');
$st->execute();
while($row = $st->fetchAll()){
$row = reset($row);
echo (isset($row['id']) ? "OK" : "FAIL") , "\n";
echo (isset($row['tésting']) ? "OK" : "FAIL") , "\n";
echo (isset($row['väriable']) ? "OK" : "FAIL") , "\n";
}
for ($i = 0; $i < $st->columnCount(); $i++) {
$meta = $st->getColumnMeta($i);
echo $meta['name'] , "\n";
}
?>
--EXPECT--
OK
OK
OK
id
väriable
tésting

View file

@ -0,0 +1,55 @@
--TEST--
Test PDO::__Construct by passing connection options and attributes.
--SKIPIF--
<?php require('skipif.inc'); ?>
--FILE--
<?php
require_once 'MsSetup.inc';
try
{
$attr = array(
PDO::SQLSRV_ATTR_ENCODING => 3,
PDO::ATTR_CASE => 2,
PDO::ATTR_PREFETCH => false,
PDO::ATTR_TIMEOUT => 35,
PDO::ATTR_ERRMODE => 2,
PDO::ATTR_STRINGIFY_FETCHES => true,
PDO::SQLSRV_ATTR_DIRECT_QUERY => true,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::SQLSRV_ATTR_CLIENT_BUFFER_MAX_KB_SIZE => 5120,
PDO::SQLSRV_ATTR_DIRECT_QUERY => true
);
$dsn = "sqlsrv:Server = $server;" .
"ConnectionPooling = false;" .
"APP = whatever;" .
"LoginTimeout = 1;" .
"ApplicationIntent = ReadOnly;" .
"Database = $databaseName;" .
"Encrypt = false;" .
"Failover_Partner = whatever;" .
"MultipleActiveResultSets = true;" .
"MultiSubnetFailover = NO;" .
"QuotedId = false;" .
"TraceFile = whatever;" .
"TraceOn = true;" .
"TransactionIsolation = " . PDO::SQLSRV_TXN_READ_UNCOMMITTED . ";" .
"TrustServerCertificate = false;" .
"WSID = whatever;"
;
$conn = new PDO( $dsn, $uid, $pwd, $attr);
echo "Test Successful";
}
catch( PDOException $e ) {
var_dump( $e );
exit;
}
?>
--EXPECT--
Test Successful

View file

@ -0,0 +1,460 @@
--TEST--
Test the different type of data for retrieving
--SKIPIF--
<?php require('skipif.inc'); ?>
--FILE--
<?php
require_once 'MsCommon.inc';
function testBigInt($db)
{
$stmt = $db->query("SELECT BigIntCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testBit($db)
{
$stmt = $db->query("SELECT BitCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testInt($db)
{
$stmt = $db->query("SELECT IntCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testSmallInt($db)
{
$stmt = $db->query("SELECT SmallIntCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testTinyInt($db)
{
$stmt = $db->query("SELECT TinyIntCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testDecimal($db)
{
$stmt = $db->query("SELECT DecimalCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testNumeric($db)
{
$stmt = $db->query("SELECT NumCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testMoney($db)
{
$stmt = $db->query("SELECT MoneyCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testSmallMoney($db)
{
$stmt = $db->query("SELECT SmallMoneyCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testFloat($db)
{
$stmt = $db->query("SELECT FloatCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testReal($db)
{
$stmt = $db->query("SELECT RealCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testChar($db)
{
$stmt = $db->query("SELECT CharCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testVarchar($db)
{
$stmt = $db->query("SELECT VarcharCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testText($db)
{
$stmt = $db->query("SELECT TextCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testNText($db)
{
$stmt = $db->query("SELECT NTextCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testNChar($db)
{
$stmt = $db->query("SELECT NCharCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testNVarchar($db)
{
$stmt = $db->query("SELECT NVarcharCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testImage($db)
{
$stmt = $db->query("SELECT ImageCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testBinary($db)
{
$stmt = $db->query("SELECT BinaryCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testVarbinary($db)
{
$stmt = $db->query("SELECT VarbinaryCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testDateTime2($db)
{
$stmt = $db->query("SELECT DateTime2Col FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testDatetimeoffset($db)
{
$stmt = $db->query("SELECT DTOffsetCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testsmalldatetime($db)
{
$stmt = $db->query("SELECT SmallDTCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testDateTime($db)
{
$stmt = $db->query("SELECT DateTimeCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testDate($db)
{
$stmt = $db->query("SELECT DateCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testNVarcharMax($db)
{
$stmt = $db->query("SELECT NVarCharMaxCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testTime($db)
{
$stmt = $db->query("SELECT TimeCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testUniqueidentifier($db)
{
$stmt = $db->query("SELECT Guidcol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testVarbinaryMax($db)
{
$stmt = $db->query("SELECT VarbinaryMaxCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testVarcharMax($db)
{
$stmt = $db->query("SELECT VarcharMaxCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
function testXml($db)
{
$stmt = $db->query("SELECT XmlCol FROM PDO_AllTypes");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
}
try
{
$db = connect();
//$sql = "INSERT INTO PDO_AllTypes(BigIntCol,BitCol,IntCol,) VALUES(" . GetSampleData(4) . ",1,)";
//$numRows = $db->exec($sql);
echo "Test_1 : bigint data type :\n";
testBigInt($db);
echo "Test_2 : bit data type :\n";
testBit($db);
echo "Test_3 : int data type :\n";
testInt($db);
echo "Test_4 : smallint data type:\n";
testSmallInt($db);
echo "Test_5 : tinyint data type:\n";
testTinyInt($db);
echo "Test_6 : decimal data type:\n";
testDecimal($db);
echo "Test_7 : numeric data type:\n";
testNumeric($db);
echo "Test_8 : money data type:\n";
testMoney($db);
echo "Test_9 : smallmoney data type:\n";
testSmallMoney($db);
echo "Test_10 : float data type:\n";
testFloat($db);
echo "Test_11 : real data type:\n";
testReal($db);
echo "Test_12 : char data type:\n";
testChar($db);
echo "Test_13 : varchar data type:\n";
testVarchar($db);
echo "Test_14 : text data type:\n";
testText($db);
echo "Test_15 : nchar data type:\n";
testNChar($db);
echo "Test_16 : nvarchar data type:\n";
testNVarchar($db);
echo "Test_17 : image data type:\n";
testImage($db);
echo "Test_18 : binary data type:\n";
testBinary($db);
echo "Test_19 : varbinary data type:\n";
testVarbinary($db);
echo "Test_20 : smalldatetime data type:\n";
testsmalldatetime($db);
echo "Test_21 : datetime data type:\n";
testDateTime($db);
echo "Test_22 : datetime2 data type:\n";
testsmalldatetime($db);
echo "Test_23 : datetimeoffset data type:\n";
testDatetimeoffset($db);
echo "Test_24 : time data type:\n";
testTime($db);
echo "Test_25 : Uniqueidentifier data type:\n";
testUniqueidentifier($db);
echo "Test_26 : VarbinaryMax data type:\n";
testVarbinaryMax($db);
echo "Test_27 : VarcharMax data type:\n";
testVarcharMax($db);
echo "Test_28 : xml data type:\n";
testXml($db);
echo "Test_29 : ntext data type:\n";
testNText($db);
echo "Test_30 : nvarcharmax data type:\n";
testNVarcharMax($db);
echo "Test_31 : date data type:\n";
testDate($db);
}
catch (PDOException $e)
{
var_dump($e);
}
?>
--EXPECT--
Test_1 : bigint data type :
array(1) {
["BigIntCol"]=>
string(1) "1"
}
Test_2 : bit data type :
array(1) {
["BitCol"]=>
string(1) "0"
}
Test_3 : int data type :
array(1) {
["IntCol"]=>
string(1) "1"
}
Test_4 : smallint data type:
array(1) {
["SmallIntCol"]=>
string(1) "1"
}
Test_5 : tinyint data type:
array(1) {
["TinyIntCol"]=>
string(1) "1"
}
Test_6 : decimal data type:
array(1) {
["DecimalCol"]=>
string(3) "111"
}
Test_7 : numeric data type:
array(1) {
["NumCol"]=>
string(1) "1"
}
Test_8 : money data type:
array(1) {
["MoneyCol"]=>
string(8) "111.1110"
}
Test_9 : smallmoney data type:
array(1) {
["SmallMoneyCol"]=>
string(8) "111.1110"
}
Test_10 : float data type:
array(1) {
["FloatCol"]=>
string(7) "111.111"
}
Test_11 : real data type:
array(1) {
["RealCol"]=>
string(7) "111.111"
}
Test_12 : char data type:
array(1) {
["CharCol"]=>
string(10) "STRINGCOL1"
}
Test_13 : varchar data type:
array(1) {
["VarcharCol"]=>
string(10) "STRINGCOL1"
}
Test_14 : text data type:
array(1) {
["TextCol"]=>
string(420) " 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417."
}
Test_15 : nchar data type:
array(1) {
["NCharCol"]=>
string(10) "STRINGCOL1"
}
Test_16 : nvarchar data type:
array(1) {
["NVarcharCol"]=>
string(10) "STRINGCOL1"
}
Test_17 : image data type:
array(1) {
["ImageCol"]=>
string(1) ""
}
Test_18 : binary data type:
array(1) {
["BinaryCol"]=>
string(5) ""
}
Test_19 : varbinary data type:
array(1) {
["VarbinaryCol"]=>
string(1) ""
}
Test_20 : smalldatetime data type:
array(1) {
["SmallDTCol"]=>
string(19) "2000-11-11 11:11:00"
}
Test_21 : datetime data type:
array(1) {
["DateTimeCol"]=>
string(23) "2000-11-11 11:11:11.110"
}
Test_22 : datetime2 data type:
array(1) {
["SmallDTCol"]=>
string(19) "2000-11-11 11:11:00"
}
Test_23 : datetimeoffset data type:
array(1) {
["DTOffsetCol"]=>
string(34) "2000-11-11 11:11:11.1110000 +00:00"
}
Test_24 : time data type:
array(1) {
["TimeCol"]=>
string(16) "11:11:11.1110000"
}
Test_25 : Uniqueidentifier data type:
array(1) {
["Guidcol"]=>
string(36) "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA"
}
Test_26 : VarbinaryMax data type:
array(1) {
["VarbinaryMaxCol"]=>
string(1) ""
}
Test_27 : VarcharMax data type:
array(1) {
["VarcharMaxCol"]=>
string(420) " 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417."
}
Test_28 : xml data type:
array(1) {
["XmlCol"]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
}
Test_29 : ntext data type:
array(1) {
["NTextCol"]=>
string(420) " 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417."
}
Test_30 : nvarcharmax data type:
array(1) {
["NVarCharMaxCol"]=>
string(420) " 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417."
}
Test_31 : date data type:
array(1) {
["DateCol"]=>
string(10) "2000-11-11"
}

Binary file not shown.

View file

@ -0,0 +1,54 @@
--TEST--
Test the fetchColumn() method.
--SKIPIF--
<?php require('skipif.inc'); ?>
--FILE--
<?php
require_once 'MsCommon.inc';
function fetch_column( $conn )
{
global $table1;
$stmt = $conn->query( "Select * from ". $table1 );
// Fetch the first column from the next row in resultset. (This wud be first row since this is a first call to fetchcol)
$result = $stmt->fetchColumn();
var_dump($result);
// Fetch the second column from the next row. (This would be second row since this is a second call to fetchcol).
$result = $stmt->fetchColumn(1);
var_dump($result);
// Test false is returned when there are no more rows.
$result = $stmt->fetchColumn(1);
var_dump($result);
}
try
{
//$verbose = false;
$db = connect();
create_and_insert_table1( $db );
fetch_column($db);
}
catch( PDOException $e ) {
var_dump( $e );
exit;
}
?>
--EXPECT--
string(1) "1"
string(10) "STRINGCOL2"
bool(false)

Binary file not shown.

View file

@ -0,0 +1,190 @@
--TEST--
Test the fetch() method for different fetch orientations with PDO::ATTR_CURSOR set to scrollable.
--ENV--
PHPT_EXEC=true
--SKIPIF--
<?php require('skipif.inc'); ?>
--FILE--
<?php
require_once 'MsCommon.inc';
function FetchAll($execMode, $fetchMode)
{
require_once 'MsCommon.inc';
require 'MsSetup.inc';
$testName = "PDO Statement - Fetch Scrollable";
StartTest($testName);
$conn1 = connect();
// Prepare test table
$dataCols = "id, val";
CreateTableEx($conn1, $tableName, "id int NOT NULL PRIMARY KEY, val VARCHAR(10)", null);
InsertRowEx($conn1, $tableName, $dataCols, "1, 'A'", null);
InsertRowEx($conn1, $tableName, $dataCols, "2, 'B'", null);
InsertRowEx($conn1, $tableName, $dataCols, "3, 'C'", null);
// Query table and retrieve data
$stmt1 = $conn1->prepare( "SELECT val FROM $tableName", array( PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL ));
$stmt1->execute();
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_LAST );
if( $row[ 'val' ] != "C" ) {
throw new Exception( "Not C" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_PRIOR );
if( $row[ 'val' ] != "B" ) {
throw new Exception( "Not B" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_PRIOR );
if( $row[ 'val' ] != "A" ) {
throw new Exception( "Not A" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_PRIOR );
if( $row[ 'val' ] != false ) {
throw new Exception( "Not false" );
}
$stmt1->execute();
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_LAST );
if( $row[ 'val' ] != "C" ) {
throw new Exception( "Not C" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, -1 );
if( $row[ 'val' ] != "B" ) {
throw new Exception( "Not B" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, -1 );
if( $row[ 'val' ] != "A" ) {
throw new Exception( "Not A" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, -1 );
if( $row[ 'val' ] != false ) {
throw new Exception( "Not false" );
}
$stmt1->execute();
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_LAST );
if( $row[ 'val' ] != "C" ) {
throw new Exception( "Not C" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT );
if( $row[ 'val' ] != false ) {
throw new Exception( "Not false" );
}
$stmt1->execute();
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_LAST );
if( $row[ 'val' ] != "C" ) {
throw new Exception( "Not C" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, 1 );
if( $row[ 'val' ] != false ) {
throw new Exception( "Not false" );
}
$stmt1->execute();
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, 2 );
if( $row[ 'val' ] != "C" ) {
throw new Exception( "Not C" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_PRIOR);
if( $row[ 'val' ] != "B" ) {
throw new Exception( "Not B" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, 1 );
if( $row[ 'val' ] != "C" ) {
throw new Exception( "Not C" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, 2 );
if( $row[ 'val' ] != "C" ) {
throw new Exception( "Not C" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, 0 );
if( $row[ 'val' ] != "A" ) {
throw new Exception( "Not A" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, -1 );
if( $row[ 'val' ] != false ) {
throw new Exception( "Not false" );
}
$stmt1->execute();
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_FIRST );
if( $row[ 'val' ] != "A" ) {
throw new Exception( "Not A" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT);
if( $row[ 'val' ] != "B" ) {
throw new Exception( "Not B" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_PRIOR);
if( $row[ 'val' ] != "A" ) {
throw new Exception( "Not A" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, 1 );
if( $row[ 'val' ] != "B" ) {
throw new Exception( "Not B" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT);
if( $row[ 'val' ] != "C" ) {
throw new Exception( "Not C" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT);
if( $row[ 'val' ] != false ) {
throw new Exception( "Not false" );
}
$stmt1->execute();
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_FIRST );
if( $row[ 'val' ] != "A" ) {
throw new Exception( "Not A" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_PRIOR);
if( $row[ 'val' ] != false ) {
throw new Exception( "Not false" );
}
$stmt1->execute();
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_FIRST );
if( $row[ 'val' ] != "A" ) {
throw new Exception( "Not A" );
}
$row = $stmt1->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, -1);
if( $row[ 'val' ] != false ) {
throw new Exception( "Not false" );
}
// Cleanup
DropTable($conn1, $tableName);
$stmt1 = null;
$conn1 = null;
EndTest($testName);
}
//--------------------------------------------------------------------
// Repro
//
//--------------------------------------------------------------------
function Repro()
{
try
{
FetchAll(false, PDO::FETCH_BOTH);
}
catch (Exception $e)
{
echo $e->getMessage();
}
}
Repro();
?>
--EXPECT--
Test "PDO Statement - Fetch Scrollable" completed successfully.

View file

@ -0,0 +1,299 @@
--TEST--
Test the PDOStatement::fetch() method with different fetch styles.
--SKIPIF--
<?php require('skipif.inc'); ?>
--FILE--
<?php
require_once 'MsCommon.inc';
function fetch_both( $conn )
{
global $table1;
$stmt = $conn->query( "Select * from ". $table1 );
$result = $stmt->fetch(PDO::FETCH_BOTH);
var_dump($result);
$stmt->closeCursor();
}
function fetch_assoc( $conn )
{
global $table1;
$stmt = $conn->query( "Select * from ". $table1 );
$result = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($result);
$stmt->closeCursor();
}
function fetch_lazy( $conn )
{
global $table1;
$stmt = $conn->query( "Select * from " . $table1 );
$result = $stmt->fetch(PDO::FETCH_LAZY);
var_dump($result);
$stmt->closeCursor();
}
function fetch_obj( $conn )
{
global $table1;
$stmt = $conn->query( "Select * from ". $table1 );
$result = $stmt->fetch(PDO::FETCH_OBJ);
var_dump($result);
$stmt->closeCursor();
}
function fetch_num( $conn )
{
global $table1;
$stmt = $conn->query( "Select * from ". $table1 );
$result = $stmt->fetch(PDO::FETCH_NUM);
var_dump($result);
$stmt->closeCursor();
}
function fetch_bound( $conn )
{
global $table1;
$stmt = $conn->query( "Select * from ". $table1 );
$stmt->bindColumn('IntCol', $IntCol);
$stmt->bindColumn('CharCol', $CharCol);
$stmt->bindColumn('NCharCol', $NCharCol);
$stmt->bindColumn('DateTimeCol', $DateTimeCol);
$stmt->bindColumn('VarcharCol', $VarcharCol);
$stmt->bindColumn('NVarCharCol', $NVarCharCol);
$stmt->bindColumn('FloatCol', $FloatCol);
$stmt->bindColumn('XmlCol', $XmlCol);
$result = $stmt->fetch(PDO::FETCH_BOUND);
if (!$result)
{
die("Error in FETCH_BOUND\n");
}
var_dump($IntCol);
var_dump($CharCol);
var_dump($NCharCol);
var_dump($DateTimeCol);
var_dump($VarcharCol);
var_dump($NVarCharCol);
var_dump($FloatCol);
var_dump($XmlCol);
$stmt->closeCursor();
}
function fetch_class( $conn )
{
global $table1;
global $table1_class;
$stmt = $conn->query( "Select * from ". $table1 );
$stmt->setFetchMode(PDO::FETCH_CLASS, $table1_class);
$result = $stmt->fetch(PDO::FETCH_CLASS);
$result->dumpAll();
$stmt->closeCursor();
}
function fetch_into( $conn )
{
global $table1;
global $table1_class;
$stmt = $conn->query( "Select * from ". $table1 );
$obj = new $table1_class;
$stmt->setFetchMode(PDO::FETCH_INTO, $obj);
$result = $stmt->fetch(PDO::FETCH_INTO);
$obj->dumpAll();
$stmt->closeCursor();
}
function fetch_invalid( $conn )
{
global $table1;
$stmt = $conn->query( "Select * from ". $table1 );
try
{
$result = $stmt->fetch(PDO::FETCH_UNKNOWN);
}
catch(PDOException $err)
{
print_r($err);
}
}
try
{
$db = connect();
create_and_insert_table1( $db );
echo "Test_1 : FETCH_BOTH :\n";
fetch_both($db);
echo "Test_2 : FETCH_ASSOC :\n";
fetch_assoc($db);
echo "Test_3 : FETCH_LAZY :\n";
fetch_lazy($db);
echo "Test_4 : FETCH_OBJ :\n";
fetch_obj($db);
echo "Test_5 : FETCH_NUM :\n";
fetch_num($db);
echo "Test_6 : FETCH_BOUND :\n";
fetch_bound($db);
echo "Test_7 : FETCH_CLASS :\n";
fetch_class($db);
echo "Test_8 : FETCH_INTO :\n";
fetch_into($db);
echo "Test_9 : FETCH_INVALID :\n";
fetch_invalid($db);
}
catch( PDOException $e ) {
var_dump( $e );
exit;
}
?>
--EXPECTF--
Test_1 : FETCH_BOTH :
array(16) {
["IntCol"]=>
string(1) "1"
[0]=>
string(1) "1"
["CharCol"]=>
string(10) "STRINGCOL1"
[1]=>
string(10) "STRINGCOL1"
["NCharCol"]=>
string(10) "STRINGCOL1"
[2]=>
string(10) "STRINGCOL1"
["DateTimeCol"]=>
string(23) "2000-11-11 11:11:11.110"
[3]=>
string(23) "2000-11-11 11:11:11.110"
["VarcharCol"]=>
string(10) "STRINGCOL1"
[4]=>
string(10) "STRINGCOL1"
["NVarCharCol"]=>
string(10) "STRINGCOL1"
[5]=>
string(10) "STRINGCOL1"
["FloatCol"]=>
string(7) "111.111"
[6]=>
string(7) "111.111"
["XmlCol"]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
[7]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
}
Test_2 : FETCH_ASSOC :
array(8) {
["IntCol"]=>
string(1) "1"
["CharCol"]=>
string(10) "STRINGCOL1"
["NCharCol"]=>
string(10) "STRINGCOL1"
["DateTimeCol"]=>
string(23) "2000-11-11 11:11:11.110"
["VarcharCol"]=>
string(10) "STRINGCOL1"
["NVarCharCol"]=>
string(10) "STRINGCOL1"
["FloatCol"]=>
string(7) "111.111"
["XmlCol"]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
}
Test_3 : FETCH_LAZY :
object(PDORow)#%x (%x) {
["queryString"]=>
string(25) "Select * from PDO_Types_1"
["IntCol"]=>
string(1) "1"
["CharCol"]=>
string(10) "STRINGCOL1"
["NCharCol"]=>
string(10) "STRINGCOL1"
["DateTimeCol"]=>
string(23) "2000-11-11 11:11:11.110"
["VarcharCol"]=>
string(10) "STRINGCOL1"
["NVarCharCol"]=>
string(10) "STRINGCOL1"
["FloatCol"]=>
string(7) "111.111"
["XmlCol"]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
}
Test_4 : FETCH_OBJ :
object(stdClass)#%x (%x) {
["IntCol"]=>
string(1) "1"
["CharCol"]=>
string(10) "STRINGCOL1"
["NCharCol"]=>
string(10) "STRINGCOL1"
["DateTimeCol"]=>
string(23) "2000-11-11 11:11:11.110"
["VarcharCol"]=>
string(10) "STRINGCOL1"
["NVarCharCol"]=>
string(10) "STRINGCOL1"
["FloatCol"]=>
string(7) "111.111"
["XmlCol"]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
}
Test_5 : FETCH_NUM :
array(8) {
[0]=>
string(1) "1"
[1]=>
string(10) "STRINGCOL1"
[2]=>
string(10) "STRINGCOL1"
[3]=>
string(23) "2000-11-11 11:11:11.110"
[4]=>
string(10) "STRINGCOL1"
[5]=>
string(10) "STRINGCOL1"
[6]=>
string(7) "111.111"
[7]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
}
Test_6 : FETCH_BOUND :
string(1) "1"
string(10) "STRINGCOL1"
string(10) "STRINGCOL1"
string(23) "2000-11-11 11:11:11.110"
string(10) "STRINGCOL1"
string(10) "STRINGCOL1"
string(7) "111.111"
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
Test_7 : FETCH_CLASS :
string(1) "1"
string(10) "STRINGCOL1"
string(10) "STRINGCOL1"
string(23) "2000-11-11 11:11:11.110"
string(10) "STRINGCOL1"
string(10) "STRINGCOL1"
string(7) "111.111"
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
Test_8 : FETCH_INTO :
string(1) "1"
string(10) "STRINGCOL1"
string(10) "STRINGCOL1"
string(23) "2000-11-11 11:11:11.110"
string(10) "STRINGCOL1"
string(10) "STRINGCOL1"
string(7) "111.111"
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
Test_9 : FETCH_INVALID :
Fatal error: Uncaught Error: Undefined class constant 'FETCH_UNKNOWN' in %s:%x
Stack trace:
#0 %s: fetch_invalid(Object(PDO))
#1 {main}
thrown in %s on line %x

View file

@ -0,0 +1,264 @@
--TEST--
PDO Fetch Mode Test with emulate prepare
--DESCRIPTION--
Basic verification for "PDOStatement::setFetchMode()”.
--ENV--
PHPT_EXEC=true
--SKIPIF--
<?php require('skipif.inc'); ?>
--FILE--
<?php
include 'MsCommon.inc';
function FetchMode()
{
include 'MsSetup.inc';
$testName = "PDO Statement - Set Fetch Mode";
StartTest($testName);
$dsn = "sqlsrv:Server=$server ; Database = $databaseName";
$conn1 = new PDO($dsn, $uid, $pwd);
$conn1->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// Prepare test table
CreateTableEx($conn1, $tableName, "ID int NOT NULL PRIMARY KEY, Policy VARCHAR(2), Label VARCHAR(10), Budget MONEY", null);
try {
$res = $conn1->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
if ($res)
{
echo "setAttribute should have failed.\n\n";
}
}
catch (Exception $e)
{
echo $e->getMessage();
}
echo "\n";
try {
$query = "SELECT * FROM [$tableName]";
$stmt = $conn1->query($query);
$stmt->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
}
catch (Exception $e)
{
echo $e->getMessage();
}
echo "\nStart inserting data...\n";
$dataCols = "ID, Policy, Label";
$query = "INSERT INTO [$tableName](ID, Policy, Label, Budget) VALUES (?, ?, ?, ?)";
$stmt = $conn1->prepare($query, array(PDO::ATTR_EMULATE_PREPARES => true));
$stmt = $conn1->prepare($query, array(PDO::ATTR_EMULATE_PREPARES => false));
for ($i = 1; $i <= 2; $i++)
{
$pol = chr(64+$i);
$grp = "Group " . $i;
$budget = $i * 1000 + $i * 15;
$stmt->execute( array( $i, $pol, $grp, $budget ) );
}
$query1 = "INSERT INTO [$tableName](ID, Policy, Label, Budget) VALUES (:col1, :col2, :col3, :col4)";
$stmt = $conn1->prepare($query1, array(PDO::ATTR_EMULATE_PREPARES => true));
for ($i = 3; $i <= 5; $i++)
{
$pol = chr(64+$i);
$grp = "Group " . $i;
$budget = $i * 1000 + $i * 15;
$stmt->execute( array( ':col1' => $i, ':col2' => $pol, ':col3' => $grp, ':col4' => $budget ) );
}
echo "....Done....\n";
echo "Now selecting....\n";
$tsql = "SELECT * FROM [$tableName]";
$stmt1 = $conn1->prepare($tsql, array(PDO::ATTR_EMULATE_PREPARES => false));
$stmt1 = $conn1->prepare($tsql, array(PDO::ATTR_EMULATE_PREPARES => true, PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt1->execute();
var_dump($stmt1->fetch( PDO::FETCH_ASSOC ));
$row = $stmt1->fetch( PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT );
print "$row[1]\n";
$row = $stmt1->fetch( PDO::FETCH_LAZY, PDO::FETCH_ORI_LAST );
print "$row[3]\n";
$row = $stmt1->fetch( PDO::FETCH_BOTH, PDO::FETCH_ORI_PRIOR );
print_r($row);
echo "\nFirst two groups or Budget > 4000....\n";
$tsql = "SELECT * FROM [$tableName] WHERE ID <= :id OR Budget > :budget";
$stmt2 = $conn1->prepare($tsql, array(PDO::ATTR_EMULATE_PREPARES => true));
$budget = 4000;
$id = 2;
$stmt2->bindParam(':id', $id);
$stmt2->bindParam(':budget', $budget);
$stmt2->execute();
while ( $result = $stmt2->fetchObject() ){
print_r($result);
echo "\n";
}
echo "\nSelect Policy = 'A'....\n";
$tsql = "SELECT * FROM [$tableName] WHERE Policy = ?";
$stmt3 = $conn1->prepare($tsql, array(PDO::ATTR_EMULATE_PREPARES => true));
$pol = 'A';
$stmt3->bindValue(1, $pol);
$id = 'C';
$stmt3->execute();
while ( $row = $stmt3->fetch( PDO::FETCH_ASSOC ) ){
print_r($row);
echo "\n";
}
echo "\nSelect id > 2....\n";
$tsql = "SELECT Policy, Label, Budget FROM [$tableName] WHERE ID > 2";
$stmt4 = $conn1->prepare($tsql, array(PDO::ATTR_EMULATE_PREPARES => true));
$stmt4->execute();
$stmt4->bindColumn('Policy', $policy);
$stmt4->bindColumn('Budget', $budget);
while ( $row = $stmt4->fetch( PDO::FETCH_BOUND ) ){
echo "Policy: $policy\tBudget: $budget\n";
}
echo "\nBudget Metadata....\n";
$metadata = $stmt4->getColumnMeta(2);
var_dump($metadata);
// Cleanup
DropTable($conn1, $tableName);
$stmt1 = null;
$stmt2 = null;
$stmt3 = null;
$stmt4 = null;
$conn1 = null;
EndTest($testName);
}
class Test
{
function __construct($name = 'N/A')
{
echo __METHOD__ . "($name)\n";
}
}
//--------------------------------------------------------------------
// Repro
//
//--------------------------------------------------------------------
function Repro()
{
try
{
FetchMode();
}
catch (Exception $e)
{
echo $e->getMessage();
}
}
Repro();
?>
--EXPECT--
SQLSTATE[IMSSP]: The given attribute is only supported on the PDOStatement object.
SQLSTATE[IMSSP]: An invalid attribute was designated on the PDOStatement object.
Start inserting data...
....Done....
Now selecting....
array(4) {
["ID"]=>
string(1) "1"
["Policy"]=>
string(1) "A"
["Label"]=>
string(7) "Group 1"
["Budget"]=>
string(9) "1015.0000"
}
B
5075.0000
Array
(
[ID] => 4
[0] => 4
[Policy] => D
[1] => D
[Label] => Group 4
[2] => Group 4
[Budget] => 4060.0000
[3] => 4060.0000
)
First two groups or Budget > 4000....
stdClass Object
(
[ID] => 1
[Policy] => A
[Label] => Group 1
[Budget] => 1015.0000
)
stdClass Object
(
[ID] => 2
[Policy] => B
[Label] => Group 2
[Budget] => 2030.0000
)
stdClass Object
(
[ID] => 4
[Policy] => D
[Label] => Group 4
[Budget] => 4060.0000
)
stdClass Object
(
[ID] => 5
[Policy] => E
[Label] => Group 5
[Budget] => 5075.0000
)
Select Policy = 'A'....
Array
(
[ID] => 1
[Policy] => A
[Label] => Group 1
[Budget] => 1015.0000
)
Select id > 2....
Policy: C Budget: 3045.0000
Policy: D Budget: 4060.0000
Policy: E Budget: 5075.0000
Budget Metadata....
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(5) "money"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(6) "Budget"
["len"]=>
int(19)
["precision"]=>
int(4)
}
Test "PDO Statement - Set Fetch Mode" completed successfully.

View file

@ -0,0 +1,222 @@
--TEST--
Test the PDOStatement::getColumnMeta() method (Note: there could be an issue about using a non-existent column index --- doesn't give any error/output/warning).
--SKIPIF--
<?php require('skipif.inc'); ?>
--FILE--
<?php
require_once 'MsCommon.inc';
function fetch_both( $conn )
{
global $table1;
$stmt = $conn->query( "Select * from ". $table1 );
// 1
$meta = $stmt->getColumnMeta( 0 );
var_dump($meta);
// 2
$meta = $stmt->getColumnMeta( 1 );
var_dump($meta);
// 3
$meta = $stmt->getColumnMeta( 2 );
var_dump($meta);
// 4
$meta = $stmt->getColumnMeta( 3 );
var_dump($meta);
// 5
$meta = $stmt->getColumnMeta( 4 );
var_dump($meta);
// 6
$meta = $stmt->getColumnMeta( 5 );
var_dump($meta);
// 7
$meta = $stmt->getColumnMeta( 6 );
var_dump($meta);
// 8
$meta = $stmt->getColumnMeta( 7 );
var_dump($meta);
// Test invalid arguments, set error mode to silent to reduce the amount of error messages generated
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
// Test negative column number, ignore the error messages
$meta = $stmt->getColumnMeta( -1 );
var_dump($meta);
// Test non-existent column number
//$meta = $stmt->getColumnMeta( 10 );
//var_dump($meta);
}
try
{
$db = connect();
create_and_insert_table1( $db );
fetch_both($db);
}
catch( PDOException $e ) {
var_dump( $e );
exit;
}
?>
--EXPECTF--
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(3) "int"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(6) "IntCol"
["len"]=>
int(10)
["precision"]=>
int(0)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(4) "char"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(7) "CharCol"
["len"]=>
int(10)
["precision"]=>
int(0)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(5) "nchar"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(8) "NCharCol"
["len"]=>
int(10)
["precision"]=>
int(0)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(8) "datetime"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(11) "DateTimeCol"
["len"]=>
int(23)
["precision"]=>
int(3)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(7) "varchar"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(10) "VarcharCol"
["len"]=>
int(50)
["precision"]=>
int(0)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(8) "nvarchar"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(11) "NVarCharCol"
["len"]=>
int(50)
["precision"]=>
int(0)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(5) "float"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(8) "FloatCol"
["len"]=>
int(53)
["precision"]=>
int(0)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(3) "xml"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(6) "XmlCol"
["len"]=>
int(0)
["precision"]=>
int(0)
}
Warning: PDOStatement::getColumnMeta(): SQLSTATE[42P10]: Invalid column reference: column number must be non-negative in %s on line %x
bool(false)

View file

@ -0,0 +1,270 @@
--TEST--
Test the PDOStatement::getColumnMeta() method for Unicode column names (Note: there could be an issue about using a non-existent column index --- doesn't give any error/output/warning).
--SKIPIF--
<?php require('skipif.inc'); ?>
--FILE--
<?php
require_once 'MsCommon.inc';
function fetch_both( $conn )
{
global $table1;
$stmt = $conn->query( "Select * from ". $table1 );
// 1
$meta = $stmt->getColumnMeta( 0 );
var_dump($meta);
// 2
$meta = $stmt->getColumnMeta( 1 );
var_dump($meta);
// 3
$meta = $stmt->getColumnMeta( 2 );
var_dump($meta);
// 4
$meta = $stmt->getColumnMeta( 3 );
var_dump($meta);
// 5
$meta = $stmt->getColumnMeta( 4 );
var_dump($meta);
// 6
$meta = $stmt->getColumnMeta( 5 );
var_dump($meta);
// 7
$meta = $stmt->getColumnMeta( 6 );
var_dump($meta);
// 8
$meta = $stmt->getColumnMeta( 7 );
var_dump($meta);
// Test invalid arguments, set error mode to silent to reduce the amount of error messages generated
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
// Test negative column number, ignore the error messages
$meta = $stmt->getColumnMeta( -1 );
var_dump($meta);
// Test non-existent column number
//$meta = $stmt->getColumnMeta( 10 );
//var_dump($meta);
}
function create_and_insert_table_unicode( $conn )
{
global $string_col, $date_col, $large_string_col, $xml_col, $binary_col, $int_col, $decimal_col, $guid_col, $null_col, $comma, $closing_brace, $table1;
try
{
$create_query =
"IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'" . $table1 . "') AND type in (N'U'))
DROP TABLE " . $table1 .
" CREATE TABLE [dbo].[" . $table1 . "](
[此是後話] [int] NULL,
[Κοντάוְאַתָּה第] [char](10) NULL,
[NΚοντάוְאַתָּה第] [nchar](10) NULL,
[ნომინავიiałopioБун] [datetime] NULL,
[VarcharCol] [varchar](50) NULL,
[NVarΚοντάוְאַתָּה第] [nvarchar](50) NULL,
[FloatCol] [float] NULL,
[XmlCol] [xml] NULL
) ON [PRIMARY]
";
$conn->query( $create_query );
for ($i = 0 ; $i <= 1; ++ $i)
{
$insert_query =
"INSERT INTO PDO_Types_1 VALUES (".
$int_col[$i] . $comma .
$string_col[$i] . $comma .
$string_col[$i] . $comma .
"Convert(datetime, ". $date_col[$i] . ")" . $comma .
$string_col[$i] . $comma .
$string_col[$i] . $comma .
$decimal_col[$i] . $comma .
$xml_col[$i] .
")";
$conn->query ( $insert_query );
}
}
catch(Exception $e)
{
var_dump( $e);
exit;
}
}
try
{
$db = connect();
create_and_insert_table_unicode( $db );
fetch_both($db);
}
catch( PDOException $e ) {
var_dump( $e );
exit;
}
?>
--EXPECTF--
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(3) "int"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(12) "此是後話"
["len"]=>
int(10)
["precision"]=>
int(0)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(4) "char"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(29) "Κοντάוְאַתָּה第"
["len"]=>
int(10)
["precision"]=>
int(0)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(5) "nchar"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(30) "NΚοντάוְאַתָּה第"
["len"]=>
int(10)
["precision"]=>
int(0)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(8) "datetime"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(38) "ნომინავიiałopioБун"
["len"]=>
int(23)
["precision"]=>
int(3)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(7) "varchar"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(10) "VarcharCol"
["len"]=>
int(50)
["precision"]=>
int(0)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(8) "nvarchar"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(33) "NVarΚοντάוְאַתָּה第"
["len"]=>
int(50)
["precision"]=>
int(0)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(5) "float"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(8) "FloatCol"
["len"]=>
int(53)
["precision"]=>
int(0)
}
array(8) {
["flags"]=>
int(0)
["sqlsrv:decl_type"]=>
string(3) "xml"
["native_type"]=>
string(6) "string"
["table"]=>
string(0) ""
["pdo_type"]=>
int(2)
["name"]=>
string(6) "XmlCol"
["len"]=>
int(0)
["precision"]=>
int(0)
}
Warning: PDOStatement::getColumnMeta(): SQLSTATE[42P10]: Invalid column reference: column number must be non-negative in %s on line %x
bool(false)

Binary file not shown.

View file

@ -0,0 +1,67 @@
--TEST--
Test PDOStatement::rowCount by adding, deleting or change
--SKIPIF--
<?php require('skipif.inc'); ?>
--FILE--
<?php
require_once 'MsCommon.inc';
function createTmpTable()
{
$db = connect();
$sql = "CREATE TABLE tmp_table( val int)";
$numRows = $db->exec($sql);
if($numRows === false)
{
die("Create table failed");
}
}
function insertTmpTable()
{
$db = connect();
$count = $db->prepare("INSERT INTO tmp_table (val) VALUES (123)");
$count->execute();
$no = $count->rowCount();
print_r("Number of row after insertion: " . $no . "\n");
}
function updateRecord()
{
$db = connect();
$count = $db->prepare("UPDATE tmp_table set val=111");
$count->execute();
$no=$count->rowCount();
print_r("Number of row after update: " . $no . "\n");
}
function deleteRecord()
{
$db = connect();
$del = $db->prepare("DELETE FROM tmp_table");
$del->execute();
$count = $del->rowCount();
print_r("Number of rows been deleted: " . $count . "\n");
}
try{
$db = connect();
createTmpTable();
insertTmpTable();
updateRecord();
insertTmpTable();
deleteRecord();
$db->exec("DROP TABLE tmp_table");
}
catch(PDOException $e)
{
var_dump($e);
}
?>
--EXPECT--
Number of row after insertion: 1
Number of row after update: 1
Number of row after insertion: 1
Number of rows been deleted: 2

View file

@ -0,0 +1,157 @@
--TEST--
Test setFetchMode method.
--SKIPIF--
<?php require 'skipif.inc'; ?>
--FILE--
<?php
require_once 'MsCommon.inc';
try{
$db = connect();
$stmt = $db->query("SELECT * FROM " . $table1 );
echo "Set Fetch Mode for PDO::FETCH_ASSOC \n";
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetch();
var_dump($result);
$stmt = $db->query("SELECT * FROM " . $table1 );
echo "Set Fetch Mode for PDO::FETCH_NUM \n";
$stmt->setFetchMode(PDO::FETCH_NUM);
$result = $stmt->fetch();
var_dump($result);
$stmt = $db->query("SELECT * FROM " . $table1 );
echo "Set Fetch Mode for PDO::FETCH_BOTH \n";
$stmt->setFetchMode(PDO::FETCH_BOTH);
$result = $stmt->fetch();
var_dump($result);
$stmt = $db->query("SELECT * FROM " . $table1 );
echo "Set Fetch Mode for PDO::FETCH_LAZY \n";
$stmt->setFetchMode(PDO::FETCH_LAZY);
$result = $stmt->fetch();
var_dump($result);
$stmt = $db->query("SELECT * FROM " . $table1 );
echo "Set Fetch Mode for PDO::FETCH_OBJ \n";
$stmt->setFetchMode(PDO::FETCH_OBJ);
$result = $stmt->fetch();
var_dump($result);
}
catch ( PDOException $e)
{
var_dump($e);
}
?>
--EXPECT--
Set Fetch Mode for PDO::FETCH_ASSOC
array(8) {
["IntCol"]=>
string(1) "1"
["CharCol"]=>
string(10) "STRINGCOL1"
["NCharCol"]=>
string(10) "STRINGCOL1"
["DateTimeCol"]=>
string(23) "2000-11-11 11:11:11.110"
["VarcharCol"]=>
string(10) "STRINGCOL1"
["NVarCharCol"]=>
string(10) "STRINGCOL1"
["FloatCol"]=>
string(7) "111.111"
["XmlCol"]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
}
Set Fetch Mode for PDO::FETCH_NUM
array(8) {
[0]=>
string(1) "1"
[1]=>
string(10) "STRINGCOL1"
[2]=>
string(10) "STRINGCOL1"
[3]=>
string(23) "2000-11-11 11:11:11.110"
[4]=>
string(10) "STRINGCOL1"
[5]=>
string(10) "STRINGCOL1"
[6]=>
string(7) "111.111"
[7]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
}
Set Fetch Mode for PDO::FETCH_BOTH
array(16) {
["IntCol"]=>
string(1) "1"
[0]=>
string(1) "1"
["CharCol"]=>
string(10) "STRINGCOL1"
[1]=>
string(10) "STRINGCOL1"
["NCharCol"]=>
string(10) "STRINGCOL1"
[2]=>
string(10) "STRINGCOL1"
["DateTimeCol"]=>
string(23) "2000-11-11 11:11:11.110"
[3]=>
string(23) "2000-11-11 11:11:11.110"
["VarcharCol"]=>
string(10) "STRINGCOL1"
[4]=>
string(10) "STRINGCOL1"
["NVarCharCol"]=>
string(10) "STRINGCOL1"
[5]=>
string(10) "STRINGCOL1"
["FloatCol"]=>
string(7) "111.111"
[6]=>
string(7) "111.111"
["XmlCol"]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
[7]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
}
Set Fetch Mode for PDO::FETCH_LAZY
object(PDORow)#3 (9) {
["queryString"]=>
string(25) "SELECT * FROM PDO_Types_1"
["IntCol"]=>
string(1) "1"
["CharCol"]=>
string(10) "STRINGCOL1"
["NCharCol"]=>
string(10) "STRINGCOL1"
["DateTimeCol"]=>
string(23) "2000-11-11 11:11:11.110"
["VarcharCol"]=>
string(10) "STRINGCOL1"
["NVarCharCol"]=>
string(10) "STRINGCOL1"
["FloatCol"]=>
string(7) "111.111"
["XmlCol"]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
}
Set Fetch Mode for PDO::FETCH_OBJ
object(stdClass)#5 (8) {
["IntCol"]=>
string(1) "1"
["CharCol"]=>
string(10) "STRINGCOL1"
["NCharCol"]=>
string(10) "STRINGCOL1"
["DateTimeCol"]=>
string(23) "2000-11-11 11:11:11.110"
["VarcharCol"]=>
string(10) "STRINGCOL1"
["NVarCharCol"]=>
string(10) "STRINGCOL1"
["FloatCol"]=>
string(7) "111.111"
["XmlCol"]=>
string(431) "<xml> 1 This is a really large string used to test certain large data types like xml data type. The length of this string is greater than 256 to correctly test a large data type. This is currently used by atleast varchar type and by xml type. The fetch tests are the primary consumer of this string to validate that fetch on large types work fine. The length of this string as counted in terms of number of characters is 417.</xml>"
}