diff --git a/server/inc/sql.hpp b/server/inc/sql.hpp index df75399..5a922f0 100644 --- a/server/inc/sql.hpp +++ b/server/inc/sql.hpp @@ -1,5 +1,15 @@ +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_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 max_gen[] = "SELECT MAX(GEN) FROM prim_table WHERE USER_ID = ?;"; + +constexpr char DB_NAME[] = "data.db"; + namespace sql { void init(); diff --git a/server/src/client.cpp b/server/src/client.cpp index 6fe34d1..0fe3033 100644 --- a/server/src/client.cpp +++ b/server/src/client.cpp @@ -53,13 +53,13 @@ int main() TcpSocket::sendt(sock, &message, sizeof(Message)); break; } - - idf.open(filename, std::ios_base::binary | std::ios_base::in); + + std::ifstream sfile(filename, std::ios_base::binary | std::ios_base::in); std::vector net; net.resize(NUM_PER_GEN); - idf.read((char *)net.data(), NUM_PER_GEN * sizeof(NetUnit)); + sfile.read((char *)net.data(), NUM_PER_GEN * sizeof(NetUnit)); message.mess = Mess::RES_OK; message.data = NUM_PER_GEN * sizeof(NetUnit); diff --git a/server/src/server.cpp b/server/src/server.cpp index 5f9f87d..0272dc2 100644 --- a/server/src/server.cpp +++ b/server/src/server.cpp @@ -2,21 +2,18 @@ #include "NetConst.hpp" #include "sql.hpp" #include "values/DnaManager.hpp" -#include +#include + +#include #include // use pthread rw_lock to lock db so you can safy clone .db file -typedef std::vector NetList; -typedef std::vector UserList; - -std::map data; - // When a new client connected: void call(int sock, sockaddr_in newSocketInfo) { - std::cout << "new User" << std::endl; + printf("new User"); int64_t conf, id; Message message; @@ -36,21 +33,41 @@ void call(int sock, sockaddr_in newSocketInfo) return; } - auto found = data.find(id); - if (found == data.end()) + bool ok = true; + + sqlite3 *db; + sqlite3_stmt *stmt; + + int rc = sqlite3_open(DB_NAME, &db); + if (rc) { - UserList list; - data.insert(std::pair(id, list)); - found = data.find(id); + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + ok = false; } - while (true) + sqlite3_prepare_v2(db, max_gen, -1, &stmt, NULL); + sqlite3_bind_int64(stmt, 1, id); + int64_t gen = 0; + while (sqlite3_step(stmt) != SQLITE_DONE) { - size_t gen = found->second.size(); + int num_cols = sqlite3_column_count(stmt); + + int type = sqlite3_column_type(stmt, 0); + + if(type == SQLITE_NULL) break; + gen = sqlite3_column_int64(stmt, 0); + gen++; + + + } + sqlite3_finalize(stmt); + sqlite3_prepare_v2(db, insert_data, -1, &stmt, NULL); + + while (ok) + { message.mess = Mess::REQ_SEND_GEN; message.data = gen; - TcpSocket::sendt(sock, &message, sizeof(Message)); TcpSocket::recvt(sock, &message, sizeof(Message)); @@ -59,16 +76,28 @@ void call(int sock, sockaddr_in newSocketInfo) break; } - NetList list; + std::vector list; list.resize(NUM_PER_GEN); TcpSocket::recvt(sock, list.data(), NUM_PER_GEN * sizeof(NetUnit)); - found->second.push_back(list); + + 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); + } + + gen++; } - printf("id: %ld, size: %ld\n", id, found->second.size()); + sqlite3_finalize(stmt); - std::cout << "del USER" << std::endl; + sqlite3_close(db); + printf("del USER"); TcpSocket::closet(sock); } diff --git a/server/src/sql.cpp b/server/src/sql.cpp index 2d5022a..7c2bf89 100644 --- a/server/src/sql.cpp +++ b/server/src/sql.cpp @@ -5,23 +5,6 @@ #include -const char create_table[] = "CREATE TABLE IF NOT EXISTS prim_table ( ID INTEGER PRIMARY KEY, GEN INTEGER, HASH INTEGER, POS INTEGER, LIKED INTEGER);"; - -const char create_index[] = "CREATE INDEX IF NOT EXISTS idx_id ON prim_table (ID);"; - -constexpr char DB_NAME[] = "data.db"; - -static int callback(void *NotUsed, int argc, char **argv, char **azColName) -{ - int i; - for (i = 0; i < argc; i++) - { - printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); - } - printf("\n"); - return 0; -} - void sql::init() { sqlite3_initialize(); @@ -43,7 +26,7 @@ void sql::init() } /* Execute SQL statement */ - rc = sqlite3_exec(db, create_table, callback, 0, &zErrMsg); + rc = sqlite3_exec(db, create_table, nullptr, 0, &zErrMsg); if (rc != SQLITE_OK) { @@ -56,7 +39,7 @@ void sql::init() } /* Execute SQL statement */ - rc = sqlite3_exec(db, create_index, callback, 0, &zErrMsg); + rc = sqlite3_exec(db, create_index, nullptr, 0, &zErrMsg); if (rc != SQLITE_OK) {