/
AppIDObjectIdFieldManager.java
248 lines (223 loc) · 8.46 KB
/
AppIDObjectIdFieldManager.java
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
/**********************************************************************
Copyright (c) 2003 Erik Bengtson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
2003 Andy Jefferson - coding standards
2006 Michael Brown - updated to cater for alternative types in the PK fields
...
**********************************************************************/
package org.datanucleus.store.rdbms.mapping;
import java.sql.PreparedStatement;
import org.datanucleus.ExecutionContext;
import org.datanucleus.api.ApiAdapter;
import org.datanucleus.enhancer.Persistable;
import org.datanucleus.state.AppIdObjectIdFieldConsumer;
import org.datanucleus.store.fieldmanager.AbstractFieldManager;
import org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping;
import org.datanucleus.store.rdbms.mapping.java.PersistableMapping;
import org.datanucleus.util.Localiser;
/**
* Parameter setter class to deal with application identity.
* Parameters must be in the same order as the fields.
*/
public class AppIDObjectIdFieldManager extends AbstractFieldManager
{
/** Localisation of messages. */
protected static final Localiser LOCALISER = Localiser.getInstance(
"org.datanucleus.Localisation", org.datanucleus.ClassConstants.NUCLEUS_CONTEXT_LOADER);
/** Parameter positions in the PreparedStatement. */
private int[] params;
/** Next parameter index. Increased each call. */
private int nextParam;
/** ExecutionContext. */
private ExecutionContext ec;
/** The PreparedStatement to populate. */
private PreparedStatement statement;
/** The mappings for the object id */
private JavaTypeMapping[] javaTypeMappings;
/** Number of the mapping being processed. */
private int mappingNum = 0;
/**
* Constructor.
* @param param Parameter positions
* @param ec ExecutionContext
* @param statement PreparedStatement
* @param javaTypeMappings Java mappings for the PC object
*/
public AppIDObjectIdFieldManager(int[] param, ExecutionContext ec, PreparedStatement statement, JavaTypeMapping[] javaTypeMappings)
{
this.params = param;
nextParam = 0;
this.ec = ec;
this.statement = statement;
// Save all mappings in the same order, allowing for PersistableMapping having sub mappings
int numMappings = 0;
for (int i=0;i<javaTypeMappings.length;i++)
{
if (javaTypeMappings[i] instanceof PersistableMapping)
{
numMappings += ((PersistableMapping)javaTypeMappings[i]).getJavaTypeMapping().length;
}
else
{
numMappings++;
}
}
this.javaTypeMappings = new JavaTypeMapping[numMappings];
int mappingNum = 0;
for (int i=0;i<javaTypeMappings.length;i++)
{
if (javaTypeMappings[i] instanceof PersistableMapping)
{
PersistableMapping m = (PersistableMapping)javaTypeMappings[i];
JavaTypeMapping[] subMappings = m.getJavaTypeMapping();
for (int j=0;j<subMappings.length;j++)
{
this.javaTypeMappings[mappingNum++] = subMappings[j];
}
}
else
{
this.javaTypeMappings[mappingNum++] = javaTypeMappings[i];
}
}
}
/**
* Convenience method to return the statement param position(s) for a field.
* @param mapping The mapping
* @return The param positions
*/
private int[] getParamsForField(JavaTypeMapping mapping)
{
if (javaTypeMappings.length == 1)
{
return params;
}
else
{
int numCols = mapping.getNumberOfDatastoreMappings();
int[] fieldParams = new int[numCols];
for (int i=0;i<numCols;i++)
{
fieldParams[i] = params[nextParam++];
}
return fieldParams;
}
}
/**
* Method to store a boolean in a field.
* @param fieldNumber Number of the field
* @param value The value to use
*/
public void storeBooleanField(int fieldNumber, boolean value)
{
JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
mapping.setBoolean(ec, statement, getParamsForField(mapping), value);
}
/**
* Method to store a byte in a field.
* @param fieldNumber Number of the field
* @param value The value to use
*/
public void storeByteField(int fieldNumber, byte value)
{
JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
mapping.setByte(ec, statement, getParamsForField(mapping), value);
}
/**
* Method to store a character in a field.
* @param fieldNumber Number of the field
* @param value The value to use
*/
public void storeCharField(int fieldNumber, char value)
{
JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
mapping.setChar(ec, statement, getParamsForField(mapping), value);
}
/**
* Method to store a double in a field.
* @param fieldNumber Number of the field
* @param value The value to use
*/
public void storeDoubleField(int fieldNumber, double value)
{
JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
mapping.setDouble(ec, statement, getParamsForField(mapping), value);
}
/**
* Method to store a float in a field.
* @param fieldNumber Number of the field
* @param value The value to use
*/
public void storeFloatField(int fieldNumber, float value)
{
JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
mapping.setFloat(ec, statement, getParamsForField(mapping), value);
}
/**
* Method to store an integer in a field.
* @param fieldNumber Number of the field
* @param value The value to use
*/
public void storeIntField(int fieldNumber, int value)
{
JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
mapping.setInt(ec, statement, getParamsForField(mapping), value);
}
/**
* Method to store a long in a field.
* @param fieldNumber Number of the field
* @param value The value to use
*/
public void storeLongField(int fieldNumber, long value)
{
JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
mapping.setLong(ec, statement, getParamsForField(mapping), value);
}
/**
* Method to store a short in a field.
* @param fieldNumber Number of the field
* @param value The value to use
*/
public void storeShortField(int fieldNumber, short value)
{
JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
mapping.setShort(ec, statement, getParamsForField(mapping), value);
}
/**
* Method to store a String in a field.
* @param fieldNumber Number of the field
* @param value The value to use
*/
public void storeStringField(int fieldNumber, String value)
{
JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
mapping.setString(ec, statement, getParamsForField(mapping), value);
}
/**
* Method to store an object in a field.
* @param fieldNumber Number of the field
* @param value The value to use
*/
public void storeObjectField(int fieldNumber, Object value)
{
ApiAdapter api = ec.getApiAdapter();
if (api.isPersistable(value))
{
((Persistable)value).dnCopyKeyFieldsFromObjectId(new AppIdObjectIdFieldConsumer(api, this), api.getIdForObject(value));
}
else
{
JavaTypeMapping mapping = javaTypeMappings[mappingNum++];
mapping.setObject(ec, statement, getParamsForField(mapping), value);
}
}
}