Menu

#492 [ODBC] OdbcDataReader GetInt16, GetInt32 doesn't returns expected exception when read an overbound integer

open
nobody
None
5
2013-04-27
2013-04-27
Jira Trac
No

* Brief:
** The GetInt16/GetInt32 function doesn't return exception when read an overbound integer

* Test Code:
{noformat}

[Test, Description(Test for DdbcDataReader GetInt16 Max value OverBound)]
[Category(Exception)]
[ExpectedException(typeof(OdbcException))]
public void OdbcDataReaderMethod_GetInt16_OverBound_Max()
{
// Open Connection
string strConn = @Driver={CUBRID Driver};db_name=odbc_test;uid=dba;pwd=;server=10.34.64.57;port=33037;fetch_size=100;
OdbcConnection connCubrid = new OdbcConnection(strConn);
connCubrid.Open();

int test_int = Int16.MaxValue + 1; //Int32 32768
string testTable = t_type_int16_overbound;
string strCreateTable = string.Format(CREATE TABLE {0}(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, f_int INT), testTable);
string strDropTable = string.Format(DROP TABLE {0}, testTable);
string strSqlInsert = string.Format(INSERT INTO {0}(f_int) VALUE({1}), testTable, test_int);

ExecuteMultiQueries(connCubrid, new string[] { strDropTable, strCreateTable, strSqlInsert });

string strSqlSelect = string.Format(SELECT * FROM {0} ORDER BY id DESC;, testTable);
OdbcDataReader odbcReader = CreateReader(connCubrid, strSqlSelect);
Int16 result = odbcReader.GetInt16(1);
Console.WriteLine(test_int); // output: 32768
Console.WriteLine(odbcReader.GetInt16(1)); // output: -32768, expected result: OdbcException

connCubrid.Close();
}

[Test, Description(Test for DdbcDataReader GetInt32 Min value Overbound)]
[Category(Exception)]
[ExpectedException(typeof(OdbcException))]
public void OdbcDataReaderMethod_GetInt16_OverBound_Min()
{
// Open Connection
string strConn = @Driver={CUBRID Driver};db_name=odbc_test;uid=dba;pwd=;server=10.34.64.57;port=33037;fetch_size=100;
OdbcConnection connCubrid = new OdbcConnection(strConn);
connCubrid.Open();

int test_int = Int16.MinValue - 1; //Int32 -32769
string testTable = t_type_int16_overbound;
string strCreateTable = string.Format(CREATE TABLE {0}(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, f_int INT), testTable);
string strDropTable = string.Format(DROP TABLE {0}, testTable);
string strSqlInsert = string.Format(INSERT INTO {0}(f_int) VALUE({1}), testTable, test_int);

ExecuteMultiQueries(connCubrid, new string[] { strDropTable, strCreateTable, strSqlInsert });

string strSqlSelect = string.Format(SELECT * FROM {0} ORDER BY id DESC;, testTable);
OdbcDataReader odbcReader = CreateReader(connCubrid, strSqlSelect);
Int16 result = odbcReader.GetInt16(1);
Console.WriteLine(test_int); // output: -32769
Console.WriteLine(odbcReader.GetInt16(1)); // output: 32767, expected result: OdbcException

connCubrid.Close();
}

private OdbcDataReader CreateReader(OdbcConnection conn, string query)
{
OdbcCommand command = new OdbcCommand();
command.Connection = conn;
command.CommandText = query;
OdbcDataReader odbcReader = command.ExecuteReader();
odbcReader.Read();
return odbcReader;
}

private void ExecuteMultiQueries(OdbcConnection conn, string[] multiQueries)
{
OdbcCommand command = new OdbcCommand();
command.Connection = conn;
foreach (string query in multiQueries)
{
try
{
command.CommandText = query;
command.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
command.Dispose();
}

{noformat}

* Result:
**GetInt16 Results
*** Max Overbound (test value 32768): Excepted: OdbcException, Real: -32768
*** Min Overbound (test value -32769): Excepted: OdbcException, Real: 32767

**GetInt32 Results
*** Max Overbound (test value 9223372036854775807): Excepted: OdbcException, Real: -1
*** Min Overbound (test value -9223372036854775808): Excepted: OdbcException, Real: 0

*Description:
It may retrun an Odbc Exception when call GetInt16/GetInt32/GetInt64 with an overrange integer value (similar as other Odbc driver like Access, Sql server)

Discussion