35 |
|
|
36 |
|
{$Mode Delphi} |
37 |
|
|
38 |
+ |
{$IF FPC_FULLVERSION >= 20700 } |
39 |
+ |
{$codepage UTF8} |
40 |
+ |
{$ENDIF} |
41 |
+ |
|
42 |
|
interface |
43 |
|
|
44 |
|
uses SysUtils, Classes, DB, IB, IBCustomDataSet, |
45 |
< |
IBHeader, IBSQL, IBUtils; |
45 |
> |
IBSQL, IBUtils; |
46 |
|
|
47 |
|
type |
48 |
|
|
151 |
|
property TableNames: TStrings read GetTableNames; |
152 |
|
|
153 |
|
published |
154 |
+ |
property AutoCommit; |
155 |
|
property Active; |
156 |
|
property BufferChunks; |
157 |
|
property CachedUpdates; |
158 |
+ |
property DataSetCloseAction; |
159 |
|
// property Constraints stored ConstraintsStored; |
160 |
|
property DefaultIndex: Boolean read FDefaultIndex write FDefaultIndex default True; |
161 |
|
property FieldDefs stored FieldDefsStored; |
240 |
|
GenerateUpdateSQL; |
241 |
|
FRegenerateSQL := False; |
242 |
|
end; |
243 |
+ |
{ writeln(SelectSQL.Text); |
244 |
+ |
writeln(InsertSQL.Text); |
245 |
+ |
writeln(DeleteSQL.Text); |
246 |
+ |
writeln(ModifySQL.Text); } |
247 |
|
SetParams; |
248 |
|
inherited InternalOpen; |
249 |
|
end; |
305 |
|
|
306 |
|
procedure TIBTable.InitFieldDefs; |
307 |
|
var |
308 |
< |
sqlscale: Integer; |
308 |
> |
DidActivate: Boolean; |
309 |
|
Query: TIBSQL; |
310 |
|
begin |
311 |
|
if FTableName = '' then IBError(ibxeNoTableName, [nil]); |
312 |
< |
if (InternalPrepared) then InternalInitFieldDefs else |
312 |
> |
if (InternalPrepared) then |
313 |
> |
InternalInitFieldDefs |
314 |
> |
else |
315 |
|
begin |
316 |
< |
Database.InternalTransaction.StartTransaction; |
316 |
> |
{Get the field defs from a simple query on the table} |
317 |
> |
ActivateConnection; |
318 |
|
Query := TIBSQL.Create(self); |
319 |
|
try |
320 |
< |
Query.GoToFirstRecordOnExecute := False; |
321 |
< |
Query.Database := DataBase; |
322 |
< |
Query.Transaction := Database.InternalTransaction; |
323 |
< |
Query.SQL.Text := 'Select R.RDB$FIELD_NAME, R.RDB$FIELD_POSITION, ' + {do not localize} |
324 |
< |
'F.RDB$COMPUTED_BLR, F.RDB$DEFAULT_VALUE, ' + {do not localize} |
325 |
< |
'F.RDB$NULL_FLAG, ' + {do not localize} |
326 |
< |
'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: |
320 |
> |
Query.Database := DataBase; |
321 |
> |
Query.Transaction := Database.InternalTransaction; |
322 |
> |
DidActivate := false; |
323 |
> |
Query.SQL.Text := 'Select * from ' + QuoteIdentifier(DataBase.SQLDialect, FTableName); |
324 |
> |
with Query.Transaction do |
325 |
> |
begin |
326 |
> |
if not InTransaction then |
327 |
|
begin |
328 |
< |
sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize} |
329 |
< |
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; |
328 |
> |
StartTransaction; |
329 |
> |
DidActivate := true |
330 |
|
end; |
331 |
< |
blr_int64: |
332 |
< |
begin |
333 |
< |
sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize} |
334 |
< |
if (sqlscale = 0) then |
335 |
< |
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; |
331 |
> |
end; |
332 |
> |
Query.Prepare; |
333 |
> |
if DidActivate then |
334 |
> |
Query.Transaction.Rollback; |
335 |
> |
FieldDefsFromQuery(Query); |
336 |
|
finally |
337 |
< |
Query.free; |
414 |
< |
Database.InternalTransaction.Commit; |
337 |
> |
Query.Free; |
338 |
|
end; |
339 |
|
end; |
340 |
|
end; |
568 |
|
Opts: TIndexOptions; |
569 |
|
Flds: string; |
570 |
|
Query, SubQuery: TIBSQL; |
571 |
+ |
fn: string; |
572 |
+ |
aField: TFieldDef; |
573 |
|
begin |
574 |
|
if not (csReading in ComponentState) then begin |
575 |
|
if not Active and not FSwitchingIndex then |
602 |
|
if Query.Current.ByName('RDB$INDEX_TYPE').AsInteger = 2 then Include(Opts, ixDescending); {do not localize} |
603 |
|
Options := Opts; |
604 |
|
if (Query.Current.ByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize} |
605 |
< |
Fields := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize} |
605 |
> |
begin |
606 |
> |
fn := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize} |
607 |
> |
aField := GetFieldDefFromAlias(fn); |
608 |
> |
if assigned(aField) then |
609 |
> |
Fields := aField.Name |
610 |
> |
else |
611 |
> |
Fields := fn; |
612 |
> |
end |
613 |
|
else begin |
614 |
|
SubQuery := TIBSQL.Create(self); |
615 |
|
try |
627 |
|
Flds := ''; |
628 |
|
while (not SubQuery.EOF) and (SubQuery.Next <> nil) do |
629 |
|
begin |
630 |
+ |
fn := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize} |
631 |
+ |
aField := GetFieldDefFromAlias(fn); |
632 |
+ |
if assigned(aField) then |
633 |
+ |
fn := aField.Name; |
634 |
|
if (Flds = '') then |
635 |
< |
Flds := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize} |
635 |
> |
Flds := fn |
636 |
|
else begin |
637 |
|
Query.Next; |
638 |
< |
Flds := Flds + ';' + TrimRight(SubQuery.Current[0].AsString); |
638 |
> |
Flds := Flds + ';' + fn; |
639 |
|
end; |
640 |
|
end; |
641 |
|
Fields := Flds; |
719 |
|
FieldList := FieldList + |
720 |
|
QuoteIdentifier(DataBase.SQLDialect, Name) + |
721 |
|
' CHAR(' + IntToStr(Size) + ')'; {do not localize} |
722 |
< |
ftBoolean, ftSmallint, ftWord: |
722 |
> |
ftBoolean: |
723 |
> |
FieldList := FieldList + |
724 |
> |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
725 |
> |
' BOOLEAN'; {do not localize} |
726 |
> |
ftSmallint, ftWord: |
727 |
|
FieldList := FieldList + |
728 |
|
QuoteIdentifier(DataBase.SQLDialect, Name) + |
729 |
|
' SMALLINT'; {do not localize} |
1239 |
|
SQL: TStrings; |
1240 |
|
OrderByStr: string; |
1241 |
|
bWhereClausePresent: Boolean; |
1242 |
+ |
fn: string; |
1243 |
+ |
aField: TFieldDef; |
1244 |
|
begin |
1245 |
|
bWhereClausePresent := False; |
1246 |
|
Database.CheckActive; |
1276 |
|
begin |
1277 |
|
if i > 0 then |
1278 |
|
SQL.Text := SQL.Text + 'AND '; |
1279 |
+ |
aField := FieldDefs.Find(FDetailFieldsList.Strings[i]); |
1280 |
+ |
if assigned(aField) then |
1281 |
+ |
fn := GetDBAliasName(aField.FieldNo) |
1282 |
+ |
else |
1283 |
+ |
fn := FDetailFieldsList.Strings[i]; {something wrong if we get here - but should word} |
1284 |
|
SQL.Text := SQL.Text + |
1285 |
< |
QuoteIdentifier(DataBase.SQLDialect, FDetailFieldsList.Strings[i]) + |
1339 |
< |
' = :' + |
1340 |
< |
QuoteIdentifier(DataBase.SQLDialect, FMasterFieldsList.Strings[i]); |
1285 |
> |
QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + FMasterFieldsList.Strings[i]; |
1286 |
|
end; |
1287 |
|
end; |
1288 |
|
if OrderByStr <> '' then |
1320 |
|
WhereAllFieldList := WhereAllFieldList + ' AND '; |
1321 |
|
end; |
1322 |
|
InsertFieldList := InsertFieldList + |
1323 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
1324 |
< |
InsertParamList := InsertParamList + ':' + |
1380 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
1323 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)); |
1324 |
> |
InsertParamList := InsertParamList + ':' + Name; |
1325 |
|
UpdateFieldList := UpdateFieldList + |
1326 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
1327 |
< |
' = :' + |
1384 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
1326 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + |
1327 |
> |
' = :' + Name; |
1328 |
|
if (DataType <> ftBlob) and (DataType <>ftMemo) then |
1329 |
|
WhereAllFieldList := WhereAllFieldList + |
1330 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + ' = :' + |
1388 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name);{do not localize} |
1330 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + ' = :' + Name; |
1331 |
|
end; |
1332 |
|
end; |
1333 |
|
end; |
1336 |
|
procedure GenerateWherePrimaryFieldList; |
1337 |
|
var |
1338 |
|
i: Integer; |
1339 |
< |
tmp: String; |
1339 |
> |
tmp, fn: String; |
1340 |
> |
aField: TFieldDef; |
1341 |
|
begin |
1342 |
|
i := 1; |
1343 |
|
while i <= Length(FPrimaryIndexFields) do |
1344 |
|
begin |
1345 |
|
tmp := ExtractFieldName(FPrimaryIndexFields, i); |
1346 |
+ |
aField := FieldDefs.Find(tmp); |
1347 |
+ |
if assigned(aField) then |
1348 |
+ |
fn := GetDBAliasName(aField.FieldNo) |
1349 |
+ |
else |
1350 |
+ |
fn := tmp; {something wrong if we get here - but will work in most cases} |
1351 |
|
tmp := |
1352 |
< |
QuoteIdentifier(DataBase.SQLDialect, tmp) + ' = :' + |
1352 |
> |
QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + |
1353 |
|
QuoteIdentifier(DataBase.SQLDialect, tmp);{do not localize} |
1354 |
|
if WherePrimaryFieldList <> '' then |
1355 |
|
WherePrimaryFieldList := |