code: cpp,dbal,uuid (7 files)

This commit is contained in:
2025-12-26 01:31:07 +00:00
parent e3df845abd
commit 2eac9dd5c0
7 changed files with 14 additions and 350 deletions
-35
View File
@@ -1,35 +0,0 @@
#include <string>
#include <vector>
namespace dbal {
// Capability detection for database features
class Capabilities {
public:
static std::vector<std::string> detect(const std::string& adapter) {
std::vector<std::string> caps;
if (adapter == "sqlite") {
caps.push_back("crud");
caps.push_back("transactions");
caps.push_back("fulltext_search");
} else if (adapter == "prisma") {
caps.push_back("crud");
caps.push_back("transactions");
caps.push_back("relations");
caps.push_back("migrations");
}
return caps;
}
static bool supports(const std::string& adapter, const std::string& capability) {
auto caps = detect(adapter);
for (const auto& cap : caps) {
if (cap == capability) return true;
}
return false;
}
};
}
-57
View File
@@ -1,57 +0,0 @@
#include <string>
#include <vector>
#include <memory>
namespace dbal {
namespace query {
enum class NodeType {
Select,
Insert,
Update,
Delete,
Where,
Join,
OrderBy,
Limit
};
class ASTNode {
public:
NodeType type;
std::string value;
std::vector<std::shared_ptr<ASTNode>> children;
ASTNode(NodeType t, const std::string& v = "") : type(t), value(v) {}
void addChild(std::shared_ptr<ASTNode> child) {
children.push_back(child);
}
};
class AST {
public:
std::shared_ptr<ASTNode> root;
AST() : root(nullptr) {}
explicit AST(std::shared_ptr<ASTNode> r) : root(r) {}
std::string toString() const {
if (!root) return "";
return nodeToString(root);
}
private:
std::string nodeToString(const std::shared_ptr<ASTNode>& node) const {
if (!node) return "";
std::string result = node->value;
for (const auto& child : node->children) {
result += " " + nodeToString(child);
}
return result;
}
};
}
}
-79
View File
@@ -1,79 +0,0 @@
#include <string>
#include <vector>
#include <map>
namespace dbal {
namespace query {
class QueryBuilder {
public:
QueryBuilder& select(const std::vector<std::string>& columns) {
query_type_ = "SELECT";
columns_ = columns;
return *this;
}
QueryBuilder& from(const std::string& table) {
table_ = table;
return *this;
}
QueryBuilder& where(const std::string& condition) {
conditions_.push_back(condition);
return *this;
}
QueryBuilder& orderBy(const std::string& column, const std::string& direction = "ASC") {
order_by_ = column + " " + direction;
return *this;
}
QueryBuilder& limit(int limit) {
limit_ = limit;
return *this;
}
std::string build() const {
std::string query = query_type_ + " ";
if (!columns_.empty()) {
for (size_t i = 0; i < columns_.size(); ++i) {
query += columns_[i];
if (i < columns_.size() - 1) query += ", ";
}
} else {
query += "*";
}
query += " FROM " + table_;
if (!conditions_.empty()) {
query += " WHERE ";
for (size_t i = 0; i < conditions_.size(); ++i) {
query += conditions_[i];
if (i < conditions_.size() - 1) query += " AND ";
}
}
if (!order_by_.empty()) {
query += " ORDER BY " + order_by_;
}
if (limit_ > 0) {
query += " LIMIT " + std::to_string(limit_);
}
return query;
}
private:
std::string query_type_;
std::vector<std::string> columns_;
std::string table_;
std::vector<std::string> conditions_;
std::string order_by_;
int limit_ = 0;
};
}
}
-53
View File
@@ -1,53 +0,0 @@
#include <string>
#include <algorithm>
#include <cctype>
namespace dbal {
namespace query {
class QueryNormalizer {
public:
static std::string normalize(const std::string& query) {
std::string normalized = query;
// Convert to uppercase
std::transform(normalized.begin(), normalized.end(), normalized.begin(),
[](unsigned char c) { return std::toupper(c); });
// Remove extra whitespace
normalized = removeExtraWhitespace(normalized);
return normalized;
}
private:
static std::string removeExtraWhitespace(const std::string& str) {
std::string result;
bool lastWasSpace = false;
for (char c : str) {
if (std::isspace(c)) {
if (!lastWasSpace) {
result += ' ';
lastWasSpace = true;
}
} else {
result += c;
lastWasSpace = false;
}
}
// Trim leading/trailing spaces
size_t start = result.find_first_not_of(' ');
size_t end = result.find_last_not_of(' ');
if (start != std::string::npos && end != std::string::npos) {
return result.substr(start, end - start + 1);
}
return result;
}
};
}
}
-37
View File
@@ -1,37 +0,0 @@
#include <thread>
#include <chrono>
#include <algorithm>
namespace dbal {
namespace util {
class ExponentialBackoff {
public:
ExponentialBackoff(int initial_ms = 100, int max_ms = 30000, double multiplier = 2.0)
: current_ms_(initial_ms), max_ms_(max_ms), multiplier_(multiplier), attempt_(0) {}
void sleep() {
std::this_thread::sleep_for(std::chrono::milliseconds(current_ms_));
// Increase backoff time for next attempt
current_ms_ = std::min(static_cast<int>(current_ms_ * multiplier_), max_ms_);
attempt_++;
}
void reset() {
current_ms_ = 100;
attempt_ = 0;
}
int currentMs() const { return current_ms_; }
int attempt() const { return attempt_; }
private:
int current_ms_;
int max_ms_;
double multiplier_;
int attempt_;
};
}
}
-65
View File
@@ -1,65 +0,0 @@
#include <string>
#include <random>
#include <sstream>
#include <iomanip>
namespace dbal {
namespace util {
class UUID {
public:
static std::string generate() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 15);
std::uniform_int_distribution<> dis2(8, 11);
std::stringstream ss;
int i;
ss << std::hex;
for (i = 0; i < 8; i++) {
ss << dis(gen);
}
ss << "-";
for (i = 0; i < 4; i++) {
ss << dis(gen);
}
ss << "-4"; // Version 4 UUID
for (i = 0; i < 3; i++) {
ss << dis(gen);
}
ss << "-";
ss << dis2(gen);
for (i = 0; i < 3; i++) {
ss << dis(gen);
}
ss << "-";
for (i = 0; i < 12; i++) {
ss << dis(gen);
}
return ss.str();
}
static bool isValid(const std::string& uuid) {
if (uuid.length() != 36) return false;
for (size_t i = 0; i < uuid.length(); i++) {
if (i == 8 || i == 13 || i == 18 || i == 23) {
if (uuid[i] != '-') return false;
} else {
if (!std::isxdigit(uuid[i])) return false;
}
}
return true;
}
};
}
}
+14 -24
View File
@@ -2,42 +2,32 @@
* @file list-users.ts
* @description List users with filtering and pagination
*/
import type { User, ListOptions, Result } from '../types';
import type { InMemoryStore } from '../store/in-memory-store';
import type { ListOptions, Result, User } from '../../types'
import type { InMemoryStore } from '../../store/in-memory-store'
/**
* List users with filtering and pagination
*/
export async function listUsers(
export const listUsers = async (
store: InMemoryStore,
options: ListOptions = {}
): Promise<Result<User[]>> {
const { filter = {}, sort = {}, page = 1, limit = 20 } = options;
): Promise<Result<User[]>> => {
const { filter = {}, sort = {}, page = 1, limit = 20 } = options
let users = Array.from(store.users.values());
let users = Array.from(store.users.values())
// Apply filters
if (filter.isActive !== undefined) {
users = users.filter((u) => u.isActive === filter.isActive);
}
if (filter.level !== undefined) {
users = users.filter((u) => u.level === filter.level);
if (filter.role !== undefined) {
users = users.filter((user) => user.role === filter.role)
}
// Apply sorting
if (sort.name) {
users.sort((a, b) => (sort.name === 'asc' ? a.name.localeCompare(b.name) : b.name.localeCompare(a.name)));
} else if (sort.createdAt) {
if (sort.username) {
users.sort((a, b) =>
sort.createdAt === 'asc'
? a.createdAt.getTime() - b.createdAt.getTime()
: b.createdAt.getTime() - a.createdAt.getTime()
);
sort.username === 'asc' ? a.username.localeCompare(b.username) : b.username.localeCompare(a.username)
)
}
// Apply pagination
const start = (page - 1) * limit;
const paginated = users.slice(start, start + limit);
const start = (page - 1) * limit
const paginated = users.slice(start, start + limit)
return { success: true, data: paginated };
return { success: true, data: paginated }
}