Lightweight 0.20261105.1
Loading...
Searching...
No Matches
Catalog.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5// <sql.h> below needs a few symbols resolved first — see SqlOdbcPrelude.hpp's header comment.
6#include "../../Api.hpp"
7#include "../../SqlBindingTypes.hpp"
8#include "../../SqlError.hpp"
9#include "../Concept.hpp"
10#include "Handles.hpp"
11#include "SqlOdbcPrelude.hpp"
12
13#include <expected>
14#include <string>
15#include <string_view>
16
17#include <sql.h>
18#include <sqlext.h>
19
20/// @file
21/// The catalog operations of the ODBC driver: the ODBC catalog functions (@c SQLTables, @c SQLColumns,
22/// @c SQLPrimaryKeys, @c SQLForeignKeys, @c SQLStatistics) that SqlSchema runs to read a database's schema.
23/// Each opens the catalog result set on the statement and leaves it there; the caller reads it by column
24/// position, as the ODBC specification lays it out.
25///
26/// Every function is a thin wrapper: the same ODBC call with the same arguments as the code it was moved
27/// from, and the return code handed back as @c Lightweight::SqlResult so that the caller keeps its check
28/// at the same point. Text arguments go to the W entry point as UTF-16 when @p wideText is set, otherwise
29/// to the ANSI entry point with the UTF-8 bytes unchanged (see SqlConnection::AcceptsWideQueryText()).
30/// An empty text argument is passed as a null pointer with length 0.
31
32namespace Lightweight::Driver::Odbc
33{
34
35/// @brief The three-part name of a catalog object (a table, or a search pattern for tables), so that the
36/// parts are passed by name rather than as adjacent strings that could be swapped silently.
38{
39 /// The catalog (database) name; empty for none.
40 std::string_view catalog;
41 /// The schema name; empty for none.
42 std::string_view schema;
43 /// The object name or search pattern; what an empty one means is documented per function.
44 std::string_view name;
45};
46
47/// @brief Opens the @c SQLTables result set of the tables of @p tables' catalog and schema, narrowed to its
48/// name search pattern. No table type is passed (null pointer, length 0).
49/// @param stmt The statement to open the result set on.
50/// @param tables The catalog name, the schema name and the table name search pattern (empty for all tables).
51/// @param wideText Whether to call @c SQLTablesW rather than @c SQLTables.
52/// @return The result of @c SQLTables.
53/// @note Reached through the concept's @c OpenCatalog (see @c OpenCatalog below), which shared code calls with a
54/// @ref Lightweight::Driver::SqlCatalogRequest.
55[[nodiscard]] LIGHTWEIGHT_API SqlResult OpenTablesCatalog(Statement const& stmt,
56 CatalogObjectName const& tables,
57 bool wideText);
58
59/// @brief Opens the @c SQLColumns result set of every column of @p table (no column name: null pointer,
60/// length 0).
61/// @param stmt The statement to open the result set on.
62/// @param table The catalog name, the schema name and the table name.
63/// @param wideText Whether to call @c SQLColumnsW rather than @c SQLColumns.
64/// @return The result of @c SQLColumns.
65/// @note Reached through the concept's @c OpenCatalog (see @c OpenCatalog below), which shared code calls with a
66/// @ref Lightweight::Driver::SqlCatalogRequest.
67[[nodiscard]] LIGHTWEIGHT_API SqlResult OpenColumnsCatalog(Statement const& stmt,
68 CatalogObjectName const& table,
69 bool wideText);
70
71/// @brief Opens the @c SQLPrimaryKeys result set of @p table.
72/// @param stmt The statement to open the result set on.
73/// @param table The catalog name, the schema name and the table name.
74/// @param wideText Whether to call @c SQLPrimaryKeysW rather than @c SQLPrimaryKeys.
75/// @return The result of @c SQLPrimaryKeys.
76/// @note Reached through the concept's @c OpenCatalog (see @c OpenCatalog below), which shared code calls with a
77/// @ref Lightweight::Driver::SqlCatalogRequest.
78[[nodiscard]] LIGHTWEIGHT_API SqlResult OpenPrimaryKeysCatalog(Statement const& stmt,
79 CatalogObjectName const& table,
80 bool wideText);
81
82/// @brief Opens the @c SQLForeignKeys result set of the foreign keys between the primary-key table and the
83/// foreign-key table; an empty table name leaves that side open.
84/// @param stmt The statement to open the result set on.
85/// @param primaryKeyTable The catalog name, the schema name and the name (empty for any) of the
86/// primary-key table.
87/// @param foreignKeyTable The catalog name, the schema name and the name (empty for any) of the
88/// foreign-key table.
89/// @param wideText Whether to call @c SQLForeignKeysW rather than @c SQLForeignKeys.
90/// @return The result of @c SQLForeignKeys.
91/// @note Reached through the concept's @c OpenCatalog (see @c OpenCatalog below), which shared code calls with a
92/// @ref Lightweight::Driver::SqlCatalogRequest.
93[[nodiscard]] LIGHTWEIGHT_API SqlResult OpenForeignKeysCatalog(Statement const& stmt,
94 CatalogObjectName const& primaryKeyTable,
95 CatalogObjectName const& foreignKeyTable,
96 bool wideText);
97
98/// @brief Opens the @c SQLStatistics result set of the indexes of @p table, with @c SQL_ENSURE.
99/// @param stmt The statement to open the result set on.
100/// @param table The catalog name, the schema name and the table name.
101/// @param uniqueOnly @c SQL_INDEX_UNIQUE when set, otherwise @c SQL_INDEX_ALL.
102/// @param wideText Whether to call @c SQLStatisticsW rather than @c SQLStatistics.
103/// @return The result of @c SQLStatistics.
104/// @note Reached through the concept's @c OpenCatalog (see @c OpenCatalog below), which shared code calls with a
105/// @ref Lightweight::Driver::SqlCatalogRequest.
106[[nodiscard]] LIGHTWEIGHT_API SqlResult OpenStatisticsCatalog(Statement const& stmt,
107 CatalogObjectName const& table,
108 bool uniqueOnly,
109 bool wideText);
110
111/// @brief Reads the driver's @c SQL_SEARCH_PATTERN_ESCAPE string through the ANSI @c SQLGetInfo, into a
112/// 32-byte buffer.
113/// @param connection The connection to ask.
114/// @return The escape string, or an empty string when the driver does not report it; never an error.
115/// @note Part of the @ref Lightweight::Driver::SqlDriver concept, which every driver implements: it escapes the
116/// search patterns of the catalog functions above. A driver without catalog functions
117/// (Driver::HasCatalogFunctions is false) answers with the not-supported error: SqlSchema reads its catalog with
118/// SQL, and the dialect's SqlCatalogQueries::searchPatternEscape states the escape, see
119/// SqlQueryFormatter::CatalogQueries().
120[[nodiscard]] LIGHTWEIGHT_API std::expected<std::string, SqlErrorInfo> SearchPatternEscape(Connection const& connection);
121
122/// @brief Opens the catalog result set @p request names: calls @c OpenTablesCatalog, @c OpenColumnsCatalog,
123/// @c OpenPrimaryKeysCatalog, @c OpenForeignKeysCatalog or @c OpenStatisticsCatalog with its arguments.
124/// @param stmt The statement to open the result set on.
125/// @param request The catalog function and its arguments; @c wideText chooses the W entry point.
126/// @return The result of the catalog function.
127/// @note Part of the @ref Lightweight::Driver::SqlDriver concept, which every driver implements: shared code reaches
128/// the catalog functions only through it. A driver without catalog functions (Driver::HasCatalogFunctions is
129/// false) fails it with the not-supported error; SqlSchema reads that driver's catalog with SQL instead, see
130/// SqlQueryFormatter::CatalogQueries().
131[[nodiscard]] LIGHTWEIGHT_API SqlResult OpenCatalog(Statement const& stmt, SqlCatalogRequest const& request);
132
133} // namespace Lightweight::Driver::Odbc
LIGHTWEIGHT_API SqlResult OpenStatisticsCatalog(Statement const &stmt, CatalogObjectName const &table, bool uniqueOnly, bool wideText)
Opens the SQLStatistics result set of the indexes of table, with SQL_ENSURE.
LIGHTWEIGHT_API SqlResult OpenForeignKeysCatalog(Statement const &stmt, CatalogObjectName const &primaryKeyTable, CatalogObjectName const &foreignKeyTable, bool wideText)
Opens the SQLForeignKeys result set of the foreign keys between the primary-key table and the foreign...
LIGHTWEIGHT_API SqlResult OpenTablesCatalog(Statement const &stmt, CatalogObjectName const &tables, bool wideText)
Opens the SQLTables result set of the tables of tables' catalog and schema, narrowed to its name sear...
LIGHTWEIGHT_API SqlResult OpenPrimaryKeysCatalog(Statement const &stmt, CatalogObjectName const &table, bool wideText)
Opens the SQLPrimaryKeys result set of table.
LIGHTWEIGHT_API SqlResult OpenColumnsCatalog(Statement const &stmt, CatalogObjectName const &table, bool wideText)
Opens the SQLColumns result set of every column of table (no column name: null pointer,...
SqlResult OpenCatalog(StatementHandle const &statement, SqlCatalogRequest const &request)
Opens the catalog result set request names on statement, in the layout the ODBC specification gives t...
Definition Dispatch.hpp:502
The three-part name of a catalog object (a table, or a search pattern for tables),...
Definition Catalog.hpp:38
std::string_view name
The object name or search pattern; what an empty one means is documented per function.
Definition Catalog.hpp:44
std::string_view schema
The schema name; empty for none.
Definition Catalog.hpp:42
std::string_view catalog
The catalog (database) name; empty for none.
Definition Catalog.hpp:40
The ODBC driver's connection handle struct.
Definition Handles.hpp:53
The ODBC driver's statement handle struct.
Definition Handles.hpp:43
What a catalog call asks for: the ODBC catalog function and its arguments, in driver-neutral terms.
Definition Concept.hpp:136