Insert.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. Result 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 { 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 { SQLCommand::Insert, SQLErrorCode::ColumnDoesNotExist, column };
  31. }
  32. Vector<Row> inserted_rows;
  33. TRY(inserted_rows.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 = row_expr.evaluate(context);
  41. if (context.result->is_error())
  42. return context.result.release_value();
  43. VERIFY(row_value.type() == SQLType::Tuple);
  44. auto values = row_value.to_vector().value();
  45. if (m_column_names.is_empty() && values.size() != row.size())
  46. return { SQLCommand::Insert, SQLErrorCode::InvalidNumberOfValues, String::empty() };
  47. for (auto ix = 0u; ix < values.size(); ix++) {
  48. auto input_value_type = values[ix].type();
  49. auto& tuple_descriptor = *row.descriptor();
  50. // In case of having column names, this must succeed since we checked for every column name for existence in the table.
  51. auto element_index = m_column_names.is_empty() ? ix : tuple_descriptor.find_if([&](auto element) { return element.name == m_column_names[ix]; }).index();
  52. auto element_type = tuple_descriptor[element_index].type;
  53. if (!does_value_data_type_match(element_type, input_value_type))
  54. return { SQLCommand::Insert, SQLErrorCode::InvalidValueType, table_def->columns()[element_index].name() };
  55. row[element_index] = values[ix];
  56. }
  57. inserted_rows.append(row);
  58. }
  59. for (auto& inserted_row : inserted_rows)
  60. TRY(context.database->insert(inserted_row));
  61. return { SQLCommand::Insert, 0, m_chained_expressions.size() };
  62. }
  63. }