-
Notifications
You must be signed in to change notification settings - Fork 16
@BindSqlInsert
Allow inserting a bean into the database. You can use bean as the unique method parameter or method parameters like bean property, but you can not use mixed case.
- conflictAlgorithm: specifies the conflict algorithm to apply during the insert operation. See here for more information.
-
excludedFields: properties to exclude into
INSERT
command. To use only if the method has only one parameter and its type is the same of supported bean. - fields: allows to specify which fields method need to take from bean used as input parameter.
-
value: bean properties to include into
INSERT
command. To use only if method has 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 the method has only one parameter and its type is the same of supported bean. - jql: allows specifying the entire query with JQL.
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;
}
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 useboolean
as return type, just to receivetrue
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 useincludeFields
attribute to include only specific fields. If you specify a return type for methods, it has to be of typeint
,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).
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);
}
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