【问题标题】:How to eliminate multiple similar sql queries with one resultset handler?如何使用一个结果集处理程序消除多个类似的 sql 查询?
【发布时间】:2015-05-22 03:29:30
【问题描述】:

我有一些类似的sql查询,所有选择的字段都是一样的,只是条件不同。因此,结果集处理程序也是相同的。我使用 sqlite3 和sqlite_modern_cpp。

这里有两个类似的功能(还有其他几个):

QVariantMap getItemById(int id) {
  QVariantMap map;
  database db(dbfile);
  db << "select id,name,price,qty from items where id=?"
     << id 
     >> [&] (int id, std::string name, double price, int qty) {
     map.insert("id", id);
     map.insert("name", QString::fromStdString(name));   
     map.insert("price", price);
     map.insert("qty", qty);
  };
  return map;
}

QVariantMap getItemByCatAndSeq(int cat, int seq) {
  QVariantMap map;
  database db(dbfile);
  db << "select id,name,price,qty from items where cat=? and seq=?"
     << cat << seq 
     >> [&] (int id, std::string name, double price, int qty) {
     map.insert("id", id);
     map.insert("name", QString::fromStdString(name));   
     map.insert("price", price);
     map.insert("qty", qty);
  };
  return map;
}

您看,查询中的所有字段都是“id,name,price,qty”,结果集处理程序也相同。

一个结果集处理程序就足够了,但我必须在每个函数中复制和粘贴相同的代码。另外,所选字段的名称和顺序必须与参数resultset handler--lambda函数相匹配。

如何消除这种重复代码?有没有更好的解决方案?提前致谢。

编辑:

我尝试了仿函数的方式,如下:

struct ItemResultHandler {
    explicit ItemResultHandler(QVariantMap& map_) : map(map_) {}
    void operator() ( int id, std::string name, double price, int qty ) {
         map.insert("id", id);
         map.insert("name", QString::fromStdString(name));   
         map.insert("price", price);
         map.insert("qty", qty);
    }
    QVariantMap& map;
};

上面两个类似的函数之一会变成这样:

QVariantMap getItemById(int id) {
  QVariantMap map;
  database db(dbfile);
  db << "select id,name,price,qty from items where id=?"
     << id 
     >> ItemResultHandler(map);
  return map;
}

但是,它有编译错误:

../../myapp/src/utility/function_traits.h:12: error: type 'void (ItemResultHandler::*)(std::__1::basic_string<char>, std::__1::basic_string<char>, int, int)' cannot be used prior to '::' because it has no members
        decltype(&Function::operator())
                  ^

 ../../myapp/src/sqlite_modern_cpp.h:218: error: incomplete definition of type 'sqlite::utility::function_traits<ItemResultHandler>'
                        binder<traits::arity>::run(*this, func);
                               ~~~~~~^~

【问题讨论】:

  • 这似乎太明显了,但你不能把重复的代码放在一个函数中吗?
  • 谢谢。变量映射是本地的,将重复的代码移动到函数中意味着我必须使用成员变量。这不是一个好办法。我尝试过仿函数,但它有编译错误。
  • 通过引用传递变量映射?

标签: c++ database qt c++11 sqlite


【解决方案1】:

映射我们处理的所有不同结果类型是个问题。所以模板变得有点复杂,看起来它不能处理这种情况。

这是一个示例,您可以如何使用类型扩展来做到这一点。

// The thing you want
struct Item {
    int id;
    string name;
    double price;
    int qty;
};
// to get it back we need to give a deserialization method:
namespace sqlite {
    template<> void get_col_from_db(database_binder& db, int inx, ::Item*& ret) {   // specify the type it will be used on, a pointer is necessary if you’re type has no default constructor. Otherwise a object ref does the job too.
        ret = new Item();
        get_col_from_db(db, inx++, ret->id);
        get_col_from_db(db, inx++, ret->name);
        get_col_from_db(db, inx++, ret->price);
        get_col_from_db(db, inx++, ret->qty);
    }
}

{// usage
    Item* item;
    db << "select val,name,price,qty from test" >> item;
    std::cout << item->name;
    delete item;
}

但这很丑,肯定需要改变,我将在下一个版本中摆脱这个指针混乱。

但是有一个更短的版本,只需用这种方式将你的仿函数转换为 lambda,用我的结构替换你的 QMap:

auto ItemResultHandler = [](Item& item) -> std::function<void(int, std::string , double , int )> {
    return [&item](int id, std::string name, double price, int qty) mutable {
        item.id = id;
        item.name = name;
        item.price = price;
        item.qty = qty;
    };
};

然后像这样使用它:

Item item;
db << "select val,name,price,qty from test" >> ItemResultHandler(item);

【讨论】:

  • 非常感谢。我使用了较短的版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多