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 = BeanGetter.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 (Throwable e) {
LoggerManager.getInstance().error(e);
status = Status.ERROR();
status.setInfo(e.getMessage());
return status;
}
return status;
}
use of com.ctrip.platform.dal.daogen.domain.Status in project dal by ctripcorp.
the class DatabaseResource method updateDB.
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("updateDB")
public Status updateDB(@Context HttpServletRequest request, @FormParam("id") int id, @FormParam("dbtype") String dbtype, @FormParam("allinonename") String allinonename, @FormParam("dbaddress") String dbaddress, @FormParam("dbport") String dbport, @FormParam("dbuser") String dbuser, @FormParam("dbpassword") String dbpassword, @FormParam("dbcatalog") String dbcatalog) throws Exception {
try {
Status status = Status.OK();
DalGroupDBDao allDbDao = BeanGetter.getDaoOfDalGroupDB();
DalGroupDB db = allDbDao.getGroupDBByDbName(allinonename);
if (db != null && db.getId() != id) {
status = Status.ERROR();
status.setInfo(allinonename + "已经存在!");
return status;
}
String userNo = RequestUtil.getUserNo(request);
LoginUser user = BeanGetter.getDaoOfLoginUser().getUserByNo(userNo);
DalGroupDB groupDb = allDbDao.getGroupDBByDbId(id);
if (!validatePermision(user.getId(), groupDb.getDal_group_id())) {
status = Status.ERROR();
status.setInfo("你没有当前DataBase的操作权限.");
return status;
}
allDbDao.updateGroupDB(id, allinonename, dbaddress, dbport, dbuser, dbpassword, dbcatalog, DatabaseType.valueOf(dbtype).getValue());
return Status.OK();
} catch (Throwable e) {
LoggerManager.getInstance().error(e);
Status status = Status.ERROR();
status.setInfo(e.getMessage());
return status;
}
}
use of com.ctrip.platform.dal.daogen.domain.Status in project dal by ctripcorp.
the class DatabaseResource method connectionTest.
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("connectionTest")
public Status connectionTest(@FormParam("dbtype") String dbtype, @FormParam("dbaddress") String dbaddress, @FormParam("dbport") String dbport, @FormParam("dbuser") String dbuser, @FormParam("dbpassword") String dbpassword) throws Exception {
Connection conn = null;
ResultSet rs = null;
try {
Status status = Status.OK();
try {
conn = DataSourceUtil.getConnection(dbaddress, dbport, dbuser, dbpassword, DatabaseType.valueOf(dbtype).getValue());
// conn.setNetworkTimeout(Executors.newFixedThreadPool(1), 5000);
rs = conn.getMetaData().getCatalogs();
Set<String> allCatalog = new HashSet<String>();
while (rs.next()) {
allCatalog.add(rs.getString("TABLE_CAT"));
}
status.setInfo(mapper.writeValueAsString(allCatalog));
} catch (SQLException e) {
status = Status.ERROR();
status.setInfo(e.getMessage());
return status;
} catch (JsonProcessingException e) {
status = Status.ERROR();
status.setInfo(e.getMessage());
return status;
}
return status;
} catch (Throwable e) {
LoggerManager.getInstance().error(e);
Status status = Status.ERROR();
status.setInfo(e.getMessage());
return status;
} finally {
ResourceUtils.close(rs);
ResourceUtils.close(conn);
}
}
use of com.ctrip.platform.dal.daogen.domain.Status in project dal by ctripcorp.
the class DatabaseResource method validationKey.
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("validation")
public Status validationKey(@QueryParam("key") String key) throws Exception {
try {
Status status = Status.ERROR();
Response res = WebUtil.getAllInOneResponse(key, null);
String httpCode = res.getStatus();
if (!httpCode.equals(WebUtil.HTTP_CODE)) {
status.setInfo("Access error.");
return status;
}
status = Status.OK();
ResponseData[] data = res.getData();
if (data != null && data.length > 0) {
String error = data[0].getErrorMessage();
if (error != null && !error.isEmpty()) {
status.setInfo(error);
} else {
status.setInfo("");
}
}
return status;
} catch (Throwable e) {
LoggerManager.getInstance().error(e);
Status status = Status.ERROR();
status.setInfo(e.getMessage());
return 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 LinkedHashMap<>();
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 = BeanGetter.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 (Throwable e) {
LoggerManager.getInstance().error(e);
status = Status.ERROR();
status.setInfo(e.getMessage());
}
return status;
}
Aggregations