nedoPC.org

Electronics hobbyists community established in 2002
Atom Feed | View unanswered posts | View active topics It is currently 28 Mar 2024 18:01



Reply to topic  [ 1 post ] 
[SRC] Программа для разделения MySQL дампа на части 
Author Message
Admin
User avatar

Joined: 08 Jan 2003 23:22
Posts: 22412
Location: Silicon Valley
Reply with quote
Во время очередного переноса БД форума с сервера на сервер (ноябрь 2005 года) возникла задача написать вот такую программу.

Иногда phpMyAdmin отказывается кушать большие дампы для восстановления базы данных - тогда возникает необходимость разбить дамп на части. В этом может помочь данная сишная прогрмма, которая кроме разбивания еще и архивирует части с помощью утилиты gzip:

Code:
/* resql.c - Alexander Shabarshin  01.11.2005 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN 65536
#define SLIMIT 5000
#define ILIMIT 1000

char str[MAXLEN];

int main(int argc, char **argv)
{
 int i,t,num,line,lines,instr,len,np,nq,work;
 char ss[256],*po,quote=0x27;
 FILE *f,*fo=NULL;
 if(argc<2) return 1;
 f = fopen(argv[1],"rt");
 num = 1;
 line = 0;
 lines = 0;
 instr = 0;
 np = 0;
 nq = 0;
 work = 1;
 while(work)
 {
   fgets(str,MAXLEN,f);
   line++;
   lines++;
   if(feof(f)) break;
   len = strlen(str);
   if(str[len-1]!='\n') printf("ERROR-01 %i\n",line);
   if(!strncmp(str,"DROP TABLE",10))
   { instr++;
     if(nq||np){printf("ERROR-02 [%i] %s\n",line,str);break;}
   } else     
   if(!strncmp(str,"CREATE TABLE",12))
   { instr++;
     if(nq||np){printf("ERROR-03 [%i] %s\n",line,str);break;}
   } else     
   if(!strncmp(str,"INSERT INTO",11))
   { instr++;
     if(nq||np){printf("ERROR-04 [%i] %s\n",line,str);break;}
   }     
   for(i=0;i<len;i++)
   {
     if(str[i]==quote)
     {
       t = 1;
       if(i>0 && str[i-1]=='\\') t = 0;
       if(i>1 && str[i-1]=='\\' && str[i-2]=='\\') t = 1;
       if(t)
       {   
         if(nq) nq=0;
         else nq=1;
       }   
     }
     else if(!nq && str[i]=='(') np++;
     else if(!nq && str[i]==')') np--;
     if(np<0)
     {str[i+1]=0;printf("ERROR-11 [%i] %s\n",line,str);work=0;break;}
     if(np>1&&nq)
     {str[i+1]=0;printf("ERROR-12 [%i] %s\n",line,str);work=0;break;}
   }
   if(fo==NULL)
   {
     sprintf(ss,"%04d.sql",num++);
     fo = fopen(ss,"wt");
   }     
   fprintf(fo,"%s",str);
   if((lines >= SLIMIT || instr>=ILIMIT) && !nq && !np)
   {
      fclose(fo);
      sprintf(ss,"gzip %04d.sql",num-1);
      system(ss);
      printf("%i) %i lines %i instructions (%i)\n",num-1,lines,instr,line);
      lines = 0;
      instr = 0;
      fo = NULL;
   }
 }
 if(fo!=NULL)
 {
   fclose(fo);
   sprintf(ss,"gzip %04d.sql",num-1);
   system(ss);
   printf("%i) %i lines %i instructions (%i)\n",num-1,lines,instr,line);
 } 
 fclose(f);
 printf("%i lines\n",line);
 printf("nq=%i np=%i\n",nq,np);
 return 0;
}

_________________
:dj: https://mastodon.social/@Shaos


19 Apr 2007 21:05
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 25 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.