Advertisement
Guest User

Dynamic Query Constructor

a guest
Mar 21st, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Reflection;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Table person = new Table("Person");
  15.             Field pPersonID = person.AddField("PersonID", false);
  16.             Field pName = person.AddField("Name", true);
  17.             Field pAge = person.AddField("Age", true);
  18.  
  19.             Table address = new Table("Address");
  20.             Field aAddressID = address.AddField("AddressID", false);
  21.             Field aStreet = address.AddField("Street", true);
  22.             Field aNumber = address.AddField("Number", true);
  23.  
  24.             Table personAddress = new Table("PersonAddress");
  25.             Field paPersonID = personAddress.AddField("PersonID", false);
  26.             Field paAddressID = personAddress.AddField("AddressID", false);
  27.  
  28.             QueryConstructor qc = new QueryConstructor();
  29.             qc.Tables.Add(person);
  30.             qc.Tables.Add(address);
  31.             qc.Tables.Add(personAddress);
  32.  
  33.             qc.Joins.Add(new Join(pPersonID, paPersonID, JoinType.INNER_JOIN, Operand.Equals));
  34.             qc.Joins.Add(new Join(aAddressID, paAddressID, JoinType.INNER_JOIN, Operand.Equals));
  35.  
  36.             qc.Conditions.Add(new Where(pName, "Per Person", ConditionType.AND, Operand.Equals));
  37.             qc.Conditions.Add(new Where(aStreet, "Stedvej", ConditionType.AND, Operand.Equals));
  38.  
  39.             qc.Construct();
  40.     }
  41.     }
  42.  
  43.     public class QueryConstructor
  44.     {
  45.         public List<Table> Tables;
  46.         public List<Join> Joins;
  47.         public List<Where> Conditions;
  48.         public string Query;
  49.  
  50.         public QueryConstructor()
  51.         {
  52.             Tables = new List<Table>();
  53.             Joins = new List<Join>();
  54.             Conditions = new List<Where>();
  55.         }
  56.  
  57.         public void Construct()
  58.         {
  59.             Query = "SELECT ";
  60.             string spacer = "";
  61.  
  62.             foreach (Table table in Tables)
  63.             {
  64.                 foreach (Field field in table.Fields.FindAll(f => f.Show))
  65.                 {
  66.                     Query += spacer + field.Table.Name + "." + field.Name;
  67.                     spacer = ", ";
  68.                 }
  69.             }
  70.  
  71.             Query += " FROM ";
  72.             if (Joins.Count > 0)
  73.             {
  74.                 List<Table> joinedTables = new List<Table>();
  75.                 for (int i = 0; i < Joins.Count; i++)
  76.                 {
  77.                     Join join = Joins[i];
  78.  
  79.                     string joinType = Enum.GetName(typeof(JoinType), join.Type).Replace('_', ' ');
  80.                     string operand = GetOperandString(join.Operand);
  81.  
  82.                     if (i == 0)
  83.                     {
  84.                         Query += join.Source.Table.Name;
  85.                         joinedTables.Add(join.Source.Table);
  86.                     }
  87.  
  88.                     Table table = joinedTables.Contains(join.Source.Table) ? join.Target.Table : join.Source.Table;
  89.                     joinedTables.Add(table);
  90.                     Query += " " + joinType + " " + table.Name + " ON " + join.Source.Table.Name + "." + join.Source.Name + " " + operand + " " + join.Target.Table.Name + "." + join.Target.Name;
  91.                 }
  92.             }
  93.             else
  94.             {
  95.                 spacer = "";
  96.                 foreach (Table table in Tables)
  97.                 {
  98.                     Query += spacer + table.Name;
  99.                     spacer = ", ";
  100.                 }
  101.             }
  102.  
  103.             if (Conditions.Count > 0)
  104.             {
  105.                 Query += " WHERE ";
  106.                 for (int i = 0; i < Conditions.Count; i++)
  107.                 {
  108.                     Where condition = Conditions[i];
  109.  
  110.                     string conditionType = i == 0 ? "" : " " + Enum.GetName(typeof(ConditionType), condition.Type) + " ";
  111.                     string operand = GetOperandString(condition.Operand);
  112.  
  113.                     Query += conditionType + condition.Condition.Key.Table.Name + "." + condition.Condition.Key.Name + " " + operand + " '" + condition.Condition.Value + "'";
  114.  
  115.                     for (int j = 0; j < condition.SubConditions.Count; j++)
  116.                     {
  117.                        
  118.                     }
  119.                 }
  120.             }
  121.         }
  122.  
  123.         private string GetOperandString(Operand operand)
  124.         {
  125.             switch (operand)
  126.             {
  127.                 case Operand.Equals:
  128.                     return "=";
  129.                 case Operand.NotEquals:
  130.                     return "<>";
  131.                 default:
  132.                     return "=";
  133.             }
  134.         }
  135.     }
  136.  
  137.     public class Table
  138.     {
  139.         public string Name;
  140.         public List<Field> Fields;
  141.  
  142.         public Table(string name)
  143.         {
  144.             Name = name;
  145.             Fields = new List<Field>();
  146.         }
  147.  
  148.         public Field AddField(string name, bool show)
  149.         {
  150.             Field field = new Field(name, show, this);
  151.             Fields.Add(field);
  152.  
  153.             return field;
  154.         }
  155.     }
  156.  
  157.     public class Field
  158.     {
  159.         public string Name;
  160.         public Table Table;
  161.         public bool Show;
  162.  
  163.         public Field(string name, bool show, Table table)
  164.         {
  165.             Name = name;
  166.             Show = show;
  167.             Table = table;
  168.         }
  169.     }
  170.  
  171.     public class Join
  172.     {
  173.         public Field Source;
  174.         public Field Target;
  175.         public JoinType Type;
  176.         public Operand Operand;
  177.  
  178.         public Join(Field source, Field target, JoinType type, Operand operand)
  179.         {
  180.             Source = source;
  181.             Target = target;
  182.             Operand = operand;
  183.             Type = type;
  184.         }
  185.     }
  186.  
  187.     public class Where
  188.     {
  189.         public KeyValuePair<Field, string> Condition;
  190.         public List<Where> SubConditions;
  191.         public ConditionType Type;
  192.         public Operand Operand;
  193.  
  194.         public Where(Field field, string value, ConditionType type, Operand operand)
  195.         {
  196.             Condition = new KeyValuePair<Field, string>(field, value);
  197.             SubConditions = new List<Where>();
  198.             Type = type;
  199.             Operand = operand;
  200.         }
  201.     }
  202.  
  203.     public enum ConditionType
  204.     {
  205.         AND = 1,
  206.         OR = 2
  207.     }
  208.  
  209.     public enum JoinType
  210.     {
  211.         INNER_JOIN = 1,
  212.         OUTER_JOIN = 2
  213.     }
  214.  
  215.     public enum Operand
  216.     {
  217.         Equals = 1,
  218.         NotEquals = 2
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement