-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathData.cs
88 lines (79 loc) · 3.07 KB
/
Data.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//*****************************************************************************
// Data.cs - This is used to hold the data to be queryed.
//*****************************************************************************
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace QEngine
{
public class Data
{
List<object> mRecords = new List<object>();
string mTableName;
/// <summary>
/// Constructor
/// </summary>
/// <param name="tableName">Name of the database.</param>
public Data(string tableName)
{
mTableName = tableName;
}
/// <summary>
/// Represents the first row (Property names) of database.
/// </summary>
public Row Headder { get; set; }
/// <summary>
/// Represents the body of database table.
/// </summary>
public IEnumerable<object> Records
{
get { return mRecords; }
}
/// <summary>
/// Queries the databse based on the given command.
/// </summary>
/// <param name="cmd">Commad to be used for querying.</param>
/// <returns>IQueryable of queried data</returns>
public IQueryable<object> ProcessCommand(string cmd)
{
Regex regEx = new Regex("SELECT (.*) FROM .*");
var match = regEx.Match(cmd);
var selectCondition = match.Groups[1].Value;
regEx = new Regex("WHERE (.*)");
match = regEx.Match(cmd);
var whereCondition = match.Groups[1].Value;
var result = mRecords.AsQueryable<object>().Where(whereCondition, Headder.Values)
.Select(selectCondition, Headder.Values);
return result;
}
/// <summary>
/// Creates and returns a database table which can be queried.
/// </summary>
/// <param name="lines">Rows to be added to database</param>
/// <param name="tableName">name of the database table</param>
/// <returns>Data representing database</returns>
public static Data CreateDB(IEnumerable<string> lines, string tableName)
{
var rows = lines.ToList();
var table = new Data(tableName);
table.Headder = new Row(rows[0]);
// Removing the headder to add the rest as body.
rows.RemoveAt(0);
foreach (var row in rows)
{
// Creating a dynamic class with headder values as properties
// and row entities as values.
var obj = DynTypeBuilder.CreateNewObject(table.Headder.Values);
var type = obj.GetType();
var values = row.Split(',');
for (int i = 0; i < values.Count(); i++)
{
var propertyInfo = type.GetProperty(table.Headder.Values.ElementAt(i));
propertyInfo.SetValue(obj, values.ElementAt(i));
}
table.mRecords.Add(obj);
}
return table;
}
}
}