示例#1
0
        public QueryGenerator(PortalDataContext dbContext, LogicModel logicModel)
        {
            _dbContext             = dbContext;
            _logicModel            = logicModel;
            _sourceType            = _logicModel.SourceType;
            _expressionsLogicModel = logicModel.ExpressionsLogic;

            if (_logicModel.SourceType == "Table")
            {
                var table = dbContext.LP_Tables.First(n => n.ID == _logicModel.SourceID);

                var converter = new TableEntityModelConverter(_dbContext);
                var model     = converter.Convert(table);

                var columns = model.Columns;
                _querySource = GetCorrectName(model.Name);

                var allColumnsQuery = (from n in columns
                                       let m = GetCorrectName(n.Name)
                                               select new KeyValuePair <String, String>(n.Name, m));

                _allColumns = allColumnsQuery.ToDictionary();

                var primaryColumnsQuery = (from n in columns
                                           where n.IsPrimary
                                           let m = GetCorrectName(n.Name)
                                                   select new KeyValuePair <String, String>(n.Name, m));

                _primaryColumns = primaryColumnsQuery.ToDictionary();

                var dbTypesQuery = (from n in columns
                                    let t = GetDataType(n.Type)
                                            let m = GetCorrectName(n.Name)
                                                    select new KeyValuePair <String, SqlDbType>(m, t));

                _dbTypes = dbTypesQuery.ToDictionary();

                _outputColumns = GetOutputs().ToHashSet();
            }
            else if (_logicModel.SourceType == "Logic")
            {
                var logic = dbContext.LP_Logics.First(n => n.ID == _logicModel.SourceID);

                var converter = new LogicEntityModelConverter(_dbContext);
                var model     = converter.Convert(logic);

                var queryGen = new QueryGenerator(_dbContext, model);

                var selectQuery = queryGen.SelectQuery(true);

                _querySource = String.Format("({0})", selectQuery);

                var allColumnsQuery = (from n in queryGen.OutputColumns
                                       let m = GetCorrectName(n)
                                               select new KeyValuePair <String, String>(n, m));

                _allColumns = allColumnsQuery.ToDictionary();

                var dbTypesQuery = (from n in queryGen.OutputColumns
                                    let m = GetCorrectName(n)
                                            let t = queryGen.DbTypes[m]
                                                    select new KeyValuePair <String, SqlDbType>(m, t));

                _dbTypes = dbTypesQuery.ToDictionary();

                _outputColumns = GetOutputs().ToHashSet();
            }

            if (_primaryColumns == null || _primaryColumns.Count == 0)
            {
                _primaryColumns = _allColumns;
            }

            var allParamsQuery = (from n in _allColumns
                                  let p = String.Format("@v{0}", n.Key.ComputeCrc16())
                                          select new KeyValuePair <String, String>(n.Value, p));

            _allColumnsParams = allParamsQuery.ToDictionary();

            var primaryParamsQuery = (from n in _primaryColumns
                                      let p = String.Format("@p{0}", n.Key.ComputeCrc16())
                                              select new KeyValuePair <String, String>(n.Value, p));

            _primaryColumnsParams = primaryParamsQuery.ToDictionary();
        }