PDA

View Full Version : Table of a field in a QSqlQuery with * operators



nicoduboulot
25th January 2016, 14:52
Hi,

I'm writing a Qt program which executes an SQL query; this query is fully configurable by the user. I need to know, for each field, its name and table, or if it's an alias or a computed value.

> select T_RECORD.*, T_TYPE.* from T_RECORD left join T_TYPE on T_RECORD.NUM_TYPE=T_TYPE.NUM_TYPE;
Say T_RECORD and T_TYPE both have a NAME field, query.record().field(i).name() return the same name. I need to be able to tell which field is from which table.

Again, the query is fully configurable. I can't make any asumption on table structures beforehand. I also want to allow table.* to be used, because some of them will have a lot of fields (100+).

Thanks for your help.

nicoduboulot
15th February 2016, 16:32
Anyone?
Is the question stupid?

ChrisW67
15th February 2016, 19:57
You cannot know without embedding a full SQL parser for each SQL backend, and even then it could not possibly deal with "select *" independently of access to the SQL engine's indeterminate ordering of columns selected that way. A column select through a view may or may not be computed: the view column list does not expose that information. Qt receives a column list from the underlying engine and reports exactly what the SQL source has named each column, which might be the base column name, a user dictated alias, or some computed name like "sum(column)" or "name_1". The engines I typically deal with will not return two result set columns with an identical name even if they came from the same column of the same table. AFAICT the origin of the column is not exposed by the SQL backends (at all, or in any consistent fashion) in a manner that would allow a generic interface to this sort of information.

You might be able to get this level of information from the native API of your database of choice.