panedrone / sdm_todolist_jsf_jdbc_sqlite3

JSF SPA project demonstrating how to use SQL DAL Maker + Java/JDBC.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sdm_todolist_jsf_jdbc_sqlite3

JSF SPA project demonstrating how to use SQL DAL Maker + Java/JDBC.

demo-jsf-1.png

sdm.xml

<sdm>

    <dto-class name="Group" ref="groups">

        <field type="Integer" column="g_tasks_count"/>

    </dto-class>

    <dto-class name="Task" ref="tasks"/>

    <dao-class name="GroupsDao">

        <crud dto="Group">
            <create/>
            <update/>
            <delete/>
        </crud>

        <query-dto-list ref="get_groups.sql" dto="Group" method="getGroups()"/>

    </dao-class>

    <dao-class name="TasksDao">

        <crud dto="Task">
            <create/>
            <update/>
            <delete/>
        </crud>

        <query-dto-list ref="get_group_tasks.sql" dto="Task" method="getGroupTasks(int t_id)"/>

    </dao-class>

</sdm>

Generated DAO class:

package com.sqldalmaker.todolist.model.dao;

import com.sqldalmaker.DataStore;

import com.sqldalmaker.todolist.model.dto.Group;
import java.util.ArrayList;
import java.util.List;

// Code generated by a tool. DO NOT EDIT.
// https://sqldalmaker.sourceforge.net/

public class GroupsDao {

    protected final DataStore ds;

    public GroupsDao(DataStore ds) {
        this.ds = ds;
    }

    /*
        (C)RUD: groups
        Generated values are passed to DTO.
        Returns the number of affected rows or -1 on error.
     */
    public int createGroup(Group p) throws Exception {
        String sql = "insert into groups (g_name) values (?)";
        String[] gen_col_nm = new String[]{"g_id"};
        Object[] gen_values = new Object[gen_col_nm.length];
        int res = ds.insert(sql, gen_col_nm, gen_values, p.getGName());
        p.setGId(ds.castGeneratedValue(Integer.class, gen_values[0]));
        return res;
    }

    /*
        CR(U)D: groups
        Returns the number of affected rows or -1 on error.
     */
    public int updateGroup(Group p) throws Exception {
        String sql = "update groups set g_name=? where g_id=?";
        return ds.execDML(sql, p.getGName(), p.getGId());
    }

    /*
        CR(U)D: groups
        Returns the number of affected rows or -1 on error.
     */
    public int updateGroup(String gName, Integer gId) throws Exception {
        String sql = "update groups set g_name=? where g_id=?";
        return ds.execDML(sql, gName, gId);
    }

    /*
        CRU(D): groups
        Returns the number of affected rows or -1 on error.
     */
    public int deleteGroup(Integer gId) throws Exception {
        String sql = "delete from groups where g_id=?";
        return ds.execDML(sql, gId);
    }

    public List<Group> getGroups() throws Exception {
        String sql = "select g.*," +
                "\n (select count(*) from tasks where g_ID=g.g_ID) as tasks_count" +
                "\n from groups g";
        final List<Group> res = new ArrayList<Group>();
        ds.queryAllRows(sql, new DataStore.RowHandler() {
            @Override
            public void handleRow(DataStore.RowData rd) throws Exception {
                Group obj = new Group();
                obj.setGId(rd.getInteger("g_id"));  // t <- q
                obj.setGName(rd.getString("g_name"));  // t <- q
                res.add(obj);
            }
        });
        return res;
    }
}

About

JSF SPA project demonstrating how to use SQL DAL Maker + Java/JDBC.


Languages

Language:Java 81.6%Language:HTML 17.3%Language:CSS 1.1%