[CDO] Programmatically get the DB mapping [message #788790] |
Thu, 02 February 2012 03:17  |
Eclipse User |
|
|
|
Hi everybody,
Is there a way the consult the DB mapping programmatically ?
My needs is to know:
- What is the table name for a given EClass ?
- What is the column name for the given EStructuralFeature ?
Thanks in adavnce.
|
|
|
|
Re: [CDO] Programmatically get the DB mapping [message #794650 is a reply to message #794291] |
Thu, 09 February 2012 09:34   |
Eclipse User |
|
|
|
Hi Christophe,
comments below.
Am 09.02.12 06:25, schrieb Eike Stepper:
> Hi Christophe,
>
> I apologize for the late reply.
me too :-(
> Comments below...
>
>
> Am 02.02.2012 09:17, schrieb Christophe Moine:
>> Hi everybody,
>>
>> Is there a way the consult the DB mapping programmatically ?
Other that the comment of Eike, you can also specify the table names for
EClasses as an EAnnotation.
The following code is from DBAnnotationsTest.java:
private void addTableNameAnnotation(EPackage model1, String value)
{
EAnnotation annotation = EcoreFactory.eINSTANCE.createEAnnotation();
annotation.setSource("http://www.eclipse.org/CDO/DBStore");
annotation.getDetails().put("tableName", value);
EClass category = (EClass)model1.getEClassifier("Category");
category.getEAnnotations().add(annotation);
}
private void addColumnNameAnnotation(EPackage model1, String value)
{
EAnnotation annotation = EcoreFactory.eINSTANCE.createEAnnotation();
annotation.setSource("http://www.eclipse.org/CDO/DBStore");
annotation.getDetails().put("columnName", value);
EClass category = (EClass)model1.getEClassifier("Category");
EStructuralFeature element =
category.getEStructuralFeature(Model1Package.CATEGORY__NAME);
element.getEAnnotations().add(annotation);
}
Cheers,
Stefan
> Sure, but not on the client. On the CDO server you can ask:
>
> IRepository repository = getRepository();
>
> EClass eClass = getModel1Package().getAddress();
>
> IDBStore store = (IDBStore)repository.getStore();
> IMappingStrategy mappingStrategy = store.getMappingStrategy();
> IClassMapping classMapping = mappingStrategy.getClassMapping(eClass);
>
> IDBTable table = classMapping.getDBTables().get(0);
> System.out.println(eClass.getName() + " --> " + table);
>
> for (ITypeMapping valueMapping : classMapping.getValueMappings())
> {
> System.out.println(" " + valueMapping.getFeature().getName() + " --> " +
> valueMapping.getField());
> }
>
> The output will be:
>
> Address --> model1_Address
> name --> name
> street --> street
> city --> city
> ------------------------- END -------------------------
>
> All columns (fields), including the cdo system ones, can be listed with:
>
> for (IDBField field : table.getFields())
> {
> System.out.println(" " + field);
> }
>
> The output will be:
>
> model1_Address
> cdo_id
> cdo_version
> cdo_created
> cdo_revised
> cdo_resource
> cdo_container
> cdo_feature
> name
> street
> city
>
> If you want to use this information at the client-side you need to write
> your own Net4j mini protocol.
> http://www.eclipse.org/cdo/documentation/presentations/DemoCampBerlin_2009/Net4j-Presentation.pdf
> gives hints on how to do this.
>
> Cheers
> /Eike
>
> ----
> http://www.esc-net.de
> http://thegordian.blogspot.com
> http://twitter.com/eikestepper
>
>
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.06576 seconds