Beispiel #1
0
        public TranslatedLinqCommand Translate(LinqCommand command)
        {
            if (command.Info == null)
            {
                LinqCommandAnalyzer.Analyze(_dbModel.EntityApp.Model, command);
            }
            try {
                switch (command.CommandType)
                {
                case LinqCommandType.Select:
                    return(TranslateSelect(command));

                case LinqCommandType.Update:
                case LinqCommandType.Delete:
                case LinqCommandType.Insert:
                    return(TranslateNonQuery(command));

                default:
                    ThrowTranslationFailed(command, "Unsupported LINQ command type.");
                    return(null);
                }
            } catch (LinqTranslationException) {
                throw; // if it is alread Linq translation exception, pass it up.
            } catch (Exception ex) {
                var message = "Linq to SQL translation failed: " + ex.Message +
                              "\r\nPossibly facilities you are trying to use are not supported. " +
                              "\r\nTry to reformulate/simplify the query. Hint: do not use c# functions/methods inside query directly. ";
                throw new LinqTranslationException(message, command, ex);
            }
        }
Beispiel #2
0
        public virtual object ExecuteLinqCommand(LinqCommand command)
        {
            this.LastLinqCommand = command;
            var cmdInfo = LinqCommandAnalyzer.Analyze(Context.App.Model, command);

            if (command.Kind == LinqCommandKind.DynamicSql)
            {
                command.EvaluateLocalValues(this);
            }
            //account for LinkedApps - get any entity involved in query and get the app it is registered in
            EntityApp targetApp = this.Context.App;

            if (cmdInfo.EntityTypes.Count > 0)
            {
                var someType    = cmdInfo.EntityTypes[0];
                var someEntInfo = GetEntityInfo(someType);
                Util.Check(someEntInfo != null, "Type {0} is not a registered entity, cannot execute the query.", someType);
                targetApp = someEntInfo.Module.App;
            }
            var ds     = targetApp.DataAccess.GetDataSource(this.Context);
            var result = ds.ExecuteLinqCommand(this, command);

            //fire events
            switch (command.CommandType)
            {
            case LinqCommandType.Select:
                _appEvents.OnExecutedQuery(this, command); break;

            default:
                _appEvents.OnExecutedNonQuery(this, command);
                NextTransactionId = Guid.NewGuid();
                break;
            }
            if (command.Info.Includes.Count > 0 || Context.HasIncludes())
            {
                IncludeQueryHelper.RunIncludeQueries(this, command, result);
            }
            return(result);
        }
Beispiel #3
0
        public static void ScheduleNonQuery <TEntity>(this IEntitySession session,
                                                      IQueryable query, LinqCommandType commandType,
                                                      CommandSchedule schedule = CommandSchedule.TransactionEnd)
        {
            var entQuery = query as EntityQuery;
            var model    = session.Context.App.Model;

            Util.Check(entQuery != null, "query parameter should an EntityQuery.");
            var prov      = entQuery.Provider as EntityQueryProvider;
            var targetEnt = model.GetEntityInfo(typeof(TEntity));

            Util.Check(targetEnt != null, "Generic parameter {0} is not an entity registered in the Model.", typeof(TEntity));
            var command = new LinqCommand(entQuery, commandType, LinqCommandKind.DynamicSql, targetEnt);

            LinqCommandAnalyzer.Analyze(model, command);
            command.EvaluateLocalValues((EntitySession)session);
            var scheduledCommand = new ScheduledLinqCommand()
            {
                Command = command, Schedule = schedule
            };
            var entSession = (EntitySession)session;

            entSession.ScheduledCommands.Add(scheduledCommand);
        }
Beispiel #4
0
        private void AddScheduledCommands(IList <ScheduledLinqCommand> commands)
        {
            if (commands.Count == 0)
            {
                return;
            }
            var entModel = _db.DbModel.EntityApp.Model;
            var engine   = new Vita.Data.Linq.Translation.LinqEngine(_db.DbModel);

            object[] fmtArgs = new object[100];
            //specifics of formatting stored proc call - the first 2 args are always two braces
            // Braces in string literals are escaped and are represented as '{0}' and '{1}'
            fmtArgs[0] = "{";
            fmtArgs[1] = "}";
            foreach (var schCmd in commands)
            {
                CheckCurrentCommand();
                var dbCmd   = _currentCommand.DbCommand;
                var linqCmd = schCmd.Command;
                LinqCommandAnalyzer.Analyze(entModel, linqCmd);
                var transCmd = engine.Translate(linqCmd);
                for (int prmIndex = 0; prmIndex < transCmd.Parameters.Count; prmIndex++)
                {
                    var linqParam = transCmd.Parameters[prmIndex];
                    var value     = linqParam.ReadValue(linqCmd.ParameterValues) ?? DBNull.Value;
                    var dbParam   = dbCmd.CreateParameter();
                    _db.DbModel.LinqSqlProvider.SetDbParameterValue(dbParam, linqParam.Type, value);
                    var globalParamIndex = dbCmd.Parameters.Count;
                    dbParam.ParameterName = _driver.DynamicSqlParameterPrefix + "P" + globalParamIndex;
                    fmtArgs[prmIndex + 2] = dbParam.ParameterName;
                    dbCmd.Parameters.Add(dbParam);
                }
                var sql = string.Format(transCmd.BatchSqlTemplate, fmtArgs);
                _sqlBuilder.AppendLine(sql);
            }//foreach schCmd
        }