Recent

Author Topic: [SOLVED] Defining Datasource Type in Runtime  (Read 3614 times)

jufau

  • New Member
  • *
  • Posts: 42
[SOLVED] Defining Datasource Type in Runtime
« on: June 17, 2015, 04:26:37 am »
Hello everyone!
I have a crazy and probably dumb question. I am creating an application which will import data from different sources (DBF, CSV, TXT) to a MySql Database. Everything is working just fine. But what I have right now is 3 procedures, each one to execute the import process based on the selected Radio option (DBF, CSV, TXT). For example: If I select the DBF Radio, then the importDBF procedure will be called. if I select the CSV Radio, the importCSV will be executed, and so on. I am using TDBF and Tsdf components to connect to my source files.
What I would like to do was create an unique procedure/function which will import the data either from DBF or CSV/TXT based on the passed type(DBF/CSV). So when I click the Radio DBF, it will call the importData procedure passing the TDBF type for the "sourceTableType". That way I only have one procedure to execute all my data type (DBF/CSV/TXT) instead of 1 procedure for each type.

My ideia was something like that:

procedure importData(dataSource:type);
begin
    while not dataSource.EOF do begin
        ...
    end;
end;

the caller should work like that:
imporData(dataModuleDB.dbfTables) for the TDBF component
or imporData(dataModuleDB.csvTables) for TSDF component

It seems simple but for some reason it does not work because in pascal, as far as I know, I have to define the data type before using the variable/object. So first i need to set dataSource type either as TDBF or TSDF in my implementation/interface, before I can use it on my procedure.

I hope I made myself clear enough, otherwise please ask me for more details about the thing.
Thanks a lot guys.


« Last Edit: June 17, 2015, 04:30:41 pm by jufau »

LacaK

  • Hero Member
  • *****
  • Posts: 703
Re: Defining Datasource Type in Runtime
« Reply #1 on: June 17, 2015, 07:34:34 am »
Mat be :

procedure importData(DataSet: TDataSet);
begin
  while not DataSet.Eof do begin
     ...
     DataSet.Next;
  end;

jufau

  • New Member
  • *
  • Posts: 42
Re: Defining Datasource Type in Runtime
« Reply #2 on: June 17, 2015, 01:09:33 pm »
Hey LacaK! Thanks for your reply.

Yes, it worked like a charm... Thank you man!
You were of a great help and lines of code saving

« Last Edit: June 17, 2015, 04:29:58 pm by jufau »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Defining Datasource Type in Runtime
« Reply #3 on: June 17, 2015, 01:27:21 pm »
Hey LacaK! Thanks for your reply.
As I said, my procedure to import the data is working fine. But I've got to create 3 different procedures, each one responsible to import data from a different datasource type (TDBF and TSDFDataset) according to the selected Radio option.
What I need is to eliminate 2 of those procedures and have only one "global" procedure which will do exactly the same thing, but the datasource type must be informed/defined/passed by the caller (radio button) at runtime.

Use the type that lacaK provided for your single procedure. If you are interested why it works go research inheritance and base types to see how and why it works.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

jufau

  • New Member
  • *
  • Posts: 42
Re: Defining Datasource Type in Runtime
« Reply #4 on: June 17, 2015, 01:39:42 pm »
OK,
I am going to try it and post back here with the results.

thanks guys.

jufau

  • New Member
  • *
  • Posts: 42
Re: [SOLVED] Defining Datasource Type in Runtime
« Reply #5 on: June 17, 2015, 04:32:14 pm »
Thank you guys for your great help

So all I've got to do was just set my datasouce parameter as TDataset as Lacak told me so.

Problem solved.

thank you all

 

TinyPortal © 2005-2018