use of com.ctrip.platform.dal.daogen.host.AbstractParameterHost in project dal by ctripcorp.
the class CsharpGivenSqlResultSetExtractor method extractData.
@Override
public List<AbstractParameterHost> extractData(ResultSet rs) throws SQLException {
List<AbstractParameterHost> hosts = new ArrayList<>();
if (rs == null) {
return hosts;
}
ResultSetMetaData metaData = rs.getMetaData();
int count = metaData.getColumnCount();
for (int i = 1; i <= count; i++) {
CSharpParameterHost host = new CSharpParameterHost();
String columnName = metaData.getColumnLabel(i);
host.setName(columnName);
String typeName = metaData.getColumnTypeName(i);
boolean isUnsigned = DbUtils.isColumnUnsigned(typeName);
int dataType = metaData.getColumnType(i);
int length = metaData.getColumnDisplaySize(i);
// 特殊处理
DbType dbType = DbUtils.getDotNetDbType(typeName, dataType, length, isUnsigned, dbCategory);
host.setDbType(dbType);
String type = DbType.getCSharpType(host.getDbType());
host.setType(type);
host.setIdentity(false);
host.setNullable(metaData.isNullable(i) == 1 ? true : false);
host.setPrimary(false);
host.setLength(length);
host.setValueType(Consts.CSharpValueTypes.contains(host.getType()));
hosts.add(host);
}
return hosts;
}
use of com.ctrip.platform.dal.daogen.host.AbstractParameterHost in project dal by ctripcorp.
the class CsharpSelectFieldResultSetExtractor method extractData.
@Override
public List<AbstractParameterHost> extractData(ResultSet rs) throws SQLException {
ResultSetMetaData rsMeta = rs.getMetaData();
List<AbstractParameterHost> hosts = new ArrayList<>();
for (int i = 1; i <= rsMeta.getColumnCount(); i++) {
CSharpParameterHost pHost = new CSharpParameterHost();
pHost.setName(rsMeta.getColumnLabel(i));
pHost.setDbType(DbType.getDbTypeFromJdbcType(rsMeta.getColumnType(i)));
pHost.setType(DbType.getCSharpType(pHost.getDbType()));
pHost.setIdentity(false);
pHost.setNullable(rsMeta.isNullable(i) == 1 ? true : false);
pHost.setValueType(Consts.CSharpValueTypes.contains(pHost.getType()));
pHost.setPrimary(false);
pHost.setLength(rsMeta.getColumnDisplaySize(i));
hosts.add(pHost);
}
return hosts;
}
use of com.ctrip.platform.dal.daogen.host.AbstractParameterHost in project dal by ctripcorp.
the class JavaGivenSqlResultSetExtractor method extractData.
@Override
public List<AbstractParameterHost> extractData(ResultSet rs) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
List<AbstractParameterHost> paramHosts = new ArrayList<>();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
JavaParameterHost paramHost = new JavaParameterHost();
paramHost.setName(rsmd.getColumnLabel(i));
paramHost.setSqlType(rsmd.getColumnType(i));
//paramHost.setJavaClass(Consts.jdbcSqlTypeToJavaClass.get(paramHost.getSqlType()));
Class<?> javaClass = null;
try {
javaClass = Class.forName(rsmd.getColumnClassName(i));
} catch (Exception e) {
e.printStackTrace();
javaClass = Consts.jdbcSqlTypeToJavaClass.get(paramHost.getSqlType());
}
paramHost.setJavaClass(javaClass);
paramHost.setIdentity(false);
paramHost.setNullable(rsmd.isNullable(i) == 1 ? true : false);
paramHost.setPrimary(false);
paramHost.setLength(rsmd.getColumnDisplaySize(i));
paramHosts.add(paramHost);
}
return paramHosts;
}
use of com.ctrip.platform.dal.daogen.host.AbstractParameterHost in project dal by ctripcorp.
the class DbUtils method getSelectFieldHosts.
public static List<AbstractParameterHost> getSelectFieldHosts(String allInOneName, String sql, DalResultSetExtractor<List<AbstractParameterHost>> extractor) throws Exception {
List<AbstractParameterHost> list = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
StatementParameters parameters = new StatementParameters();
DalHints hints = DalHints.createIfAbsent(null);
String testSql = sql;
int whereIndex = StringUtils.indexOfIgnoreCase(testSql, "where");
if (whereIndex > 0)
testSql = sql.substring(0, whereIndex);
if (isMySqlServer(allInOneName)) {
testSql = testSql + " limit 1";
} else {
testSql = testSql.replace("select", "select top(1)");
}
try {
connection = DataSourceUtil.getConnection(allInOneName);
preparedStatement = statementCreator.createPreparedStatement(connection, testSql, parameters, hints);
resultSet = preparedStatement.executeQuery();
list = extractor.extract(resultSet);
} catch (Throwable e) {
throw e;
} finally {
ResourceUtils.close(resultSet);
ResourceUtils.close(preparedStatement);
ResourceUtils.close(connection);
}
return list;
}
use of com.ctrip.platform.dal.daogen.host.AbstractParameterHost in project dal by ctripcorp.
the class DbUtils method testAQuerySql.
public static List<AbstractParameterHost> testAQuerySql(String allInOneName, String sql, String params, DalResultSetExtractor<List<AbstractParameterHost>> extractor) throws Exception {
List<AbstractParameterHost> result = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = DataSourceUtil.getConnection(allInOneName);
List<Parameter> list = new ArrayList<>();
String[] parameters = params.split(";");
if (parameters != null && parameters.length > 0) {
for (String p : parameters) {
if (p.isEmpty()) {
continue;
}
String[] tuple = p.split(",");
if (tuple != null && tuple.length > 0) {
Parameter parameter = new Parameter();
parameter.setName(tuple[0]);
parameter.setType(Integer.valueOf(tuple[1]));
list.add(parameter);
}
}
}
Boolean containsQuestionMark = sql.indexOf("?") > -1;
String temp1 = sql;
if (!containsQuestionMark) {
Matcher matcher = pattern.matcher(sql);
// Match C# parameters
if (matcher.find()) {
list = getActualParameters(sql, list);
}
Matcher m = inRegxPattern.matcher(sql);
String temp = sql;
while (m.find()) {
temp = temp.replace(m.group(1), String.format("(?) "));
}
temp1 = temp.replaceAll(expression, "?");
}
preparedStatement = connection.prepareStatement(temp1);
int index = 0;
for (Parameter parameter : list) {
String name = parameter.getName();
int type = parameter.getType();
try {
index = Integer.valueOf(name);
} catch (NumberFormatException ex) {
index++;
}
if (type == 10001) {
preparedStatement.setObject(index, mockATest(type), Types.BINARY);
} else {
preparedStatement.setObject(index, mockATest(type), type);
}
}
ResultSet rs = preparedStatement.executeQuery();
result = extractor.extract(rs);
} catch (Throwable e) {
throw e;
} finally {
ResourceUtils.close(resultSet);
ResourceUtils.close(preparedStatement);
ResourceUtils.close(connection);
}
return result;
}
Aggregations