<commit_before>/**
	Functions used for accessing the RTR database.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <inttypes.h>

#include <my_global.h>
#include <mysql.h>

#include "connect.h"
#include "db-internal.h"
#include "logging.h"
#include "prep-stmt.h"
#include "rtr.h"
#include "util.h"


struct query_state {
    uint32_t ser_num;   // ser_num to search for first row to send
    uint64_t first_row; // first row to send.  zero-based
    int bad_ser_num;    // neither the given ser_num, nor its successor, exist
    int data_sent;      // true if a pdu has been created for this serial/reset query
    int no_new_data;    // the given ser_num exists, but its successor does not
    int not_ready;      // no valid ser_nums exist, yet
    uint16_t session;   // the session_id number
};


/**=============================================================================
------------------------------------------------------------------------------*/
int db_rtr_get_session_id(dbconn *conn, session_id_t *session) {
    MYSQL_STMT *stmt = conn->stmts[DB_CLIENT_TYPE_RTR][DB_PSTMT_RTR_GET_SESSION];
    int ret;

    if (wrap_mysql_stmt_execute(conn, stmt, "mysql_stmt_execute() failed")) {
        return -1;
    }

    MYSQL_BIND bind[1];
    memset(bind, 0, sizeof(bind));
    uint16_t db_session;
    // session_id parameter
    bind[0].buffer_type= MYSQL_TYPE_SHORT;
    bind[0].buffer= &db_session;

    if (mysql_stmt_bind_result(stmt, bind)) {
        LOG(LOG_ERR, "mysql_stmt_bind_result() failed");
        LOG(LOG_ERR, "    %u: %s\n", mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
        mysql_stmt_free_result(stmt);
        return -1;
    }

    if (mysql_stmt_store_result(stmt)) {
        LOG(LOG_ERR, "mysql_stmt_store_result() failed");
        LOG(LOG_ERR, "    %u: %s\n", mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
        mysql_stmt_free_result(stmt);
        return -1;
    }

    ret = mysql_stmt_fetch(stmt);
    if (ret != 0) {
        LOG(LOG_ERR, "mysql_stmt_fetch() failed");
        if (ret == 1)
	        LOG(LOG_ERR, "    %u: %s\n", mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
        mysql_stmt_free_result(stmt);
        return -1;
    }

    *session = db_session;
    mysql_stmt_free_result(stmt);

    return 0;
}


/**=============================================================================
 * @note Does not matter if serial_num is not null, or has_full.<commit_msg>oops, just made a mistake with whitespace<commit_after>/**
	Functions used for accessing the RTR database.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <inttypes.h>

#include <my_global.h>
#include <mysql.h>

#include "connect.h"
#include "db-internal.h"
#include "logging.h"
#include "prep-stmt.h"
#include "rtr.h"
#include "util.h"


struct query_state {
    uint32_t ser_num;   // ser_num to search for first row to send
    uint64_t first_row; // first row to send.  zero-based
    int bad_ser_num;    // neither the given ser_num, nor its successor, exist
    int data_sent;      // true if a pdu has been created for this serial/reset query
    int no_new_data;    // the given ser_num exists, but its successor does not
    int not_ready;      // no valid ser_nums exist, yet
    uint16_t session;   // the session_id number
};


/**=============================================================================
------------------------------------------------------------------------------*/
int db_rtr_get_session_id(dbconn *conn, session_id_t *session) {
    MYSQL_STMT *stmt = conn->stmts[DB_CLIENT_TYPE_RTR][DB_PSTMT_RTR_GET_SESSION];
    int ret;

    if (wrap_mysql_stmt_execute(conn, stmt, "mysql_stmt_execute() failed")) {
        return -1;
    }

    MYSQL_BIND bind[1];
    memset(bind, 0, sizeof(bind));
    uint16_t db_session;
    // session_id parameter
    bind[0].buffer_type= MYSQL_TYPE_SHORT;
    bind[0].buffer= &db_session;

    if (mysql_stmt_bind_result(stmt, bind)) {
        LOG(LOG_ERR, "mysql_stmt_bind_result() failed");
        LOG(LOG_ERR, "    %u: %s\n", mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
        mysql_stmt_free_result(stmt);
        return -1;
    }

    if (mysql_stmt_store_result(stmt)) {
        LOG(LOG_ERR, "mysql_stmt_store_result() failed");
        LOG(LOG_ERR, "    %u: %s\n", mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
        mysql_stmt_free_result(stmt);
        return -1;
    }

    ret = mysql_stmt_fetch(stmt);
    if (ret != 0) {
        LOG(LOG_ERR, "mysql_stmt_fetch() failed");
        if (ret == 1)
            LOG(LOG_ERR, "    %u: %s\n", mysql_stmt_errno(stmt), mysql_stmt_error(stmt));
        mysql_stmt_free_result(stmt);
        return -1;
    }

    *session = db_session;
    mysql_stmt_free_result(stmt);

    return 0;
}


/**=============================================================================
 * @note Does not matter if serial_num is not null, or has_full.
{'fraction_non_alphanumeric': 0.2066972294199721, 'fraction_numerical': 0.007574247558301774, 'mean_word_length': 2.976228209191759, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1038836', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1654, 'n_words': 387}