PL / pgSQL样式指南

时间:2015-02-04 10:16:55

标签: postgresql coding-style

互联网上有很多针对不同语言的风格指针,例如CSSJavascriptRubyRails等。

但是我在哪里可以找到适合PostgreSQL数据库系统的程序语言的现代样式指南 - PL/pgSQL

我还要感谢自动代码分析器,例如rubocop用于ruby。

3 个答案:

答案 0 :(得分:7)

我不知道任何官方风格指南。 (对于Postgres本身有coding conventions,但这是用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;
$$;

答案 1 :(得分:6)

我发现了http://www.sqlstyle.guide

这些指南旨在与Joe Celko的SQL编程风格书兼容,以便为已经阅读过该书的团队提供更多便利。本指南在某些方面更有说服力,而在其他方面稍微放松一些。毫无疑问,Celko的书中包含了每条规则背后的轶事和推理作为有思想的散文。

答案 2 :(得分:3)

我不知道任何官员&#39; SQL的样式指南,单独留下Postgres SQL。话虽如此,如果代码美化是您的主要目标,您可以使用以下选项(即以特定方式书写,以便更容易阅读/抚慰您的眼睛)。

  - 尝试使用sourceforge的pgFormatter(PostgreSQL SQL语法美化器)   - 从Oracle的站点中免费试用Oracle SQL Developer工具(具有格式化SQL的选项)   - 尝试从Dell软件(试用/付费)的第三方程序,如SQL Navigator或TOAD   - 尝试使用SQL代码格式化选项
的Notepad ++(一个免费编辑器)
抱歉你的第二个问题,不知道。

相关问题