Skip to content

Commit

Permalink
Review .net ResultSet wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Jan 3, 2024
1 parent a0a5f44 commit c0c3137
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 15 deletions.
19 changes: 6 additions & 13 deletions net/TdRules/Java.Sql/ResultSet.N.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,35 +102,28 @@ public int GetInt(string field)
object Value=GetCellValue(field);
if (this.LastFieldWasNull)
return 0;
return (int)Value;
return Convert.ToInt32(Value);
}
public int GetInt(int col)
{
object Value = GetCellValue(col);
if (this.LastFieldWasNull)
return 0;
return (int)Value;
return Convert.ToInt32(Value);
}
public long GetLong(int col)
{
object Value = GetCellValue(col);
if (this.LastFieldWasNull)
return 0;
return (long)Value;
}
public short GetShort(string field)
public long GetLong(string field)
{
object Value = GetCellValue(field);
if (this.LastFieldWasNull)
return 0;
return short.Parse(Value.ToString()); //no permite cast short
return Convert.ToInt64(Value);
}
public short GetShort(int col)
public long GetLong(int col)
{
object Value = GetCellValue(col);
if (this.LastFieldWasNull)
return 0;
return short.Parse(Value.ToString());
return Convert.ToInt64(Value);
}
public bool GetBoolean(string field)
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package test4giis.tdrules.store.rdb;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import giis.portable.util.JavaCs;

/**
* Reading basic data types from a ResultSet, check nullability and access by name or column number.
* Intended to test the .net ResultSet wrapper implementation
*/
public class TestSqlserverResultSet extends Base {
protected Connection dbt;
protected ResultSet rs;

protected String sCreate1 = "create table tabrs1 (pk integer primary key not null, "
+ "tint integer, tchar varchar(16), tdate date, tlong bigint)";
protected String sSelect1 = "select pk, tint, tchar, tdate, tlong from tabrs1";

@Before
public void setUp() throws SQLException {
super.setUp();
dbt = getConnection(TEST_DBNAME2);
executeNotThrow(dbt, "drop table tabrs1");
execute(dbt, sCreate1);
}

@After
public void tearDown() throws SQLException {
if (rs != null)
rs.close();
dbt.close();
}

@Test
public void testReadResultSet() throws SQLException {
execute(dbt, "insert into tabrs1 (pk,tint,tchar,tdate,tlong) values(1, 2, null, '2023-01-02', null)");
execute(dbt, "insert into tabrs1 (pk,tint,tchar,tdate,tlong) values(2, null, 'chrchr', null, 55)");

// 1st round: even columns are not null/odd columns are null (except pk),
// read by column number
rs = query(dbt, sSelect1);
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));

int tint = rs.getInt(2);
assertFalse(rs.wasNull());
assertEquals(2, tint);
assertEquals("2", rs.getString(2));

String tchar = rs.getString(3);
assertTrue(rs.wasNull());
assertEquals(null, tchar);

Date tdate;
if (!"sqlite".equals(dbmsname)) { // sqlite does not have native Date
tdate = rs.getDate(4);
assertFalse(rs.wasNull());
assertEquals("2023-01-02", dateString(tdate)); // normalize Date for .net compatibility
assertNotNull(rs.getString(4)); // do not check value because is system/locale dependent
}

long tlong = rs.getLong(5);
assertTrue(rs.wasNull());
assertEquals(0, tlong);

// 2nd round: alternate nullability, read by column name
assertTrue(rs.next());
assertEquals(2, rs.getInt("pk"));

tint = rs.getInt("tint");
assertTrue(rs.wasNull());
assertEquals(0, tint);
assertEquals(null, rs.getString("tint"));

tchar = rs.getString("tchar");
assertFalse(rs.wasNull());
assertEquals("chrchr", tchar);

if (!"sqlite".equals(dbmsname)) {
tdate = rs.getDate("tdate");
assertTrue(rs.wasNull());
if ("java".equals(PLATFORM))
assertEquals(null, tdate);
else // on .net, DateTime is no nullable, returns DateTime.MinValue
assertEquals("0001-01-01", dateString(tdate));
assertEquals(null, rs.getString("tdate"));
}

tlong = rs.getLong("tlong");
assertFalse(rs.wasNull());
assertEquals(55, tlong);
assertEquals("55", rs.getString("tlong"));

// resultset end
assertFalse(rs.next());
}

private String dateString(Date dt) {
return JavaCs.substring(JavaCs.getIsoDate(dt), 0, 10);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected void assertMetadata(String metadata, String fileName) {
FileUtil.fileWrite(TEST_PATH_OUTPUT, fileName, metadata);
String expected = FileUtil.fileRead(TEST_PATH_BENCHMARK, fileName);
VisualAssert va = new VisualAssert().setFramework(Framework.JUNIT4);
va.assertEquals(expected, metadata);
va.assertEquals(expected.replace("\r", ""), metadata.replace("\r", ""));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test4giis.tdrules.store.rdb.sqlite;

import test4giis.tdrules.store.rdb.TestSqlserverResultSet;

public class TestSqliteResultSet extends TestSqlserverResultSet {

public TestSqliteResultSet() {
this.dbmsname = "sqlite";
}

}

0 comments on commit c0c3137

Please sign in to comment.