All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rt.orm.jdbc.JdbcContext Maven / Gradle / Ivy

There is a newer version: 1.1.17
Show newest version
package com.rt.orm.jdbc;

import com.json.JSONArray;
import com.json.JSONObject;
import com.rt.core.constant.RTConst;
import com.rt.core.util.RTUtil;
import com.rt.web.beans.WebBean;

import java.io.Serializable;
import java.util.Map;

/**
 * 抽象数据库访问动作以及返回值
 */
public class JdbcContext extends WebBean {


    public static void main(String[] args) {
        // 生成参数示例
        JdbcContext bean = new JdbcContext();
        bean.setTraceId("testTraceId");
        bean.setLogicId("/dao/getXXX");

        // update 类型
        String updateId = bean.addUpdateSql("update SYS_USER set sys_version = 1 where id = :id", JdbcParam.newIdParam("11"));

        // query 类型
//        String queryId = bean.addQuery("select * from SYS_USER", new JSONObject());

        // get entity 类型
        String getEntityId = bean.addGetEntity("ORDER_INFO", "1");

        // insert entity 类型
        String insertEntityId = bean.addInsertEntity("ORDER_INFO", JdbcParam.newBuilder()
                .withParam("kk", "vv").withParam("abc", "def").build());

        // update entity 类型
        String updateEntityId = bean.addUpdateEntity("ORDER_INFO2", JdbcParam.newBuilder()
                .withParam("kk", "vv2").withParam("abc", "def2").build());

        // save entity 类型
        String saveEntityId = bean.addSaveEntity("ORDER_INFO3", JdbcParam.newBuilder()
                .withParam("kk", "vv3").withParam("abc", "def3").build());

        // delete  entity 类型
        String deleteEntityId = bean.addDeleteEntity("ORDER_INFO4", "1");


        System.out.println("-----------------------------");
        System.out.println(bean.toString(4));
        System.out.println("-----------------------------");

    }

    public JdbcContext() {
    }

    public JdbcContext(String string) throws Exception {
        super(string);
    }

    public JdbcContext(Map map) {
        super(map);
    }

    private void addOperation(JSONObject op) {
        JSONArray dataArray = this.getData();
        if (RTUtil.isEmpty(dataArray)) {
            dataArray = new JSONArray();
            this.setData(dataArray);
        }
        dataArray.add(op);
    }

    /**
     * 快速生成 query 类型的参数
     *
     * @param sql   sql
     * @param param param
     * @return String
     */
    public String addQuery(String sql, JSONObject param) {
        JSONObject op = JdbcBean.newBuilder()
                .withType(JdbcBean.TYPE_QUERY)
                .withSql(sql)
                .withParams(param)
                .build();
        addOperation(op);
        return op.getId();
    }

    /**
     * 快速生成 query 类型的参数
     *
     * @param sql   sql
     * @param param param
     * @param startRow startRow
     * @param maxRow maxRow
     * @param ignoreError ignoreError
     * @return String id
     */
    public String addQuery(String sql, JSONObject param, Long startRow, Long maxRow, String ignoreError) {
        JSONObject op = JdbcBean.newBuilder()
                .withType(JdbcBean.TYPE_QUERY)
                .withSql(sql)
                .withParams(param)
                .withStartRow(startRow)
                .withMaxRow(maxRow)
                .withIgnoreError(ignoreError)
                .build();

        addOperation(op);
        return op.getId();
    }

    /**
     * 快速生成 query 类型的参数
     *
     * @param sql      sql
     * @param param    param
     * @param startRow startRow
     * @param maxRow   maxRow
     * @return String
     */
    public String addQuery(String sql, JSONObject param, Long startRow, Long maxRow) {
        return addQuery(sql, param, startRow, maxRow, RTConst.FALSE);
    }

    /**
     * 快速生成 getEntity 类型的参数
     *
     * @param tableName tableName
     * @param id        id
     * @param ignoreError        ignoreError
     * @return String
     */
    public String addGetEntity(String tableName, Serializable id, String ignoreError) {
        JSONObject op = JdbcBean.newBuilder()
                .withType(JdbcBean.TYPE_GET_ENTITY)
                .withTableName(tableName)
                .withParams(JdbcParam.newIdParam(id))
                .withIgnoreError(ignoreError)
                .build();

        addOperation(op);
        return op.getId();
    }

    /**
     * 快速生成 getEntity 类型的参数
     *
     * @param tableName tableName
     * @param id        id
     * @return String id
     */
    public String addGetEntity(String tableName, Serializable id) {
        return addGetEntity(tableName, id, RTConst.FALSE);
    }

    /**
     * 快速生成 getEntity 类型的参数
     *
     * @param tableName tableName
     * @param entity    entity
     * @param ignoreError    ignoreError
     * @return String id
     */
    public String addInsertEntity(String tableName, JSONObject entity, String ignoreError) {
        JSONObject op = JdbcBean.newBuilder()
                .withType(JdbcBean.TYPE_INSERT_ENTITY)
                .withTableName(tableName)
                .withParams(entity)
                .withIgnoreError(ignoreError)
                .build();

        addOperation(op);
        return op.getId();
    }

    /**
     * 快速生成 getEntity 类型的参数
     *
     * @param tableName tableName
     * @param entity    entity
     * @return String id
     */
    public String addInsertEntity(String tableName, JSONObject entity) {
        return addInsertEntity(tableName, entity, RTConst.FALSE);
    }

    /**
     * 快速生成 updateEntity 类型的参数
     *
     * @param tableName tableName
     * @param entity    entity
     * @param ignoreError    ignoreError
     * @return String id
     */
    public String addUpdateEntity(String tableName, JSONObject entity, String ignoreError) {
        JSONObject op = JdbcBean.newBuilder()
                .withType(JdbcBean.TYPE_UPDATE_ENTITY)
                .withTableName(tableName)
                .withParams(entity)
                .withIgnoreError(ignoreError)
                .build();

        addOperation(op);
        return op.getId();
    }

    /**
     * 快速生成 updateEntity 类型的参数
     *
     * @param tableName tableName
     * @param entity    entity
     * @return String id
     */
    public String addUpdateEntity(String tableName, JSONObject entity) {
        return addUpdateEntity(tableName, entity, RTConst.FALSE);
    }

    /**
     * 快速生成 saveEntity 类型的参数
     *
     * @param tableName tableName
     * @param entity    entity
     * @param ignoreError    ignoreError
     * @return String id
     */
    public String addSaveEntity(String tableName, JSONObject entity, String ignoreError) {
        JSONObject op = JdbcBean.newBuilder()
                .withType(JdbcBean.TYPE_SAVE_ENTITY)
                .withTableName(tableName)
                .withParams(entity)
                .withIgnoreError(ignoreError)
                .build();

        addOperation(op);
        return op.getId();
    }

    /**
     * 快速生成 saveEntity 类型的参数
     *
     * @param tableName tableName
     * @param entity    entity
     * @return String id
     */
    public String addSaveEntity(String tableName, JSONObject entity) {
        return addSaveEntity(tableName, entity, RTConst.FALSE);
    }

    /**
     * 快速生成 getEntity 类型的参数
     *
     * @param tableName   tableName
     * @param entity      entity
     * @param ignoreError ignoreError
     * @return String id
     */
    public String addDeleteEntity(String tableName, JSONObject entity, String ignoreError) {
        JSONObject op = JdbcBean.newBuilder()
                .withType(JdbcBean.TYPE_DELETE_ENTITY)
                .withTableName(tableName)
                .withParams(entity)
                .withIgnoreError(ignoreError)
                .build();

        addOperation(op);
        return op.getId();
    }

    /**
     * 快速生成 getEntity 类型的参数
     *
     * @param tableName tableName
     * @param entity    entity
     * @return String id
     */
    public String addDeleteEntity(String tableName, JSONObject entity) {
        return addDeleteEntity(tableName, entity, RTConst.FALSE);
    }

    /**
     * 快速生成 getEntity 类型的参数
     *
     * @param tableName tableName
     * @param id        id
     * @param ignoreError        ignoreError
     * @return String id
     */
    public String addDeleteEntity(String tableName, Serializable id, String ignoreError) {
        JSONObject op = JdbcBean.newBuilder()
                .withType(JdbcBean.TYPE_DELETE_ENTITY)
                .withTableName(tableName)
                .withParams(JdbcParam.newIdParam(id))
                .withIgnoreError(ignoreError)
                .build();

        addOperation(op);
        return op.getId();
    }

    /**
     * 快速生成 getEntity 类型的参数
     *
     * @param tableName tableName
     * @param id        id
     * @return String id
     */
    public String addDeleteEntity(String tableName, Serializable id) {
        return addDeleteEntity(tableName, id, RTConst.FALSE);
    }

    /**
     * 快速生成 Update 类型的参数
     *
     * @param sql         sql
     * @param param       param
     * @param ignoreError ignoreError
     * @return String id
     */
    public String addUpdateSql(String sql, JSONObject param, String ignoreError) {
        JSONObject op = JdbcBean.newBuilder()
                .withType(JdbcBean.TYPE_SQL)
                .withSql(sql)
                .withParams(param)
                .withIgnoreError(ignoreError)
                .build();
        addOperation(op);
        return op.getId();
    }

    /**
     * 快速生成 Update 类型的参数
     *
     * @param sql   sql
     * @param param param
     * @return String id
     */
    public String addUpdateSql(String sql, JSONObject param) {
        return addUpdateSql(sql, param, RTConst.FALSE);
    }

    /**
     * 获取Data列表中获取第一个id为指定id的数据对象
     *
     * @param id id
     * @return JdbcBean
     */
    public JdbcBean getDataItem(String id) {
        Object data = getData();
        if (data == null) {
            return null;
        }
        if (data instanceof JSONArray) {
            JSONArray array = (JSONArray) data;
            if (array.isEmpty()) {
                return null;
            }
            for (Object item : array) {
                JdbcBean dataObj = new JdbcBean((Map) item);
                if (id.equals(dataObj.getId())) {
                    return dataObj;
                }
            }
            return null;
        }
        return null;
    }

    /**
     * 快速取得第一个数据对象的执行结果
     *
     * @param  object
     * @return T object
     */
    public  T getResult() {
        JSONObject dataObj = getDataItem();
        if (RTUtil.isEmpty(dataObj)) {
            return null;
        }
        return (T) dataObj.get("data");
    }

    /**
     * 快速取得id为指定id的数据对象的执行结果
     *
     * @param id id
     * @param  object
     * @return T object
     */
    public  T getResult(String id) {
        JdbcBean dataObj = getDataItem(id);
        if (RTUtil.isEmpty(dataObj)) {
            return null;
        }
        return (T) dataObj.getData();
    }

    public void setDependDataId(String id, String dependId) {
        JdbcBean bean = getDataItem(id);
        if (RTUtil.isNotEmpty(bean)) {
            bean.setDependDataId(dependId);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy