Skip to main content

rupg_catalog/
database.rs

1//! The rows of `pg_database`. The catalog of a database holds the objects of that database, and the rows of `pg_database` are the same for all databases of the cluster, so the store of the server keeps them.
2
3/// The OID of `template1`.
4pub const TEMPLATE1: u32 = 1;
5/// The OID of `template0`.
6pub const TEMPLATE0: u32 = 4;
7/// The OID of `postgres`.
8pub const POSTGRES: u32 = 5;
9/// `DATCONNLIMIT_UNLIMITED`.
10pub const UNLIMITED: i32 = -1;
11
12/// A row of `pg_database`.
13#[derive(Clone, Debug, PartialEq, Eq)]
14pub struct Database {
15    pub oid: u32,
16    pub name: String,
17    /// `datdba`, the owner.
18    pub owner: u32,
19    /// The number of the encoding, 6 for `UTF8`.
20    pub encoding: i32,
21    /// `datlocprovider`: `b` for builtin, `c` for libc and `i` for ICU.
22    pub locale_provider: u8,
23    pub is_template: bool,
24    pub allow_connections: bool,
25    /// `datconnlimit`, or [`UNLIMITED`].
26    pub connection_limit: i32,
27    pub frozen_xid: u32,
28    pub min_mxid: u32,
29    /// `dattablespace`, the default tablespace.
30    pub tablespace: u32,
31    pub collate: String,
32    pub ctype: String,
33    /// `datlocale`, the locale of the builtin or the ICU provider.
34    pub locale: Option<String>,
35    pub icu_rules: Option<String>,
36    pub collation_version: Option<String>,
37    /// `datacl` as the texts of its items, or `None` for the default privileges.
38    pub acl: Option<Vec<String>>,
39}
40
41impl Database {
42    /// The three databases of `initdb`, in the order of their OIDs. They are the same as the static rows of `pg_database`.
43    pub fn initdb() -> [Database; 3] {
44        let acl = Some(vec!["=c/postgres".to_owned(), "postgres=CTc/postgres".to_owned()]);
45        let row = |oid: u32, name: &str, is_template: bool, allow_connections: bool| Database {
46            oid,
47            name: name.to_owned(),
48            owner: 10,
49            encoding: 6,
50            locale_provider: b'c',
51            is_template,
52            allow_connections,
53            connection_limit: UNLIMITED,
54            frozen_xid: 0,
55            min_mxid: 1,
56            tablespace: 1663,
57            collate: "C".to_owned(),
58            ctype: "C".to_owned(),
59            locale: None,
60            icu_rules: None,
61            collation_version: None,
62            acl: if is_template { acl.clone() } else { None },
63        };
64        [
65            row(TEMPLATE1, "template1", true, true),
66            row(TEMPLATE0, "template0", true, false),
67            row(POSTGRES, "postgres", false, true),
68        ]
69    }
70}