#include <iostream>
#include <sys/resource.h>
#include "mysql/mysql.h"

using namespace std;

void Connect();

//MySQL server details
//Modify to match your server details
#define HOST "127.0.0.1"
#define PORT 3306
#define USERNAME "root"
#define PASSWORD ""

int main() {
    /*
     * mysql_library_init() - initializes the MySQL library before any other MySQL function is called.
     * 
     * Refer: http://dev.mysql.com/doc/refman/5.0/en/mysql-library-init.html
     * 
     * Returns:
     * 0 (Zero) if successful. Nonzero if an error occurred.
     */
    if (mysql_library_init(0, NULL, NULL)) {
        cout << "mysql_library_init() is un-successful\n";
        cout << "Exiting program\n";
        return -1;
    } else {
        cout << "mysql_library_init() is successful\n";
    }

	Connect();
    /*
     * mysql_library_end() - finalizes the MySQL library. This is called when done using the library.
     * 
     * Refer: http://dev.mysql.com/doc/refman/5.0/en/mysql-library-end.html
     * 
     * Returns: none
     */
    mysql_library_end();

    return 0;
}

void Connect() {
    /*
     * Calling implicit call to mysql_thread_init(). 
     * 
     * Refer: http://dev.mysql.com/doc/refman/5.0/en/mysql-thread-init.html
     * 
     */
    mysql_thread_init();

    cout<<"Client version: "<< mysql_get_client_info()<<endl;
    int connect_timeout(30);

    MYSQL * mysql;
    MYSQL_RES * res;

    /*
     * Initializing a MySQL object suitable for mysql_real_connect()
     * if the object initialized is NULL then we are returning from the function. 
     * 
     * Refer: http://dev.mysql.com/doc/refman/5.0/en/my-init.html
     * 
     * Returns: none
     */
    mysql = mysql_init(NULL);
    if (mysql == NULL) {
        cout << "Failed to initialize the MySQL object\n";
        mysql_thread_end();
        return;
    }

    /*
     * Setting up MySQL client connect timeout to 30 seconds.
     * MYSQL_OPT_CONNECT_TIMEOUT (argument type: unsigned int *)
     * Connect timeout is in seconds.
     * 
     * Refer: http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html
     * 
     * Returns:
     * 0 (Zero) for success. Nonzero if unknown option specified.
     */
   if (mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, (const char *) &connect_timeout)) {
        cout << "Unknown option set\n";
    }

    /*
     * attempting to establish connection to a MySQL Server.
     * 
     * Refer: http://dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html
     * 
     * Returns:
     * A MYSQL* connection handle if the connection was successful, 
     * NULL if the connection was unsuccessful.
     */
    if (mysql_real_connect(mysql, HOST, USERNAME, PASSWORD, NULL, PORT, NULL, 0) == NULL) {
        cout << "Error mysql_real_connect: " << mysql_error(mysql) << "\n";
        if (mysql) {
            mysql_close(mysql);
        }

        /*
         * Freeing memory allocated by mysql_thread_init().
         */
        mysql_thread_end();
        return;
    }
	cout << "Suucessfully connected " << "\n";

    /*
     * Executing SQL statement.
     * 
     * Refer: http://dev.mysql.com/doc/refman/5.0/en/mysql-real-query.html
     * 
     * Returns: 
     * 0 (Zero) if the statement was successful. Nonzero if an error occurred.
    */
    if (mysql_query(mysql, "SHOW GRANTS")) {
        cout << "Error mysql_query: " << mysql_error(mysql) << "\n";
        if (mysql) {
            mysql_close(mysql);
        }

        /*
         * Freeing memory allocated by mysql_thread_init().
         */
         mysql_thread_end();
         return;
    }
	cout << "Suucessfully executed " << "\n";

    /*
    * Storing the result to MYSQL_RES, to reuse the connection for querying again
    * and then freeing the result set. 
    * 
    * Refer: http://dev.mysql.com/doc/refman/5.0/en/mysql-store-result.html
    * Refer: http://dev.mysql.com/doc/refman/5.0/en/mysql-free-result.html
    */
    res = mysql_store_result(mysql);
    mysql_free_result(res);

    /*
     * Closing a previously opened connection.
     * 
     * Refer: http://dev.mysql.com/doc/refman/5.0/en/mysql-close.html
     */
    mysql_close(mysql);

    /*
     * Freeing memory allocated by mysql_thread_init().
     * 
     * Refer: http://dev.mysql.com/doc/refman/5.0/en/mysql-thread-end.html
     */
    mysql_thread_end();

    return ;
}
