2
0

db.pp 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt, member of the
  4. Free Pascal development team
  5. DB header file with interface section.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit db;
  13. {$mode objfpc}
  14. {$h+}
  15. interface
  16. uses Classes,Sysutils,Variants;
  17. const
  18. dsMaxBufferCount = MAXINT div 8;
  19. dsMaxStringSize = 8192;
  20. // Used in AsBoolean for string fields to determine
  21. // whether it's true or false.
  22. YesNoChars : Array[Boolean] of char = ('N', 'Y');
  23. SQLDelimiterCharacters = [';',',',' ','(',')',#13,#10,#9];
  24. type
  25. {LargeInt}
  26. LargeInt = Int64;
  27. PLargeInt= ^LargeInt;
  28. { Auxiliary type }
  29. TStringFieldBuffer = Array[0..dsMaxStringSize] of Char;
  30. { Misc Dataset types }
  31. TDataSetState = (dsInactive, dsBrowse, dsEdit, dsInsert, dsSetKey,
  32. dsCalcFields, dsFilter, dsNewValue, dsOldValue, dsCurValue, dsBlockRead,
  33. dsInternalCalc, dsOpening);
  34. TDataEvent = (deFieldChange, deRecordChange, deDataSetChange,
  35. deDataSetScroll, deLayoutChange, deUpdateRecord, deUpdateState,
  36. deCheckBrowseMode, dePropertyChange, deFieldListChange, deFocusControl,
  37. deParentScroll,deConnectChange,deReconcileError,deDisabledStateChange);
  38. TUpdateStatus = (usUnmodified, usModified, usInserted, usDeleted);
  39. TUpdateStatusSet = SET OF TUpdateStatus;
  40. TUpdateMode = (upWhereAll, upWhereChanged, upWhereKeyOnly);
  41. TResolverResponse = (rrSkip, rrAbort, rrMerge, rrApply, rrIgnore);
  42. TProviderFlag = (pfInUpdate, pfInWhere, pfInKey, pfHidden);
  43. TProviderFlags = set of TProviderFlag;
  44. { Forward declarations }
  45. TFieldDef = class;
  46. TFieldDefs = class;
  47. TField = class;
  48. TFields = Class;
  49. TDataSet = class;
  50. TDataBase = Class;
  51. TDatasource = Class;
  52. TDatalink = Class;
  53. TDBTransaction = Class;
  54. { Exception classes }
  55. EDatabaseError = class(Exception);
  56. EUpdateError = class(EDatabaseError)
  57. private
  58. FContext : String;
  59. FErrorCode : integer;
  60. FOriginalException : Exception;
  61. FPreviousError : Integer;
  62. public
  63. constructor Create(NativeError, Context : String;
  64. ErrCode, PrevError : integer; E: Exception);
  65. Destructor Destroy;
  66. property Context : String read FContext;
  67. property ErrorCode : integer read FErrorcode;
  68. property OriginalExcaption : Exception read FOriginalException;
  69. property PreviousError : Integer read FPreviousError;
  70. end;
  71. { TFieldDef }
  72. TFieldClass = class of TField;
  73. {
  74. TFieldType = (ftUnknown, ftString, ftSmallint, ftInteger, ftWord,
  75. ftBoolean, ftFloat, ftDate, ftTime, ftDateTime,
  76. ftBytes, ftVarBytes, ftAutoInc, ftBlob, ftMemo, ftGraphic,
  77. ftFmtMemo, ftParadoxOle, ftDBaseOle, ftTypedBinary, ftCursor);
  78. }
  79. TFieldType = (ftUnknown, ftString, ftSmallint, ftInteger, ftWord,
  80. ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate, ftTime, ftDateTime,
  81. ftBytes, ftVarBytes, ftAutoInc, ftBlob, ftMemo, ftGraphic, ftFmtMemo,
  82. ftParadoxOle, ftDBaseOle, ftTypedBinary, ftCursor, ftFixedChar,
  83. ftWideString, ftLargeint, ftADT, ftArray, ftReference,
  84. ftDataSet, ftOraBlob, ftOraClob, ftVariant, ftInterface,
  85. ftIDispatch, ftGuid, ftTimeStamp, ftFMTBcd);
  86. { TDateTimeRec }
  87. TDateTimeAlias = type TDateTime;
  88. PDateTimeRec = ^TdateTimeRec;
  89. TDateTimeRec = record
  90. case TFieldType of
  91. ftDate: (Date: Longint);
  92. ftTime: (Time: Longint);
  93. ftDateTime: (DateTime: TDateTimeAlias);
  94. end;
  95. TFieldAttribute = (faHiddenCol, faReadonly, faRequired, faLink, faUnNamed, faFixed);
  96. TFieldAttributes = set of TFieldAttribute;
  97. { TFieldDef }
  98. TFieldDef = class(TCollectionItem)
  99. Private
  100. FDataType : TFieldType;
  101. FFieldNo : Longint;
  102. FInternalCalcField : Boolean;
  103. FPrecision : Longint;
  104. FRequired : Boolean;
  105. FSize : Word;
  106. FName : String;
  107. FDisplayName : String;
  108. FAttributes : TFieldAttributes;
  109. Function GetFieldClass : TFieldClass;
  110. procedure SetAttributes(AValue: TFieldAttributes);
  111. procedure SetDataType(AValue: TFieldType);
  112. procedure SetPrecision(const AValue: Longint);
  113. procedure SetSize(const AValue: Word);
  114. procedure SetRequired(const AValue: Boolean);
  115. protected
  116. function GetDisplayName: string; override;
  117. procedure SetDisplayName(const AValue: string); override;
  118. public
  119. constructor Create(AOwner: TFieldDefs; const AName: string;
  120. ADataType: TFieldType; ASize: Word; ARequired: Boolean; AFieldNo: Longint);
  121. destructor Destroy; override;
  122. procedure Assign(APersistent: TPersistent); override;
  123. function CreateField(AOwner: TComponent): TField;
  124. property FieldClass: TFieldClass read GetFieldClass;
  125. property FieldNo: Longint read FFieldNo;
  126. property InternalCalcField: Boolean read FInternalCalcField write FInternalCalcField;
  127. property Required: Boolean read FRequired write SetRequired;
  128. Published
  129. property Attributes: TFieldAttributes read FAttributes write SetAttributes default [];
  130. property Name: string read FName write FName; // Must move to TNamedItem
  131. property DisplayName : string read FDisplayName write FDisplayName; // Must move to TNamedItem
  132. property DataType: TFieldType read FDataType write SetDataType;
  133. property Precision: Longint read FPrecision write SetPrecision;
  134. property Size: Word read FSize write SetSize;
  135. end;
  136. { TFieldDefs }
  137. TFieldDefs = class(TOwnedCollection)
  138. private
  139. FUpdated: Boolean;
  140. FHiddenFields : Boolean;
  141. function GetItem(Index: Longint): TFieldDef;
  142. function GetDataset: TDataset;
  143. procedure SetItem(Index: Longint; const AValue: TFieldDef);
  144. protected
  145. procedure SetItemName(AItem: TCollectionItem); override;
  146. public
  147. constructor Create(ADataSet: TDataSet);
  148. // destructor Destroy; override;
  149. procedure Add(const AName: string; ADataType: TFieldType; ASize: Word; ARequired: Boolean);
  150. procedure Add(const AName: string; ADataType: TFieldType; ASize: Word);
  151. procedure Add(const AName: string; ADataType: TFieldType);
  152. Function AddFieldDef : TFieldDef;
  153. procedure Assign(FieldDefs: TFieldDefs);
  154. // procedure Clear;
  155. // procedure Delete(Index: Longint);
  156. function Find(const AName: string): TFieldDef;
  157. function IndexOf(const AName: string): Longint;
  158. procedure Update;
  159. Property HiddenFields : Boolean Read FHiddenFields Write FHiddenFields;
  160. property Items[Index: Longint]: TFieldDef read GetItem write SetItem; default;
  161. property Dataset: TDataset read GetDataset;
  162. property Updated: Boolean read FUpdated write FUpdated;
  163. end;
  164. { TField }
  165. TFieldKind = (fkData, fkCalculated, fkLookup, fkInternalCalc);
  166. TFieldKinds = Set of TFieldKind;
  167. TFieldNotifyEvent = procedure(Sender: TField) of object;
  168. TFieldGetTextEvent = procedure(Sender: TField; var Text: string;
  169. DisplayText: Boolean) of object;
  170. TFieldSetTextEvent = procedure(Sender: TField; const Text: string) of object;
  171. TFieldRef = ^TField;
  172. TFieldChars = set of Char;
  173. PLookupListRec = ^TLookupListRec;
  174. TLookupListRec = record
  175. Key: Variant;
  176. Value: Variant;
  177. end;
  178. { TLookupList }
  179. TLookupList = class(TObject)
  180. private
  181. FList: TList;
  182. public
  183. constructor Create;
  184. destructor Destroy; override;
  185. procedure Add(const AKey, AValue: Variant);
  186. procedure Clear;
  187. function ValueOfKey(const AKey: Variant): Variant;
  188. end;
  189. { TField }
  190. TField = class(TComponent)
  191. Private
  192. FAlignMent : TAlignment;
  193. FAttributeSet : String;
  194. FCalculated : Boolean;
  195. FConstraintErrorMessage : String;
  196. FCustomConstraint : String;
  197. FDataSet : TDataSet;
  198. // FDataSize : Word;
  199. FDataType : TFieldType;
  200. FDefaultExpression : String;
  201. FDisplayLabel : String;
  202. FDisplayWidth : Longint;
  203. FFieldKind : TFieldKind;
  204. FFieldName : String;
  205. FFieldNo : Longint;
  206. FFields : TFields;
  207. FHasConstraints : Boolean;
  208. FImportedConstraint : String;
  209. FIsIndexField : Boolean;
  210. FKeyFields : String;
  211. FLookupCache : Boolean;
  212. FLookupDataSet : TDataSet;
  213. FLookupKeyfields : String;
  214. FLookupresultField : String;
  215. FLookupList: TLookupList;
  216. FOffset : Word;
  217. FOnChange : TFieldNotifyEvent;
  218. FOnGetText: TFieldGetTextEvent;
  219. FOnSetText: TFieldSetTextEvent;
  220. FOnValidate: TFieldNotifyEvent;
  221. FOrigin : String;
  222. FReadOnly : Boolean;
  223. FRequired : Boolean;
  224. FSize : Word;
  225. FValidChars : TFieldChars;
  226. FValueBuffer : Pointer;
  227. FValidating : Boolean;
  228. FVisible : Boolean;
  229. FProviderFlags : TProviderFlags;
  230. Function GetIndex : longint;
  231. procedure SetAlignment(const AValue: TAlignMent);
  232. procedure SetIndex(AValue: Integer);
  233. Procedure SetDataset(AValue : TDataset);
  234. function GetDisplayText: String;
  235. function GetEditText: String;
  236. procedure SetEditText(const AValue: string);
  237. procedure SetDisplayLabel(const AValue: string);
  238. procedure SetDisplayWidth(const AValue: Longint);
  239. function GetDisplayWidth: integer;
  240. procedure SetReadOnly(const AValue: Boolean);
  241. procedure SetVisible(const AValue: Boolean);
  242. function IsDisplayStored : Boolean;
  243. function GetLookupList: TLookupList;
  244. procedure CalcLookupValue;
  245. protected
  246. function AccessError(const TypeName: string): EDatabaseError;
  247. procedure CheckInactive;
  248. class procedure CheckTypeSize(AValue: Longint); virtual;
  249. procedure Change; virtual;
  250. procedure DataChanged;
  251. procedure FreeBuffers; virtual;
  252. function GetAsBoolean: Boolean; virtual;
  253. function GetAsCurrency: Currency; virtual;
  254. function GetAsLargeInt: LargeInt; virtual;
  255. function GetAsDateTime: TDateTime; virtual;
  256. function GetAsFloat: Double; virtual;
  257. function GetAsLongint: Longint; virtual;
  258. function GetAsInteger: Longint; virtual;
  259. function GetAsVariant: variant; virtual;
  260. function GetOldValue: variant; virtual;
  261. function GetAsString: string; virtual;
  262. function GetCanModify: Boolean; virtual;
  263. function GetDataSize: Word; virtual;
  264. function GetDefaultWidth: Longint; virtual;
  265. function GetDisplayName : String;
  266. function GetCurValue: Variant; virtual;
  267. function GetNewValue: Variant; virtual;
  268. function GetIsNull: Boolean; virtual;
  269. function GetParentComponent: TComponent; override;
  270. procedure GetText(var AText: string; ADisplayText: Boolean); virtual;
  271. function HasParent: Boolean; override;
  272. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  273. procedure PropertyChanged(LayoutAffected: Boolean);
  274. procedure ReadState(Reader: TReader); override;
  275. procedure SetAsBoolean(AValue: Boolean); virtual;
  276. procedure SetAsCurrency(AValue: Currency); virtual;
  277. procedure SetAsDateTime(AValue: TDateTime); virtual;
  278. procedure SetAsFloat(AValue: Double); virtual;
  279. procedure SetAsLongint(AValue: Longint); virtual;
  280. procedure SetAsInteger(AValue: Integer); virtual;
  281. procedure SetAsLargeint(AValue: Largeint); virtual;
  282. procedure SetAsVariant(AValue: variant); virtual;
  283. procedure SetAsString(const AValue: string); virtual;
  284. procedure SetDataType(AValue: TFieldType);
  285. procedure SetNewValue(const AValue: Variant);
  286. procedure SetSize(AValue: Word); virtual;
  287. procedure SetParentComponent(AParent: TComponent); override;
  288. procedure SetText(const AValue: string); virtual;
  289. procedure SetVarValue(const AValue: Variant); virtual;
  290. public
  291. constructor Create(AOwner: TComponent); override;
  292. destructor Destroy; override;
  293. procedure Assign(Source: TPersistent); override;
  294. procedure AssignValue(const AValue: TVarRec);
  295. procedure Clear; virtual;
  296. procedure FocusControl;
  297. function GetData(Buffer: Pointer): Boolean;
  298. function GetData(Buffer: Pointer; NativeFormat : Boolean): Boolean;
  299. class function IsBlob: Boolean; virtual;
  300. function IsValidChar(InputChar: Char): Boolean; virtual;
  301. procedure RefreshLookupList;
  302. procedure SetData(Buffer: Pointer);
  303. procedure SetData(Buffer: Pointer; NativeFormat : Boolean);
  304. procedure SetFieldType(AValue: TFieldType); virtual;
  305. procedure Validate(Buffer: Pointer);
  306. property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
  307. property AsCurrency: Currency read GetAsCurrency write SetAsCurrency;
  308. property AsDateTime: TDateTime read GetAsDateTime write SetAsDateTime;
  309. property AsFloat: Double read GetAsFloat write SetAsFloat;
  310. property AsLongint: Longint read GetAsLongint write SetAsLongint;
  311. property AsLargeInt: LargeInt read GetAsLargeInt write SetAsLargeInt;
  312. property AsInteger: Integer read GetAsInteger write SetAsInteger;
  313. property AsString: string read GetAsString write SetAsString;
  314. property AsVariant: variant read GetAsVariant write SetAsVariant;
  315. property AttributeSet: string read FAttributeSet write FAttributeSet;
  316. property Calculated: Boolean read FCalculated write FCalculated;
  317. property CanModify: Boolean read GetCanModify;
  318. property CurValue: Variant read GetCurValue;
  319. property DataSet: TDataSet read FDataSet write SetDataSet;
  320. property DataSize: Word read GetDataSize;
  321. property DataType: TFieldType read FDataType;
  322. property DisplayName: String Read GetDisplayName;
  323. property DisplayText: String read GetDisplayText;
  324. property FieldNo: Longint read FFieldNo;
  325. property IsIndexField: Boolean read FIsIndexField;
  326. property IsNull: Boolean read GetIsNull;
  327. property NewValue: Variant read GetNewValue write SetNewValue;
  328. property Offset: word read FOffset;
  329. property Size: Word read FSize write FSize;
  330. property Text: string read GetEditText write SetEditText;
  331. property ValidChars : TFieldChars Read FValidChars;
  332. property Value: variant read GetAsVariant write SetAsVariant;
  333. property OldValue: variant read GetOldValue;
  334. property LookupList: TLookupList read GetLookupList;
  335. published
  336. property AlignMent : TAlignMent Read FAlignMent write SetAlignment default taLeftJustify;
  337. property CustomConstraint: string read FCustomConstraint write FCustomConstraint;
  338. property ConstraintErrorMessage: string read FConstraintErrorMessage write FConstraintErrorMessage;
  339. property DefaultExpression: string read FDefaultExpression write FDefaultExpression;
  340. property DisplayLabel : string read GetDisplayName write SetDisplayLabel stored IsDisplayStored;
  341. property DisplayWidth: Longint read GetDisplayWidth write SetDisplayWidth;
  342. property FieldKind: TFieldKind read FFieldKind write FFieldKind;
  343. property FieldName: string read FFieldName write FFieldName;
  344. property HasConstraints: Boolean read FHasConstraints;
  345. property Index: Longint read GetIndex write SetIndex;
  346. property ImportedConstraint: string read FImportedConstraint write FImportedConstraint;
  347. property LookupDataSet: TDataSet read FLookupDataSet write FLookupDataSet;
  348. property LookupKeyFields: string read FLookupKeyFields write FLookupKeyFields;
  349. property LookupResultField: string read FLookupResultField write FLookupResultField;
  350. property KeyFields: string read FKeyFields write FKeyFields;
  351. property LookupCache: Boolean read FLookupCache write FLookupCache;
  352. property Origin: string read FOrigin write FOrigin;
  353. property ProviderFlags : TProviderFlags read FProviderFlags write FProviderFlags;
  354. property ReadOnly: Boolean read FReadOnly write SetReadOnly;
  355. property Required: Boolean read FRequired write FRequired;
  356. property Visible: Boolean read FVisible write SetVisible default True;
  357. property OnChange: TFieldNotifyEvent read FOnChange write FOnChange;
  358. property OnGetText: TFieldGetTextEvent read FOnGetText write FOnGetText;
  359. property OnSetText: TFieldSetTextEvent read FOnSetText write FOnSetText;
  360. property OnValidate: TFieldNotifyEvent read FOnValidate write FOnValidate;
  361. end;
  362. { TStringField }
  363. TStringField = class(TField)
  364. private
  365. FFixedChar : boolean;
  366. FTransliterate : Boolean;
  367. protected
  368. class procedure CheckTypeSize(AValue: Longint); override;
  369. function GetAsBoolean: Boolean; override;
  370. function GetAsDateTime: TDateTime; override;
  371. function GetAsFloat: Double; override;
  372. function GetAsLongint: Longint; override;
  373. function GetAsString: string; override;
  374. function GetAsVariant: variant; override;
  375. function GetDataSize: Word; override;
  376. function GetDefaultWidth: Longint; override;
  377. procedure GetText(var AText: string; ADisplayText: Boolean); override;
  378. function GetValue(var AValue: string): Boolean;
  379. procedure SetAsBoolean(AValue: Boolean); override;
  380. procedure SetAsDateTime(AValue: TDateTime); override;
  381. procedure SetAsFloat(AValue: Double); override;
  382. procedure SetAsLongint(AValue: Longint); override;
  383. procedure SetAsString(const AValue: string); override;
  384. procedure SetVarValue(const AValue: Variant); override;
  385. public
  386. constructor Create(AOwner: TComponent); override;
  387. property FixedChar : Boolean read FFixedChar write FFixedChar;
  388. property Transliterate: Boolean read FTransliterate write FTransliterate;
  389. published
  390. property Size default 20;
  391. end;
  392. { TNumericField }
  393. TNumericField = class(TField)
  394. Private
  395. FDisplayFormat : String;
  396. FEditFormat : String;
  397. protected
  398. procedure RangeError(AValue, Min, Max: Double);
  399. procedure SetDisplayFormat(const AValue: string);
  400. procedure SetEditFormat(const AValue: string);
  401. public
  402. constructor Create(AOwner: TComponent); override;
  403. published
  404. property DisplayFormat: string read FDisplayFormat write SetDisplayFormat;
  405. property EditFormat: string read FEditFormat write SetEditFormat;
  406. end;
  407. { TLongintField }
  408. TLongintField = class(TNumericField)
  409. private
  410. FMinValue,
  411. FMaxValue,
  412. FMinRange,
  413. FMAxRange : Longint;
  414. Procedure SetMinValue (AValue : longint);
  415. Procedure SetMaxValue (AValue : longint);
  416. protected
  417. function GetAsFloat: Double; override;
  418. function GetAsLongint: Longint; override;
  419. function GetAsString: string; override;
  420. function GetAsVariant: variant; override;
  421. function GetDataSize: Word; override;
  422. procedure GetText(var AText: string; ADisplayText: Boolean); override;
  423. function GetValue(var AValue: Longint): Boolean;
  424. procedure SetAsFloat(AValue: Double); override;
  425. procedure SetAsLongint(AValue: Longint); override;
  426. procedure SetAsString(const AValue: string); override;
  427. procedure SetVarValue(const AValue: Variant); override;
  428. public
  429. constructor Create(AOwner: TComponent); override;
  430. Function CheckRange(AValue : longint) : Boolean;
  431. property Value: Longint read GetAsLongint write SetAsLongint;
  432. published
  433. property MaxValue: Longint read FMaxValue write SetMaxValue default 0;
  434. property MinValue: Longint read FMinValue write SetMinValue default 0;
  435. end;
  436. TIntegerField = TLongintField;
  437. { TLargeintField }
  438. TLargeintField = class(TNumericField)
  439. private
  440. FMinValue,
  441. FMaxValue,
  442. FMinRange,
  443. FMAxRange : Largeint;
  444. Procedure SetMinValue (AValue : Largeint);
  445. Procedure SetMaxValue (AValue : Largeint);
  446. protected
  447. function GetAsFloat: Double; override;
  448. function GetAsLongint: Longint; override;
  449. function GetAsLargeint: Largeint; override;
  450. function GetAsString: string; override;
  451. function GetAsVariant: variant; override;
  452. function GetDataSize: Word; override;
  453. procedure GetText(var AText: string; ADisplayText: Boolean); override;
  454. function GetValue(var AValue: Largeint): Boolean;
  455. procedure SetAsFloat(AValue: Double); override;
  456. procedure SetAsLongint(AValue: Longint); override;
  457. procedure SetAsLargeint(AValue: Largeint); override;
  458. procedure SetAsString(const AValue: string); override;
  459. procedure SetVarValue(const AValue: Variant); override;
  460. public
  461. constructor Create(AOwner: TComponent); override;
  462. Function CheckRange(AValue : largeint) : Boolean;
  463. property Value: Longint read GetAsLongint write SetAsLongint;
  464. published
  465. property MaxValue: Largeint read FMaxValue write SetMaxValue default 0;
  466. property MinValue: Largeint read FMinValue write SetMinValue default 0;
  467. end;
  468. { TSmallintField }
  469. TSmallintField = class(TLongintField)
  470. protected
  471. function GetDataSize: Word; override;
  472. public
  473. constructor Create(AOwner: TComponent); override;
  474. end;
  475. { TWordField }
  476. TWordField = class(TLongintField)
  477. protected
  478. function GetDataSize: Word; override;
  479. public
  480. constructor Create(AOwner: TComponent); override;
  481. end;
  482. { TAutoIncField }
  483. TAutoIncField = class(TLongintField)
  484. Protected
  485. Procedure SetAsLongInt(AValue : Longint); override;
  486. public
  487. constructor Create(AOwner: TComponent); override;
  488. end;
  489. { TFloatField }
  490. TFloatField = class(TNumericField)
  491. private
  492. FMaxValue : Double;
  493. FMinValue : Double;
  494. FPrecision : Longint;
  495. protected
  496. function GetAsFloat: Double; override;
  497. function GetAsLongint: Longint; override;
  498. function GetAsVariant: variant; override;
  499. function GetAsString: string; override;
  500. function GetDataSize: Word; override;
  501. procedure GetText(var theText: string; ADisplayText: Boolean); override;
  502. procedure SetAsFloat(AValue: Double); override;
  503. procedure SetAsLongint(AValue: Longint); override;
  504. procedure SetAsString(const AValue: string); override;
  505. procedure SetVarValue(const AValue: Variant); override;
  506. public
  507. constructor Create(AOwner: TComponent); override;
  508. Function CheckRange(AValue : Double) : Boolean;
  509. property Value: Double read GetAsFloat write SetAsFloat;
  510. published
  511. property MaxValue: Double read FMaxValue write FMaxValue;
  512. property MinValue: Double read FMinValue write FMinValue;
  513. property Precision: Longint read FPrecision write FPrecision default 15;
  514. end;
  515. { TCurrencyField }
  516. TCurrencyField = class(TFloatField)
  517. public
  518. constructor Create(AOwner: TComponent); override;
  519. procedure GetText(var TheText: string; ADisplayText: Boolean); override;
  520. end;
  521. { TBooleanField }
  522. TBooleanField = class(TField)
  523. private
  524. FDisplayValues : String;
  525. // First byte indicates uppercase or not.
  526. FDisplays : Array[Boolean,Boolean] of string;
  527. Procedure SetDisplayValues(AValue : String);
  528. protected
  529. function GetAsBoolean: Boolean; override;
  530. function GetAsString: string; override;
  531. function GetAsVariant: variant; override;
  532. function GetDataSize: Word; override;
  533. function GetDefaultWidth: Longint; override;
  534. procedure SetAsBoolean(AValue: Boolean); override;
  535. procedure SetAsString(const AValue: string); override;
  536. procedure SetVarValue(const AValue: Variant); override;
  537. public
  538. constructor Create(AOwner: TComponent); override;
  539. property Value: Boolean read GetAsBoolean write SetAsBoolean;
  540. published
  541. property DisplayValues: string read FDisplayValues write SetDisplayValues;
  542. end;
  543. { TDateTimeField }
  544. TDateTimeField = class(TField)
  545. private
  546. FDisplayFormat : String;
  547. procedure SetDisplayFormat(const AValue: string);
  548. protected
  549. function GetAsDateTime: TDateTime; override;
  550. function GetAsFloat: Double; override;
  551. function GetAsString: string; override;
  552. function GetAsVariant: variant; override;
  553. function GetDataSize: Word; override;
  554. procedure GetText(var theText: string; ADisplayText: Boolean); override;
  555. procedure SetAsDateTime(AValue: TDateTime); override;
  556. procedure SetAsFloat(AValue: Double); override;
  557. procedure SetAsString(const AValue: string); override;
  558. procedure SetVarValue(const AValue: Variant); override;
  559. public
  560. constructor Create(AOwner: TComponent); override;
  561. property Value: TDateTime read GetAsDateTime write SetAsDateTime;
  562. published
  563. property DisplayFormat: string read FDisplayFormat write SetDisplayFormat;
  564. end;
  565. { TDateField }
  566. TDateField = class(TDateTimeField)
  567. public
  568. constructor Create(AOwner: TComponent); override;
  569. end;
  570. { TTimeField }
  571. TTimeField = class(TDateTimeField)
  572. protected
  573. procedure SetAsString(const AValue: string); override;
  574. public
  575. constructor Create(AOwner: TComponent); override;
  576. end;
  577. { TBinaryField }
  578. TBinaryField = class(TField)
  579. protected
  580. class procedure CheckTypeSize(AValue: Longint); override;
  581. function GetAsString: string; override;
  582. procedure GetText(var TheText: string; ADisplayText: Boolean); override;
  583. procedure SetAsString(const AValue: string); override;
  584. procedure SetText(const AValue: string); override;
  585. procedure SetVarValue(const AValue: Variant); override;
  586. public
  587. constructor Create(AOwner: TComponent); override;
  588. published
  589. property Size default 16;
  590. end;
  591. { TBytesField }
  592. TBytesField = class(TBinaryField)
  593. protected
  594. function GetDataSize: Word; override;
  595. public
  596. constructor Create(AOwner: TComponent); override;
  597. end;
  598. { TVarBytesField }
  599. TVarBytesField = class(TBytesField)
  600. protected
  601. function GetDataSize: Word; override;
  602. public
  603. constructor Create(AOwner: TComponent); override;
  604. end;
  605. { TBCDField }
  606. TBCDField = class(TNumericField)
  607. private
  608. FMinValue,
  609. FMaxValue : currency;
  610. FPrecision : Longint;
  611. FCurrency : boolean;
  612. protected
  613. class procedure CheckTypeSize(AValue: Longint); override;
  614. function GetAsCurrency: Currency; override;
  615. function GetAsFloat: Double; override;
  616. function GetAsLongint: Longint; override;
  617. function GetAsString: string; override;
  618. function GetValue(var AValue: Currency): Boolean;
  619. function GetAsVariant: variant; override;
  620. function GetDataSize: Word; override;
  621. function GetDefaultWidth: Longint; override;
  622. procedure GetText(var TheText: string; ADisplayText: Boolean); override;
  623. procedure SetAsFloat(AValue: Double); override;
  624. procedure SetAsLongint(AValue: Longint); override;
  625. procedure SetAsString(const AValue: string); override;
  626. procedure SetAsCurrency(AValue: Currency); override;
  627. procedure SetVarValue(const AValue: Variant); override;
  628. public
  629. constructor Create(AOwner: TComponent); override;
  630. Function CheckRange(AValue : Currency) : Boolean;
  631. property Value: Longint read GetAsLongint write SetAsLongint;
  632. published
  633. property Precision: Longint read FPrecision write FPrecision;
  634. property Currency: Boolean read FCurrency write FCurrency;
  635. property MaxValue: Currency read FMaxValue write FMaxValue;
  636. property MinValue: Currency read FMinValue write FMinValue;
  637. property Size default 4;
  638. end;
  639. { TBlobField }
  640. TBlobStreamMode = (bmRead, bmWrite, bmReadWrite);
  641. TBlobType = ftBlob..ftTypedBinary;
  642. TBlobField = class(TField)
  643. private
  644. FBlobSize : Longint;
  645. FBlobType : TBlobType;
  646. FModified : Boolean;
  647. FTransliterate : Boolean;
  648. Function GetBlobStream (Mode : TBlobStreamMode) : TStream;
  649. protected
  650. procedure AssignTo(Dest: TPersistent); override;
  651. procedure FreeBuffers; override;
  652. function GetAsString: string; override;
  653. function GetAsVariant: Variant; override;
  654. function GetBlobSize: Longint; virtual;
  655. function GetIsNull: Boolean; override;
  656. procedure GetText(var TheText: string; ADisplayText: Boolean); override;
  657. procedure SetAsString(const AValue: string); override;
  658. procedure SetText(const AValue: string); override;
  659. procedure SetVarValue(const AValue: Variant); override;
  660. public
  661. constructor Create(AOwner: TComponent); override;
  662. procedure Assign(Source: TPersistent); override;
  663. procedure Clear; override;
  664. class function IsBlob: Boolean; override;
  665. procedure LoadFromFile(const FileName: string);
  666. procedure LoadFromStream(Stream: TStream);
  667. procedure SaveToFile(const FileName: string);
  668. procedure SaveToStream(Stream: TStream);
  669. procedure SetFieldType(AValue: TFieldType); override;
  670. property BlobSize: Longint read FBlobSize;
  671. property Modified: Boolean read FModified write FModified;
  672. property Value: string read GetAsString write SetAsString;
  673. property Transliterate: Boolean read FTransliterate write FTransliterate;
  674. published
  675. property BlobType: TBlobType read FBlobType write FBlobType;
  676. property Size default 0;
  677. end;
  678. { TMemoField }
  679. TMemoField = class(TBlobField)
  680. public
  681. constructor Create(AOwner: TComponent); override;
  682. published
  683. property Transliterate default True;
  684. end;
  685. { TGraphicField }
  686. TGraphicField = class(TBlobField)
  687. public
  688. constructor Create(AOwner: TComponent); override;
  689. end;
  690. { TIndexDef }
  691. TIndexDefs = class;
  692. TIndexOption = (ixPrimary, ixUnique, ixDescending, ixCaseInsensitive,
  693. ixExpression, ixNonMaintained);
  694. TIndexOptions = set of TIndexOption;
  695. TIndexDef = class(TCollectionItem)
  696. Private
  697. FExpression : String;
  698. FFields : String;
  699. FName : String;
  700. FOptions : TIndexOptions;
  701. FSource : String;
  702. public
  703. constructor Create(Owner: TIndexDefs; const AName, TheFields: string;
  704. TheOptions: TIndexOptions);
  705. destructor Destroy; override;
  706. property Expression: string read FExpression;
  707. property Fields: string read FFields write FFields;
  708. property Name: string read FName write FName;
  709. property Options: TIndexOptions read FOptions write FOptions;
  710. property Source: string read FSource write FSource;
  711. end;
  712. { TIndexDefs }
  713. TIndexDefs = class(TOwnedCollection)
  714. Private
  715. FUpDated : Boolean;
  716. FDataset : Tdataset;
  717. Function GetItem(Index: Integer): TIndexDef;
  718. Procedure SetItem(Index: Integer; Value: TIndexDef);
  719. public
  720. constructor Create(DataSet: TDataSet); overload;
  721. destructor Destroy; override;
  722. procedure Add(const Name, Fields: string; Options: TIndexOptions);
  723. Function AddIndexDef: TIndexDef;
  724. procedure Assign(IndexDefs: TIndexDefs);
  725. // procedure Clear;
  726. function Find(const IndexName: string): TIndexDef;
  727. function FindIndexForFields(const Fields: string): TIndexDef;
  728. function GetIndexForFields(const Fields: string;
  729. CaseInsensitive: Boolean): TIndexDef;
  730. function IndexOf(const Name: string): Longint;
  731. procedure Update;
  732. // property Count: Longint read FCount;
  733. Property Items[Index: Integer] : TIndexDef read GetItem write SetItem; default;
  734. property Updated: Boolean read FUpdated write FUpdated;
  735. end;
  736. { TCheckConstraint }
  737. TCheckConstraint = class(TCollectionItem)
  738. Private
  739. FCustomConstraint : String;
  740. FErrorMessage : String;
  741. FFromDictionary : Boolean;
  742. FImportedConstraint : String;
  743. public
  744. procedure Assign(Source: TPersistent); override;
  745. // function GetDisplayName: string; override;
  746. published
  747. property CustomConstraint: string read FCustomConstraint write FCustomConstraint;
  748. property ErrorMessage: string read FErrorMessage write FErrorMessage;
  749. property FromDictionary: Boolean read FFromDictionary write FFromDictionary;
  750. property ImportedConstraint: string read FImportedConstraint write FImportedConstraint;
  751. end;
  752. { TCheckConstraints }
  753. TCheckConstraints = class(TCollection)
  754. Private
  755. Function GetItem(Index : Longint) : TCheckConstraint;
  756. Procedure SetItem(index : Longint; Value : TCheckConstraint);
  757. protected
  758. function GetOwner: TPersistent; override;
  759. public
  760. constructor Create(AOwner: TPersistent);
  761. function Add: TCheckConstraint;
  762. property Items[Index: Longint]: TCheckConstraint read GetItem write SetItem; default;
  763. end;
  764. { TFields }
  765. Tfields = Class(TObject)
  766. Private
  767. FDataset : TDataset;
  768. FFieldList : TList;
  769. FOnChange : TNotifyEvent;
  770. FValidFieldKinds : TFieldKinds;
  771. Protected
  772. Procedure Changed;
  773. Procedure CheckfieldKind(Fieldkind : TFieldKind; Field : TField);
  774. Function GetCount : Longint;
  775. Function GetField (Index : longint) : TField;
  776. Procedure SetField(Index: Integer; Value: TField);
  777. Procedure SetFieldIndex (Field : TField;Value : Integer);
  778. Property OnChange : TNotifyEvent Read FOnChange Write FOnChange;
  779. Property ValidFieldKinds : TFieldKinds Read FValidFieldKinds;
  780. Public
  781. Constructor Create(ADataset : TDataset);
  782. Destructor Destroy;override;
  783. Procedure Add(Field : TField);
  784. Procedure CheckFieldName (Const Value : String);
  785. Procedure CheckFieldNames (Const Value : String);
  786. Procedure Clear;
  787. Function FindField (Const Value : String) : TField;
  788. Function FieldByName (Const Value : String) : TField;
  789. Function FieldByNumber(FieldNo : Integer) : TField;
  790. Procedure GetFieldNames (Values : TStrings);
  791. Function IndexOf(Field : TField) : Longint;
  792. procedure Remove(Value : TField);
  793. Property Count : Integer Read GetCount;
  794. Property Dataset : TDataset Read FDataset;
  795. Property Fields [Index : Integer] : TField Read GetField Write SetField; default;
  796. end;
  797. { TDataSet }
  798. TBookmark = Pointer;
  799. TBookmarkStr = string;
  800. PBookmarkFlag = ^TBookmarkFlag;
  801. TBookmarkFlag = (bfCurrent, bfBOF, bfEOF, bfInserted);
  802. PBufferList = ^TBufferList;
  803. TBufferList = array[0..dsMaxBufferCount - 1] of PChar;
  804. TGetMode = (gmCurrent, gmNext, gmPrior);
  805. TGetResult = (grOK, grBOF, grEOF, grError);
  806. TResyncMode = set of (rmExact, rmCenter);
  807. TDataAction = (daFail, daAbort, daRetry);
  808. TUpdateAction = (uaFail, uaAbort, uaSkip, uaRetry, uaApplied);
  809. TUpdateKind = (ukModify, ukInsert, ukDelete);
  810. TLocateOption = (loCaseInsensitive, loPartialKey);
  811. TLocateOptions = set of TLocateOption;
  812. TDataOperation = procedure of object;
  813. TDataSetNotifyEvent = procedure(DataSet: TDataSet) of object;
  814. TDataSetErrorEvent = procedure(DataSet: TDataSet; E: EDatabaseError;
  815. var DataAction: TDataAction) of object;
  816. TFilterOption = (foCaseInsensitive, foNoPartialCompare);
  817. TFilterOptions = set of TFilterOption;
  818. TFilterRecordEvent = procedure(DataSet: TDataSet;
  819. var Accept: Boolean) of object;
  820. TDatasetClass = Class of TDataset;
  821. TBufferArray = ^pchar;
  822. TDataSet = class(TComponent)
  823. Private
  824. FOpenAfterRead : boolean;
  825. FActiveRecord: Longint;
  826. FAfterCancel: TDataSetNotifyEvent;
  827. FAfterClose: TDataSetNotifyEvent;
  828. FAfterDelete: TDataSetNotifyEvent;
  829. FAfterEdit: TDataSetNotifyEvent;
  830. FAfterInsert: TDataSetNotifyEvent;
  831. FAfterOpen: TDataSetNotifyEvent;
  832. FAfterPost: TDataSetNotifyEvent;
  833. FAfterRefresh: TDataSetNotifyEvent;
  834. FAfterScroll: TDataSetNotifyEvent;
  835. FAutoCalcFields: Boolean;
  836. FBOF: Boolean;
  837. FBeforeCancel: TDataSetNotifyEvent;
  838. FBeforeClose: TDataSetNotifyEvent;
  839. FBeforeDelete: TDataSetNotifyEvent;
  840. FBeforeEdit: TDataSetNotifyEvent;
  841. FBeforeInsert: TDataSetNotifyEvent;
  842. FBeforeOpen: TDataSetNotifyEvent;
  843. FBeforePost: TDataSetNotifyEvent;
  844. FBeforeRefresh: TDataSetNotifyEvent;
  845. FBeforeScroll: TDataSetNotifyEvent;
  846. FBlobFieldCount: Longint;
  847. FBookmarkSize: Longint;
  848. FBuffers : TBufferArray;
  849. FBufferCount: Longint;
  850. FCalcBuffer: PChar;
  851. FCalcFieldsSize: Longint;
  852. FConstraints: TCheckConstraints;
  853. FDisableControlsCount : Integer;
  854. FDisableControlsState : TDatasetState;
  855. FCurrentRecord: Longint;
  856. FDataSources : TList;
  857. FDefaultFields: Boolean;
  858. FEOF: Boolean;
  859. FEnableControlsEvent : TDataEvent;
  860. FFieldList : TFields;
  861. FFieldDefs: TFieldDefs;
  862. FFilterOptions: TFilterOptions;
  863. FFilterText: string;
  864. FFiltered: Boolean;
  865. FFound: Boolean;
  866. FInternalCalcFields: Boolean;
  867. FModified: Boolean;
  868. FOnCalcFields: TDataSetNotifyEvent;
  869. FOnDeleteError: TDataSetErrorEvent;
  870. FOnEditError: TDataSetErrorEvent;
  871. FOnFilterRecord: TFilterRecordEvent;
  872. FOnNewRecord: TDataSetNotifyEvent;
  873. FOnPostError: TDataSetErrorEvent;
  874. FRecordCount: Longint;
  875. FIsUniDirectional: Boolean;
  876. FState : TDataSetState;
  877. Procedure DoInsertAppend(DoAppend : Boolean);
  878. Procedure DoInternalOpen;
  879. Procedure DoInternalClose;
  880. Function GetBuffer (Index : longint) : Pchar;
  881. Function GetField (Index : Longint) : TField;
  882. Procedure RegisterDataSource(ADatasource : TDataSource);
  883. Procedure RemoveField (Field : TField);
  884. Procedure SetField (Index : Longint;Value : TField);
  885. Procedure ShiftBuffersForward;
  886. Procedure ShiftBuffersBackward;
  887. Function TryDoing (P : TDataOperation; Ev : TDatasetErrorEvent) : Boolean;
  888. Function GetActive : boolean;
  889. Procedure UnRegisterDataSource(ADatasource : TDatasource);
  890. Procedure UpdateFieldDefs;
  891. protected
  892. procedure RecalcBufListSize;
  893. procedure ActivateBuffers; virtual;
  894. procedure BindFields(Binding: Boolean);
  895. function BookmarkAvailable: Boolean;
  896. procedure CalculateFields(Buffer: PChar); virtual;
  897. procedure CheckActive; virtual;
  898. procedure CheckInactive; virtual;
  899. procedure CheckBiDirectional;
  900. procedure Loaded; override;
  901. procedure ClearBuffers; virtual;
  902. procedure ClearCalcFields(Buffer: PChar); virtual;
  903. procedure CloseBlob(Field: TField); virtual;
  904. procedure CloseCursor; virtual;
  905. procedure CreateFields; virtual;
  906. procedure DataEvent(Event: TDataEvent; Info: Ptrint); virtual;
  907. procedure DestroyFields; virtual;
  908. procedure DoAfterCancel; virtual;
  909. procedure DoAfterClose; virtual;
  910. procedure DoAfterDelete; virtual;
  911. procedure DoAfterEdit; virtual;
  912. procedure DoAfterInsert; virtual;
  913. procedure DoAfterOpen; virtual;
  914. procedure DoAfterPost; virtual;
  915. procedure DoAfterScroll; virtual;
  916. procedure DoAfterRefresh; virtual;
  917. procedure DoBeforeCancel; virtual;
  918. procedure DoBeforeClose; virtual;
  919. procedure DoBeforeDelete; virtual;
  920. procedure DoBeforeEdit; virtual;
  921. procedure DoBeforeInsert; virtual;
  922. procedure DoBeforeOpen; virtual;
  923. procedure DoBeforePost; virtual;
  924. procedure DoBeforeScroll; virtual;
  925. procedure DoBeforeRefresh; virtual;
  926. procedure DoOnCalcFields; virtual;
  927. procedure DoOnNewRecord; virtual;
  928. function FieldByNumber(FieldNo: Longint): TField;
  929. function FindRecord(Restart, GoForward: Boolean): Boolean; virtual;
  930. procedure FreeFieldBuffers; virtual;
  931. function GetBookmarkStr: TBookmarkStr; virtual;
  932. procedure GetCalcFields(Buffer: PChar); virtual;
  933. function GetCanModify: Boolean; virtual;
  934. procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
  935. function GetFieldClass(FieldType: TFieldType): TFieldClass; virtual;
  936. Function GetfieldCount : Integer;
  937. function GetFieldValues(fieldname : string) : Variant; virtual;
  938. function GetIsIndexField(Field: TField): Boolean; virtual;
  939. function GetNextRecords: Longint; virtual;
  940. function GetNextRecord: Boolean; virtual;
  941. function GetPriorRecords: Longint; virtual;
  942. function GetPriorRecord: Boolean; virtual;
  943. function GetRecordCount: Longint; virtual;
  944. function GetRecNo: Longint; virtual;
  945. procedure InitFieldDefs; virtual;
  946. procedure InitRecord(Buffer: PChar); virtual;
  947. procedure InternalCancel; virtual;
  948. procedure InternalEdit; virtual;
  949. procedure InternalInsert; virtual;
  950. procedure InternalRefresh; virtual;
  951. procedure OpenCursor(InfoQuery: Boolean); virtual;
  952. procedure RefreshInternalCalcFields(Buffer: PChar); virtual;
  953. procedure RestoreState(const Value: TDataSetState);
  954. Procedure SetActive (Value : Boolean); virtual;
  955. procedure SetBookmarkStr(const Value: TBookmarkStr); virtual;
  956. procedure SetBufListSize(Value: Longint);
  957. procedure SetChildOrder(Component: TComponent; Order: Longint); override;
  958. procedure SetCurrentRecord(Index: Longint); virtual;
  959. procedure SetFiltered(Value: Boolean); virtual;
  960. procedure SetFilterOptions(Value: TFilterOptions); virtual;
  961. procedure SetFilterText(const Value: string); virtual;
  962. procedure SetFound(const Value: Boolean);
  963. procedure SetFieldValues(fieldname: string; Value: Variant); virtual;
  964. procedure SetModified(Value: Boolean);
  965. procedure SetName(const Value: TComponentName); override;
  966. procedure SetOnFilterRecord(const Value: TFilterRecordEvent); virtual;
  967. procedure SetRecNo(Value: Longint); virtual;
  968. procedure SetState(Value: TDataSetState);
  969. function SetTempState(const Value: TDataSetState): TDataSetState;
  970. Function Tempbuffer: PChar;
  971. procedure UpdateIndexDefs; virtual;
  972. property ActiveRecord: Longint read FActiveRecord;
  973. property CurrentRecord: Longint read FCurrentRecord;
  974. property BlobFieldCount: Longint read FBlobFieldCount;
  975. property BookmarkSize: Longint read FBookmarkSize write FBookmarkSize;
  976. property Buffers[Index: Longint]: PChar read GetBuffer;
  977. property BufferCount: Longint read FBufferCount;
  978. property CalcBuffer: PChar read FCalcBuffer;
  979. property CalcFieldsSize: Longint read FCalcFieldsSize;
  980. property InternalCalcFields: Boolean read FInternalCalcFields;
  981. property Constraints: TCheckConstraints read FConstraints write FConstraints;
  982. protected { abstract methods }
  983. function AllocRecordBuffer: PChar; virtual; abstract;
  984. procedure FreeRecordBuffer(var Buffer: PChar); virtual; abstract;
  985. procedure GetBookmarkData(Buffer: PChar; Data: Pointer); virtual; abstract;
  986. function GetBookmarkFlag(Buffer: PChar): TBookmarkFlag; virtual; abstract;
  987. function GetDataSource: TDataSource; virtual;
  988. function GetRecord(Buffer: PChar; GetMode: TGetMode; DoCheck: Boolean): TGetResult; virtual; abstract;
  989. function GetRecordSize: Word; virtual; abstract;
  990. procedure InternalAddRecord(Buffer: Pointer; AAppend: Boolean); virtual; abstract;
  991. procedure InternalClose; virtual; abstract;
  992. procedure InternalDelete; virtual; abstract;
  993. procedure InternalFirst; virtual; abstract;
  994. procedure InternalGotoBookmark(ABookmark: Pointer); virtual; abstract;
  995. procedure InternalHandleException; virtual;
  996. procedure InternalInitFieldDefs; virtual; abstract;
  997. procedure InternalInitRecord(Buffer: PChar); virtual; abstract;
  998. procedure InternalLast; virtual; abstract;
  999. procedure InternalOpen; virtual; abstract;
  1000. procedure InternalPost; virtual; abstract;
  1001. procedure InternalSetToRecord(Buffer: PChar); virtual; abstract;
  1002. function IsCursorOpen: Boolean; virtual; abstract;
  1003. procedure SetBookmarkFlag(Buffer: PChar; Value: TBookmarkFlag); virtual; abstract;
  1004. procedure SetBookmarkData(Buffer: PChar; Data: Pointer); virtual; abstract;
  1005. public
  1006. constructor Create(AOwner: TComponent); override;
  1007. destructor Destroy; override;
  1008. function ActiveBuffer: PChar;
  1009. function GetFieldData(Field: TField; Buffer: Pointer): Boolean; overload; virtual;
  1010. function GetFieldData(Field: TField; Buffer: Pointer; NativeFormat: Boolean): Boolean; overload; virtual;
  1011. procedure SetFieldData(Field: TField; Buffer: Pointer); overload; virtual;
  1012. procedure SetFieldData(Field: TField; Buffer: Pointer; NativeFormat: Boolean); overload; virtual;
  1013. procedure Append;
  1014. procedure AppendRecord(const Values: array of const);
  1015. function BookmarkValid(ABookmark: TBookmark): Boolean; virtual;
  1016. procedure Cancel; virtual;
  1017. procedure CheckBrowseMode;
  1018. procedure ClearFields;
  1019. procedure Close;
  1020. function ControlsDisabled: Boolean;
  1021. function CompareBookmarks(Bookmark1, Bookmark2: TBookmark): Longint; virtual;
  1022. function CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream; virtual;
  1023. procedure CursorPosChanged;
  1024. procedure Delete;
  1025. procedure DisableControls;
  1026. procedure Edit;
  1027. procedure EnableControls;
  1028. function FieldByName(const FieldName: string): TField;
  1029. function FindField(const FieldName: string): TField;
  1030. function FindFirst: Boolean;
  1031. function FindLast: Boolean;
  1032. function FindNext: Boolean;
  1033. function FindPrior: Boolean;
  1034. procedure First;
  1035. procedure FreeBookmark(ABookmark: TBookmark); virtual;
  1036. function GetBookmark: TBookmark; virtual;
  1037. function GetCurrentRecord(Buffer: PChar): Boolean; virtual;
  1038. procedure GetFieldList(List: TList; const FieldNames: string);
  1039. procedure GetFieldNames(List: TStrings);
  1040. procedure GotoBookmark(ABookmark: TBookmark);
  1041. procedure Insert;
  1042. procedure InsertRecord(const Values: array of const);
  1043. function IsEmpty: Boolean;
  1044. function IsLinkedTo(ADataSource: TDataSource): Boolean;
  1045. function IsSequenced: Boolean; virtual;
  1046. procedure Last;
  1047. function Locate(const keyfields: string; const keyvalues: Variant; options: TLocateOptions) : boolean; virtual;
  1048. function Lookup(const KeyFields: string; const KeyValues: Variant; const ResultFields: string): Variant; virtual;
  1049. function MoveBy(Distance: Longint): Longint;
  1050. procedure Next;
  1051. procedure Open;
  1052. procedure Post; virtual;
  1053. procedure Prior;
  1054. procedure Refresh;
  1055. procedure Resync(Mode: TResyncMode); virtual;
  1056. procedure SetFields(const Values: array of const);
  1057. function Translate(Src, Dest: PChar; ToOem: Boolean): Integer; virtual;
  1058. procedure UpdateCursorPos;
  1059. procedure UpdateRecord;
  1060. function UpdateStatus: TUpdateStatus; virtual;
  1061. property BOF: Boolean read FBOF;
  1062. property Bookmark: TBookmarkStr read GetBookmarkStr write SetBookmarkStr;
  1063. property CanModify: Boolean read GetCanModify;
  1064. property DataSource: TDataSource read GetDataSource;
  1065. property DefaultFields: Boolean read FDefaultFields;
  1066. property EOF: Boolean read FEOF;
  1067. property FieldCount: Longint read GetFieldCount;
  1068. property FieldDefs: TFieldDefs read FFieldDefs write FFieldDefs;
  1069. // property Fields[Index: Longint]: TField read GetField write SetField;
  1070. property Found: Boolean read FFound;
  1071. property Modified: Boolean read FModified write SetModified;
  1072. property IsUniDirectional: Boolean read FIsUniDirectional write FIsUniDirectional default False;
  1073. property RecordCount: Longint read GetRecordCount;
  1074. property RecNo: Longint read GetRecNo write SetRecNo;
  1075. property RecordSize: Word read GetRecordSize;
  1076. property State: TDataSetState read FState;
  1077. property Fields : TFields read FFieldList;
  1078. property FieldValues[fieldname : string] : Variant read GetFieldValues write SetFieldValues; default;
  1079. property Filter: string read FFilterText write SetFilterText;
  1080. property Filtered: Boolean read FFiltered write SetFiltered default False;
  1081. property FilterOptions: TFilterOptions read FFilterOptions write SetFilterOptions;
  1082. property Active: Boolean read GetActive write SetActive default False;
  1083. property AutoCalcFields: Boolean read FAutoCalcFields write FAutoCalcFields;
  1084. property BeforeOpen: TDataSetNotifyEvent read FBeforeOpen write FBeforeOpen;
  1085. property AfterOpen: TDataSetNotifyEvent read FAfterOpen write FAfterOpen;
  1086. property BeforeClose: TDataSetNotifyEvent read FBeforeClose write FBeforeClose;
  1087. property AfterClose: TDataSetNotifyEvent read FAfterClose write FAfterClose;
  1088. property BeforeInsert: TDataSetNotifyEvent read FBeforeInsert write FBeforeInsert;
  1089. property AfterInsert: TDataSetNotifyEvent read FAfterInsert write FAfterInsert;
  1090. property BeforeEdit: TDataSetNotifyEvent read FBeforeEdit write FBeforeEdit;
  1091. property AfterEdit: TDataSetNotifyEvent read FAfterEdit write FAfterEdit;
  1092. property BeforePost: TDataSetNotifyEvent read FBeforePost write FBeforePost;
  1093. property AfterPost: TDataSetNotifyEvent read FAfterPost write FAfterPost;
  1094. property BeforeCancel: TDataSetNotifyEvent read FBeforeCancel write FBeforeCancel;
  1095. property AfterCancel: TDataSetNotifyEvent read FAfterCancel write FAfterCancel;
  1096. property BeforeDelete: TDataSetNotifyEvent read FBeforeDelete write FBeforeDelete;
  1097. property AfterDelete: TDataSetNotifyEvent read FAfterDelete write FAfterDelete;
  1098. property BeforeScroll: TDataSetNotifyEvent read FBeforeScroll write FBeforeScroll;
  1099. property AfterScroll: TDataSetNotifyEvent read FAfterScroll write FAfterScroll;
  1100. property BeforeRefresh: TDataSetNotifyEvent read FBeforeRefresh write FBeforeRefresh;
  1101. property AfterRefresh: TDataSetNotifyEvent read FAfterRefresh write FAfterRefresh;
  1102. property OnCalcFields: TDataSetNotifyEvent read FOnCalcFields write FOnCalcFields;
  1103. property OnDeleteError: TDataSetErrorEvent read FOnDeleteError write FOnDeleteError;
  1104. property OnEditError: TDataSetErrorEvent read FOnEditError write FOnEditError;
  1105. property OnFilterRecord: TFilterRecordEvent read FOnFilterRecord write SetOnFilterRecord;
  1106. property OnNewRecord: TDataSetNotifyEvent read FOnNewRecord write FOnNewRecord;
  1107. property OnPostError: TDataSetErrorEvent read FOnPostError write FOnPostError;
  1108. end;
  1109. TDataLink = class(TPersistent)
  1110. private
  1111. FFIrstRecord,
  1112. FBufferCount : Integer;
  1113. FActive,
  1114. FDataSourceFixed,
  1115. FEditing,
  1116. FReadOnly,
  1117. FUpdatingRecord,
  1118. FVisualControl : Boolean;
  1119. FDataSource : TDataSource;
  1120. Function CalcFirstRecord(Index : Integer) : Integer;
  1121. Procedure CalcRange;
  1122. Procedure CheckActiveAndEditing;
  1123. Function GetDataset : TDataset;
  1124. procedure SetActive(AActive: Boolean);
  1125. procedure SetDataSource(Value: TDataSource);
  1126. Procedure SetReadOnly(Value : Boolean);
  1127. protected
  1128. procedure ActiveChanged; virtual;
  1129. procedure CheckBrowseMode; virtual;
  1130. procedure DataEvent(Event: TDataEvent; Info: Ptrint); virtual;
  1131. procedure DataSetChanged; virtual;
  1132. procedure DataSetScrolled(Distance: Integer); virtual;
  1133. procedure EditingChanged; virtual;
  1134. procedure FocusControl(Field: TFieldRef); virtual;
  1135. function GetActiveRecord: Integer; virtual;
  1136. function GetBOF: Boolean; virtual;
  1137. function GetBufferCount: Integer; virtual;
  1138. function GetEOF: Boolean; virtual;
  1139. function GetRecordCount: Integer; virtual;
  1140. procedure LayoutChanged; virtual;
  1141. function MoveBy(Distance: Integer): Integer; virtual;
  1142. procedure RecordChanged(Field: TField); virtual;
  1143. procedure SetActiveRecord(Value: Integer); virtual;
  1144. procedure SetBufferCount(Value: Integer); virtual;
  1145. procedure UpdateData; virtual;
  1146. property VisualControl: Boolean read FVisualControl write FVisualControl;
  1147. property FirstRecord: Integer read FFirstRecord write FFirstRecord;
  1148. public
  1149. constructor Create;
  1150. destructor Destroy; override;
  1151. function Edit: Boolean;
  1152. procedure UpdateRecord;
  1153. property Active: Boolean read FActive;
  1154. property ActiveRecord: Integer read GetActiveRecord write SetActiveRecord;
  1155. property BOF: Boolean read GetBOF;
  1156. property BufferCount: Integer read FBufferCount write SetBufferCount;
  1157. property DataSet: TDataSet read GetDataSet;
  1158. property DataSource: TDataSource read FDataSource write SetDataSource;
  1159. property DataSourceFixed: Boolean read FDataSourceFixed write FDataSourceFixed;
  1160. property Editing: Boolean read FEditing;
  1161. property Eof: Boolean read GetEOF;
  1162. property ReadOnly: Boolean read FReadOnly write SetReadOnly;
  1163. property RecordCount: Integer read GetRecordCount;
  1164. end;
  1165. { TDetailDataLink }
  1166. TDetailDataLink = class(TDataLink)
  1167. protected
  1168. function GetDetailDataSet: TDataSet; virtual;
  1169. public
  1170. property DetailDataSet: TDataSet read GetDetailDataSet;
  1171. end;
  1172. { TMasterDataLink }
  1173. TMasterDataLink = class(TDetailDataLink)
  1174. private
  1175. FDetailDataSet: TDataSet;
  1176. FFieldNames: string;
  1177. FFields: TList;
  1178. FOnMasterChange: TNotifyEvent;
  1179. FOnMasterDisable: TNotifyEvent;
  1180. procedure SetFieldNames(const Value: string);
  1181. protected
  1182. procedure ActiveChanged; override;
  1183. procedure CheckBrowseMode; override;
  1184. function GetDetailDataSet: TDataSet; override;
  1185. procedure LayoutChanged; override;
  1186. procedure RecordChanged(Field: TField); override;
  1187. Procedure DoMasterDisable; virtual;
  1188. Procedure DoMasterChange; virtual;
  1189. public
  1190. constructor Create(ADataSet: TDataSet);virtual;
  1191. destructor Destroy; override;
  1192. property FieldNames: string read FFieldNames write SetFieldNames;
  1193. property Fields: TList read FFields;
  1194. property OnMasterChange: TNotifyEvent read FOnMasterChange write FOnMasterChange;
  1195. property OnMasterDisable: TNotifyEvent read FOnMasterDisable write FOnMasterDisable;
  1196. end;
  1197. { TDataSource }
  1198. TDataChangeEvent = procedure(Sender: TObject; Field: TField) of object;
  1199. TDataSource = class(TComponent)
  1200. private
  1201. FDataSet: TDataSet;
  1202. FDataLinks: TList;
  1203. FEnabled: Boolean;
  1204. FAutoEdit: Boolean;
  1205. FState: TDataSetState;
  1206. FOnStateChange: TNotifyEvent;
  1207. FOnDataChange: TDataChangeEvent;
  1208. FOnUpdateData: TNotifyEvent;
  1209. procedure DistributeEvent(Event: TDataEvent; Info: Ptrint);
  1210. procedure RegisterDataLink(DataLink: TDataLink);
  1211. Procedure ProcessEvent(Event : TDataEvent; Info : Ptrint);
  1212. procedure SetDataSet(ADataSet: TDataSet);
  1213. procedure SetEnabled(Value: Boolean);
  1214. procedure UnregisterDataLink(DataLink: TDataLink);
  1215. protected
  1216. Procedure DoDataChange (Info : Pointer);virtual;
  1217. Procedure DoStateChange; virtual;
  1218. Procedure DoUpdateData;
  1219. property DataLinks: TList read FDataLinks;
  1220. public
  1221. constructor Create(AOwner: TComponent); override;
  1222. destructor Destroy; override;
  1223. procedure Edit;
  1224. function IsLinkedTo(ADataSet: TDataSet): Boolean;
  1225. property State: TDataSetState read FState;
  1226. published
  1227. property AutoEdit: Boolean read FAutoEdit write FAutoEdit default True;
  1228. property DataSet: TDataSet read FDataSet write SetDataSet;
  1229. property Enabled: Boolean read FEnabled write SetEnabled default True;
  1230. property OnStateChange: TNotifyEvent read FOnStateChange write FOnStateChange;
  1231. property OnDataChange: TDataChangeEvent read FOnDataChange write FOnDataChange;
  1232. property OnUpdateData: TNotifyEvent read FOnUpdateData write FOnUpdateData;
  1233. end;
  1234. { TDBDataset }
  1235. TDBDatasetClass = Class of TDBDataset;
  1236. TDBDataset = Class(TDataset)
  1237. Private
  1238. FDatabase : TDatabase;
  1239. FTransaction : TDBTransaction;
  1240. Protected
  1241. Procedure SetDatabase (Value : TDatabase); virtual;
  1242. Procedure SetTransaction(Value : TDBTransaction); virtual;
  1243. Procedure CheckDatabase;
  1244. Public
  1245. Destructor destroy; override;
  1246. Property DataBase : TDatabase Read FDatabase Write SetDatabase;
  1247. Property Transaction : TDBTransaction Read FTransaction Write SetTransaction;
  1248. end;
  1249. { TDBTransaction }
  1250. TDBTransactionClass = Class of TDBTransaction;
  1251. TDBTransaction = Class(TComponent)
  1252. Private
  1253. FActive : boolean;
  1254. FDatabase : TDatabase;
  1255. FDataSets : TList;
  1256. FOpenAfterRead : boolean;
  1257. Function GetDataSetCount : Longint;
  1258. Function GetDataset(Index : longint) : TDBDataset;
  1259. procedure RegisterDataset (DS : TDBDataset);
  1260. procedure UnRegisterDataset (DS : TDBDataset);
  1261. procedure RemoveDataSets;
  1262. procedure SetActive(Value : boolean);
  1263. Protected
  1264. Procedure SetDatabase (Value : TDatabase); virtual;
  1265. procedure CloseTrans;
  1266. procedure openTrans;
  1267. Procedure CheckDatabase;
  1268. Procedure CheckActive;
  1269. Procedure CheckInactive;
  1270. procedure EndTransaction; virtual; abstract;
  1271. procedure StartTransaction; virtual; abstract;
  1272. procedure InternalHandleException; virtual;
  1273. procedure Loaded; override;
  1274. Public
  1275. constructor Create(AOwner: TComponent); override;
  1276. Destructor destroy; override;
  1277. procedure CloseDataSets;
  1278. Property DataBase : TDatabase Read FDatabase Write SetDatabase;
  1279. published
  1280. property Active : boolean read FActive write setactive;
  1281. end;
  1282. { TDatabase }
  1283. TLoginEvent = procedure(Sender: TObject; Username, Password: string) of object;
  1284. TDatabaseClass = Class Of TDatabase;
  1285. TDatabase = class(TComponent)
  1286. private
  1287. FConnected : Boolean;
  1288. FDataBaseName : String;
  1289. FDataSets : TList;
  1290. FTransactions : TList;
  1291. FDirectory : String;
  1292. FKeepConnection : Boolean;
  1293. FLoginPrompt : Boolean;
  1294. FOnLogin : TLoginEvent;
  1295. FParams : TStrings;
  1296. FSQLBased : Boolean;
  1297. FOpenAfterRead : boolean;
  1298. Function GetDataSetCount : Longint;
  1299. Function GetTransactionCount : Longint;
  1300. Function GetDataset(Index : longint) : TDBDataset;
  1301. Function GetTransaction(Index : longint) : TDBTransaction;
  1302. procedure SetConnected (Value : boolean);
  1303. procedure RegisterDataset (DS : TDBDataset);
  1304. procedure RegisterTransaction (TA : TDBTransaction);
  1305. procedure UnRegisterDataset (DS : TDBDataset);
  1306. procedure UnRegisterTransaction(TA : TDBTransaction);
  1307. procedure RemoveDataSets;
  1308. procedure RemoveTransactions;
  1309. protected
  1310. Procedure CheckConnected;
  1311. Procedure CheckDisConnected;
  1312. procedure InternalHandleException; virtual;
  1313. procedure Loaded; override;
  1314. Procedure DoInternalConnect; Virtual;Abstract;
  1315. Procedure DoInternalDisConnect; Virtual;Abstract;
  1316. public
  1317. constructor Create(AOwner: TComponent); override;
  1318. destructor Destroy; override;
  1319. procedure Close;
  1320. procedure Open;
  1321. procedure CloseDataSets;
  1322. procedure CloseTransactions;
  1323. // procedure ApplyUpdates;
  1324. procedure StartTransaction; virtual; abstract;
  1325. procedure EndTransaction; virtual; abstract;
  1326. property DataSetCount: Longint read GetDataSetCount;
  1327. property DataSets[Index: Longint]: TDBDataSet read GetDataSet;
  1328. property TransactionCount: Longint read GetTransactionCount;
  1329. property Transactions[Index: Longint]: TDBTransaction read GetTransaction;
  1330. property Directory: string read FDirectory write FDirectory;
  1331. property IsSQLBased: Boolean read FSQLBased;
  1332. published
  1333. property Connected: Boolean read FConnected write SetConnected;
  1334. property DatabaseName: string read FDatabaseName write FDatabaseName;
  1335. property KeepConnection: Boolean read FKeepConnection write FKeepConnection;
  1336. property LoginPrompt: Boolean read FLoginPrompt write FLoginPrompt;
  1337. property Params : TStrings read FParams Write FParams;
  1338. property OnLogin: TLoginEvent read FOnLogin write FOnLogin;
  1339. end;
  1340. { TCustomConnection }
  1341. TCustomConnection = class(TDatabase)
  1342. private
  1343. FAfterConnect: TNotifyEvent;
  1344. FAfterDisconnect: TNotifyEvent;
  1345. FBeforeConnect: TNotifyEvent;
  1346. FBeforeDisconnect: TNotifyEvent;
  1347. procedure SetAfterConnect(const AValue: TNotifyEvent);
  1348. procedure SetAfterDisconnect(const AValue: TNotifyEvent);
  1349. procedure SetBeforeConnect(const AValue: TNotifyEvent);
  1350. procedure SetBeforeDisconnect(const AValue: TNotifyEvent);
  1351. protected
  1352. procedure DoInternalConnect; override;
  1353. procedure DoInternalDisconnect; override;
  1354. procedure DoConnect; virtual;
  1355. procedure DoDisconnect; virtual;
  1356. function GetConnected : boolean; virtual;
  1357. procedure StartTransaction; override;
  1358. procedure EndTransaction; override;
  1359. published
  1360. property AfterConnect : TNotifyEvent read FAfterConnect write SetAfterConnect;
  1361. property BeforeConnect : TNotifyEvent read FBeforeConnect write SetBeforeConnect;
  1362. property AfterDisconnect : TNotifyEvent read FAfterDisconnect write SetAfterDisconnect;
  1363. property BeforeDisconnect : TNotifyEvent read FBeforeDisconnect write SetBeforeDisconnect;
  1364. end;
  1365. { TParam }
  1366. TBlobData = string;
  1367. TParamBinding = array of integer;
  1368. TParamType = (ptUnknown, ptInput, ptOutput, ptInputOutput, ptResult);
  1369. TParamTypes = set of TParamType;
  1370. TParamStyle = (psInterbase,psPostgreSQL,psSimulated);
  1371. TParams = class;
  1372. TParam = class(TCollectionItem)
  1373. private
  1374. FNativeStr: string;
  1375. FValue: Variant;
  1376. FPrecision: Integer;
  1377. FNumericScale: Integer;
  1378. FName: string;
  1379. FDataType: TFieldType;
  1380. FBound: Boolean;
  1381. FParamType: TParamType;
  1382. FSize: Integer;
  1383. Function GetDataSet: TDataSet;
  1384. Function IsParamStored: Boolean;
  1385. protected
  1386. Procedure AssignParam(Param: TParam);
  1387. Procedure AssignTo(Dest: TPersistent); override;
  1388. Function GetAsBoolean: Boolean;
  1389. Function GetAsCurrency: Currency;
  1390. Function GetAsDateTime: TDateTime;
  1391. Function GetAsFloat: Double;
  1392. Function GetAsInteger: Longint;
  1393. Function GetAsLargeInt: LargeInt;
  1394. Function GetAsMemo: string;
  1395. Function GetAsString: string;
  1396. Function GetAsVariant: Variant;
  1397. Function GetDisplayName: string; override;
  1398. Function GetIsNull: Boolean;
  1399. Function IsEqual(AValue: TParam): Boolean;
  1400. Procedure SetAsBlob(const AValue: TBlobData);
  1401. Procedure SetAsBoolean(AValue: Boolean);
  1402. Procedure SetAsCurrency(const AValue: Currency);
  1403. Procedure SetAsDate(const AValue: TDateTime);
  1404. Procedure SetAsDateTime(const AValue: TDateTime);
  1405. Procedure SetAsFloat(const AValue: Double);
  1406. Procedure SetAsInteger(AValue: Longint);
  1407. Procedure SetAsLargeInt(AValue: LargeInt);
  1408. Procedure SetAsMemo(const AValue: string);
  1409. Procedure SetAsSmallInt(AValue: LongInt);
  1410. Procedure SetAsString(const AValue: string);
  1411. Procedure SetAsTime(const AValue: TDateTime);
  1412. Procedure SetAsVariant(const AValue: Variant);
  1413. Procedure SetAsWord(AValue: LongInt);
  1414. Procedure SetDataType(AValue: TFieldType);
  1415. Procedure SetText(const AValue: string);
  1416. public
  1417. constructor Create(ACollection: TCollection); overload; override;
  1418. constructor Create(AParams: TParams; AParamType: TParamType); reintroduce; overload;
  1419. Procedure Assign(Source: TPersistent); override;
  1420. Procedure AssignField(Field: TField);
  1421. Procedure AssignToField(Field: TField);
  1422. Procedure AssignFieldValue(Field: TField; const AValue: Variant);
  1423. procedure AssignFromField(Field : TField);
  1424. Procedure Clear;
  1425. Procedure GetData(Buffer: Pointer);
  1426. Function GetDataSize: Integer;
  1427. Procedure LoadFromFile(const FileName: string; BlobType: TBlobType);
  1428. Procedure LoadFromStream(Stream: TStream; BlobType: TBlobType);
  1429. Procedure SetBlobData(Buffer: Pointer; ASize: Integer);
  1430. Procedure SetData(Buffer: Pointer);
  1431. Property AsBlob : TBlobData read GetAsString write SetAsBlob;
  1432. Property AsBoolean : Boolean read GetAsBoolean write SetAsBoolean;
  1433. Property AsCurrency : Currency read GetAsCurrency write SetAsCurrency;
  1434. Property AsDate : TDateTime read GetAsDateTime write SetAsDate;
  1435. Property AsDateTime : TDateTime read GetAsDateTime write SetAsDateTime;
  1436. Property AsFloat : Double read GetAsFloat write SetAsFloat;
  1437. Property AsInteger : LongInt read GetAsInteger write SetAsInteger;
  1438. Property AsLargeInt : LargeInt read GetAsLargeInt write SetAsLargeInt;
  1439. Property AsMemo : string read GetAsMemo write SetAsMemo;
  1440. Property AsSmallInt : LongInt read GetAsInteger write SetAsSmallInt;
  1441. Property AsString : string read GetAsString write SetAsString;
  1442. Property AsTime : TDateTime read GetAsDateTime write SetAsTime;
  1443. Property AsWord : LongInt read GetAsInteger write SetAsWord;
  1444. Property Bound : Boolean read FBound write FBound;
  1445. Property Dataset : TDataset Read GetDataset;
  1446. Property IsNull : Boolean read GetIsNull;
  1447. Property NativeStr : string read FNativeStr write FNativeStr;
  1448. Property Text : string read GetAsString write SetText;
  1449. Property Value : Variant read GetAsVariant write SetAsVariant stored IsParamStored;
  1450. published
  1451. Property DataType : TFieldType read FDataType write SetDataType;
  1452. Property Name : string read FName write FName;
  1453. Property NumericScale : Integer read FNumericScale write FNumericScale default 0;
  1454. Property ParamType : TParamType read FParamType write FParamType;
  1455. Property Precision : Integer read FPrecision write FPrecision default 0;
  1456. Property Size : Integer read FSize write FSize default 0;
  1457. end;
  1458. { TParams }
  1459. TParams = class(TCollection)
  1460. private
  1461. FOwner: TPersistent;
  1462. Function GetItem(Index: Integer): TParam;
  1463. Function GetParamValue(const ParamName: string): Variant;
  1464. Procedure SetItem(Index: Integer; Value: TParam);
  1465. Procedure SetParamValue(const ParamName: string; const Value: Variant);
  1466. protected
  1467. Procedure AssignTo(Dest: TPersistent); override;
  1468. Function GetDataSet: TDataSet;
  1469. Function GetOwner: TPersistent; override;
  1470. public
  1471. Constructor Create(AOwner: TPersistent); overload;
  1472. Constructor Create; overload;
  1473. Procedure AddParam(Value: TParam);
  1474. Procedure AssignValues(Value: TParams);
  1475. Function CreateParam(FldType: TFieldType; const ParamName: string; ParamType: TParamType): TParam;
  1476. Function FindParam(const Value: string): TParam;
  1477. Procedure GetParamList(List: TList; const ParamNames: string);
  1478. Function IsEqual(Value: TParams): Boolean;
  1479. Function ParamByName(const Value: string): TParam;
  1480. Function ParseSQL(SQL: String; DoCreate: Boolean): String;
  1481. Function ParseSQL(SQL: String; DoCreate: Boolean; ParameterStyle : TParamStyle): String; overload;
  1482. Function ParseSQL(SQL: String; DoCreate: Boolean; ParameterStyle : TParamStyle; var ParamBinding: TParambinding): String; overload;
  1483. Function ParseSQL(SQL: String; DoCreate: Boolean; ParameterStyle : TParamStyle; var ParamBinding: TParambinding; var ReplaceString : string): String;
  1484. Procedure RemoveParam(Value: TParam);
  1485. Procedure CopyParamValuesFromDataset(ADataset : TDataset; CopyBound : Boolean);
  1486. Property Dataset : TDataset Read GetDataset;
  1487. Property Items[Index: Integer] : TParam read GetItem write SetItem; default;
  1488. Property ParamValues[const ParamName: string] : Variant read GetParamValue write SetParamValue;
  1489. end;
  1490. TMasterParamsDataLink = Class(TMasterDataLink)
  1491. Private
  1492. FParams : TParams;
  1493. Procedure SetParams(AVAlue : TParams);
  1494. Protected
  1495. Procedure DoMasterDisable; override;
  1496. Procedure DoMasterChange; override;
  1497. Public
  1498. constructor Create(ADataSet: TDataSet); override;
  1499. Procedure RefreshParamNames; virtual;
  1500. Procedure CopyParamsFromMaster(CopyBound : Boolean); virtual;
  1501. Property Params : TParams Read FParams Write SetParams;
  1502. end;
  1503. const
  1504. FieldTypetoVariantMap : array[TFieldType] of Integer = (varError, varOleStr, varSmallint,
  1505. varInteger, varSmallint, varBoolean, varDouble, varCurrency, varCurrency,
  1506. varDate, varDate, varDate, varOleStr, varOleStr, varInteger, varOleStr,
  1507. varOleStr, varOleStr, varOleStr, varOleStr, varOleStr, varOleStr, varError,
  1508. varOleStr, varOleStr, varError, varError, varError, varError, varError,
  1509. varOleStr, varOleStr, varVariant, varUnknown, varDispatch, varOleStr, varOleStr,varOleStr);
  1510. Const
  1511. Fieldtypenames : Array [TFieldType] of String[15] =
  1512. (
  1513. 'Unknown',
  1514. 'String',
  1515. 'Smallint',
  1516. 'Integer',
  1517. 'Word',
  1518. 'Boolean',
  1519. 'Float',
  1520. 'Currency',
  1521. 'BCD',
  1522. 'Date',
  1523. 'Time',
  1524. 'DateTime',
  1525. 'Bytes',
  1526. 'VarBytes',
  1527. 'AutoInc',
  1528. 'Blob',
  1529. 'Memo',
  1530. 'Graphic',
  1531. 'FmtMemo',
  1532. 'ParadoxOle',
  1533. 'DBaseOle',
  1534. 'TypedBinary',
  1535. 'Cursor',
  1536. 'FixedChar',
  1537. 'WideString',
  1538. 'Largeint',
  1539. 'ADT',
  1540. 'Array',
  1541. 'Reference',
  1542. 'DataSet',
  1543. 'OraBlob',
  1544. 'OraClob',
  1545. 'Variant',
  1546. 'Interface',
  1547. 'IDispatch',
  1548. 'Guid',
  1549. 'TimeStamp',
  1550. 'FMTBcd'
  1551. );
  1552. { 'Unknown',
  1553. 'String',
  1554. 'Smallint',
  1555. 'Integer',
  1556. 'Word',
  1557. 'Boolean',
  1558. 'Float',
  1559. 'Date',
  1560. 'Time',
  1561. 'DateTime',
  1562. 'Bytes',
  1563. 'VarBytes',
  1564. 'AutoInc',
  1565. 'Blob',
  1566. 'Memo',
  1567. 'Graphic',
  1568. 'FmtMemo',
  1569. 'ParadoxOle',
  1570. 'DBaseOle',
  1571. 'TypedBinary',
  1572. 'Cursor'
  1573. );}
  1574. const
  1575. DefaultFieldClasses : Array [TFieldType] of TFieldClass =
  1576. ( { ftUnknown} Tfield,
  1577. { ftString} TStringField,
  1578. { ftSmallint} TSmallIntField,
  1579. { ftInteger} TLongintField,
  1580. { ftWord} TLongintField,
  1581. { ftBoolean} TBooleanField,
  1582. { ftFloat} TFloatField,
  1583. { ftCurrency} Nil,
  1584. { ftBCD} TBCDField,
  1585. { ftDate} TDateField,
  1586. { ftTime} TTimeField,
  1587. { ftDateTime} TDateTimeField,
  1588. { ftBytes} TBytesField,
  1589. { ftVarBytes} TVarBytesField,
  1590. { ftAutoInc} TAutoIncField,
  1591. { ftBlob} TBlobField,
  1592. { ftMemo} TMemoField,
  1593. { ftGraphic} TGraphicField,
  1594. { ftFmtMemo} TMemoField,
  1595. { ftParadoxOle} Nil,
  1596. { ftDBaseOle} Nil,
  1597. { ftTypedBinary} Nil,
  1598. { ftCursor} Nil,
  1599. { ftFixedChar} TStringField,
  1600. { ftWideString} Nil,
  1601. { ftLargeint} TLargeIntField,
  1602. { ftADT} Nil,
  1603. { ftArray} Nil,
  1604. { ftReference} Nil,
  1605. { ftDataSet} Nil,
  1606. { ftOraBlob} TBlobField,
  1607. { ftOraClob} TMemoField,
  1608. { ftVariant} Nil,
  1609. { ftInterface} Nil,
  1610. { ftIDispatch} Nil,
  1611. { ftGuid} Nil,
  1612. { ftTimeStamp} Nil,
  1613. { ftFMTBcd} Nil
  1614. );
  1615. dsEditModes = [dsEdit, dsInsert, dsSetKey];
  1616. dsWriteModes = [dsEdit, dsInsert, dsSetKey, dsCalcFields, dsFilter,
  1617. dsNewValue, dsInternalCalc];
  1618. { Auxiliary functions }
  1619. Procedure DatabaseError (Const Msg : String);
  1620. Procedure DatabaseError (Const Msg : String; Comp : TComponent);
  1621. Procedure DatabaseErrorFmt (Const Fmt : String; Args : Array Of Const);
  1622. Procedure DatabaseErrorFmt (Const Fmt : String; Args : Array Of const;
  1623. Comp : TComponent);
  1624. Function ExtractFieldName(Const Fields: String; var Pos: Integer): String;
  1625. Function DateTimeRecToDateTime(DT: TFieldType; Data: TDateTimeRec): TDateTime;
  1626. Function DateTimeToDateTimeRec(DT: TFieldType; Data: TDateTime): TDateTimeRec;
  1627. procedure DisposeMem(var Buffer; Size: Integer);
  1628. function BuffersEqual(Buf1, Buf2: Pointer; Size: Integer): Boolean;
  1629. function SkipComments(var p: PChar; EscapeSlash, EscapeRepeat : Boolean) : boolean;
  1630. implementation
  1631. uses dbconst,typinfo;
  1632. { ---------------------------------------------------------------------
  1633. Auxiliary functions
  1634. ---------------------------------------------------------------------}
  1635. Procedure DatabaseError (Const Msg : String);
  1636. begin
  1637. Raise EDataBaseError.Create(Msg);
  1638. end;
  1639. Procedure DatabaseError (Const Msg : String; Comp : TComponent);
  1640. begin
  1641. if assigned(Comp) and (Comp.Name <> '') then
  1642. Raise EDatabaseError.CreateFmt('%s : %s',[Comp.Name,Msg])
  1643. else
  1644. DatabaseError(Msg);
  1645. end;
  1646. Procedure DatabaseErrorFmt (Const Fmt : String; Args : Array Of Const);
  1647. begin
  1648. Raise EDatabaseError.CreateFmt(Fmt,Args);
  1649. end;
  1650. Procedure DatabaseErrorFmt (Const Fmt : String; Args : Array Of const;
  1651. Comp : TComponent);
  1652. begin
  1653. if assigned(comp) then
  1654. Raise EDatabaseError.CreateFmt(Format('%s : %s',[Comp.Name,Fmt]),Args)
  1655. else
  1656. DatabaseErrorFmt(Fmt, Args);
  1657. end;
  1658. Function ExtractFieldName(Const Fields: String; var Pos: Integer): String;
  1659. var
  1660. i: integer;
  1661. begin
  1662. for i := Pos to Length(Fields) do begin
  1663. if Fields[i] = ';' then begin
  1664. Result := Copy(Fields, Pos, i - Pos);
  1665. Pos := i + 1;
  1666. Exit;
  1667. end;
  1668. end;
  1669. Result := Copy(Fields, Pos, Length(Fields));
  1670. Pos := Length(Fields) + 1;
  1671. end;
  1672. { EUpdateError }
  1673. constructor EUpdateError.Create(NativeError, Context : String;
  1674. ErrCode, PrevError : integer; E: Exception);
  1675. begin
  1676. Inherited CreateFmt(NativeError,[Context]);
  1677. FContext := Context;
  1678. FErrorCode := ErrCode;
  1679. FPreviousError := PrevError;
  1680. FOriginalException := E;
  1681. end;
  1682. Destructor EUpdateError.Destroy;
  1683. begin
  1684. FOriginalException.Free;
  1685. end;
  1686. { TIndexDef }
  1687. constructor TIndexDef.Create(Owner: TIndexDefs; const AName, TheFields: string;
  1688. TheOptions: TIndexOptions);
  1689. begin
  1690. inherited create(Owner);
  1691. FName := aname;
  1692. FFields := TheFields;
  1693. FOptions := TheOptions;
  1694. end;
  1695. destructor TIndexDef.Destroy;
  1696. begin
  1697. inherited Destroy;
  1698. end;
  1699. { TIndexDefs }
  1700. Function TIndexDefs.GetItem (Index : integer) : TIndexDef;
  1701. begin
  1702. Result:=(Inherited GetItem(Index)) as TIndexDef;
  1703. end;
  1704. Procedure TIndexDefs.SetItem(Index: Integer; Value: TIndexDef);
  1705. begin
  1706. Inherited SetItem(Index,Value);
  1707. end;
  1708. constructor TIndexDefs.Create(DataSet: TDataSet);
  1709. begin
  1710. FDataset := Dataset;
  1711. inherited create(Dataset, TIndexDef);
  1712. end;
  1713. destructor TIndexDefs.Destroy;
  1714. begin
  1715. inherited Destroy;
  1716. end;
  1717. Function TIndexDefs.AddIndexDef: TIndexDef;
  1718. begin
  1719. // Result := inherited add as TIndexDef;
  1720. Result:=TIndexDef.Create(Self,'','',[]);
  1721. end;
  1722. procedure TIndexDefs.Add(const Name, Fields: string; Options: TIndexOptions);
  1723. begin
  1724. TIndexDef.Create(Self,Name,Fields,Options);
  1725. end;
  1726. procedure TIndexDefs.Assign(IndexDefs: TIndexDefs);
  1727. begin
  1728. //!! To be implemented
  1729. end;
  1730. {procedure TIndexDefs.Clear;
  1731. begin
  1732. //!! To be implemented
  1733. end;}
  1734. function TIndexDefs.Find(const IndexName: string): TIndexDef;
  1735. var i: integer;
  1736. begin
  1737. Result := Nil;
  1738. for i := 0 to Count - 1 do
  1739. if AnsiSameText(Items[i].Name, IndexName) then begin
  1740. Result := Items[i];
  1741. Break;
  1742. end;
  1743. if (Result=Nil) Then
  1744. DatabaseErrorFmt(SIndexNotFound, [IndexName], FDataSet);
  1745. end;
  1746. function TIndexDefs.FindIndexForFields(const Fields: string): TIndexDef;
  1747. begin
  1748. //!! To be implemented
  1749. end;
  1750. function TIndexDefs.GetIndexForFields(const Fields: string;
  1751. CaseInsensitive: Boolean): TIndexDef;
  1752. var
  1753. i, FieldsLen: integer;
  1754. Last: TIndexDef;
  1755. begin
  1756. Last := nil;
  1757. FieldsLen := Length(Fields);
  1758. for i := 0 to Count - 1 do
  1759. begin
  1760. Result := Items[I];
  1761. if (Result.Options * [ixDescending, ixExpression] = []) and
  1762. (not CaseInsensitive or (ixCaseInsensitive in Result.Options)) and
  1763. AnsiSameText(Fields, Result.Fields) then
  1764. begin
  1765. Exit;
  1766. end else
  1767. if AnsiSameText(Fields, Copy(Result.Fields, 1, FieldsLen)) and
  1768. ((Length(Result.Fields) = FieldsLen) or
  1769. (Result.Fields[FieldsLen + 1] = ';')) then
  1770. begin
  1771. if (Last = nil) or
  1772. ((Last <> nil) And (Length(Last.Fields) > Length(Result.Fields))) then
  1773. Last := Result;
  1774. end;
  1775. end;
  1776. Result := Last;
  1777. end;
  1778. function TIndexDefs.IndexOf(const Name: string): Longint;
  1779. var i: LongInt;
  1780. begin
  1781. Result := -1;
  1782. for i := 0 to Count - 1 do
  1783. if AnsiSameText(Items[i].Name, Name) then
  1784. begin
  1785. Result := i;
  1786. Break;
  1787. end;
  1788. end;
  1789. procedure TIndexDefs.Update;
  1790. begin
  1791. if assigned(Fdataset) then
  1792. Fdataset.UpdateIndexDefs;
  1793. end;
  1794. { TCheckConstraint }
  1795. procedure TCheckConstraint.Assign(Source: TPersistent);
  1796. begin
  1797. //!! To be implemented
  1798. end;
  1799. { TCheckConstraints }
  1800. Function TCheckConstraints.GetItem(Index : Longint) : TCheckConstraint;
  1801. begin
  1802. //!! To be implemented
  1803. end;
  1804. Procedure TCheckConstraints.SetItem(index : Longint; Value : TCheckConstraint);
  1805. begin
  1806. //!! To be implemented
  1807. end;
  1808. function TCheckConstraints.GetOwner: TPersistent;
  1809. begin
  1810. //!! To be implemented
  1811. end;
  1812. constructor TCheckConstraints.Create(AOwner: TPersistent);
  1813. begin
  1814. //!! To be implemented
  1815. end;
  1816. function TCheckConstraints.Add: TCheckConstraint;
  1817. begin
  1818. //!! To be implemented
  1819. end;
  1820. { TLookupList }
  1821. constructor TLookupList.Create;
  1822. begin
  1823. FList := TList.Create;
  1824. end;
  1825. destructor TLookupList.Destroy;
  1826. begin
  1827. if FList <> nil then Clear;
  1828. FList.Free;
  1829. inherited Destroy;
  1830. end;
  1831. procedure TLookupList.Add(const AKey, AValue: Variant);
  1832. var LookupRec: PLookupListRec;
  1833. begin
  1834. New(LookupRec);
  1835. LookupRec^.Key := AKey;
  1836. LookupRec^.Value := AValue;
  1837. FList.Add(LookupRec);
  1838. end;
  1839. procedure TLookupList.Clear;
  1840. var i: integer;
  1841. begin
  1842. for i := 0 to FList.Count - 1 do Dispose(PLookupListRec(FList[i]));
  1843. FList.Clear;
  1844. end;
  1845. function TLookupList.ValueOfKey(const AKey: Variant): Variant;
  1846. var I: Integer;
  1847. begin
  1848. Result := Null;
  1849. if VarIsNull(AKey) then Exit;
  1850. i := FList.Count - 1;
  1851. while (i > 0) And (PLookupListRec(FList.Items[I])^.Key <> AKey) do Dec(i);
  1852. if i >= 0 then Result := PLookupListRec(FList.Items[I])^.Value;
  1853. end;
  1854. procedure DisposeMem(var Buffer; Size: Integer);
  1855. begin
  1856. if Pointer(Buffer) <> nil then
  1857. begin
  1858. FreeMem(Pointer(Buffer), Size);
  1859. Pointer(Buffer) := nil;
  1860. end;
  1861. end;
  1862. function BuffersEqual(Buf1, Buf2: Pointer; Size: Integer): Boolean;
  1863. begin
  1864. Result:=CompareByte(Buf1,Buf2,Size)=0
  1865. end;
  1866. {$i dataset.inc}
  1867. {$i fields.inc}
  1868. {$i datasource.inc}
  1869. {$i database.inc}
  1870. {$i dsparams.inc}
  1871. end.