forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindHelper.cs
More file actions
75 lines (63 loc) · 2.77 KB
/
FindHelper.cs
File metadata and controls
75 lines (63 loc) · 2.77 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using Simple.Data.Ado.Schema;
namespace Simple.Data.Ado
{
using System.Linq;
internal class FindHelper
{
private readonly DatabaseSchema _schema;
private readonly ICommandBuilder _commandBuilder;
private readonly IExpressionFormatter _expressionFormatter;
public FindHelper(DatabaseSchema schema)
{
_schema = schema;
_commandBuilder = new CommandBuilder(schema);
_expressionFormatter = new ExpressionFormatter(_commandBuilder, _schema);
}
public ICommandBuilder GetFindByCommand(ObjectName tableName, SimpleExpression criteria)
{
_commandBuilder.Append(GetSelectClause(tableName));
if (criteria != null)
{
_commandBuilder.Append(" ");
_commandBuilder.Append(string.Join(" ", new Joiner(JoinType.Inner, _schema).GetJoinClauses(tableName, criteria)));
_commandBuilder.Append(" where ");
_commandBuilder.Append(_expressionFormatter.Format(criteria));
}
return _commandBuilder;
}
private string GetSelectClause(ObjectName tableName)
{
var table = _schema.FindTable(tableName);
return string.Format("select {0} from {1}", string.Join(", ", table.Columns.Select(c => c.QualifiedName)), table.QualifiedName);
}
}
internal class GetHelper
{
private readonly DatabaseSchema _schema;
private readonly ICommandBuilder _commandBuilder;
public GetHelper(DatabaseSchema schema)
{
_schema = schema;
_commandBuilder = new CommandBuilder(schema);
}
public ICommandBuilder GetCommand(Table table, params object[] keyValues)
{
_commandBuilder.Append(GetSelectClause(table));
var param = _commandBuilder.AddParameter(keyValues[0], table.FindColumn(table.PrimaryKey[0]));
_commandBuilder.Append(string.Format(" where {0} = {1}", _schema.QuoteObjectName(table.PrimaryKey[0]), param.Name));
for (int i = 1; i < table.PrimaryKey.Length; i++)
{
param = _commandBuilder.AddParameter(keyValues[i], table.FindColumn(table.PrimaryKey[i]));
_commandBuilder.Append(string.Format(" and {0} = {1}", _schema.QuoteObjectName(table.PrimaryKey[i]), param.Name));
}
return _commandBuilder;
}
private string GetSelectClause(Table table)
{
return string.Format("select {0} from {1}", string.Join(", ", table.Columns.Select(c => c.QualifiedName)), table.QualifiedName);
}
}
}