^ 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-paste generated XML tags to '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-auto dto="Project" table="projects" /> <crud-auto dto="Task" table="tasks" /> <query-dto-list method="get_tasks_by_project(p_id)" dto="Product" ref="tasks(p_id)" />
Copy-paste generated XML tags to 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 "type-map" from zero
Start with this:
<type-map default=""> </type-map>
Code generator will render something like this:
type Actor struct { ActorId java.lang.Integer // not a Go type! ...
Extend 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.
Only for Go 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
- SQLite3 database file must be located on both Android-Device and Development-PC. A development/testing version of this file is also OK: just ensure identical Schemas.
- To generate the code, use JDBC driver like this: xerial / sqlite-jdbc.
- An example of DataStoreManager.java for Android is shipped with the plug-ins (tab 'Admin'). Also, it is available on GitHub.
- JDBC drivers for SQLite3 do not provide information about the
types of ResultSet columns and SQL parameters. If you need to
generate 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 INTEGER-type is allowed for AUTOINCREMENT PRIMARY KEY.
- In CRUD scenario, INTEGER is detected by code generator (SQLite3 JDBC driver) as java.lang.Integer.
- On Android-side (at run-time), INTEGER-s are fetched as java.lang.Long and exceptions are thrown.
Workaround in 'settings.xml 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: only for Go and Java so far.
This is how it looks alike:
<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 this functionality 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
Each plug-in extends IDE toolbar with a drop-down button for navigation between XML meta-programs.
Also, this button allows to generate/validate the code without opening plug-in GUI.
^ IDE Error Logging
There is a possibility to display errors of code generation/validation in IDE console. Update 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 allow you to be sure (periodically) that all is OK with your XML meta-program, referenced SQL statements, and the code generated earlier. Errors and inconsistencies are logged and may be used for further analysis and planing of fixes.
^ Inheritance of generated DTO classes
'settings.xml':
<dto inheritance="implements java.io.Serializable" ...
Generated Java class:
public class Actor implements java.io.Serializable {