georgysavva / scany

Library for scanning data from a database into Go structs and more
MIT License
1.27k stars 67 forks source link

Thank you for this library #12

Closed dclipca closed 2 years ago

dclipca commented 4 years ago

Thank you so much for building this library, it saved me a lot of time. You rock!

Does it support omitting certain properties from a struct?

navirick commented 4 years ago

To add to this.. does it support struct tags in any way?

georgysavva commented 4 years ago

@virtumonde Thanks for your warm words! Yes it does. you can do it via db:"-" struct tag, please see this docs section for details and example.

@navirick Yep, apart from omitting struct fields, you can set a custom column name via db struct tag, see this docs section for details and example

navirick commented 4 years ago

@georgysavva Excellent. I tried that out and it works great. Very cool. Question.. though it may not apply to this project.. is there any chance you'll add (or make another project) that allows the storage of a struct similar to how this is done? Seems like going both ways would be ideal when working with structs.

georgysavva commented 4 years ago

You mean when we have a struct filled with data and we want to save this data to the database?

I thought about implementing this actually. The only thing that stopped me is that different databases have a different symbol for query arguments:$ for postgres, ? for mysql and etc. It will require the user to configure this somehow and it will complicate the library, so I decided to not add it.

georgysavva commented 4 years ago

BTW I added the list of all features to the readme file.

navirick commented 4 years ago

Yah.. and that's a good point. Oddly I thought this was a library for pgx which was postgresql. I came to it from pgx.. but I see you support others as well. Until we have generics you would be stuck implementing db specific implementations. I am good with that :D. The name though.. scany.. may contradict the opposite direction, pushy or story... :D

georgysavva commented 4 years ago

I don't like the idea of db specific implementations. I have some plans for adding configuration ability for the user, maybe after this is implemented I can start thinking about this feature.

Alternatively, we could use this approach: For Postgres:

"INSERT INTO users (id, name, age) ($id, $name, $age)" -> "INSERT INTO users (id, name, age) ($1, $2, $3)" 

For MySQL:

"INSERT INTO users (id, name, age) (?id, ?name, ?age)" -> "INSERT INTO users (id, name, age) (?, ?, ?)" 

For SQL server:

"INSERT INTO users (id, name, age) (@id, @name, @age)" -> "INSERT INTO users (id, name, age) (@p1, @p2, @p3)" 

So the query will contain named argument with the special symbol specific for the database of the user and scany library will detect this symbol and resolve it to the proper query, valid for this database.

This way scany doesn't need any configuration from the user, because it gets the special symbol in every query. But I have some doubts about this solution as well. Tell me what you think

pashagolub commented 4 years ago

Hello. Greetings and thanks for a great piece of code.

Do you have any kind of answer how scany is similar/different to sqlx?

Some of my projects are build with sqlx, but I want to switch to pgx and that's where scany arrives. :)

Thanks in advance!

georgysavva commented 4 years ago

@pashagolub Thanks for reaching out!

  1. scany supports both database/sql and pgx native interface (when you use pgx as a standalone library, without database/sql layer). Also it can be extended to work with any db library. On the other hand, sqlx only works with database/sql.
  2. In terms of scanning and mapping abilities, scany provides all features of sqlx.
  3. Scany has a much simpler API/interface and much fewer concepts compared to sqlx, so I believe it should be easier to use.
  4. Scany doesn't support the opposite direction of mapping when you insert data from your struct into the query, at least for now. We had a discussion about implementing this: https://github.com/georgysavva/scany/issues/12#issuecomment-682006759. sqlxhas this feature.

Those are the main points.

UPD: added section to readme with the comparison with sqlx

anton7r commented 2 years ago

I don't like the idea of db specific implementations. I have some plans for adding configuration ability for the user, maybe after this is implemented I can start thinking about this feature.

Alternatively, we could use this approach: For Postgres:

"INSERT INTO users (id, name, age) ($id, $name, $age)" -> "INSERT INTO users (id, name, age) ($1, $2, $3)" 

For MySQL:

"INSERT INTO users (id, name, age) (?id, ?name, ?age)" -> "INSERT INTO users (id, name, age) (?, ?, ?)" 

For SQL server:

"INSERT INTO users (id, name, age) (@id, @name, @age)" -> "INSERT INTO users (id, name, age) (@p1, @p2, @p3)" 

So the query will contain named argument with the special symbol specific for the database of the user and scany library will detect this symbol and resolve it to the proper query, valid for this database.

This way scany doesn't need any configuration from the user, because it gets the special symbol in every query. But I have some doubts about this solution as well. Tell me what you think

@georgysavva are these named query parameters already implemented into scany? i could perhaps integrate that feature set from my library (https://github.com/anton7r/pgxe) into scany.

georgysavva commented 2 years ago

Hey @anton7r. No, I actually haven't started working on this yet. So feel free to shoot a PR with your integration!

anton7r commented 2 years ago

@georgysavva yeah, opened it #71