File: SimpleResultSetRecord.java

package info (click to toggle)
eclipselink 2.6.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 44,528 kB
  • sloc: java: 475,126; xml: 72; makefile: 21; sh: 10
file content (324 lines) | stat: -rw-r--r-- 14,679 bytes parent folder | download | duplicates (2)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*******************************************************************************
 * Copyright (c) 2013, 2015 Oracle, IBM Corporation. All rights reserved.
 * This program and the accompanying materials are made available under the 
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 
 * which accompanies this distribution. 
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation from Oracle TopLink
 *     02/19/2015 - Rick Curtis  
 *       - 458877 : Add national character support
 ******************************************************************************/  
package org.eclipse.persistence.internal.sessions;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import org.eclipse.persistence.exceptions.DatabaseException;
import org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor;
import org.eclipse.persistence.internal.databaseaccess.DatabasePlatform;
import org.eclipse.persistence.internal.helper.ClassConstants;
import org.eclipse.persistence.internal.helper.DatabaseField;

/**
 * PERF: Record used by ObjectLevelReadQuery ResultSet optimization.
 * This is a subclass of ResultSetRecord that's used with simple descriptors
 * (descriptor.getObjectBuilder().isSimple()) - those having only DirectToField mappings.
 * In this case the record is not cached by any mapping therefore could be reused
 * in ReadAllQuery - (which calls resultSet.next()).
 * In case the cached object used instead of creating a new one from the record,
 * the not needed fields' values are never obtained from resultSet 
 * (that's especially important for expensive LOBs).
 * If alternatively the record is used to populate an object then all
 * the values obtained from resultSet are nullified after use.
 */
public class SimpleResultSetRecord extends ResultSetRecord {

    /** Indicates whether the values requested while populating object (isPopulatingObject == true) should be saved in valuesArray */
    transient protected boolean shouldKeepValues;
    /** Indicates whether to use optimization while getting values from result set instead of calling accessor.getObject method. */
    transient protected boolean shouldUseOptimization;
    /** 
     * Indicates that the  whole object is being populated.
     * At first the primary key is extracted from the row - to see
     * if the object with the same pk is already in the cache.
     * Then in case of refresh the locking field value is extracted, too.
     * Indication of the object being populated from the record (looping through all the mappings)
     * is request for a value, which has been already returned before (usually primary key, but may be version). 
     * At this point the flag is set to true. 
     */
    transient protected boolean isPopulatingObject;
    
    protected SimpleResultSetRecord() {
        super();
    }
    
    public SimpleResultSetRecord(Vector fields, DatabaseField[] fieldsArray, ResultSet resultSet, ResultSetMetaData metaData, DatabaseAccessor accessor, AbstractSession session, DatabasePlatform platform, boolean optimizeData) {
        super(fields, fieldsArray, resultSet, metaData, accessor, session, platform, optimizeData);
    }    
    
    /**
     * Obtains all the value from resultSet and removes it.
     * resultSet must be non null.
     */
    public void loadAllValuesFromResultSet() {
        int size = this.valuesArray.length;
        for (int index = 0; index < size; index++) {
            if (this.valuesArray[index] == null) {
                DatabaseField field = this.fieldsArray[index];
                // Field can be null for fetch groups.
                if (field != null) {
                    this.valuesArray[index] = getValueFromResultSet(index, field);
                }
            }
        }
        this.resultSet = null;
        this.metaData = null;
        this.accessor = null;
        this.platform = null;
        this.session = null;
    }
    
    /**
     * INTERNAL:
     * Retrieve the value for the field. If missing null is returned.
     */
    @Override
    public Object get(DatabaseField key) {
        if (this.fieldsArray != null) {
            // Optimize check.
            int index = key.index;
            if ((index < 0) || (index >= this.size)) {
                index = 0;
            }            
            DatabaseField field = this.fieldsArray[index];
            if ((field != key) && !field.equals(key)) {
                index = -1;
                for (int fieldIndex = 0; fieldIndex < this.size; fieldIndex++) {
                    field = this.fieldsArray[fieldIndex];
                    if ((field == key) || field.equals(key)) {
                        // PERF: If the fields index was not set, then set it.
                        if (key.index == -1) {
                            key.setIndex(fieldIndex);
                        }
                        index = fieldIndex;
                        break;
                    }
                }                
                if (index < 0) {
                    return null;
                }
            }
            if (this.resultSet != null) {
                Object value = this.valuesArray[index];
                if (value != null) {
                    if (!this.isPopulatingObject) {
                        this.isPopulatingObject = true;
                    }
                    if (!this.shouldKeepValues) {
                        this.valuesArray[index] = null;
                    }
                } else {
                    if (this.shouldUseOptimization) {
                        try {
                            Class fieldType = field.getType(); 
                            if (fieldType == ClassConstants.STRING) {
                                if(platform.shouldUseGetSetNString()){
                                    value = resultSet.getNString(index + 1);
                                }else {
                                    value = resultSet.getString(index + 1);
                                }
                                
                            } else if (fieldType == ClassConstants.LONG) {
                                value = resultSet.getLong(index + 1);
                            } else if (fieldType == ClassConstants.INTEGER) {
                                value = resultSet.getInt(index + 1);
                            } else {
                                value = this.accessor.getObject(this.resultSet, field, this.metaData, index + 1, this.platform, this.optimizeData, this.session);
                            }
                        } catch (SQLException exception) {
                            DatabaseException commException = this.accessor.processExceptionForCommError(session, exception, null);
                            if (commException != null) {
                                throw commException;
                            }
                            throw DatabaseException.sqlException(exception, accessor, session, false);
                        }            } else {
                        value = this.accessor.getObject(this.resultSet, field, this.metaData, index + 1, this.platform, this.optimizeData, this.session);
                    }
                    if (!this.isPopulatingObject || this.shouldKeepValues) {
                        this.valuesArray[index] = value;
                    }
                }
                return value;
            } else {
                return this.valuesArray[index];
            }
        } else {
            return super.get(key);
        }
    }
    
    /**
     * INTERNAL:
     * Retrieve the value for the field. If missing DatabaseRow.noEntry is returned.
     * PERF: This method is a clone of get() for performance.
     */
    @Override
    public Object getIndicatingNoEntry(DatabaseField key) {
        if (this.fieldsArray != null) {
            // Optimize check.
            int index = key.index;
            if ((index < 0) || (index >= this.size)) {
                index = 0;
            }            
            DatabaseField field = this.fieldsArray[index];
            if ((field != key) && !field.equals(key)) {
                index = -1;
                for (int fieldIndex = 0; fieldIndex < this.size; fieldIndex++) {
                    field = this.fieldsArray[fieldIndex];
                    if ((field == key) || field.equals(key)) {
                        // PERF: If the fields index was not set, then set it.
                        if (key.index == -1) {
                            key.setIndex(fieldIndex);
                        }
                        index = fieldIndex;
                        break;
                    }
                }                
                if (index < 0) {
                    return null;
                }
            }
            if (this.resultSet != null) {
                Object value = this.valuesArray[index];
                if (value != null) {
                    if (!this.isPopulatingObject) {
                        this.isPopulatingObject = true;
                    }
                    if (!this.shouldKeepValues) {
                        this.valuesArray[index] = null;
                    }
                } else {
                    if (this.shouldUseOptimization) {
                        try {
                            Class fieldType = field.getType(); 
                            if (fieldType == ClassConstants.STRING) {
                                if(platform.shouldUseGetSetNString()){
                                    value = resultSet.getNString(index + 1);
                                }else {
                                    value = resultSet.getString(index + 1);
                                }
                            } else if (fieldType == ClassConstants.LONG) {
                                value = resultSet.getLong(index + 1);
                            } else if (fieldType == ClassConstants.INTEGER) {
                                value = resultSet.getInt(index + 1);
                            } else {
                                value = this.accessor.getObject(this.resultSet, field, this.metaData, index + 1, this.platform, this.optimizeData, this.session);
                            }
                        } catch (SQLException exception) {
                            DatabaseException commException = this.accessor.processExceptionForCommError(session, exception, null);
                            if (commException != null) {
                                throw commException;
                            }
                            throw DatabaseException.sqlException(exception, accessor, session, false);
                        }            } else {
                        value = this.accessor.getObject(this.resultSet, field, this.metaData, index + 1, this.platform, this.optimizeData, this.session);
                    }
                    if (!this.isPopulatingObject || this.shouldKeepValues) {
                        this.valuesArray[index] = value;
                    }
                }
                return value;
            } else {
                return this.valuesArray[index];
            }
        } else {
            return super.get(key);
        }
    }
    
    protected Object getValueFromResultSet(int index, DatabaseField field) {
        if (this.shouldUseOptimization) {
            try {
                Class fieldType = field.getType(); 
                if (fieldType == ClassConstants.STRING) {
                    if(platform.shouldUseGetSetNString()){
                        return resultSet.getNString(index + 1);
                    }else {
                        return resultSet.getString(index + 1);
                    }
                } else if (fieldType == ClassConstants.LONG) {
                    return resultSet.getLong(index + 1);
                } else if (fieldType == ClassConstants.INTEGER) {
                    return resultSet.getInt(index + 1);
                }
            } catch (SQLException exception) {
                DatabaseException commException = this.accessor.processExceptionForCommError(session, exception, null);
                if (commException != null) {
                    throw commException;
                }
                throw DatabaseException.sqlException(exception, accessor, session, false);
            }
        }
        return this.accessor.getObject(this.resultSet, field, this.metaData, index + 1, this.platform, this.optimizeData, this.session);
    }
    
    public void reset() {
        if (this.isPopulatingObject) {
            this.isPopulatingObject = false;
            if (this.shouldKeepValues) {
                int size = this.valuesArray.length;
                for (int index = 0; index < size; index++) {
                    this.valuesArray[index] = null;
                }
            }
        } else {
            // TODO: this row hasn't been used to populate object, so values for all fields are null except for primary key fields and, possibly, version field.
            int size = this.valuesArray.length;
            for (int index = 0; index < size; index++) {
                this.valuesArray[index] = null;
            }
        }
    }
    
    public boolean shouldKeepValues() {
        return this.shouldKeepValues;
    }
    
    public void setShouldKeepValues(boolean shouldKeepValues) {
        this.shouldKeepValues = shouldKeepValues;
    }
    
    public boolean shouldUseOptimization() {
        return this.shouldUseOptimization;
    }
    
    public void setShouldUseOptimization(boolean shouldUseOptimization) {
        this.shouldUseOptimization = shouldUseOptimization;
    }
    
    public boolean hasValues() {
        return this.valuesArray[0] != null;
    }

    public boolean isPopulatingObject() {
        return this.isPopulatingObject;
    }    

    @Override
    protected String toStringAditional() {
        return (this.shouldKeepValues ? " shouldKeepValues" : "") + (shouldUseOptimization ? " shouldUseOptimization" : "") + (isPopulatingObject ? " isPopulatingObject" : "");
    }
    
    @Override 
    public void setSopObject(Object sopObject) {
        this.sopObject = sopObject;
        // sopObject is set - the row is used to populate object
        this.isPopulatingObject = true;
    }
}