I would like to create a QUOTENAME() function in mySQL like the one that exists in M$ SQL Server.
This is what it does:
QUOTENAME returns a Unicode string with the delimiters added to make
the input string a valid identifier. The QUOTENAME function uses this
syntax:
QUOTENAME ( 'string' [ , 'delimiter' ] )
You pass QUOTENAME a string to be delimited and a one-character string
to use as the delimiter. The delimiter can be a square bracket or a
single or double quotation mark.
Is this even possible?
You could start with something like this - building off of eggyal's comment
DROP FUNCTION IF EXISTS QUOTENAME;
CREATE FUNCTION QUOTENAME (s varchar(50), d CHAR(1))
RETURNS VARCHAR(52)
RETURN CONCAT (d, REPLACE(s, d, CONCAT(d,d)), d);
And then add your special cases and additional functionality.
Related
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 ?
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
Could someone please help me with this one?
So I need to write a user input function in which I need to concatenate two strings. When outputted, there must be a space between the two strings, note there is not a space in the two strings when inputting them. Test functions with the following, String 1: Spring, String 2: Break!
This is my solution:
create function concatenate(X CHAR,Y CHAR)
Returns CHAR(50)
Return concat(X, ' ', Y);
select concatenate('Spring','Break')
However, the problem is that sql only returns the first letter of each word, which is "S B". But I want it to be "Spring Break"
Any ideas on this one? Helps are very appreciated
Supply a length for the input parameters as well:
create function concatenate(X CHAR(24),Y CHAR(24))
Returns CHAR(50)
Return concat(X, ' ', Y);
select concatenate('Spring','Break')
You need to define the size when you declare the argument.
create function con(X char(50), Y char(50))
returns char(100)
You have to specify the size of CHAR(), otherwise it will use the default CHAR(1), and you can't get want you want.
eg:
create function hello(x char(10),y char(10))
returns char(30) deterministic
return concat(x,' ',y)`
select hello('Hello','World');
Hello World
I have a MySQL function which does some string replacement in a specified string. I simplyfied this function to return it's argument.
CREATE FUNCTION replace_details(
id INT UNSIGNED,
message VARCHAR(1000))
RETURNS VARCHAR(1000) LANGUAGE SQL
BEGIN
RETURN message;
END;
;;
This function is called from before insert trigger, so the inserted string is modified. Everything works as expected while I'm using english text, but as soon as I'm trying to insert something else (for example russian) it makes my string unreadable. Actually I get only ? sign in a database. If I'm using any other function in trigger it works fine
For example:
SET NEW.message = CONCAT(NEW.message, ' test');
but as soon as I'm using my custom function which actually does nothing, but returns what it got, inserted string becomes unreadable.
I guess I need to somehow specify that my function works with unicode, but how?
MySQL server version: 5.7.1-m11
I found it.
CREATE FUNCTION replace_details(
id INT UNSIGNED,
message VARCHAR(1000) CHARACTER SET UTF8)
RETURNS VARCHAR(1000) CHARACTER SET UTF8 LANGUAGE SQL
BEGIN
RETURN message;
END;
;;
works!
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.