Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions include/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,6 @@ class REFLECTION_EXPORT Database {
return Hydrate<T>(query_result, record)[0];
}

/// Retrieves the max id of a given record from the database
/// This corresponds to SELECT MAX(id) FROM TABLE in the SQL syntax
template <typename T>
int64_t GetMaxId() const {
const auto type_id = typeid(T).name();
const auto& record = GetRecord(type_id);
return GetMaxId(record);
}

/// Saves a given record in the database.
/// This corresponds to an INSERT query in the SQL syntax
template <typename T>
Expand Down Expand Up @@ -213,9 +204,6 @@ class REFLECTION_EXPORT Database {
/// Returns a record type from its type information, retrieved from typeid(...).name()
static const Reflection& GetRecord(const std::string& type_id);

/// Returns the max id currently stored for a given record (SELECT MAX(id) FROM table)
int64_t GetMaxId(const Reflection& record) const;

/// Creates concrete record types with initialized members,
/// based on the textual representation of results from a fetch query
template <typename T>
Expand Down
14 changes: 0 additions & 14 deletions include/queries.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,6 @@ class REFLECTION_EXPORT UpdateQuery final : public ExecutionQuery {
void* p_;
};

/// A query for retrieving the max id of a given record from the database
class REFLECTION_EXPORT FetchMaxIdQuery final : public Query {
public:
explicit FetchMaxIdQuery(sqlite3* db, const Reflection& record);
~FetchMaxIdQuery() override;

/// Retrieve the max id currently used for the given record type
int64_t GetMaxId();

protected:
std::string PrepareSql() const override;
sqlite3_stmt* stmt_;
};

struct FetchQueryResults;

/// A query for retrieving all records from the database, which match a given predicate condition
Expand Down
6 changes: 0 additions & 6 deletions src/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,6 @@ const Reflection& Database::GetRecord(const std::string& type_id) {
return GetReflectionRegister().records.at(type_id);
}

int64_t Database::GetMaxId(const Reflection& record) const {
std::lock_guard<std::mutex> lock(db_mutex_);
FetchMaxIdQuery query(db_, record);
return query.GetMaxId();
}

void Database::Save(void* p, const Reflection& record) const {
std::lock_guard<std::mutex> lock(db_mutex_);
InsertQuery query(db_, record, p);
Expand Down
32 changes: 0 additions & 32 deletions src/queries.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,38 +277,6 @@ std::vector<SqlValue> UpdateQuery::Bindings() const {
return bindings;
}

FetchMaxIdQuery::FetchMaxIdQuery(sqlite3* db, const Reflection& record) : Query(db, record), stmt_(nullptr) {}

FetchMaxIdQuery::~FetchMaxIdQuery() {
if (stmt_) {
sqlite3_finalize(stmt_);
}
}

std::string FetchMaxIdQuery::PrepareSql() const {
return "SELECT MAX(id) FROM " + record_.name + ";";
}

int64_t FetchMaxIdQuery::GetMaxId() {
const auto sql = PrepareSql();

if (sqlite3_prepare_v2(db_, sql.data(), -1, &stmt_, nullptr)) {
throw std::runtime_error("Could not retrieve max id for table " + record_.name);
}

const auto column_count = sqlite3_column_count(stmt_);
if (column_count != 1) {
throw std::runtime_error("Number of columns for max id is wrong for table " + record_.name);
}

if (sqlite3_step(stmt_) != SQLITE_ROW) {
throw std::runtime_error("Row result could not be read for max id of table " + record_.name);
}

const auto max_id = sqlite3_column_int(stmt_, 0);
return max_id;
}

FetchRecordsQuery::FetchRecordsQuery(sqlite3* db, const Reflection& record, const QueryPredicateBase* predicate)
: Query(db, record), stmt_(nullptr), predicate_(predicate) {}

Expand Down
17 changes: 0 additions & 17 deletions tests/database_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -571,23 +571,6 @@ TEST_F(DatabaseTest, FetchWithPredicateChaining) {
EXPECT_EQ(37, fetched_persons[1].age);
}

TEST_F(DatabaseTest, ReadMaxId) {
const auto db = Database::Instance();

std::vector<Person> persons;

persons.push_back({L"john", L"appleseed", 28, false, 54});
persons.push_back({L"mary", L"poppins", 20, false, 156});

db->Save(persons);

const auto max_id_person = db->GetMaxId<Person>();
EXPECT_EQ(156, max_id_person);

const auto max_id_pet = db->GetMaxId<Pet>();
EXPECT_EQ(0, max_id_pet);
}

TEST_F(DatabaseTest, RawSqlQueryForPersistedRecord) {
const auto db = Database::Instance();

Expand Down
Loading