6#include "SqlQuery/MigrationPlan.hpp"
24 constexpr std::string_view rtrim(std::string_view value)
noexcept
26 while (!value.empty() && (std::isspace(value.back()) || value.back() ==
'\0'))
27 value.remove_suffix(1);
32 struct FullyQualifiedTableName
38 bool operator==(FullyQualifiedTableName
const& other)
const noexcept
40 return catalog == other.catalog && schema == other.schema && table == other.table;
43 bool operator!=(FullyQualifiedTableName
const& other)
const noexcept
45 return !(*
this == other);
48 bool operator<(FullyQualifiedTableName
const& other)
const noexcept
50 return std::tie(catalog, schema, table) < std::tie(other.catalog, other.schema, other.table);
54 struct FullyQualifiedTableColumn
56 FullyQualifiedTableName table;
59 bool operator==(FullyQualifiedTableColumn
const& other)
const noexcept
61 return table == other.table && column == other.column;
64 bool operator!=(FullyQualifiedTableColumn
const& other)
const noexcept
66 return !(*
this == other);
69 bool operator<(FullyQualifiedTableColumn
const& other)
const noexcept
71 return std::tie(table, column) < std::tie(other.table, other.column);
75 struct FullyQualifiedTableColumnSequence
77 FullyQualifiedTableName table;
78 std::vector<std::string> columns;
81 inline bool operator<(FullyQualifiedTableColumnSequence
const& a, FullyQualifiedTableColumnSequence
const& b)
noexcept
83 return std::tie(a.table, a.columns) < std::tie(b.table, b.columns);
86 struct ForeignKeyConstraint
88 FullyQualifiedTableColumnSequence foreignKey;
89 FullyQualifiedTableColumnSequence primaryKey;
92 inline bool operator<(ForeignKeyConstraint
const& a, ForeignKeyConstraint
const& b)
noexcept
94 return std::tie(a.foreignKey, a.primaryKey) < std::tie(b.foreignKey, b.primaryKey);
100 std::string name = {};
101 SqlColumnTypeDefinition type = {};
102 std::string dialectDependantTypeString = {};
103 bool isNullable =
true;
104 bool isUnique =
false;
106 unsigned short decimalDigits = 0;
107 bool isAutoIncrement =
false;
108 bool isPrimaryKey =
false;
109 bool isForeignKey =
false;
110 std::optional<ForeignKeyConstraint> foreignKeyConstraint {};
111 std::string defaultValue = {};
126 virtual void OnTables(std::vector<std::string>
const& tables) = 0;
128 virtual bool OnTable(std::string_view table) = 0;
129 virtual void OnPrimaryKeys(std::string_view table, std::vector<std::string>
const& columns) = 0;
130 virtual void OnForeignKey(ForeignKeyConstraint
const& foreignKeyConstraint) = 0;
131 virtual void OnColumn(
Column const& column) = 0;
132 virtual void OnExternalForeignKey(ForeignKeyConstraint
const& foreignKeyConstraint) = 0;
133 virtual void OnTableEnd() = 0;
137 LIGHTWEIGHT_API
void ReadAllTables(std::string_view database, std::string_view schema,
EventHandler& eventHandler);
161 using TableList = std::vector<Table>;
163 using ReadAllTablesCallback = std::function<void(std::string_view ,
size_t ,
size_t )>;
166 LIGHTWEIGHT_API TableList ReadAllTables(std::string_view database,
167 std::string_view schema = {},
168 ReadAllTablesCallback callback = {});
171 LIGHTWEIGHT_API std::vector<ForeignKeyConstraint> AllForeignKeysTo(SqlStatement& stmt,
172 FullyQualifiedTableName
const& table);
175 LIGHTWEIGHT_API std::vector<ForeignKeyConstraint> AllForeignKeysFrom(SqlStatement& stmt,
176 FullyQualifiedTableName
const& table);
183struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableName>: std::formatter<std::string>
185 auto format(Lightweight::SqlSchema::FullyQualifiedTableName
const& value, format_context& ctx)
const
186 -> format_context::iterator
188 string output = std::string(Lightweight::SqlSchema::detail::rtrim(value.schema));
191 auto const trimmedSchema = Lightweight::SqlSchema::detail::rtrim(value.catalog);
192 output += trimmedSchema;
193 if (!output.empty() && !trimmedSchema.empty())
195 output += Lightweight::SqlSchema::detail::rtrim(value.table);
196 return formatter<string>::format(output, ctx);
201struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableColumn>: std::formatter<std::string>
203 auto format(Lightweight::SqlSchema::FullyQualifiedTableColumn
const& value, format_context& ctx)
const
204 -> format_context::iterator
206 auto const table = std::format(
"{}", value.table);
208 return formatter<string>::format(std::format(
"{}", value.column), ctx);
210 return formatter<string>::format(std::format(
"{}.{}", value.table, value.column), ctx);
215struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableColumnSequence>: std::formatter<std::string>
217 auto format(Lightweight::SqlSchema::FullyQualifiedTableColumnSequence
const& value, format_context& ctx)
const
218 -> format_context::iterator
220 auto const resolvedTableName = std::format(
"{}", value.table);
222 output += resolvedTableName;
225#if !defined(__cpp_lib_ranges_enumerate)
227 for (
auto const& column: value.columns)
231 for (
auto const [i, column]: value.columns | std::views::enumerate)
240 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.
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< 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.
std::vector< Column > columns
The columns of the table.