db.pp 70 KB

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