Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2020-06

How to pass an unknown record to a function as argument?

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

warleyalex

I use the record structure to map to a table and store/retrieve the data.

type
  TViewsRecord = record
    Category  : string;
    date        : string;
  end;
function JSON2TViewsRecord(const Value: TJSObject): TViewsRecord;
begin
  result.Category   := string(Value['Category']);
  result.date         := string(Value['date']);
end;
function TViewsRecord2JSON(const Value: TViewsRecord): TJSObject;
begin
  result := TJSObject.New;
  result['Category'] := Value.Category;
  result['date']        := Value.date;
end;
-----

------------
...this definition external class

type
  JRec = record
  end;
  TProcColumnNameParams = procedure(columnName: @record);

Of course this not work, I want to write a function and allow the user of the function to pass a record
that I do not know in advance.
Then, the idea is to show the columnNames (code autocompletion) of an unknown DB table.

How to pass an unknown record to a function as argument?

(...)
procedure each(sql: String; callback: TProcColumnNameParams; doneProc:
TProcColumnName); overload;
--------------

Something like to use this:

localDB.each('SELECT date FROM views ORDER BY date ASC',
procedure(columnName: TViewsRecord)
  begin
    console.log(columnName.Category);
    console.log(columnName.date);
  end,
  procedure()
  begin
    console.log('done');
  end);

and so on... // without overloading the external class definition

localDB.each('SELECT * FROM album', procedure(columnName: TAlbumRecord)
begin
  console.log(columnName.Nome);
  console.log(columnName.CPF);
end,
procedure()
begin
end);

Reply by email

Michael Van Canneyt
to warleyalex
warleyalex wrote
I use the record structure to map to a table and store/retrieve the data.

[snip]

Show quoted text 11 lines
and so on... // without overloading the external class definition localDB.each('SELECT * FROM album', procedure(columnName: TAlbumRecord) begin console.log(columnName.Nome); console.log(columnName.CPF); end, procedure() begin end);

When generics are released, a generic function could be used for this.

In the current version you can also write a helper class with an overloaded version of the call.

Michael.

Reply by email

Mattias Gaertner
to warleyalex: I use the record structure to map to a table and store/retrieve the data. type…
Show quoted text 15 lines
[...] type JRec = record end; TProcColumnNameParams = procedure(columnName: @record); Of course this not work, I want to write a function and allow the user of the function to pass a record that I do not know in advance. Then, the idea is to show the columnNames (code autocompletion) of an unknown DB table. How to pass an unknown record to a function as argument?

You can pass any record to untyped, pointer, JSValue and TJSObject:

procedure Run(var r; p: Pointer; j: JSValue, o: TJSObject);
var a: JRec;
Run(a, at a,a,a);

Mattias

Reply by email