Re: generate enum from db at runtime
Thomas Fox <[email protected]> Wed, 15 Oct 2014 12:54:08 +0200 (CEST)
| Newsgroups | gmane.comp.jakarta.turbine.torque.user |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_5750475_1109879854.1413370448000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Ok so the agreement is that colum enums should be supported but table enmus= should not.=20 Thomas=20 ----- Urspr=C3=BCngliche Mail ----- Von: "Youngho Cho" <[email protected]>=20 An: "Thomas Fox" <[email protected]>=20 Gesendet: Mittwoch, 15. Oktober 2014 12:18:33=20 Betreff: Re: Fwd: generate enum from db at runtime=20 Hello Thomas=20 you are right !=20 I don't need primary value in the enum.=20 I have.getByName Method in the manager.=20 So only NameValue is ABSOLUTELY right!=20 Thanks.=20 Youngho=20 2014. 10. 15. =EC=98=A4=ED=9B=84 4:53=EC=97=90 "Thomas Fox" < thomas.fox@se= itenbau.com >=EB=8B=98=EC=9D=B4 =EC=9E=91=EC=84=B1:=20 ----- Forwarded Mail -----=20 Von: "Thomas Fox" < [email protected] >=20 An: "Apache Torque Users List" < [email protected] >=20 Gesendet: Mittwoch, 15. Oktober 2014 09:52:13=20 Betreff: Re: generate enum from db at runtime=20 Hi Youngho,=20 I am afraid my current draft does not allow TABLE enums (as you use), only = COLUMN enums (which was my primary use case).=20 I now see that your use case also makes sense, I did not think of it before= .=20 For your current ROLE table, do you need the ROLE_NAME in database?=20 If not, you could still use the column enum solution for the id column.=20 I need to think about whether it would be possible to make Tables enum.=20 Perhaps something like this=20 <table name=3D"TURBINE_ROLE" idMethod=3D"idbroker" isEnum=3D"true">=20 <column name=3D"ROLE_ID" required=3D"true" primaryKey=3D"true" type=3D"INTE= GER">=20 <enum-value value=3D"1"/>=20 <enum-value value=3D"2"/>=20 <enum-value value=3D"3"/>=20 </column>=20 <column name=3D"ROLE_NAME" required=3D"true" size=3D"99" type=3D"VARCHAR" j= avaName=3D"Name">=20 <enum-value value=3D"Guest"/>=20 <enum-value value=3D"Member"/>=20 <enum-value value=3D"Owner"/>=20 <column>=20 <unique>=20 <unique-column name=3D"ROLE_NAME"/>=20 </unique>=20 </table>=20 Obviously, there needs to be the same number of <enum-value> elements for e= ach column for this to work.=20 About primary keys: in most databases you can define primary key values alt= hough the database can determine it itself.=20 So in your case It should be possible to fill the TURBINE_ROLE table by a m= anual SQL where you determine the id (and not the database).=20 Besides: is there any reason you use idbroker? All currently supported data= bases have a native mechanism to create ids.=20 Thomas=20 ----- Urspr=C3=BCngliche Mail -----=20 Von: "Youngho Cho" < [email protected] >=20 An: "Thomas Fox" < [email protected] >=20 CC: "Apache Torque Users List" < [email protected] >=20 Gesendet: Mittwoch, 15. Oktober 2014 09:25:25=20 Betreff: Re: generate enum from db at runtime=20 Hello Thomas,=20 Ah now I understand your previous message regarding reverse engineering.=20 The primary key information is allocated by the inserting the role=20 therefore torque generator has no way to know the primary key value=20 when it generate the enum=20 Umm..=20 But we can set the primary key value in the schema enum-value, don't you ?= =20 also the description value is also needed as you mentioned in the jira.=20 Thanks,=20 Youngho=20 2014-10-15 15:52 GMT+09:00 Youngho Cho < [email protected] >:=20 > Hello Thomas,=20 >=20 > I generate enum with your working draft for TORQUE-331.=20 > Looks very cool feature !.=20 >=20 > I would like to give to my enum usage with om object.=20 > ( I am not sure how you use enum with om but please let me know you my us= age )=20 >=20 > For the role table=20 >=20 > <table name=3D"TURBINE_ROLE" idMethod=3D"idbroker">=20 > <column name=3D"ROLE_ID" required=3D"true" primaryKey=3D"true" type=3D"IN= TEGER"/>=20 > <column name=3D"ROLE_NAME" required=3D"true" size=3D"99" type=3D"VARCHAR"= =20 > javaName=3D"Name"/>=20 > <unique>=20 > <unique-column name=3D"ROLE_NAME"/>=20 > </unique>=20 > </table>=20 >=20 > I made RoleEnum.java manually likes=20 > ( enum members are existing column values in the database)=20 >=20 > public enum RoleEnum=20 > {=20 > Guest(1, "Guest"),=20 > Member(2, "Member"),=20 > Owner(3, "Owner");=20 >=20 > private Integer id;=20 > private String name;=20 >=20 > private RoleEnum(final Integer id, final String name)=20 > {=20 > this.id =3D id;=20 > this.name =3D name;=20 > }=20 >=20 > public Integer getId()=20 > {=20 > return this.id ;=20 > }=20 >=20 > public String getName()=20 > {=20 > return this.name ;=20 > }=20 >=20 > public TurbineRole getInstance()=20 > throws TorqueException=20 > {=20 > return TurbineRoleManager.getInstance(getId());=20 > }=20 >=20 > public boolean represent(TurbineRole role)=20 > {=20 > return this.id.equals(role.getRoleId());=20 > }=20 >=20 > public static Set<RoleEnum> getAllEnum()=20 > {=20 > return EnumSet.allOf(RoleEnum.class);=20 > }=20 > }=20 >=20 > ----------------------------------------=20 > And add method in TurbineRole.java=20 >=20 > public RoleEnum getEnum()=20 > {=20 > return RoleEnum.valueOf(getName());=20 > }=20 >=20 >=20 > ----------------------------------------------------------------=20 > At the java code or template I use=20 >=20 > Role role =3D RoleEnum.valueOf(cond.getValue()).getInstance();=20 > ....=20 >=20 > or=20 >=20 > if(RoleEnum.Guest.represent(role)){=20 > ....=20 > }=20 >=20 > ..=20 >=20 > using custom template, we can add custom method for their usage.=20 > But for the the enum private fields, I hope that=20 > we need unique name and their primary key value also I think.=20 >=20 > Thanks,=20 >=20 > Youngho=20 >=20 >=20 > 2014-10-10 16:49 GMT+09:00 Thomas Fox < [email protected] >:=20 >> Hi Youngo=20 >>=20 >> Torque is built on the idea that the database schema is static, i.e. doe= s not change at runtime, and that the database structure is defined in the = schema file, not in the database.=20 >> There is some benefit in reverse-engineering from the database, however = it is also a very difficult and large field. The "Torque main path" is to d= efine the structure in the schema xml file and then re-run the generator, n= ot to change the database and re-engineer it.=20 >> Therefore, to support enums, I have created a ticket in Jira https://iss= ues.apache.org/jira/browse/TORQUE-331 , which supports enum definition in t= he schema.xml. I'm currently working on it, because I have also missed the = enum feature.=20 >>=20 >> To elaborate the reverse engineering from the database: The generator ca= n generate a schema file from the database, but it is not in the core focus= of the Torque team and only works for very coarse features. See http://db.= apache.org/torque/torque-4.0/documentation/orm-reference/running-the-genera= tor.html#Generation_of_XML_schema_from_an_existing_database . However, if y= ou'd like to improve it, please go ahead.=20 >>=20 >> Thomas=20 >>=20 >> ----- Urspr=C3=BCngliche Mail -----=20 >> Von: "Youngho Cho" < [email protected] >=20 >> An: "Thomas Fox" < [email protected] >=20 >> CC: "Apache Torque Users List" < [email protected] >=20 >> Gesendet: Donnerstag, 9. Oktober 2014 09:06:17=20 >> Betreff: Re: generate enum from db at runtime=20 >>=20 >> Hello Thomas,=20 >>=20 >> I think "generated at runtime" was a little exaggerated expression=20 >> because of my lack of english expression.=20 >>=20 >> It means that=20 >> If ROLE table changed by adding a new role such as 'Owner' Role,=20 >> than some task run torque generator to generate new RoleEnum class=20 >> which is include the Owner.=20 >>=20 >> If torque has an ability to read database and generate corresponding=20 >> enum, I think it is very useful feature.=20 >> ( It is very cumbersome work to add/remove by hand whenever some kind=20 >> of type table changed)=20 >>=20 >> And your eum with om usage tip is very helpful to me.=20 >> Thanks a lot.=20 >>=20 >> Thanks,=20 >>=20 >> Youngho=20 >>=20 >>=20 >> 2014-10-08 21:36 GMT+09:00 Thomas Fox < [email protected] >:=20 >>> Hi Youngho,=20 >>>=20 >>> currently it is not possible to generate enums. This would be an intere= sting feature in my opinion.=20 >>> It needs to be seen whether this requires a schema change.=20 >>>=20 >>> I'm not sure what you mean by "generated at runtime".=20 >>>=20 >>> What I currently do when I need an enum is to define "internal" methods= which take and return strings and on top of that hand-written methods whic= h take and return the enum. It looks something like:=20 >>>=20 >>> in the schema:=20 >>> <column name=3D"role" type=3D"VARCHAR" javaName=3D"roleInternal" />=20 >>>=20 >>> in the data object (assuming Role is an enum):=20 >>> public void setRole(Role role)=20 >>> {=20 >>> super.setRoleInternal(Role.toString());=20 >>> }=20 >>>=20 >>> public Role getRole()=20 >>> {=20 >>> return Role.valueOf(super.getRoleInternal());=20 >>> }=20 >>>=20 >>> // only for torque's internal use=20 >>> @Deprecated=20 >>> @Override=20 >>> public String getRoleInternal()=20 >>> {=20 >>> return super.getRoleInternal();=20 >>> }=20 >>>=20 >>> // only for torque's internal use=20 >>> @Deprecated=20 >>> @Override=20 >>> public void setRoleInternal(String role)=20 >>> {=20 >>> super.setRoleInternal(role);=20 >>> }=20 >>>=20 >>> Thomas=20 >>>=20 >>> ----- Urspr=C3=BCngliche Mail -----=20 >>> Von: "Youngho Cho" < [email protected] >=20 >>> An: "Thomas Fox" < [email protected] >, "Apache Torque Users Lis= t" < [email protected] >=20 >>> Gesendet: Mittwoch, 8. Oktober 2014 08:32:10=20 >>> Betreff: generate enum from db at runtime=20 >>>=20 >>> Hello Thomas,=20 >>>=20 >>> Can torque generate enum class from database ?=20 >>>=20 >>> For example,=20 >>>=20 >>> following turbine-fucrum-torque security model=20 >>>=20 >>> http://svn.apache.org/viewvc/turbine/fulcrum/trunk/security/torque/sche= ma/fulcrum-turbine-schema.xml?revision=3D1575232&view=3Dmarkup=20 >>>=20 >>> has Role, Permission table.=20 >>>=20 >>> And It can be added during system running.=20 >>>=20 >>> When those table column added/removed,=20 >>> I hope to running maven-plugin or ant target etc.. to generate=20 >>> corresponding enum class.=20 >>>=20 >>> Is it possible senario ?=20 >>>=20 >>>=20 >>> Thanks,=20 >>>=20 >>> Youngho=20 >>>=20 >>> ---------------------------------------------------------------------= =20 >>> To unsubscribe, e-mail: [email protected]=20 >>> For additional commands, e-mail: [email protected]=20 >>>=20 >>=20 >> ---------------------------------------------------------------------=20 >> To unsubscribe, e-mail: [email protected]=20 >> For additional commands, e-mail: [email protected]=20 >>=20 ---------------------------------------------------------------------=20 To unsubscribe, e-mail: [email protected]=20 For additional commands, e-mail: [email protected]=20 ------=_Part_5750475_1109879854.1413370448000--