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.
Comments
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:
-
Using the
%SQL.StatementInterface:- Prepare the class query using the
%PrepareClassQuerymethod. - 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
FileSetquery, allowing you to fetch and process its results 1. - Prepare the class query using the
-
Using the
%Library.ResultSetInterface:- 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
Datamethod.
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: