提交 a4faba38 编写于 作者: X Xiangquan Xiao

Common: Change KVDB API to accept string_view.

上级 cda115d8
......@@ -60,7 +60,7 @@ class SqliteWraper {
~SqliteWraper() { Release(); }
bool SQL(const std::string &sql, std::string *value = nullptr) {
bool SQL(absl::string_view sql, std::string *value = nullptr) {
AINFO << "Executing SQL: " << sql;
if (db_ == nullptr) {
AERROR << "DB is not open properly.";
......@@ -68,7 +68,7 @@ class SqliteWraper {
}
char *error = nullptr;
if (sqlite3_exec(db_, sql.c_str(), Callback, value, &error) != SQLITE_OK) {
if (sqlite3_exec(db_, sql.data(), Callback, value, &error) != SQLITE_OK) {
AERROR << "Failed to execute SQL: " << error;
sqlite3_free(error);
return false;
......@@ -89,20 +89,20 @@ class SqliteWraper {
} // namespace
bool KVDB::Put(const std::string &key, const std::string &value) {
bool KVDB::Put(absl::string_view key, absl::string_view value) {
SqliteWraper sqlite;
return sqlite.SQL(
absl::StrCat("INSERT OR REPLACE INTO key_value (key, value) VALUES ('",
key, "', '", value, "');"));
}
bool KVDB::Delete(const std::string &key) {
bool KVDB::Delete(absl::string_view key) {
SqliteWraper sqlite;
return sqlite.SQL(
absl::StrCat("DELETE FROM key_value WHERE key='", key, "';"));
}
bool KVDB::Has(const std::string &key) {
bool KVDB::Has(absl::string_view key) {
SqliteWraper sqlite;
std::string value;
const bool ret = sqlite.SQL(
......@@ -112,14 +112,13 @@ bool KVDB::Has(const std::string &key) {
return ret && !value.empty();
}
std::string KVDB::Get(const std::string &key,
const std::string &default_value) {
std::string KVDB::Get(absl::string_view key, absl::string_view default_value) {
SqliteWraper sqlite;
std::string value;
const bool ret = sqlite.SQL(
absl::StrCat("SELECT value FROM key_value WHERE key='", key, "';"),
&value);
return (ret && !value.empty()) ? value : default_value;
return (ret && !value.empty()) ? value : std::string(default_value);
}
} // namespace common
......
......@@ -18,6 +18,8 @@
#include <string>
#include "absl/strings/string_view.h"
/**
* @namespace apollo::common
* @brief apollo::common
......@@ -38,19 +40,19 @@ class KVDB {
* @param sync Whether flush right after writing.
* @return Success or not.
*/
static bool Put(const std::string &key, const std::string &value);
static bool Put(absl::string_view key, absl::string_view value);
/**
* @brief Delete a key.
* @param sync Whether flush right after writing.
* @return Success or not.
*/
static bool Delete(const std::string &key);
static bool Delete(absl::string_view key);
static bool Has(const std::string &key);
static bool Has(absl::string_view key);
static std::string Get(const std::string &key,
const std::string &default_value = "");
static std::string Get(absl::string_view key,
absl::string_view default_value = "");
};
} // namespace common
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册