Prosa SQL Coding Style

Table of contents

  1. Introduction
    1. Terminology notes
    2. Guide notes
  2. Formatting
    1. Braces
    2. Block indentation
    3. Column limit
    4. Grouping parentheses
    5. SELECT statement
    6. FROM statement
    7. JOIN statement
    8. WHERE statement
  3. Naming
    1. Rules common to all identifiers
    2. Rules by identifier type

1 Introduction

This document serves as the complete definition of Prosa's coding standards for source code in the SQL Programming Language.

1.1 Terminology notes

1.2 Guide notes

Example code in this document is non-normative. That is, while the examples are in Prosa Style, they may not illustrate the only stylish way to represent the code. Optional formatting choices made in examples should not be enforced as rules.

2 Formatting

2.1 Braces

2.1.1 K & R style

Braces follow the Kernighan and Ritchie style ("Egyptian brackets")

2.2 Block indentation: +2 spaces

Each time a new block or block-like construct is opened, the indent increases by 2 spaces. When the block ends, the indent returns to the previous indent level. The indent level applies to both code and comments throughout the block. (See the example in Section 4.1.2, K & R Style.)

2.3 Column limit: 120

SQL code has a column limit of 120 characters

2.3.1 Comments

Any line break may be preceded by arbitrary whitespace followed by an implementation comment. Such a comment renders the line non-blank.

2.3.1.1 Block comment style

Block comments are indented at the same level as the surrounding code. They may be in /* ... */ style or -- ... style.

Comments are not enclosed in boxes drawn with asterisks or other characters.

2.4 Grouping parentheses: recommended

Optional grouping parentheses are omitted only when author and reviewer agree that there is no reasonable chance the code will be misinterpreted without them, nor would they have made the code easier to read. It is not reasonable to assume that every reader has the entire SQL operator precedence table memorized.

2.5 SELECT

The expressions following the SELECT statement are either all in the SELECT line or each in a new line, indented by 2 spaces.

Examples:

// Correct
SELECT col1, col2, col3

// Correct
SELECT
  col1,
  col2,
  col3

// Wrong
SELECT col1,
  col2,
  col3

2.6 FROM

The FROM keyword is aligned vertically with the SELECT statement.

Examples:

// Correct
SELECT *
FROM tbl

// Wrong
SELECT *
  FROM tbl

2.7 JOIN

The JOIN keyword always start in a new line and is indented by 2 spaces with respect of the FROM keyword.

The ON keyword is in the same line of the respective JOIN. Additional conditions must begin in a new line and be indented by at least 4 spaces with respect of the JOIN keyword.

When a JOIN is present, all tables must have an alias.

Examples:

// Correct
SELECT *
FROM tbl t
  JOIN tbl1 t1 ON t.tbl1_id = t1.id
      AND tbl1.code = 1

// Wrong
SELECT *
FROM tbl t
  JOIN tbl1 t1 ON t.tbl1_id = t1.id AND tbl1.code = 1

// Wrong
SELECT *
FROM tbl
JOIN tbl1 ON tbl.tbl1_id = tbl1.id

2.8 WHERE

The WHERE keyword is aligned vertically with the SELECT and FROM keywords.

All conditions must be either in the same line of the WHERE keyword or each in one line, with the first one in the same line of the WHERE, and others indented by 2 spaces.

Examples:

// Correct
SELECT *
FROM tbl t
WHERE t.code > 1 AND t.code < 10

// Correct
SELECT *
FROM tbl t
WHERE t.code > 1
  AND t.code < 10
  OR t.code = 5

Conditions wrapped in parenthesis are indented by 2 spaces.

Examples:

// Correct
SELECT *
FROM tbl t
WHERE t.code > 1
  AND (
    t.code = 1
    OR t.code = 2
  )

// Wrong
SELECT *
FROM tbl t
WHERE t.code > 1
  AND (
  t.code = 1
  OR t.code = 2
  )

3 Naming

3.1 Rules common to all identifiers

Identifiers use only ASCII letters, digits and underscores. Thus each valid identifier name is matched by the regular expression \w+ .

Identifiers are written in snake_case.

3.2 Rules by identifier type

3.2.1 Table names

Table names are in plural form.

3.2.2 Column names

Foreign key columns must end with _id. A column that is not a foreign key cannot end with _id.

The column with the record creation date must be called created_at.

The column with the record update date must be called updated_at.