@@ -58,6 +58,7 @@ class MySQLQuery {
5858 const std::variant<std::nullptr_t , int , double , std::string>& arg3) {
5959 if (this ->isOperator (arg2)) {
6060 const std::string operator_ = std::get<std::string>(arg2);
61+
6162 if (std::holds_alternative<std::nullptr_t >(arg3)) {
6263 this ->query += column + " IS NULL" ;
6364 return ;
@@ -84,6 +85,7 @@ class MySQLQuery {
8485 }
8586
8687 this ->query += column + " " + operator_ + " " + value;
88+ return ;
8789 }
8890
8991 const std::string value = std::get<std::string>(arg3);
@@ -94,6 +96,7 @@ class MySQLQuery {
9496 }
9597
9698 this ->query += column + " " + operator_ + " " + value;
99+ return ;
97100 }
98101
99102 if (std::holds_alternative<std::nullptr_t >(arg2)) {
@@ -110,6 +113,7 @@ class MySQLQuery {
110113 }
111114
112115 this ->query += column + " = " + value;
116+ return ;
113117 }
114118
115119 if (std::holds_alternative<double >(arg2)) {
@@ -121,17 +125,39 @@ class MySQLQuery {
121125 }
122126
123127 this ->query += column + " = " + value;
128+ return ;
124129 }
125130
126131 const std::string value = std::get<std::string>(arg2);
127132 if (bind) {
128133 this ->query += column + " = ?" ;
129134 this ->bindings .emplace_back (value);
135+ return ;
130136 }
131137
132138 this ->query += column + " = " + value;
133139 }
134140
141+ const bool hasGroupCondition () {
142+ return this ->query .ends_with (" )" );
143+ }
144+
145+ const bool hasCondition () {
146+ std::vector<std::string> tokens;
147+ std::istringstream stream (this ->query );
148+ std::string token;
149+ while (stream >> token) {
150+ tokens.push_back (token);
151+ }
152+
153+ if (tokens.size () < 3 ) return false ;
154+
155+ const std::string& op = tokens[tokens.size () - 2 ];
156+ const std::string& rhs = tokens[tokens.size () - 1 ];
157+ if (rhs == " IS NULL" ) return true ;
158+ return isOperator (op);
159+ }
160+
135161 public:
136162 MySQLQuery () : hasHaving(false ), hasOn(false ), hasWhere(false ) {}
137163
@@ -343,8 +369,7 @@ class MySQLQuery {
343369
344370 MySQLQuery* having (const std::function<void (MySQLQuery*)>& condition) {
345371 if (this ->hasHaving ) {
346- const bool hasCondition = this ->query .ends_with (" )" );
347- if (hasCondition) this ->query += " AND " ;
372+ if (this ->hasGroupCondition ()) this ->query += " AND " ;
348373 } else {
349374 this ->query += " HAVING " ;
350375 this ->hasHaving = true ;
@@ -362,8 +387,7 @@ class MySQLQuery {
362387 const std::variant<std::nullptr_t , int , double , std::string>& arg3 =
363388 nullptr ) {
364389 if (this ->hasHaving ) {
365- const bool hasCondition = this ->query .ends_with (" ?" );
366- if (hasCondition) this ->query += " AND " ;
390+ if (this ->hasCondition ()) this ->query += " AND " ;
367391 } else {
368392 this ->query += " HAVING " ;
369393 this ->hasHaving = true ;
@@ -470,8 +494,7 @@ class MySQLQuery {
470494
471495 MySQLQuery* on (const std::function<void (MySQLQuery*)>& condition) {
472496 if (this ->hasOn ) {
473- const bool hasCondition = this ->query .ends_with (" )" );
474- if (hasCondition) this ->query += " AND " ;
497+ if (this ->hasGroupCondition ()) this ->query += " AND " ;
475498 } else {
476499 this ->query += " ON " ;
477500 this ->hasOn = true ;
@@ -489,8 +512,7 @@ class MySQLQuery {
489512 const std::variant<std::nullptr_t , int , double , std::string>& arg3 =
490513 nullptr ) {
491514 if (this ->hasOn ) {
492- const bool hasCondition = this ->query .ends_with (" ?" );
493- if (hasCondition) this ->query += " AND " ;
515+ if (this ->hasCondition ()) this ->query += " AND " ;
494516 } else {
495517 this ->query += " ON " ;
496518 this ->hasOn = true ;
@@ -513,8 +535,7 @@ class MySQLQuery {
513535
514536 MySQLQuery* orHaving (const std::function<void (MySQLQuery*)>& condition) {
515537 if (this ->hasHaving ) {
516- const bool hasCondition = this ->query .ends_with (" )" );
517- if (hasCondition) this ->query += " OR " ;
538+ if (this ->hasGroupCondition ()) this ->query += " OR " ;
518539 } else {
519540 this ->query += " HAVING " ;
520541 this ->hasHaving = true ;
@@ -532,8 +553,7 @@ class MySQLQuery {
532553 const std::variant<std::nullptr_t , int , double , std::string>& arg3 =
533554 nullptr ) {
534555 if (this ->hasHaving ) {
535- const bool hasCondition = this ->query .ends_with (" ?" );
536- if (hasCondition) this ->query += " OR " ;
556+ if (this ->hasCondition ()) this ->query += " OR " ;
537557 } else {
538558 this ->query += " HAVING " ;
539559 this ->hasHaving = true ;
@@ -545,8 +565,7 @@ class MySQLQuery {
545565
546566 MySQLQuery* orOn (const std::function<void (MySQLQuery*)>& condition) {
547567 if (this ->hasOn ) {
548- const bool hasCondition = this ->query .ends_with (" )" );
549- if (hasCondition) this ->query += " OR " ;
568+ if (this ->hasGroupCondition ()) this ->query += " OR " ;
550569 } else {
551570 this ->query += " ON " ;
552571 this ->hasOn = true ;
@@ -564,8 +583,7 @@ class MySQLQuery {
564583 const std::variant<std::nullptr_t , int , double , std::string>& arg3 =
565584 nullptr ) {
566585 if (this ->hasOn ) {
567- const bool hasCondition = this ->query .ends_with (" ?" );
568- if (hasCondition) this ->query += " OR " ;
586+ if (this ->hasCondition ()) this ->query += " OR " ;
569587 } else {
570588 this ->query += " ON " ;
571589 this ->hasOn = true ;
@@ -577,8 +595,7 @@ class MySQLQuery {
577595
578596 MySQLQuery* orWhere (const std::function<void (MySQLQuery*)>& condition) {
579597 if (this ->hasWhere ) {
580- const bool hasCondition = this ->query .ends_with (" )" );
581- if (hasCondition) this ->query += " OR " ;
598+ if (this ->hasGroupCondition ()) this ->query += " OR " ;
582599 } else {
583600 this ->query += " WHERE " ;
584601 this ->hasWhere = true ;
@@ -596,9 +613,7 @@ class MySQLQuery {
596613 const std::variant<std::nullptr_t , int , double , std::string>& arg3 =
597614 nullptr ) {
598615 if (this ->hasWhere ) {
599- const bool hasCondition =
600- this ->query .ends_with (" ?" ) || this ->query .ends_with (" IS NULL" );
601- if (hasCondition) this ->query += " OR " ;
616+ if (this ->hasCondition ()) this ->query += " OR " ;
602617 } else {
603618 this ->query += " WHERE " ;
604619 this ->hasWhere = true ;
@@ -704,8 +719,7 @@ class MySQLQuery {
704719
705720 MySQLQuery* where (const std::function<void (MySQLQuery*)>& condition) {
706721 if (this ->hasWhere ) {
707- const bool hasCondition = this ->query .ends_with (" )" );
708- if (hasCondition) this ->query += " AND " ;
722+ if (this ->hasGroupCondition ()) this ->query += " AND " ;
709723 } else {
710724 this ->query += " WHERE " ;
711725 this ->hasWhere = true ;
@@ -723,9 +737,7 @@ class MySQLQuery {
723737 const std::variant<std::nullptr_t , int , double , std::string>& arg3 =
724738 nullptr ) {
725739 if (this ->hasWhere ) {
726- const bool hasCondition =
727- this ->query .ends_with (" ?" ) || this ->query .ends_with (" IS NULL" );
728- if (hasCondition) this ->query += " AND " ;
740+ if (this ->hasCondition ()) this ->query += " AND " ;
729741 } else {
730742 this ->query += " WHERE " ;
731743 this ->hasWhere = true ;
0 commit comments