Skip to content

Commit dfd5d03

Browse files
committed
Renamed WsjcppSqlBuilderType to WsjcppSqlQueryType, redesign WsjcppSqlQuery, preparing Insert
1 parent b765785 commit dfd5d03

File tree

2 files changed

+57
-47
lines changed

2 files changed

+57
-47
lines changed

src/wsjcpp_sql_builder.cpp

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,23 @@ std::string WsjcppSqlBuilderHelpers::escapingStringValue(const std::string &sVal
6363
}
6464

6565

66-
WsjcppSqlQuery::WsjcppSqlQuery(WsjcppSqlBuilderType sqlType, const std::string &tableName)
67-
: m_sqlType(sqlType), m_tableName(tableName) {
66+
WsjcppSqlQuery::WsjcppSqlQuery(WsjcppSqlQueryType sqlType, WsjcppSqlBuilder *builder, const std::string &tableName)
67+
: m_sqlType(sqlType), m_builder(builder), m_tableName(tableName) {
6868

6969
}
7070

71-
WsjcppSqlBuilderType WsjcppSqlQuery::sqlType() {
71+
WsjcppSqlQueryType WsjcppSqlQuery::sqlType() {
7272
return m_sqlType;
7373
}
7474

75+
WsjcppSqlBuilder &WsjcppSqlQuery::builder() {
76+
return *m_builder;
77+
}
78+
79+
WsjcppSqlBuilder *WsjcppSqlQuery::builderRawPtr() {
80+
return m_builder;
81+
}
82+
7583
const std::string &WsjcppSqlQuery::tableName() {
7684
return m_tableName;
7785
}
@@ -175,19 +183,17 @@ std::string WsjcppSqlWhereCondition::sql() {
175183
}
176184

177185
// ---------------------------------------------------------------------
178-
// WsjcppSqlBuilderUpdate
186+
// WsjcppSqlSelect
179187

180188
WsjcppSqlSelect::WsjcppSqlSelect(const std::string &tableName, WsjcppSqlBuilder *builder)
181-
: WsjcppSqlQuery(WsjcppSqlBuilderType::SELECT, tableName) {
189+
: WsjcppSqlQuery(WsjcppSqlQueryType::SELECT, builder, tableName) {
182190
// TODO multitype table names with AS
183-
m_tableName = tableName;
184-
m_builder = builder;
185191
}
186192

187193
WsjcppSqlSelect &WsjcppSqlSelect::colum(const std::string &col, const std::string &col_as) {
188194
auto it = std::find(m_columns.begin(), m_columns.end(), col);
189195
if (it != m_columns.end()) {
190-
m_builder->addError("Column '" + col + "' already added to select");
196+
builder().addError("Column '" + col + "' already added to select");
191197
} else {
192198
m_columns.push_back(col);
193199
m_columns_as[col] = col_as;
@@ -197,16 +203,12 @@ WsjcppSqlSelect &WsjcppSqlSelect::colum(const std::string &col, const std::strin
197203

198204
WsjcppSqlWhere<WsjcppSqlSelect> &WsjcppSqlSelect::where() {
199205
if (!m_where) {
200-
m_where = std::make_shared<WsjcppSqlWhere<WsjcppSqlSelect>>(nullptr, m_builder, this);
206+
m_where = std::make_shared<WsjcppSqlWhere<WsjcppSqlSelect>>(nullptr, builderRawPtr(), this);
201207
}
202208

203209
return *(m_where.get());
204210
}
205211

206-
WsjcppSqlBuilder &WsjcppSqlSelect::compile() {
207-
return *m_builder;
208-
}
209-
210212
std::string WsjcppSqlSelect::sql() {
211213
std::string ret = "SELECT ";
212214
// TODO TOP OR LIMIT for different databases
@@ -226,45 +228,59 @@ std::string WsjcppSqlSelect::sql() {
226228
first = false;
227229
}
228230
ret += " FROM ";
229-
ret += m_tableName;
231+
ret += tableName();
230232
}
231233

232234
if (m_where) {
233235
ret += " WHERE " + m_where->sql();
234236
}
235237

236-
// TODO where
237238
// TODO group by
238239
// TODO order by
239240
return ret;
240241
}
241242

243+
// ---------------------------------------------------------------------
244+
// WsjcppSqlInsert
245+
246+
WsjcppSqlInsert::WsjcppSqlInsert(const std::string &tableName, WsjcppSqlBuilder *builder)
247+
: WsjcppSqlQuery(WsjcppSqlQueryType::INSERT, builder, tableName) {
248+
249+
}
250+
251+
252+
std::string WsjcppSqlInsert::sql() {
253+
std::string ret = "INSERT INTO " + tableName();
254+
255+
return ret;
256+
};
257+
242258
// ---------------------------------------------------------------------
243259
// WsjcppSqlBuilder
244260

245261
WsjcppSqlSelect &WsjcppSqlBuilder::selectFrom(const std::string &tableName) {
246262
m_tableName = tableName;
247-
m_nSqlType = WsjcppSqlBuilderType::SELECT;
263+
m_nSqlType = WsjcppSqlQueryType::SELECT;
248264
m_queries.push_back(std::make_shared<WsjcppSqlSelect>(m_tableName, this));
249265
// TODO check must be select last one;
250266
return *(WsjcppSqlSelect *)(m_queries[m_queries.size() -1].get());
251267
}
252268

253269
WsjcppSqlBuilder &WsjcppSqlBuilder::insertInto(const std::string &tableName) {
254270
m_tableName = tableName;
255-
m_nSqlType = WsjcppSqlBuilderType::INSERT;
271+
m_nSqlType = WsjcppSqlQueryType::INSERT;
256272
return *this;
257273
}
258274

259275
// WsjcppSqlBuilder &WsjcppSqlBuilder::makeUpdate(const std::string &tableName) {
260276
// m_tableName = tableName;
261-
// m_nSqlType = WsjcppSqlBuilderType::UPDATE;
277+
// m_nSqlType = WsjcppSqlQueryType::UPDATE;
262278
// return *this;
263279
// }
264280

265281
// WsjcppSqlBuilder &WsjcppSqlBuilder::makeDelete(const std::string &tableName) {
266282
// m_tableName = tableName;
267-
// m_nSqlType = WsjcppSqlBuilderType::DELETE;
283+
// m_nSqlType = WsjcppSqlQueryType::DELETE;
268284
// return *this;
269285
// }
270286

src/wsjcpp_sql_builder.h

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,25 @@ class WsjcppSqlBuilderHelpers {
3737
static std::string escapingStringValue(const std::string &sValue);
3838
};
3939

40-
enum class WsjcppSqlBuilderType { SELECT, INSERT, UPDATE, DELETE };
40+
enum class WsjcppSqlQueryType { SELECT, INSERT, UPDATE, DELETE };
41+
42+
class WsjcppSqlBuilder;
4143

4244
class WsjcppSqlQuery {
4345
public:
44-
WsjcppSqlQuery(WsjcppSqlBuilderType sqlType, const std::string &tableName);
45-
WsjcppSqlBuilderType sqlType();
46+
WsjcppSqlQuery(WsjcppSqlQueryType sqlType, WsjcppSqlBuilder *builder, const std::string &tableName);
47+
WsjcppSqlQueryType sqlType();
48+
WsjcppSqlBuilder &builder();
49+
WsjcppSqlBuilder *builderRawPtr();
4650
const std::string &tableName();
4751
virtual std::string sql() = 0;
4852

4953
private:
50-
WsjcppSqlBuilderType m_sqlType;
54+
WsjcppSqlQueryType m_sqlType;
5155
std::string m_tableName;
56+
WsjcppSqlBuilder *m_builder;
5257
};
5358

54-
class WsjcppSqlBuilder;
55-
5659
enum class WsjcppSqlWhereType { LOGICAL_OPERATOR, CONDITION, SUB_CONDITION };
5760

5861
class WsjcppSqlWhereBase {
@@ -224,45 +227,36 @@ class WsjcppSqlSelect : public WsjcppSqlQuery {
224227
WsjcppSqlWhere<WsjcppSqlSelect> &where();
225228
// TODO group by
226229
// TODO order by
227-
WsjcppSqlBuilder &compile();
228230
virtual std::string sql() override;
229231

230232
private:
231-
std::string m_tableName;
232-
WsjcppSqlBuilder *m_builder;
233233
std::shared_ptr<WsjcppSqlWhere<WsjcppSqlSelect>> m_where;
234234
std::vector<std::string> m_columns;
235235
std::map<std::string, std::string> m_columns_as;
236236
};
237237

238238

239-
// class WsjcppSqlInsert : public WsjcppSqlQuery {
240-
// public:
241-
// WsjcppSqlInsert(const std::string &tableName, WsjcppSqlBuilder *builder);
242-
// WsjcppSqlInsert &colum(const std::string &col, const std::string &col_as = "");
239+
class WsjcppSqlInsert : public WsjcppSqlQuery {
240+
public:
241+
WsjcppSqlInsert(const std::string &tableName, WsjcppSqlBuilder *builder);
242+
// WsjcppSqlInsert &colum(const std::string &col);
243243

244-
// WsjcppSqlWhere<WsjcppSqlInsert> &where();
245-
// // TODO group by
246-
// // TODO order by
247-
// WsjcppSqlBuilder &compile();
248-
// virtual std::string sql() override;
244+
WsjcppSqlBuilder &builder();
245+
virtual std::string sql() override;
249246

250-
// private:
251-
// std::string m_tableName;
252-
// WsjcppSqlBuilder *m_builder;
253-
// std::shared_ptr<WsjcppSqlWhere<WsjcppSqlSelect>> m_where;
254-
// std::vector<std::string> m_columns;
255-
// std::map<std::string, std::string> m_columns_as;
256-
// };
247+
private:
248+
std::vector<std::string> m_columns;
249+
std::map<std::string, std::string> m_columns_as;
250+
};
257251

258252
class WsjcppSqlBuilder {
259253
public:
260254
// TODO begin / end transaction can be added here
261255

262256
WsjcppSqlSelect &selectFrom(const std::string &tableName);
263257
WsjcppSqlBuilder &insertInto(const std::string &tableName);
264-
// WsjcppSqlBuilder &makeUpdate(const std::string &sSqlTable);
265-
// WsjcppSqlBuilder &makeDelete(const std::string &sSqlTable);
258+
// WsjcppSqlBuilder &update(const std::string &sSqlTable);
259+
// WsjcppSqlBuilder &deleteFrom(const std::string &sSqlTable);
266260

267261
bool hasErrors();
268262
std::string sql();
@@ -276,6 +270,6 @@ class WsjcppSqlBuilder {
276270
private:
277271
std::vector<std::string> m_errors;
278272
std::string m_tableName;
279-
WsjcppSqlBuilderType m_nSqlType;
273+
WsjcppSqlQueryType m_nSqlType;
280274
std::vector<std::shared_ptr<WsjcppSqlQuery>> m_queries;
281275
};

0 commit comments

Comments
 (0)