Files
XplorePlane/XP.Common/Database/Helpers/SqlBuilderHelper.cs
T

92 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XP.Common.Database.Helpers
{
/// <summary>
/// 通用CRUD SQL构建器(适配SQLite
/// </summary>
public static class SqlBuilderHelper
{
/// <summary>
/// 构建插入SQL
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="columns">列名集合</param>
/// <returns>插入SQL(参数名:@列名)</returns>
public static string BuildInsertSql(string tableName, IEnumerable<string> columns)
{
if (string.IsNullOrEmpty(tableName)) throw new ArgumentNullException(nameof(tableName));
var columnList = columns?.ToList() ?? throw new ArgumentNullException(nameof(columns));
if (columnList.Count == 0) throw new ArgumentException("列名集合不能为空");
var columnsStr = string.Join(", ", columnList);
var paramsStr = string.Join(", ", columnList.Select(c => $"@{c}"));
return $"INSERT INTO {tableName} ({columnsStr}) VALUES ({paramsStr})";
}
/// <summary>
/// 构建更新SQL
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="updateColumns">更新列名</param>
/// <param name="whereColumns">条件列名(如Id</param>
/// <returns>更新SQL</returns>
public static string BuildUpdateSql(string tableName, IEnumerable<string> updateColumns, IEnumerable<string> whereColumns)
{
if (string.IsNullOrEmpty(tableName)) throw new ArgumentNullException(nameof(tableName));
var updateList = updateColumns?.ToList() ?? throw new ArgumentNullException(nameof(updateColumns));
var whereList = whereColumns?.ToList() ?? throw new ArgumentNullException(nameof(whereColumns));
if (updateList.Count == 0) throw new ArgumentException("更新列名集合不能为空");
if (whereList.Count == 0) throw new ArgumentException("条件列名集合不能为空");
var updateStr = string.Join(", ", updateList.Select(c => $"{c}=@{c}"));
var whereStr = string.Join(" AND ", whereList.Select(c => $"{c}=@{c}"));
return $"UPDATE {tableName} SET {updateStr} WHERE {whereStr}";
}
/// <summary>
/// 构建删除SQL
/// </summary>
public static string BuildDeleteSql(string tableName, IEnumerable<string> whereColumns)
{
if (string.IsNullOrEmpty(tableName)) throw new ArgumentNullException(nameof(tableName));
var whereList = whereColumns?.ToList() ?? throw new ArgumentNullException(nameof(whereColumns));
if (whereList.Count == 0) throw new ArgumentException("条件列名集合不能为空");
var whereStr = string.Join(" AND ", whereList.Select(c => $"{c}=@{c}"));
return $"DELETE FROM {tableName} WHERE {whereStr}";
}
/// <summary>
/// 构建查询SQL
/// </summary>
public static string BuildSelectSql(string tableName, IEnumerable<string> columns, IEnumerable<string>? whereColumns = null, string orderBy = "")
{
if (string.IsNullOrEmpty(tableName)) throw new ArgumentNullException(nameof(tableName));
var columnList = columns?.ToList() ?? throw new ArgumentNullException(nameof(columns));
if (columnList.Count == 0) columnList.Add("*");
var columnsStr = string.Join(", ", columnList);
var sql = new StringBuilder($"SELECT {columnsStr} FROM {tableName}");
// 添加WHERE条件
if (whereColumns != null && whereColumns.Any())
{
var whereStr = string.Join(" AND ", whereColumns.Select(c => $"{c}=@{c}"));
sql.Append($" WHERE {whereStr}");
}
// 添加排序
if (!string.IsNullOrEmpty(orderBy))
{
sql.Append($" ORDER BY {orderBy}");
}
return sql.ToString();
}
}
}