Exemplo n.º 1
0
/* bool update_record(id, table_name, sid, o) */
static int cc_update_record(lua_State* L)
{
	int id;
	int64_t sid;
	size_t len;
	const char* data;
	const char* table_name;
	bool retcode;
	database_handler_t* database_handler;
	worker_handler_t* handler = find_handler_by_stack(L);
	int args = lua_gettop(L);
	if (args < 4)
	{
		error_log("`%s` parameter lack:%d\n", __FUNCTION__, args);
		return 0;
	}

	if (!lua_isnumber(L, -args))
	{
		error_log("`%s` parameter error:%s\n", __FUNCTION__, lua_typename(L, lua_type(L, -args)));
		return 0;
	}

	if (!lua_isstring(L, -(args-1)))
	{
		error_log("`%s` parameter error:%s\n", __FUNCTION__, lua_typename(L, lua_type(L, -(args-1))));
		return 0;
	}

	if (!lua_isnumber(L, -(args-2)))
	{
		error_log("`%s` parameter error:%s\n", __FUNCTION__, lua_typename(L, lua_type(L, -(args-2))));
		return 0;
	}

	if (!lua_isstring(L, -(args-3)))
	{
		error_log("`%s` parameter error:%s\n", __FUNCTION__, lua_typename(L, lua_type(L, -(args-3))));
		return 0;
	}

	id = lua_tointeger(L, -args);
	table_name = lua_tostring(L, -(args-1));
	sid = lua_tonumber(L, -(args-2));
	len = 0;
	data = lua_tolstring(L, -(args-3), &len);
	database_handler = hash_table_find(handler->database_table, id);
	if (!database_handler)
	{
		error_log("`%s` not found database:%d\n", __FUNCTION__, id);
		return 0;
	}
	retcode = record_update(database_handler, table_name, sid, data, len);
	lua_pushboolean(L, retcode ? 1 : 0);

	return 1;
}
Exemplo n.º 2
0
void menu()
{
 emp_rec ser_emp;
 int c=0,emp_no;
 long pos;
 FILE * fp,* eno,* delemp;
 if((fp=fopen("emp.dat","rb+"))==NULL)
 	{
	printf("Error 1: Problem with main file opening.\n");
	exit(ferror(fp));
	}
if((eno=fopen("empno.dat","rb+"))==NULL)
	{
	printf("Error 2: Problem with employee no file.\n");
	exit(ferror(fp));
	}
if((delemp=fopen("delemp.dat","rb+"))==NULL)
	{
	printf("Error 3: Problem with deleted employee file.\n");
	delemp=fopen("delemp.dat","wb+");
	}


	

 while(c!=6)
 	{
	printf("\t\t\t\t Menu for Employee Record Base\n");
	printf("\t\t\t1.Employee Record Insertion\n");
	printf("\t\t\t2.Employee Record Search\n");
	printf("\t\t\t3.Employee Record Update\n");
	printf("\t\t\t4.Employee Record Delete\n");
	printf("\t\t\t5.Display All Employees\n");
	printf("\t\t\t6.Exit the program.\n");

	printf("\t\t\t\t Enter your choice:");
	scanf("%d",&c);
		switch(c)
		{
			case 1:
				record_insert(fp,eno);
				break;
			case 2:
				printf("Enter the employee no to search:");
				scanf("%d",&emp_no);
				if((pos=record_search(fp,emp_no))==-1)
				   printf("Employee does not exist.\n");
				else
				{
				   printf("Employee exists.\n");
				   fseek(fp,pos,SEEK_SET);
				   fread(&ser_emp,sizeof(emp_rec),1,fp);
	printf("\t\t\tDetails of Employee No: %d\n",ser_emp.emp_no);
	printf("\tEmployee Name:%s\n",ser_emp.emp_name);
	printf("\tEmployee Salary:%lf",ser_emp.emp_sal);
	printf("\tEmployee Address:%s\n",ser_emp.emp_add);
	printf("\tEmployee Mobile:%s\n",ser_emp.emp_mobile);
				}
				break;
			case 3:
				record_update(fp);
				break;
			case 4:
				record_delete(fp,delemp);
				break;
			case 5:
				fseek(fp,0,SEEK_END);
				pos=ftell(fp);
				fseek(fp,0,SEEK_SET);
				while((pos!=ftell(fp))&&(!feof(fp)))
				{
				fread(&ser_emp,sizeof(emp_rec),1,fp);
				if(ser_emp.emp_no!=-1)
				{
	printf("\t\t\tDetails of Employee No: %d\n",ser_emp.emp_no);
	printf("\tEmployee Name:%s\n",ser_emp.emp_name);
	printf("\tEmployee Salary:%lf",ser_emp.emp_sal);
	printf("\tEmployee Address:%s\n",ser_emp.emp_add);
	printf("\tEmployee Mobile:%s\n",ser_emp.emp_mobile);
	printf("\n\n");		}	

				}
				break;
			case 6:
				printf("Exiting...and closing the files\n");
				fclose(fp);
				fclose(eno);
				fclose(delemp);
				break;
		}


	}

}