
/* OpenWebSpider
 *
 *  Authors:     Stefano Alimonti AND Stefano Fantin
 *  Version:     0.8
 *  E-Mails:     shen139 [at] openwebspider (dot) org AND stefanofantinguz@yahoo.it
 *
 *
 * This file is part of OpenWebSpider
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef __TEMPTABLE
#define __TEMPTABLE

void RandomTable(char* table)
{
char key[5];

	srand((unsigned)GetTickCount());
	key[0] = (char) (rand() % 21)+'a';
	key[1] = (char) (rand() % 21)+'a';
	key[2] = (char) (rand() % 21)+'a';
	key[3] = (char) (rand() % 21)+'a';
	key[4] = 0;
	strcpy(table,"OTP");
	strcat(table,key);

return;
}

int CreateTmpTable(char* table)
{
char sqlQuery[MAXQUERYSIZE];
MYSQL_RES gRes;
MYSQL_RES** tmpRes=NULL;
int ret;

	my_mysql_ping(&gMysqlDB2,BLOCKINDEX);

	sprintf(sqlQuery,"SELECT idtable FROM tablelist WHERE name='%s' limit 1",table);

	tmpRes=(MYSQL_RES**)malloc(sizeof(MYSQL_RES));
	
	if(tmpRes==NULL)
		MemoryCorruptedHandler("CreateTmpTable");

	ret=my_mysql_query_and_store_results(&gMysqlDB2, sqlQuery,tmpRes,&gRes,BLOCKINDEX);	//May return null!!!

	if(ret==-1)
	{
		ERROR_LOG(mysql_error(&gMysqlDB2))
		fprintf(stderr, "\r\nSQL ERROR (DB: %s): %s\r\n",DB2,mysql_error(&gMysqlDB2));

		FREE(tmpRes);

		mysql_close(&gMysqlDB1);
		mysql_close(&gMysqlDB2);
		
		exit(0);
	}

	if(mysql_affected_rows(&gMysqlDB2)==0)			//Table does not exists
	{
		if(*tmpRes)
		{
			mysql_free_result(*tmpRes);
		}

		FREE(tmpRes);
	
		//Insert table name in the table list
		sprintf(sqlQuery,"INSERT INTO tablelist SET name ='%s',status=1,strdate=curdate()",table);		//Status = 1 = scanning
		my_mysql_query(&gMysqlDB2, sqlQuery,NO_BLOCK);

		//Create Table
		sprintf(sqlQuery,CREATE_TMP_TABLE(table));
		my_mysql_query(&gMysqlDB2,sqlQuery,NO_BLOCK);

		return 1;
	}
	else
	{
		if(*tmpRes)
		{
			mysql_free_result(*tmpRes);
		}

		FREE(tmpRes);
	
		return 0;
	}

}


int FlushTempTable(char* table)
{
char sqlQuery[MAXQUERYSIZE];

	my_mysql_ping(&gMysqlDB2,NO_BLOCK);

	//Direct copy
	
	printf("\r\nSwapping temporary table to the index...");
	sprintf(sqlQuery,"INSERT INTO %s.pagelist (host_id,hostname,page,`title`,`text`,`cache`,version,date,time,level) select host_id,hostname,page,`title`,`text`,`cache`,version,date,time,level from %s;",DB2,table);
	my_mysql_query(&gMysqlDB2, sqlQuery,NO_BLOCK);
	printf("OK\r\n");
	

	printf("\r\nFlushing temporary data...");
	sprintf(sqlQuery,"DELETE FROM %s;",table);
	my_mysql_query(&gMysqlDB2, sqlQuery,NO_BLOCK);
	printf("OK\r\n\r\n");

return 1;
}

int DropTempTable(char* table)
{
char sqlQuery[MAXQUERYSIZE];

	my_mysql_ping(&gMysqlDB2,NO_BLOCK);

	printf("Deleting temporary data...");
	sprintf(sqlQuery,"DELETE FROM tablelist WHERE name='%s'",table);
	my_mysql_query(&gMysqlDB2, sqlQuery,NO_BLOCK);
	printf("OK\r\n");
	

	
	printf("Dropping temporary data...");
	sprintf(sqlQuery,"DROP TABLE %s",table);
	my_mysql_query(&gMysqlDB2,sqlQuery,NO_BLOCK);
	printf("OK\r\n");

return 1;
}

#endif

/*EOF*/

