-
Notifications
You must be signed in to change notification settings - Fork 4
Propoid db
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.
To insert a new object just pass it to Repository#insert():
repository.insert(new Foo());
An object can similarily be updated:
repository.update(foo);
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
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);
Each repository is highly configurable through a set of settings, e.g.:
Repository repository = new Repository(context, "foos", new FoosMapping(), new FoosFactory());
Mapping provides propoid.db.mapping.Mappers which are used to map properties to database columns. propoid.db.mapping.DefaultMapping handles most common types.
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.
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.
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.
On opening the database this strategy allows to perform required upgrades.