Beispiel #1
0
 public void UnprepareStatement(IServerStatementPlan plan)
 {
     try
     {
         _script.Process.UnprepareStatement(plan);
     }
     catch (Exception E)
     {
         throw _script.Process.ServerSession.WrapException(E);
     }
 }
Beispiel #2
0
 public void UnprepareStatement(IServerStatementPlan plan)
 {
     try
     {
         _batch.UnprepareStatement(((LocalStatementPlan)plan).RemotePlan);
     }
     catch
     {
         // ignore exceptions here
     }
     ((LocalStatementPlan)plan).Dispose();
 }
Beispiel #3
0
        public static void Execute(Program program, string statement, DataParams dataParams)
        {
            IServerStatementPlan plan = ((IServerProcess)program.ServerProcess).PrepareStatement(statement, dataParams);

            try
            {
                plan.CheckCompiled();
                plan.Execute(dataParams);
            }
            finally
            {
                ((IServerProcess)program.ServerProcess).UnprepareStatement(plan);
            }
        }
Beispiel #4
0
 /// <summary> Unprepares a statement plan. </summary>
 /// <param name="plan"> A reference to a plan object returned from a call to PrepareStatement. </param>
 public void UnprepareStatement(IServerStatementPlan plan)
 {
     try
     {
         // The plan will be unprepared on the next prepare call
         ReportCleanup(((LocalStatementPlan)plan).RemotePlan);
         //FProcess.UnprepareStatement(((LocalStatementPlan)APlan).RemotePlan);
     }
     catch
     {
         // ignore exceptions here
     }
     ((LocalStatementPlan)plan).Dispose();
 }
Beispiel #5
0
 protected override void InternalDelete()
 {
     if (_deleteStatement == String.Empty)
     {
         base.InternalDelete();
     }
     else
     {
         DAE.Runtime.DataParams paramsValue = GetParamsFromRow(_buffer[_activeOffset].Row, "Old.");
         IServerStatementPlan   plan        = _process.PrepareStatement(_deleteStatement, paramsValue);
         try
         {
             plan.Execute(paramsValue);
         }
         finally
         {
             _process.UnprepareStatement(plan);
         }
     }
 }
Beispiel #6
0
        /// <summary>Executes the given statement using the utility process.</summary>
        public void Execute(IServerProcess process, string statement, DAE.Runtime.DataParams paramsValue)
        {
            CheckActive();

            if (process == null)
            {
                process = UtilityProcess;
            }

            IServerStatementPlan plan = process.PrepareStatement(statement, paramsValue);

            try
            {
                plan.Execute(paramsValue);
            }
            finally
            {
                process.UnprepareStatement(plan);
            }
        }
Beispiel #7
0
 public void Execute(DataParams paramsValue)
 {
     try
     {
         if (IsExpression())
         {
             IServerExpressionPlan plan = PrepareExpression(paramsValue);
             try
             {
                 if (plan.DataType is Schema.TableType)
                 {
                     plan.Close(plan.Open(paramsValue));
                 }
                 else
                 {
                     DataValue.DisposeValue(this.Script.Process.ValueManager, plan.Evaluate(paramsValue));
                 }
             }
             finally
             {
                 UnprepareExpression(plan);
             }
         }
         else
         {
             IServerStatementPlan plan = PrepareStatement(paramsValue);
             try
             {
                 plan.Execute(paramsValue);
             }
             finally
             {
                 UnprepareStatement(plan);
             }
         }
     }
     catch (Exception E)
     {
         throw _script.Process.ServerSession.WrapException(E);
     }
 }
Beispiel #8
0
 protected override void InternalUpdate(IRow row)
 {
     if (_updateStatement == String.Empty)
     {
         base.InternalUpdate(row);
     }
     else
     {
         DAE.Runtime.DataParams paramsValue = GetParamsFromRow(_originalRow, "Old.");
         GetParamsFromRow(row, paramsValue, "New.");
         IServerStatementPlan plan = _process.PrepareStatement(_updateStatement, paramsValue);
         try
         {
             plan.Execute(paramsValue);
         }
         finally
         {
             _process.UnprepareStatement(plan);
         }
     }
 }
Beispiel #9
0
        public void Prepare()
        {
            _resultPanel.Clear();
            PrepareForExecute();

            var result = new StringBuilder();

            var errors = new ErrorList();

            try
            {
                using (var statusForm = new StatusForm(Strings.ProcessingQuery))
                {
                    bool attemptExecute = true;
                    try
                    {
                        DateTime startTime = DateTime.Now;
                        try
                        {
                            IServerScript  script;
                            IServerProcess process =
                                DataSession.ServerSession.StartProcess(
                                    new ProcessInfo(DataSession.ServerSession.SessionInfo));
                            try
                            {
                                script = process.PrepareScript(GetTextToExecute());
                                try
                                {
                                    if (ScriptExecutionUtility.ConvertParserErrors(script.Messages, errors))
                                    {
                                        foreach (IServerBatch batch in script.Batches)
                                        {
                                            if (batch.IsExpression())
                                            {
                                                IServerExpressionPlan plan = batch.PrepareExpression(null);
                                                try
                                                {
                                                    attemptExecute &=
                                                        ScriptExecutionUtility.ConvertCompilerErrors(plan.Messages,
                                                                                                     errors);
                                                    if (attemptExecute)
                                                    {
                                                        result.AppendFormat
                                                        (
                                                            Strings.PrepareSuccessful,
                                                            new object[]
                                                        {
                                                            plan.PlanStatistics.PrepareTime.ToString(),
                                                            plan.PlanStatistics.CompileTime.ToString(),
                                                            plan.PlanStatistics.OptimizeTime.ToString(),
                                                            plan.PlanStatistics.BindingTime.ToString()
                                                        }
                                                        );
                                                        result.Append("\r\n");
                                                    }
                                                }
                                                finally
                                                {
                                                    batch.UnprepareExpression(plan);
                                                }
                                            }
                                            else
                                            {
                                                IServerStatementPlan plan = batch.PrepareStatement(null);
                                                try
                                                {
                                                    attemptExecute &=
                                                        ScriptExecutionUtility.ConvertCompilerErrors(plan.Messages,
                                                                                                     errors);
                                                    if (attemptExecute)
                                                    {
                                                        result.AppendFormat
                                                        (
                                                            Strings.PrepareSuccessful,
                                                            new object[]
                                                        {
                                                            plan.PlanStatistics.PrepareTime.ToString(),
                                                            plan.PlanStatistics.CompileTime.ToString(),
                                                            plan.PlanStatistics.OptimizeTime.ToString(),
                                                            plan.PlanStatistics.BindingTime.ToString()
                                                        }
                                                        );
                                                        result.Append("\r\n");
                                                    }
                                                }
                                                finally
                                                {
                                                    batch.UnprepareStatement(plan);
                                                }
                                            }

                                            AppendResultPanel(result.ToString());
                                            result.Length = 0;
                                        }
                                    }
                                }
                                finally
                                {
                                    process.UnprepareScript(script);
                                }
                            }
                            finally
                            {
                                DataSession.ServerSession.StopProcess(process);
                            }
                        }
                        finally
                        {
                            TimeSpan elapsed = DateTime.Now - startTime;
                            _executionTimeStatus.Text = elapsed.ToString();
                        }

                        if (attemptExecute)
                        {
                            SetStatus(Strings.ScriptPrepareSuccessful);
                        }
                        else
                        {
                            SetStatus(Strings.ScriptPrepareFailed);
                        }
                    }
                    catch (Exception exception)
                    {
                        SetStatus(Strings.ScriptFailed);
                        errors.Add(exception);
                    }
                }
            }
            finally
            {
                ProcessErrors(errors);
            }
        }
Beispiel #10
0
        public NativeResult Execute(string statement, NativeParam[] paramsValue, NativeExecutionOptions options)
        {
            IServerScript script = _process.PrepareScript(statement);

            try
            {
                if (script.Batches.Count != 1)
                {
                    throw new ArgumentException("Execution statement must contain one, and only one, batch.");
                }

                IServerBatch batch      = script.Batches[0];
                DataParams   dataParams = NativeMarshal.NativeParamsToDataParams((Server.ServerProcess)_process, paramsValue);
                NativeResult result     = new NativeResult();
                result.Params = paramsValue;

                if (batch.IsExpression())
                {
                    IServerExpressionPlan expressionPlan = batch.PrepareExpression(dataParams);
                    try
                    {
                        if (expressionPlan.DataType is Schema.TableType)
                        {
                            if (options != NativeExecutionOptions.SchemaOnly)
                            {
                                IServerCursor cursor = expressionPlan.Open(dataParams);
                                try
                                {
                                    result.Value = NativeMarshal.ServerCursorToNativeValue(_process, cursor);
                                }
                                finally
                                {
                                    expressionPlan.Close(cursor);
                                }
                            }
                            else
                            {
                                result.Value = NativeMarshal.TableVarToNativeTableValue(_process, expressionPlan.TableVar);
                            }
                        }
                        else
                        {
                            if (options != NativeExecutionOptions.SchemaOnly)
                            {
                                using (IDataValue tempValue = expressionPlan.Evaluate(dataParams))
                                {
                                    result.Value = NativeMarshal.DataValueToNativeValue(_process, tempValue);
                                }
                            }
                            else
                            {
                                result.Value = NativeMarshal.DataTypeToNativeValue(_process, expressionPlan.DataType);
                            }
                        }
                    }
                    finally
                    {
                        batch.UnprepareExpression(expressionPlan);
                    }
                }
                else
                {
                    IServerStatementPlan statementPlan = batch.PrepareStatement(dataParams);
                    try
                    {
                        if (options != NativeExecutionOptions.SchemaOnly)
                        {
                            statementPlan.Execute(dataParams);
                        }
                    }
                    finally
                    {
                        batch.UnprepareStatement(statementPlan);
                    }
                }

                if (options != NativeExecutionOptions.SchemaOnly)
                {
                    NativeMarshal.SetNativeOutputParams(_process, result.Params, dataParams);
                }
                return(result);
            }
            finally
            {
                _process.UnprepareScript(script);
            }
        }
Beispiel #11
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);
            }
        }
        public static void ExecuteScript(IServerProcess process, string script, ScriptExecuteOption options, out ErrorList errors, out TimeSpan timeElapsed, ReportScriptProgressHandler reportScriptProgress, DebugLocator locator)
        {
            StringBuilder result = new StringBuilder();

            errors      = new ErrorList();
            timeElapsed = TimeSpan.Zero;

            bool     attemptExecute = true;
            DateTime startTime      = DateTime.Now;

            try
            {
                IServerScript localScript = process.PrepareScript(script, locator);
                try
                {
                    if (ConvertParserErrors(localScript.Messages, errors))
                    {
                        foreach (IServerBatch batch in localScript.Batches)
                        {
                            PlanStatistics statistics = null;
                            try
                            {
                                if (batch.IsExpression())
                                {
                                    IServerExpressionPlan plan = batch.PrepareExpression(null);
                                    try
                                    {
                                        attemptExecute &= ConvertCompilerErrors(plan.Messages, errors);
                                        if (attemptExecute)
                                        {
                                            int rowCount = ReadResult(result, plan);

                                            AppendStatistics(result, options, plan.PlanStatistics, plan.ProgramStatistics, rowCount);
                                            statistics = plan.PlanStatistics;
                                        }
                                    }
                                    finally
                                    {
                                        batch.UnprepareExpression(plan);
                                    }
                                }
                                else
                                {
                                    IServerStatementPlan plan = batch.PrepareStatement(null);
                                    try
                                    {
                                        attemptExecute &= ConvertCompilerErrors(plan.Messages, errors);
                                        if (attemptExecute)
                                        {
                                            plan.Execute(null);

                                            AppendStatistics(result, options, plan.PlanStatistics, plan.ProgramStatistics, -1);
                                            statistics = plan.PlanStatistics;
                                        }
                                    }
                                    finally
                                    {
                                        batch.UnprepareStatement(plan);
                                    }
                                }
                            }
                            finally
                            {
                                if (reportScriptProgress != null)
                                {
                                    reportScriptProgress(statistics, result.ToString());
                                }
                                result.Length = 0;
                            }
                        }                       // foreach batch...
                    }                           // if (no parser errors)...
                }
                finally
                {
                    process.UnprepareScript(localScript);
                }
            }
            catch (Exception exception)
            {
                errors.Add(exception);
            }
            timeElapsed = DateTime.Now - startTime;
        }
Beispiel #13
0
        private DataFhirServerResult Execute(IServerProcess process, string statement, DataParam[] paramsValue)
        {
            IServerScript script = process.PrepareScript(statement);

            try
            {
                if (script.Batches.Count != 1)
                {
                    throw new ArgumentException("Execution statement must contain one, and only one, batch.");
                }

                IServerBatch         batch      = script.Batches[0];
                DataParams           dataParams = DataFhirMarshal.ParamsArrayToDataParams(process, paramsValue);
                DataFhirServerResult result     = new DataFhirServerResult();
                result.Params = paramsValue;

                if (batch.IsExpression())
                {
                    IServerExpressionPlan expressionPlan = batch.PrepareExpression(dataParams);
                    try
                    {
                        if (expressionPlan.DataType is TableType)
                        {
                            IServerCursor cursor = expressionPlan.Open(dataParams);
                            try
                            {
                                result.Value = DataFhirMarshal.ServerCursorToValue(process, cursor);
                            }
                            finally
                            {
                                expressionPlan.Close(cursor);
                            }
                        }
                        else
                        {
                            using (IDataValue tempValue = expressionPlan.Evaluate(dataParams))
                            {
                                result.Value = DataFhirMarshal.DataValueToValue(process, tempValue);
                            }
                        }
                    }
                    finally
                    {
                        batch.UnprepareExpression(expressionPlan);
                    }
                }
                else
                {
                    IServerStatementPlan statementPlan = batch.PrepareStatement(dataParams);
                    try
                    {
                        statementPlan.Execute(dataParams);
                    }
                    finally
                    {
                        batch.UnprepareStatement(statementPlan);
                    }
                }

                //DotNetMarshal.SetNativeOutputParams(process, result.Params, dataParams);
                return(result);
            }
            finally
            {
                process.UnprepareScript(script);
            }
        }