Skip to content

Deep vs Shallow

Siim Kinks edited this page Nov 21, 2016 · 1 revision

SqliteMagic follows table columns nullability contracts very strictly and only allows querying objects that follow these contracts.

Shallow

By default, SqliteMagic builds queries that select and parse the most shallow representation of selected table (defined in the from clause). For tables without complex columns shallow means all selected table columns. For tables with complex columns shallow means all table columns plus minimal possible representation of its complex columns, which means that all referenced object's non-null columns are selected (this is applied recursively); if all columns are nullable then non of the complex columns get parsed; if complex column is not-nullable, but its columns are nullable then complex column's object gets created with only its id column filled.

All columns are not nullable

// With a structure like the following
// non of the parent table and its complex
// column's columns are nullable

@Table(persistAll = true)
@AutoValue
public abstract class Author {
  @Id(autoIncrement = false)
  public abstract long id();
  public abstract String firstName();
  public abstract String lastName();
}

@Table(persistAll = true)
@AutoValue
public abstract class Book {
  @Id(autoIncrement = false)
  public abstract long id();
  public abstract String title();
  public abstract Author author();
}

// So if we create a query like the following,
// all columns in both Book and Author
// objects will be filled

List<Book> allBooks = Select
        .from(BOOK)
        .execute();

All columns are nullable

// With a structure like the following
// all of the parent table and its
// complex column's columns are nullable

@Table(persistAll = true)
@AutoValue
public abstract class Author {
  @Nullable
  @Id(autoIncrement = false)
  public abstract long id();
  @Nullable
  public abstract String firstName();
  @Nullable
  public abstract String lastName();
}

@Table(persistAll = true)
@AutoValue
public abstract class Book {
  @Nullable
  @Id(autoIncrement = false)
  public abstract long id();
  @Nullable
  public abstract String title();
  @Nullable
  public abstract Author author();
}

// So if we create a query like the following,
// only Book object id and title
// will be filled. Author column
// will be null

List<Book> allBooks = Select
        .from(BOOK)
        .execute();

All columns except complex column are nullable

// With a structure like the following
// all of the Book and Author table
// columns are nullable
// with the exception of Book
// table's Author column

@Table(persistAll = true)
@AutoValue
public abstract class Author {
  @Nullable
  @Id(autoIncrement = false)
  public abstract long id();
  @Nullable
  public abstract String firstName();
  @Nullable
  public abstract String lastName();
}

@Table(persistAll = true)
@AutoValue
public abstract class Book {
  @Nullable
  @Id(autoIncrement = false)
  public abstract long id();
  @Nullable
  public abstract String title();
  public abstract Author author();
}

// So if we create a query like the following,
// Book object has filled id, title and
// Author column. Author object
// has only id filled.

List<Book> allBooks = Select
        .from(BOOK)
        .execute();

Deep

If the whole object tree structure is needed, queryDeep() method must be called on the query builder. In that case all the complex columns and their columns and complex columns, etc are queried and parsed regardless of their nullability.

Example:

// With a structure like the following
// all of the parent table and its
// complex column's
// columns are nullable

@Table(persistAll = true)
@AutoValue
public abstract class Author {
  @Nullable
  @Id(autoIncrement = false)
  public abstract long id();
  @Nullable
  public abstract String firstName();
  @Nullable
  public abstract String lastName();
}

@Table(persistAll = true)
@AutoValue
public abstract class Book {
  @Nullable
  @Id(autoIncrement = false)
  public abstract long id();
  @Nullable
  public abstract String title();
  @Nullable
  public abstract Author author();
}

// If we create a query like the following,
// all of the Book and Author
// object columns are filled

List<Book> allBooks = Select
        .from(BOOK)
        .queryDeep()
        .execute();

See Next

Clone this wiki locally