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 | public class Column implements Cloneable{ |
40 | |
41 | //private Expression value; |
42 | private Expression defaultValue = Expression.NULL; // Default value for INSERT |
43 | private String defaultDefinition; // String representation for Default Value |
44 | private String name; |
45 | //private String tableAlias; // für SELECT Syntax tableAlias.colName |
46 | //private String colName; // Name der Tabellenspalte |
47 | private boolean identity; |
48 | private boolean caseSensitive; |
49 | private boolean nullable = true; |
50 | private int scale; |
51 | private int precision; |
52 | private int dataType; |
53 | private Identity counter; // counter for identity values |
54 | |
55 | Column(){} |
56 | |
57 | |
58 | void setName( String name ){ |
59 | this.name = name; |
60 | } |
61 | |
62 | |
63 | void setDefaultValue(Expression defaultValue, String defaultDefinition){ |
64 | this.defaultValue = defaultValue; |
65 | this.defaultDefinition = defaultDefinition; |
66 | } |
67 | |
68 | /** |
69 | * Return the default expression for this column. If there is no default vale then it return Expression.NULL. |
70 | * @param con SSConnection for transactions |
71 | */ |
72 | Expression getDefaultValue(SSConnection con) throws SQLException{ |
73 | if(identity) |
74 | counter.createNextValue(con); |
75 | return defaultValue; |
76 | } |
77 | |
78 | String getDefaultDefinition(){ |
79 | return defaultDefinition; |
80 | } |
81 | |
82 | String getName(){ |
83 | return name; |
84 | } |
85 | |
86 | boolean isAutoIncrement(){ |
87 | return identity; |
88 | } |
89 | |
90 | void setAutoIncrement(boolean identity){ |
91 | this.identity = identity; |
92 | } |
93 | |
94 | int initAutoIncrement( Table table, RandomAccessFile raFile, long filePos) throws IOException{ |
95 | if(identity){ |
96 | counter = new Identity( table, raFile, filePos); |
97 | defaultValue = new ExpressionValue( counter, SQLTokenizer.BIGINT ); |
98 | } |
99 | return 8; |
100 | } |
101 | |
102 | void setNewAutoIncrementValue(Expression obj) throws Exception{ |
103 | if(identity){ |
104 | counter.setNextValue(obj); |
105 | } |
106 | } |
107 | |
108 | boolean isCaseSensitive(){ |
109 | return caseSensitive; |
110 | } |
111 | |
112 | void setNullable(boolean nullable){ |
113 | this.nullable = nullable; |
114 | } |
115 | |
116 | boolean isNullable(){ |
117 | return nullable; |
118 | } |
119 | |
120 | void setDataType(int dataType){ |
121 | this.dataType = dataType; |
122 | } |
123 | |
124 | int getDataType(){ |
125 | return dataType; |
126 | } |
127 | |
128 | |
129 | int getDisplaySize(){ |
130 | return SSResultSetMetaData.getDisplaySize( dataType, precision, scale); |
131 | } |
132 | |
133 | void setScale(int scale){ |
134 | this.scale = scale; |
135 | } |
136 | |
137 | int getScale(){ |
138 | switch(dataType){ |
139 | case SQLTokenizer.DECIMAL: |
140 | case SQLTokenizer.NUMERIC: |
141 | return scale; |
142 | default: |
143 | return Expression.getScale(dataType); |
144 | } |
145 | } |
146 | |
147 | void setPrecision(int precision) throws SQLException{ |
148 | if(precision<0) throw Utils.createSQLException("Invalid column size " + precision + " for column '"+name+"'"); |
149 | this.precision = precision; |
150 | } |
151 | |
152 | int getPrecision(){ |
153 | return SSResultSetMetaData.getDataTypePrecision( dataType, precision ); |
154 | } |
155 | |
156 | int getColumnSize(){ |
157 | if(SSResultSetMetaData.isNumberDataType(dataType)) |
158 | return getPrecision(); |
159 | else return getDisplaySize(); |
160 | } |
161 | |
162 | |
163 | int getFlag(){ |
164 | return (identity ? 1 : 0) | |
165 | (caseSensitive ? 2 : 0) | |
166 | (nullable ? 4 : 0); |
167 | } |
168 | |
169 | |
170 | void setFlag(int flag){ |
171 | identity = (flag & 1) > 0; |
172 | caseSensitive = (flag & 2) > 0; |
173 | nullable = (flag & 4) > 0; |
174 | } |
175 | |
176 | |
177 | Column copy(){ |
178 | try{ |
179 | return (Column)clone(); |
180 | }catch(Exception e){return null;} |
181 | |
182 | } |
183 | } |