技术成就梦想

代码如诗

原 MySQL Dump (备份) vga

2017年9月14日 未分类 0


procedure TMySQLDump.GetTableStructure(Table :String);
var
  SQL : String;
  Res : TMysqlResult;
  A   : Boolean;
begin
   Res := TMySQLClient(TNativeConnect(FDatabase.Handle).Handle).query(Format('SHOW CREATE TABLE %s',[Table]),True, A);
   SQL := Res.FieldValue(1);
   Res.Free;
   WriteLn(F,'--');
   WriteLn(F,Format('--  Table structure for table "%s"',[Table]));
   WriteLn(F,'--');
   if FDropObject then WriteLn(F,Format('DROP TABLE IF EXISTS %s%s'+#13#10,[Table,FDelimiter]));
   WriteLn(F,SQL+FDelimiter+#13#10);
end;

procedure TMySQLDump.GetTableData(Table : String);
var
   Ins_tmp : String;
   I,Rows : Integer;
   S1 : String;
   Percent : Integer;
   A : Boolean;
   Res : TMysqlResult;
begin
  if FDisableKeys then
     WriteLn(F,Format('/*!40000 ALTER TABLE %s DISABLE KEYS%s */',[Table, FDelimiter]));
  Res := TMySQLClient(TNativeConnect(FDatabase.Handle).Handle).query(Format('select * from %s',[Table]),True, A);
  Rows := res.RowsCount;
  WriteLn(F,'--');
  WriteLn(F,Format('--  Table data for table "%s". Record count - %s ',[Table,IntToStr(Rows)]));
  WriteLn(F,'--');
  if FAddLocks then
     WriteLn(F,Format('LOCK TABLES %s WRITE%s',[Table,FDelimiter]));
  WriteLn(F,'');

  ins_tmp := Format('INSERT INTO %s VALUES',[Table]);
  // Added for ExtendInsert support
  if ExtInsert and not (Res.RowsCount =0) then
     Write(F,Ins_tmp);
  // End modification
  while not Res.Eof do
  begin
     S1:='';
     Res.FetchLengths;
     for I := 0 to Res.FieldsCount-2 do
     begin
        if Res.FieldValue(I) = nil then
           S1 := S1 +'NULL'+',' else
           begin
              if isBLOB(Res.FieldDef(I)) or (Res.FieldDef(I).FieldType in [FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING])  then
                 S1 := S1 + ''''+StrValue(Res.FieldValue(I),Res.FieldLenght(I))+''','else
                 begin
                    if Res.FieldDef(I).FieldType in [FIELD_TYPE_TIMESTAMP, FIELD_TYPE_DATE, FIELD_TYPE_TIME, FIELD_TYPE_DATETIME] then
                       S1 := S1 + ''''+Res.FieldValue(I)+''',' else
                       S1 := S1 + Res.FieldValue(I)+',';
                 end;
           end;
     end;
     if Res.FieldValue(Res.FieldsCount-1) = nil then
        S1 := S1 +'NULL' else
        begin
           if isBLOB(Res.FieldDef(Res.FieldsCount-1)) or (Res.FieldDef(Res.FieldsCount-1).FieldType in [FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING])  then
               S1 := S1 + ''''+StrValue(Res.FieldValue(Res.FieldsCount-1),Res.FieldLenght(Res.FieldsCount-1))+'''' else
               begin
                  if Res.FieldDef(Res.FieldsCount-1).FieldType in [FIELD_TYPE_TIMESTAMP, FIELD_TYPE_DATE, FIELD_TYPE_TIME, FIELD_TYPE_DATETIME] then
                     S1 := S1 + ''''+Res.FieldValue(Res.FieldsCount-1)+'''' else
                     S1 := S1 + Res.FieldValue(Res.FieldsCount-1);
               end;
        end;
     // Added for ExtendInsert support
     if Not ExtInsert then
     begin
        S1 := Format('%s (%s)%s',[ins_tmp,S1,FDelimiter]);
        WriteLn(F,S1);
     end else
     begin
        if Res.RecNo = 0 then
           S1 := Format('(%s)',[S1]) else
           S1 := Format(',(%s)',[S1]);
        Write(F, S1);
     end;
     // End modification
     if Assigned(FonDataProcess) then
     begin
        Percent := Trunc(Res.RecNo*100/(Rows-1));
        FOnDataProcess(Self, Percent);
        Application.ProcessMessages;
     end;
     Res.Next;
  end;
  // Added for ExtendInsert support
  if ExtInsert and not (Res.RowsCount =0) then
  begin
     S1 := FDelimiter;
     WriteLn(F,S1);
  end;
  Res.Free;
  WriteLn(F,'');
  // End modification
  if FDisableKeys then
     WriteLn(F,Format('/*!40000 ALTER TABLE %s ENABLE KEYS%s */',[Table,FDelimiter]));
  WriteLn(F,'');
  if FAddLocks then
     WriteLn(F,Format('UNLOCK TABLES%s',[FDelimiter]));
  WriteLn(F,'');
end;