public static string CreateByType(IDbCommand command, BinaryExpression exp, EntityStruct obj) { var type = obj.Key; var tableName = CommonCommandBuilder.GetTableName(type); var cmdBulder = new StringBuilder(string.Format(@" UPDATE [{0}].[{1}].[{2}] ", command.Connection.Database, CommonCommandBuilder.DbSchemaName, tableName)); var nameValuePairs = CommonCommandBuilder.FieldByValueClauseBuilder(type, obj.Value); var parameterizatedNames = nameValuePairs.Select( x => new KeyValuePair <string, string>(x.Key, CommonCommandBuilder.GetParamsFormat(x.Key, x.Value, command.Connection.Database)) ); foreach (var pair in nameValuePairs) { var prop = CommonCommandBuilder.GetPropertyByAttrName(type, pair.Key); var parameter = command.CreateParameter(); parameter.ParameterName = CommonCommandBuilder.GetParamsFormat(pair.Key, pair.Value, command.Connection.Database); parameter.DbType = TypeMap[prop.PropertyType]; parameter.Value = pair.Value; if (!command.Parameters.Contains(parameter.ParameterName)) { command.Parameters.Add(parameter); } } var updateClause = string.Join(",", parameterizatedNames.Select(x => string.Format(" [{0}].[{1}] = {2}", tableName, x.Key, x.Value))); cmdBulder.Append(string.Format(" SET {0} WHERE {1}", updateClause.Trim(','), CommonCommandBuilder.BuildClauseByExpression(command, type, exp))); return(cmdBulder.ToString()); }
public static string Create <T>(IDbCommand command, BinaryExpression exp) where T : class, IEntity, new() { var type = typeof(T); var selectBody = CreateBody(command.Connection.Database, type); return(string.Format("{0} WHERE {1}", selectBody, CommonCommandBuilder.BuildClauseByExpression(command, type, exp))); }
private static string CreateByType(IDbCommand command, BinaryExpression exp, Type type) { var tableName = CommonCommandBuilder.GetTableName(type); var cmdBulder = new StringBuilder( string.Format(@" DELETE FROM [{0}].[{1}].[{2}] ", command.Connection.Database, CommonCommandBuilder.DbSchemaName, tableName)); cmdBulder.Append(string.Format(" WHERE {0}", CommonCommandBuilder.BuildClauseByExpression(command, type, exp))); return(cmdBulder.ToString()); }
private static string CreateByType(IDbCommand command, EntityStruct objPair) { var type = objPair.Key; var tableName = CommonCommandBuilder.GetTableName(type); var cmdBulder = new StringBuilder( string.Format(@" DELETE FROM [{0}].[{1}].[{2}] ", command.Connection.Database, CommonCommandBuilder.DbSchemaName, tableName)); cmdBulder.Append(CommonCommandBuilder.WhereByIdClause(command, type, objPair)); return(cmdBulder.ToString()); }
private static string CreateBody(string dbName, Type type) { var tableName = CommonCommandBuilder.GetTableName(type); var cmdBulder = new StringBuilder(); foreach (var prop in ReflectionWrapper.GetPropertiesByFieldNamesAttrs(type)) { var attrs = ReflectionWrapper.GetFieldNameAttribute(prop); if (attrs.Count == 0) { continue; } cmdBulder.Append(string.Format("[{0}].[{1}],", tableName, attrs.First().Value)); } return(string.Format("SELECT {0} FROM [{1}].[dbo].[{2}] WITH(NOLOCK)", cmdBulder.ToString().Trim(','), dbName, tableName)); }
private static string CreateByType(IDbCommand command, KeyValuePair <Type, ICollection <EntityBase> > objs) { var type = objs.Key; var tableName = CommonCommandBuilder.GetTableName(type); var names = CommonCommandBuilder.FieldByValueClauseBuilder(type, objs.Value.First()).Select(x => x.Key); var insertedFieldNames = string.Join(",", names); var cmdBulder = new StringBuilder( string.Format(@" INSERT INTO [{0}].[{1}].[{2}] ({3}) VALUES ", command.Connection.Database, CommonCommandBuilder.DbSchemaName, tableName, insertedFieldNames.Trim(','))); var valuesBuilder = new StringBuilder(); foreach (var obj in objs.Value) { var nameValuePair = CommonCommandBuilder.FieldByValueClauseBuilder(type, obj); var parameterizatedNames = nameValuePair.Select(x => CommonCommandBuilder.GetParamsFormat(x.Key, x.Value, command.Connection.Database)); var insertedNames = string.Join(",", parameterizatedNames); valuesBuilder.Append(string.Format(" ({0}),", insertedNames.Trim(','))); foreach (var pair in nameValuePair) { var prop = CommonCommandBuilder.GetPropertyByAttrName(type, pair.Key); var parameter = command.CreateParameter(); parameter.ParameterName = CommonCommandBuilder.GetParamsFormat(pair.Key, pair.Value, command.Connection.Database); parameter.DbType = TypeMap[prop.PropertyType]; parameter.Value = pair.Value; if (!command.Parameters.Contains(parameter.ParameterName)) { command.Parameters.Add(parameter); } } } cmdBulder.Append(valuesBuilder.ToString().Trim(',')); return(cmdBulder.ToString()); }
private static string WhereBuilder(PropertyInfo prop, Type type) { var tableName = CommonCommandBuilder.GetTableName(type); return(string.Format("[{0}].[{1}]", tableName, GetFieldValue(prop))); }