Perl DBIx::Class 能否覆盖从数据库中检索列的方式? [英] Can Perl DBIx::Class override the way a column is retrieved from the database?
问题描述
直到今天我才使用 DBIx::Class,所以我对它完全陌生.
I have never used DBIx::Class until today, so I'm completely new at it.
我不确定这是否可行,但基本上我的 SQLite 数据库中有一个表,其中有一个时间戳列.时间戳列的默认值为CURRENT_TIMESTAMP".SQLite 将其存储在 GMT 时区,但我的服务器位于 CDT 时区.
I'm not sure if this is possible or not, but basically I have a table in my SQLite database that has a timestamp column in it. The default value for the timestamp column is "CURRENT_TIMESTAMP". SQLite stores this in the GMT timezone, but my server is in the CDT timeszone.
我用来获取正确时区时间戳的 SQLite 查询是这样的:
My SQLite query to get the timestamp in the correct timezone is this:
select datetime(timestamp, 'localtime') from mytable where id=1;
我想知道在我的 DBIx 模式中是否可以使用MyTable"强制它在每次检索时间戳"时应用日期时间函数;数据库中的字段?
I am wondering if it is possible in my DBIx schema for "MyTable" to force it to apply the datetime function every time it is retrieving the "timestamp" field from the database?
在 cookbook 看起来在使用 ->search() 函数时可以做到这一点,但我想知道是否可以这样做,如果我使用的是 search()、find()、all(),find_or_new() 或任何将从数据库中提取此列的函数,它会将 datetime() SQLite 函数应用于它吗?
In the cookbook it looks like it is possible to do this when using the ->search() function, but I am wondering if it's possible to make it so if I'm using search(), find(), all(), find_or_new(), or any function that will pull this column from the database, it will apply the datetime() SQLite function to it?
DBIx::Class 似乎有很好的文档 - 我想我对它太陌生了,我没有找到合适的地方/东西来搜索.
DBIx::Class seems to have great documentation - I think I'm just so new at it I'm not finding the right places/things to search for.
提前致谢!
推荐答案
我已经以这种方式使用了 InflateColumn::DateTime 并带有时间戳,我可以确认它有效,但我想知道您是否有这种倒退.
I've used InflateColumn::DateTime in this way and with a timestamp, and I can confirm it works, but I wonder if you have this backward.
如果您的列是 UTC,则将该列标记为 UTC,然后加载它时应该是 UTC 时间.然后,当您在 DateTime 上 set_timezone 时(大概这将是一个输出问题 - 在输出时您关心它是本地分区的)您可以将其设置为本地时间,它会进行必要的调整.
If your column is in UTC, mark the column UTC, and then it should be a UTC time when you load it. Then when you set_timezone on the DateTime (presumably that would be an output issue - it's at output that you care it's locally zoned) you can set it to local time and it will make the necessary adjustment.
这篇关于Perl DBIx::Class 能否覆盖从数据库中检索列的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!