234 |
|
GenerateUpdateSQL; |
235 |
|
FRegenerateSQL := False; |
236 |
|
end; |
237 |
+ |
{ writeln(SelectSQL.Text); |
238 |
+ |
writeln(InsertSQL.Text); |
239 |
+ |
writeln(DeleteSQL.Text); |
240 |
+ |
writeln(ModifySQL.Text); } |
241 |
|
SetParams; |
242 |
|
inherited InternalOpen; |
243 |
|
end; |
299 |
|
|
300 |
|
procedure TIBTable.InitFieldDefs; |
301 |
|
var |
302 |
< |
sqlscale: Integer; |
302 |
> |
DidActivate: Boolean; |
303 |
|
Query: TIBSQL; |
304 |
|
begin |
305 |
|
if FTableName = '' then IBError(ibxeNoTableName, [nil]); |
306 |
< |
if (InternalPrepared) then InternalInitFieldDefs else |
306 |
> |
if (InternalPrepared) then |
307 |
> |
InternalInitFieldDefs |
308 |
> |
else |
309 |
|
begin |
310 |
< |
Database.InternalTransaction.StartTransaction; |
310 |
> |
{Get the field defs from a simple query on the table} |
311 |
> |
ActivateConnection; |
312 |
|
Query := TIBSQL.Create(self); |
313 |
|
try |
314 |
< |
Query.GoToFirstRecordOnExecute := False; |
315 |
< |
Query.Database := DataBase; |
316 |
< |
Query.Transaction := Database.InternalTransaction; |
317 |
< |
Query.SQL.Text := 'Select R.RDB$FIELD_NAME, R.RDB$FIELD_POSITION, ' + {do not localize} |
318 |
< |
'F.RDB$COMPUTED_BLR, F.RDB$DEFAULT_VALUE, ' + {do not localize} |
319 |
< |
'F.RDB$NULL_FLAG, ' + {do not localize} |
320 |
< |
'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: |
314 |
> |
Query.Database := DataBase; |
315 |
> |
Query.Transaction := Database.InternalTransaction; |
316 |
> |
DidActivate := false; |
317 |
> |
Query.SQL.Text := 'Select * from ' + QuoteIdentifier(DataBase.SQLDialect, FTableName); |
318 |
> |
with Query.Transaction do |
319 |
> |
begin |
320 |
> |
if not InTransaction then |
321 |
|
begin |
322 |
< |
sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize} |
323 |
< |
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; |
322 |
> |
StartTransaction; |
323 |
> |
DidActivate := true |
324 |
|
end; |
325 |
< |
blr_int64: |
326 |
< |
begin |
327 |
< |
sqlscale := Query.Current.ByName('RDB$FIELD_SCALE').AsInteger; {do not localize} |
328 |
< |
if (sqlscale = 0) then |
329 |
< |
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; |
325 |
> |
end; |
326 |
> |
Query.Prepare; |
327 |
> |
if DidActivate then |
328 |
> |
Query.Transaction.Rollback; |
329 |
> |
FieldDefsFromQuery(Query); |
330 |
|
finally |
331 |
< |
Query.free; |
414 |
< |
Database.InternalTransaction.Commit; |
331 |
> |
Query.Free; |
332 |
|
end; |
333 |
|
end; |
334 |
|
end; |
562 |
|
Opts: TIndexOptions; |
563 |
|
Flds: string; |
564 |
|
Query, SubQuery: TIBSQL; |
565 |
+ |
fn: string; |
566 |
+ |
aField: TFieldDef; |
567 |
|
begin |
568 |
|
if not (csReading in ComponentState) then begin |
569 |
|
if not Active and not FSwitchingIndex then |
596 |
|
if Query.Current.ByName('RDB$INDEX_TYPE').AsInteger = 2 then Include(Opts, ixDescending); {do not localize} |
597 |
|
Options := Opts; |
598 |
|
if (Query.Current.ByName('RDB$SEGMENT_COUNT').AsInteger = 1) then {do not localize} |
599 |
< |
Fields := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize} |
599 |
> |
begin |
600 |
> |
fn := Trim(Query.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize} |
601 |
> |
aField := GetFieldDefFromAlias(fn); |
602 |
> |
if assigned(aField) then |
603 |
> |
Fields := aField.Name |
604 |
> |
else |
605 |
> |
Fields := fn; |
606 |
> |
end |
607 |
|
else begin |
608 |
|
SubQuery := TIBSQL.Create(self); |
609 |
|
try |
621 |
|
Flds := ''; |
622 |
|
while (not SubQuery.EOF) and (SubQuery.Next <> nil) do |
623 |
|
begin |
624 |
+ |
fn := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString); {do not localize} |
625 |
+ |
aField := GetFieldDefFromAlias(fn); |
626 |
+ |
if assigned(aField) then |
627 |
+ |
fn := aField.Name; |
628 |
|
if (Flds = '') then |
629 |
< |
Flds := TrimRight(SubQuery.Current.ByName('RDB$FIELD_NAME').AsString) {do not localize} |
629 |
> |
Flds := fn |
630 |
|
else begin |
631 |
|
Query.Next; |
632 |
< |
Flds := Flds + ';' + TrimRight(SubQuery.Current[0].AsString); |
632 |
> |
Flds := Flds + ';' + fn; |
633 |
|
end; |
634 |
|
end; |
635 |
|
Fields := Flds; |
1229 |
|
SQL: TStrings; |
1230 |
|
OrderByStr: string; |
1231 |
|
bWhereClausePresent: Boolean; |
1232 |
+ |
fn: string; |
1233 |
+ |
aField: TFieldDef; |
1234 |
|
begin |
1235 |
|
bWhereClausePresent := False; |
1236 |
|
Database.CheckActive; |
1266 |
|
begin |
1267 |
|
if i > 0 then |
1268 |
|
SQL.Text := SQL.Text + 'AND '; |
1269 |
+ |
aField := FieldDefs.Find(FDetailFieldsList.Strings[i]); |
1270 |
+ |
if assigned(aField) then |
1271 |
+ |
fn := GetDBAliasName(aField.FieldNo) |
1272 |
+ |
else |
1273 |
+ |
fn := FDetailFieldsList.Strings[i]; {something wrong if we get here - but should word} |
1274 |
|
SQL.Text := SQL.Text + |
1275 |
< |
QuoteIdentifier(DataBase.SQLDialect, FDetailFieldsList.Strings[i]) + |
1339 |
< |
' = :' + |
1340 |
< |
QuoteIdentifier(DataBase.SQLDialect, FMasterFieldsList.Strings[i]); |
1275 |
> |
QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + FMasterFieldsList.Strings[i]; |
1276 |
|
end; |
1277 |
|
end; |
1278 |
|
if OrderByStr <> '' then |
1310 |
|
WhereAllFieldList := WhereAllFieldList + ' AND '; |
1311 |
|
end; |
1312 |
|
InsertFieldList := InsertFieldList + |
1313 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
1314 |
< |
InsertParamList := InsertParamList + ':' + |
1380 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
1313 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)); |
1314 |
> |
InsertParamList := InsertParamList + ':' + Name; |
1315 |
|
UpdateFieldList := UpdateFieldList + |
1316 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + |
1317 |
< |
' = :' + |
1384 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name); |
1316 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + |
1317 |
> |
' = :' + Name; |
1318 |
|
if (DataType <> ftBlob) and (DataType <>ftMemo) then |
1319 |
|
WhereAllFieldList := WhereAllFieldList + |
1320 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name) + ' = :' + |
1388 |
< |
QuoteIdentifier(DataBase.SQLDialect, Name);{do not localize} |
1320 |
> |
QuoteIdentifier(DataBase.SQLDialect, GetDBAliasName(FieldNo)) + ' = :' + Name; |
1321 |
|
end; |
1322 |
|
end; |
1323 |
|
end; |
1326 |
|
procedure GenerateWherePrimaryFieldList; |
1327 |
|
var |
1328 |
|
i: Integer; |
1329 |
< |
tmp: String; |
1329 |
> |
tmp, fn: String; |
1330 |
> |
aField: TFieldDef; |
1331 |
|
begin |
1332 |
|
i := 1; |
1333 |
|
while i <= Length(FPrimaryIndexFields) do |
1334 |
|
begin |
1335 |
|
tmp := ExtractFieldName(FPrimaryIndexFields, i); |
1336 |
+ |
aField := FieldDefs.Find(tmp); |
1337 |
+ |
if assigned(aField) then |
1338 |
+ |
fn := GetDBAliasName(aField.FieldNo) |
1339 |
+ |
else |
1340 |
+ |
fn := tmp; {something wrong if we get here - but will work in most cases} |
1341 |
|
tmp := |
1342 |
< |
QuoteIdentifier(DataBase.SQLDialect, tmp) + ' = :' + |
1342 |
> |
QuoteIdentifier(DataBase.SQLDialect, fn) + ' = :' + |
1343 |
|
QuoteIdentifier(DataBase.SQLDialect, tmp);{do not localize} |
1344 |
|
if WherePrimaryFieldList <> '' then |
1345 |
|
WherePrimaryFieldList := |
1465 |
|
end; |
1466 |
|
end; |
1467 |
|
|
1468 |
< |
end. |
1468 |
> |
end. |