// Conn.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include"stdafx.h"
#include <iostream>
//#include <sys/resource.h>
//#include <pthread.h>
//#include "mysql.h"
//#include "mysql\mysql.h"
#include<thread>
#include<process.h>
#include"mysql.h"



using namespace std;

//MySQL server details
//Modify to match your server details
#define HOST "192.168.3.26" 
#define PORT 3333  
#define USERNAME "ssl2" 
#define PASSWORD "ssl2" 
#define SSL_CA_CERT "C:\\Users\\deep\\Documents\\cert\\ca-cert.pem"
#define SSL_CLIENT_KEY "C:\\Users\\deep\\Documents\\cert\\client-key-ubuntu16.pem"
#define SSL_CLIENT_CERT "C:\\Users\\deep\\Documents\\cert\\client-cert-ubuntu16.pem"
#define SSL_CA_PATH "C:\\Users\\deep\\Documents\\cert\\"

void connect();

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();

	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();

	}



	/*
	* 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";
	}

	mysql_ssl_set(mysql, SSL_CLIENT_KEY, SSL_CLIENT_CERT, 
		SSL_CA_CERT, NULL,
		 NULL);
	
	/*
	* 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, 1) == 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();

	}
	else
	{
		cout << "\nConnection is made"<<endl;
	}


	/*
	* 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();


}




