//build:  gcc testcase.c -g -o testcase -L/home/sbester/server/5.0/mysql-5.0-bk/lib -I/home/sbester/server/5.0/mysql-5.0-bk/include  -lmysqlclient_r -lz -lpthread
/*This is generated code!*/

/*section: includes*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mysql.h>
#include <pthread.h>

#define NUMTHREADS (25)

/*section: globals*/
char host[]="127.0.0.1";
char username[]="root";
char password[]="";
char database[]="test";
int port=3306;
pthread_t pthreads[NUMTHREADS];
int threaddone=0;

/*section: function declarations*/
void *worker_thread(void *arg);
MYSQL *db_connect(MYSQL *dbc);

/*section: functions*/
int db_query(MYSQL *dbc,char *sql)
{
	int res=0;
	MYSQL_RES *r=NULL;
	MYSQL_ROW w;
	MYSQL_FIELD *field=NULL;
	int i=0,moreresult=0;
	res = mysql_query(dbc,sql);
	if(res != 0)
	{
		printf("query failed '%s' : %d (%s)\n",sql,mysql_errno(dbc),mysql_error(dbc));
		return 0;
	}

	do
	{
		r = mysql_store_result(dbc);
		if(r)
		{
			unsigned int numfields = mysql_num_fields(r);
			unsigned int numrows=mysql_num_rows(r);
			while((field = mysql_fetch_field(r)))
			{
					//print metadata information about each field
			}
			while ((w = mysql_fetch_row(r)))
			{
				for(i = 0; i < numfields; i++)
				{
					//print each field here
				}

			}
			mysql_free_result(r);
		}
		else //no rows returned. was it a select?
		{
			if(mysql_field_count(dbc)>0)
			{
				printf("No results for '%s'.  (%d) - %s\n",sql,mysql_errno(dbc),mysql_error(dbc));
			}
			else //it could have been some insert/update/delete
			{
				//this is successful query
			}
		}
		moreresult=mysql_next_result(dbc);
		if(moreresult>0)
		{
			printf("mysql_next_result returned %d, mysql error %s, (%d)\n",moreresult,mysql_error(dbc),mysql_errno(dbc));
			break;
		}
	} while (0==moreresult);
}

void *worker_thread(void *arg)
{
	MYSQL *dbc=NULL;
	int cancelstate=0;
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&cancelstate);

	if (!(dbc = mysql_init(NULL)))
	{
		printf("mysql_init\n");
		dbc=NULL;
		goto threadexit;
	}
	else
	{
		if (!mysql_real_connect(dbc,host,username,password,database,port, NULL, CLIENT_FOUND_ROWS|CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS))
		{
			printf("mysql_real_connect failed: %s (%d)", mysql_error(dbc),mysql_errno(dbc));
			dbc=NULL;
		}
	}

	unsigned int counter=0;
	while(!threaddone && dbc!=NULL)
	{
		db_query(dbc,"alter table `bug` disable keys");
		db_query(dbc,"alter table `bug_2` disable keys");
		db_query(dbc,"insert delayed into `bug_2`(`name`,`add1`,`add2`) values ('Rhynd','eyedropper','alacks'),('dissects','Aquarius\\'s','Sorel'),('Delano','acetylide','toboggans'),('frightfulness\\'s','stratus','cloggier'),('remembrance','filleter','definitions')");
		db_query(dbc,"select `id`,`name` from `bug_2` where `add1` = 'carapace\\'s' or `add2` = 'trustily'");
		db_query(dbc,"select `id`,`name` from `bug_2` where `add1` = 'adjustable' or `add2` = 'brouhaha\\'s'");
		db_query(dbc,"select `id`,`name` from `bug_2` where `add1` = 'fasted' or `add2` = 'protestation'");
		db_query(dbc,"select `id`,`name` from `bug_2` where `add1` = 'dislimn' or `add2` = 'Boas\\'s'");
		db_query(dbc,"select `id`,`name` from `bug_2` where `add1` = 'Guerin\\'s' or `add2` = 'quites'");
		db_query(dbc,"insert delayed into `bug`(`name`,`add1`,`add2`) values ('magnetoelectricity\\'s','creditable','fashioners'),('spongin\\'s','initiatory','slumbrous'),('Fong','pointelles','Maitreya\\'s'),('albatross\\'s','authenticator','Gibeon'),('sienna\\'s','diluter','Basque')");
		db_query(dbc,"select `id`,`name` from `bug` where `add1` = 'partied' or `add2` = 'moggie'");
		db_query(dbc,"select `id`,`name` from `bug` where `add1` = 'heckler' or `add2` = 'turbid'");
		db_query(dbc,"select `id`,`name` from `bug` where `add1` = 'squawkers' or `add2` = 'Beatty\\'s'");
		db_query(dbc,"select `id`,`name` from `bug` where `add1` = 'stratagem\\'s' or `add2` = 'greenhouse'");
		db_query(dbc,"select `id`,`name` from `bug` where `add1` = 'Malathion\\'s' or `add2` = 'destruction\\'s'");
		db_query(dbc,"alter table `bug` enable keys");
		db_query(dbc,"alter table `bug_2` enable keys");
		db_query(dbc,"flush tables");
		//db_query(dbc,"SHOW /*!50100 GLOBAL*/ STATUS");
	}
threadexit:
	mysql_close(dbc);
	mysql_thread_end();
	pthread_exit(0);
}


int main(int argc, const char *argv[])
{
	MYSQL *dbc=NULL;
	int i=0,err=0;

	time_t timestart=0,timenow=0;

	my_init();
	if (!(dbc = mysql_init(NULL)))
	{
		printf("mysql_init\n");
		dbc=NULL;
		goto threadexit;
	}
	else
	{
		if (!mysql_real_connect(dbc,host,username,password,database,port, NULL, CLIENT_FOUND_ROWS|CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS))
		{
			printf("mysql_real_connect failed: %s (%d)", mysql_error(dbc),mysql_errno(dbc));
			dbc=NULL;
			goto threadexit;
		}
	}

	printf("running initializations");
	db_query(dbc,"drop table if exists `bug`");
	db_query(dbc,"create table `bug`(`id` int not null auto_increment primary key,`name` varchar(255), `add1` varchar(255), `add2` varchar(255),key(`add1`,`add2`),key(`add2`,`add1`))engine=myisam");
	db_query(dbc,"drop table if exists `bug_2`");
	db_query(dbc,"create table `bug_2`(`id` int not null auto_increment primary key,`name` varchar(255), `add1` varchar(255), `add2` varchar(255),key(`add1`,`add2`),key(`add2`,`add1`))engine=myisam");
	mysql_close(dbc);

	for (i=0;i<NUMTHREADS;i++)
	{
		err=pthread_create(&pthreads[i], NULL, worker_thread, (void *)i);
		if(err!=0)
		{
			printf("error spawning thread %lu, pthread_create returned %lu\n",(unsigned long)i,(unsigned long)err);
		}
	}
	printf("completed spawning new database worker threads\n");

	timestart=time(NULL);
	timenow=time(NULL);
	for(i=0;(timenow-timestart) < 3600;timenow=time(NULL))
	{
		sleep(1);
	}
	threaddone=1;

	printf("waiting for worker threads to finish\n");

	for (i=0;i<NUMTHREADS;i++)
	{
		pthread_join(pthreads[i], NULL);
	}
threadexit:
}


