Ejemplo n.º 1
0
        public override object InternalExecute(Program program, object[] arguments)
        {
            var tableVar           = APIUtilities.ResolveTableVarName(program, (string)arguments[0]);
            var identifyingKey     = APIUtilities.FindIdentifyingKey(program, tableVar);
            var keyValues          = APIUtilities.GetKeyValues(program, identifyingKey, (string)arguments[1]);
            var keyEqualExpression = Compiler.BuildKeyEqualExpression(program.Plan, String.Empty, "AKey", new Schema.RowType(identifyingKey.Columns).Columns, keyValues.DataType.Columns);

            var updateStatement = new UpdateStatement();

            updateStatement.Target = new IdentifierExpression(tableVar.Name);

            var row = (IRow)arguments[2];

            for (int i = 0; i < row.DataType.Columns.Count; i++)
            {
                updateStatement.Columns.Add(new UpdateColumnExpression(new IdentifierExpression(row.DataType.Columns[i].Name), new IdentifierExpression(Schema.Object.Qualify(row.DataType.Columns[i].Name, "ARow"))));
            }

            updateStatement.Condition = keyEqualExpression;

            var statement  = new D4TextEmitter().Emit(updateStatement);
            var dataParams = new DataParams();

            dataParams.Add(new DataParam("AKey", keyValues.DataType, Modifier.In, keyValues));
            dataParams.Add(new DataParam("ARow", row.DataType, Modifier.In, row));

            APIUtilities.Execute(program, statement, dataParams);

            return(null);
        }
Ejemplo n.º 2
0
        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
            );
        }
Ejemplo n.º 3
0
        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
            );
        }
Ejemplo n.º 4
0
        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());
        }
Ejemplo n.º 5
0
        public DataParams RemoteParamDataToDataParams(RemoteParamData paramsValue)
        {
            if ((paramsValue.Params != null) && (paramsValue.Params.Length > 0))
            {
                DataParams     localParamsValue = new DataParams();
                Schema.RowType rowType          = new Schema.RowType();
                for (int index = 0; index < paramsValue.Params.Length; index++)
                {
                    rowType.Columns.Add(new Schema.Column(paramsValue.Params[index].Name, (Schema.ScalarType)_serverProcess.ServerSession.Server.Catalog[paramsValue.Params[index].TypeName]));
                }

                Row row = new Row(_serverProcess.ValueManager, rowType);
                try
                {
                    row.ValuesOwned = false;
                    row.AsPhysical  = paramsValue.Data.Data;

                    for (int index = 0; index < paramsValue.Params.Length; index++)
                    {
                        if (row.HasValue(index))
                        {
                            localParamsValue.Add(new DataParam(row.DataType.Columns[index].Name, row.DataType.Columns[index].DataType, RemoteModifierToModifier(paramsValue.Params[index].Modifier), DataValue.CopyValue(_serverProcess.ValueManager, row[index])));
                        }
                        else
                        {
                            localParamsValue.Add(new DataParam(row.DataType.Columns[index].Name, row.DataType.Columns[index].DataType, RemoteModifierToModifier(paramsValue.Params[index].Modifier), null));
                        }
                    }

                    return(localParamsValue);
                }
                finally
                {
                    row.Dispose();
                }
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 6
0
        public static DataParams ParamsArrayToDataParams(IServerProcess process, DataParam[] paramsValue)
        {
            DataParams dataParams = new DataParams();

            if (paramsValue != null)
            {
                foreach (var dataParam in paramsValue)
                {
                    dataParams.Add(dataParam);
                }
            }
            return(dataParams);
        }
Ejemplo n.º 7
0
        public override object InternalExecute(Program program, object[] arguments)
        {
            var tableVar = APIUtilities.ResolveTableVarName(program, (string)arguments[0]);

            var insertStatement = new InsertStatement(new IdentifierExpression("ARow"), new IdentifierExpression(tableVar.Name));
            var statement       = new D4TextEmitter().Emit(insertStatement);
            var dataParams      = new DataParams();
            var row             = (IRow)arguments[1];

            dataParams.Add(new DataParam("ARow", row.DataType, Modifier.In, row));

            APIUtilities.Execute(program, statement, dataParams);

            return(null);
        }
Ejemplo n.º 8
0
        public override object InternalExecute(Program program, object[] arguments)
        {
            var tableVar           = APIUtilities.ResolveTableVarName(program, (string)arguments[0]);
            var identifyingKey     = APIUtilities.FindIdentifyingKey(program, tableVar);
            var keyValues          = APIUtilities.GetKeyValues(program, identifyingKey, (string)arguments[1]);
            var keyEqualExpression = Compiler.BuildKeyEqualExpression(program.Plan, String.Empty, "AKey", new Schema.RowType(identifyingKey.Columns).Columns, keyValues.DataType.Columns);
            var tableVarExpression = new IdentifierExpression(tableVar.Name);
            var restrictExpression = new RestrictExpression(tableVarExpression, keyEqualExpression);
            var expression         = new D4TextEmitter().Emit(restrictExpression);
            var dataParams         = new DataParams();

            dataParams.Add(new DataParam("AKey", keyValues.DataType, Modifier.In, keyValues));

            return(APIUtilities.EvaluateSingleton(program, expression, dataParams));
        }
Ejemplo n.º 9
0
        // Parameter Translation
        public DataParams RemoteParamsToDataParams(RemoteParam[] paramsValue)
        {
            if ((paramsValue != null) && (paramsValue.Length > 0))
            {
                DataParams localParamsValue = new DataParams();
                foreach (RemoteParam remoteParam in paramsValue)
                {
                    localParamsValue.Add(new DataParam(remoteParam.Name, (Schema.ScalarType)_serverProcess.ServerSession.Server.Catalog[remoteParam.TypeName], RemoteModifierToModifier(remoteParam.Modifier)));
                }

                return(localParamsValue);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        public static DataParams NativeParamsToDataParams(ServerProcess process, NativeParam[] nativeParams)
        {
            DataParams dataParams = new DataParams();

            if (nativeParams != null)
            {
                foreach (var nativeParam in nativeParams)
                {
                    var       value     = NativeValueToDataValue(process, nativeParam.Value);
                    DataParam dataParam =
                        new DataParam
                        (
                            nativeParam.Name,
                            value.DataType,
                            NativeCLIUtility.NativeModifierToModifier(nativeParam.Modifier),
                            value.IsNil ? null : (value is IScalar ? value.AsNative : value)
                        );
                    dataParams.Add(dataParam);
                }
            }
            return(dataParams);
        }
Ejemplo n.º 12
0
        public static DataParams ToDataParams(IServerProcess process, IEnumerable <KeyValuePair <string, object> > args)
        {
            var dataParams = new DataParams();

            if (args != null)
            {
                foreach (var e in args)
                {
                    var value = ValueToDataValue((ServerProcess)process, e.Value);
                    dataParams.Add
                    (
                        new DataParam
                        (
                            e.Key,
                            value.DataType,
                            DAE.Language.Modifier.In,
                            value.IsNil ? null : (value is IScalar ? value.AsNative : value)
                        )
                    );
                }
            }
            return(dataParams);
        }
Ejemplo n.º 13
0
        public static string Copy(Program program, TableNode sourceTable, TableNode targetTable, GenerateStatementHandler generateStatement)
        {
            Statement targetStatement =
                generateStatement
                (
                    sourceTable,
                    targetTable,
                    (Expression)targetTable.EmitStatement(Alphora.Dataphor.DAE.Language.D4.EmitMode.ForCopy)
                );

            Schema.Key displaySourceKey = program.FindClusteringKey(sourceTable.TableVar);

            StringBuilder result = new StringBuilder();
            long          succeededUpdateCount          = 0;
            long          failedUpdateCount             = 0;
            bool          maxResultLengthMessageWritten = false;

            // Start a new process so that we don't mess with the transaction context of this one
            ProcessInfo info = new ProcessInfo(program.ServerProcess.ServerSession.SessionInfo);

            info.UseImplicitTransactions = false;
            IServerProcess targetProcess = ((IServerSession)program.ServerProcess.ServerSession).StartProcess(info);

            try
            {
                Program targetProgram = new Program((ServerProcess)targetProcess);
                targetProgram.Code = targetTable;
                targetProgram.Start(null);
                try
                {
                    // Have the target program use the main program's context
                    Stack oldTargetContext = targetProgram.SwitchContext(program.Stack);
                    try
                    {
                        info.DefaultIsolationLevel = IsolationLevel.Browse;
                        IServerProcess sourceProcess = ((IServerSession)program.ServerProcess.ServerSession).StartProcess(info);
                        try
                        {
                            Program sourceProgram = new Program((ServerProcess)sourceProcess);
                            sourceProgram.Code = sourceTable;
                            sourceProgram.Start(null);
                            try
                            {
                                // Have the source program use the main program's context
                                Stack oldSourceContext = sourceProgram.SwitchContext(program.Stack);
                                try
                                {
                                    ITable source = (ITable)sourceTable.Execute(sourceProgram);
                                    try
                                    {
                                        source.Open();

                                        // TODO: IBAS Project #26790 - allow cross-process row copies for streamed types
                                        // There is a MarshalRow call in the LocalProcess, would that solve this problem?
                                        using (Row row = new Row(targetProcess.ValueManager, sourceTable.DataType.CreateRowType()))
                                        {
                                            DataParams paramsValue = new DataParams();
                                            paramsValue.Add(new DataParam("ASourceRow", row.DataType, DAE.Language.Modifier.Const, row));

                                            IServerStatementPlan target = targetProcess.PrepareStatement(new D4TextEmitter().Emit(targetStatement), paramsValue);
                                            try
                                            {
                                                target.CheckCompiled();

                                                while (source.Next())
                                                {
                                                    row.ClearValues();
                                                    targetProcess.BeginTransaction(IsolationLevel.Isolated);
                                                    try
                                                    {
                                                        source.Select(row);
                                                        target.Execute(paramsValue);
                                                        targetProcess.CommitTransaction();
                                                        succeededUpdateCount++;
                                                    }
                                                    catch (Exception exception)
                                                    {
                                                        failedUpdateCount++;
                                                        targetProcess.RollbackTransaction();
                                                        if (result.Length < 100000)
                                                        {
                                                            result.Append(KeyValuesToString(displaySourceKey, row) + " - " + exception.Message + "\r\n");
                                                        }
                                                        else
                                                        {
                                                            if (!maxResultLengthMessageWritten)
                                                            {
                                                                result.Append(Strings.Get("MaxResultLengthExceeded"));
                                                                maxResultLengthMessageWritten = true;
                                                            }
                                                        }
                                                    }

                                                    // Yield in case our process is aborted.
                                                    program.CheckAborted();
                                                }
                                            }
                                            finally
                                            {
                                                targetProcess.UnprepareStatement(target);
                                            }
                                        }
                                    }
                                    finally
                                    {
                                        source.Close();
                                    }
                                }
                                finally
                                {
                                    sourceProgram.SwitchContext(oldSourceContext);                                      // Don't let the source program cleanup the main context
                                }
                            }
                            finally
                            {
                                sourceProgram.Stop(null);
                            }
                        }
                        finally
                        {
                            ((IServerSession)program.ServerProcess.ServerSession).StopProcess(sourceProcess);
                        }

                        result.AppendFormat(Strings.Get("Results"), succeededUpdateCount, failedUpdateCount);
                        return(result.ToString());
                    }
                    finally
                    {
                        targetProgram.SwitchContext(oldTargetContext);                          // Don't let the target program cleanup the main context
                    }
                }
                finally
                {
                    targetProgram.Stop(null);
                }
            }
            finally
            {
                ((IServerSession)program.ServerProcess.ServerSession).StopProcess(targetProcess);
            }
        }
Ejemplo n.º 14
0
        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();
        }
Ejemplo n.º 15
0
        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);
        }