Question Mikhail (VetsEZ) Akselrod · 2 hr ago

How to obtain query syntax of a ClassQuery prior of execution?

For example in %Library.File Class Documentation,  FiieSet query is listed as: 
"Selects Name As %String, Type As %String, Size As %BigInt, DateCreated As %TimeStamp, DateModified As %TimeStamp, ItemName As %Stringof ..."

How to obtain a "real" syntax?

Thank you.

Product version: IRIS 2022.1
$ZV: HealthShare Unified Care Record 2023.1.0 Build: 1006 Adhoc: 23407 [HealthShare Modules: Core:25.0] -

Comments

DC AI Bot · 2 hr ago Bot

To obtain the actual query syntax or use the defined ClassQuery, such as %Library.File:FileSet, you can use the %SQL.Statement or the %Library.ResultSet interface to prepare and execute the query programmatically. Here are the detailed steps:

  1. Using the %SQL.Statement Interface:

    • Prepare the class query using the %PrepareClassQuery method.
    • Execute the prepared query using %Execute, specifying the parameters needed by the query, such as the directory, file pattern, etc.
    • Iterate over and handle the results using methods like %Next() and %Get(field).

    Example:

    ClassMethod ShowDir(dir As %String = "", wildcard As %String = "", sort As %String = "Name") {
        set stmt = ##class(%SQL.Statement).%New()
        set status = stmt.%PrepareClassQuery("%File", "FileSet")
        if $$$ISERR(status) { write "%Prepare failed:" do $SYSTEM.Status.DisplayError(status) quit }
    
        set rset = stmt.%Execute(dir, wildcard, sort)
        if (rset.%SQLCODE '= 0) { write "%Execute failed:", !, "SQLCODE ", rset.%SQLCODE, ": ", rset.%Message quit }
    
        while rset.%Next() {
            write !, rset.%Get("Name"), " ", rset.%Get("Type"), " ", rset.%Get("Size")
        }
        if (rset.%SQLCODE < 0) { write "%Next failed:", !, "SQLCODE ", rset.%SQLCODE, ": ", rset.%Message quit }
    }
    

    This method prepares and runs the FileSet query, allowing you to fetch and process its results 1.

  2. Using the %Library.ResultSet Interface:

    • It is considered the older interface but is still functional.
    • You prepare and execute the query similarly by specifying the query name.
    • Access the results using the Data method.

    Example:

    set rs = ##class(%Library.ResultSet).%New("%Library.File:FileSet")
    set sc = rs.Execute("c:\\", "*.txt")
    while rs.%Next() { write !, rs.Data("Name") }
    

Both methods enable obtaining and executing the “real” query syntax of the FileSet query, providing flexibility depending on the use case and preferred interface 23.

If you need to call the query directly using a more direct approach, you can use the auto-generated method, such as FileSetFunc, which acts as a simplified wrapper around the query, directly returning the result set 2.


Sources:

Tell us more:
Don't like the style
Not factually correct
Didn't fully follow instructions
Refused when it shouldn't have
Being lazy
More...
Submit
0 0