private static TypeQueryInfo CreateQueryInfo(IDbConnection connection, TypeInfo typeInfo) { TypeQueryInfo typeQueryInfo = new TypeQueryInfo(); ISqlDbHelper sqlHelper = GetSqlHelper(connection); var sb = new StringBuilder(); GenerateColumnEqualsParamSequence(typeInfo.PrimaryKeyProperties, sqlHelper, sb, " and "); var keyEqualsParams = sb.ToString(); // Id1 = @Id1 and Id2 = @Id2 sb.Clear(); GenerateColumnEqualsParamSequence(typeInfo.WritableProperties, sqlHelper, sb, ", "); var updateColumnsEqualsParams = sb.ToString(); // Attr1 = @Attr1, Attr2 = @Attr2 sb.Clear(); GenerateColumnSequence(sb, typeInfo.ReadableProperties, sqlHelper); var readableColumns = sb.ToString(); // Attr1, Attr2 sb.Clear(); GenerateColumnSequence(sb, typeInfo.InsertableProperties, sqlHelper); // Id1, Id2, Attr1, Attr2 var insertColumns = sb.ToString(); sb.Clear(); GenerateColumnSequence(sb, typeInfo.InsertableProperties, sqlHelper, true); // @Id1, @Id2, @Attr1, @Attr2 var insertParams = sb.ToString(); var tableName = sqlHelper.FormatDbEntityName(typeInfo.TableName); typeQueryInfo.ExistsQuery = string.Format(ExistsTemplate, tableName, keyEqualsParams); typeQueryInfo.SelectQuery = string.Format(SelectTemplate, readableColumns, tableName, keyEqualsParams); typeQueryInfo.SelectAllQuery = string.Format(SelectAllTemplate, readableColumns, tableName); typeQueryInfo.SelectCountQuery = string.Format(SelectCountTemplate, tableName); typeQueryInfo.InsertQuery = string.Format(InsertTemplate, tableName, insertColumns, insertParams); typeQueryInfo.InsertNoValuesQuery = string.Format(InsertNoValuesTemplate, tableName, insertColumns); typeQueryInfo.UpdateQuery = string.Format(UpdateTemplate, tableName, updateColumnsEqualsParams, keyEqualsParams); typeQueryInfo.UpdateFieldsQuery = string.Format(UpdateFieldsTemplate, tableName, keyEqualsParams); typeQueryInfo.DeleteQuery = string.Format(DeleteTemplate, tableName, keyEqualsParams); typeQueryInfo.DeleteAllQuery = string.Format(DeleteAllTemplate, tableName); QueryInfos.TryAdd(typeInfo.TypeHandle, typeQueryInfo); return(typeQueryInfo); }
private static void GenerateColumnEqualsParamSequence(IEnumerable <PropertyInfo> properties, ISqlDbHelper sqlHelper, StringBuilder sb, string separator) { for (var i = 0; i < properties.Count(); i++) { var property = properties.ElementAt(i); sb.Append(GetColumnEqualsToParam(property.Name, sqlHelper)); if (i < properties.Count() - 1) { sb.AppendFormat(separator); } } }
private static void GenerateColumnSequence(StringBuilder sb, IEnumerable <PropertyInfo> properties, ISqlDbHelper sqlHelper, bool forParams = false, string paramSufix = "") { for (var i = 0; i < properties.Count(); i++) { var property = properties.ElementAt(i); sb.Append(forParams ? GetParamName(property, paramSufix) : sqlHelper.FormatDbEntityName(property.Name)); if (i < properties.Count() - 1) { sb.AppendFormat(", "); } } }
private static string GetColumnEqualsToParam(string columnName, ISqlDbHelper sqlHelper) { return(string.Format(ColumnEqualsToParamTemplate, sqlHelper.FormatDbEntityName(columnName), columnName)); }