Skip to content

Commit

Permalink
sqlite: More guidance on avoiding NULLs.
Browse files Browse the repository at this point in the history
Change-Id: I60a262dc0a8d37bad44615bba98d5ff8970a0110
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2918006
Reviewed-by: Marijn Kruisselbrink <mek@chromium.org>
Reviewed-by: Ayu Ishii <ayui@chromium.org>
Commit-Queue: Victor Costan <pwnall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#886843}
  • Loading branch information
pwnall authored and Chromium LUCI CQ committed May 26, 2021
1 parent d973bf2 commit 0166412
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
21 changes: 15 additions & 6 deletions sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,21 @@ using `sql::Statement::ColumnTime()`.
Column types should not include information ignored by SQLite, such as numeric
precision or scale specifiers, or string length specifiers.

Columns should have the `NOT NULL` constraint whenever possible. This saves
maintainers from having to reason about the less intuitive cases of
[`NULL` handling](https://sqlite.org/nulls.html).

Columns should avoid `DEFAULT` values. This moves the burden of checking that
`INSERT` statements aren't missing any columns from the code reviewer to SQLite.
Columns should have
[`NOT NULL` constraints](https://sqlite.org/lang_createtable.html#not_null_constraints)
whenever possible. This saves maintainers from having to reason about the less
intuitive cases of [`NULL` handling](https://sqlite.org/nulls.html).

`NOT NULL` constraints must be explicitly stated in column definitions that
include `PRIMARY KEY` specifiers. For historical reasons, SQLite
[allows NULL primary keys](https://sqlite.org/lang_createtable.html#the_primary_key)
in most cases. When a table's primary key is composed of multiple columns,
each column's definition should have a `NOT NULL` constraint.

Columns should avoid `DEFAULT` values. Columns that have `NOT NULL` constraints
and lack a `DEFAULT` value are easier to review and maintain, as SQLite takes
over the burden of checking that `INSERT` statements aren't missing these
columns.

Surrogate primary keys should use the column type `INTEGER PRIMARY KEY`, to take
advantage of SQLite's rowid optimizations.
Expand Down
2 changes: 1 addition & 1 deletion third_party/sqlite/sqlite3_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This is a shim that injects Chrome-specific defitions into sqlite3.c
// This is a shim that injects Chrome-specific definitions into sqlite3.c
// BUILD.gn uses this instead of building the sqlite3 amalgamation directly.

// We prefix chrome_ to SQLite's exported symbols, so that we don't clash with
Expand Down

0 comments on commit 0166412

Please sign in to comment.