ColumnInfo

public struct ColumnInfo : FetchableRecord

A column of a database table.

This type closely matches the information returned by the table_info and table_xinfo pragmas.

sqlite> CREATE TABLE player (
   ...>   id INTEGER PRIMARY KEY,
   ...>   firstName TEXT,
   ...>   lastName TEXT);
sqlite> PRAGMA table_info(player);
cid     name        type        notnull     dflt_value  pk
------  ----------  ----------  ----------  ----------  -----
0       id          INTEGER     0                       1
1       firstName   TEXT        0                       0
2       lastName    TEXT        0                       0
sqlite> PRAGMA table_xinfo(player);
cid     name        type        notnull     dflt_value  pk     hidden
------  ----------  ----------  ----------  ----------  -----  ----------
0       id          INTEGER     0                       1      0
1       firstName   TEXT        0                       0      0
2       lastName    TEXT        0                       0      0

See Database.columns(in:) and https://www.sqlite.org/pragma.html#pragma_table_info

  • The column name

    Declaration

    Swift

    public let name: String
  • The column data type

    Declaration

    Swift

    public let type: String
  • True if and only if the column is constrained to be not null.

    Declaration

    Swift

    public let isNotNull: Bool
  • The SQL snippet that defines the default value, if any.

    When nil, the column has no default value.

    When not nil, it contains an SQL string that defines an expression. That expression may be a literal, as 1, or 'foo'. It may also contain a non-constant expression such as CURRENT_TIMESTAMP.

    For example:

    try db.execute(sql: """
        CREATE TABLE player(
            id INTEGER PRIMARY KEY,
            name TEXT DEFAULT 'Anonymous',
            score INT DEFAULT 0,
            creationDate DATE DEFAULT CURRENT_TIMESTAMP
        )
        """)
    let columnInfos = try db.columns(in: "player")
    columnInfos[0].defaultValueSQL // nil
    columnInfos[1].defaultValueSQL // "'Anoynymous'"
    columnInfos[2].defaultValueSQL // "0"
    columnInfos[3].defaultValueSQL // "CURRENT_TIMESTAMP"
    

    Declaration

    Swift

    public let defaultValueSQL: String?
  • Zero for columns that are not part of the primary key.

    Before SQLite 3.7.16, it is 1 for columns that are part of the primary key.

    Starting from SQLite 3.7.16, it is the one-based index of the column in the primary key for columns that are part of the primary key.

    References:

    Declaration

    Swift

    public let primaryKeyIndex: Int