public override object InternalExecute(Program program, object[] arguments) { StringWriter text = new StringWriter(); XmlTextWriter writer = new XmlTextWriter(text); writer.Formatting = Formatting.Indented; // Find the root element DataParams paramsValue = new DataParams(); paramsValue.Add(DataParam.Create(program.ServerProcess, "ADocumentID", (Guid)arguments[0])); Guid rootElementID = ((IScalar) ((IServerProcess)program.ServerProcess).Evaluate ( "Root_Element_ID from row from (XMLDocument where ID = ADocumentID)", paramsValue ) ).AsGuid; // Write the root element WriteElement(program, writer, rootElementID); writer.Flush(); return(text.ToString()); }
private static void InsertDocument(IServerProcess process, Guid documentID, Guid rootElementID) { DataParams paramsValue = new DataParams(); paramsValue.Add(DataParam.Create(process, "ADocumentID", documentID)); paramsValue.Add(DataParam.Create(process, "AElementID", rootElementID)); process.Execute ( "insert table { row { ADocumentID ID, AElementID Root_Element_ID }, key { } } into .System.Internet.XMLDocument", paramsValue ); }
private static void WriteContent(IServerProcess process, Guid elementID, string content, int childSequence, byte type) { DataParams paramsValue = new DataParams(); paramsValue.Add(DataParam.Create(process, "AElementID", elementID)); paramsValue.Add(DataParam.Create(process, "ASequence", childSequence)); paramsValue.Add(DataParam.Create(process, "AContent", content)); paramsValue.Add(DataParam.Create(process, "AType", type)); process.Execute ( "insert table { row { AElementID Element_ID, ASequence Sequence, AContent Content, AType Type }, key { } } into .System.Internet.XMLContent", paramsValue ); }
public void Analyze() { PrepareForExecute(); string plan; var errors = new ErrorList(); try { using (var statusForm = new StatusForm(Strings.ProcessingQuery)) { DateTime startTime = DateTime.Now; try { var paramsValue = new DataParams(); paramsValue.Add(DataParam.Create(Dataphoria.UtilityProcess, "AQuery", GetTextToExecute())); plan = ((DAE.Runtime.Data.Scalar)Dataphoria.EvaluateQuery("ShowPlan(AQuery)", paramsValue)).AsString; } finally { TimeSpan elapsed = DateTime.Now - startTime; _executionTimeStatus.Text = elapsed.ToString(); } } } catch (Exception exception) { errors.Add(exception); ProcessErrors(errors); SetStatus(Strings.ScriptAnalyzeFailed); return; } SetStatus(Strings.ScriptAnalyzeSuccessful); var analyzer = (Analyzer.Analyzer)Dataphoria.OpenDesigner(Dataphoria.GetDefaultDesigner("pla"), null); analyzer.LoadPlan(plan); }
private void WriteElement(Program program, XmlTextWriter writer, Guid elementID) { // Write the element header DataParams paramsValue = new DataParams(); paramsValue.Add(DataParam.Create(program.ServerProcess, "AElementID", elementID)); using ( IRow element = (IRow)((IServerProcess)program.ServerProcess).Evaluate ( "row from (XMLElement where ID = AElementID)", paramsValue ) ) { string namespaceAlias = (string)element["NamespaceAlias"]; if (namespaceAlias != String.Empty) { namespaceAlias = namespaceAlias + ":"; } writer.WriteStartElement(namespaceAlias + (string)element["Name"]); } // Write any default namespace changes IScalar defaultValue = (IScalar)((IServerProcess)program.ServerProcess).Evaluate ( "URI from row from (XMLDefaultNamespace where Element_ID = AElementID)", paramsValue ); if (defaultValue != null) { writer.WriteAttributeString("xmlns", defaultValue.AsString); } // Write namespace aliases IServerCursor aliases = (IServerCursor)((IServerProcess)program.ServerProcess).OpenCursor ( "XMLNamespaceAlias where Element_ID = AElementID", paramsValue ); try { while (aliases.Next()) { using (IRow row = aliases.Select()) writer.WriteAttributeString("xmlns:" + (string)row["NamespaceAlias"], (string)row["URI"]); } } finally { ((IServerProcess)program.ServerProcess).CloseCursor(aliases); } // Write the attributes IServerCursor attributes = (IServerCursor)((IServerProcess)program.ServerProcess).OpenCursor ( "XMLAttribute where Element_ID = AElementID", paramsValue ); try { while (attributes.Next()) { using (IRow row = attributes.Select()) { string alias = (string)row["NamespaceAlias"]; if (alias != String.Empty) { alias = alias + ":"; } writer.WriteAttributeString(alias + (string)row["Name"], (string)row["Value"]); } } } finally { ((IServerProcess)program.ServerProcess).CloseCursor(attributes); } // Write the child content and elements IServerCursor children = (IServerCursor)((IServerProcess)program.ServerProcess).OpenCursor ( @" (XMLContent where Element_ID = AElementID over { Element_ID, Sequence }) union ( XMLElementParent where Parent_Element_ID = AElementID over { Parent_Element_ID, Sequence } rename { Parent_Element_ID Element_ID } ) left join (XMLContent rename { Element_ID Content_Element_ID, Sequence Content_Sequence }) by Element_ID = Content_Element_ID and Sequence = Content_Sequence left join (XMLElementParent rename { Element_ID Child_Element_ID, Sequence Child_Sequence }) by Element_ID = Parent_Element_ID and Sequence = Child_Sequence order by { Element_ID, Sequence } " , paramsValue ); try { while (children.Next()) { using (IRow row = children.Select()) { if (row.HasValue("Content_Element_ID")) // Content { if ((byte)row["Type"] == 0) { writer.WriteString((string)row["Content"]); } else { writer.WriteCData((string)row["Content"]); } } else // Child element { WriteElement(program, writer, (Guid)row["Child_Element_ID"]); } } } } finally { ((IServerProcess)program.ServerProcess).CloseCursor(children); } // Write the end element writer.WriteEndElement(); }
private static Guid InsertElement(IServerProcess process, Guid documentID, XmlTextReader reader, Guid parentID, int sequence) { Guid elementID = Guid.NewGuid(); DataParams paramsValue = new DataParams(); // Insert the element paramsValue.Add(DataParam.Create(process, "AElementID", elementID)); paramsValue.Add(DataParam.Create(process, "ADocumentID", documentID)); paramsValue.Add(DataParam.Create(process, "ANamespaceAlias", reader.Prefix)); paramsValue.Add(DataParam.Create(process, "AName", reader.LocalName)); process.Execute ( "insert table { row { AElementID ID, ADocumentID Document_ID, ANamespaceAlias NamespaceAlias, " + "AName Name }, key { } } into .System.Internet.XMLElement", paramsValue ); // Attach to parent if (parentID != Guid.Empty) { paramsValue.Clear(); paramsValue.Add(DataParam.Create(process, "AElementID", elementID)); paramsValue.Add(DataParam.Create(process, "AParentElementID", parentID)); paramsValue.Add(DataParam.Create(process, "ASequence", sequence)); process.Execute ( "insert table { row { AElementID Element_ID, AParentElementID Parent_Element_ID, ASequence Sequence }, key { } } into .System.Internet.XMLElementParent", paramsValue ); } // Add attributes while (reader.MoveToNextAttribute()) { paramsValue.Clear(); paramsValue.Add(DataParam.Create(process, "AElementID", elementID)); paramsValue.Add(DataParam.Create(process, "AValue", reader.Value)); if (String.Compare(reader.Name, "xmlns", true) == 0) // Default namespace { process.Execute ( "insert table { row { AElementID Element_ID, AValue URI }, key { } } into .System.Internet.XMLDefaultNamespace", paramsValue ); } else if (String.Compare(reader.Prefix, "xmlns", true) == 0) // Namespace alias { paramsValue.Add(DataParam.Create(process, "ANamespaceAlias", reader.LocalName)); process.Execute ( "insert table { row { AElementID Element_ID, ANamespaceAlias NamespaceAlias, AValue URI }, key { } } into .System.Internet.XMLNamespaceAlias", paramsValue ); } else // regular attribute { paramsValue.Add(DataParam.Create(process, "ANamespaceAlias", reader.Prefix)); paramsValue.Add(DataParam.Create(process, "AName", reader.LocalName)); process.Execute ( "insert table { row { AElementID Element_ID, ANamespaceAlias NamespaceAlias, AName Name, AValue Value }, key { } } into .System.Internet.XMLAttribute", paramsValue ); } } reader.MoveToElement(); if (!reader.IsEmptyElement) { int childSequence = 0; XmlNodeType nodeType; // Add child elements do { reader.Read(); nodeType = reader.NodeType; switch (nodeType) { case XmlNodeType.Text: WriteContent(process, elementID, reader.Value, childSequence++, 0); break; case XmlNodeType.CDATA: WriteContent(process, elementID, reader.Value, childSequence++, 1); break; case XmlNodeType.Element: InsertElement(process, documentID, reader, elementID, childSequence++); break; } } while (nodeType != XmlNodeType.EndElement); } return(elementID); }