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);
34 struct FullyQualifiedTableName
40 bool operator==(FullyQualifiedTableName
const& other)
const noexcept
42 return catalog == other.catalog && schema == other.schema && table == other.table;
45 bool operator!=(FullyQualifiedTableName
const& other)
const noexcept
47 return !(*
this == other);
50 bool operator<(FullyQualifiedTableName
const& other)
const noexcept
52 return std::tie(catalog, schema, table) < std::tie(other.catalog, other.schema, other.table);
56 struct FullyQualifiedTableColumn
58 FullyQualifiedTableName table;
61 bool operator==(FullyQualifiedTableColumn
const& other)
const noexcept
63 return table == other.table && column == other.column;
66 bool operator!=(FullyQualifiedTableColumn
const& other)
const noexcept
68 return !(*
this == other);
71 bool operator<(FullyQualifiedTableColumn
const& other)
const noexcept
73 return std::tie(table, column) < std::tie(other.table, other.column);
77 struct FullyQualifiedTableColumnSequence
79 FullyQualifiedTableName table;
80 std::vector<std::string> columns;
83 inline bool operator<(FullyQualifiedTableColumnSequence
const& a, FullyQualifiedTableColumnSequence
const& b)
noexcept
85 return std::tie(a.table, a.columns) < std::tie(b.table, b.columns);
88 struct ForeignKeyConstraint
90 FullyQualifiedTableColumnSequence foreignKey;
91 FullyQualifiedTableColumnSequence primaryKey;
94 inline bool operator<(ForeignKeyConstraint
const& a, ForeignKeyConstraint
const& b)
noexcept
96 return std::tie(a.foreignKey, a.primaryKey) < std::tie(b.foreignKey, b.primaryKey);
112 using KeyPair = std::pair<FullyQualifiedTableName , FullyQualifiedTableName >;
114 inline bool operator<(KeyPair
const& a, KeyPair
const& b)
116 return std::tie(a.first, a.second) < std::tie(b.first, b.second);
125 SqlColumnTypeDefinition
type = {};
165 virtual void OnTables(std::vector<std::string>
const& tables) = 0;
170 virtual bool OnTable(std::string_view schema, std::string_view table) = 0;
172 virtual void OnPrimaryKeys(std::string_view table, std::vector<std::string>
const& columns) = 0;
174 virtual void OnForeignKey(ForeignKeyConstraint
const& foreignKeyConstraint) = 0;
180 virtual void OnIndexes(std::vector<IndexDefinition>
const& indexes) = 0;
194 std::string_view database,
195 std::string_view schema,
226 using TableList = std::vector<Table>;
228 using ReadAllTablesCallback = std::function<void(std::string_view ,
size_t ,
size_t )>;
234 using TableReadyCallback = std::function<void(Table&&)>;
245 using TableFilterPredicate = std::function<bool(std::string_view , std::string_view )>;
257 LIGHTWEIGHT_API TableList ReadAllTables(
SqlStatement& stmt,
258 std::string_view database,
259 std::string_view schema = {},
260 ReadAllTablesCallback callback = {},
261 TableReadyCallback tableReadyCallback = {},
262 TableFilterPredicate tableFilter = {});
265 LIGHTWEIGHT_API std::vector<ForeignKeyConstraint> AllForeignKeysTo(SqlStatement& stmt,
266 FullyQualifiedTableName
const& table);
269 LIGHTWEIGHT_API std::vector<ForeignKeyConstraint> AllForeignKeysFrom(SqlStatement& stmt,
270 FullyQualifiedTableName
const& table);
277 LIGHTWEIGHT_API SqlCreateTablePlan MakeCreateTablePlan(Table
const& tableDescription);
284 LIGHTWEIGHT_API std::vector<SqlCreateTablePlan> MakeCreateTablePlan(TableList
const& tableDescriptions);
291struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableName>: std::formatter<std::string>
293 auto format(Lightweight::SqlSchema::FullyQualifiedTableName
const& value, format_context& ctx)
const
294 -> format_context::iterator
296 string output = std::string(Lightweight::SqlSchema::detail::rtrim(value.schema));
299 auto const trimmedSchema = Lightweight::SqlSchema::detail::rtrim(value.catalog);
300 output += trimmedSchema;
301 if (!output.empty() && !trimmedSchema.empty())
303 output += Lightweight::SqlSchema::detail::rtrim(value.table);
304 return formatter<string>::format(output, ctx);
309struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableColumn>: std::formatter<std::string>
311 auto format(Lightweight::SqlSchema::FullyQualifiedTableColumn
const& value, format_context& ctx)
const
312 -> format_context::iterator
314 auto const table = std::format(
"{}", value.table);
316 return formatter<string>::format(std::format(
"{}", value.column), ctx);
318 return formatter<string>::format(std::format(
"{}.{}", value.table, value.column), ctx);
323struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableColumnSequence>: std::formatter<std::string>
325 auto format(Lightweight::SqlSchema::FullyQualifiedTableColumnSequence
const& value, format_context& ctx)
const
326 -> format_context::iterator
328 auto const resolvedTableName = std::format(
"{}", value.table);
330 output += resolvedTableName;
333#if !defined(__cpp_lib_ranges_enumerate)
335 for (
auto const& column: value.columns)
339 for (
auto const [i, column]: value.columns | std::views::enumerate)
348 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).