Skip to content

SQL Insert

xcesco edited this page Apr 26, 2018 · 1 revision

To bind a DAO interface method to an INSERT SQL statement is necessary to decorate it with @BindSqlInsert. Like other SQL statement type, you can write entire JQL statement or write only specific parts of the JQL statment.

Moreover there are two kind of INSERT:

  • Raw insert: method parameters are directly used like table column: all method parameter's name are used are transformed in column with same name. It is possibile define an alias with @BindSqlParam. If you specify a return type for methods, it has to be of type int, long, Integer, Long. In this case, the return value will be the inserted row's id. @BindSqlParam annotation can be used to specify a different column name associated to a specific method's parameter. Moreover, you can use boolean as return type, just to receive true if insert operation was completed (row id != -1).

  • Managed bean insert: method has only one managed bean as parameter: method accepts only one managed bean type parameter. It is possible to use excludedFields attribute to avoid to insert some fields, or you can use includeFields attribute to include only specific fields. If you specify a return type for methods, it has to be of type int, long, Integer, Long and it will contains inserted rows count, or boolean to receive a true value if insert statement was successfully completed (row id != -1).

Suppose we want to persist bean Person defined as follow:

@BindType
public class Person {
  public long id;
  public String name;
  public String surname;
  public String birthCity;
  public Date birthDay;
}

So define a DAO interface for class Person:

@BindDao(Person.class)
public interface PersonDAO {
 
  // raw insert - Method using a bean type parameter
  @BindSqlInsert
  void insertOne(String name, String surname, String birthCity, Date birthDay);
 
  // raw insert - Method use a bean type parameter
  @BindSqlInsert
  long insertTwo(String name, String surname, String birthCity, @BindSqlParam("birthDay") Date date);

  // managed bean insert - method use its parameters like bean properties
  @BindSqlInsert(excludedFields={"name", "surname"})
  void insertThree(Person bean);

  @BindSqlInsert(jql="INSERT INTO person (name, surname, birth_city, birth_day) VALUES (${bean.name}, ${bean.surname}, ${bean.birthCity},{bean.birthDay}")
  void insertFour(Person bean);
}

For more details about SQL Insert, please consult @BindSqlInsert page.

Table of Contents

Query definition

Features

Relations

Multithread supports

Modularization

Annotations for data convertion

Annotations for SQLite ORM

Annotations for shared preferences

Clone this wiki locally