diff --git a/Userland/Libraries/LibSQL/AST/Insert.cpp b/Userland/Libraries/LibSQL/AST/Insert.cpp index 2f7edc2486..a6effe5d82 100644 --- a/Userland/Libraries/LibSQL/AST/Insert.cpp +++ b/Userland/Libraries/LibSQL/AST/Insert.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Jan de Visser + * Copyright (c) 2021, Mahmoud Mandour * * SPDX-License-Identifier: BSD-2-Clause */ @@ -37,9 +38,20 @@ RefPtr Insert::execute(ExecutionContext& context) const auto row_value = row_expr.evaluate(context); VERIFY(row_value.type() == SQLType::Tuple); auto values = row_value.to_vector().value(); - for (auto ix = 0u; ix < values.size(); ix++) { - auto& column_name = m_column_names[ix]; - row[column_name] = values[ix]; + + // FIXME: Check that the values[ix] match the data type of the column. + if (m_column_names.size() > 0) { + for (auto ix = 0u; ix < values.size(); ix++) { + auto& column_name = m_column_names[ix]; + row[column_name] = values[ix]; + } + } else { + if (values.size() != row.size()) { + return SQLResult::construct(SQLCommand::Insert, SQLErrorCode::InvalidNumberOfValues, ""); + } + for (auto ix = 0u; ix < values.size(); ix++) { + row[ix] = values[ix]; + } } context.database->insert(row); } diff --git a/Userland/Libraries/LibSQL/SQLResult.h b/Userland/Libraries/LibSQL/SQLResult.h index ccfa0c4486..a22d15e998 100644 --- a/Userland/Libraries/LibSQL/SQLResult.h +++ b/Userland/Libraries/LibSQL/SQLResult.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Jan de Visser + * Copyright (c) 2021, Mahmoud Mandour * * SPDX-License-Identifier: BSD-2-Clause */ @@ -52,7 +53,8 @@ constexpr char const* command_tag(SQLCommand command) S(ColumnDoesNotExist, "Column '{}' does not exist") \ S(TableExists, "Table '{}' already exist") \ S(InvalidType, "Invalid type '{}'") \ - S(InvalidDatabaseName, "Invalid database name '{}'") + S(InvalidDatabaseName, "Invalid database name '{}'") \ + S(InvalidNumberOfValues, "Number of values does not match number of columns") enum class SQLErrorCode { #undef __ENUMERATE_SQL_ERROR