
com.jfinal.plugin.activerecord.ModelBuilder Maven / Gradle / Ivy
/**
* Copyright (c) 2011-2021, James Zhan 詹波 ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jfinal.plugin.activerecord;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* ModelBuilder.
*/
public class ModelBuilder {
public static final ModelBuilder me = new ModelBuilder();
@SuppressWarnings({"rawtypes"})
public List build(ResultSet rs, Class extends Model> modelClass) throws SQLException, ReflectiveOperationException {
return build(rs, modelClass, null);
}
@SuppressWarnings({"rawtypes", "unchecked"})
public List build(ResultSet rs, Class extends Model> modelClass, Function func) throws SQLException, ReflectiveOperationException {
List result = new ArrayList();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String[] labelNames = new String[columnCount + 1];
int[] types = new int[columnCount + 1];
buildLabelNamesAndTypes(rsmd, labelNames, types);
while (rs.next()) {
Model> ar = modelClass.newInstance();
Map attrs = ar._getAttrs();
for (int i=1; i<=columnCount; i++) {
Object value;
if (types[i] < Types.BLOB) {
value = rs.getObject(i);
} else {
if (types[i] == Types.CLOB) {
value = handleClob(rs.getClob(i));
} else if (types[i] == Types.NCLOB) {
value = handleClob(rs.getNClob(i));
} else if (types[i] == Types.BLOB) {
value = handleBlob(rs.getBlob(i));
} else {
value = rs.getObject(i);
}
}
attrs.put(labelNames[i], value);
}
if (func == null) {
result.add((T)ar);
} else {
if ( ! func.apply((T)ar) ) {
break ;
}
}
}
return result;
}
public void buildLabelNamesAndTypes(ResultSetMetaData rsmd, String[] labelNames, int[] types) throws SQLException {
for (int i=1; i List build(ResultSet rs, Class extends Model> modelClass) throws SQLException, ReflectiveOperationException {
List result = new ArrayList();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String[] labelNames = getLabelNames(rsmd, columnCount);
while (rs.next()) {
Model> ar = modelClass.newInstance();
Map attrs = ar.getAttrs();
for (int i=1; i<=columnCount; i++) {
Object attrValue = rs.getObject(i);
attrs.put(labelNames[i], attrValue);
}
result.add((T)ar);
}
return result;
}
private static final String[] getLabelNames(ResultSetMetaData rsmd, int columnCount) throws SQLException {
String[] result = new String[columnCount + 1];
for (int i=1; i<=columnCount; i++)
result[i] = rsmd.getColumnLabel(i);
return result;
}
*/
/* backup
@SuppressWarnings({"rawtypes", "unchecked"})
static final List build(ResultSet rs, Class extends Model> modelClass) throws SQLException, ReflectiveOperationException {
List result = new ArrayList();
ResultSetMetaData rsmd = rs.getMetaData();
List labelNames = getLabelNames(rsmd);
while (rs.next()) {
Model> ar = modelClass.newInstance();
Map attrs = ar.getAttrs();
for (String lableName : labelNames) {
Object attrValue = rs.getObject(lableName);
attrs.put(lableName, attrValue);
}
result.add((T)ar);
}
return result;
}
private static final List getLabelNames(ResultSetMetaData rsmd) throws SQLException {
int columCount = rsmd.getColumnCount();
List result = new ArrayList();
for (int i=1; i<=columCount; i++) {
result.add(rsmd.getColumnLabel(i));
}
return result;
}
*/
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy