/// <summary> /// Constructs the actual query /// </summary> /// <returns>Delete query</returns> public string Build() { StringBuilder query = new StringBuilder(); if (_between) { var pk = SQLUtil.GetFields <T>().Single(f => f.Item2 == SQLUtil.GetFirstPrimaryKey <T>()); query.Append("DELETE FROM "); query.Append(SQLUtil.GetTableName <T>()); query.Append(" WHERE "); query.Append(pk.Item1); query.Append(" BETWEEN "); query.Append(_valuesBetweenDouble.Item1); query.Append(" AND "); query.Append(_valuesBetweenDouble.Item2); query.Append(";"); } else { query.Append("DELETE FROM "); query.Append(SQLUtil.GetTableName <T>()); query.Append(" WHERE "); query.Append(new SQLWhere <T>(_rows, true).Build()); query.Append(";"); } query.Append(Environment.NewLine); return(query.ToString()); }
public string Build() { if (_conditions == null || _conditions.Count == 0) { return(string.Empty); } StringBuilder whereClause = new StringBuilder(); if (_onlyPrimaryKeys && _conditions.GetPrimaryKeyCount() == 1) { var field = SQLUtil.GetFields <T>().Single(f => f.Item2 == SQLUtil.GetFirstPrimaryKey <T>()); whereClause.Append(field.Item1); if (_conditions.Count == 1) { whereClause.Append("="); whereClause.Append(field.Item2.GetValue(_conditions.First().Data)); } else { whereClause.Append(" IN ("); foreach (Row <T> condition in _conditions) { object value = field.Item2.GetValue(condition.Data); whereClause.Append(SQLUtil.ToSQLValue(value)); if (!string.IsNullOrEmpty(condition.Comment)) { whereClause.Append(" /*" + condition.Comment + "*/"); } whereClause.Append(SQLUtil.CommaSeparator); } whereClause.Remove(whereClause.Length - SQLUtil.CommaSeparator.Length, SQLUtil.CommaSeparator.Length); // remove last ", " whereClause.Append(")"); } } else { foreach (Row <T> condition in _conditions) { whereClause.Append("("); foreach (var field in SQLUtil.GetFields <T>()) { object value = field.Item2.GetValue(condition.Data); if (value == null || (_onlyPrimaryKeys && field.Item3.Any(a => !a.IsPrimaryKey))) { continue; } whereClause.Append(field.Item1); whereClause.Append("="); whereClause.Append(SQLUtil.ToSQLValue(value)); whereClause.Append(" AND "); } whereClause.Remove(whereClause.Length - 5, 5); // remove last " AND " whereClause.Append(")"); whereClause.Append(" OR "); } whereClause.Remove(whereClause.Length - 4, 4); // remove last " OR "; } return(whereClause.ToString()); }