(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
[inherit('psqlitedir:sqlite', 'psqlitedir:psqlite')]
program list_after_2(input, output);

%include 'asset.pas'

var
   con : sqlite_ptr;
   sel : sqlite_stmt_ptr;
   count : integer;
   res : array [1..1000] of asset;
   i : integer;

begin
   con := psqlite_open('asset.db');
   count := 0;
   sel := psqlite_prepare(con, 'SELECT asset.id,asset.description,asset.assettype,truckasset.model,truckasset.license,truckasset.towcapacity FROM asset JOIN truckasset ON asset.id = truckasset.id');
   while psqlite_step_query(sel) do begin
      count := count + 1;
      res[count].id := psqlite_column_int(sel, 0);
      res[count].description := psqlite_column_text(sel, 1);
      res[count].assettype := psqlite_column_int(sel, 2);
      res[count].model := psqlite_column_text(sel, 3);
      res[count].license := psqlite_column_text(sel, 4);
      res[count].towcapacity := psqlite_column_int(sel, 5);
   end;
   psqlite_finalize(sel);
   sel := psqlite_prepare(con, 'SELECT asset.id,asset.description,asset.assettype,trailerasset.ownweight,trailerasset.maxcargo,trailerasset.roof FROM asset JOIN trailerasset ON asset.id = trailerasset.id');
   while psqlite_step_query(sel) do begin
      count := count + 1;
      res[count].id := psqlite_column_int(sel, 0);
      res[count].description := psqlite_column_text(sel, 1);
      res[count].assettype := psqlite_column_int(sel, 2);
      res[count].ownweight := psqlite_column_int(sel, 3);
      res[count].maxcargo := psqlite_column_int(sel, 4);
      res[count].roof := psqlite_column_int(sel, 5);
   end;
   psqlite_finalize(sel);
   sel := psqlite_prepare(con, 'SELECT asset.id,asset.description,asset.assettype,toolasset.tooltype,toolasset.name,toolasset.weight FROM asset JOIN toolasset ON asset.id = toolasset.id');
   while psqlite_step_query(sel) do begin
      count := count + 1;
      res[count].id := psqlite_column_int(sel, 0);
      res[count].description := psqlite_column_text(sel, 1);
      res[count].assettype := psqlite_column_int(sel, 2);
      res[count].tooltype := psqlite_column_text(sel, 3);
      res[count].name := psqlite_column_text(sel, 4);
      res[count].weight := psqlite_column_int(sel, 5);
   end;
   psqlite_finalize(sel);
   psqlite_close(con);
   for i := 1 to count do begin
      write(res[i].id, ' ', res[i].description, ' ');
      case res[i].assettype of
         TRUCK:
            begin
               write(res[i].model, ' ', res[i].license, ' ', res[i].towcapacity);
            end;
         TRAILER:
            begin
               write(res[i].ownweight, ' ', res[i].maxcargo, ' ', res[i].roof);
            end;
         TOOL:
            begin
               write(res[i].tooltype, ' ', res[i].name, ' ', res[i].weight);
            end;
      end;
      writeln;
   end;
end.
