Skip to content

@BindSqlDelete

xcesco edited this page Jan 24, 2017 · 6 revisions

Allow to delete a bean from a database. You can use bean as input parameter or method parameters like bean property, but you can not use mixed case.

Attributes

  • where: where condition. It is possible to specify a parameter binded to method's parameter with the syntax ${<parameter name>}.

Usage

Suppose we 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 {
 
  // raw delete
  @BindDelete(where="name=${name} and surname=${surname}")
  void deleteOne(String name, @BindSqlParam("surname") temp);
 
  // raw delete
  @BindDelete(where="name=${name} and surname=${surname}")
  long deleteTwo(String name, @BindSqlParam("surname") temp);

  // managed bean delete
  @BindDelete(where="id=${bean.id}")
  void deleteThree(Person bean);
}

There are two kind of delete:

  • Raw delete: method parameters are used parameters of where condition: every method's parameter must be used as query parameter. If you specify a return type for methods (like method deleteTwo), it has to be of type int, long, Integer, Long. In this case, the return value will be the deleted rows count. @BindSqlParam annotation can be used to specify a different column name associated to a specific method's parameter.

  • Managed bean delete: method has only one managed bean as parameter: method accepts only one managed bean class type parameter. If you specify a return type for methods, it has to be of type int, long, Integer, Long and it will contains delete rows count. If method return a boolean value, it will be set to true in case any row was deleted.

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 delete:</p>
   * <pre>DELETE person WHERE name=${name} and surname=${surname}</pre>
   *
   * <p><strong>Where parameters:</strong></p>
   * <dl>
   * 	<dt>${name}</dt><dd>is mapped to method's parameter <strong>name</strong></dd>
   * 	<dt>${surname}</dt><dd>is mapped to method's parameter <strong>temp</strong></dd>
   * </dl>
   *
   * @param name
   * 	is used as where parameter <strong>${name}</strong>
   * @param temp
   * 	is used as where parameter <strong>${surname}</strong>
   */
  @Override
  public void deleteOne(String name, String temp) {
    String[] whereConditions={(name==null?null:name), (temp==null?null:temp)};

    Logger.info(StringUtils.formatSQL("DELETE person WHERE name=%s and surname=%s"), (Object[])whereConditions);
    int result = database().delete("person", "name=? and surname=?", whereConditions);
  }

  /**
   * <p>SQL delete:</p>
   * <pre>DELETE person WHERE name=${name} and surname=${surname}</pre>
   *
   * <p><strong>Where parameters:</strong></p>
   * <dl>
   * 	<dt>${name}</dt><dd>is mapped to method's parameter <strong>name</strong></dd>
   * 	<dt>${surname}</dt><dd>is mapped to method's parameter <strong>temp</strong></dd>
   * </dl>
   *
   * @param name
   * 	is used as where parameter <strong>${name}</strong>
   * @param temp
   * 	is used as where parameter <strong>${surname}</strong>
   *
   * @return number of deleted records
   */
  @Override
  public long deleteTwo(String name, String temp) {
    String[] whereConditions={(name==null?null:name), (temp==null?null:temp)};

    Logger.info(StringUtils.formatSQL("DELETE person WHERE name=%s and surname=%s"), (Object[])whereConditions);
    int result = database().delete("person", "name=? and surname=?", whereConditions);
    return result;
  }

  /**
   * <p>SQL delete:</p>
   * <pre>DELETE person WHERE id = ${bean.id}</pre>
   *
   * <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 deleteThree(Person bean) {
    String[] whereConditions={String.valueOf(bean.id)};

    Logger.info(StringUtils.formatSQL("id = %s"), (Object[])whereConditions);
    int result = database().delete("person", "id = ?", whereConditions);
  }
}

An example of usage:

BindPersonDataSource instance=BindPersonDataSource.instance();
instance.openDatabase();
instance.getPersonDAO().deleteOne("hellp", "aa");
instance.close();

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