/// <summary> /// Called after initialization has completed. /// </summary> /// <param name="link"></param> private void Initialized(ChildToParentEntityLink link) { if (link == null) { throw new ArgumentNullException("link"); } // do we have a list? if (this.DataSource != null) { return; } // get the items... if (link.ParentEntityType == null) { throw new InvalidOperationException("link.ParentEntityType is null."); } IEntityPersistence persistence = link.ParentEntityType.Persistence; if (persistence == null) { throw new InvalidOperationException("persistence is null."); } // get... this.DataSource = persistence.GetAll(); }
/// <summary> /// Constructor. /// </summary> internal SqlChildToParentLink(SqlSchema schema, ChildToParentEntityLink entityLink) : base(entityLink.NativeName.Name) { // mbr - 04-10-2007 - force the schema... // _schema = schema; this.SetSchema(schema); _childToParentEntityLink = entityLink; }
internal DtoLink(string name, string jsonName, PropertyInfo dtoProp, ChildToParentEntityLink link) : base(name, jsonName, dtoProp) { if (link == null) { throw new ArgumentNullException("link"); } this.Link = link; }
internal SqlJoin(string alias, JoinType type, ChildToParentEntityLink link) : this(alias, type) { if (link == null) { throw new ArgumentNullException("link"); } // set... _targetMember = link; // et... _targetEntityType = link.ParentEntityType; if (_targetEntityType == null) { throw new InvalidOperationException("_targetEntityType is null."); } // keys... _targetKeyFields = _targetEntityType.GetKeyFields(); if (_targetKeyFields == null) { throw new InvalidOperationException("'_targetKeyFields' is null."); } if (_targetKeyFields.Length == 0) { throw new InvalidOperationException("'_targetKeyFields' is zero-length."); } // get... _sourceFields = link.GetLinkFields(); if (_sourceFields == null) { throw new InvalidOperationException("_sourceFields is null."); } if (_targetKeyFields.Length != _sourceFields.Length) { throw new InvalidOperationException(string.Format("Length mismatch for '_targetKeyFields' and '_sourceFields': {0} cf {1}.", _targetKeyFields.Length, _sourceFields.Length)); } }
/// <summary> /// Initializes the view. /// </summary> /// <param name="member"></param> private void Initializing(ChildToParentEntityLink link) { if (link == null) { throw new ArgumentNullException("link"); } // get the fields... EntityField[] fields = link.GetLinkFields(); if (fields == null) { throw new InvalidOperationException("fields is null."); } this.AllowNullSelection = false; foreach (EntityField field in fields) { if (field.IsNullable()) { this.AllowNullSelection = true; break; } } }
/// <summary> /// Reads the next entity. /// </summary> /// <returns></returns> public object Read() { // assert... if (Reader == null) { throw new ArgumentNullException("Reader"); } if (SelectMap == null) { throw new ArgumentNullException("SelectMap"); } // get... if (this.Reader.Read() == false) { return(null); } // mbr - 04-10-2007 - for c7 - provide a lookahead to create inherited types... object newEntity = this.SelectMap.EntityType.CreateInstance(this); if (newEntity == null) { throw new InvalidOperationException("newEntity is null."); } // get the storage service... IEntityStorage storage = this.SelectMap.EntityType.Storage; if (storage == null) { throw new ArgumentNullException("storage"); } // mbr - 10-10-2007 - case 875 - create a lookup that stores other entities in it... IDictionary joinedEntities = new HybridDictionary(); // setup... storage.BeginInitialize(newEntity); try { // hydrate... foreach (SelectMapField mapField in this.SelectMap.MapFields) { if (mapField.Field == null) { throw new InvalidOperationException("mapField.Field is null."); } // set... object value = this.Reader.GetValue(mapField.ResultOrdinal); //// encrypted? //if (mapField.Field.IsEncrypted) //{ // if (value == null) // value = new EncryptedValue(null, (byte[])null); // else if (value is DBNull) // value = new EncryptedValue(typeof(DBNull), (byte[])null); // else // { // // switch... // switch (mapField.Field.DBType) // { // case DbType.String: // case DbType.StringFixedLength: // case DbType.AnsiString: // case DbType.AnsiStringFixedLength: // value = new EncryptedValue(mapField.Field.Type, (string)value); // break; // default: // throw new NotSupportedException(string.Format("Cannot handle '{0}' ({1}).", mapField.Field.DBType, mapField.Field.DBType.GetType())); // } // } //} // mbr - 2016-02-25 - trim the end of any fixed length string... if ((mapField.Field.DBType == DbType.AnsiStringFixedLength || mapField.Field.DBType == DbType.StringFixedLength) && value is string) { value = ((string)value).TrimEnd(); } // mbr - 10-10-2007 - case 875 - are we from another entity? object toUse = newEntity; IEntityStorage storageToUse = storage; if (mapField.EntityType != this.SelectMap.EntityType) { // entity... toUse = joinedEntities[mapField.EntityType]; if (toUse == null) { // get... toUse = mapField.EntityType.CreateInstance(); if (toUse == null) { throw new InvalidOperationException("toUse is null."); } // initialize it... mapField.EntityType.Storage.BeginInitialize(toUse); // what's the join? if (mapField.Join == null) { throw new InvalidOperationException("mapField.Join is null."); } // check... if (mapField.Join.ChildToParentEntityLink != null) { // find the link on the entity that has the right foreign name... ChildToParentEntityLink toSet = mapField.Join.ChildToParentEntityLink; // foreach(ChildToParentEntityLink link in mapField.EntityType.Links) // { // if(string.Compare(link.NativeName.Name, mapField.Join.ChildToParentEntityLink.NativeName.Name, true, // Cultures.System) == 0) // { // toSet = link; // break; // } // } // set... if (toSet != null) { storage.EntityType.Storage.SetParent(newEntity, toSet, toUse); } else { throw new InvalidOperationException(string.Format("A link matching name '{0}' was not found.", mapField.Join.ChildToParentEntityLink.NativeName.Name)); } } // add... joinedEntities[mapField.EntityType] = toUse; } // storage... storageToUse = EntityType.GetEntityType(toUse, OnNotFound.ThrowException).Storage; if (storageToUse == null) { throw new InvalidOperationException("storageToUse is null."); } } else { toUse = newEntity; } // set... storageToUse.SetValue(toUse, mapField.Field, value, SetValueReason.ReaderLoad); } } finally { // mbr - 02-10-2007 - for c7 - flipped these around to make the AfterLoad event work (plus it storage.ResetIsNew(newEntity); storage.EndInitialize(newEntity); // mbr - 10-10-2007 - case 875 - fixup joined entities... if (joinedEntities.Count > 0) { // walk... foreach (object joinedEntity in joinedEntities.Values) { EntityType et = EntityType.GetEntityType(joinedEntity, OnNotFound.ThrowException); if (et == null) { throw new InvalidOperationException("et is null."); } // storage... et.Storage.ResetIsNew(joinedEntity); et.Storage.EndInitialize(joinedEntity); } } } // return the new entity... return(newEntity); }
/// <summary> /// Creates constraints for the given link. /// </summary> /// <param name="linkName"></param> /// <param name="filterOperator"></param> /// <param name="relatedEntity"></param> /// <returns></returns> private void AddConstraintsForLink(ChildToParentEntityLink link, SqlOperator filterOperator, IList relatedEntities) { if (link == null) { throw new ArgumentNullException("link"); } // check.. if (filterOperator != SqlOperator.EqualTo && filterOperator != SqlOperator.NotEqualTo) { throw new NotSupportedException(string.Format(Cultures.System, "'{0}' is an invalid operator. Only 'EqualTo' and 'NotEqualTo' are supported.", filterOperator)); } // check... if (link.ParentEntityType == null) { throw new ArgumentNullException("link.ParentEntityType"); } // create... IEntityStorage storage = link.ParentEntityType.Storage; if (storage == null) { throw new ArgumentNullException("storage"); } // get the key fields of the related type... EntityField[] foreignKeyFields = link.ParentEntityType.GetKeyFields(); if (foreignKeyFields.Length == 0) { throw new InvalidOperationException(string.Format(Cultures.Exceptions, "Key fields on '{0}' was zero-length.", link.ParentEntityType)); } // get teh fields in this... EntityField[] relatedFields = link.GetLinkFields(); // compare... if (foreignKeyFields.Length != relatedFields.Length) { throw ExceptionHelper.CreateLengthMismatchException("foreignKeyFields", "relatedFields", foreignKeyFields.Length, relatedFields.Length); } // now loop... FilterConstraint lastTopConstraint = null; for (int entityIndex = 0; entityIndex < relatedEntities.Count; entityIndex++) { // get and check... object relatedEntity = relatedEntities[entityIndex]; link.ParentEntityType.AssertIsOfType(relatedEntity); // create a top level constraint... FilterConstraint topConstraint = new FilterConstraint(this.Creator); Add(topConstraint); // set... if (lastTopConstraint != null) { lastTopConstraint.CombineWithNext = SqlCombine.Or; } // new? if (storage.IsNew(relatedEntity) == false && storage.IsDeleted(relatedEntity, false) == false) { // loop... for (int index = 0; index < foreignKeyFields.Length; index++) { // get... object foreignValue = storage.GetValue(relatedEntity, foreignKeyFields[index]); // add a constraint... topConstraint.ChildConstraints.Add(new EntityFieldFilterConstraint(this.Creator, relatedFields[index], filterOperator, foreignValue)); } } // next... lastTopConstraint = topConstraint; } }