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
302275WsjcppSqlBuilderSelect::WsjcppSqlBuilderSelect (const std::string &sSqlTable )
303- : WsjcppSqlBuilder (WsjcppSqlBuilderType::SELECT, sSqlTable ) {}
276+ : WsjcppSqlQuery (WsjcppSqlBuilderType::SELECT, sSqlTable ) {}
304277
305278// ---------------------------------------------------------------------
306279// WsjcppSqlBuilderInsert
307280
308281WsjcppSqlBuilderInsert::WsjcppSqlBuilderInsert (const std::string &sSqlTable )
309- : WsjcppSqlBuilder (WsjcppSqlBuilderType::INSERT, sSqlTable ) {}
282+ : WsjcppSqlQuery (WsjcppSqlBuilderType::INSERT, sSqlTable ) {}
310283
311284// ---------------------------------------------------------------------
312285// WsjcppSqlBuilderUpdate
313286
314287WsjcppSqlBuilderUpdate::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+ }
0 commit comments