CallableStatement を使用して Postgres ストアド関数を呼び出す Java アプリケーションでエラーが発生しました。エラーは次のとおりです。
org.postgresql.util.PSQLException: Malformed function or procedure escape syntax at offset 6.
at org.postgresql.jdbc2.AbstractJdbc2Statement.modifyJdbcCall(AbstractJdbc2Statement.java:2390)
at org.postgresql.jdbc2.AbstractJdbc2Statement.<init>(AbstractJdbc2Statement.java:149)
at org.postgresql.jdbc3.AbstractJdbc3Statement.<init>(AbstractJdbc3Statement.java:40)
at org.postgresql.jdbc3g.AbstractJdbc3gStatement.<init>(AbstractJdbc3gStatement.java:26)
at org.postgresql.jdbc3g.Jdbc3gStatement.<init>(Jdbc3gStatement.java:28)
at org.postgresql.jdbc3g.Jdbc3gPreparedStatement.<init>(Jdbc3gPreparedStatement.java:21)
at org.postgresql.jdbc3g.Jdbc3gCallableStatement.<init>(Jdbc3gCallableStatement.java:17)
at org.postgresql.jdbc3g.Jdbc3gConnection.prepareCall(Jdbc3gConnection.java:45)
at org.postgresql.jdbc3.AbstractJdbc3Connection.prepareCall(AbstractJdbc3Connection.java:316)
at org.postgresql.jdbc2.AbstractJdbc2Connection.prepareCall(AbstractJdbc2Connection.java:203)
at com.mchange.v2.c3p0.impl.NewProxyConnection.prepareCall(NewProxyConnection.java:632)
at zm.co.freight.model.user.UserRoleModel.getUserRoleDocumentPermission(UserRoleModel.java:212)
Postgres 9.2、posgresql-9.1-902.jdbc3.jar、および c3p0-0.9.2-pre4.jar 接続プールを使用していることに注意してください。
呼び出したいストアド関数は次のとおりです。
CREATE OR REPLACE FUNCTION user_role_document_permission_select_all_per_user_role(userroleidin integer)
RETURNS refcursor AS
$BODY$
declare
mycurs refcursor;
begin
open mycurs for
select document._name,
document_user_role_permission.viewing,
document_user_role_permission.processing,
document_user_role_permission.editing,
document_user_role_permission.updating,
document_user_role_permission.user_role_id,
document_user_role_permission.document_id
from document_user_role_permission, document where document_user_role_permission.document_id = document.id and document_user_role_permission.user_role_id = userroleidin;
return mycurs;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
呼び出しを行う Java メソッド:
public ArrayList<UserRoleDocumentPermissionDao> getUserRoleDocumentPermission (int userRoleId){
ArrayList<UserRoleDocumentPermissionDao> result = new ArrayList<UserRoleDocumentPermissionDao>();
try{
cstmt = myConnection.prepareCall("{ ? = \"user_role_document_permission_select_all_per_user_role\"(?)}");
cstmt.registerOutParameter(1, Types.OTHER);
cstmt.setInt(2, userRoleId);
cstmt.execute();
rs = (ResultSet) cstmt.getObject(1);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
UserRoleDocumentPermissionDao udao;
while (rs.next()){
udao = new UserRoleDocumentPermissionDao();
for (int i = 1; i <= columnCount; i++){
if (i == 1){ // document name
udao.setDocumentName(rs.getString(i));
} else if (i == 2){ // view
udao.setViewing(rs.getBoolean(i));
} else if (i == 3){ // process
udao.setProcessing(rs.getBoolean(i));
} else if (i == 4){ // edit
udao.setEditing(rs.getBoolean(i));
} else if (i == 5){ // update
udao.setUpdating(rs.getBoolean(i));
} else if (i == 6){ // userRoleId
udao.setUserRoleId(rs.getInt(i));
} else if (i == 7){ // documentId
udao.setDocumentId(rs.getLong(i));
}
}
result.add(udao);
}
} catch (SQLException e){
System.out.println("SQL Exception: ");
e.printStackTrace();
} finally {
closeConnection();
}
return result;
}
スーパークラスからのいくつかのグローバル変数
protected Connection myConnection;
protected Statement stmt;
protected CallableStatement cstmt;
protected ResultSet rs;
DAO:
public class UserRoleDocumentPermissionDao {
private String documentName;
private boolean viewing;
private boolean processing;
private boolean editing;
private boolean updating;
private int userRoleId;
private Long documentId;
public UserRoleDocumentPermissionDao(){}
public UserRoleDocumentPermissionDao(String docName, boolean view,
boolean process, boolean edit, boolean update, int userRoleIdin, Long docId){
this.documentName = docName;
this.viewing = view;
this.processing = process;
this.editing = edit;
this.updating = update;
this.userRoleId = userRoleIdin;
this.documentId = docId;
}
/////// getters and setters
なぜこれが起こっているのか、私には考えがありません。refcursor を返す Postgres Stored Function から結果を正常に取得できましたが、その関数/関数呼び出しとこれとの違いを理解できないようです。
どんなアイデアでも大歓迎です