procedure GenerateUpdateScriptsForOldDataCadastro; function BytesToHex(const Bytes:…
- User
procedure GenerateUpdateScriptsForOldDataCadastro;
function BytesToHex(const Bytes: TBytes): string; var i: Integer; begin Result := ''; for i := 0 to Length(Bytes) - 1 do Result := Result + IntToHex(Bytes[i], 2); end;
function FormatSQLValue(const Value: Variant; const FieldType: TFieldType): string; begin if VarIsNull(Value) then Result := 'NULL' else case FieldType of ftString, ftWideString, ftMemo, ftWideMemo, ftFixedChar, ftFixedWideChar: Result := QuotedStr(VarToStr(Value)); ftDate, ftDateTime, ftTime: Result := QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', Value)); ftInteger, ftSmallint, ftWord, ftLargeint, ftAutoInc, ftFloat, ftCurrency, ftBCD, ftFMTBcd: Result := VarToStr(Value); ftBytes, ftVarBytes, ftBlob, ftGraphic, ftOraBlob, ftOraClob, ftParadoxOle, ftDBaseOle: Result := '0x' + BytesToHex(Value); else Result := QuotedStr(VarToStr(Value)); end; end;
var QueryStore1, QueryStore2: TUniQuery; ScriptFile, ReportFile: TextFile; ScriptFileName, ReportFileName: string; enderecoId, clienteId: Integer; dataCadastro1, dataCadastro2: TDateTime; isStore1Newer: Boolean; FieldList: TStringList; i: Integer; FieldName: string; ValueStore1, ValueStore2: Variant; FieldType: TFieldType; Connection: TUniConnection; Store2Keys: TDictionary<string, TDateTime>; CompositeKey: string; SourceQuery, TargetQuery: TUniQuery; begin ScriptFileName := 'update_data_cadastro.sql'; ReportFileName := 'update_data_cadastro_report.txt';
AssignFile(ScriptFile, ScriptFileName); Rewrite(ScriptFile);
AssignFile(ReportFile, ReportFileName); Rewrite(ReportFile);
QueryStore1 := TUniQuery.Create(nil); QueryStore2 := TUniQuery.Create(nil); Store2Keys := TDictionary<string, TDateTime>.Create;
try WriteLn(ReportFile, 'Update Scripts for endereco Records with Older data_cadastro'); WriteLn(ReportFile, 'Generated on: ' + DateTimeToStr(Now)); WriteLn(ReportFile, '--------------------------------------------------------'); WriteLn(ReportFile, '');
Connection := DM.LocalServer;
QueryStore1.Connection := Connection; QueryStore2.Connection := Connection;
QueryStore1.SQL.Text := 'SELECT endereco_id, cliente_id, data_cadastro ' + 'FROM store1.endereco';
QueryStore2.SQL.Text := 'SELECT endereco_id, cliente_id, data_cadastro ' + 'FROM store2.endereco';
QueryStore1.Open; QueryStore2.Open;
while not QueryStore2.EOF do begin enderecoId := QueryStore2.FieldByName('endereco_id').AsInteger; clienteId := QueryStore2.FieldByName('cliente_id').AsInteger; dataCadastro2 := QueryStore2.FieldByName('data_cadastro').AsDateTime;
CompositeKey := Format('%d:%d', [enderecoId, clienteId]); if not Store2Keys.ContainsKey(CompositeKey) then Store2Keys.Add(CompositeKey, dataCadastro2) else WriteLn(ReportFile, Format( 'Duplicate key found in store2 for endereco_id=%d and cliente_id=%d.', [enderecoId, clienteId]));
QueryStore2.Next; end;
QueryStore1.First; while not QueryStore1.EOF do begin enderecoId := QueryStore1.FieldByName('endereco_id').AsInteger; clienteId := QueryStore1.FieldByName('cliente_id').AsInteger; dataCadastro1 := QueryStore1.FieldByName('data_cadastro').AsDateTime;
- Assistant