KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > dbms > ColumnInfo


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * ColumnSpec.java
25  *
26  * Created on 11 janvier 2001, 16:57
27  */

28
29 package org.xquark.mapper.dbms;
30
31 import java.sql.Types JavaDoc;
32
33 import org.xquark.jdbc.typing.ColumnMetaDataImpl;
34
35 /**
36  * Class containing information on collections table columns constructed from
37  * repository configuration (no JDBC Access).
38  *
39  */

40 public class ColumnInfo
41 {
42     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
43     private static final String JavaDoc RCSName = "$Name: $";
44     
45     private ColumnSpec template = null;
46     protected long size = -1;
47     protected int prec = -1;
48     protected int scale = -1;
49     int realisticJDBCType; // data type modified to fit dbms limitations in terms of size
50
private StringBuffer JavaDoc columnClause = null;
51     private StringBuffer JavaDoc constraintColumnClause = null;
52     private StringBuffer JavaDoc compactColumnClause = null;
53     
54     //////////////////////////////////////////////////////////////////////////
55
// CONSTRUCTORS
56
//////////////////////////////////////////////////////////////////////////
57
/** Creates new ColumnInfo
58      */

59     public ColumnInfo(ColumnSpec template, long[] sizeParameters, AbstractConnection dbmsInfo)
60     {
61         this.template = template;
62         size = template.getSize(sizeParameters);
63         prec = template.getPrecision(sizeParameters);
64         scale = template.getScale(sizeParameters);
65         calculateIntegerPrecision();
66         realisticJDBCType = ColumnMetaDataImpl.findRealisticType(getDataType(), size, dbmsInfo.getTypeMap());
67         initializeSatements(dbmsInfo);
68     }
69     
70     //////////////////////////////////////////////////////////////////////////
71
// ACCESSORS
72
//////////////////////////////////////////////////////////////////////////
73

74     public long getSize()
75     {
76         return size;
77     }
78     
79     public int getPrecision()
80     {
81         return prec;
82     }
83     
84     public int getScale()
85     {
86         return scale;
87     }
88     
89     public String JavaDoc getColumnClause()
90     {
91         return columnClause.toString();
92     }
93     public String JavaDoc getConstraintColumnClause()
94     {
95         return constraintColumnClause.toString();
96     }
97     public String JavaDoc getCompactColumnClause()
98     {
99         return compactColumnClause.toString();
100     }
101     
102     public String JavaDoc getName()
103     {
104         return template.getName();
105     }
106     
107     public int getDataType()
108     {
109         return template.getDataType();
110     }
111     
112     /** Return true if the columns belongs to the primary key */
113     public boolean isPrimaryKey()
114     {
115         return template.isPrimaryKey();
116     }
117     
118     public boolean isNullable()
119     {
120         return template.isNullable();
121     }
122     
123     //////////////////////////////////////////////////////////////////////////
124
// PUBLIC METHODS
125
//////////////////////////////////////////////////////////////////////////
126
private void initializeSatements(AbstractConnection dbmsInfo)
127     {
128         columnClause = new StringBuffer JavaDoc();
129         constraintColumnClause = new StringBuffer JavaDoc();
130         columnClause = generateCreationString(columnClause, dbmsInfo);
131         compactColumnClause = new StringBuffer JavaDoc(columnClause.toString());
132         
133         if (isPrimaryKey())
134         {
135             constraintColumnClause.append(getName());
136             constraintColumnClause.append(",");
137             columnClause.append(" NOT NULL");
138             compactColumnClause.append(" NOT NULL PRIMARY KEY");
139         }
140         else if (isNullable())
141         {
142             columnClause.append(" NULL");
143             compactColumnClause.append(" NULL");
144         }
145         else
146         {
147             columnClause.append(" NOT NULL");
148             compactColumnClause.append(" NOT NULL");
149         }
150     }
151     public boolean isIndexable(int type, long size, AbstractConnection dbmsInfo)
152     {
153         return realisticJDBCType != Types.LONGVARBINARY
154                 && realisticJDBCType != Types.LONGVARCHAR
155                 && realisticJDBCType != Types.BLOB
156                 && realisticJDBCType != Types.CLOB;
157     }
158     
159     //////////////////////////////////////////////////////////////////////////
160
// PRIVATE
161
//////////////////////////////////////////////////////////////////////////
162
/* Compact method (PRIMARY KEY INLINE) */
163     private StringBuffer JavaDoc generateCreationString(StringBuffer JavaDoc stmt, AbstractConnection dbmsInfo)
164     {
165         /* Column name */
166         stmt.append(getName());
167         
168         /* Column type */
169         
170         // First set the correct JDBC type according to size issue specificities
171
stmt.append(" ");
172         stmt.append(ColumnMetaDataImpl.getTypeCreationString(
173                                         realisticJDBCType,
174                                         dbmsInfo.getTypeMap(),
175                                         size,
176                                         prec,
177                                         scale
178                                         ));
179         
180         return stmt;
181     }
182     
183     
184         private void calculateIntegerPrecision()
185         {
186             switch (getDataType())
187             {
188                 case Types.DECIMAL: case Types.NUMERIC:
189                     if (scale <= 0) // assume that precision was in bits !
190
prec = (int)Math.ceil(Math.log(Math.pow(2,prec))/Math.log(10));
191                     break;
192                 case Types.BIGINT:
193                     prec = 19;
194                     break;
195                 case Types.INTEGER:
196                     prec = 10;
197                     break;
198                 case Types.SMALLINT:
199                     prec = 5;
200                     break;
201                 case Types.TINYINT:
202                     prec = 3;
203                     break;
204                 case Types.BIT:
205                     prec = 1;
206                     break;
207                 default:
208             }
209         }
210     
211 }
212
Popular Tags