This commit is contained in:
parent
6d5d810f0d
commit
681fbc3a96
36
cli.h
36
cli.h
@ -1,15 +1,15 @@
|
||||
#ifndef SORM_CLI_H
|
||||
#define SORM_CLI_H
|
||||
#include "sorm.h"
|
||||
#include <fcntl.h>
|
||||
#include <readline/history.h>
|
||||
#include <readline/readline.h>
|
||||
#include <rlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
#include "sorm.h"
|
||||
#include <unistd.h>
|
||||
|
||||
const char *history_filename = ".sorm_history";
|
||||
|
||||
@ -24,16 +24,7 @@ const char * history_filename = ".sorm_history";
|
||||
}
|
||||
|
||||
char *_sorm_autocompletion_generator(const char *text, int state) {
|
||||
const char* completions[] = {
|
||||
"exit",
|
||||
sorm_last_query,
|
||||
sorm_last_query_expanded,
|
||||
"python",
|
||||
"history",
|
||||
"memory",
|
||||
"truncate",
|
||||
NULL
|
||||
};
|
||||
const char *completions[] = {"exit", sorm_last_query, sorm_last_query_expanded, "python", "history", "memory", "truncate", NULL};
|
||||
int list_index;
|
||||
|
||||
if (!state) {
|
||||
@ -55,11 +46,7 @@ char* _sorm_autocompletion_generator(const char* text, int state) {
|
||||
return rl_completion_matches(text, _sorm_autocompletion_generator);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int
|
||||
_hs_read_file (const char *filename, char *buffer, size_t size)
|
||||
{
|
||||
int _hs_read_file(const char *filename, char *buffer, size_t size) {
|
||||
int fd;
|
||||
ssize_t bytes_read;
|
||||
|
||||
@ -77,10 +64,7 @@ _hs_read_file (const char *filename, char *buffer, size_t size)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
sorm_cli_history_dump (const char *filename)
|
||||
{
|
||||
int sorm_cli_history_dump(const char *filename) {
|
||||
register int line_start, line_end;
|
||||
char *input;
|
||||
struct stat finfo;
|
||||
@ -147,7 +131,6 @@ char * sorm_cli_readline(char * prompt){
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
bool sormrepl_handle_command(char *command) {
|
||||
if (!strncmp(command, "history", 7)) {
|
||||
sorm_cli_history_dump(history_filename);
|
||||
@ -177,7 +160,8 @@ void sormrepl(int sorm){
|
||||
}
|
||||
}
|
||||
|
||||
printf("Rows: %lld, Execute %s, Format: %s\n",sorm_row_count, format_time(_sorm_query_duration),format_time(_sorm_result_format_duration));
|
||||
printf("Rows: %lld, Execute %s, Format: %s\n", sorm_row_count, format_time(_sorm_query_duration),
|
||||
format_time(_sorm_result_format_duration));
|
||||
printf("%s\n", rmalloc_stats());
|
||||
}
|
||||
}
|
||||
|
22
main.c
22
main.c
@ -1,28 +1,16 @@
|
||||
#include "sorm.h"
|
||||
#include "cli.h"
|
||||
#include "sorm.h"
|
||||
|
||||
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
|
||||
);
|
||||
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
|
||||
);
|
||||
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");
|
||||
|
45
sorm.h
45
sorm.h
@ -1,19 +1,18 @@
|
||||
#ifndef SORM_H
|
||||
#define SORM_H
|
||||
#include "str.h"
|
||||
#include <ctype.h>
|
||||
#include <rlib.h>
|
||||
#include <sqlite3.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
#include <ctype.h>
|
||||
#include "str.h"
|
||||
|
||||
sqlite3 *db;
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
|
||||
char *sorm_last_query = NULL;
|
||||
char *sorm_last_query_expanded = NULL;
|
||||
|
||||
@ -58,8 +57,6 @@ char * sorm_csvc(int db, sqlite3_stmt * stmt);
|
||||
char *sorm_csvd(int db, sqlite3_stmt *stmt);
|
||||
char *sorm_csv(int db, sqlite3_stmt *stmt);
|
||||
|
||||
|
||||
|
||||
typedef enum sorm_query_t {
|
||||
SORM_UNKNOWN = 0,
|
||||
SORM_SELECT = 1,
|
||||
@ -71,12 +68,9 @@ typedef enum sorm_query_t {
|
||||
|
||||
const int sorm_null = -1337;
|
||||
|
||||
|
||||
sorm_t **sorm_instances = NULL;
|
||||
int sorm_instance_count = 0;
|
||||
|
||||
|
||||
|
||||
int sormc(char *path) {
|
||||
// sorm connect
|
||||
printf("HIERR\n");
|
||||
@ -101,17 +95,14 @@ int sormc(char * path){
|
||||
db->time_result_format_end = 0;
|
||||
db->time_result_format_start = 0;
|
||||
|
||||
if(sqlite3_open(path,&db->conn))
|
||||
{
|
||||
if (sqlite3_open(path, &db->conn)) {
|
||||
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_instances[ptr - 1]; }
|
||||
|
||||
char *sormgcsv(int ptr) {
|
||||
/* sorm get csv*/
|
||||
@ -121,8 +112,7 @@ char * sormgcsv(int ptr){
|
||||
|
||||
void sormd(int sorm) {
|
||||
sorm_t *db = sormg(sorm);
|
||||
if(sqlite3_close(db->conn))
|
||||
{
|
||||
if (sqlite3_close(db->conn)) {
|
||||
fprintf(stderr, "Error closing database: %s\n", sqlite3_errmsg(db->conn));
|
||||
}
|
||||
if (sorm_last_query) {
|
||||
@ -133,7 +123,6 @@ void sormd(int sorm){
|
||||
free(sorm_last_query_expanded);
|
||||
sorm_last_query_expanded = NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
char *sormpt(char *sql, int number) {
|
||||
@ -163,7 +152,6 @@ char * sormpt(char * sql, int number){
|
||||
}
|
||||
sqlp++;
|
||||
index++;
|
||||
|
||||
}
|
||||
if (index == number) {
|
||||
return result;
|
||||
@ -178,7 +166,6 @@ char * sormpt(char * sql, int number){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
unsigned int sormcq(char *sql, char *out) {
|
||||
// clean query
|
||||
// converts %s %i parameters to ?
|
||||
@ -351,7 +338,8 @@ sorm_ptr sormq(int sorm, char * sql, ...){
|
||||
rc = sqlite3_bind_int(stmt, number, va_arg(args, int));
|
||||
} else if (!strcmp(column_type, "int64")) {
|
||||
rc = sqlite3_bind_int64(stmt, number, va_arg(args, __uint64_t));
|
||||
}else if(!strcmp(column_type, "double") || !strcmp(column_type, "dec") || !strcmp(column_type, "decimal") || !strcmp(column_type, "float")){
|
||||
} else if (!strcmp(column_type, "double") || !strcmp(column_type, "dec") || !strcmp(column_type, "decimal") ||
|
||||
!strcmp(column_type, "float")) {
|
||||
rc = sqlite3_bind_double(stmt, number, va_arg(args, double));
|
||||
} else if (!strcmp(column_type, "blob")) {
|
||||
size_t size = (size_t)va_arg(args, size_t);
|
||||
@ -381,7 +369,6 @@ sorm_ptr sormq(int sorm, char * sql, ...){
|
||||
|
||||
else {
|
||||
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db->conn));
|
||||
|
||||
}
|
||||
if (sorm_last_query) {
|
||||
free(sorm_last_query);
|
||||
@ -400,8 +387,6 @@ sorm_ptr sormq(int sorm, char * sql, ...){
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
char sormlc(char *sql) {
|
||||
// returns last char
|
||||
char last_char = 0;
|
||||
@ -456,8 +441,7 @@ char * sormrq(FILE * f){
|
||||
bool in_string = false;
|
||||
while ((c = fgetc(f)) != EOF && strlen(buffer) != sizeof(buffer) - 2) {
|
||||
*bufferp = c;
|
||||
if(c == '"')
|
||||
{
|
||||
if (c == '"') {
|
||||
in_string = !in_string;
|
||||
}
|
||||
if (!in_string && c == ';') {
|
||||
@ -467,7 +451,6 @@ char * sormrq(FILE * f){
|
||||
*bufferp = 0;
|
||||
}
|
||||
return strdup(buffer);
|
||||
|
||||
}
|
||||
|
||||
char *sormcsvn(char *csv) {
|
||||
@ -538,8 +521,7 @@ void apply_colors(char * csv){
|
||||
printf("%s\n", csv);
|
||||
end = strstr(csv, "\n");
|
||||
char *line;
|
||||
if(end)
|
||||
{
|
||||
if (end) {
|
||||
line = (char *)malloc(end - csv + 1024);
|
||||
strncpy(line, csv, end - csv);
|
||||
} else {
|
||||
@ -560,7 +542,6 @@ void apply_colors(char * csv){
|
||||
if (*csv && *(csv + 1))
|
||||
csv++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void sormfmtd(char *csv) {
|
||||
@ -569,8 +550,4 @@ void sormfmtd(char * csv){
|
||||
free(formatted);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
57
sorm.py
57
sorm.py
@ -5,6 +5,7 @@ import io
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
|
||||
class DictReader:
|
||||
|
||||
def get_column_types(self):
|
||||
@ -25,12 +26,13 @@ class DictReader:
|
||||
name = column.split("(")[0]
|
||||
names.append(name)
|
||||
return names
|
||||
|
||||
def __init__(self, data):
|
||||
self.result = 0
|
||||
if type(data) == int:
|
||||
self.data = ''
|
||||
self.data = ""
|
||||
else:
|
||||
self.data = data.decode();
|
||||
self.data = data.decode()
|
||||
self.rows = [row.split(";")[:-1] for row in self.data.split("\n")]
|
||||
|
||||
self.columns = self.rows[0]
|
||||
@ -45,43 +47,38 @@ class DictReader:
|
||||
row[index] = self.column_types[index](field)
|
||||
self.column_names = self.get_column_names()
|
||||
|
||||
|
||||
|
||||
def __iter__(self):
|
||||
return self.rows.__iter__()
|
||||
|
||||
|
||||
|
||||
|
||||
libc = ctypes.CDLL('libc.so.6');
|
||||
libc = ctypes.CDLL("libc.so.6")
|
||||
|
||||
|
||||
class Sorm:
|
||||
|
||||
def __init__(self):
|
||||
self.lib = ctypes.CDLL('./sorm.so')
|
||||
self.lib = ctypes.CDLL("./sorm.so")
|
||||
self.sormq = self.lib.sormq
|
||||
self.sormq.argtypes = [ctypes.c_int, ctypes.c_char_p];
|
||||
self.sormq.argtypes = [ctypes.c_int, ctypes.c_char_p]
|
||||
self.sormq.restype = ctypes.c_char_p
|
||||
|
||||
self.sormc = self.lib.sormc
|
||||
self.sormc.argtypes = [ctypes.c_char_p];
|
||||
self.sormc.restype = ctypes.c_int;
|
||||
self.sormc.argtypes = [ctypes.c_char_p]
|
||||
self.sormc.restype = ctypes.c_int
|
||||
|
||||
self.sormd = self.lib.sormd
|
||||
self.sormd.argtypes = [ctypes.c_int];
|
||||
self.sormd.argtypes = [ctypes.c_int]
|
||||
self.sormd.restype = None
|
||||
|
||||
self.sormm = self.lib.sormm
|
||||
self.sormm.argtypes = [ctypes.c_int];
|
||||
self.sormm.argtypes = [ctypes.c_int]
|
||||
self.sormm.restype = ctypes.c_char_p
|
||||
|
||||
|
||||
class SormDb(Sorm):
|
||||
|
||||
def __init__(self, path):
|
||||
super().__init__(
|
||||
|
||||
)
|
||||
super().__init__()
|
||||
self.path = path
|
||||
self.conn = None
|
||||
|
||||
@ -109,7 +106,10 @@ class SormDb(Sorm):
|
||||
self.sormq.restype = ctypes.c_int
|
||||
else:
|
||||
self.sormq.restype = ctypes.c_char_p
|
||||
params = tuple([self.conn, sql.encode()] + list(arg.encode() if type(arg) == str else arg for arg in args))
|
||||
params = tuple(
|
||||
[self.conn, sql.encode()]
|
||||
+ list(arg.encode() if type(arg) == str else arg for arg in args)
|
||||
)
|
||||
result = DictReader(self.sormq(*params))
|
||||
self.m = self.sormm(self.conn).decode()
|
||||
return result
|
||||
@ -120,43 +120,42 @@ class SormDb(Sorm):
|
||||
self.sormd(self.conn)
|
||||
self.conn = None
|
||||
|
||||
|
||||
# Load the shared library
|
||||
lib = ctypes.CDLL('./sorm.so')
|
||||
lib = ctypes.CDLL("./sorm.so")
|
||||
|
||||
free = libc.free
|
||||
free.argtypes = [ctypes.c_void_p]
|
||||
free.restype = None
|
||||
|
||||
|
||||
|
||||
|
||||
rsomm = lib.sormm
|
||||
rsomm.argtypes = [ctypes.c_int];
|
||||
rsomm.restype = ctypes.c_char_p;
|
||||
rsomm.argtypes = [ctypes.c_int]
|
||||
rsomm.restype = ctypes.c_char_p
|
||||
|
||||
disconnect = lib.sormd
|
||||
disconnect.argtypes = [ctypes.c_int];
|
||||
disconnect.argtypes = [ctypes.c_int]
|
||||
|
||||
start = time.time()
|
||||
for x in range(1):
|
||||
|
||||
with SormDb("db.sqlite3") as db:
|
||||
|
||||
|
||||
|
||||
for x in range(1):
|
||||
# db.q("BEGIN TRANSACTION")
|
||||
# for x in range(100000):
|
||||
# db.q("INSERT INTO pony (name,age) VALUES (?s,?d);","Python Pony",1337)
|
||||
# db.q("COMMIT")
|
||||
result = db.q("SELECT * FROM pony WHERE id > ?d AND name like ?s ORDER BY id",1337, "%hon Pon%");
|
||||
result = db.q(
|
||||
"SELECT * FROM pony WHERE id > ?d AND name like ?s ORDER BY id",
|
||||
1337,
|
||||
"%hon Pon%",
|
||||
)
|
||||
# for row in result:
|
||||
# print(row)
|
||||
print(result.column_names)
|
||||
print(len(result.rows), "records")
|
||||
print(db.m);
|
||||
print(db.m)
|
||||
end = time.time()
|
||||
duration = end - start
|
||||
print("Duration: {}s".format(duration))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user