1+ /* *********************************************************************************
2+ * MIT License
3+ *
4+ * Copyright (c) 2025-2026 Evgenii Sopov <mrseakg@gmail.com>
5+ *
6+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7+ * of this software and associated documentation files (the "Software"), to deal
8+ * in the Software without restriction, including without limitation the rights
9+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ * copies of the Software, and to permit persons to whom the Software is
11+ * furnished to do so, subject to the following conditions:
12+ *
13+ * The above copyright notice and this permission notice shall be included in
14+ *all copies or substantial portions of the Software.
15+ *
16+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ * SOFTWARE.
23+ *
24+ * Official Source Code: https://github.com/wsjcpp/wsjcpp-sql-builder
25+ *
26+ ***********************************************************************************/
27+
28+ #include < iostream>
29+ #include < wsjcpp_sql_builder.h>
30+
31+ int main () {
32+ wsjcpp::SqlBuilder builder;
33+ builder.selectFrom (" table1" )
34+ .colum (" col1" )
35+ .colum (" col2" , " c3" )
36+ .colum (" col3" )
37+ .colum (" col4" )
38+ .where ()
39+ .equal (" col1" , " 1" )
40+ .or_ ()
41+ .notEqual (" col2" , " 2" )
42+ .or_ ()
43+ .subCondition ()
44+ .equal (" c3" , " 4" )
45+ // .and_() // be default must be added and
46+ .equal (" col2" , " 5" )
47+ .finishSubCondition ()
48+ .or_ ()
49+ .lessThen (" col4" , 111 )
50+ .endWhere () // need only for groupBy havingBy and etc
51+ ;
52+ if (builder.hasErrors ()) {
53+ std::cerr << " Select builder has some errors" << std::endl;
54+ return -1 ;
55+ }
56+ std::string sqlQuery = builder.sql ();
57+ std::string sqlQueryExpected =
58+ " SELECT col1, col2 AS c3, col3, col4 "
59+ " FROM table1 "
60+ " WHERE col1 = '1' OR col2 <> '2' OR (c3 = '4' AND col2 = '5') OR col4 < 111" ;
61+ if (sqlQuery != sqlQueryExpected) {
62+ std::cerr
63+ << " Expected:" << std::endl
64+ << " " << sqlQueryExpected << std::endl
65+ << " , but got:" << std::endl
66+ << " " << sqlQuery << std::endl
67+ ;
68+ return -1 ;
69+ }
70+
71+ builder.clear ();
72+ sqlQuery = builder.sql ();
73+ if (sqlQuery != " " ) {
74+ std::cerr
75+ << " Expected empty, but got: " << std::endl
76+ << " " << sqlQuery << std::endl
77+ ;
78+ return -1 ;
79+ }
80+
81+ return 0 ;
82+ }
0 commit comments