32 |
|
{$IFDEF FPC} |
33 |
|
{$mode delphi} |
34 |
|
{$interfaces COM} |
35 |
+ |
{$define HASREQEX} |
36 |
|
{$ENDIF} |
37 |
|
|
38 |
|
interface |
55 |
|
private |
56 |
|
FDPB: IDPB; |
57 |
|
FFirebirdAPI: IFirebirdAPI; |
58 |
+ |
FODSMajorVersion: integer; |
59 |
+ |
FODSMinorVersion: integer; |
60 |
|
FUserCharSetMap: array of TCharSetMap; |
61 |
+ |
FSecDatabase: AnsiString; |
62 |
|
protected |
63 |
|
FDatabaseName: AnsiString; |
64 |
|
FRaiseExceptionOnConnectError: boolean; |
66 |
|
FHasDefaultCharSet: boolean; |
67 |
|
FCharSetID: integer; |
68 |
|
FCodePage: TSystemCodePage; |
69 |
+ |
FRemoteProtocol: AnsiString; |
70 |
+ |
FAuthMethod: AnsiString; |
71 |
|
constructor Create(DatabaseName: AnsiString; DPB: IDPB; |
72 |
|
RaiseExceptionOnConnectError: boolean); |
73 |
|
procedure CheckHandle; virtual; abstract; |
74 |
|
function GenerateCreateDatabaseSQL(DatabaseName: AnsiString; aDPB: IDPB): AnsiString; |
75 |
+ |
procedure GetODSAndConnectionInfo; |
76 |
+ |
function GetDBInfo(ReqBuffer: PByte; ReqBufLen: integer): IDBInformation; virtual; abstract; |
77 |
+ |
function IsConnected: boolean; virtual; abstract; |
78 |
|
procedure EndAllTransactions; |
79 |
+ |
procedure DPBFromCreateSQL(CreateSQL: AnsiString); |
80 |
|
procedure SetParameters(SQLParams: ISQLParams; params: array of const); |
81 |
|
public |
82 |
|
destructor Destroy; override; |
83 |
|
function getDPB: IDPB; |
84 |
|
function AllocateBPB: IBPB; |
85 |
+ |
function AllocateDIRB: IDIRB; |
86 |
|
function StartTransaction(TPB: array of byte; DefaultCompletion: TTransactionCompletion): ITransaction; overload; virtual; abstract; |
87 |
|
function StartTransaction(TPB: ITPB; DefaultCompletion: TTransactionCompletion): ITransaction; overload; virtual; abstract; |
88 |
|
procedure Disconnect(Force: boolean=false); virtual; abstract; |
124 |
|
property SQLDialect: integer read FSQLDialect; |
125 |
|
property DPB: IDPB read FDPB; |
126 |
|
public |
127 |
< |
{Character Sets} |
127 |
> |
function GetDBInformation(Requests: array of byte): IDBInformation; overload; |
128 |
> |
function GetDBInformation(Request: byte): IDBInformation; overload; |
129 |
> |
function GetDBInformation(Requests: IDIRB): IDBInformation; overload; |
130 |
> |
function GetConnectString: AnsiString; |
131 |
> |
function GetRemoteProtocol: AnsiString; |
132 |
> |
function GetAuthenticationMethod: AnsiString; |
133 |
> |
function GetSecurityDatabase: AnsiString; |
134 |
> |
function GetODSMajorVersion: integer; |
135 |
> |
function GetODSMinorVersion: integer; |
136 |
> |
{Character Sets} |
137 |
|
function HasDefaultCharSet: boolean; |
138 |
|
function GetDefaultCharSetID: integer; |
139 |
|
function GetCharsetName(CharSetID: integer): AnsiString; |
149 |
|
|
150 |
|
implementation |
151 |
|
|
152 |
< |
uses FBMessages, FBTransaction; |
152 |
> |
uses FBMessages, IBUtils, FBTransaction {$IFDEF HASREQEX}, RegExpr{$ENDIF}; |
153 |
|
|
154 |
|
const |
155 |
|
CharSetMap: array [0..69] of TCharsetMap = ( |
230 |
|
|
231 |
|
{ TFBAttachment } |
232 |
|
|
233 |
+ |
procedure TFBAttachment.GetODSAndConnectionInfo; |
234 |
+ |
var DBInfo: IDBInformation; |
235 |
+ |
i: integer; |
236 |
+ |
Stmt: IStatement; |
237 |
+ |
ResultSet: IResultSet; |
238 |
+ |
Param: IDPBItem; |
239 |
+ |
begin |
240 |
+ |
if not IsConnected then Exit; |
241 |
+ |
DBInfo := GetDBInformation([isc_info_db_id,isc_info_ods_version,isc_info_ods_minor_version, |
242 |
+ |
isc_info_db_SQL_Dialect]); |
243 |
+ |
for i := 0 to DBInfo.GetCount - 1 do |
244 |
+ |
with DBInfo[i] do |
245 |
+ |
case getItemType of |
246 |
+ |
isc_info_ods_minor_version: |
247 |
+ |
FODSMinorVersion := getAsInteger; |
248 |
+ |
isc_info_ods_version: |
249 |
+ |
FODSMajorVersion := getAsInteger; |
250 |
+ |
isc_info_db_SQL_Dialect: |
251 |
+ |
FSQLDialect := getAsInteger; |
252 |
+ |
end; |
253 |
+ |
|
254 |
+ |
FCharSetID := 0; |
255 |
+ |
FRemoteProtocol := ''; |
256 |
+ |
FAuthMethod := 'Legacy_Auth'; |
257 |
+ |
FSecDatabase := 'Default'; |
258 |
+ |
if FODSMajorVersion > 11 then |
259 |
+ |
begin |
260 |
+ |
Stmt := Prepare(StartTransaction([isc_tpb_read,isc_tpb_nowait,isc_tpb_concurrency],taCommit), |
261 |
+ |
'Select MON$CHARACTER_SET_ID, MON$REMOTE_PROTOCOL, MON$AUTH_METHOD, MON$SEC_DATABASE From MON$ATTACHMENTS, MON$DATABASE '+ |
262 |
+ |
'Where MON$ATTACHMENT_ID = CURRENT_CONNECTION'); |
263 |
+ |
ResultSet := Stmt.OpenCursor; |
264 |
+ |
if ResultSet.FetchNext then |
265 |
+ |
begin |
266 |
+ |
FCharSetID := ResultSet[0].AsInteger; |
267 |
+ |
FRemoteProtocol := Trim(ResultSet[1].AsString); |
268 |
+ |
FAuthMethod := Trim(ResultSet[2].AsString); |
269 |
+ |
FSecDatabase := Trim(ResultSet[3].AsString); |
270 |
+ |
end |
271 |
+ |
end |
272 |
+ |
else |
273 |
+ |
if (FODSMajorVersion = 11) and (FODSMinorVersion >= 1) then |
274 |
+ |
begin |
275 |
+ |
Stmt := Prepare(StartTransaction([isc_tpb_read,isc_tpb_nowait,isc_tpb_concurrency],taCommit), |
276 |
+ |
'Select MON$CHARACTER_SET_ID, MON$REMOTE_PROTOCOL From MON$ATTACHMENTS '+ |
277 |
+ |
'Where MON$ATTACHMENT_ID = CURRENT_CONNECTION'); |
278 |
+ |
ResultSet := Stmt.OpenCursor; |
279 |
+ |
if ResultSet.FetchNext then |
280 |
+ |
begin |
281 |
+ |
FCharSetID := ResultSet[0].AsInteger; |
282 |
+ |
FRemoteProtocol := Trim(ResultSet[1].AsString); |
283 |
+ |
end |
284 |
+ |
end |
285 |
+ |
else |
286 |
+ |
if DPB <> nil then |
287 |
+ |
begin |
288 |
+ |
Param := DPB.Find(isc_dpb_lc_ctype); |
289 |
+ |
if (Param = nil) or not CharSetName2CharSetID(Param.AsString,FCharSetID) then |
290 |
+ |
FCharSetID := 0; |
291 |
+ |
case GetProtocol(FDatabaseName) of |
292 |
+ |
TCP: FRemoteProtocol := 'TCPv4'; |
293 |
+ |
Local: FRemoteProtocol := ''; |
294 |
+ |
NamedPipe: FRemoteProtocol := 'Netbui'; |
295 |
+ |
SPX: FRemoteProtocol := 'SPX' |
296 |
+ |
end; |
297 |
+ |
end; |
298 |
+ |
FHasDefaultCharSet := CharSetID2CodePage(FCharSetID,FCodePage) and (FCharSetID > 1); |
299 |
+ |
end; |
300 |
+ |
|
301 |
|
constructor TFBAttachment.Create(DatabaseName: AnsiString; DPB: IDPB; |
302 |
|
RaiseExceptionOnConnectError: boolean); |
303 |
|
begin |
308 |
|
FDPB := DPB; |
309 |
|
SetLength(FUserCharSetMap,0); |
310 |
|
FRaiseExceptionOnConnectError := RaiseExceptionOnConnectError; |
311 |
+ |
FODSMajorVersion := 0; |
312 |
+ |
FODSMinorVersion := 0; |
313 |
|
end; |
314 |
|
|
315 |
|
function TFBAttachment.GenerateCreateDatabaseSQL(DatabaseName: AnsiString; aDPB: IDPB): AnsiString; |
356 |
|
end; |
357 |
|
end; |
358 |
|
|
359 |
+ |
{$IFDEF HASREQEX} |
360 |
+ |
procedure TFBAttachment.DPBFromCreateSQL(CreateSQL: AnsiString); |
361 |
+ |
var RegexObj: TRegExpr; |
362 |
+ |
begin |
363 |
+ |
FDPB := FFirebirdAPI.AllocateDPB; |
364 |
+ |
RegexObj := TRegExpr.Create; |
365 |
+ |
try |
366 |
+ |
{extact database file spec} |
367 |
+ |
RegexObj.ModifierG := false; {turn off greedy matches} |
368 |
+ |
RegexObj.ModifierI := true; {case insensitive match} |
369 |
+ |
RegexObj.Expression := '^ *CREATE +(DATABASE|SCHEMA) +''.*'' +USER +''(.+)'' PASSWORD +''(.+)'''; |
370 |
+ |
if RegexObj.Exec(CreateSQL) then |
371 |
+ |
begin |
372 |
+ |
DPB.Add(isc_dpb_user_name).AsString := system.copy(CreateSQL,RegexObj.MatchPos[2],RegexObj.MatchLen[2]); |
373 |
+ |
DPB.Add(isc_dpb_password).AsString := system.copy(CreateSQL,RegexObj.MatchPos[3],RegexObj.MatchLen[3]); |
374 |
+ |
end |
375 |
+ |
else |
376 |
+ |
begin |
377 |
+ |
RegexObj.Expression := '^ *CREATE +(DATABASE|SCHEMA) +(''.*'') +USER +''(.+)'''; |
378 |
+ |
if RegexObj.Exec(CreateSQL) then |
379 |
+ |
DPB.Add(isc_dpb_user_name).AsString := system.copy(CreateSQL,RegexObj.MatchPos[2],RegexObj.MatchLen[2]); |
380 |
+ |
end; |
381 |
+ |
finally |
382 |
+ |
RegexObj.Free; |
383 |
+ |
end; |
384 |
+ |
if FCharSetID > 0 then |
385 |
+ |
DPB.Add(isc_dpb_lc_ctype).AsString := GetCharSetName(FCharSetID); |
386 |
+ |
DPB.Add(isc_dpb_set_db_SQL_dialect).setAsByte(FSQLDialect); |
387 |
+ |
end; |
388 |
+ |
{$ELSE} |
389 |
+ |
procedure TFBAttachment.DPBFromCreateSQL(CreateSQL: AnsiString); |
390 |
+ |
begin |
391 |
+ |
FDPB := FFirebirdAPI.AllocateDPB; |
392 |
+ |
if FCharSetID > 0 then |
393 |
+ |
DPB.Add(isc_dpb_lc_ctype).AsString := GetCharSetName(FCharSetID); |
394 |
+ |
DPB.Add(isc_dpb_set_db_SQL_dialect).setAsByte(FSQLDialect); |
395 |
+ |
end; |
396 |
+ |
{$ENDIF} |
397 |
+ |
|
398 |
|
procedure TFBAttachment.SetParameters(SQLParams: ISQLParams; |
399 |
|
params: array of const); |
400 |
|
var i: integer; |
459 |
|
Result := TBPB.Create; |
460 |
|
end; |
461 |
|
|
462 |
+ |
function TFBAttachment.AllocateDIRB: IDIRB; |
463 |
+ |
begin |
464 |
+ |
Result := TDIRB.Create; |
465 |
+ |
end; |
466 |
+ |
|
467 |
|
procedure TFBAttachment.ExecImmediate(TPB: array of byte; sql: AnsiString; |
468 |
|
aSQLDialect: integer); |
469 |
|
begin |
483 |
|
function TFBAttachment.ExecuteSQL(TPB: array of byte; sql: AnsiString; |
484 |
|
SQLDialect: integer; params: array of const): IResults; |
485 |
|
begin |
486 |
< |
Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,FSQLDialect,params); |
486 |
> |
Result := ExecuteSQL(StartTransaction(TPB,taCommit),sql,SQLDialect,params); |
487 |
|
end; |
488 |
|
|
489 |
|
function TFBAttachment.ExecuteSQL(transaction: ITransaction; sql: AnsiString; |
612 |
|
Result := OpenBlob(Transaction,Field.GetBlobMetadata, Field.AsQuad,BPB); |
613 |
|
end; |
614 |
|
|
615 |
+ |
function TFBAttachment.GetDBInformation(Requests: array of byte |
616 |
+ |
): IDBInformation; |
617 |
+ |
var ReqBuffer: PByte; |
618 |
+ |
i: integer; |
619 |
+ |
begin |
620 |
+ |
CheckHandle; |
621 |
+ |
if Length(Requests) = 1 then |
622 |
+ |
Result := GetDBInformation(Requests[0]) |
623 |
+ |
else |
624 |
+ |
begin |
625 |
+ |
GetMem(ReqBuffer,Length(Requests)); |
626 |
+ |
try |
627 |
+ |
for i := 0 to Length(Requests) - 1 do |
628 |
+ |
ReqBuffer[i] := Requests[i]; |
629 |
+ |
|
630 |
+ |
Result := GetDBInfo(ReqBuffer,Length(Requests)); |
631 |
+ |
|
632 |
+ |
finally |
633 |
+ |
FreeMem(ReqBuffer); |
634 |
+ |
end; |
635 |
+ |
end; |
636 |
+ |
end; |
637 |
+ |
|
638 |
+ |
function TFBAttachment.GetDBInformation(Request: byte): IDBInformation; |
639 |
+ |
begin |
640 |
+ |
CheckHandle; |
641 |
+ |
Result := GetDBInfo(@Request,1); |
642 |
+ |
end; |
643 |
+ |
|
644 |
+ |
function TFBAttachment.GetDBInformation(Requests: IDIRB): IDBInformation; |
645 |
+ |
begin |
646 |
+ |
CheckHandle; |
647 |
+ |
with Requests as TDIRB do |
648 |
+ |
Result := GetDBInfo(getBuffer,getDataLength); |
649 |
+ |
end; |
650 |
+ |
|
651 |
+ |
function TFBAttachment.GetConnectString: AnsiString; |
652 |
+ |
begin |
653 |
+ |
Result := FDatabaseName; |
654 |
+ |
end; |
655 |
+ |
|
656 |
+ |
function TFBAttachment.GetRemoteProtocol: AnsiString; |
657 |
+ |
begin |
658 |
+ |
Result := FRemoteProtocol; |
659 |
+ |
end; |
660 |
+ |
|
661 |
+ |
function TFBAttachment.GetAuthenticationMethod: AnsiString; |
662 |
+ |
begin |
663 |
+ |
Result := FAuthMethod; |
664 |
+ |
end; |
665 |
+ |
|
666 |
+ |
function TFBAttachment.GetSecurityDatabase: AnsiString; |
667 |
+ |
begin |
668 |
+ |
Result := FSecDatabase; |
669 |
+ |
end; |
670 |
+ |
|
671 |
+ |
function TFBAttachment.GetODSMajorVersion: integer; |
672 |
+ |
begin |
673 |
+ |
Result := FODSMajorVersion; |
674 |
+ |
end; |
675 |
+ |
|
676 |
+ |
function TFBAttachment.GetODSMinorVersion: integer; |
677 |
+ |
begin |
678 |
+ |
Result := FODSMinorVersion; |
679 |
+ |
end; |
680 |
+ |
|
681 |
|
function TFBAttachment.HasDefaultCharSet: boolean; |
682 |
|
begin |
683 |
|
Result := FHasDefaultCharSet |
757 |
|
begin |
758 |
|
Result := false; |
759 |
|
for i := Low(CharSetMap) to High(CharSetMap) do |
760 |
< |
if AnsiCompareStr(CharSetMap[i].CharSetName, CharSetName) = 0 then |
760 |
> |
if AnsiCompareText(CharSetMap[i].CharSetName, CharSetName) = 0 then |
761 |
|
begin |
762 |
|
CharSetID := CharSetMap[i].CharSetID; |
763 |
|
Result := true; |
765 |
|
end; |
766 |
|
|
767 |
|
for i := 0 to Length(FUserCharSetMap) - 1 do |
768 |
< |
if AnsiCompareStr(FUserCharSetMap[i].CharSetName, CharSetName) = 0 then |
768 |
> |
if AnsiCompareText(FUserCharSetMap[i].CharSetName, CharSetName) = 0 then |
769 |
|
begin |
770 |
|
CharSetID := FUserCharSetMap[i].CharSetID; |
771 |
|
Result := true; |