Begin replacing emit() placeholders with API function calls

CREATE DATABASE, DROP DATABASE, DROP TABLE, sql statement, and list
of table names all have associated API calls.

Signed-off-by: Jeff Garzik <jgarzik@redhat.com>
This commit is contained in:
Jeff Garzik
2009-11-08 18:57:33 -05:00
committed by Jeff Garzik
parent 93996b7097
commit af321368f2
4 changed files with 63 additions and 21 deletions

View File

@ -15,9 +15,9 @@ all: ${PROGRAMS}
# chapter 4 # chapter 4
OBJS = sql.tab.o sql.o OBJS = sql.tab.o sql.o exec.o
sql: ${OBJS} sql: ${OBJS} sql-parser.h
${CC} -o $@ ${OBJS} ${CC} -o $@ ${OBJS}
sql.tab.c sql.tab.h: sql.y sql.tab.c sql.tab.h: sql.y

31
exec.c Normal file
View File

@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sql-parser.h"
void sqlp_create_db(int if_ne, const char *name)
{
printf("exec CREATE-DB %d %s\n", if_ne, name);
}
void sqlp_drop_db(int if_exists, const char *name)
{
printf("exec DROP-DB %d %s\n", if_exists, name);
}
void sqlp_drop_table(int temp, int if_exists, int n_tables)
{
printf("exec DROP-TABLE %d %d %d\n", temp, if_exists, n_tables);
}
void sqlp_stmt(void)
{
printf("exec STMT\n");
}
void sqlp_table(const char *name)
{
printf("exec TABLE %s\n", name);
}

10
sql-parser.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef __SQL_PARSER_H__
#define __SQL_PARSER_H__
extern void sqlp_create_db(int if_n_exists, const char *name);
extern void sqlp_drop_db(int if_exists, const char *name);
extern void sqlp_drop_table(int temp, int if_exists, int n_tables);
extern void sqlp_stmt(void);
extern void sqlp_table(const char *name);
#endif /* __SQL_PARSER_H__ */

39
sql.y
View File

@ -11,6 +11,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include "sql-parser.h"
%} %}
@ -347,7 +348,7 @@ stmt_list: error ';'
/* statements: select statement */ /* statements: select statement */
stmt: select_stmt { emit("STMT"); } stmt: select_stmt { sqlp_stmt(); }
; ;
select_stmt: SELECT select_opts select_expr_list select_stmt: SELECT select_opts select_expr_list
@ -506,7 +507,7 @@ table_subquery: '(' select_stmt ')' { emit("SUBQUERY"); }
/* statements: delete statement */ /* statements: delete statement */
stmt: delete_stmt { emit("STMT"); } stmt: delete_stmt { sqlp_stmt(); }
; ;
delete_stmt: DELETE delete_opts FROM NAME delete_stmt: DELETE delete_opts FROM NAME
@ -540,7 +541,7 @@ delete_stmt: DELETE delete_opts
/* statements: insert statement */ /* statements: insert statement */
stmt: insert_stmt { emit("STMT"); } stmt: insert_stmt { sqlp_stmt(); }
; ;
insert_stmt: INSERT insert_opts opt_into NAME insert_stmt: INSERT insert_opts opt_into NAME
@ -604,7 +605,7 @@ insert_asgn_list:
; ;
/** replace just like insert **/ /** replace just like insert **/
stmt: replace_stmt { emit("STMT"); } stmt: replace_stmt { sqlp_stmt(); }
; ;
replace_stmt: REPLACE insert_opts opt_into NAME replace_stmt: REPLACE insert_opts opt_into NAME
@ -625,7 +626,7 @@ replace_stmt: REPLACE insert_opts opt_into NAME opt_col_names
; ;
/** update **/ /** update **/
stmt: update_stmt { emit("STMT"); } stmt: update_stmt { sqlp_stmt(); }
; ;
update_stmt: UPDATE update_opts table_references update_stmt: UPDATE update_opts table_references
@ -658,12 +659,12 @@ update_asgn_list:
/** create database **/ /** create database **/
stmt: create_database_stmt { emit("STMT"); } stmt: create_database_stmt { sqlp_stmt(); }
; ;
create_database_stmt: create_database_stmt:
CREATE DATABASE opt_if_not_exists NAME { emit("CREATEDATABASE %d %s", $3, $4); free($4); } CREATE DATABASE opt_if_not_exists NAME { sqlp_create_db($3, $4); free($4); }
| CREATE SCHEMA opt_if_not_exists NAME { emit("CREATEDATABASE %d %s", $3, $4); free($4); } | CREATE SCHEMA opt_if_not_exists NAME { sqlp_create_db($3, $4); free($4); }
; ;
opt_if_not_exists: /* nil */ { $$ = 0; } opt_if_not_exists: /* nil */ { $$ = 0; }
@ -673,12 +674,12 @@ opt_if_not_exists: /* nil */ { $$ = 0; }
/** drop database **/ /** drop database **/
stmt: drop_database_stmt { emit("STMT"); } stmt: drop_database_stmt { sqlp_stmt(); }
; ;
drop_database_stmt: drop_database_stmt:
DROP DATABASE opt_if_exists NAME { emit("DROPDATABASE %d %s", $3, $4); free($4); } DROP DATABASE opt_if_exists NAME { sqlp_drop_db($3, $4); free($4); }
| DROP SCHEMA opt_if_exists NAME { emit("DROPDATABASE %d %s", $3, $4); free($4); } | DROP SCHEMA opt_if_exists NAME { sqlp_drop_db($3, $4); free($4); }
; ;
opt_if_exists: /* nil */ { $$ = 0; } opt_if_exists: /* nil */ { $$ = 0; }
@ -687,7 +688,7 @@ opt_if_exists: /* nil */ { $$ = 0; }
/** create table **/ /** create table **/
stmt: create_table_stmt { emit("STMT"); } stmt: create_table_stmt { sqlp_stmt(); }
; ;
create_table_stmt: CREATE opt_temporary TABLE opt_if_not_exists NAME create_table_stmt: CREATE opt_temporary TABLE opt_if_not_exists NAME
@ -818,24 +819,24 @@ opt_temporary: /* nil */ { $$ = 0; }
/** drop table **/ /** drop table **/
stmt: drop_table_stmt { emit("STMT"); } stmt: drop_table_stmt { sqlp_stmt(); }
; ;
drop_table_stmt: drop_table_stmt:
DROP opt_temporary TABLE opt_if_exists table_list { emit("DROPDATABASE %d %d %d", $2, $4, $5); } DROP opt_temporary TABLE opt_if_exists table_list { sqlp_drop_table($2, $4, $5); }
; ;
table_list: NAME { emit("TABLE %s", $1); free($1); $$ = 1; } table_list: NAME { sqlp_table($1); free($1); $$ = 1; }
| STRING { lyyerror(@1, "string %s found where name required", $1); | STRING { lyyerror(@1, "string %s found where name required", $1);
emit("TABLE %s", $1); free($1); $$ = 1; } sqlp_table($1); free($1); $$ = 1; }
| table_list ',' NAME { emit("TABLE %s", $3); free($3); $$ = $1 + 1; } | table_list ',' NAME { sqlp_table($3); free($3); $$ = $1 + 1; }
| table_list ',' STRING { lyyerror(@3, "string %s found where name required", $1); | table_list ',' STRING { lyyerror(@3, "string %s found where name required", $1);
emit("TABLE %s", $3); free($3); $$ = $1 + 1; } sqlp_table($3); free($3); $$ = $1 + 1; }
; ;
/**** set user variables ****/ /**** set user variables ****/
stmt: set_stmt { emit("STMT"); } stmt: set_stmt { sqlp_stmt(); }
; ;
set_stmt: SET set_list ; set_stmt: SET set_list ;