You may generate SQL queries or YQL queries (for YDB) from external .sql/.yql files. To do this, call the following cmake function in your CMakeLists.txt:
userver_add_sql_library(
${PROJECT_NAME}_sql
NAMESPACE samples_postgres_service
OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}
SQL_FILES *.sql
)
target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}_sql)
It will generate the .hpp file with following variable:
extern const USERVER_NAMESPACE::storages::Query kCreateTable;
And the definition in .cpp looks something like that:
const USERVER_NAMESPACE::storages::Query kCreateTable = {
R"-(
CREATE TABLE IF NOT EXISTS key_value_table (
key VARCHAR PRIMARY KEY,
value VARCHAR
)
)-",
USERVER_NAMESPACE::storages::Query::Name("create_table"),
USERVER_NAMESPACE::storages::Query::LogMode::kFull,
};
You may use it as usual by passing to storages::postgres::Cluster::Execute()
for SQL files or ydb::TableClient::ExecuteDataQuery()
for YQL files.