connection.select_value只返回被Postgres串与皮克宝石 [英] connection.select_value only returns strings in postgres with pg gem

查看:142
本文介绍了connection.select_value只返回被Postgres串与皮克宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从用mysql(mysql2 GEM)给Postgres(PG GEM)将一个Rails应用程序。

I'm converting a rails app from using mysql (mysql2 gem) to postgres (pg gem).

与MySQL,的ActiveRecord :: Base.connection.select_value 调用返回根据数据类型的值,例如:

With mysql, ActiveRecord::Base.connection.select_value calls return values typed according to the data, for example:

> ActiveRecord::Base.connection.select_value("SELECT COUNT(*) FROM errors")
=> 86
> ActiveRecord::Base.connection.select_value("SELECT exception FROM errors where id=565")
=> "TechTalk.Genome.SqlExecutionException"
> ActiveRecord::Base.connection.select_value("SELECT id FROM errors where id=565")
=> 565

然而,Postgres的, connection.select_value 总是返回一个字符串:

However, with postgres, connection.select_value always returns a string:

> ActiveRecord::Base.connection.select_value("SELECT COUNT(*) FROM errors")
=> "1"
> ActiveRecord::Base.connection.select_value("SELECT id FROM errors")
=> "1"
> ActiveRecord::Base.connection.select_value("SELECT source FROM errors limit 1")
=> "webapp"

这打破了一些单元测试,而这些都是可以解决的,我敢肯定,我们有ç依靠这些返回值等$ C $。 有没有一种方式来获得正确类型的返回值 connection.select_value 使用Postgres的时候?

This broke a few unit tests, and while those are fixable, I'm certain we have other code relying on these return values. Is there a way to get properly-typed return values from connection.select_value when using postgres?

推荐答案

简短的答案是否定的。在PG司机故意提供薄了一层尽可能在原生'libpq的驱动程序之上。它不这样做的类型转换,因为这是对具有一些洞察其结果将被使用的域更高级别的库的责任。作出这一决定的理由是记录对PostgreSQL的维基,我很乐意与你讨论进一步在邮件列表

The short answer is no. The 'pg' driver intentionally provides as thin a layer as possible on top of the native 'libpq' driver. It doesn't do typecasting, as that's the responsibility of higher-level libraries that have some insight into the domain in which the results will be used. The rationale for this decision is documented on the PostgreSQL Wiki, and I'd be happy to discuss it with you further on the mailing list.

这篇关于connection.select_value只返回被Postgres串与皮克宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆