Skip to content

Commit

Permalink
refactor(c/driver/postgresql): cleanups for result_helper signatures (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAyd authored Nov 5, 2024
1 parent b180380 commit b33f0a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
8 changes: 4 additions & 4 deletions c/driver/postgresql/result_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace adbcpq {

PqResultHelper::~PqResultHelper() { ClearResult(); }

Status PqResultHelper::PrepareInternal(int n_params, const Oid* param_oids) {
Status PqResultHelper::PrepareInternal(int n_params, const Oid* param_oids) const {
// TODO: make stmtName a unique identifier?
PGresult* result =
PQprepare(conn_, /*stmtName=*/"", query_.c_str(), n_params, param_oids);
Expand All @@ -43,9 +43,9 @@ Status PqResultHelper::PrepareInternal(int n_params, const Oid* param_oids) {
return Status::Ok();
}

Status PqResultHelper::Prepare() { return PrepareInternal(0, nullptr); }
Status PqResultHelper::Prepare() const { return PrepareInternal(0, nullptr); }

Status PqResultHelper::Prepare(const std::vector<Oid>& param_oids) {
Status PqResultHelper::Prepare(const std::vector<Oid>& param_oids) const {
return PrepareInternal(param_oids.size(), param_oids.data());
}

Expand Down Expand Up @@ -187,7 +187,7 @@ PGresult* PqResultHelper::ReleaseResult() {
return out;
}

int64_t PqResultHelper::AffectedRows() {
int64_t PqResultHelper::AffectedRows() const {
if (result_ == nullptr) {
return -1;
}
Expand Down
28 changes: 15 additions & 13 deletions c/driver/postgresql/result_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct PqRecord {
}
}

Result<std::vector<std::string>> ParseTextArray() {
Result<std::vector<std::string>> ParseTextArray() const {
std::string text_array(data, len);
text_array.erase(0, 1);
text_array.erase(text_array.size() - 1);
Expand All @@ -89,7 +89,7 @@ class PqResultRow {
PqResultRow() : result_(nullptr), row_num_(-1) {}
PqResultRow(PGresult* result, int row_num) : result_(result), row_num_(row_num) {}

PqRecord operator[](const int& col_num) const {
PqRecord operator[](int col_num) const {
assert(col_num < PQnfields(result_));
const char* data = PQgetvalue(result_, row_num_, col_num);
const int len = PQgetlength(result_, row_num_, col_num);
Expand All @@ -98,9 +98,11 @@ class PqResultRow {
return PqRecord{data, len, is_null};
}

bool IsValid() { return result_ && row_num_ >= 0 && row_num_ < PQntuples(result_); }
bool IsValid() const {
return result_ && row_num_ >= 0 && row_num_ < PQntuples(result_);
}

PqResultRow Next() { return PqResultRow(result_, row_num_ + 1); }
PqResultRow Next() const { return PqResultRow(result_, row_num_ + 1); }

private:
PGresult* result_ = nullptr;
Expand Down Expand Up @@ -132,8 +134,8 @@ class PqResultHelper {
void set_param_format(Format format) { param_format_ = format; }
void set_output_format(Format format) { output_format_ = format; }

Status Prepare();
Status Prepare(const std::vector<Oid>& param_oids);
Status Prepare() const;
Status Prepare(const std::vector<Oid>& param_oids) const;
Status DescribePrepared();
Status Execute(const std::vector<std::string>& params = {},
PostgresType* param_types = nullptr);
Expand All @@ -143,7 +145,7 @@ class PqResultHelper {
Status ResolveOutputTypes(PostgresTypeResolver& type_resolver,
PostgresType* result_types);

bool HasResult() { return result_ != nullptr; }
bool HasResult() const { return result_ != nullptr; }

void SetResult(PGresult* result) {
ClearResult();
Expand All @@ -157,7 +159,7 @@ class PqResultHelper {
result_ = nullptr;
}

int64_t AffectedRows();
int64_t AffectedRows() const;

int NumRows() const { return PQntuples(result_); }

Expand All @@ -167,7 +169,7 @@ class PqResultHelper {
return PQfname(result_, column_number);
}
Oid FieldType(int column_number) const { return PQftype(result_, column_number); }
PqResultRow Row(int i) { return PqResultRow(result_, i); }
PqResultRow Row(int i) const { return PqResultRow(result_, i); }

class iterator {
const PqResultHelper& outer_;
Expand All @@ -189,16 +191,16 @@ class PqResultHelper {
return outer_.result_ == other.outer_.result_ && curr_row_ == other.curr_row_;
}
bool operator!=(iterator other) const { return !(*this == other); }
PqResultRow operator*() { return PqResultRow(outer_.result_, curr_row_); }
PqResultRow operator*() const { return PqResultRow(outer_.result_, curr_row_); }
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = std::vector<PqResultRow>;
using pointer = const std::vector<PqResultRow>*;
using reference = const std::vector<PqResultRow>&;
};

iterator begin() { return iterator(*this); }
iterator end() { return iterator(*this, NumRows()); }
iterator begin() const { return iterator(*this); }
iterator end() const { return iterator(*this, NumRows()); }

private:
PGresult* result_ = nullptr;
Expand All @@ -207,7 +209,7 @@ class PqResultHelper {
Format param_format_ = Format::kText;
Format output_format_ = Format::kText;

Status PrepareInternal(int n_params, const Oid* param_oids);
Status PrepareInternal(int n_params, const Oid* param_oids) const;
};

} // namespace adbcpq

0 comments on commit b33f0a9

Please sign in to comment.