Skip to content

Commit ec3c9f2

Browse files
committed
Prepare class select
1 parent 83582a9 commit ec3c9f2

File tree

3 files changed

+166
-61
lines changed

3 files changed

+166
-61
lines changed

src/tests/test_select.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**********************************************************************************
2+
* MIT License
3+
*
4+
* Copyright (c) 2025-2026 Evgenii Sopov <mrseakg@gmail.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
*all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
* Official Source Code: https://github.com/wsjcpp/wsjcpp-sql-builder
25+
*
26+
***********************************************************************************/
27+
28+
#include <iostream>
29+
#include <wsjcpp_sql_builder.h>
30+
31+
int main() {
32+
WsjcppSqlBuilder2 builder;
33+
builder.selectFrom("TABLE_NAME")
34+
.colum("COL1")
35+
.colum("COL2")
36+
.colum("COL3")
37+
;
38+
builder.hasErrors();
39+
// builder.sql();
40+
41+
return 0;
42+
}

src/wsjcpp_sql_builder.cpp

Lines changed: 78 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,12 @@
2626
***********************************************************************************/
2727

2828
#include "wsjcpp_sql_builder.h"
29+
#include <algorithm>
2930

3031
// ---------------------------------------------------------------------
31-
// WsjcppSqlBuilder2
32-
33-
34-
WsjcppSqlBuilder2 &WsjcppSqlBuilder2::makeSelect(const std::string &sSqlTable) {
35-
m_sTableName = sSqlTable;
36-
m_nSqlType = WsjcppSqlBuilderType::SELECT;
37-
return *this;
38-
}
39-
40-
WsjcppSqlBuilder2 &WsjcppSqlBuilder2::makeInsert(const std::string &sSqlTable) {
41-
m_sTableName = sSqlTable;
42-
m_nSqlType = WsjcppSqlBuilderType::INSERT;
43-
return *this;
44-
}
45-
46-
WsjcppSqlBuilder2 &WsjcppSqlBuilder2::makeUpdate(const std::string &sSqlTable) {
47-
m_sTableName = sSqlTable;
48-
m_nSqlType = WsjcppSqlBuilderType::UPDATE;
49-
return *this;
50-
}
32+
// WsjcppSqlQuery
5133

52-
WsjcppSqlBuilder2 &WsjcppSqlBuilder2::makeDelete(const std::string &sSqlTable) {
53-
m_sTableName = sSqlTable;
54-
m_nSqlType = WsjcppSqlBuilderType::DELETE;
55-
return *this;
56-
}
57-
58-
// ---------------------------------------------------------------------
59-
// WsjcppSqlBuilder
60-
61-
WsjcppSqlBuilder::WsjcppSqlBuilder(WsjcppSqlBuilderType nSqlType,
34+
WsjcppSqlQuery::WsjcppSqlQuery(WsjcppSqlBuilderType nSqlType,
6235
const std::string &sSqlTable) {
6336
m_nSqlType = nSqlType;
6437
m_sSqlTable = sSqlTable;
@@ -80,15 +53,15 @@ WsjcppSqlBuilder::WsjcppSqlBuilder(WsjcppSqlBuilderType nSqlType,
8053
}
8154
}
8255

83-
bool WsjcppSqlBuilder::sel(const std::string &sColumnName) {
56+
bool WsjcppSqlQuery::sel(const std::string &sColumnName) {
8457
if (!checkName(sColumnName)) {
8558
return false;
8659
}
8760
m_sSqlQuery0 += sColumnName + ", ";
8861
return true;
8962
}
9063

91-
bool WsjcppSqlBuilder::add(const std::string &sColumnName,
64+
bool WsjcppSqlQuery::add(const std::string &sColumnName,
9265
const std::string &sValue) {
9366
if (!checkName(sColumnName)) {
9467
return false;
@@ -123,7 +96,7 @@ bool WsjcppSqlBuilder::add(const std::string &sColumnName,
12396
return true;
12497
}
12598

126-
bool WsjcppSqlBuilder::add(const std::string &sColumnName, int nValue) {
99+
bool WsjcppSqlQuery::add(const std::string &sColumnName, int nValue) {
127100
if (!checkName(sColumnName)) {
128101
return false;
129102
}
@@ -141,7 +114,7 @@ bool WsjcppSqlBuilder::add(const std::string &sColumnName, int nValue) {
141114
return true;
142115
}
143116

144-
bool WsjcppSqlBuilder::add(const std::string &sColumnName, long nValue) {
117+
bool WsjcppSqlQuery::add(const std::string &sColumnName, long nValue) {
145118
if (!checkName(sColumnName)) {
146119
return false;
147120
}
@@ -159,7 +132,7 @@ bool WsjcppSqlBuilder::add(const std::string &sColumnName, long nValue) {
159132
return true;
160133
}
161134

162-
bool WsjcppSqlBuilder::where(const std::string &sColumnName,
135+
bool WsjcppSqlQuery::where(const std::string &sColumnName,
163136
const std::string &sValue) {
164137
if (!checkName(sColumnName)) {
165138
return false;
@@ -176,7 +149,7 @@ bool WsjcppSqlBuilder::where(const std::string &sColumnName,
176149
return true;
177150
}
178151

179-
bool WsjcppSqlBuilder::where(const std::string &sColumnName, int nValue) {
152+
bool WsjcppSqlQuery::where(const std::string &sColumnName, int nValue) {
180153
if (!checkName(sColumnName)) {
181154
return false;
182155
}
@@ -191,7 +164,7 @@ bool WsjcppSqlBuilder::where(const std::string &sColumnName, int nValue) {
191164
return true;
192165
}
193166

194-
bool WsjcppSqlBuilder::where(const std::string &sColumnName, long nValue) {
167+
bool WsjcppSqlQuery::where(const std::string &sColumnName, long nValue) {
195168
if (!checkName(sColumnName)) {
196169
return false;
197170
}
@@ -206,7 +179,7 @@ bool WsjcppSqlBuilder::where(const std::string &sColumnName, long nValue) {
206179
return true;
207180
}
208181

209-
std::string WsjcppSqlBuilder::getTextQuery() {
182+
std::string WsjcppSqlQuery::getTextQuery() {
210183
std::string sSqlQuery = "";
211184
size_t size0 = m_sSqlQuery0.size();
212185
size_t size1 = m_sSqlQuery1.size();
@@ -239,11 +212,11 @@ std::string WsjcppSqlBuilder::getTextQuery() {
239212
return sSqlQuery;
240213
}
241214

242-
bool WsjcppSqlBuilder::isValid() { return m_bValid; }
215+
bool WsjcppSqlQuery::isValid() { return m_bValid; }
243216

244-
std::string WsjcppSqlBuilder::getErrorMessage() { return m_sErrorMessage; }
217+
std::string WsjcppSqlQuery::getErrorMessage() { return m_sErrorMessage; }
245218

246-
bool WsjcppSqlBuilder::checkName(const std::string &sColumnName) {
219+
bool WsjcppSqlQuery::checkName(const std::string &sColumnName) {
247220
if (sColumnName.size() < 2) {
248221
m_sErrorMessage =
249222
"Parameter '" + sColumnName + "' must more than 2 characters";
@@ -266,7 +239,7 @@ bool WsjcppSqlBuilder::checkName(const std::string &sColumnName) {
266239
return true;
267240
}
268241

269-
std::string WsjcppSqlBuilder::prepareStringValue(const std::string &sValue) {
242+
std::string WsjcppSqlQuery::prepareStringValue(const std::string &sValue) {
270243
// escaping simbols NUL (ASCII 0), \n, \r, \, ', ", и Control-Z.
271244
std::string sResult;
272245
sResult.reserve(sValue.size() * 2);
@@ -300,16 +273,76 @@ std::string WsjcppSqlBuilder::prepareStringValue(const std::string &sValue) {
300273
// WsjcppSqlBuilderSelect
301274

302275
WsjcppSqlBuilderSelect::WsjcppSqlBuilderSelect(const std::string &sSqlTable)
303-
: WsjcppSqlBuilder(WsjcppSqlBuilderType::SELECT, sSqlTable) {}
276+
: WsjcppSqlQuery(WsjcppSqlBuilderType::SELECT, sSqlTable) {}
304277

305278
// ---------------------------------------------------------------------
306279
// WsjcppSqlBuilderInsert
307280

308281
WsjcppSqlBuilderInsert::WsjcppSqlBuilderInsert(const std::string &sSqlTable)
309-
: WsjcppSqlBuilder(WsjcppSqlBuilderType::INSERT, sSqlTable) {}
282+
: WsjcppSqlQuery(WsjcppSqlBuilderType::INSERT, sSqlTable) {}
310283

311284
// ---------------------------------------------------------------------
312285
// WsjcppSqlBuilderUpdate
313286

314287
WsjcppSqlBuilderUpdate::WsjcppSqlBuilderUpdate(const std::string &sSqlTable)
315-
: WsjcppSqlBuilder(WsjcppSqlBuilderType::UPDATE, sSqlTable) {}
288+
: WsjcppSqlQuery(WsjcppSqlBuilderType::UPDATE, sSqlTable) {}
289+
290+
291+
// ---------------------------------------------------------------------
292+
// WsjcppSqlBuilderUpdate
293+
294+
WsjcppSqlSelect::WsjcppSqlSelect(const std::string &tableName, WsjcppSqlBuilder2 *builder)
295+
: WsjcppSqlQuery(WsjcppSqlBuilderType::SELECT, tableName) {
296+
m_tableName = tableName;
297+
m_builder = builder;
298+
}
299+
300+
WsjcppSqlSelect &WsjcppSqlSelect::colum(const std::string &col) {
301+
auto it = std::find(m_columns.begin(), m_columns.end(), col);
302+
if (it != m_columns.end()) {
303+
m_builder->addError("Column '" + col + "' already added to select");
304+
} else {
305+
m_columns.push_back(col);
306+
}
307+
return *this;
308+
}
309+
310+
WsjcppSqlBuilder2 &WsjcppSqlSelect::compile() {
311+
return *m_builder;
312+
}
313+
314+
// ---------------------------------------------------------------------
315+
// WsjcppSqlBuilder2
316+
317+
WsjcppSqlSelect &WsjcppSqlBuilder2::selectFrom(const std::string &tableName) {
318+
m_tableName = tableName;
319+
m_nSqlType = WsjcppSqlBuilderType::SELECT;
320+
m_queries.push_back(std::make_shared<WsjcppSqlSelect>(m_tableName, this));
321+
return *(WsjcppSqlSelect *)(m_queries[m_queries.size() -1].get());
322+
}
323+
324+
WsjcppSqlBuilder2 &WsjcppSqlBuilder2::makeInsert(const std::string &tableName) {
325+
m_tableName = tableName;
326+
m_nSqlType = WsjcppSqlBuilderType::INSERT;
327+
return *this;
328+
}
329+
330+
WsjcppSqlBuilder2 &WsjcppSqlBuilder2::makeUpdate(const std::string &tableName) {
331+
m_tableName = tableName;
332+
m_nSqlType = WsjcppSqlBuilderType::UPDATE;
333+
return *this;
334+
}
335+
336+
WsjcppSqlBuilder2 &WsjcppSqlBuilder2::makeDelete(const std::string &tableName) {
337+
m_tableName = tableName;
338+
m_nSqlType = WsjcppSqlBuilderType::DELETE;
339+
return *this;
340+
}
341+
342+
bool WsjcppSqlBuilder2::hasErrors() {
343+
return m_errors.size() > 0;
344+
}
345+
346+
void WsjcppSqlBuilder2::addError(const std::string &err) {
347+
m_errors.push_back(err);
348+
}

src/wsjcpp_sql_builder.h

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,16 @@
2929

3030
#include <string>
3131
#include <map>
32+
#include <vector>
33+
#include <memory>
3234

33-
enum class WsjcppSqlBuilderType { SELECT, INSERT, UPDATE, DELETE };
3435

35-
class WsjcppSqlBuilder2 {
36-
public:
37-
WsjcppSqlBuilder2 &makeSelect(const std::string &sSqlTable);
38-
WsjcppSqlBuilder2 &makeInsert(const std::string &sSqlTable);
39-
WsjcppSqlBuilder2 &makeUpdate(const std::string &sSqlTable);
40-
WsjcppSqlBuilder2 &makeDelete(const std::string &sSqlTable);
4136

42-
private:
43-
std::string m_sTableName;
44-
WsjcppSqlBuilderType m_nSqlType;
45-
};
37+
enum class WsjcppSqlBuilderType { SELECT, INSERT, UPDATE, DELETE };
4638

47-
class WsjcppSqlBuilder {
39+
class WsjcppSqlQuery {
4840
public:
49-
WsjcppSqlBuilder(WsjcppSqlBuilderType nSqlType, const std::string &sSqlTable);
41+
WsjcppSqlQuery(WsjcppSqlBuilderType nSqlType, const std::string &sSqlTable);
5042
bool sel(const std::string &sColumnName);
5143
bool add(const std::string &sColumnName, const std::string &sValue);
5244
bool add(const std::string &sColumnName, int nValue);
@@ -74,17 +66,55 @@ class WsjcppSqlBuilder {
7466
std::map<std::string, std::string> m_mapFields;
7567
};
7668

77-
class WsjcppSqlBuilderSelect : public WsjcppSqlBuilder {
69+
class WsjcppSqlBuilderSelect : public WsjcppSqlQuery {
7870
public:
7971
WsjcppSqlBuilderSelect(const std::string &sSqlTable);
8072
};
8173

82-
class WsjcppSqlBuilderInsert : public WsjcppSqlBuilder {
74+
class WsjcppSqlBuilderInsert : public WsjcppSqlQuery {
8375
public:
8476
WsjcppSqlBuilderInsert(const std::string &sSqlTable);
8577
};
8678

87-
class WsjcppSqlBuilderUpdate : public WsjcppSqlBuilder {
79+
class WsjcppSqlBuilderUpdate : public WsjcppSqlQuery {
8880
public:
8981
WsjcppSqlBuilderUpdate(const std::string &sSqlTable);
9082
};
83+
84+
class WsjcppSqlBuilder2;
85+
86+
class WsjcppSqlSelect : public WsjcppSqlQuery {
87+
public:
88+
WsjcppSqlSelect(const std::string &tableName, WsjcppSqlBuilder2 *builder);
89+
WsjcppSqlSelect &colum(const std::string &col);
90+
// TODO where
91+
// TODO group by
92+
// TODO order by
93+
WsjcppSqlBuilder2 &compile();
94+
std::string getSql();
95+
96+
private:
97+
std::string m_tableName;
98+
WsjcppSqlBuilder2 *m_builder;
99+
std::vector<std::string> m_columns;
100+
};
101+
102+
class WsjcppSqlBuilder2 {
103+
public:
104+
WsjcppSqlSelect &selectFrom(const std::string &sSqlTable);
105+
WsjcppSqlBuilder2 &makeInsert(const std::string &sSqlTable);
106+
WsjcppSqlBuilder2 &makeUpdate(const std::string &sSqlTable);
107+
WsjcppSqlBuilder2 &makeDelete(const std::string &sSqlTable);
108+
109+
bool hasErrors();
110+
111+
protected:
112+
friend WsjcppSqlSelect;
113+
void addError(const std::string &err);
114+
115+
private:
116+
std::vector<std::string> m_errors;
117+
std::string m_tableName;
118+
WsjcppSqlBuilderType m_nSqlType;
119+
std::vector<std::shared_ptr<WsjcppSqlQuery>> m_queries;
120+
};

0 commit comments

Comments
 (0)