Re: Concatenating bytea types...

Поиск
Список
Период
Сортировка
От Richard Huxton
Тема Re: Concatenating bytea types...
Дата
Msg-id 5130BBE7.3030201@archonet.com
обсуждение исходный текст
Ответ на Concatenating bytea types...  (Marko Rihtar <rihtar.marko@gmail.com>)
Список pgsql-sql
On 28/02/13 10:21, Marko Rihtar wrote:
> Hi all,
>
> i have a little problem.
> I'm trying to rewrite one procedure from mysql that involves bytes
> concatenation.
> This is my snippet from postgres code:

You seem to be mixing up escape and hex literal formatting along with 
decode(). The following should help.

BEGIN;

CREATE FUNCTION to_hex_pair(int) RETURNS text AS $$    SELECT right('0' || to_hex($1), 2);
$$ LANGUAGE sql;

CREATE FUNCTION f_concat_bytea1(bytea, bytea) RETURNS text AS $$
BEGIN    RETURN $1 || $2;
END;
$$ LANGUAGE plpgsql;

CREATE FUNCTION f_concat_bytea2(bytea, bytea) RETURNS bytea AS $$
DECLARE    cv1 bytea;
BEGIN    cv1 := '\x01'::bytea;    RETURN $1 || cv1 || $2;
END;
$$ LANGUAGE plpgsql;

CREATE FUNCTION f_concat_bytea3(text, text) RETURNS bytea AS $$
DECLARE    cv1 text;
BEGIN    cv1 := '01';    RETURN decode($1 || cv1 || $2, 'hex');
END;
$$ LANGUAGE plpgsql;

SELECT '\x000b'::bytea             AS want_this,       decode('000b','hex')        AS or_this1,
decode('\000\013','escape')AS or_this2;
 
SELECT f_concat_bytea1('\x00', '\x0b');
SELECT f_concat_bytea2('\x00', '\x0b');
SELECT f_concat_bytea3('00', '0b');

ROLLBACK;



--   Richard Huxton  Archonet Ltd



В списке pgsql-sql по дате отправления:

Предыдущее
От: Mark Stosberg
Дата:
Сообщение: Re: Need help revoking access WHERE state = 'deleted'
Следующее
От: Jasen Betts
Дата:
Сообщение: Re: Concatenating bytea types...