예제 #1
0
        public TObjectType AddNew()
        {
            TObjectType obj = CSObject <TObjectType> .New();

            Add(obj);

            return(obj);
        }
예제 #2
0
        internal override void AddFromPrefetch(CSObject csObject)
        {
            csObject.Fire_ObjectReading();

            ((TObjectType)csObject).ObjectDeleted += OnObjectDeleted;

            _objectArray.Add((TObjectType)csObject);

            csObject.Fire_ObjectRead();
        }
예제 #3
0
        private List <TObjectType> GetObjects(string sqlQuery, CSParameterCollection parameters, Dictionary <string, string> aliasMap, IEnumerable <PrefetchField> prefetchFields)
        {
            using (CSTransaction csTransaction = new CSTransaction(Schema))
            {
                List <TObjectType> objectList = new List <TObjectType>();

                using (ICSDbReader reader = DB.CreateReader(sqlQuery, parameters))
                {
                    while (reader.Read())
                    {
                        TObjectType csObject = CSObject <TObjectType> .New();

                        csObject.Fire_ObjectReading();

                        csObject.FromDataReader(reader, aliasMap);

                        foreach (PrefetchField prefetchField in prefetchFields)
                        {
                            csObject.ReadRelationToOne(prefetchField.SchemaField, reader, prefetchField.AliasMap);
                        }

                        if (FilterPredicate != null)
                        {
                            bool shouldAdd = true;

                            foreach (Predicate <TObjectType> predicate in FilterPredicate.GetInvocationList())
                            {
                                if (!predicate(csObject))
                                {
                                    shouldAdd = false;
                                    break;
                                }
                            }

                            if (!shouldAdd)
                            {
                                continue;
                            }
                        }

                        csObject.ObjectDeleted += OnObjectDeleted;

                        objectList.Add(csObject);

                        csObject.Fire_ObjectRead();
                    }
                }

                csTransaction.Commit();

                return(objectList);
            }
        }
예제 #4
0
        public object GetScalar(string fieldName, CSAggregate aggregate, string filterExpression, CSParameterCollection filterParameters)
        {
            string tableAlias = CSNameGenerator.NextTableAlias;

            CSFilter queryFilter = Filter.And(BuildRelationFilter(tableAlias));

            if (!string.IsNullOrEmpty(filterExpression))
            {
                queryFilter = queryFilter.And(filterExpression, filterParameters);
            }

            return(CSObject <TObjectType> .GetScalar(fieldName, tableAlias, aggregate, queryFilter));
        }
예제 #5
0
        internal CSFieldValueCollection(CSObject csObject)
        {
            foreach (CSSchemaField schemaField in csObject.Schema.Fields)
            {
                CSFieldValue fieldValue = new CSFieldValue(csObject, schemaField);

                _list.Add(fieldValue);

                if (fieldValue.SchemaField.HasProperty)
                {
                    _map[fieldValue.SchemaField.Name] = fieldValue;
                }

                if (fieldValue.SchemaField.MappedColumn != null)
                {
                    _map["#" + fieldValue.SchemaField.MappedColumn.Name] = fieldValue;
                }
            }
        }
예제 #6
0
        private void Populate()
        {
            if (Populated)
            {
                return;
            }

            if (Relation != null && RelationObject != null && Relation.RelationType == CSSchemaRelationType.OneToMany && RelationObject.IsNew)
            {
                _objectArray = new List <TObjectType>();
                Populated    = true;

                return;
            }

            CSTable table = new CSTable(Schema);

            //string mainAlias = CSHelper.NextTableAlias;

            List <string> columnList             = new List <string>(Schema.ColumnsToRead.Count);
            List <string> aliasList              = new List <string>(Schema.ColumnsToRead.Count);
            Dictionary <string, string> aliasMap = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (string columnName in Schema.ColumnsToRead)
            {
                string alias = CSNameGenerator.NextFieldAlias;

                columnList.Add(table.TableAlias + "." + columnName);
                aliasList.Add(alias);
                aliasMap.Add(alias, columnName);
            }

            CSJoinList filterJoins = new CSJoinList();

            List <PrefetchField> prefetchFields = CSObject.GetPrefetchFieldsOne(table, columnList, aliasList, filterJoins, PrefetchPaths);

            CSFilter whereFilter;

            if (PrefetchFilter != null)
            {
                whereFilter = new CSFilter(DB.QuoteField(table.TableAlias + "." + PrefetchFilter.ForeignKey) + " in (" + PrefetchFilter.InStatement + ")", PrefetchFilter.Parameters);
            }
            else
            {
                string parsedFilterExpression = CSExpressionParser.ParseFilter(Filter.Expression, Schema, table.TableAlias, filterJoins);

                whereFilter = new CSFilter(parsedFilterExpression, Filter.Parameters);

                CSFilter relationFilter = BuildRelationFilter(table.TableAlias);

                whereFilter = whereFilter.And(CSExpressionParser.ParseFilter(relationFilter.Expression, Schema, table.TableAlias, filterJoins), relationFilter.Parameters);
            }

            string parsedOrderBy = CSExpressionParser.ParseOrderBy(OrderBy, Schema, table.TableAlias, filterJoins);

            string sqlQuery = DB.BuildSelectSQL(table.TableName, table.TableAlias, columnList.ToArray(), aliasList.ToArray(), filterJoins.BuildJoinExpressions(), whereFilter.Expression, parsedOrderBy, StartRecord, MaxRecords, true, false);

            _objectArray = GetObjects(sqlQuery, whereFilter.Parameters, aliasMap, prefetchFields);

            if (Schema.KeyColumns.Count == 1)
            {
                string columnName = Schema.KeyColumns[0].Name;

                _objectMap = new Dictionary <object, TObjectType>();

                foreach (TObjectType csObject in _objectArray)
                {
                    _objectMap.Add(csObject.Data["#" + columnName].Value, csObject);
                }
            }

            foreach (CSSchemaField prefetchField in GetPrefetchFieldsMany())
            {
                CSRelation relation = prefetchField.Relation;

                Dictionary <object, TObjectType> prefetchMap = new Dictionary <object, TObjectType>();

                // Creates empty lists in each object of this list
                foreach (TObjectType csObject in _objectArray)
                {
                    prefetchMap[csObject.Data["#" + relation.LocalKey].Value] = csObject;

                    CSList relationCollection = (CSList)Activator.CreateInstance(prefetchField.FieldType);

                    relationCollection.Relation       = relation;
                    relationCollection.RelationObject = csObject;

                    relationCollection.InitializePrefetch();

                    csObject.Data[prefetchField.Name].ValueDirect = relationCollection;
                    csObject.Data[prefetchField.Name].ValueState  = CSFieldValueState.Read;
                }

                Type objectType = relation.ForeignSchema.ClassType;

                CSList csList = (CSList)Activator.CreateInstance(typeof(CSList <>).MakeGenericType(objectType));

                //string prefetchTableAlias = CSNameGenerator.NextTableAlias;

                string prefetchFilter = DB.BuildSelectSQL(table.TableName, table.TableAlias, new[] { table.TableAlias + "." + relation.LocalKey }, new[] { CSNameGenerator.NextFieldAlias }, filterJoins.BuildJoinExpressions(), whereFilter.Expression, parsedOrderBy, StartRecord, MaxRecords, true, true);

                csList.PrefetchFilter = new PrefetchFilter(relation.ForeignKey, prefetchFilter, whereFilter.Parameters);

                if (PrefetchPaths != null && PrefetchPaths.Length > 0)
                {
                    List <string> newPrefetchPaths = new List <string>();

                    foreach (string path in PrefetchPaths)
                    {
                        if (path.StartsWith(prefetchField.Name + "."))
                        {
                            newPrefetchPaths.Add(path.Substring(prefetchField.Name.Length + 1));
                        }
                    }

                    if (newPrefetchPaths.Count > 0)
                    {
                        csList.PrefetchPaths = newPrefetchPaths.ToArray();
                    }
                }

                foreach (CSObject csObject in csList)
                {
                    object localKey = csObject.Data["#" + relation.ForeignKey].ValueDirect;

                    CSList relationCollection = (CSList)prefetchMap[localKey].Data[prefetchField.Name].ValueDirect;

                    relationCollection.AddFromPrefetch(csObject);
                }
            }


            Populated = true;
        }
예제 #7
0
 internal override void Remove(CSObject obj)
 {
     Remove((TObjectType)obj);
 }
예제 #8
0
 public void Insert(int index, CSObject value)
 {
     throw new NotSupportedException("Insert() not supported for CSList");
 }
예제 #9
0
 internal abstract void AddFromPrefetch(CSObject obj);
예제 #10
0
 internal abstract void Remove(CSObject obj);
예제 #11
0
 internal CSFieldValue(CSObject objectData, CSSchemaField schemaField)
 {
     _csObject    = objectData;
     _schemaField = schemaField;
 }