Created
June 8, 2011 11:02
-
-
Save margusmartsepp/1014211 to your computer and use it in GitHub Desktop.
Data updater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit DataUI; | |
interface | |
uses | |
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, | |
Dialogs, ExtCtrls, DBCtrls, Grids, DBGrids, DBClient, StdCtrls, DataUnit, DB, | |
DBTables; | |
type | |
TForm2 = class(TForm) | |
DBNavigator1: TDBNavigator; | |
saveDialog: TSaveDialog; | |
Button1: TButton; | |
Button2: TButton; | |
Panel1: TPanel; | |
openDialog: TOpenDialog; | |
DBGrid1: TDBGrid; | |
procedure Button2Click(Sender: TObject); | |
procedure Button1Click(Sender: TObject); | |
procedure DBGrid1TitleClick(Column: TColumn); | |
private | |
{ Private declarations } | |
public | |
{ Public declarations } | |
end; | |
Const | |
FILE_FILTER1 = 'Text file|*.txt|' + 'Comma-separated values file|*.csv|' + | |
'Tab-separated values file|*.tsv|' + 'Tab-separated values file|*.tab|'; | |
FILE_FILTER2 = 'Extensible Markup Language file|*.xml|' + | |
'Delphi TClientDataset data file|*.cds|'; | |
var | |
Form2: TForm2; | |
implementation | |
{ | |
Import. | |
} | |
procedure TForm2.Button1Click(Sender: TObject); | |
var | |
ext: string; | |
begin | |
openDialog := TOpenDialog.Create(self); | |
openDialog.Title := 'Pick a file, to load.'; | |
openDialog.InitialDir := GetCurrentDir; | |
openDialog.Filter := FILE_FILTER2; | |
openDialog.DefaultExt := 'cds'; | |
openDialog.FilterIndex := 2; | |
// Display the open file dialog | |
if not openDialog.Execute then | |
ShowMessage('Action was cancelled') | |
else | |
begin | |
ext := ExtractFileExt(openDialog.FileName); | |
if (AnsiCompareText(ext, '.cds') = 0) or | |
(AnsiCompareText(ext, '.xml') = 0) then | |
begin | |
DataModule3.cdsMaster.Close; | |
DataModule3.cdsMaster.FileName := openDialog.FileName; | |
DataModule3.cdsMaster.Open; | |
Button2.Enabled := True; | |
end; | |
end; | |
saveDialog.Free; | |
end; | |
{ | |
Export. | |
} | |
procedure TForm2.Button2Click(Sender: TObject); | |
begin | |
saveDialog := TSaveDialog.Create(self); | |
saveDialog.Title := 'Pick a file, where to Save your data.'; | |
saveDialog.InitialDir := GetCurrentDir; | |
saveDialog.Filter := FILE_FILTER1 + FILE_FILTER2; | |
saveDialog.DefaultExt := 'tab'; | |
saveDialog.FilterIndex := 4; | |
if not saveDialog.Execute then | |
ShowMessage('Action was cancelled') | |
else | |
DataModule3.saveDataSet(saveDialog.FileName); | |
saveDialog.Free; | |
end; | |
{ | |
Sorts field ascendingly. | |
} | |
procedure TForm2.DBGrid1TitleClick(Column: TColumn); | |
begin | |
try | |
DataModule3.cdsMaster.IndexFieldNames := Column.FieldName; | |
except | |
on E: Exception do | |
ShowMessage('Could not sort.'); | |
end; | |
end; | |
{$R *.dfm} | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit DataUnit; | |
interface | |
uses | |
SysUtils, Classes, DB, DBClient, Dialogs, Provider; | |
type | |
TDataModule3 = class(TDataModule) | |
cdsMaster: TClientDataSet; | |
cdMaster: TDataSource; | |
procedure DataModuleDestroy(Sender: TObject); | |
procedure writeTsvRow(var AFile: Textfile); | |
procedure writeCsvRow(var AFile: Textfile); | |
procedure writeDsvRow(var AFile: Textfile; delimiter: String); | |
procedure cdsMasterAfterPost(DataSet: TDataSet); | |
private | |
{ Private declarations } | |
public | |
{ Public declarations } | |
procedure saveDataSet(FileName: String); | |
end; | |
var | |
DataModule3: TDataModule3; | |
implementation | |
{$R *.dfm} | |
{ | |
Does `Autosave`. | |
} | |
procedure TDataModule3.cdsMasterAfterPost(DataSet: TDataSet); | |
begin | |
if cdsMaster.Active then | |
begin | |
cdsMaster.SaveToFile(cdsMaster.FileName); | |
cdsMaster.Close; | |
cdsMaster.LoadFromFile(cdsMaster.FileName); | |
end; | |
end; | |
{ | |
TODO: Remove this or add provider and option to use it. | |
} | |
procedure TDataModule3.DataModuleDestroy(Sender: TObject); | |
begin | |
// cdsMaster.MergeChangeLog(); | |
end; | |
{ | |
Selects appropriate export method. | |
} | |
procedure TDataModule3.saveDataSet(FileName: String); | |
Const | |
TAB = Chr(9); | |
var | |
AFile: Textfile; | |
I: Integer; | |
ext: string; | |
begin | |
ext := ExtractFileExt(FileName); | |
if (AnsiCompareText(ext, '.cds') = 0) or | |
(AnsiCompareText(ext, '.xml') = 0) then | |
begin | |
cdsMaster.SaveToFile(FileName); | |
end | |
else | |
begin | |
AssignFile(AFile, FileName); | |
Rewrite(AFile); | |
try | |
if (AnsiCompareText(ext, '.tab') = 0) or | |
(AnsiCompareText(ext, '.tsv') = 0) then | |
writeTsvRow(AFile) | |
else if (AnsiCompareText(ext, '.txt') = 0) or | |
(AnsiCompareText(ext, '.csv') = 0) then | |
writeCsvRow(AFile); | |
finally | |
CloseFile(AFile); | |
end; | |
end; | |
end; | |
{ | |
Writes datatable to file, in tab separated format. | |
} | |
procedure TDataModule3.writeTsvRow(var AFile: Textfile); | |
begin | |
writeDsvRow(AFile, Chr(9) { This is tab character. } ); | |
end; | |
{ | |
Writes datatable to file, in comma separated format. | |
} | |
procedure TDataModule3.writeCsvRow(var AFile: Textfile); | |
begin | |
writeDsvRow(AFile, ','); | |
end; | |
{ | |
Writes datatable to file, in delimiter separated format. | |
TODO: `cdMaster.DataSet` should be passed as a parameter. | |
} | |
procedure TDataModule3.writeDsvRow(var AFile: Textfile; delimiter: String); | |
var | |
I: Integer; | |
begin | |
with cdMaster.DataSet do | |
begin | |
// field names | |
for I := 0 to FieldCount - 2 do | |
Write(AFile, Fields[I].FieldName, delimiter); | |
Writeln(AFile, Fields[FieldCount - 1].FieldName); | |
// content | |
First; | |
while not cdMaster.DataSet.EOF do | |
begin | |
for I := 0 to FieldCount - 2 do | |
Write(AFile, FieldByName(Fields[I].FieldName).AsString, delimiter); | |
Writeln(AFile, FieldByName(Fields[FieldCount - 1].FieldName).AsString); | |
Next; | |
end; | |
end; | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment