📄 sqljdbcutil.java
字号:
char[] inCharBuffer = new char[CHAR_BUFFER_SIZE];
StringBuffer strBuf = new StringBuffer();
int charsRead = 0;
try {
while ((charsRead = valueReader.read(inCharBuffer, 0, CHAR_BUFFER_SIZE)) > 0) {
strBuf.append(inCharBuffer, 0, charsRead);
}
valueReader.close();
} catch (IOException e) {
throw new GenericEntityException("Error reading long character stream for field " + curField.getName() + " of entity " + entity.getEntityName(), e);
}
entity.dangerousSetNoCheckButFast(curField, strBuf.toString());
} else {
entity.dangerousSetNoCheckButFast(curField, null);
}
} else {
String value = rs.getString(ind);
entity.dangerousSetNoCheckButFast(curField, value);
}
break;
case 2:
entity.dangerousSetNoCheckButFast(curField, rs.getTimestamp(ind));
break;
case 3:
entity.dangerousSetNoCheckButFast(curField, rs.getTime(ind));
break;
case 4:
entity.dangerousSetNoCheckButFast(curField, rs.getDate(ind));
break;
case 10:
Object obj = null;
InputStream binaryInput = null;
byte[] fieldBytes = rs.getBytes(ind);
if (fieldBytes != null && fieldBytes.length > 0) {
binaryInput = new ByteArrayInputStream(fieldBytes);
}
if (fieldBytes != null && fieldBytes.length <= 0) {
Debug.logWarning("Got bytes back for Object field with length: " + fieldBytes.length + " while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + "): ", module);
}
//alt 1: binaryInput = rs.getBinaryStream(ind);
//alt 2: Blob blobLocator = rs.getBlob(ind);
//if (blobLocator != null) {
// binaryInput = blobLocator.getBinaryStream();
//}
if (binaryInput != null) {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(binaryInput);
obj = in.readObject();
} catch (IOException ex) {
throw new GenericDataSourceException("Unable to read BLOB data from input stream while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + "): " + ex.toString(), ex);
} catch (ClassNotFoundException ex) {
throw new GenericDataSourceException("Class not found: Unable to cast BLOB data to an Java object while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + "): " + ex.toString(), ex);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
throw new GenericDataSourceException("Unable to close binary input stream while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + "): " + e.toString(), e);
}
}
}
}
binaryInput = null;
entity.dangerousSetNoCheckButFast(curField, obj);
break;
case 11:
entity.dangerousSetNoCheckButFast(curField, rs.getBlob(ind));
break;
case 12:
entity.dangerousSetNoCheckButFast(curField, rs.getClob(ind));
break;
}
} else {
switch (typeValue) {
case 5:
int intValue = rs.getInt(ind);
if (rs.wasNull()) {
entity.dangerousSetNoCheckButFast(curField, null);
} else {
entity.dangerousSetNoCheckButFast(curField, new Integer(intValue));
}
break;
case 6:
long longValue = rs.getLong(ind);
if (rs.wasNull()) {
entity.dangerousSetNoCheckButFast(curField, null);
} else {
entity.dangerousSetNoCheckButFast(curField, new Long(longValue));
}
break;
case 7:
float floatValue = rs.getFloat(ind);
if (rs.wasNull()) {
entity.dangerousSetNoCheckButFast(curField, null);
} else {
entity.dangerousSetNoCheckButFast(curField, new Float(floatValue));
}
break;
case 8:
double doubleValue = rs.getDouble(ind);
if (rs.wasNull()) {
entity.dangerousSetNoCheckButFast(curField, null);
} else {
entity.dangerousSetNoCheckButFast(curField, new Double(doubleValue));
}
break;
case 9:
boolean booleanValue = rs.getBoolean(ind);
if (rs.wasNull()) {
entity.dangerousSetNoCheckButFast(curField, null);
} else {
entity.dangerousSetNoCheckButFast(curField, new Boolean(booleanValue));
}
break;
}
}
} catch (SQLException sqle) {
throw new GenericDataSourceException("SQL Exception while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + ")", sqle);
}
}
public static void setValue(SQLProcessor sqlP, ModelField modelField, GenericEntity entity, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException {
Object fieldValue = entity.dangerousGetNoCheckButFast(modelField);
setValue(sqlP, modelField, entity.getEntityName(), fieldValue, modelFieldTypeReader);
}
public static void setValue(SQLProcessor sqlP, ModelField modelField, String entityName, Object fieldValue, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException {
ModelFieldType mft = modelFieldTypeReader.getModelFieldType(modelField.getType());
if (mft == null) {
throw new GenericModelException("GenericDAO.getValue: definition fieldType " + modelField.getType() + " not found, cannot setValue for field " +
entityName + "." + modelField.getName() + ".");
}
String fieldType = mft.getJavaType();
if (fieldValue != null) {
if (!ObjectType.instanceOf(fieldValue, fieldType)) {
// this is only an info level message because under normal operation for most JDBC
// drivers this will be okay, but if not then the JDBC driver will throw an exception
// and when lower debug levels are on this should help give more info on what happened
Class fieldClass = fieldValue.getClass();
String fieldClassName = fieldClass.getName();
if (Debug.verboseOn()) Debug.logVerbose("type of field " + entityName + "." + modelField.getName() +
" is " + fieldClassName + ", was expecting " + mft.getJavaType() + "; this may " +
"indicate an error in the configuration or in the class, and may result " +
"in an SQL-Java data conversion error. Will use the real field type: " +
fieldClassName + ", not the definition.", module);
fieldType = fieldClassName;
}
}
try {
int typeValue = getType(fieldType);
switch (typeValue) {
case 1:
String strValue = (String) fieldValue;
sqlP.setValue(strValue);
break;
case 2:
sqlP.setValue((java.sql.Timestamp) fieldValue);
break;
case 3:
sqlP.setValue((java.sql.Time) fieldValue);
break;
case 4:
sqlP.setValue((java.sql.Date) fieldValue);
break;
case 5:
sqlP.setValue((java.lang.Integer) fieldValue);
break;
case 6:
sqlP.setValue((java.lang.Long) fieldValue);
break;
case 7:
sqlP.setValue((java.lang.Float) fieldValue);
break;
case 8:
sqlP.setValue((java.lang.Double) fieldValue);
break;
case 9:
sqlP.setValue((java.lang.Boolean) fieldValue);
break;
case 10:
sqlP.setBinaryStream(fieldValue);
break;
case 11:
sqlP.setValue((java.sql.Blob) fieldValue);
break;
case 12:
sqlP.setValue((java.sql.Clob) fieldValue);
break;
}
} catch (SQLException sqle) {
throw new GenericDataSourceException("SQL Exception while setting value (" + modelField.getName() + "): ", sqle);
}
}
protected static Map fieldTypeMap = new HashMap();
static {
fieldTypeMap.put("java.lang.String", new Integer(1));
fieldTypeMap.put("String", new Integer(1));
fieldTypeMap.put("java.sql.Timestamp", new Integer(2));
fieldTypeMap.put("Timestamp", new Integer(2));
fieldTypeMap.put("java.sql.Time", new Integer(3));
fieldTypeMap.put("Time", new Integer(3));
fieldTypeMap.put("java.sql.Date", new Integer(4));
fieldTypeMap.put("Date", new Integer(4));
fieldTypeMap.put("java.lang.Integer", new Integer(5));
fieldTypeMap.put("Integer", new Integer(5));
fieldTypeMap.put("java.lang.Long", new Integer(6));
fieldTypeMap.put("Long", new Integer(6));
fieldTypeMap.put("java.lang.Float", new Integer(7));
fieldTypeMap.put("Float", new Integer(7));
fieldTypeMap.put("java.lang.Double", new Integer(8));
fieldTypeMap.put("Double", new Integer(8));
fieldTypeMap.put("java.lang.Boolean", new Integer(9));
fieldTypeMap.put("Boolean", new Integer(9));
fieldTypeMap.put("java.lang.Object", new Integer(10));
fieldTypeMap.put("Object", new Integer(10));
fieldTypeMap.put("java.sql.Blob", new Integer(11));
fieldTypeMap.put("Blob", new Integer(11));
fieldTypeMap.put("java.sql.Clob", new Integer(12));
fieldTypeMap.put("Clob", new Integer(12));
}
public static int getType(String fieldType) throws GenericNotImplementedException {
Integer val = (Integer) fieldTypeMap.get(fieldType);
if (val == null) {
throw new GenericNotImplementedException("Java type " + fieldType + " not currently supported. Sorry.");
}
return val.intValue();
}
public static void addValue(StringBuffer buffer, ModelField field, Object value, List params) {
if (field != null) {
buffer.append('?');
} else {
buffer.append('\'').append(value).append('\'');
}
if (field != null && params != null) params.add(new EntityConditionParam(field, value));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -