Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Reflection;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Table person = new Table("Person");
- Field pPersonID = person.AddField("PersonID", false);
- Field pName = person.AddField("Name", true);
- Field pAge = person.AddField("Age", true);
- Table address = new Table("Address");
- Field aAddressID = address.AddField("AddressID", false);
- Field aStreet = address.AddField("Street", true);
- Field aNumber = address.AddField("Number", true);
- Table personAddress = new Table("PersonAddress");
- Field paPersonID = personAddress.AddField("PersonID", false);
- Field paAddressID = personAddress.AddField("AddressID", false);
- QueryConstructor qc = new QueryConstructor();
- qc.Tables.Add(person);
- qc.Tables.Add(address);
- qc.Tables.Add(personAddress);
- qc.Joins.Add(new Join(pPersonID, paPersonID, JoinType.INNER_JOIN, Operand.Equals));
- qc.Joins.Add(new Join(aAddressID, paAddressID, JoinType.INNER_JOIN, Operand.Equals));
- qc.Conditions.Add(new Where(pName, "Per Person", ConditionType.AND, Operand.Equals));
- qc.Conditions.Add(new Where(aStreet, "Stedvej", ConditionType.AND, Operand.Equals));
- qc.Construct();
- }
- }
- public class QueryConstructor
- {
- public List<Table> Tables;
- public List<Join> Joins;
- public List<Where> Conditions;
- public string Query;
- public QueryConstructor()
- {
- Tables = new List<Table>();
- Joins = new List<Join>();
- Conditions = new List<Where>();
- }
- public void Construct()
- {
- Query = "SELECT ";
- string spacer = "";
- foreach (Table table in Tables)
- {
- foreach (Field field in table.Fields.FindAll(f => f.Show))
- {
- Query += spacer + field.Table.Name + "." + field.Name;
- spacer = ", ";
- }
- }
- Query += " FROM ";
- if (Joins.Count > 0)
- {
- List<Table> joinedTables = new List<Table>();
- for (int i = 0; i < Joins.Count; i++)
- {
- Join join = Joins[i];
- string joinType = Enum.GetName(typeof(JoinType), join.Type).Replace('_', ' ');
- string operand = GetOperandString(join.Operand);
- if (i == 0)
- {
- Query += join.Source.Table.Name;
- joinedTables.Add(join.Source.Table);
- }
- Table table = joinedTables.Contains(join.Source.Table) ? join.Target.Table : join.Source.Table;
- joinedTables.Add(table);
- Query += " " + joinType + " " + table.Name + " ON " + join.Source.Table.Name + "." + join.Source.Name + " " + operand + " " + join.Target.Table.Name + "." + join.Target.Name;
- }
- }
- else
- {
- spacer = "";
- foreach (Table table in Tables)
- {
- Query += spacer + table.Name;
- spacer = ", ";
- }
- }
- if (Conditions.Count > 0)
- {
- Query += " WHERE ";
- for (int i = 0; i < Conditions.Count; i++)
- {
- Where condition = Conditions[i];
- string conditionType = i == 0 ? "" : " " + Enum.GetName(typeof(ConditionType), condition.Type) + " ";
- string operand = GetOperandString(condition.Operand);
- Query += conditionType + condition.Condition.Key.Table.Name + "." + condition.Condition.Key.Name + " " + operand + " '" + condition.Condition.Value + "'";
- for (int j = 0; j < condition.SubConditions.Count; j++)
- {
- }
- }
- }
- }
- private string GetOperandString(Operand operand)
- {
- switch (operand)
- {
- case Operand.Equals:
- return "=";
- case Operand.NotEquals:
- return "<>";
- default:
- return "=";
- }
- }
- }
- public class Table
- {
- public string Name;
- public List<Field> Fields;
- public Table(string name)
- {
- Name = name;
- Fields = new List<Field>();
- }
- public Field AddField(string name, bool show)
- {
- Field field = new Field(name, show, this);
- Fields.Add(field);
- return field;
- }
- }
- public class Field
- {
- public string Name;
- public Table Table;
- public bool Show;
- public Field(string name, bool show, Table table)
- {
- Name = name;
- Show = show;
- Table = table;
- }
- }
- public class Join
- {
- public Field Source;
- public Field Target;
- public JoinType Type;
- public Operand Operand;
- public Join(Field source, Field target, JoinType type, Operand operand)
- {
- Source = source;
- Target = target;
- Operand = operand;
- Type = type;
- }
- }
- public class Where
- {
- public KeyValuePair<Field, string> Condition;
- public List<Where> SubConditions;
- public ConditionType Type;
- public Operand Operand;
- public Where(Field field, string value, ConditionType type, Operand operand)
- {
- Condition = new KeyValuePair<Field, string>(field, value);
- SubConditions = new List<Where>();
- Type = type;
- Operand = operand;
- }
- }
- public enum ConditionType
- {
- AND = 1,
- OR = 2
- }
- public enum JoinType
- {
- INNER_JOIN = 1,
- OUTER_JOIN = 2
- }
- public enum Operand
- {
- Equals = 1,
- NotEquals = 2
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement