CreateSchema.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibSQL/AST/AST.h>
  7. #include <LibSQL/Database.h>
  8. #include <LibSQL/Meta.h>
  9. namespace SQL::AST {
  10. RefPtr<SQLResult> CreateSchema::execute(ExecutionContext& context) const
  11. {
  12. auto schema_def_or_error = context.database->get_schema(m_schema_name);
  13. if (schema_def_or_error.is_error())
  14. return SQLResult::construct(SQLCommand::Create, SQLErrorCode::InternalError, schema_def_or_error.error());
  15. auto schema_def = schema_def_or_error.release_value();
  16. if (schema_def) {
  17. if (m_is_error_if_schema_exists) {
  18. return SQLResult::construct(SQLCommand::Create, SQLErrorCode::SchemaExists, m_schema_name);
  19. }
  20. return SQLResult::construct(SQLCommand::Create);
  21. }
  22. schema_def = SchemaDef::construct(m_schema_name);
  23. if (auto maybe_error = context.database->add_schema(*schema_def); maybe_error.is_error())
  24. return SQLResult::construct(SQLCommand::Create, SQLErrorCode::InternalError, maybe_error.error());
  25. return SQLResult::construct(SQLCommand::Create, 0, 1);
  26. }
  27. }