return false;
}
try
{
Schema fieldSchema = SchemaHelper.unwindUnionSchema(f); // == f.schema() if f is not a union
Type avroFieldType = fieldSchema.getType();
if (_sDebug)
{
LOG.debug("Checking for type:" + avroFieldType + ", Field:" + f.name() +
", Exp:" + databaseFieldValue + ", Got:" + avroField);
}
switch (avroFieldType)
{
case BOOLEAN:
assertEquals(f.name(),databaseFieldValue,avroField );
break;
case BYTES:
byte[] byteArr = null;
if (databaseFieldValue instanceof Blob)
{
Blob b = (Blob) databaseFieldValue;
byteArr = b.getBytes(1,(int) b.length());
}
else
{
byteArr = (byte[])databaseFieldValue;
}
assertEquals(f.name(), byteArr, avroField);
break;
case DOUBLE:
assertEquals(f.name(), new Double(((Number)databaseFieldValue).doubleValue()), (avroField));
break;
case FLOAT:
assertEquals(f.name(), new Float(((Number)databaseFieldValue).floatValue()), (avroField));
break;
case INT:
assertEquals(f.name(), Integer.valueOf(((Number)databaseFieldValue).intValue()), (avroField));
break;
case LONG:
if(databaseFieldValue instanceof Number)
{
long lvalue = ((Number) databaseFieldValue).longValue();
assertEquals(f.name(),lvalue,((Long)avroField).longValue());
}
else if(databaseFieldValue instanceof Timestamp)
{
long time = ((Timestamp) databaseFieldValue).getTime();
assertEquals(f.name(),time,((Long)avroField).longValue());
}
else if(databaseFieldValue instanceof Date)
{
long time = ((Date) databaseFieldValue).getTime();
assertEquals(f.name(),time,((Long)avroField).longValue());
}
else
{
Class timestampClass = null, dateClass = null;
try
{
timestampClass = OracleJarUtils.loadClass("oracle.sql.TIMESTAMP");
dateClass = OracleJarUtils.loadClass("oracle.sql.DATE");
}
catch (Exception e)
{
String errMsg = "Cannot convert " + databaseFieldValue.getClass() +
" to long. Unable to get Oracle datatypes " + e.getMessage();
LOG.error(errMsg);
throw new EventCreationException(errMsg);
}
if (timestampClass.isInstance(databaseFieldValue))
{
try
{
Object tsc = timestampClass.cast(databaseFieldValue);
Method dateValueMethod = timestampClass.getMethod("dateValue");
Date dateValue = (Date) dateValueMethod.invoke(tsc);
long time = dateValue.getTime();
assertEquals(f.name(),time,((Long)avroField).longValue());
}
catch(Exception ex)
{
String errMsg = "SQLException reading oracle.sql.TIMESTAMP value for field " + f.name();
LOG.error(errMsg);
throw new RuntimeException(errMsg, ex);
}
}
else if (dateClass.isInstance(databaseFieldValue))
{
try
{
Object dsc = dateClass.cast(databaseFieldValue);
Method dateValueMethod = dateClass.getMethod("dateValue");
Date dateValue = (Date) dateValueMethod.invoke(dsc);
long time = dateValue.getTime();
assertEquals(f.name(),time,((Long)avroField).longValue());
}
catch (Exception ex)
{
String errMsg = "SQLException reading oracle.sql.DATE value for field " + f.name();
LOG.error(errMsg);
throw new RuntimeException(errMsg, ex);
}
}
else
{
String errMsg = "Cannot convert " + databaseFieldValue.getClass() + " to long for field " + f.name();
LOG.error(errMsg);
throw new RuntimeException();
}
}
break;
case STRING:
if (databaseFieldValue instanceof Clob)
{
String text = null;
try
{
text = OracleAvroGenericEventFactory.extractClobText((Clob)databaseFieldValue, f.name());
}
catch (EventCreationException ex)
{
LOG.error("compareField error: " + ex.getMessage(), ex);
}
assertEquals(f.name(), text, ((Utf8)avroField).toString());
}
else
{
String text = databaseFieldValue.toString();
assertEquals(f.name(), text, ((Utf8)avroField).toString());
}
break;
case NULL:
assertNull(f.name(), databaseFieldValue);
assertNull(f.name(), avroField);
break;
case ARRAY:
GenericArray<GenericRecord> avroArray = (GenericArray<GenericRecord>)avroField;
Schema elementSchema = fieldSchema.getElementType();
Array array = (Array)databaseFieldValue;
ResultSet arrayResultSet = array.getResultSet();
int i = 0;
while (arrayResultSet.next())
{
// Get the underlying structure from the database. Oracle returns the structure in the
// second column of the array's ResultSet
Struct struct = (Struct) arrayResultSet.getObject(2);
Object[] attributes = struct.getAttributes();
GenericRecord avroElement = avroArray.get(i++);
// Iterate over the fields in the JSON array of fields.
// We can read the structure elements only by position, not by field name, so we
// have to use dbFieldPosition recorded in the schema definition.
for (Field field : elementSchema.getFields())
{
int dbFieldPosition = Integer.valueOf(SchemaHelper.getMetaField(field, "dbFieldPosition"));
Object dbFieldValue = attributes[dbFieldPosition];
Object avroFieldValue = avroElement.get(field.name());
compareField(field, dbFieldValue, avroFieldValue);