generated from userver-framework/service_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
afbc373
commit 43ff626
Showing
61 changed files
with
1,728 additions
and
309 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import os | ||
import sys | ||
|
||
def read_sql(result: dict, sql_file, directory_path): | ||
# Получите относительный путь относительно directory_path | ||
relative_path = os.path.relpath(sql_file, directory_path) | ||
file_name = os.path.basename(sql_file) | ||
file_name_without_extension, _ = os.path.splitext(file_name) | ||
if file_name_without_extension in result.keys(): | ||
raise ValueError(f"Дублируется имя файла. Первый путь:{result['path']}, второй путь:{relative_path}") | ||
with open(sql_file, 'r') as file: | ||
content = file.read() | ||
result[file_name_without_extension] = { | ||
'path': directory_path + '/' + relative_path, | ||
'content': content | ||
} | ||
def read_sqls(directory_path): | ||
result = {} | ||
for root, dir, files in os.walk(directory_path): | ||
for file in files: | ||
if file.endswith(".sql"): | ||
read_sql(result, os.path.join(root, file), directory_path) | ||
return result | ||
|
||
|
||
def place_begin_namespace(content, namespace): | ||
return content + '\nnamespace ' + namespace + '{\n\n' | ||
|
||
def place_end_namespace(content, namespace): | ||
return content + '}\n' | ||
|
||
def place_extern_sql_query(result_hpp: str, name: str, path: str): | ||
add = '//Generated from: ' + path + '\nextern const userver::storages::postgres::Query ' + name + ';\n\n' | ||
return result_hpp + add | ||
|
||
def place_impl_sql_query(result_cpp: str, name: str, path: str, content: str): | ||
add = '//Generated from: ' + path + '\nconst userver::storages::postgres::Query ' + name + ' = {\n' | ||
add = add + 'R"-(\n' | ||
add = add + content | ||
add = add + ')-",\n' | ||
add = add + '\tuserver::storages::postgres::Query::Name("' + name + '"),\n' | ||
add = add + '\tuserver::storages::postgres::Query::LogMode::kFull\n' | ||
add = add + '};\n\n' | ||
return result_cpp + add | ||
|
||
def prepare_result_contents(sqls: dict, namespace, name_result): | ||
result_hpp = '''#pragma once | ||
/* THIS FILE IS AUTOGENERATED, DON'T EDIT! */ | ||
#include <userver/storages/postgres/query.hpp> | ||
''' | ||
result_cpp = ''' | ||
/* THIS FILE IS AUTOGENERATED, DON'T EDIT! */ | ||
#include "''' + name_result + '.hpp"\n' | ||
|
||
result_hpp = place_begin_namespace(result_hpp, namespace) | ||
result_cpp = place_begin_namespace(result_cpp, namespace) | ||
for name, item in sqls.items(): | ||
result_hpp = place_extern_sql_query(result_hpp, name, item['path']) | ||
result_cpp = place_impl_sql_query(result_cpp, name, item['path'], item['content']) | ||
result_hpp = place_end_namespace(result_hpp, namespace) | ||
result_cpp = place_end_namespace(result_cpp, namespace) | ||
return result_hpp, result_cpp | ||
|
||
def main(): | ||
if len(sys.argv) < 5: | ||
print('Usage: python3 generate_sql_queries.py <sql_directory> <path/to/result> <name_result> <namespace>') | ||
return | ||
|
||
sql_directory = sys.argv[1] | ||
path_to_result = sys.argv[2] | ||
name_result = sys.argv[3] | ||
namespace = sys.argv[4] | ||
sqls = read_sqls(sql_directory) | ||
result_hpp, result_cpp = prepare_result_contents(sqls, namespace, name_result) | ||
hpp_path = path_to_result + '/' + name_result + '.hpp' | ||
cpp_path = path_to_result + '/' + name_result + '.cpp' | ||
with open(hpp_path, 'w') as hpp: | ||
hpp.write(result_hpp) | ||
with open(cpp_path, 'w') as hpp: | ||
hpp.write(result_cpp) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
|
||
/* THIS FILE IS AUTOGENERATED, DON'T EDIT! */ | ||
|
||
#include "sql.hpp" | ||
|
||
namespace sql{ | ||
|
||
//Generated from: src/sql/token/get_user_id_by_token.sql | ||
const userver::storages::postgres::Query get_user_id_by_token = { | ||
R"-( | ||
WITH found_token | ||
AS | ||
( | ||
select id_user | ||
from timetable_vsu.token | ||
WHERE id = $1 AND expire_time > $2 | ||
) | ||
SELECT id, login, password, user_type | ||
from timetable_vsu."user" | ||
LEFT OUTER JOIN found_token ON id_user = "user".id | ||
)-", | ||
userver::storages::postgres::Query::Name("get_user_id_by_token"), | ||
userver::storages::postgres::Query::LogMode::kFull | ||
}; | ||
|
||
//Generated from: src/sql/token/add_token_to_user.sql | ||
const userver::storages::postgres::Query add_token_to_user = { | ||
R"-( | ||
insert into timetable_vsu."token" (id_user, expire_time) values ($1, $2) RETURNING id | ||
)-", | ||
userver::storages::postgres::Query::Name("add_token_to_user"), | ||
userver::storages::postgres::Query::LogMode::kFull | ||
}; | ||
|
||
//Generated from: src/sql/user/drop_user_by_login.sql | ||
const userver::storages::postgres::Query drop_user_by_login = { | ||
R"-( | ||
DELETE FROM timetable_vsu."user" WHERE login=$1; | ||
)-", | ||
userver::storages::postgres::Query::Name("drop_user_by_login"), | ||
userver::storages::postgres::Query::LogMode::kFull | ||
}; | ||
|
||
//Generated from: src/sql/user/create_teacher_request.sql | ||
const userver::storages::postgres::Query create_teacher_request = { | ||
R"-( | ||
INSERT INTO timetable_vsu."teacher_requests"(id_user, description) values ($1, $2) ON CONFLICT DO NOTHING | ||
)-", | ||
userver::storages::postgres::Query::Name("create_teacher_request"), | ||
userver::storages::postgres::Query::LogMode::kFull | ||
}; | ||
|
||
//Generated from: src/sql/user/get_user_by_token.sql | ||
const userver::storages::postgres::Query get_user_by_token = { | ||
R"-( | ||
WITH all_user AS | ||
( | ||
SELECT | ||
u.id AS user_id, | ||
a.id AS admin_id, | ||
t.id AS token_id, | ||
tl.id_teacher AS teacher_id | ||
from timetable_vsu.token AS t | ||
left join timetable_vsu.user AS u on u.id = t.id_user | ||
left join timetable_vsu.admin AS a on u.id = a.id_user | ||
left join timetable_vsu.teacher_link AS tl on u.id = tl.id_user | ||
where t.id = $1 | ||
) | ||
SELECT | ||
user_id, | ||
CASE | ||
WHEN admin_id IS NOT NULL THEN 'admin'::timetable_vsu.user_type | ||
WHEN teacher_id IS NOT NULL THEN 'teacher'::timetable_vsu.user_type | ||
ELSE 'user' | ||
END AS type | ||
FROM all_user; | ||
)-", | ||
userver::storages::postgres::Query::Name("get_user_by_token"), | ||
userver::storages::postgres::Query::LogMode::kFull | ||
}; | ||
|
||
//Generated from: src/sql/user/internal_add_user.sql | ||
const userver::storages::postgres::Query internal_add_user = { | ||
R"-( | ||
insert into timetable_vsu."user"(id, login, password) values ($1, $2.login, $2.password) | ||
)-", | ||
userver::storages::postgres::Query::Name("internal_add_user"), | ||
userver::storages::postgres::Query::LogMode::kFull | ||
}; | ||
|
||
//Generated from: src/sql/user/create_admin_request.sql | ||
const userver::storages::postgres::Query create_admin_request = { | ||
R"-( | ||
INSERT INTO timetable_vsu."admin_requests"(id_user, description) values ($1, $2) ON CONFLICT DO NOTHING | ||
)-", | ||
userver::storages::postgres::Query::Name("create_admin_request"), | ||
userver::storages::postgres::Query::LogMode::kFull | ||
}; | ||
|
||
//Generated from: src/sql/user/drop_user_by_id.sql | ||
const userver::storages::postgres::Query drop_user_by_id = { | ||
R"-( | ||
DELETE FROM timetable_vsu."user" WHERE id=$1; | ||
)-", | ||
userver::storages::postgres::Query::Name("drop_user_by_id"), | ||
userver::storages::postgres::Query::LogMode::kFull | ||
}; | ||
|
||
//Generated from: src/sql/user/add_user.sql | ||
const userver::storages::postgres::Query add_user = { | ||
R"-( | ||
insert into timetable_vsu."user" | ||
( login, password) | ||
values ($1.login, $1.password) | ||
ON CONFLICT DO NOTHING | ||
RETURNING id | ||
)-", | ||
userver::storages::postgres::Query::Name("add_user"), | ||
userver::storages::postgres::Query::LogMode::kFull | ||
}; | ||
|
||
//Generated from: src/sql/user/get_user_by_credentials.sql | ||
const userver::storages::postgres::Query get_user_by_credentials = { | ||
R"-( | ||
WITH all_user AS | ||
( | ||
SELECT | ||
u.id AS user_id, | ||
a.id AS admin_id, | ||
tl.id_teacher AS teacher_id | ||
from | ||
timetable_vsu.user AS u | ||
left join timetable_vsu.admin AS a on u.id = a.id_user | ||
left join timetable_vsu.teacher_link AS tl on u.id = tl.id_user | ||
where u.login = $1.login and u."password" = $1.password | ||
) | ||
SELECT | ||
user_id, | ||
CASE | ||
WHEN admin_id IS NOT NULL THEN 'admin'::timetable_vsu.user_type | ||
WHEN teacher_id IS NOT NULL THEN 'teacher'::timetable_vsu.user_type | ||
ELSE 'user' | ||
END AS type | ||
FROM all_user; | ||
)-", | ||
userver::storages::postgres::Query::Name("get_user_by_credentials"), | ||
userver::storages::postgres::Query::LogMode::kFull | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
|
||
/* THIS FILE IS AUTOGENERATED, DON'T EDIT! */ | ||
|
||
#include <userver/storages/postgres/query.hpp> | ||
|
||
namespace sql{ | ||
|
||
//Generated from: src/sql/token/get_user_id_by_token.sql | ||
extern const userver::storages::postgres::Query get_user_id_by_token; | ||
|
||
//Generated from: src/sql/token/add_token_to_user.sql | ||
extern const userver::storages::postgres::Query add_token_to_user; | ||
|
||
//Generated from: src/sql/user/drop_user_by_login.sql | ||
extern const userver::storages::postgres::Query drop_user_by_login; | ||
|
||
//Generated from: src/sql/user/create_teacher_request.sql | ||
extern const userver::storages::postgres::Query create_teacher_request; | ||
|
||
//Generated from: src/sql/user/get_user_by_token.sql | ||
extern const userver::storages::postgres::Query get_user_by_token; | ||
|
||
//Generated from: src/sql/user/internal_add_user.sql | ||
extern const userver::storages::postgres::Query internal_add_user; | ||
|
||
//Generated from: src/sql/user/create_admin_request.sql | ||
extern const userver::storages::postgres::Query create_admin_request; | ||
|
||
//Generated from: src/sql/user/drop_user_by_id.sql | ||
extern const userver::storages::postgres::Query drop_user_by_id; | ||
|
||
//Generated from: src/sql/user/add_user.sql | ||
extern const userver::storages::postgres::Query add_user; | ||
|
||
//Generated from: src/sql/user/get_user_by_credentials.sql | ||
extern const userver::storages::postgres::Query get_user_by_credentials; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "controller.hpp" | ||
|
||
#include <boost/uuid/uuid.hpp> | ||
#include <chrono> | ||
#include <codegen/sql.hpp> | ||
#include <openapi/postgres/mapping.hpp> | ||
#include <userver/components/component_config.hpp> | ||
#include <userver/components/component_context.hpp> | ||
#include <userver/components/component_list.hpp> | ||
#include <userver/components/loggable_component_base.hpp> | ||
#include <userver/storages/postgres/component.hpp> | ||
#include <userver/storages/postgres/io/uuid.hpp> | ||
#include <userver/utils/datetime.hpp> | ||
#include <utils/shared_transaction.hpp> | ||
|
||
#include "controller_fwd.hpp" | ||
#include "models/user_credentials/postgres.hpp" | ||
|
||
namespace controllers::lesson | ||
{ | ||
void Append(userver::components::ComponentList& list) | ||
{ | ||
list.Append<Controller>(); | ||
} | ||
Controller::Controller(const userver::components::ComponentConfig& cfg, | ||
const userver::components::ComponentContext& ctx) | ||
: Base(cfg, ctx) | ||
{ | ||
} | ||
boost::uuids::uuid Controller::CreateNew( | ||
const boost::uuids::uuid& id_user, | ||
utils::SharedTransaction transaction) const | ||
{ | ||
utils::FillSharedTransaction(transaction, pg_cluster_); | ||
auto pg_result = transaction->transaction_.Execute( | ||
sql::add_token_to_user, id_user, | ||
userver::utils::datetime::Now() + std::chrono::hours(24)); | ||
return pg_result.AsSingleRow<boost::uuids::uuid>(); | ||
} | ||
} // namespace controllers::token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include <boost/uuid/uuid.hpp> | ||
#include <userver/components/loggable_component_base.hpp> | ||
#include <utils/component_list_fwd.hpp> | ||
#include <utils/pg_controller.hpp> | ||
#include <utils/shared_transaction.hpp> | ||
|
||
namespace controllers::lesson | ||
{ | ||
using Base = utils::PgController<"new-lesson-controller", "postgres-db-1">; | ||
struct Controller : Base | ||
{ | ||
Controller(const userver::components::ComponentConfig& cfg, | ||
const userver::components::ComponentContext& ctx); | ||
boost::uuids::uuid CreateNew( | ||
const boost::uuids::uuid& id_user, | ||
utils::SharedTransaction transaction = nullptr) const; | ||
}; | ||
} // namespace controllers::token |
Oops, something went wrong.