-
Notifications
You must be signed in to change notification settings - Fork 16
@BindSqlInsert
Allow to insert a bean into database. You can use bean as the unique method parameter or method parameters like bean property, but you can not use mixed case.
- value: bean properties to include into INSERT command. To use only if method have only one parameter and its type is the same of supported bean.
- includePrimaryKey: Allow to include primary key into INSERT command. To use only if method have only one parameter and its type is the same of supported bean.
- excludedFields: properties to exclude into INSERT command. To use only if method have only one parameter and its type is the same of supported bean.
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 {
// case 1 - Method use a bean type parameter
@BindSqlInsert
void insertOne(String name, String surname, String birthCity, Date birthDay);
// case 1 - Method use a bean type parameter
@BindSqlInsert
long insertTwo(String name, String surname, String birthCity, @BindSqlParam("birthDay") Date date);
// case 2 - method use its parameters like bean properties
@BindSqlInsert(excludedFields={"name", "surname"})
void insertThree(Person bean);
}There are two kind of INSERT:
-
Case 1 - Method use a bean type parameter: It's possible define a
INSERTquery with annotation@BindSqlInsert. It is possibile to define query parameter simply using method parameter with same name of the bean property. Each method parameter will be use like input parameter for query. The name of parameters will be used to map field bean and then the column name of the associated table. If you specify a return type for methods (like methodinsertTwo), it has to be of typeint,long,Integer,Long. In this case, the return value will be the id value of just inserted row. @BindSqlParam annotation can be used to specify a different column name associated to bean field. -
Case 2 - method use its parameters like bean properties: The other way to define an
INSERTquery is using a bean as input parameter. You can use attribute value to define which property insert into query or you can use attributeexcludedFieldsto avoid to insert some fields, but you can not use both attributes in the same method definition. Values of this attribute will be used like bean property name. At the end of the insert, bean will have id value set to last row id used to insert bean into table. If you specify a return type for methods, it has to be of typeint,long,Integer,Longand it will contains same value like row id.
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 insert:</p>
* <pre>INSERT INTO person (name, surname, birth_city, birth_day) VALUES (${name}, ${surname}, ${birthCity}, ${birthDay})</pre>
*
* <p><strong>Inserted 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>birthDay</strong></dd>
* </dl>
*
* @param name
* is binded to column <strong>name</strong>
* @param surname
* is binded to column <strong>surname</strong>
* @param birthCity
* is binded to column <strong>birth_city</strong>
* @param birthDay
* is binded to column <strong>birth_day</strong>
*
*
*/
@Override
public void insertOne(String name, String surname, String birthCity, Date birthDay) {
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 (birthDay!=null) {
contentValues.put("birth_day", DateUtils.write(birthDay));
} else {
contentValues.putNull("birth_day");
}
// log
Logger.info(StringUtils.formatSQL("INSERT INTO person (name, surname, birth_city, birth_day) VALUES ('"+StringUtils.checkSize(contentValues.get("name"))+"', '"+StringUtils.checkSize(contentValues.get("surname"))+"', '"+StringUtils.checkSize(contentValues.get("birth_city"))+"', '"+StringUtils.checkSize(contentValues.get("birth_day"))+"')"));
database().insert("person", null, contentValues);
}
/**
* <p>SQL insert:</p>
* <pre>INSERT INTO person (name, surname, birth_city, birth_day) VALUES (${name}, ${surname}, ${birthCity}, ${birthDay})</pre>
*
* <p><strong>Inserted 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>
*
* @param name
* is binded to column <strong>name</strong>
* @param surname
* is binded to column <strong>surname</strong>
* @param birthCity
* is binded to column <strong>birth_city</strong>
* @param date
* is binded to column <strong>birth_day</strong>
*
* @return <strong>id</strong> of inserted record
*/
@Override
public long insertTwo(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");
}
// log
Logger.info(StringUtils.formatSQL("INSERT INTO person (name, surname, birth_city, birth_day) VALUES ('"+StringUtils.checkSize(contentValues.get("name"))+"', '"+StringUtils.checkSize(contentValues.get("surname"))+"', '"+StringUtils.checkSize(contentValues.get("birth_city"))+"', '"+StringUtils.checkSize(contentValues.get("birth_day"))+"')"));
long result = database().insert("person", null, contentValues);
return result;
}
/**
* <p>SQL insert:</p>
* <pre>INSERT INTO person (birth_city, birth_day) VALUES (${bean.birthCity}, ${bean.birthDay})</pre>
*
* <p><code>bean.id</code> is automatically updated because it is the primary key</p>
*
* <p><strong>Inserted columns:</strong></p>
* <dl>
* <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>
*
* @param bean
* is mapped to parameter <strong>bean</strong>
*
*
*/
@Override
public void insertThree(Person bean) {
ContentValues contentValues=contentValues();
contentValues.clear();
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");
}
// log
Logger.info(StringUtils.formatSQL("INSERT INTO person (birth_city, birth_day) VALUES ('"+StringUtils.checkSize(contentValues.get("birth_city"))+"', '"+StringUtils.checkSize(contentValues.get("birth_day"))+"')"));
long result = database().insert("person", null, contentValues);
bean.id=result;
}
}- Introduction
- Goals & Features
- Kotlin
- Immutable or Mutable Pojo
- Annotation Processor Args
- Credits
- Articles
- Benchmarks
- Setup
- Tutorial
- Usage
- Dependencies and inspirations
- Stackoverflow
- Documentation
- SQL logging
- Data source options
- Indices
- SQL Type adapter
- Global SQL Type adapter
- Constraints
- Live data: welcome Architectural components!!
- Paged Live data
- Dynamic parts
- Transactional and batch operations
- Async Transactional and batch operations
- Global transaction
- Support for immutable POJO
- Generate Content provider
- Generate Database schema generation
- Database migration
- BindSqlColumn
- BindContentProvider
- BindContentProviderEntry
- BindContentProviderPath
- BindDao
- BindDaoMany2Many
- BindDataSource
- BindDataSourceOptions
- BindDataSourceUpdateTask
- BindIndex
- BindSqlRelation
- BindSqlAdapter
- BindSqlChildSelect
- BindSqlDelete
- BindSqlDynamicOrderBy
- BindSqlDynamicWhere
- BindSqlDynamicWhereParams
- BindSqlInsert
- BindSqlPageSize
- BindSqlParam
- BindSqlSelect
- BindSqlUpdate
- BindSqlType
- BindSqlTransaction