InterSystems FAQ rubric
Class definitions created by users are stored in class definition classes. They can be used to obtain a list of class definitions from a program.
Note: Class definition classes refer to all classes contained in the %Dictionary package.
In the sample code below, a list of class definitions is obtained using the query Summary of the class %Dictionary.ClassDefinitionQuery.
Class ISJ.Utils
{
ClassMethod ClassInfo()
{
#dim ex As %Exception.AbstractException
try {
set currentNS=$NAMESPACE
while (1) {
read "Please specify namespace: ",x
if x'="" quit
}
set $NAMESPACE=x
write !!
Set statement = ##class(%SQL.Statement).%New()
Do statement.%PrepareClassQuery("%Dictionary.ClassDefinitionQuery","Summary")
set rs = statement.%Execute()
while rs.%Next() {
set name=rs.%Get("Name")
if name["%" continue // Skip the class with % in the name
if $extract(name,1,3)="csp" continue // skip csp.*
if $extract(name,1,3)="csr" continue // skip csr.*
write name,!
}
set $NAMESPACE=currentNS
}
catch ex {
write "Error occured: ",ex.DisplayString(),!
set $NAMESPACE=$get(currentNS)
}
}
}
ObjectScriptObjectScript
An example of execution is as follows.
When you run the class method, you will be asked to specify a namespace, so please specify the namespace name you want to reference.
USER>do ##class(ISJ.Utils).ClassInfo()
Please specify namespace : USER
CSPX.Dashboard.BarChart
CSPX.Dashboard.Chart
CSPX.Dashboard.ChartSeries
CSPX.Dashboard.FuelGauge
<skip>
INFORMATION.SCHEMA.VIEWTABLEUSAGE
ISJ.Utils
Test.JSONTest
Test.Person
Test.REST
Test.VSCode.REST
USER>
ObjectScriptObjectScript
Related article: Getting the list of routines programmatically
Thanks. I need this to help build an export. Is there a way to filter a class namespace? Or is that something I would need to add?
Hi @Phil Burkhalter
Does the "class namespace" mean package? (Sorry if I'm wrong.)
If it's correct, you can get class name (=package.class) using WHERE clause to INFORMATION_SCHEMA.TABLES.
Query example: I can filter "Test" package.
SELECT CLASSNAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='Test'