-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathField.java
More file actions
300 lines (249 loc) · 9.33 KB
/
Field.java
File metadata and controls
300 lines (249 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package javaxt.sql;
import javaxt.json.JSONArray;
import javaxt.json.JSONObject;
//******************************************************************************
//** Field Class
//******************************************************************************
/**
* Used to represent a field in a Recordset.
*
******************************************************************************/
public class Field {
private String name = null;
private String type = null;
private Value value = null;
private String tableName = null;
private String schema = null;
private String className = null;
private boolean requiresUpdate = false;
private Table table;
//**************************************************************************
//** Constructor
//**************************************************************************
protected Field(int i, java.sql.ResultSetMetaData rsmd){
try{ name = getValue(rsmd.getColumnName(i)); } catch(Exception e){}
try{ tableName = getValue(rsmd.getTableName(i)); } catch(Exception e){}
try{ schema = getValue(rsmd.getSchemaName(i)); } catch(Exception e){}
try{ type = getValue(rsmd.getColumnTypeName(i)); } catch(Exception e){}
try{ className = getValue(rsmd.getColumnClassName(i)); } catch(Exception e){}
//Special case. Discovered that the column name was returning a
//table prefix when performing a union queries with SQLite
if (name!=null && name.contains(".")){
String[] arr = name.split("\\.");
if (arr.length==3){
name = arr[2];
tableName = arr[1];
schema = arr[0];
}
else if (arr.length==2){
name = arr[1];
tableName = arr[0];
}
else if (arr.length==1){
name = arr[0];
}
}
}
//**************************************************************************
//** Constructor
//**************************************************************************
private Field(){}
//**************************************************************************
//** clone
//**************************************************************************
/** Used to create a shallow copy of the current field. Does not include the
* field value.
*/
public Field clone(){
Field field = new Field();
field.name = name;
field.type = type;
field.tableName = tableName;
field.schema = schema;
field.className = className;
field.table = table;
return field;
}
//**************************************************************************
//** getName
//**************************************************************************
/** Returns the name of the column associated with this field. Returns null
* if the column name is unknown.
*/
public String getName(){
return name;
}
//**************************************************************************
//** getType
//**************************************************************************
/** Returns the column type name (e.g. VARCHAR, INTEGER, BLOB, etc).
*/
public String getType(){
return type;
}
//**************************************************************************
//** getClassName
//**************************************************************************
/** Returns the Java class name that is associated with the column type.
* For example, most JDBC drivers map VARCHAR columns to a java.lang.String.
* In this case, the method would return "java.lang.String" for the field
* class name.
*/
public String getClassName(){
return className;
}
protected void setClassName(String className){
this.className = className;
}
//**************************************************************************
//** getValue
//**************************************************************************
/** Returns the value for this field.
*/
public Value getValue(){
if (value==null) value = new Value(null);
return value;
}
protected void setValue(Value value){
this.value = value;
}
//**************************************************************************
//** getTable
//**************************************************************************
/** @deprecated This method will be refactored in a future release. Use the
* getTableName() method to get the name of the table
*/
public String getTable(){
return tableName;
}
protected Table getT(){ //<- rename to getTable() in a future release...
return table;
}
protected void setTable(Table table){
this.table = table;
}
//**************************************************************************
//** getTableName
//**************************************************************************
/** Returns the name of the table in which this field is found. Returns null
* if the table name is unknown.
*/
public String getTableName(){
return tableName;
}
protected void setTableName(String tableName){
this.tableName = getValue(tableName);
}
//**************************************************************************
//** getSchema
//**************************************************************************
/** Returns the name of the schema to which this field belongs. Schemas are
* used to group objects in the database and are often used for access
* control. Returns null if the schema name is unknown.
*/
protected String getSchema(){
return schema;
}
protected void setSchemaName(String schema){
this.schema = getValue(schema);
}
//**************************************************************************
//** isDirty
//**************************************************************************
/** Returns true if the value for this field has changed.
*/
public boolean isDirty(){
return requiresUpdate;
}
protected void requiresUpdate(boolean b){
requiresUpdate = b;
}
//**************************************************************************
//** toString
//**************************************************************************
/** Returns the field name.
*/
public String toString(){
return name;
}
//**************************************************************************
//** toJson
//**************************************************************************
/** Returns a JSON representation of the field.
*/
public JSONObject toJson(){
JSONObject json = new JSONObject();
Value val = getValue();
if (!val.isNull()){
Object obj = val.toObject();
Class cls = obj.getClass();
String className = cls.getSimpleName();
Package pkg = cls.getPackage();
String packageName = pkg==null ? "" : pkg.getName();
//Special case for json objects
if ((packageName.equals("java.lang") && className.equals("String")) ||
!packageName.startsWith("java"))
{
String s = obj.toString().trim();
if (s.startsWith("{") && s.endsWith("}")){
try{
val = new Value(new JSONObject(s));
}
catch(Exception e){}
}
else if (s.startsWith("[") && s.endsWith("]")){
try{
val = new Value(new JSONArray(s));
}
catch(Exception e){}
}
}
//Special case for arrays
if (obj instanceof java.sql.Array){
try{
JSONArray arr = new JSONArray();
for (Object o : (Object[]) ((java.sql.Array) obj).getArray()){
arr.add(o);
}
val = new Value(arr);
}
catch(Exception e){}
}
//Special case for H2's TimestampWithTimeZone
if (packageName.equals("org.h2.api")){
if (className.equals("TimestampWithTimeZone")){
val = new Value(val.toDate());
}
}
}
json.set("name", name);
json.set("value", val);
json.set("type", type);
json.set("table", tableName);
json.set("schema", schema);
return json;
}
//**************************************************************************
//** clear
//**************************************************************************
/** Used to delete all the attributes of this field.
*/
protected void clear(){
name = null;
type = null;
value = new Value(null);
tableName = null;
schema = null;
className = null;
table = null;
}
//**************************************************************************
//** getValue
//**************************************************************************
private String getValue(String str){
if (str!=null){
if (str.trim().length()==0) return null;
}
return str;
}
}