Recent

Author Topic: Add method parameters  (Read 3159 times)

johnmc

  • New Member
  • *
  • Posts: 47
Add method parameters
« on: April 02, 2019, 06:22:35 pm »
I asked in another topic about what the True parameter of the add method does lucamar kindly pointed me at the limited ducumentation but I need a fuller answer.
Quote
It's in the documentation (and help files) for the FCL:
Quote from: Reference for unit 'db' (#fcl)

    TFieldDefs.Add : Add a new field definition to the collection.
    Declaration
    Source position: db.pas line 211
    [...]
    procedure TFieldDefs.Add(
      const AName: string;
      ADataType: TFieldType;
      ASize: Word;
      ARequired: Boolean
    ); overload;
    [...]

    Arguments
    AName
      Value for the Name property of the new item.
    ADataType
      Value for the DataType property of the new item.
    ASize
      Value for the Size property of the new item.
    ARequired
      Value for the Required property of the new item.
    [...]

I interpreted that as meaning when True the field must be entered for the record to be written into the dataset, with an exception if not present. So the following should raise an exception but does not.
Code: Pascal  [Select][+][-]
  1. with WineDBf do
  2.     begin
  3.       TableLevel := 7;
  4.       Exclusive := True;
  5.       FieldDefs.Add('ID', ftAutoInc, 0, True);  // Field[0]
  6.       FieldDefs.Add('WineID', ftInteger, 0, True);
  7.       FieldDefs.Add('WineName', ftString, 60, True);
  8.       FieldDefs.Add('Vintage', ftString, 4, True);
  9.       FieldDefs.Add('Quantity', ftInteger, 0, True);
  10.       FieldDefs.Add('Size', ftString, 12, True);
  11.       FieldDefs.Add('Region', ftString, 25, True);
  12.       FieldDefs.Add('Country', ftString, 25, True);
  13.       FieldDefs.Add('Date', ftDateTime, 0, True);  // Field[8]
  14.       FieldDefs.Add('NoteID', ftInteger, 0, True);
  15.       CreateTable;
  16.       Open;
  17.  
  18.       // Add data here
  19.       Append;
  20.       Fields[1].AsInteger := 1;
  21.       Fields[2].AsString := 'Ch. Smith Haut Lafitte';
  22.       Fields[3].AsString := '1988';
  23.       Fields[6].AsString := 'Bordeaux';
  24.       Fields[7].AsString := 'France';
  25.       Fields[4].AsInteger := 0;
  26.       Fields[5].AsString := 'Bottle';
  27.       //Fields[8].AsDateTime := StrToDateTime('10/2/1991');
  28.       Fields[9].AsInteger := 0;
  29.       Post;
  30.  
  31.  
  32.       AddIndex('idxByID', 'ID', [ixPrimary, ixUnique]);
  33.       AddIndex('idxByWineID', 'WineID', [ixUnique]);
  34.       AddIndex('idxByCountry', 'Country', [ixCaseInsensitive]);
  35.       AddIndex('idxByName', 'WineName', [ixCaseInsensitive]);
  36.       AddIndex('idxByNameRev', 'WineName', [ixDescending, ixCaseInsensitive]);
  37.     end;

Does anyone know what this should do.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Add method parameters
« Reply #1 on: April 02, 2019, 07:14:11 pm »
It would appear that although TDbf allows you to set the Required property for a field, its actual implementation does not enforce this.

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Add method parameters
« Reply #2 on: April 03, 2019, 07:14:41 am »
In layman‘s terms: you‘re not allowed to enter „nothing“
You must enter a valid (!) value
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Add method parameters
« Reply #3 on: April 03, 2019, 07:16:25 am »
It would appear that although TDbf allows you to set the Required property for a field, its actual implementation does not enforce this.
Howard, of course it doen‘t enforce it, since the underlying real datatype of a date in a dbf is a String. It just fills it up with 8 zero-characters
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Add method parameters
« Reply #4 on: April 03, 2019, 08:05:58 am »
To my mind there is no "of course" about such an approach to enforcing a requirement.
You could equally say that if a required field has a blank/null value then "of course" an exception should be raised rather than a contrived value inserted.


johnmc

  • New Member
  • *
  • Posts: 47
Re: Add method parameters
« Reply #5 on: April 03, 2019, 11:34:29 am »
As this is a datetime field I think it should be stored as a timestamp therefore as 2 long ints (2 x 4bytes). See https://www.dbase.com/Knowledgebase/INT/db7_file_fmt.htm.

For my example above I would expect an exception to be raised whatever the underlying data format being used to store it, otherwise why have it (required).

So is this a bug? If so is it likely to be fixed? Where do I report it?

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Add method parameters
« Reply #6 on: April 03, 2019, 12:02:36 pm »
To my mind there is no "of course" about such an approach to enforcing a requirement.
You could equally say that if a required field has a blank/null value then "of course" an exception should be raised rather than a contrived value inserted.
Not if the Field has the Option "Default-Value" set to something, then NO, noone is going to enforce anything from you
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Add method parameters
« Reply #7 on: April 03, 2019, 12:15:42 pm »
Where in the code johnmc showed are any default values specified?

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Add method parameters
« Reply #8 on: April 03, 2019, 12:21:50 pm »
True, but i found something else going through dbf_fields.pas
What Version of dbase is he using?

Code: Pascal  [Select][+][-]
  1. procedure TDbfFieldDef.VCLToNative;
  2. begin
  3.   FNativeFieldType := #0;
  4.   // to do: look into ftBytes support; e.g. Visual FoxPro varbytes?
  5.   case FFieldType of
  6.     {ftAutoInc  :
  7.       if DbfVersion=xVisualFoxPro then
  8.         FNativeFieldType  := 'I'
  9.       else
  10.         FNativeFieldType  := '+'; //Apparently xbaseV/7+ only; not (Visual) Foxpro}
  11.     ftDateTime :
  12.       if DbfVersion = xBaseVII then
  13.         FNativeFieldType := '@'
  14.       else
  15.       if (DbfVersion = xFoxPro) or (DbfVersion = xVisualFoxPro) then
  16.         FNativeFieldType := 'T'
  17.       else
  18.         FNativeFieldType := 'D';
  19.  
There. If it's dBaseVII then ftDateTime tranlates to "@", if it's FoxPro it translates to "T", everyting else to "D", WHICH IS A STRING IN FORMAT "YYYYMMDD"

Next:
Code: Pascal  [Select][+][-]
  1. {$ifdef SUPPORT_DEFAULT_PARAMS}
  2.     procedure Add(const Name: string; DataType: TFieldType; Size: Integer = 0; Required: Boolean = False);
  3. {$else}
  4.     procedure Add(const Name: string; DataType: TFieldType; Size: Integer; Required: Boolean);
  5. {$endif}
There! Do we know what switches are turned on in his program?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Add method parameters
« Reply #9 on: April 03, 2019, 01:07:04 pm »
I find that  dbf_common.inc has this at line 214:
Code: Pascal  [Select][+][-]
  1.   {$define SUPPORT_DEFAULT_PARAMS}  

johnmc

  • New Member
  • *
  • Posts: 47
Re: Add method parameters
« Reply #10 on: April 03, 2019, 03:59:01 pm »
From what you have howardpc and zvoni are saying is that it won't work because theres is some setting buried in the TDbf source code that disables it.

That does seem to make things difficult.

I did find a reponse to a query on the Sourceforge TDbf forum that says it is not implemented, although that dates from 2003, the author of the comment was Micha Nelissen. https://sourceforge.net/p/tdbf/discussion/107245/thread/b440c61a/

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Add method parameters
« Reply #11 on: April 03, 2019, 10:22:49 pm »
John, i repeat, what i said in your other thread: create yor dbf, fill it with sample data, and then try to open it in Excel, Access, whatever.
The only thing you need is the ISAM-Driver (AFAIK. It's been 20 years for me working with such stuff)

https://smallbusiness.chron.com/convert-dbf-xls-55554.html

EDIT: What i'd like to know: Why in blazes are you using such an archaic format? If you need filebased, i'd say SQLite!
« Last Edit: April 03, 2019, 10:25:59 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

johnmc

  • New Member
  • *
  • Posts: 47
Re: Add method parameters
« Reply #12 on: April 04, 2019, 10:14:50 am »
It seemed like a good idea when I started out. A database without having to install anything else. And to be fair it has allowed me to build a simple app that works. It may not be the most robust app. I can read, write sort and filter records. I am now begining to understand some of the limitations of my choices but it has given me some insight into how to find solutions to my problems. I will probably continue with this for a little longer while I make a choice as to which route to take from here.

Thanks for all your help so far.

John

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Add method parameters
« Reply #13 on: April 04, 2019, 10:38:01 am »
Well, SQLite has been around now for over 18 years, and is one (if not THE) most popular filebased database.
FPC/Laz supports it natively.
For Design i'd recommend DB Browser for SQLite and/or SQLiteStudio.
You'll notice that finding information on SQLite is much easier than on dBase, especially if you're struggling with an issue, you'll probably get it solved much quicker, since most of us are familiar with it.
The only thing to remember is, when shipping your program under Windows, to ship the sqlite3.dll with it (if it's not already installed on the target).

As a last advice to you (and unfortunately this applies to SQLite too!): If you're using a Database, which doesn't have a "native"-DateTime-DataType (like dBase and SQLite --> SQLite stores it as some weird String IIRC), i'd rather choose Double (If you want to store Date and Time) or Integer (if only Date), because, internally, that's what a Date/Time actually is!
DateTime --> Double --> The integer-part being the Day since BaseDay, the Fraction the Time of that day

I just did a "Debug.Print CLng(Now)" in VBA/Excel, and it returned 43559, meaning: 43559 days have passed since your BaseDay (Whatever that is)
As Double "Apr 4th 2019 - 6PM" would equal 43559.75 (The fraction being the Time-part --> at 6PM 3/4 of a day have passed)
« Last Edit: April 04, 2019, 10:46:11 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dsiders

  • Hero Member
  • *****
  • Posts: 1078
Re: Add method parameters
« Reply #14 on: April 04, 2019, 11:30:39 am »
It seemed like a good idea when I started out. A database without having to install anything else. And to be fair it has allowed me to build a simple app that works. It may not be the most robust app. I can read, write sort and filter records. I am now begining to understand some of the limitations of my choices but it has given me some insight into how to find solutions to my problems. I will probably continue with this for a little longer while I make a choice as to which route to take from here.

Thanks for all your help so far.

John

I'm partial to Firebird 3 Embedded. It's a solid SQL database with a light footprint, and the added benefit of talking to its full-fledged remote server if needed.

Check out: http://www.ibphoenix.com/files/Embedded_fb3.pdf

But I do have fond nostalgic memories of Clipper and DBFs from the 1980's and 1990's. ;)
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

 

TinyPortal © 2005-2018