Skip to content

Database Operations

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

In order to provide the most convenient library usage experience and API, SqliteMagic bends the boundaries of Java programming language a bit. By leveraging the power of annotation processing, AST (or Abstract Syntax Tree) transformations (by using the famous Lombok library) and bytecode manipulation, SqliteMagic generates extension methods on classes annotated with @Table annotation. These methods are starting points for doing database operations with those objects.

For bytecode manipulation sceptics: Generated methods and bytecode manipulation is used only as a "glue" between callable code and annotation processor generated methods in order to eliminate the need to use any reflection. These methods do not contain any extra logic - nothing sneaky or tricky going on underneath. To see human readable code during debugging just press "step into" when "magic method" is encountered. Thanks to using bytecode manipulation one can use Proguard and shrinker worry free - generated code will not be stripped out if anybody is calling it.

Thanks to above mentioned technique there is no need to extend any base classes or implement interfaces on database objects which makes using SqliteMagic very flexible and easy.

POJO Decompiled bytecode
@Table(persistAll = true)
public class Author {
  String firstName;
  
  String lastName;
}
























@Table(persistAll = true)
public class Author {
  @Id
  @Column("_id")
  long id;
  
  String firstName;
  
  String lastName;
  
  public Author() {}
  
  @Invokes(
          value = "com.sample.app.SqliteMagic_Author_Handler.InsertBuilder#create",
          useThisAsOnlyParam = true)
  public final SqliteMagic_Author_Handler.InsertBuilder insert() {
    return SqliteMagic_Author_Handler.InsertBuilder.create(this);
  }

  /** Some generated methods omitted */

  @Invokes("com.sample.app.SqliteMagic_Author_Handler.BulkInsertBuilder#create")
  public static SqliteMagic_Author_Handler.BulkInsertBuilder insert(Iterable<Author> var0) {
    return SqliteMagic_Author_Handler.BulkInsertBuilder.create(var0);
  }

  /** Some generated methods omitted */
  
  public final long id() {
      return this.id;
  }
}

SqliteMagic adds the following extension methods on classes with @Table annotation:

  • insert()
  • insert(Iterable<T>)
  • persist()
  • persist(Iterable<T>)
  • update()
  • update(Iterable<T>)
  • delete()
  • delete(Collection<T>)
  • deleteTable()

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.

Also for convenience, if POJO class has no @Id annotated field, it is generated with a getter method (see the example above). Generated field is of type long, has package private visibility and is annotated with the @Id annotation. The getter mehtod is named id(), has public visibility and returns long.

See next