unit uLibrary;

interface

uses
  System.Classes, Uni;

procedure LoadDatabaseSettings( uc: TUniConnection; iniFilename: string );
procedure DoQuery( uq: TUniQuery; sql: string );
function CalculateAge( const dob, dt: TDateTime ): Integer;

implementation

uses
  System.SysUtils,
  System.IniFiles,
  Vcl.Forms,
  Data.DB;

procedure LoadDatabaseSettings( uc: TUniConnection; iniFilename: string );
var
  iniFile: TIniFile;
begin
  iniFile := TIniFile.Create( ExtractFilePath(Application.ExeName) + iniFilename );
  try
    uc.Server := iniFile.ReadString('Database', 'Server', uc.Server);
    uc.Database := iniFile.ReadString('Database', 'Database', uc.Database);
    uc.Username := iniFile.ReadString('Database', 'Username', uc.Username);
    uc.Password := iniFile.ReadString('Database', 'Password', uc.Password);
  finally
    iniFile.Free;
  end;
end;

procedure DoQuery(uq: TUniQuery; sql: string);
begin
  uq.Close;
  uq.SQL.Text := sql;
  uq.Open;
end;

function CalculateAge( const dob, dt: TDateTime): Integer;
var
  age: Integer;
  y1, m1, d1, y2, m2, d2: Word;
begin
  Result := 0;

  if dt < dob then
    Exit;

  DecodeDate( dob, y1, m1, d1);
  DecodeDate( dt, y2, m2, d2);
  age := y2 - y1;
  // Feb 29
  //if ( (m1=2) and (d1=29) ) and ( not IsLeapYear(y2) ) then
  //  d1 := 28;

  if (m1 = 2) and (d1 = 29) and (not (IsLeapYear (y2))) then
  begin
    m1 := 3;
    d1 := 1;
  end;

  if (m2 < m1) or ((m2 = m1) and (d2 < d1)) then
    Dec(age);
  Result := age
end;


end.
