c++ - 有效地遍历数据库行(libpqxx),为结构分配值

标签 c++ postgresql libpqxx

我正在使用 libpqxx 从数据库中获取一行并将 pqxx::row 中的字段分配给专门设计用于保存这些值的结构:

struct driveOperationRecord
{
    long int id = 0;
    long int drive_operator_id = 0;
    long int operator_operation_index = 0;
    long int specific_operation_id = 0;
    bool operation_outcome = false;
    std::string error_code = "";
    long int operation_seconds = 0;
    std::string operation_date = "";
    std::string serial_number = "";
    std::string model = "";
    long long int size = 0;
    std::string firmware = "";
};

为此,我目前使用以下代码:

void driveOperationEntryToRecord(pqxx::row entry, driveOperationRecord& record)
{
    try
    {
        if (!entry["id"].is_null()) record.id = entry["id"].as<int>();
        if (!entry["drive_operator_id"].is_null()) record.drive_operator_id = entry["drive_operator_id"].as<int>();
        if (!entry["operator_operation_index"].is_null()) record.operator_operation_index = entry["operator_operation_index"].as<int>();
        if (!entry["specific_operation_id"].is_null()) record.specific_operation_id = entry["specific_operation_id"].as<int>();
        if (!entry["operation_outcome"].is_null()) record.operation_outcome = entry["operation_outcome"].as<bool>();
        if (!entry["error_code"].is_null()) record.error_code = entry["error_code"].as<std::string>();
        if (!entry["operation_seconds"].is_null()) record.operation_seconds = entry["operation_seconds"].as<long int>();
        if (!entry["operation_date"].is_null()) record.operation_date = entry["operation_date"].as<std::string>();
        if (!entry["serial_number"].is_null()) record.serial_number = entry["serial_number"].as<std::string>();
        if (!entry["model"].is_null()) record.model = entry["model"].as<std::string>();
        if (!entry["size"].is_null()) record.size = entry["size"].as<long long int>();
        if (!entry["firmware"].is_null()) record.firmware = entry["firmware"].as<std::string>();
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
}

这绝对是可怕的,我讨厌它。我考虑过使用具有适当枚举的开关,但迭代开关 12 次感觉更糟。理想情况下,我可以使用类似的东西:

for (auto field : entry)
{
    if (!field.is_null()) record.<if only i could dynamically reference using field.name()> = field.as<somehow magically find appropriate type>();
}

但是这些事情是不可能的。有什么想法吗?

编辑(回应无用):

当前数据库包含大约 4,000 个条目,需要相当定期地通过此函数。预计从这里开始呈指数增长。为了面向 future ,运营效率也很重要。我是 c++ 的新手,我知道有很多我不知道的东西!我只是真的希望找到稍微优雅一点的东西,并且一直在努力这样做。

最佳答案

for (auto field : entry)
{
    if (!field.is_null()) record.<if only i could dynamically reference using field.name()> = field.as<somehow magically find appropriate type>();
}

这是完全可行的,尽管不太像这样。

让我们假设我们可以记录driveOperationRecord(以下简称Record,为简洁起见)支持哪些字段,以及它们的名称和类型,以及哪些数据他们对应的成员。然后我们可以走过它们,比如:

for (auto& field : record_fields)
{
  field.assign(row, record);
}

现在我们需要一些字段描述类型,比如

struct FieldDescriptor
{
  std::string name_;

  void assign(pqxx::row const& row, Record& record)
  {
    auto entry = row[name_];
    if (!entry.is_null()) {
      // do something
    }
  }
};

到目前为止一切顺利。 “做某事”这一步是我们需要使用虚拟分派(dispatch)之类的东西的地方,因为我们要处理不同的类型。

我们可以枚举我们关心的所有类型并编写一个大的 switch 语句,但这很乏味。我会先从最简单的事情开始,如果事实证明您确实需要它更快,那么再做一些更复杂的事情:

struct FieldDescriptor
{
  std::string name_;
  std::function<void(pqxx::field const&, Record&)> assign_;

  void assign(pqxx::row const& row, Record& record)
  {
    auto entry = row[name_];
    if (!entry.is_null()) {
      assign_(entry, record);
    }
  }
};

好的,现在我们只需要一些方法来构建类似于

的函数或可调用对象
void assignFieldName(pqxx::field const& entry, Record& record) {
    record.fieldName = entry.as<decltype(record.fieldName)>();
}

例如,我们可以这样写

template <typename RecordType, typename FieldType>
FieldDescriptor describe(const char *name, FieldType RecordType::*member)
{
    return
    { std::string{name},
      [=member](pqxx::field const& entry, Record& record)
      {
        record.*member = entry.as<FieldType>();
      }
    };
}

甚至将它包装在一个宏中以节省更多的输入:

#define DESCRIBE_RECORD_FIELD(F) describe(#F, &Record:: F)

FieldDescriptor record_fields[] = {
    DESCRIBE_RECORD_FIELD(id),
    DESCRIBE_RECORD_FIELD(drive_operator_id),
    DESCRIBE_RECORD_FIELD(operator_operation_index),
    DESCRIBE_RECORD_FIELD(specific_operation_id),
    DESCRIBE_RECORD_FIELD(operation_outcome),
    DESCRIBE_RECORD_FIELD(error_code),
    DESCRIBE_RECORD_FIELD(operation_seconds),
    DESCRIBE_RECORD_FIELD(operation_date),
    DESCRIBE_RECORD_FIELD(serial_number),
    DESCRIBE_RECORD_FIELD(model),
    DESCRIBE_RECORD_FIELD(size),
    DESCRIBE_RECORD_FIELD(firmware)
};

现在,我们再次隐藏了 Record 的整个定义,这很不幸。有可能避免这种情况……但只能通过将整个定义替换为一堆执行双重职责的宏来声明成员并注册它们的描述符。

对于少量记录,我可能会坚持使用我们现有的记录。如果有很多,则值得付出更多努力实现自动化(并确保定义不会与已注册的描述符不同步)。

关于c++ - 有效地遍历数据库行(libpqxx),为结构分配值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68899042/

相关文章:

c++ - 如何更改我的应用程序使用的 .dll 搜索路径?

c# - 用于 Postgre SQL 10 的 linq2db 提供程序

sql - 错误 : function date_trunc(timestamp without time zone) does not exist

c++ - 如何使用libpqxx接收PostgreSQL数据库的通知?

c++ - 如何使用 C++ libpqxx API 将二进制数据插入到 PostgreSQL BYTEA 列中?

postgresql libpqxx 多个查询作为一个事务

c# - 模拟硬件设备

c++ - 检查数组中的数字

c++ - Objective-C 如何使用 vector 对?

sql - 选择查询需要更长的时间才能返回结果