Comparing Oracle Objects

Discussion of open issues, suggestions and bugs regarding ODAC (Oracle Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
m.ghilardi
Posts: 41
Joined: Thu 13 Mar 2014 11:14

Comparing Oracle Objects

Post by m.ghilardi » Wed 17 Dec 2014 15:44

Is there a general way to Compare the contents of two TOraObjects?

Here is what I do now

Code: Select all

bool isObjectEqual(TOraObject* obj1, TOraObject* obj2)
{
	bool isEqual = obj1->ObjectType->Name.Compare(obj2->ObjectType->Name) == 0;
	isEqual = isEqual && (obj1->IsNull == obj2->IsNull);
	if (isEqual && !obj1->IsNull)
	{
		if (obj1->ObjectType->Name.CompareIC(L"mytype")==0)
		{
			isEqual = isEqual && (obj1->AttrAsLargeInt[L"attr1"] == obj2->AttrAsLargeInt[L"attr1"]);
			isEqual = isEqual && (obj1->AttrAsString[L"attr2"] == obj2->AttrAsString[L"attr2"]);
		}
		else
		{
			//throw exception obj1->ObjectType->Name not supported
		}
	}
	return isEqual;
}
Basically, for each object type i have to write another "else if" segment, it is a pain!
The Attribute list is not accessible, and there is no AttrAsVariant function, so I have to explicitly list all the attributes with the correct attribute type.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Comparing Oracle Objects

Post by AlexP » Thu 18 Dec 2014 09:12

Hello,

Since objects can have a rather complicated structure, nesting, descend from other objects, etc., then it is quite difficult to create a "universal" object comparison method. Therefore you will have to use different comparison algorithms for different objects.
For access to an attribute by index, you can use AttributeCount methods and the Attributes indexer.

Code: Select all

for i := 0 to OraObject.ObjectType.AttributeCount - 1 do begin
  OraObject.ObjectType.Attributes[i]. .....
....
end;

m.ghilardi
Posts: 41
Joined: Thu 13 Mar 2014 11:14

Re: Comparing Oracle Objects

Post by m.ghilardi » Thu 08 Jan 2015 08:39

Hello AlexP,

I agree, writing such a method would be quite difficult.

ObjectType->Attributes returns a TAttribute*, which is the definition of the Attribute (not the instance I need) so I have to extract the Name from it, the type, and then call the correct method on the OraObject.
So it would be something like this (pseudo code)

Code: Select all

for i:=0 to AttributeCount-1 
  name := obj.ObjectType.Attributes[i].Name;
  type := obj.ObjectType.Attributes[i].DataType;
  
  switch (type)
  case string: obj.AttrAsString(name)...
  case largeint: obj.AttrAsLargeInt(name)
  case object: //recursion?
  case integer://...and so on
What I dream of is instead

Code: Select all

for i:=0 to AttributeCount-1 do begin
  if not(obj1.Attributes[i].isObject) then
    result:= obj1.Attributes[i].Value = obj2.Attributes[i].Value
  else
    result:= isObjectEqual(obj1.Attributes[i].AsObject,obj2.Attributes[i].AsObject);
end;

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Comparing Oracle Objects

Post by AlexP » Mon 12 Jan 2015 09:18

As I wrote you earlier, implementation of such comparison is difficult. Therefore you can use any implementation suiting your purposes.

Post Reply