Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support encoding PG::Interval params #283

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Blacksmoke16
Copy link

Resolves #272.

I opted to encode the PG::Interval using the textual representation of a PG interval type, primarily to just have the most accuracy.

@@ -68,6 +68,11 @@ module PQ
text string
end

def self.encode(val : PG::Interval)
# https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-INTERVAL-INPUT
text "#{val.months} months #{val.days} days #{val.microseconds} microseconds"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, could move this logic into PG::Interval#to_s(io)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come no minutes or seconds? If I had something like this

db.exec("INSERT INTO events (recurring_interval) VALUES (?)", args: 5.minutes)

Wouldn't this insert 0 months 0 days 0 microseconds? Or is that unrelated to what this PR is for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered about this too. But as far as I can tell the PG::Interval type only exposes the three properties I used.

However your example would work just fine as Time::Span also works for inserting. I could also see it making sense to handle the opposite case of #272 where we allow reading INTERVAL PG types into a Time::Span. Then for most use cases you could use that, but if you really need the exact same behavior/precision you could use PG::Interval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support inserting PG::Interval instances
2 participants