Skip to content

Commit 70e108c

Browse files
Merge pull request #46 from dreamer-coding/add_apple_runtime
2 parents be61f8e + edb1897 commit 70e108c

File tree

3 files changed

+271
-1
lines changed

3 files changed

+271
-1
lines changed

code/logic/fossil/crabdb/query.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#ifndef FOSSIL_BLUECRAB_QUERY_H
2+
#define FOSSIL_BLUECRAB_QUERY_H
3+
4+
#include <stdbool.h>
5+
#include <stddef.h>
6+
#include <stdint.h>
7+
8+
// Include all backends
9+
#include "cacheshell.h"
10+
#include "fileshell.h"
11+
#include "timeshell.h"
12+
#include "myshell.h"
13+
#include "noshell.h"
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
// -----------------------------------------------------------------------------
20+
// Forward Declarations
21+
// -----------------------------------------------------------------------------
22+
23+
typedef struct crabql_context crabql_context_t;
24+
25+
// -----------------------------------------------------------------------------
26+
// Context Management
27+
// -----------------------------------------------------------------------------
28+
29+
/**
30+
* @brief Create a new CrabQL context.
31+
*/
32+
crabql_context_t* fossil_bluecrab_query_create(void);
33+
34+
/**
35+
* @brief Destroy a CrabQL context.
36+
*/
37+
void fossil_bluecrab_query_destroy(crabql_context_t* ctx);
38+
39+
// -----------------------------------------------------------------------------
40+
// Script Execution
41+
// -----------------------------------------------------------------------------
42+
43+
/**
44+
* @brief Execute a CrabQL script from a string buffer.
45+
*
46+
* @param ctx CrabQL context
47+
* @param code CrabQL source code (UTF-8)
48+
* @return true if script ran successfully, false otherwise
49+
*/
50+
bool fossil_bluecrab_query_exec(crabql_context_t* ctx, const char* code);
51+
52+
/**
53+
* @brief Execute a CrabQL script from a file (.crabql).
54+
*
55+
* @param ctx CrabQL context
56+
* @param path Path to script file
57+
* @return true if successful
58+
*/
59+
bool fossil_bluecrab_query_exec_file(crabql_context_t* ctx, const char* path);
60+
61+
// -----------------------------------------------------------------------------
62+
// Database Utilities (Backend Agnostic)
63+
// -----------------------------------------------------------------------------
64+
65+
bool fossil_bluecrab_query_open(crabql_context_t* ctx, const char* dbfile);
66+
bool fossil_bluecrab_query_close(crabql_context_t* ctx);
67+
68+
bool fossil_bluecrab_query_insert(crabql_context_t* ctx,
69+
const char* key,
70+
const char* json_value);
71+
72+
bool fossil_bluecrab_query_update(crabql_context_t* ctx,
73+
const char* key,
74+
const char* json_value);
75+
76+
bool fossil_bluecrab_query_remove(crabql_context_t* ctx, const char* key);
77+
78+
char* fossil_bluecrab_query_get(crabql_context_t* ctx, const char* key);
79+
80+
size_t fossil_bluecrab_query_count(crabql_context_t* ctx);
81+
82+
// -----------------------------------------------------------------------------
83+
// Module Import API
84+
// -----------------------------------------------------------------------------
85+
86+
/**
87+
* @brief Import a backend-specific module into CrabQL runtime.
88+
*
89+
* Example: fossil_bluecrab_query_import(ctx, "timeshell");
90+
*/
91+
bool fossil_bluecrab_query_import(crabql_context_t* ctx, const char* module);
92+
93+
#ifdef __cplusplus
94+
}
95+
#endif
96+
97+
#endif // FOSSIL_BLUECRAB_QUERY_H

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+
}

code/meson.build

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
if host_machine.system() == 'darwin'
2+
add_languages('objc', native: false, required: true)
3+
add_languages('objcpp', native: false, required: true)
4+
endif
5+
16
subdir('logic')
2-
subdir('tests')
7+
subdir('tests')

0 commit comments

Comments
 (0)