Insert.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  17. if (expected == SQLType::Integer) {
  18. return actual == SQLType::Integer || actual == SQLType::Float;
  19. }
  20. return expected == actual;
  21. }
  22. RefPtr<SQLResult> Insert::execute(ExecutionContext& context) const
  23. {
  24. auto table_def_or_error = context.database->get_table(m_schema_name, m_table_name);
  25. if (table_def_or_error.is_error())
  26. return SQLResult::construct(SQLCommand::Insert, SQLErrorCode::InternalError, table_def_or_error.release_error());
  27. auto table_def = table_def_or_error.release_value();
  28. if (!table_def) {
  29. auto schema_name = m_schema_name;
  30. if (schema_name.is_null() || schema_name.is_empty())
  31. schema_name = "default";
  32. return SQLResult::construct(SQLCommand::Insert, SQLErrorCode::TableDoesNotExist, String::formatted("{}.{}", schema_name, m_table_name));
  33. }
  34. Row row(table_def);
  35. for (auto& column : m_column_names) {
  36. if (!row.has(column)) {
  37. return SQLResult::construct(SQLCommand::Insert, SQLErrorCode::ColumnDoesNotExist, column);
  38. }
  39. }
  40. Vector<Row> inserted_rows;
  41. inserted_rows.ensure_capacity(m_chained_expressions.size());
  42. context.result = SQLResult::construct();
  43. for (auto& row_expr : m_chained_expressions) {
  44. for (auto& column_def : table_def->columns()) {
  45. if (!m_column_names.contains_slow(column_def.name())) {
  46. row[column_def.name()] = column_def.default_value();
  47. }
  48. }
  49. auto row_value = row_expr.evaluate(context);
  50. if (context.result->has_error())
  51. return context.result;
  52. VERIFY(row_value.type() == SQLType::Tuple);
  53. auto values = row_value.to_vector().value();
  54. if (m_column_names.size() == 0 && values.size() != row.size()) {
  55. return SQLResult::construct(SQLCommand::Insert, SQLErrorCode::InvalidNumberOfValues, "");
  56. }
  57. for (auto ix = 0u; ix < values.size(); ix++) {
  58. auto input_value_type = values[ix].type();
  59. auto& tuple_descriptor = *row.descriptor();
  60. // In case of having column names, this must succeed since we checked for every column name for existence in the table.
  61. auto element_index = (m_column_names.size() == 0) ? ix : tuple_descriptor.find_if([&](auto element) { return element.name == m_column_names[ix]; }).index();
  62. auto element_type = tuple_descriptor[element_index].type;
  63. if (!does_value_data_type_match(element_type, input_value_type)) {
  64. return SQLResult::construct(SQLCommand::Insert, SQLErrorCode::InvalidValueType, table_def->columns()[element_index].name());
  65. }
  66. row[element_index] = values[ix];
  67. }
  68. inserted_rows.append(row);
  69. }
  70. for (auto& inserted_row : inserted_rows) {
  71. if (auto maybe_error = context.database->insert(inserted_row); maybe_error.is_error())
  72. return SQLResult::construct(SQLCommand::Insert, SQLErrorCode::InternalError, maybe_error.release_error());
  73. }
  74. return SQLResult::construct(SQLCommand::Insert, 0, m_chained_expressions.size(), 0);
  75. }
  76. }