What are '$$' used for in PL/pgSQL

守給你的承諾、 提交于 2019-11-26 00:33:45

问题


Being completely new to PL/pgSQL , what is the meaning of double dollar signs in this function:

CREATE OR REPLACE FUNCTION check_phone_number(text)
RETURNS boolean AS $$
BEGIN
  IF NOT $1 ~  e\'^\\\\+\\\\d{3}\\\\ \\\\d{3} \\\\d{3} \\\\d{3}$\' THEN
    RAISE EXCEPTION \'Wrong formated string \"%\". Expected format is +999 999\';
  END IF;
  RETURN true; 
END;
$$ LANGUAGE plpgsql STRICT IMMUTABLE;

I\'m guessing that, in RETURNS boolean AS $$, $$ is a placeholder.

The last line is a bit of a mystery: $$ LANGUAGE plpgsql STRICT IMMUTABLE;

By the way, what does the last line mean?


回答1:


The dollar signs are used for dollar quoting and are in no way specific to function definitions. It can be used to replace single quotes practically anywhere in SQL scripts.

The body of a function happens to be a string literal which has to be enclosed in single quotes. Dollar-quoting is a PostgreSQL-specific substitute for single quotes to avoid quoting issues inside the function body. You could write your function definition with single-quotes just as well. But then you'd have to escape all single-quotes in the body:

CREATE OR REPLACE FUNCTION check_phone_number(text)
RETURNS boolean AS
'
BEGIN
  IF NOT $1 ~  e''^\\+\\d{3}\\ \\d{3} \\d{3} \\d{3}$'' THEN
    RAISE EXCEPTION ''Malformed string "%". Expected format is +999 999'';
  END IF;
  RETURN true; 
END
' LANGUAGE plpgsql STRICT IMMUTABLE;

This isn't such a good idea. Use dollar-quoting instead, more specifically also put a token between the $$ to make it unique - you might want to use $-quotes inside the function body, too. I do that a lot, actually.

CREATE OR REPLACE FUNCTION check_phone_number(text)
  RETURNS boolean  
AS
$func$
BEGIN
 ...
END
$func$  LANGUAGE plpgsql STRICT IMMUTABLE;

Details:

  • Insert text with single quotes in PostgreSQL

As to your second question:
Read the most excellent manual on CREATE FUNCTION to understand the last line of your example.




回答2:


The $$ is a delimiter you use to to indicate where the function definition starts and ends. Consider the following,

CREATE TABLE <name> <definition goes here> <options go here, eg: WITH OIDS>

The create function syntax is similar, but because you are going to use all sorts of SQL in your function (especially the end of statement; character), the parser would trip if you didn't delimit it. So you should read your statement as:

CREATE OR REPLACE FUNCTION check_phone_number(text)
RETURNS boolean AS <code delimited by $$> LANGUAGE plpgsql STRICT IMMUTABLE;

The stuff after the actual definition are options to give the database more information about your function, so it can optimize its usage.

In fact if you look under "4.1.2.2. Dollar-Quoted String Constants" in the manual, you will see that you can even use characters in between the dollar symbols and it will all count as one delimiter.



来源:https://stackoverflow.com/questions/12144284/what-are-used-for-in-pl-pgsql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!