Page 1 of 1

Is a TField in primary key?

Posted: Tue 05 Jul 2011 12:54
by chkaufmann
Hi,

after loading a TDataSet I would like to know, if a field is used in the primary key index. I tried the following code, which seems to work (at least with Firebird)

m.MetaDataKind := 'IndexColumns';
m.Restrictions.Values['TABLE_SCHEMA'] := FUniConnection.SpecificOptions.Values['Schema'];
m.Restrictions.Values['TABLE_NAME'] := ATableName;
m.Restrictions.Values['UNIQUE'] := '1';
m.Open;
while not m.Eof do begin
Result.Include(m.FieldByName('COLUMN_NAME').AsString);
m.Next;
end;


But is there a way to read the information from the TField directly? E.g. like from ProviderFlags or FieldKind?

cu Christian

Posted: Wed 06 Jul 2011 07:49
by AndreyZ
Hello,

To determine primary key fields for MySQL and SQL Server, you can use the following code:

Code: Select all

if UniQuery.GetFieldDesc(UniQuery.Fields[0].FieldNo).IsKey then
  ShowMessage('Primary key field');
For all other database servers UniDAC supports, you should use the TUniMetaData component. We will investigate ways of implementing unified functionality for retrieving primary key fields.

Posted: Wed 06 Jul 2011 08:01
by chkaufmann
I suggest something like

m.Restrictions.Values['PRIMARYKEY'] := 'Yes';

cu Christian

Posted: Wed 06 Jul 2011 12:56
by AndreyZ
Thank you for your suggestion.

Posted: Mon 11 Jul 2011 05:45
by chkaufmann
AndreyZ wrote:Thank you for your suggestion.
Do you have an idea, when this extension could be available?

cu Christian

Posted: Mon 11 Jul 2011 11:41
by AndreyZ
We are still investigating this question. We will try to implement this functionality in one of the nearest UniDAC builds.

Re: Is a TField in primary key?

Posted: Fri 21 Feb 2014 15:43
by Krampus
AndreyZ wrote:We will try to implement this functionality in one of the nearest UniDAC builds.
Is this implemented yet?
(just started evaluating UniDac and cant find anything in BOL)

thanks
h

BTW: from chkaufsman´s solution
the following line:
Restrictions.Values['UNIQUE'] := '1';
doesnt seem to have any effect - ALL index-fields are listed, not only PK-fields
(UniDAC 5.2.6)

Re: Is a TField in primary key?

Posted: Mon 24 Feb 2014 09:36
by AlexP
Hello,

Yes, it is already done.
In order for only primary keys to be displayed, you can use the following code:

Code: Select all

  UniMetaData1.MetaDataKind := 'Constraints';
  UniMetaData1.Restrictions.Values['CONSTRAINT_TYPE'] := 'PRIMARY KEY';
  UniMetaData1.Open;