Passing dynamic arguments to gman_do mysql udf function - mysql

I am trying to use gearman's mysql udf functions (version 0.6) .
When i call it like this :
select gman_do_background("test", "_", "_");
or even
select gman_do_background(CONCAT("te","st"), "_", "_");
It works, and my worker "test" procedure is triggered.
But if i try to do :
CREATE DEFINER PROCEDURE `CallGearmanBackground`(IN `jobtocall` VARCHAR(70))
BEGIN select gman_do_background(jobtocall, "_", "_" );
END
and then
call CallGearmanBackground("test");
I get this error :
Can't initialize function 'gman_do_background'; First argument must be
a string.
I tried several type for the procedure argument (VARBINARY, TEXT, BLOB ) ... all trigger the same error.
Any way around this ?

Related

oracle pl/sql exception handling for input parameter (bad argument i.e. non numeric value for number)

I have stored procedure with input parameter of type number.
CREATE OR REPLACE PROCEDURE my_procedure (p_x number)
AS
I included exception handling code as below, but that do not handle following:
execute my_procedure ('sads')
EXCEPTION
WHEN VALUE_ERROR THEN
DBMS_OUTPUT.PUT_LINE('SQLERRM: ' || SQLERRM);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('SQLERRM: ' || SQLERRM);
is there any way to change exception for bad arguments?
The error will happen when the procedure is invoked, not inside of the procedure so you can't capture the error as an exception within that procedure itself. A possible solution would be use an additional procedure that validates the arguments. In the example code below the procedure so_arg_test.my_procedure accepts a VARCHAR2 but it will only invoke my_procedure_hidden if the argument actually is a number. For anything other it will raise a value error.
CREATE OR REPLACE PACKAGE so_arg_test AS
PROCEDURE my_procedure (
p_x IN VARCHAR2
);
END so_arg_test;
/
CREATE OR REPLACE
PACKAGE BODY SO_ARG_TEST AS
procedure my_procedure_hidden (p_x IN NUMBER) AS
BEGIN
-- your code
dbms_output.put_line('Inside my_procedure_hidden');
END my_procedure_hidden;
procedure my_procedure (p_x IN VARCHAR2)
AS
BEGIN
IF VALIDATE_CONVERSION(p_x AS NUMBER) = 0 THEN
RAISE VALUE_ERROR;
END IF;
my_procedure_hidden (p_x => my_procedure.p_x);
END my_procedure;
END SO_ARG_TEST;
/

r2dbc:Calling Stored Proc in mysql with in and out parameters

Does r2dbc currently supports calling Stored proc in my sql with in/out parameters ?
I am trying to call Stored Proc using databaseClient which returns a String.But I am getting the below exception.Do I have to add anything to call SP from r2dbc.My stored procedure looks like this.How to call SP using database-client
CREATE PROCEDURE usp_get_data(in someId varchar(255),
in someName varchar(255),
out email varchar(255))
BEGIN
SELECT email FROM products where id=:someId and name=:someName LIMIT 1;
END //
Below is my code.
String STORED_PROC = "call usp_get_data(:someId,:someName)"
def result = databaseClient.execute(STORED_PROC)
.bindNull("someId","someId")
.bindNull("someName", "someName")
.as(String.class)
.fetch().one()
Incorrect number of arguments for PROCEDURE usp_get_data expected 3, got 2))
Forgot to add #out in the call stored procedure statement.
modifying my execution below worked.
String STORED_PROC = "call usp_get_data(:someId,:someName,#email);
Select #email"

Token "(" is invalid error calling a function that assigns into a custom type containing a json type

I'm decoding a JWT with Postgresql. The function test is a simplified version of what it actually does. Here it serves to show an error message that I'm getting too.
CREATE TYPE parts AS (header json, payload json, valid boolean);
CREATE OR REPLACE FUNCTION verify(token text, secret text, algorithm text DEFAULT 'HS256')
RETURNS parts AS $$
SELECT
'{"alg": "HS256","typ": "JWT"}'::json AS header,
'{"id": 1, "exp": 1524683318}'::json AS payload,
TRUE AS valid;
$$ LANGUAGE sql;
CREATE OR REPLACE FUNCTION test(token text)
RETURNS parts AS $$
DECLARE
jwt_parts parts;
BEGIN
SELECT verify(test.token, 'secret')
INTO jwt_parts;
RETURN jwt_parts;
END
$$ LANGUAGE plpgsql;
After running SELECT test('xx') this is the error I'm getting (on Postgres v10.1):
ERROR: invalid input syntax for type json
DETAIL: Token "(" is invalid.
CONTEXT: JSON data, line 1: (...
PL/pgSQL function test(text) line 5 at SQL statement
I would like to SELECT ... INTO my custom parts type. I have tried many ways to rephrase, but I suspect this can be done much simpler than what I've tried so far.
PS: The real function test(token text) I'm using is larger and needs the plpgsql language.
PS2: Just to make sure that the currently given answers make sense to people visiting this question. The original version of my question had a test function like this:
CREATE OR REPLACE FUNCTION test(token text)
RETURNS parts AS $$
DECLARE
jwt_parts parts;
BEGIN
SELECT ('{"alg": "HS256","typ": "JWT"}', '{"id": 1, "exp": 1524683318}', TRUE)
INTO jwt_parts;
RETURN jwt_parts;
END
$$ LANGUAGE plpgsql;
The parentheses cause you to select a single column (of type row), where you actually need three columns. For example:
select (1,2,3)
>>
row
--------
(1,2,3)
(1 row)
So omit the parentheses after select:
SELECT ('{"alg": "HS256","typ": "JWT"}', '{"id": 1, "exp": 1524683318}', TRUE)
^^^ ^^^
In reply to your comment, the error in your new query comes from:
SELECT verify('xxx', 'yyy')
INTO jwt_parts;
This again tries to assign a single column to a three-column type. Instead, you can use:
SELECT * FROM verify('xxx', 'yyy')
INTO jwt_parts;
You shouldn't select a tuple but a regular row (remove parenthesis):
SELECT '{"alg": "HS256","typ": "JWT"}', '{"id": 1, "exp": 1524683318}', TRUE

hsqldb procedure with date Input parameter

I need to write a test for some download operation. This operation call procedure from MSSQL database, take result set and java make some stuf. For test I use hsqldb.
My procedure:
CREATE PROCEDURE map.Get1(IN packageName varchar(100),
IN downloadDate DATE)
READS SQL DATA DYNAMIC RESULT SETS 1 BEGIN ATOMIC
DECLARE result CURSOR WITH RETURN FOR SELECT * FROM map.tvschedule FOR READ ONLY;
OPEN result;
END
This procedure wan't work, i have an exception
call map.GET1('Genre','2018-03-10');
[42561][-5561] incompatible data type in conversion
java.lang.RuntimeException: org.hsqldb.HsqlException: incompatible data type
in conversion
But this(without date parameter) work well:
CREATE PROCEDURE map.Get1(IN packageName varchar(100))
READS SQL DATA DYNAMIC RESULT SETS 1 BEGIN ATOMIC
DECLARE result CURSOR WITH RETURN FOR SELECT * FROM map.tvschedule FOR READ ONLY;
OPEN result;
END
call map.GET1('Genre');
first needed row
second needed row
I am not going to use input parameter, but i need this procedure to be looking i am going to.
My question is How to use date input parameter with hsqldb procedures?
UPDATE1:
I used TO_DATE and now it works well, but i have no data in my result set, my java code is:
try (CallableStatement callableStatement = connection.prepareCall("{ call
map.GetGenreProtocol( ?, ? ) }")) {
callableStatement.setString(1, packageName);
callableStatement.setDate(2, date);
callableStatement.execute();
ResultSet resultSet = callableStatement.getResultSet();
while (resultSet.next()) {
Interval Interval = new Interval();
Interval.setDuration(resultSet.getInt("duration"));
Interval.setMappingTargetId(resultSet.getInt("mappingTargetId"));
Interval.setGenreId(resultSet.getInt("genreId"));
Interval.setStart(resultSet.getLong("start"));
Interval.setCategoryId(resultSet.getInt("categoryId"));
Interval.setCategoryName(resultSet.getString("categoryName"));
Interval.setGenreName(resultSet.getString("genreName"));
Interval.setDescription(resultSet.getString("description"));
Intervals.add(Interval);
}
}
Use the TO_DATE function.
For example:
call map.GET1('Genre', TO_DATE('2018-03-10', 'YYYY-MM-DD'));
I guess you need to create a function that returns a table instead of a procedure:
CREATE FUNCTION map.Get1(IN packageName VARCHAR(100),
IN downloadDate DATE)
RETURNS TABLE(.....)
READS SQL DATA
BEGIN ATOMIC
....
END;

How to get Postgres function name as well as function specific name from pg_catalog.pg_proc?

Since Postgres supports function overloading, getting function name as well as function specific name(System generated without duplicates) is more meaning full.
Assume i have 2 functions in the name as Func1 which are overloaded as shown below,
CREATE FUNCTION "Schema"."Func1"(IN param1 INTEGER,
IN Param2 CHAR)
RETURNS INTEGER
AS $BODY$
begin
return param1+1;
end $BODY$
LANGUAGE PLPGSQL;#
CREATE FUNCTION "Schema"."Func1"(IN param1 INTEGER)
RETURNS INTEGER
AS $BODY$
begin
return param1+1;
end $BODY$
LANGUAGE PLPGSQL;#
How do i load the functions as well as input parameters correctly from pg_catalog.pg_proc.
With the help of information_schema.routines, there is a way to load function 1)specific_name 2) routine_name
But many other attributes are missing in information_schema.routines like 1) isWindow function 2) isStrict function 3) isProRetSet function
So is there some other means to get the function specific_name from pg_catalog.....
A general method is to use psql -E or set ECHO_HIDDEN inside psql and look at the queries it generates for backslash commands.
For instance, \df "Func1" produces this with PostgreSQL 9.1:
SELECT n.nspname as "Schema",
p.proname as "Name",
pg_catalog.pg_get_function_result(p.oid) as "Result data type",
pg_catalog.pg_get_function_arguments(p.oid) as "Argument data types",
CASE
WHEN p.proisagg THEN 'agg'
WHEN p.proiswindow THEN 'window'
WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger'
ELSE 'normal'
END as "Type"
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE p.proname ~ '^(Func1)$'
AND pg_catalog.pg_function_is_visible(p.oid)
ORDER BY 1, 2, 4;
which gives you directions about how to get the different function signatures associated to the name "Func1"
The same with \df+ would lead to the other attributes, like volatility.