@@ -708,6 +708,7 @@ TSqlSrvConnection = class(TDBConnection)
708708 function GetLastResults : TDBQueryList; override;
709709 property LastRawResults: TSqlSrvResults read FLastRawResults;
710710 function GetTableColumns (Table: TDBObject): TTableColumnList; override;
711+ function GetTableKeys (Table: TDBObject): TTableKeyList; override;
711712 function GetTableForeignKeys (Table: TDBObject): TForeignKeyList; override;
712713 end ;
713714 { $ENDIF}
@@ -6249,6 +6250,51 @@ function TPGConnection.GetTableKeys(Table: TDBObject): TTableKeyList;
62496250 KeyQuery.Free;
62506251end ;
62516252
6253+ { $IFDEF HASMSSQL}
6254+ function TSqlSrvConnection.GetTableKeys (Table: TDBObject): TTableKeyList;
6255+ var
6256+ KeyQuery: TDBQuery;
6257+ NewKey: TTableKey;
6258+ begin
6259+ // IS.TABLE_CONSTRAINTS only lists PRIMARY and UNIQUE constraints, not indexes created
6260+ // with CREATE INDEX. Included (non-key) columns are left out.
6261+ Result := TTableKeyList.Create(True);
6262+ KeyQuery := GetResults(' SELECT' +
6263+ ' i.name AS CONSTRAINT_NAME,' +
6264+ ' CASE WHEN i.is_primary_key=1 THEN ' +EscapeString(TTableKey.PRIMARY)+
6265+ ' WHEN i.is_unique=1 THEN ' +EscapeString(TTableKey.UNIQUE)+
6266+ ' ELSE ' +EscapeString(TTableKey.KEY)+' END AS CONSTRAINT_TYPE,' +
6267+ ' c.name AS COLUMN_NAME,' +
6268+ ' CASE WHEN ic.is_descending_key=1 THEN ' +EscapeString(' D' )+' ELSE ' +EscapeString(' A' )+' END AS SORT_ORDER' +
6269+ ' FROM sys.indexes AS i' +
6270+ ' INNER JOIN sys.index_columns AS ic' +
6271+ ' ON ic.object_id = i.object_id AND ic.index_id = i.index_id' +
6272+ ' INNER JOIN sys.columns AS c' +
6273+ ' ON c.object_id = ic.object_id AND c.column_id = ic.column_id' +
6274+ ' WHERE i.object_id = OBJECT_ID(' +EscapeString(Table.QuotedDbAndTableName)+' )' +
6275+ ' AND i.type IN (1, 2)' + // Clustered and nonclustered rowstore indexes
6276+ ' AND ic.key_ordinal > 0' +
6277+ ' ORDER BY i.index_id, ic.key_ordinal'
6278+ );
6279+ NewKey := nil ;
6280+ while not KeyQuery.Eof do begin
6281+ if (not Assigned(NewKey)) or (NewKey.Name <> KeyQuery.Col(' CONSTRAINT_NAME' )) then begin
6282+ NewKey := TTableKey.Create(Self);
6283+ Result.Add(NewKey);
6284+ NewKey.Name := KeyQuery.Col(' CONSTRAINT_NAME' );
6285+ NewKey.OldName := NewKey.Name ;
6286+ NewKey.IndexType := KeyQuery.Col(' CONSTRAINT_TYPE' );
6287+ NewKey.OldIndexType := NewKey.IndexType;
6288+ end ;
6289+ NewKey.Columns.Add(KeyQuery.Col(' COLUMN_NAME' ));
6290+ NewKey.SubParts.Add(' ' );
6291+ NewKey.Collations.Add(KeyQuery.Col(' SORT_ORDER' ));
6292+ KeyQuery.Next;
6293+ end ;
6294+ KeyQuery.Free;
6295+ end ;
6296+ { $ENDIF}
6297+
62526298
62536299function TSQLiteConnection.GetTableKeys (Table: TDBObject): TTableKeyList;
62546300var
0 commit comments