Files
scylladb/cql3/statements/cf_statement.cc
Kefu Chai f96d25a0a7 tool: check for existence of keyspace before getting it
in general, user should save output of `DESC foo.bar` to a file,
and pass the path to the file as the argument of `--schema-file`
option of `scylla sstable` commands. the CQL statement generated
from `DESC` command always include the keyspace name of the table.
but in case user create the CQL statement manually and misses
the keyspace name. he/she would have following assertion failure
```
scylla: cql3/statements/cf_statement.cc:49: virtual const sstring &cql3::statements::raw::cf_statement::keyspace() const: Assertion `_cf_name->has_keyspace()' failed.
```
this is not a great user experience.

so, in this change, we check for the existence of keyspace before
looking it up. and throw a runtime error with a better error mesage.
so when the CQL statement does not have the keyspace name, the new
error message would look like:
```
error processing arguments: could not load schema via schema-file: std::runtime_error (tools::do_load_schemas(): CQL statement does not have keyspace specified)
```

since this check is only performed by `do_load_schemas()` which
care about the existence of keyspace, and it only expects the
CQL statement to create table/keyspace/type, we just override the
new `has_keyspace()` method of the corresponding types derived
from `cf_statement`.

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>

Closes scylladb/scylladb#16981
2024-01-29 09:02:01 +02:00

63 lines
1.4 KiB
C++

/*
* Copyright 2014-present-2015 ScyllaDB
*
* Modified by ScyllaDB
*/
/*
* SPDX-License-Identifier: (AGPL-3.0-or-later and Apache-2.0)
*/
#include "raw/cf_statement.hh"
#include "service/client_state.hh"
namespace cql3 {
namespace statements {
namespace raw {
cf_statement::cf_statement(std::optional<cf_name> cf_name)
: _cf_name(std::move(cf_name))
{
}
void cf_statement::prepare_keyspace(const service::client_state& state)
{
if (!_cf_name->has_keyspace()) {
// XXX: We explicitly only want to call state.getKeyspace() in this case, as we don't want to throw
// if not logged in any keyspace but a keyspace is explicitly set on the statement. So don't move
// the call outside the 'if' or replace the method by 'prepareKeyspace(state.getKeyspace())'
_cf_name->set_keyspace(state.get_keyspace(), true);
}
}
void cf_statement::prepare_keyspace(std::string_view keyspace)
{
if (!_cf_name->has_keyspace()) {
_cf_name->set_keyspace(keyspace, true);
}
}
bool cf_statement::has_keyspace() const {
assert(_cf_name.has_value());
return _cf_name->has_keyspace();
}
const sstring& cf_statement::keyspace() const
{
assert(_cf_name->has_keyspace()); // "The statement hasn't be prepared correctly";
return _cf_name->get_keyspace();
}
const sstring& cf_statement::column_family() const
{
return _cf_name->get_column_family();
}
}
}
}