我有一个SQLAlchemy查询对象,想要获取已编译的SQL语句的文本,并绑定其所有参数(例如,没有%s或其他变量等待语句编译器或MySQLdb方言引擎等绑定).
调用str()查询会显示如下内容:
SELECT id WHERE date_added <= %s AND date_added >= %s ORDER BY count DESC
Run Code Online (Sandbox Code Playgroud)
我试过查询query._params,但它是一个空的字典.我使用这个sqlalchemy.ext.compiler.compiles装饰器的例子编写了我自己的编译器,但即使是那里的语句仍然有%s我想要的数据.
我无法弄清楚何时混入我的参数来创建查询; 在检查查询对象时,它们总是一个空字典(尽管查询执行正常,引擎在打开echo日志时将其打印出来).
我开始得到SQLAlchemy不希望我知道底层查询的消息,因为它打破了表达式API接口所有不同DB-API的一般性质.我不介意在我发现它之前是否执行了查询; 我只是想知道!
我一直在使用Boost序列化库,这实际上非常好,并且让我使用简单的包装器将可序列化对象保存到字符串中,如下所示:
namespace bar = boost::archive;
namespace bio = boost::iostreams;
template <class T> inline std::string saveString(const T & o) {
std::ostringstream oss;
bar::binary_oarchive oa(oss);
oa << o;
return oss.str();
}
template <class T> inline void saveFile(const T & o, const char* fname) {
std::ofstream ofs(fname, std::ios::out|std::ios::binary|std::ios::trunc);
bar::binary_oarchive oa(ofs);
oa << o;
}
template <class T> inline void loadFile(T & o, const char* fname) {
std::ifstream ifs(fname, std::ios::in|std::ios::binary);
assert(ifs.good()); // XXX catch if file not found
bar::binary_iarchive ia(ifs);
ia >> o;
} …Run Code Online (Sandbox Code Playgroud)