Begin conversion of stdout data to JSON stream.
Tested with tst-create*.sql new tests.
This commit is contained in:
parent
bfe394f29e
commit
0a18c5ef1a
@ -18,9 +18,12 @@ EXTRA_DIST = sql.y sql.l \
|
|||||||
fail4.sql \
|
fail4.sql \
|
||||||
ok1.sql \
|
ok1.sql \
|
||||||
ok2.sql \
|
ok2.sql \
|
||||||
ok3.sql
|
ok3.sql \
|
||||||
|
tst-create-db.sql \
|
||||||
|
tst-create-table1.sql
|
||||||
|
|
||||||
sql_SOURCES = exec.c sql-parser.h
|
sql_SOURCES = exec.c sql-parser.h
|
||||||
|
sql_LDADD = @JANSSON_LIBS@
|
||||||
nodist_sql_SOURCES = sql.c sql.tab.c sql.tab.h sql.lex.h
|
nodist_sql_SOURCES = sql.c sql.tab.c sql.tab.h sql.lex.h
|
||||||
|
|
||||||
BUILT_SOURCES = $(nodist_sql_SOURCES)
|
BUILT_SOURCES = $(nodist_sql_SOURCES)
|
||||||
|
@ -81,11 +81,14 @@ case $build in
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
AC_CHECK_LIB(jansson, json_loads, JANSSON_LIBS=-ljansson)
|
||||||
|
|
||||||
AC_CONFIG_FILES([
|
AC_CONFIG_FILES([
|
||||||
Makefile
|
Makefile
|
||||||
libsqlpars.pc
|
libsqlpars.pc
|
||||||
libsqlpars-uninstalled.pc])
|
libsqlpars-uninstalled.pc])
|
||||||
|
|
||||||
|
AC_SUBST(JANSSON_LIBS)
|
||||||
AC_SUBST(LIBTOOL_APP_LDFLAGS)
|
AC_SUBST(LIBTOOL_APP_LDFLAGS)
|
||||||
AC_SUBST(BUILD_EXEEXT)
|
AC_SUBST(BUILD_EXEEXT)
|
||||||
AC_OUTPUT
|
AC_OUTPUT
|
||||||
|
72
exec.c
72
exec.c
@ -2,6 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <jansson.h>
|
||||||
#include "sql.tab.h"
|
#include "sql.tab.h"
|
||||||
#include "sql-parser.h"
|
#include "sql-parser.h"
|
||||||
|
|
||||||
@ -56,6 +57,30 @@ static const char *interval_names[] = {
|
|||||||
[SDI_HOUR_SECOND] = "SDI_HOUR_SECOND",
|
[SDI_HOUR_SECOND] = "SDI_HOUR_SECOND",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void print_and_free(json_t *jval)
|
||||||
|
{
|
||||||
|
json_dumpf(jval, stdout, JSON_COMPACT | JSON_SORT_KEYS);
|
||||||
|
printf("\n");
|
||||||
|
json_decref(jval);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void opout(const char *opname)
|
||||||
|
{
|
||||||
|
json_t *obj = json_object();
|
||||||
|
json_object_set_new(obj, "op", json_string(opname));
|
||||||
|
print_and_free(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void opstr(const char *opname,
|
||||||
|
const char *str_name,
|
||||||
|
const char *str_val)
|
||||||
|
{
|
||||||
|
json_t *obj = json_object();
|
||||||
|
json_object_set_new(obj, "op", json_string(opname));
|
||||||
|
json_object_set_new(obj, str_name, json_string(str_val));
|
||||||
|
print_and_free(obj);
|
||||||
|
}
|
||||||
|
|
||||||
void sqlp_alias(struct psql_state *pstate, const char *alias)
|
void sqlp_alias(struct psql_state *pstate, const char *alias)
|
||||||
{
|
{
|
||||||
printf("exec ALIAS %s\n", alias);
|
printf("exec ALIAS %s\n", alias);
|
||||||
@ -108,7 +133,7 @@ void sqlp_caseval(struct psql_state *pstate, int n_list, int have_else)
|
|||||||
|
|
||||||
void sqlp_col_attr(struct psql_state *pstate, enum sqlp_col_attribs attr)
|
void sqlp_col_attr(struct psql_state *pstate, enum sqlp_col_attribs attr)
|
||||||
{
|
{
|
||||||
printf("exec ATTR %s\n", attr_names[attr]);
|
opstr("ATTR", "type", attr_names[attr]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_col_attr_uniq(struct psql_state *pstate, int n_cols)
|
void sqlp_col_attr_uniq(struct psql_state *pstate, int n_cols)
|
||||||
@ -133,7 +158,7 @@ void sqlp_col_collate(struct psql_state *pstate, const char *collate)
|
|||||||
|
|
||||||
void sqlp_col_def_str(struct psql_state *pstate, const char *str)
|
void sqlp_col_def_str(struct psql_state *pstate, const char *str)
|
||||||
{
|
{
|
||||||
printf("exec ATTR DEFAULT-STR %s\n", str);
|
opstr("ATTR", "DEFAULT-STR", str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_col_def_num(struct psql_state *pstate, int num)
|
void sqlp_col_def_num(struct psql_state *pstate, int num)
|
||||||
@ -173,7 +198,11 @@ void sqlp_column(struct psql_state *pstate, const char *name)
|
|||||||
|
|
||||||
void sqlp_create_db(struct psql_state *pstate, int if_ne, const char *name)
|
void sqlp_create_db(struct psql_state *pstate, int if_ne, const char *name)
|
||||||
{
|
{
|
||||||
printf("exec CREATE-DB %d %s\n", if_ne, name);
|
json_t *obj = json_object();
|
||||||
|
json_object_set_new(obj, "op", json_string("CREATE-DB"));
|
||||||
|
json_object_set_new(obj, "if_ne", json_boolean(if_ne));
|
||||||
|
json_object_set_new(obj, "name", json_string(name));
|
||||||
|
print_and_free(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_create_sel(struct psql_state *pstate, int ignore_replace)
|
void sqlp_create_sel(struct psql_state *pstate, int ignore_replace)
|
||||||
@ -184,11 +213,14 @@ void sqlp_create_sel(struct psql_state *pstate, int ignore_replace)
|
|||||||
void sqlp_create_tbl(struct psql_state *pstate, int temp, int if_n_exists, int n_cols,
|
void sqlp_create_tbl(struct psql_state *pstate, int temp, int if_n_exists, int n_cols,
|
||||||
const char *db_name, const char *name)
|
const char *db_name, const char *name)
|
||||||
{
|
{
|
||||||
printf("exec CREATE-TABLE %d %d %d %s%s%s\n",
|
json_t *obj = json_object();
|
||||||
temp, if_n_exists, n_cols,
|
json_object_set_new(obj, "op", json_string("CREATE-TABLE"));
|
||||||
db_name ? db_name : "",
|
json_object_set_new(obj, "temp", json_boolean(temp));
|
||||||
db_name ? "." : "",
|
json_object_set_new(obj, "if_n_exists", json_boolean(if_n_exists));
|
||||||
name);
|
json_object_set_new(obj, "n_cols", json_integer(n_cols));
|
||||||
|
json_object_set_new(obj, "db_name", json_string(db_name ? db_name : ""));
|
||||||
|
json_object_set_new(obj, "name", json_string(name));
|
||||||
|
print_and_free(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_create_tbl_sel(struct psql_state *pstate, int temp, int if_n_exists, int n_cols,
|
void sqlp_create_tbl_sel(struct psql_state *pstate, int temp, int if_n_exists, int n_cols,
|
||||||
@ -208,7 +240,11 @@ void sqlp_date_interval(struct psql_state *pstate, enum sqlp_date_intervals inte
|
|||||||
|
|
||||||
void sqlp_def_col(struct psql_state *pstate, int flags, const char *name)
|
void sqlp_def_col(struct psql_state *pstate, int flags, const char *name)
|
||||||
{
|
{
|
||||||
printf("exec DEFINE-COL %d %s\n", flags, name);
|
json_t *obj = json_object();
|
||||||
|
json_object_set_new(obj, "op", json_string("DEFINE-COL"));
|
||||||
|
json_object_set_new(obj, "flags", json_integer(flags));
|
||||||
|
json_object_set_new(obj, "name", json_string(name));
|
||||||
|
print_and_free(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_delete(struct psql_state *pstate, int opts, const char *name)
|
void sqlp_delete(struct psql_state *pstate, int opts, const char *name)
|
||||||
@ -283,7 +319,7 @@ void sqlp_group_by(struct psql_state *pstate, int opts)
|
|||||||
|
|
||||||
void sqlp_having(struct psql_state *pstate)
|
void sqlp_having(struct psql_state *pstate)
|
||||||
{
|
{
|
||||||
printf("exec HAVING\n");
|
opout("HAVING");
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_index(struct psql_state *pstate, const char *name)
|
void sqlp_index(struct psql_state *pstate, const char *name)
|
||||||
@ -303,7 +339,7 @@ void sqlp_ins_cols(struct psql_state *pstate, int n_cols)
|
|||||||
|
|
||||||
void sqlp_ins_default(struct psql_state *pstate)
|
void sqlp_ins_default(struct psql_state *pstate)
|
||||||
{
|
{
|
||||||
printf("exec INSERT-DEFAULT\n");
|
opout("INSERT-DEFAULT");
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_ins_dup_update(struct psql_state *pstate, int n_assn)
|
void sqlp_ins_dup_update(struct psql_state *pstate, int n_assn)
|
||||||
@ -338,7 +374,7 @@ void sqlp_join(struct psql_state *pstate, int opts)
|
|||||||
|
|
||||||
void sqlp_join_expr(struct psql_state *pstate)
|
void sqlp_join_expr(struct psql_state *pstate)
|
||||||
{
|
{
|
||||||
printf("exec JOIN-ON EXPR\n");
|
opout("JOIN-ON EXPR");
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_join_using(struct psql_state *pstate, int n_cols)
|
void sqlp_join_using(struct psql_state *pstate, int n_cols)
|
||||||
@ -358,7 +394,7 @@ void sqlp_name(struct psql_state *pstate, const char *name)
|
|||||||
|
|
||||||
void sqlp_now(struct psql_state *pstate)
|
void sqlp_now(struct psql_state *pstate)
|
||||||
{
|
{
|
||||||
printf("exec NOW\n");
|
opout("NOW");
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_number(struct psql_state *pstate, int val)
|
void sqlp_number(struct psql_state *pstate, int val)
|
||||||
@ -398,7 +434,7 @@ void sqlp_select_nodata(struct psql_state *pstate, int opts, int n_expr)
|
|||||||
|
|
||||||
void sqlp_select_all(struct psql_state *pstate)
|
void sqlp_select_all(struct psql_state *pstate)
|
||||||
{
|
{
|
||||||
printf("exec SELECT-ALL\n");
|
opout("SELECT-ALL");
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_set(struct psql_state *pstate, const char *name)
|
void sqlp_set(struct psql_state *pstate, const char *name)
|
||||||
@ -408,12 +444,12 @@ void sqlp_set(struct psql_state *pstate, const char *name)
|
|||||||
|
|
||||||
void sqlp_start_col(struct psql_state *pstate)
|
void sqlp_start_col(struct psql_state *pstate)
|
||||||
{
|
{
|
||||||
printf("exec START-COL\n");
|
opout("START-COL");
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_stmt(struct psql_state *pstate)
|
void sqlp_stmt(struct psql_state *pstate)
|
||||||
{
|
{
|
||||||
printf("exec STMT\n");
|
opout("STMT");
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_string(struct psql_state *pstate, const char *str)
|
void sqlp_string(struct psql_state *pstate, const char *str)
|
||||||
@ -423,7 +459,7 @@ void sqlp_string(struct psql_state *pstate, const char *str)
|
|||||||
|
|
||||||
void sqlp_subquery(struct psql_state *pstate)
|
void sqlp_subquery(struct psql_state *pstate)
|
||||||
{
|
{
|
||||||
printf("exec SUBQUERY\n");
|
opout("SUBQUERY");
|
||||||
}
|
}
|
||||||
|
|
||||||
void sqlp_subquery_as(struct psql_state *pstate, const char *name)
|
void sqlp_subquery_as(struct psql_state *pstate, const char *name)
|
||||||
@ -461,6 +497,6 @@ void sqlp_values(struct psql_state *pstate, int n_vals)
|
|||||||
|
|
||||||
void sqlp_where(struct psql_state *pstate)
|
void sqlp_where(struct psql_state *pstate)
|
||||||
{
|
{
|
||||||
printf("exec WHERE\n");
|
opout("WHERE");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
sql.y
4
sql.y
@ -1048,10 +1048,10 @@ main(int ac, char **av)
|
|||||||
yyset_in(in_f, pstate.scanner);
|
yyset_in(in_f, pstate.scanner);
|
||||||
|
|
||||||
if(!yyparse(pstate.scanner, &pstate)) {
|
if(!yyparse(pstate.scanner, &pstate)) {
|
||||||
printf("SQL parse worked\n");
|
printf("{\"result\":true}\n");
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
printf("SQL parse failed\n");
|
printf("{\"result\":false}\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
} /* main */
|
} /* main */
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
for testfn in $srcdir/ok*.sql
|
for testfn in $srcdir/ok*.sql $srcdir/tst-*.sql
|
||||||
do
|
do
|
||||||
cat $testfn | ./sql
|
cat $testfn | ./sql
|
||||||
if [ $? -ne 0 ]
|
if [ $? -ne 0 ]
|
||||||
|
1
tst-create-db.sql
Normal file
1
tst-create-db.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
CREATE DATABASE IF NOT EXISTS TheDbName;
|
6
tst-create-table1.sql
Normal file
6
tst-create-table1.sql
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
create table if not exists TheTableName (
|
||||||
|
id int(11) not null auto_increment primary key,
|
||||||
|
region varchar(32) not null,
|
||||||
|
data_size bigint not null default '0',
|
||||||
|
createtime datetime not null
|
||||||
|
);
|
Loading…
x
Reference in New Issue
Block a user