1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:28:12 +00:00

SQLServer: Parse SQL a single time to actually "prepare" the statement

One of the benefits of prepared statements is that the SQL string is
parsed just once and re-used. This updates SQLStatement to do just that
and store the parsed result.
This commit is contained in:
Timothy Flynn 2022-12-02 08:04:05 -05:00 committed by Andreas Kling
parent 83bb25611e
commit b13527b8b2
5 changed files with 40 additions and 39 deletions

View file

@ -24,12 +24,23 @@ RefPtr<SQLStatement> SQLStatement::statement_for(int statement_id)
static int s_next_statement_id = 0;
SQLStatement::SQLStatement(DatabaseConnection& connection, DeprecatedString sql)
SQL::ResultOr<NonnullRefPtr<SQLStatement>> SQLStatement::create(DatabaseConnection& connection, StringView sql)
{
auto parser = SQL::AST::Parser(SQL::AST::Lexer(sql));
auto statement = parser.next_statement();
if (parser.has_errors())
return SQL::Result { SQL::SQLCommand::Unknown, SQL::SQLErrorCode::SyntaxError, parser.errors()[0].to_deprecated_string() };
return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) SQLStatement(connection, move(statement))));
}
SQLStatement::SQLStatement(DatabaseConnection& connection, NonnullRefPtr<SQL::AST::Statement> statement)
: Core::Object(&connection)
, m_statement_id(s_next_statement_id++)
, m_sql(move(sql))
, m_statement(move(statement))
{
dbgln_if(SQLSERVER_DEBUG, "SQLStatement({}, {})", connection.connection_id(), sql);
dbgln_if(SQLSERVER_DEBUG, "SQLStatement({})", connection.connection_id());
s_statements.set(m_statement_id, *this);
}
@ -47,7 +58,6 @@ void SQLStatement::report_error(SQL::Result result)
else
warnln("Cannot return execution error. Client disconnected");
m_statement = nullptr;
m_result = {};
}
@ -61,12 +71,6 @@ void SQLStatement::execute()
}
deferred_invoke([this] {
auto parse_result = parse();
if (parse_result.is_error()) {
report_error(parse_result.release_error());
return;
}
VERIFY(!connection()->database().is_null());
auto execution_result = m_statement->execute(connection()->database().release_nonnull());
@ -93,16 +97,6 @@ void SQLStatement::execute()
});
}
SQL::ResultOr<void> SQLStatement::parse()
{
auto parser = SQL::AST::Parser(SQL::AST::Lexer(m_sql));
m_statement = parser.next_statement();
if (parser.has_errors())
return SQL::Result { SQL::SQLCommand::Unknown, SQL::SQLErrorCode::SyntaxError, parser.errors()[0].to_deprecated_string() };
return {};
}
bool SQLStatement::should_send_result_rows() const
{
VERIFY(m_result.has_value());