Skip to content

Commit ddbcb35

Browse files
v0.6.2
1 parent 48b720b commit ddbcb35

5 files changed

Lines changed: 87 additions & 24 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.6.1-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.6.2-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.6.1 - Minimalistic dynamic library
56+
Version 0.6.2 - 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

src/cpp/addon.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,16 @@ Napi::Value orm_exec(const Napi::CallbackInfo& info) {
140140
return Napi::String::New(env, out);
141141
}
142142

143+
Napi::Value orm_get_uuid(const Napi::CallbackInfo& info) {
144+
Napi::Env env = info.Env();
145+
return Napi::String::New(env, orm->getUuId());
146+
}
147+
143148
Napi::Object Init(Napi::Env env, Napi::Object exports) {
144149
exports.Set("orm_create", Napi::Function::New(env, orm_create));
145150
exports.Set("orm_destroy", Napi::Function::New(env, orm_destroy));
146151
exports.Set("orm_exec", Napi::Function::New(env, orm_exec));
152+
exports.Set("orm_get_uuid", Napi::Function::New(env, orm_get_uuid));
147153
return exports;
148154
}
149155

src/cpp/ffi.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ const char* orm_exec(const char* cQuery, const char* cSerialized) {
107107
void orm_free(const char* cPtr) {
108108
if (cPtr) delete[] cPtr;
109109
}
110+
111+
const char* orm_get_uuid() {
112+
const std::string uuid = orm->getUuId();
113+
char* cUuId = new char[uuid.length() + 1];
114+
std::strcpy(cUuId, uuid.c_str());
115+
return cUuId;
116+
}
110117
}
111118

112119
#endif

src/cpp/index.cpp

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#include <functional>
2+
#include <future>
23
#include <iostream>
34
#include <map>
45
#include <vector>
56

67
#include <iostream>
8+
#include <iomanip>
9+
#include <random>
710
#include <sstream>
811
#include <string>
912
#include <optional>
@@ -161,14 +164,16 @@ class ArnelifyORM {
161164
} else if (std::holds_alternative<bool>(default_)) {
162165
const bool value = std::get<bool>(default_);
163166
query += " " + std::string(value ? "DEFAULT NULL" : "NOT NULL");
167+
} else if (std::holds_alternative<double>(default_)) {
168+
query += " NOT NULL DEFAULT " + std::to_string(std::get<double>(default_));
164169
} else if (std::holds_alternative<int>(default_)) {
165-
query += std::to_string(std::get<int>(default_));
170+
query += " NOT NULL DEFAULT " + std::to_string(std::get<int>(default_));
166171
} else if (std::holds_alternative<std::string>(default_)) {
167172
const std::string value = std::get<std::string>(default_);
168173
if (value == "CURRENT_TIMESTAMP") {
169-
query += " DEFAULT CURRENT_TIMESTAMP";
174+
query += " NOT NULL DEFAULT CURRENT_TIMESTAMP";
170175
} else {
171-
query += "'" + std::get<std::string>(default_) + "'";
176+
query += " NOT NULL DEFAULT '" + value + "'";
172177
}
173178
}
174179

@@ -278,6 +283,37 @@ class ArnelifyORM {
278283
return res;
279284
}
280285

286+
const std::string getUuId() {
287+
std::random_device rd;
288+
std::mt19937 gen(rd());
289+
std::uniform_int_distribution<> dis(10000, 19999);
290+
int random = dis(gen);
291+
const auto now = std::chrono::system_clock::now();
292+
const auto milliseconds =
293+
std::chrono::duration_cast<std::chrono::milliseconds>(
294+
now.time_since_epoch())
295+
.count();
296+
297+
const std::string code =
298+
std::to_string(milliseconds) + std::to_string(random);
299+
std::hash<std::string> hasher;
300+
size_t v1 = hasher(code);
301+
size_t v2 = hasher(std::to_string(v1));
302+
unsigned char hash[16];
303+
for (int i = 0; i < 8; ++i) {
304+
hash[i] = (v1 >> (i * 8)) & 0xFF;
305+
hash[i + 8] = (v2 >> (i * 8)) & 0xFF;
306+
}
307+
308+
std::stringstream ss;
309+
for (int i = 0; i < 16; ++i) {
310+
ss << std::hex << std::setw(2) << std::setfill('0')
311+
<< static_cast<int>(hash[i]);
312+
}
313+
314+
return ss.str();
315+
}
316+
281317
ArnelifyORM* groupBy(const std::vector<std::string>& args) {
282318
this->query += " GROUP BY ";
283319
for (size_t i = 0; args.size() > i; i++) {
@@ -554,14 +590,15 @@ class ArnelifyORM {
554590
void reference(const std::string& column, const std::string& tableName,
555591
const std::string& foreign,
556592
const std::vector<std::string> args) {
557-
std::string query = "CONSTRAINT fk_" + tableName + " FOREIGN KEY (" +
558-
column + ") REFERENCES " + tableName + "(" + foreign +
559-
")";
593+
std::string query = "CONSTRAINT fk_" + tableName + "_" + this->getUuId() +
594+
" FOREIGN KEY (" + column + ") REFERENCES " +
595+
tableName + "(" + foreign + ")";
560596

561597
const bool isAlter = this->query.starts_with("ALTER");
562598
if (isAlter) {
563-
query = "ADD CONSTRAINT fk_" + tableName + " FOREIGN KEY (" + column +
564-
") REFERENCES " + tableName + "(" + foreign + ")";
599+
query = "ADD CONSTRAINT fk_" + tableName + "_" + this->getUuId() +
600+
" FOREIGN KEY (" + column + ") REFERENCES " + tableName + "(" +
601+
foreign + ")";
565602
}
566603

567604
for (size_t i = 0; args.size() > i; i++) {

src/index.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class ArnelifyORM {
4141
void (*orm_destroy)();
4242
const char* (*orm_exec)(const char*, const char*);
4343
void (*orm_free)(const char*);
44+
const char* (*orm_get_uuid)();
4445

4546
template <typename T>
4647
void loadFunction(const std::string& name, T& func) {
@@ -165,6 +166,7 @@ class ArnelifyORM {
165166
loadFunction("orm_destroy", this->orm_destroy);
166167
loadFunction("orm_exec", this->orm_exec);
167168
loadFunction("orm_free", this->orm_free);
169+
loadFunction("orm_get_uuid", this->orm_get_uuid);
168170

169171
Json::StreamWriterBuilder writer;
170172
writer["indentation"] = "";
@@ -215,14 +217,16 @@ class ArnelifyORM {
215217
} else if (std::holds_alternative<bool>(default_)) {
216218
const bool value = std::get<bool>(default_);
217219
query += " " + std::string(value ? "DEFAULT NULL" : "NOT NULL");
220+
} else if (std::holds_alternative<double>(default_)) {
221+
query += " NOT NULL DEFAULT " + std::to_string(std::get<double>(default_));
218222
} else if (std::holds_alternative<int>(default_)) {
219-
query += std::to_string(std::get<int>(default_));
223+
query += " NOT NULL DEFAULT " + std::to_string(std::get<int>(default_));
220224
} else if (std::holds_alternative<std::string>(default_)) {
221225
const std::string value = std::get<std::string>(default_);
222226
if (value == "CURRENT_TIMESTAMP") {
223-
query += " DEFAULT CURRENT_TIMESTAMP";
227+
query += " NOT NULL DEFAULT CURRENT_TIMESTAMP";
224228
} else {
225-
query += "'" + std::get<std::string>(default_) + "'";
229+
query += " NOT NULL DEFAULT '" + value + "'";
226230
}
227231
}
228232

@@ -362,7 +366,8 @@ class ArnelifyORM {
362366
writer["emitUTF8"] = true;
363367

364368
const std::string bindingsString = Json::writeString(writer, bindingsJson);
365-
const char* cRes = this->orm_exec(this->query.c_str(), bindingsString.c_str());
369+
const char* cRes =
370+
this->orm_exec(this->query.c_str(), bindingsString.c_str());
366371
const std::string serialized = cRes;
367372

368373
Json::Value resJson;
@@ -407,6 +412,13 @@ class ArnelifyORM {
407412
return res;
408413
}
409414

415+
const std::string getUuid() {
416+
const char* cUuId = this->orm_get_uuid();
417+
const std::string uuid = cUuId;
418+
this->orm_free(cUuId);
419+
return uuid;
420+
}
421+
410422
ArnelifyORM* groupBy(const std::vector<std::string>& args) {
411423
this->query += " GROUP BY ";
412424
for (size_t i = 0; args.size() > i; i++) {
@@ -471,15 +483,15 @@ class ArnelifyORM {
471483
continue;
472484
}
473485

474-
if (std::holds_alternative<int>(value)) {
475-
const std::string binding = std::to_string(std::get<int>(value));
486+
if (std::holds_alternative<double>(value)) {
487+
const std::string binding = std::to_string(std::get<double>(value));
476488
this->bindings.emplace_back(binding);
477489
values << "?";
478490
continue;
479491
}
480-
481-
if (std::holds_alternative<double>(value)) {
482-
const std::string binding = std::to_string(std::get<double>(value));
492+
493+
if (std::holds_alternative<int>(value)) {
494+
const std::string binding = std::to_string(std::get<int>(value));
483495
this->bindings.emplace_back(binding);
484496
values << "?";
485497
continue;
@@ -683,14 +695,15 @@ class ArnelifyORM {
683695
void reference(const std::string& column, const std::string& tableName,
684696
const std::string& foreign,
685697
const std::vector<std::string> args) {
686-
std::string query = "CONSTRAINT fk_" + tableName + " FOREIGN KEY (" +
687-
column + ") REFERENCES " + tableName + "(" + foreign +
688-
")";
698+
std::string query = "CONSTRAINT fk_" + tableName + "_" + this->getUuid() +
699+
" FOREIGN KEY (" + column + ") REFERENCES " +
700+
tableName + "(" + foreign + ")";
689701

690702
const bool isAlter = this->query.starts_with("ALTER");
691703
if (isAlter) {
692-
query = "ADD CONSTRAINT fk_" + tableName + " FOREIGN KEY (" + column +
693-
") REFERENCES " + tableName + "(" + foreign + ")";
704+
query = "ADD CONSTRAINT fk_" + tableName + "_" + this->getUuid() +
705+
" FOREIGN KEY (" + column + ") REFERENCES " + tableName + "(" +
706+
foreign + ")";
694707
}
695708

696709
for (size_t i = 0; args.size() > i; i++) {

0 commit comments

Comments
 (0)