forked from xerial/sqlite-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteDataSource.java
More file actions
executable file
·489 lines (441 loc) · 17.4 KB
/
Copy pathSQLiteDataSource.java
File metadata and controls
executable file
·489 lines (441 loc) · 17.4 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
/*--------------------------------------------------------------------------
* Copyright 2010 Taro L. Saito
*
* 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.
*--------------------------------------------------------------------------*/
// --------------------------------------
// sqlite-jdbc Project
//
// SQLiteDataSource.java
// Since: Mar 11, 2010
//
// $URL$
// $Author$
// --------------------------------------
package org.sqlite;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.sqlite.SQLiteConfig.*;
/**
* Provides {@link DataSource} API for configuring SQLite database connection
*
* @author leo
*/
public class SQLiteDataSource implements DataSource {
private SQLiteConfig config;
private transient PrintWriter logger;
private int loginTimeout = 1;
private String url = JDBC.PREFIX; // use memory database in default
private String databaseName = ""; // the name of the current database
/** Default constructor. */
public SQLiteDataSource() {
this.config = new SQLiteConfig(); // default configuration
}
/**
* Creates a data source based on the provided configuration.
*
* @param config The configuration for the data source.
*/
public SQLiteDataSource(SQLiteConfig config) {
this.config = config;
}
/**
* Sets a data source's configuration.
*
* @param config The configuration.
*/
public void setConfig(SQLiteConfig config) {
this.config = config;
}
/** @return The configuration for the data source. */
public SQLiteConfig getConfig() {
return config;
}
/**
* Sets the location of the database file.
*
* @param url The location of the database file.
*/
public void setUrl(String url) {
this.url = url;
}
/** @return The location of the database file. */
public String getUrl() {
return url;
}
/**
* Sets the database name.
*
* @param databaseName The name of the database
*/
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
/**
* @return The name of the database if one was set.
* @see SQLiteDataSource#setDatabaseName(String)
*/
public String getDatabaseName() {
return databaseName;
}
/**
* Enables or disables the sharing of the database cache and schema data structures between
* connections to the same database.
*
* @param enable True to enable; false to disable.
* @see <a
* href="http://www.sqlite.org/c3ref/enable_shared_cache.html">http://www.sqlite.org/c3ref/enable_shared_cache.html</a>
*/
public void setSharedCache(boolean enable) {
config.setSharedCache(enable);
}
/**
* Enables or disables extension loading.
*
* @param enable True to enable; false to disable.
* @see <a
* href="http://www.sqlite.org/c3ref/load_extension.html">http://www.sqlite.org/c3ref/load_extension.html</a>
*/
public void setLoadExtension(boolean enable) {
config.enableLoadExtension(enable);
}
/**
* Sets the database to be opened in read-only mode
*
* @param readOnly True to enable; false to disable.
* @see <a
* href="http://www.sqlite.org/c3ref/c_open_autoproxy.html">http://www.sqlite.org/c3ref/c_open_autoproxy.html</a>
*/
public void setReadOnly(boolean readOnly) {
config.setReadOnly(readOnly);
}
/**
* Sets the suggested maximum number of database disk pages that SQLite will hold in memory at
* once per open database file.
*
* @param numberOfPages The number of database disk pages.
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_cache_size">http://www.sqlite.org/pragma.html#pragma_cache_size</a>
*/
public void setCacheSize(int numberOfPages) {
config.setCacheSize(numberOfPages);
}
/**
* Enables or disables case sensitivity for the built-in LIKE operator.
*
* @param enable True to enable; false to disable.
* @see <a
* href="http://www.sqlite.org/compile.html#case_sensitive_like">http://www.sqlite.org/compile.html#case_sensitive_like</a>
*/
public void setCaseSensitiveLike(boolean enable) {
config.enableCaseSensitiveLike(enable);
}
/**
* Enables or disables the count-changes flag. When enabled INSERT, UPDATE and DELETE statements
* return the number of rows they modified.
*
* @param enable True to enable; false to disable.
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_count_changes">http://www.sqlite.org/pragma.html#pragma_count_changes</a>
*/
public void setCountChanges(boolean enable) {
config.enableCountChanges(enable);
}
/**
* Sets the default maximum number of database disk pages that SQLite will hold in memory at
* once per open database file.
*
* @param numberOfPages The default suggested cache size.
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_cache_size">http://www.sqlite.org/pragma.html#pragma_cache_size</a>
*/
public void setDefaultCacheSize(int numberOfPages) {
config.setDefaultCacheSize(numberOfPages);
}
/**
* Sets the text encoding used by the main database.
*
* @param encoding One of "UTF-8", "UTF-16le" (little-endian UTF-16) or "UTF-16be" (big-endian
* UTF-16).
* @see <a href="http://www.sqlite.org/pragma.html#pragma_encoding">
* http://www.sqlite.org/pragma.html#pragma_encoding</a>
*/
public void setEncoding(String encoding) {
config.setEncoding(Encoding.getEncoding(encoding));
}
/**
* Enables or disables the enforcement of foreign key constraints.
*
* @param enforce True to enable; false to disable.
* @see <a href="http://www.sqlite.org/pragma.html#pragma_foreign_keys">
* http://www.sqlite.org/pragma.html#pragma_foreign_keys</a>
*/
public void setEnforceForeignKeys(boolean enforce) {
config.enforceForeignKeys(enforce);
}
/**
* Enables or disables the full_column_names flag. This flag together with the
* short_column_names flag determine the way SQLite assigns names to result columns of SELECT
* statements.
*
* @param enable True to enable; false to disable.
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_full_column_names">http://www.sqlite.org/pragma.html#pragma_full_column_names</a>
*/
public void setFullColumnNames(boolean enable) {
config.enableFullColumnNames(enable);
}
/**
* Enables or disables the fullfsync flag. This flag determines whether or not the F_FULLFSYNC
* syncing method is used on systems that support it.
*
* @param enable True to enable; false to disable.
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_fullfsync">http://www.sqlite.org/pragma.html#pragma_fullfsync</a>
*/
public void setFullSync(boolean enable) {
config.enableFullSync(enable);
}
/**
* Set the incremental_vacuum value that causes up to N pages to be removed from the <a
* href="http://www.sqlite.org/fileformat2.html#freelist">http://www.sqlite.org/fileformat2.html#freelist</a>.
*
* @param numberOfPagesToBeRemoved
* @see <a href="http://www.sqlite.org/pragma.html#pragma_incremental_vacuum">
* http://www.sqlite.org/pragma.html#pragma_incremental_vacuum</a>
*/
public void setIncrementalVacuum(int numberOfPagesToBeRemoved) {
config.incrementalVacuum(numberOfPagesToBeRemoved);
}
/**
* Sets the journal mode for databases associated with the current database connection.
*
* @param mode One of DELETE, TRUNCATE, PERSIST, MEMORY, WAL or OFF.
* @see <a href="http://www.sqlite.org/pragma.html#pragma_journal_mode">
* http://www.sqlite.org/pragma.html#pragma_journal_mode</a>
*/
public void setJournalMode(String mode) {
config.setJournalMode(JournalMode.valueOf(mode));
}
/**
* Sets the limit of the size of rollback-journal and WAL files left in the file-system after
* transactions or checkpoints.
*
* @param limit The default journal size limit is -1 (no limit).
* @see <a href="http://www.sqlite.org/pragma.html#pragma_journal_size_limit">
* http://www.sqlite.org/pragma.html#pragma_journal_size_limit</a>
*/
public void setJournalSizeLimit(int limit) {
config.setJounalSizeLimit(limit);
}
/**
* Set the value of the legacy_file_format flag. When this flag is on, new databases are created
* in a file format that is readable and writable by all versions of SQLite going back to 3.0.0.
* When the flag is off, new databases are created using the latest file format which might not
* be readable or writable by versions of SQLite prior to 3.3.0.
*
* @param use True to turn on; false to turn off.
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_legacy_file_format">http://www.sqlite.org/pragma.html#pragma_legacy_file_format</a>
*/
public void setLegacyFileFormat(boolean use) {
config.useLegacyFileFormat(use);
}
/**
* Sets the database connection locking-mode.
*
* @param mode Either NORMAL or EXCLUSIVE.
* @see <a href="http://www.sqlite.org/pragma.html#pragma_locking_mode">
* http://www.sqlite.org/pragma.html#pragma_locking_mode</a>
*/
public void setLockingMode(String mode) {
config.setLockingMode(LockingMode.valueOf(mode));
}
/**
* Set the page size of the database.
*
* @param numBytes The page size must be a power of two between 512 and 65536 inclusive.
* @see <a href="http://www.sqlite.org/pragma.html#pragma_page_size">
* http://www.sqlite.org/pragma.html#pragma_page_size</a>
*/
public void setPageSize(int numBytes) {
config.setPageSize(numBytes);
}
/**
* Set the maximum number of pages in the database file.
*
* @param numPages The maximum page count cannot be reduced below the current database size.
* @see <a href="http://www.sqlite.org/pragma.html#pragma_max_page_count">
* http://www.sqlite.org/pragma.html#pragma_max_page_count</a>
*/
public void setMaxPageCount(int numPages) {
config.setMaxPageCount(numPages);
}
/**
* Set READ UNCOMMITTED isolation
*
* @param useReadUncommitedIsolationMode True to turn on; false to turn off.
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_read_uncommitted">http://www.sqlite.org/pragma.html#pragma_read_uncommitted</a>
*/
public void setReadUncommited(boolean useReadUncommitedIsolationMode) {
config.setReadUncommited(useReadUncommitedIsolationMode);
}
/**
* Enables or disables the recursive trigger capability. Changing the recursive_triggers setting
* affects the execution of all statements prepared using the database connection, including
* those prepared before the setting was changed.
*
* @param enable True to enable; fase to disable.
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_recursive_triggers">http://www.sqlite.org/pragma.html#pragma_recursive_triggers</a>
*/
public void setRecursiveTriggers(boolean enable) {
config.enableRecursiveTriggers(enable);
}
/**
* Enables or disables the reverse_unordered_selects flag. When enabled it causes SELECT
* statements without an ORDER BY clause to emit their results in the reverse order of what they
* normally would.
*
* @param enable True to enable; fase to disable.
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_reverse_unordered_selects">http://www.sqlite.org/pragma.html#pragma_reverse_unordered_selects</a>
*/
public void setReverseUnorderedSelects(boolean enable) {
config.enableReverseUnorderedSelects(enable);
}
/**
* Enables or disables the short_column_names flag. This flag affects the way SQLite names
* columns of data returned by SELECT statements.
*
* @param enable True to enable; fase to disable.
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_short_column_names">http://www.sqlite.org/pragma.html#pragma_short_column_names</a>
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_fullfsync">http://www.sqlite.org/pragma.html#pragma_fullfsync</a>
*/
public void setShortColumnNames(boolean enable) {
config.enableShortColumnNames(enable);
}
/**
* Sets the setting of the "synchronous" flag.
*
* @param mode One of OFF, NORMAL or FULL;
* @see <a href="http://www.sqlite.org/pragma.html#pragma_synchronous">
* http://www.sqlite.org/pragma.html#pragma_synchronous</a>
*/
public void setSynchronous(String mode) {
config.setSynchronous(SynchronousMode.valueOf(mode));
}
/**
* Set the temp_store type which is used to determine where temporary tables and indices are
* stored.
*
* @param storeType One of "DEFAULT", "FILE", "MEMORY"
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_temp_store">http://www.sqlite.org/pragma.html#pragma_temp_store</a>
*/
public void setTempStore(String storeType) {
config.setTempStore(TempStore.valueOf(storeType));
}
/**
* Set the value of the sqlite3_temp_directory global variable, which many operating-system
* interface backends use to determine where to store temporary tables and indices.
*
* @param directoryName The temporary directory name.
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_temp_store_directory">http://www.sqlite.org/pragma.html#pragma_temp_store_directory</a>
*/
public void setTempStoreDirectory(String directoryName) {
config.setTempStoreDirectory(directoryName);
}
/**
* Sets the mode that will be used to start transactions for this database.
*
* @param transactionMode One of DEFERRED, IMMEDIATE or EXCLUSIVE.
* @see <a
* href="http://www.sqlite.org/lang_transaction.html">http://www.sqlite.org/lang_transaction.html</a>
*/
public void setTransactionMode(String transactionMode) {
config.setTransactionMode(transactionMode);
}
/**
* Sets the value of the user-version. It is a big-endian 32-bit signed integer stored in the
* database header at offset 60.
*
* @param version
* @see <a
* href="http://www.sqlite.org/pragma.html#pragma_schema_version">http://www.sqlite.org/pragma.html#pragma_schema_version</a>
*/
public void setUserVersion(int version) {
config.setUserVersion(version);
}
// codes for the DataSource interface
/** @see javax.sql.DataSource#getConnection() */
public Connection getConnection() throws SQLException {
return getConnection(null, null);
}
/** @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String) */
public SQLiteConnection getConnection(String username, String password) throws SQLException {
Properties p = config.toProperties();
if (username != null) p.put("user", username);
if (password != null) p.put("pass", password);
return JDBC.createConnection(url, p);
}
/** @see javax.sql.DataSource#getLogWriter() */
public PrintWriter getLogWriter() throws SQLException {
return logger;
}
/** @see javax.sql.DataSource#getLoginTimeout() */
public int getLoginTimeout() throws SQLException {
return loginTimeout;
}
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException("getParentLogger");
}
/** @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter) */
public void setLogWriter(PrintWriter out) throws SQLException {
this.logger = out;
}
/** @see javax.sql.DataSource#setLoginTimeout(int) */
public void setLoginTimeout(int seconds) throws SQLException {
loginTimeout = seconds;
}
/**
* Determines if this object wraps a given class.
*
* @param iface The class to check.
* @return True if it is an instance of the current class; false otherwise.
* @throws SQLException
*/
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return iface.isInstance(this);
}
/**
* Casts this object to the given class.
*
* @param iface The class to cast to.
* @return The casted class.
* @throws SQLException
*/
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
return (T) this;
}
}