#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    86
    Rep Power
    13

    Can this be a function instead of a procedure...


    Hi, I hope you had a great weekend!

    I have the following code that I'd like to turn into a function returning the "sltb" as the result, have tried but can't get it to work... also I can't "free" sltb or it won't be the "result"

    Basically the code is going to be reused all over the place with various SQL statements in it, rather than repeat it all the time I'm trying to make it reusable and more efficient. my approach may not be very smart, if you have a better idea I'm all ears...

    I am using Tim Andersons SQLite Wrapper in case you are wondering....
    Code:
    function dbconnect(str_application :string ; str_query : string) : ????;
      var
        slDBpath: string;
        sldb: TSQLiteDatabase;
        sltb: TSQLIteTable;
        sSQL: String;
        colname : string;
        arr_collectionids : array of string;
        c_counter:integer;
    begin
    
    slDBPath := ExtractFilepath(str_application) + 'test.db';
    sldb := TSQLiteDatabase.Create(slDBPath);
    sltb := slDb.GetTable(str_query);
    
    //result := IntToStr(sltb.Count);//This was a test that worked.
    
    result := sltb;//this is what I would like to output but
    // I'm not sure how to register it in the function
    // i.e. string;integer etc... or whether it can even be done... 
    
    end;
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    Typically what you want to do is not a good idea.
    From a design perspective one should not generally create an object within a function
    return it and leave it to the caller to recognize and understand that they are responsible
    for managing its life cycle and disposing of it.
    Technically to do what you want (though I do not generally approve):
    Code:
    function dbconnect(str_application :string ; str_query : string) : TSQLIteTable;
      var
        slDBpath: string;
        sldb: TSQLiteDatabase;
        sSQL: String;
        colname : string;
        arr_collectionids : array of string;
        c_counter:integer;
    begin
      slDBPath := ExtractFilepath(str_application) + 'test.db';
      sldb := TSQLiteDatabase.Create(slDBPath);
      result := slDb.GetTable(str_query);
    end;
    I am not familiar with SQLLite (I typically work with Oracle or Firebird) so looking at the code you are using I am not sure how you would normally dispose of the TSQLIteTable object.

    My typical preference would be to use a method of the form or datamodule and make
    sltb (and sldb?) private fields of the form or dataModule itself.
    Code:
    procedure TMyDataModule.dbconnect(str_application :string ; str_query : string);
      var
        slDBpath: string;
    begin
      slDBPath := ExtractFilepath(str_application) + 'test.db';
      Fsldb := TSQLiteDatabase.Create(slDBPath);
      Fsltb  := FslDb.GetTable(str_query);
    end;
    Now it is up to the DataModule to manage the life cycles of its private fields Fsldb and Fsltb.

    Just suggestions about approaches.

    Clive
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    86
    Rep Power
    13
    Hi Clive, thank you for your reply, it is most appreciated!

    I suspected that my approach was not that wise either but was looking for an option, I'll further my research :-)

    I come from a Php background so this is the way I used to work but on a desktop application there clearly are better ways.

    Greg.

IMN logo majestic logo threadwatch logo seochat tools logo