PL/pgSQL风格指南

9

互联网上有许多针对不同语言的样式指南,例如CSSJavascriptRubyRails等。

但是我在哪里可以找到关于PostgreSQL数据库系统的过程性语言PL/pgSQL的好的现代样式指南呢?

我也希望能有像ruby的rubocop一样的自动代码分析器。


2
我曾经遵循过PL/SQL风格指南http://www.williamrobertson.net/documents/plsqlcodingstandards.html,但不仅限于PG。 - Rajarshi Das
3个回答

10
我不知道是否有官方的样式指南。(Postgres本身有编码约定,但这是针对用C语言编写的核心代码。)
话虽如此,请查看PGXN源代码中核心开发人员使用的样式:

https://github.com/pgxn/pgxn-manager/tree/master/sql

这里是一个简单的插入语句包装器,用于说明:
CREATE OR REPLACE FUNCTION insert_user(
    nickname   LABEL,
    password   TEXT,
    full_name  TEXT   DEFAULT '',
    email      EMAIL  DEFAULT NULL,
    uri        URI    DEFAULT NULL,
    twitter    CITEXT DEFAULT NULL,
    why        TEXT   DEFAULT NULL
) RETURNS BOOLEAN LANGUAGE plpgsql SECURITY DEFINER AS $$
/*
    % SELECT insert_user(
        nickname  := 'theory',
        password  := '***',
        full_name := 'David Wheeler',
        email     := 'theory@pgxn.org',
        uri       := 'http://justatheory.com/',
        twitter   := 'theory',
        why       := 'Because I’m a bitchin’ Pg developer, yo.'
    );
     insert_user 
    ─────────────
     t
Inserts a new user into the database. The nickname must not already exist or
an exception will be thrown. The password must be at least four characters
long or an exception will be thrown. The status will be set to "new" and the
`set_by` set to the new user's nickname. The other parameters are:
full_name
: The full name of the user.
email
: The email address of the user. Must be a valid email address as verified by
  [Email::Valid](http://search.cpan.org/perldoc?Email::Valid).
uri
: Optional URI for the user. Should be a valid URI as verified by
  [Data::Validate::URI](http://search.cpan.org/perldoc?Data::Validate::URI).
twitter
: Optional Twitter username. Case-insensitive. A leading "@" will be removed.
why
: Optional text from the user explaining why she should be allowed access.
Returns true if the user was inserted, and false if not.
*/
BEGIN
    IF char_length(password) < 4 THEN
       RAISE EXCEPTION 'Password must be at least four characters long';
    END IF;
    INSERT INTO users (
        nickname,
        password,
        full_name,
        email,
        uri,
        twitter,
        why,
        set_by
    )
    VALUES (
        insert_user.nickname,
        crypt(insert_user.password, gen_salt('des')),
        COALESCE(insert_user.full_name, ''),
        insert_user.email,
        insert_user.uri,
        COALESCE(trim(leading '@' FROM insert_user.twitter), ''),
        COALESCE(insert_user.why, ''),
        insert_user.nickname
    );
    RETURN FOUND;
END;
$$;

9
我找到了这个http://www.sqlstyle.guide网站:
这些准则旨在与Joe Celko的SQL编程风格书籍兼容,以使已经阅读该书的团队更容易采用。在某些方面,这个指南更加主观,在其他方面则更加宽松。相比于Celko的书籍中包含有关每个规则背后的思考和推理的散文,本指南肯定更加简洁。

3

关于SQL风格指南,特别是Postgres SQL方面,我目前不知道有任何官方的指南。如果你的主要目标是代码美化(即编写特定的方式使其更易读/让眼睛舒服),那么你可以选择以下选项:

- 尝试从sourceforge获取pgFormatter(PostgreSQL SQL语法美化程序)
- 从Oracle网站免费获取Oracle SQL Developer工具(有格式化SQL的选项)
- 尝试使用Dell软件提供的第三方程序,如SQL Navigator或TOAD(试用版/付费版)
- 使用带有SQL代码格式化选项的免费编辑器Notepad++

抱歉,对于你的第二个问题,我尚不清楚。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接