VinGarcia / ksql

A Simple and Powerful Golang SQL Library
MIT License
308 stars 22 forks source link

support for database/sql.Scanner and database/sql/driver.Valuer #39

Closed phenpessoa closed 1 year ago

phenpessoa commented 1 year ago

Does this lib offers support for database/sql.Scanner and database/sql/driver.Valuer interfaces for custom types?

Custom use case is for null fields where you don't want to (or can't) add pointers, for example.

database/sql.NullTime is one of the most famous use cases.

VinGarcia commented 1 year ago

Hey @phenpessoa sorry for the delay I missed the email notification. Yes, it does. But what do you mean by custom types?

phenpessoa commented 1 year ago

Hey @VinGarcia no worries! For example, with pgx we can use decimal for Numeric values on PostgreSQL. This is thanks to database/sql.Scanner and database/sql/driver.Valuer.

But I can create any custom type I want and serialize/deserialize them to/from the database as long as they implement those interfaces.

Check this PR for an example.

VinGarcia commented 1 year ago

Hey @phenpessoa, yeah this is actually defined by the sql package and its a standard.

About KSQL in particular it doesn't do much with the arguments you pass to it, except if you decide to use a modifier.

So if you just send us an attribute that implements sql.Scanner or sql.Valuer it will just be forwarded to the underlying driver, in your case pgx.

But this issue makes me think of two things:

  1. I need to write some examples of what you can do using pgx.
  2. I need to think about whether it is possible to support sql.Scanner and sql.Valuer even when the attribute is using a modifier, it might be possible 🤔

Again sorry for taking so long to answer you xP. I have finished a couple tasks I was working on these last weeks so I will try to improve on this issue this next few weeks.

Thanks for asking.

VinGarcia commented 1 year ago

Also just so you understand what I mean about a modifier:

https://github.com/VinGarcia/ksql/wiki/Modifiers

It is an extra annotation on the ksql tag. e.g.: ksql:"address,json"

The modifier can specify a scanner and/or valuer function so if it does we will use that instead of the .Scan or .Value functions of your type, at least that is how it is right now. I may be able to check if the original type implements Scan and/or Value and call this function myself inside the modifier functions.

VinGarcia commented 1 year ago

Hey @phenpessoa, I finally improved the docs on this, on the README I am now mentioning it as an outstanding feature:

https://github.com/VinGarcia/ksql#outstanding-features

And I also updated the "Modifiers" Wiki page to explain that KSQL supports the Scanner and Valuer interfaces and also explain the differences between these interfaces and the Modifiers:

https://github.com/VinGarcia/ksql/wiki/sql.Scan,-sql.Value-and-Modifiers

VinGarcia commented 1 year ago

I will leave this Issue open for a few days, if you want me to add anything to these docs just say so. After that, I will close the issue but feel free to create a new one at any time.

Kind Regards.

phenpessoa commented 1 year ago

I think the docs now make it crystal clear. Thank you!

VinGarcia commented 1 year ago

Nice, I will close this issue then, feel free to open others whenever you need =]