diff --git a/CMakeLists.txt b/CMakeLists.txt index b0e35db..aec95dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,10 +65,4 @@ add_executable(server ) # Add include directories target_include_directories(server PRIVATE server/inc shared/inc ${CMAKE_BINARY_DIR}/sqlite/) -target_link_libraries(server PRIVATE pthread dl m ${CMAKE_BINARY_DIR}/sqlite-prefix/libsqlite3.a) - -add_executable(client - server/src/client.cpp - shared/src/TcpSocket.cpp -) -target_include_directories(client PRIVATE server/inc shared/inc) \ No newline at end of file +target_link_libraries(server PRIVATE pthread dl m ${CMAKE_BINARY_DIR}/sqlite-prefix/libsqlite3.a) \ No newline at end of file diff --git a/server/inc/sql.hpp b/server/inc/sql.hpp index 5a922f0..9a04e4b 100644 --- a/server/inc/sql.hpp +++ b/server/inc/sql.hpp @@ -1,18 +1,36 @@ +#include - -const char create_table[] = "CREATE TABLE IF NOT EXISTS prim_table ( ID INTEGER PRIMARY KEY, USER_ID INTEGER, GEN INTEGER, HASH INTEGER, POS INTEGER, LIKED INTEGER);"; +const char create_table[] = "CREATE TABLE IF NOT EXISTS prim_table ( ID INTEGER PRIMARY KEY, USER_ID INTEGER, GEN INTEGER, HASH INTEGER, POS INTEGER, LIKED INTEGER, CHECKED INTEGER);"; const char create_index[] = "CREATE INDEX IF NOT EXISTS idx_usrer_id ON prim_table (USER_ID);"; -const char insert_data[] = "INSERT INTO prim_table (USER_ID, GEN, HASH, POS, LIKED) VALUES(?, ?, ?, ?, ?);"; +const char insert_data[] = "INSERT INTO prim_table (USER_ID, GEN, HASH, POS, LIKED, CHECKED) VALUES(?, ?, ?, ?, ?, 0);"; const char max_gen[] = "SELECT MAX(GEN) FROM prim_table WHERE USER_ID = ?;"; +const char get_unchecked[] = "SELECT USER_ID FROM prim_table WHERE CHECKED = 0 GROUP BY USER_ID;"; + constexpr char DB_NAME[] = "data.db"; +struct sqlite3; +struct sqlite3_stmt; + +#define SQL_DONE 101 +#define SQL_NULL 5 + namespace sql { void init(); void shutdown(); - + int open(const char *filename, sqlite3 **ppDb); + int prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **pStmt, const char **pzTail); + int bind_int64(sqlite3_stmt *pStmt, int column, int64_t value); + int step(sqlite3_stmt *pStmt); + int column_count(sqlite3_stmt *pStmt); + int column_type(sqlite3_stmt *pStmt, int column); + int64_t column_int64(sqlite3_stmt *pStmt, int column); + int finalize(sqlite3_stmt *pStmt); + int reset(sqlite3_stmt *pStmt); + int close(sqlite3 *db); + const char *errmsg(sqlite3 *db); } diff --git a/server/src/client.cpp b/server/src/client.cpp deleted file mode 100644 index da05266..0000000 --- a/server/src/client.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "TcpSocket.hpp" -#include "NetConst.hpp" -#include "values/DnaManager.hpp" -#include -#include -#include -#include - -int main() -{ - int sock = TcpSocket::connectt("petrovv.com", 8888); - - if (sock < 0) - { - printf("Error %d", sock); - return 0; - } - - std::ifstream idf("ID.bin", std::ios_base::binary); - - uint64_t ID = 0; - - idf.read((char *)&ID, sizeof(ID)); - idf.close(); - - printf("id: %ld\n", ID); - - TcpSocket::sendt(sock, &StartHeader, sizeof(StartHeader)); - TcpSocket::sendt(sock, &ID, sizeof(ID)); - - Message message; - - size_t needed_gen = 0; - char filename[20]; - - while (true) - { - TcpSocket::recvt(sock, &message, sizeof(Message)); - if (message.mess != Mess::REQ_SEND_GEN) - { - message.mess = Mess::RES_NO; - TcpSocket::sendt(sock, &message, sizeof(Message)); - break; - } - - needed_gen = message.data; - - int ret = sprintf(filename, "gen%04ld.bin", needed_gen); - filename[ret] = 0; - if (!std::filesystem::exists(filename)) - { - message.mess = Mess::RES_NO; - TcpSocket::sendt(sock, &message, sizeof(Message)); - break; - } - - std::ifstream sfile(filename, std::ios_base::binary | std::ios_base::in); - - std::vector net; - net.resize(NUM_PER_GEN); - - sfile.read((char *)net.data(), NUM_PER_GEN * sizeof(NetUnit)); - - message.mess = Mess::RES_OK; - message.data = NUM_PER_GEN * sizeof(NetUnit); - - TcpSocket::sendt(sock, &message, sizeof(Message)); - - TcpSocket::sendt(sock, net.data(), NUM_PER_GEN * sizeof(NetUnit)); - } - - TcpSocket::closet(sock); - - return 0; -} \ No newline at end of file diff --git a/server/src/server.cpp b/server/src/server.cpp index 2c47121..035ccc6 100644 --- a/server/src/server.cpp +++ b/server/src/server.cpp @@ -3,8 +3,6 @@ #include "sql.hpp" #include "values/DnaManager.hpp" -#include - #include #include @@ -40,30 +38,30 @@ void call(int sock, sockaddr_in newSocketInfo) sqlite3 *db; sqlite3_stmt *stmt; - int rc = sqlite3_open(DB_NAME, &db); + int rc = sql::open(DB_NAME, &db); if (rc) { - fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + fprintf(stderr, "Can't open database: %s\n", sql::errmsg(db)); ok = false; } - sqlite3_prepare_v2(db, max_gen, -1, &stmt, NULL); - sqlite3_bind_int64(stmt, 1, id); + sql::prepare_v2(db, max_gen, -1, &stmt, NULL); + sql::bind_int64(stmt, 1, id); int64_t gen = 0; - while (sqlite3_step(stmt) != SQLITE_DONE) + while (sql::step(stmt) != SQL_DONE) { - int num_cols = sqlite3_column_count(stmt); + int num_cols = sql::column_count(stmt); - int type = sqlite3_column_type(stmt, 0); + int type = sql::column_type(stmt, 0); - if (type == SQLITE_NULL) + if (type == SQL_NULL) break; - gen = sqlite3_column_int64(stmt, 0); + gen = sql::column_int64(stmt, 0); gen++; } - sqlite3_finalize(stmt); + sql::finalize(stmt); - sqlite3_prepare_v2(db, insert_data, -1, &stmt, NULL); + sql::prepare_v2(db, insert_data, -1, &stmt, NULL); while (ok) { @@ -85,21 +83,21 @@ void call(int sock, sockaddr_in newSocketInfo) for (size_t i = 0; i < list.size(); i++) { - sqlite3_bind_int64(stmt, 1, id); - sqlite3_bind_int64(stmt, 2, gen); - sqlite3_bind_int64(stmt, 3, list[i].hash); - sqlite3_bind_int64(stmt, 4, list[i].index); - sqlite3_bind_int64(stmt, 5, list[i].liked); - sqlite3_step(stmt); - sqlite3_reset(stmt); + sql::bind_int64(stmt, 1, id); + sql::bind_int64(stmt, 2, gen); + sql::bind_int64(stmt, 3, list[i].hash); + sql::bind_int64(stmt, 4, list[i].index); + sql::bind_int64(stmt, 5, list[i].liked); + sql::step(stmt); + sql::reset(stmt); } gen++; } - sqlite3_finalize(stmt); + sql::finalize(stmt); - sqlite3_close(db); + sql::close(db); printf("del\n"); TcpSocket::closet(sock); } diff --git a/server/src/sql.cpp b/server/src/sql.cpp index 7c2bf89..7acf906 100644 --- a/server/src/sql.cpp +++ b/server/src/sql.cpp @@ -57,4 +57,52 @@ void sql::init() void sql::shutdown() { sqlite3_shutdown(); +} + +namespace sql +{ + int open(const char *filename, sqlite3 **ppDb) + { + return sqlite3_open(filename, ppDb); + } + int prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **pStmt, const char **pzTail) + { + return sqlite3_prepare_v2(db, zSql, nByte, pStmt, pzTail); + } + int bind_int64(sqlite3_stmt *pStmt, int column, int64_t value) + { + return sqlite3_bind_int64(pStmt, column, value); + } + int step(sqlite3_stmt *pStmt) + { + return sqlite3_step(pStmt); + } + int column_count(sqlite3_stmt *pStmt) + { + return sqlite3_column_count(pStmt); + } + int column_type(sqlite3_stmt *pStmt, int column) + { + return sqlite3_column_type(pStmt, column); + } + int64_t column_int64(sqlite3_stmt *pStmt, int column) + { + return sqlite3_column_int64(pStmt, column); + } + int finalize(sqlite3_stmt *pStmt) + { + return sqlite3_finalize(pStmt); + } + int reset(sqlite3_stmt *pStmt) + { + return sqlite3_reset(pStmt); + } + int close(sqlite3 *db) + { + return sqlite3_close(db); + } + const char *errmsg(sqlite3 *db) + { + return sqlite3_errmsg(db); + } } \ No newline at end of file