org.positronicnet.db

Database

class Database extends ThreadlessDatabase with WorkerThread

Class for database singletons. Manages setup and schema upgrades, and makes the tables database available as Positronic Net ContentRepositorys which can be queried directly, or manipulated via the orm.

Or, if you just want the schema maintenance support, you can call getReadableDatabase and getWritableDatabase, and use the standard platform APIs for everything else.

Most behavior is inherited from ThreadlessDatabase. (In fact, this is just ThreadlessDatabase with WorkerThread mixed in. On Android, it's usually best practice to run database work in the background, so by default, the machinery to facilitate that is there; if you really want a ThreadlessDatabase, you can get one, but you do have to ask.)

Schema definition and management

Note that opening a Database does not trigger any I/O; that action is delayed until the first query (at which point we also perform any schema updates, etc.). It is strongly recommended that this not be done on an Activity main UI thread.

Schema definitions are ordinarily done by supplying a list of table definitions, ALTER TABLE upgrades, etc., as the value of schemaUpdates. The idea is that each of these statements defines a database upgrade. The way this relates to the standard SQLiteOpenHelper machinery is as follows:

The upshot is that you can add statements to the list; the current schema is defined by what you get from evaluating them all, and updates will run the missing ones on the fly.

(This arrangement is effectively a very cut-down version of the migrations system in the Ruby on Rails ActiveRecord ORM, which also features database versions defined implicitly by a stream of updates. However, there's no support here for schema downgrades, nor for transparently handling updates coming from multiple development streams.)

Using the database

There are two ways to use a database, once you've opened it.

The first is simply to call getWritableDatabase and getReadableDatabase, which gets you a straight SQLiteDatabase object, with the full standard API.

The second is that if you have a database named, say, TodoDb, with a table named todo_items then TodoDb("todo_items") returns a ContentQuery which may be used to manipulate all rows in the table directly, or through a RecordManager.

Attributes
abstract
Linear Supertypes
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Hide All
  2. Show all
  1. Database
  2. WorkerThread
  3. ThreadlessDatabase
  4. AppFacility
  5. AnyRef
  6. Any
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Database (filename: String, logTag: String)

Abstract Value Members

  1. def schemaUpdates : List[String]

    List of "one-way migrations" that define the current DB schema.

    List of "one-way migrations" that define the current DB schema... See the Database overview.

    Attributes
    abstract
    Definition Classes
    ThreadlessDatabase

Concrete Value Members

  1. def != (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  2. def != (arg0: Any): Boolean

    Attributes
    final
    Definition Classes
    Any
  3. def ## (): Int

    Attributes
    final
    Definition Classes
    AnyRef → Any
  4. def == (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  5. def == (arg0: Any): Boolean

    Attributes
    final
    Definition Classes
    Any
  6. def apply (table: String): DbQuery

    Returns a ContentQuery referring to all rows in one of the tables of this database, which may be used directly, or used to seed a RecordManager for the ORM.

    Returns a ContentQuery referring to all rows in one of the tables of this database, which may be used directly, or used to seed a RecordManager for the ORM.

    Definition Classes
    ThreadlessDatabase
  7. def asInstanceOf [T0] : T0

    Attributes
    final
    Definition Classes
    Any
  8. def assertOnThread (s: String): Unit

    Throws an assertion error if called from anywhere but this activity's thread.

    Throws an assertion error if called from anywhere but this activity's thread.

    Definition Classes
    WorkerThread
  9. def clone (): AnyRef

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  10. def close : Unit

    Close the facility.

    Close the facility.

    First, decrements the "nested open" count. If it has reached zero, do a "real" close, using the protected realclose method. Otherwise, some other activity is still using the facility, and it stays open.

    When the last activity or service calls close, the facility actually does shut down, using the protected realClose method, which will shut down threads, close files, or whatever.

    Definition Classes
    AppFacility
  11. def eq (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  12. def equals (arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  13. def finalize (): Unit

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  14. def getClass (): java.lang.Class[_]

    Attributes
    final
    Definition Classes
    AnyRef
  15. def getLogTag : String

    Tag for the facility to use in log entries

    Tag for the facility to use in log entries

    Definition Classes
    AppFacility
  16. def getReadableDatabase : SQLiteDatabase

    Get a readable Android SQLiteDatabase object, for direct invocation of core APIs.

    Get a readable Android SQLiteDatabase object, for direct invocation of core APIs.

    Definition Classes
    ThreadlessDatabase
  17. def getWritableDatabase : SQLiteDatabase

    Get a writable Android SQLiteDatabase object, for direct invocation of core APIs.

    Get a writable Android SQLiteDatabase object, for direct invocation of core APIs.

    Definition Classes
    ThreadlessDatabase
  18. def hashCode (): Int

    Definition Classes
    AnyRef → Any
  19. def isInstanceOf [T0] : Boolean

    Attributes
    final
    Definition Classes
    Any
  20. def log (s: String): AnyVal

    Writes a debugging log method, using the facility's logTag

    Writes a debugging log method, using the facility's logTag

    Definition Classes
    AppFacility
  21. def ne (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  22. def notify (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  23. def notifyAll (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  24. def onCreate (db: SQLiteDatabase): Unit

    Invoked when the database is created.

    Invoked when the database is created. Default behavior is to run through the complete list of schemaUpdates.

    Definition Classes
    ThreadlessDatabase
  25. def onUpgrade (db: SQLiteDatabase, oldVersion: Int, newVersion: Int): Unit

    Invoked when the database is created.

    Invoked when the database is created. Default behavior is to run through the sublist of schemaUpdates from the prior version to the end (which defines the current version).

    Definition Classes
    ThreadlessDatabase
  26. def openInContext (ctx: Context): Unit

    Open the facility.

    Open the facility.

    If the facility isn't already open, does setup which depends on the particular facility (which may involve opening files, starting threads, or whatever) --- a "real" open, using the protected realOpen method. The given Context may be used for setup, depending on the nature of the facility.

    If it is already open, just increments a "nested open" count; see close below.

    Definition Classes
    AppFacility
  27. def realClose : Unit

    Attributes
    protected
    Definition Classes
    WorkerThreadAppFacility
  28. def realOpen (ctx: Context): Unit

    Attributes
    protected
    Definition Classes
    WorkerThreadAppFacility
  29. def runOnThread (func: ⇒ Unit): Unit

    Run func on the facility's thread.

    Run func on the facility's thread.

    Definition Classes
    WorkerThread
  30. def synchronized [T0] (arg0: ⇒ T0): T0

    Attributes
    final
    Definition Classes
    AnyRef
  31. def toString (): String

    Definition Classes
    AnyRef → Any
  32. def version : Int

    Schema version.

    Schema version. By default, the length of the schemaUpdates list.

    Definition Classes
    ThreadlessDatabase
  33. def wait (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()
  34. def wait (arg0: Long, arg1: Int): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()
  35. def wait (arg0: Long): Unit

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()

Inherited from WorkerThread

Inherited from ThreadlessDatabase

Inherited from AppFacility

Inherited from AnyRef

Inherited from Any