[persistence]完善ConversionPstParamMapper类SQL参数类型转换功能

This commit is contained in:
datagear 2020-03-18 09:14:11 +08:00
parent 439db82b01
commit c47cc7e8ff
2 changed files with 187 additions and 23 deletions

View File

@ -35,6 +35,7 @@ import org.datagear.persistence.support.expression.SqlExpressionSyntaxErrorExcep
import org.datagear.persistence.support.expression.VariableExpressionErrorException;
import org.datagear.persistence.support.expression.VariableExpressionResolver;
import org.datagear.persistence.support.expression.VariableExpressionSyntaxErrorException;
import org.datagear.util.JdbcSupport;
import org.datagear.util.JdbcUtil;
import org.springframework.core.convert.ConversionService;
import org.springframework.expression.spel.standard.SpelExpressionParser;
@ -175,7 +176,7 @@ public class ConversionPstParamMapper extends PersistenceSupport implements PstP
}
/**
* 将源值转换为可用于{@linkplain PreparedStatement}参数值的对象
* 将源值转换为可用于{@linkplain JdbcSupport#setParamValue(Connection, PreparedStatement, int, org.datagear.util.SqlParamValue)}参数值的对象
*
* @param cn
* @param value
@ -275,6 +276,15 @@ public class ConversionPstParamMapper extends PersistenceSupport implements PstP
pstParam = value;
else if (value instanceof File)
pstParam = this.filePathValueResolver.getInputStream((File) value);
else if (value instanceof String)
{
String v = (String) value;
if (this.filePathValueResolver.isFilePathValue(v))
pstParam = this.filePathValueResolver.getReader(v);
else
pstParam = convertToType(value, InputStream.class);
}
else
pstParam = convertToType(value, InputStream.class);
@ -303,10 +313,6 @@ public class ConversionPstParamMapper extends PersistenceSupport implements PstP
{
if (value instanceof Clob)
pstParam = value;
else if (value instanceof Reader)
pstParam = value;
else if (value instanceof File)
pstParam = this.filePathValueResolver.getReader((File) value);
else if (value instanceof String)
{
String v = (String) value;
@ -314,8 +320,14 @@ public class ConversionPstParamMapper extends PersistenceSupport implements PstP
if (this.filePathValueResolver.isFilePathValue(v))
pstParam = this.filePathValueResolver.getReader(v);
else
pstParam = convertToType(value, Reader.class);
pstParam = value;
}
else if (value instanceof Reader)
pstParam = value;
else if (value instanceof InputStream)
pstParam = value;
else if (value instanceof File)
pstParam = this.filePathValueResolver.getReader((File) value);
else
pstParam = convertToType(value, Reader.class);
@ -326,6 +338,8 @@ public class ConversionPstParamMapper extends PersistenceSupport implements PstP
{
if (value instanceof Blob)
pstParam = value;
else if (value instanceof byte[])
pstParam = value;
else if (value instanceof InputStream)
pstParam = value;
else if (value instanceof File)
@ -359,20 +373,18 @@ public class ConversionPstParamMapper extends PersistenceSupport implements PstP
case Types.LONGNVARCHAR:
{
if (value instanceof String)
pstParam = value;
else if (value instanceof Reader)
pstParam = value;
else if (value instanceof File)
pstParam = this.filePathValueResolver.getReader((File) value);
else if (value instanceof String)
{
String v = (String) value;
if (this.filePathValueResolver.isFilePathValue(v))
pstParam = this.filePathValueResolver.getReader(v);
else
pstParam = convertToType(value, Reader.class);
pstParam = value;
}
else if (value instanceof Reader)
pstParam = value;
else if (value instanceof File)
pstParam = this.filePathValueResolver.getReader((File) value);
else
pstParam = convertToType(value, Reader.class);
@ -383,10 +395,6 @@ public class ConversionPstParamMapper extends PersistenceSupport implements PstP
{
if (value instanceof NClob)
pstParam = value;
else if (value instanceof Reader)
pstParam = value;
else if (value instanceof File)
pstParam = this.filePathValueResolver.getReader((File) value);
else if (value instanceof String)
{
String v = (String) value;
@ -394,8 +402,14 @@ public class ConversionPstParamMapper extends PersistenceSupport implements PstP
if (this.filePathValueResolver.isFilePathValue(v))
pstParam = this.filePathValueResolver.getReader(v);
else
pstParam = convertToType(value, Reader.class);
pstParam = value;
}
else if (value instanceof Reader)
pstParam = value;
else if (value instanceof InputStream)
pstParam = value;
else if (value instanceof File)
pstParam = this.filePathValueResolver.getReader((File) value);
else
pstParam = convertToType(value, Reader.class);
@ -406,10 +420,6 @@ public class ConversionPstParamMapper extends PersistenceSupport implements PstP
{
if (value instanceof SQLXML)
pstParam = value;
else if (value instanceof Reader)
pstParam = value;
else if (value instanceof File)
pstParam = this.filePathValueResolver.getReader((File) value);
else if (value instanceof String)
{
String v = (String) value;
@ -417,8 +427,14 @@ public class ConversionPstParamMapper extends PersistenceSupport implements PstP
if (this.filePathValueResolver.isFilePathValue(v))
pstParam = this.filePathValueResolver.getReader(v);
else
pstParam = convertToType(value, Reader.class);
pstParam = value;
}
else if (value instanceof Reader)
pstParam = value;
else if (value instanceof InputStream)
pstParam = value;
else if (value instanceof File)
pstParam = this.filePathValueResolver.getReader((File) value);
else
pstParam = convertToType(value, Reader.class);

View File

@ -6,8 +6,11 @@ package org.datagear.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Blob;
@ -546,8 +549,34 @@ public class JdbcSupport
{
if (value instanceof Clob)
st.setClob(paramIndex, (Clob) value);
else if (value instanceof String)
{
Clob clob = cn.createClob();
clob.setString(0, (String) value);
st.setClob(paramIndex, clob);
value = clob;
}
else if (value instanceof Reader)
st.setClob(paramIndex, (Reader) value);
else if (value instanceof InputStream)
{
Clob clob = cn.createClob();
OutputStream out = clob.setAsciiStream(0);
write((InputStream) value, out);
st.setClob(paramIndex, clob);
value = clob;
}
else if (value instanceof File)
{
Clob clob = cn.createClob();
OutputStream out = clob.setAsciiStream(0);
write((File) value, out);
st.setClob(paramIndex, clob);
value = clob;
}
else
value = setParamValueExt(cn, st, paramIndex, paramValue);
@ -558,6 +587,14 @@ public class JdbcSupport
{
if (value instanceof Blob)
st.setBlob(paramIndex, (Blob) value);
else if (value instanceof byte[])
{
Blob blob = cn.createBlob();
blob.setBytes(0, (byte[]) value);
st.setBlob(paramIndex, blob);
value = blob;
}
else if (value instanceof InputStream)
st.setBlob(paramIndex, (InputStream) value);
else if (value instanceof File)
@ -590,8 +627,34 @@ public class JdbcSupport
{
if (value instanceof NClob)
st.setNClob(paramIndex, (NClob) value);
else if (value instanceof String)
{
NClob nClob = cn.createNClob();
nClob.setString(0, (String) value);
st.setNClob(paramIndex, nClob);
value = nClob;
}
else if (value instanceof Reader)
st.setNClob(paramIndex, (Reader) value);
else if (value instanceof InputStream)
{
NClob nClob = cn.createNClob();
OutputStream out = nClob.setAsciiStream(0);
write((InputStream) value, out);
st.setNClob(paramIndex, nClob);
value = nClob;
}
else if (value instanceof File)
{
NClob nClob = cn.createNClob();
OutputStream out = nClob.setAsciiStream(0);
write((File) value, out);
st.setNClob(paramIndex, nClob);
value = nClob;
}
else
value = setParamValueExt(cn, st, paramIndex, paramValue);
@ -602,6 +665,41 @@ public class JdbcSupport
{
if (value instanceof SQLXML)
st.setSQLXML(paramIndex, (SQLXML) value);
else if (value instanceof String)
{
SQLXML sqlxml = cn.createSQLXML();
sqlxml.setString((String) value);
st.setSQLXML(paramIndex, sqlxml);
value = sqlxml;
}
else if (value instanceof Reader)
{
SQLXML sqlxml = cn.createSQLXML();
Writer out = sqlxml.setCharacterStream();
write((Reader) value, out);
st.setSQLXML(paramIndex, sqlxml);
value = sqlxml;
}
else if (value instanceof InputStream)
{
SQLXML sqlxml = cn.createSQLXML();
OutputStream out = sqlxml.setBinaryStream();
write((InputStream) value, out);
st.setSQLXML(paramIndex, sqlxml);
value = sqlxml;
}
else if (value instanceof File)
{
SQLXML sqlxml = cn.createSQLXML();
OutputStream out = sqlxml.setBinaryStream();
write((File) value, out);
st.setSQLXML(paramIndex, sqlxml);
value = sqlxml;
}
else
value = setParamValueExt(cn, st, paramIndex, paramValue);
@ -631,6 +729,56 @@ public class JdbcSupport
throw new UnsupportedOperationException("Set JDBC [" + paramValue.getType() + "] type value is not supported");
}
protected void write(File file, OutputStream out) throws SQLException
{
try
{
IOUtil.write(file, out);
}
catch(IOException e)
{
throw new SQLException(e);
}
finally
{
IOUtil.close(out);
}
}
protected void write(InputStream in, OutputStream out) throws SQLException
{
try
{
IOUtil.write(in, out);
}
catch(IOException e)
{
throw new SQLException(e);
}
finally
{
IOUtil.close(in);
IOUtil.close(out);
}
}
protected void write(Reader in, Writer out) throws SQLException
{
try
{
IOUtil.write(in, out);
}
catch(IOException e)
{
throw new SQLException(e);
}
finally
{
IOUtil.close(in);
IOUtil.close(out);
}
}
protected InputStream getInputStreamForSql(File file) throws SQLException
{
try