create table as

This commit is contained in:
2021-07-18 21:49:13 +02:00
parent 3e913263fc
commit 9afbe6435e
13 changed files with 203 additions and 137 deletions

View File

@@ -45,53 +45,60 @@ namespace usql {
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
std::string table_name = lexer.consumeCurrentToken().token_string;
lexer.skipToken(TokenType::open_paren);
int column_order = 0;
do {
std::string column_name;
ColumnType column_type;
int column_len{1};
bool column_nullable{true};
// create as select
if (lexer.tokenType() == TokenType::keyword_as) {
lexer.skipToken(TokenType::keyword_as);
// column name
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
column_name = lexer.consumeCurrentToken().token_string;
std::unique_ptr<Node> select = parse_select_from_table();
return std::make_unique<CreateTableAsSelectNode>(table_name, std::move(select));
} else {
lexer.skipToken(TokenType::open_paren);
int column_order = 0;
do {
std::string column_name;
ColumnType column_type;
int column_len{1};
bool column_nullable{true};
// column type and optionally len
if (lexer.tokenType() == TokenType::keyword_int) {
column_type = ColumnType::integer_type;
lexer.nextToken();
} else if (lexer.tokenType() == TokenType::keyword_float) {
column_type = ColumnType::float_type;
lexer.nextToken();
} else if (lexer.tokenType() == TokenType::keyword_varchar) {
column_type = ColumnType::varchar_type;
lexer.nextToken();
lexer.skipToken(TokenType::open_paren);
if (lexer.tokenType() == TokenType::int_number) {
column_len = std::stoi(lexer.consumeCurrentToken().token_string);
} else { /* TODO handle error */ }
lexer.skipToken(TokenType::close_paren);
} else { /* TODO handle error */ }
// column name
if (lexer.tokenType() != TokenType::identifier) { /* TODO handle error */ }
column_name = lexer.consumeCurrentToken().token_string;
if (lexer.tokenType() == TokenType::keyword_not) {
lexer.nextToken();
lexer.skipToken(TokenType::keyword_null);
column_nullable = false;
} else if (lexer.tokenType() == TokenType::keyword_null) {
lexer.nextToken();
}
// column type and optionally len
if (lexer.tokenType() == TokenType::keyword_int) {
column_type = ColumnType::integer_type;
lexer.nextToken();
} else if (lexer.tokenType() == TokenType::keyword_float) {
column_type = ColumnType::float_type;
lexer.nextToken();
} else if (lexer.tokenType() == TokenType::keyword_varchar) {
column_type = ColumnType::varchar_type;
lexer.nextToken();
lexer.skipToken(TokenType::open_paren);
if (lexer.tokenType() == TokenType::int_number) {
column_len = std::stoi(lexer.consumeCurrentToken().token_string);
} else { /* TODO handle error */ }
lexer.skipToken(TokenType::close_paren);
} else { /* TODO handle error */ }
cols_def.push_back( ColDefNode(column_name, column_type, column_order++, column_len, column_nullable));
if (lexer.tokenType() == TokenType::keyword_not) {
lexer.nextToken();
lexer.skipToken(TokenType::keyword_null);
column_nullable = false;
} else if (lexer.tokenType() == TokenType::keyword_null) {
lexer.nextToken();
}
lexer.skipTokenOptional(TokenType::comma);
cols_def.push_back( ColDefNode(column_name, column_type, column_order++, column_len, column_nullable));
// TODO in future constraints
lexer.skipTokenOptional(TokenType::comma);
} while (lexer.tokenType() != TokenType::close_paren);
// TODO in future constraints
} while (lexer.tokenType() != TokenType::close_paren);
return std::make_unique<CreateTableNode>(table_name, cols_def);
return std::make_unique<CreateTableNode>(table_name, cols_def);
}
}