
/* OpenWebSpider
 *
 *  Author:     Stefano Alimonti aka Shen139
 *  Mail:       shen139 [at] openwebspider (dot) org
 *
 *
 * 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(&gMysqlDB3,NO_BLOCK);

	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(&gMysqlDB3, sqlQuery,tmpRes,&gRes,NO_BLOCK);	//May return null!!!

	if(ret==-1)
	{
		ERROR_LOG(mysql_error(&gMysqlDB3))
		fprintf(stderr, "\r\nSQL ERROR (DB: %s): %s\r\n",DB3,mysql_error(&gMysqlDB3));

		FREE(tmpRes);

		mysql_close(&gMysqlDB1);
		mysql_close(&gMysqlDB2);
		mysql_close(&gMysqlDB3);
		
		exit(0);
	}

	if(mysql_affected_rows(&gMysqlDB3)==0)			//Table does not exists
	{
		if(*tmpRes)
		{
			mysql_free_result(*tmpRes);
		}

		FREE(tmpRes);
	
		//Insert table name in the table list
		sprintf(sqlQuery,"INSERT DELAYED INTO tablelist SET name ='%s',status=1,strdate=curdate()",table);		//Status = 1 = scanning
		my_mysql_query(&gMysqlDB3, sqlQuery,NO_BLOCK);

		//Create Table
		sprintf(sqlQuery,CREATE_TMP_TABLE(table));
		my_mysql_query(&gMysqlDB3,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(&gMysqlDB3,NO_BLOCK);

	//Direct copy
	
	printf("\r\nSwapping temporany table to the index...");
	sprintf(sqlQuery,"INSERT DELAYED INTO %s.pagelist (hostname,page,description,html,htmlcache,version,date,time,level) select hostname,page,description,html,htmlcache,version,date,time,level from %s;",DB2,table);
	my_mysql_query(&gMysqlDB3, sqlQuery,NO_BLOCK);
	printf("OK\r\n");
	

	printf("\r\nFlushing temporany data...");
	sprintf(sqlQuery,"DELETE FROM %s;",table);
	my_mysql_query(&gMysqlDB3, sqlQuery,NO_BLOCK);
	printf("OK\r\n");

return 1;
}

int DropTempTable(char* table)
{
char sqlQuery[MAXQUERYSIZE];

	my_mysql_ping(&gMysqlDB3,NO_BLOCK);

	printf("Deleting temporany data...");
	sprintf(sqlQuery,"DELETE FROM tablelist WHERE name='%s'",table);
	my_mysql_query(&gMysqlDB3, sqlQuery,NO_BLOCK);
	printf("OK\r\n");
	

	
	printf("Dropping temporany data...");
	sprintf(sqlQuery,"DROP TABLE %s",table);
	my_mysql_query(&gMysqlDB3,sqlQuery,NO_BLOCK);
	printf("OK\r\n");

return 1;
}

#endif

/*EOF*/

