File tree Expand file tree Collapse file tree 3 files changed +65
-3
lines changed
Expand file tree Collapse file tree 3 files changed +65
-3
lines changed Original file line number Diff line number Diff line change @@ -35,8 +35,24 @@ int main() {
3535 .colum (" COL2" )
3636 .colum (" COL3" )
3737 ;
38- builder.hasErrors ();
39- // builder.sql();
38+ if (builder.hasErrors ()) {
39+ return -1 ;
40+ }
41+ std::string sqlQuery = builder.sql ();
42+ std::string sqlQueryExpected = " SELECT COL1, COL2, COL3 FROM TABLE_NAME" ;
43+ if (sqlQuery != sqlQueryExpected) {
44+ std::cerr
45+ << " Expected:" << std::endl
46+ << " " << sqlQueryExpected << std::endl
47+ << " , but got:" << std::endl
48+ << " " << sqlQuery << std::endl
49+ ;
50+ return -1 ;
51+ }
52+
53+ builder.clear ();
54+
55+
4056
4157 return 0 ;
4258}
Original file line number Diff line number Diff line change @@ -311,6 +311,26 @@ WsjcppSqlBuilder2 &WsjcppSqlSelect::compile() {
311311 return *m_builder;
312312}
313313
314+ std::string WsjcppSqlSelect::sql () {
315+ std::string ret = " SELECT " ;
316+ // TODO TOP OR LINIT for different databases
317+ bool first = true ;
318+ for (auto col : m_columns) {
319+ // TODO and 'AS'
320+ if (!first) {
321+ ret += " , " ;
322+ }
323+ ret += col;
324+ first = false ;
325+ }
326+ ret += " FROM " ;
327+ ret += m_tableName;
328+ // TODO where
329+ // TODO group by
330+ // TODO order by
331+ return ret;
332+ }
333+
314334// ---------------------------------------------------------------------
315335// WsjcppSqlBuilder2
316336
@@ -345,4 +365,19 @@ bool WsjcppSqlBuilder2::hasErrors() {
345365
346366void WsjcppSqlBuilder2::addError (const std::string &err) {
347367 m_errors.push_back (err);
368+ }
369+
370+ std::string WsjcppSqlBuilder2::sql () {
371+ std::string ret = " " ;
372+ for (auto query : m_queries) {
373+ if (ret.size () > 0 ) {
374+ ret += " \n " ;
375+ }
376+ ret += query->sql ();
377+ }
378+ return ret;
379+ }
380+
381+ void WsjcppSqlBuilder2::clear () {
382+ m_queries.clear ();
348383}
Original file line number Diff line number Diff line change @@ -51,6 +51,8 @@ class WsjcppSqlQuery {
5151 bool isValid ();
5252 std::string getErrorMessage ();
5353
54+ virtual std::string sql () { return " " ; }; // TODO = 0;
55+
5456private:
5557 std::string prepareStringValue (const std::string &sValue );
5658 bool checkName (const std::string &sColumnName );
@@ -83,6 +85,13 @@ class WsjcppSqlBuilderUpdate : public WsjcppSqlQuery {
8385
8486class WsjcppSqlBuilder2 ;
8587
88+ // class WsjcppSqlWhere {
89+ // public:
90+ // WsjcppSqlWhere();
91+
92+ // };
93+
94+
8695class WsjcppSqlSelect : public WsjcppSqlQuery {
8796public:
8897 WsjcppSqlSelect (const std::string &tableName, WsjcppSqlBuilder2 *builder);
@@ -91,7 +100,7 @@ class WsjcppSqlSelect : public WsjcppSqlQuery {
91100 // TODO group by
92101 // TODO order by
93102 WsjcppSqlBuilder2 &compile ();
94- std::string getSql () ;
103+ virtual std::string sql () override ;
95104
96105private:
97106 std::string m_tableName;
@@ -107,6 +116,8 @@ class WsjcppSqlBuilder2 {
107116 WsjcppSqlBuilder2 &makeDelete (const std::string &sSqlTable );
108117
109118 bool hasErrors ();
119+ std::string sql ();
120+ void clear ();
110121
111122protected:
112123 friend WsjcppSqlSelect;
You can’t perform that action at this time.
0 commit comments