Skip to content

@BindSqlUpdate

xcesco edited this page Apr 26, 2018 · 6 revisions

This annotation allows updating a bean into database. You can use the bean as the unique method parameter or method parameters like bean property, but you can not use mixed case.

Attributes

  • value: bean properties to include in UPDATE command. To use only if method has only one parameter and its type is the same of supported bean.
  • excludedFields: properties to exclude into UPDATE command. To use only if method has only one parameter and its type is the same of supported bean.
  • where: where condition. It is possible to specify a parameter bound to method's parameter with the syntax ${<parameter name>}.
  • jql: allows specifying the entire query with JQL.

Usage

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;
}

The associated DAO interface is

@BindDao(Person.class)
public interface PersonDAO {
	
  @BindSqlUpdate(where="name=${name}")
  void updateOne(String name, String surname, String birthCity, Date birthDay);

  @BindSqlUpdate
  long updateTwo(String name, String surname, String birthCity, @BindSqlParam("birthDay") Date date);

  @BindSqlUpdate(where="id=${bean.id}")
  void updateThree(Person bean);
}

There are two kinds of UPDATE:

  • Raw update: method parameters are directly used like table column or query parameter: method's parameter that is not used as the query parameter is used to update column name with the same name or alias (with @BindSqlParam). If you specify a return type for methods (like method updateTwo), it has to be of type int, long, Integer, Long. In this case, the return value will be the updated rows count. @BindSqlParam annotation can be used to specify a different column name associated to a specific method's parameter.

  • Managed bean update: method has only one managed bean as parameter: method accepts only one managed bean class type parameter. It is possible to use excludedFields attribute to avoid to update 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 contain updated rows count.

For interface PersonDAO, Kripton annotation processor will generate the following Person DAO implementation:

/**
 * <p>
 * DAO implementation for entity <code>Person</code>, based on interface <code>PersonDAO</code>
 * </p>
 *
 *  @see Person
 *  @see PersonDAO
 *  @see PersonTable
 */
public class PersonDAOImpl extends AbstractDao implements PersonDAO {
  public PersonDAOImpl(BindPersonDataSource dataSet) {
    super(dataSet);
  }

  /**
   * <p>SQL update:</p>
   * <pre>UPDATE person SET surname=${surname}, birthCity=${birthCity}, birthDay=${birthDay} WHERE name=${name}</pre>
   *
   * <p><strong>Updated columns:</strong></p>
   * <dl>
   * 	<dt>surname</dt><dd>is binded to query's parameter <strong>${surname}</strong> and method's parameter <strong>surname</strong></dd>
   * 	<dt>birth_city</dt><dd>is binded to query's parameter <strong>${birthCity}</strong> and method's parameter <strong>birthCity</strong></dd>
   * 	<dt>birth_day</dt><dd>is binded to query's parameter <strong>${birthDay}</strong> and method's parameter <strong>birthDay</strong></dd>
   * </dl>
   *
   * <p><strong>Where parameters:</strong></p>
   * <dl>
   * 	<dt>${name}</dt><dd>is mapped to method's parameter <strong>name</strong></dd>
   * </dl>
   *
   * @param name
   * 	is used as where parameter <strong>${name}</strong>
   * @param surname
   * 	is used as updated field <strong>surname</strong>
   * @param birthCity
   * 	is used as updated field <strong>birthCity</strong>
   * @param birthDay
   * 	is used as updated field <strong>birthDay</strong>
   */
  @Override
  public void updateOne(String name, String surname, String birthCity, Date birthDay) {
    ContentValues contentValues=contentValues();
    contentValues.clear();
    if (surname!=null) {
      contentValues.put("surname", surname);
    } else {
      contentValues.putNull("surname");
    }
    if (birthCity!=null) {
      contentValues.put("birth_city", birthCity);
    } else {
      contentValues.putNull("birth_city");
    }
    if (birthDay!=null) {
      contentValues.put("birth_day", DateUtils.write(birthDay));
    } else {
      contentValues.putNull("birth_day");
    }

    String[] whereConditions={(name==null?null:name)};

    Logger.info(StringUtils.formatSQL("UPDATE person SET surname='"+StringUtils.checkSize(contentValues.get("surname"))+"', birthCity='"+StringUtils.checkSize(contentValues.get("birth_city"))+"', birthDay='"+StringUtils.checkSize(contentValues.get("birth_day"))+"' WHERE name=%s"), (Object[])whereConditions);
    int result = database().update("person", contentValues, "name=?", whereConditions);
  }

  /**
   * <p>SQL update:</p>
   * <pre>UPDATE person SET name=${name}, surname=${surname}, birthCity=${birthCity}, birthDay=${birthDay} WHERE 1=1</pre>
   *
   * <p><strong>Updated columns:</strong></p>
   * <dl>
   * 	<dt>name</dt><dd>is binded to query's parameter <strong>${name}</strong> and method's parameter <strong>name</strong></dd>
   * 	<dt>surname</dt><dd>is binded to query's parameter <strong>${surname}</strong> and method's parameter <strong>surname</strong></dd>
   * 	<dt>birth_city</dt><dd>is binded to query's parameter <strong>${birthCity}</strong> and method's parameter <strong>birthCity</strong></dd>
   * 	<dt>birth_day</dt><dd>is binded to query's parameter <strong>${birthDay}</strong> and method's parameter <strong>date</strong></dd>
   * </dl>
   *
   * <p><strong>Where parameters:</strong></p>
   * <dl>
   * </dl>
   *
   * @param name
   * 	is used as updated field <strong>name</strong>
   * @param surname
   * 	is used as updated field <strong>surname</strong>
   * @param birthCity
   * 	is used as updated field <strong>birthCity</strong>
   * @param date
   * 	is used as updated field <strong>birthDay</strong>
   *
   * @return number of updated records
   */
  @Override
  public long updateTwo(String name, String surname, String birthCity, Date date) {
    ContentValues contentValues=contentValues();
    contentValues.clear();
    if (name!=null) {
      contentValues.put("name", name);
    } else {
      contentValues.putNull("name");
    }
    if (surname!=null) {
      contentValues.put("surname", surname);
    } else {
      contentValues.putNull("surname");
    }
    if (birthCity!=null) {
      contentValues.put("birth_city", birthCity);
    } else {
      contentValues.putNull("birth_city");
    }
    if (date!=null) {
      contentValues.put("birth_day", DateUtils.write(date));
    } else {
      contentValues.putNull("birth_day");
    }

    String[] whereConditions={};

    Logger.info(StringUtils.formatSQL("UPDATE person SET name='"+StringUtils.checkSize(contentValues.get("name"))+"', surname='"+StringUtils.checkSize(contentValues.get("surname"))+"', birthCity='"+StringUtils.checkSize(contentValues.get("birth_city"))+"', birthDay='"+StringUtils.checkSize(contentValues.get("birth_day"))+"' WHERE 1=1"), (Object[])whereConditions);
    int result = database().update("person", contentValues, "1=1", whereConditions);
    return result;
  }

  /**
   * <p>SQL Update:</p>
   * <pre>UPDATE person SET name=${bean.name}, surname=${bean.surname}, birth_city=${bean.birthCity}, birth_day=${bean.birthDay} WHERE id=${bean.id}</pre>
   *
   * <p><strong>Updated columns:</strong></p>
   * <dl>
   * 	<dt>name</dt><dd>is mapped to <strong>${bean.name}</strong></dd>
   * 	<dt>surname</dt><dd>is mapped to <strong>${bean.surname}</strong></dd>
   * 	<dt>birth_city</dt><dd>is mapped to <strong>${bean.birthCity}</strong></dd>
   * 	<dt>birth_day</dt><dd>is mapped to <strong>${bean.birthDay}</strong></dd>
   * </dl>
   *
   * <p><strong>Parameters used in where conditions:</strong></p>
   * <dl>
   * 	<dt>${bean.id}</dt><dd>is mapped to method's parameter <strong>bean.id</strong></dd>
   * </dl>
   *
   * @param bean
   * 	is used as ${bean}
   */
  @Override
  public void updateThree(Person bean) {
    ContentValues contentValues=contentValues();
    contentValues.clear();

    if (bean.name!=null) {
      contentValues.put("name", bean.name);
    } else {
      contentValues.putNull("name");
    }

    if (bean.surname!=null) {
      contentValues.put("surname", bean.surname);
    } else {
      contentValues.putNull("surname");
    }

    if (bean.birthCity!=null) {
      contentValues.put("birth_city", bean.birthCity);
    } else {
      contentValues.putNull("birth_city");
    }

    if (bean.birthDay!=null) {
      contentValues.put("birth_day", DateUtils.write(bean.birthDay));
    } else {
      contentValues.putNull("birth_day");
    }

    String[] whereConditions={String.valueOf(bean.id)};

    Logger.info(StringUtils.formatSQL("UPDATE person SET name='"+StringUtils.checkSize(contentValues.get("name"))+"', surname='"+StringUtils.checkSize(contentValues.get("surname"))+"', birth_city='"+StringUtils.checkSize(contentValues.get("birth_city"))+"', birth_day='"+StringUtils.checkSize(contentValues.get("birth_day"))+"' WHERE id='%s'"), (Object[])whereConditions);
    int result = database().update("person", contentValues, "id=?", whereConditions);
  }
}

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