^ Inspired By
^ CRUD code for all data tables/views in 5 minutes
1. Generate declarations of DTO classes (Plug-in GUI -> tab 'DTO' -> local tool-bar -> DTO CRUD XML Assistant):
<dto-class name="Project" ref="projects" /> <dto-class name="Task" ref="tasks" />
Copy and paste the generated XML tags into 'dto.xml'.
Generate DTO classes.
2. Generate XML declarations of CRUD actions (Plug-in GUI -> tab 'DAO' -> local tool-bar -> DAO CRUD XML Assistant):
<crud dto="Project" table="projects" /> <crud dto="Task" table="tasks" /> <query-dto-list method="get_tasks_by_project(p_id)" dto="Task" ref="tasks(p_id)" />
Copy and paste the generated XML tags into your existing DAO XML files. Use Plug-in GUI -> tab 'DAO' -> local tool-bar -> 'New XML File' if needed.
Generate DAO classes. DONE.
^ If you want to create a "type-map" from scratch
Start with this:
<type-map default=""> </type-map>
The code generator will render something like this:
type Actor struct {
ActorId java.lang.Integer // not a Go type!
...Extend the type-map:
<type-map default="">
<type detected="java.lang.Integer" target="int64" />
</type-map>
type Actor struct {
ActorId int64
...Do the same for all detected types.
For Go only, so far: if you need additional imports, use this:
<type detected="java.util.Date" target="time:time.Time"/>
<type detected="gorm-java.util.UUID" target="github.com/google/uuid:uuid.UUID${json-gorm}"/>
^ Android + Java + SQLite3
- The SQLite3 database file must be present on both the Android device and the development PC. A development or testing copy of the file is fine too, as long as the schemas are identical.
- To generate the code, use a JDBC driver such as xerial / sqlite-jdbc.
- An example of DataStoreManager.java for Android ships with the plug-ins (tab 'Admin'). It is also available on GitHub.
- JDBC drivers for SQLite3 do not report the types of ResultSet
columns and SQL parameters. If you need strongly typed code,
specify the types in XML:
<dto-class name="Board" ref="get_boards.sql" > <field column="b_id" type="Long" /> <field column="b_name" type="String" /> <field column="b_dt" type="String" /> <field column="b_tasks_count" type="Long" /> </dto-class> - Problem:
- In SQLite3, only the INTEGER type is allowed for an AUTOINCREMENT PRIMARY KEY.
- In the CRUD scenario, the code generator (through the SQLite3 JDBC driver) detects INTEGER as java.lang.Integer.
- On the Android side, at run time, INTEGERs are fetched as java.lang.Long, and exceptions are thrown.
A workaround in 'settings.xml' looks like this:
<type-map default=""> <type detected="java.lang.Integer" target="java.lang.Long"/> </type-map>or this:
<type-map default=""> <type detected="java.lang.Integer" target="Long --> SetLong"/> </type-map>
^ Strong typing in generated DAO classes
NOTE: for Go and Java only, so far.
This is what it looks like:
<type-map default="">
<type detected="java.lang.Integer" target="int64${json} -> SetInt64"/>
<type detected="java.lang.Double" target="float64${json} -> SetFloat64"/>
<type detected="java.lang.String" target="string${json} -> SetString"/>
<type detected="byte[]" target="byte[]${json} -> SetBytes"/>
<type detected="java.lang.Object" target="interface{}${json}"/>
</type-map>
func (dao *ProjectsDao) GetProjectList(ctx context.Context) (res []*dto.Project, err error) {
sql := `select p.*,
(select count(*) from tasks where p_id=p.p_id) as p_tasks_count
from projects p
order by p.p_id`
errMap := make(map[string]int)
_onRow := func(row map[string]interface{}) {
obj := dto.Project{}
SetInt64(&obj.PId, row, "p_id", errMap)
SetString(&obj.PName, row, "p_name", errMap)
SetInt64(&obj.PTasksCount, row, "p_tasks_count", errMap)
res = append(res, &obj)
}
err = dao.ds.QueryAllRows(ctx, sql, _onRow)
if err == nil {
err = ErrMapToErr(errMap)
}
return
}
^ Automation of XML coding
By default, IDE XML editors provide code completion for XML tags (CTRL+Space). The plug-ins extend it with
- code completion/navigation to select SQL files
- code completion/navigation to select DTO classes
- code completion to select data tables/views.
^ IDE toolbar button (Eclipse only, removed from JB since 1.314)
Each plug-in extends the IDE toolbar with a drop-down button for navigating between XML manifests.
The button also lets you generate and validate the code without opening the plug-in GUI.
^ IDE Error Logging
Errors of code generation and validation can be shown in the IDE console. Update the XSD files from the tab 'Admin' and add <ide event-log='true' /> to 'settings.xml'.
^ Code Validation Tools
1) Plug-in GUI > tab 'DTO' > local toolbar > 'Validate'. 2) Plug-in GUI > tab 'DAO' > local toolbar > 'Validate'.
These tools let you check periodically that everything is still fine with your XML manifest, the referenced SQL statements, and the code generated earlier. Errors and inconsistencies are logged, so you can analyze them and plan the fixes.
^ Inheritance of generated DTO classes
'settings.xml':
<dto inheritance="implements java.io.Serializable" ...
Generated Java class:
public class Actor implements java.io.Serializable {