Author Topic: GetElementsByQuery - sort/ordering results  (Read 11929 times)

OpenIT Solutions

  • EA User
  • **
  • Posts: 555
  • Karma: +9/-1
    • View Profile
GetElementsByQuery - sort/ordering results
« on: October 30, 2013, 01:20:43 am »
Hi,

I'm using GetElementsByQuery. The query has an order by clause. However the collection returned by GetElementsByQuery is not using the same order.

(its not using TreePos and Pos is not avalible)...Any idea how i can force the ordering of the collection returned by GetElementsByQuery ?

Regards,

Jon.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: GetElementsByQuery - sort/ordering results
« Reply #1 on: October 30, 2013, 01:31:49 am »
If it doesn't sort (bug?!) then you probably need to move the result into a sortable collection and have that sorted externally.

q.

P.S. I tried it with my own SQL search and it did not return anything. Using e.g. Simple works. So how do you get the order clause into it?
« Last Edit: October 30, 2013, 01:39:58 am by qwerty »

OpenIT Solutions

  • EA User
  • **
  • Posts: 555
  • Karma: +9/-1
    • View Profile
Re: GetElementsByQuery - sort/ordering results
« Reply #2 on: October 30, 2013, 02:49:24 am »
Hi,

Your Model Search will need the CLASSGUID and CLASSTYPE columns, ie:

SELECT distinct t_object.ea_guid AS CLASSGUID, t_object.Object_Type AS CLASSTYPE, t_object.name, t_diagramobjects.RectLeft, t_diagramobjects.RectTop FROM t_object, t_diagramobjects, t_connector, t_diagramlinks
WHERE t_diagramobjects.Diagram_ID = '<Search Term>'
AND t_object.Object_ID = t_diagramobjects.Object_ID
AND t_object.Object_Type IN ('Activity')
AND t_connector.Stereotype = 'SequenceFlow'
AND (t_connector.Start_Object_ID = t_object.Object_ID OR t_connector.End_Object_ID = t_object.Object_ID)
AND t_connector.Connector_ID = t_diagramlinks.ConnectorID
AND t_diagramlinks.DiagramID = t_diagramobjects.Diagram_ID
ORDER BY t_diagramobjects.RectLeft ASC, t_diagramobjects.RectTop DESC

I then call Repository.GetElementsByQuery ("My Query Name", <diagram_id>). I'd expect the activities returned to be in the order returned by the SQL query - they are not !

Regards,

Jon.
« Last Edit: October 30, 2013, 02:58:33 am by openit »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: GetElementsByQuery - sort/ordering results
« Reply #3 on: October 30, 2013, 04:01:45 am »
I can confirm that the sort order is wrong. So: you need to sort yourself and to report a bug...

q.

OpenIT Solutions

  • EA User
  • **
  • Posts: 555
  • Karma: +9/-1
    • View Profile
Re: GetElementsByQuery - sort/ordering results
« Reply #4 on: October 30, 2013, 04:41:21 am »
OK - Do you know how i can get the RectLeft and RectTop coordinates from t_diagramobjects via the automation interface, ie i have a Diagram object, i can call Diagram.DiagramObjects, but this returns a collection of Elements....i need the rectleft and recttop in order to sort the collection ???

Any ideas ?

Thanks,

Jon.

OpenIT Solutions

  • EA User
  • **
  • Posts: 555
  • Karma: +9/-1
    • View Profile
Re: GetElementsByQuery - sort/ordering results
« Reply #5 on: October 30, 2013, 10:54:39 pm »
Hi,

One you might want to add into your book qwerty - see response from Sparx re this:

Hello Jon,

Thank you for your enquiry.

Try using Repository.GetElementSet() instead of GetElementsByQuery.
GetElementSet supports loading of a collection of elements based upon a SQL query, and should observe any ORDER BY clause that is applied.

Example (JScript):

      var sql, i;
      var elements as EA.Collection;
      
      sql = "SELECT [...] FROM [...] WHERE [...] ORDER BY [...]";
      elements = Repository.GetElementSet(sql, 2);
      
      Repository.EnsureOutputVisible("Script");
      Session.Output("elements.Count=" + elements.Count);
      for (var i = 0; i < elements.Count; i++)
      {
            Session.Output("elements[" + i + "]=" + elements.GetAt(i).Name);
      }


This worked for me after i realised that Object_ID must be a column returned in the resultset....


Regards,


Jon.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: GetElementsByQuery - sort/ordering results
« Reply #6 on: October 31, 2013, 05:07:34 am »
Thanks for the pointer! I'll definitely add that info.

q.

stewartd

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: GetElementsByQuery - sort/ordering results
« Reply #7 on: April 17, 2014, 02:52:54 pm »
Quote
Hi,

One you might want to add into your book qwerty - see response from Sparx re this:

Hello Jon,

Thank you for your enquiry.

Try using Repository.GetElementSet() instead of GetElementsByQuery.

Hi there,

I flagged this thread last year when I was experimenting with scripts that would run SQL queries against our model. I found a curious result and wonder if it is a bug?

In our project we've been using VBScript as the language of choice. I wrote a simple query (returning CLASSGUID, CLASSTYPE, Object_ID, name from t_object...) and pass that into GetElementSet

Code: [Select]
dim elements as EA.Collection
elements = Repository.GetElementSet(sql,2)

This fails with the error: Argument not optional.

So I then recrafted the script as a JScript one:

Code: [Select]
var elements as EA.Collection
elements = Repository.GetElementSet(sql,2)

This works as expected. Can anyone else confirm?

This is using EA 10, Build 1006.

Regards,

Damien.

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: GetElementsByQuery - sort/ordering results
« Reply #8 on: April 17, 2014, 03:28:15 pm »
GetElementSet returns an Object, so you need to use the Set keyword in VBScript when assigning the return value to a variable.

Code: [Select]
dim elements as EA.Collection
Set elements = Repository.GetElementSet(sql,2)
Also, your SQL should only return the Object_ID column (i.e. ElementID values).  Other columns are not needed or used in this context and may only confuse things.

stewartd

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: GetElementsByQuery - sort/ordering results
« Reply #9 on: June 25, 2014, 06:09:03 pm »
Thanks Aaron B - that was the bit that was missing! I had the elements defined as EA.Collection, but had omitted the set keyword.

Finally, the SQL query ran fine. I could return the ID and other values without any problems (in this case, the name of the element.)

Thanks,

Damien.