Search in sources :

Example 26 with Status

use of com.ctrip.platform.dal.daogen.domain.Status in project dal by ctripcorp.

the class GenTaskByTableViewResource method getApiList.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("apiList")
public Status getApiList(@QueryParam("db_name") String db_set_name, @QueryParam("table_names") String table_names, @QueryParam("sql_style") String sql_style) {
    Status status = Status.OK;
    try {
        List<DalApi> apis = null;
        DatabaseSetEntry databaseSetEntry = SpringBeanGetter.getDaoOfDatabaseSet().getMasterDatabaseSetEntryByDatabaseSetName(db_set_name);
        DatabaseCategory dbCategory = DbUtils.getDatabaseCategory(databaseSetEntry.getConnectionString());
        if ("csharp".equalsIgnoreCase(sql_style)) {
            if (dbCategory == DatabaseCategory.MySql) {
                apis = SpringBeanGetter.getDalApiDao().getDalApiByLanguageAndDbtype("csharp", "MySQL");
            } else {
                apis = SpringBeanGetter.getDalApiDao().getDalApiByLanguageAndDbtype("csharp", "SQLServer");
            }
        } else {
            if (dbCategory == DatabaseCategory.MySql) {
                apis = SpringBeanGetter.getDalApiDao().getDalApiByLanguageAndDbtype("java", "MySQL");
            } else {
                // SpType spType =
                // spType(databaseSetEntry.getConnectionString(),
                // table_names);
                // apis =
                // SpringBeanGetter.getDalApiDao().getDalApiByLanguageAndDbtypeAndSptype("java",
                // "SQLServer", spType.getValue());
                apis = SpringBeanGetter.getDalApiDao().getDalApiByLanguageAndDbtype("java", "SQLServer");
            }
        }
        for (DalApi api : apis) {
            String method_declaration = api.getMethod_declaration();
            method_declaration = method_declaration.replaceAll("<", "&lt;");
            method_declaration = method_declaration.replaceAll(">", "&gt;");
            api.setMethod_declaration(method_declaration);
        }
        java.util.Collections.sort(apis, new Comparator<DalApi>() {

            @Override
            public int compare(DalApi o1, DalApi o2) {
                return o1.getMethod_declaration().compareToIgnoreCase(o2.getMethod_declaration());
            }
        });
        status.setInfo(mapper.writeValueAsString(apis));
    } catch (Exception e) {
        status = Status.ERROR;
        status.setInfo(e.getMessage());
        return status;
    }
    return status;
}
Also used : Status(com.ctrip.platform.dal.daogen.domain.Status) DatabaseCategory(com.ctrip.platform.dal.daogen.enums.DatabaseCategory)

Example 27 with Status

use of com.ctrip.platform.dal.daogen.domain.Status in project dal by ctripcorp.

the class ProjectResource method validateDbsetPermision.

private Status validateDbsetPermision(int groupId, int project_id) {
    Status status = Status.ERROR;
    Set<String> notExistDbset = getLackDbset(groupId, project_id);
    if (notExistDbset == null || notExistDbset.size() <= 0) {
        return Status.OK;
    } else {
        String info = "<ul>";
        for (String temp : notExistDbset) {
            info += "<li>" + temp + "</li>";
        }
        info += "</ul>";
        DalGroup group = SpringBeanGetter.getDaoOfDalGroup().getDalGroupById(groupId);
        info = "你所在" + group.getGroup_name() + "中不存在以下逻辑数据库(databaseSet):</br>" + info + "请先添加逻辑数据库(databaseSet)到你所在DAL Team!</br>" + "点击此处添加逻辑数据库(databaseSet) : <a href='dbsetsmanage.jsp' target='_blank'>逻辑数据库管理</a>";
        status.setInfo(info);
        return status;
    }
}
Also used : Status(com.ctrip.platform.dal.daogen.domain.Status)

Example 28 with Status

use of com.ctrip.platform.dal.daogen.domain.Status in project dal by ctripcorp.

the class DatabaseResource method getTableSPNames.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("table_sps")
public Status getTableSPNames(@QueryParam("db_name") String setName) {
    Status status = Status.OK;
    TableSpNames tableSpNames = new TableSpNames();
    List<String> views;
    List<String> tables;
    List<StoredProcedure> sps;
    try {
        DatabaseSetEntry databaseSetEntry = SpringBeanGetter.getDaoOfDatabaseSet().getMasterDatabaseSetEntryByDatabaseSetName(setName);
        String dbName = databaseSetEntry.getConnectionString();
        views = DbUtils.getAllViewNames(dbName);
        tables = DbUtils.getAllTableNames(dbName);
        sps = DbUtils.getAllSpNames(dbName);
        sps = filterSP(tables, sps);
        java.util.Collections.sort(views, new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o1.toLowerCase().compareTo(o2.toLowerCase());
            }
        });
        java.util.Collections.sort(tables, new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o1.toLowerCase().compareTo(o2.toLowerCase());
            }
        });
        java.util.Collections.sort(sps);
        tableSpNames.setSps(sps);
        tableSpNames.setViews(views);
        tableSpNames.setTables(tables);
        tableSpNames.setDbType(DbUtils.getDbType(dbName));
        status.setInfo(mapper.writeValueAsString(tableSpNames));
    } catch (Exception e1) {
        status = Status.ERROR;
        status.setInfo(e1.getMessage());
        return status;
    }
    return status;
}
Also used : Status(com.ctrip.platform.dal.daogen.domain.Status) StoredProcedure(com.ctrip.platform.dal.daogen.domain.StoredProcedure) TableSpNames(com.ctrip.platform.dal.daogen.domain.TableSpNames) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SQLException(java.sql.SQLException)

Example 29 with Status

use of com.ctrip.platform.dal.daogen.domain.Status in project dal by ctripcorp.

the class GenTaskByFreeSqlResource method getMockValue.

@POST
@Path("getMockValue")
public Status getMockValue(@FormParam("params") String params) {
    Status status = Status.OK;
    int[] sqlTypes = getSqlTypes(params);
    Object[] values = SQLValidation.mockStringValues(sqlTypes);
    try {
        status.setInfo(mapper.writeValueAsString(values));
    } catch (JsonProcessingException e) {
        status = Status.ERROR;
        status.setInfo("获取mock value异常.");
    }
    return status;
}
Also used : Status(com.ctrip.platform.dal.daogen.domain.Status) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 30 with Status

use of com.ctrip.platform.dal.daogen.domain.Status in project dal by ctripcorp.

the class GenTaskByFreeSqlResource method validateSQL.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("sqlValidate")
public Status validateSQL(@FormParam("db_name") String set_name, @FormParam("crud_type") String crud_type, @FormParam("sql_content") String sql_content, @FormParam("params") String params, @FormParam("pagination") boolean pagination, @FormParam("mockValues") String mockValues) {
    Status status = Status.OK;
    try {
        Map<String, Parameter> map = new HashMap<>();
        List<Parameter> list = new ArrayList<>();
        Matcher matcher = pattern.matcher(sql_content);
        while (matcher.find()) {
            String parameter = matcher.group();
            Parameter p = new Parameter();
            //trim @
            p.setName(parameter.substring(1));
            list.add(p);
        }
        String[] values = mockValues.split(";");
        String[] parameters = params.split(";");
        if (parameters != null && parameters.length > 0) {
            for (int i = 0; i < parameters.length; i++) {
                if (parameters[i].isEmpty()) {
                    continue;
                }
                String[] array = parameters[i].split(",");
                if (array != null && array.length > 0) {
                    String name = array[0];
                    if (name.isEmpty()) {
                        continue;
                    }
                    int type = Integer.valueOf(array[1]);
                    if (!map.containsKey(name)) {
                        Parameter p = new Parameter();
                        p.setType(type);
                        p.setValue(values[i]);
                        map.put(name, p);
                    }
                }
            }
        }
        if (list.size() > 0) {
            for (Parameter p : list) {
                String name = p.getName();
                Parameter temp = map.get(name);
                if (temp != null) {
                    p.setType(temp.getType());
                    p.setValue(temp.getValue());
                }
            }
        } else {
            for (Map.Entry<String, Parameter> entry : map.entrySet()) {
                Parameter p = new Parameter();
                Parameter temp = entry.getValue();
                p.setType(temp.getType());
                p.setValue(temp.getValue());
                list.add(p);
            }
        }
        sql_content = sql_content.replaceAll(expression, "?");
        int[] sqlTypes = getTypes(list);
        values = getValues(list);
        DatabaseSetEntry databaseSetEntry = SpringBeanGetter.getDaoOfDatabaseSet().getMasterDatabaseSetEntryByDatabaseSetName(set_name);
        String dbName = databaseSetEntry.getConnectionString();
        ValidateResult validResult = null;
        String resultPrefix = "The affected rows is ";
        if (pagination && "select".equalsIgnoreCase(crud_type)) {
            sql_content = SqlBuilder.pagingQuerySql(sql_content, DbUtils.getDatabaseCategory(dbName), CurrentLanguage.Java);
            sql_content = String.format(sql_content, 1, 2);
        }
        if ("select".equalsIgnoreCase(crud_type)) {
            validResult = SQLValidation.queryValidate(dbName, sql_content, sqlTypes, values);
            resultPrefix = "The result count is ";
        } else {
            validResult = SQLValidation.updateValidate(dbName, sql_content, sqlTypes, values);
        }
        if (validResult != null && validResult.isPassed()) {
            status.setInfo(resultPrefix + validResult.getAffectRows());
            status.setExplanJson(validResult.getMessage());
        } else {
            status = Status.ERROR;
            status.setInfo(validResult.getMessage());
        }
    } catch (Exception e) {
        status = Status.ERROR;
        status.setInfo(e.getMessage());
    }
    return status;
}
Also used : Status(com.ctrip.platform.dal.daogen.domain.Status) Matcher(java.util.regex.Matcher) ValidateResult(com.ctrip.platform.dal.daogen.sql.validate.ValidateResult) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

Status (com.ctrip.platform.dal.daogen.domain.Status)60 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)14 Timestamp (java.sql.Timestamp)12 LoginUser (com.ctrip.platform.dal.daogen.entity.LoginUser)6 SQLException (java.sql.SQLException)6 DalGroupDBDao (com.ctrip.platform.dal.daogen.dao.DalGroupDBDao)5 ConfigTemplate (com.ctrip.platform.dal.daogen.entity.ConfigTemplate)3 Connection (java.sql.Connection)3 CurrentLanguage (com.ctrip.platform.dal.daogen.enums.CurrentLanguage)2 DatabaseCategory (com.ctrip.platform.dal.daogen.enums.DatabaseCategory)2 ValidateResult (com.ctrip.platform.dal.daogen.sql.validate.ValidateResult)2 ResultSet (java.sql.ResultSet)2 HashSet (java.util.HashSet)2 EmailException (org.apache.commons.mail.EmailException)2 HtmlEmail (org.apache.commons.mail.HtmlEmail)2 VelocityContext (org.apache.velocity.VelocityContext)2 CodeGenContext (com.ctrip.platform.dal.daogen.CodeGenContext)1 DalGenerator (com.ctrip.platform.dal.daogen.DalGenerator)1 StoredProcedure (com.ctrip.platform.dal.daogen.domain.StoredProcedure)1 TableSpNames (com.ctrip.platform.dal.daogen.domain.TableSpNames)1