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

How to use my own date type with pgx #1132

Closed
kaatinga opened this issue Dec 27, 2021 · 5 comments
Closed

How to use my own date type with pgx #1132

kaatinga opened this issue Dec 27, 2021 · 5 comments

Comments

@kaatinga
Copy link

Howdy folks!

In case I use my own date model, how to use it directly with Scan(), Query, QueryRow and Exec? Did not find an obvious way to do that.

@jackc
Copy link
Owner

jackc commented Dec 27, 2021

There are multiple ways.

pgx has a compatibility layer for database/sql style types. The simplest approach is to implement Scan() and Value() as you would for database/sql.

If that does not provide sufficient performance you can implement pgtype.Value, pgtype.BinaryDecoder, pgtype.TextDecoder, pgtype.BinaryEncoder, and pgtype.TextEncoder. Most of that could be reused from pgtype.Date.

@kaatinga
Copy link
Author

There are multiple ways.

pgx has a compatibility layer for database/sql style types. The simplest approach is to implement Scan() and Value() as you would for database/sql.

If that does not provide sufficient performance you can implement pgtype.Value, pgtype.BinaryDecoder, pgtype.TextDecoder, pgtype.BinaryEncoder, and pgtype.TextEncoder. Most of that could be reused from pgtype.Date.

Thank you for your response, @jackc! Will try.

@kaatinga
Copy link
Author

kaatinga commented Oct 21, 2022

Hi @jackc!

I know you are very busy, but I hope you will find time for me here!

I have finished to test my solution, the question is the following. In my tests only EncodeBinary and DecodeBinary are eventually called. The pair of EncodeText and DecodeText do not seem to be used. Their absence means nothing. How they might be used, in which case?

The commit with the code is here:
kaatinga/bublyk@v1.0.3...v1.0.4#diff-1f00a7010439f4727593a8cb04b229b647be3815d9ceb23638d4d496e7546b51

@jackc
Copy link
Owner

jackc commented Oct 22, 2022

I have finished to test my solution, the question is the following. In my tests only EncodeBinary and DecodeBinary are eventually called. The pair of EncodeText and DecodeText do not seem to be used. Their absence means nothing. How they might be used, in which case?

The binary format is usually faster. So pgx will usually prefer that when it is available. Under most conditions the text format will not be used. If you want to force the text format then you could connect to pgx with prefer_simple_protocol=true in your connection string.

@kaatinga
Copy link
Author

Thank you @jackc for the answer!

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

No branches or pull requests

2 participants