You could maybe use nth1/1
and the "univ" operator, doing something like this:
fieldnames(t, [id,this,that]).
get_field(Field, Tuple, Value) :-
Tuple =.. [Table|Fields],
fieldnames(Table, Names),
nth1(Idx, Names, Field),
nth1(Idx, Fields, Value).
You'd need to create fieldnames/2
records for each table structure, and you'd have to pass the table structure along to this query. It wouldn't be terrifically efficient, but it would work.
?- get_field(this, t(testId, testThis, testThat), Value)
Value = testThis
You could then build your accessors on top of this pretty easily:
findThisById(X, This) :- get_field(this, X, This).
Edit: Boris points out rightly that arg/3
will do this with even less work:
get_field(Field, Tuple, Value) :-
functor(Tuple, Table, _),
fieldnames(Table, Names),
nth1(Idx, Names, Field),
arg(Idx, Tuple, Value).
Prolog is so awesome.