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);
121 std::string name = {};
122 SqlColumnTypeDefinition type = {};
123 std::string dialectDependantTypeString = {};
124 bool isNullable =
true;
125 bool isUnique =
false;
127 unsigned short decimalDigits = 0;
128 bool isAutoIncrement =
false;
129 bool isPrimaryKey =
false;
130 bool isForeignKey =
false;
131 std::optional<ForeignKeyConstraint> foreignKeyConstraint {};
132 std::string defaultValue = {};
147 virtual void OnTables(std::vector<std::string>
const& tables) = 0;
152 virtual bool OnTable(std::string_view schema, std::string_view table) = 0;
153 virtual void OnPrimaryKeys(std::string_view table, std::vector<std::string>
const& columns) = 0;
154 virtual void OnForeignKey(ForeignKeyConstraint
const& foreignKeyConstraint) = 0;
155 virtual void OnColumn(
Column const& column) = 0;
156 virtual void OnExternalForeignKey(ForeignKeyConstraint
const& foreignKeyConstraint) = 0;
157 virtual void OnIndexes(std::vector<IndexDefinition>
const& indexes) = 0;
158 virtual void OnTableEnd() = 0;
170 std::string_view database,
171 std::string_view schema,
202 using TableList = std::vector<Table>;
204 using ReadAllTablesCallback = std::function<void(std::string_view ,
size_t ,
size_t )>;
210 using TableReadyCallback = std::function<void(Table&&)>;
221 using TableFilterPredicate = std::function<bool(std::string_view , std::string_view )>;
233 LIGHTWEIGHT_API TableList ReadAllTables(
SqlStatement& stmt,
234 std::string_view database,
235 std::string_view schema = {},
236 ReadAllTablesCallback callback = {},
237 TableReadyCallback tableReadyCallback = {},
238 TableFilterPredicate tableFilter = {});
241 LIGHTWEIGHT_API std::vector<ForeignKeyConstraint> AllForeignKeysTo(SqlStatement& stmt,
242 FullyQualifiedTableName
const& table);
245 LIGHTWEIGHT_API std::vector<ForeignKeyConstraint> AllForeignKeysFrom(SqlStatement& stmt,
246 FullyQualifiedTableName
const& table);
253 LIGHTWEIGHT_API SqlCreateTablePlan MakeCreateTablePlan(Table
const& tableDescription);
260 LIGHTWEIGHT_API std::vector<SqlCreateTablePlan> MakeCreateTablePlan(TableList
const& tableDescriptions);
267struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableName>: std::formatter<std::string>
269 auto format(Lightweight::SqlSchema::FullyQualifiedTableName
const& value, format_context& ctx)
const
270 -> format_context::iterator
272 string output = std::string(Lightweight::SqlSchema::detail::rtrim(value.schema));
275 auto const trimmedSchema = Lightweight::SqlSchema::detail::rtrim(value.catalog);
276 output += trimmedSchema;
277 if (!output.empty() && !trimmedSchema.empty())
279 output += Lightweight::SqlSchema::detail::rtrim(value.table);
280 return formatter<string>::format(output, ctx);
285struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableColumn>: std::formatter<std::string>
287 auto format(Lightweight::SqlSchema::FullyQualifiedTableColumn
const& value, format_context& ctx)
const
288 -> format_context::iterator
290 auto const table = std::format(
"{}", value.table);
292 return formatter<string>::format(std::format(
"{}", value.column), ctx);
294 return formatter<string>::format(std::format(
"{}.{}", value.table, value.column), ctx);
299struct std::formatter<Lightweight::SqlSchema::FullyQualifiedTableColumnSequence>: std::formatter<std::string>
301 auto format(Lightweight::SqlSchema::FullyQualifiedTableColumnSequence
const& value, format_context& ctx)
const
302 -> format_context::iterator
304 auto const resolvedTableName = std::format(
"{}", value.table);
306 output += resolvedTableName;
309#if !defined(__cpp_lib_ranges_enumerate)
311 for (
auto const& column: value.columns)
315 for (
auto const [i, column]: value.columns | std::views::enumerate)
324 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.
virtual bool OnTable(std::string_view schema, std::string_view table)=0
High level API for (prepared) raw SQL statements.
Holds the definition of a column in a SQL table as read from the database schema.
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).