Skip to content

Commit

Permalink
[#24754] YSQL: Cleanup PgStatement* casts
Browse files Browse the repository at this point in the history
Summary:
There are lot of boiler plate code in `pggate.cc` related to `PgStatement*` cast.

```
Status PgApiImpl::AlterTableAddColumn(PgStatement *handle, const char *name,
                                      int order,
                                      const YBCPgTypeEntity *attr_type,
                                      YBCPgExpr missing_value) {
  if (!PgStatement::IsValidStmt(handle, StmtOp::STMT_ALTER_TABLE)) {
    // Invalid handle.
    return STATUS(InvalidArgument, "Invalid statement handle");
  }

  PgAlterTable *pg_stmt = down_cast<PgAlterTable*>(handle);
  return pg_stmt->AddColumn(name, attr_type, order, missing_value);
}

Status PgApiImpl::AlterTableRenameColumn(PgStatement *handle, const char *oldname,
                                         const char *newname) {
  if (!PgStatement::IsValidStmt(handle, StmtOp::STMT_ALTER_TABLE)) {
    // Invalid handle.
    return STATUS(InvalidArgument, "Invalid statement handle");
  }

  PgAlterTable *pg_stmt = down_cast<PgAlterTable*>(handle);
  return pg_stmt->RenameColumn(oldname, newname);
}
```

It is reasonable to introduce template helper function `GetStatementAs<...>` to make the code above look like

```
Status PgApiImpl::AlterTableAddColumn(
    PgStatement *handle, const char *name, int order, const YBCPgTypeEntity *attr_type,
    YBCPgExpr missing_value) {
  return VERIFY_RESULT_REF(GetStatementAs<PgAlterTable>(handle)).AddColumn(
      name, attr_type, order, missing_value);
}

Status PgApiImpl::AlterTableRenameColumn(
    PgStatement *handle, const char *oldname, const char *newname) {
  return VERIFY_RESULT_REF(GetStatementAs<PgAlterTable>(handle)).RenameColumn(oldname, newname);
}
```

Test Plan: Jenkins

Reviewers: pjain, sergei, kramanathan, jason, tnayak

Reviewed By: tnayak

Subscribers: ybase, yql

Tags: #jenkins-ready

Differential Revision: https://phorge.dev.yugabyte.com/D39460
  • Loading branch information
d-uspenskiy committed Nov 12, 2024
1 parent 85247b4 commit e86712a
Show file tree
Hide file tree
Showing 16 changed files with 905 additions and 1,099 deletions.
16 changes: 8 additions & 8 deletions src/yb/util/strongly_typed_bool.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#pragma once

#include <vector>
#include <initializer_list>

#include <boost/preprocessor/cat.hpp>

Expand All @@ -31,8 +31,8 @@ template <class Tag>
struct StronglyTypedBoolProxy {
bool value;

operator bool() const { return value; }
StronglyTypedBoolProxy<Tag> operator!() const { return StronglyTypedBoolProxy<Tag>{!value}; }
constexpr operator bool() const { return value; }
constexpr auto operator!() const { return StronglyTypedBoolProxy<Tag>{!value}; }
};

template <class Tag>
Expand All @@ -44,17 +44,17 @@ class StronglyTypedBool {
StronglyTypedBool<Tag>(false), StronglyTypedBool<Tag>(true)
};

StronglyTypedBool(StronglyTypedBoolProxy<Tag> proxy) : value_(proxy.value) {} // NOLINT
constexpr StronglyTypedBool(StronglyTypedBoolProxy<Tag> proxy) : value_(proxy.value) {} // NOLINT

// This is public so that we can construct a strongly-typed boolean value out of a regular one if
// needed. In that case we'll have to spell out the class name, which will enforce readability.
explicit constexpr StronglyTypedBool(bool value) : value_(value) {}
constexpr explicit StronglyTypedBool(bool value) : value_(value) {}

// These operators return regular bools so that it is easy to use strongly-typed bools in logical
// expressions.
operator bool() const { return value_; }
StronglyTypedBool<Tag> operator!() const { return StronglyTypedBool<Tag>(!value_); }
bool get() const { return value_; }
constexpr operator bool() const { return value_; }
constexpr auto operator!() const { return StronglyTypedBool<Tag>(!value_); }
constexpr bool get() const { return value_; }

private:
bool value_;
Expand Down
Loading

0 comments on commit e86712a

Please sign in to comment.