KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > database > Column


1 /* =============================================================
2  * SmallSQL : a free Java DBMS library for the Java(tm) platform
3  * =============================================================
4  *
5  * (C) Copyright 2004-2006, by Volker Berlin.
6  *
7  * Project Info: http://www.smallsql.de/
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------
28  * Column.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  */

33 package smallsql.database;
34
35 import java.io.*;
36 import java.sql.*;
37
38
39 class Column implements Cloneable JavaDoc{
40
41     //private Expression value;
42
private Expression defaultValue = Expression.NULL; // Default value for INSERT
43
private String JavaDoc defaultDefinition; // String representation for Default Value
44
private String JavaDoc name;
45     private boolean identity;
46     private boolean caseSensitive;
47     private boolean nullable = true;
48     private int scale;
49     private int precision;
50     private int dataType;
51     private Identity counter; // counter for identity values
52

53     
54     void setName( String JavaDoc name ){
55         this.name = name;
56     }
57
58
59     void setDefaultValue(Expression defaultValue, String JavaDoc defaultDefinition){
60         this.defaultValue = defaultValue;
61         this.defaultDefinition = defaultDefinition;
62     }
63
64     /**
65      * Return the default expression for this column. If there is no default vale then it return Expression.NULL.
66      * @param con SSConnection for transactions
67      */

68     Expression getDefaultValue(SSConnection con) throws SQLException{
69         if(identity)
70             counter.createNextValue(con);
71         return defaultValue;
72     }
73
74     String JavaDoc getDefaultDefinition(){
75         return defaultDefinition;
76     }
77
78     String JavaDoc getName(){
79         return name;
80     }
81
82     boolean isAutoIncrement(){
83         return identity;
84     }
85
86     void setAutoIncrement(boolean identity){
87         this.identity = identity;
88     }
89     
90     int initAutoIncrement( Table table, RandomAccessFile raFile, long filePos) throws IOException{
91         if(identity){
92             counter = new Identity( table, raFile, filePos);
93             defaultValue = new ExpressionValue( counter, SQLTokenizer.BIGINT );
94         }
95         return 8;
96     }
97     
98     void setNewAutoIncrementValue(Expression obj) throws Exception JavaDoc{
99         if(identity){
100             counter.setNextValue(obj);
101         }
102     }
103
104     boolean isCaseSensitive(){
105         return caseSensitive;
106     }
107
108     void setNullable(boolean nullable){
109         this.nullable = nullable;
110     }
111
112     boolean isNullable(){
113         return nullable;
114     }
115
116     void setDataType(int dataType){
117         this.dataType = dataType;
118     }
119
120     int getDataType(){
121         return dataType;
122     }
123
124
125     int getDisplaySize(){
126         return SSResultSetMetaData.getDisplaySize( dataType, precision, scale);
127     }
128
129     void setScale(int scale){
130         this.scale = scale;
131     }
132
133     int getScale(){
134         switch(dataType){
135             case SQLTokenizer.DECIMAL:
136             case SQLTokenizer.NUMERIC:
137                 return scale;
138             default:
139                 return Expression.getScale(dataType);
140         }
141     }
142
143     void setPrecision(int precision) throws SQLException{
144         if(precision<0) throw Utils.createSQLException("Invalid column size " + precision + " for column '"+name+"'");
145         this.precision = precision;
146     }
147
148     int getPrecision(){
149         return SSResultSetMetaData.getDataTypePrecision( dataType, precision );
150     }
151
152     int getColumnSize(){
153         if(SSResultSetMetaData.isNumberDataType(dataType))
154              return getPrecision();
155         else return getDisplaySize();
156     }
157     
158
159     int getFlag(){
160         return (identity ? 1 : 0) |
161                (caseSensitive ? 2 : 0) |
162                (nullable ? 4 : 0);
163     }
164     
165
166     void setFlag(int flag){
167         identity = (flag & 1) > 0;
168         caseSensitive = (flag & 2) > 0;
169         nullable = (flag & 4) > 0;
170     }
171     
172
173     Column copy(){
174         try{
175             return (Column)clone();
176         }catch(Exception JavaDoc e){return null;}
177         
178     }
179 }
Popular Tags