KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.xquark.jdbc.typing.ColumnMetaDataImpl;
32 import org.xquark.jdbc.typing.TypeMap;
33
34 /**
35  * Class containing information for relational column creation.
36  *
37  */

38 public class ColumnSpec
39 {
40     private static final String JavaDoc RCSRevision = "$Revision: 1.3 $";
41     private static final String JavaDoc RCSName = "$Name: $";
42     
43     /* Column name */
44     protected String JavaDoc name;
45     protected int JDBCType;
46     protected long size = -1;
47     protected int prec = -1;
48     protected int scale = -1;
49     protected short paramSize = -1;
50     protected short paramPrec = -1;
51     protected short paramScale = -1;
52     
53     /* Is part of the primary key */
54     protected boolean key = false;
55     protected boolean nullable = true;
56
57     //////////////////////////////////////////////////////////////////////////
58
// CONSTRUCTORS
59
//////////////////////////////////////////////////////////////////////////
60
public ColumnSpec() {}
61     
62     /** Creates new ColumnSpec
63      * @param name The column name.
64      * @param key true if the column belongs to the primary key.
65      */

66     public ColumnSpec(String JavaDoc name, int JDBCType, long size)
67     {
68         set(name, JDBCType, size);
69     }
70     
71     /** Creates new ColumnSpec
72      * @param name The column name.
73      * @param key true if the column belongs to the primary key.
74      */

75     public ColumnSpec(String JavaDoc name, int JDBCType, int prec, int scale)
76     {
77         set(name, JDBCType, prec, scale);
78     }
79     
80     /** Creates new ColumnSpec
81      * @param name The column name.
82      * @param key true if the column belongs to the primary key.
83      */

84     public ColumnSpec(String JavaDoc name, int JDBCType, boolean key)
85     {
86         set(name, JDBCType, key);
87     }
88     
89     //////////////////////////////////////////////////////////////////////////
90
// ACCESSORS
91
//////////////////////////////////////////////////////////////////////////
92

93     public void setName(String JavaDoc name)
94     {
95         this.name = name;
96     }
97     
98     public void set(String JavaDoc name, int JDBCType, long size)
99     {
100         this.name = name;
101         this.JDBCType = JDBCType;
102         this.size = size;
103     }
104     
105     public void set(String JavaDoc name, int JDBCType, int prec, int scale)
106     {
107         set(name, JDBCType, size);
108         this.prec = prec;
109         this.scale = scale;
110     }
111     
112     /** Accessor.
113      * <p>Used for object recycling.</p>
114      * @param name The column name.
115      * @param key true if the column belongs to the primary key.
116      */

117     public void set(String JavaDoc name, int JDBCType, boolean key)
118     {
119         this.name = name;
120         this.JDBCType = JDBCType;
121         setKeyRole(key);
122     }
123     
124     public void setDataType(String JavaDoc type)
125     {
126         JDBCType = TypeMap.parseJDBCType(type);
127     }
128     
129     /** Accessor.
130      * @param key true if the column belongs to the primary key.
131      */

132     public void setKeyRole(boolean key)
133     {
134         this.key = key;
135     }
136     
137     public void setSize(long size, short param)
138     {
139         this.size = size;
140         paramSize = param;
141     }
142     
143     public void setPrecision(int prec, short param)
144     {
145         this.prec = prec;
146         paramPrec = param;
147     }
148     
149     public void setScale(int scale, short param)
150     {
151         this.scale = scale;
152         paramScale = param;
153     }
154     
155     public void setNullable(boolean nullable)
156     {
157         this.nullable = nullable;
158     }
159     
160     /** Return the column name */
161     public String JavaDoc getName()
162     {
163         return name;
164     }
165     
166     public int getDataType()
167     {
168         return JDBCType;
169     }
170     
171     /** Return true if the columns belongs to the primary key */
172     public boolean isPrimaryKey()
173     {
174         return key;
175     }
176     
177     public boolean isNullable()
178     {
179         return nullable;
180     }
181     
182     public long getSize(long[] sizeParameters)
183     {
184         if (paramSize >= 0 && sizeParameters[paramSize] >= 0)
185             return sizeParameters[paramSize];
186         else
187             return size;
188     }
189     
190     public int getPrecision(long[] sizeParameters)
191     {
192         if (paramPrec >= 0 && sizeParameters[paramPrec] >= 0)
193             return (int)sizeParameters[paramPrec];
194         else
195             return prec;
196     }
197     
198     public int getScale(long[] sizeParameters)
199     {
200         if (paramScale >= 0 && sizeParameters[paramScale] >= 0)
201             return (int)sizeParameters[paramScale];
202         else
203             return scale;
204     }
205     
206     /**
207      * Use default values (no parameter).
208      */

209     public String JavaDoc getCreationString(TypeMap typeMap)
210     {
211         return name + ' ' + ColumnMetaDataImpl.getTypeCreationString(JDBCType, typeMap, size, prec,scale);
212     }
213     
214     /**
215      * Use default values (no parameter).
216      */

217     public String JavaDoc getTypeCreationString(TypeMap typeMap)
218     {
219         return ColumnMetaDataImpl.getTypeCreationString(JDBCType, typeMap, size, prec,scale);
220     }
221     
222 }
223
Popular Tags