Skip to content

Commit 2637d63

Browse files
committed
Added columns 'AS' to select
1 parent a7e1801 commit 2637d63

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/tests/test_select.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030

3131
int main() {
3232
WsjcppSqlBuilder2 builder;
33-
builder.selectFrom("TABLE_NAME")
34-
.colum("COL1")
35-
.colum("COL2")
36-
.colum("COL3")
33+
builder.selectFrom("table1")
34+
.colum("col1")
35+
.colum("col2", "c3")
36+
.colum("col3")
3737
;
3838
if (builder.hasErrors()) {
3939
return -1;
4040
}
4141
std::string sqlQuery = builder.sql();
42-
std::string sqlQueryExpected = "SELECT COL1, COL2, COL3 FROM TABLE_NAME";
42+
std::string sqlQueryExpected = "SELECT col1, col2 AS c3, col3 FROM table1";
4343
if (sqlQuery != sqlQueryExpected) {
4444
std::cerr
4545
<< "Expected:" << std::endl
@@ -52,7 +52,7 @@ int main() {
5252

5353
builder.clear();
5454

55-
55+
5656

5757
return 0;
5858
}

src/wsjcpp_sql_builder.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,13 @@ WsjcppSqlSelect::WsjcppSqlSelect(const std::string &tableName, WsjcppSqlBuilder2
297297
m_builder = builder;
298298
}
299299

300-
WsjcppSqlSelect &WsjcppSqlSelect::colum(const std::string &col) {
300+
WsjcppSqlSelect &WsjcppSqlSelect::colum(const std::string &col, const std::string &col_as) {
301301
auto it = std::find(m_columns.begin(), m_columns.end(), col);
302302
if (it != m_columns.end()) {
303303
m_builder->addError("Column '" + col + "' already added to select");
304304
} else {
305305
m_columns.push_back(col);
306+
m_columns_as[col] = col_as;
306307
}
307308
return *this;
308309
}
@@ -313,14 +314,16 @@ WsjcppSqlBuilder2 &WsjcppSqlSelect::compile() {
313314

314315
std::string WsjcppSqlSelect::sql() {
315316
std::string ret = "SELECT ";
316-
// TODO TOP OR LINIT for different databases
317+
// TODO TOP OR LIMIT for different databases
317318
bool first = true;
318319
for (auto col : m_columns) {
319-
// TODO and 'AS'
320320
if (!first) {
321321
ret += ", ";
322322
}
323323
ret += col;
324+
if (m_columns_as[col] != "") {
325+
ret += " AS " + m_columns_as[col];
326+
}
324327
first = false;
325328
}
326329
ret += " FROM ";

src/wsjcpp_sql_builder.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,19 @@ class WsjcppSqlBuilderUpdate : public WsjcppSqlQuery {
8585

8686
class WsjcppSqlBuilder2;
8787

88-
// class WsjcppSqlWhere {
89-
// public:
90-
// WsjcppSqlWhere();
88+
class WsjcppSqlWhere {
89+
public:
90+
WsjcppSqlWhere();
91+
private:
9192

92-
// };
93+
};
9394

9495

9596
class WsjcppSqlSelect : public WsjcppSqlQuery {
9697
public:
9798
WsjcppSqlSelect(const std::string &tableName, WsjcppSqlBuilder2 *builder);
98-
WsjcppSqlSelect &colum(const std::string &col);
99+
WsjcppSqlSelect &colum(const std::string &col, const std::string &col_as = "");
100+
99101
// TODO where
100102
// TODO group by
101103
// TODO order by
@@ -106,6 +108,7 @@ class WsjcppSqlSelect : public WsjcppSqlQuery {
106108
std::string m_tableName;
107109
WsjcppSqlBuilder2 *m_builder;
108110
std::vector<std::string> m_columns;
111+
std::map<std::string, std::string> m_columns_as;
109112
};
110113

111114
class WsjcppSqlBuilder2 {

0 commit comments

Comments
 (0)