db.pp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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;
  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. {LargeInt}
  26. LargeInt = Int64;
  27. { Auxiliary type }
  28. TStringFieldBuffer = Array[0..dsMaxStringSize] of Char;
  29. { Misc Dataset types }
  30. TDataSetState = (dsInactive, dsBrowse, dsEdit, dsInsert, dsSetKey,
  31. dsCalcFields, dsFilter, dsNewValue, dsOldValue, dsCurValue);
  32. TDataEvent = (deFieldChange, deRecordChange, deDataSetChange,
  33. deDataSetScroll, deLayoutChange, deUpdateRecord, deUpdateState,
  34. deCheckBrowseMode, dePropertyChange, deFieldListChange, deFocusControl);
  35. TUpdateStatus = (usUnmodified, usModified, usInserted, usDeleted);
  36. { Forward declarations }
  37. TFieldDef = class;
  38. TFieldDefs = class;
  39. TField = class;
  40. TFields = Class;
  41. TDataSet = class;
  42. TDataBase = Class;
  43. TDatasource = Class;
  44. TDatalink = Class;
  45. { Exception classes }
  46. EDatabaseError = class(Exception);
  47. { TFieldDef }
  48. TFieldClass = class of TField;
  49. {
  50. TFieldType = (ftUnknown, ftString, ftSmallint, ftInteger, ftWord,
  51. ftBoolean, ftFloat, ftDate, ftTime, ftDateTime,
  52. ftBytes, ftVarBytes, ftAutoInc, ftBlob, ftMemo, ftGraphic,
  53. ftFmtMemo, ftParadoxOle, ftDBaseOle, ftTypedBinary, ftCursor);
  54. }
  55. TFieldType = (ftUnknown, ftString, ftSmallint, ftInteger, ftWord,
  56. ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate, ftTime, ftDateTime,
  57. ftBytes, ftVarBytes, ftAutoInc, ftBlob, ftMemo, ftGraphic, ftFmtMemo,
  58. ftParadoxOle, ftDBaseOle, ftTypedBinary, ftCursor, ftFixedChar,
  59. ftWideString, ftLargeint, ftADT, ftArray, ftReference,
  60. ftDataSet, ftOraBlob, ftOraClob, ftVariant, ftInterface,
  61. ftIDispatch, ftGuid, ftTimeStamp, ftFMTBcd);
  62. { TDateTimeRec }
  63. TDateTimeAlias = type TDateTime;
  64. TDateTimeRec = record
  65. case TFieldType of
  66. ftDate: (Date: Longint);
  67. ftTime: (Time: Longint);
  68. ftDateTime: (DateTime: TDateTimeAlias);
  69. end;
  70. TFieldDef = class(TComponent)
  71. Private
  72. FDataType : TFieldType;
  73. FFieldNo : Longint;
  74. FInternalCalcField : Boolean;
  75. FPrecision : Longint;
  76. FRequired : Boolean;
  77. FSize : Word;
  78. FName : String;
  79. Function GetFieldClass : TFieldClass;
  80. public
  81. constructor Create(AOwner: TFieldDefs; const AName: string;
  82. ADataType: TFieldType; ASize: Word; ARequired: Boolean; AFieldNo: Longint);
  83. destructor Destroy; override;
  84. function CreateField(AOwner: TComponent): TField;
  85. property InternalCalcField: Boolean read FInternalCalcField write FInternalCalcField;
  86. property DataType: TFieldType read FDataType;
  87. property FieldClass: TFieldClass read GetFieldClass;
  88. property FieldNo: Longint read FFieldNo;
  89. property Name: string read FName;
  90. property Precision: Longint read FPrecision write FPrecision;
  91. property Required: Boolean read FRequired;
  92. property Size: Word read FSize;
  93. end;
  94. { TFieldDefs }
  95. TFieldDefs = class(TComponent)
  96. private
  97. FDataSet: TDataSet;
  98. FItems: TList;
  99. FUpdated: Boolean;
  100. function GetCount: Longint;
  101. function GetItem(Index: Longint): TFieldDef;
  102. public
  103. constructor Create(ADataSet: TDataSet);
  104. destructor Destroy; override;
  105. procedure Add(const AName: string; ADataType: TFieldType; ASize: Word;
  106. ARequired: Boolean);
  107. procedure Assign(FieldDefs: TFieldDefs);
  108. procedure Clear;
  109. function Find(const AName: string): TFieldDef;
  110. function IndexOf(const AName: string): Longint;
  111. procedure Update;
  112. property Count: Longint read GetCount;
  113. property Items[Index: Longint]: TFieldDef read GetItem; default;
  114. end;
  115. { TField }
  116. TFieldKind = (fkData, fkCalculated, fkLookup, fkInternalCalc);
  117. TFieldKinds = Set of TFieldKind;
  118. TFieldNotifyEvent = procedure(Sender: TField) of object;
  119. TFieldGetTextEvent = procedure(Sender: TField; var Text: string;
  120. DisplayText: Boolean) of object;
  121. TFieldSetTextEvent = procedure(Sender: TField; const Text: string) of object;
  122. TFieldRef = ^TField;
  123. TFieldChars = set of Char;
  124. { TAlignment may need to come from somewhere else }
  125. TAlignMent = (taLeftjustify,taCenter,taRightJustify);
  126. TField = class(TComponent)
  127. Private
  128. FAlignMent : TAlignment;
  129. FAttributeSet : String;
  130. FBuffers : ppchar;
  131. FCalculated : Boolean;
  132. FCanModify : Boolean;
  133. FConstraintErrorMessage : String;
  134. FCustomConstraint : String;
  135. FDataSet : TDataSet;
  136. FDataSize : Word;
  137. FDataType : TFieldType;
  138. FDefaultExpression : String;
  139. FDisplayLabel : String;
  140. FDisplayWidth : Longint;
  141. FEditText : String;
  142. FFieldKind : TFieldKind;
  143. FFieldName : String;
  144. FFieldNo : Longint;
  145. FFields : TFields;
  146. FHasConstraints : Boolean;
  147. FImportedConstraint : String;
  148. FIsIndexField : Boolean;
  149. FKeyFields : String;
  150. FLookupCache : Boolean;
  151. FLookupDataSet : TDataSet;
  152. FLookupKeyfields : String;
  153. FLookupresultField : String;
  154. FOffset : Word;
  155. FOnChange : TFieldNotifyEvent;
  156. FOnGetText: TFieldGetTextEvent;
  157. FOnSetText: TFieldSetTextEvent;
  158. FOnValidate: TFieldNotifyEvent;
  159. FOrigin : String;
  160. FReadOnly : Boolean;
  161. FRequired : Boolean;
  162. FSize : Word;
  163. FValidChars : TFieldChars;
  164. FValueBuffer : Pointer;
  165. FValidating : Boolean;
  166. FVisible : Boolean;
  167. Function GetIndex : longint;
  168. Procedure SetDataset(VAlue : TDataset);
  169. function GetDisplayText: String;
  170. protected
  171. function AccessError(const TypeName: string): EDatabaseError;
  172. procedure CheckInactive;
  173. class procedure CheckTypeSize(AValue: Longint); virtual;
  174. procedure Change; virtual;
  175. procedure DataChanged;
  176. procedure FreeBuffers; virtual;
  177. function GetAsBoolean: Boolean; virtual;
  178. function GetAsDateTime: TDateTime; virtual;
  179. function GetAsFloat: Extended; virtual;
  180. function GetAsLongint: Longint; virtual;
  181. function GetAsString: string; virtual;
  182. function GetCanModify: Boolean; virtual;
  183. function GetDataSize: Word; virtual;
  184. function GetDefaultWidth: Longint; virtual;
  185. function GetDisplayName : String;
  186. function GetIsNull: Boolean; virtual;
  187. function GetParentComponent: TComponent; override;
  188. procedure GetText(var AText: string; ADisplayText: Boolean); virtual;
  189. function HasParent: Boolean; override;
  190. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  191. procedure PropertyChanged(LayoutAffected: Boolean);
  192. procedure ReadState(Reader: TReader); override;
  193. procedure SetAsBoolean(AValue: Boolean); virtual;
  194. procedure SetAsDateTime(AValue: TDateTime); virtual;
  195. procedure SetAsFloat(AValue: Extended); virtual;
  196. procedure SetAsLongint(AValue: Longint); virtual;
  197. procedure SetAsString(const AValue: string); virtual;
  198. procedure SetDataType(AValue: TFieldType);
  199. procedure SetSize(AValue: Word); virtual;
  200. procedure SetParentComponent(AParent: TComponent); override;
  201. procedure SetText(const AValue: string); virtual;
  202. public
  203. constructor Create(AOwner: TComponent); override;
  204. destructor Destroy; override;
  205. procedure Assign(Source: TPersistent); override;
  206. procedure Clear; virtual;
  207. procedure FocusControl;
  208. function GetData(Buffer: Pointer): Boolean;
  209. class function IsBlob: Boolean; virtual;
  210. function IsValidChar(InputChar: Char): Boolean; virtual;
  211. procedure SetData(Buffer: Pointer);
  212. procedure SetFieldType(AValue: TFieldType); virtual;
  213. procedure Validate(Buffer: Pointer);
  214. property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
  215. property AsDateTime: TDateTime read GetAsDateTime write SetAsDateTime;
  216. property AsFloat: Extended read GetAsFloat write SetAsFloat;
  217. property AsLongint: Longint read GetAsLongint write SetAsLongint;
  218. property AsString: string read GetAsString write SetAsString;
  219. property AttributeSet: string read FAttributeSet write FAttributeSet;
  220. property Calculated: Boolean read FCalculated write FCalculated;
  221. property CanModify: Boolean read FCanModify;
  222. property DataSet: TDataSet read FDataSet write SetDataSet;
  223. property DataSize: Word read GetDataSize;
  224. property DataType: TFieldType read FDataType;
  225. property DisplayName: String Read GetDisplayName;
  226. property DisplayText: String read GetDisplayText;
  227. property FieldNo: Longint read FFieldNo;
  228. property IsIndexField: Boolean read FIsIndexField;
  229. property IsNull: Boolean read GetIsNull;
  230. property Offset: word read FOffset;
  231. property Size: Word read FSize write FSize;
  232. property Text: string read FEditText write FEditText;
  233. property ValidChars : TFieldChars Read FValidChars;
  234. published
  235. property AlignMent : TAlignMent Read FAlignMent write FAlignment;
  236. property CustomConstraint: string read FCustomConstraint write FCustomConstraint;
  237. property ConstraintErrorMessage: string read FConstraintErrorMessage write FConstraintErrorMessage;
  238. property DefaultExpression: string read FDefaultExpression write FDefaultExpression;
  239. property DisplayLabel : string read FDisplayLabel write FDisplayLabel;
  240. property DisplayWidth: Longint read FDisplayWidth write FDisplayWidth;
  241. property FieldKind: TFieldKind read FFieldKind write FFieldKind;
  242. property FieldName: string read FFieldName write FFieldName;
  243. property HasConstraints: Boolean read FHasConstraints;
  244. property Index: Longint read GetIndex;
  245. property ImportedConstraint: string read FImportedConstraint write FImportedConstraint;
  246. property LookupDataSet: TDataSet read FLookupDataSet write FLookupDataSet;
  247. property LookupKeyFields: string read FLookupKeyFields write FLookupKeyFields;
  248. property LookupResultField: string read FLookupResultField write FLookupResultField;
  249. property KeyFields: string read FKeyFields write FKeyFields;
  250. property LookupCache: Boolean read FLookupCache write FLookupCache;
  251. property Origin: string read FOrigin write FOrigin;
  252. property ReadOnly: Boolean read FReadOnly write FReadOnly;
  253. property Required: Boolean read FRequired write FRequired;
  254. property Visible: Boolean read FVisible write FVisible;
  255. property OnChange: TFieldNotifyEvent read FOnChange write FOnChange;
  256. property OnGetText: TFieldGetTextEvent read FOnGetText write FOnGetText;
  257. property OnSetText: TFieldSetTextEvent read FOnSetText write FOnSetText;
  258. property OnValidate: TFieldNotifyEvent read FOnValidate write FOnValidate;
  259. end;
  260. { TStringField }
  261. TStringField = class(TField)
  262. protected
  263. class procedure CheckTypeSize(AValue: Longint); override;
  264. function GetAsBoolean: Boolean; override;
  265. function GetAsDateTime: TDateTime; override;
  266. function GetAsFloat: Extended; override;
  267. function GetAsLongint: Longint; override;
  268. function GetAsString: string; override;
  269. function GetDataSize: Word; override;
  270. function GetDefaultWidth: Longint; override;
  271. procedure GetText(var AText: string; ADisplayText: Boolean); override;
  272. function GetValue(var AValue: string): Boolean;
  273. procedure SetAsBoolean(AValue: Boolean); override;
  274. procedure SetAsDateTime(AValue: TDateTime); override;
  275. procedure SetAsFloat(AValue: Extended); override;
  276. procedure SetAsLongint(AValue: Longint); override;
  277. procedure SetAsString(const AValue: string); override;
  278. public
  279. constructor Create(AOwner: TComponent); override;
  280. property Value: string read GetAsString write SetAsString;
  281. published
  282. property Size default 20;
  283. end;
  284. { TNumericField }
  285. TNumericField = class(TField)
  286. Private
  287. FDisplayFormat : String;
  288. FEditFormat : String;
  289. protected
  290. procedure RangeError(AValue, Min, Max: Extended);
  291. procedure SetDisplayFormat(const AValue: string);
  292. procedure SetEditFormat(const AValue: string);
  293. public
  294. constructor Create(AOwner: TComponent); override;
  295. published
  296. property DisplayFormat: string read FDisplayFormat write SetDisplayFormat;
  297. property EditFormat: string read FEditFormat write SetEditFormat;
  298. end;
  299. { TLongintField }
  300. TLongintField = class(TNumericField)
  301. private
  302. FMinValue,
  303. FMaxValue,
  304. FMinRange,
  305. FMAxRange : Longint;
  306. Procedure SetMinValue (AValue : longint);
  307. Procedure SetMaxValue (AValue : longint);
  308. protected
  309. function GetAsFloat: Extended; override;
  310. function GetAsLongint: Longint; override;
  311. function GetAsString: string; override;
  312. function GetDataSize: Word; override;
  313. procedure GetText(var AText: string; ADisplayText: Boolean); override;
  314. function GetValue(var AValue: Longint): Boolean;
  315. procedure SetAsFloat(AValue: Extended); override;
  316. procedure SetAsLongint(AValue: Longint); override;
  317. procedure SetAsString(const AValue: string); override;
  318. public
  319. constructor Create(AOwner: TComponent); override;
  320. Function CheckRange(AValue : longint) : Boolean;
  321. property Value: Longint read GetAsLongint write SetAsLongint;
  322. published
  323. property MaxValue: Longint read FMaxValue write SetMaxValue default 0;
  324. property MinValue: Longint read FMinValue write SetMinValue default 0;
  325. end;
  326. TIntegerField = TLongintField;
  327. { TSmallintField }
  328. TSmallintField = class(TLongintField)
  329. protected
  330. function GetDataSize: Word; override;
  331. public
  332. constructor Create(AOwner: TComponent); override;
  333. end;
  334. { TWordField }
  335. TWordField = class(TLongintField)
  336. protected
  337. function GetDataSize: Word; override;
  338. public
  339. constructor Create(AOwner: TComponent); override;
  340. end;
  341. { TAutoIncField }
  342. TAutoIncField = class(TLongintField)
  343. Protected
  344. Procedure SetAsLongInt(AValue : Longint); override;
  345. public
  346. constructor Create(AOwner: TComponent); override;
  347. end;
  348. { TFloatField }
  349. TFloatField = class(TNumericField)
  350. private
  351. FMaxValue : Extended;
  352. FMinValue : Extended;
  353. FPrecision : Longint;
  354. protected
  355. function GetAsFloat: Extended; override;
  356. function GetAsLongint: Longint; override;
  357. function GetAsString: string; override;
  358. function GetDataSize: Word; override;
  359. procedure GetText(var theText: string; ADisplayText: Boolean); override;
  360. procedure SetAsFloat(AValue: Extended); override;
  361. procedure SetAsLongint(AValue: Longint); override;
  362. procedure SetAsString(const AValue: string); override;
  363. public
  364. constructor Create(AOwner: TComponent); override;
  365. Function CheckRange(AValue : Extended) : Boolean;
  366. property Value: Extended read GetAsFloat write SetAsFloat;
  367. published
  368. property MaxValue: Extended read FMaxValue write FMaxValue;
  369. property MinValue: Extended read FMinValue write FMinValue;
  370. property Precision: Longint read FPrecision write FPrecision default 15;
  371. end;
  372. { TBooleanField }
  373. TBooleanField = class(TField)
  374. private
  375. FDisplayValues : String;
  376. // First byte indicates uppercase or not.
  377. FDisplays : Array[Boolean,Boolean] of string;
  378. Procedure SetDisplayValues(AValue : String);
  379. protected
  380. function GetAsBoolean: Boolean; override;
  381. function GetAsString: string; override;
  382. function GetDataSize: Word; override;
  383. function GetDefaultWidth: Longint; override;
  384. procedure SetAsBoolean(AValue: Boolean); override;
  385. procedure SetAsString(const AValue: string); override;
  386. public
  387. constructor Create(AOwner: TComponent); override;
  388. property Value: Boolean read GetAsBoolean write SetAsBoolean;
  389. published
  390. property DisplayValues: string read FDisplayValues write SetDisplayValues;
  391. end;
  392. { TDateTimeField }
  393. TDateTimeField = class(TField)
  394. private
  395. FDisplayFormat : String;
  396. protected
  397. function GetAsDateTime: TDateTime; override;
  398. function GetAsFloat: Extended; override;
  399. function GetAsString: string; override;
  400. function GetDataSize: Word; override;
  401. procedure GetText(var theText: string; ADisplayText: Boolean); override;
  402. procedure SetAsDateTime(AValue: TDateTime); override;
  403. procedure SetAsFloat(AValue: Extended); override;
  404. procedure SetAsString(const AValue: string); override;
  405. public
  406. constructor Create(AOwner: TComponent); override;
  407. property Value: TDateTime read GetAsDateTime write SetAsDateTime;
  408. published
  409. property DisplayFormat: string read FDisplayFormat write FDisplayFormat;
  410. end;
  411. { TDateField }
  412. TDateField = class(TDateTimeField)
  413. protected
  414. function GetDataSize: Word; override;
  415. public
  416. constructor Create(AOwner: TComponent); override;
  417. end;
  418. { TTimeField }
  419. TTimeField = class(TDateTimeField)
  420. protected
  421. function GetDataSize: Word; override;
  422. public
  423. constructor Create(AOwner: TComponent); override;
  424. end;
  425. { TBinaryField }
  426. TBinaryField = class(TField)
  427. protected
  428. class procedure CheckTypeSize(AValue: Longint); override;
  429. function GetAsString: string; override;
  430. procedure GetText(var TheText: string; ADisplayText: Boolean); override;
  431. procedure SetAsString(const AValue: string); override;
  432. procedure SetText(const AValue: string); override;
  433. public
  434. constructor Create(AOwner: TComponent); override;
  435. published
  436. property Size default 16;
  437. end;
  438. { TBytesField }
  439. TBytesField = class(TBinaryField)
  440. protected
  441. function GetDataSize: Word; override;
  442. public
  443. constructor Create(AOwner: TComponent); override;
  444. end;
  445. { TVarBytesField }
  446. TVarBytesField = class(TBytesField)
  447. protected
  448. function GetDataSize: Word; override;
  449. public
  450. constructor Create(AOwner: TComponent); override;
  451. end;
  452. { TBCDField }
  453. TBCDField = class(TNumericField)
  454. private
  455. protected
  456. class procedure CheckTypeSize(AValue: Longint); override;
  457. function GetAsFloat: Extended; override;
  458. function GetAsLongint: Longint; override;
  459. function GetAsString: string; override;
  460. function GetDataSize: Word; override;
  461. function GetDefaultWidth: Longint; override;
  462. procedure GetText(var TheText: string; ADisplayText: Boolean); override;
  463. procedure SetAsFloat(AValue: Extended); override;
  464. procedure SetAsLongint(AValue: Longint); override;
  465. procedure SetAsString(const AValue: string); override;
  466. public
  467. constructor Create(AOwner: TComponent); override;
  468. published
  469. property Size default 4;
  470. end;
  471. { TBlobField }
  472. TBlobStreamMode = (bmRead, bmWrite, bmReadWrite);
  473. TBlobType = ftBlob..ftTypedBinary;
  474. TBlobField = class(TField)
  475. private
  476. FBlobSize : Longint;
  477. FBlobType : TBlobType;
  478. FModified : Boolean;
  479. FTransliterate : Boolean;
  480. Function GetBlobStream (Mode : TBlobStreamMode) : TStream;
  481. protected
  482. procedure AssignTo(Dest: TPersistent); override;
  483. procedure FreeBuffers; override;
  484. function GetAsString: string; override;
  485. function GetBlobSize: Longint; virtual;
  486. function GetIsNull: Boolean; override;
  487. procedure GetText(var TheText: string; ADisplayText: Boolean); override;
  488. procedure SetAsString(const AValue: string); override;
  489. procedure SetText(const AValue: string); override;
  490. public
  491. constructor Create(AOwner: TComponent); override;
  492. procedure Assign(Source: TPersistent); override;
  493. procedure Clear; override;
  494. class function IsBlob: Boolean; override;
  495. procedure LoadFromFile(const FileName: string);
  496. procedure LoadFromStream(Stream: TStream);
  497. procedure SaveToFile(const FileName: string);
  498. procedure SaveToStream(Stream: TStream);
  499. procedure SetFieldType(AValue: TFieldType); override;
  500. property BlobSize: Longint read FBlobSize;
  501. property Modified: Boolean read FModified write FModified;
  502. property Value: string read GetAsString write SetAsString;
  503. property Transliterate: Boolean read FTransliterate write FTransliterate;
  504. published
  505. property BlobType: TBlobType read FBlobType write FBlobType;
  506. property Size default 0;
  507. end;
  508. { TMemoField }
  509. TMemoField = class(TBlobField)
  510. public
  511. constructor Create(AOwner: TComponent); override;
  512. published
  513. property Transliterate default True;
  514. end;
  515. { TGraphicField }
  516. TGraphicField = class(TBlobField)
  517. public
  518. constructor Create(AOwner: TComponent); override;
  519. end;
  520. { TIndexDef }
  521. TIndexDefs = class;
  522. TIndexOptions = set of (ixPrimary, ixUnique, ixDescending,
  523. ixCaseInsensitive, ixExpression);
  524. TIndexDef = class
  525. Private
  526. FExpression : String;
  527. FFields : String;
  528. FName : String;
  529. FOptions : TIndexOptions;
  530. FSource : String;
  531. public
  532. constructor Create(Owner: TIndexDefs; const AName, TheFields: string;
  533. TheOptions: TIndexOptions);
  534. destructor Destroy; override;
  535. property Expression: string read FExpression;
  536. property Fields: string read FFields;
  537. property Name: string read FName;
  538. property Options: TIndexOptions read FOptions;
  539. property Source: string read FSource write FSource;
  540. end;
  541. { TIndexDefs }
  542. TIndexDefs = class
  543. Private
  544. FCount : Longint;
  545. FUpDated : Boolean;
  546. Function GetItem (Index : longint) : TindexDef;
  547. public
  548. constructor Create(DataSet: TDataSet);
  549. destructor Destroy; override;
  550. procedure Add(const Name, Fields: string; Options: TIndexOptions);
  551. procedure Assign(IndexDefs: TIndexDefs);
  552. procedure Clear;
  553. function FindIndexForFields(const Fields: string): TIndexDef;
  554. function GetIndexForFields(const Fields: string;
  555. CaseInsensitive: Boolean): TIndexDef;
  556. function IndexOf(const Name: string): Longint;
  557. procedure Update;
  558. property Count: Longint read FCount;
  559. property Items[Index: Longint]: TIndexDef read GetItem; default;
  560. property Updated: Boolean read FUpdated write FUpdated;
  561. end;
  562. { TCheckConstraint }
  563. TCheckConstraint = class(TCollectionItem)
  564. Private
  565. FCustomConstraint : String;
  566. FErrorMessage : String;
  567. FFromDictionary : Boolean;
  568. FImportedConstraint : String;
  569. public
  570. procedure Assign(Source: TPersistent); override;
  571. // function GetDisplayName: string; override;
  572. published
  573. property CustomConstraint: string read FCustomConstraint write FCustomConstraint;
  574. property ErrorMessage: string read FErrorMessage write FErrorMessage;
  575. property FromDictionary: Boolean read FFromDictionary write FFromDictionary;
  576. property ImportedConstraint: string read FImportedConstraint write FImportedConstraint;
  577. end;
  578. { TCheckConstraints }
  579. TCheckConstraints = class(TCollection)
  580. Private
  581. Function GetItem(Index : Longint) : TCheckConstraint;
  582. Procedure SetItem(index : Longint; Value : TCheckConstraint);
  583. protected
  584. function GetOwner: TPersistent; override;
  585. public
  586. constructor Create(Owner: TPersistent);
  587. function Add: TCheckConstraint;
  588. property Items[Index: Longint]: TCheckConstraint read GetItem write SetItem; default;
  589. end;
  590. { TFields }
  591. Tfields = Class(TObject)
  592. Private
  593. FDataset : TDataset;
  594. FFieldList : TList;
  595. FOnChange : TNotifyEvent;
  596. FValidFieldKinds : TFieldKinds;
  597. Protected
  598. Procedure Changed;
  599. Procedure CheckfieldKind(Fieldkind : TFieldKind; Field : TField);
  600. Function GetCount : Longint;
  601. Function GetField (Index : longint) : TField;
  602. Procedure SetFieldIndex (Field : TField;Value : Integer);
  603. Property OnChange : TNotifyEvent Read FOnChange Write FOnChange;
  604. Property ValidFieldKinds : TFieldKinds Read FValidFieldKinds;
  605. Public
  606. Constructor Create(ADataset : TDataset);
  607. Destructor Destroy;override;
  608. Procedure Add(Field : TField);
  609. Procedure CheckFieldName (Const Value : String);
  610. Procedure CheckFieldNames (Const Value : String);
  611. Procedure Clear;
  612. Function FindField (Const Value : String) : TField;
  613. Function FieldByName (Const Value : String) : TField;
  614. Function FieldByNumber(FieldNo : Integer) : TField;
  615. Procedure GetFieldNames (Values : TStrings);
  616. Function IndexOf(Field : TField) : Longint;
  617. procedure Remove(Value : TField);
  618. Property Count : Integer Read GetCount;
  619. Property Dataset : TDataset Read FDataset;
  620. Property Fields [Index : Integer] : TField Read GetField; default;
  621. end;
  622. { TDataSet }
  623. TBookmark = Pointer;
  624. TBookmarkStr = string;
  625. PBookmarkFlag = ^TBookmarkFlag;
  626. TBookmarkFlag = (bfCurrent, bfBOF, bfEOF, bfInserted);
  627. PBufferList = ^TBufferList;
  628. TBufferList = array[0..dsMaxBufferCount - 1] of PChar;
  629. TGetMode = (gmCurrent, gmNext, gmPrior);
  630. TGetResult = (grOK, grBOF, grEOF, grError);
  631. TResyncMode = set of (rmExact, rmCenter);
  632. TDataAction = (daFail, daAbort, daRetry);
  633. TUpdateKind = (ukModify, ukInsert, ukDelete);
  634. TLocateOption = (loCaseInsensitive, loPartialKey);
  635. TLocateOptions = set of TLocateOption;
  636. TDataOperation = procedure of object;
  637. TDataSetNotifyEvent = procedure(DataSet: TDataSet) of object;
  638. TDataSetErrorEvent = procedure(DataSet: TDataSet; E: EDatabaseError;
  639. var Action: TDataAction) of object;
  640. TFilterOption = (foCaseInsensitive, foNoPartialCompare);
  641. TFilterOptions = set of TFilterOption;
  642. TFilterRecordEvent = procedure(DataSet: TDataSet;
  643. var Accept: Boolean) of object;
  644. TDatasetClass = Class of TDataset;
  645. TBufferArray = ^pchar;
  646. TDataSet = class(TComponent)
  647. Private
  648. FActive: Boolean;
  649. FActiveRecord: Longint;
  650. FAfterCancel: TDataSetNotifyEvent;
  651. FAfterClose: TDataSetNotifyEvent;
  652. FAfterDelete: TDataSetNotifyEvent;
  653. FAfterEdit: TDataSetNotifyEvent;
  654. FAfterInsert: TDataSetNotifyEvent;
  655. FAfterOpen: TDataSetNotifyEvent;
  656. FAfterPost: TDataSetNotifyEvent;
  657. FAfterScroll: TDataSetNotifyEvent;
  658. FAutoCalcFields: Boolean;
  659. FBOF: Boolean;
  660. FBeforeCancel: TDataSetNotifyEvent;
  661. FBeforeClose: TDataSetNotifyEvent;
  662. FBeforeDelete: TDataSetNotifyEvent;
  663. FBeforeEdit: TDataSetNotifyEvent;
  664. FBeforeInsert: TDataSetNotifyEvent;
  665. FBeforeOpen: TDataSetNotifyEvent;
  666. FBeforePost: TDataSetNotifyEvent;
  667. FBeforeScroll: TDataSetNotifyEvent;
  668. FBlobFieldCount: Longint;
  669. FBookmark: TBookmarkStr;
  670. FBookmarkSize: Longint;
  671. FBuffers : TBufferArray;
  672. FBufferCount: Longint;
  673. FCalcBuffer: PChar;
  674. FCalcFieldsSize: Longint;
  675. FCanModify: Boolean;
  676. FConstraints: TCheckConstraints;
  677. FDisableControlsCount : Integer;
  678. FDisableControlsState : TDatasetState;
  679. FCurrentRecord: Longint;
  680. FDataSources : TList;
  681. FDefaultFields: Boolean;
  682. FEOF: Boolean;
  683. FEnableControlsEvent : TDataEvent;
  684. FFieldList : TFields;
  685. FFieldCount : Longint;
  686. FFieldDefs: TFieldDefs;
  687. FFilterOptions: TFilterOptions;
  688. FFilterText: string;
  689. FFiltered: Boolean;
  690. FFound: Boolean;
  691. FInternalCalcFields: Boolean;
  692. FModified: Boolean;
  693. FOnCalcFields: TDataSetNotifyEvent;
  694. FOnDeleteError: TDataSetErrorEvent;
  695. FOnEditError: TDataSetErrorEvent;
  696. FOnFilterRecord: TFilterRecordEvent;
  697. FOnNewRecord: TDataSetNotifyEvent;
  698. FOnPostError: TDataSetErrorEvent;
  699. FRecNo: Longint;
  700. FRecordCount: Longint;
  701. FRecordSize: Word;
  702. FState : TDataSetState;
  703. Procedure DoInsertAppend(DoAppend : Boolean);
  704. Procedure DoInternalOpen;
  705. Procedure DoInternalClose;
  706. Function GetBuffer (Index : longint) : Pchar;
  707. Function GetField (Index : Longint) : TField;
  708. procedure RecalcBufListSize;
  709. Procedure RegisterDataSource(ADatasource : TDataSource);
  710. Procedure RemoveField (Field : TField);
  711. Procedure SetActive (Value : Boolean);
  712. procedure SetBufferCount(const AValue: Longint);
  713. Procedure SetField (Index : Longint;Value : TField);
  714. Procedure ShiftBuffers (Offset,Distance : Longint);
  715. Function TryDoing (P : TDataOperation; Ev : TDatasetErrorEvent) : Boolean;
  716. Procedure UnRegisterDataSource(ADatasource : TDatasource);
  717. Procedure UpdateFieldDefs;
  718. protected
  719. procedure ActivateBuffers; virtual;
  720. procedure BindFields(Binding: Boolean);
  721. function BookmarkAvailable: Boolean;
  722. procedure CalculateFields(Buffer: PChar); virtual;
  723. procedure CheckActive; virtual;
  724. procedure CheckInactive; virtual;
  725. procedure ClearBuffers; virtual;
  726. procedure ClearCalcFields(Buffer: PChar); virtual;
  727. procedure CloseBlob(Field: TField); virtual;
  728. procedure CloseCursor; virtual;
  729. procedure CreateFields;
  730. procedure DataEvent(Event: TDataEvent; Info: Longint); virtual;
  731. procedure DestroyFields; virtual;
  732. procedure DoAfterCancel; virtual;
  733. procedure DoAfterClose; virtual;
  734. procedure DoAfterDelete; virtual;
  735. procedure DoAfterEdit; virtual;
  736. procedure DoAfterInsert; virtual;
  737. procedure DoAfterOpen; virtual;
  738. procedure DoAfterPost; virtual;
  739. procedure DoAfterScroll; virtual;
  740. procedure DoBeforeCancel; virtual;
  741. procedure DoBeforeClose; virtual;
  742. procedure DoBeforeDelete; virtual;
  743. procedure DoBeforeEdit; virtual;
  744. procedure DoBeforeInsert; virtual;
  745. procedure DoBeforeOpen; virtual;
  746. procedure DoBeforePost; virtual;
  747. procedure DoBeforeScroll; virtual;
  748. procedure DoOnCalcFields; virtual;
  749. procedure DoOnNewRecord; virtual;
  750. function FieldByNumber(FieldNo: Longint): TField;
  751. function FindRecord(Restart, GoForward: Boolean): Boolean; virtual;
  752. procedure FreeFieldBuffers; virtual;
  753. function GetBookmarkStr: TBookmarkStr; virtual;
  754. procedure GetCalcFields(Buffer: PChar); virtual;
  755. function GetCanModify: Boolean; virtual;
  756. procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
  757. function GetFieldClass(FieldType: TFieldType): TFieldClass; virtual;
  758. Function GetfieldCount : Integer;
  759. function GetIsIndexField(Field: TField): Boolean; virtual;
  760. function GetNextRecords: Longint; virtual;
  761. function GetNextRecord: Boolean; virtual;
  762. function GetPriorRecords: Longint; virtual;
  763. function GetPriorRecord: Boolean; virtual;
  764. function GetRecordCount: Longint; virtual;
  765. function GetRecNo: Longint; virtual;
  766. procedure InitFieldDefs; virtual;
  767. procedure InitRecord(Buffer: PChar); virtual;
  768. procedure InternalCancel; virtual;
  769. procedure InternalEdit; virtual;
  770. procedure InternalRefresh; virtual;
  771. procedure Loaded; override;
  772. procedure OpenCursor(InfoQuery: Boolean); virtual;
  773. procedure RefreshInternalCalcFields(Buffer: PChar); virtual;
  774. Function RequiredBuffers : longint;
  775. procedure RestoreState(const Value: TDataSetState);
  776. procedure SetBookmarkStr(const Value: TBookmarkStr); virtual;
  777. procedure SetBufListSize(Value: Longint);
  778. procedure SetChildOrder(Component: TComponent; Order: Longint); override;
  779. procedure SetCurrentRecord(Index: Longint); virtual;
  780. procedure SetFiltered(Value: Boolean); virtual;
  781. procedure SetFilterOptions(Value: TFilterOptions); virtual;
  782. procedure SetFilterText(const Value: string); virtual;
  783. procedure SetFound(const Value: Boolean);
  784. procedure SetModified(Value: Boolean);
  785. procedure SetName(const Value: TComponentName); override;
  786. procedure SetOnFilterRecord(const Value: TFilterRecordEvent); virtual;
  787. procedure SetRecNo(Value: Longint); virtual;
  788. procedure SetState(Value: TDataSetState);
  789. function SetTempState(const Value: TDataSetState): TDataSetState;
  790. function TempBuffer: PChar;
  791. procedure UpdateIndexDefs; virtual;
  792. property ActiveRecord: Longint read FActiveRecord;
  793. property CurrentRecord: Longint read FCurrentRecord;
  794. property BlobFieldCount: Longint read FBlobFieldCount;
  795. property BookmarkSize: Longint read FBookmarkSize write FBookmarkSize;
  796. property Buffers[Index: Longint]: PChar read GetBuffer;
  797. property BufferCount: Longint read FBufferCount;
  798. property CalcBuffer: PChar read FCalcBuffer;
  799. property CalcFieldsSize: Longint read FCalcFieldsSize;
  800. property InternalCalcFields: Boolean read FInternalCalcFields;
  801. property Constraints: TCheckConstraints read FConstraints write FConstraints;
  802. protected { abstract methods }
  803. function AllocRecordBuffer: PChar; virtual; abstract;
  804. procedure FreeRecordBuffer(var Buffer: PChar); virtual; abstract;
  805. procedure GetBookmarkData(Buffer: PChar; Data: Pointer); virtual; abstract;
  806. function GetBookmarkFlag(Buffer: PChar): TBookmarkFlag; virtual; abstract;
  807. function GetDataSource: TDataSource; virtual;
  808. function GetFieldData(Field: TField; Buffer: Pointer): Boolean; virtual; abstract;
  809. function GetRecord(Buffer: PChar; GetMode: TGetMode; DoCheck: Boolean): TGetResult; virtual; abstract;
  810. function GetRecordSize: Word; virtual; abstract;
  811. procedure InternalAddRecord(Buffer: Pointer; Append: Boolean); virtual; abstract;
  812. procedure InternalClose; virtual; abstract;
  813. procedure InternalDelete; virtual; abstract;
  814. procedure InternalFirst; virtual; abstract;
  815. procedure InternalGotoBookmark(ABookmark: Pointer); virtual; abstract;
  816. procedure InternalHandleException; virtual; abstract;
  817. procedure InternalInitFieldDefs; virtual; abstract;
  818. procedure InternalInitRecord(Buffer: PChar); virtual; abstract;
  819. procedure InternalLast; virtual; abstract;
  820. procedure InternalOpen; virtual; abstract;
  821. procedure InternalPost; virtual; abstract;
  822. procedure InternalSetToRecord(Buffer: PChar); virtual; abstract;
  823. function IsCursorOpen: Boolean; virtual; abstract;
  824. procedure SetBookmarkFlag(Buffer: PChar; Value: TBookmarkFlag); virtual; abstract;
  825. procedure SetBookmarkData(Buffer: PChar; Data: Pointer); virtual; abstract;
  826. procedure SetFieldData(Field: TField; Buffer: Pointer); virtual; abstract;
  827. public
  828. constructor Create(AOwner: TComponent); override;
  829. destructor Destroy; override;
  830. function ActiveBuffer: PChar;
  831. procedure Append;
  832. procedure AppendRecord(const Values: array of const);
  833. function BookmarkValid(ABookmark: TBookmark): Boolean; virtual;
  834. procedure Cancel; virtual;
  835. procedure CheckBrowseMode;
  836. procedure ClearFields;
  837. procedure Close;
  838. function ControlsDisabled: Boolean;
  839. function CompareBookmarks(Bookmark1, Bookmark2: TBookmark): Longint; virtual;
  840. function CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream; virtual;
  841. procedure CursorPosChanged;
  842. procedure Delete;
  843. procedure DisableControls;
  844. procedure Edit;
  845. procedure EnableControls;
  846. function FieldByName(const FieldName: string): TField;
  847. function FindField(const FieldName: string): TField;
  848. function FindFirst: Boolean;
  849. function FindLast: Boolean;
  850. function FindNext: Boolean;
  851. function FindPrior: Boolean;
  852. procedure First;
  853. procedure FreeBookmark(ABookmark: TBookmark); virtual;
  854. function GetBookmark: TBookmark; virtual;
  855. function GetCurrentRecord(Buffer: PChar): Boolean; virtual;
  856. procedure GetFieldList(List: TList; const FieldNames: string);
  857. procedure GetFieldNames(List: TStrings);
  858. procedure GotoBookmark(ABookmark: TBookmark);
  859. procedure Insert;
  860. procedure InsertRecord(const Values: array of const);
  861. function IsEmpty: Boolean;
  862. function IsSequenced: Boolean; virtual;
  863. procedure Last;
  864. function MoveBy(Distance: Longint): Longint;
  865. procedure Next;
  866. procedure Open;
  867. procedure Post; virtual;
  868. procedure Prior;
  869. procedure Refresh;
  870. procedure Resync(Mode: TResyncMode); virtual;
  871. procedure SetFields(const Values: array of const);
  872. function Translate(Src, Dest: PChar; ToOem: Boolean): Integer; virtual;
  873. procedure UpdateCursorPos;
  874. procedure UpdateRecord;
  875. property BOF: Boolean read FBOF;
  876. property Bookmark: TBookmarkStr read GetBookmarkStr write SetBookmarkStr;
  877. property CanModify: Boolean read GetCanModify;
  878. property DataSource: TDataSource read GetDataSource;
  879. property DefaultFields: Boolean read FDefaultFields;
  880. property EOF: Boolean read FEOF;
  881. property FieldCount: Longint read GetFieldCount;
  882. property FieldDefs: TFieldDefs read FFieldDefs write FFieldDefs;
  883. property Fields[Index: Longint]: TField read GetField write SetField;
  884. property Found: Boolean read FFound;
  885. property Modified: Boolean read FModified;
  886. property RecordCount: Longint read GetRecordCount;
  887. property RecNo: Longint read FRecNo write FRecNo;
  888. property RecordSize: Word read FRecordSize;
  889. property State: TDataSetState read FState;
  890. // property Fields : TFields Read FFieldList;
  891. property Filter: string read FFilterText write FFilterText;
  892. property Filtered: Boolean read FFiltered write FFiltered default False;
  893. property FilterOptions: TFilterOptions read FFilterOptions write FFilterOptions;
  894. property Active: Boolean read FActive write SetActive default False;
  895. property AutoCalcFields: Boolean read FAutoCalcFields write FAutoCalcFields;
  896. property BeforeOpen: TDataSetNotifyEvent read FBeforeOpen write FBeforeOpen;
  897. property AfterOpen: TDataSetNotifyEvent read FAfterOpen write FAfterOpen;
  898. property BeforeClose: TDataSetNotifyEvent read FBeforeClose write FBeforeClose;
  899. property AfterClose: TDataSetNotifyEvent read FAfterClose write FAfterClose;
  900. property BeforeInsert: TDataSetNotifyEvent read FBeforeInsert write FBeforeInsert;
  901. property AfterInsert: TDataSetNotifyEvent read FAfterInsert write FAfterInsert;
  902. property BeforeEdit: TDataSetNotifyEvent read FBeforeEdit write FBeforeEdit;
  903. property AfterEdit: TDataSetNotifyEvent read FAfterEdit write FAfterEdit;
  904. property BeforePost: TDataSetNotifyEvent read FBeforePost write FBeforePost;
  905. property AfterPost: TDataSetNotifyEvent read FAfterPost write FAfterPost;
  906. property BeforeCancel: TDataSetNotifyEvent read FBeforeCancel write FBeforeCancel;
  907. property AfterCancel: TDataSetNotifyEvent read FAfterCancel write FAfterCancel;
  908. property BeforeDelete: TDataSetNotifyEvent read FBeforeDelete write FBeforeDelete;
  909. property AfterDelete: TDataSetNotifyEvent read FAfterDelete write FAfterDelete;
  910. property BeforeScroll: TDataSetNotifyEvent read FBeforeScroll write FBeforeScroll;
  911. property AfterScroll: TDataSetNotifyEvent read FAfterScroll write FAfterScroll;
  912. property OnCalcFields: TDataSetNotifyEvent read FOnCalcFields write FOnCalcFields;
  913. property OnDeleteError: TDataSetErrorEvent read FOnDeleteError write FOnDeleteError;
  914. property OnEditError: TDataSetErrorEvent read FOnEditError write FOnEditError;
  915. property OnFilterRecord: TFilterRecordEvent read FOnFilterRecord write SetOnFilterRecord;
  916. property OnNewRecord: TDataSetNotifyEvent read FOnNewRecord write FOnNewRecord;
  917. property OnPostError: TDataSetErrorEvent read FOnPostError write FOnPostError;
  918. end;
  919. TDataLink = class(TPersistent)
  920. private
  921. FFIrstRecord,
  922. FBufferCount : Integer;
  923. FActive,
  924. FDataSourceFixed,
  925. FEditing,
  926. FReadOnly,
  927. FUpdatingRecord,
  928. FVisualControl : Boolean;
  929. FDataSource : TDataSource;
  930. Function CalcFirstRecord(Index : Integer) : Integer;
  931. Procedure CalcRange;
  932. Procedure CheckActiveAndEditing;
  933. Function GetDataset : TDataset;
  934. procedure SetActive(AActive: Boolean);
  935. procedure SetDataSource(Value: TDataSource);
  936. Procedure SetReadOnly(Value : Boolean);
  937. protected
  938. procedure ActiveChanged; virtual;
  939. procedure CheckBrowseMode; virtual;
  940. procedure DataEvent(Event: TDataEvent; Info: Longint); virtual;
  941. procedure DataSetChanged; virtual;
  942. procedure DataSetScrolled(Distance: Integer); virtual;
  943. procedure EditingChanged; virtual;
  944. procedure FocusControl(Field: TFieldRef); virtual;
  945. function GetActiveRecord: Integer; virtual;
  946. function GetBOF: Boolean; virtual;
  947. function GetBufferCount: Integer; virtual;
  948. function GetEOF: Boolean; virtual;
  949. function GetRecordCount: Integer; virtual;
  950. procedure LayoutChanged; virtual;
  951. function MoveBy(Distance: Integer): Integer; virtual;
  952. procedure RecordChanged(Field: TField); virtual;
  953. procedure SetActiveRecord(Value: Integer); virtual;
  954. procedure SetBufferCount(Value: Integer); virtual;
  955. procedure UpdateData; virtual;
  956. property VisualControl: Boolean read FVisualControl write FVisualControl;
  957. public
  958. constructor Create;
  959. destructor Destroy; override;
  960. function Edit: Boolean;
  961. procedure UpdateRecord;
  962. property Active: Boolean read FActive;
  963. property ActiveRecord: Integer read GetActiveRecord write SetActiveRecord;
  964. property BOF: Boolean read GetBOF;
  965. property BufferCount: Integer read FBufferCount write SetBufferCount;
  966. property DataSet: TDataSet read GetDataSet;
  967. property DataSource: TDataSource read FDataSource write SetDataSource;
  968. property DataSourceFixed: Boolean read FDataSourceFixed write FDataSourceFixed;
  969. property Editing: Boolean read FEditing;
  970. property Eof: Boolean read GetEOF;
  971. property ReadOnly: Boolean read FReadOnly write SetReadOnly;
  972. property RecordCount: Integer read GetRecordCount;
  973. end;
  974. { TDetailDataLink }
  975. TDetailDataLink = class(TDataLink)
  976. protected
  977. function GetDetailDataSet: TDataSet; virtual;
  978. public
  979. property DetailDataSet: TDataSet read GetDetailDataSet;
  980. end;
  981. { TMasterDataLink }
  982. TMasterDataLink = class(TDetailDataLink)
  983. private
  984. FDataSet: TDataSet;
  985. FFieldNames: string;
  986. FFields: TList;
  987. FOnMasterChange: TNotifyEvent;
  988. FOnMasterDisable: TNotifyEvent;
  989. procedure SetFieldNames(const Value: string);
  990. protected
  991. procedure ActiveChanged; override;
  992. procedure CheckBrowseMode; override;
  993. function GetDetailDataSet: TDataSet; override;
  994. procedure LayoutChanged; override;
  995. procedure RecordChanged(Field: TField); override;
  996. public
  997. constructor Create(ADataSet: TDataSet);
  998. destructor Destroy; override;
  999. property FieldNames: string read FFieldNames write SetFieldNames;
  1000. property Fields: TList read FFields;
  1001. property OnMasterChange: TNotifyEvent read FOnMasterChange write FOnMasterChange;
  1002. property OnMasterDisable: TNotifyEvent read FOnMasterDisable write FOnMasterDisable;
  1003. end;
  1004. { TDataSource }
  1005. TDataChangeEvent = procedure(Sender: TObject; Field: TField) of object;
  1006. TDataSource = class(TComponent)
  1007. private
  1008. FDataSet: TDataSet;
  1009. FDataLinks: TList;
  1010. FEnabled: Boolean;
  1011. FAutoEdit: Boolean;
  1012. FState: TDataSetState;
  1013. FOnStateChange: TNotifyEvent;
  1014. FOnDataChange: TDataChangeEvent;
  1015. FOnUpdateData: TNotifyEvent;
  1016. procedure DistributeEvent(Event: TDataEvent; Info: Longint);
  1017. procedure RegisterDataLink(DataLink: TDataLink);
  1018. Procedure ProcessEvent(Event : TDataEvent; Info : longint);
  1019. procedure SetDataSet(ADataSet: TDataSet);
  1020. procedure SetEnabled(Value: Boolean);
  1021. procedure UnregisterDataLink(DataLink: TDataLink);
  1022. protected
  1023. Procedure DoDataChange (Info : Pointer);virtual;
  1024. Procedure DoStateChange; virtual;
  1025. Procedure DoUpdateData;
  1026. property DataLinks: TList read FDataLinks;
  1027. public
  1028. constructor Create(AOwner: TComponent); override;
  1029. destructor Destroy; override;
  1030. procedure Edit;
  1031. function IsLinkedTo(ADataSet: TDataSet): Boolean;
  1032. property State: TDataSetState read FState;
  1033. published
  1034. property AutoEdit: Boolean read FAutoEdit write FAutoEdit default True;
  1035. property DataSet: TDataSet read FDataSet write SetDataSet;
  1036. property Enabled: Boolean read FEnabled write SetEnabled default True;
  1037. property OnStateChange: TNotifyEvent read FOnStateChange write FOnStateChange;
  1038. property OnDataChange: TDataChangeEvent read FOnDataChange write FOnDataChange;
  1039. property OnUpdateData: TNotifyEvent read FOnUpdateData write FOnUpdateData;
  1040. end;
  1041. { TDBDataset }
  1042. TDBDatasetClass = Class of TDBDataset;
  1043. TDBDataset = Class(TDataset)
  1044. Private
  1045. FDatabase : TDatabase;
  1046. Procedure SetDatabase (Value : TDatabase);
  1047. Protected
  1048. Procedure CheckDatabase;
  1049. Public
  1050. Destructor destroy; override;
  1051. Property DataBase : TDatabase Read FDatabase Write SetDatabase;
  1052. end;
  1053. { TDatabase }
  1054. TLoginEvent = procedure(Database: TDatabase;
  1055. LoginParams: TStrings) of object;
  1056. TDatabaseClass = Class Of TDatabase;
  1057. TDatabase = class(TComponent)
  1058. private
  1059. FConnected : Boolean;
  1060. FDataBaseName : String;
  1061. FDataSets : TList;
  1062. FDirectory : String;
  1063. FKeepConnection : Boolean;
  1064. FLoginPrompt : Boolean;
  1065. FOnLogin : TLoginEvent;
  1066. FParams : TStrings;
  1067. FSQLBased : Boolean;
  1068. Function GetDataSetCount : Longint;
  1069. Function GetDataset(Index : longint) : TDBDataset;
  1070. procedure SetConnected (Value : boolean);
  1071. procedure RegisterDataset (DS : TDBDataset);
  1072. procedure UnRegisterDataset (DS : TDBDataset);
  1073. procedure RemoveDataSets;
  1074. protected
  1075. Procedure CheckConnected;
  1076. Procedure CheckDisConnected;
  1077. procedure Loaded; override;
  1078. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  1079. Procedure DoInternalConnect; Virtual;Abstract;
  1080. Procedure DoInternalDisConnect; Virtual;Abstract;
  1081. public
  1082. constructor Create(AOwner: TComponent); override;
  1083. destructor Destroy; override;
  1084. procedure Close;
  1085. procedure Open;
  1086. procedure CloseDataSets;
  1087. procedure StartTransaction; virtual; abstract;
  1088. procedure EndTransaction; virtual; abstract;
  1089. property DataSetCount: Longint read GetDataSetCount;
  1090. property DataSets[Index: Longint]: TDBDataSet read GetDataSet;
  1091. property Directory: string read FDirectory write FDirectory;
  1092. property IsSQLBased: Boolean read FSQLBased;
  1093. published
  1094. property Connected: Boolean read FConnected write SetConnected;
  1095. property DatabaseName: string read FDatabaseName write FDatabaseName;
  1096. property KeepConnection: Boolean read FKeepConnection write FKeepConnection;
  1097. property LoginPrompt: Boolean read FLoginPrompt write FLoginPrompt;
  1098. property Params : TStrings read FParams Write FParams;
  1099. property OnLogin: TLoginEvent read FOnLogin write FOnLogin;
  1100. end;
  1101. Const
  1102. Fieldtypenames : Array [TFieldType] of String[15] =
  1103. (
  1104. 'Unknown',
  1105. 'String',
  1106. 'Smallint',
  1107. 'Integer',
  1108. 'Word',
  1109. 'Boolean',
  1110. 'Float',
  1111. 'Currency',
  1112. 'BCD',
  1113. 'Date',
  1114. 'Time',
  1115. 'DateTime',
  1116. 'Bytes',
  1117. 'VarBytes',
  1118. 'AutoInc',
  1119. 'Blob',
  1120. 'Memo',
  1121. 'Graphic',
  1122. 'FmtMemo',
  1123. 'ParadoxOle',
  1124. 'DBaseOle',
  1125. 'TypedBinary',
  1126. 'Cursor',
  1127. 'FixedChar',
  1128. 'WideString',
  1129. 'Largeint',
  1130. 'ADT',
  1131. 'Array',
  1132. 'Reference',
  1133. 'DataSet',
  1134. 'OraBlob',
  1135. 'OraClob',
  1136. 'Variant',
  1137. 'Interface',
  1138. 'IDispatch',
  1139. 'Guid',
  1140. 'TimeStamp',
  1141. 'FMTBcd'
  1142. );
  1143. { 'Unknown',
  1144. 'String',
  1145. 'Smallint',
  1146. 'Integer',
  1147. 'Word',
  1148. 'Boolean',
  1149. 'Float',
  1150. 'Date',
  1151. 'Time',
  1152. 'DateTime',
  1153. 'Bytes',
  1154. 'VarBytes',
  1155. 'AutoInc',
  1156. 'Blob',
  1157. 'Memo',
  1158. 'Graphic',
  1159. 'FmtMemo',
  1160. 'ParadoxOle',
  1161. 'DBaseOle',
  1162. 'TypedBinary',
  1163. 'Cursor'
  1164. );}
  1165. dsEditModes = [dsEdit, dsInsert];
  1166. { Auxiliary functions }
  1167. Procedure DatabaseError (Const Msg : String);
  1168. Procedure DatabaseError (Const Msg : String; Comp : TComponent);
  1169. Procedure DatabaseErrorFmt (Const Fmt : String; Args : Array Of Const);
  1170. Procedure DatabaseErrorFmt (Const Fmt : String; Args : Array Of const;
  1171. Comp : TComponent);
  1172. implementation
  1173. { ---------------------------------------------------------------------
  1174. Auxiliary functions
  1175. ---------------------------------------------------------------------}
  1176. Procedure DatabaseError (Const Msg : String);
  1177. begin
  1178. Raise EDataBaseError.Create(Msg);
  1179. end;
  1180. Procedure DatabaseError (Const Msg : String; Comp : TComponent);
  1181. begin
  1182. Raise EDatabaseError.CreateFmt('%s : %s',[Comp.Name,Msg]);
  1183. end;
  1184. Procedure DatabaseErrorFmt (Const Fmt : String; Args : Array Of Const);
  1185. begin
  1186. Raise EDatabaseError.CreateFmt(Fmt,Args);
  1187. end;
  1188. Procedure DatabaseErrorFmt (Const Fmt : String; Args : Array Of const;
  1189. Comp : TComponent);
  1190. begin
  1191. Raise EDatabaseError.CreateFmt(Format('%s : %s',[Comp.Name,Fmt]),Args);
  1192. end;
  1193. {$i dbs.inc}
  1194. { TIndexDef }
  1195. constructor TIndexDef.Create(Owner: TIndexDefs; const AName, TheFields: string;
  1196. TheOptions: TIndexOptions);
  1197. begin
  1198. //!! To be implemented
  1199. end;
  1200. destructor TIndexDef.Destroy;
  1201. begin
  1202. //!! To be implemented
  1203. end;
  1204. { TIndexDefs }
  1205. Function TIndexDefs.GetItem (Index : longint) : TindexDef;
  1206. begin
  1207. //!! To be implemented
  1208. end;
  1209. constructor TIndexDefs.Create(DataSet: TDataSet);
  1210. begin
  1211. //!! To be implemented
  1212. end;
  1213. destructor TIndexDefs.Destroy;
  1214. begin
  1215. //!! To be implemented
  1216. end;
  1217. procedure TIndexDefs.Add(const Name, Fields: string; Options: TIndexOptions);
  1218. begin
  1219. //!! To be implemented
  1220. end;
  1221. procedure TIndexDefs.Assign(IndexDefs: TIndexDefs);
  1222. begin
  1223. //!! To be implemented
  1224. end;
  1225. procedure TIndexDefs.Clear;
  1226. begin
  1227. //!! To be implemented
  1228. end;
  1229. function TIndexDefs.FindIndexForFields(const Fields: string): TIndexDef;
  1230. begin
  1231. //!! To be implemented
  1232. end;
  1233. function TIndexDefs.GetIndexForFields(const Fields: string;
  1234. CaseInsensitive: Boolean): TIndexDef;
  1235. begin
  1236. //!! To be implemented
  1237. end;
  1238. function TIndexDefs.IndexOf(const Name: string): Longint;
  1239. begin
  1240. //!! To be implemented
  1241. end;
  1242. procedure TIndexDefs.Update;
  1243. begin
  1244. //!! To be implemented
  1245. end;
  1246. { TCheckConstraint }
  1247. procedure TCheckConstraint.Assign(Source: TPersistent);
  1248. begin
  1249. //!! To be implemented
  1250. end;
  1251. { TCheckConstraints }
  1252. Function TCheckConstraints.GetItem(Index : Longint) : TCheckConstraint;
  1253. begin
  1254. //!! To be implemented
  1255. end;
  1256. Procedure TCheckConstraints.SetItem(index : Longint; Value : TCheckConstraint);
  1257. begin
  1258. //!! To be implemented
  1259. end;
  1260. function TCheckConstraints.GetOwner: TPersistent;
  1261. begin
  1262. //!! To be implemented
  1263. end;
  1264. constructor TCheckConstraints.Create(Owner: TPersistent);
  1265. begin
  1266. //!! To be implemented
  1267. end;
  1268. function TCheckConstraints.Add: TCheckConstraint;
  1269. begin
  1270. //!! To be implemented
  1271. end;
  1272. {$i dataset.inc}
  1273. {$i fields.inc}
  1274. {$i datasource.inc}
  1275. {$i database.inc}
  1276. end.
  1277. {
  1278. $Log$
  1279. Revision 1.12 2003-11-09 21:23:10 michael
  1280. + Patch from Micha Nelissen, fixing some Delphi compatibility issues
  1281. Revision 1.11 2003/10/06 17:04:27 florian
  1282. * small step towards calculated fields
  1283. Revision 1.10 2003/08/16 16:42:21 michael
  1284. + Fixes in TDBDataset etc. Changed MySQLDb to use database as well
  1285. Revision 1.9 2003/05/15 15:15:15 michael
  1286. + Database class in TDBDataset is public, not published
  1287. Revision 1.8 2003/05/08 21:52:41 michael
  1288. + Patch from Jesus Reyes
  1289. Revision 1.7 2003/02/20 19:30:28 michael
  1290. + Fixes from Jesus Reyes
  1291. Revision 1.6 2002/09/07 15:15:23 peter
  1292. * old logs removed and tabs fixed
  1293. }