Sharin is a Java library including these functions.
Sample code:
EntityInfo entityInfo = new EntityInfo(EmployeeEntity.class);
SqlGenerator sqlGenerator = new BasicSqlGenerator(entityInfo);
EmployeeEntity sample = new EmployeeEntity();
sample.departmentId = 20;
sample.job = "CLERK";
Sql sql = sqlGenerator.selectBySample("*, -name", "id ASC", sample);
Input (EmployeeEntity):
@Table(name = "emp")
public class EmployeeEntity {
@Id
@Column(name = "empno")
public Integer id;
@Column(name = "ename")
public String name;
public String job;
@Column(name = "deptno")
public Integer departmentId;
@ManyToOne
@JoinColumn(name = "deptno")
public transient DepartmentEntity department;
}
Input (DepartmentEntity):
@Table(name = "dept")
public class DepartmentEntity {
@Id
@Column(name = "deptno")
public Integer id;
@Column(name = "dname")
public String name;
@Column(name = "loc")
public String location;
}
Output (sql.text):
SELECT emp.deptno AS "departmentId", emp.empno AS "id", emp.job AS "job", department.deptno AS "department.id", department.loc AS "department.location", department.dname AS "department.name" FROM emp LEFT JOIN dept department ON emp.deptno = department.deptno WHERE emp.deptno = ? AND emp.job = ? ORDER BY "id" ASC
Output (sql.params):
[20, CLERK]
Sample code:
BasicSqlFormatter sqlFormatter = new BasicSqlFormatter(template);
Map<String, Object> context = new HashMap<String, Object>();
context.put("deptno", new Integer[] { 20, 30 });
context.put("job", new String[] { "MANAGER", "CLERK" });
context.put("order", "empno");
String result = sqlFormatter.format(context);
Input (template):
SELECT *
FROM emp
-- #begin
WHERE
-- #begin
ename LIKE
'%A%' -- $ename
-- #end
-- #begin
AND -- #prepend
deptno IN (
30 -- $deptno
)
-- #end
-- #begin
AND -- #prepend
job IN (
'SALESMAN' -- $job
)
-- #end
-- #end
ORDER BY
ename -- &order
ASC
Output (result):
SELECT * FROM emp WHERE deptno IN ( 20, 30 ) AND job IN ( 'MANAGER', 'CLERK' ) ORDER BY empno ASC
Sample code:
ResultSetProcessor objectProcessor = new BeanResultSetProcessor(
EmployeeEntity.class);
SqlRunner sqlRunner = new BasicSqlRunner(dataSource, objectProcessor);
Sql sql = new Sql(sqlText, "ANALYST", "S%");
EmployeeEntity employee = sqlRunner.selectForObject(sql);
Input (sqlText):
SELECT job AS "job", ename AS "name", dname AS "department.name" FROM emp INNER JOIN dept ON emp.deptno = dept.deptno WHERE job = ? AND ename LIKE ?
Output (employee):
id=<null> name=SCOTT job=ANALYST departmentId=<null>
Output (employee.department):
id=<null> name=RESEARCH location=<null>
Sample code:
import static sharin.doc.builder.xhtml1.Xhtml1DocBuilder.Static.*;
...
Elem elem = form().methodGet().action("search")._(
input().typeText().name("q"), input().typeSubmit());
String result = elem.toString();
Output (result):
<form method="get" action="search"> <input type="text" name="q" /> <input type="submit" /> </form>
Sample code (CsvPullParser):
CsvPullParser csvParser = new BasicCsvPullParser();
List<String[]> recordList = new ArrayList<String[]>();
for (String[] record : csvParser.parse(reader)) {
recordList.add(record);
}
String[][] records = recordList
.toArray(new String[recordList.size()][]);
Sample code (CsvPushParser):
CsvPushParser csvParser = new BasicCsvPushParser(); BasicCsvHandler csvHandler = new BasicCsvHandler(); csvParser.parse(reader, csvHandler); String[][] records = csvHandler.getRecords();
Input (reader):
1,First 2,"S,econd" 3,"T""hird" 4,"F ourth"
Output (records):
[1, First] [2, S,econd] [3, T"hird] [4, F ourth]