API Reference
This section documents the C++ classes and functions that make up the Perseid libraries.
Database and base object classes
-
class Config
A database connection configuration
Public Functions
-
Config() = delete
-
Config(const string &dbname, const string &host = {}, int port = 0, const string &user = {})
Constructor
- Parameters:
dbname – database name
host – server hostname
port – server port
user – user name
-
inline const string &dbname() const
-
inline const string &host() const
-
inline int port() const
-
inline const string &user() const
-
Config() = delete
-
class Database
A database definition, from its catalogs and/or a YAML spec A Database is initialized with a Config object specifying connection information.
Public Functions
-
Database() = delete
-
Database(const Config &cfg)
Initialize the database
- Parameters:
cfg – database connection configuration
-
void from_catalog()
Populate the database objects by querying the catalogs
-
YAML::Node to_yaml(bool no_owner = true, bool no_privs = true, const vector<string> &schemas = {}, const vector<string> &tables = {})
Convert the database schema to a YAML node
- Parameters:
no_owner – exclude object owner information
no_privs – exclude privilege information
schemas – schema names to include
tables – table names to include
- Returns:
YAML node
-
void from_yaml(const YAML::Node &node)
Populate the database schema from an input YAML node
- Parameters:
node – a YAML node defining the database schema
-
vector<Statement> compare(const YAML::Node &node, bool no_owner = true, bool no_privs = true)
Generate SQL statements to convert the database
Compares the database (as read from the catalogs) to the input YAML node and generates SQL statements to convert it to the one represented by the input.
- Parameters:
node – the input YAML node defining the database
no_owner – exclude object owner changes
no_privs – exclude privilege changes
- Returns:
SQL statements
-
void reserved_words()
Get PostgreSQL reserved words
-
inline Rel_db *db() const
Return the underlying relational database connection wrapper
- Returns:
the Rel_db instance, or nullptr if none
-
inline const Database_map &map() const
Return the database object maps
- Returns:
the object maps
-
inline Database_map &map()
-
Database() = delete
-
class Database_map
The collection of all database objects
Public Functions
-
void from_catalog(Rel_db &db)
Initialize the map from the database catalogs
- Parameters:
db – database connection
-
void from_yaml(const YAML::Node &node)
Initialize the map from a YAML node
The top-level node is partitioned by object type and dispatched to the individual object maps. Unrecognized object types are rejected.
- Parameters:
node – a YAML node defining the database schema
-
vector<Statement> compare(const YAML::Node &node)
Generate SQL statements to convert the database
- Parameters:
node – the input YAML node defining the database
- Returns:
SQL statements
-
void from_catalog(Rel_db &db)
-
class Db_object
A single object in a database catalog, e.g., a schema, a table.
Subclassed by perseid::Extension, perseid::Schema, perseid::Schema_object
Public Functions
-
Db_object() = default
-
inline Db_object(const string &name, const string &description, const string &owner)
Constructor
- Parameters:
name – object name
description – comment text describing object
owner – owner name
-
virtual ~Db_object() = default
-
virtual string obj_type() const = 0
Type of object as an uppercase string, for SQL syntax generation
This is used in most CREATE, ALTER and DROP statements. It is also used by extern_key() in lowercase form.
- Returns:
string
-
virtual string extern_key() const
Return the key to be used in external maps for this object
The common format for an external key is
object-type non-schema-qualified-name, whereobject-typeis the lowercase version of obj_type, e.g.,table tablename.- Returns:
string
-
virtual string identifier() const
Returns a full identifier for the database object
This is used by comment(), alter_owner() and drop() to generate SQL syntax referring to the object. It does not include the object type, but it may include (in overridden methods) other elements.
- Returns:
string
-
virtual vector<Statement> create() const = 0
Return SQL statements to create the object
- Returns:
SQL statements
-
virtual vector<Statement> drop() const
Generate SQL to drop the object
- Returns:
SQL statements
-
string comment() const
Return SQL statement to create a COMMENT on the object
- Returns:
SQL statement
-
string alter_owner() const
Return ALTER statement to set the OWNER of an object
- Returns:
SQL statement
-
virtual vector<Statement> alter(const Db_object &rhs, bool no_owner = false) const
Generate SQL to alter a database object
Compares the object to an input object and generates SQL statements to alter it into the one represented by the input. The default implementation handles description and owner changes.
- Parameters:
rhs – the new object
no_owner – exclude object owner changes
- Returns:
SQL statements
-
inline string name() const
-
inline void set_name(const string &nm)
-
inline string description() const
-
inline void set_description(const string &desc)
-
inline string owner() const
-
inline void set_owner(const string &own)
-
Db_object() = default
Non-SCHEMA objects
-
class Extension : public perseid::Db_object
A PostgreSQL extension definition
This corresponds to a pg_extension catalog entry. Only the attributes needed to identify the extension and to recreate it are retained.
Public Functions
-
Extension() = default
-
inline Extension(const string &name, const string &schema = {}, const string &version = {}, const string &description = {}, const string &owner = {})
Constructor
- Parameters:
name – extension name
schema – schema where the extension is installed
version – extension version
description – comment text
owner – owner name
-
inline virtual string obj_type() const override
Type of object as an uppercase string, for SQL syntax generation
This is used in most CREATE, ALTER and DROP statements. It is also used by extern_key() in lowercase form.
- Returns:
string
-
virtual vector<Statement> create() const override
Return SQL statements to CREATE the extension
- Returns:
SQL statements
-
virtual vector<Statement> alter(const Db_object &rhs, bool no_owner = true) const override
Generate SQL to alter an extension
Extension ownership cannot be changed with ALTER EXTENSION, so owner differences are always ignored.
- Parameters:
rhs – the new extension definition
no_owner – ignored; owner changes are never generated
- Returns:
SQL statements
-
YAML::Node to_yaml(bool no_owner = false) const
Convert to a YAML node
- Parameters:
no_owner – exclude owner information
- Returns:
YAML node
-
inline const string &schema() const
-
inline void set_schema(const string &sch)
-
inline const string &version() const
-
inline void set_version(const string &vers)
Public Static Functions
-
static string catalog_query(unsigned dbversion = 0)
Return the catalog query to load extensions
- Parameters:
dbversion – database server version (e.g. 160006 for 16.6), 0 for unknown; may alter the query on version-specific catalogs
- Returns:
SQL query string
-
Extension() = default
-
class Extension_map
The collection of extensions in a database
Public Functions
-
void from_catalog(Rel_db &db)
Initialize the map from the database catalogs
- Parameters:
db – database connection
-
void from_yaml(const YAML::Node &node)
Initialize the map from a YAML node
The node is expected to contain only
extension <name>entries.- Parameters:
node – the input YAML node defining the extensions
-
vector<Statement> compare(const Extension_map &rhs) const
Compare the Extension_map to another
Compares the map to the input map and generates SQL statements to convert it into the one represented by the input. Extensions no longer present are dropped, except for core procedural languages.
- Parameters:
rhs – the target collection of extensions
- Returns:
SQL statements
-
void from_catalog(Rel_db &db)
Base SCHEMA objects
-
class Schema_object : public perseid::Db_object
A database object that is owned by a certain schema
Subclassed by perseid::Constraint, perseid::Relation
Public Functions
-
Schema_object() = default
-
inline Schema_object(const string &name, const string &schema, const string &description, const string &owner)
Constructor
- Parameters:
name – object name
schema – schema name
description – comment text
owner – owner name
-
virtual string extern_key() const override
Return the key to be used in external maps for this object
- Returns:
string
-
virtual string identifier() const override
Return a full identifier for a schema object
- Returns:
string
-
string qual_name(const string &schema = {}, const string &objname = {}) const
Return the schema-qualified name of self or a related object
- Parameters:
schema – schema name (defaults to own schema)
objname – object name (defaults to own name)
- Returns:
string
-
inline string schema() const
-
inline void set_schema(const string &sch)
-
Schema_object() = default
-
class Schema : public perseid::Db_object
A database schema definition, i.e., a named collection of tables, views, triggers and other schema objects.
Public Functions
-
Schema() = default
-
inline Schema(const string &name, const string &description = {}, const string &owner = {})
Constructor
- Parameters:
name – schema name
description – comment text
owner – owner name
-
inline virtual string obj_type() const override
Type of object as an uppercase string, for SQL syntax generation
This is used in most CREATE, ALTER and DROP statements. It is also used by extern_key() in lowercase form.
- Returns:
string
-
virtual vector<Statement> create() const override
Generate SQL statements to CREATE the schema
- Returns:
SQL statements
-
virtual vector<Statement> drop() const override
Generate SQL to drop the schema
- Returns:
SQL statements
-
YAML::Node to_yaml(bool no_owner = false) const
Convert to a YAML node
- Parameters:
no_owner – exclude owner information
- Returns:
YAML node
Public Static Functions
-
static string catalog_query(unsigned dbversion = 0)
Return the catalog query to load schemas
- Parameters:
dbversion – database server version (e.g. 160006 for 16.6), 0 for unknown; may alter the query on version-specific catalogs
- Returns:
SQL query string
-
Schema() = default
-
class Schema_map
The collection of schemas in a database
Public Functions
-
void from_catalog(Rel_db &db)
Initialize the map from the database catalogs
Queries catalogs for schemas other than system/information schemas, and populates the map.
- Parameters:
db – database connection
-
void from_yaml(const YAML::Node &node)
Initialize the map from a YAML node
The node is expected to contain only
schema <name>entries.- Parameters:
node – the input YAML node defining the schemas
-
vector<Statement> compare(const Schema_map &rhs) const
Compare the Schema_map to another
Compares the map to the input map and generates SQL statements to convert it to the one represented by the input.
- Parameters:
rhs – the target collection of schemas
- Returns:
SQL statements
-
void from_catalog(Rel_db &db)
Command-line arguments
-
struct Common_args
Common command line options shared by dbtoyaml and yamltodb
An argparse::ArgumentParser pre-populated with the PostgreSQL connection options common to both tools. Tool-specific options are added by each utility before parsing.
Public Functions
-
Common_args(const string &program, const string &description)
Create a parser with the common PostgreSQL options
- Parameters:
program – argv[0], used for the usage line and –version output
description – text describing the program
-
void add_connection_options()
Register the server connection options in a “Connection options” help group. Called by parse_args after the tool-specific options so the latter appear before the group.
-
Common_args(const string &program, const string &description)
-
optional<int> perseid::parse_args(Common_args &cmd, int argc, char *argv[])
Parse the command line, displaying usage errors argparse-style
- Parameters:
cmd – the parser created by the Common_args constructor
argc – number of arguments
argv – argument vector
- Returns:
nullopt if parsing succeeded and execution should continue, otherwise the process exit code to return (0 for –help or –version, 1 for errors)