|
1 | 1 | #include <functional> |
| 2 | +#include <future> |
2 | 3 | #include <iostream> |
3 | 4 | #include <map> |
4 | 5 | #include <vector> |
5 | 6 |
|
6 | 7 | #include <iostream> |
| 8 | +#include <iomanip> |
| 9 | +#include <random> |
7 | 10 | #include <sstream> |
8 | 11 | #include <string> |
9 | 12 | #include <optional> |
@@ -161,14 +164,16 @@ class ArnelifyORM { |
161 | 164 | } else if (std::holds_alternative<bool>(default_)) { |
162 | 165 | const bool value = std::get<bool>(default_); |
163 | 166 | 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_)); |
164 | 169 | } 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_)); |
166 | 171 | } else if (std::holds_alternative<std::string>(default_)) { |
167 | 172 | const std::string value = std::get<std::string>(default_); |
168 | 173 | if (value == "CURRENT_TIMESTAMP") { |
169 | | - query += " DEFAULT CURRENT_TIMESTAMP"; |
| 174 | + query += " NOT NULL DEFAULT CURRENT_TIMESTAMP"; |
170 | 175 | } else { |
171 | | - query += "'" + std::get<std::string>(default_) + "'"; |
| 176 | + query += " NOT NULL DEFAULT '" + value + "'"; |
172 | 177 | } |
173 | 178 | } |
174 | 179 |
|
@@ -278,6 +283,37 @@ class ArnelifyORM { |
278 | 283 | return res; |
279 | 284 | } |
280 | 285 |
|
| 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 | + |
281 | 317 | ArnelifyORM* groupBy(const std::vector<std::string>& args) { |
282 | 318 | this->query += " GROUP BY "; |
283 | 319 | for (size_t i = 0; args.size() > i; i++) { |
@@ -554,14 +590,15 @@ class ArnelifyORM { |
554 | 590 | void reference(const std::string& column, const std::string& tableName, |
555 | 591 | const std::string& foreign, |
556 | 592 | 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 + ")"; |
560 | 596 |
|
561 | 597 | const bool isAlter = this->query.starts_with("ALTER"); |
562 | 598 | 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 + ")"; |
565 | 602 | } |
566 | 603 |
|
567 | 604 | for (size_t i = 0; args.size() > i; i++) { |
|
0 commit comments