added if not exists option to create table stmt

This commit is contained in:
Pedro 2014-11-17 02:18:31 +01:00
parent 1c1902538e
commit 541a666e32
5 changed files with 22 additions and 8 deletions

View File

@ -18,12 +18,14 @@ typedef enum {
struct CreateStatement : Statement { struct CreateStatement : Statement {
CreateStatement() : CreateStatement() :
Statement(kStmtCreate), Statement(kStmtCreate),
if_not_exists(false),
file_path(NULL), file_path(NULL),
table_name(NULL) {}; table_name(NULL) {};
virtual ~CreateStatement(); // defined in destructors.cpp virtual ~CreateStatement(); // defined in destructors.cpp
CreateType create_type; CreateType create_type;
bool if_not_exists;
const char* file_path; const char* file_path;
const char* table_name; const char* table_name;
}; };

View File

@ -76,6 +76,7 @@ typedef void* yyscan_t;
int64_t ival; int64_t ival;
char* sval; char* sval;
uint uval; uint uval;
bool bval;
hsql::Statement* statement; hsql::Statement* statement;
hsql::SelectStatement* select_stmt; hsql::SelectStatement* select_stmt;
@ -106,11 +107,11 @@ typedef void* yyscan_t;
/* SQL Keywords */ /* SQL Keywords */
%token DATABASE DISTINCT BETWEEN CONTROL NATURAL COLUMN %token DATABASE DISTINCT BETWEEN CONTROL NATURAL COLUMN
%token CREATE DELETE HAVING IMPORT INSERT ISNULL OFFSET %token CREATE DELETE EXISTS HAVING IMPORT INSERT ISNULL
%token RENAME SELECT UNLOAD UPDATE ALTER CROSS GROUP INDEX %token OFFSET RENAME SELECT UNLOAD UPDATE ALTER CROSS GROUP
%token INNER LIMIT ORDER OUTER RIGHT TABLE UNION USING WHERE %token INDEX INNER LIMIT ORDER OUTER RIGHT TABLE UNION USING
%token DESC DROP FILE FROM INTO JOIN LEFT LIKE LOAD NULL ALL %token WHERE DESC DROP FILE FROM INTO JOIN LEFT LIKE LOAD
%token AND ASC CSV NOT TBL TOP AS BY IN IS ON OR %token NULL ALL AND ASC CSV NOT TBL TOP AS BY IF IN IS ON OR
/********************************* /*********************************
@ -122,6 +123,7 @@ typedef void* yyscan_t;
%type <import_stmt> import_statement %type <import_stmt> import_statement
%type <create_stmt> create_statement %type <create_stmt> create_statement
%type <sval> table_name opt_alias alias file_path %type <sval> table_name opt_alias alias file_path
%type <bval> opt_not_exists
%type <table> from_clause table_ref table_ref_atomic table_ref_name %type <table> from_clause table_ref table_ref_atomic table_ref_name
%type <table> join_clause join_table %type <table> join_clause join_table
%type <expr> expr scalar_expr unary_expr binary_expr function_expr star_expr expr_alias %type <expr> expr scalar_expr unary_expr binary_expr function_expr star_expr expr_alias
@ -212,14 +214,19 @@ file_path:
** Create Statement ** Create Statement
******************************/ ******************************/
create_statement: create_statement:
CREATE TABLE table_name FROM TBL FILE file_path { CREATE TABLE opt_not_exists table_name FROM TBL FILE file_path {
$$ = new CreateStatement(); $$ = new CreateStatement();
$$->create_type = kTableFromTbl; $$->create_type = kTableFromTbl;
$$->file_path = $7; $$->if_not_exists = $3;
$$->table_name = $3; $$->table_name = $4;
$$->file_path = $8;
} }
; ;
opt_not_exists:
IF NOT EXISTS { $$ = true; }
| /* empty */ { $$ = false; }
;
/****************************** /******************************
** Select Statement ** Select Statement

View File

@ -65,6 +65,7 @@ NATURAL TOKEN(NATURAL)
COLUMN TOKEN(COLUMN) COLUMN TOKEN(COLUMN)
CREATE TOKEN(CREATE) CREATE TOKEN(CREATE)
DELETE TOKEN(DELETE) DELETE TOKEN(DELETE)
EXISTS TOKEN(EXISTS)
HAVING TOKEN(HAVING) HAVING TOKEN(HAVING)
IMPORT TOKEN(IMPORT) IMPORT TOKEN(IMPORT)
INSERT TOKEN(INSERT) INSERT TOKEN(INSERT)
@ -106,6 +107,7 @@ TBL TOKEN(TBL)
TOP TOKEN(TOP) TOP TOKEN(TOP)
AS TOKEN(AS) AS TOKEN(AS)
BY TOKEN(BY) BY TOKEN(BY)
IF TOKEN(IF)
IN TOKEN(IN) IN TOKEN(IN)
IS TOKEN(IS) IS TOKEN(IS)
ON TOKEN(ON) ON TOKEN(ON)

View File

@ -33,6 +33,8 @@ CREATE
TABLE TABLE
DATABASE DATABASE
INDEX INDEX
IF
EXISTS
// Import statement // Import statement
IMPORT IMPORT

View File

@ -11,5 +11,6 @@ SELECT * FROM t1 UNION (SELECT * FROM t2) ORDER BY col1;
SELECT * FROM t1 UNION (SELECT * FROM t2 UNION SELECT * FROM t3) ORDER BY col1; SELECT * FROM t1 UNION (SELECT * FROM t2 UNION SELECT * FROM t3) ORDER BY col1;
# CREATE statement # CREATE statement
CREATE TABLE "table" FROM TBL FILE 'students.tbl' CREATE TABLE "table" FROM TBL FILE 'students.tbl'
CREATE TABLE IF NOT EXISTS "table" FROM TBL FILE 'students.tbl'
# Multiple statements # Multiple statements
CREATE TABLE "table" FROM TBL FILE 'students.tbl'; SELECT * FROM "table"; CREATE TABLE "table" FROM TBL FILE 'students.tbl'; SELECT * FROM "table";