-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrom_clause.cpp
More file actions
119 lines (96 loc) · 2.66 KB
/
from_clause.cpp
File metadata and controls
119 lines (96 loc) · 2.66 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "stdafx.h"
#include "from_clause.h"
#include <algorithm>
#include <functional>
#include "join_clause.h"
void orm::sql::swap(orm::sql::from_clause &left, orm::sql::from_clause &right)
{
using std::swap;
swap(left._joins, right._joins);
swap(left._mainTable, right._mainTable);
}
orm::sql::from_clause::from_clause()
{
}
orm::sql::from_clause::from_clause(const orm::sql::from_clause &other) :
_mainTable(other._mainTable)
, _joins(other._joins)
{
}
orm::sql::from_clause::from_clause(orm::sql::from_clause &&other) :
orm::sql::from_clause()
{
swap(*this, other);
}
orm::sql::from_clause::~from_clause()
{
}
orm::sql::from_clause &orm::sql::from_clause::operator =(orm::sql::from_clause other)
{
swap(*this, other);
return *this;
}
void orm::sql::from_clause::SetMainTable(const orm::sql::sql_table &mainTable)
{
_mainTable = mainTable;
}
orm::sql::sql_table &orm::sql::from_clause::GetMainTable()
{
return _mainTable;
}
const orm::sql::sql_table &orm::sql::from_clause::GetMainTable() const
{
return _mainTable;
}
// TODO: verify correctness of join
// if the source table is already in the from clause
// if the join is already in the from clause
// if the source column's table matches the source table
// if the destination column's table matches the destination table
void orm::sql::from_clause::AddJoin(const orm::sql::join_clause &join)
{
_joins.push_back(join);
}
std::vector<orm::sql::join_clause> &orm::sql::from_clause::GetJoins()
{
return _joins;
}
const std::vector<orm::sql::join_clause> &orm::sql::from_clause::GetJoins() const
{
return _joins;
}
std::string orm::sql::from_clause::BuildSqlClause() const
{
std::string result;
if ((_mainTable.GetSchema().empty() == false) && (_mainTable.GetTable().empty() == false))
{
result.append("FROM ");
result.append(_mainTable.GetSchema());
result.append(".");
result.append(_mainTable.GetTable());
std::function<void (const orm::sql::join_clause &)> fn = [&result] (const orm::sql::join_clause &join)
{
if (join.IsOuterJoin())
{
result.append(" LEFT OUTER JOIN ");
}
else
{
result.append(" INNER JOIN ");
}
result.append(join.GetDestinationTable().GetSchema());
result.append(".");
result.append(join.GetDestinationTable().GetTable());
result.append(" ON ");
result.append(join.GetDestinationColumn().GetTable());
result.append(".");
result.append(join.GetDestinationColumn().GetColumn());
result.append(" = ");
result.append(join.GetSourceColumn().GetTable());
result.append(".");
result.append(join.GetSourceColumn().GetColumn());
};
std::for_each(_joins.cbegin(), _joins.cend(), fn);
}
return result;
}