6#include "SqlQuery/MigrationPlan.hpp"
25 constexpr std::string_view rtrim(std::string_view value)
noexcept
27 while (!value.empty() && (std::isspace(value.back()) || value.back() ==
'\0'))
28 value.remove_suffix(1);
33 struct FullyQualifiedTableName
39 bool operator==(FullyQualifiedTableName
const& other)
const noexcept
41 return catalog == other.catalog && schema == other.schema && table == other.table;
44 bool operator!=(FullyQualifiedTableName
const& other)
const noexcept
46 return !(*
this == other);
49 bool operator<(FullyQualifiedTableName
const& other)
const noexcept
51 return std::tie(catalog, schema, table) < std::tie(other.catalog, other.schema, other.table);
55 struct FullyQualifiedTableColumn
57 FullyQualifiedTableName table;
60 bool operator==(FullyQualifiedTableColumn
const& other)
const noexcept
62 return table == other.table && column == other.column;
65 bool operator!=(FullyQualifiedTableColumn
const& other)
const noexcept
67 return !(*
this == other);
70 bool operator<(FullyQualifiedTableColumn
const& other)
const noexcept
72 return std::tie(table, column) < std::tie(other.table, other.column);
76 struct FullyQualifiedTableColumnSequence
78 FullyQualifiedTableName table;
79 std::vector<std::string> columns;
82 inline bool operator<(FullyQualifiedTableColumnSequence
const& a, FullyQualifiedTableColumnSequence
const& b)
noexcept
84 return std::tie(a.table, a.columns) < std::tie(b.table, b.columns);
87 struct ForeignKeyConstraint
89 FullyQualifiedTableColumnSequence foreignKey;
90 FullyQualifiedTableColumnSequence primaryKey;
93 inline bool operator<(ForeignKeyConstraint
const& a, ForeignKeyConstraint
const& b)
noexcept
95 return std::tie(a.foreignKey, a.primaryKey) < std::tie(b.foreignKey, b.primaryKey);
111 using KeyPair = std::pair<FullyQualifiedTableName , FullyQualifiedTableName >;
113 inline bool operator<(KeyPair
const& a, KeyPair
const& b)
115 return std::tie(a.first, a.second) < std::tie(b.first, b.second);
124 SqlColumnTypeDefinition
type = {};
164 virtual void OnTables(std::vector<std::string>
const& tables) = 0;
169 virtual bool OnTable(std::string_view schema, std::string_view table) = 0;
171 virtual void OnPrimaryKeys(std::string_view table, std::vector<std::string>
const& columns) = 0;
173 virtual void OnForeignKey(ForeignKeyConstraint
const& foreignKeyConstraint) = 0;
179 virtual void OnIndexes(std::vector<IndexDefinition>
const& indexes) = 0;
193 std::string_view database,
194 std::string_view schema,
225 using TableList = std::vector<Table>;
227 using ReadAllTablesCallback = std::function<void(std::string_view ,
size_t ,
size_t )>;
233 using TableReadyCallback = std::function<void(Table&&)>;
244 using TableFilterPredicate = std::function<bool(std::string_view , std::string_view )>;
256 LIGHTWEIGHT_API TableList ReadAllTables(
SqlStatement& stmt,
257 std::string_view database,
258 std::string_view schema = {},
259 ReadAllTablesCallback callback = {},
260 TableReadyCallback tableReadyCallback = {},
261 TableFilterPredicate tableFilter = {});
264 LIGHTWEIGHT_API std::vector<ForeignKeyConstraint> AllForeignKeysTo(SqlStatement& stmt,
265 FullyQualifiedTableName
const& table);
268 LIGHTWEIGHT_API std::vector<ForeignKeyConstraint> AllForeignKeysFrom(SqlStatement& stmt,
269 FullyQualifiedTableName
const& table);
276 LIGHTWEIGHT_API SqlCreateTablePlan MakeCreateTablePlan(Table
const& tableDescription);
283 LIGHTWEIGHT_API std::vector<SqlCreateTablePlan> MakeCreateTablePlan(TableList
const& tableDescriptions);
290struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableName>: std::formatter<std::string>
292 auto format(Lightweight::SqlSchema::FullyQualifiedTableName
const& value, format_context& ctx)
const
293 -> format_context::iterator
295 string output = std::string(Lightweight::SqlSchema::detail::rtrim(value.schema));
298 auto const trimmedSchema = Lightweight::SqlSchema::detail::rtrim(value.catalog);
299 output += trimmedSchema;
300 if (!output.empty() && !trimmedSchema.empty())
302 output += Lightweight::SqlSchema::detail::rtrim(value.table);
303 return formatter<string>::format(output, ctx);
308struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableColumn>: std::formatter<std::string>
310 auto format(Lightweight::SqlSchema::FullyQualifiedTableColumn
const& value, format_context& ctx)
const
311 -> format_context::iterator
313 auto const table = std::format(
"{}", value.table);
315 return formatter<string>::format(std::format(
"{}", value.column), ctx);
317 return formatter<string>::format(std::format(
"{}.{}", value.table, value.column), ctx);
322struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableColumnSequence>: std::formatter<std::string>
324 auto format(Lightweight::SqlSchema::FullyQualifiedTableColumnSequence
const& value, format_context& ctx)
const
325 -> format_context::iterator
327 auto const resolvedTableName = std::format(
"{}", value.table);
329 output += resolvedTableName;
332#if !defined(__cpp_lib_ranges_enumerate)
334 for (
auto const& column: value.columns)
338 for (
auto const [i, column]: value.columns | std::views::enumerate)
347 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.
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).