示例#1
0
/**=============================================================================
------------------------------------------------------------------------------*/
int stmtAddAll(
    dbconn * conn)
{
    int client_type,
        qry_num;

    // initialize to NULL so stmtDeleteAll can be called below
    for (client_type = 0; client_type < DB_CLIENT_NUM_TYPES; ++client_type)
    {
        conn->stmts[client_type] = NULL;
    }

    for (client_type = 0; client_type < DB_CLIENT_NUM_TYPES; ++client_type)
    {
        if (!((1 << client_type) & conn->client_flags))
            continue;

        // find number of queries
        for (qry_num = 0; queries[client_type][qry_num] != NULL; ++qry_num)
        {
        }

        conn->stmts[client_type] = calloc(qry_num, sizeof(MYSQL_STMT *));
        if (conn->stmts[client_type] == NULL)
        {
            LOG(LOG_ERR, "could not alloc array of statement handles");
            stmtDeleteAll(conn);
            return -1;
        }

        for (qry_num = 0; queries[client_type][qry_num] != NULL; ++qry_num)
        {
            if (stmtAdd(conn, client_type, qry_num) != 0)
            {
                LOG(LOG_ERR, "could not prepare a statement");
                stmtDeleteAll(conn);
                return -1;
            }
        }
    }

    return 0;
}
示例#2
0
文件: connect.c 项目: dseomn/rpstir
/*==============================================================================
------------------------------------------------------------------------------*/
void db_disconnect(
    dbconn * conn)
{
    if (conn)
    {
        stmtDeleteAll(conn);

        free(conn->host);
        conn->host = NULL;
        free(conn->user);
        conn->user = NULL;
        free(conn->pass);
        conn->pass = NULL;
        free(conn->db);
        conn->db = NULL;

        mysql_close(conn->mysql);

        free(conn);
    }
}
示例#3
0
文件: connect.c 项目: dseomn/rpstir
/*==============================================================================
------------------------------------------------------------------------------*/
int reconnectMysqlCApi(
    dbconn ** old_conn)
{
    dbconn *conn = *old_conn;
    MYSQL *mysql = conn->mysql;

    stmtDeleteAll(conn);

    // @see MySQL 5.1 Reference Manual, section 20.9.3.49 under
    // MYSQL_OPT_RECONNECT for the reason for this unusual sequence.
    my_bool reconnect = 0;
    if (mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect))
    {
        LOG(LOG_WARNING, " MySQL reconnect option might not be set properly");
    }
    if (!mysql_real_connect(mysql, conn->host, conn->user, conn->pass,
                            conn->db, 0, NULL, 0))
    {
        LOG(LOG_ERR, "could not reconnect to MySQL db");
        LOG(LOG_ERR, "    %u: %s", mysql_errno(mysql), mysql_error(mysql));
        if (mysql)
        {
            mysql_close(mysql);
        }
        return -1;
    }
    if (mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect))
    {
        LOG(LOG_WARNING, " MySQL reconnect option might not be set properly");
    }

    if (stmtAddAll(conn) != 0)
    {
        db_disconnect(conn);
        return -1;
    }

    return 0;
}