Skip to content

Commit 0c95659

Browse files
Create query.c
1 parent 4a44bf8 commit 0c95659

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

code/logic/query.c

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* -----------------------------------------------------------------------------
3+
* Project: Fossil Logic
4+
*
5+
* This file is part of the Fossil Logic project, which aims to develop high-
6+
* performance, cross-platform applications and libraries. The code contained
7+
* herein is subject to the terms and conditions defined in the project license.
8+
*
9+
* Author: Michael Gene Brockus (Dreamer)
10+
*
11+
* Copyright (C) 2024 Fossil Logic. All rights reserved.
12+
* -----------------------------------------------------------------------------
13+
*/
14+
#include "fossil/crabdb/query.h"
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
#include <string.h>
18+
19+
// -----------------------------------------------------------------------------
20+
// Internal Context
21+
// -----------------------------------------------------------------------------
22+
23+
struct crabql_context {
24+
void* active_db; // Handle to whichever backend is open
25+
char backend[32]; // "myshell", "noshell", "timeshell", etc.
26+
};
27+
28+
// -----------------------------------------------------------------------------
29+
// Context Management
30+
// -----------------------------------------------------------------------------
31+
32+
crabql_context_t* fossil_bluecrab_query_create(void) {
33+
crabql_context_t* ctx = calloc(1, sizeof(crabql_context_t));
34+
return ctx;
35+
}
36+
37+
void fossil_bluecrab_query_destroy(crabql_context_t* ctx) {
38+
if (ctx) {
39+
fossil_bluecrab_query_close(ctx);
40+
free(ctx);
41+
}
42+
}
43+
44+
// -----------------------------------------------------------------------------
45+
// Script Execution (stub for interpreter engine)
46+
// -----------------------------------------------------------------------------
47+
48+
bool fossil_bluecrab_query_exec(crabql_context_t* ctx, const char* code) {
49+
if (!ctx || !code) return false;
50+
// TODO: Parse & run CrabQL code
51+
printf("[CrabQL] Executing inline script:\n%s\n", code);
52+
return true;
53+
}
54+
55+
bool fossil_bluecrab_query_exec_file(crabql_context_t* ctx, const char* path) {
56+
if (!ctx || !path) return false;
57+
FILE* f = fopen(path, "r");
58+
if (!f) return false;
59+
60+
fseek(f, 0, SEEK_END);
61+
long size = ftell(f);
62+
fseek(f, 0, SEEK_SET);
63+
64+
char* buffer = malloc(size + 1);
65+
fread(buffer, 1, size, f);
66+
buffer[size] = '\0';
67+
fclose(f);
68+
69+
bool ok = fossil_bluecrab_query_exec(ctx, buffer);
70+
free(buffer);
71+
return ok;
72+
}
73+
74+
// -----------------------------------------------------------------------------
75+
// Database Operations (stubs; delegate to backends)
76+
// -----------------------------------------------------------------------------
77+
78+
bool fossil_bluecrab_query_open(crabql_context_t* ctx, const char* dbfile) {
79+
if (!ctx || !dbfile) return false;
80+
81+
// Simple heuristic: choose backend by file naming convention
82+
if (strstr(dbfile, "time")) {
83+
strcpy(ctx->backend, "timeshell");
84+
ctx->active_db = timeshell_open(dbfile);
85+
} else if (strstr(dbfile, "cache")) {
86+
strcpy(ctx->backend, "cacheshell");
87+
ctx->active_db = cacheshell_open(dbfile);
88+
} else {
89+
strcpy(ctx->backend, "myshell"); // default
90+
ctx->active_db = myshell_open(dbfile);
91+
}
92+
93+
return ctx->active_db != NULL;
94+
}
95+
96+
bool fossil_bluecrab_query_close(crabql_context_t* ctx) {
97+
if (!ctx || !ctx->active_db) return false;
98+
99+
if (strcmp(ctx->backend, "timeshell") == 0)
100+
timeshell_close(ctx->active_db);
101+
else if (strcmp(ctx->backend, "cacheshell") == 0)
102+
cacheshell_close(ctx->active_db);
103+
else
104+
myshell_close(ctx->active_db);
105+
106+
ctx->active_db = NULL;
107+
return true;
108+
}
109+
110+
bool fossil_bluecrab_query_insert(crabql_context_t* ctx,
111+
const char* key,
112+
const char* json_value) {
113+
if (!ctx || !ctx->active_db) return false;
114+
if (strcmp(ctx->backend, "timeshell") == 0)
115+
return timeshell_insert(ctx->active_db, key, json_value);
116+
if (strcmp(ctx->backend, "cacheshell") == 0)
117+
return cacheshell_insert(ctx->active_db, key, json_value);
118+
return myshell_insert(ctx->active_db, key, json_value);
119+
}
120+
121+
bool fossil_bluecrab_query_update(crabql_context_t* ctx,
122+
const char* key,
123+
const char* json_value) {
124+
if (!ctx || !ctx->active_db) return false;
125+
if (strcmp(ctx->backend, "timeshell") == 0)
126+
return timeshell_update(ctx->active_db, key, json_value);
127+
if (strcmp(ctx->backend, "cacheshell") == 0)
128+
return cacheshell_update(ctx->active_db, key, json_value);
129+
return myshell_update(ctx->active_db, key, json_value);
130+
}
131+
132+
bool fossil_bluecrab_query_remove(crabql_context_t* ctx, const char* key) {
133+
if (!ctx || !ctx->active_db) return false;
134+
if (strcmp(ctx->backend, "timeshell") == 0)
135+
return timeshell_remove(ctx->active_db, key);
136+
if (strcmp(ctx->backend, "cacheshell") == 0)
137+
return cacheshell_remove(ctx->active_db, key);
138+
return myshell_remove(ctx->active_db, key);
139+
}
140+
141+
char* fossil_bluecrab_query_get(crabql_context_t* ctx, const char* key) {
142+
if (!ctx || !ctx->active_db) return NULL;
143+
if (strcmp(ctx->backend, "timeshell") == 0)
144+
return timeshell_get(ctx->active_db, key);
145+
if (strcmp(ctx->backend, "cacheshell") == 0)
146+
return cacheshell_get(ctx->active_db, key);
147+
return myshell_get(ctx->active_db, key);
148+
}
149+
150+
size_t fossil_bluecrab_query_count(crabql_context_t* ctx) {
151+
if (!ctx || !ctx->active_db) return 0;
152+
if (strcmp(ctx->backend, "timeshell") == 0)
153+
return timeshell_count(ctx->active_db);
154+
if (strcmp(ctx->backend, "cacheshell") == 0)
155+
return cacheshell_count(ctx->active_db);
156+
return myshell_count(ctx->active_db);
157+
}
158+
159+
// -----------------------------------------------------------------------------
160+
// Module Import
161+
// -----------------------------------------------------------------------------
162+
163+
bool fossil_bluecrab_query_import(crabql_context_t* ctx, const char* module) {
164+
if (!ctx || !module) return false;
165+
printf("[CrabQL] Importing module: %s\n", module);
166+
// TODO: integrate with interpreter module registry
167+
return true;
168+
}

0 commit comments

Comments
 (0)