@@ -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,40 @@ 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& lhs = tokens[tokens.size () - 3 ];
156+ const std::string& op = tokens[tokens.size () - 2 ];
157+ const std::string& rhs = tokens[tokens.size () - 1 ];
158+
159+ return isOperator (op);
160+ }
161+
135162 public:
136163 MySQLQuery () : hasHaving(false ), hasOn(false ), hasWhere(false ) {}
137164
@@ -343,8 +370,7 @@ class MySQLQuery {
343370
344371 MySQLQuery* having (const std::function<void (MySQLQuery*)>& condition) {
345372 if (this ->hasHaving ) {
346- const bool hasCondition = this ->query .ends_with (" )" );
347- if (hasCondition) this ->query += " AND " ;
373+ if (this ->hasGroupCondition ()) this ->query += " AND " ;
348374 } else {
349375 this ->query += " HAVING " ;
350376 this ->hasHaving = true ;
@@ -362,8 +388,7 @@ class MySQLQuery {
362388 const std::variant<std::nullptr_t , int , double , std::string>& arg3 =
363389 nullptr ) {
364390 if (this ->hasHaving ) {
365- const bool hasCondition = this ->query .ends_with (" ?" );
366- if (hasCondition) this ->query += " AND " ;
391+ if (this ->hasCondition ()) this ->query += " AND " ;
367392 } else {
368393 this ->query += " HAVING " ;
369394 this ->hasHaving = true ;
@@ -470,8 +495,7 @@ class MySQLQuery {
470495
471496 MySQLQuery* on (const std::function<void (MySQLQuery*)>& condition) {
472497 if (this ->hasOn ) {
473- const bool hasCondition = this ->query .ends_with (" )" );
474- if (hasCondition) this ->query += " AND " ;
498+ if (this ->hasGroupCondition ()) this ->query += " AND " ;
475499 } else {
476500 this ->query += " ON " ;
477501 this ->hasOn = true ;
@@ -489,8 +513,7 @@ class MySQLQuery {
489513 const std::variant<std::nullptr_t , int , double , std::string>& arg3 =
490514 nullptr ) {
491515 if (this ->hasOn ) {
492- const bool hasCondition = this ->query .ends_with (" ?" );
493- if (hasCondition) this ->query += " AND " ;
516+ if (this ->hasCondition ()) this ->query += " AND " ;
494517 } else {
495518 this ->query += " ON " ;
496519 this ->hasOn = true ;
@@ -513,8 +536,7 @@ class MySQLQuery {
513536
514537 MySQLQuery* orHaving (const std::function<void (MySQLQuery*)>& condition) {
515538 if (this ->hasHaving ) {
516- const bool hasCondition = this ->query .ends_with (" )" );
517- if (hasCondition) this ->query += " OR " ;
539+ if (this ->hasGroupCondition ()) this ->query += " OR " ;
518540 } else {
519541 this ->query += " HAVING " ;
520542 this ->hasHaving = true ;
@@ -532,8 +554,7 @@ class MySQLQuery {
532554 const std::variant<std::nullptr_t , int , double , std::string>& arg3 =
533555 nullptr ) {
534556 if (this ->hasHaving ) {
535- const bool hasCondition = this ->query .ends_with (" ?" );
536- if (hasCondition) this ->query += " OR " ;
557+ if (this ->hasCondition ()) this ->query += " OR " ;
537558 } else {
538559 this ->query += " HAVING " ;
539560 this ->hasHaving = true ;
@@ -545,8 +566,7 @@ class MySQLQuery {
545566
546567 MySQLQuery* orOn (const std::function<void (MySQLQuery*)>& condition) {
547568 if (this ->hasOn ) {
548- const bool hasCondition = this ->query .ends_with (" )" );
549- if (hasCondition) this ->query += " OR " ;
569+ if (this ->hasGroupCondition ()) this ->query += " OR " ;
550570 } else {
551571 this ->query += " ON " ;
552572 this ->hasOn = true ;
@@ -564,8 +584,7 @@ class MySQLQuery {
564584 const std::variant<std::nullptr_t , int , double , std::string>& arg3 =
565585 nullptr ) {
566586 if (this ->hasOn ) {
567- const bool hasCondition = this ->query .ends_with (" ?" );
568- if (hasCondition) this ->query += " OR " ;
587+ if (this ->hasCondition ()) this ->query += " OR " ;
569588 } else {
570589 this ->query += " ON " ;
571590 this ->hasOn = true ;
@@ -577,8 +596,7 @@ class MySQLQuery {
577596
578597 MySQLQuery* orWhere (const std::function<void (MySQLQuery*)>& condition) {
579598 if (this ->hasWhere ) {
580- const bool hasCondition = this ->query .ends_with (" )" );
581- if (hasCondition) this ->query += " OR " ;
599+ if (this ->hasGroupCondition ()) this ->query += " OR " ;
582600 } else {
583601 this ->query += " WHERE " ;
584602 this ->hasWhere = true ;
@@ -596,9 +614,7 @@ class MySQLQuery {
596614 const std::variant<std::nullptr_t , int , double , std::string>& arg3 =
597615 nullptr ) {
598616 if (this ->hasWhere ) {
599- const bool hasCondition =
600- this ->query .ends_with (" ?" ) || this ->query .ends_with (" IS NULL" );
601- if (hasCondition) this ->query += " OR " ;
617+ if (this ->hasCondition ()) this ->query += " OR " ;
602618 } else {
603619 this ->query += " WHERE " ;
604620 this ->hasWhere = true ;
@@ -704,8 +720,7 @@ class MySQLQuery {
704720
705721 MySQLQuery* where (const std::function<void (MySQLQuery*)>& condition) {
706722 if (this ->hasWhere ) {
707- const bool hasCondition = this ->query .ends_with (" )" );
708- if (hasCondition) this ->query += " AND " ;
723+ if (this ->hasGroupCondition ()) this ->query += " AND " ;
709724 } else {
710725 this ->query += " WHERE " ;
711726 this ->hasWhere = true ;
@@ -723,9 +738,7 @@ class MySQLQuery {
723738 const std::variant<std::nullptr_t , int , double , std::string>& arg3 =
724739 nullptr ) {
725740 if (this ->hasWhere ) {
726- const bool hasCondition =
727- this ->query .ends_with (" ?" ) || this ->query .ends_with (" IS NULL" );
728- if (hasCondition) this ->query += " AND " ;
741+ if (this ->hasCondition ()) this ->query += " AND " ;
729742 } else {
730743 this ->query += " WHERE " ;
731744 this ->hasWhere = true ;
0 commit comments