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

Enum codegen example #171

Open
Yaneeve opened this issue Jun 16, 2015 · 17 comments
Open

Enum codegen example #171

Yaneeve opened this issue Jun 16, 2015 · 17 comments

Comments

@Yaneeve
Copy link

Yaneeve commented Jun 16, 2015

Thanks for your awesome work!

Could you please provide an example where I would generate code that would convert a postgres enum. I am new to scala and slick, and can't seem to get this right.

Thanks!

@tminglei
Copy link
Owner

Well, I'll update the code gen example project asap.

@Yaneeve
Copy link
Author

Yaneeve commented Jun 21, 2015

Thanks! Appreciate it very much :)

@tminglei
Copy link
Owner

tminglei commented Jul 8, 2015

Hi @Yaneeve, I have submitted some updates in this pull, but it's not ready yet.

@Yaneeve
Copy link
Author

Yaneeve commented Jul 8, 2015

Yeah, I noticed... I certainly appreciate your responsiveness :) take as much time as you need

@tminglei
Copy link
Owner

tminglei commented Jul 8, 2015

Ok, I need more time. ;-)

@bearrito
Copy link
Contributor

I have working example if interested.

@tminglei
Copy link
Owner

@bearrito, yes, that's great. Can you send it to me?
Thanks so much!

@victrcodes
Copy link

I can see this issue is still open? Is there a clear example somewhere of slick codegen working with Postgres enum types?

@tminglei
Copy link
Owner

tminglei commented Jan 7, 2016

Not yet.

But pg enum types can only be mapped to String, since it can't distinguish pg enum types from others and generate a scala type for them.

@victrcodes
Copy link

Curiously, just to note something - JOOQ code generator works just fine with this situation - recognizes postgres enum types just fine and creates java enums accordingly, so it's not impossible, is it?

@tminglei
Copy link
Owner

tminglei commented Jan 7, 2016

@VRcOd thanks for the reminding! :-)

I'll check JOOQ's codes to find how to distinguish and generate it.

@tlewowski
Copy link

just a follow up - I'd also love to see possibility of generating enumeration classes from database schema.

For my case (it's the simplest - just download all enum values together with labels and orders) sufficient SQL query was

select t.typename, e.enumlabel, e.enumsortorder from pg_enum e 
  inner join pg_type t on t.oid=e.enumtypid

then some postprocessing is needed, but it generates just fine.

I should be able to post a full solution to GitHub later this week, let me know if you're interested

@tminglei
Copy link
Owner

tminglei commented Sep 7, 2017

@tlewowski yes, pls. 👍

@tlewowski
Copy link

tlewowski commented Sep 8, 2017

I've posted a project here: https://github.com/tlewowski/pg-scala-enums

what it needs:

  • adjustment of dbUrl in build.sbt
  • direct access to database for executing user (no user/password thing)

what it does:

  • fetches all enums via vanilla JDBC (no layers in between)
  • generates a file with all enumerations (as subclasses of Enumeration) - gen-enums task
  • generates a profile, based on ExPostgresProfile with many extensions (not configurable, changing extensions requires changes in code), with implicits for generated enumerations included - gen-pg-profile task
  • generates a file with tables, where enumerations are included as normal types - gen-tables

stuff like package or file name is hardcoded in several places and naming convention for generated classes is pretty opinionated, but I don't think it'd be a big deal to change it if anybody needs it.

quite bad news is that it needs three execution steps - generate enum, generate profile and generate tables (they are now in three separate, dependent projects). This is required, because enums are needed for profile generation (profile includes implicits for them) and profile is (I suppose?) needed for downloading tables.

I've checked the mapping in my project (not included) and it works well with PgEnumSupport.

BTW: thanks for this project, it really helps a lot!

@tminglei
Copy link
Owner

@tlewowski it looks good. :-)

@tlewowski
Copy link

well, the thing is that it doesn't fit very well into the current flow, so I'm not sure if it's mergeable anyhow, or should I maintain it as separate project (ideally an SBT plugin). any ideas?

@tminglei
Copy link
Owner

I think it should be kept as a separate project, since it's more like a solution instead of a patch.

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

No branches or pull requests

5 participants