Skip to content

Commit 46f85e0

Browse files
committed
feat(android): optional Database open flags
1 parent 21e0a9e commit 46f85e0

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

android/modules/database/src/java/ti/modules/titanium/database/DatabaseModule.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.appcelerator.titanium.io.TiBaseFile;
2424
import org.appcelerator.titanium.io.TiFile;
2525
import org.appcelerator.titanium.io.TiFileFactory;
26+
import org.appcelerator.titanium.util.TiConvert;
2627
import org.appcelerator.titanium.util.TiUrl;
2728

2829
import android.content.Context;
@@ -45,13 +46,24 @@ public class DatabaseModule extends KrollModule
4546
@Kroll.constant
4647
public static final int FIELD_TYPE_DOUBLE = 3;
4748

49+
@Kroll.constant
50+
public static final int CREATE_IF_NECESSARY = SQLiteDatabase.CREATE_IF_NECESSARY;
51+
@Kroll.constant
52+
public static final int NO_LOCALIZED_COLLATORS = SQLiteDatabase.NO_LOCALIZED_COLLATORS;
53+
@Kroll.constant
54+
public static final int ENABLE_WRITE_AHEAD_LOGGING = SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
55+
@Kroll.constant
56+
public static final int OPEN_READWRITE = SQLiteDatabase.OPEN_READWRITE;
57+
@Kroll.constant
58+
public static final int OPEN_READONLY = SQLiteDatabase.OPEN_READONLY;
59+
4860
public DatabaseModule()
4961
{
5062
super();
5163
}
5264

5365
@Kroll.method
54-
public TiDatabaseProxy open(Object file)
66+
public TiDatabaseProxy open(Object file, @Kroll.argument(optional = true) Object flags)
5567
{
5668
// Acquire database name or file object providing full file path from argument.
5769
TiBaseFile dbTiBaseFile = null;
@@ -97,8 +109,12 @@ public TiDatabaseProxy open(Object file)
97109
throw new IllegalArgumentException(message);
98110
}
99111
Log.d(TAG, "Opening database from filesystem: " + absolutePath);
112+
int flagValues = SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.NO_LOCALIZED_COLLATORS;
113+
if (flags != null) {
114+
flagValues = TiConvert.toInt(flags);
115+
}
100116
SQLiteDatabase db = SQLiteDatabase.openDatabase(
101-
absolutePath, null, SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
117+
absolutePath, null, flagValues);
102118
if (db != null) {
103119
dbp = new TiDatabaseProxy(db);
104120
} else {
@@ -137,7 +153,7 @@ public TiDatabaseProxy install(KrollInvocation invocation, String url, String na
137153
Context ctx = TiApplication.getInstance();
138154
for (String dbname : ctx.databaseList()) {
139155
if (dbname.equals(name)) {
140-
return open(name);
156+
return open(name, null);
141157
}
142158
}
143159

@@ -195,7 +211,7 @@ public TiDatabaseProxy install(KrollInvocation invocation, String url, String na
195211
}
196212

197213
// Open a connection to the installed database.
198-
return open(name);
214+
return open(name, null);
199215
}
200216

201217
@Override

apidoc/Titanium/Database/Database.yml

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ methods:
1111
summary: Installs an SQLite database to device's internal storage.
1212
description: |
1313
Copies an SQLite database file to the device's internal storage (only) and
14-
creates a persistent name that is available for the lifetime of the app.
14+
creates a persistent name that is available for the lifetime of the app.
1515
On Android, if the source file does not exist, an empty database is created.
1616
1717
Returns a reference to the opened database. If the destination file already
1818
exists, behaves as <Titanium.Database.open>.
1919
2020
This method is primarily used for iOS.
21-
21+
2222
With Android, as there is often minimal internal storage available, `install`
2323
may only be appropriate for small databases or for prototyping. When database
2424
files are to be stored on external storage (for example, SD Card), a combination of
@@ -34,8 +34,8 @@ methods:
3434
automatically backed up to iCloud and removed from the device in low
3535
storage situations. See
3636
[How do I prevent files from being backed up to iCloud?](https://developer.apple.com/library/ios/qa/qa1719/_index.html)
37-
for details. To prevent this for database files, use the <Titanium.Database.DB.file>
38-
object with the <Titanium.Filesystem.File.remoteBackup> property.
37+
for details. To prevent this for database files, use the <Titanium.Database.DB.file>
38+
object with the <Titanium.Filesystem.File.remoteBackup> property.
3939
4040
Always [close](Titanium.Database.DB.close) the database after use.
4141
returns:
@@ -45,7 +45,7 @@ methods:
4545
summary: |
4646
Path and filename of the database file to copy to internal storage.
4747
File location is relative to the script's context unless an absolute
48-
path, such as one constructed with a <Titanium.Filesystem>
48+
path, such as one constructed with a <Titanium.Filesystem>
4949
constant, is used.
5050
type: String
5151
- name: dbName
@@ -129,10 +129,17 @@ methods:
129129
parameters:
130130
- name: dbName
131131
summary: |
132-
The dbname previously passed to <Titanium.Database.install>. An absolute path
133-
to the file, including one that is constructed with a <Titanium.Filesystem>
132+
The dbname previously passed to <Titanium.Database.install>. An absolute path
133+
to the file, including one that is constructed with a <Titanium.Filesystem>
134134
constant or <Titanium.Filesystem.directoryForSuite> method, may be used.
135135
type: String
136+
- name: flags
137+
summary: |
138+
Optional flag on Android to pass open flags. If not specified it will use
139+
CREATE_IF_NECESSARY | NO_LOCALIZED_COLLATORS.
140+
type: int
141+
platforms: [android]
142+
since: { android: "13.1.0" }
136143
examples:
137144
- title: Open a Database from Internal Storage (iOS)
138145
example: |
@@ -145,7 +152,7 @@ methods:
145152
146153
A file extension of `sql` is added, and the file is opened from the
147154
following location.
148-
155+
149156
On simulator
150157
151158
* `/Users/<user name>/Library/Application Support/iPhone Simulator/<iOS version>/Applications/<apple app id>/Library/Private Documents/mydb1Installed.sql` (Titanium 1.8.0.1)
@@ -217,3 +224,28 @@ properties:
217224
summary: Constant for requesting a column's value returned in string form.
218225
type: Number
219226
permission: read-only
227+
228+
- name: CREATE_IF_NECESSARY
229+
summary: Constant for database flag.
230+
type: Number
231+
permission: read-only
232+
233+
- name: NO_LOCALIZED_COLLATORS
234+
summary: Constant for database flag.
235+
type: Number
236+
permission: read-only
237+
238+
- name: ENABLE_WRITE_AHEAD_LOGGING
239+
summary: Constant for database flag.
240+
type: Number
241+
permission: read-only
242+
243+
- name: OPEN_READWRITE
244+
summary: Constant for database flag.
245+
type: Number
246+
permission: read-only
247+
248+
- name: OPEN_READONLY
249+
summary: Constant for database flag.
250+
type: Number
251+
permission: read-only

0 commit comments

Comments
 (0)