Sharin is a Java library including these functions.
Usage:
EntityInfo entityInfo = new EntityInfo(Employee.class); SqlGenerator sqlGenerator = new BasicSqlGenerator(entityInfo); Employee example = new Employee(); example.departmentId = 20; example.job = "ANALYST"; Sql sql = sqlGenerator.selectByExample("*, -department.location", "id ASC", example); String sqlText = sql.getText(); Object[] sqlParams = sql.getParams();
Input (Employee):
@Table(name = "emp") public class Employee { @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 Department department; }
Input (Department):
@Table(name = "dept") public class Department { @Id @Column(name = "deptno") public Integer id; @Column(name = "dname") public String name; @Column(name = "loc") public String location; }
Output (sqlText):
SELECT emp.deptno AS "departmentId", emp.empno AS "id", emp.job AS "job", emp.ename AS "name", department.deptno AS "department.id", 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 (sqlParams):
[20, ANALYST]
Usage:
BasicSqlFormatter sqlFormatter = new BasicSqlFormatter(template); Map<String, Object> context = new HashMap<String, Object>(); context.put("ename", null); context.put("deptno", 20); context.put("jobs", new String[] { "ANALYST", "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 = 30 -- $deptno -- #end -- #begin AND -- #prepend job IN ( 'SALESMAN' -- $jobs ) -- #end -- #end ORDER BY ename -- &order ASC
Output (result):
SELECT * FROM emp WHERE deptno = 20 AND job IN ( 'ANALYST', 'CLERK' ) ORDER BY empno ASC
Usage:
ResultSetProcessor objectProcessor = new BeanResultSetProcessor( Employee.class); SqlRunner sqlRunner = new BasicSqlRunner(dataSource, objectProcessor); Sql sql = new Sql(sqlText, sqlParams); Employee employee = sqlRunner.selectForObject(sql);
Input (sqlText):
SELECT emp.empno AS "id", emp.ename AS "name", department.dname AS "department.name" FROM emp INNER JOIN dept department ON emp.deptno = department.deptno WHERE emp.deptno = ? AND emp.job = ? ORDER BY "id" ASC
Input (sqlParams):
[20, ANALYST]
Output (employee):
id=7788 name=SCOTT job=<null> departmentId=<null>
Output (employee.department):
id=<null> name=RESEARCH location=<null>
Usage:
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>
Usage (CsvPullParser):
CsvPullParser csvPullParser = new BasicCsvPullParser(); for (String[] record : csvPullParser.parse(reader)) { /* do something */ }
Usage (CsvPushParser):
CsvPushParser csvPushParser = new BasicCsvPushParser(); BasicCsvHandler csvHandler = new BasicCsvHandler(); csvPushParser.parse(reader, csvHandler); String[][] records = csvHandler.getRecords();
Input (reader):
1,One 2,"T,wo" 3,"T""hree" 4,"F our"
Output (records):
[1, One] [2, T,wo] [3, T"hree] [4, F our]
Usage:
import static sharin.unlinq.BasicEnumerable.*; ... Func<Integer, Boolean> whereFunc = new Func<Integer, Boolean>() { public Boolean call(Integer arg1) { return arg1 % 3 == 1; } }; Func<Integer, Character> selectFunc = new Func<Integer, Character>() { public Character call(Integer arg1) { return (char) ('A' + arg1 - 1); } }; List<Character> result = from(10, 21, 14, 12, 1, 22, 9, 14, 1, 17) .where(whereFunc).select(selectFunc).toList();
Output (result):
[J, A, V, A]