Skip to content

Commit c5f72d0

Browse files
v0.7.4
1 parent 33f4b4b commit c5f72d0

4 files changed

Lines changed: 47 additions & 44 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<img src="https://static.wikia.nocookie.net/arnelify/images/c/c8/Arnelify-logo-2024.png/revision/latest?cb=20240701012515" style="width:336px;" alt="Arnelify Logo" />
22

3-
![Arnelify ORM for C++](https://img.shields.io/badge/Arnelify%20ORM%20for%20C++-0.7.3-yellow) ![C++](https://img.shields.io/badge/C++-2b-red) ![G++](https://img.shields.io/badge/G++-14.2.0-blue) ![C-Lang](https://img.shields.io/badge/CLang-14.0.6-blue)
3+
![Arnelify ORM for C++](https://img.shields.io/badge/Arnelify%20ORM%20for%20C++-0.7.4-yellow) ![C++](https://img.shields.io/badge/C++-2b-red) ![G++](https://img.shields.io/badge/G++-14.2.0-blue) ![C-Lang](https://img.shields.io/badge/CLang-14.0.6-blue)
44

55
## 🚀 About
66
**Arnelify® ORM for C++** - is a minimalistic dynamic library which is an ORM written in C and C++.
@@ -53,7 +53,7 @@ This software is licensed under the <a href="https://github.com/arnelify/arnelif
5353
Join us to help improve this software, fix bugs or implement new functionality. Active participation will help keep the software up-to-date, reliable, and aligned with the needs of its users.
5454

5555
## ⭐ Release Notes
56-
Version 0.7.3 - Minimalistic dynamic library
56+
Version 0.7.4 - Minimalistic dynamic library
5757

5858
We are excited to introduce the Arnelify ORM for C++ dynamic library! Please note that this version is raw and still in active development.
5959

index.cpp

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/mysql/query/index.h

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

src/tests/index.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "../index.h"
99

1010
int main(int argc, char* argv[]) {
11-
1211
MySQLOpts opts(10, "mysql", "test", "root", "pass", 3306);
1312
MySQL* db = new MySQL(opts);
1413
MySQLRes res;
@@ -25,14 +24,14 @@ int main(int argc, char* argv[]) {
2524
db->dropTable("posts");
2625
db->foreignKeyChecks(true);
2726

28-
db->createTable("users", [](MySQLQuery* query){
27+
db->createTable("users", [](MySQLQuery* query) {
2928
query->column("id", "BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY");
3029
query->column("email", "VARCHAR(255) UNIQUE", nullptr);
3130
query->column("created_at", "DATETIME", "CURRENT_TIMESTAMP");
3231
query->column("updated_at", "DATETIME", nullptr);
3332
});
3433

35-
db->createTable("posts", [](MySQLQuery* query){
34+
db->createTable("posts", [](MySQLQuery* query) {
3635
query->column("id", "BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY");
3736
query->column("user_id", "BIGINT UNSIGNED", nullptr);
3837
query->column("contents", "VARCHAR(2048)", nullptr);
@@ -42,20 +41,22 @@ int main(int argc, char* argv[]) {
4241
query->index("INDEX", {"user_id"});
4342
query->reference("user_id", "users", "id", {"ON DELETE CASCADE"});
4443
});
45-
44+
4645
res = db->table("users")
4746
->insert({{"email", "email@example.com"}});
4847

4948
Json::Value insert = db->toJson(res);
50-
std::cout << "last inserted id: " << Json::writeString(writer, insert) << std::endl;
49+
std::cout << "last inserted id: " << Json::writeString(writer, insert)
50+
<< std::endl;
5151

5252
res = db->table("users")
5353
->select({"id", "email"})
5454
->where("id", 1)
5555
->limit(1);
5656

5757
Json::Value select = db->toJson(res);
58-
std::cout << "inserted row: " << Json::writeString(writer, select) << std::endl;
58+
std::cout << "inserted row: " << Json::writeString(writer, select)
59+
<< std::endl;
5960

6061
db->table("users")
6162
->update({{"email", "user@example.com"}})

0 commit comments

Comments
 (0)