Insert.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. * Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibSQL/AST/AST.h>
  8. #include <LibSQL/Database.h>
  9. #include <LibSQL/Meta.h>
  10. #include <LibSQL/Row.h>
  11. namespace SQL::AST {
  12. static bool does_value_data_type_match(SQLType expected, SQLType actual)
  13. {
  14. if (actual == SQLType::Null)
  15. return false;
  16. if (expected == SQLType::Integer)
  17. return actual == SQLType::Integer || actual == SQLType::Float;
  18. return expected == actual;
  19. }
  20. ResultOr<ResultSet> Insert::execute(ExecutionContext& context) const
  21. {
  22. auto table_def = TRY(context.database->get_table(m_schema_name, m_table_name));
  23. if (!table_def) {
  24. auto schema_name = m_schema_name.is_empty() ? String("default"sv) : m_schema_name;
  25. return Result { SQLCommand::Insert, SQLErrorCode::TableDoesNotExist, String::formatted("{}.{}", schema_name, m_table_name) };
  26. }
  27. Row row(table_def);
  28. for (auto& column : m_column_names) {
  29. if (!row.has(column))
  30. return Result { SQLCommand::Insert, SQLErrorCode::ColumnDoesNotExist, column };
  31. }
  32. ResultSet result { SQLCommand::Insert };
  33. TRY(result.try_ensure_capacity(m_chained_expressions.size()));
  34. context.result = Result { SQLCommand::Insert };
  35. for (auto& row_expr : m_chained_expressions) {
  36. for (auto& column_def : table_def->columns()) {
  37. if (!m_column_names.contains_slow(column_def.name()))
  38. row[column_def.name()] = column_def.default_value();
  39. }
  40. auto row_value = TRY(row_expr.evaluate(context));
  41. VERIFY(row_value.type() == SQLType::Tuple);
  42. auto values = row_value.to_vector().value();
  43. if (m_column_names.is_empty() && values.size() != row.size())
  44. return Result { SQLCommand::Insert, SQLErrorCode::InvalidNumberOfValues, String::empty() };
  45. for (auto ix = 0u; ix < values.size(); ix++) {
  46. auto input_value_type = values[ix].type();
  47. auto& tuple_descriptor = *row.descriptor();
  48. // In case of having column names, this must succeed since we checked for every column name for existence in the table.
  49. auto element_index = m_column_names.is_empty() ? ix : tuple_descriptor.find_if([&](auto element) { return element.name == m_column_names[ix]; }).index();
  50. auto element_type = tuple_descriptor[element_index].type;
  51. if (!does_value_data_type_match(element_type, input_value_type))
  52. return Result { SQLCommand::Insert, SQLErrorCode::InvalidValueType, table_def->columns()[element_index].name() };
  53. row[element_index] = values[ix];
  54. }
  55. TRY(context.database->insert(row));
  56. result.insert_row(row, {});
  57. }
  58. return result;
  59. }
  60. }