-
Notifications
You must be signed in to change notification settings - Fork 666
Expand file tree
/
Copy pathSQLiteConnection.java
More file actions
497 lines (439 loc) · 16.9 KB
/
SQLiteConnection.java
File metadata and controls
497 lines (439 loc) · 16.9 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package org.sqlite;
import org.sqlite.core.CoreDatabaseMetaData;
import org.sqlite.core.DB;
import org.sqlite.core.NativeDB;
import org.sqlite.jdbc4.JDBC4DatabaseMetaData;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Executor;
/**
*
*/
public abstract class SQLiteConnection
implements Connection
{
private static final String RESOURCE_NAME_PREFIX = ":resource:";
private final DB db;
private CoreDatabaseMetaData meta = null;
private final SQLiteConnectionConfig connectionConfig;
/**
* Connection constructor for reusing an existing DB handle
* @param db
*/
public SQLiteConnection(DB db) {
this.db = db;
connectionConfig = db.getConfig().newConnectionConfig();
}
/**
* Constructor to create a connection to a database at the given location.
* @param url The location of the database.
* @param fileName The database.
* @throws SQLException
*/
public SQLiteConnection(String url, String fileName) throws SQLException {
this(url, fileName, new Properties());
}
/**
* Constructor to create a pre-configured connection to a database at the
* given location.
* @param url The location of the database file.
* @param fileName The database.
* @param prop The configurations to apply.
* @throws SQLException
*/
public SQLiteConnection(String url, String fileName, Properties prop) throws SQLException {
this.db = open(url, fileName, prop);
SQLiteConfig config = db.getConfig();
this.connectionConfig = db.getConfig().newConnectionConfig();
config.apply(this);
}
public SQLiteConnectionConfig getConnectionConfig() {
return connectionConfig;
}
public CoreDatabaseMetaData getSQLiteDatabaseMetaData() throws SQLException {
checkOpen();
if (meta == null) {
meta = new JDBC4DatabaseMetaData(this);
}
return meta;
}
@Override
public DatabaseMetaData getMetaData()
throws SQLException
{
return (DatabaseMetaData) getSQLiteDatabaseMetaData();
}
public String getUrl() {
return db.getUrl();
}
public void setSchema(String schema) throws SQLException {
// TODO
}
public String getSchema() throws SQLException {
// TODO
return null;
}
public void abort(Executor executor) throws SQLException {
// TODO
}
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
// TODO
}
public int getNetworkTimeout() throws SQLException {
// TODO
return 0;
}
/**
* Checks whether the type, concurrency, and holdability settings for a
* {@link ResultSet} are supported by the SQLite interface. Supported
* settings are:<ul>
* <li>type: {@link ResultSet#TYPE_FORWARD_ONLY}</li>
* <li>concurrency: {@link ResultSet#CONCUR_READ_ONLY})</li>
* <li>holdability: {@link ResultSet#CLOSE_CURSORS_AT_COMMIT}</li></ul>
* @param rst the type setting.
* @param rsc the concurrency setting.
* @param rsh the holdability setting.
* @throws SQLException
*/
protected void checkCursor(int rst, int rsc, int rsh) throws SQLException {
if (rst != ResultSet.TYPE_FORWARD_ONLY)
throw new SQLException("SQLite only supports TYPE_FORWARD_ONLY cursors");
if (rsc != ResultSet.CONCUR_READ_ONLY)
throw new SQLException("SQLite only supports CONCUR_READ_ONLY cursors");
if (rsh != ResultSet.CLOSE_CURSORS_AT_COMMIT)
throw new SQLException("SQLite only supports closing cursors at commit");
}
/**
* Sets the mode that will be used to start transactions on this connection.
* @param mode One of {@link SQLiteConfig.TransactionMode}
* @see <a href="http://www.sqlite.org/lang_transaction.html">http://www.sqlite.org/lang_transaction.html</a>
*/
protected void setTransactionMode(SQLiteConfig.TransactionMode mode) {
connectionConfig.setTransactionMode(mode);
}
/**
* @see java.sql.Connection#getTransactionIsolation()
*/
@Override
public int getTransactionIsolation() {
return connectionConfig.getTransactionIsolation();
}
/**
* @see java.sql.Connection#setTransactionIsolation(int)
*/
public void setTransactionIsolation(int level) throws SQLException {
checkOpen();
switch (level) {
case java.sql.Connection.TRANSACTION_SERIALIZABLE:
getDatabase().exec("PRAGMA read_uncommitted = false;", getAutoCommit());
break;
case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED:
getDatabase().exec("PRAGMA read_uncommitted = true;", getAutoCommit());
break;
default:
throw new SQLException("SQLite supports only TRANSACTION_SERIALIZABLE and TRANSACTION_READ_UNCOMMITTED.");
}
connectionConfig.setTransactionIsolation(level);
}
/**
* Opens a connection to the database using an SQLite library.
* * @throws SQLException
* @see <a href="http://www.sqlite.org/c3ref/c_open_autoproxy.html">http://www.sqlite.org/c3ref/c_open_autoproxy.html</a>
*/
private static DB open(String url, String origFileName, Properties props) throws SQLException {
// Create a copy of the given properties
Properties newProps = new Properties();
newProps.putAll(props);
// Extract pragma as properties
String fileName = extractPragmasFromFilename(url, origFileName, newProps);
SQLiteConfig config = new SQLiteConfig(newProps);
// check the path to the file exists
if (!":memory:".equals(fileName) && !fileName.startsWith("file:") && !fileName.contains("mode=memory")) {
if (fileName.startsWith(RESOURCE_NAME_PREFIX)) {
String resourceName = fileName.substring(RESOURCE_NAME_PREFIX.length());
// search the class path
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
URL resourceAddr = contextCL.getResource(resourceName);
if (resourceAddr == null) {
try {
resourceAddr = new URL(resourceName);
}
catch (MalformedURLException e) {
throw new SQLException(String.format("resource %s not found: %s", resourceName, e));
}
}
try {
fileName = extractResource(resourceAddr).getAbsolutePath();
}
catch (IOException e) {
throw new SQLException(String.format("failed to load %s: %s", resourceName, e));
}
}
else {
File file = new File(fileName).getAbsoluteFile();
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
for (File up = parent; up != null && !up.exists();) {
parent = up;
up = up.getParentFile();
}
throw new SQLException("path to '" + fileName + "': '" + parent + "' does not exist");
}
// check write access if file does not exist
try {
// The extra check to exists() is necessary as createNewFile()
// does not follow the JavaDoc when used on read-only shares.
if (!file.exists() && file.createNewFile())
file.delete();
}
catch (Exception e) {
throw new SQLException("opening db: '" + fileName + "': " + e.getMessage());
}
fileName = file.getAbsolutePath();
}
}
// load the native DB
DB db = null;
try {
NativeDB.load();
db = new NativeDB(url, fileName, config);
}
catch (Exception e) {
SQLException err = new SQLException("Error opening connection");
err.initCause(e);
throw err;
}
db.open(fileName, config.getOpenModeFlags());
Set<ExtensionInfo> loadExtensions = config.getLoadExtensions();
if (!loadExtensions.isEmpty()) {
db.dbconfig_enable_load_extension(true);
for (ExtensionInfo ei : loadExtensions) {
db.load_extension(ei.getFile(), ei.getEntry());
}
db.dbconfig_enable_load_extension(false);
}
db.enable_load_extension(config.isEnabledLoadExtension());
return db;
}
/**
* Returns a file name from the given resource address.
* @param resourceAddr The resource address.
* @return The extracted file name.
* @throws IOException
*/
private static File extractResource(URL resourceAddr) throws IOException {
if (resourceAddr.getProtocol().equals("file")) {
try {
return new File(resourceAddr.toURI());
}
catch (URISyntaxException e) {
throw new IOException(e.getMessage());
}
}
String tempFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath();
String dbFileName = String.format("sqlite-jdbc-tmp-%d.db", resourceAddr.hashCode());
File dbFile = new File(tempFolder, dbFileName);
if (dbFile.exists()) {
long resourceLastModified = resourceAddr.openConnection().getLastModified();
long tmpFileLastModified = dbFile.lastModified();
if (resourceLastModified < tmpFileLastModified) {
return dbFile;
}
else {
// remove the old DB file
boolean deletionSucceeded = dbFile.delete();
if (!deletionSucceeded) {
throw new IOException("failed to remove existing DB file: " + dbFile.getAbsolutePath());
}
}
// String md5sum1 = SQLiteJDBCLoader.md5sum(resourceAddr.openStream());
// String md5sum2 = SQLiteJDBCLoader.md5sum(new FileInputStream(dbFile));
//
// if (md5sum1.equals(md5sum2))
// return dbFile; // no need to extract the DB file
// else
// {
// }
}
byte[] buffer = new byte[8192]; // 8K buffer
FileOutputStream writer = new FileOutputStream(dbFile);
InputStream reader = resourceAddr.openStream();
try {
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, bytesRead);
}
return dbFile;
}
finally {
writer.close();
reader.close();
}
}
public DB getDatabase() {
return db;
}
/**
* @see java.sql.Connection#getAutoCommit()
*/
@Override
public boolean getAutoCommit() throws SQLException {
checkOpen();
return connectionConfig.isAutoCommit();
}
/**
* @see java.sql.Connection#setAutoCommit(boolean)
*/
@Override
public void setAutoCommit(boolean ac) throws SQLException {
checkOpen();
if (connectionConfig.isAutoCommit() == ac)
return;
connectionConfig.setAutoCommit(ac);
db.exec(connectionConfig.isAutoCommit() ? "commit;" : connectionConfig.transactionPrefix(), ac);
}
/**
* @return The busy timeout value for the connection.
* @see <a href="http://www.sqlite.org/c3ref/busy_timeout.html">http://www.sqlite.org/c3ref/busy_timeout.html</a>
*/
public int getBusyTimeout() {
return db.getConfig().getBusyTimeout();
}
/**
* Sets the timeout value for the connection.
* A timeout value less than or equal to zero turns off all busy handlers.
* @see <a href="http://www.sqlite.org/c3ref/busy_timeout.html">http://www.sqlite.org/c3ref/busy_timeout.html</a>
* @param timeoutMillis The timeout value in milliseconds.
* @throws SQLException
*/
public void setBusyTimeout(int timeoutMillis)
throws SQLException
{
db.getConfig().setBusyTimeout(timeoutMillis);
db.busy_timeout(timeoutMillis);
}
@Override
public boolean isClosed() throws SQLException
{
return db.isClosed();
}
/**
* @see java.sql.Connection#close()
*/
@Override
public void close() throws SQLException {
if (isClosed())
return;
if (meta != null)
meta.close();
db.close();
}
/**
* Whether an SQLite library interface to the database has been established.
* @throws SQLException
*/
protected void checkOpen() throws SQLException {
if (isClosed())
throw new SQLException("database connection closed");
}
/**
* @return Compile-time library version numbers.
* @throws SQLException
* @see <a href="http://www.sqlite.org/c3ref/c_source_id.html">http://www.sqlite.org/c3ref/c_source_id.html</a>
*/
public String libversion() throws SQLException {
checkOpen();
return db.libversion();
}
/**
* @see java.sql.Connection#commit()
*/
@Override
public void commit() throws SQLException {
checkOpen();
if (connectionConfig.isAutoCommit())
throw new SQLException("database in auto-commit mode");
db.exec("commit;", getAutoCommit());
db.exec(connectionConfig.transactionPrefix(), getAutoCommit());
}
/**
* @see java.sql.Connection#rollback()
*/
@Override
public void rollback() throws SQLException {
checkOpen();
if (connectionConfig.isAutoCommit())
throw new SQLException("database in auto-commit mode");
db.exec("rollback;", getAutoCommit());
db.exec(connectionConfig.transactionPrefix(), getAutoCommit());
}
/**
* Extracts PRAGMA values from the filename and sets them into the Properties
* object which will be used to build the SQLConfig. The sanitized filename
* is returned.
*
* @param filename
* @param prop
* @return a PRAGMA-sanitized filename
* @throws SQLException
*/
protected static String extractPragmasFromFilename(String url, String filename, Properties prop) throws SQLException {
int parameterDelimiter = filename.indexOf('?');
if (parameterDelimiter == -1) {
// nothing to extract
return filename;
}
StringBuilder sb = new StringBuilder();
sb.append(filename.substring(0, parameterDelimiter));
int nonPragmaCount = 0;
String [] parameters = filename.substring(parameterDelimiter + 1).split("&");
for (int i = 0; i < parameters.length; i++) {
// process parameters in reverse-order, last specified pragma value wins
String parameter = parameters[parameters.length - 1 - i].trim();
if (parameter.isEmpty()) {
// duplicated &&& sequence, drop
continue;
}
String [] kvp = parameter.split("=");
String key = kvp[0].trim().toLowerCase();
if (SQLiteConfig.pragmaSet.contains(key)) {
if (kvp.length == 1) {
throw new SQLException(String.format("Please specify a value for PRAGMA %s in URL %s", key, url));
}
String value = kvp[1].trim();
if (!value.isEmpty()) {
if (prop.containsKey(key)) {
//
// IGNORE
//
// this allows DriverManager.getConnection(String, Properties)
// to override URL parameters programmatically.
//
// It also ignores duplicate pragma keys in the URL. The reversed
// processing order ensures the last-supplied pragma value is used.
} else {
prop.setProperty(key, value);
}
}
} else {
// not a Pragma, retain as part of filename
sb.append(nonPragmaCount == 0 ? '?' : '&');
sb.append(parameter);
nonPragmaCount++;
}
}
final String newFilename = sb.toString();
return newFilename;
}
}