Skip to content

Commit

Permalink
Room first in getting started, generally discourage direct database a…
Browse files Browse the repository at this point in the history
…ccess.

Signed-off-by: Kenneth J. Shackleton <[email protected]>
  • Loading branch information
kennethshackleton committed Feb 12, 2024
1 parent fd56d26 commit cb67ba9
Showing 1 changed file with 34 additions and 64 deletions.
98 changes: 34 additions & 64 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@

## Getting a database

### Using Room

=== "Kotlin"
``` kotlin
private fun deriveKey(): ByteArray? = TODO(
"Optional key, must be exactly 32-bytes long.")

private val factory = createSupportSQLiteOpenHelperFactory(
SQLiteJournalMode.WAL,
deriveKey()
)

val database = Room.databaseBuilder(context, MyAppDatabase::class.java, "app")
.openHelperFactory(factory)
.build()
```

=== "Java"
``` java
private byte[] deriveKey() {
// TODO Optional key, must be exactly 32-bytes long.
}

private SupportSQLiteOpenHelper.Factory factory =
SupportSQLiteOpenHelperKt.createSupportSQLiteOpenHelperFactory(
SQLiteJournalMode.WAL,
deriveKey());

final RoomDatabase database = Room.databaseBuilder(
context, MyAppDatabase.class, "app"
).openHelperFactory(factory)
.build();
```

### Using an open helper

=== "Kotlin"
Expand Down Expand Up @@ -95,70 +129,6 @@
);
```

### Using Room

=== "Kotlin"
``` kotlin
private fun deriveKey(): ByteArray? = TODO(
"Optional key, must be exactly 32-bytes long.")

private val factory = createSupportSQLiteOpenHelperFactory(
SQLiteJournalMode.WAL,
deriveKey()
)

val database = Room.databaseBuilder(context, MyAppDatabase::class.java, "app")
.openHelperFactory(factory)
.build()
```

=== "Java"
``` java
private byte[] deriveKey() {
// TODO Optional key, must be exactly 32-bytes long.
}

private SupportSQLiteOpenHelper.Factory factory =
SupportSQLiteOpenHelperKt.createSupportSQLiteOpenHelperFactory(
SQLiteJournalMode.WAL,
deriveKey());

final RoomDatabase database = Room.databaseBuilder(
context, MyAppDatabase.class, "app"
).openHelperFactory(factory)
.build();
```

### Directly

=== "Kotlin"
``` kotlin
private fun deriveKey(): ByteArray? = TODO(
"Optional key, must be exactly 32-bytes long.")

SQLiteDatabase.openOrCreateDatabase(
context.getDatabasePath("sample"),
SQLiteJournalMode.WAL.databaseConfiguration,
deriveKey()
).apply {
exec("PRAGMA journal_mode=${SQLiteJournalMode.WAL}")
}
```

=== "Java"
``` java
private byte[] deriveKey() {
// TODO Optional key, must be exactly 32-bytes long.
}

final SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
targetContext.getDatabasePath("sample"),
SQLiteJournalMode.WAL.databaseConfiguration,
deriveKey()
);
database.exec("PRAGMA journal_mode=WAL");
```

## Interaction

### Querying the database
Expand Down

0 comments on commit cb67ba9

Please sign in to comment.