6#include "SqlQuery/MigrationPlan.hpp"
25 constexpr std::string_view rtrim(std::string_view value)
noexcept
28 while (!value.empty() && (std::isspace(
static_cast<unsigned char>(value.back())) || value.back() ==
'\0'))
29 value.remove_suffix(1);
36 struct MssqlColumnMetrics
58 [[nodiscard]] LIGHTWEIGHT_API SqlColumnTypeDefinition MakeColumnTypeFromMssqlSysType(std::string_view sysTypeName,
59 MssqlColumnMetrics metrics);
62 struct FullyQualifiedTableName
68 bool operator==(FullyQualifiedTableName
const& other)
const noexcept
70 return catalog == other.catalog && schema == other.schema && table == other.table;
73 bool operator!=(FullyQualifiedTableName
const& other)
const noexcept
75 return !(*
this == other);
78 bool operator<(FullyQualifiedTableName
const& other)
const noexcept
80 return std::tie(catalog, schema, table) < std::tie(other.catalog, other.schema, other.table);
98 return table == other.table &&
column == other.column;
104 return !(*
this == other);
110 return std::tie(
table,
column) < std::tie(other.table, other.column);
125 return std::tie(a.table, a.columns) < std::tie(b.table, b.columns);
128 struct ForeignKeyConstraint
130 ColumnIdentifierSequence foreignKey;
131 ColumnIdentifierSequence primaryKey;
134 inline bool operator<(ForeignKeyConstraint
const& a, ForeignKeyConstraint
const& b)
noexcept
136 return std::tie(a.foreignKey, a.primaryKey) < std::tie(b.foreignKey, b.primaryKey);
152 using KeyPair = std::pair<FullyQualifiedTableName , FullyQualifiedTableName >;
154 inline bool operator<(KeyPair
const& a, KeyPair
const& b)
156 return std::tie(a.first, a.second) < std::tie(b.first, b.second);
165 SqlColumnTypeDefinition
type = {};
205 virtual void OnTables(std::vector<std::string>
const& tables) = 0;
210 virtual bool OnTable(std::string_view schema, std::string_view table) = 0;
212 virtual void OnPrimaryKeys(std::string_view table, std::vector<std::string>
const& columns) = 0;
214 virtual void OnForeignKey(ForeignKeyConstraint
const& foreignKeyConstraint) = 0;
220 virtual void OnIndexes(std::vector<IndexDefinition>
const& indexes) = 0;
234 std::string_view database,
235 std::string_view schema,
266 using TableList = std::vector<Table>;
268 using ReadAllTablesCallback = std::function<void(std::string_view ,
size_t ,
size_t )>;
274 using TableReadyCallback = std::function<void(Table&&)>;
285 using TableFilterPredicate = std::function<bool(std::string_view , std::string_view )>;
297 LIGHTWEIGHT_API TableList ReadAllTables(
SqlStatement& stmt,
298 std::string_view database,
299 std::string_view schema = {},
300 ReadAllTablesCallback callback = {},
301 TableReadyCallback tableReadyCallback = {},
302 TableFilterPredicate tableFilter = {});
305 LIGHTWEIGHT_API std::vector<ForeignKeyConstraint> AllForeignKeysTo(SqlStatement& stmt,
306 FullyQualifiedTableName
const& table);
309 LIGHTWEIGHT_API std::vector<ForeignKeyConstraint> AllForeignKeysFrom(SqlStatement& stmt,
310 FullyQualifiedTableName
const& table);
317 LIGHTWEIGHT_API SqlCreateTablePlan MakeCreateTablePlan(Table
const& tableDescription);
324 LIGHTWEIGHT_API std::vector<SqlCreateTablePlan> MakeCreateTablePlan(TableList
const& tableDescriptions);
331struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableName>: std::formatter<std::string>
333 auto format(Lightweight::SqlSchema::FullyQualifiedTableName
const& value, format_context& ctx)
const
334 -> format_context::iterator
336 string output = std::string(Lightweight::SqlSchema::detail::rtrim(value.schema));
339 auto const trimmedSchema = Lightweight::SqlSchema::detail::rtrim(value.catalog);
340 output += trimmedSchema;
341 if (!output.empty() && !trimmedSchema.empty())
343 output += Lightweight::SqlSchema::detail::rtrim(value.table);
344 return formatter<string>::format(output, ctx);
349struct std::formatter<Lightweight::SqlSchema::ColumnIdentifier>: std::formatter<std::string>
353 auto const table = std::format(
"{}", value.table);
355 return formatter<string>::format(std::format(
"{}", value.column), ctx);
357 return formatter<string>::format(std::format(
"{}.{}", value.table, value.column), ctx);
362struct std::formatter<Lightweight::SqlSchema::ColumnIdentifierSequence>: std::formatter<std::string>
365 -> format_context::iterator
367 auto const resolvedTableName = std::format(
"{}", value.table);
369 output += resolvedTableName;
372#if !defined(__cpp_lib_ranges_enumerate)
374 for (
auto const& column: value.columns)
378 for (
auto const [i, column]: value.columns | std::views::enumerate)
387 return formatter<string>::format(output, ctx);
Callback interface for handling events while reading a database schema.
virtual void OnColumn(Column const &column)=0
Called for each column in a table.
EventHandler(EventHandler &&)=default
Default move constructor.
virtual void OnIndexes(std::vector< IndexDefinition > const &indexes)=0
Called when the indexes of a table are read.
virtual void OnPrimaryKeys(std::string_view table, std::vector< std::string > const &columns)=0
Called when the primary keys of a table are read.
virtual void OnTables(std::vector< std::string > const &tables)=0
Called when the names of all tables are read.
EventHandler & operator=(EventHandler &&)=default
Default move assignment operator.
EventHandler & operator=(EventHandler const &)=default
Default copy assignment operator.
virtual void OnTableEnd()=0
Called when a table's schema reading is complete.
EventHandler()=default
Default constructor.
virtual bool OnTable(std::string_view schema, std::string_view table)=0
virtual void OnExternalForeignKey(ForeignKeyConstraint const &foreignKeyConstraint)=0
Called when an external foreign key referencing this table is read.
virtual void OnForeignKey(ForeignKeyConstraint const &foreignKeyConstraint)=0
Called when a foreign key constraint is read.
EventHandler(EventHandler const &)=default
Default copy constructor.
High level API for (prepared) raw SQL statements.
Identifies an ordered set of columns within a single table (for composite keys / indexes).
std::vector< std::string > columns
Ordered list of column names (order is significant for composite keys / indexes).
FullyQualifiedTableName table
Fully qualified table the column sequence belongs to.
std::string column
Column name within the table.
bool operator==(ColumnIdentifier const &other) const noexcept
Equality compares both the owning table and the column name.
bool operator!=(ColumnIdentifier const &other) const noexcept
Inequality is the negation of equality.
bool operator<(ColumnIdentifier const &other) const noexcept
Lexicographic ordering by (table, column) so ColumnIdentifier can be used as a key in ordered contain...
FullyQualifiedTableName table
Fully qualified table the column belongs to.
Holds the definition of a column in a SQL table as read from the database schema.
std::string name
The name of the column.
bool isNullable
Whether the column allows NULL values.
std::string defaultValue
The default value of the column.
bool isForeignKey
Whether the column is a foreign key.
SqlColumnTypeDefinition type
The SQL column type definition.
bool isPrimaryKey
Whether the column is a primary key.
std::string dialectDependantTypeString
The dialect-dependent type string.
std::optional< ForeignKeyConstraint > foreignKeyConstraint
The foreign key constraint, if any.
size_t size
The size of the column (for character/binary types).
bool isUnique
Whether the column has a UNIQUE constraint.
unsigned short decimalDigits
The number of decimal digits (for numeric types).
bool isAutoIncrement
Whether the column auto-increments.
Represents an index definition on a table.
std::string name
The name of the index.
std::vector< std::string > columns
The columns in the index (in order for composite indexes).
bool isUnique
Whether the index enforces uniqueness.
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< std::string > primaryKeys
The primary keys of the table.
std::string schema
The schema the table belongs to.
std::vector< ForeignKeyConstraint > externalForeignKeys
The foreign keys of other tables that reference this table.
std::string name
The name of the table.
std::vector< Column > columns
The columns of the table.
std::vector< IndexDefinition > indexes
The indexes on the table (excluding primary key index).