Is a TField in primary key?

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
chkaufmann
Posts: 82
Joined: Sat 01 Jul 2006 11:42

Is a TField in primary key?

Post by chkaufmann » Tue 05 Jul 2011 12:54

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

AndreyZ

Post by AndreyZ » Wed 06 Jul 2011 07:49

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.

chkaufmann
Posts: 82
Joined: Sat 01 Jul 2006 11:42

Post by chkaufmann » Wed 06 Jul 2011 08:01

I suggest something like

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

cu Christian

AndreyZ

Post by AndreyZ » Wed 06 Jul 2011 12:56

Thank you for your suggestion.

chkaufmann
Posts: 82
Joined: Sat 01 Jul 2006 11:42

Post by chkaufmann » Mon 11 Jul 2011 05:45

AndreyZ wrote:Thank you for your suggestion.
Do you have an idea, when this extension could be available?

cu Christian

AndreyZ

Post by AndreyZ » Mon 11 Jul 2011 11:41

We are still investigating this question. We will try to implement this functionality in one of the nearest UniDAC builds.

Krampus
Posts: 3
Joined: Fri 21 Feb 2014 12:23

Re: Is a TField in primary key?

Post by Krampus » Fri 21 Feb 2014 15:43

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)

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Is a TField in primary key?

Post by AlexP » Mon 24 Feb 2014 09:36

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;

Post Reply