public static object GetSimpleValue(EntityRecord record, EntityMemberInfo member) { var value = record.GetValueDirect(member); if (value == null && record.Status != EntityStatus.New) { record.Reload(); value = record.GetValueDirect(member); } if (value == DBNull.Value) { return(null); } return(value); }
}//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); }
private static bool ReferencesNewIdentity(EntityRecord rec, DbColumnInfo fkCol, out EntityRecord targetRecord) { targetRecord = null; if (!rec.EntityInfo.Flags.IsSet(EntityFlags.ReferencesIdentity)) { return(false); } if (!fkCol.Flags.IsSet(DbColumnFlags.IdentityForeignKey)) { return(false); } var targetRef = rec.GetValueDirect(fkCol.Member.ForeignKeyOwner); if (targetRef == null || targetRef == DBNull.Value) { return(false); } var targetRec = (EntityRecord)targetRef; if (targetRec.Status != EntityStatus.New) { return(false); } targetRecord = targetRec; return(true); }
private void SetCrudCommandParameterValues(DbCommandInfo commandInfo, IDbCommand command, EntityRecord record) { if (record.Status == EntityStatus.Stub) { record.Reload(); } for (int i = 0; i < commandInfo.Parameters.Count; i++) { var prm = (IDbDataParameter)command.Parameters[i]; prm.Value = DBNull.Value; var prmInfo = commandInfo.Parameters[i]; var isInput = prmInfo.Direction == ParameterDirection.Input || prmInfo.Direction == ParameterDirection.InputOutput; if (!isInput) { continue; } var col = prmInfo.SourceColumn; if (col == null || col.Member == null) { continue; } var value = record.GetValueDirect(col.Member); if (value == null) { value = DBNull.Value; } var conv = prmInfo.TypeInfo.PropertyToColumnConverter; if (value != DBNull.Value && conv != null) { value = conv(value); } prm.Value = value; } //for i }
public string FormatColumnValuePlaceHolder(SqlColumnValuePlaceHolder cph, EntityRecord rec) { object memberValue = null; object colValue = null; // If it is output, it must be parameters if (cph.ParamDirection != ParameterDirection.Input) { if (cph.ParamDirection == ParameterDirection.InputOutput) //do we need initial value { memberValue = rec.GetValueDirect(cph.Column.Member); } else { memberValue = cph.Column.Member.DefaultValue; //this is important to setup prm.DbType for output parameter } colValue = cph.Column.Converter.PropertyToColumn(memberValue); return(AddParameter(cph, colValue, rec).ParameterName); } // special case: column references new identity value for record inserted in the same transaction if (_batchMode && CheckReferencesNewIdentity(rec, cph, out string prmName)) { return(prmName); } // get value and check if we can use literal memberValue = rec.GetValueDirect(cph.Column.Member); colValue = cph.Column.Converter.PropertyToColumn(memberValue); if (ShouldUseLiteral(memberValue, cph.Column)) { return(FormatAsLiteral(cph, colValue)); } else { return(AddParameter(cph, colValue, rec).ParameterName); } }
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; } }
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); } }
}//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
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); }