0% found this document useful (0 votes)
11 views4 pages

Global 2

Uploaded by

yamash koshofali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Global 2

Uploaded by

yamash koshofali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

public enum FilterType


{
Range,
MultiSelect,
GreaterThan,
LessThan,
Text,
Equal
}

public class FilterCriteria


{
public FilterType FilterType { get; set; }
public List<string> MultiSelectValues { get; set; } = new();
public object Value { get; set; }
public (object Min, object Max) Range { get; set; } = (null, null);
public string Text { get; set; }

public bool IsValid =>


(FilterType == FilterType.MultiSelect && MultiSelectValues.Count > 0) ||
(FilterType == FilterType.Range && Range.Min != null && Range.Max != null)
||
(Value != null) ||
(!string.IsNullOrEmpty(Text));
}

public class TableDefinition


{
public string TableName { get; set; }
public List<string> Columns { get; set; }

public TableDefinition(string tableName, List<string> columns)


{
TableName = tableName;
Columns = columns;
}
}

public static class QueryBuilder


{
public static string BuildDynamicQuery(
List<TableDefinition> tableDefinitions,
Dictionary<string, FilterCriteria> filters,
string groupByClause,
string orderByClause
)
{
// ‫بناء الرموز الديناميكية‬
var selectColumns = tableDefinitions.ToDictionary(td =>
td.TableName.Substring(0, 2).ToUpper(), td => td.Columns);

var queryBuilder = new StringBuilder();

// ‫ إضافة جملة‬SELECT
var columns = new List<string>();
foreach (var entry in selectColumns)
{
columns.AddRange(entry.Value.Select(column => $"{entry.Key}.
{column}"));
}
queryBuilder.AppendLine($"SELECT {string.Join(", ", columns)}");

// ‫إضافة الجداول‬
queryBuilder.AppendLine($"FROM {string.Join(", ",
tableDefinitions.Select(td => $"{td.TableName} AS {td.TableName.Substring(0,
2).ToUpper()}"))}");

// ‫إضافة الفالتر‬
if (filters != null && filters.Any())
{
var conditions = new List<string>();
foreach (var filter in filters)
{
switch (filter.Value.FilterType)
{
case FilterType.Range:
if (filter.Value.Range.Min != null &&
filter.Value.Range.Max != null)
{
conditions.Add($"{filter.Key} BETWEEN
{FilterValue(filter.Value.Range.Min)} AND {FilterValue(filter.Value.Range.Max)}");
}
break;

case FilterType.MultiSelect:
if (filter.Value.MultiSelectValues != null &&
filter.Value.MultiSelectValues.Any())
{
var values = string.Join(", ",
filter.Value.MultiSelectValues);
conditions.Add($"{filter.Key} IN ({values})");
}
break;

case FilterType.GreaterThan:
if (filter.Value.Value != null)
{
conditions.Add($"{filter.Key} >
{FilterValue(filter.Value.Value)}");
}
break;

case FilterType.LessThan:
if (filter.Value.Value != null)
{
conditions.Add($"{filter.Key} <
{FilterValue(filter.Value.Value)}");
}
break;

case FilterType.Equal:
if (filter.Value.Value != null)
{
conditions.Add($"{filter.Key} =
{FilterValue(filter.Value.Value)}");
}
break;

case FilterType.Text:
if (!string.IsNullOrEmpty(filter.Value.Text))
{
conditions.Add($"{filter.Key} LIKE '%
{filter.Value.Text}%'");
}
break;
}
}

if (conditions.Any())
{
queryBuilder.AppendLine("WHERE " + string.Join(" AND ",
conditions));
}
}

// ‫ إضافة جملة‬GROUP BY ‫إذا كانت موجودة‬


if (!string.IsNullOrEmpty(groupByClause))
{
queryBuilder.AppendLine($"GROUP BY {groupByClause}");
}

// ‫ إضافة جملة‬ORDER BY ‫إذا كانت موجودة‬


if (!string.IsNullOrEmpty(orderByClause))
{
queryBuilder.AppendLine($"ORDER BY {orderByClause}");
}

return queryBuilder.ToString();
}

// ‫دالة لتحويل القيم إلى صيغة صحيحة لالستخدام في االستعالم‬


private static object FilterValue(object value)
{
if (value is string)
{
return $"\"{value}\"";
}
else if (value is DateTime date)
{
return $"'{date:yyyy-MM-dd}'";
}
return value.ToString();
}
}

// ‫مثال على االستخدام‬


public class Program
{
public static void Main()
{
var tableDefinitions = new List<TableDefinition>
{
new TableDefinition("AccountMove", new List<string> { "Id", "Number",
"Date", "Ref", "Statement", "AccountMoveDocumentTypeId" }),
new TableDefinition("AccountMoveType", new List<string> { "Name" }),
new TableDefinition("Account", new List<string> { "Number", "Name" }),
new TableDefinition("AnalyticAccount", new List<string> { "Number",
"Name" }),
new TableDefinition("Currency", new List<string> { "Name" }),
new TableDefinition("TempAccountMoveLine", new List<string>
{ "Statement", "Debit", "Credit" }),
new TableDefinition("Activity", new List<string> { "Number" }),
new TableDefinition("Representative", new List<string> { "Number" }),
new TableDefinition("Collector", new List<string> { "Number" })
};

var filters = new Dictionary<string, FilterCriteria>


{
{ "AccountMove.Number", new FilterCriteria { FilterType =
FilterType.Equal, Value = "123" } },
{ "TempAccountMoveLine.Debit", new FilterCriteria { FilterType =
FilterType.GreaterThan, Value = 100 } }
};

var query = QueryBuilder.BuildDynamicQuery(tableDefinitions, filters, null,


"AccountMove.Date DESC");
Console.WriteLine(query);
}
}

You might also like