ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/runtime/IBTable.pas
(Generate patch)

Comparing ibx/trunk/runtime/IBTable.pas (file contents):
Revision 17 by tony, Sat Dec 28 19:22:24 2013 UTC vs.
Revision 118 by tony, Mon Jan 22 13:58:14 2018 UTC

# Line 1 | Line 1
1 < {************************************************************************}
2 < {                                                                        }
3 < {       Borland Delphi Visual Component Library                          }
4 < {       InterBase Express core components                                }
5 < {                                                                        }
6 < {       Copyright (c) 1998-2000 Inprise Corporation                      }
7 < {                                                                        }
8 < {    InterBase Express is based in part on the product                   }
9 < {    Free IB Components, written by Gregory H. Deatz for                 }
10 < {    Hoagland, Longo, Moran, Dunst & Doukas Company.                     }
11 < {    Free IB Components is used under license.                           }
12 < {                                                                        }
13 < {    The contents of this file are subject to the InterBase              }
14 < {    Public License Version 1.0 (the "License"); you may not             }
15 < {    use this file except in compliance with the License. You            }
16 < {    may obtain a copy of the License at http://www.Inprise.com/IPL.html }
17 < {    Software distributed under the License is distributed on            }
18 < {    an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either              }
19 < {    express or implied. See the License for the specific language       }
20 < {    governing rights and limitations under the License.                 }
21 < {    The Original Code was created by InterBase Software Corporation     }
22 < {       and its successors.                                              }
23 < {    Portions created by Inprise Corporation are Copyright (C) Inprise   }
24 < {       Corporation. All Rights Reserved.                                }
25 < {    Contributor(s): Jeff Overcash                                       }
26 < {                                                                        }
27 < {    IBX For Lazarus (Firebird Express)                                  }
28 < {    Contributor: Tony Whyman, MWA Software http://www.mwasoftware.co.uk }
29 < {    Portions created by MWA Software are copyright McCallum Whyman      }
30 < {    Associates Ltd 2011                                                 }
31 < {                                                                        }
32 < {************************************************************************}
33 <
34 < unit IBTable;
35 <
36 < {$Mode Delphi}
37 <
38 < interface
39 <
40 < uses SysUtils, Classes, DB, IB,  IBCustomDataSet,
41 <     IBHeader, IBSQL, IBUtils;
42 <    
43 < type
44 <
45 < { TIBTable }
46 <
47 <  TIBTableType = (ttSystem, ttView);
48 <  TIBTableTypes = set of TIBTableType;
49 <  TIndexName = String;
50 <
51 <  TIBTable = class;
52 <
53 <  TIBTable = class(TIBCustomDataSet)
54 <  private
55 <    FSystemTable: Boolean;
56 <    FMultiTableView: Boolean;
57 <    FMasterLink: TMasterDataLink;
58 <    FMasterFieldsList: TStringList;
59 <    FDetailFieldsList: TStringList;
60 <    FStoreDefs: Boolean;
61 <    FIndexDefs: TIndexDefs;
62 <    FDefaultIndex: Boolean;
63 <    FReadOnly: Boolean;
64 <    FFieldsIndex: Boolean;
65 <    FTableName: String;
66 <    FIndexName: TIndexName;
67 <    FRegenerateSQL: Boolean;
68 <    FNameList: TStrings;
69 <    FSwitchingIndex: Boolean;
70 <    FPrimaryIndexFields: string;
71 <    FTableTypes: TIBTableTypes;
72 <    WhereAllRefreshSQL: TStrings;
73 <    WhereDBKeyRefreshSQL: TStrings;
74 <    WherePrimaryRefreshSQL: TStrings;
75 <
76 <    function GetIndexFieldCount: Integer;
77 <    function GetIndexField(Index: Integer): TField;
78 <    procedure MasterChanged(Sender: TObject);
79 <    procedure MasterDisabled(Sender: TObject);
80 <    procedure SetDataSource(Value: TDataSource);
81 <    procedure SetIndexField(Index: Integer; Value: TField);
82 <    procedure SetIndexFieldNames(const Value: string);
83 <    procedure GenerateSQL;
84 <    procedure GenerateUpdateSQL;
85 <    procedure SwitchToIndex();
86 <    procedure InternalTableRefresh();
87 <    function GetTableNames: TStrings;
88 <    procedure GetTableNamesFromServer;
89 <    procedure SetTableTypes(
90 <    const Value: TIBTableTypes);
91 <    function InternalGotoDBKey(DBKey: TIBDBKey): Boolean;
92 <    function FormatFieldsList(Value: string): string;
93 <    function GetCurrentDBKey: TIBDBKey;
94 <    function InternalGetUpdatable: Boolean;
95 <    function GetExists: Boolean;
96 <    procedure SetIndexDefs(Value: TIndexDefs);
97 <    procedure ExtractLinkFields;
98 <    function FieldDefsStored: Boolean;
99 <    function IndexDefsStored: Boolean;
100 <    function GetMasterFields: string;
101 <    procedure SetMasterFields(const Value: string);
102 <    function GetIndexFieldNames: string;
103 <    function GetIndexName: string;
104 <    procedure SetIndexName(const Value: string);
105 <    procedure SetParams;
106 <    procedure SetReadOnly(Value: Boolean);
107 <    procedure SetTableName(Value: String);
108 <    procedure SetIndex(const Value: string; FieldsIndex: Boolean);
109 <    procedure ResetSQLStatements;
110 <    procedure Reopen;
111 <
112 <  protected
113 <
114 <    procedure DoOnNewRecord; override;
115 <    procedure GetIndexParams(const IndexName: string; FieldsIndex: Boolean;
116 <      var IndexedName: string);
117 <    function GetCanModify: Boolean; override;
118 <    procedure UpdateIndexDefs; override;
119 <    procedure DataEvent(Event: TDataEvent; Info: Ptrint); override;
120 <    procedure DefChanged(Sender: TObject); virtual;
121 <    function GetDataSource: TDataSource; override;
122 <    procedure InitFieldDefs; override;
123 <    procedure InternalClose; override;
124 <    procedure InternalOpen; override;
125 <    procedure InternalRefresh; override;
126 <    procedure SetFiltered(Value: Boolean); override;
127 <    procedure SetFilterText(const Value: string); override;
128 <    procedure SetFilterOptions(Value: TFilterOptions); override;
129 <    procedure InternalRefreshRow; override;
130 <
131 <  public
132 <    constructor Create(AOwner: TComponent); override;
133 <    destructor Destroy; override;
134 <    procedure AddIndex(const Name, Fields: string; Options: TIndexOptions;
135 <      const DescFields: string = '');
136 <    procedure CreateTable;
137 <    procedure DeleteIndex(const Name: string);
138 <    procedure DeleteTable;
139 <    procedure EmptyTable;
140 <    procedure GetDetailLinkFields(MasterFields, DetailFields: TList); virtual;
141 <    procedure GetIndexNames(List: TStrings);
142 <    procedure GotoCurrent(Table: TIBTable);
143 <    property CurrentDBKey: TIBDBKey read GetCurrentDBKey;
144 <    property Exists: Boolean read GetExists;
145 <    property IndexFieldCount: Integer read GetIndexFieldCount;
146 <    property IndexFields[Index: Integer]: TField read GetIndexField write SetIndexField;
147 <    property TableNames: TStrings read GetTableNames;
148 <
149 <  published
150 <    property Active;
151 <    property BufferChunks;
152 <    property CachedUpdates;
153 < //    property Constraints stored ConstraintsStored;
154 <    property DefaultIndex: Boolean read FDefaultIndex write FDefaultIndex default True;
155 <    property FieldDefs stored FieldDefsStored;
156 <    property Filter;
157 <    property Filtered;
158 <    property IndexDefs: TIndexDefs read FIndexDefs write SetIndexDefs stored IndexDefsStored;
159 <    property IndexFieldNames: string read GetIndexFieldNames write SetIndexFieldNames;
160 <    property IndexName: string read GetIndexName write SetIndexName;
161 <    property MasterFields: string read GetMasterFields write SetMasterFields;
162 <    property MasterSource: TDataSource read GetDataSource write SetDataSource;
163 <    property ReadOnly: Boolean read FReadOnly write SetReadOnly default False;
164 <    property StoreDefs: Boolean read FStoreDefs write FStoreDefs default False;
165 <    property TableName: String read FTableName write SetTableName;
166 <    property TableTypes: TIBTableTypes read FTableTypes write SetTableTypes default [];
167 <    property UpdateObject;
168 <    property UniDirectional;
169 <
170 <    property BeforeDatabaseDisconnect;
171 <    property AfterDatabaseDisconnect;
172 <    property DatabaseFree;
173 <    property BeforeTransactionEnd;
174 <    property AfterTransactionEnd;
175 <    property TransactionFree;
176 <    property OnFilterRecord;
177 <  end;
178 <
179 < implementation
180 <
181 < { TIBTable }
182 <
183 < constructor TIBTable.Create(AOwner: TComponent);
184 < begin
185 <  inherited Create(AOwner);
186 <  FNameList := TStringList.Create;
187 <  FSwitchingIndex := False;
188 <  FIndexDefs := TIndexDefs.Create(Self);
189 <  WhereAllRefreshSQL := TStringList.Create;
190 <  WhereDBKeyRefreshSQL := TStringList.Create;
191 <  WherePrimaryRefreshSQL := TStringList.Create;
192 <  FDefaultIndex := True;
193 <  FRegenerateSQL := True;
194 <  FMasterFieldsList := TStringList.Create;
195 <  FDetailFieldsList := TStringList.Create;
196 <  FMasterLink := TMasterDataLink.Create(Self);
197 <  FMasterLink.OnMasterChange := MasterChanged;
198 <  FMasterLink.OnMasterDisable := MasterDisabled;
199 <  QRefresh.OnSQLChanging := nil;
200 <  QDelete.OnSQLChanging := nil;
201 <  QInsert.OnSQLChanging := nil;
202 <  QModify.OnSQLChanging := nil;
203 < end;
204 <
205 < destructor TIBTable.Destroy;
206 < begin
207 <  FNameList.Free;
208 <  FIndexDefs.Free;
209 <  FMasterFieldsList.Free;
210 <  FDetailFieldsList.Free;
211 <  FMasterLink.Free;
212 <  WhereAllRefreshSQL.Free;
213 <  WhereDBKeyRefreshSQL.Free;
214 <  WherePrimaryRefreshSQL.Free;
215 <  inherited Destroy;
216 < end;
217 <
218 < procedure TIBTable.InternalClose;
219 < begin
220 <  DataEvent(dePropertyChange, 0);
221 <  inherited InternalClose;
222 < end;
223 <
224 < procedure TIBTable.InternalOpen;
225 < begin
226 <  if FTableName = '' then IBError(ibxeNoTableName, [nil]);
227 <  ActivateConnection;
228 <  ActivateTransaction;
229 <  if FRegenerateSQL then
230 <  begin
231 <    InternalUnprepare;
232 <    GenerateSQL;
233 <    if not FReadOnly then
234 <      GenerateUpdateSQL;
235 <    FRegenerateSQL := False;
236 <  end;
237 <  SetParams;
238 <  inherited InternalOpen;
239 < end;
240 <
241 < procedure TIBTable.InternalRefresh;
242 < var
243 <  DBKey: TIBDBKey;
244 < begin
245 <  DBKey := CurrentDBKey;
246 <  Reopen;
247 <  if DBKey.DBKey[0] <> 0 then
248 <    InternalGotoDBKey(DBKey);
249 < end;
250 <
251 < procedure TIBTable.SetFiltered(Value: Boolean);
252 < begin
253 <  if(Filtered <> Value) then
254 <  begin
255 <    inherited SetFiltered(value);
256 <    if Active then
257 <      InternalTableRefresh;
258 <  end
259 <  else
260 <    inherited SetFiltered(value);
261 < end;
262 <
263 < procedure TIBTable.SetFilterText(const Value: string);
264 < begin
265 <  if Filtered and (Value <> Filter) then
266 <  begin
267 <    inherited SetFilterText(value);
268 <    InternalTableRefresh;
269 <  end
270 <  else
271 <    inherited SetFilterText(value);
272 < end;
273 <
274 < procedure TIBTable.SetFilterOptions(Value: TFilterOptions);
275 < begin
276 <  if Value <> [] then
277 <    IBError(ibxeNotSupported, [nil]);
278 < end;
279 <
280 < procedure TIBTable.InternalRefreshRow;
281 < begin
282 <  if CurrentDBKey.DBKey[0] <> 0 then
283 <    QRefresh.SQL.Assign(WhereDBKeyRefreshSQL)
284 <  else if WherePrimaryRefreshSQL.Text <> '' then
285 <    QRefresh.SQL.Assign(WherePrimaryRefreshSQL)
286 <  else
287 <    QRefresh.SQL.Assign(WhereAllRefreshSQL);
288 <  inherited InternalRefreshRow;
289 < end;
290 <
291 < procedure TIBTable.DefChanged(Sender: TObject);
292 < begin
293 <  StoreDefs := True;
294 < end;
295 <
296 < procedure TIBTable.InitFieldDefs;
297 < var
298 <  sqlscale: Integer;
299 <  Query: TIBSQL;
300 < begin
301 <  if FTableName = '' then IBError(ibxeNoTableName, [nil]);
302 <  if (InternalPrepared) then InternalInitFieldDefs else
303 <  begin
304 <    Database.InternalTransaction.StartTransaction;
305 <    Query := TIBSQL.Create(self);
306 <    try
307 <      Query.GoToFirstRecordOnExecute := False;
308 <      Query.Database := DataBase;
309 <      Query.Transaction := Database.InternalTransaction;
310 <      Query.SQL.Text := 'Select R.RDB$FIELD_NAME, R.RDB$FIELD_POSITION, ' + {do not localize}
311 <                        'F.RDB$COMPUTED_BLR, F.RDB$DEFAULT_VALUE, ' + {do not localize}
312 <                        'F.RDB$NULL_FLAG, ' + {do not localize}
313 <                        'F.RDB$FIELD_LENGTH, F.RDB$FIELD_SCALE, ' + {do not localize}
314 <                        'F.RDB$FIELD_TYPE, F.RDB$FIELD_SUB_TYPE, ' + {do not localize}
315 <                        'F.RDB$EXTERNAL_LENGTH, F.RDB$EXTERNAL_SCALE, F.RDB$EXTERNAL_TYPE ' + {do not localize}
316 <                        'from RDB$RELATION_FIELDS R, RDB$FIELDS F ' + {do not localize}
317 <                        'where R.RDB$RELATION_NAME = ' + {do not localize}
318 <                        '''' +
319 <                        FormatIdentifierValue(Database.SQLDialect,
320 <                          QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
321 <                        ''' ' +
322 <                        'and R.RDB$FIELD_SOURCE = F.RDB$FIELD_NAME '+ {do not localize}
323 <                        'order by R.RDB$FIELD_POSITION'; {do not localize}
324 <
325 <      Query.Prepare;
326 <      Query.ExecQuery;
327 <      FieldDefs.BeginUpdate;
328 <      FieldDefs.Clear;
329 <      while (not Query.EOF) and (Query.Next <> nil) do
330 <      begin
331 <          with FieldDefs.AddFieldDef do
332 <          begin
333 < (*           FieldNo := Query.Current.ByName('RDB$FIELD_POSITION').AsInteger; {do not localize}*)
334 <            Name := TrimRight(Query.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize}
335 <            case Query.Current.ByName('RDB$FIELD_TYPE').AsInteger of {do not localize}
336 <              blr_varying, blr_text:
337 <              begin
338 <                DataType := ftString;
339 <                Size := Query.Current.ByName('RDB$FIELD_LENGTH').AsInteger; {do not localize}
340 <              end;
341 <              blr_float, blr_double, blr_d_float: DataType := ftFloat;
342 <              blr_short:
343 <              begin
344 <                sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize}
345 <                if (sqlscale = 0) then
346 <                  DataType := ftSmallInt
347 <                else
348 <                begin
349 <                  DataType := ftBCD;
350 <                  Precision := 4;
351 <                end;
352 <              end;
353 <              blr_long:
354 <              begin
355 <                sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize}
356 <                if (sqlscale = 0) then
357 <                  DataType := ftInteger
358 <                else if (sqlscale >= (-4)) then
359 <                begin
360 <                  DataType := ftBCD;
361 <                  Precision := 9;
362 <                end
363 <                else
364 <                  DataType := ftFloat;
365 <              end;
366 <              blr_int64:
367 <              begin
368 <                sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize}
369 <                if (sqlscale = 0) then
370 <                  DataType := ftLargeInt
371 <                else if (sqlscale >= (-4)) then
372 <                begin
373 <                  DataType := ftBCD;
374 <                  Precision := 18;
375 <                end
376 <                else
377 <                  DataType := ftFloat;
378 <              end;
379 <              blr_timestamp: DataType := ftDateTime;
380 <              blr_sql_time: DataType := ftTime;
381 <              blr_sql_date: DataType := ftDate;
382 <              blr_blob:
383 <                if (Query.Current.ByName('RDB$FIELD_SUB_TYPE').AsInteger = 1) then {do not localize}
384 <                  DataType := ftMemo
385 <                else
386 <                  DataType := ftBlob;
387 <              blr_quad:
388 <              begin
389 <                DataType := ftUnknown;
390 <                Size := sizeof (TISC_QUAD);
391 <              end;
392 <              else
393 <                DataType := ftUnknown;
394 <            end;
395 <            if not (Query.Current.ByName('RDB$COMPUTED_BLR').IsNull) then {do not localize}
396 <            begin
397 <              Attributes := [faReadOnly];
398 <              InternalCalcField := True
399 <            end
400 <            else
401 <              InternalCalcField := False;
402 <            if ((not InternalCalcField) and
403 <                 Query.Current.ByName('RDB$DEFAULT_VALUE').IsNull and {do not localize}
404 <                 (Query.Current.ByName('RDB$NULL_FLAG').AsInteger = 1) )then {do not localize}
405 <            begin
406 <              Attributes := [faRequired];
407 <              Required := True;
408 <            end;
409 <          end;
410 <      end;
411 <      FieldDefs.EndUpdate;
412 <    finally
413 <      Query.free;
414 <      Database.InternalTransaction.Commit;
415 <    end;
416 <  end;
417 < end;
418 <
419 < { Index / Ranges / Keys }
420 <
421 < procedure TIBTable.AddIndex(const Name, Fields: string; Options: TIndexOptions;
422 <  const DescFields: string);
423 < var
424 <  Query: TIBSQL;
425 <  FieldList: string;
426 < begin
427 <  FieldDefs.Update;
428 <  if Active then begin
429 <    CheckBrowseMode;
430 <    CursorPosChanged;
431 <  end;
432 <  Query := TIBSQL.Create(self);
433 <  try
434 <    Query.Database := DataBase;
435 <    Query.Transaction := Transaction;
436 <    FieldList := FormatFieldsList(Fields);
437 <    if (ixPrimary in Options) then
438 <    begin
439 <     Query.SQL.Text := 'Alter Table ' + {do not localize}
440 <       QuoteIdentifier(Database.SQLDialect, FTableName) +
441 <       ' Add CONSTRAINT ' +   {do not localize}
442 <       QuoteIdentifier(Database.SQLDialect, Name)
443 <       + ' Primary Key (' + {do not localize}
444 <       FormatFieldsList(Fields) +
445 <       ')';
446 <    end
447 <    else if ([ixUnique, ixDescending] * Options = [ixUnique, ixDescending]) then
448 <      Query.SQL.Text := 'Create unique Descending Index ' + {do not localize}
449 <                        QuoteIdentifier(Database.SQLDialect, Name) +
450 <                        ' on ' + {do not localize}
451 <                        QuoteIdentifier(Database.SQLDialect, FTableName) +
452 <                        ' (' + FieldList + ')'
453 <    else if (ixUnique in Options) then
454 <      Query.SQL.Text := 'Create unique Index ' + {do not localize}
455 <                        QuoteIdentifier(Database.SQLDialect, Name) +
456 <                        ' on ' + {do not localize}
457 <                        QuoteIdentifier(Database.SQLDialect, FTableName) +
458 <                        ' (' + FieldList + ')'
459 <    else if (ixDescending in Options) then
460 <      Query.SQL.Text := 'Create Descending Index ' + {do not localize}
461 <                        QuoteIdentifier(Database.SQLDialect, Name) +
462 <                        ' on ' + {do not localize}
463 <                        QuoteIdentifier(Database.SQLDialect, FTableName) +
464 <                        ' (' + FieldList + ')'
465 <    else
466 <      Query.SQL.Text := 'Create Index ' + {do not localize}
467 <                        QuoteIdentifier(Database.SQLDialect, Name) +
468 <                        ' on ' + {do not localize}
469 <                        QuoteIdentifier(Database.SQLDialect, FTableName) +
470 <                        ' (' + FieldList + ')';
471 <    Query.Prepare;
472 <    Query.ExecQuery;
473 <    IndexDefs.Updated := False;
474 <  finally
475 <    Query.free
476 <  end;
477 < end;
478 <
479 < procedure TIBTable.DeleteIndex(const Name: string);
480 < var
481 <  Query: TIBSQL;
482 <
483 <  procedure DeleteByIndex;
484 <  begin
485 <    Query := TIBSQL.Create(self);
486 <    try
487 <      Query.Database := DataBase;
488 <      Query.Transaction := Transaction;
489 <      Query.SQL.Text := 'Drop index ' +  {do not localize}
490 <                         QuoteIdentifier(Database.SQLDialect, Name);
491 <      Query.Prepare;
492 <      Query.ExecQuery;
493 <      IndexDefs.Updated := False;
494 <    finally
495 <      Query.Free;
496 <    end;
497 <  end;
498 <
499 <  function DeleteByConstraint: Boolean;
500 <  begin
501 <    Result := False;
502 <    Query := TIBSQL.Create(self);
503 <    try
504 <      Query.Database := DataBase;
505 <      Query.Transaction := Transaction;
506 <      Query.SQL.Text := 'Select ''foo'' from RDB$RELATION_CONSTRAINTS ' +
507 <        'where RDB$RELATION_NAME = ' +
508 <        '''' +
509 <        FormatIdentifierValue(Database.SQLDialect,
510 <          QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
511 <        ''' ' +
512 <        ' AND RDB$CONSTRAINT_NAME = ' +
513 <        '''' +
514 <        FormatIdentifierValue(Database.SQLDialect,
515 <          QuoteIdentifier(DataBase.SQLDialect, Name)) +
516 <        ''' ' +
517 <        'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''';
518 <      Query.Prepare;
519 <      Query.ExecQuery;
520 <      if not Query.EOF then
521 <      begin
522 <        Query.Close;
523 <        Query.SQL.Text := 'Alter Table ' +  {do not localize}
524 <          QuoteIdentifier(DataBase.SQLDialect, FTableName) +
525 <          ' Drop Constraint ' +
526 <          QuoteIdentifier(DataBase.SQLDialect, Name);
527 <        Query.Prepare;
528 <        Query.ExecQuery;
529 <        IndexDefs.Updated := False;
530 <        Result := True;
531 <      end;
532 <    finally
533 <      Query.Free;
534 <    end;
535 <  end;
536 <
537 <  procedure DeleteByKey;
538 <  begin
539 <    Query := TIBSQL.Create(self);
540 <    try
541 <      Query.Database := DataBase;
542 <      Query.Transaction := Transaction;
543 <      Query.SQL.Text := 'Select RDB$CONSTRAINT_NAME from RDB$RELATION_CONSTRAINTS ' +
544 <        'where RDB$RELATION_NAME = ' +
545 <        '''' +
546 <        FormatIdentifierValue(Database.SQLDialect,
547 <          QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
548 <        ''' ' +
549 <        'AND RDB$INDEX_NAME = ' +
550 <        '''' +
551 <        FormatIdentifierValue(Database.SQLDialect,
552 <          QuoteIdentifier(DataBase.SQLDialect, Name)) +
553 <        ''' ' +
554 <        'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''';
555 <      Query.Prepare;
556 <      Query.ExecQuery;
557 <      if not Query.EOF then
558 <      begin
559 <        Query.Close;
560 <        Query.SQL.Text := 'Alter Table ' +  {do not localize}
561 <          QuoteIdentifier(DataBase.SQLDialect, FTableName) +
562 <          ' Drop Constraint ' +
563 <          QuoteIdentifier(DataBase.SQLDialect, Query.Current.ByName('RDB$CONSTRAINT_NAME').AsString);
564 <        Query.Prepare;
565 <        Query.ExecQuery;
566 <        IndexDefs.Updated := False;
567 <      end;
568 <    finally
569 <      Query.Free;
570 <    end;
571 <  end;
572 <
573 < begin
574 <  if Active then
575 <    CheckBrowseMode;
576 <  IndexDefs.Update;
577 <  if (Pos('RDB$PRIMARY', Name) <> 0 ) then {do not localize} {mbcs ok}
578 <    DeleteByKey
579 <  else if not DeleteByConstraint then
580 <    DeleteByIndex;
581 < end;
582 <
583 < function TIBTable.GetIndexFieldNames: string;
584 < begin
585 <  if FFieldsIndex then Result := FIndexName else Result := '';
586 < end;
587 <
588 < function TIBTable.GetIndexName: string;
589 < begin
590 <  if FFieldsIndex then Result := '' else Result := FIndexName;
591 < end;
592 <
593 < procedure TIBTable.GetIndexNames(List: TStrings);
594 < begin
595 <  IndexDefs.Update;
596 <  IndexDefs.GetItemNames(List);
597 < end;
598 <
599 < procedure TIBTable.GetIndexParams(const IndexName: string;
600 <  FieldsIndex: Boolean; var IndexedName: string);
601 < var
602 <  IndexStr: TIndexName;
603 < begin
604 <  if IndexName <> '' then
605 <  begin
606 <    IndexDefs.Update;
607 <    IndexStr := IndexName;
608 <    if FieldsIndex then
609 <      IndexStr := IndexDefs.FindIndexForFields(IndexName).Name;
610 <  end;
611 <  IndexedName := IndexStr;
612 < end;
613 <
614 < procedure TIBTable.SetIndexDefs(Value: TIndexDefs);
615 < begin
616 <  IndexDefs.Assign(Value);
617 < end;
618 <
619 < procedure TIBTable.SetIndex(const Value: string; FieldsIndex: Boolean);
620 < begin
621 <  if Active then CheckBrowseMode;
622 <  if (FIndexName <> Value) or (FFieldsIndex <> FieldsIndex) then
623 <  begin
624 <    FIndexName := Value;
625 <    FFieldsIndex := FieldsIndex;
626 <    if Active then
627 <    begin
628 <      SwitchToIndex;
629 <    end;
630 <  end;
631 < end;
632 <
633 < procedure TIBTable.SetIndexFieldNames(const Value: string);
634 < begin
635 <  SetIndex(Value, Value <> '');
636 < end;
637 <
638 < procedure TIBTable.SetIndexName(const Value: string);
639 < begin
640 <  SetIndex(Value, False);
641 < end;
642 <
643 < procedure TIBTable.UpdateIndexDefs;
644 < var
645 <  Opts: TIndexOptions;
646 <  Flds: string;
647 <  Query, SubQuery: TIBSQL;
648 < begin
649 <  if not (csReading in ComponentState) then begin
650 <  if not Active and not FSwitchingIndex  then
651 <    FieldDefs.Update;
652 <  IndexDefs.Clear;
653 <  Database.InternalTransaction.StartTransaction;
654 <  Query := TIBSQL.Create(self);
655 <  try
656 <    FPrimaryIndexFields := '';
657 <    Query.GoToFirstRecordOnExecute := False;
658 <    Query.Database := DataBase;
659 <    Query.Transaction := Database.InternalTransaction;
660 <    Query.SQL.Text :=
661 <    'Select I.RDB$INDEX_NAME, I.RDB$UNIQUE_FLAG, I.RDB$INDEX_TYPE, ' + {do not localize}
662 <    'I.RDB$SEGMENT_COUNT, S.RDB$FIELD_NAME from RDB$INDICES I, ' + {do not localize}
663 <    'RDB$INDEX_SEGMENTS S where I.RDB$INDEX_NAME = S.RDB$INDEX_NAME '+ {do not localize}
664 <    'and I.RDB$RELATION_NAME = ' + '''' + {do not localize}
665 <     FormatIdentifierValue(Database.SQLDialect,
666 <       QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
667 <    Query.Prepare;
668 <    Query.ExecQuery;
669 <    while (not Query.EOF) and (Query.Next <> nil) do
670 <    begin
671 <      with IndexDefs.AddIndexDef do
672 <      begin
673 <        Name := TrimRight(Query.Current.ByName('RDB$INDEX_NAME').AsString); {do not localize}
674 <        Opts := [];
675 <        if Pos ('RDB$PRIMARY', Name) = 1 then Include(Opts, ixPrimary); {do not localize} {mbcs ok}
676 <        if Query.Current.ByName('RDB$UNIQUE_FLAG').AsInteger = 1 then Include(Opts, ixUnique); {do not localize}
677 <        if Query.Current.ByName('RDB$INDEX_TYPE').AsInteger = 2  then Include(Opts, ixDescending); {do not localize}
678 <        Options := Opts;
679 <        if (Query.Current.ByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize}
680 <          Fields := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize}
681 <        else begin
682 <          SubQuery := TIBSQL.Create(self);
683 <        try
684 <          SubQuery.GoToFirstRecordOnExecute := False;
685 <          SubQuery.Database := DataBase;
686 <          SubQuery.Transaction := Database.InternalTransaction;
687 <          SubQuery.SQL.Text :=
688 <         'Select RDB$FIELD_NAME from RDB$INDEX_SEGMENTS where RDB$INDEX_NAME = ' + {do not localize}
689 <          '''' +
690 <          FormatIdentifierValue(Database.SQLDialect,
691 <            QuoteIdentifier(DataBase.SQLDialect, Name)) +
692 <          '''' + 'ORDER BY RDB$FIELD_POSITION'; {do not localize}
693 <          SubQuery.Prepare;
694 <          SubQuery.ExecQuery;
695 <          Flds := '';
696 <          while (not SubQuery.EOF) and (SubQuery.Next <> nil) do
697 <          begin
698 <            if (Flds = '') then
699 <              Flds := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize}
700 <            else begin
701 <              Query.Next;
702 <              Flds := Flds + ';' + TrimRight(SubQuery.Current[0].AsString);
703 <            end;
704 <          end;
705 <          Fields := Flds;
706 <        finally
707 <          SubQuery.Free;
708 <        end;
709 <        end;
710 <        if (ixDescending in Opts) then
711 <          DescFields := Fields;
712 <        if ixPrimary in Opts then
713 <          FPrimaryIndexFields := Fields;
714 <      end;
715 <    end;
716 <  finally
717 <    Query.Free;
718 <    Database.InternalTransaction.Commit;
719 <  end;
720 <  end;
721 < end;
722 <
723 < function TIBTable.GetExists: Boolean;
724 < var
725 <  Query: TIBSQL;
726 < begin
727 <  Result := Active;
728 <  if Result or (TableName = '') then Exit;
729 <  Database.InternalTransaction.StartTransaction;
730 <  Query := TIBSQL.Create(self);
731 <  try
732 <    Query.Database := DataBase;
733 <    Query.Transaction := Database.InternalTransaction;
734 <    Query.SQL.Text :=
735 <    'Select USER from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize}
736 <    '''' +
737 <    FormatIdentifierValue(Database.SQLDialect,
738 <      QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
739 <    Query.Prepare;
740 <    Query.ExecQuery;
741 <    Result := not Query.EOF;
742 <  finally
743 <    Query.Free;
744 <    Database.InternalTransaction.Commit;
745 <  end;
746 < end;
747 <
748 < procedure TIBTable.GotoCurrent(Table: TIBTable);
749 < begin
750 <  CheckBrowseMode;
751 <  Table.CheckBrowseMode;
752 <  if (Database <> Table.Database) or
753 <    (CompareText(TableName, Table.TableName) <> 0) then
754 <    IBError(ibxeTableNameMismatch, [nil]);
755 <  Table.UpdateCursorPos;
756 <  InternalGotoDBKey(Table.CurrentDBKey);
757 <  DoBeforeScroll;
758 <  Resync([rmExact, rmCenter]);
759 <  DoAfterScroll;
760 < end;
761 <
762 <
763 < procedure TIBTable.CreateTable;
764 < var
765 <  FieldList: string;
766 <
767 <  procedure InitFieldsList;
768 <  var
769 <    I: Integer;
770 <  begin
771 <    InitFieldDefsFromFields;
772 <    for I := 0 to FieldDefs.Count - 1 do begin
773 <      if ( I > 0) then
774 <        FieldList := FieldList + ', ';
775 <      with FieldDefs[I] do
776 <      begin
777 <        case DataType of
778 <          ftString:
779 <            FieldList := FieldList +
780 <              QuoteIdentifier(DataBase.SQLDialect, Name) +
781 <              ' VARCHAR(' + IntToStr(Size) + ')'; {do not localize}
782 <          ftFixedChar:
783 <            FieldList := FieldList +
784 <              QuoteIdentifier(DataBase.SQLDialect, Name) +
785 <              ' CHAR(' + IntToStr(Size) + ')'; {do not localize}
786 <          ftBoolean, ftSmallint, ftWord:
787 <            FieldList := FieldList +
788 <              QuoteIdentifier(DataBase.SQLDialect, Name) +
789 <              ' SMALLINT'; {do not localize}
790 <          ftInteger:
791 <            FieldList := FieldList +
792 <              QuoteIdentifier(DataBase.SQLDialect, Name) +
793 <              ' INTEGER'; {do not localize}
794 <          ftFloat, ftCurrency:
795 <            FieldList := FieldList +
796 <              QuoteIdentifier(DataBase.SQLDialect, Name) +
797 <              ' DOUBLE PRECISION'; {do not localize}
798 <          ftBCD: begin
799 <            if (Database.SQLDialect = 1) then begin
800 <              if (Precision > 9) then
801 <                IBError(ibxeFieldUnsupportedType,[nil]);
802 <              if (Precision <= 4) then
803 <                Precision := 9;
804 <            end;
805 <            if (Precision <= 4 ) then
806 <              FieldList := FieldList +
807 <                QuoteIdentifier(DataBase.SQLDialect, Name) +
808 <                ' Numeric(18, 4)' {do not localize}
809 <            else
810 <              FieldList := FieldList +
811 <                QuoteIdentifier(DataBase.SQLDialect, Name) +
812 <                ' Numeric(' + IntToStr(Precision) + ', 4)'; {do not localize}
813 <          end;
814 <          ftDate:
815 <            FieldList := FieldList +
816 <              QuoteIdentifier(DataBase.SQLDialect, Name) +
817 <              ' DATE'; {do not localize}
818 <          ftTime:
819 <            FieldList := FieldList +
820 <              QuoteIdentifier(DataBase.SQLDialect, Name) +
821 <              ' TIME'; {do not localize}
822 <          ftDateTime:
823 <            if (Database.SQLDialect = 1) then
824 <              FieldList := FieldList +
825 <              QuoteIdentifier(DataBase.SQLDialect, Name) +
826 <              ' DATE' {do not localize}
827 <            else
828 <              FieldList := FieldList +
829 <              QuoteIdentifier(DataBase.SQLDialect, Name) +
830 <              ' TIMESTAMP'; {do not localize}
831 <          ftLargeInt:
832 <            if (Database.SQLDialect = 1) then
833 <              IBError(ibxeFieldUnsupportedType,[nil])
834 <            else
835 <              FieldList := FieldList +
836 <                QuoteIdentifier(DataBase.SQLDialect, Name) +
837 <                ' Numeric(18, 0)'; {do not localize}
838 <          ftBlob, ftMemo:
839 <            FieldList := FieldList +
840 <              QuoteIdentifier(DataBase.SQLDialect, Name) +
841 <              ' BLOB SUB_TYPE 1'; {do not localize}
842 <          ftBytes, ftVarBytes, ftGraphic..ftTypedBinary:
843 <            FieldList := FieldList +
844 <              QuoteIdentifier(DataBase.SQLDialect, Name) +
845 <              ' BLOB SUB_TYPE 0'; {do not localize}
846 <          ftUnknown, ftADT, ftArray, ftReference, ftDataSet,
847 <          ftCursor, ftWideString, ftAutoInc:
848 <            IBError(ibxeFieldUnsupportedType,[nil]);
849 <          else
850 <            IBError(ibxeFieldUnsupportedType,[nil]);
851 <        end;
852 <        if faRequired in Attributes then
853 <          FieldList := FieldList + ' NOT NULL'; {do not localize}
854 <      end;
855 <    end;
856 <  end;
857 <
858 <  procedure InternalCreateTable;
859 <  var
860 <    I: Integer;
861 <    Query: TIBSQL;
862 <  begin
863 <    if (FieldList = '') then
864 <      IBError(ibxeFieldUnsupportedType,[nil]);
865 <    Query := TIBSQL.Create(self);
866 <    try
867 <      Query.Database := Database;
868 <      Query.transaction := Transaction;
869 <      Query.SQL.Text := 'Create Table ' +
870 <        QuoteIdentifier(DataBase.SQLDialect, FTableName) +
871 <        ' (' + FieldList; {do not localize}
872 <      for I := 0 to IndexDefs.Count - 1 do
873 <      with IndexDefs[I] do
874 <        if ixPrimary in Options then
875 <        begin
876 <          Query.SQL.Text := Query.SQL.Text + ', CONSTRAINT ' +
877 <            QuoteIdentifier(DataBase.SQLDialect, Name) +
878 <            ' Primary Key (' +
879 <            FormatFieldsList(Fields) +
880 <            ')';
881 <        end;
882 <      Query.SQL.Text := Query.SQL.Text + ')';
883 <      Query.Prepare;
884 <      Query.ExecQuery;
885 <    finally
886 <      Query.Free;
887 <    end;
888 <  end;
889 <
890 <  procedure InternalCreateIndex;
891 <  var
892 <    I: Integer;
893 <  begin
894 <    for I := 0 to IndexDefs.Count - 1 do
895 <    with IndexDefs[I] do
896 <      if not (ixPrimary in Options) then
897 <        AddIndex(Name, Fields, Options);
898 <  end;
899 <
900 < begin
901 <  CheckInactive;
902 <  InitFieldsList;
903 <  InternalCreateTable;
904 <  InternalCreateIndex;
905 < end;
906 <
907 < procedure TIBTable.DeleteTable;
908 < var
909 <  Query: TIBSQL;
910 < begin
911 <  CheckInactive;
912 <  Query := TIBSQL.Create(self);
913 <  try
914 <    Query.Database := DataBase;
915 <    Query.Transaction := Transaction;
916 <    Query.SQL.Text := 'drop table ' +  {do not localize}
917 <      QuoteIdentifier(DataBase.SQLDialect, FTableName);
918 <    Query.Prepare;
919 <    Query.ExecQuery;
920 <  finally
921 <    Query.Free;
922 <  end;
923 < end;
924 <
925 < procedure TIBTable.EmptyTable;
926 < var
927 <  Query: TIBSQL;
928 < begin
929 <  if Active then
930 <    CheckBrowseMode;
931 <  Query := TIBSQL.Create(self);
932 <  try
933 <    Query.Database := DataBase;
934 <    Query.Transaction := Transaction;
935 <    Query.SQL.Text := 'delete from ' + {do not localize}
936 <      QuoteIdentifier(DataBase.SQLDialect, FTableName);
937 <    Query.Prepare;
938 <    Query.ExecQuery;
939 <    if Active then
940 <    begin
941 <      ClearBuffers;
942 <      DataEvent(deDataSetChange, 0);
943 <    end;
944 <  finally
945 <    Query.Free;
946 <  end;
947 < end;
948 <
949 < procedure TIBTable.DataEvent(Event: TDataEvent; Info: Ptrint);
950 < begin
951 <  if Event = dePropertyChange then begin
952 <    IndexDefs.Updated := False;
953 <    FRegenerateSQL := True;
954 <  end;
955 <  inherited DataEvent(Event, Info);
956 < end;
957 <
958 < { Informational & Property }
959 <
960 < function TIBTable.GetCanModify: Boolean;
961 < begin
962 <  Result := True;
963 <  if (FTableName = '') or FReadOnly
964 <    or FSystemTable or FMultiTableView then
965 <    Result := False;
966 < end;
967 <
968 < function TIBTable.InternalGetUpdatable: Boolean;
969 < var
970 <  Query : TIBSQL;
971 < begin
972 <  Database.InternalTransaction.StartTransaction;
973 <  Query := TIBSQL.Create(self);
974 <  try
975 <    Query.Database := DataBase;
976 <    Query.Transaction := Database.InternalTransaction;
977 <    Query.SQL.Text := 'Select RDB$SYSTEM_FLAG, RDB$DBKEY_LENGTH ' + {do not localize}
978 <                    'from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize}
979 <                    '''' +
980 <                    FormatIdentifierValue(Database.SQLDialect,
981 <                      QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
982 <    Query.Prepare;
983 <    Query.ExecQuery;
984 <    if (Query.Current[0].AsInteger <> 0) or
985 <       (Query.Current[1].AsInteger <> 8) then
986 <      Result := False
987 <    else
988 <      Result := True;
989 <  finally
990 <    Query.Free;
991 <    Database.InternalTransaction.Commit;
992 <  end;
993 < end;
994 <
995 < function TIBTable.FieldDefsStored: Boolean;
996 < begin
997 <  Result := StoreDefs and (FieldDefs.Count > 0);
998 < end;
999 <
1000 < function TIBTable.IndexDefsStored: Boolean;
1001 < begin
1002 <  Result := StoreDefs and (IndexDefs.Count > 0);
1003 < end;
1004 <
1005 < procedure TIBTable.SetParams;
1006 < var
1007 <  i: Integer;
1008 < begin
1009 <  if (MasterSource = nil) or (MasterSource.DataSet = nil) or
1010 <  (not MasterSource.DataSet.Active) or (FMasterFieldsList.Count = 0) then
1011 <    exit;
1012 <  for i := 0 to FMasterFieldsList.Count - 1 do
1013 <    QSelect.Params.ByName(FMasterFieldsList.Strings[i]).Value :=
1014 <    MasterSource.DataSet.FieldByName(FMasterFieldsList.Strings[i]).Value;
1015 < end;
1016 <
1017 < procedure TIBTable.MasterChanged(Sender: TObject);
1018 < begin
1019 <  CheckBrowseMode;
1020 <  SetParams;
1021 <  ReQuery;
1022 < end;
1023 <
1024 < procedure TIBTable.MasterDisabled(Sender: TObject);
1025 < begin
1026 <  DataEvent(dePropertyChange, 0);
1027 <  ReQuery;
1028 < end;
1029 <
1030 < function TIBTable.GetDataSource: TDataSource;
1031 < begin
1032 <  Result := FMasterLink.DataSource;
1033 < end;
1034 <
1035 < procedure TIBTable.SetDataSource(Value: TDataSource);
1036 < begin
1037 <  if IsLinkedTo(Value) then IBError(ibxeCircularDataLink, [Self]);
1038 <  if FMasterLink.DataSource <> Value then
1039 <    DataEvent(dePropertyChange, 0);
1040 <  FMasterLink.DataSource := Value;
1041 < end;
1042 <
1043 < function TIBTable.GetMasterFields: string;
1044 < begin
1045 <  Result := FMasterLink.FieldNames;
1046 < end;
1047 <
1048 < procedure TIBTable.SetMasterFields(const Value: string);
1049 < begin
1050 <  if FMasterLink.FieldNames <> Value then
1051 <    DataEvent(dePropertyChange, 0);
1052 <  FMasterLink.FieldNames := Value;
1053 < end;
1054 <
1055 < procedure TIBTable.DoOnNewRecord;
1056 < var
1057 <  I: Integer;
1058 < begin
1059 <  if FMasterLink.Active and (FMasterLink.Fields.Count > 0) then
1060 <    for I := 0 to FMasterLink.Fields.Count - 1 do
1061 <      IndexFields[I] := TField(FMasterLink.Fields[I]);
1062 <  inherited DoOnNewRecord;
1063 < end;
1064 <
1065 < function TIBTable.FormatFieldsList(Value: String): String;
1066 < var
1067 <  FieldName: string;
1068 <  i: Integer;
1069 < begin
1070 <  if Database.SQLDialect = 1 then begin
1071 <    Value := QuoteIdentifier(Database.SQLDialect, Value);
1072 <    Result := StringReplace (Value, ';', ', ', [rfReplaceAll]);
1073 <  end
1074 <  else begin
1075 <    i := 1;
1076 <    Result := '';
1077 <    while i <= Length(Value) do
1078 <    begin
1079 <      FieldName := ExtractFieldName(Value, i);
1080 <      if Result = '' then
1081 <        Result := QuoteIdentifier(Database.SQLDialect, FieldName)
1082 <      else
1083 <        Result := Result + ', ' + QuoteIdentifier(Database.SQLDialect, FieldName);
1084 <    end;
1085 <  end;
1086 < end;
1087 <
1088 < procedure TIBTable.ExtractLinkFields;
1089 < var
1090 <  i: Integer;
1091 <  DetailFieldNames: String;
1092 < begin
1093 <  FMasterFieldsList.Clear;
1094 <  FDetailFieldsList.Clear;
1095 <  i := 1;
1096 <  while i <= Length(MasterFields) do
1097 <    FMasterFieldsList.Add(ExtractFieldName(MasterFields, i));
1098 <  i := 1;
1099 <  if IndexFieldNames = '' then
1100 <    DetailFieldNames := FPrimaryIndexFields
1101 <  else
1102 <    DetailFieldNames := IndexFieldNames;
1103 <  while i <= Length(DetailFieldNames) do
1104 <    FDetailFieldsList.Add(ExtractFieldName(DetailFieldNames, i));
1105 < end;
1106 <
1107 < procedure TIBTable.GetDetailLinkFields(MasterFields, DetailFields: TList);
1108 < var
1109 <  i: Integer;
1110 <  Idx: TIndexDef;
1111 < begin
1112 <  MasterFields.Clear;
1113 <  DetailFields.Clear;
1114 <  if (MasterSource <> nil) and (MasterSource.DataSet <> nil) and
1115 <     (Self.MasterFields <> '') then
1116 <  begin
1117 <    Idx := nil;
1118 <    MasterSource.DataSet.GetFieldList(MasterFields, Self.MasterFields);
1119 <    UpdateIndexDefs;
1120 <    if IndexName <> '' then
1121 <      Idx := IndexDefs.Find(IndexName)
1122 <    else if IndexFieldNames <> '' then
1123 <      Idx := IndexDefs.GetIndexForFields(IndexFieldNames, False)
1124 <    else
1125 <      for i := 0 to IndexDefs.Count - 1 do
1126 <        if ixPrimary in IndexDefs[i].Options then
1127 <        begin
1128 <          Idx := IndexDefs[i];
1129 <          break;
1130 <        end;
1131 <    if Idx <> nil then
1132 <      GetFieldList(DetailFields, Idx.Fields);
1133 <  end;
1134 < end;
1135 <
1136 < procedure TIBTable.SetReadOnly(Value: Boolean);
1137 < begin
1138 <  CheckInactive;
1139 <  FReadOnly := Value;
1140 < end;
1141 <
1142 < procedure TIBTable.SetTableName(Value: String);
1143 < begin
1144 <  if not (csReading in ComponentState) then
1145 <  begin
1146 <    CheckInactive;
1147 <    if Value <> FTableName then
1148 <    begin
1149 <      ResetSQLStatements;
1150 <      FRegenerateSQL := True;
1151 <      FTableName := Value;
1152 <      IndexName := '';
1153 <      IndexFieldNames := '';
1154 <      FPrimaryIndexFields := '';
1155 <      DataEvent(dePropertyChange, 0);
1156 <    end;
1157 <  end
1158 <  else if Value <> FTableName then
1159 <    FTableName := Value;
1160 < end;
1161 <
1162 < function TIBTable.GetIndexField(Index: Integer): TField;
1163 < var
1164 <  I, Count: Integer;
1165 <  FieldNames, FieldName: String;
1166 < begin
1167 <  Result := nil;
1168 <  FieldName := '';
1169 <  FieldNames := IndexFieldNames;
1170 <  if FieldNames = '' then
1171 <  begin
1172 <    for I := 0 to IndexDefs.Count - 1 do
1173 <      if (IndexDefs[i].Name = FIndexName) then
1174 <      begin
1175 <        FieldNames := IndexDefs[i].Fields;
1176 <        break;
1177 <      end;
1178 <  end;
1179 <  for I := 0 to Index do
1180 <  begin
1181 <    Count := Pos(';', FieldNames); {mbcs OK}
1182 <    if Count = 0 then
1183 <      FieldName := FieldNames
1184 <    else begin
1185 <      FieldName := Copy(FieldNames, 0, Count - 1);
1186 <      System.Delete(FieldNames, 1, Count);
1187 <    end;
1188 <  end;
1189 <  if FieldName <> '' then
1190 <    Result := FieldByName(FieldName)
1191 <  else
1192 <    IBError(ibxeIndexFieldMissing, [nil]);
1193 < end;
1194 <
1195 <
1196 < procedure TIBTable.SetIndexField(Index: Integer; Value: TField);
1197 < begin
1198 <  GetIndexField(Index).Assign(Value);
1199 < end;
1200 <
1201 < function TIBTable.GetIndexFieldCount: Integer;
1202 < var
1203 <  I, Index: Integer;
1204 <  FieldNames: String;
1205 <  done: Boolean;
1206 < begin
1207 <  FieldNames := IndexFieldNames;
1208 <  if FieldNames = '' then
1209 <  begin
1210 <    for I := 0 to IndexDefs.Count - 1 do
1211 <      if (IndexDefs[i].Name = FIndexName) then
1212 <      begin
1213 <        FieldNames := IndexDefs[i].Fields;
1214 <        break;
1215 <      end;
1216 <  end;
1217 <  if FieldNames = '' then
1218 <    Result := 0
1219 <  else
1220 <  begin
1221 <    done := False;
1222 <    Result := 1;
1223 <    while not done do
1224 <    begin
1225 <      Index := Pos(';', FieldNames); {mbcs ok}
1226 <      if Index <> 0 then
1227 <      begin
1228 <        System.Delete(FieldNames, 1, Index);
1229 <        Inc(Result);
1230 <      end else
1231 <        done := True;
1232 <    end;
1233 <  end;
1234 < end;
1235 <
1236 < function TIBTable.GetTableNames: TStrings;
1237 < begin
1238 <  FNameList.clear;
1239 <  GetTableNamesFromServer;
1240 <  Result := FNameList;
1241 < end;
1242 <
1243 < procedure TIBTable.GetTableNamesFromServer;
1244 < var
1245 <  Query : TIBSQL;
1246 < begin
1247 <  if not (csReading in ComponentState) then begin
1248 <    ActivateConnection;
1249 <    Database.InternalTransaction.StartTransaction;
1250 <    Query := TIBSQL.Create(self);
1251 <    try
1252 <      Query.GoToFirstRecordOnExecute := False;
1253 <      Query.Database := DataBase;
1254 <      Query.Transaction := Database.InternalTransaction;
1255 <      if (TableTypes * [ttSystem, ttView] = [ttSystem, ttView]) then
1256 <        Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' {do not localize}
1257 <      else if ttSystem in TableTypes then
1258 <        Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' + {do not localize}
1259 <                          ' where RDB$VIEW_BLR is NULL' {do not localize}
1260 <      else if ttView in TableTypes then
1261 <        Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' + {do not localize}
1262 <                          ' where RDB$SYSTEM_FLAG = 0' {do not localize}
1263 <      else
1264 <        Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' + {do not localize}
1265 <                          ' where RDB$VIEW_BLR is NULL and RDB$SYSTEM_FLAG = 0'; {do not localize}
1266 <      Query.Prepare;
1267 <      Query.ExecQuery;
1268 <      while (not Query.EOF) and (Query.Next <> nil) do
1269 <        FNameList.Add (TrimRight(Query.Current[0].AsString));
1270 <    finally
1271 <      Query.Free;
1272 <      Database.InternalTransaction.Commit;
1273 <    end;
1274 <  end;
1275 < end;
1276 <
1277 < procedure TIBTable.SwitchToIndex();
1278 < begin
1279 <  FSwitchingIndex := True;
1280 <  InternalTableRefresh;
1281 <  FSwitchingIndex := False;
1282 < end;
1283 <
1284 < procedure TIBTable.InternalTableRefresh();
1285 < var
1286 <  DBKey: TIBDBKey;
1287 < begin
1288 <  CheckActive;
1289 <  DBKey := CurrentDBKey;
1290 <  FRegenerateSQL := True;
1291 <  Reopen;
1292 <  if DBKey.DBKey[0] <> 0 then
1293 <    InternalGotoDBKey(DBKey);
1294 < end;
1295 <
1296 < procedure TIBTable.GenerateSQL;
1297 < var
1298 <  i: Integer;
1299 <  SQL: TStrings;
1300 <  OrderByStr: string;
1301 <  bWhereClausePresent: Boolean;
1302 < begin
1303 <  bWhereClausePresent := False;
1304 <  Database.CheckActive;
1305 <  Transaction.CheckInTransaction;
1306 <  if IndexDefs.Updated = False then
1307 <    IndexDefs.Update;
1308 <  if IndexFieldNames <> '' then
1309 <    OrderByStr := FormatFieldsList(IndexFieldNames)
1310 <  else if IndexName <> '' then
1311 <    OrderByStr := FormatFieldsList(IndexDefs[IndexDefs.Indexof (IndexName)].Fields)
1312 <  else if FDefaultIndex and (FPrimaryIndexFields <> '') then
1313 <    OrderByStr := FormatFieldsList(FPrimaryIndexFields);
1314 <  SQL := TStringList.Create;
1315 <  SQL.Text := 'select ' + {do not localize}
1316 <    QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1317 <    + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1318 <    + QuoteIdentifier(DataBase.SQLDialect, FTableName);
1319 <  if Filtered and (Filter <> '') then
1320 <  begin
1321 <    SQL.Text := SQL.Text + ' where ' + Filter; {do not localize}
1322 <    bWhereClausePresent := True;
1323 <  end;
1324 <  if (MasterSource <> nil) and (MasterSource.DataSet <> nil) and (MasterFields <> '') then
1325 <  begin
1326 <    if bWhereClausePresent then
1327 <      SQL.Text := SQL.Text + ' AND ' {do not localize}
1328 <    else
1329 <      SQL.Text := SQL.Text + ' WHERE '; {do not localize}
1330 <    ExtractLinkfields;
1331 <    if FDetailFieldsList.Count < FMasterFieldsList.Count then
1332 <      IBError(ibxeUnknownError, [nil]);
1333 <    for i := 0 to FMasterFieldsList.Count - 1 do
1334 <    begin
1335 <      if i > 0 then
1336 <        SQL.Text := SQL.Text + 'AND ';
1337 <      SQL.Text := SQL.Text +
1338 <        QuoteIdentifier(DataBase.SQLDialect, FDetailFieldsList.Strings[i]) +
1339 <        ' = :' +
1340 <        QuoteIdentifier(DataBase.SQLDialect, FMasterFieldsList.Strings[i]);
1341 <    end;
1342 <  end;
1343 <  if OrderByStr <> '' then
1344 <    SQL.Text := SQL.Text + ' order by ' + OrderByStr; {do not localize}
1345 <  SelectSQL.Assign(SQL);
1346 <  RefreshSQL.Text := 'select ' + {do not localize}
1347 <    QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1348 <    + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1349 <    + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1350 <    ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize}
1351 <  WhereDBKeyRefreshSQL.Assign(RefreshSQL);
1352 <  InternalPrepare;
1353 <  SQL.Free;
1354 < end;
1355 <
1356 < procedure TIBTable.GenerateUpdateSQL;
1357 < var
1358 <  InsertFieldList, InsertParamList, UpdateFieldList: string;
1359 <  WherePrimaryFieldList, WhereAllFieldList: string;
1360 <
1361 <  procedure GenerateFieldLists;
1362 <  var
1363 <    I: Integer;
1364 <  begin
1365 <    for I := 0 to FieldDefs.Count - 1 do begin
1366 <      with FieldDefs[I] do begin
1367 <        if not (InternalCalcField or (faReadOnly in Attributes) or
1368 <          (DataType = ftUnknown)) then
1369 <        begin
1370 <          if ( InsertFieldList <> '' ) then begin
1371 <            InsertFieldList := InsertFieldList + ', ';
1372 <            InsertParamList := InsertParamList + ', ';
1373 <            UpdateFieldList := UpdateFieldList + ', ';
1374 <            if (DataType <> ftBlob) and (DataType <>ftMemo) then
1375 <              WhereAllFieldList := WhereAllFieldList + ' AND ';
1376 <          end;
1377 <          InsertFieldList := InsertFieldList +
1378 <            QuoteIdentifier(DataBase.SQLDialect, Name);
1379 <          InsertParamList := InsertParamList + ':' +
1380 <            QuoteIdentifier(DataBase.SQLDialect, Name);
1381 <          UpdateFieldList := UpdateFieldList +
1382 <            QuoteIdentifier(DataBase.SQLDialect, Name) +
1383 <            ' = :' +
1384 <            QuoteIdentifier(DataBase.SQLDialect, Name);
1385 <          if (DataType <> ftBlob) and (DataType <>ftMemo) then
1386 <            WhereAllFieldList := WhereAllFieldList +
1387 <              QuoteIdentifier(DataBase.SQLDialect, Name) + ' = :' +
1388 <              QuoteIdentifier(DataBase.SQLDialect, Name);{do not localize}
1389 <        end;
1390 <      end;
1391 <    end;
1392 <  end;
1393 <
1394 <  procedure GenerateWherePrimaryFieldList;
1395 <  var
1396 <    i: Integer;
1397 <    tmp: String;
1398 <  begin
1399 <    i := 1;
1400 <    while i <= Length(FPrimaryIndexFields) do
1401 <    begin
1402 <      tmp := ExtractFieldName(FPrimaryIndexFields, i);
1403 <      tmp :=
1404 <        QuoteIdentifier(DataBase.SQLDialect, tmp) +  ' = :' +
1405 <        QuoteIdentifier(DataBase.SQLDialect, tmp);{do not localize}
1406 <      if WherePrimaryFieldList <> '' then
1407 <        WherePrimaryFieldList :=
1408 <          WherePrimaryFieldList + ' AND ' + tmp
1409 <      else
1410 <        WherePrimaryFieldList := tmp;
1411 <    end;
1412 <  end;
1413 <
1414 < begin
1415 <  if InternalGetUpdatable = False  then
1416 <    FReadOnly := True
1417 <  else
1418 <  begin
1419 <    DeleteSQL.Text := 'delete from ' + {do not localize}
1420 <      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1421 <      ' where RDB$DB_KEY = ' + ':IBX_INTERNAL_DBKEY'; {do not localize}
1422 <    GenerateFieldLists;
1423 <    InsertSQL.Text := 'insert into ' + {do not localize}
1424 <      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1425 <    ' (' + InsertFieldList + {do not localize}
1426 <      ') values (' + InsertParamList + ')'; {do not localize}
1427 <    ModifySQL.Text := 'update ' +
1428 <      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1429 <      ' set ' + UpdateFieldList + {do not localize}
1430 <      ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize}
1431 <    WhereAllRefreshSQL.Text := 'select ' +  {do not localize}
1432 <      QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, '
1433 <      + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1434 <      + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1435 <      ' where ' + WhereAllFieldList; {do not localize}
1436 <    if FPrimaryIndexFields <> '' then
1437 <    begin
1438 <      GenerateWherePrimaryFieldList;
1439 <      WherePrimaryRefreshSQL.Text := 'select ' + {do not localize}
1440 <        QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1441 <        + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1442 <        + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1443 <        ' where ' + WherePrimaryFieldList; {do not localize}
1444 <    end;
1445 <    try
1446 <      InternalPrepare;
1447 <    except
1448 <      FReadonly := True;
1449 <    end;
1450 <  end;
1451 < end;
1452 <
1453 < procedure TIBTable.ResetSQLStatements;
1454 < begin
1455 <  SelectSQL.Text := '';
1456 <  DeleteSQL.Text := '';
1457 <  InsertSQL.Text := '';
1458 <  ModifySQL.Text := '';
1459 <  RefreshSQL.Text := '';
1460 < end;
1461 <
1462 < procedure TIBTable.SetTableTypes(
1463 <  const Value: TIBTableTypes);
1464 < begin
1465 <  FTableTypes := Value;
1466 < end;
1467 <
1468 < function TIBTable.InternalGotoDBKey(DBKey: TIBDBKey): Boolean;
1469 <
1470 <  function DBKeyCompare (DBKey1, DBKey2: TIBDBKey): Boolean;
1471 <  var
1472 <  I: Integer;
1473 <  begin
1474 <    for I := 0 to 7 do
1475 <      if (DBKey1.DBKey[i] <> DBKey2.DBKey[i]) then begin
1476 <        result := False;
1477 <        exit;
1478 <      end;
1479 <    result := True;
1480 <  end;
1481 < begin
1482 <  CheckActive;
1483 <  DisableControls;
1484 < try
1485 <    result := False;
1486 <    First;
1487 <    while ((not result) and (not EOF)) do begin
1488 <      if (DBKeyCompare (DBKey, PRecordData(GetActiveBuf)^.rdDBKey)) then
1489 <        result := True
1490 <      else
1491 <        Next;
1492 <    end;
1493 <    if not result then
1494 <      First
1495 <    else
1496 <      CursorPosChanged;
1497 <  finally
1498 <    EnableControls;
1499 <  end;
1500 < end;
1501 <
1502 < function TIBTable.GetCurrentDBKey: TIBDBKey;
1503 < var
1504 <  Buf: pChar;
1505 < begin
1506 <  CheckActive;
1507 <  buf := GetActiveBuf;
1508 <  if Buf <> nil then
1509 <    Result := PRecordData(Buf)^.rdDBKey
1510 <  else
1511 <    Result.DBKey[0] := 0;
1512 < end;
1513 <
1514 < procedure TIBTable.Reopen;
1515 < begin
1516 <  DisableControls;
1517 <  try
1518 <    if Active then
1519 <    begin
1520 <      SetState(dsInactive);
1521 <      CloseCursor;
1522 <      OpenCursor(false);
1523 <      SetState(dsBrowse);
1524 <    end;
1525 <  finally
1526 <    EnableControls;
1527 <  end;
1528 < end;
1529 <
1530 < end.
1 > {************************************************************************}
2 > {                                                                        }
3 > {       Borland Delphi Visual Component Library                          }
4 > {       InterBase Express core components                                }
5 > {                                                                        }
6 > {       Copyright (c) 1998-2000 Inprise Corporation                      }
7 > {                                                                        }
8 > {    InterBase Express is based in part on the product                   }
9 > {    Free IB Components, written by Gregory H. Deatz for                 }
10 > {    Hoagland, Longo, Moran, Dunst & Doukas Company.                     }
11 > {    Free IB Components is used under license.                           }
12 > {                                                                        }
13 > {    The contents of this file are subject to the InterBase              }
14 > {    Public License Version 1.0 (the "License"); you may not             }
15 > {    use this file except in compliance with the License. You            }
16 > {    may obtain a copy of the License at http://www.Inprise.com/IPL.html }
17 > {    Software distributed under the License is distributed on            }
18 > {    an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either              }
19 > {    express or implied. See the License for the specific language       }
20 > {    governing rights and limitations under the License.                 }
21 > {    The Original Code was created by InterBase Software Corporation     }
22 > {       and its successors.                                              }
23 > {    Portions created by Inprise Corporation are Copyright (C) Inprise   }
24 > {       Corporation. All Rights Reserved.                                }
25 > {    Contributor(s): Jeff Overcash                                       }
26 > {                                                                        }
27 > {    IBX For Lazarus (Firebird Express)                                  }
28 > {    Contributor: Tony Whyman, MWA Software http://www.mwasoftware.co.uk }
29 > {    Portions created by MWA Software are copyright McCallum Whyman      }
30 > {    Associates Ltd 2011                                                 }
31 > {                                                                        }
32 > {************************************************************************}
33 >
34 > unit IBTable;
35 >
36 > {$Mode Delphi}
37 >
38 > {$codepage UTF8}
39 >
40 > interface
41 >
42 > uses SysUtils, Classes, DB, IB,  IBCustomDataSet,
43 >     IBSQL, IBUtils;
44 >    
45 > type
46 >
47 > { TIBTable }
48 >
49 >  TIBTableType = (ttSystem, ttView);
50 >  TIBTableTypes = set of TIBTableType;
51 >  TIndexName = String;
52 >
53 >  TIBTable = class;
54 >
55 >  TIBTable = class(TIBCustomDataSet)
56 >  private
57 >    FSystemTable: Boolean;
58 >    FMultiTableView: Boolean;
59 >    FMasterLink: TMasterDataLink;
60 >    FMasterFieldsList: TStringList;
61 >    FDetailFieldsList: TStringList;
62 >    FStoreDefs: Boolean;
63 >    FIndexDefs: TIndexDefs;
64 >    FDefaultIndex: Boolean;
65 >    FReadOnly: Boolean;
66 >    FFieldsIndex: Boolean;
67 >    FTableName: String;
68 >    FIndexName: TIndexName;
69 >    FRegenerateSQL: Boolean;
70 >    FNameList: TStrings;
71 >    FSwitchingIndex: Boolean;
72 >    FPrimaryIndexFields: string;
73 >    FTableTypes: TIBTableTypes;
74 >    WhereAllRefreshSQL: TStrings;
75 >    WhereDBKeyRefreshSQL: TStrings;
76 >    WherePrimaryRefreshSQL: TStrings;
77 >
78 >    function GetIndexFieldCount: Integer;
79 >    function GetIndexField(Index: Integer): TField;
80 >    procedure MasterChanged(Sender: TObject);
81 >    procedure MasterDisabled(Sender: TObject);
82 >    procedure SetDataSource(Value: TDataSource);
83 >    procedure SetIndexField(Index: Integer; Value: TField);
84 >    procedure SetIndexFieldNames(const Value: string);
85 >    procedure GenerateSQL;
86 >    procedure GenerateUpdateSQL;
87 >    procedure SwitchToIndex();
88 >    procedure InternalTableRefresh();
89 >    function GetTableNames: TStrings;
90 >    procedure GetTableNamesFromServer;
91 >    procedure SetTableTypes(
92 >    const Value: TIBTableTypes);
93 >    function InternalGotoDBKey(DBKey: TIBDBKey): Boolean;
94 >    function FormatFieldsList(Value: string): string;
95 >    function GetCurrentDBKey: TIBDBKey;
96 >    function InternalGetUpdatable: Boolean;
97 >    function GetExists: Boolean;
98 >    procedure SetIndexDefs(Value: TIndexDefs);
99 >    procedure ExtractLinkFields;
100 >    function FieldDefsStored: Boolean;
101 >    function IndexDefsStored: Boolean;
102 >    function GetMasterFields: string;
103 >    procedure SetMasterFields(const Value: string);
104 >    function GetIndexFieldNames: string;
105 >    function GetIndexName: string;
106 >    procedure SetIndexName(const Value: string);
107 >    procedure SetParams;
108 >    procedure SetReadOnly(Value: Boolean);
109 >    procedure SetTableName(Value: String);
110 >    procedure SetIndex(const Value: string; FieldsIndex: Boolean);
111 >    procedure ResetSQLStatements;
112 >    procedure Reopen;
113 >
114 >  protected
115 >
116 >    procedure DoOnNewRecord; override;
117 >    procedure GetIndexParams(const IndexName: string; FieldsIndex: Boolean;
118 >      var IndexedName: string);
119 >    function GetCanModify: Boolean; override;
120 >    procedure UpdateIndexDefs; override;
121 >    procedure DataEvent(Event: TDataEvent; Info: Ptrint); override;
122 >    procedure DefChanged(Sender: TObject); virtual;
123 >    function GetDataSource: TDataSource; override;
124 >    procedure InitFieldDefs; override;
125 >    procedure InternalClose; override;
126 >    procedure InternalOpen; override;
127 >    procedure InternalRefresh; override;
128 >    procedure SetFiltered(Value: Boolean); override;
129 >    procedure SetFilterText(const Value: string); override;
130 >    procedure SetFilterOptions(Value: TFilterOptions); override;
131 >    procedure InternalRefreshRow; override;
132 >
133 >  public
134 >    constructor Create(AOwner: TComponent); override;
135 >    destructor Destroy; override;
136 >    procedure AddIndex(const Name, Fields: string; Options: TIndexOptions;
137 >      const DescFields: string = '');
138 >    procedure CreateTable;
139 >    procedure DeleteIndex(const Name: string);
140 >    procedure DeleteTable;
141 >    procedure EmptyTable;
142 >    procedure GetDetailLinkFields(MasterFields, DetailFields: TList); virtual;
143 >    procedure GetIndexNames(List: TStrings);
144 >    procedure GotoCurrent(Table: TIBTable);
145 >    property CurrentDBKey: TIBDBKey read GetCurrentDBKey;
146 >    property Exists: Boolean read GetExists;
147 >    property IndexFieldCount: Integer read GetIndexFieldCount;
148 >    property IndexFields[Index: Integer]: TField read GetIndexField write SetIndexField;
149 >    property TableNames: TStrings read GetTableNames;
150 >
151 >  published
152 >    property AutoCommit;
153 >    property Active;
154 >    property BufferChunks;
155 >    property CachedUpdates;
156 >    property DataSetCloseAction;
157 > //    property Constraints stored ConstraintsStored;
158 >    property DefaultIndex: Boolean read FDefaultIndex write FDefaultIndex default True;
159 >    property FieldDefs stored FieldDefsStored;
160 >    property Filter;
161 >    property Filtered;
162 >    property GeneratorField;
163 >    property IndexDefs: TIndexDefs read FIndexDefs write SetIndexDefs stored IndexDefsStored;
164 >    property IndexFieldNames: string read GetIndexFieldNames write SetIndexFieldNames;
165 >    property IndexName: string read GetIndexName write SetIndexName;
166 >    property MasterFields: string read GetMasterFields write SetMasterFields;
167 >    property MasterSource: TDataSource read GetDataSource write SetDataSource;
168 >    property ReadOnly: Boolean read FReadOnly write SetReadOnly default False;
169 >    property StoreDefs: Boolean read FStoreDefs write FStoreDefs default False;
170 >    property TableName: String read FTableName write SetTableName;
171 >    property TableTypes: TIBTableTypes read FTableTypes write SetTableTypes default [];
172 >    property UpdateObject;
173 >    property UniDirectional;
174 >
175 >    property BeforeDatabaseDisconnect;
176 >    property AfterDatabaseDisconnect;
177 >    property DatabaseFree;
178 >    property BeforeTransactionEnd;
179 >    property AfterTransactionEnd;
180 >    property TransactionFree;
181 >    property OnFilterRecord;
182 >  end;
183 >
184 > implementation
185 >
186 > uses FBMessages;
187 >
188 > { TIBTable }
189 >
190 > constructor TIBTable.Create(AOwner: TComponent);
191 > begin
192 >  inherited Create(AOwner);
193 >  FNameList := TStringList.Create;
194 >  FSwitchingIndex := False;
195 >  FIndexDefs := TIndexDefs.Create(Self);
196 >  WhereAllRefreshSQL := TStringList.Create;
197 >  WhereDBKeyRefreshSQL := TStringList.Create;
198 >  WherePrimaryRefreshSQL := TStringList.Create;
199 >  FDefaultIndex := True;
200 >  FRegenerateSQL := True;
201 >  FMasterFieldsList := TStringList.Create;
202 >  FDetailFieldsList := TStringList.Create;
203 >  FMasterLink := TMasterDataLink.Create(Self);
204 >  FMasterLink.OnMasterChange := MasterChanged;
205 >  FMasterLink.OnMasterDisable := MasterDisabled;
206 >  QRefresh.OnSQLChanging := nil;
207 >  QDelete.OnSQLChanging := nil;
208 >  QInsert.OnSQLChanging := nil;
209 >  QModify.OnSQLChanging := nil;
210 > end;
211 >
212 > destructor TIBTable.Destroy;
213 > begin
214 >  FNameList.Free;
215 >  FIndexDefs.Free;
216 >  FMasterFieldsList.Free;
217 >  FDetailFieldsList.Free;
218 >  FMasterLink.Free;
219 >  WhereAllRefreshSQL.Free;
220 >  WhereDBKeyRefreshSQL.Free;
221 >  WherePrimaryRefreshSQL.Free;
222 >  inherited Destroy;
223 > end;
224 >
225 > procedure TIBTable.InternalClose;
226 > begin
227 >  DataEvent(dePropertyChange, 0);
228 >  inherited InternalClose;
229 > end;
230 >
231 > procedure TIBTable.InternalOpen;
232 > begin
233 >  if FTableName = '' then IBError(ibxeNoTableName, [nil]);
234 >  ActivateConnection;
235 >  ActivateTransaction;
236 >  if FRegenerateSQL then
237 >  begin
238 >    InternalUnprepare;
239 >    GenerateSQL;
240 >    if not FReadOnly then
241 >      GenerateUpdateSQL;
242 >    FRegenerateSQL := False;
243 >  end;
244 > {  writeln(SelectSQL.Text);
245 >  writeln(InsertSQL.Text);
246 >  writeln(DeleteSQL.Text);
247 >  writeln(ModifySQL.Text); }
248 >  SetParams;
249 >  inherited InternalOpen;
250 > end;
251 >
252 > procedure TIBTable.InternalRefresh;
253 > var
254 >  DBKey: TIBDBKey;
255 > begin
256 >  DBKey := CurrentDBKey;
257 >  Reopen;
258 >  if DBKey.DBKey[0] <> 0 then
259 >    InternalGotoDBKey(DBKey);
260 > end;
261 >
262 > procedure TIBTable.SetFiltered(Value: Boolean);
263 > begin
264 >  if(Filtered <> Value) then
265 >  begin
266 >    inherited SetFiltered(value);
267 >    if Active then
268 >      InternalTableRefresh;
269 >  end
270 >  else
271 >    inherited SetFiltered(value);
272 > end;
273 >
274 > procedure TIBTable.SetFilterText(const Value: string);
275 > begin
276 >  if Filtered and (Value <> Filter) then
277 >  begin
278 >    inherited SetFilterText(value);
279 >    InternalTableRefresh;
280 >  end
281 >  else
282 >    inherited SetFilterText(value);
283 > end;
284 >
285 > procedure TIBTable.SetFilterOptions(Value: TFilterOptions);
286 > begin
287 >  if Value <> [] then
288 >    IBError(ibxeNotSupported, [nil]);
289 > end;
290 >
291 > procedure TIBTable.InternalRefreshRow;
292 > begin
293 >  if CurrentDBKey.DBKey[0] <> 0 then
294 >    QRefresh.SQL.Assign(WhereDBKeyRefreshSQL)
295 >  else if WherePrimaryRefreshSQL.Text <> '' then
296 >    QRefresh.SQL.Assign(WherePrimaryRefreshSQL)
297 >  else
298 >    QRefresh.SQL.Assign(WhereAllRefreshSQL);
299 >  inherited InternalRefreshRow;
300 > end;
301 >
302 > procedure TIBTable.DefChanged(Sender: TObject);
303 > begin
304 >  StoreDefs := True;
305 > end;
306 >
307 > procedure TIBTable.InitFieldDefs;
308 > var
309 >  DidActivate: Boolean;
310 >  Query: TIBSQL;
311 > begin
312 >  if FTableName = '' then IBError(ibxeNoTableName, [nil]);
313 >  if (InternalPrepared) then
314 >     InternalInitFieldDefs
315 >  else
316 >  begin
317 >    {Get the field defs from a simple query on the table}
318 >    ActivateConnection;
319 >    Query := TIBSQL.Create(self);
320 >    try
321 >         Query.Database := DataBase;
322 >         Query.Transaction := Database.InternalTransaction;
323 >         DidActivate := false;
324 >         Query.SQL.Text := 'Select * from ' + QuoteIdentifier(DataBase.SQLDialect, FTableName);
325 >         with Query.Transaction do
326 >         begin
327 >              if not InTransaction then
328 >              begin
329 >                StartTransaction;
330 >                DidActivate := true
331 >              end;
332 >         end;
333 >         Query.Prepare;
334 >         FieldDefsFromQuery(Query);
335 >         if DidActivate then
336 >            Query.Transaction.Rollback;
337 >    finally
338 >         Query.Free;
339 >    end;
340 >  end;
341 > end;
342 >
343 > { Index / Ranges / Keys }
344 >
345 > procedure TIBTable.AddIndex(const Name, Fields: string; Options: TIndexOptions;
346 >  const DescFields: string);
347 > var
348 >  Query: TIBSQL;
349 >  FieldList: string;
350 > begin
351 >  FieldDefs.Update;
352 >  if Active then begin
353 >    CheckBrowseMode;
354 >    CursorPosChanged;
355 >  end;
356 >  Query := TIBSQL.Create(self);
357 >  try
358 >    Query.Database := DataBase;
359 >    Query.Transaction := Transaction;
360 >    FieldList := FormatFieldsList(Fields);
361 >    if (ixPrimary in Options) then
362 >    begin
363 >     Query.SQL.Text := 'Alter Table ' + {do not localize}
364 >       QuoteIdentifier(Database.SQLDialect, FTableName) +
365 >       ' Add CONSTRAINT ' +   {do not localize}
366 >       QuoteIdentifier(Database.SQLDialect, Name)
367 >       + ' Primary Key (' + {do not localize}
368 >       FormatFieldsList(Fields) +
369 >       ')';
370 >    end
371 >    else if ([ixUnique, ixDescending] * Options = [ixUnique, ixDescending]) then
372 >      Query.SQL.Text := 'Create unique Descending Index ' + {do not localize}
373 >                        QuoteIdentifier(Database.SQLDialect, Name) +
374 >                        ' on ' + {do not localize}
375 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
376 >                        ' (' + FieldList + ')'
377 >    else if (ixUnique in Options) then
378 >      Query.SQL.Text := 'Create unique Index ' + {do not localize}
379 >                        QuoteIdentifier(Database.SQLDialect, Name) +
380 >                        ' on ' + {do not localize}
381 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
382 >                        ' (' + FieldList + ')'
383 >    else if (ixDescending in Options) then
384 >      Query.SQL.Text := 'Create Descending Index ' + {do not localize}
385 >                        QuoteIdentifier(Database.SQLDialect, Name) +
386 >                        ' on ' + {do not localize}
387 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
388 >                        ' (' + FieldList + ')'
389 >    else
390 >      Query.SQL.Text := 'Create Index ' + {do not localize}
391 >                        QuoteIdentifier(Database.SQLDialect, Name) +
392 >                        ' on ' + {do not localize}
393 >                        QuoteIdentifier(Database.SQLDialect, FTableName) +
394 >                        ' (' + FieldList + ')';
395 >    Query.Prepare;
396 >    Query.ExecQuery;
397 >    IndexDefs.Updated := False;
398 >  finally
399 >    Query.free
400 >  end;
401 > end;
402 >
403 > procedure TIBTable.DeleteIndex(const Name: string);
404 > var
405 >  Query: TIBSQL;
406 >
407 >  procedure DeleteByIndex;
408 >  begin
409 >    Query := TIBSQL.Create(self);
410 >    try
411 >      Query.Database := DataBase;
412 >      Query.Transaction := Transaction;
413 >      Query.SQL.Text := 'Drop index ' +  {do not localize}
414 >                         QuoteIdentifier(Database.SQLDialect, Name);
415 >      Query.Prepare;
416 >      Query.ExecQuery;
417 >      IndexDefs.Updated := False;
418 >    finally
419 >      Query.Free;
420 >    end;
421 >  end;
422 >
423 >  function DeleteByConstraint: Boolean;
424 >  begin
425 >    Result := False;
426 >    Query := TIBSQL.Create(self);
427 >    try
428 >      Query.Database := DataBase;
429 >      Query.Transaction := Transaction;
430 >      Query.SQL.Text := 'Select ''foo'' from RDB$RELATION_CONSTRAINTS ' +
431 >        'where RDB$RELATION_NAME = ' +
432 >        '''' +
433 >        ExtractIdentifier(Database.SQLDialect,
434 >          QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
435 >        ''' ' +
436 >        ' AND RDB$CONSTRAINT_NAME = ' +
437 >        '''' +
438 >        ExtractIdentifier(Database.SQLDialect,
439 >          QuoteIdentifier(DataBase.SQLDialect, Name)) +
440 >        ''' ' +
441 >        'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''';
442 >      Query.Prepare;
443 >      Query.ExecQuery;
444 >      if not Query.EOF then
445 >      begin
446 >        Query.Close;
447 >        Query.SQL.Text := 'Alter Table ' +  {do not localize}
448 >          QuoteIdentifier(DataBase.SQLDialect, FTableName) +
449 >          ' Drop Constraint ' +
450 >          QuoteIdentifier(DataBase.SQLDialect, Name);
451 >        Query.Prepare;
452 >        Query.ExecQuery;
453 >        IndexDefs.Updated := False;
454 >        Result := True;
455 >      end;
456 >    finally
457 >      Query.Free;
458 >    end;
459 >  end;
460 >
461 >  procedure DeleteByKey;
462 >  begin
463 >    Query := TIBSQL.Create(self);
464 >    try
465 >      Query.Database := DataBase;
466 >      Query.Transaction := Transaction;
467 >      Query.SQL.Text := 'Select RDB$CONSTRAINT_NAME from RDB$RELATION_CONSTRAINTS ' +
468 >        'where RDB$RELATION_NAME = ' +
469 >        '''' +
470 >        ExtractIdentifier(Database.SQLDialect,
471 >          QuoteIdentifier(DataBase.SQLDialect, FTableName)) +
472 >        ''' ' +
473 >        'AND RDB$INDEX_NAME = ' +
474 >        '''' +
475 >        ExtractIdentifier(Database.SQLDialect,
476 >          QuoteIdentifier(DataBase.SQLDialect, Name)) +
477 >        ''' ' +
478 >        'AND RDB$CONSTRAINT_TYPE = ''PRIMARY KEY''';
479 >      Query.Prepare;
480 >      Query.ExecQuery;
481 >      if not Query.EOF then
482 >      begin
483 >        Query.Close;
484 >        Query.SQL.Text := 'Alter Table ' +  {do not localize}
485 >          QuoteIdentifier(DataBase.SQLDialect, FTableName) +
486 >          ' Drop Constraint ' +
487 >          QuoteIdentifier(DataBase.SQLDialect, Query.FieldByName('RDB$CONSTRAINT_NAME').AsString);
488 >        Query.Prepare;
489 >        Query.ExecQuery;
490 >        IndexDefs.Updated := False;
491 >      end;
492 >    finally
493 >      Query.Free;
494 >    end;
495 >  end;
496 >
497 > begin
498 >  if Active then
499 >    CheckBrowseMode;
500 >  IndexDefs.Update;
501 >  if (Pos('RDB$PRIMARY', Name) <> 0 ) then {do not localize} {mbcs ok}
502 >    DeleteByKey
503 >  else if not DeleteByConstraint then
504 >    DeleteByIndex;
505 > end;
506 >
507 > function TIBTable.GetIndexFieldNames: string;
508 > begin
509 >  if FFieldsIndex then Result := FIndexName else Result := '';
510 > end;
511 >
512 > function TIBTable.GetIndexName: string;
513 > begin
514 >  if FFieldsIndex then Result := '' else Result := FIndexName;
515 > end;
516 >
517 > procedure TIBTable.GetIndexNames(List: TStrings);
518 > begin
519 >  IndexDefs.Update;
520 >  IndexDefs.GetItemNames(List);
521 > end;
522 >
523 > procedure TIBTable.GetIndexParams(const IndexName: string;
524 >  FieldsIndex: Boolean; var IndexedName: string);
525 > var
526 >  IndexStr: TIndexName;
527 > begin
528 >  if IndexName <> '' then
529 >  begin
530 >    IndexDefs.Update;
531 >    IndexStr := IndexName;
532 >    if FieldsIndex then
533 >      IndexStr := IndexDefs.FindIndexForFields(IndexName).Name;
534 >  end;
535 >  IndexedName := IndexStr;
536 > end;
537 >
538 > procedure TIBTable.SetIndexDefs(Value: TIndexDefs);
539 > begin
540 >  IndexDefs.Assign(Value);
541 > end;
542 >
543 > procedure TIBTable.SetIndex(const Value: string; FieldsIndex: Boolean);
544 > begin
545 >  if Active then CheckBrowseMode;
546 >  if (FIndexName <> Value) or (FFieldsIndex <> FieldsIndex) then
547 >  begin
548 >    FIndexName := Value;
549 >    FFieldsIndex := FieldsIndex;
550 >    if Active then
551 >    begin
552 >      SwitchToIndex;
553 >    end;
554 >  end;
555 > end;
556 >
557 > procedure TIBTable.SetIndexFieldNames(const Value: string);
558 > begin
559 >  SetIndex(Value, Value <> '');
560 > end;
561 >
562 > procedure TIBTable.SetIndexName(const Value: string);
563 > begin
564 >  SetIndex(Value, False);
565 > end;
566 >
567 > procedure TIBTable.UpdateIndexDefs;
568 > var
569 >  Opts: TIndexOptions;
570 >  Flds: string;
571 >  Query, SubQuery: TIBSQL;
572 >  fn: string;
573 >  aField: TFieldDef;
574 > begin
575 >  if not (csReading in ComponentState) then begin
576 >  if not Active and not FSwitchingIndex  then
577 >    FieldDefs.Update;
578 >  IndexDefs.Clear;
579 >  Database.InternalTransaction.StartTransaction;
580 >  Query := TIBSQL.Create(self);
581 >  try
582 >    FPrimaryIndexFields := '';
583 >    Query.GoToFirstRecordOnExecute := False;
584 >    Query.Database := DataBase;
585 >    Query.Transaction := Database.InternalTransaction;
586 >    Query.SQL.Text :=
587 >    'Select I.RDB$INDEX_NAME, I.RDB$UNIQUE_FLAG, I.RDB$INDEX_TYPE, ' + {do not localize}
588 >    'I.RDB$SEGMENT_COUNT, S.RDB$FIELD_NAME from RDB$INDICES I, ' + {do not localize}
589 >    'RDB$INDEX_SEGMENTS S where I.RDB$INDEX_NAME = S.RDB$INDEX_NAME '+ {do not localize}
590 >    'and I.RDB$RELATION_NAME = ' + '''' + {do not localize}
591 >     ExtractIdentifier(Database.SQLDialect,
592 >       QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
593 >    Query.Prepare;
594 >    Query.ExecQuery;
595 >    while (not Query.EOF) and Query.Next do
596 >    begin
597 >      with IndexDefs.AddIndexDef do
598 >      begin
599 >        Name := TrimRight(Query.FieldByName('RDB$INDEX_NAME').AsString); {do not localize}
600 >        Opts := [];
601 >        if Pos ('RDB$PRIMARY', Name) = 1 then Include(Opts, ixPrimary); {do not localize} {mbcs ok}
602 >        if Query.FieldByName('RDB$UNIQUE_FLAG').AsInteger = 1 then Include(Opts, ixUnique); {do not localize}
603 >        if Query.FieldByName('RDB$INDEX_TYPE').AsInteger = 2  then Include(Opts, ixDescending); {do not localize}
604 >        Options := Opts;
605 >        if (Query.FieldByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize}
606 >        begin
607 >          fn := Trim(Query.FieldByName('RDB$FIELD_NAME').AsString); {do not localize}
608 >          aField := GetFieldDefFromAlias(fn);
609 >          if assigned(aField) then
610 >             Fields := aField.Name
611 >          else
612 >              Fields := fn;
613 >        end
614 >        else begin
615 >          SubQuery := TIBSQL.Create(self);
616 >        try
617 >          SubQuery.GoToFirstRecordOnExecute := False;
618 >          SubQuery.Database := DataBase;
619 >          SubQuery.Transaction := Database.InternalTransaction;
620 >          SubQuery.SQL.Text :=
621 >         'Select RDB$FIELD_NAME from RDB$INDEX_SEGMENTS where RDB$INDEX_NAME = ' + {do not localize}
622 >          '''' +
623 >          ExtractIdentifier(Database.SQLDialect,
624 >            QuoteIdentifier(DataBase.SQLDialect, Name)) +
625 >          '''' + 'ORDER BY RDB$FIELD_POSITION'; {do not localize}
626 >          SubQuery.Prepare;
627 >          SubQuery.ExecQuery;
628 >          Flds := '';
629 >          while (not SubQuery.EOF) and SubQuery.Next do
630 >          begin
631 >            fn := TrimRight(SubQuery.FieldByName('RDB$FIELD_NAME').AsString); {do not localize}
632 >            aField := GetFieldDefFromAlias(fn);
633 >            if assigned(aField) then
634 >               fn := aField.Name;
635 >            if (Flds = '') then
636 >               Flds := fn
637 >            else begin
638 >              Query.Next;
639 >              Flds := Flds + ';' + fn;
640 >            end;
641 >          end;
642 >          Fields := Flds;
643 >        finally
644 >          SubQuery.Free;
645 >        end;
646 >        end;
647 >        if (ixDescending in Opts) then
648 >          DescFields := Fields;
649 >        if ixPrimary in Opts then
650 >          FPrimaryIndexFields := Fields;
651 >      end;
652 >    end;
653 >  finally
654 >    Query.Free;
655 >    Database.InternalTransaction.Commit;
656 >  end;
657 >  end;
658 > end;
659 >
660 > function TIBTable.GetExists: Boolean;
661 > var
662 >  Query: TIBSQL;
663 > begin
664 >  Result := Active;
665 >  if Result or (TableName = '') then Exit;
666 >  Database.InternalTransaction.StartTransaction;
667 >  Query := TIBSQL.Create(self);
668 >  try
669 >    Query.Database := DataBase;
670 >    Query.Transaction := Database.InternalTransaction;
671 >    Query.SQL.Text :=
672 >    'Select USER from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize}
673 >    '''' +
674 >    ExtractIdentifier(Database.SQLDialect,
675 >      QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
676 >    Query.Prepare;
677 >    Query.ExecQuery;
678 >    Result := not Query.EOF;
679 >  finally
680 >    Query.Free;
681 >    Database.InternalTransaction.Commit;
682 >  end;
683 > end;
684 >
685 > procedure TIBTable.GotoCurrent(Table: TIBTable);
686 > begin
687 >  CheckBrowseMode;
688 >  Table.CheckBrowseMode;
689 >  if (Database <> Table.Database) or
690 >    (CompareText(TableName, Table.TableName) <> 0) then
691 >    IBError(ibxeTableNameMismatch, [nil]);
692 >  Table.UpdateCursorPos;
693 >  InternalGotoDBKey(Table.CurrentDBKey);
694 >  DoBeforeScroll;
695 >  Resync([rmExact, rmCenter]);
696 >  DoAfterScroll;
697 > end;
698 >
699 >
700 > procedure TIBTable.CreateTable;
701 > var
702 >  FieldList: string;
703 >
704 >  procedure InitFieldsList;
705 >  var
706 >    I: Integer;
707 >  begin
708 >    InitFieldDefsFromFields;
709 >    for I := 0 to FieldDefs.Count - 1 do begin
710 >      if ( I > 0) then
711 >        FieldList := FieldList + ', ';
712 >      with FieldDefs[I] do
713 >      begin
714 >        case DataType of
715 >          ftString:
716 >            FieldList := FieldList +
717 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
718 >              ' VARCHAR(' + IntToStr(Size) + ')'; {do not localize}
719 >          ftFixedChar:
720 >            FieldList := FieldList +
721 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
722 >              ' CHAR(' + IntToStr(Size) + ')'; {do not localize}
723 >          ftBoolean:
724 >            FieldList := FieldList +
725 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
726 >              ' BOOLEAN'; {do not localize}
727 >          ftSmallint, ftWord:
728 >            FieldList := FieldList +
729 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
730 >              ' SMALLINT'; {do not localize}
731 >          ftInteger:
732 >            FieldList := FieldList +
733 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
734 >              ' INTEGER'; {do not localize}
735 >          ftFloat, ftCurrency:
736 >            FieldList := FieldList +
737 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
738 >              ' DOUBLE PRECISION'; {do not localize}
739 >          ftBCD: begin
740 >            if (Database.SQLDialect = 1) then begin
741 >              if (Precision > 9) then
742 >                IBError(ibxeFieldUnsupportedType,[nil]);
743 >              if (Precision <= 4) then
744 >                Precision := 9;
745 >            end;
746 >            if (Precision <= 4 ) then
747 >              FieldList := FieldList +
748 >                QuoteIdentifier(DataBase.SQLDialect, Name) +
749 >                ' Numeric(18, 4)' {do not localize}
750 >            else
751 >              FieldList := FieldList +
752 >                QuoteIdentifier(DataBase.SQLDialect, Name) +
753 >                ' Numeric(' + IntToStr(Precision) + ', 4)'; {do not localize}
754 >          end;
755 >          ftDate:
756 >            FieldList := FieldList +
757 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
758 >              ' DATE'; {do not localize}
759 >          ftTime:
760 >            FieldList := FieldList +
761 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
762 >              ' TIME'; {do not localize}
763 >          ftDateTime:
764 >            if (Database.SQLDialect = 1) then
765 >              FieldList := FieldList +
766 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
767 >              ' DATE' {do not localize}
768 >            else
769 >              FieldList := FieldList +
770 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
771 >              ' TIMESTAMP'; {do not localize}
772 >          ftLargeInt:
773 >            if (Database.SQLDialect = 1) then
774 >              IBError(ibxeFieldUnsupportedType,[nil])
775 >            else
776 >              FieldList := FieldList +
777 >                QuoteIdentifier(DataBase.SQLDialect, Name) +
778 >                ' Numeric(18, 0)'; {do not localize}
779 >          ftBlob, ftMemo:
780 >            FieldList := FieldList +
781 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
782 >              ' BLOB SUB_TYPE 1'; {do not localize}
783 >          ftBytes, ftVarBytes, ftGraphic..ftTypedBinary:
784 >            FieldList := FieldList +
785 >              QuoteIdentifier(DataBase.SQLDialect, Name) +
786 >              ' BLOB SUB_TYPE 0'; {do not localize}
787 >          ftUnknown, ftADT, ftArray, ftReference, ftDataSet,
788 >          ftCursor, ftWideString, ftAutoInc:
789 >            IBError(ibxeFieldUnsupportedType,[nil]);
790 >          else
791 >            IBError(ibxeFieldUnsupportedType,[nil]);
792 >        end;
793 >        if faRequired in Attributes then
794 >          FieldList := FieldList + ' NOT NULL'; {do not localize}
795 >      end;
796 >    end;
797 >  end;
798 >
799 >  procedure InternalCreateTable;
800 >  var
801 >    I: Integer;
802 >    Query: TIBSQL;
803 >  begin
804 >    if (FieldList = '') then
805 >      IBError(ibxeFieldUnsupportedType,[nil]);
806 >    Query := TIBSQL.Create(self);
807 >    try
808 >      Query.Database := Database;
809 >      Query.transaction := Transaction;
810 >      Query.SQL.Text := 'Create Table ' +
811 >        QuoteIdentifier(DataBase.SQLDialect, FTableName) +
812 >        ' (' + FieldList; {do not localize}
813 >      for I := 0 to IndexDefs.Count - 1 do
814 >      with IndexDefs[I] do
815 >        if ixPrimary in Options then
816 >        begin
817 >          Query.SQL.Text := Query.SQL.Text + ', CONSTRAINT ' +
818 >            QuoteIdentifier(DataBase.SQLDialect, Name) +
819 >            ' Primary Key (' +
820 >            FormatFieldsList(Fields) +
821 >            ')';
822 >        end;
823 >      Query.SQL.Text := Query.SQL.Text + ')';
824 >      Query.Prepare;
825 >      Query.ExecQuery;
826 >    finally
827 >      Query.Free;
828 >    end;
829 >  end;
830 >
831 >  procedure InternalCreateIndex;
832 >  var
833 >    I: Integer;
834 >  begin
835 >    for I := 0 to IndexDefs.Count - 1 do
836 >    with IndexDefs[I] do
837 >      if not (ixPrimary in Options) then
838 >        AddIndex(Name, Fields, Options);
839 >  end;
840 >
841 > begin
842 >  CheckInactive;
843 >  InitFieldsList;
844 >  InternalCreateTable;
845 >  InternalCreateIndex;
846 > end;
847 >
848 > procedure TIBTable.DeleteTable;
849 > var
850 >  Query: TIBSQL;
851 > begin
852 >  CheckInactive;
853 >  Query := TIBSQL.Create(self);
854 >  try
855 >    Query.Database := DataBase;
856 >    Query.Transaction := Transaction;
857 >    Query.SQL.Text := 'drop table ' +  {do not localize}
858 >      QuoteIdentifier(DataBase.SQLDialect, FTableName);
859 >    Query.Prepare;
860 >    Query.ExecQuery;
861 >  finally
862 >    Query.Free;
863 >  end;
864 > end;
865 >
866 > procedure TIBTable.EmptyTable;
867 > var
868 >  Query: TIBSQL;
869 > begin
870 >  if Active then
871 >    CheckBrowseMode;
872 >  Query := TIBSQL.Create(self);
873 >  try
874 >    Query.Database := DataBase;
875 >    Query.Transaction := Transaction;
876 >    Query.SQL.Text := 'delete from ' + {do not localize}
877 >      QuoteIdentifier(DataBase.SQLDialect, FTableName);
878 >    Query.Prepare;
879 >    Query.ExecQuery;
880 >    if Active then
881 >    begin
882 >      ClearBuffers;
883 >      DataEvent(deDataSetChange, 0);
884 >    end;
885 >  finally
886 >    Query.Free;
887 >  end;
888 > end;
889 >
890 > procedure TIBTable.DataEvent(Event: TDataEvent; Info: Ptrint);
891 > begin
892 >  if Event = dePropertyChange then begin
893 >    IndexDefs.Updated := False;
894 >    FRegenerateSQL := True;
895 >  end;
896 >  inherited DataEvent(Event, Info);
897 > end;
898 >
899 > { Informational & Property }
900 >
901 > function TIBTable.GetCanModify: Boolean;
902 > begin
903 >  Result := True;
904 >  if (FTableName = '') or FReadOnly
905 >    or FSystemTable or FMultiTableView then
906 >    Result := False;
907 > end;
908 >
909 > function TIBTable.InternalGetUpdatable: Boolean;
910 > var
911 >  Query : TIBSQL;
912 > begin
913 >  Database.InternalTransaction.StartTransaction;
914 >  Query := TIBSQL.Create(self);
915 >  try
916 >    Query.Database := DataBase;
917 >    Query.Transaction := Database.InternalTransaction;
918 >    Query.SQL.Text := 'Select RDB$SYSTEM_FLAG, RDB$DBKEY_LENGTH ' + {do not localize}
919 >                    'from RDB$RELATIONS where RDB$RELATION_NAME = ' + {do not localize}
920 >                    '''' +
921 >                    ExtractIdentifier(Database.SQLDialect,
922 >                      QuoteIdentifier(DataBase.SQLDialect, FTableName)) + '''';
923 >    Query.Prepare;
924 >    Query.ExecQuery;
925 >    if (Query.Fields[0].AsInteger <> 0) or
926 >       (Query.Fields[1].AsInteger <> 8) then
927 >      Result := False
928 >    else
929 >      Result := True;
930 >  finally
931 >    Query.Free;
932 >    Database.InternalTransaction.Commit;
933 >  end;
934 > end;
935 >
936 > function TIBTable.FieldDefsStored: Boolean;
937 > begin
938 >  Result := StoreDefs and (FieldDefs.Count > 0);
939 > end;
940 >
941 > function TIBTable.IndexDefsStored: Boolean;
942 > begin
943 >  Result := StoreDefs and (IndexDefs.Count > 0);
944 > end;
945 >
946 > procedure TIBTable.SetParams;
947 > var
948 >  i: Integer;
949 > begin
950 >  if (MasterSource = nil) or (MasterSource.DataSet = nil) or
951 >  (not MasterSource.DataSet.Active) or (FMasterFieldsList.Count = 0) then
952 >    exit;
953 >  for i := 0 to FMasterFieldsList.Count - 1 do
954 >    QSelect.Params.ByName(FMasterFieldsList.Strings[i]).Value :=
955 >    MasterSource.DataSet.FieldByName(FMasterFieldsList.Strings[i]).Value;
956 > end;
957 >
958 > procedure TIBTable.MasterChanged(Sender: TObject);
959 > begin
960 >  CheckBrowseMode;
961 >  SetParams;
962 >  ReQuery;
963 > end;
964 >
965 > procedure TIBTable.MasterDisabled(Sender: TObject);
966 > begin
967 >  DataEvent(dePropertyChange, 0);
968 >  ReQuery;
969 > end;
970 >
971 > function TIBTable.GetDataSource: TDataSource;
972 > begin
973 >  Result := FMasterLink.DataSource;
974 > end;
975 >
976 > procedure TIBTable.SetDataSource(Value: TDataSource);
977 > begin
978 >  if IsLinkedTo(Value) then IBError(ibxeCircularDataLink, [Self]);
979 >  if FMasterLink.DataSource <> Value then
980 >    DataEvent(dePropertyChange, 0);
981 >  FMasterLink.DataSource := Value;
982 > end;
983 >
984 > function TIBTable.GetMasterFields: string;
985 > begin
986 >  Result := FMasterLink.FieldNames;
987 > end;
988 >
989 > procedure TIBTable.SetMasterFields(const Value: string);
990 > begin
991 >  if FMasterLink.FieldNames <> Value then
992 >    DataEvent(dePropertyChange, 0);
993 >  FMasterLink.FieldNames := Value;
994 > end;
995 >
996 > procedure TIBTable.DoOnNewRecord;
997 > var
998 >  I: Integer;
999 > begin
1000 >  if FMasterLink.Active and (FMasterLink.Fields.Count > 0) then
1001 >    for I := 0 to FMasterLink.Fields.Count - 1 do
1002 >      IndexFields[I] := TField(FMasterLink.Fields[I]);
1003 >  inherited DoOnNewRecord;
1004 > end;
1005 >
1006 > function TIBTable.FormatFieldsList(Value: String): String;
1007 > var
1008 >  FieldName: string;
1009 >  i: Integer;
1010 > begin
1011 >  if Database.SQLDialect = 1 then begin
1012 >    Value := QuoteIdentifier(Database.SQLDialect, Value);
1013 >    Result := StringReplace (Value, ';', ', ', [rfReplaceAll]);
1014 >  end
1015 >  else begin
1016 >    i := 1;
1017 >    Result := '';
1018 >    while i <= Length(Value) do
1019 >    begin
1020 >      FieldName := ExtractFieldName(Value, i);
1021 >      if Result = '' then
1022 >        Result := QuoteIdentifier(Database.SQLDialect, FieldName)
1023 >      else
1024 >        Result := Result + ', ' + QuoteIdentifier(Database.SQLDialect, FieldName);
1025 >    end;
1026 >  end;
1027 > end;
1028 >
1029 > procedure TIBTable.ExtractLinkFields;
1030 > var
1031 >  i: Integer;
1032 >  DetailFieldNames: String;
1033 > begin
1034 >  FMasterFieldsList.Clear;
1035 >  FDetailFieldsList.Clear;
1036 >  i := 1;
1037 >  while i <= Length(MasterFields) do
1038 >    FMasterFieldsList.Add(ExtractFieldName(MasterFields, i));
1039 >  i := 1;
1040 >  if IndexFieldNames = '' then
1041 >    DetailFieldNames := FPrimaryIndexFields
1042 >  else
1043 >    DetailFieldNames := IndexFieldNames;
1044 >  while i <= Length(DetailFieldNames) do
1045 >    FDetailFieldsList.Add(ExtractFieldName(DetailFieldNames, i));
1046 > end;
1047 >
1048 > procedure TIBTable.GetDetailLinkFields(MasterFields, DetailFields: TList);
1049 > var
1050 >  i: Integer;
1051 >  Idx: TIndexDef;
1052 > begin
1053 >  MasterFields.Clear;
1054 >  DetailFields.Clear;
1055 >  if (MasterSource <> nil) and (MasterSource.DataSet <> nil) and
1056 >     (Self.MasterFields <> '') then
1057 >  begin
1058 >    Idx := nil;
1059 >    MasterSource.DataSet.GetFieldList(MasterFields, Self.MasterFields);
1060 >    UpdateIndexDefs;
1061 >    if IndexName <> '' then
1062 >      Idx := IndexDefs.Find(IndexName)
1063 >    else if IndexFieldNames <> '' then
1064 >      Idx := IndexDefs.GetIndexForFields(IndexFieldNames, False)
1065 >    else
1066 >      for i := 0 to IndexDefs.Count - 1 do
1067 >        if ixPrimary in IndexDefs[i].Options then
1068 >        begin
1069 >          Idx := IndexDefs[i];
1070 >          break;
1071 >        end;
1072 >    if Idx <> nil then
1073 >      GetFieldList(DetailFields, Idx.Fields);
1074 >  end;
1075 > end;
1076 >
1077 > procedure TIBTable.SetReadOnly(Value: Boolean);
1078 > begin
1079 >  CheckInactive;
1080 >  FReadOnly := Value;
1081 > end;
1082 >
1083 > procedure TIBTable.SetTableName(Value: String);
1084 > begin
1085 >  if not (csReading in ComponentState) then
1086 >  begin
1087 >    CheckInactive;
1088 >    if Value <> FTableName then
1089 >    begin
1090 >      ResetSQLStatements;
1091 >      FRegenerateSQL := True;
1092 >      FTableName := Value;
1093 >      IndexName := '';
1094 >      IndexFieldNames := '';
1095 >      FPrimaryIndexFields := '';
1096 >      DataEvent(dePropertyChange, 0);
1097 >    end;
1098 >  end
1099 >  else if Value <> FTableName then
1100 >    FTableName := Value;
1101 > end;
1102 >
1103 > function TIBTable.GetIndexField(Index: Integer): TField;
1104 > var
1105 >  I, Count: Integer;
1106 >  FieldNames, FieldName: String;
1107 > begin
1108 >  Result := nil;
1109 >  FieldName := '';
1110 >  FieldNames := IndexFieldNames;
1111 >  if FieldNames = '' then
1112 >  begin
1113 >    for I := 0 to IndexDefs.Count - 1 do
1114 >      if (IndexDefs[i].Name = FIndexName) then
1115 >      begin
1116 >        FieldNames := IndexDefs[i].Fields;
1117 >        break;
1118 >      end;
1119 >  end;
1120 >  for I := 0 to Index do
1121 >  begin
1122 >    Count := Pos(';', FieldNames); {mbcs OK}
1123 >    if Count = 0 then
1124 >      FieldName := FieldNames
1125 >    else begin
1126 >      FieldName := Copy(FieldNames, 0, Count - 1);
1127 >      System.Delete(FieldNames, 1, Count);
1128 >    end;
1129 >  end;
1130 >  if FieldName <> '' then
1131 >    Result := FieldByName(FieldName)
1132 >  else
1133 >    IBError(ibxeIndexFieldMissing, [nil]);
1134 > end;
1135 >
1136 >
1137 > procedure TIBTable.SetIndexField(Index: Integer; Value: TField);
1138 > begin
1139 >  GetIndexField(Index).Assign(Value);
1140 > end;
1141 >
1142 > function TIBTable.GetIndexFieldCount: Integer;
1143 > var
1144 >  I, Index: Integer;
1145 >  FieldNames: String;
1146 >  done: Boolean;
1147 > begin
1148 >  FieldNames := IndexFieldNames;
1149 >  if FieldNames = '' then
1150 >  begin
1151 >    for I := 0 to IndexDefs.Count - 1 do
1152 >      if (IndexDefs[i].Name = FIndexName) then
1153 >      begin
1154 >        FieldNames := IndexDefs[i].Fields;
1155 >        break;
1156 >      end;
1157 >  end;
1158 >  if FieldNames = '' then
1159 >    Result := 0
1160 >  else
1161 >  begin
1162 >    done := False;
1163 >    Result := 1;
1164 >    while not done do
1165 >    begin
1166 >      Index := Pos(';', FieldNames); {mbcs ok}
1167 >      if Index <> 0 then
1168 >      begin
1169 >        System.Delete(FieldNames, 1, Index);
1170 >        Inc(Result);
1171 >      end else
1172 >        done := True;
1173 >    end;
1174 >  end;
1175 > end;
1176 >
1177 > function TIBTable.GetTableNames: TStrings;
1178 > begin
1179 >  FNameList.clear;
1180 >  GetTableNamesFromServer;
1181 >  Result := FNameList;
1182 > end;
1183 >
1184 > procedure TIBTable.GetTableNamesFromServer;
1185 > var
1186 >  Query : TIBSQL;
1187 > begin
1188 >  if not (csReading in ComponentState) then begin
1189 >    ActivateConnection;
1190 >    Database.InternalTransaction.StartTransaction;
1191 >    Query := TIBSQL.Create(self);
1192 >    try
1193 >      Query.GoToFirstRecordOnExecute := False;
1194 >      Query.Database := DataBase;
1195 >      Query.Transaction := Database.InternalTransaction;
1196 >      if (TableTypes * [ttSystem, ttView] = [ttSystem, ttView]) then
1197 >        Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' {do not localize}
1198 >      else if ttSystem in TableTypes then
1199 >        Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' + {do not localize}
1200 >                          ' where RDB$VIEW_BLR is NULL' {do not localize}
1201 >      else if ttView in TableTypes then
1202 >        Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' + {do not localize}
1203 >                          ' where RDB$SYSTEM_FLAG = 0' {do not localize}
1204 >      else
1205 >        Query.SQL.Text := 'Select RDB$RELATION_NAME from RDB$RELATIONS' + {do not localize}
1206 >                          ' where RDB$VIEW_BLR is NULL and RDB$SYSTEM_FLAG = 0'; {do not localize}
1207 >      Query.Prepare;
1208 >      Query.ExecQuery;
1209 >      while (not Query.EOF) and Query.Next  do
1210 >        FNameList.Add (TrimRight(Query.Fields[0].AsString));
1211 >    finally
1212 >      Query.Free;
1213 >      Database.InternalTransaction.Commit;
1214 >    end;
1215 >  end;
1216 > end;
1217 >
1218 > procedure TIBTable.SwitchToIndex();
1219 > begin
1220 >  FSwitchingIndex := True;
1221 >  InternalTableRefresh;
1222 >  FSwitchingIndex := False;
1223 > end;
1224 >
1225 > procedure TIBTable.InternalTableRefresh();
1226 > var
1227 >  DBKey: TIBDBKey;
1228 > begin
1229 >  CheckActive;
1230 >  DBKey := CurrentDBKey;
1231 >  FRegenerateSQL := True;
1232 >  Reopen;
1233 >  if DBKey.DBKey[0] <> 0 then
1234 >    InternalGotoDBKey(DBKey);
1235 > end;
1236 >
1237 > procedure TIBTable.GenerateSQL;
1238 > var
1239 >  i: Integer;
1240 >  SQL: TStrings;
1241 >  OrderByStr: string;
1242 >  bWhereClausePresent: Boolean;
1243 >  fn: string;
1244 >  aField: TFieldDef;
1245 > begin
1246 >  bWhereClausePresent := False;
1247 >  Database.CheckActive;
1248 >  Transaction.CheckInTransaction;
1249 >  if IndexDefs.Updated = False then
1250 >    IndexDefs.Update;
1251 >  if IndexFieldNames <> '' then
1252 >    OrderByStr := FormatFieldsList(IndexFieldNames)
1253 >  else if IndexName <> '' then
1254 >    OrderByStr := FormatFieldsList(IndexDefs[IndexDefs.Indexof (IndexName)].Fields)
1255 >  else if FDefaultIndex and (FPrimaryIndexFields <> '') then
1256 >    OrderByStr := FormatFieldsList(FPrimaryIndexFields);
1257 >  SQL := TStringList.Create;
1258 >  SQL.Text := 'select ' + {do not localize}
1259 >    QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1260 >    + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1261 >    + QuoteIdentifier(DataBase.SQLDialect, FTableName);
1262 >  if Filtered and (Filter <> '') then
1263 >  begin
1264 >    SQL.Text := SQL.Text + ' where ' + Filter; {do not localize}
1265 >    bWhereClausePresent := True;
1266 >  end;
1267 >  if (MasterSource <> nil) and (MasterSource.DataSet <> nil) and (MasterFields <> '') then
1268 >  begin
1269 >    if bWhereClausePresent then
1270 >      SQL.Text := SQL.Text + ' AND ' {do not localize}
1271 >    else
1272 >      SQL.Text := SQL.Text + ' WHERE '; {do not localize}
1273 >    ExtractLinkfields;
1274 >    if FDetailFieldsList.Count < FMasterFieldsList.Count then
1275 >      IBError(ibxeUnknownError, [nil]);
1276 >    for i := 0 to FMasterFieldsList.Count - 1 do
1277 >    begin
1278 >      if i > 0 then
1279 >        SQL.Text := SQL.Text + 'AND ';
1280 >      aField := FieldDefs.Find(FDetailFieldsList.Strings[i]);
1281 >      if assigned(aField) then
1282 >         fn := GetDBAliasName(aField.FieldNo)
1283 >      else
1284 >          fn := FDetailFieldsList.Strings[i]; {something wrong if we get here - but should word}
1285 >      SQL.Text := SQL.Text +
1286 >        QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + FMasterFieldsList.Strings[i];
1287 >    end;
1288 >  end;
1289 >  if OrderByStr <> '' then
1290 >    SQL.Text := SQL.Text + ' order by ' + OrderByStr; {do not localize}
1291 >  SelectSQL.Assign(SQL);
1292 >  RefreshSQL.Text := 'select ' + {do not localize}
1293 >    QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1294 >    + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1295 >    + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1296 >    ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY'; {do not localize}
1297 >  WhereDBKeyRefreshSQL.Assign(RefreshSQL);
1298 >  InternalPrepare;
1299 >  SQL.Free;
1300 > end;
1301 >
1302 > procedure TIBTable.GenerateUpdateSQL;
1303 > var
1304 >  InsertFieldList, InsertParamList, UpdateFieldList: string;
1305 >  WherePrimaryFieldList, WhereAllFieldList,
1306 >    InsertReturningFieldList, UpdateReturningFieldList: string;
1307 >
1308 >  procedure GenerateFieldLists;
1309 >  var
1310 >    I: Integer;
1311 >    AllowReturningClause: boolean;
1312 >  begin
1313 >     {Is database Firebird 2.1 or later?}
1314 >    AllowReturningClause := (DatabaseInfo.ODSMajorVersion > 11) or
1315 >        ((DatabaseInfo.ODSMajorVersion = 11) and (DatabaseInfo.ODSMinorVersion >= 1));
1316 >    for I := 0 to FieldDefs.Count - 1 do begin
1317 >      with TIBFieldDef(FieldDefs[I]) do begin
1318 >        if AllowReturningClause then
1319 >        begin
1320 >          if (faReadOnly in Attributes) or IdentityColumn then
1321 >          begin
1322 >            if InsertReturningFieldList <> '' then
1323 >              InsertReturningFieldList := InsertReturningFieldList + ', ' +
1324 >                       QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo))
1325 >            else
1326 >              InsertReturningFieldList := ' RETURNING ' +
1327 >                QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo))
1328 >          end;
1329 >          if (faReadOnly in Attributes)  then
1330 >          begin
1331 >            if UpdateReturningFieldList <> '' then
1332 >              UpdateReturningFieldList := UpdateReturningFieldList + ', ' +
1333 >                       QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo))
1334 >            else
1335 >              UpdateReturningFieldList := ' RETURNING ' +
1336 >                QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo))
1337 >          end;
1338 >        end;
1339 >        if not (InternalCalcField or (faReadOnly in Attributes) or
1340 >           (DataType = ftUnknown)) then
1341 >        begin
1342 >          if not IdentityColumn then
1343 >          begin
1344 >            if ( InsertFieldList <> '' ) then begin
1345 >              InsertFieldList := InsertFieldList + ', ';
1346 >              InsertParamList := InsertParamList + ', ';
1347 >            end;
1348 >            InsertFieldList := InsertFieldList +
1349 >              QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo));
1350 >            InsertParamList := InsertParamList + ':' +  Name;
1351 >          end;
1352 >          if (UpdateFieldList <> '') then begin
1353 >            UpdateFieldList := UpdateFieldList + ', ';
1354 >            if (DataType <> ftBlob) and (DataType <>ftMemo) then
1355 >              WhereAllFieldList := WhereAllFieldList + ' AND ';
1356 >          end;
1357 >          UpdateFieldList := UpdateFieldList +
1358 >            QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) +
1359 >            ' = :' + Name;
1360 >          if (DataType <> ftBlob) and (DataType <>ftMemo) then
1361 >            WhereAllFieldList := WhereAllFieldList +
1362 >              QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + ' = :' +  Name;
1363 >        end;
1364 >      end;
1365 >    end;
1366 >  end;
1367 >
1368 >  procedure GenerateWherePrimaryFieldList;
1369 >  var
1370 >    i: Integer;
1371 >    tmp, fn: String;
1372 >    aField: TFieldDef;
1373 >  begin
1374 >    i := 1;
1375 >    while i <= Length(FPrimaryIndexFields) do
1376 >    begin
1377 >      tmp := ExtractFieldName(FPrimaryIndexFields, i);
1378 >      aField := FieldDefs.Find(tmp);
1379 >      if assigned(aField) then
1380 >         fn := GetDBAliasName(aField.FieldNo)
1381 >      else
1382 >         fn := tmp; {something wrong if we get here - but will work in most cases}
1383 >      tmp :=
1384 >        QuoteIdentifier(DataBase.SQLDialect, fn) +  ' = :' +
1385 >        QuoteIdentifier(DataBase.SQLDialect, tmp);{do not localize}
1386 >      if WherePrimaryFieldList <> '' then
1387 >        WherePrimaryFieldList :=
1388 >          WherePrimaryFieldList + ' AND ' + tmp
1389 >      else
1390 >        WherePrimaryFieldList := tmp;
1391 >    end;
1392 >  end;
1393 >
1394 > begin
1395 >  if InternalGetUpdatable = False  then
1396 >    FReadOnly := True
1397 >  else
1398 >  begin
1399 >    DeleteSQL.Text := 'delete from ' + {do not localize}
1400 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1401 >      ' where RDB$DB_KEY = ' + ':IBX_INTERNAL_DBKEY'; {do not localize}
1402 >    GenerateFieldLists;
1403 >    InsertSQL.Text := 'insert into ' + {do not localize}
1404 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1405 >    ' (' + InsertFieldList + {do not localize}
1406 >      ') values (' + InsertParamList + ')' + InsertReturningFieldList; {do not localize}
1407 >    ModifySQL.Text := 'update ' +
1408 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1409 >      ' set ' + UpdateFieldList + {do not localize}
1410 >      ' where RDB$DB_KEY = :IBX_INTERNAL_DBKEY' + UpdateReturningFieldList; {do not localize}
1411 >    WhereAllRefreshSQL.Text := 'select ' +  {do not localize}
1412 >      QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, '
1413 >      + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1414 >      + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1415 >      ' where ' + WhereAllFieldList ; {do not localize}
1416 >    if FPrimaryIndexFields <> '' then
1417 >    begin
1418 >      GenerateWherePrimaryFieldList;
1419 >      WherePrimaryRefreshSQL.Text := 'select ' + {do not localize}
1420 >        QuoteIdentifier(DataBase.SQLDialect, FTableName) + '.*, ' {do not localize}
1421 >        + 'RDB$DB_KEY as IBX_INTERNAL_DBKEY from ' {do not localize}
1422 >        + QuoteIdentifier(DataBase.SQLDialect, FTableName) +
1423 >        ' where ' + WherePrimaryFieldList; {do not localize}
1424 >    end;
1425 >    try
1426 >      InternalPrepare;
1427 >    except
1428 >      FReadonly := True;
1429 >    end;
1430 >  end;
1431 > end;
1432 >
1433 > procedure TIBTable.ResetSQLStatements;
1434 > begin
1435 >  SelectSQL.Text := '';
1436 >  DeleteSQL.Text := '';
1437 >  InsertSQL.Text := '';
1438 >  ModifySQL.Text := '';
1439 >  RefreshSQL.Text := '';
1440 > end;
1441 >
1442 > procedure TIBTable.SetTableTypes(
1443 >  const Value: TIBTableTypes);
1444 > begin
1445 >  FTableTypes := Value;
1446 > end;
1447 >
1448 > function TIBTable.InternalGotoDBKey(DBKey: TIBDBKey): Boolean;
1449 >
1450 >  function DBKeyCompare (DBKey1, DBKey2: TIBDBKey): Boolean;
1451 >  var
1452 >  I: Integer;
1453 >  begin
1454 >    for I := 0 to 7 do
1455 >      if (DBKey1.DBKey[i] <> DBKey2.DBKey[i]) then begin
1456 >        result := False;
1457 >        exit;
1458 >      end;
1459 >    result := True;
1460 >  end;
1461 > begin
1462 >  CheckActive;
1463 >  DisableControls;
1464 > try
1465 >    result := False;
1466 >    First;
1467 >    while ((not result) and (not EOF)) do begin
1468 >      if (DBKeyCompare (DBKey, PRecordData(GetActiveBuf)^.rdDBKey)) then
1469 >        result := True
1470 >      else
1471 >        Next;
1472 >    end;
1473 >    if not result then
1474 >      First
1475 >    else
1476 >      CursorPosChanged;
1477 >  finally
1478 >    EnableControls;
1479 >  end;
1480 > end;
1481 >
1482 > function TIBTable.GetCurrentDBKey: TIBDBKey;
1483 > var
1484 >  Buf: pChar;
1485 > begin
1486 >  CheckActive;
1487 >  buf := GetActiveBuf;
1488 >  if Buf <> nil then
1489 >    Result := PRecordData(Buf)^.rdDBKey
1490 >  else
1491 >    Result.DBKey[0] := 0;
1492 > end;
1493 >
1494 > procedure TIBTable.Reopen;
1495 > begin
1496 >  DisableControls;
1497 >  try
1498 >    if Active then
1499 >    begin
1500 >      SetState(dsInactive);
1501 >      CloseCursor;
1502 >      OpenCursor(false);
1503 >      SetState(dsBrowse);
1504 >    end;
1505 >  finally
1506 >    EnableControls;
1507 >  end;
1508 > end;
1509 >
1510 > end.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines