From b76a11ffa0a6d7e2027e57518d25bd3933df75e4 Mon Sep 17 00:00:00 2001 From: Stephane Nolin Date: Fri, 9 Jan 2026 11:57:30 -0500 Subject: [PATCH] - Move sql3parse_expression() - Use sql3parse_expression() to extract table constraint's CHECK expression - Rephrased comments --- sql3parse_table.c | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/sql3parse_table.c b/sql3parse_table.c index 7b75ee8..e7501c1 100644 --- a/sql3parse_table.c +++ b/sql3parse_table.c @@ -84,7 +84,7 @@ struct sql3tableconstraint { }; // if type SQL3TABLECONSTRAINT_CHECK - sql3string check_expr; // check expression (always NULL in this version) + sql3string check_expr; // check expression // if type SQL3TABLECONSTRAINT_FOREIGNKEY struct { @@ -468,6 +468,24 @@ static sql3token_t sql3lexer_peek (sql3state *state) { // MARK: - Internal Parser - +static sql3string sql3parse_expression (sql3state *state) { + // '(' expression ')' + sql3lexer_checkskip(state); + size_t offset = state->offset; + sql3char c = NEXT; // '(' + uint32_t count = 1; // count number of '(' + while (!IS_EOF) { + c = NEXT; + if (c == '(') ++count; + else if (c == ')') { if (--count == 0) break; } + else if (c == 0) break; + } + const char *ptr = &state->buffer[offset]; + size_t length = state->offset - offset; + sql3string result = {ptr, length}; + return result; +} + static sql3error_code sql3parse_optionalorder (sql3state *state, sql3order_clause *clause) { sql3token_t token = sql3lexer_peek(state); *clause = SQL3ORDER_NONE; @@ -687,9 +705,8 @@ static sql3tableconstraint *sql3parse_table_constraint (sql3state *state) { if (token == TOK_CHECK) { token = sql3lexer_next(state); // consume token constraint->type = SQL3TABLECONSTRAINT_CHECK; - - // expressions are not supported in this version - goto error; + // expressions are extracted (not fully parsed) in this version + constraint->check_expr = sql3parse_expression(state); } // same code to execute for PRIMARY KEY or UNIQUE constraint else if ((token == TOK_PRIMARY) || (token == TOK_UNIQUE)) { @@ -821,25 +838,6 @@ static sql3string sql3parse_literal (sql3state *state) { return result; } -static sql3string sql3parse_expression (sql3state *state) { - // '(' expression ')' - sql3lexer_checkskip(state); - size_t offset = state->offset; - sql3char c = NEXT; // '(' - uint32_t count = 1; // count number of '(' - while (!IS_EOF) { - c = NEXT; - if (c == '(') ++count; - else if (c == ')') { if (--count == 0) break; } - else if (c == 0) break; - } - const char *ptr = &state->buffer[offset]; - size_t length = state->offset - offset; - sql3string result = {ptr, length}; - return result; -} - - static sql3error_code sql3parse_column_type (sql3state *state, sql3column *column) { // column type is reported as a string size_t offset = 0; @@ -937,7 +935,7 @@ static sql3error_code sql3parse_column_constraints (sql3state *state, sql3column // CURRENT_TIMESTAMP // '(' expression ')' - // expressions are not supported in this version + // expressions are extracted (not fully parsed) in this version if (sql3lexer_peek(state) == TOK_OPEN_PARENTHESIS) column->default_expr = sql3parse_expression(state); else column->default_expr = sql3parse_literal(state); break;