MySQL的HEX()和UNHEX()等同于Postgres?

Nee*_*imo 9 mysql sql postgresql

我正在将一些工具转换为使用MySQL到PostgreSQL的工具.有了这个,我遇到了很多问题,但能够找到最重要的一切.我遇到问题的是HEX()UNHEX().我试着encode(%s, 'hex')decode(%s, 'hex')它没有真正停止使我有错误,但它仍然似乎没有这样的伎俩.有没有人知道Postgres中这些函数的等价物是什么?

这是旧的MySQL查询:

SELECT HEX(test_table.hash),
       title,
       user,
       reason,
       description,
       url,
       performed,
       comment,
       authenticated,
       status
FROM alerts
JOIN user_responses ON test_table.hash = user_responses.hash
JOIN test_status ON test_table.hash = test_status.hash
WHERE status = %s
Run Code Online (Sandbox Code Playgroud)

以下是PostgreSQL格式的更新查询:

SELECT encode(test_table.hash, 'hex') as hash,
       title,
       user,
       reason,
       description,
       url,
       performed,
       comment,
       authenticated,
       status
FROM test_table
JOIN user_responses ON test_table.hash = user_responses.hash
JOIN test_status ON test_table.hash = test_status.hash
WHERE status = %s
Run Code Online (Sandbox Code Playgroud)

谢谢!

Abe*_*sto 9

create function hex(text) returns text language sql immutable strict as $$
  select encode($1::bytea, 'hex')
$$;

create function hex(bigint) returns text language sql immutable strict as $$
  select to_hex($1)
$$;

create function unhex(text) returns text language sql immutable strict as $$
  select encode(decode($1, 'hex'), 'escape')
$$;


select hex('abc'), hex(123), unhex(hex('PostgreSQL'));
Run Code Online (Sandbox Code Playgroud)

结果:

?????????????????????????????
?  hex   ? hex ?   unhex    ?
?????????????????????????????
? 616263 ? 7b  ? PostgreSQL ?
?????????????????????????????