InsertDB.cpp
上传用户:haiweijt
上传日期:2018-02-23
资源大小:8195k
文件大小:9k
- // TMyBuffer.cpp: implementation of the TMyBuffer class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "InsertDB.h"
- #include <stdio.h>
- #include <string.h>
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- InsertDB::InsertDB()
- {
- m_Offset = 0;//行中的字符索引
- m_flag=10; //标记命令索引
- m_zhizhen=0; //标记是否读取字段名行并与配置命令所在的字段名行相符情况下的条件
- m_zhi=0; // 标记正在读取字段名行
- tco.Open();
- char ip[50];
- tco.ReadSystemString("IP", ip,32);
- char User[50];
- tco.ReadSystemString("User",User,sizeof(User));
- char PWD[50];
- tco.ReadSystemString("PWD",PWD,sizeof(PWD));
- char DB[50];
- tco.ReadSystemString("DB",DB,sizeof(DB));
- tn.Open(ip,User,PWD,DB);
- tn.Connect();
-
- char value[3];
- int count;
- int tt;
- char field[10][512];
- char v_field[6][50];
- char v_str[300];
- tco.ReadCount(value,sizeof(value));
- count=value[0]-48;//命令条数
- for(int t=0;t<count;t++)//将配置文件所有参数赋给文件变量
- {
- tco.ReadCmd(t,m_cmd[t],sizeof(m_cmd[t]));
- tco.ReadField(t,field[t],sizeof(field[t]));
- int m=0;
- tt=0;
- while(field[t][tt]!=' '&&(field[t][tt]!=' ')&&(field[t][tt]>0))
- {
- while(field[t][tt]==' ')
- {
- tt++;
- }
- int r=0;
- while((field[t][tt]!='^')&&(field[t][tt]!=' '))
- {
- if(field[t][tt]=='-')
- {
- m_field[t][m][r++]='_'; //建表时字段不允许“-”符号
- field[t][tt]=='_';
- tt++;
- }
- m_field[t][m][r++]=field[t][tt++];
-
- }
- m_field[t][m][r]=' ';
- m++;
- m_fies[t]=m+48;
- if(field[t][tt]!=' ')
- tt++;
- }
- }
- for(int n=0;n<count;n++)//建表
- { int i=0;
- int j=0;
- while(m_cmd[n][i]!=' ')
- {
- if(m_cmd[n][i]==' ')
- { i++;
- v_cmd[n][j++]='_';
- }else{
- v_cmd[n][j++]=m_cmd[n][i++];
- }
- }
- v_cmd[n][j]=' ';
- strcpy(v_str,"CREATE TABLE ");
- strcat(v_str,v_cmd[n]);
- strcat(v_str,"( ");
- int k;
- for(k=0;k<m_fies[n]-49;k++)
- {
- strcat(v_str,m_field[n][k]);
- strcat(v_str," [nvarchar](300) ,");
- }
- strcat(v_str,m_field[n][k]);
- strcat(v_str," [nvarchar](300) )");
- tn.Execute(v_str);
- strcpy(v_str,"");
-
- }
- }
- InsertDB::~InsertDB()
- {
-
- }
- void InsertDB::InsertInto(char *str)
- {
-
- char Fie[6][200]={{""}};
- char Do[50]; //一个字段还有多个分离的字符串
- char str1[500]="";
- char path[256];
- char cmd[100];
- int zd; //标记字段起始点
- int m_duozd=0; //标记遇到上条记录余下的记录
- // TNewADO* tn=new TNewADO;
- // char* s=tco->GetConfigFileName(path,256);
-
-
- int i=0;
- int j=0;
- while((str[i]!=' ')&&(str[i]!='>'))
- {
- cmd[j++]=str[i++];
- }
- cmd[j]=' ';
- if(strcmp(cmd,"TCS_Switch")==0)
- {
- strcpy(cmd,"");
- i++;
- j=0;
- while(str[i]!='x0d')
- {
- cmd[j++]=str[i++];
- }
- cmd[j]=' ';
- for(int t=0;t<10;t++)
- {
- if(strcmp(cmd,m_cmd[t])==0)
- { //判断命令属于配置文件中的第几条,并用m_flag标注
- m_flag=t;
- for(int fo=0;fo<6;fo++)
- {
- ziduan[fo]=0;
- }
- return;
- }
- if(t==9) return;
- }
-
- //printf("---%sn",cmd);
- }
- i=0;j=0;
- while((i<strlen(str))&&m_flag!=10)//这里默认设置最多配置10条命令
- {
- if((str[i]!=' '))//(str[i]!=' ')&&
- {
- while((str[i]=='-')&&(str[i+1]=='-'))
- return;
- zd=i;
- char zid[20]="";
- if(ziduan[0]==0){
- while(str[i]!=' ')
- {
- Fie[0][j++]=str[i++];
- }
- }else{
- while((i-zd)<ziduan[0]-1)
- {
- Fie[0][j++]=str[i++];
-
- int r;
- for( r=0;r<ziduan[0]-1;r++)
- {
- zid[r]=' ';
- }
- zid[r+1]=' ';
- }
- }
- Fie[0][j]=' ';//intferace
-
- //strcpy(v_duozd[0],Fie[0]);
- if(strcmp(Fie[0],m_field[m_flag][0])==0)
- { //判断命令后,在找出信息列表中的表头首字段,如果是就继续读取
- m_zhizhen=1;
- m_zhi=1;
- }
- if((strcmp(Fie[0],zid)==0))
- {
- m_duozd=1;
- }
- if(m_zhizhen==1)
- {
- j=0;
- if(m_zhi==1)
- {
- while(str[i]==' ')
- {
- i++;
- }
- if(m_zhi==1)
- ziduan[0]=i-zd;
- zd=i;
- while(str[i]!=' ')
- {
- if(str[i]=='-')
- {
- Fie[1][j++]='_';
- i++;
- }
- Fie[1][j++]=str[i++];
- }
-
- }else{
- zd=i;
- while((i-zd)<ziduan[1]-1){
- Fie[1][j++]=str[i++];
- }
- }
- Fie[1][j]=' ';
-
- //ip
- //strcpy(v_duozd[1],Fie[1]);
- j=0;
- if(m_zhi==1)
- {
- while(str[i]==' ')
- {
- i++;
- }
- if(m_zhi==1)
- ziduan[1]=i-zd;
- zd=i;
- while(str[i]!=' ')
- {
- if(str[i]=='-')
- {
- Fie[1][j++]='_';
- i++;
- }
- Fie[2][j++]=str[i++];
- }
-
- }else{
- zd=i;
- while((i-zd)<ziduan[2]+1){
- Fie[2][j++]=str[i++];
- }
- }
-
- Fie[2][j]=' '; //ok
- //strcpy(v_duozd[2],Fie[2]);
- j=0;
- while((str[i]==' ')&&(str[i]!=' '))
- {
- i++;
- }
- if(m_zhi==1)
- ziduan[2]=i-zd;
- zd=i;
- if((m_zhi!=1)&&(ziduan[4]==2))
- { int f=i;
- while(str[f]!='n')
- {
- f++;
- }
- ziduan[3]=f-i;
- }
- while((str[i]!=' ')&&(str[i]!=13))
- {
- Fie[3][j++]=str[i];
-
- i++;
- }
- Fie[3][j]=' '; //method
- //strcpy(v_duozd[3],Fie[3]);
-
- j=0;
- while((i-zd)<ziduan[3]-1)
- {
- Do[j++]=str[i++];
- }
- Do[j]=' ';
- strcat(Fie[3],Do);
- strcpy(Do,"");
-
-
-
- j=0;
- while((str[i]==' ')&&(str[i]!=' '))
- {
- i++;
- }
- if(m_zhi==1)
- ziduan[3]=i-zd;
- zd=i;
- while(str[i]!=' '&&(str[i]!=' '))
- {
- Fie[4][j++]=str[i++];
- }
- Fie[4][j]=' ';
- Fie[4][j+1]=' ';//status
- while((i-zd)<(ziduan[4]-2)){
- i++;
- j=0;
- while(i<(ziduan[4]+zd-1))
- {
- Do[j++]=str[i++];
- }
- Do[j]=' ';
- strcat(Fie[4],Do);
- strcpy(Do,"");
- }
- //strcpy(v_duozd[4],Fie[4]);
- j=0;
- while(str[i]==' '&&(str[i]!=' '))
- {
- i++;
- }
- if(m_zhi==1)
- ziduan[4]=i-zd;
- zd=i;
- while((str[i]!=' ')&&(str[i]!=' '))
- {
- Fie[5][j++]=str[i++];
- }
- Fie[5][j]=' '; //prot
- // strcpy(v_duozd[5],Fie[5]);
- if(m_zhi==1)
- {
- if(strcmp(Fie[1],m_field[m_flag][1])==0)
- {
- m_zhi=0;
- return;
- }else{
- m_zhi=0;
- m_flag=10;
- return;
- }
- }
- while(i<strlen(str))
- {
- i++;
- }
- if(m_zhi==1)
- ziduan[4]=i-zd;
-
- }
- }
- i++;
- }
- if(m_duozd==1){ //遇到一条记录占多行的情况 通过删除和插入累加到数据库中
- strcat(v_duozd[3],Fie[3]);
- char str2[200]="";
- strcpy(str2,"DELETE FROM ");
- strcat(str2,v_cmd[m_flag]);
- strcat(str2," WHERE ");
- strcat(str2,m_field[m_flag][0]);
- strcat(str2,"='");
- strcat(str2,v_duozd[0]);
- strcat(str2,"'");
- tn.Execute(str2);
- //m_duozd=0;
- char str3[200]="";
- strcpy(str3,"INSERT INTO ");
- strcat(str3,v_cmd[m_flag]);
- strcat(str3," (");
- int k;
- for(k=0;k<m_fies[m_flag]-49;k++)
- {
- strcat(str3,m_field[m_flag][k]);
- strcat(str3," ,");
- }
- strcat(str3,m_field[m_flag][k]);
- strcat(str3,") VALUES('");
- int n;
- for(n=0;n<m_fies[m_flag]-49;n++)
- {
- strcat(str3,v_duozd[n]);
- strcat(str3,"','");
- }
- strcat(str3,v_duozd[n]);
- strcat(str3,"')");
- tn.Execute(str3);
- }
- if((m_flag!=10)&&(m_duozd==0))
- {
- strcpy(str1,"INSERT INTO ");
- strcat(str1,v_cmd[m_flag]);
- strcat(str1," (");
- int k;
- for(k=0;k<m_fies[m_flag]-49;k++)
- {
- strcat(str1,m_field[m_flag][k]);
- strcat(str1," ,");
- }
- strcat(str1,m_field[m_flag][k]);
- strcat(str1,") VALUES('");
- int n;
- for(n=0;n<m_fies[m_flag]-49;n++)
- {
- strcat(str1,Fie[n]);
- strcat(str1,"','");
- }
- strcat(str1,Fie[n]);
- strcat(str1,"')");
- tn.Execute(str1);
-
- }
- if(m_duozd==0)
- {
- for(int k=0;k<6;k++){
- strcpy(v_duozd[k],Fie[k]);
- }
- }
- //一行一行的写入硬盘文件中
- FILE* fp = fopen("c:\Telnet.txt","at");
- fprintf(fp, "%s",str);
- fclose(fp);
-
-
- }
- void InsertDB::Insert(char *buf, int size)
- {
- if (size == 0)
- return;
- unsigned char tmpc = *(unsigned char *)buf;
- switch(tmpc)
- {
- case 8:
- if (m_Offset > 0)
- m_Offset --;
- break;
- case 13:
- m_Buffer[m_Offset++] = 'x0d';
- break;
- case 10:
- m_Buffer[m_Offset++] = 'x0a';
- m_Buffer[m_Offset] = ' ';
- InsertInto(m_Buffer);
- strcpy(m_Buffer,"");
- m_Offset = 0;
- break;
- default:
- m_Buffer[m_Offset++] = buf[0];
- break;
- }
- Insert(buf+1, size-1);
- }