The %RemoteResultSet class provides a way to use
class queries from a different namespace from within a Caché ObjectScript application. It is
similar in operation to the ResultSet objects provides with the ActiveX and
Java bindings. It uses C++ binding and therefore the C++ binding library must be installed.
; Display the results of the Person class' ByName query to the console.
Set rs=##class(%RemoteResultSet).%New("Sample.Person:ByName")
s rs.UserName="_system"
s rs.Password="sys"
s rs.ConnectionString="localhost[1972]:SAMPLES"
w rs.Prepare()
w rs.Execute("S")
While rs.Next() {
Write rs.GetDataByName("Name"),!
}
Note you can bind a %ResultSet object to a query by either
a) setting the ClassName and QueryName
properties or b) passing a string containing the class name and query name (separated
by a :) to the %New method:
Set result=##class(%ResultSet).%New("Person:ByName")
Dynamic SQL
You can use the %ResultSet class to execute dynamic SQL queries
using the system-provided %DynamicQuery:SQL query. In this case, use the
Prepare() method to supply the text of the query. For example:
Set result=##class(%ResultSet).%New("%DynamicQuery:SQL")
Do result.Prepare("SELECT ID, Name, Salary FROM Employee WHERE Salary > ?")
Do result.Execute(10000)
While result.Next() {
Write result.Data("Name"),result.Data("Salary"),!
}
Dynamic SQL queries are cached in the same query cache as used by Caché ODBC and JDBC.
This means that repeated calls to the same dynamic SQL query do not incur any additional
query preparation and optimization overhead. You can view and manage this cache using the
Caché SQL Manager.
Used to store the data returned from the resultset by column name. This can
be accessed directly for more performance than the Get() and
GetDataByName() as it avoids a method call. For example code
that said:
While result.Next() {
Write result.Get("Name"),result.Get("Salary"),!
}
; Becomes this faster code
While result.Next() {
Write $get(result.Data("Name")),$get(result.Data("Salary")),!
}
Note that as this 'Data' property is multidimensional if there
is no such column name as 'Salary' you will get an UNDEFINED error without
the $get around it. If
there are two columns with the same name in the result set then the second
one will be the one referenced by the 'Data' property. If you need to refer
to both of them use the GetData() and give the position
of the column you want.
Inherited description: Use this method to set the SQL runtime mode for the query to be
executed. Setting the runtime mode for this ResultSet does not
permanently change the $zu(115,5) value. Possible values mode are:
Inherited description: If the current query contains an object Id (based on the CONTAINSID parameter
being set), return the column position of the object Id.
Otherwise return 0.
Inherited description: Returns the value of the column with the name name in the current row of the result set.
If name is not a valid column name, this method returns an empty string.
Look at updating the code to use the Data multidimensional property to
access the fields faster than using this method call.
Inherited description: The name of the extent that this query will return Id values from (based on the EXTENT parameter
being set). Only returns a value if the query contains Id values.
Inherited description: Use this method with dynamic queries to provide the query to be
executed. In the case of the %DynamicQuery:SQL query, p1
is a string containing an SQL query. The query may contain parameters represented
by ? characters within the query. The values of any parameters are
supplied via the Execute() method. For example:
Set result=##class(%ResultSet).%New("%DynamicQuery:SQL")
Do result.Prepare("SELECT Name,City FROM Person WHERE Name %STARTSWITH ? AND City = ?")
Do result.Execute("A","Boston")
While result.Next() {
Write result.Data("Name"),result.Data("City"),!
}
Inherited description: Returns true (1) if the ClassName and QueryName properties of this
%ResultSet object refer to a valid class query.
Otherwise it returns false (0).