Adds simple transaction commands (#137)

This commit is contained in:
Till Later 2020-01-14 13:21:19 +01:00 committed by mrks
parent e3cfc80975
commit 4b617bca96
14 changed files with 2816 additions and 2557 deletions

File diff suppressed because it is too large Load Diff

View File

@ -218,14 +218,18 @@ extern int hsql_debug;
SQL_YEAR = 390, SQL_YEAR = 390,
SQL_TRUE = 391, SQL_TRUE = 391,
SQL_FALSE = 392, SQL_FALSE = 392,
SQL_EQUALS = 393, SQL_TRANSACTION = 393,
SQL_NOTEQUALS = 394, SQL_BEGIN = 394,
SQL_LESS = 395, SQL_COMMIT = 395,
SQL_GREATER = 396, SQL_ROLLBACK = 396,
SQL_LESSEQ = 397, SQL_EQUALS = 397,
SQL_GREATEREQ = 398, SQL_NOTEQUALS = 398,
SQL_NOTNULL = 399, SQL_LESS = 399,
SQL_UMINUS = 400 SQL_GREATER = 400,
SQL_LESSEQ = 401,
SQL_GREATEREQ = 402,
SQL_NOTNULL = 403,
SQL_UMINUS = 404
}; };
#endif #endif
@ -253,6 +257,7 @@ union HSQL_STYPE
hsql::PrepareStatement* prep_stmt; hsql::PrepareStatement* prep_stmt;
hsql::ExecuteStatement* exec_stmt; hsql::ExecuteStatement* exec_stmt;
hsql::ShowStatement* show_stmt; hsql::ShowStatement* show_stmt;
hsql::TransactionStatement* transaction_stmt;
hsql::TableName table_name; hsql::TableName table_name;
hsql::TableRef* table; hsql::TableRef* table;
@ -279,7 +284,7 @@ union HSQL_STYPE
std::vector<hsql::OrderDescription*>* order_vec; std::vector<hsql::OrderDescription*>* order_vec;
std::vector<hsql::WithDescription*>* with_description_vec; std::vector<hsql::WithDescription*>* with_description_vec;
#line 283 "bison_parser.h" #line 288 "bison_parser.h"
}; };
typedef union HSQL_STYPE HSQL_STYPE; typedef union HSQL_STYPE HSQL_STYPE;

View File

@ -111,6 +111,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha
hsql::PrepareStatement* prep_stmt; hsql::PrepareStatement* prep_stmt;
hsql::ExecuteStatement* exec_stmt; hsql::ExecuteStatement* exec_stmt;
hsql::ShowStatement* show_stmt; hsql::ShowStatement* show_stmt;
hsql::TransactionStatement* transaction_stmt;
hsql::TableName table_name; hsql::TableName table_name;
hsql::TableRef* table; hsql::TableRef* table;
@ -180,6 +181,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha
%token NOT OFF SET TOP AS BY IF IN IS OF ON OR TO %token NOT OFF SET TOP AS BY IF IN IS OF ON OR TO
%token ARRAY CONCAT ILIKE SECOND MINUTE HOUR DAY MONTH YEAR %token ARRAY CONCAT ILIKE SECOND MINUTE HOUR DAY MONTH YEAR
%token TRUE FALSE %token TRUE FALSE
%token TRANSACTION BEGIN COMMIT ROLLBACK
/********************************* /*********************************
** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html) ** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html)
@ -187,6 +189,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha
%type <stmt_vec> statement_list %type <stmt_vec> statement_list
%type <statement> statement preparable_statement %type <statement> statement preparable_statement
%type <exec_stmt> execute_statement %type <exec_stmt> execute_statement
%type <transaction_stmt> transaction_statement
%type <prep_stmt> prepare_statement %type <prep_stmt> prepare_statement
%type <select_stmt> select_statement select_with_paren select_no_paren select_clause select_paren_or_clause %type <select_stmt> select_statement select_with_paren select_no_paren select_clause select_paren_or_clause
%type <import_stmt> import_statement %type <import_stmt> import_statement
@ -326,6 +329,7 @@ preparable_statement:
| update_statement { $$ = $1; } | update_statement { $$ = $1; }
| drop_statement { $$ = $1; } | drop_statement { $$ = $1; }
| execute_statement { $$ = $1; } | execute_statement { $$ = $1; }
| transaction_statement { $$ = $1; }
; ;
@ -356,6 +360,26 @@ hint:
} }
; ;
/******************************
* Transaction Statement
******************************/
transaction_statement:
BEGIN opt_transaction_keyword {
$$ = new TransactionStatement(kBeginTransaction);
}
| ROLLBACK opt_transaction_keyword {
$$ = new TransactionStatement(kRollbackTransaction);
}
| COMMIT opt_transaction_keyword {
$$ = new TransactionStatement(kCommitTransaction);
}
;
opt_transaction_keyword:
TRANSACTION
| /* empty */
;
/****************************** /******************************
* Prepared Statement * Prepared Statement

File diff suppressed because it is too large Load Diff

View File

@ -730,7 +730,7 @@ extern int yylex \
#undef yyTABLES_NAME #undef yyTABLES_NAME
#endif #endif
#line 236 "flex_lexer.l" #line 240 "flex_lexer.l"
#line 737 "flex_lexer.h" #line 737 "flex_lexer.h"

View File

@ -191,6 +191,10 @@ MONTH TOKEN(MONTH)
YEAR TOKEN(YEAR) YEAR TOKEN(YEAR)
TRUE TOKEN(TRUE) TRUE TOKEN(TRUE)
FALSE TOKEN(FALSE) FALSE TOKEN(FALSE)
TRANSACTION TOKEN(TRANSACTION)
BEGIN TOKEN(BEGIN)
ROLLBACK TOKEN(ROLLBACK)
COMMIT TOKEN(COMMIT)
/* Allow =/== see https://sqlite.org/lang_expr.html#collateop */ /* Allow =/== see https://sqlite.org/lang_expr.html#collateop */
"==" TOKEN(EQUALS) "==" TOKEN(EQUALS)

View File

@ -20,7 +20,8 @@ namespace hsql {
kStmtExport, kStmtExport,
kStmtRename, kStmtRename,
kStmtAlter, kStmtAlter,
kStmtShow kStmtShow,
kStmtTransaction
}; };
// Base struct for every SQL statement // Base struct for every SQL statement

View File

@ -0,0 +1,25 @@
#ifndef HYRISE_TRANSACTIONSTATEMENT_H
#define HYRISE_TRANSACTIONSTATEMENT_H
#include "SQLStatement.h"
namespace hsql {
// Represents SQL Transaction statements.
// Example: BEGIN TRANSACTION;
enum TransactionCommand {
kBeginTransaction,
kCommitTransaction,
kRollbackTransaction
};
struct TransactionStatement : SQLStatement {
TransactionStatement(TransactionCommand command);
virtual ~TransactionStatement();
TransactionCommand command;
};
} // namsepace hsql
#endif

View File

@ -105,7 +105,7 @@ namespace hsql {
delete expr; delete expr;
} }
// DropStatament // DropStatement
DropStatement::DropStatement(DropType type) : DropStatement::DropStatement(DropType type) :
SQLStatement(kStmtDrop), SQLStatement(kStmtDrop),
type(type), type(type),
@ -117,6 +117,14 @@ namespace hsql {
free(name); free(name);
} }
// TransactionStatement
TransactionStatement::TransactionStatement(TransactionCommand command) :
SQLStatement(kStmtTransaction),
command(command) {}
TransactionStatement::~TransactionStatement() {}
// ExecuteStatement // ExecuteStatement
ExecuteStatement::ExecuteStatement() : ExecuteStatement::ExecuteStatement() :
SQLStatement(kStmtExecute), SQLStatement(kStmtExecute),

View File

@ -12,5 +12,6 @@
#include "ExecuteStatement.h" #include "ExecuteStatement.h"
#include "ShowStatement.h" #include "ShowStatement.h"
#include "ExportStatement.h" #include "ExportStatement.h"
#include "TransactionStatement.h"
#endif // SQLPARSER_STATEMENTS_H #endif // SQLPARSER_STATEMENTS_H

View File

@ -261,6 +261,21 @@ namespace hsql {
} }
} }
void printTransactionStatementInfo(const TransactionStatement* stmt, uintmax_t numIndent) {
inprint("TransactionStatement", numIndent);
switch (stmt->command){
case kBeginTransaction:
inprint("BEGIN", numIndent + 1);
break;
case kCommitTransaction:
inprint("COMMIT", numIndent + 1);
break;
case kRollbackTransaction:
inprint("ROLLBACK", numIndent + 1);
break;
}
}
void printStatementInfo(const SQLStatement* stmt) { void printStatementInfo(const SQLStatement* stmt) {
switch (stmt->type()) { switch (stmt->type()) {
case kStmtSelect: case kStmtSelect:
@ -278,6 +293,9 @@ namespace hsql {
case kStmtExport: case kStmtExport:
printExportStatementInfo((const ExportStatement*) stmt, 0); printExportStatementInfo((const ExportStatement*) stmt, 0);
break; break;
case kStmtTransaction:
printTransactionStatementInfo((const TransactionStatement*) stmt, 0);
break;
default: default:
break; break;
} }

View File

@ -23,6 +23,9 @@ namespace hsql {
// Prints a summary of the given CreateStatement with the given indentation. // Prints a summary of the given CreateStatement with the given indentation.
void printCreateStatementInfo(const CreateStatement* stmt, uintmax_t num_indent); void printCreateStatementInfo(const CreateStatement* stmt, uintmax_t num_indent);
// Prints a summary of the given TransactionStatement with the given indentation.
void printTransactionStatementInfo(const TransactionStatement* stmt, uintmax_t numIndent);
// Prints a summary of the given Expression with the given indentation. // Prints a summary of the given Expression with the given indentation.
void printExpression(Expr* expr, uintmax_t num_indent); void printExpression(Expr* expr, uintmax_t num_indent);

View File

@ -19,3 +19,4 @@
!WITH a AS (SELECT ) SELECT 1; !WITH a AS (SELECT ) SELECT 1;
!WITH a AS (WITH b AS (SELECT 1) SELECT 1) SELECT 1; # We do not support nested WITH clauses !WITH a AS (WITH b AS (SELECT 1) SELECT 1) SELECT 1; # We do not support nested WITH clauses
!WITH a AS (SELECT ) b AS (SELECT ) SELECT 1; # Missing comma between WITH descriptions !WITH a AS (SELECT ) b AS (SELECT ) SELECT 1; # Missing comma between WITH descriptions
!BEGIN TRANSACTION transName; # Transaction naming is currently not supported

View File

@ -301,4 +301,50 @@ TEST(StringLengthTest) {
ASSERT_EQ(result.getStatement(2)->stringLength, 21); ASSERT_EQ(result.getStatement(2)->stringLength, 21);
} }
TEST(BeginTransactionTest) {
{
TEST_PARSE_SINGLE_SQL(
"BEGIN TRANSACTION;",
kStmtTransaction,
TransactionStatement,
transaction_result,
transaction_stmt);
ASSERT_EQ(transaction_stmt->command, kBeginTransaction);
}
{
TEST_PARSE_SINGLE_SQL(
"BEGIN;",
kStmtTransaction,
TransactionStatement,
transaction_result,
transaction_stmt);
ASSERT_EQ(transaction_stmt->command, kBeginTransaction);
}
}
TEST(RollbackTransactionTest) {
TEST_PARSE_SINGLE_SQL(
"ROLLBACK TRANSACTION;",
kStmtTransaction,
TransactionStatement,
transaction_result,
transaction_stmt);
ASSERT_EQ(transaction_stmt->command, kRollbackTransaction);
}
TEST(CommitTransactionTest) {
TEST_PARSE_SINGLE_SQL(
"COMMIT TRANSACTION;",
kStmtTransaction,
TransactionStatement,
transaction_result,
transaction_stmt);
ASSERT_EQ(transaction_stmt->command, kCommitTransaction);
}
TEST_MAIN(); TEST_MAIN();