internal static string GetLocalToDbSchemaMapping(this DbClassInfoCache type, string name) { if (IsAnonymousType(type)) { return(name); } return(type.SchemaMappingLocalToDatabase(name)); }
/// <summary> /// Create a Update statement that will update the entry in the Database /// </summary> /// <param name="database">The database.</param> /// <param name="classInfo">The class information.</param> /// <param name="entry">The entry.</param> /// <returns></returns> /// <exception cref="Exception">No primarykey Provied. An autogenerated Update statement could not be created</exception> public static IDbCommand CreateUpdate(IDatabase database, DbClassInfoCache classInfo, object entry) { var pkProperty = classInfo.PrimaryKeyProperty; if (pkProperty == null) { throw new Exception("No primarykey Provied. An autogenerated Update statement could not be created"); } var pk = classInfo.SchemaMappingLocalToDatabase(pkProperty.PropertyName); var identityInsert = DbIdentityInsertScope.Current != null; var ignore = classInfo .Propertys .Select(f => f.Value) .Where(s => (!identityInsert && s.PrimaryKeyAttribute != null) || s.InsertIgnore || s.UpdateIgnore || s.ForginKeyAttribute != null) .Select(s => s.DbName) .ToArray(); if (identityInsert) { DbIdentityInsertScope.Current.EnableIdentityModfiy(classInfo.TableName, database); } var propertyInfos = classInfo.FilterDbSchemaMapping(ignore).ToArray(); var sb = new StringBuilder(); sb.Append("UPDATE "); sb.Append(classInfo.TableName); sb.Append(" SET "); var para = new List <IQueryParameter>(); for (var index = 0; index < propertyInfos.Length; index++) { var info = propertyInfos[index]; var schemaName = classInfo.GetDbToLocalSchemaMapping(info); DbPropertyInfoCache property; classInfo.Propertys.TryGetValue(schemaName, out property); var dataValue = DataConverterExtensions.GetDataValue(property.GetConvertedValue(entry)); para.Add(new QueryParameter(index.ToString(), dataValue, property.PropertyType)); sb.Append(string.Format(" {0} = @{1} ", info, index)); if (index + 1 < propertyInfos.Length) { sb.Append(","); } } para.Add(new QueryParameter("pkValue", pkProperty.Getter.Invoke(entry), pkProperty.PropertyType)); sb.Append(string.Format("WHERE {0} = @pkValue ", pk)); return(database.CreateCommandWithParameterValues(sb.ToString(), para.ToArray())); }