соединение с базой!

This commit is contained in:
Антон
2025-08-24 17:08:36 +03:00
parent 029f9c2fd3
commit 918db80742
5 changed files with 53 additions and 11 deletions
+2
View File
@@ -35,6 +35,8 @@ add_executable(App ./src/main.cpp
./src/session/WebsocketSession.cpp
./src/listener/Listener.h
./src/listener/Listener.cpp
./src/db/mysql_connector.cpp
./src/db/mysql_connector.h
)
target_link_libraries(App PRIVATE Boost::boost Threads::Threads mysql::concpp)
+9 -9
View File
@@ -52,6 +52,15 @@ CREATE TABLE `up_and_down`.`anxiety` (
PRIMARY KEY (`level`)
);
CREATE TABLE `up_and_down`.`user_treatment_schemes` (
`guid` CHAR(36) NOT NULL,
`user_guid` CHAR(36) NOT NULL,
`treatment_name` TEXT NOT NULL,
`instructions` TEXT,
PRIMARY KEY (`guid`),
FOREIGN KEY (`user_guid`) REFERENCES `users`(`guid`)
);
CREATE TABLE `up_and_down`.`diaries` (
`guid` CHAR(36) NOT NULL,
`user_guid` CHAR(36) NOT NULL,
@@ -93,15 +102,6 @@ CREATE TABLE `up_and_down`.`treatment_schemes` (
FOREIGN KEY (`medication_guid`) REFERENCES `medications`(`guid`)
);
CREATE TABLE `up_and_down`.`user_treatment_schemes` (
`guid` CHAR(36) NOT NULL,
`user_guid` CHAR(36) NOT NULL,
`treatment_name` TEXT NOT NULL,
`instructions` TEXT,
PRIMARY KEY (`guid`),
FOREIGN KEY (`user_guid`) REFERENCES `users`(`guid`)
);
-- insert constants
INSERT INTO `up_and_down`.`mania` (`level`, `description`) VALUES (1, 'Полное отсутствие мании');
INSERT INTO `up_and_down`.`mania` (`level`, `description`) VALUES (2, 'Слегка приподнятое настроение');
+22
View File
@@ -0,0 +1,22 @@
#include <string>
#include "mysql_connector.h"
using namespace std::string_literals;
namespace uad
{
static mysqlx::Session* mysql_session = nullptr;
void SetMySqlSession(mysqlx::Session* ptr)
{
mysql_session = ptr;
}
mysqlx::Session& GetMySqlSession()
{
mysqlx::Schema schema = mysql_session->getSchema("up_and_down"s);
return *mysql_session;
}
}
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include <mysqlx/xdevapi.h>
namespace uad
{
void SetMySqlSession(mysqlx::Session* ptr);
mysqlx::Session& GetMySqlSession();
}
+10 -2
View File
@@ -17,6 +17,7 @@
#include "./session/WebsocketSession.h"
#include "./listener/Listener.h"
#include "./db/mysql_connector.h"
namespace beast = boost::beast;
namespace http = beast::http;
@@ -24,12 +25,14 @@ namespace websocket = beast::websocket;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;
using namespace uad;
using namespace std;
using namespace std::string_literals;
int main(int argc, char* argv[])
{
if (argc != 5)
if (argc != 6)
{
std::cerr << "Usage: advanced-server <address> <port> <doc_root> <threads>\n"
std::cerr << "Usage: advanced-server <address> <port> <doc_root> <threads> <mysqlx://user:password@localhost:3306>\n"
<< "Example:\n"
<< " advanced-server 0.0.0.0 8080 . 1\n";
return EXIT_FAILURE;
@@ -38,6 +41,11 @@ int main(int argc, char* argv[])
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const doc_root = std::make_shared<std::string>(argv[3]);
auto const threads = std::max<int>(1, std::atoi(argv[4]));
string mysql_credentials = argv[5];
mysqlx::Session session(mysql_credentials);
uad::SetMySqlSession(&session);
net::io_context ioc{threads};