Compare commits
3 Commits
d810fc29c9
...
9fa9f8a3d7
Author | SHA1 | Date | |
---|---|---|---|
9fa9f8a3d7 | |||
ed7bd9e72c | |||
1c3b867b68 |
9
Makefile
9
Makefile
@ -1,9 +1,10 @@
|
||||
all: build run
|
||||
|
||||
build:
|
||||
gcc main.c -lsqlite3 -lreadline -o sorm
|
||||
gcc -shared -o sorm.so -fPIC main.c -lsqlite3 -lreadline
|
||||
|
||||
gcc main.c -Wextra -Wall -lsqlite3 -lreadline -o sorm
|
||||
gcc -shared -o sorm.so -fPIC main.c -lsqlite3 -lreadline
|
||||
|
||||
run:
|
||||
./sorm
|
||||
./sorm
|
||||
|
||||
|
12
cli.h
12
cli.h
@ -14,6 +14,8 @@
|
||||
const char *history_filename = ".sorm_history";
|
||||
|
||||
int _sorm_readline_accept_line(int count, int key) {
|
||||
(void)count;
|
||||
(void)key;
|
||||
if (strchr(rl_line_buffer, ';')) {
|
||||
|
||||
rl_done = 1;
|
||||
@ -42,6 +44,8 @@ char *_sorm_autocompletion_generator(const char *text, int state) {
|
||||
}
|
||||
|
||||
char **_sorm_autocomplete(const char *text, int start, int end) {
|
||||
(void)start;
|
||||
(void)end;
|
||||
rl_attempted_completion_over = 1;
|
||||
return rl_completion_matches(text, _sorm_autocompletion_generator);
|
||||
}
|
||||
@ -84,7 +88,7 @@ int sorm_cli_history_dump(const char *filename) {
|
||||
}
|
||||
input[file_size] = '\0';
|
||||
|
||||
for (line_start = line_end = 0; line_end < file_size; line_end++) {
|
||||
for (line_start = line_end = 0; (size_t)line_end < file_size; line_end++) {
|
||||
if (input[line_end] == '\n') {
|
||||
input[line_end] = '\0';
|
||||
|
||||
@ -94,7 +98,7 @@ int sorm_cli_history_dump(const char *filename) {
|
||||
}
|
||||
}
|
||||
|
||||
if (line_start < file_size)
|
||||
if ((size_t)line_start < file_size)
|
||||
printf("%s\n", input + line_start);
|
||||
|
||||
free(input);
|
||||
@ -139,9 +143,6 @@ bool sormrepl_handle_command(char *command) {
|
||||
return false;
|
||||
}
|
||||
void sormrepl(int sorm) {
|
||||
sorm_t *db = sormg(sorm);
|
||||
char sql[4097];
|
||||
|
||||
sorm_cli_init(history_filename);
|
||||
char *query;
|
||||
while ((query = sorm_cli_readline("sql> "))) {
|
||||
@ -150,7 +151,6 @@ void sormrepl(int sorm) {
|
||||
sorm_ptr res = sormq(sorm, query);
|
||||
if (res) {
|
||||
if (sormqt(query) == SORM_SELECT) {
|
||||
int length = sormlv(res);
|
||||
sormfmtd(res);
|
||||
free(res);
|
||||
} else if (sormqt(query) == SORM_DELETE) {
|
||||
|
15
main.c
15
main.c
@ -4,16 +4,13 @@
|
||||
int main() {
|
||||
int db = sormc("db.sqlite3");
|
||||
// sormq(db,"DROP TABLE IF EXISTS pony;");
|
||||
printf("%d\n", db);
|
||||
sormq(db, "CREATE TABLE IF NOT EXISTS pony (id INTEGER PRIMARY KEY AUTOINCREMENT,name,age);", NULL);
|
||||
sorm_pk iid = sormq(db, "INSERT INTO pony (id,name,age) VALUES (NULL,%s,%d);", "Teenii", 19);
|
||||
iid = sormq(db, "INSERT INTO pony (id,name,age) VALUES (NULL,%s,%d);", "Amber", 20);
|
||||
iid = sormq(db, "INSERT INTO pony (id,name,age) VALUES (NULL,%s,%d);", "Feuerherz", 20);
|
||||
|
||||
iid = sormq(db, "INSERT INTO pony (id,name,age) VALUES (NULL,%s,%d);", "Retoor", 34);
|
||||
sorm_str csv = sormq(db, "SELECT * FROM pony WHERE id in (?i,?i,?i)", 1, 2, 3);
|
||||
sorm_str csv2 = sormq(db, "SELECT * FROM pony WHERE id = %d and age = %d ", 1, 33);
|
||||
sorm_str csv3 = sormq(db, "SELECT * FROM pony LIMIT 2");
|
||||
sormq(db, "INSERT INTO pony (id,name,age) VALUES (NULL,%s,%d);", "Teenii", 19);
|
||||
sormq(db, "INSERT INTO pony (id,name,age) VALUES (NULL,%s,%d);", "Amber", 20);
|
||||
sormq(db, "INSERT INTO pony (id,name,age) VALUES (NULL,%s,%d);", "Feuerherz", 20);
|
||||
sormq(db, "INSERT INTO pony (id,name,age) VALUES (NULL,%s,%d);", "Retoor", 34);
|
||||
sorm_str csv2 = (sorm_str)sormq(db, "SELECT * FROM pony WHERE id = %d and age = %d ", 1, 33);
|
||||
sorm_str csv3 = (sorm_str)sormq(db, "SELECT * FROM pony LIMIT 2");
|
||||
// free(csv3);
|
||||
// free(csv2);
|
||||
if (csv2)
|
||||
|
46
sorm.h
46
sorm.h
@ -51,7 +51,6 @@ void sormd(int db);
|
||||
char *sormpt(char *sql, int number);
|
||||
unsigned int sormcq(char *sql, char *out);
|
||||
unsigned int sormpc(char *sql);
|
||||
sqlite3_stmt *sormb(sorm_t *db, char *sql, ...);
|
||||
sorm_ptr sormq(int db, char *sql, ...);
|
||||
char *sorm_csvc(int db, sqlite3_stmt *stmt);
|
||||
char *sorm_csvd(int db, sqlite3_stmt *stmt);
|
||||
@ -73,18 +72,14 @@ int sorm_instance_count = 0;
|
||||
|
||||
int sormc(char *path) {
|
||||
// sorm connect
|
||||
printf("HIERR\n");
|
||||
sorm_instance_count++;
|
||||
sorm_instance_count++;
|
||||
sorm_instances = realloc(sorm_instances, sorm_instance_count * sizeof(sorm_t *) + sorm_instance_count * sizeof(sorm_t));
|
||||
|
||||
printf("HIERR\n");
|
||||
sorm_t *db = &sorm_instances[sorm_instance_count - 1];
|
||||
sorm_t *db = (sorm_t *)&sorm_instances[sorm_instance_count - 1];
|
||||
|
||||
printf("HIERR\n");
|
||||
db->conn = NULL;
|
||||
|
||||
printf("HIERR\n");
|
||||
db->csv = NULL;
|
||||
db->current_column = 0;
|
||||
db->current_row = 0;
|
||||
@ -99,10 +94,9 @@ int sormc(char *path) {
|
||||
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db->conn));
|
||||
return 0;
|
||||
}
|
||||
printf("DONE!\n");
|
||||
return sorm_instance_count;
|
||||
}
|
||||
sorm_t *sormg(int ptr) { return &sorm_instances[ptr - 1]; }
|
||||
sorm_t *sormg(int ptr) { return (sorm_t *)&sorm_instances[ptr - 1]; }
|
||||
|
||||
char *sormgcsv(int ptr) {
|
||||
/* sorm get csv*/
|
||||
@ -213,9 +207,10 @@ Execute 3.35s, Format: 36.77s
|
||||
Memory usage: 537 GB, 96.026 allocated, 96.024 freed, 2 in use.
|
||||
*/
|
||||
char *sorm_csvc(int db, sqlite3_stmt *stmt) {
|
||||
(void)db;
|
||||
sormstr_t *str = sormstrn(512);
|
||||
unsigned int column_count = sqlite3_column_count(stmt);
|
||||
for (int i = 0; i < column_count; i++) {
|
||||
for (uint i = 0; i < column_count; i++) {
|
||||
const char *column_name = sqlite3_column_name(stmt, i);
|
||||
sormstra(str, column_name);
|
||||
sormstra(str, "(");
|
||||
@ -230,7 +225,7 @@ char *sorm_csvc(int db, sqlite3_stmt *stmt) {
|
||||
return sormstrc(str);
|
||||
}
|
||||
char *sorm_csvd(int sorm, sqlite3_stmt *stmt) {
|
||||
sorm_t *db = sormg(sorm);
|
||||
(void)sorm;
|
||||
int rc = SQLITE_ROW;
|
||||
int column_count = sqlite3_column_count(stmt);
|
||||
/*
|
||||
@ -257,7 +252,7 @@ char *sorm_csvd(int sorm, sqlite3_stmt *stmt) {
|
||||
sprintf(temp, "%f", sqlite3_column_double(stmt, field_index));
|
||||
sormstra(str, temp);
|
||||
} else if (sqlite3_column_type(stmt, field_index) == SQLITE3_TEXT) {
|
||||
const char *temp = sqlite3_column_text(stmt, field_index);
|
||||
const char *temp = ( char *)sqlite3_column_text(stmt, field_index);
|
||||
sormstra(str, temp);
|
||||
} else {
|
||||
// exit(1);
|
||||
@ -276,7 +271,6 @@ char *sorm_csvd(int sorm, sqlite3_stmt *stmt) {
|
||||
}
|
||||
|
||||
char *sorm_csv(int sorm, sqlite3_stmt *stmt) {
|
||||
sorm_t *db = sormg(sorm);
|
||||
sorm_row_count = 0;
|
||||
char *column_names = sorm_csvc(sorm, stmt);
|
||||
char *data = sorm_csvd(sorm, stmt);
|
||||
@ -291,31 +285,16 @@ char *sorm_csv(int sorm, sqlite3_stmt *stmt) {
|
||||
return result;
|
||||
}
|
||||
|
||||
sqlite3_stmt *sormb(sorm_t *db, char *sql, ...) {
|
||||
// Bind parameters to statement and return amount of parameters
|
||||
int rc = 0;
|
||||
sqlite3_stmt *stmt;
|
||||
va_list args;
|
||||
va_start(args, sql);
|
||||
unsigned int number = 0;
|
||||
char *clean_query = (char *)malloc(strlen(sql) + 1);
|
||||
unsigned int parameter_count = sormcq(sql, clean_query);
|
||||
free(clean_query);
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
char *sormm(sorm_t *db) {
|
||||
(void)db;
|
||||
/* sormmemory */
|
||||
return rmalloc_stats();
|
||||
}
|
||||
|
||||
sorm_ptr sormq(int sorm, char *sql, ...) {
|
||||
sorm_t *db = sormg(sorm);
|
||||
if (db->csv) {
|
||||
// free(db->csv);
|
||||
// db->csv = NULL;
|
||||
}
|
||||
_sorm_query_start = nsecs();
|
||||
db->time_query_start = nsecs();
|
||||
va_list args;
|
||||
@ -323,17 +302,16 @@ sorm_ptr sormq(int sorm, char *sql, ...) {
|
||||
sqlite3_stmt *stmt;
|
||||
sorm_ptr result = NULL;
|
||||
char *clean_query = malloc(strlen(sql) + 1);
|
||||
unsigned int parameter_count = sormcq(sql, clean_query);
|
||||
uint parameter_count = sormcq(sql, clean_query);
|
||||
int rc = sqlite3_prepare_v2(db->conn, clean_query, -1, &stmt, 0);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "%s\n", sqlite3_errmsg(db->conn));
|
||||
}
|
||||
free(clean_query);
|
||||
int number = 0;
|
||||
for (int i = 0; i < parameter_count; i++) {
|
||||
for (uint i = 0; i < parameter_count; i++) {
|
||||
number++;
|
||||
char *column_type = sormpt(sql, number);
|
||||
int arg_index = number - 1;
|
||||
if (!strcmp(column_type, "int") || !strcmp(column_type, "integer") || !strcmp(column_type, "number")) {
|
||||
rc = sqlite3_bind_int(stmt, number, va_arg(args, int));
|
||||
} else if (!strcmp(column_type, "int64")) {
|
||||
@ -347,7 +325,7 @@ sorm_ptr sormq(int sorm, char *sql, ...) {
|
||||
rc = sqlite3_bind_blob(stmt, number, data, size, SQLITE_STATIC);
|
||||
} else if (!strcmp(column_type, "text") || !strcmp(column_type, "str") || !strcmp(column_type, "string")) {
|
||||
unsigned char *data = va_arg(args, unsigned char *);
|
||||
rc = sqlite3_bind_text(stmt, number, data, -1, SQLITE_STATIC);
|
||||
rc = sqlite3_bind_text(stmt, number, (char *)data, -1, SQLITE_STATIC);
|
||||
}
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to bind parameters: %s\n", sqlite3_errmsg(db->conn));
|
||||
@ -406,7 +384,7 @@ int sormlv(char *csv) {
|
||||
while (*csv) {
|
||||
char *found = strstr(csv, ";");
|
||||
if (found) {
|
||||
if (found - csv > longest)
|
||||
if (found - csv > (long int)longest)
|
||||
longest = found - csv;
|
||||
csv = csv + (found - csv) + 1;
|
||||
} else {
|
||||
@ -497,7 +475,7 @@ char *sormfmt(char *csv) {
|
||||
sormstr_t *str = sormstrn(longest + 2);
|
||||
while (*csv && (field = sormcsvn(csv))) {
|
||||
sormstra(str, field);
|
||||
for (int i = 0; i < longest - strlen(field); i++)
|
||||
for (size_t i = 0; i < longest - strlen(field); i++)
|
||||
sormstra(str, " ");
|
||||
|
||||
csv += strlen(field);
|
||||
|
Loading…
Reference in New Issue
Block a user