-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_h.txt
191 lines (171 loc) · 4.06 KB
/
database_h.txt
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#pragma once
#include "stdafx.h"
//
//#include <std::vector>
//#include <list>
//#include <map>
//#include <memory>
//#include <algorithm>
//#include <sql.h>
namespace db
{
using namespace std;
class column;
class table;
class database;
class join;
class server
{
string server_name;
std::vector<std::shared_ptr<database>> databases_;
};
class database
{
string database_name_;
std::shared_ptr<server> parent_;
std::vector<std::shared_ptr<table>> tables_;
public:
database(const string& database_name)
: database_name_(database_name)
{
}
const string& database_name() const
{
return database_name_;
}
const std::vector<std::shared_ptr<table>>& tables() const
{
return tables_;
}
void set_tables(const std::vector<std::shared_ptr<table>>& tables_)
{
this->tables_ = tables_;
}
};
class query
{
string query_name_;
std::shared_ptr<server> parent_;
std::vector<join> joins_;
};
class join
{
std::vector<tuple<column,column>> join_column_pairs_;
};
class table
{
string table_name_;
std::shared_ptr<database> parent_;
std::vector<std::shared_ptr<column>> columns_;
public:
table(const string& table_name, const std::vector<std::shared_ptr<column>>& columns_, const std::shared_ptr<database>& database)
: table_name_(table_name),
columns_(columns_),
parent_(database)
{
}
table(const string& table_name, const std::shared_ptr<database>& database)
: table_name_(table_name),
parent_(database)
{
}
const string& table_name() const
{
return table_name_;
}
const std::vector<std::shared_ptr<column>>& columns() const
{
return columns_;
}
const std::shared_ptr<database>& parent() const
{
return parent_;
}
};
class column
{
string column_name_;
std::shared_ptr<table> parent_;
int precision_;
int scale_;
int max_width_;
bool is_nullable_;
bool is_computed_;
string column_alias_;
public:
column(const string& column_name, int precision, int scale, int max_width, bool is_nullable, const std::shared_ptr<table>& parent)
: column_name_(column_name),
precision_(precision),
scale_(scale),
max_width_(max_width),
is_nullable_(is_nullable),
parent_(parent)
{
}
column(const string& column_name, int precision, int scale, int max_width, bool is_nullable, bool is_candidate_key, int distinct_count, const std::shared_ptr<table>& parent)
: column_name_(column_name),
precision_(precision),
scale_(scale),
max_width_(max_width),
is_nullable_(is_nullable),
parent_(parent)
{
}
const string& column_name() const
{
return column_name_;
}
const int& precision() const
{
return precision_;
}
const int& scale() const
{
return scale_;
}
const int& max_width() const
{
return max_width_;
}
const bool& is_nullable() const
{
return is_nullable_;
}
const std::shared_ptr<table>& parent() const
{
return parent_;
}
};
}
std::vector<std::tuple<std::wstring, std::wstring, std::wstring, std::wstring>> ms_database_builder::execute_schema_query()
{
std::wstring sql(L"\
SELECT\n\
DB_NAME() AS database_name\n\
, (sch.name) AS schema_name\n\
, (tab.name) AS table_name\n\
, (col.name) AS column_name\n\
FROM sys.schemas AS sch\n\
JOIN sys.tables AS tab\n\
ON sch.schema_id = tab.schema_id\n\
JOIN sys.columns as col\n\
ON tab.object_id = col.object_id;\n\
"
);
std::vector<std::tuple<std::wstring, std::wstring, std::wstring, std::wstring>> tmp;
std::vector<std::vector<std::wstring>> rows_of_columns_ = ms_connection_.execute_sql_query(sql);
std::tuple<std::wstring, std::wstring, std::wstring, std::wstring> meta_row;
std::wstring database_name;
std::wstring schema_name;
std::wstring table_name;
std::wstring column_name;
std::for_each(begin(rows_of_columns_), end(rows_of_columns_), [&](auto row) {
database_name = row.at(0);
schema_name = row.at(1);
table_name = row.at(2);
column_name = row.at(3);
meta_row = std::tuple<std::wstring, std::wstring, std::wstring, std::wstring>(database_name, schema_name, table_name, column_name);
tmp.push_back(meta_row);
});
return tmp;
}