Skip to content
Siim Kinks edited this page Mar 21, 2017 · 5 revisions

For saving SqliteMagic generates the following builder methods on @Table annotated classes:

  • insert() Builder for inserting object into database using INSERT SQLite statement. Execution methods produce saved row primary key.

    • conflictAlgorithm(@ConflictAlgorithm int) Builder method for defining a conflict algorithm for operation.
    • usingConnection(DbConnection) Define which database connection to use for executing this operation. Whithout defining it SqliteMagic uses the default connection.
  • insert(Iterable<T>) Static builder for inserting multiple objects into database inside a transaction using INSERT SQLite statement. Execution methods produce boolean result indicating the success of the operation.

    • usingConnection(DbConnection) Define which database connection to use for executing this operation. Whithout defining it SqliteMagic uses the default connection.
  • persist() Builder for persisting object into database using either INSERT SQLite statement, if there is no row with the object's primary key or UPDATE SQLite statement, if object can be updated. Execution methods produce persisted row primary key.

    • ignoreNullValues() Configure this operation to ignore null column values when persisting provided object.
    • usingConnection(DbConnection) Define which database connection to use for executing this operation. Whithout defining it SqliteMagic uses the default connection.
  • persist(Iterable<T>) Static builder for persisting multiple objects into database inside a transaction using either INSERT SQLite statement, if there is no row with persistable object's primary key or UPDATE SQLite statement, if object can be updated. Execution methods produce boolean result indicating the success of the operation.

    • Builder options same as persist().

Important! Generated methods are starting points to building database operations. Each builder contains several operation options and must end with an "executive" method - either execute() for synchronous execution or observe() for starting point into reactive world.

Example:

Synchronous RxJava
Author author = new Author(
        73, "Foo", "Bar");
Book book = new Book(
        77, "Bar", author);

// insert -- NOTE: author
// object also gets
// inserted and the whole
// operation is wrapped
// in transaction
long id = book
    .insert()
    .execute();

// update or insert
id = author
    .persist()
    .execute();
    
// update or insert, but
// ignore null values
id = author
    .persist()
    .ignoreNullValues()
    .execute();
    
// Bulk operations
boolean success = Author
    .persist(someAuthors)
    .ignoreNullValues()
    .execute();
Author author = new Author(
        73, "Foo", "Bar");
Book book = new Book(
        77, "Bar", author);

// insert -- NOTE: author
// object also gets
// inserted and the whole
// operation is wrapped
// in transaction when result
// object gets subscribed
Single<Long> insert = book
    .insert()
    .observe();

// update or insert
Single<Long> persist = author
    .persist()
    .observe();
    
// update or insert, but
// ignore null values
persist = author
    .persist()
    .ignoreNullValues()
    .observe();
    
// Bulk operations
Completable bulkPersist = Author
    .persist(someAuthors)
    .ignoreNullValues()
    .observe();

See next

Clone this wiki locally