美文网首页
Ureport丰富SQL数据集的SQL参数的数据类型

Ureport丰富SQL数据集的SQL参数的数据类型

作者: 看书的蜗牛 | 来源:发表于2020-09-26 00:10 被阅读0次

Ureport的SQL参数默认支持的数据类型包括:String、Integer、Float、Boolean、Date、List。

当前数据类型不满足我们的功能需要,我们的时间再数据库中都是通过时间戳(Long)的形式存放的。

需要修改的地方有两部分:

一、页面部分(JS代码)

在ureport.console工程中,找到并打开resources/ureport-asserts/js/designer.bundle.js文件,搜索如下内容

<option>Integer</option>

在<option>Integer</option>的前后位置增加如下代码后保存即可

<option>Long</option>

二、后台部分(Java代码)

在ureport.core工程中,修改com.bstek.ureport.definition.datasource.DataType.java

package com.bstek.ureport.definition.datasource;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import com.bstek.ureport.Utils;

import com.bstek.ureport.exception.ReportComputeException;

/**

* @author Jacky.gao

* @since 2016年12月27日

*/

public enum DataType {

    Integer,Float,Boolean,String,Date,List,Long;

    public Object parse(Object obj){

        if(obj==null)return null;

        switch(this){

        case Boolean:

            if(obj.toString().equals("")){

                return null;

            }

            if(obj instanceof Boolean){

                return (Boolean)obj;

            }else{

                return java.lang.Boolean.valueOf(obj.toString());

            }

        case Float:

            if(obj.toString().equals("")){

                return null;

            }

            if(obj instanceof Float){

                return (Float)obj;

            }else{

                return Utils.toBigDecimal(obj).doubleValue();

            }

        case Integer:

            if(obj.toString().equals("")){

                return null;

            }

            if(obj instanceof Integer){

                return (Integer)obj;

            }else{

                return Utils.toBigDecimal(obj).intValue();

            }

case Long:

            if(obj.toString().equals("")){

                return null;

            }

            if(obj instanceof Long){

                return (Long)obj;

            }else{

                return Utils.toBigDecimal(obj).longValue();

            }

        case String:

            if(obj instanceof String){

                return (String)obj;

            }else{

                return obj.toString();

            }

        case List:

            if(obj.toString().equals("")){

                return null;

            }

            if(obj instanceof List){

                return (List<?>)obj;

            }else{

                String[] arrs=obj.toString().split(",");

                List<String> list=new ArrayList<String>();

                for(int i=0;i<arrs.length;i++){

                    list.add(arrs[i]);

                }

                return list;

            }

        case Date:

            if(obj.toString().equals("")){

                return null;

            }

            if(obj instanceof Date){

                return (Date)obj;

            }else{

                Date date=null;

                SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                try{

                    date=sd.parse(obj.toString());

                }catch(ParseException e){

                    sd=new SimpleDateFormat("yyyy-MM-dd");

                    try{

                        date=sd.parse(obj.toString());                        

                    }catch(ParseException ex){

                        throw new ReportComputeException("Date parameter value pattern must be \"yyyy-MM-dd\" or \"yyyy-MM-dd HH:mm:ss\".");

                    }

                }

                return date;

            }

        }

        throw new ReportComputeException("Unknow parameter type : " + this);

    }

}

加粗倾斜字体部分为增加的内容。

相关文章

网友评论

      本文标题:Ureport丰富SQL数据集的SQL参数的数据类型

      本文链接:https://www.haomeiwen.com/subject/cmyfuktx.html