Eu criei uma função PostgreSQL que é testada no lado de back-end e funciona como esperado. No entanto, quando estou tentando chamá-lo através do módulo Scala, ele diz que a função não existe.
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
Maneira:
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
maneira:
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()
}
}
Aqui testing
está o meu esquema no qual a função é criada. Quando executado usando as duas maneiras que ele lança abaixo do erro:
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.
Bem, seu primeiro parâmetro não é uma string, portanto, a chamada setString(1, ...)
resultará no erro que você citou na sua pergunta.
Seu segundo parâmetro é declarado json
assim , assim você não pode passar diretamente um valor String também. O seguinte deve funcionar (dada a definição da função):
pstmt = conn.prepareStatement("select testing.compareData(?,cast(? as json),?)")
pstmt.setInt(1, id)
pstmt.setString(2, message)
pstmt.setString(3, resourceType)
Você também pode precisar usar em pstmt.execute()
vez de, executeQuery()
pois sua função não retorna nada.
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras