【发布时间】:2020-07-20 14:29:34
【问题描述】:
我创建了一个 PostgreSQL 函数,该函数在后端进行了测试,它按预期工作。但是,当我尝试通过 Scala 模块调用它时,它说该函数不存在。
Function:
create or replace function testing.compareData(ab integer, b json, tablename varchar) RETURNS void as $$
DECLARE
actualTableName varchar := tablename;
histTableName varchar:= actualTableName ||'_hist';
job_id Integer:=0;
begin --<<<< HERE
set search_path to testing; -- Set the schema name
execute 'SELECT id FROM '||actualTableName||' WHERE id =$1' into job_id using ab;
-- if there is data for id in the table then perform below operations
if job_id is not null then
execute FORMAT('INSERT INTO %I select * from %I where id = $1',histTableName,actualTableName) USING ab;
execute FORMAT('DELETE FROM %I where id = $1',actualTableName) USING ab;
EXECUTE FORMAT('INSERT INTO %I values($1,$2)',actualTableName) USING ab,b;
-- if id is not present then create a new record in the actualTable
ELSE
EXECUTE FORMAT('INSERT INTO %I values($1,$2)',actualTableName) USING ab,b;
END IF;
END; --<<<< END HERE
$$ LANGUAGE plpgsql;
Callable Statement方式:
def callingStoredProcedure(message: String, id: Integer, resourceType: String): Unit = {
val connectionUrl: String = ReadingConfig.postgreDBDetails().get("url").getOrElse("None")
var conn: Connection = null
var callableStatement: CallableStatement = null
try {
conn = DriverManager.getConnection(connectionUrl)
callableStatement = conn.prepareCall("{ call testing.compareData( ?,?,? ) }")
callableStatement.setString(1, message)
callableStatement.setInt(2, id)
callableStatement.setString(3, resourceType)
callableStatement.execute()
} catch {
case up: Exception =>
throw up
} finally {
conn.close()
}
}
Prepared Statement方式:
def callDataCompareAndInsertFunction(message: String, id: Integer, resourceType: String): Unit = {
val connectionUrl: String = ReadingConfig.postgreDBDetails().get("url").getOrElse("None")
var pstmt: PreparedStatement = null
var conn: Connection = null
try {
conn = DriverManager.getConnection(connectionUrl)
pstmt = conn.prepareStatement("select testing.compareData(?,?,?)")
pstmt.setInt(1, id)
pstmt.setString(2, message)
pstmt.setString(3, resourceType)
pstmt.executeQuery()
}
catch {
case e: Exception => throw e
}
finally {
conn.close()
}
}
这里,testing 是我创建函数的架构。当使用这两种方式运行时,它会抛出以下错误:
Exception in thread "main" org.postgresql.util.PSQLException: ERROR: function testing.comparedata(character varying, integer, character varying) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
【问题讨论】:
-
请edit您的问题并添加您用于创建函数的确切
create function语句 -
新增功能。请看一眼
-
会推荐使用一些 Scala DB 库
-
你能推荐一些吗?
标签: postgresql scala function stored-procedures