Skip to main content

HS.Types.Dynamic

datatype class HS.Types.Dynamic extends %Library.String

ODBC Type: VARCHAR

The Dynamic class represent a collection of attribute/value pairs where the attribute names are not determined at compile time. This class may be used to implement dynamic/virtual properties in a persistent class, provided that the persistent class overrides the %DispatchGetProperty and %DispatchSetProperty methods as follows:

Property Additional As HS.Types.Dynamic;
Method %DispatchGetProperty(pProperty As %String)
		{ Quit ##class(HS.Types.Dynamic).GetSubvalue(..Additional, pProperty) }
Method %DispatchSetProperty(pProperty As %String, pValue As %String)
	{ Set ..Additional = ##class(HS.Types.Dynamic).SetSubvalue(..Additional, pProperty, pValue) }

With the definition above, virtual properties may be invented on the fly and read or written as a property from ObjectScript. Set and Get methods for the virtual property are implemented in the SetSubvalue() and GetSubvalue() class methods, which may be called from SQL via a SQLPROC or extrinsic function. A virtual property may also appear in a Cache SQL "FOR SOME %ELEMENT" predicate, as described below.

For convenience and ease of setting/getting values to external systems, the attribute/value pairs are represented as a flat XML fragment; that is: '<' tag '>' value '', where tag is the attribute name and value is the attribute value. When setting an attribute from SQL it can be convenient to use the XMLFOREST operator, which suppresses NULL terms:

INSERT INTO MyTable SET Additional =
	 XMLFOREST(	:PAPER_ForeignAddress AS "foreignaddress",
			 	:PAPER_Marital_DR AS "maritalstatus", 	
				:PAPER_CityArea_DR AS "cityarea")

The XML must be well-formed, escaped using standard XML conventions, and attribute names (XML tags) must be unique within an XML fragment and must satisfy the naming requirements of a class property.

Properties of type Dynamic may be indexed and also support the Text interface; therefore you can efficiently search for the tag, the value, or both from SQL, and the %SIMILARITY operator may be used to determine how similar two collections of attributes are. You may override the Similarity() and SimilarityIdx() class methods to redefine how the %SIMILARITY operator behaves, if desired.

A collection index may be defined on an Dynamic property to support fast pattern matching. By default the values are hashed so that arbitrarily long values can be conveniently indexed.

The following example illustrates how to create a collection index on a property of this type:
Property Attributes As HS.Types.Dynamic;
Index XMLIndex On Attributes(KEYS) [ Data = Attributes(ELEMENTS) ];

To find all rows where birthorder=2, issue the following query:

SELECT TOP 10 id FROM sample.person
WHERE FOR SOME %ELEMENT(Attributes) (%KEY = 'birthorder' AND %VALUE = 2)

To find all rows where the mother's maiden name sounds like 'Flamel', where a phonetic encoding function named 'soundslike' is available, issue the following query:

SELECT TOP 10 id FROM sample.person
WHERE FOR SOME %ELEMENT(Attributes) (%KEY = 'mothersmaidenname' AND 
		$$soundslike^Pearl.cmp(%VALUE) = $$soundslike^Pearl.cmp('Flamel'))

Method Inventory

Methods

classmethod BuildValueArray(thisXML As %Binary, ByRef valueArray As %Binary) as %Status
classmethod GetSubvalue(thisXML As %String, AttrName As %String)
classmethod SetSubvalue(thisXML As %String, AttrName As %String, AttrValue As %String)

Inherited Members

Inherited Methods

FeedbackOpens in a new tab