/
NuoDBAdapter.java
327 lines (288 loc) · 11.5 KB
/
NuoDBAdapter.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
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
325
326
327
/**********************************************************************
Copyright (c) 2013 Andy Jefferson 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:
...
**********************************************************************/
package org.datanucleus.store.rdbms.adapter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import org.datanucleus.exceptions.NucleusDataStoreException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.store.rdbms.identifier.IdentifierFactory;
import org.datanucleus.store.rdbms.key.Index;
import org.datanucleus.store.rdbms.key.PrimaryKey;
import org.datanucleus.store.rdbms.schema.SQLTypeInfo;
import org.datanucleus.store.schema.StoreSchemaHandler;
import org.datanucleus.util.StringUtils;
/**
* Adapter for NuoDB (http://www.nuodb.com).
* This adapter was written based on v2.0.2 of the NuoDB JDBC driver.
*/
public class NuoDBAdapter extends BaseDatastoreAdapter
{
public static final String NONSQL92_RESERVED_WORDS =
"BIGINT,BINARY,BLOB,BOOLEAN,CLOB,LIMIT,NCLOB,OFFSET,ROLE,TRIGGER";
public static final String NUODB_EXTRA_RESERVED_WORDS =
"BITS,BREAK,CATCH,CONTAINING,END_FOR,END_IF,END_PROCEDURE,END_TRIGGER,END_TRY,END_WHILE,ENUM,FOR_UPDATE,IF," +
"LOGICAL_AND,LOGICAL_NOT,LOGICAL_OR,NEXT_VALUE,NOT_BETWEEN,NOT_CONTAINING,NOT_IN,NOT_LIKE,NOT_STARTING,NVARCHAR,OFF," +
"RECORD_BATCHING,REGEXP,SHOW,SMALLDATETIME,STARTING,STRING_TYPE,THROW,TINYBLOB,TINYINT,TRY,VAR,VER";
public NuoDBAdapter(DatabaseMetaData metadata)
{
super(metadata);
reservedKeywords.addAll(StringUtils.convertCommaSeparatedStringToSet(NONSQL92_RESERVED_WORDS));
reservedKeywords.addAll(StringUtils.convertCommaSeparatedStringToSet(NUODB_EXTRA_RESERVED_WORDS));
supportedOptions.add(IDENTITY_COLUMNS);
supportedOptions.add(SEQUENCES);
supportedOptions.add(PRIMARYKEY_IN_CREATE_STATEMENTS);
supportedOptions.add(LOCK_WITH_SELECT_FOR_UPDATE);
supportedOptions.add(STORED_PROCEDURES);
// NuoDB JDBC driver doesn't specify lengths in 2.0.2
if (maxTableNameLength <= 0)
{
maxTableNameLength = 128;
}
if (maxColumnNameLength <= 0)
{
maxColumnNameLength = 128;
}
if (maxConstraintNameLength <= 0)
{
maxConstraintNameLength = 128;
}
if (maxIndexNameLength <= 0)
{
maxIndexNameLength = 128;
}
// CROSS JOIN syntax is not supported
supportedOptions.remove(ANSI_CROSSJOIN_SYNTAX);
supportedOptions.add(CROSSJOIN_ASINNER11_SYNTAX);
// Doesn't seem to support FK constraints
supportedOptions.remove(FK_DELETE_ACTION_RESTRICT);
supportedOptions.remove(FK_DELETE_ACTION_NULL);
supportedOptions.remove(FK_UPDATE_ACTION_RESTRICT);
supportedOptions.remove(FK_UPDATE_ACTION_NULL);
supportedOptions.remove(FK_DELETE_ACTION_CASCADE);
supportedOptions.remove(FK_DELETE_ACTION_DEFAULT);
supportedOptions.remove(FK_UPDATE_ACTION_CASCADE);
supportedOptions.remove(FK_UPDATE_ACTION_DEFAULT);
supportedOptions.remove(DEFERRED_CONSTRAINTS);
supportedOptions.remove(RESULTSET_TYPE_SCROLL_SENSITIVE);
supportedOptions.remove(RESULTSET_TYPE_SCROLL_INSENSITIVE);
supportedOptions.remove(TX_ISOLATION_REPEATABLE_READ);
supportedOptions.remove(TX_ISOLATION_READ_UNCOMMITTED);
supportedOptions.remove(TX_ISOLATION_NONE);
supportedOptions.remove(ACCESS_PARENTQUERY_IN_SUBQUERY_JOINED);
}
public String getVendorID()
{
return "nuodb";
}
public String getCatalogName(Connection conn) throws SQLException
{
return null;
}
public String getSchemaName(Connection conn) throws SQLException
{
Statement stmt = conn.createStatement();
try
{
String stmtText = "SELECT CURRENT_SCHEMA FROM DUAL";
ResultSet rs = stmt.executeQuery(stmtText);
try
{
if (!rs.next())
{
throw new NucleusDataStoreException("No result returned from " + stmtText).setFatal();
}
return rs.getString(1);
}
finally
{
rs.close();
}
}
finally
{
stmt.close();
}
}
public void initialiseTypes(StoreSchemaHandler handler, ManagedConnection mconn)
{
super.initialiseTypes(handler, mconn);
SQLTypeInfo sqlType = new org.datanucleus.store.rdbms.schema.NuoDBTypeInfo(
"FLOAT", (short) Types.DOUBLE, 53, null, null, null, 1, false, (short) 2,
false, false, false, null, (short) 0, (short) 0, 2);
addSQLTypeForJDBCType(handler, mconn, (short) Types.DOUBLE, sqlType, true);
sqlType = new org.datanucleus.store.rdbms.schema.NuoDBTypeInfo(
"TEXT", (short) Types.CLOB, 2147483647, null, null, null, 1, true, (short) 1,
false, false, false, "TEXT", (short) 0, (short) 0, 0);
addSQLTypeForJDBCType(handler, mconn, (short) Types.CLOB, sqlType, true);
}
/**
* Returns the appropriate DDL to create an index. It should return something like:
* <pre>
* CREATE INDEX FOO_N1 ON FOO (BAR,BAZ) [Extended Settings]
* CREATE UNIQUE INDEX FOO_U1 ON FOO (BAR,BAZ) [Extended Settings]
* </pre>
* @param idx An object describing the index.
* @param factory Identifier factory
* @return The text of the SQL statement.
*/
public String getCreateIndexStatement(Index idx, IdentifierFactory factory)
{
String idxIdentifier = factory.getIdentifierInAdapterCase(idx.getName());
return "CREATE " + (idx.getUnique() ? "UNIQUE " : "") + "INDEX " + idxIdentifier +
" ON " + idx.getTable().toString() + ' ' +
idx + (idx.getExtendedIndexSettings() == null ? "" : " " + idx.getExtendedIndexSettings());
}
public String getDropDatabaseStatement(String catalogName, String schemaName)
{
return "DROP SCHEMA " + schemaName + " CASCADE";
}
/**
* Override the default implementation since we accept the PK in the CREATE TABLE statement.
* @param pk An object describing the primary key.
* @param factory Identifier factory
* @return The PK statement
*/
public String getAddPrimaryKeyStatement(PrimaryKey pk, IdentifierFactory factory)
{
// We generate the PK in the CREATE TABLE statement
return null;
}
public boolean sequenceExists(Connection conn, String catalogName, String schemaName, String seqName)
{
/*PreparedStatement ps = null;
ResultSet rs = null;
try
{
String GET_SEQUENCE_STMT = "SELECT * FROM SYSTEM.SEQUENCES";
NucleusLogger.DATASTORE_SCHEMA.debug("Retrieving sequence info using the following SQL : " + GET_SEQUENCE_STMT);
ps = conn.prepareStatement(GET_SEQUENCE_STMT);
rs = ps.executeQuery();
while (rs.next())
{
}
}
catch (SQLException sqle)
{
NucleusLogger.DATASTORE_SCHEMA.warn(">> Exception caught", sqle);
}
finally
{
try
{
if (rs != null && !rs.isClosed())
{
rs.close();
}
ps.close();
}
catch (SQLException sqle)
{
}
}*/
// TODO Make use of the following
// SELECT * FROM SYSTEM.SEQUENCES;
//SCHEMA SEQUENCENAME
//====== ============
//HOCKEY HOCKEY$IDENTITY_SEQUENCE
return true;
}
/**
* Accessor for the sequence statement to create the sequence.
* @param sequence_name Name of the sequence
* @param min Minimum value for the sequence
* @param max Maximum value for the sequence
* @param start Start value for the sequence
* @param increment Increment value for the sequence
* @param cache_size Cache size for the sequence
* @return The statement for getting the next id from the sequence
*/
public String getSequenceCreateStmt(String sequence_name,
Integer min, Integer max, Integer start, Integer increment, Integer cache_size)
{
if (sequence_name == null)
{
throw new NucleusUserException(LOCALISER.msg("051028"));
}
StringBuilder stmt = new StringBuilder("CREATE SEQUENCE ");
stmt.append(sequence_name);
if (start != null)
{
stmt.append(" START WITH " + start);
}
// TODO Support other parameters if NuoDB ever supports them
return stmt.toString();
}
/**
* Accessor for the statement for getting the next id from the sequence for this datastore.
* @param sequence_name Name of the sequence
* @return The statement for getting the next id for the sequence
**/
public String getSequenceNextStmt(String sequence_name)
{
if (sequence_name == null)
{
throw new NucleusUserException(LOCALISER.msg("051028"));
}
StringBuilder stmt=new StringBuilder("SELECT NEXT VALUE FOR ");
stmt.append(sequence_name);
stmt.append(" FROM DUAL");
return stmt.toString();
}
/**
* Accessor for the auto-increment keyword for generating DDLs (CREATE TABLEs...).
* @return The keyword for a column using auto-increment
*/
public String getAutoIncrementKeyword()
{
return "GENERATED BY DEFAULT AS IDENTITY";
}
/**
* Method to return the SQL to append to the WHERE clause of a SELECT statement to handle
* restriction of ranges using the OFFSET/FETCH keywords.
* @param offset The offset to return from
* @param count The number of items to return
* @return The SQL to append to allow for ranges using OFFSET/FETCH.
*/
public String getRangeByLimitEndOfStatementClause(long offset, long count)
{
if (datastoreMajorVersion < 10 || (datastoreMajorVersion == 10 && datastoreMinorVersion < 5))
{
return super.getRangeByLimitEndOfStatementClause(offset, count);
}
else if (offset <= 0 && count <= 0)
{
return "";
}
StringBuilder str = new StringBuilder();
if (offset > 0)
{
str.append("OFFSET " + offset + (offset > 1 ? " ROWS " : " ROW "));
}
if (count > 0)
{
str.append("FETCH NEXT " + (count > 1 ? (count + " ROWS ONLY ") : "ROW ONLY "));
}
return str.toString();
}
public String getDatastoreDateStatement()
{
return "SELECT CURRENT_DATE FROM DUAL";
}
}