Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.io.TiFile;
import org.appcelerator.titanium.io.TiFileFactory;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUrl;

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

@Kroll.constant
public static final int CREATE_IF_NECESSARY = SQLiteDatabase.CREATE_IF_NECESSARY;
@Kroll.constant
public static final int NO_LOCALIZED_COLLATORS = SQLiteDatabase.NO_LOCALIZED_COLLATORS;
@Kroll.constant
public static final int ENABLE_WRITE_AHEAD_LOGGING = SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
@Kroll.constant
public static final int OPEN_READWRITE = SQLiteDatabase.OPEN_READWRITE;
@Kroll.constant
public static final int OPEN_READONLY = SQLiteDatabase.OPEN_READONLY;

public DatabaseModule()
{
super();
}

@Kroll.method
public TiDatabaseProxy open(Object file)
public TiDatabaseProxy open(Object file, @Kroll.argument(optional = true) Object flags)
{
// Acquire database name or file object providing full file path from argument.
TiBaseFile dbTiBaseFile = null;
Expand Down Expand Up @@ -97,8 +109,12 @@ public TiDatabaseProxy open(Object file)
throw new IllegalArgumentException(message);
}
Log.d(TAG, "Opening database from filesystem: " + absolutePath);
int flagValues = SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.NO_LOCALIZED_COLLATORS;
if (flags != null) {
flagValues = TiConvert.toInt(flags);
}
SQLiteDatabase db = SQLiteDatabase.openDatabase(
absolutePath, null, SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
absolutePath, null, flagValues);
if (db != null) {
dbp = new TiDatabaseProxy(db);
} else {
Expand Down Expand Up @@ -137,7 +153,7 @@ public TiDatabaseProxy install(KrollInvocation invocation, String url, String na
Context ctx = TiApplication.getInstance();
for (String dbname : ctx.databaseList()) {
if (dbname.equals(name)) {
return open(name);
return open(name, null);
}
}

Expand Down Expand Up @@ -195,7 +211,7 @@ public TiDatabaseProxy install(KrollInvocation invocation, String url, String na
}

// Open a connection to the installed database.
return open(name);
return open(name, null);
}

@Override
Expand Down
46 changes: 38 additions & 8 deletions apidoc/Titanium/Database/Database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ methods:
summary: Installs an SQLite database to device's internal storage.
description: |
Copies an SQLite database file to the device's internal storage (only) and
creates a persistent name that is available for the lifetime of the app.
creates a persistent name that is available for the lifetime of the app.
On Android, if the source file does not exist, an empty database is created.

Returns a reference to the opened database. If the destination file already
exists, behaves as <Titanium.Database.open>.

This method is primarily used for iOS.

With Android, as there is often minimal internal storage available, `install`
may only be appropriate for small databases or for prototyping. When database
files are to be stored on external storage (for example, SD Card), a combination of
Expand All @@ -34,8 +34,8 @@ methods:
automatically backed up to iCloud and removed from the device in low
storage situations. See
[How do I prevent files from being backed up to iCloud?](https://developer.apple.com/library/ios/qa/qa1719/_index.html)
for details. To prevent this for database files, use the <Titanium.Database.DB.file>
object with the <Titanium.Filesystem.File.remoteBackup> property.
for details. To prevent this for database files, use the <Titanium.Database.DB.file>
object with the <Titanium.Filesystem.File.remoteBackup> property.

Always [close](Titanium.Database.DB.close) the database after use.
returns:
Expand All @@ -45,7 +45,7 @@ methods:
summary: |
Path and filename of the database file to copy to internal storage.
File location is relative to the script's context unless an absolute
path, such as one constructed with a <Titanium.Filesystem>
path, such as one constructed with a <Titanium.Filesystem>
constant, is used.
type: String
- name: dbName
Expand Down Expand Up @@ -129,10 +129,15 @@ methods:
parameters:
- name: dbName
summary: |
The dbname previously passed to <Titanium.Database.install>. An absolute path
to the file, including one that is constructed with a <Titanium.Filesystem>
The dbname previously passed to <Titanium.Database.install>. An absolute path
to the file, including one that is constructed with a <Titanium.Filesystem>
constant or <Titanium.Filesystem.directoryForSuite> method, may be used.
type: String
- name: flags
summary: |
Optional flag on Android since 13.1.0 to pass open flags. If not specified it will use
CREATE_IF_NECESSARY | NO_LOCALIZED_COLLATORS.
type: Number
examples:
- title: Open a Database from Internal Storage (iOS)
example: |
Expand All @@ -145,7 +150,7 @@ methods:

A file extension of `sql` is added, and the file is opened from the
following location.

On simulator

* `/Users/<user name>/Library/Application Support/iPhone Simulator/<iOS version>/Applications/<apple app id>/Library/Private Documents/mydb1Installed.sql` (Titanium 1.8.0.1)
Expand Down Expand Up @@ -217,3 +222,28 @@ properties:
summary: Constant for requesting a column's value returned in string form.
type: Number
permission: read-only

- name: CREATE_IF_NECESSARY
summary: Constant for database flag.
type: Number
permission: read-only

- name: NO_LOCALIZED_COLLATORS
summary: Constant for database flag.
type: Number
permission: read-only

- name: ENABLE_WRITE_AHEAD_LOGGING
summary: Constant for database flag.
type: Number
permission: read-only

- name: OPEN_READWRITE
summary: Constant for database flag.
type: Number
permission: read-only

- name: OPEN_READONLY
summary: Constant for database flag.
type: Number
permission: read-only
Loading