}//method object GetValue(EntityRecord record, EntityMemberInfo member) { var v = record.GetValueDirect(member); if (v != null) { if (v == DBNull.Value) { return(null); } var rec = (EntityRecord)v; return(rec.EntityInstance); } //retrieve entity var targetPk = EntityKey.CreateSafe(_targetEntity.PrimaryKey, record.PrimaryKey.Values); var targetRec = record.Session.GetRecord(targetPk); if (targetRec == null) { record.SetValueDirect(member, DBNull.Value); return(null); } record.SetValueDirect(member, targetRec); if (targetRec.ByRefUserPermissions == null) { targetRec.ByRefUserPermissions = member.ByRefPermissions; } return(targetRec.EntityInstance); }
/* Disabled for being terribly inefficient for large batches * // identities generated in db are copied back to PK members. We need to propagate them to referencing (child) entities * void EntityEvent_IdentityEntitySubmitted(EntityRecord record, EventArgs args) { * if (record.Status != EntityStatus.New) return; * var recs = record.Session.RecordsChanged; * foreach (var refMember in record.EntityInfo.IncomingReferences) { * var childRecs = recs.Where(r => r.EntityInfo == refMember.Entity); * foreach (var childRec in childRecs) { * var refBackValue = childRec.ValuesTransient[refMember.ValueIndex]; * if (refBackValue == record) * refMember.SetValueRef(childRec, refMember, record.EntityInstance); //this will copy foreign key * }//foreach childRec * } * }//method */ void EntityEvent_HandleNewGuid(EntityRecord record, EventArgs args) { if (record.SuppressAutoValues) { return; } var newGuid = Guid.NewGuid(); record.SetValueDirect(_member, newGuid); }
void EntityEvent_HandleUpdatedCreatedBy(EntityRecord record, EventArgs args) { if (record.SuppressAutoValues) { return; } var userName = record.Session.Context.User.UserName; record.SetValueDirect(_member, userName); }
private long _identityCount; //used for temp values in identity attributes void EntityEvent_NewEntityHandleIdentity(EntityRecord record, EventArgs args) { if (record.SuppressAutoValues) { return; } var newIdValue = System.Threading.Interlocked.Decrement(ref _identityCount); var initValue = Convert.ChangeType(newIdValue, _member.DataType); record.SetValueDirect(_member, initValue); //0, Int32 or Int64 }
void EntityEvent_HandleUpdatedCreatedById(EntityRecord record, EventArgs args) { if (record.SuppressAutoValues) { return; } // Application might be using either Guids for UserIDs or Ints (int32 or Int64); // take this into account if (_member.DataType == typeof(Guid)) { var userId = record.Session.Context.User.UserId; record.SetValueDirect(_member, userId); } else { //UserID is Int (identity); use AltUserId var altUserId = record.Session.Context.User.AltUserId; object userId = _member.DataType == typeof(Int64) ? altUserId : Convert.ChangeType(altUserId, _member.DataType); record.SetValueDirect(_member, userId); } }
public object GetComputedValue(EntityRecord rec, EntityMemberInfo member) { if (_method == null) { return(null); } var value = _method.Invoke(null, new object[] { rec.EntityInstance }); if (this.Persist && rec.Status != EntityStatus.Loaded) { rec.SetValueDirect(member, value); } return(value); }
public object GetComputedValue(EntityRecord record, EntityMemberInfo member) { if (_method == null) { return(null); } var value = _method.Invoke(null, new object[] { record.EntityInstance }); if (Persist) { record.SetValueDirect(member, value); } return(value); }
}//method //Copies PK values into corresponding FK public static void CopyPrimaryKeyToForeign(EntityRecord record, EntityMemberInfo entityRefMember, EntityRecord refTarget) { var refInfo = entityRefMember.ReferenceInfo; var fkMembers = refInfo.FromKey.ExpandedKeyMembers; //If target is null, set all to DbNull if (refTarget == null) { for (int i = 0; i < fkMembers.Count; i++) { record.SetValueDirect(fkMembers[i].Member, DBNull.Value); } return; } //refTarget is not null var pkMembers = refInfo.ToKey.ExpandedKeyMembers; for (int i = 0; i < pkMembers.Count; i++) { //copy value from PK to FK member var value = refTarget.GetValueDirect(pkMembers[i].Member); record.SetValueDirect(fkMembers[i].Member, value); } }//method
void EntityEvent_HandleCreatedUpdatedOnDateTimeOffset(EntityRecord record, EventArgs args) { if (record.SuppressAutoValues) { return; } //Do CreatedOn only if it is new record if (this.Type == AutoType.CreatedOn && record.Status != EntityStatus.New) { return; } var offset = new DateTimeOffset(record.Session.TransactionDateTime); record.SetValueDirect(_member, offset); }
public static void SetSimpleValue(EntityRecord record, EntityMemberInfo member, object value) { if (value == null) { value = DBNull.Value; } var oldValue = record.GetValueDirect(member); if (member.AreValuesEqual(oldValue, value)) { return; } record.SetValueDirect(member, value); if (record.Status == EntityStatus.Loaded) { record.Status = EntityStatus.Modified; } }
void EntityEvent_NewEntityHandleSequence(EntityRecord record, EventArgs args) { if (record.SuppressAutoValues) { return; } object value; if (_sequence.DataType == typeof(int)) { value = record.Session.GetSequenceNextValue <int>(_sequence); } else { value = record.Session.GetSequenceNextValue <long>(_sequence); } record.SetValueDirect(_member, value); }
void EntityEvent_HandleCreatedUpdatedOnDateTime(EntityRecord record, EventArgs args) { if (record.SuppressAutoValues) { return; } //Do CreatedOn only if it is new record if (this.Type == AutoType.CreatedOn && record.Status != EntityStatus.New) { return; } var dateTime = record.Session.TransactionDateTime; if (_member.Flags.IsSet(EntityMemberFlags.Utc)) { dateTime = dateTime.ToUniversalTime(); } record.SetValueDirect(_member, dateTime); }
private void CheckRecordIdentityReferencesBeforeUpdate(EntityRecord record) { var ent = record.EntityInfo; foreach (var refM in ent.RefMembers) { var targetEnt = record.GetValueDirect(refM); if (targetEnt == null) { continue; } var targetRec = EntityHelper.GetRecord(targetEnt); if (targetRec.Status != EntityStatus.New) { continue; } // target should be already saved and must have generated identity value; copy this value var targetPkMember = targetRec.PrimaryKey.KeyInfo.ExpandedKeyMembers[0].Member; var idValue = targetRec.GetValueDirect(targetPkMember); var refFkMember = refM.ReferenceInfo.FromKey.ExpandedKeyMembers[0].Member; record.SetValueDirect(refFkMember, idValue); } }
private void HandleCreatedUpdatedInTransId(EntityRecord record, EventArgs args) { record.SetValueDirect(HostMember, record.Session.NextTransactionId); }
private void AddRecordUpdateToBatch(EntityRecord rec) { CheckCurrentCommand(); var dbCmd = _currentCommand.DbCommand; var argValues = new List <string>(); var cmdInfo = _db.GetDbCommandForSave(rec); foreach (var prmInfo in cmdInfo.Parameters) { var col = prmInfo.SourceColumn; //Check if we need to use already defined parameter; // this happens when we insert parent and child records with parent having identity primary key column if (_updateSet.UsesOutParams && col != null && col.Flags.IsSet(DbColumnFlags.IdentityForeignKey)) { //Find out parameter that returns the identity of the parent record var parentRec = rec.GetValueDirect(col.Member.ForeignKeyOwner) as EntityRecord; if (parentRec != null && parentRec.CustomTag != null) //it must have customTag already - after recs sorting parent should come first, so it must have idSource already in customTag //parentRec has identity PK { var idSource = (IdentitySource)parentRec.CustomTag; if (idSource.BatchCommand == _currentCommand) { argValues.Add(idSource.Parameter.ParameterName); //if it is the same command, just add ref to parameter } else { //different command - create new parameter, and add action to copy param value from source //NOte - reusing param not allowed, so ' dbCmd.Parameters.Add(idSource.Parameter); ' -this does not work - parameters cannot be shared between commands var dbParam = _driver.AddParameter(dbCmd, prmInfo); //override parameter name dbParam.ParameterName = _driver.DynamicSqlParameterPrefix + "P" + (dbCmd.Parameters.Count - 1); argValues.Add(dbParam.ParameterName); idSource.BatchCommand.AddPostAction(() => dbParam.Value = idSource.Parameter.Value); } // Set value of foreign key idSource.BatchCommand.AddPostAction(() => rec.SetValueDirect(col.Member, idSource.Parameter.Value)); continue; //next param } //if parentRec!=null }//if //Get the value, analyze it, see if it is ok to use literal or it's better to put the value into parameter var value = rec.GetValueDirect(col.Member); if (value == null) { value = DBNull.Value; } var conv = prmInfo.TypeInfo.PropertyToColumnConverter; if (value != DBNull.Value && conv != null) { value = conv(value); } if (BatchShouldUseParameterFor(prmInfo, value)) { //create parameter var dbParam = _driver.AddParameter(dbCmd, prmInfo); //override parameter name dbParam.ParameterName = _driver.DynamicSqlParameterPrefix + "P" + (dbCmd.Parameters.Count - 1); dbParam.Value = value; //If it is parameter holding identity returned from stored proc, then save its info in the rec.CustomTag bool isIdentityOut = rec.Status == EntityStatus.New && col.Flags.IsSet(DbColumnFlags.Identity) && dbParam.Direction == ParameterDirection.Output; if (isIdentityOut) { rec.CustomTag = new IdentitySource() { BatchCommand = _currentCommand, Parameter = dbParam } } ; //add reference to parameter in arg list var strArg = dbParam.ParameterName; if (dbParam.Direction != ParameterDirection.Input) { strArg = string.Format(_driver.CommandCallOutParamFormat, strArg); //copy value returned from sp into entity property _currentCommand.AddPostAction(() => rec.SetValueDirect(col.Member, dbParam.Value) ); } argValues.Add(strArg); } else { string argValue; if (value == DBNull.Value) { argValue = "NULL"; } else { argValue = prmInfo.TypeInfo.ToLiteral(value); } argValues.Add(argValue); } // if BatchShouldUseParameterFor } //foreach prm //format method call var strArgs = string.Join(", ", argValues); var strCall = string.Format(_driver.CommandCallFormat, cmdInfo.FullCommandName, strArgs); //append it to sql _sqlBuilder.AppendLine(strCall); }