Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/main/java/org/sqlite/SQLiteConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,18 @@ public enum Pragma {
OnOff.Values),

// Pragmas that can be set after opening the database
AUTOMATIC_INDEX(
"automatic_index",
"When automatic_index is on, SQLite may create transient indexes for queries that would otherwise do a full table scan",
OnOff.Values),
CACHE_SIZE(
"cache_size",
"Maximum number of database disk pages that SQLite will hold in memory at once per open database file",
null),
CACHE_SPILL(
"cache_spill",
"When cache_spill is on, the pager can spill dirty cache pages to the database file in the middle of a transaction. An integer N sets the spill threshold in pages",
null),
MMAP_SIZE(
"mmap_size",
"Maximum number of bytes that are set aside for memory-mapped I/O on a single database",
Expand Down Expand Up @@ -665,6 +673,42 @@ public void setCacheSize(int numberOfPages) {
set(Pragma.CACHE_SIZE, numberOfPages);
}

/**
* Enables or disables automatic indexes. When enabled, SQLite may create transient indexes for
* queries that would otherwise do a full table scan.
*
* @param enable True to enable; false to disable.
* @see <a
* href="https://www.sqlite.org/pragma.html#pragma_automatic_index">www.sqlite.org/pragma.html#pragma_automatic_index</a>
*/
public void enableAutomaticIndex(boolean enable) {
set(Pragma.AUTOMATIC_INDEX, enable);
}

/**
* Enables or disables cache spill. When enabled, the pager can write dirty cache pages to the
* database file in the middle of a transaction.
*
* @param enable True to enable; false to disable.
* @see <a
* href="https://www.sqlite.org/pragma.html#pragma_cache_spill">www.sqlite.org/pragma.html#pragma_cache_spill</a>
*/
public void setCacheSpill(boolean enable) {
set(Pragma.CACHE_SPILL, enable);
}

/**
* Sets the cache-spill threshold in pages. Dirty pages are spilled when that many pages of
* cache are dirty.
*
* @param numberOfPages Spill threshold in pages.
* @see <a
* href="https://www.sqlite.org/pragma.html#pragma_cache_spill">www.sqlite.org/pragma.html#pragma_cache_spill</a>
*/
public void setCacheSpill(int numberOfPages) {
set(Pragma.CACHE_SPILL, numberOfPages);
}

/**
* Enables or disables case sensitive for the LIKE operator.
*
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/org/sqlite/SQLiteDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,39 @@ public void setCacheSize(int numberOfPages) {
config.setCacheSize(numberOfPages);
}

/**
* Enables or disables automatic indexes.
*
* @param enable True to enable; false to disable.
* @see <a
* href="https://www.sqlite.org/pragma.html#pragma_automatic_index">https://www.sqlite.org/pragma.html#pragma_automatic_index</a>
*/
public void setAutomaticIndex(boolean enable) {
config.enableAutomaticIndex(enable);
}

/**
* Enables or disables cache spill.
*
* @param enable True to enable; false to disable.
* @see <a
* href="https://www.sqlite.org/pragma.html#pragma_cache_spill">https://www.sqlite.org/pragma.html#pragma_cache_spill</a>
*/
public void setCacheSpill(boolean enable) {
config.setCacheSpill(enable);
}

/**
* Sets the cache-spill threshold in pages.
*
* @param numberOfPages Spill threshold in pages.
* @see <a
* href="https://www.sqlite.org/pragma.html#pragma_cache_spill">https://www.sqlite.org/pragma.html#pragma_cache_spill</a>
*/
public void setCacheSpill(int numberOfPages) {
config.setCacheSpill(numberOfPages);
}

/**
* Enables or disables case sensitivity for the built-in LIKE operator.
*
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/sqlite/ConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,25 @@ public void limits() throws Exception {
conn.close();
}

@Test
public void automaticIndexAndCacheSpillInURI() throws Exception {
Connection conn =
DriverManager.getConnection(
"jdbc:sqlite:file::memory:?automatic_index=OFF&cache_spill=OFF");
Statement stat = conn.createStatement();

ResultSet rs = stat.executeQuery("pragma automatic_index");
assertThat(rs.getBoolean(1)).isFalse();
rs.close();

rs = stat.executeQuery("pragma cache_spill");
assertThat(rs.getBoolean(1)).isFalse();
rs.close();

stat.close();
conn.close();
}

@Test
public void ignoreUnknownParametersInURI() throws Exception {
Connection conn =
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/sqlite/SQLiteConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ public void setWalAutocheckpointDisabled() {
.isEqualTo("0");
}

@Test
public void enableAutomaticIndex() {
SQLiteConfig config = new SQLiteConfig();
config.enableAutomaticIndex(false);
assertThat(
config.toProperties()
.getProperty(SQLiteConfig.Pragma.AUTOMATIC_INDEX.getPragmaName()))
.isEqualTo("false");
}

@Test
public void setCacheSpill() {
SQLiteConfig config = new SQLiteConfig();
config.setCacheSpill(false);
assertThat(
config.toProperties()
.getProperty(SQLiteConfig.Pragma.CACHE_SPILL.getPragmaName()))
.isEqualTo("false");
config.setCacheSpill(100);
assertThat(
config.toProperties()
.getProperty(SQLiteConfig.Pragma.CACHE_SPILL.getPragmaName()))
.isEqualTo("100");
}

@Test
public void pragmaSet() {
Set<String> expectedPragmaSet = new HashSet<>();
Expand Down
Loading