Skip to content
svenmeier edited this page Dec 20, 2011 · 35 revisions

Working with Sqlite is very easy with propoid-db, create a repository and start right away without any setup:

Repository repository = new Repositiory(context, "foos");

All required tables will automagically be created once you perform operations on the repository.

Inserting and updating

To insert a new object just pass it to Repository#insert():

repository.insert(new Foo());

An object can similarily be updated:

repository.update(foo);

Queries

Objects can be queried with a fully typed API:

Match<Foo> match = repository.query(Where.equal(new Foo().bar, "BAR"));

Note that a database operation is performed only after you invoke a method on the query match, e.g.:

List<Foo> foos = match.list(Order.ascending(new Foo().bar);

The #list() method is special in that all its elements are backed by a life database cursor. To close its resources you have to call Closable#close() on it or even simpler just consume all its elements, e.g.:

for (Foo foo : foos) {
}
// all foos are consumed now

Indices

To increase performance of your application you might want to add indices to your repository:

Foo foo = new Foo();
repository.index(foo, false, foo.bar);

Locator

A Locator specifies how the SQLiteDatabase is openened:

  • InMemoryLocator opens a database in memory
  • FileLocator (the default) opens a database file, either speficied explicitely or relative to the application context

FileLocator allows initializing the database content on creation:

new FileLocator(context, "repository") {
    protected InputStream initial() {
        return context.getAssets().open("default-repository");
    }
};

Settings

Each repository is highly configurable through a set of settings, e.g.:

Repository repository = new Repository(context, "foos", new FoosMapping(), new FoosFactory());

Mapping

Mapping provides propoid.db.mapping.Mappers which are used to map properties to database columns. propoid.db.mapping.DefaultMapping handles most common types:

  • primitives and their wrapper classes
  • Strings
  • byte arrays
  • java.util.Locale
  • java.util.Date
  • android.location.Location
  • enums
  • types (i.e. class instances)
  • relations to propoids

Naming

Naming decides which table to be used for each Propoid and how to encode subclasses. propoid.db.naming.DefaultNaming uses a single-table-mapping for inheritance.

Factory

All Propoids read from the database are created by a factory. propoid.db.factory.DefaultFactory creates a new instance on each requested Propoid. propoid.db.factory.IdentityFactory caches Propoids by their type and identifier, thus offering object identity if required.

Cascading

Before Propoids are inserted, updated or deleted, this strategy is allowed to cascade operations to referenced Propoids. propoid.db.cascading.DefaultCascading allows registering of properties, which should be cascaded.

Versioning

On opening the database this strategy allows to perform required upgrades.

DefaultVersioning keeps a list of propoid.db.version.Upgrades, each being able to upgrade a database to the subsequent version:

DefaultVersioning versioning = new DefaultVersioning();
versioning.add(new SQLExec(new SQL("alter table foo drop column bar")));
versioning.add(new SQLExec(new SQL("drop table foo")));

DefaultVersioning keeps track of the current database's version so that upgrades are applied automagically.

Clone this wiki locally