PageRenderTime 66ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/EndTask/2008.04.16 ?????/Oracle Data Access Components(ODAC) v6/Source/OraClasses.pas

http://xinhaining-dianjianyiqi-tongxunchengxu.googlecode.com/
Pascal | 2090 lines | 1557 code | 330 blank | 203 comment | 78 complexity | 9d5a9360d91c8ba693baba823cef1d02 MD5 | raw file
Possible License(s): GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. //////////////////////////////////////////////////
  2. // Oracle Data Access Components
  3. // Copyright Š 1998-2006 Core Lab. All right reserved.
  4. // Oracle Classes
  5. // Created: 01.03.98
  6. // Last modified: 16.03.04
  7. //////////////////////////////////////////////////
  8. {$IFNDEF CLR}
  9. {$I Odac.inc}
  10. unit OraClasses;
  11. {$ENDIF}
  12. {$J+}
  13. {$DEFINE _LOCAL_ERROR_HANDLE} // LOCAL_ERROR_HANDLE
  14. {$IFDEF VER6P}
  15. {$WARN SYMBOL_PLATFORM OFF}
  16. {$ENDIF}
  17. interface
  18. uses
  19. {$IFDEF MSWINDOWS}
  20. Windows, Messages,
  21. {$ENDIF}
  22. {$IFDEF CLR}
  23. Variants, WinUtils,
  24. {$ELSE}
  25. CLRClasses,
  26. {$ENDIF}
  27. {$IFDEF LITE}
  28. FMTBcd,
  29. {$ENDIF}
  30. SysUtils, Classes, SyncObjs, MemData, OraCall, OraError, CRAccess, MemUtils;
  31. const
  32. dtRowId = 100;
  33. dtCursor = 101;
  34. dtOraBlob = 102;
  35. dtOraClob = 103;
  36. dtBFILE = 104;
  37. dtCFILE = 105;
  38. dtLabel = 106; // MLSLABEL
  39. dtFixedChar = 107;
  40. dtUndefined = 108;
  41. dtTimeStamp = 109;
  42. dtTimeStampTZ = 110;
  43. dtTimeStampLTZ = 111;
  44. dtIntervalYM = 112;
  45. dtIntervalDS = 113;
  46. dtURowId = 114;
  47. dtNumber = 115;
  48. dtXML = 116;
  49. dtFixedWideChar = 117;
  50. dtBFloat = 118;
  51. dtBDouble = 119;
  52. dtNString = 120;
  53. dtNWideString = 121;
  54. dtNClob = 122;
  55. // obsolete
  56. dtBLOBLocator = dtOraBlob;
  57. dtCLOBLocator = dtOraClob;
  58. // Props
  59. prNonBlocking = 50; // bool
  60. prThreadSafety = 51; // bool
  61. prAutoClose = 55; // bool
  62. prErrorOffset = 57; // word
  63. prMaxStringSize = 58; // word
  64. prFieldsAsString = 59; // bool
  65. prDateFormat = 60; // string
  66. prDeferredLobRead = 61; // bool
  67. prConnectMode = 62; // enum
  68. prCharLength = 63; // word
  69. prCacheLobs = 64; // bool
  70. prEnableIntegers = 65; // bool
  71. prInternalName = 66; // string
  72. prScrollableCursor = 67; // bool
  73. prStoreRowId = 68; // bool
  74. prCharset = 69; // word
  75. prDateLanguage = 70; // string
  76. prTimeStampFormat = 71; // string
  77. prTimeStampTZFormat = 72; // string
  78. prRawAsString = 73; // bool
  79. prNumberAsString = 74; // bool
  80. prNumericCharacters = 75; // string
  81. prEnableNumbers = 76; // bool
  82. prUseUnicode = 77; // bool
  83. prIntegerPrecision = 78; // word;
  84. prFloatPrecision = 79; // word;
  85. prTemporaryLobUpdate= 82; // bool
  86. prDisconnectMode = 84; // bool
  87. prInactiveTimeout = 85; // integer
  88. prResumeTimeout = 86; // integer
  89. prGlobalCoordinator = 87; // integer
  90. prTransactionName = 88; // string
  91. prIsolationLevel = 89; // integer
  92. prDefaultCloseAction= 90; // integer
  93. prConnectionTimeOut = 91; // integer
  94. prHasObjectFields = 92; // bool
  95. prStatementCache = 93; // bool
  96. prStatementCacheSize= 94; // integer
  97. prEnabled = 95; // bool
  98. prTimeout = 96; // integer
  99. prPersistent = 97; // bool
  100. prOperations = 98; // set
  101. RowIdSize = 18;
  102. MaxBlobSize: longint = 2147483647;
  103. MaxTransactionIdLength = 64; // Maximum length for TransactionId and BranchQualifier
  104. type
  105. TOraCursor = class;
  106. TOraLob = class;
  107. TOraFile = class;
  108. TOraTimeStamp = class;
  109. TOraInterval = class;
  110. TOraNumber = class;
  111. TOraParamDesc = class;
  112. TOCICommand = class;
  113. {$IFNDEF LITE}
  114. TOCITransaction = class;
  115. {$ENDIF}
  116. { OraAccess level }
  117. TTransactionMode = (tmReadOnly, tmReadWrite, tmReadCommitted, tmSerializable);
  118. TErrorProc = procedure (E: EOraError; var Fail: boolean) of object;
  119. TConnectMode = (cmNormal, cmSysOper, cmSysDBA);
  120. { TOraParamDesc }
  121. TOraParamDesc = class (TParamDesc)
  122. private
  123. FValue: IntPtr;
  124. FActualLengthPtr: IntPtr;
  125. FDefIndicator: IntPtr;
  126. FIndicator: IntPtr;
  127. FTable: boolean;
  128. FLength: integer; // Table Length
  129. FHandle: IntPtr;
  130. FBindBufferSize: integer;
  131. FBlobPiece: integer; // number of piece
  132. FQuotedName: boolean;
  133. FLen: integer;
  134. FTableIndicator: boolean;
  135. FNational : boolean;
  136. FHasDefault: boolean;
  137. function GetActualLength: integer;
  138. procedure SetActualLength(Value: integer);
  139. property ActualLength: integer read GetActualLength write SetActualLength;
  140. protected
  141. procedure AllocBuffer;
  142. procedure FreeBuffer;
  143. procedure CheckRange(Index: integer);
  144. property Name;
  145. property DataType;
  146. property ParamType;
  147. property Size;
  148. procedure ClearBindData;
  149. public
  150. constructor Create; override;
  151. destructor Destroy; override;
  152. procedure SetDataType(Value: word); override;
  153. procedure SetSize(Value: integer); override;
  154. procedure SetTable(Value: boolean);
  155. procedure SetLength(Value: integer);
  156. procedure SetNational(Value: boolean);
  157. procedure SetHasDefault(Value: boolean);
  158. // TEMP for describe
  159. function GetSize: integer;
  160. function GetTable: boolean;
  161. function GetLength: integer;
  162. function GetNational: boolean;
  163. function GetHasDefault: boolean;
  164. function GetIndicator(Index: integer): smallint;
  165. procedure SetIndicator(Index: integer; Value: smallint);
  166. function ValuePtr: IntPtr;
  167. procedure SetValuePtr(Buf: IntPtr);
  168. function IndicatorPtr: IntPtr;
  169. procedure SyncIndicator;
  170. function GetItemAsDateTime(Index: integer): TDateTime;
  171. procedure SetItemAsDateTime(Index: integer; Value: TDateTime);
  172. function GetItemAsFloat(Index: integer): double;
  173. procedure SetItemAsFloat(Index: integer; Value: double);
  174. function GetItemAsInteger(Index: integer): integer;
  175. procedure SetItemAsInteger(Index: integer; Value: integer);
  176. function GetItemAsLargeInt(Index: integer): Int64;
  177. procedure SetItemAsLargeInt(Index: integer; Value: Int64);
  178. function GetItemAsString(Index: integer): string;
  179. procedure SetItemAsString(Index: integer; Value: string);
  180. function GetItemAsWideString(Index: integer): WideString;
  181. procedure SetItemAsWideString(Index: integer; Value: WideString);
  182. function GetItemAsBoolean(Index: integer): boolean;
  183. procedure SetItemAsBoolean(Index: integer; Value: boolean);
  184. procedure SetItemAsObject(Index: integer; Value: TSharedObject);
  185. function GetItemAsObject(Index: integer): TSharedObject;
  186. function GetItemAsVariant(Index: integer): variant;
  187. procedure SetItemAsVariant(Index: integer; const Value: variant);
  188. function GetValue: variant; override;
  189. procedure SetValue(const Value: variant); override;
  190. function GetAsBlobRef: TBlob;
  191. function GetAsCursor: TOraCursor;
  192. function GetAsOraBlob: TOraLob;
  193. function GetAsBFile: TOraFile;
  194. function GetAsTimeStamp: TOraTimeStamp;
  195. function GetAsInterval: TOraInterval;
  196. function GetAsNumber: TOraNumber;
  197. function GetObject: TSharedObject; override;
  198. procedure SetObject(Value: TSharedObject); override;
  199. function GetNull: boolean; override;
  200. procedure SetNull(const Value: boolean); override;
  201. function GetItemNull(Index: integer): boolean;
  202. procedure SetItemNull(Index: integer; Value: boolean);
  203. end;
  204. { TOCIConnection }
  205. TRunMethod = procedure of object;
  206. TEndMethod = procedure(E: Exception) of object;
  207. TMethodDesc = class
  208. public
  209. RunMethod : TRunMethod;
  210. EndMethod : TEndMethod;
  211. {$IFDEF MSWINDOWS}
  212. hWindow :HWND;
  213. {$ENDIF}
  214. end;
  215. {$IFDEF LINUX}
  216. THandle = integer;
  217. {$ENDIF}
  218. TNlsParamType = (nlsDateLanguage, nlsDateFormat, nlsNumericCharacters, nlsTimeStampFormat,
  219. nlsTimeStampTZFormat);
  220. TNlsSessionParam = record
  221. Name: string;
  222. Value: string;
  223. IsUserDefined: boolean;
  224. end;
  225. TFailoverCallback = procedure (FailoverState: cardinal; FailoverType: cardinal;
  226. var Retry: boolean) of object;
  227. TConnectionType = (ctDefault, ctOCIPooled{$IFDEF MSWINDOWS}{$IFNDEF LITE}, ctMTSPooled{$ENDIF}{$ENDIF});
  228. TOCIConnection = class (TCRConnection)
  229. private
  230. FThreadSafety: boolean;
  231. FMaxStringSize: word;
  232. FOCICallStyle: TOCICallStyle;
  233. FOCICallStyleCommand: TOCICallStyle;
  234. FNativeHandle: boolean;
  235. FLastError: integer;
  236. FConnectMode: TConnectMode;
  237. FEnableIntegers: boolean;
  238. FEnableNumbers: boolean;
  239. FInternalName: string;
  240. FCommand: TOCICommand;
  241. FOracleVersionSt: string;
  242. FOracleVersion: word;
  243. FProxyConnection : TOCIConnection;
  244. FDisconnectMode: boolean;
  245. FConnectionTimeout: integer;
  246. {$IFNDEF LITE}
  247. FTransaction: TOCITransaction;
  248. {$ENDIF}
  249. FOCIPoolName: string;
  250. FStatementCache: boolean;
  251. FStatementCacheSize: integer;
  252. { Charset parameters }
  253. FCharset: string;
  254. FCharsetId: word;
  255. FCharLength: word;
  256. FQueryCharLength: boolean;
  257. FUseUnicode: boolean;
  258. { NLS session parameters }
  259. FNlsParams: array[TNlsParamType] of TNlsSessionParam;
  260. { OCI73 }
  261. LDA: PLDA;
  262. HDA: PHDA;
  263. { OCI80 }
  264. hSvcCtx : pOCISvcCtx;
  265. hServer : pOCIServer;
  266. hSession : pOCISession;
  267. hOCIError : pOCIError; // local error handle
  268. hOCIEnv : pOCIEnv;
  269. hOCIAuthInfo : pOCIAuthInfo;
  270. //hTrans : pOCITrans;
  271. FConnectionType: TConnectionType;
  272. {$IFDEF MSWINDOWS}
  273. hBusy: THandle;
  274. hWindow :HWND;
  275. {$ENDIF}
  276. procedure CheckCommand;
  277. procedure GetSessionParameters;
  278. procedure SetNlsParameter(const Name, Value: string);
  279. function GetMaxStringSize: word;
  280. protected
  281. FOnFailover: TFailoverCallback;
  282. FInTransaction: boolean;
  283. procedure DoError(E: Exception; var Fail: boolean); override;
  284. procedure SetStatementCacheSize(Size: integer);
  285. property AutoCommit;
  286. public
  287. constructor Create; override;
  288. destructor Destroy; override;
  289. procedure CheckOCI;
  290. procedure CheckOCI73;
  291. procedure CheckOCI80;
  292. procedure Check(Status: sword);
  293. procedure OraError(FOCICallStyle: TOCICallStyle; var ErrorCode: sword; UseCallback: boolean; Component: TObject);
  294. procedure SetConnectionType(ConnectionType: TConnectionType);
  295. procedure Connect(const ConnectString: string); override;
  296. procedure Disconnect; override;
  297. function GetOracleVersionSt: string;
  298. function GetOracleVersion: word;
  299. { Transaction control }
  300. procedure StartTransaction; override; // (Mode: TTransactionMode);
  301. procedure Commit; override;
  302. procedure Rollback; override;
  303. procedure BreakExec;
  304. { Multi Thread }
  305. procedure Busy;
  306. procedure BusyWait;
  307. procedure Release;
  308. function RunThread(RunMethod: TRunMethod; EndMethod: TEndMethod): TThread;
  309. function StopThread(var hThread: TThread{$IFDEF MSWINDOWS}; APeekMessage: boolean = False{$ENDIF}): boolean;
  310. {$IFDEF MSWINDOWS}
  311. procedure AllocWnd;
  312. {$ENDIF}
  313. { OCI73 }
  314. function GetLDA: PLDA;
  315. procedure SetLDA(Value: PLDA);
  316. { OCI80 }
  317. function GetSvcCtx: pOCISvcCtx;
  318. procedure SetSvcCtx(Value: pOCISvcCtx);
  319. {$IFDEF MSWINDOWS}
  320. {$IFNDEF LITE}
  321. procedure GetMTSSvcCtx(var OCISvcCtx: pOCISvcCtx);
  322. {$ENDIF}
  323. {$ENDIF}
  324. procedure ChangePassword(NewPassword: string);
  325. procedure AssignConnect(Source: TOCIConnection);
  326. procedure SetNonBlocking(Value: boolean); // nonblocking connection
  327. function GetOCICallStyle: TOCICallStyle;
  328. procedure SetOCICallStyle(Value: TOCICallStyle);
  329. function GetOCICallStyleCommand: TOCICallStyle;
  330. function GetLastError: integer;
  331. procedure SetLastError(Value: integer);
  332. procedure GetTableFields(TableName: string; Fields: TStringList);
  333. function SetProp(Prop: integer; const Value: variant): boolean; override;
  334. function GetProp(Prop: integer; var Value: variant): boolean; override;
  335. function CheckIsValid: boolean; override;
  336. {$IFNDEF LITE}
  337. procedure ReturnToPool; override;
  338. {$ENDIF}
  339. property OnFailover: TFailoverCallback read FOnFailover write FOnFailover;
  340. property InTransaction: boolean read FInTransaction;
  341. property ProxyConnection : TOCIConnection read FProxyConnection write FProxyConnection;
  342. procedure SetClientIdentifier(const Value: string);
  343. {$IFNDEF LITE}
  344. property Transaction: TOCITransaction read FTransaction;
  345. {$ENDIF}
  346. end;
  347. { TCursor }
  348. TOraCursor = class (TSharedObject)
  349. private
  350. FCDA: PCDA;
  351. phOCIStmt: ppOCIStmt;
  352. hOCIError: pOCIError; // local error handle
  353. hOCIEnv: pOCIEnv;
  354. FState: TCursorState;
  355. FOCICallStyle: TOCICallStyle;
  356. FScrollable: boolean;
  357. FStatementCache: boolean;
  358. FPrefetchRows: integer;
  359. procedure DisablePrefetching;
  360. function GetCDA: PCDA;
  361. function GethOCIStmt: pOCIStmt;
  362. procedure SethOCIStmt(Value: pOCIStmt);
  363. function GetOCIStmt: pOCIStmt;
  364. function GetOCIStmtPtr: ppOCIStmt;
  365. procedure SetOCICallStyle(Value: TOCICallStyle);
  366. procedure SetPrefetchRows(Value: integer);
  367. property hOCIStmt: pOCIStmt read GethOCIStmt write SethOCIStmt;
  368. protected
  369. procedure CheckOCI;
  370. procedure CheckOCI73;
  371. procedure CheckOCI80;
  372. procedure InternalFreeCursor;
  373. public
  374. constructor Create;
  375. destructor Destroy; override;
  376. procedure AllocCursor(StatementCache: boolean = False);
  377. procedure FreeCursor;
  378. function CanFetch: boolean;
  379. property CDA: PCDA read GetCDA;
  380. property OCIStmt: pOCIStmt read GetOCIStmt;
  381. property OCIStmtPtr: ppOCIStmt read GetOCIStmtPtr;
  382. property State: TCursorState read FState write FState;
  383. property OCICallStyle: TOCICallStyle read FOCICallStyle write SetOCICallStyle;
  384. property PrefetchRows: integer read FPrefetchRows write SetPrefetchRows;
  385. end;
  386. {$IFDEF MSWINDOWS}
  387. { TOCIChangeNotification }
  388. TChangeNotifyEventType = (cneNone, cneStartup, cneShutdown, cneShutdownAny,
  389. cneDropDB, cneDereg, cneObjChange);
  390. TCustomNotifyChanges = class
  391. private
  392. function GetCount: integer;
  393. protected
  394. FItems: array of TObject;
  395. function CreateItem(ChangeDescriptor: IntPtr): TObject; virtual; abstract;
  396. public
  397. constructor Create(OCIColl: pOCIColl);
  398. destructor Destroy; override;
  399. property Count: integer read GetCount;
  400. end;
  401. TNotifyRowChange = class
  402. private
  403. FRowId: string;
  404. FOperations: TChangeNotifyOperations;
  405. public
  406. constructor Create(ChangeDescriptor: IntPtr);
  407. property RowId: string read FRowId;
  408. property Operations: TChangeNotifyOperations read FOperations;
  409. end;
  410. TNotifyRowChanges = class(TCustomNotifyChanges)
  411. private
  412. function GetChanges(Index: integer): TNotifyRowChange;
  413. protected
  414. function CreateItem(ChangeDescriptor: IntPtr): TObject; override;
  415. public
  416. property Changes[Index: integer]: TNotifyRowChange read GetChanges; default;
  417. end;
  418. TNotifyTableChange = class
  419. private
  420. FTableName: string;
  421. FOperations: TChangeNotifyOperations;
  422. FRowChanges: TNotifyRowChanges;
  423. public
  424. constructor Create(ChangeDescriptor: IntPtr);
  425. destructor Destroy; override;
  426. property TableName: string read FTableName;
  427. property Operations: TChangeNotifyOperations read FOperations;
  428. property RowChanges: TNotifyRowChanges read FRowChanges;
  429. end;
  430. TNotifyTableChanges = class(TCustomNotifyChanges)
  431. private
  432. function GetChanges(Index: integer): TNotifyTableChange;
  433. protected
  434. function CreateItem(ChangeDescriptor: IntPtr): TObject; override;
  435. public
  436. property Changes[Index: integer]: TNotifyTableChange read GetChanges; default;
  437. end;
  438. TNotifyChange = class
  439. private
  440. FNotifyType: TChangeNotifyEventType;
  441. FTableChanges: TNotifyTableChanges;
  442. public
  443. constructor Create(ChangeDescriptor: IntPtr);
  444. destructor Destroy; override;
  445. property NotifyType: TChangeNotifyEventType read FNotifyType;
  446. property TableChanges: TNotifyTableChanges read FTableChanges;
  447. end;
  448. TChangeNotifyCallback = procedure(NotifyType: TChangeNotifyEventType;
  449. TableChanges: TNotifyTableChanges) of object;
  450. TOCIChangeNotification = class
  451. private
  452. FGCHandle: IntPtr;
  453. FEnabled: boolean;
  454. FPersistent: boolean;
  455. FTimeOut: integer;
  456. FOperations: TChangeNotifyDMLOperations;
  457. FOnChange: TChangeNotifyCallback;
  458. hOCISubscription: pOCISubscription;
  459. hWindow: HWND;
  460. function GetGCHandle: IntPtr;
  461. procedure AllocWnd;
  462. procedure SetEnabled(Value: boolean);
  463. function CallbackChangeNotify(pCtx: IntPtr; pSubscrHp: pOCISubscription;
  464. pPayload: IntPtr; iPayloadLen: ub4; pDescriptor: IntPtr; iMode: ub4): sword;
  465. protected
  466. property GCHandle: IntPtr read GetGCHandle;
  467. public
  468. constructor Create;
  469. destructor Destroy; override;
  470. function SetProp(Prop: integer; const Value: variant): boolean;
  471. function GetProp(Prop: integer; var Value: variant): boolean;
  472. function GetSubscriptionHandle(Connection: TOCIConnection): pOCISubscription;
  473. procedure Register(Connection: TOCIConnection);
  474. procedure Unregister(Connection: TOCIConnection);
  475. function IsActive: boolean;
  476. property OnChange: TChangeNotifyCallback read FOnChange write FOnChange;
  477. end;
  478. {$ENDIF}
  479. { TOCICommand }
  480. TOCICommand = class (TCRCommand)
  481. private
  482. FCursor: TOraCursor;
  483. FCursorRef: TOraCursor;
  484. FOCICallStyle: TOCICallStyle;
  485. FScanParams: boolean;
  486. FNonBlocking: boolean;
  487. FSQLType: word;
  488. FRowsProcessed: integer;
  489. FFetchedRows: integer;
  490. FErrorOffset: word;
  491. FIterCount: integer;
  492. FFieldsAsString: boolean;
  493. FCacheLobs: boolean;
  494. FStoreRowId: boolean;
  495. FRowId: string;
  496. FRawAsString: boolean;
  497. FNumberAsString: boolean;
  498. FIntegerPrecision: Integer;
  499. FLargeIntPrecision: Integer;
  500. FFloatPrecision: Integer;
  501. FForceUnprepare: boolean;
  502. FGCHandle: IntPtr;
  503. FDisableParamScan: boolean; // prevents updating of ParamDescs in SetSQL (used in CreateProcCall)
  504. FTemporaryLobUpdate: boolean;
  505. FStatementCache: boolean;
  506. {$IFDEF MSWINDOWS}
  507. FChangeNotification: TOCIChangeNotification;
  508. {$ENDIF}
  509. { OCI8 }
  510. hOCIError: pOCIError; // local error handle
  511. {$IFDEF MSWINDOWS}
  512. hBusy: THandle;
  513. {$ENDIF}
  514. {$IFDEF MSWINDOWS}
  515. hExecThread: TThread;
  516. {$ENDIF}
  517. {$IFDEF WIN32}
  518. hExecuted: TEvent;
  519. {$ENDIF}
  520. function GetGCHandle: IntPtr;
  521. function RemoveCRSymbols(SQLText: string; var ErrorOffset: integer): string;
  522. protected
  523. FConnection: TOCIConnection;
  524. procedure DoExecute;
  525. procedure EndExecute(E: Exception);
  526. //neaded for trial call
  527. procedure CheckOCI;
  528. procedure CheckOCI73;
  529. procedure CheckOCI80;
  530. procedure CheckActive;
  531. procedure CheckInactive;
  532. procedure CheckSession;
  533. procedure Check(Status: sword);
  534. { OCI73 }
  535. function GetOraType7(DataType: integer; SubDataType: integer{ = 0}): integer;
  536. function GetFieldDesc7(FieldNo: integer; var Field: TFieldDesc; LongString: boolean; FlatBuffer: boolean): boolean;
  537. function InternalFetch7(Rows: word): word;
  538. function InternalFetchPiece7: integer;
  539. procedure InitProcParams7(Name: string; Overload: integer);
  540. { OCI80 }
  541. function GetOraType8(DataType: integer; SubDataType: integer): integer;
  542. function GetFieldDesc8(FieldNo: integer; var Field: TFieldDesc; LongString: boolean; FlatBuffer: boolean): boolean;
  543. function InternalFetch8(Rows: word; Orientation: integer; Offset: integer): word;
  544. function InternalExecuteFetch8(Rows: word): word;
  545. function InternalFetchPiece8(Orientation: integer; Offset: integer): integer;
  546. procedure InitProcParams8(Name: string; Overload: integer);
  547. function CallbackInBind(Bind: pOCIBind; Iter: ub4; Index: ub4; var Buf: IntPtr;
  548. var BufLen: ub4; var PieceStatus: ub1; var Ind: IntPtr): sb4;
  549. function CallbackOutBind(Bind: pOCIBind; Iter: ub4; Index: ub4; var Buf: IntPtr;
  550. var BufLen: pub4; var PieceStatus: ub1; var Ind: IntPtr): sb4;
  551. procedure SetArrayLength(Value: integer);
  552. function GetActive: boolean;
  553. property Params;
  554. property Executing;
  555. property GCHandle: IntPtr read GetGCHandle;
  556. public
  557. constructor Create; override;
  558. destructor Destroy; override;
  559. function GetOraType(DataType: integer; SubDataType: integer): integer;
  560. procedure InternalOpen;
  561. procedure InternalParse;
  562. procedure InternalPrepare;
  563. procedure BindParam(Param: TOraParamDesc);
  564. function InternalExecute(Mode: integer; Rows: Integer = 0): sword;
  565. procedure Exec;
  566. function GetFieldDesc(FieldNo: integer; var Field: TFieldDesc; LongString: boolean; FlatBuffer: boolean): boolean;
  567. procedure DefineData(Field: TFieldDesc; Buf: IntPtr; Ind: psb2);
  568. procedure DefineArrayData(Field: TFieldDesc; Buf: IntPtr; Ind: psb2; BufSkip: integer;
  569. IndSkip: integer);
  570. procedure DefinePieceData(Field: TFieldDesc; Buf: IntPtr; Ind: psb2);
  571. procedure DefineDynamic(Field: TFieldDesc; Owner: IntPtr; Proc: IntPtr; CharsetId: Integer);
  572. function InternalFetch(Rows: word; Orientation: integer = OCI_FETCH_NEXT; Offset: integer = 0): word;
  573. function InternalFetchPiece(Orientation: integer = OCI_FETCH_NEXT; Offset: integer = 0): integer;
  574. procedure InternalCancel;
  575. procedure InternalClose;
  576. procedure Finish;
  577. procedure GetPI(var Handle: pOCIHandle; var Piece: byte; var Buf: IntPtr;
  578. var Iteration: cardinal; var Index: cardinal; var Mode: TParamDirection);
  579. procedure SetPI(Handle: pOCIHandle; HType: cardinal; Piece: byte; Buf: IntPtr;
  580. var BufLen: cardinal; Ind: psb2);
  581. function NativeCursor: boolean;
  582. function RowsReturn: boolean;
  583. procedure CheckRowsReturn;
  584. { Params }
  585. function AddParam: TParamDesc; override;
  586. procedure ScanParams;
  587. procedure BindParams;
  588. procedure InitProcParams(Name: string; Overload: integer);
  589. //procedure DisconnectParams;
  590. function GetParam(Index: integer): TOraParamDesc;
  591. procedure BreakExec;
  592. procedure HardBreak;
  593. procedure Busy;
  594. procedure Release;
  595. function CreateProcCall(Name: PChar; Overload: integer;
  596. NeedDescribe: boolean = True; PassByName: boolean = False): string;
  597. procedure Prepare; override;
  598. procedure Unprepare; override;
  599. function GetPrepared: boolean; override;
  600. procedure Execute(Iters: integer = 1); override;
  601. procedure SetConnection(Value: TCRConnection); override;
  602. procedure SetSQL(const Value: string); override;
  603. function GetCursor: TOraCursor;
  604. procedure SetCursor(Value: TOraCursor);
  605. procedure SetOCICallStyle(Value: TOCICallStyle);
  606. function GetCursorState: TCursorState; override;
  607. procedure SetCursorState(Value: TCursorState); override;
  608. function GetSQLType: integer;
  609. procedure SetSQLType(Value: integer);
  610. function GetRowId: string;
  611. function SetProp(Prop: integer; const Value: variant): boolean; override;
  612. function GetProp(Prop: integer; var Value: variant): boolean; override;
  613. {$IFDEF MSWINDOWS}
  614. property ChangeNotification: TOCIChangeNotification read FChangeNotification write FChangeNotification;
  615. {$ENDIF}
  616. end;
  617. { TOCITableInfo }
  618. {$IFNDEF LITE}
  619. TOCITableInfo = class(TCRTableInfo)
  620. public
  621. class function NormalizeName(Value: string; const QuoteNames: boolean): string; overload; override;
  622. class function NormalizeName(Value: string; const LeftQ: char; const RightQ: char; const QuoteNames: boolean = False): string; overload; override;
  623. class function NormalizeName(Value: string; const LeftQ: char; const RightQ: char; const QuoteNames, UnQuoteNames: boolean): string; overload;
  624. class function NormalizeName(Value: string; const QuoteNames, UnQuoteNames: boolean): string; overload;
  625. {$IFDEF VER8}
  626. class function LeftQuote: Char; override;
  627. class function RightQuote: Char; override;
  628. class function IsQuoted(const Value: string): boolean; override;
  629. class function QuotesNeeded(Value: string): boolean; override;
  630. {$ENDIF}
  631. end;
  632. {$ENDIF}
  633. { TOCIRecordSet }
  634. TModifyAction = procedure of object;
  635. TOCIRecordSet = class (TCRRecordSet)
  636. private
  637. FAutoClose: boolean;
  638. FDeferredLobRead: boolean;
  639. hExecFetchThread: TThread;
  640. hFetchAllThread: TThread;
  641. FFetchCursor: TOraCursor;
  642. FFetchBlock: IntPtr;
  643. FFetchBlockItemSize: integer;
  644. FPieceFetch: boolean;
  645. FFetchItems: IntPtr; // for callback fetch
  646. // for backward fetch
  647. FFetchAbsolute: boolean;
  648. FFetchStart: integer;
  649. FFetchEnd: integer;
  650. FNoData: boolean;
  651. FGCHandle: IntPtr;
  652. FHasLargeIntFields: boolean;
  653. {$IFDEF MSWINDOWS}
  654. hEvent: TEvent;
  655. {$ENDIF}
  656. FStopFetch : boolean;
  657. FFetching : boolean;
  658. FHasObjectFields: boolean;
  659. FTempFilterText: string;
  660. //PreCached FConection properties
  661. FDisconnectedMode: boolean;
  662. FUseUnicode: boolean;
  663. FCharLength: integer;
  664. procedure InitFetchCursor;
  665. function FetchArray(FetchBack: boolean = False): boolean;
  666. function FetchPiece(FetchBack: boolean = False): boolean;
  667. procedure AllocFetchBlock;
  668. procedure FreeFetchBlock;
  669. function GetNonBlocking: boolean;
  670. function GetGCHandle: IntPtr;
  671. function GetDisconnectedMode: boolean;
  672. function GetUseUnicode: boolean;
  673. function GetCharLength: integer;
  674. function IsConvertedFieldType(DataType: word): boolean;
  675. protected
  676. FCommand: TOCICommand;
  677. FConnection: TOCIConnection; // for perf
  678. procedure CreateCommand; override;
  679. procedure SetCommand(Value: TCRCommand); override;
  680. { Open/Close }
  681. procedure InternalPrepare; override;
  682. procedure InternalUnPrepare; override;
  683. procedure InternalOpen; override;
  684. procedure InternalClose; override;
  685. procedure InternalInitFields; override;
  686. procedure ExecFetch; override;
  687. function GetIndicatorSize: word; override;
  688. function GetEOF: boolean; override;
  689. { Filter/Find/Locate/Sorting }
  690. {$IFNDEF CLR}
  691. function InternalAnsiStrComp(const Value1, Value2: IntPtr;
  692. const Options: TLocateExOptions): integer; override;
  693. {$ENDIF}
  694. function InternalAnsiCompareText(const Value1, Value2: string;
  695. const Options: TLocateExOptions): integer; override;
  696. function InternalWStrLComp(const Value1, Value2: WideString;
  697. const Options: TLocateExOptions): integer; override;
  698. function InternalWStrComp(const Value1, Value2: WideString;
  699. const Options: TLocateExOptions): integer; override;
  700. procedure SetFilterText(Value: string); override;
  701. { Fetch }
  702. function Fetch(FetchBack: boolean = False): boolean; override;
  703. function CanFetchBack: boolean; override;
  704. { Items }
  705. procedure FreeAllItems;
  706. { Edit }
  707. procedure DoExecFetch;
  708. procedure EndExecFetch(E: Exception);
  709. procedure DoFetchAll;
  710. procedure DoFetchAllPulse;
  711. procedure EndFetchAll(E: Exception);
  712. function CallbackDefine(Define: pOCIDefine; Iter: cardinal; var Buf: IntPtr;
  713. var BufLen: pub4; var PieceStatus: ub1; var Ind: IntPtr): sb4;
  714. {$IFNDEF LITE}
  715. { TablesInfo }
  716. class function GetTableInfoClass: TTableInfoClass; override;
  717. {$ENDIF}
  718. property GCHandle: IntPtr read GetGCHandle;
  719. //PreCached FConection properties
  720. property DisconnectedMode: boolean read GetDisconnectedMode;
  721. property UseUnicode: boolean read GetUseUnicode;
  722. property CharLength: integer read GetCharLength;
  723. public
  724. constructor Create; override;
  725. destructor Destroy; override;
  726. { Open/Close }
  727. function IsFullReopen: boolean; override;
  728. procedure Reopen; override;
  729. procedure SetCommandType;
  730. procedure ExecCommand; override; // Execute command
  731. procedure BreakExec;
  732. procedure Disconnect; override;
  733. { Fetch }
  734. procedure FetchAll; override;
  735. function RowsReturn: boolean; override;
  736. { Fields }
  737. procedure SetNull(FieldNo: word; RecBuf: IntPtr; Value: boolean); override;
  738. function GetNull(FieldNo: word; RecBuf: IntPtr): boolean; override;
  739. procedure GetDateFromBuf(Buf: IntPtr; Offset: integer; Date: IntPtr; Format: TDateFormat); override;
  740. procedure PutDateToBuf(Buf: IntPtr; Offset: integer; Date: IntPtr; Format: TDateFormat); override;
  741. function IsBlobFieldType(DataType: word): boolean; override;
  742. function IsComplexFieldType(DataType: word): boolean; override;
  743. procedure GetFieldData(Field: TFieldDesc; RecBuf: IntPtr; Dest: IntPtr); override;
  744. procedure GetFieldAsVariant(FieldNo: word; RecBuf: IntPtr; var Value: variant); override;
  745. procedure PutFieldAsVariant(FieldNo: word; RecBuf: IntPtr; const Value: variant); override;
  746. { Records }
  747. procedure CreateComplexFields(RecBuf: IntPtr; WithBlob: boolean); override;
  748. procedure FreeComplexFields(RecBuf: IntPtr; WithBlob: boolean); override;
  749. procedure CopyComplexFields(Source: IntPtr; Dest: IntPtr; WithBlob: boolean); override;
  750. function CompareFieldValue(ValuePtr: IntPtr; const ValueType: integer; FieldDesc: TFieldDesc; RecBuf: IntPtr; const Options: TLocateExOptions): integer; override;
  751. function CompareFields(RecBuf1: IntPtr; RecBuf2: IntPtr; FieldDesc: TFieldDesc; Options: TLocateExOptions = []): integer; override;
  752. procedure SortItems; override;
  753. procedure FilterUpdated; override;
  754. { Navigation }
  755. procedure SetToBegin; override;
  756. procedure SetToEnd; override;
  757. { BookMarks }
  758. procedure GetBookmark(Bookmark: PRecBookmark); override;
  759. procedure SetToBookmark(Bookmark: PRecBookmark); override;
  760. function GetBlockFetchPos(Block: PBlockHeader): integer;
  761. function GetItemFetchPos(Item: PItemHeader): integer;
  762. { Blobs }
  763. procedure SetConnection(Value: TCRConnection); override;
  764. function SetProp(Prop: integer; const Value: variant): boolean; override;
  765. function GetProp(Prop: integer; var Value: variant): boolean; override;
  766. end;
  767. { TOraLob }
  768. TLobType = (ltBlob, ltClob, ltNClob);
  769. TOraLob = class (TCompressedBlob)
  770. private
  771. phLobLocator: ppOCILobLocator;
  772. FSvcCtx: pOCISvcCtx;
  773. hOCIEnv: pOCIEnv;
  774. FNativeHandle: boolean;
  775. FCached: boolean;
  776. FCharsetForm: integer;
  777. FLobType: TLobType;
  778. FCharLength: byte;
  779. function GetOCILobLocator: pOCILobLocator;
  780. function GethOCILobLocator: pOCIStmt;
  781. procedure SethOCILobLocator(Value: pOCIStmt);
  782. procedure SetOCILobLocator(Value: pOCILobLocator);
  783. function GetOCILobLocatorPtr: ppOCILobLocator;
  784. procedure SetCached(const Value: boolean);
  785. procedure SetOCISvcCtx(const Value: pOCISvcCtx);
  786. property hLobLocator: pOCILobLocator read GethOCILobLocator write SethOCILobLocator;
  787. protected
  788. FNeedReadLob: boolean;
  789. procedure CheckValue; override;
  790. function GetSize: cardinal; override;
  791. procedure CheckAlloc;
  792. procedure CheckSession;
  793. procedure CheckInit;
  794. procedure CheckCharSetForm;
  795. function CharSize: Byte; virtual;
  796. public
  797. constructor Create(ASvcCtx: pOCISvcCtx);
  798. destructor Destroy; override;
  799. procedure AllocLob; virtual;
  800. procedure FreeLob; virtual;
  801. procedure Init;
  802. procedure CreateTemporary(LobType: TLobType);
  803. procedure FreeTemporary;
  804. function IsTemporary: LongBool;
  805. function IsInit: boolean;
  806. function LengthLob: longint;
  807. procedure EnableBuffering;
  808. procedure DisableBuffering;
  809. procedure ReadLob;
  810. procedure WriteLob;
  811. function Read(Position, Count: cardinal; Dest: IntPtr): cardinal; override;
  812. procedure Write(Position, Count: cardinal; Source: IntPtr); override;
  813. procedure Clear; override;
  814. procedure Truncate(NewSize: cardinal); override;
  815. procedure LoadFromStream(Stream: TStream); override;
  816. procedure SaveToStream(Stream: TStream); override;
  817. property OCILobLocator: pOCILobLocator read GetOCILobLocator write SetOCILobLocator;
  818. property OCILobLocatorPtr: ppOCILobLocator read GetOCILobLocatorPtr;
  819. property OCISvcCtx: pOCISvcCtx read FSvcCtx write SetOCISvcCtx;
  820. property Cached: boolean read FCached write SetCached;
  821. property LobType: TLobType read FLobType write FLobType;
  822. end;
  823. { TOraFile }
  824. TOraFile = class (TOraLob)
  825. private
  826. FNeedRollback: boolean;
  827. FRollbackFileDir: string;
  828. FRollbackFileName: string;
  829. function GetFileDir: string;
  830. procedure SetFileDir(Value: string);
  831. function GetFileName: string;
  832. procedure SetFileName(Value: string);
  833. protected
  834. CanRollback: boolean;
  835. procedure CheckValue; override;
  836. function CharSize: Byte; override;
  837. procedure SaveToRollback; override;
  838. public
  839. destructor Destroy; override;
  840. procedure AllocLob; override;
  841. procedure FreeLob; override;
  842. procedure Open;
  843. procedure Close;
  844. procedure EnableRollback;
  845. procedure Commit; override;
  846. procedure Cancel; override;
  847. procedure Refresh;
  848. function IsOpen: boolean;
  849. function Exists: boolean;
  850. property FileDir: string read GetFileDir write SetFileDir;
  851. property FileName: string read GetFileName write SetFileName;
  852. end;
  853. { TOraTimeStamp }
  854. TOraTimeStamp = class (TSharedObject)
  855. private
  856. phOCIDateTime: ppOCIDateTime;
  857. FDescriptorType: cardinal;
  858. FPrecision: byte;
  859. FFormat: string;
  860. FNativeHandle: boolean;
  861. FIndicator: IntPtr;
  862. function GetAsString: string;
  863. procedure SetAsString(const Value: string);
  864. function GetAsDateTime: TDateTime;
  865. procedure SetAsDateTime(Value: TDateTime);
  866. function GetTimeZone: string;
  867. function GethOCIDateTime: pOCIDateTime;
  868. procedure SethOCIDateTime(Value: pOCIDateTime);
  869. function GetOCIDateTime: pOCIDateTime;
  870. procedure SetOCIDateTime(const Value: pOCIDateTime);
  871. procedure SetDescriptorType(const Value: cardinal);
  872. function GetOCIDateTimePtr: ppOCIDateTime;
  873. procedure CheckValid;
  874. procedure SetFormat(const AFormat: string);
  875. property hOCIDateTime: pOCIDateTime read GethOCIDateTime write SethOCIDateTime;
  876. public
  877. constructor Create(DataType: word);
  878. destructor Destroy; override;
  879. procedure AllocDateTime;
  880. procedure FreeDateTime;
  881. procedure AssignTo(Dest: TOraTimeStamp);
  882. function Compare(Dest: TOraTimeStamp): integer;
  883. function GetIsNull: boolean;
  884. procedure SetIsNull(Value: boolean);
  885. procedure Construct(Year: smallint; Month, Day, Hour, Min, Sec: byte;
  886. FSec: cardinal; TimeZone: string);
  887. procedure GetDate(var Year: smallint; var Month, Day: byte);
  888. procedure SetDate(Year: smallint; Month, Day: byte);
  889. procedure GetTime(var Hour, Min, Sec: byte; var FSec: cardinal);
  890. procedure SetTime(Hour, Min, Sec: byte; FSec: cardinal);
  891. procedure GetTimeZoneOffset(var Hour, Min: shortint);
  892. procedure SetTimeZoneOffset(TZHour, TZMin: shortint);
  893. property DescriptorType: cardinal read FDescriptorType write SetDescriptorType;
  894. property OCIDateTime: pOCIDateTime read GetOCIDateTime write SetOCIDateTime;
  895. property OCIDateTimePtr: ppOCIDateTime read GetOCIDateTimePtr;
  896. property Format: string read FFormat write SetFormat;
  897. property Precision: byte read FPrecision write FPrecision;
  898. property TimeZone: string read GetTimeZone;
  899. property AsString: string read GetAsString write SetAsString;
  900. property AsDateTime: TDateTime read GetAsDateTime write SetAsDateTime;
  901. property IsNull: boolean read GetIsNull write SetIsNull;
  902. end;
  903. TOraInterval = class (TSharedObject)
  904. private
  905. phOCIInterval: ppOCIInterval;
  906. FDescriptorType: cardinal;
  907. FNativeHandle: boolean;
  908. FFracPrecision: byte;
  909. FLeadPrecision: byte;
  910. FIndicator: IntPtr;
  911. procedure Init;
  912. procedure CheckValid;
  913. function GetAsString: string;
  914. function GethOCIInterval: pOCIInterval;
  915. procedure SethOCIInterval(Value: pOCIInterval);
  916. function GetOCIInterval: pOCIInterval;
  917. function GetOCIIntervalPtr: ppOCIInterval;
  918. procedure SetAsString(const Value: string);
  919. procedure SetDescriptorType(const Value: cardinal);
  920. procedure SetOCIInterval(const Value: pOCIInterval);
  921. property hOCIInterval: pOCIInterval read GethOCIInterval write SethOCIInterval;
  922. public
  923. constructor Create(DataType: word);
  924. destructor Destroy; override;
  925. procedure AllocInterval;
  926. procedure FreeInterval;
  927. procedure AssignTo(Dest: TOraInterval);
  928. function Compare(Dest: TOraInterval): integer;
  929. function GetIsNull: boolean;
  930. procedure SetIsNull(Value: boolean);
  931. procedure GetYearMonth(var Year, Month: integer);
  932. procedure SetYearMonth(Year, Month: integer);
  933. procedure GetDaySecond(var Day, Hour, Min, Sec, FSec: integer);
  934. procedure SetDaySecond(Day, Hour, Min, Sec, FSec: integer);
  935. property DescriptorType: cardinal read FDescriptorType write SetDescriptorType;
  936. property OCIInterval: pOCIInterval read GetOCIInterval write SetOCIInterval;
  937. property OCIIntervalPtr: ppOCIInterval read GetOCIIntervalPtr;
  938. property LeadPrecision: byte read FLeadPrecision write FLeadPrecision;
  939. property FracPrecision: byte read FFracPrecision write FFracPrecision;
  940. property AsString: string read GetAsString write SetAsString;
  941. property IsNull: boolean read GetIsNull write SetIsNull;
  942. end;
  943. TOraNumber = class (TSharedObject)
  944. private
  945. phOCINumber: pOCINumber;
  946. FIndicator: IntPtr;
  947. FNativeHandle: boolean;
  948. function GetOCINumberPtr: pOCINumber;
  949. procedure SetOCINumberPtr(Value: pOCINumber);
  950. function GetOCINumber: OCINumber;
  951. procedure SetOCINumber(Value: OCINumber);
  952. function GetAsString: string;
  953. procedure SetAsString(const Value: string);
  954. function GetAsInteger: integer;
  955. procedure SetAsInteger(const Value: integer);
  956. function GetAsLargeInt: int64;
  957. procedure SetAsLargeInt(const Value: int64);
  958. function GetAsFloat: double;
  959. procedure SetAsFloat(const Value: double);
  960. function GetIsNull: boolean;
  961. procedure SetIsNull(Value: boolean);
  962. {$IFDEF LITE}
  963. function GetAsBCD: TBCD;
  964. procedure SetAsBCD(const Value: TBCD);
  965. {$ENDIF}
  966. public
  967. constructor Create;
  968. destructor Destroy; override;
  969. procedure AssignTo(Dest: TOraNumber);
  970. function Compare(Dest: TOraNumber): integer;
  971. property OCINumber: OCINumber read GetOCINumber write SetOCINumber;
  972. property OCINumberPtr: pOCINumber read GetOCINumberPtr write SetOCINumberPtr;
  973. property AsString: string read GetAsString write SetAsString;
  974. property AsInteger: integer read GetAsInteger write SetAsInteger;
  975. property AsLargeInt: Int64 read GetAsLargeInt write SetAsLargeInt;
  976. property AsFloat: double read GetAsFloat write SetAsFloat;
  977. property IsNull: boolean read GetIsNull write SetIsNull;
  978. {$IFDEF LITE}
  979. property AsBCD: TBCD read GetAsBCD write SetAsBCD;
  980. {$ENDIF}
  981. end;
  982. {$IFNDEF LITE}
  983. { TOCITransaction }
  984. TGlobalCoordinator = (gcInternal{$IFDEF MSWINDOWS}, gcMTS{$ENDIF});
  985. TOraIsolationLevel = (ilReadCommitted, ilSerializable, ilReadOnly);
  986. TOraTransactionAction = (taCommit, taRollback);
  987. TOraTransactionState = (tsInactive, tsActive, tsPrepared, tsFinished);
  988. TTransactionLink = class
  989. Connection: TOCIConnection;
  990. BranchQualifier: TBytes;
  991. State: TOraTransactionState;
  992. OCITrans: pOCITrans;
  993. {$IFDEF MSWINDOWS}
  994. MTSSvcCtx: pOCISvcCtx;
  995. {$ENDIF}
  996. end;
  997. TTransactionLinks = class (TDAList)
  998. private
  999. function GetItems(Index: integer): TTransactionLink;
  1000. public
  1001. procedure Clear; override;
  1002. function IndexOfConnection(Connection: TOCIConnection): integer;
  1003. function AddConnection(Connection: TOCIConnection; BranchQualifier: TBytes): boolean;
  1004. function RemoveConnection(Connection: TOCIConnection): boolean;
  1005. property Items[Index: Integer]: TTransactionLink read GetItems; default;
  1006. end;
  1007. TOCITransaction = class
  1008. private
  1009. FOnError: TErrorProc;
  1010. FTransactionLinks: TTransactionLinks;
  1011. FActive: boolean;
  1012. FInactiveTimeOut: integer;
  1013. FResumeTimeOut: integer;
  1014. FTransactionName: string;
  1015. FXID: IntPtr;
  1016. FTransactionId: TBytes;
  1017. FGlobalCoordinator: TGlobalCoordinator;
  1018. FIsolationLevel: TOraIsolationLevel;
  1019. FDefaultCloseAction: TOraTransactionAction;
  1020. {$IFDEF MSWINDOWS}
  1021. FMTSGC: ICRTransactionDispenserSC;
  1022. FMTSTrans: ICRTransactionSC;
  1023. {$ENDIF}
  1024. procedure WriteTransactionId;
  1025. procedure WriteBranchQualifier(TransactionLink: TTransactionLink);
  1026. procedure FreeTransaction;
  1027. {$IFDEF MSWINDOWS}
  1028. procedure StartMTSTransaction;
  1029. procedure CompleteMTSTransaction(Commit: boolean);
  1030. {$ENDIF}
  1031. public
  1032. constructor Create;
  1033. destructor Destroy; override;
  1034. procedure CloseTransaction;
  1035. procedure Check(Status: sword);
  1036. procedure OraError(var ErrorCode: sword; UseCallback: boolean);
  1037. {$IFDEF MSWINDOWS}
  1038. procedure MTSCheck(status: sword);
  1039. procedure MTSError(var ErrorCode: sword; UseCallback: boolean);
  1040. {$ENDIF}
  1041. procedure CheckState(State: boolean);
  1042. procedure SetTransactionId(TransactionId: TBytes);
  1043. function AddConnection(Connection: TOCIConnection; BranchQualifier: TBytes): boolean;
  1044. function RemoveConnection(Connection: TOCIConnection): boolean;
  1045. function SetProp(Prop: integer; const Value: variant): boolean;
  1046. function GetProp(Prop: integer; var Value: variant): boolean;
  1047. procedure StartTransaction(Resume: boolean);
  1048. procedure Commit;
  1049. procedure Rollback;
  1050. procedure Detach;
  1051. procedure Resume;
  1052. property Active: boolean read FActive;
  1053. property OnError: TErrorProc read FOnError write FOnError;
  1054. end;
  1055. {$ENDIF}
  1056. TOraClassesUtils = class
  1057. public
  1058. class procedure InternalUnPrepare(Obj: TOCIRecordSet);
  1059. end;
  1060. const
  1061. IntegerPrecision: integer = 9;
  1062. LargeIntPrecision: integer = 0;
  1063. FloatPrecision: integer = 15;
  1064. var
  1065. UseOCI7ProcDesc: boolean;
  1066. TimeFormat: string;
  1067. function OraDateToDateTime(Buf: IntPtr): TDateTime;
  1068. function OraDateToMSecs(Buf: IntPtr): double;
  1069. procedure DateTimeToOraDate(DateTime: TDateTime; Buf: IntPtr);
  1070. procedure MSecsToOraDate(MSecs: double; Buf: IntPtr);
  1071. procedure OCIInit;
  1072. procedure OCIFinish;
  1073. procedure GetTimeFormat;
  1074. function QuotedOCIName(Name: string): string;
  1075. function QuotedSQLName(Name: string): string;
  1076. {$IFNDEF LITE}
  1077. function GetTablesInfo(SQL: string; TablesInfo: TCRTablesInfo): integer;
  1078. {$ENDIF}
  1079. implementation
  1080. uses
  1081. {$IFDEF PROF}OraProf, {$ENDIF}
  1082. {$IFDEF CLR}
  1083. System.Runtime.InteropServices, System.Text, System.Threading,
  1084. {$ELSE}
  1085. {$IFDEF VER6P}Variants,{$ENDIF}
  1086. {$ENDIF}
  1087. {$IFDEF MSWINDOWS}
  1088. {$IFNDEF LITE}
  1089. ComObj,
  1090. {$ENDIF}
  1091. {$ENDIF}
  1092. DAConsts, OraConsts, OraObjects, CRParser, OraParser, Math;
  1093. const
  1094. WM_ENDTHREAD = $400;
  1095. WM_EXCEPTTHREAD = $401;
  1096. WM_ENDEXECUTE = $402; // WAR
  1097. WM_AFTERFETCH = $404;
  1098. WM_CHANGENOTIFY = $405;
  1099. type
  1100. TArr = array [0..100] of byte; // DEBUG TEMP
  1101. PArr = ^TArr;
  1102. TArrC = array [0..100] of char; // DEBUG TEMP
  1103. PArrC = ^TArrC;
  1104. {$IFNDEF LINUX}
  1105. type
  1106. TExecThread = class(TThread)
  1107. protected
  1108. FMethodDesc: TMethodDesc;
  1109. FException: Exception;
  1110. public
  1111. constructor Create(MethodDesc: TMethodDesc; CreateSuspended: Boolean);
  1112. destructor Destroy; override;
  1113. procedure Execute; override;
  1114. end;
  1115. {$ENDIF}
  1116. var
  1117. {$IFDEF LINUX}
  1118. hLockConnect: TCriticalSection;
  1119. {$ENDIF}
  1120. OCICallbackDefinePtr: IntPtr;
  1121. OCICallbackInBindPtr: IntPtr;
  1122. OCICallbackOutBindPtr: IntPtr;
  1123. OCICallbackFailoverPtr: IntPtr;
  1124. {$IFDEF MSWINDOWS}
  1125. OCICallbackChangeNotifyPtr: IntPtr;
  1126. {$ENDIF}
  1127. {$IFDEF CLR}
  1128. HOCICallbackDefine: GCHandle;
  1129. HOCICallbackInBind: GCHandle;
  1130. HOCICallbackOutBind: GCHandle;
  1131. HOCICallbackFailover: GCHandle;
  1132. HOCICallbackChangeNotify: GCHandle;
  1133. HWndProc: GCHandle;
  1134. {$ENDIF}
  1135. procedure OCIInit;
  1136. begin
  1137. OraCall.InitOCI;
  1138. end;
  1139. procedure OCIFinish;
  1140. begin
  1141. FinishOCI;
  1142. end;
  1143. function Shift(Value: cardinal): cardinal;
  1144. begin
  1145. Result := Value;
  1146. if Result <> 0 then
  1147. if (Result and $FF) = 0 then // while do
  1148. Result := Result shr 8;
  1149. end;
  1150. function Reverse2(Value: word): TBytes;
  1151. begin
  1152. SetLength(Result, 2);
  1153. Result[0] := byte(Value shr 8);
  1154. Result[1] := byte(Value);
  1155. end;
  1156. function Reverse4(Value: cardinal): TBytes;
  1157. begin
  1158. SetLength(Result, 4);
  1159. Result[0] := byte(Value shr 24);
  1160. Result[1] := byte(Value shr 16);
  1161. Result[2] := byte(Value shr 8);
  1162. Result[3] := byte(Value);
  1163. end;
  1164. // Converts Count bytes from memory pointed by Bytes to 64 base string. Starting
  1165. // digit (6-bit chunk) may be shifted by -4, -2, 0 or 2 bits. Missing bits
  1166. // assumed to be zero.
  1167. // Bytes are converted in the following way (example for Shift = 0):
  1168. // 0 byte 1 byte hi lo
  1169. // 00000100|00000000|01000001|01000011|...
  1170. // ------++ ++++---- --++++++ ------++ +++
  1171. // B(1) A(0) B(1) B(1) Q(16)
  1172. function BytesTo64BaseString(Bytes: TBytes; Count: integer; Shift: integer): string;
  1173. const
  1174. Map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  1175. var
  1176. i, RestBits: cardinal;
  1177. CurByte, Digit, NextDigit: byte;
  1178. begin
  1179. Result :='';
  1180. RestBits := 2 - Shift;
  1181. NextDigit := $FF;
  1182. for i := 0 to Count - 1 do begin
  1183. CurByte := Bytes[i];
  1184. Digit := CurByte shr RestBits;
  1185. if NextDigit <> $FF then
  1186. Digit := Digit or NextDigit;
  1187. Result := Result + Map[Digit + 1];
  1188. NextDigit := (CurByte and ($FF shr (8 - RestBits))) shl (6 - RestBits);
  1189. if RestBits = 6 then begin
  1190. Result := Result + Map[NextDigit + 1];
  1191. NextDigit := $FF;
  1192. RestBits := 2;
  1193. end
  1194. else
  1195. RestBits := RestBits + 2;
  1196. end;
  1197. if NextDigit <> $FF then
  1198. Result := Result + Map[NextDigit + 1];
  1199. end;
  1200. function RowId7ToString(RowId: PRowId7): string;
  1201. var
  1202. Buf: TBytes;
  1203. begin
  1204. Buf := nil;
  1205. if (RowId.rd.rcs4 = 0) then begin // obj num
  1206. // restricted (Oracle 7)
  1207. Result :=
  1208. IntToHex(Shift(RowId.rcs7), 8) + '.' +
  1209. IntToHex(Shift(RowId.rcs8), 4) + '.' + // use 2 byte
  1210. IntToHex(RowId.rd.rcs5, 4)
  1211. end
  1212. else begin
  1213. // extended (Oracle 8 and higher)
  1214. Buf := Reverse4(RowId.rd.rcs4);
  1215. Result := BytesTo64BaseString(Buf, 4, -4);
  1216. Buf := Reverse2(RowId.rd.rcs5);
  1217. Result := Result + BytesTo64BaseString(Buf, 2, -2);
  1218. Buf := Reverse4(Shift(RowId.rcs7));
  1219. Result := Result + BytesTo64BaseString(Buf, 4, -4);
  1220. Buf := Reverse2(Shift(RowId.rcs8)); // use 3 byte
  1221. Result := Result + BytesTo64BaseString(Buf, 2, -2);
  1222. end;
  1223. end;
  1224. function RowId8ToString(RowId: PRowId8): string;
  1225. var
  1226. Buf: TBytes;
  1227. begin
  1228. Buf := nil;
  1229. if (RowId.ridobjnum = 0) then
  1230. // restricted (Oracle 7)
  1231. Result :=
  1232. IntToHex(Shift(RowId.ridblocknum), 8) + '.' +
  1233. IntToHex(Shift(RowId.ridslotnum), 4) + '.' +
  1234. IntToHex(RowId.ridfilenum, 4)
  1235. else begin
  1236. // extended (Oracle 8 and higher)
  1237. Buf := Reverse4(RowId.ridobjnum);
  1238. Result := BytesTo64BaseString(Buf, 4, -4);
  1239. Buf := Reverse2(RowId.ridfilenum);
  1240. Result := Result + BytesTo64BaseString(Buf, 2, -2);
  1241. Buf := Reverse4(RowId.ridblocknum);
  1242. Result := Result + BytesTo64BaseString(Buf, 4, -4);
  1243. Buf := Reverse2(RowId.ridslotnum);
  1244. Result := Result + BytesTo64BaseString(Buf, 2, -2);
  1245. end;
  1246. end;
  1247. function RowId81ToString(RowIdPtr: PRowId81): string;
  1248. var
  1249. Bytes: TBytes;
  1250. begin
  1251. if RowIdPtr.ridobjnum = 0 then begin
  1252. // restricted (Oracle 7)
  1253. Result :=
  1254. IntToHex(BitConverter.ToInt32(Reverse4(RowIdPtr.ridblocknum), 0), 8) + '.' +
  1255. IntToHex(BitConverter.ToInt16(Reverse2(RowIdPtr.ridslotnum), 0), 4) + '.' +
  1256. IntToHex(BitConverter.ToInt16(Reverse2(RowIdPtr.ridfilenum), 0), 4)
  1257. end
  1258. else begin
  1259. // extended (Oracle 8 and higher)
  1260. SetLength(Bytes, 4);
  1261. Marshal.Copy(IntPtr(Integer(IntPtr(RowIdPtr)) + 1{TRowId81.ridobjnum}), Bytes, 0, 4);
  1262. Result := BytesTo64BaseString(Bytes, 4, -4);
  1263. Marshal.Copy(IntPtr(Integer(IntPtr(RowIdPtr)) + 5{TRowId81.ridfilenum}), Bytes, 0, 2);
  1264. Result := Result + BytesTo64BaseString(Bytes, 2, -2);
  1265. Marshal.Copy(IntPtr(Integer(IntPtr(RowIdPtr)) + 7{TRowId81.ridblocknum}), Bytes, 0, 4);
  1266. Result := Result + BytesTo64BaseString(Bytes, 4, -4);
  1267. Marshal.Copy(IntPtr(Integer(IntPtr(RowIdPtr)) + 11{TRowId81.ridslotnum}), Bytes, 0, 2);
  1268. Result := Result + BytesTo64BaseString(Bytes, 2, -2);
  1269. end;
  1270. end;
  1271. function URowIdToString(RowIdPtr: PRowId81; Length: integer): string;
  1272. var
  1273. Bytes: TBytes;
  1274. begin
  1275. SetLength(Bytes, Length - 1);
  1276. Marshal.Copy(IntPtr(Integer(IntPtr(RowIdPtr)) + 1), Bytes, 0, Length - 1);
  1277. Result := '*' + BytesTo64BaseString(Bytes, Length - 1, 0);
  1278. end;
  1279. { Data convertion }
  1280. function OraDateToDateTime(Buf: IntPtr): TDateTime;
  1281. var
  1282. Time: TDateTime;
  1283. OraDate: TBytes;
  1284. begin
  1285. SetLength(OraDate, 7);
  1286. Marshal.Copy(Buf, OraDate, 0, 7);
  1287. Result := EncodeDate(Abs((OraDate[0] - 100) * 100 + OraDate[1] - 100),
  1288. OraDate[2], OraDate[3]);
  1289. Time := EncodeTime(OraDate[4] - 1, OraDate[5] - 1, OraDate[6] - 1, 0);
  1290. if Result < 0 then
  1291. Result := Result - Time
  1292. else
  1293. Result := Result + Time;
  1294. end;
  1295. function OraDateToMSecs(Buf: IntPtr): double;
  1296. begin
  1297. Result := TimeStampToMSecs(DateTimeToTimeStamp(OraDateToDateTime(Buf)));//{$IFNDEF CLR}BitConverter.DoubleToInt64Bits{$ENDIF}
  1298. end;
  1299. procedure DateTimeToOraDate(DateTime: TDateTime; Buf: IntPtr);
  1300. var
  1301. Year, Month, Day, Hour, Min, Sec, MSec: word;
  1302. OraDate: TBytes;
  1303. begin
  1304. SetLength(OraDate, 8);
  1305. DecodeDate(DateTime, Year, Month, Day);
  1306. DecodeTime(DateTime, Hour, Min, Sec, MSec);
  1307. OraDate[0] := Year div 100 + 100;

Large files files are truncated, but you can click here to view the full file