-
Notifications
You must be signed in to change notification settings - Fork 10
Defining Database Schema
SqliteMagic supports two types of objects to define database schema with - POJOs (or Plain Old Java Objects) and AutoValue immutable objects. Generally, there is no difference for SqliteMagic which one (or both) to use, except some minor differences which are described below.
Minimal requirement to define a database table is to annotate an object with @Table(persistAll=true). No other extra configuration needed - the annotation processor can find its way from there on.
Also, when using AutoValue there has to be one long or Long type returning method with @Id annotation which represents the primary key.
| POJO | SQL |
|---|---|
@Table(persistAll = true)
public class Author {
String firstName;
String lastName;
...
} |
CREATE TABLE IF NOT EXISTS author (
_id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name TEXT,
last_name TEXT
); |
| AutoValue | SQL |
|---|---|
@Table(persistAll = true)
@AutoValue
public abstract class Author {
@Id
public abstract long id();
public abstract String firstName();
public abstract String lastName();
...
} |
CREATE TABLE IF NOT EXISTS author (
id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name TEXT,
last_name TEXT
);
|
Note that there is no need to extend or implement any base classes or interfaces.
By default only fields/methods with @Column annotation are added to the schema. In the example above there is an annotation @Table(persistAll = true) which configures annotation processor to add all suitable fields/methods to the schema.
There are following annotations for defining database table schema:
-
@TableDefines a table in a database.-
value: StringTable name. The default is lowercased object name where camel case is replaced with "_" character. -
persistAll: booleanPersist all suitable columns using the default behavior of@Column. If any column has@Columnannotation then it uses its preferences. If any column is annotated with@IgnoreColumnthen this field is ignored. The default is false. -
useAccessMethods: booleanRespect access methods when accessing fields. This parameter has effect only on POJOs. Access methods can be with names representing field names e.g.name(),name(String)or with JavaBeans-style prefixes likegetName(),setName(String),isMale(). The default is false.
-
-
@ColumnDefines a column in a table. This annotation is valid on methods for AutoValue objects and on fields for POJO objects.-
value: StringColumn name. The default is field or method name where camel case is replaced with "_" character. -
handleRecursively: booleanDefine if complex column should be handled recursively, meaning that its values are persisted/retrieved in full extent. If set to false, only its@Idannotated column is persisted/retrieved. Note that complex column must also be defined with@Tableannotation for system to know how to persist/retrieve them. The default value is true. -
onDeleteCascade: booleanDefine for complex column ifON DELETE CASCADEforeign key action should be added to table definition. The default value is false. -
useAccessMethods: booleanRespect access methods when accessing this column. This parameter has effect only on POJOs. Access methods can be with names representing field names e.g.name(),name(String)or with JavaBeans-style prefixes likegetName(),setName(String),isMale(). The default is false.
-
-
@IdDefine a column to be the primary key for table. Annotated field/method must have eitherlongorLongJava type. Each table must have one and only one column with@Idannotation. The only exception is POJO objects where it can be omitted. In that case system generates "magic"longtype private field named "id" (see also Database Operations). There is no need to define@Columnannotation if column already has@Idannotation.-
autoIncrement: booleanDefine if id column should be auto-incremented. The default value is true.
-
-
@IgnoreColumnAll fields with this annotation are ignored when@Table#persistAll()is true. -
@UniqueDefine a SQLUNIQUEconstraint on column.
SqliteMagic supports @NonNull/@Nullable annotations on columns which adds/removes null checks when parsing data.
These annotations are also used to determine how shallow object can be when querying complex data structures. See more at Deep vs Shallow.
All primitive Java datatypes and their boxed counterparts plus byte arrays and String objects are supported.
All Java datatypes are translated into SQLite supported datatypes:
-
NULL- all null objects -
INTEGER- long, Long, int, Integer, boolean and Boolean -
REAL- double, Double, float and Float -
TEXT- String -
BLOB- byte, Byte, byte[] and Byte[]
SqliteMagic supports the following objects as columns:
- Third party objects using object transformers
- User defined objects (also called "complex columns" throughout the library)