Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# wsjcpp-sql-builder Changelog

## [v0.2.0] - 2026-01-31 (2026 Jan 31)

- Implemented a select, update, insert, delete with chain concept

## [v0.1.0] - 2025-08-10 (2025 Aug 10)

- Added first implementation from fhq-server
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,32 @@ Example main func:
#include <wsjcpp_sql_builder.h>

int main(int argc, const char* argv[]) {
WsjcppSqlBuilderInsert sql("TABLE_NAME");
sql.add("COL1", "val1"); // will be escaped
sql.add("COL2", 1);
// sql.add("COL3", 1.1);
if (!sql.isValid()) {
std::cerr << "Something wrong with query: " << sql.getErrorMessage() << std::endl;
return -1;
}
std::cout << sql.getTextQuery() << std::endl;
return 0;
wsjcpp::SqlBuilder builder;
builder.selectFrom("table1")
.colum("col1")
.colum("col2", "c3")
.colum("col3")
.colum("col4")
.where()
.equal("col1", "1")
.or_()
.notEqual("col2", "2")
.or_()
.subCondition()
.equal("c3", "4")
// .and_() // be default must be added and
.equal("col2", "5")
.finishSubCondition()
.or_()
.lessThen("col4", 111)
.endWhere() // need only for groupBy havingBy and etc
;
std::cout << builder.sql() << std::endl;
}
```

Example output:
```
$ ./wsjcpp-sql-builder
INSERT INTO TABLE_NAME(COL1, COL2) VALUES ('val1', 1);
SELECT col1, col2 AS c3, col3, col4 FROM table1 WHERE col1 = '1' OR col2 <> '2' OR (c3 = '4' AND col2 = '5') OR col4 < 111
```
4 changes: 2 additions & 2 deletions src.wsjcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Automaticly generated by wsjcpp@v0.2.5
# Automaticly generated by wsjcpp@v0.2.7
cmake_minimum_required(VERSION 3.0)

add_definitions(-DWSJCPP_APP_VERSION="v0.1.0")
add_definitions(-DWSJCPP_APP_VERSION="v0.2.0")
add_definitions(-DWSJCPP_APP_NAME="wsjcpp-sql-builder")

if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
Expand Down
28 changes: 14 additions & 14 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
#include <wsjcpp_sql_builder.h>

int main(int argc, const char* argv[]) {
WsjcppSqlBuilderInsert sql("TABLE_NAME");
sql.add("COL1", "val1"); // will be escaped
sql.add("COL2", 1);
// sql.add("COL3", 1.1);
if (!sql.isValid()) {
std::cerr << "Something wrong with query: " << sql.getErrorMessage() << std::endl;
return -1;
}
// WsjcppSqlBuilderInsert sql("TABLE_NAME");
// sql.add("COL1", "val1"); // will be escaped
// sql.add("COL2", 1);
// // sql.add("COL3", 1.1);
// if (!sql.isValid()) {
// std::cerr << "Something wrong with query: " << sql.getErrorMessage() << std::endl;
// return -1;
// }

std::string expectedIns = "INSERT INTO TABLE_NAME(COL1, COL2) VALUES ('val1', 1);";
if (expectedIns != sql.getTextQuery()) {
std::cerr << "Expeceted sql: " << expectedIns << ", but got " << sql.getTextQuery() << std::endl;
return -1;
}
std::cout << sql.getTextQuery() << std::endl;
// std::string expectedIns = "INSERT INTO TABLE_NAME(COL1, COL2) VALUES ('val1', 1);";
// if (expectedIns != sql.getTextQuery()) {
// std::cerr << "Expeceted sql: " << expectedIns << ", but got " << sql.getTextQuery() << std::endl;
// return -1;
// }
// std::cout << sql.getTextQuery() << std::endl;

return 0;
}
Expand Down
65 changes: 65 additions & 0 deletions src/tests/test_delete.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**********************************************************************************
* MIT License
*
* Copyright (c) 2025-2026 Evgenii Sopov <mrseakg@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
*all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Official Source Code: https://github.com/wsjcpp/wsjcpp-sql-builder
*
***********************************************************************************/

#include <iostream>
#include <wsjcpp_sql_builder.h>

int main() {
wsjcpp::SqlBuilder builder;
builder.deleteFrom("table4")
.where()
.equal("col1", "1")
.or_()
.notEqual("col2", "2")
.or_()
.subCondition()
.equal("c3", "4")
// .and_() // be default must be added and
.equal("col2", "5")
.finishSubCondition()
.or_()
.lessThen("col4", 111)
;

if (builder.hasErrors()) {
std::cerr << "Select builder has some errors" << std::endl;
return -1;
}
std::string sqlQuery = builder.sql();
std::string sqlQueryExpected = "DELETE FROM table4 WHERE col1 = '1' OR col2 <> '2' OR (c3 = '4' AND col2 = '5') OR col4 < 111";
if (sqlQuery != sqlQueryExpected) {
std::cerr
<< "Expected:" << std::endl
<< " {" << sqlQueryExpected << "}" << std::endl
<< ", but got:" << std::endl
<< " {" << sqlQuery << "}" << std::endl
;
return -1;
}

return 0;
}
57 changes: 45 additions & 12 deletions src/tests/test_insert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,49 @@
#include <wsjcpp_sql_builder.h>

int main() {
WsjcppSqlBuilderInsert sql("TABLE_NAME");
sql.add("COL1", "val1"); // will be escaped
sql.add("COL2", 1);
// sql.add("COL3", 1.1);
if (!sql.isValid()) {
std::cerr << "Something wrong with query: " << sql.getErrorMessage() << std::endl;
return -1;
}
if (sql.getTextQuery() != "INSERT INTO TABLE_NAME(COL1, COL2) VALUES ('val1', 1);") {
return -1;
}
return 0;
wsjcpp::SqlBuilder builder;
builder.insertInto("table2")
.colum("col1")
.addColums({"col2", "col3"})
.val("val1")
.val(1)
.val(2.0)
;

if (builder.hasErrors()) {
std::cerr << "Select builder has some errors" << std::endl;
return -1;
}
std::string sqlQuery = builder.sql();
std::string sqlQueryExpected = "INSERT INTO table2(col1, col2, col3) VALUES('val1', 1, 2.000000)";
if (sqlQuery != sqlQueryExpected) {
std::cerr
<< "Expected:" << std::endl
<< " {" << sqlQueryExpected << "}" << std::endl
<< ", but got:" << std::endl
<< " {" << sqlQuery << "}" << std::endl
;
return -1;
}

builder.findInsertOrCreate("table2")
.clearValues()
.val("val2")
.val(2)
.val(10.0)
;

sqlQuery = builder.sql();
sqlQueryExpected = "INSERT INTO table2(col1, col2, col3) VALUES('val2', 2, 10.000000)";
if (sqlQuery != sqlQueryExpected) {
std::cerr
<< "Expected:" << std::endl
<< " {" << sqlQueryExpected << "}" << std::endl
<< ", but got:" << std::endl
<< " {" << sqlQuery << "}" << std::endl
;
return -1;
}

return 0;
}
82 changes: 82 additions & 0 deletions src/tests/test_select.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**********************************************************************************
* MIT License
*
* Copyright (c) 2025-2026 Evgenii Sopov <mrseakg@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
*all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Official Source Code: https://github.com/wsjcpp/wsjcpp-sql-builder
*
***********************************************************************************/

#include <iostream>
#include <wsjcpp_sql_builder.h>

int main() {
wsjcpp::SqlBuilder builder;
builder.selectFrom("table1")
.colum("col1")
.colum("col2", "c3")
.colum("col3")
.colum("col4")
.where()
.equal("col1", "1")
.or_()
.notEqual("col2", "2")
.or_()
.subCondition()
.equal("c3", "4")
// .and_() // be default must be added and
.equal("col2", "5")
.finishSubCondition()
.or_()
.lessThen("col4", 111)
.endWhere() // need only for groupBy havingBy and etc
;
if (builder.hasErrors()) {
std::cerr << "Select builder has some errors" << std::endl;
return -1;
}
std::string sqlQuery = builder.sql();
std::string sqlQueryExpected =
"SELECT col1, col2 AS c3, col3, col4 "
"FROM table1 "
"WHERE col1 = '1' OR col2 <> '2' OR (c3 = '4' AND col2 = '5') OR col4 < 111";
if (sqlQuery != sqlQueryExpected) {
std::cerr
<< "Expected:" << std::endl
<< " " << sqlQueryExpected << std::endl
<< ", but got:" << std::endl
<< " " << sqlQuery << std::endl
;
return -1;
}

builder.clear();
sqlQuery = builder.sql();
if (sqlQuery != "") {
std::cerr
<< "Expected empty, but got: " << std::endl
<< " " << sqlQuery << std::endl
;
return -1;
}

return 0;
}
68 changes: 68 additions & 0 deletions src/tests/test_update.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**********************************************************************************
* MIT License
*
* Copyright (c) 2025-2026 Evgenii Sopov <mrseakg@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
*all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Official Source Code: https://github.com/wsjcpp/wsjcpp-sql-builder
*
***********************************************************************************/

#include <iostream>
#include <wsjcpp_sql_builder.h>

int main() {
wsjcpp::SqlBuilder builder;
builder.update("table3")
.set("col1", "val uuu")
.set("col2", 1)
.set("col3", 1.000)
.where()
.equal("col1", "1")
.or_()
.notEqual("col2", "2")
.or_()
.subCondition()
.equal("c3", "4")
// .and_() // be default must be added and
.equal("col2", "5")
.finishSubCondition()
.or_()
.lessThen("col4", 111)
;

if (builder.hasErrors()) {
std::cerr << "Select builder has some errors" << std::endl;
return -1;
}
std::string sqlQuery = builder.sql();
std::string sqlQueryExpected = "UPDATE table3 SET col1 = 'val uuu', col2 = 1, col3 = 1.000000 WHERE col1 = '1' OR col2 <> '2' OR (c3 = '4' AND col2 = '5') OR col4 < 111";
if (sqlQuery != sqlQueryExpected) {
std::cerr
<< "Expected:" << std::endl
<< " {" << sqlQueryExpected << "}" << std::endl
<< ", but got:" << std::endl
<< " {" << sqlQuery << "}" << std::endl
;
return -1;
}

return 0;
}
Loading