This commit is contained in:
anon
2024-02-13 19:46:34 +01:00
commit 8d050f3c30
12 changed files with 726 additions and 0 deletions

5
source/Database.hpp Normal file
View File

@ -0,0 +1,5 @@
class Database {
public:
virtual char * connect(const char * const to, const char * const as) = 0;
virtual char * eval(const char * const sql) = 0;
};

57
source/esql.l Normal file
View File

@ -0,0 +1,57 @@
%{
#include <stdio.h>
#include <string>
#include "Database.hpp"
using namespace std;
extern Database * db;
string sql = "";
%}
%option noyywrap
%option nodefault
%x SQL
ws [ \t\r\v\f]
wsnl [ \t\r\v\f\n]
%%
<INITIAL>{
EXEC{wsnl}+SQL{wsnl}+ {
BEGIN SQL;
}
. {
;
}
\n {
++yylineno;
}
}
<SQL>{
; {
sql += yytext;
printf("Found SQL: \"%s\"\n", sql.c_str());
db->eval(sql.c_str());
sql = "";
BEGIN INITIAL;
}
. {
sql += yytext;
}
}
%%

72
source/main.cpp Normal file
View File

@ -0,0 +1,72 @@
#include <stdio.h>
#include <sqlite3.h>
#include "esql.yy.h"
#include "sqlite.yy.h"
extern "C" {
#include "sql.tab.h"
#include "yyl.h"
#include "sql.lex.h"
#include "sql-parser.h"
}
#include "Database.hpp"
#include "sqlite.tab.h"
class Sqlite : public Database {
struct psql_state *pstate;
int validate(const char * const sql) {
int r;
char * dup = strdup(sql);
psql_set_string_input(pstate, dup);
r = psql_parse(pstate);
free(dup);
return !r
&& !yyerrno;
}
char * connect(const char * const to, const char * const as) override {
char * r = NULL;
#if DEBUG
printf("Connecting to \"%s\" as \"%s\"\n", to, as);
#endif
return r;
}
char * eval(const char * const sql) override {
char * r = NULL;
int v = validate(sql);
#if DEBUG
printf("Recieved SQL: \"%s\" (%d)\n", sql, v);
#endif
return r;
}
public:
Sqlite() {
pstate = psql_new();
if (!pstate) {
throw 1;
}
}
~Sqlite() {
psql_free(pstate);
}
};
Database * db;
signed main(int argc, char * * argv) {
esql_in = fopen(argv[1], "r");
if (!esql_in) {
exit(1);
}
Sqlite sqlite;
db = &sqlite;
esql_lex();
return 0;
}

23
source/sqlite.l Normal file
View File

@ -0,0 +1,23 @@
%{
#include "sqlite.tab.h"
%}
%option noyywrap
%option nodefault
%%
<INITIAL>{
CONNECT\ TO { return CONNECT; }
AS { return AS; }
[a-zA-Z]+ { sqlite_lval.strval = strdup(yytext); return STRING; }
\( { sqlite_lval.strval = strdup(yytext); return STRING; }
\) { sqlite_lval.strval = strdup(yytext); return STRING; }
[0-9]+ { sqlite_lval.strval = strdup(yytext); return STRING; }
[ \t\n] { ; }
; { return END; }
}
%%

47
source/sqlite.y Normal file
View File

@ -0,0 +1,47 @@
%{
#include <stdio.h>
#include <string>
#include "Database.hpp"
extern Database * db;
extern int sqlite_lex();
extern int sqlite_parse();
extern FILE* sqlite_in;
void sqlite_error(const char *s);
using namespace std;
string statement_buffer = "";
%}
%union {
int intval;
char* strval;
}
%token CONNECT AS
%token <strval> STRING
%token END
%%
sql: connect
| statement
;
connect: CONNECT STRING AS STRING END { db->connect($2, $4); }
;
statement: STRING { statement_buffer += $1; }
| statement STRING { statement_buffer += $2; }
| statement END { db->eval(statement_buffer.c_str()); statement_buffer = ""; }
;
%%
void sqlite_error(const char *s) {
fprintf(stderr, "Error: %s\n", s);
}