Skip to content

Instantly share code, notes, and snippets.

@daimor
Created April 20, 2014 06:52
Show Gist options
  • Save daimor/11107056 to your computer and use it in GitHub Desktop.
Save daimor/11107056 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25">
<Class name="%CDEV.JSON.Base">
<Abstract>1</Abstract>
<Super>%Library.RegisteredObject</Super>
<TimeCreated>63290,83008.228357</TimeCreated>
<Method name="%WriteJSON">
</Method>
<Method name="%WriteJSONString">
<ClassMethod>1</ClassMethod>
<FormalSpec>x:%String</FormalSpec>
<Implementation><![CDATA[
try {
w $zcvt(x, "O", "JSON")
} catch {
w $replace($replace($zcvt(x, "O", "JS"), "\/", "/"), "\'", "'")
}
]]></Implementation>
</Method>
</Class>
<Class name="%CDEV.JSON.Boolean">
<Super>Base</Super>
<TimeCreated>63290,83008.67636</TimeCreated>
<Property name="%data">
<Type>%String</Type>
<Internal>1</Internal>
</Property>
<Method name="%OnNew">
<FormalSpec>data</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
s %data = data
q $$$OK
]]></Implementation>
</Method>
<Method name="%WriteJSON">
<Implementation><![CDATA[ w $s(%data: "true", 1: "false")
]]></Implementation>
</Method>
</Class>
<Class name="%CDEV.JSON.List">
<Super>Base</Super>
<TimeCreated>63290,83008.964801</TimeCreated>
<Property name="%data">
<Type>%ZEN.Datatype.string</Type>
<Internal>1</Internal>
<MultiDimensional>1</MultiDimensional>
<Parameter name="XMLPROJECTION" value="NONE"/>
</Property>
<Method name="%WriteJSON">
<Implementation><![CDATA[
w "["
f i=0:1:..Count()-1
{
s x = ..Get(i)
i $isobject(x)
{
d x.%WriteJSON()
} else {
w """"
d ..%WriteJSONString(x)
w """"
}
w:..Count()>(i+1) ","
}
w "]"
]]></Implementation>
</Method>
<Method name="Create">
<Internal>1</Internal>
<ClassMethod>1</ClassMethod>
<FormalSpec>data...</FormalSpec>
<ReturnType>List</ReturnType>
<Implementation><![CDATA[
s x = ##class(List).%New()
d x.OnCreate(data...)
q x
]]></Implementation>
</Method>
<Method name="OnCreate">
<Internal>1</Internal>
<FormalSpec>data...</FormalSpec>
<Implementation><![CDATA[ m ..%data = data
]]></Implementation>
</Method>
<Method name="Get">
<FormalSpec>index:%Integer</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
s trueStart = $o(..%data(""))
s trueEnd = $o(..%data(""), -1)
s trueIndex = $s(index >= 0: trueStart + index, 1: trueEnd + (index+1))
q ..%data(trueIndex)
]]></Implementation>
</Method>
<Method name="Slice">
<FormalSpec>start="",end=""</FormalSpec>
<ReturnType>List</ReturnType>
<Implementation><![CDATA[
s trueStart = $o(..%data(""))
s trueEnd = $o(..%data(""), -1)
s startIndex = $s(start = "": trueStart, start >= 0: trueStart + start, 1: trueEnd + start + 1)
s endIndex = $s(end = "": trueEnd, end >= 0: trueStart + end - 1, 1: trueEnd + end)
f i=startIndex:1:endIndex
{
s x($i(x)) = ..%data(i)
}
q ##class(List).Create(x...)
]]></Implementation>
</Method>
<Method name="Append">
<FormalSpec>value:%String</FormalSpec>
<Implementation><![CDATA[ d ..Add(value, -1)
]]></Implementation>
</Method>
<Method name="Prepend">
<FormalSpec>value:%String</FormalSpec>
<Implementation><![CDATA[ d ..Add(value, 1)
]]></Implementation>
</Method>
<Method name="PopBack">
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[ q ..Pop(-1)
]]></Implementation>
</Method>
<Method name="PopFront">
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[ q ..Pop(1)
]]></Implementation>
</Method>
<Method name="Count">
<ReturnType>%Integer</ReturnType>
<Implementation><![CDATA[ q ..%data
]]></Implementation>
</Method>
<Method name="Add">
<Internal>1</Internal>
<FormalSpec>value:%String,order</FormalSpec>
<Implementation><![CDATA[
s index = $o(..%data(""), order)
s:index="" index = order
s ..%data(index-order) = value
s ..%data = $i(..%data)
]]></Implementation>
</Method>
<Method name="Pop">
<Internal>1</Internal>
<FormalSpec>order</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
s lastIndex = $o(..%data(""), order)
s x = ..%data(lastIndex)
k ..%data(lastIndex)
s ..%data = $i(..%data, -1)
q x
]]></Implementation>
</Method>
</Class>
<Class name="%CDEV.JSON.Null">
<Super>Base</Super>
<TimeCreated>63290,83009.219996</TimeCreated>
<Method name="%WriteJSON">
<Implementation><![CDATA[ w "null"
]]></Implementation>
</Method>
</Class>
<Class name="%CDEV.JSON.Number">
<Super>Base</Super>
<TimeCreated>63290,83009.427167</TimeCreated>
<Property name="%data">
<Type>%String</Type>
<Internal>1</Internal>
</Property>
<Method name="%OnNew">
<FormalSpec>data</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
s %data = data
q $$$OK
]]></Implementation>
</Method>
<Method name="%WriteJSON">
<Implementation><![CDATA[ w %data
]]></Implementation>
</Method>
</Class>
<Class name="%CDEV.JSON.Object">
<Super>Base</Super>
<TimeCreated>63290,83009.635359</TimeCreated>
<Property name="%data">
<Type>%String</Type>
<Internal>1</Internal>
<MultiDimensional>1</MultiDimensional>
</Property>
<Method name="%WriteJSON">
<Implementation><![CDATA[
w "{"
s k = $o(..%data(""), 1, v)
f
{
q:k=""
w """"
d ..%WriteJSONString(k)
w """:"
if $isobject(v)
{
d v.%WriteJSON()
} else {
w """"
d ..%WriteJSONString(v)
w """"
}
s k = $o(..%data(k), 1, v)
q:k=""
w ","
}
w "}"
]]></Implementation>
</Method>
<Method name="Count">
<ReturnType>%Integer</ReturnType>
<Implementation><![CDATA[ q ..%data
]]></Implementation>
</Method>
<Method name="%DispatchGetProperty">
<Final>1</Final>
<Internal>1</Internal>
<FormalSpec>name:%String</FormalSpec>
<Implementation><![CDATA[ q $G(..%data(name))
]]></Implementation>
</Method>
<Method name="%DispatchSetProperty">
<Final>1</Final>
<Internal>1</Internal>
<FormalSpec>name:%String,value:%String</FormalSpec>
<Implementation><![CDATA[
s:'$data(..%data(name)) ..%data = $i(..%data)
s ..%data(name) = value
]]></Implementation>
</Method>
</Class>
<Class name="%CDEV.JSON.Stream">
<Super>Base</Super>
<TimeCreated>63290,83009.845137</TimeCreated>
<Property name="%data">
<Type>%Stream.Object</Type>
<Internal>1</Internal>
</Property>
<Method name="%OnNew">
<FormalSpec>data:%Stream.Object</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
s ..%data = ##class(%Stream.TmpCharacter).%New()
s sc = ..%data.CopyFrom(data)
q sc
]]></Implementation>
</Method>
<Method name="%WriteJSON">
<Implementation><![CDATA[
s len = 32000
w """"
f
{
d ..%WriteJSONString(..%data.Read(.len, .sc))
q:len=-1
}
w """"
]]></Implementation>
</Method>
</Class>
<Class name="%CDEV.Server">
<IncludeCode>%CDEV.JSON.Includes</IncludeCode>
<Super>%CSP.REST</Super>
<TimeCreated>63290,83432.401149</TimeCreated>
<UDLText name="T">
<Content><![CDATA[
// Used to handle Namespace Switching. If a ClassMethod that ends in an N and does not exist is called, it will switch the NameSpace
]]></Content>
</UDLText>
<UDLText name="T">
<Content><![CDATA[
// to the first argument, and call the classmethod with the same arguments, but without the N
]]></Content>
</UDLText>
<Method name="%DispatchClassMethod">
<ClassMethod>1</ClassMethod>
<FormalSpec>Class:%String,Method:%String,Args...</FormalSpec>
<Implementation><![CDATA[
if $extract(Method, *) = "N"
{
s actualMethod = $extract(Method, 1, *-1)
if $g(Args) {
s namespace = Args(1)
n $namespace
s $namespace = namespace
}
s %response.ContentType = "application/json"
s %input = ..GetRequest()
s response = $classmethod(Class, actualMethod, Args...)
d response.%WriteJSON()
q $$$OK
} else {
q "Class Method Not Found"
}
]]></Implementation>
</Method>
<XData name="UrlMap">
<XMLNamespace>http://www.intersystems.com/urlmap</XMLNamespace>
<Data><![CDATA[
<Routes>
<Route Url="/" Method="GET" Call="ListOptionsN" />
<Route Url="/namespaces" Method="GET" Call="GetNamespaceListN" />
<Route Url="/namespaces/:namespace" Method="GET" Call="GetNamespaceN" />
<Route Url="/namespaces/:namespace/files" Method="GET" Call="GetFileListN" />
<Route Url="/namespaces/:namespace/files" Method="PUT" Call="PutNewFileN" />
<Route Url="/namespaces/:namespace/files/:filename" Method="GET" Call="GetFileN" />
<Route Url="/namespaces/:namespace/files/:filename" Method="PUT" Call="PutFileN" />
<Route Url="/namespaces/:namespace/files/:filename" Method="POST" Call="FileActionN" />
<Route Url="/namespaces/:namespace/files/:filename/generated" Method="GET" Call="GetFileGeneratedN" />
<Route Url="/namespaces/:namespace/files/:filename/xml" Method="GET" Call="GetXMLN" />
<Route Url="/namespaces/:namespace/files/:filename/xml" Method="PUT" Call="PutXMLN" />
<Route Url="/namespaces/:namespace/xml" Method="PUT" Call="PutNewXMLN" />
<Route Url="/namespaces/:namespace/globals" Method="GET" Call="GetGlobalListN" />
<Route Url="/namespaces/:namespace/globals/:globalname" Method="GET" Call="GetGlobalN" />
<Route Url="/namespaces/:namespace/queries" Method="GET" Call="GetQueryListN" />
<Route Url="/namespaces/:namespace/queries" Method="PUT" Call="PutNewQueryN" />
<Route Url="/namespaces/:namespace/queries/:query" Method="GET" Call="GetQueryN" />
<Route Url="/namespaces/:namespace/queries/:query" Method="POST" Call="QueryActionN" />
<Route Url="/namespaces/:namespace/queries/:query/plan" Method="GET" Call="GetQueryPlanN" />
<Route Url="/namespaces/:namespace/queries/:query/cached" Method="GET" Call="GetQueryCachedN" />
<Route Url="/namespaces/:namespace/code" Method="GET" Call="GetCodeListN" />
<Route Url="/namespaces/:namespace/code" Method="PUT" Call="PutNewCodeN" />
<Route Url="/namespaces/:namespace/code/:code" Method="GET" Call="GetCodeN" />
<Route Url="/namespaces/:namespace/code/:code" Method="POST" Call="CodeActionN" />
</Routes>
]]></Data>
</XData>
<Method name="ListOptions">
<ClassMethod>1</ClassMethod>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
s result = $$$jsobj
s result.namespaces = ..CreatePath("namespaces")
s result.version = "v0.0.3"
q result
]]></Implementation>
</Method>
<Method name="GetNamespaceList">
<ClassMethod>1</ClassMethod>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
s rs = ..DoSQL("%SYS.Namespace::List")
s results = $$$jslist
while rs.%Next()
{
if '$d(%request.Data("name", 1)) || (rs.%GetData(1) = $zcvt(%request.Data("name", 1), "U"))
{
s obj = ..NamespaceObject($zcvt(rs.%GetData(1), "L"))
d results.Append(obj)
}
}
q results
]]></Implementation>
</Method>
<Method name="GetFileList">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
s results = $$$jslist
s sql = "SELECT Name||'.cls' FROM %Dictionary.ClassDefinition"
s rs = ..DoSQL(sql)
while rs.%Next() {
s obj = $$$jsobj
s obj.name = rs.%GetData(1)
s obj.id = ..CreatePath("namespaces", namespace, "files", obj.name)
d results.Append(obj)
}
s sql = "%Library.Routine::RoutineList"
s rs = ..DoSQL(sql)
while rs.%Next() {
s obj = $$$jsobj
s obj.name = ..FileName(rs.%GetData(1)) _ "." _ ..FileExtension(rs.%GetData(1))
s obj.id = ..CreatePath("namespaces", namespace, "files", obj.name)
d results.Append(obj)
}
q results
]]></Implementation>
</Method>
<Method name="GetNamespace">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
s result = ..NamespaceObject(namespace)
q result
]]></Implementation>
</Method>
<Method name="NamespaceObject">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String</FormalSpec>
<ReturnType>%CDEV.JSON.Object</ReturnType>
<Implementation><![CDATA[
s obj = $$$jsobj
s obj.id = ..CreatePath("namespaces", namespace)
s obj.name = $zcvt(namespace, "U")
s obj.files = ..CreatePath("namespaces", namespace, "files")
s obj.globals = ..CreatePath("namespaces", namespace, "globals")
s obj.xml = ..CreatePath("namespaces", namespace, "xml")
s obj.queries = ..CreatePath("namespaces", namespace, "queries")
q obj
]]></Implementation>
</Method>
<Method name="FileObject">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,filename:%String</FormalSpec>
<ReturnType>%CDEV.JSON.Object</ReturnType>
<Implementation><![CDATA[
s obj = $$$jsobj
if ..FileExtension(filename) = "cls"
{
s systemName = ..FileName(filename)
if ##class(%Dictionary.ClassDefinition).%ExistsId(systemName)
{
d ##class(%Compiler.UDL.TextServices).GetTextAsStream(,..FileName(filename),.classStream)
s obj.content = $$$jsstream(classStream)
} else {
q ""
}
} else {
s systemName = filename
i ##class(%Routine).Exists(filename)
{
s routine = ##class(%Routine).%OpenId(filename)
s obj.content = $$$jsstream(routine)
} else {
q ""
}
}
s obj.id = ..CreatePath("namespaces", namespace, "files", filename)
s obj.generatedfiles = obj.id _ "/generated"
s obj.name = filename
s obj.xml = ..CreatePath("namespaces", namespace, "files", filename, "xml")
s obj.url = ..GetURLForClass(systemName)
q obj
]]></Implementation>
</Method>
<Method name="GetFile">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,filename:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
s result = ..FileObject(namespace, filename)
q result
]]></Implementation>
</Method>
<Method name="PutFile">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,filename:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
if ..FileExtension(filename) = "cls"
{
s systemName = ..FileName(filename)
i $isobject(%input.content)
{
s sc = ##class(%Compiler.UDL.TextServices).SetTextFromStream(,systemName,%input.content.Read())
} else {
s sc = ##class(%Compiler.UDL.TextServices).SetTextFromString(,systemName,%input.content)
}
} else {
s trueName = ..FileName(filename) _ "." _ $zcvt(..FileExtension(filename), "U")
if ##class(%Routine).Exists(trueName)
{
s routine = ##class(%Routine).%OpenId(trueName)
d routine.Clear()
} else {
s routine = ##class(%Routine).%New(trueName)
}
i $isobject(%input.content)
{
d routine.CopyFrom(%input.content)
} else {
d routine.Write(%input.content)
}
s sc = routine.%Save()
}
s result = $$$jsobj
i $$$ISERR(sc)
{
s result.success = $$$jsfalse
s result.errors = ..ErrorsList(sc)
} else {
s result.success = $$$jstrue
s result.name = filename
s result.file = ..FileObject(namespace, filename)
}
q result
]]></Implementation>
</Method>
<Method name="PutNewFile">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[ q ..PutFile(namespace, %input.name)
]]></Implementation>
</Method>
<Method name="FileAction">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,filename:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
s classname = ..FileName(filename)
s result = $$$jsobj
if %input.action = "compile"
{
d $system.OBJ.Compile(classname, %input.spec_"-d", .errorlog)
i errorlog {
s result.success = $$$jsfalse
s result.errors = $$$jslist
f i=1:1:errorlog
{
d result.errors.Append(errorlog(i))
}
} else {
s result.success = $$$jstrue
}
}
s result.file = ..FileObject(namespace, filename)
q result
]]></Implementation>
</Method>
<Method name="GetXML">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,filename:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
s exportname = ..FileName(filename) _ "." _ $zcvt(..FileExtension(filename), "U")
s sc = $system.OBJ.ExportToStream(exportname, .stream, "-d /diffexport")
s result = $$$jsobj
s result.id = ..CreatePath("namespaces", namespace, "files", filename, "xml")
s result.content = $$$jsstream(stream)
q result
]]></Implementation>
</Method>
<Method name="PutXML">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,filename:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
if '$isobject(%input.content) {
s stream = ##class(%Stream.TmpCharacter).%New()
d stream.Write(%input.content)
} else {
s stream = %input.content
}
s sc = $system.OBJ.LoadStream(stream, "-c-d",,.loadedlist)
s result = $$$jsobj
if $$$ISERR(sc)
{
s result.success = $$$jsfalse
s result.errors = ..ErrorsList(sc)
} else {
s result.success = $$$jstrue
s name = $o(loadedlist(""))
s displayname = ..FileName(name) _ "." _ $zcvt(..FileExtension(name),"L")
s result.file = ..FileObject(namespace, displayname)
}
q result
]]></Implementation>
</Method>
<Method name="PutNewXML">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
//This is fine, because it doesn't make use of filename, yet
q ..PutXML(namespace, "")
]]></Implementation>
</Method>
<Method name="GetFileGenerated">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,filename:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
s type = $zcvt(..FileExtension(filename),"U")
s file = ..FileName(filename)
s results = $$$jslist
s genFileList = ##class(%RoutineMgr).GetOther(file,type)
i $l(genFileList)
{
f i=1:1:$l(genFileList, ",")
{
s obj = $$$jsobj
s name = $p(genFileList, ",", i)
s obj.name = ..FileName(name) _ "." _ $zcvt(..FileExtension(name),"L")
s obj.id = ..CreatePath("namespaces",namespace,"files",obj.name)
d results.Append(obj)
}
}
q results
]]></Implementation>
</Method>
<Method name="GlobalObject">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,global:%String</FormalSpec>
<ReturnType>%CDEV.JSON.Object</ReturnType>
<Implementation><![CDATA[
s obj = $$$jsobj
s obj.id = ..CreatePath("namespaces", namespace, "globals", global)
s jsrep = $$$jsobj
s node = "^"_global_"("""")"
f {
s node = $query(@node, 1, target)
q:node=""
s nodename = $replace($replace($e($p(node, "(", 2,9999), 1, *-1), ",""", ","), """,", ",")
s $property(jsrep, nodename) = target
}
s obj.content = jsrep
s obj.name = global
q obj
]]></Implementation>
</Method>
<Method name="GetGlobalList">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String</FormalSpec>
<Implementation><![CDATA[
s results = $$$jslist
s global = ""
f
{
s global = $o(^$g(global))
q:global=""
s obj = $$$jsobj
s obj.name = $e(global,2,999)
s obj.id = ..CreatePath("namespaces", namespace, "globals", obj.name)
d results.Append(obj)
}
q results
]]></Implementation>
</Method>
<Method name="GetGlobal">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,global:%String</FormalSpec>
<Implementation><![CDATA[ q ..GlobalObject(namespace, global)
]]></Implementation>
</Method>
<Method name="QueryObject">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,id:%Integer</FormalSpec>
<ReturnType>%CDEV.JSON.Object</ReturnType>
<Implementation><![CDATA[
s obj = $$$jsobj
s obj.id = ..CreatePath("namespaces", namespace, "queries", id)
s obj.content = ^CacheTempCDEVQuery(id, "text")
s obj.plan = ..CreatePath("namespaces", namespace, "queries", id, "plan")
s obj.cached = ..CreatePath("namespaces", namespace, "queries", id, "cached")
q obj
]]></Implementation>
</Method>
<Method name="GetQueryList">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String</FormalSpec>
<Implementation><![CDATA[
s results = $$$jslist
s id = ""
if $data(^CacheTempCDEVQuery)
{
f
{
s id = $o(^CacheTempCDEVQuery(id))
q:id=""
d results.Append(..QueryObject(namespace, id))
}
}
q results
]]></Implementation>
</Method>
<Method name="GetQuery">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,query:%Integer</FormalSpec>
<Implementation><![CDATA[
s result = ..QueryObject(namespace, query)
q result
]]></Implementation>
</Method>
<Method name="PutNewQuery">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String</FormalSpec>
<Implementation><![CDATA[
s result = $$$jsobj
if $length(%input.content)
{
s id = $increment(^CacheTempCDEVQuery)
s ^CacheTempCDEVQuery(id, "text") = %input.content
s result.success = $$$jstrue
s result.query = ..QueryObject(namespace, id)
} else {
s result.success = $$$jsfalse
s result.errors = "No query supplied"
}
q result
]]></Implementation>
</Method>
<Method name="QueryAction">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,query:%Integer</FormalSpec>
<Implementation><![CDATA[
s queryObject = ..QueryObject(namespace, query)
s result = $$$jsobj
i %input.action = "execute"
{
s st = ##class(%SQL.Statement).%New()
s sc = st.%Prepare(queryObject.content)
if $$$ISERR(sc)
{
s result.success = $$$jsfalse
s result.errors = ..ErrorsList(sc)
} else {
d st.%GetImplementationDetails(.cachedQueryName)
s ^CacheTempCDEVQuery(query, "cachedQueryName") = cachedQueryName
s result.success = $$$jstrue
s result.resultset = $$$jsobj
s rs = st.%Execute()
s mt = rs.%GetMetadata()
//This could probably be optimized significantly!
for i=1:1:mt.columnCount
{
s $property(result.resultset, mt.columns.GetAt(i).colName) = $$$jslist
}
while rs.%Next()
{
for i=1:1:mt.columnCount
{
d $method($property(result.resultset, mt.columns.GetAt(i).colName), "Append", rs.%GetData(i))
}
}
}
}
q result
]]></Implementation>
</Method>
<Method name="CodeObject">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,id:%Integer</FormalSpec>
<ReturnType>%CDEV.JSON.Object</ReturnType>
<Implementation><![CDATA[
s obj = $$$jsobj
s obj.id = ..CreatePath("namespaces", namespace, "code", id)
s obj.content = ^CacheTempCDEVCode(id, "text")
q obj
]]></Implementation>
</Method>
<Method name="GetCodeList">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String</FormalSpec>
<Implementation><![CDATA[
s results = $$$jslist
s id = ""
if $data(^CacheTempCDEVQuery)
{
f
{
s id = $o(^CacheTempCDEVCode(i))
d results.Append(..QueryObject(namespace, id))
}
}
q results
]]></Implementation>
</Method>
<Method name="GetCode">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,code:%Integer</FormalSpec>
<Implementation><![CDATA[
s result = ..CodeObject(namespace, code)
q result
]]></Implementation>
</Method>
<Method name="PutNewCode">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String</FormalSpec>
<Implementation><![CDATA[
s result = $$$jsobj
if $length(%input.content)
{
s id = $increment(^CacheTempCDEVCode)
s ^CacheTempCDEVCode(id, "text") = %input.content
s result.success = $$$jstrue
s result.code = ..CodeObject(namespace, id)
} else {
s result.success = $$$jsfalse
s result.errors = "No code supplied"
}
q result
]]></Implementation>
</Method>
<Method name="CodeAction">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,code:%Integer</FormalSpec>
<Implementation><![CDATA[
s queryObject = ..CodeObject(namespace, code)
i %input.action = "execute"
{
s result = $$$jsobj
try {
s resultStream = ..CaptureIO(queryObject.content)
s result.success = $$$jstrue
s result.results = $$$jsstream(resultStream)
} catch {
s result.success = $$$jsfalse
s result.errors = ..ErrorsList($ze)
}
}
q result
]]></Implementation>
</Method>
<Method name="QueryPlanOperationObject">
<ClassMethod>1</ClassMethod>
<FormalSpec>text:%String</FormalSpec>
<ReturnType>%CDEV.JSON.Object</ReturnType>
<Implementation><![CDATA[
s %queryArray = 1
s %queryArray(1) = text
s xStatement = "do ShowPlan^%apiSQL(.%queryArray, 0, 0, 1)"
s planStream = ..CaptureIO(xStatement)
s sc = ##class(%XML.TextReader).ParseStream(planStream, .reader)
s obj = $$$jsobj
if $$$ISERR(sc)
{
s obj.success = $$$jsfalse
s obj.errors = $$$jslist
d planStream.Rewind()
d obj.errors.Append(planStream.Read())
} else {
s obj.success = $$$jstrue
s textStream = ##class(%Stream.TmpCharacter).%New()
s elementStack = $$$jslist
while reader.Read()
{
if reader.NodeType = "element"
{
d elementStack.Append(reader.Name)
} elseif reader.NodeType = "endelement"
{
d elementStack.PopBack()
}
elseif reader.NodeType = "chars"
{
if elementStack.Get(-1) = "plan"
{
d textStream.Write(reader.Value)
}
}
}
s obj.content = $$$jsstream(textStream)
}
q obj
]]></Implementation>
</Method>
<Method name="GetQueryPlan">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,query:%Integer</FormalSpec>
<Implementation><![CDATA[
s obj = ..QueryObject(namespace, query)
s result = ..QueryPlanOperationObject(obj.content)
q result
]]></Implementation>
</Method>
<Method name="GetQueryCached">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String,query:%Integer</FormalSpec>
<Implementation><![CDATA[
s filename = ^CacheTempCDEVQuery(query, "cachedQueryName")
s result = ..FileObject(namespace, filename _ ".1.int")
q result
]]></Implementation>
</Method>
<Method name="RunCode">
<ClassMethod>1</ClassMethod>
<FormalSpec>namespace:%String</FormalSpec>
<Implementation><![CDATA[
i %input.action = "execute"
{
}
]]></Implementation>
</Method>
<Method name="FileExtension">
<ClassMethod>1</ClassMethod>
<FormalSpec>filename:%String</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[ q $zcvt($p(filename, ".", *), "L")
]]></Implementation>
</Method>
<Method name="FileName">
<ClassMethod>1</ClassMethod>
<FormalSpec>filename:%String</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[ q $p(filename, ".", 1, *-1)
]]></Implementation>
</Method>
<Method name="CreatePath">
<ClassMethod>1</ClassMethod>
<FormalSpec>components...</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
s path = $e(%request.Application, 1, *-1)
s i = ""
f
{
s i = $o(components(i), 1, component)
q:i=""
s path = path _ "/" _ $zcvt(component,"O","URL")
}
q path
]]></Implementation>
</Method>
<Method name="GetRequest">
<ClassMethod>1</ClassMethod>
<ReturnType>%ZEN.proxyObject</ReturnType>
<Implementation><![CDATA[
s request = ""
if %request.Content
{
d %request.Content.Rewind()
d ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(%request.Content,,.request)
}
q request
]]></Implementation>
</Method>
<Method name="DoSQL">
<ClassMethod>1</ClassMethod>
<FormalSpec>sql:%String,args...</FormalSpec>
<ReturnType>%SQL.StatementResult</ReturnType>
<Implementation><![CDATA[
s st = ##class(%SQL.Statement).%New()
if $find(sql,"::") {
d st.%PrepareClassQuery($p(sql,"::",1), $p(sql,"::",2))
} else {
d st.%Prepare(sql)
}
s x = st.%Execute(args...)
q x
]]></Implementation>
</Method>
<Method name="GetURLForClass">
<ClassMethod>1</ClassMethod>
<FormalSpec>classname:%String</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
s classNamespace = $namespace
n $namespace
s $namespace = "%SYS"
s rs = ..DoSQL("SELECT %ID FROM Security.Applications WHERE NameSpace=?", classNamespace)
s cspapp = rs.%GetData(1)
q cspapp _ classname _ ".cls"
]]></Implementation>
</Method>
<Method name="ErrorsList">
<ClassMethod>1</ClassMethod>
<FormalSpec>sc:%Status</FormalSpec>
<ReturnType>%CDEV.JSON.List</ReturnType>
<Implementation><![CDATA[
s list = $$$jslist
d DecomposeStatus^%apiOBJ(sc, .err, "-d")
for i=1:1:err
{
d list.Append(err(i))
}
q list
]]></Implementation>
</Method>
<Method name="CaptureIO">
<ClassMethod>1</ClassMethod>
<FormalSpec>textString:%String</FormalSpec>
<ProcedureBlock>0</ProcedureBlock>
<ReturnType>%Stream.TmpCharacter</ReturnType>
<Implementation><![CDATA[
//I don't know why this is required but it is.
w " "
set myStream = ##class(%Stream.TmpCharacter).%New()
use $io::("^"_$ZNAME)
do ##class(%Device).ReDirectIO(1)
xecute textString
do ##class(%Device).ReDirectIO(0)
quit myStream
rchr(c) quit
rstr(sz,to) quit
wchr(s) do output($char(s)) quit
wff() do output($char(12)) quit
wnl() do output($char(13,10)) quit
wstr(s) do output(s) quit
wtab(s) do output($char(9)) quit
output(s) do myStream.Write(s) quit
]]></Implementation>
</Method>
</Class>
</Export>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment