KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > mapper > rdb > generator > RdbGenColumn


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.mapper.rdb.generator;
25
26 import org.objectweb.jorm.type.api.PType;
27 import org.objectweb.jorm.api.PException;
28 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
29 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter;
30
31 import java.util.List JavaDoc;
32
33 /**
34  * This class contains all required information related to a column to which
35  * some field of a JORM class is mapped.
36  * @author P. Dechamboux
37  */

38 public class RdbGenColumn {
39     /**
40      * The name of the field that is mapped to this column.
41      */

42     public String JavaDoc fieldName = null;
43     /**
44      * Tells is this field is an hidden one.
45      */

46     public boolean hiddenField = true;
47
48     /**
49      * Contains the constant as a String in the case is this field is a constant
50      * field, otherwise (not a constant field) null.
51      */

52     public String JavaDoc constant;
53
54     /**
55      * The name of the RDB column.
56      */

57     public String JavaDoc columnName = null;
58     /**
59      * The size of the RDB column in case of string, serialized,
60      * [byte|char]array...
61      */

62     public int columnSize = RdbAdapter.NOSIZE;
63     public int columnScale = RdbAdapter.NOSIZE;
64     /**
65      * The JORM type associated with this mapped field.
66      */

67     public PType columnType = null;
68     /**
69      * Tells if this column may store null values.
70      */

71     public boolean columnNotNull = false;
72     /**
73      * Tells if this column belongs to the table primary key.
74      */

75     public boolean columnInPK = false;
76     /**
77      * The table descrption which this column belongs to.
78      */

79     public RdbGenTable table = null;
80
81     /**
82      * the sql type of the column. A null value means to use the value of the
83      * choosen by the adapter.
84      */

85     public String JavaDoc columnSqlType = null;
86
87     /**
88      * references a column in the main table in case of this column participates
89      * in the join with the main table. In other case this field is null.
90      */

91     public RdbGenColumn joinCol = null;
92
93     /**
94      * contains the association between a RdbGenJoin and a primitive element.
95      * This field is null is the current column is a column of the main table,
96      * because no join is needed of course.
97      */

98     public List JavaDoc joins = null;
99     public List JavaDoc pes = null;
100
101     public RdbGenColumn getJoinCol() {
102         return joinCol;
103     }
104
105     public String JavaDoc getColumnSqlType() {
106         return columnSqlType;
107     }
108
109     public String JavaDoc getFieldName() {
110         return fieldName;
111     }
112
113     public String JavaDoc getFieldName(RdbGenJoin rgj) throws PException {
114         if (rgj != null && joins != null) {
115             int idx = joins.indexOf(rgj);
116             if (idx == -1) {
117                 throw new PException("No field found for the column "
118                                      + columnName + " with the join " + rgj);
119             }
120             return ((PrimitiveElement) pes.get(idx)).getName();
121         }
122         return fieldName;
123     }
124
125     public String JavaDoc getConstant() {
126         return constant;
127     }
128
129     public List JavaDoc getJoins() {
130         return joins;
131     }
132
133     public boolean isHiddenField() {
134         return hiddenField;
135     }
136
137     public String JavaDoc getColumnName() {
138         return columnName;
139     }
140
141     public PType getColumnType() {
142         return columnType;
143     }
144
145     public boolean isColumnNotNull() {
146         return columnNotNull;
147     }
148
149     public boolean isColumnInPK() {
150         return columnInPK;
151     }
152
153     public RdbGenTable getTable() {
154         return table;
155     }
156
157     public String JavaDoc getColumnNotNull() {
158         return "" + columnNotNull;
159     }
160
161     public String JavaDoc getColumnInPK() {
162         return "" + columnInPK();
163     }
164
165     public boolean columnInPK() {
166         if (table.isMainTable) {
167             return table.genInfos.colInGenId(this);
168         } else if (table.colocatedTable) {
169             return false;
170         } else {
171             return this.joinCol != null;
172         }
173     }
174
175     /**
176      * Gets the SQL type associated with this column into the database for
177      * which we define the mapping.
178      * @return The string containing the name of the relevant SQL type.
179      * @throws PException
180      */

181     public String JavaDoc getSqlType(String JavaDoc adapter) throws PException {
182         if (columnSqlType != null) {
183             return "\"" + columnSqlType + "\"";
184         } else {
185             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(adapter);
186             sb.append(".getSqlType(");
187             sb.append(columnType.getProgName());
188             sb.append(".getTypeCode(), ");
189             sb.append(columnInPK());
190             sb.append(", ");
191             sb.append(columnSize);
192             sb.append(", ");
193             sb.append(columnScale);
194             sb.append(")");
195             return sb.toString();
196         }
197     }
198
199     /**
200      * Constructs the string that defines the way to access to the relevant
201      * result part into a particular ResultSet.
202      * @param adapter The name of the RdbAdaptervariable used to set the
203      * given query parameter.
204      * @param resultset The name of the ResultSet variable used to get the query
205      * result.
206      * @param pos The position of the result to get.
207      * @return The string defining the access to the relevant result part into
208      * a particular ResultSet.
209      * @throws PException
210      *
211      * example: "adapter.getBoolean(pstmt, 4);
212      * example: "adapter.getDate(pstmt, 4, "TIME");
213      */

214     public String JavaDoc getSqlGet(String JavaDoc adapter,
215                             String JavaDoc resultset,
216                             String JavaDoc pos,
217                             String JavaDoc nullValue) throws PException {
218         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(adapter);
219         sb.append(".");
220         sb.append(getAdapterGet(columnType));
221         sb.append("(");
222         sb.append(resultset);
223         sb.append(", ");
224         sb.append(pos);
225         switch (columnType.getTypeCode()) {
226         case PType.TYPECODE_DATE:
227             sb.append(", ");
228             if (columnSqlType != null && columnSqlType.length() > 0) {
229                 sb.append("\"");
230             }
231             sb.append(columnSqlType);
232             if (columnSqlType != null && columnSqlType.length() > 0) {
233                 sb.append("\"");
234             }
235             break;
236         }
237         sb.append(", ");
238         if (nullValue != null && nullValue.length()>0) {
239             sb.append(nullValue);
240         } else {
241             sb.append(getNullValue());
242         }
243         sb.append(")");
244         return sb.toString();
245     }
246
247     public String JavaDoc getNullValue() {
248         switch (columnType.getTypeCode()) {
249         case PType.TYPECODE_BOOLEAN:
250             return "false";
251         case PType.TYPECODE_CHAR:
252             return "((" + columnType.getJavaName() + ") 0)";
253         case PType.TYPECODE_BYTE:
254         case PType.TYPECODE_SHORT:
255         case PType.TYPECODE_INT:
256         case PType.TYPECODE_LONG:
257         case PType.TYPECODE_FLOAT:
258         case PType.TYPECODE_DOUBLE:
259             return "((" + columnType.getJavaName() + ") -1)";
260         default:
261             return "null";
262         }
263     }
264
265
266     /**
267      * Constructs the string that defines the way to assign to the relevant
268      * query parameter into a particular prepared statement through the adapter.
269      * @param adapter The name of the RdbAdaptervariable used to set the
270      * given query parameter.
271      * @param pstmt The name of the PreparedStatement variable used to set the
272      * given query parameter.
273      * @param pos The position of the query parameter to be assigned.
274      * @param value is the String value which must be assigned into the
275      * prepared statment.
276      * @return The string defining the assignement of the relevant query
277      * parameter into a particular PreparedStatement throught the
278      * rdb adapter.
279      * @throws PException
280      *
281      * example: "adapter.setBoolean(pstmt, 4, true);
282      * example: "adapter.setDate(pstmt, 4, ((java.util.Date) fieldVal), "TIME");
283      */

284     public String JavaDoc getSqlSet(String JavaDoc adapter,
285                             String JavaDoc pstmt,
286                             String JavaDoc value,
287                             int pos) throws PException {
288         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(adapter);
289         sb.append(".");
290         sb.append(getAdapterSet(columnType));
291         sb.append("(");
292         sb.append(pstmt);
293         sb.append(", ");
294         sb.append(pos);
295         sb.append(", ");
296         sb.append(value);
297         switch (columnType.getTypeCode()) {
298         case PType.TYPECODE_DATE:
299             sb.append(", ");
300             if (columnSqlType != null && columnSqlType.length() > 0) {
301                 sb.append("\"");
302             }
303             sb.append(columnSqlType);
304             if (columnSqlType != null && columnSqlType.length() > 0) {
305                 sb.append("\"");
306             }
307             break;
308         }
309         sb.append(")");
310         return sb.toString();
311     }
312
313     public String JavaDoc getAdapterGet(PType type) {
314         return "get" + getAdapterSuffix(type);
315     }
316     public String JavaDoc getAdapterSet(PType type) {
317         return "set" + getAdapterSuffix(type);
318     }
319
320     private String JavaDoc getAdapterSuffix(PType type) {
321         switch (type.getTypeCode()) {
322         case PType.TYPECODE_BYTEARRAY:
323             return "ByteArray";
324         case PType.TYPECODE_BOOLEAN:
325             return "Boolean";
326         case PType.TYPECODE_OBJBOOLEAN:
327             return "Oboolean";
328         case PType.TYPECODE_FLOAT:
329             return "Float";
330         case PType.TYPECODE_OBJFLOAT:
331             return "Ofloat";
332         case PType.TYPECODE_DOUBLE:
333             return "Double";
334         case PType.TYPECODE_OBJDOUBLE:
335             return "Odouble";
336         case PType.TYPECODE_SERIALIZED:
337             return "Serialized";
338         default:
339             return type.getCodingName();
340         }
341     }
342
343     public String JavaDoc getSqlTypeCode(PType pt) throws PException {
344         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("adapter.getSqlTypeCode(");
345         sb.append(pt.getProgName());
346         sb.append(".getTypeCode(), ");
347         if (columnSqlType != null && columnSqlType.length() > 0) {
348             sb.append("\"");
349         }
350         sb.append(columnSqlType);
351         if (columnSqlType != null && columnSqlType.length() > 0) {
352             sb.append("\"");
353         }
354         sb.append(")");
355         return sb.toString();
356     }
357
358     public String JavaDoc getAccessorValue(RdbGenInfos genInfos,
359                                    boolean isSpecific) throws Exception JavaDoc {
360         return getAccessorValue(genInfos, isSpecific, null);
361     }
362
363     public String JavaDoc getAccessorValue(RdbGenInfos genInfos,
364                                    boolean isSpecific, RdbGenJoin rgj) throws Exception JavaDoc {
365
366         if (hiddenField) {
367             if (genInfos.colInGenId(this)) {
368                 throw new PException("unmanaged case");
369             } else {
370                 RdbGenRef gen_ref = genInfos.getGenRefOfColumn(this, rgj);
371                 if (isSpecific) {
372                     return "paccessor.paGet"
373                             + RdbGenInfos.commonHelper.upperFL(gen_ref.fieldName)
374                             + "(conn)";
375                 } else {
376                     return "paccessorGen.paGetRefField("
377                             + gen_ref.fieldName + "_NAME, conn)";
378                 }
379             }
380         } else {
381             if (isSpecific) {
382                 return "paccessor.paGet"
383                         + RdbGenInfos.commonHelper.upperFL(getFieldName(rgj))
384                         + "()";
385             } else {
386                 return "paccessorGen." +
387                         RdbGenInfos.commonHelper.getPaGetMethod(columnType)
388                         + "(" + getFieldName(rgj) + "_NAME)";
389             }
390         }
391     }
392
393     public String JavaDoc getRefValue(RdbGenInfos genInfos,
394                               boolean isSpecific) throws Exception JavaDoc {
395         return getRefValue(genInfos, isSpecific, null);
396     }
397
398     public String JavaDoc getRefValue(RdbGenInfos genInfos,
399                               boolean isSpecific, RdbGenJoin rgj) throws Exception JavaDoc {
400         try {
401             if (!hiddenField) {
402                 //A visible field is fetch through the PAccessor
403
throw new PException("unmanaged case");
404             }
405             RdbGenRef gen_ref = null;
406             if (genInfos.colInGenId(this)) {
407                 //The column is included into the identifier
408
gen_ref = genInfos.genId;
409                 if (gen_ref.refColumn != null) {
410                     //the identifier is composed by a single column
411
return "pname.encode" + columnType.getCodingName() + "()";
412                 } else {
413                     // composite identifier, the PNameGetter of the identifier is computed before
414
//TODO: pass as parameter, the variable name of the PNameGetter
415
return "_pngId.pnGet" +
416                             RdbGenInfos.commonHelper.upperFL(gen_ref.getFieldName(this))
417                             + "(null)";
418                 }
419             } else {
420                 //The column is not part of the identifier (however it is a hidden field)
421
// get the corresponding reference field of the column (there is obligatory at least one)
422
gen_ref = genInfos.getGenRefOfColumn(this, rgj);
423                 if (gen_ref == null) {
424                     System.out.println("Pb in getRefValue of column " + getColumnName());
425                     return "JORM_GENERATION_ERROR";
426                 }
427                 // check if the name def of the ref is a composite ref
428
if (gen_ref.refColumn != null) {
429                     // not composite, compute the name of the PNC variable name corresponding
430
// to the reference field, then call the encode method corresp?nding to
431
// the type of the column, pass in parameter the Pname provided by the PAccessor
432
return gen_ref.fieldName + "Pnc.encode" + columnType.getCodingName()
433                             + "(" + getAccessorValue(genInfos, isSpecific, rgj) + ")";
434                 } else {
435                     // composite case : compute the name of the variable correponding to the pname
436
// getter of the reference, call the pnGetXXX of the right type, pass null in parameter
437
return "png" + gen_ref.cnId + ".pnGet"
438                             + RdbGenInfos.commonHelper.upperFL(
439                                     gen_ref.getFieldName(this))
440                             + "(null)";
441                 }
442
443             }
444         } catch (Exception JavaDoc e) {
445             e.printStackTrace();
446             System.exit(-1);
447             throw e;
448         }
449     }
450
451     public String JavaDoc getColumnName(String JavaDoc tableSuffix) {
452         return table.tableName
453                 + (tableSuffix == null ? "" : tableSuffix)
454                 + "." + columnName;
455     }
456
457     public boolean isInJoin(RdbGenJoin rgj) {
458         return (joins != null && joins.contains(rgj))
459                 || rgj.getJoinColumnsInExt().contains(this);
460
461     }
462     
463     public boolean equals(RdbGenColumn rgc) {
464         if (rgc == null)
465             return false;
466         return ( (columnName == null ? rgc.columnName == null : columnName.equals(rgc.columnName))
467                 && (table == null ? rgc.table == null : table.tableName.equals(rgc.table.tableName))
468                 && (columnSqlType == null ? rgc.columnSqlType == null : columnSqlType.equals(rgc.columnSqlType))
469                 && (columnType == rgc.columnType)
470                 && (fieldName == null ? rgc.fieldName == null : fieldName.equals(rgc.fieldName))
471                 && (hiddenField == rgc.hiddenField)
472                 && (joinCol == null ? rgc.joinCol == null : joinCol.equals(rgc.joinCol))
473                 && (columnInPK == rgc.columnInPK)
474                 && (columnNotNull == rgc.columnNotNull)
475                 && (columnScale == rgc.columnScale)
476                 && (constant == null ? rgc.constant == null : constant.equals(rgc.constant)));
477     }
478 }
479
Popular Tags