private static void GenerateParameterTypeDefinitions(Union <StringBuilder, StreamWriter> sBuilder, List <PropertyEntry> handledProperties, int handledPropertyCount, PropertyEntry primaryKey) { for (var i = 0; i < handledPropertyCount; i++) { GenerateParameterTypeString(sBuilder, handledProperties[i], i); sBuilder.Append((i != (handledPropertyCount - 1)) ? "," : ""); } if (primaryKey != null) { sBuilder.Append(","); GenerateParameterTypeString(sBuilder, primaryKey, handledPropertyCount); } sBuilder.Append("'"); }
private static void GenerateParameterValueString(Union <StringBuilder, StreamWriter> sBuilder, PropertyEntry prop, int position) { sBuilder.Append(GenerateParameterName(position)); sBuilder.Append(" = "); if (IsQuoteNeededForProperty(prop.Metadata.SqlServer().ColumnType)) { //quote property sBuilder.Append("'"); sBuilder.Append(prop.CurrentValue.ToString()); sBuilder.Append("'"); } else { //check for binary type (may need to do this for image type as well) if (prop.Metadata.SqlServer().ColumnType == "varbinary(max)") { // This seems hacky, but it works var binaryBuilder = new StringBuilder(); binaryBuilder.Append("0x"); binaryBuilder.Append(BitConverter.ToString((byte[])prop.CurrentValue).Replace("-", "")); sBuilder.Append(binaryBuilder.ToString()); binaryBuilder = null; } else { sBuilder.Append(prop.CurrentValue.ToString()); } } }
private static void GenerateParameters(Union <StringBuilder, StreamWriter> sBuilder, List <PropertyEntry> handledProperties, int handledPropertyCount, PropertyEntry primaryKey) { GenerateParameterTypeDefinitions(sBuilder, handledProperties, handledPropertyCount, primaryKey); sBuilder.Append(", "); //Generate Parameter Values GenerateParameterValues(sBuilder, handledProperties, handledPropertyCount, primaryKey); }
private static void GenerateParameterValues(Union <StringBuilder, StreamWriter> sBuilder, List <PropertyEntry> handledProperties, int handledPropertyCount, PropertyEntry primaryKey) { GenerateParameterValues(sBuilder, handledProperties, handledPropertyCount); if (primaryKey != null) { sBuilder.Append(","); GenerateParameterValue(sBuilder, primaryKey, handledPropertyCount, handledPropertyCount); } }
private static void GenerateParameterValue(Union <StringBuilder, StreamWriter> sBuilder, PropertyEntry prop, int position, int handledPropertyCount) { GenerateParameterValueString(sBuilder, prop, position); // if we're not on the key and we're not on the last property (excluding the key) add a comma if (position != handledPropertyCount) { sBuilder.Append((position != (handledPropertyCount - 1)) ? "," : ""); } }
private static void GenerateDeletes(Union <StringBuilder, StreamWriter> sBuilder, DbContext context, IEnumerable <EntityEntry> entries, SqlServerTypeMapper mapper) { foreach (var entry in entries) { var tableName = GetTableName(entry.Entity.GetType(), context); var lastProperty = entry.Properties.Last(); var primaryKeyProperty = GetPrimaryKeyProperty(entry.Properties); sBuilder.Append("EXECUTE sp_executesql N'DELETE FROM "); sBuilder.Append(tableName); sBuilder.Append(" WHERE "); GenerateColumnName(sBuilder, primaryKeyProperty); sBuilder.Append(" = "); sBuilder.Append(GenerateParameterName(0)); sBuilder.Append("'"); sBuilder.Append(", N'"); GenerateParameterTypeString(sBuilder, primaryKeyProperty, 0); sBuilder.Append("', "); GenerateParameterValueString(sBuilder, primaryKeyProperty, 0); sBuilder.Append(";"); } }
private static void GenerateInserts(Union <StringBuilder, StreamWriter> sBuilder, DbContext context, IEnumerable <EntityEntry> entries, SqlServerTypeMapper mapper) { foreach (var entry in entries) { var tableName = GetTableName(entry.Entity.GetType(), context); var lastProperty = entry.Properties.Last(); sBuilder.Append("EXECUTE sp_executesql N'INSERT INTO "); sBuilder.Append(tableName); sBuilder.Append(" "); sBuilder.Append("("); //We'll need this list later to add only the necesary parameters in order var handledProperties = GetPropertiesToHandle(entry); foreach (var prop in entry.Properties) { //Skip identity columnns and other generated values if (prop.Metadata.ValueGenerated == Microsoft.EntityFrameworkCore.Metadata.ValueGenerated.OnAdd) { continue; } GenerateColumnName(sBuilder, prop); sBuilder.Append((prop.Metadata.Name != lastProperty.Metadata.Name) ? ", ": ""); handledProperties.Add(prop); } sBuilder.Append(") VALUES ("); var handledPropertyCount = handledProperties.Count(); for (var i = 0; i < handledPropertyCount; i++) { var prop = handledProperties[i]; sBuilder.Append(GenerateParameterName(i)); sBuilder.Append((i != (handledPropertyCount - 1)) ? "," : ""); } sBuilder.Append(")'"); //Generate Parameters sBuilder.Append(", N'"); GenerateParameters(sBuilder, handledProperties, handledPropertyCount); sBuilder.Append(";"); } }
private static void GenerateColumnName(Union <StringBuilder, StreamWriter> sBuilder, PropertyEntry property) { sBuilder.Append("["); sBuilder.Append(property.Metadata.SqlServer().ColumnName); sBuilder.Append("]"); }
private static void GenerateParameterTypeString(Union <StringBuilder, StreamWriter> sBuilder, PropertyEntry prop, int position) { sBuilder.Append(GenerateParameterName(position)); sBuilder.Append(" "); sBuilder.Append(prop.Metadata.SqlServer().ColumnType); }
private static void GenerateUpdates(Union <StringBuilder, StreamWriter> sBuilder, DbContext context, IEnumerable <EntityEntry> entries, SqlServerTypeMapper mapper) { foreach (var entry in entries) { var tableName = GetTableName(entry.Entity.GetType(), context); var lastProperty = entry.Properties.Last(); sBuilder.Append("EXECUTE sp_executesql N'UPDATE "); sBuilder.Append(tableName); sBuilder.Append(" SET "); //We'll need this list later to deal with only the necesary parameters in order var handledProperties = GetPropertiesToHandle(entry); PropertyEntry primaryKeyProperty = GetPrimaryKeyProperty(entry.Properties); var handledPropertyCount = handledProperties.Count; for (var i = 0; i < handledPropertyCount; i++) { var prop = handledProperties[i]; GenerateColumnName(sBuilder, prop); sBuilder.Append(" = "); sBuilder.Append(GenerateParameterName(i)); sBuilder.Append((prop.Metadata.Name != lastProperty.Metadata.Name) ? ", " : ""); } sBuilder.Append(" WHERE "); GenerateColumnName(sBuilder, primaryKeyProperty); sBuilder.Append("="); sBuilder.Append(GenerateParameterName(handledPropertyCount)); sBuilder.Append("', N'"); GenerateParameters(sBuilder, handledProperties, handledPropertyCount, primaryKeyProperty); sBuilder.Append(";"); } }