Author Topic: Limit SQL search to single project root  (Read 25051 times)

Stenvang

  • EA User
  • **
  • Posts: 50
  • Karma: +0/-0
    • View Profile
Limit SQL search to single project root
« on: July 12, 2016, 11:48:26 pm »
I'm trying to get all the Elements with object type "Class" within a single project root.

However the following SQL query finds the elements from every project root within my project.

SELECT * FROM t_object WHERE Object_Type = 'Class'

Couldn't find a solution in the documentation.

PeterHeintz

  • EA User
  • **
  • Posts: 976
  • Karma: +58/-18
    • View Profile
Re: Limit SQL search to single project root
« Reply #1 on: July 13, 2016, 12:07:07 am »
Best regards,

Peter Heintz

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Limit SQL search to single project root
« Reply #2 on: July 13, 2016, 01:42:03 am »
Adding
Code: [Select]
and Package_ID in (#Branch#)
Will limit the results to only elements in the selected package and its subpackages.

So if you select the correct root the results will be limited to that root.

Geert

Stenvang

  • EA User
  • **
  • Posts: 50
  • Karma: +0/-0
    • View Profile
Re: Limit SQL search to single project root
« Reply #3 on: July 13, 2016, 06:31:12 pm »
SELECT * FROM t_object
WHERE Object_Type = 'Class'
AND Package_ID in (#Branch#)

I run this from Find In Project/Builder/SQL and get the following error:

"Microsoft OLE DB Provider for ODBC Drivers. [MySQL] [ODBC 5.3(a) Driver][mysqld-5.5.27-log] You have an error in your SQL syntax; check the manual that corresponds to you MySQL server version for the right syntax to use near '' at line 3"


Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Limit SQL search to single project root
« Reply #4 on: July 13, 2016, 06:36:12 pm »
It only works in an actual SQL search.

Geert

Stenvang

  • EA User
  • **
  • Posts: 50
  • Karma: +0/-0
    • View Profile
Re: Limit SQL search to single project root
« Reply #5 on: July 13, 2016, 07:03:43 pm »
So there is no way to do it from the Builder/SQL or Repository.SQLQuery() from the API?

I guess I just have to loop through the packages by code and then perform
SELECT * FROM t_object WHERE Object_Type = 'Class' AND t_object.Package_ID = <ID>
on each package then.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Limit SQL search to single project root
« Reply #6 on: July 13, 2016, 07:21:08 pm »
No, you'll have to do that yourself when using the API.

Here's the code I use:

Code: [Select]
'returns an ArrayList of the given package and all its subpackages recursively
function getPackageTree(package)
dim packageList
set packageList = CreateObject("System.Collections.ArrayList")
addPackagesToList package, packageList
set getPackageTree = packageList
end function

'add the given package and all subPackges to the list (recursively
function addPackagesToList(package, packageList)
dim subPackage as EA.Package
'add the package itself
packageList.Add package
'add subpackages
for each subPackage in package.Packages
addPackagesToList subPackage, packageList
next
end function

'make an id string out of the package ID of the given packages
function makePackageIDString(packages)
dim package as EA.Package
dim idString
idString = ""
dim addComma
addComma = false
for each package in packages
if addComma then
idString = idString & ","
else
addComma = true
end if
idString = idString & package.PackageID
next
'if there are no packages then we return "0"
if packages.Count = 0 then
idString = "0"
end if
'return idString
makePackageIDString = idString
end function

Geert