175 lines
5.0 KiB
C#
175 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace ChaosBot.Database
|
|
{
|
|
public class QueryBuilderRaw
|
|
{
|
|
private StringBuilder _query;
|
|
private Dictionary<string, object> _parameters;
|
|
private int keyIndex = 0;
|
|
|
|
public QueryBuilderRaw()
|
|
{
|
|
_query = new StringBuilder();
|
|
_parameters = new Dictionary<string, object>();
|
|
}
|
|
|
|
public QueryBuilderRaw Append(string text)
|
|
{
|
|
_query.Append(text);
|
|
if (!text.EndsWith(" "))
|
|
_query.Append(" ");
|
|
return this;
|
|
}
|
|
|
|
public DataTable Run()
|
|
{
|
|
return Controller.RawQuery(_query.ToString(), _parameters);
|
|
}
|
|
|
|
public QueryBuilderRaw Select()
|
|
{
|
|
return Append("SELECT");
|
|
}
|
|
|
|
public QueryBuilderRaw Insert(string alternativeAction = null)
|
|
{
|
|
Append("INSERT");
|
|
if (alternativeAction != null)
|
|
Append(alternativeAction);
|
|
return this;
|
|
}
|
|
|
|
public QueryBuilderRaw Update(string alternativeAction = null)
|
|
{
|
|
Append("UPDATE");
|
|
if (alternativeAction != null)
|
|
Append(alternativeAction);
|
|
return this;
|
|
}
|
|
|
|
public QueryBuilderRaw Delete()
|
|
{
|
|
return Append("DELETE");
|
|
}
|
|
|
|
public QueryBuilderRaw Distinct()
|
|
{
|
|
return Append("DISTINCT");
|
|
}
|
|
|
|
public QueryBuilderRaw Table(string table)
|
|
{
|
|
return Append(table);
|
|
}
|
|
|
|
public QueryBuilderRaw FromTable(string table)
|
|
{
|
|
Append("FROM");
|
|
return Table(table);
|
|
}
|
|
|
|
public QueryBuilderRaw IntoTable(string table)
|
|
{
|
|
Append("INTO");
|
|
return Table(table);
|
|
}
|
|
|
|
public QueryBuilderRaw Columns(List<string> columnList)
|
|
{
|
|
Append("(");
|
|
Append(String.Join(", ", columnList));
|
|
Append(")");
|
|
return this;
|
|
}
|
|
|
|
public QueryBuilderRaw Values(Dictionary<string, object> keyValueList)
|
|
{
|
|
Append("VALUES (");
|
|
List<string> parameterKeys = new List<string>();
|
|
foreach (KeyValuePair<string, object> pair in keyValueList)
|
|
{
|
|
string parameterName = AddParameterAndReturnKey(pair.Key, pair.Value);
|
|
parameterKeys.Add(parameterName);
|
|
}
|
|
Append(string.Join(", ", parameterKeys));
|
|
Append(")");
|
|
return this;
|
|
}
|
|
|
|
public QueryBuilderRaw Set(Dictionary<string, object> keyValueList)
|
|
{
|
|
Append("SET");
|
|
List<string> parameterKeys = new List<string>();
|
|
foreach (KeyValuePair<string, object> pair in keyValueList)
|
|
{
|
|
string parameterName = AddParameterAndReturnKey(pair.Key, pair.Value);
|
|
parameterKeys.Add($"{pair.Key} = {parameterName}");
|
|
}
|
|
Append(string.Join(", ", parameterKeys));
|
|
return this;
|
|
}
|
|
|
|
public QueryBuilderRaw Where(Dictionary<string, FilterValue> whereCondition)
|
|
{
|
|
Append("WHERE");
|
|
List<string> whereConditionList = new List<string>();
|
|
foreach (string key in whereCondition.Keys)
|
|
{
|
|
FilterValue value = whereCondition.GetValueOrDefault(key);
|
|
string parameterName = AddParameterAndReturnKey(key, value.Value);
|
|
whereConditionList.Add($"{key} {value.Comparison ?? "="} {parameterName}");
|
|
}
|
|
Append(string.Join(" AND ", whereConditionList));
|
|
return this;
|
|
}
|
|
|
|
public QueryBuilderRaw GroupBy(List<string> groupByList)
|
|
{
|
|
Append("GROUP BY");
|
|
return Append(string.Join(", ", groupByList));
|
|
}
|
|
|
|
public QueryBuilderRaw OrderBy(List<string> orderKeys)
|
|
{
|
|
Append("ORDER BY");
|
|
return Append(string.Join(", ", orderKeys));
|
|
}
|
|
|
|
public QueryBuilderRaw Limit(int limit)
|
|
{
|
|
return Append($"LIMIT {limit}");
|
|
}
|
|
|
|
public QueryBuilderRaw Limit(int limit, int offset)
|
|
{
|
|
return Append($"LIMIT {limit} OFFSET {offset}");
|
|
}
|
|
|
|
private void AddParameter(string parameterName, object value)
|
|
{
|
|
_parameters.Add(parameterName, value);
|
|
}
|
|
|
|
private string AddParameterAndReturnKey(string key, object value)
|
|
{
|
|
string parameterKey = GetUniqueParameterName(key);
|
|
AddParameter(parameterKey, value);
|
|
return parameterKey;
|
|
}
|
|
|
|
private string GetStaticParameterName(string key)
|
|
{
|
|
return $"@databasevalue{key}";
|
|
}
|
|
|
|
private string GetUniqueParameterName(string key)
|
|
{
|
|
return $"{GetStaticParameterName(key)}_{keyIndex++}";
|
|
}
|
|
}
|
|
} |