mirror of
https://github.com/getml/sqlgen.git
synced 2025-12-31 06:30:18 -06:00
Began designing the iterator
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
#ifndef SQLGEN_POSTGRES_ITERATOR_HPP_
|
||||
#define SQLGEN_POSTGRES_ITERATOR_HPP_
|
||||
|
||||
#include <sqlite3.h>
|
||||
#include <libpq-fe.h>
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -15,8 +16,14 @@
|
||||
namespace sqlgen::postgres {
|
||||
|
||||
class Iterator : public sqlgen::IteratorBase {
|
||||
using ConnPtr = Ref<PGconn>;
|
||||
|
||||
public:
|
||||
Iterator();
|
||||
Iterator(const std::string& _sql, const ConnPtr& _conn);
|
||||
|
||||
Iterator(const Iterator& _other) = delete;
|
||||
|
||||
Iterator(Iterator&& _other) noexcept;
|
||||
|
||||
~Iterator();
|
||||
|
||||
@@ -29,7 +36,26 @@ class Iterator : public sqlgen::IteratorBase {
|
||||
Result<std::vector<std::vector<std::optional<std::string>>>> next(
|
||||
const size_t _batch_size) final;
|
||||
|
||||
Iterator& operator=(const Iterator& _other) = delete;
|
||||
|
||||
Iterator& operator=(Iterator&& _other) noexcept;
|
||||
|
||||
private:
|
||||
static std::string make_cursor_name() {
|
||||
// TODO: Create unique cursor names.
|
||||
return "sqlgen_cursor";
|
||||
}
|
||||
|
||||
private:
|
||||
/// A unique name to identify the cursor.
|
||||
std::string cursor_name_;
|
||||
|
||||
/// The underlying postgres connection. We have this in here to prevent its
|
||||
/// destruction for the lifetime of the iterator.
|
||||
ConnPtr conn_;
|
||||
|
||||
/// Indicates that the iterator has been moved
|
||||
bool moved_;
|
||||
};
|
||||
|
||||
} // namespace sqlgen::postgres
|
||||
|
||||
@@ -6,13 +6,29 @@
|
||||
|
||||
#include "sqlgen/internal/collect/vector.hpp"
|
||||
#include "sqlgen/internal/strings/strings.hpp"
|
||||
#include "sqlgen/sqlite/Iterator.hpp"
|
||||
#include "sqlgen/postgres/exec.hpp"
|
||||
|
||||
namespace sqlgen::postgres {
|
||||
|
||||
Iterator::Iterator() {}
|
||||
Iterator::Iterator(const std::string& _sql, const ConnPtr& _conn)
|
||||
: cursor_name_(make_cursor_name()), conn_(_conn), moved_(false) {
|
||||
exec(conn_, "BEGIN").value();
|
||||
exec(conn_, "DECLARE " + cursor_name_ + " CURSOR FOR " + _sql).value();
|
||||
}
|
||||
|
||||
Iterator::~Iterator() = default;
|
||||
Iterator::Iterator(Iterator&& _other) noexcept
|
||||
: cursor_name_(std::move(_other.cursor_name_)),
|
||||
conn_(std::move(_other.conn_)),
|
||||
moved_(_other.moved_) {
|
||||
_other.moved_ = true;
|
||||
}
|
||||
|
||||
Iterator::~Iterator() {
|
||||
if (!moved_) {
|
||||
exec(conn_, "CLOSE " + cursor_name_);
|
||||
exec(conn_, "END");
|
||||
}
|
||||
}
|
||||
|
||||
bool Iterator::end() const { return true; }
|
||||
|
||||
@@ -25,4 +41,15 @@ Result<std::vector<std::vector<std::optional<std::string>>>> Iterator::next(
|
||||
return error("TODO");
|
||||
}
|
||||
|
||||
Iterator& Iterator::operator=(Iterator&& _other) noexcept {
|
||||
if (this == &_other) {
|
||||
return *this;
|
||||
}
|
||||
cursor_name_ = _other.cursor_name_;
|
||||
conn_ = _other.conn_;
|
||||
moved_ = _other.moved_;
|
||||
_other.moved_ = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace sqlgen::postgres
|
||||
|
||||
Reference in New Issue
Block a user