6#include "SqlQuery/MigrationPlan.hpp"
20 constexpr std::string_view rtrim(std::string_view value)
noexcept
22 while (!value.empty() && (std::isspace(value.back()) || value.back() ==
'\0'))
23 value.remove_suffix(1);
28struct FullyQualifiedTableName
34 bool operator==(FullyQualifiedTableName
const& other)
const noexcept
36 return catalog == other.catalog && schema == other.schema && table == other.table;
39 bool operator!=(FullyQualifiedTableName
const& other)
const noexcept
41 return !(*
this == other);
44 bool operator<(FullyQualifiedTableName
const& other)
const noexcept
46 return std::tie(catalog, schema, table) < std::tie(other.catalog, other.schema, other.table);
50struct FullyQualifiedTableColumn
52 FullyQualifiedTableName table;
55 bool operator==(FullyQualifiedTableColumn
const& other)
const noexcept
57 return table == other.table && column == other.column;
60 bool operator!=(FullyQualifiedTableColumn
const& other)
const noexcept
62 return !(*
this == other);
65 bool operator<(FullyQualifiedTableColumn
const& other)
const noexcept
67 return std::tie(table, column) < std::tie(other.table, other.column);
71struct FullyQualifiedTableColumnSequence
73 FullyQualifiedTableName table;
74 std::vector<std::string> columns;
77inline bool operator<(FullyQualifiedTableColumnSequence
const& a, FullyQualifiedTableColumnSequence
const& b)
noexcept
79 return std::tie(a.table, a.columns) < std::tie(b.table, b.columns);
82struct ForeignKeyConstraint
84 FullyQualifiedTableColumnSequence foreignKey;
85 FullyQualifiedTableColumnSequence primaryKey;
88inline bool operator<(ForeignKeyConstraint
const& a, ForeignKeyConstraint
const& b)
noexcept
90 return std::tie(a.foreignKey, a.primaryKey) < std::tie(b.foreignKey, b.primaryKey);
96 std::string name = {};
97 SqlColumnTypeDefinition type = {};
98 std::string dialectDependantTypeString = {};
99 bool isNullable =
true;
100 bool isUnique =
false;
102 unsigned short decimalDigits = 0;
103 bool isAutoIncrement =
false;
104 bool isPrimaryKey =
false;
105 bool isForeignKey =
false;
106 std::optional<ForeignKeyConstraint> foreignKeyConstraint {};
107 std::string defaultValue = {};
122 virtual void OnTables(std::vector<std::string>
const& tables) = 0;
124 virtual bool OnTable(std::string_view table) = 0;
125 virtual void OnPrimaryKeys(std::string_view table, std::vector<std::string>
const& columns) = 0;
126 virtual void OnForeignKey(ForeignKeyConstraint
const& foreignKeyConstraint) = 0;
127 virtual void OnColumn(
Column const& column) = 0;
128 virtual void OnExternalForeignKey(ForeignKeyConstraint
const& foreignKeyConstraint) = 0;
129 virtual void OnTableEnd() = 0;
133LIGHTWEIGHT_API
void ReadAllTables(std::string_view database, std::string_view schema,
EventHandler& eventHandler);
157using TableList = std::vector<Table>;
159using ReadAllTablesCallback = std::function<void(std::string_view ,
size_t ,
size_t )>;
162LIGHTWEIGHT_API TableList ReadAllTables(std::string_view database,
163 std::string_view schema = {},
164 ReadAllTablesCallback callback = {});
167LIGHTWEIGHT_API std::vector<ForeignKeyConstraint> AllForeignKeysTo(
SqlStatement& stmt, FullyQualifiedTableName
const& table);
170LIGHTWEIGHT_API std::vector<ForeignKeyConstraint> AllForeignKeysFrom(
SqlStatement& stmt,
171 FullyQualifiedTableName
const& table);
176struct std::formatter<SqlSchema::FullyQualifiedTableName>: std::formatter<std::string>
178 auto format(SqlSchema::FullyQualifiedTableName
const& value, format_context& ctx)
const -> format_context::iterator
180 string output = std::string(SqlSchema::detail::rtrim(value.schema));
183 auto const trimmedSchema = SqlSchema::detail::rtrim(value.catalog);
184 output += trimmedSchema;
185 if (!output.empty() && !trimmedSchema.empty())
187 output += SqlSchema::detail::rtrim(value.table);
188 return formatter<string>::format(output, ctx);
193struct std::formatter<SqlSchema::FullyQualifiedTableColumn>: std::formatter<std::string>
195 auto format(SqlSchema::FullyQualifiedTableColumn
const& value, format_context& ctx)
const -> format_context::iterator
197 auto const table = std::format(
"{}", value.table);
199 return formatter<string>::format(std::format(
"{}", value.column), ctx);
201 return formatter<string>::format(std::format(
"{}.{}", value.table, value.column), ctx);
206struct std::formatter<SqlSchema::FullyQualifiedTableColumnSequence>: std::formatter<std::string>
208 auto format(SqlSchema::FullyQualifiedTableColumnSequence
const& value, format_context& ctx)
const
209 -> format_context::iterator
211 auto const resolvedTableName = std::format(
"{}", value.table);
213 output += resolvedTableName;
216 for (
auto const [i, column]: value.columns | std::views::enumerate)
224 return formatter<string>::format(output, ctx);
Callback interface for handling events while reading a database schema.
virtual void OnTables(std::vector< std::string > const &tables)=0
Called when the names of all tables are read.
High level API for (prepared) raw SQL statements.
Holds the definition of a column in a SQL table as read from the database schema.
Holds the definition of a table in a SQL database as read from the database schema.
std::vector< ForeignKeyConstraint > foreignKeys
The foreign keys of the table.
std::vector< Column > columns
The columns of the table.
std::vector< std::string > primaryKeys
The primary keys of the table.
std::vector< ForeignKeyConstraint > externalForeignKeys
The foreign keys of other tables that reference this table.
std::string name
The name of the table.