Beispiel #1
0
        public CSJoin(CSRelation relation, string sourceAlias)
        {
            Type = CSJoinType.Left;

            _leftSchema  = relation.Schema;
            _rightSchema = relation.ForeignSchema;

            _leftAlias  = sourceAlias;
            _rightAlias = CSNameGenerator.NextTableAlias;

            LeftColumn  = relation.LocalKey;
            RightColumn = relation.ForeignKey;
        }
Beispiel #2
0
        private bool SaveChildrenAfter()
        {
            foreach (CSFieldValue fieldValue in _fieldData)
            {
                if (fieldValue.SchemaField != null && fieldValue.SchemaField.Relation != null)
                {
                    CSRelation relation = fieldValue.SchemaField.Relation;

                    if (relation.RelationType == CSSchemaRelationType.OneToMany || (relation.RelationType == CSSchemaRelationType.ManyToMany && relation.PureManyToMany))
                    {
                        ((CSList)fieldValue.Value).Save();
                    }
                }
            }

            return(true);
        }
Beispiel #3
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;
        }
		internal CSSchemaField(PropertyInfo propInfo , CSSchema schema)
		{
			_propertyInfo = propInfo;
			_schema = schema;
            _fieldType = _propertyInfo.PropertyType;
		    _realType = _fieldType.Inspector().RealType;

			RelationAttribute  attRelation = (RelationAttribute) Attribute.GetCustomAttribute(propInfo,typeof(RelationAttribute),true);
			
            _prefetch = propInfo.IsDefined(typeof(PrefetchAttribute), true);

			if (attRelation != null)
			{
				_relation = new CSRelation(schema,attRelation);

 				return;
			}

			_lazy = propInfo.IsDefined(typeof(LazyAttribute) , true);
			_noCreate = propInfo.IsDefined(typeof(NoCreateAttribute) , true);
            _trim = propInfo.IsDefined(typeof(TrimAttribute), true);
            _optimisticLock = propInfo.IsDefined(typeof(OptimisticLockAttribute), true);
		    _clientGenerated = propInfo.IsDefined(typeof(ClientGeneratedAttribute), true);
		    _serverGenerated = propInfo.IsDefined(typeof(ServerGeneratedAttribute), true);
		    _notMapped = propInfo.IsDefined(typeof (NotMappedAttribute), true);


			MapToAttribute     attMapTo = (MapToAttribute)     Attribute.GetCustomAttribute(propInfo,typeof(MapToAttribute),true);
			NullValueAttribute attNull  = (NullValueAttribute) Attribute.GetCustomAttribute(propInfo,typeof(NullValueAttribute),true);
            IdentityAttribute attIdentity = (IdentityAttribute)Attribute.GetCustomAttribute(propInfo, typeof(IdentityAttribute), true);

            if (!_notMapped)
            {
                if (CSConfig.ColumnMappingOverrideMap.ContainsValue(propInfo.DeclaringType.Name + ":" + propInfo.Name))
                    _mappedColumn =
                        schema.Columns[
                            CSConfig.ColumnMappingOverrideMap[propInfo.DeclaringType.Name + ":" + propInfo.Name]];
                else if (attMapTo != null)
                    _mappedColumn = schema.Columns[attMapTo.Name];
                else
                    _mappedColumn = schema.Columns[propInfo.Name];

                if (_mappedColumn != null)
                    _mappedColumn.MappedField = this;
            }


            SequenceAttribute sequenceAttribute = (SequenceAttribute) Attribute.GetCustomAttribute(propInfo,typeof(SequenceAttribute), true);

            if (sequenceAttribute != null && _schema.DB.SupportsSequences)
            {
                _sequenceName = sequenceAttribute.SequenceName;

                if (_mappedColumn != null && sequenceAttribute.Identity)
                {
                    _mappedColumn.Identity = true;
                    _schema.IdentityColumn = _mappedColumn;
                }
            }

            if (attIdentity != null)
            {
                if (_mappedColumn != null)
                {
                    _mappedColumn.Identity = true;
                    _schema.IdentityColumn = _mappedColumn;
                }
            }

		    if (attNull != null)
			{
				_nullValue = attNull.NullValue;
			}
			else
			{
				Type fieldType = FieldType;

				if (fieldType == typeof(string))         
                    _nullValue = String.Empty;
                else if (fieldType.IsValueType)
                    _nullValue = Activator.CreateInstance(fieldType);
			}

			if (_mappedColumn != null && _mappedColumn.ReadOnly)
			{
				if (_propertyInfo.CanWrite)
					throw new CSException("Property [" + Name + "] for class [" + _schema.ClassType.Name + "] should be read-only");
			}
		}
        internal CSSchemaField(PropertyInfo propInfo, CSSchema schema)
        {
            _propertyInfo = propInfo;
            _schema       = schema;
            _fieldType    = _propertyInfo.PropertyType;
            _realType     = _fieldType.Inspector().RealType;

            RelationAttribute attRelation = propInfo.GetCustomAttribute <RelationAttribute>(true);

            _prefetch = propInfo.IsDefined(typeof(PrefetchAttribute), true);

            if (attRelation != null)
            {
                _relation = new CSRelation(schema, attRelation);

                return;
            }

            _lazy            = propInfo.IsDefined(typeof(LazyAttribute), true);
            _noCreate        = propInfo.IsDefined(typeof(NoCreateAttribute), true);
            _trim            = propInfo.IsDefined(typeof(TrimAttribute), true);
            _optimisticLock  = propInfo.IsDefined(typeof(OptimisticLockAttribute), true);
            _clientGenerated = propInfo.IsDefined(typeof(ClientGeneratedAttribute), true);
            _serverGenerated = propInfo.IsDefined(typeof(ServerGeneratedAttribute), true);
            _notMapped       = propInfo.IsDefined(typeof(NotMappedAttribute), true);

            var mapToAttribute     = propInfo.GetCustomAttribute <MapToAttribute>(true);
            var nullValueAttribute = propInfo.GetCustomAttribute <NullValueAttribute>(true);
            var identityAttribute  = propInfo.GetCustomAttribute <IdentityAttribute>(true);

            if (!_notMapped)
            {
                if (CSConfig.ColumnMappingOverrideMap.ContainsValue(propInfo.DeclaringType.Name + ":" + propInfo.Name))
                {
                    _mappedColumn =
                        schema.Columns[
                            CSConfig.ColumnMappingOverrideMap[propInfo.DeclaringType.Name + ":" + propInfo.Name]];
                }
                else if (mapToAttribute != null)
                {
                    _mappedColumn = schema.Columns[mapToAttribute.Name];
                }
                else
                {
                    _mappedColumn = schema.Columns[propInfo.Name];
                }

                if (_mappedColumn != null)
                {
                    _mappedColumn.MappedField = this;
                }
            }


            var sequenceAttribute = propInfo.GetCustomAttribute <SequenceAttribute>(true);

            if (sequenceAttribute != null && _schema.DB.SupportsSequences)
            {
                _sequenceName = sequenceAttribute.SequenceName;

                if (_mappedColumn != null && sequenceAttribute.Identity)
                {
                    _mappedColumn.Identity = true;
                    _schema.IdentityColumn = _mappedColumn;
                }
            }

            if (identityAttribute != null)
            {
                if (_mappedColumn != null)
                {
                    _mappedColumn.Identity = true;
                    _schema.IdentityColumn = _mappedColumn;
                }
            }

            if (nullValueAttribute != null)
            {
                _nullValue = nullValueAttribute.NullValue;
            }
            else
            {
                Type fieldType = FieldType;

                if (fieldType == typeof(string))
                {
                    _nullValue = String.Empty;
                }
                else if (fieldType.GetTypeInfo().IsValueType)
                {
                    _nullValue = Activator.CreateInstance(fieldType);
                }
            }

            if (_mappedColumn != null && _mappedColumn.ReadOnly)
            {
                if (_propertyInfo.CanWrite)
                {
                    throw new CSException("Property [" + Name + "] for class [" + _schema.ClassType.Name + "] should be read-only");
                }
            }
        }
Beispiel #6
0
        private bool SaveChildrenBefore()
        {
            foreach (CSFieldValue fieldValue in _fieldData)
            {
                if (fieldValue.SchemaField != null && fieldValue.SchemaField.Relation != null)
                {
                    CSRelation relation = fieldValue.SchemaField.Relation;

                    if (fieldValue.IsDirty)
                    {
                        switch (relation.RelationType)
                        {
                        case CSSchemaRelationType.OneToMany:
                        {
                            break;
                        }

                        case CSSchemaRelationType.ManyToMany:
                        {
                            break;
                        }

                        case CSSchemaRelationType.OneToOne:
                        {
                            goto case CSSchemaRelationType.ManyToOne;
                        }

                        case CSSchemaRelationType.ManyToOne:     // Set local keys to correct values
                        {
                            if (fieldValue.Value == null)
                            {
                                if (_fieldData["#" + relation.LocalKey].SchemaField.MappedColumn.AllowNull)
                                {
                                    _fieldData["#" + relation.LocalKey].Value = null;
                                }
                                else
                                {
                                    throw new CSException("Column [" + fieldValue.SchemaField.MappedColumn.Name + "] cannot be set to null");
                                }
                            }
                            else
                            {
                                CSObject valueObj = (CSObject)fieldValue.Value;

                                if (valueObj.IsNew || valueObj.IsDirty)
                                {
                                    valueObj.Save();
                                }

                                _fieldData["#" + relation.LocalKey].Value = valueObj.Data["#" + relation.ForeignKey].Value;
                            }

                            break;
                        }
                        }
                    }
                    else
                    {
                        if (fieldValue.ValueState != CSFieldValueState.Unread)
                        {
                            if (fieldValue.BaseType.GetTypeInfo().IsSubclassOf(typeof(CSObject)))
                            {
                                CSObject obj = (CSObject)fieldValue.ValueDirect;

                                if (obj != null && !obj.Save())
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }
            }

            return(true);
        }