dbf.pas 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015
  1. unit dbf;
  2. { design info in dbf_reg.pas }
  3. interface
  4. {$I dbf_common.inc}
  5. uses
  6. Classes,
  7. Db,
  8. dbf_common,
  9. dbf_dbffile,
  10. dbf_parser,
  11. dbf_prsdef,
  12. dbf_cursor,
  13. dbf_fields,
  14. dbf_pgfile,
  15. dbf_idxfile;
  16. // If you got a compilation error here or asking for dsgnintf.pas, then just add
  17. // this file in your project:
  18. // dsgnintf.pas in 'C: \Program Files\Borland\Delphi5\Source\Toolsapi\dsgnintf.pas'
  19. type
  20. //====================================================================
  21. pBookmarkData = ^TBookmarkData;
  22. TBookmarkData = record
  23. PhysicalRecNo: Integer;
  24. end;
  25. pDbfRecord = ^TDbfRecordHeader;
  26. TDbfRecordHeader = record
  27. BookmarkData: TBookmarkData;
  28. BookmarkFlag: TBookmarkFlag;
  29. SequentialRecNo: Integer;
  30. DeletedFlag: Char;
  31. end;
  32. //====================================================================
  33. TDbf = class;
  34. //====================================================================
  35. TDbfStorage = (stoMemory,stoFile);
  36. TDbfOpenMode = (omNormal,omAutoCreate,omTemporary);
  37. TDbfLanguageAction = (laReadOnly, laForceOEM, laForceANSI, laDefault);
  38. TDbfTranslationMode = (tmNoneAvailable, tmNoneNeeded, tmSimple, tmAdvanced);
  39. TDbfFileName = (dfDbf, dfMemo, dfIndex);
  40. //====================================================================
  41. TDbfFileNames = set of TDbfFileName;
  42. //====================================================================
  43. TCompareRecordEvent = procedure(Dbf: TDbf; var Accept: Boolean) of object;
  44. TTranslateEvent = function(Dbf: TDbf; Src, Dest: PChar; ToOem: Boolean): Integer of object;
  45. TLanguageWarningEvent = procedure(Dbf: TDbf; var Action: TDbfLanguageAction) of object;
  46. TConvertFieldEvent = procedure(Dbf: TDbf; DstField, SrcField: TField) of object;
  47. TBeforeAutoCreateEvent = procedure(Dbf: TDbf; var DoCreate: Boolean) of object;
  48. //====================================================================
  49. // TDbfBlobStream keeps a reference count to number of references to
  50. // this instance. Only if FRefCount reaches zero, then the object will be
  51. // destructed. AddReference `clones' a reference.
  52. // This allows the VCL to use Free on the object to `free' that
  53. // particular reference.
  54. TDbfBlobStream = class(TMemoryStream)
  55. private
  56. FBlobField: TBlobField;
  57. FMode: TBlobStreamMode;
  58. FDirty: boolean; { has possibly modified data, needs to be written }
  59. FMemoRecNo: Integer;
  60. { -1 : invalid contents }
  61. { 0 : clear, no contents }
  62. { >0 : data from page x }
  63. FReadSize: Integer;
  64. FRefCount: Integer;
  65. function GetTransliterate: Boolean;
  66. procedure Translate(ToOem: Boolean);
  67. procedure SetMode(NewMode: TBlobStreamMode);
  68. public
  69. constructor Create(FieldVal: TField);
  70. destructor Destroy; override;
  71. function AddReference: TDbfBlobStream;
  72. procedure FreeInstance; override;
  73. procedure Cancel;
  74. procedure Commit;
  75. property Dirty: boolean read FDirty;
  76. property Transliterate: Boolean read GetTransliterate;
  77. property MemoRecNo: Integer read FMemoRecNo write FMemoRecNo;
  78. property ReadSize: Integer read FReadSize write FReadSize;
  79. property Mode: TBlobStreamMode write SetMode;
  80. property BlobField: TBlobField read FBlobField;
  81. end;
  82. //====================================================================
  83. TDbfIndexDefs = class(TCollection)
  84. public
  85. FOwner: TDbf;
  86. private
  87. function GetItem(N: Integer): TDbfIndexDef;
  88. procedure SetItem(N: Integer; Value: TDbfIndexDef);
  89. protected
  90. function GetOwner: TPersistent; override;
  91. public
  92. constructor Create(AOwner: TDbf);
  93. function Add: TDbfIndexDef;
  94. function GetIndexByName(const Name: string): TDbfIndexDef;
  95. function GetIndexByField(const Name: string): TDbfIndexDef;
  96. procedure Update; {$ifdef SUPPORT_REINTRODUCE} reintroduce; {$endif}
  97. property Items[N: Integer]: TDbfIndexDef read GetItem write SetItem; default;
  98. end;
  99. //====================================================================
  100. TDbfMasterLink = class(TDataLink)
  101. private
  102. FDetailDataSet: TDbf;
  103. FParser: TDbfParser;
  104. FFieldNames: string;
  105. FValidExpression: Boolean;
  106. FKeyTranslation: boolean;
  107. FOnMasterChange: TNotifyEvent;
  108. FOnMasterDisable: TNotifyEvent;
  109. function GetFieldsVal: PChar;
  110. procedure SetFieldNames(const Value: string);
  111. protected
  112. procedure ActiveChanged; override;
  113. procedure CheckBrowseMode; override;
  114. procedure LayoutChanged; override;
  115. procedure RecordChanged(Field: TField); override;
  116. public
  117. constructor Create(ADataSet: TDbf);
  118. destructor Destroy; override;
  119. property FieldNames: string read FFieldNames write SetFieldNames;
  120. property KeyTranslation: boolean read FKeyTranslation;
  121. property ValidExpression: Boolean read FValidExpression write FValidExpression;
  122. property FieldsVal: PChar read GetFieldsVal;
  123. property Parser: TDbfParser read FParser;
  124. property OnMasterChange: TNotifyEvent read FOnMasterChange write FOnMasterChange;
  125. property OnMasterDisable: TNotifyEvent read FOnMasterDisable write FOnMasterDisable;
  126. end;
  127. //====================================================================
  128. PDbfBlobList = ^TDbfBlobList;
  129. TDbfBlobList = array[0..MaxListSize-1] of TDbfBlobStream;
  130. //====================================================================
  131. TDbf = class(TDataSet)
  132. private
  133. FDbfFile: TDbfFile;
  134. FCursor: TVirtualCursor;
  135. FOpenMode: TDbfOpenMode;
  136. FStorage: TDbfStorage;
  137. FMasterLink: TDbfMasterLink;
  138. FParser: TDbfParser;
  139. FBlobStreams: PDbfBlobList;
  140. FUserStream: TStream; // user stream to open
  141. FTableName: string; // table path and file name
  142. FRelativePath: string;
  143. FAbsolutePath: string;
  144. FIndexName: string;
  145. FReadOnly: Boolean;
  146. FFilterBuffer: PChar;
  147. FTempBuffer: PChar;
  148. FEditingRecNo: Integer;
  149. {$ifdef SUPPORT_VARIANTS}
  150. FLocateRecNo: Integer;
  151. {$endif}
  152. FLanguageID: Byte;
  153. FTableLevel: Integer;
  154. FExclusive: Boolean;
  155. FShowDeleted: Boolean;
  156. FPosting: Boolean;
  157. FDisableResyncOnPost: Boolean;
  158. FTempExclusive: Boolean;
  159. FInCopyFrom: Boolean;
  160. FStoreDefs: Boolean;
  161. FCopyDateTimeAsString: Boolean;
  162. FFindRecordFilter: Boolean;
  163. FIndexFile: TIndexFile;
  164. FDateTimeHandling: TDateTimeHandling;
  165. FTranslationMode: TDbfTranslationMode;
  166. FIndexDefs: TDbfIndexDefs;
  167. FBeforeAutoCreate: TBeforeAutoCreateEvent;
  168. FOnTranslate: TTranslateEvent;
  169. FOnLanguageWarning: TLanguageWarningEvent;
  170. FOnLocaleError: TDbfLocaleErrorEvent;
  171. FOnIndexMissing: TDbfIndexMissingEvent;
  172. FOnCompareRecord: TNotifyEvent;
  173. FOnCopyDateTimeAsString: TConvertFieldEvent;
  174. function GetIndexName: string;
  175. function GetVersion: string;
  176. function GetPhysicalRecNo: Integer;
  177. function GetLanguageStr: string;
  178. function GetCodePage: Cardinal;
  179. function GetExactRecordCount: Integer;
  180. function GetPhysicalRecordCount: Integer;
  181. function GetKeySize: Integer;
  182. function GetMasterFields: string;
  183. function FieldDefsStored: Boolean;
  184. procedure SetIndexName(AIndexName: string);
  185. procedure SetDbfIndexDefs(const Value: TDbfIndexDefs);
  186. procedure SetFilePath(const Value: string);
  187. procedure SetTableName(const S: string);
  188. procedure SetVersion(const S: string);
  189. procedure SetLanguageID(NewID: Byte);
  190. procedure SetDataSource(Value: TDataSource);
  191. procedure SetMasterFields(const Value: string);
  192. procedure SetTableLevel(const NewLevel: Integer);
  193. procedure SetPhysicalRecNo(const NewRecNo: Integer);
  194. procedure MasterChanged(Sender: TObject);
  195. procedure MasterDisabled(Sender: TObject);
  196. procedure DetermineTranslationMode;
  197. procedure UpdateRange;
  198. procedure SetShowDeleted(Value: Boolean);
  199. procedure GetFieldDefsFromDbfFieldDefs;
  200. procedure InitDbfFile(FileOpenMode: TPagedFileMode);
  201. function ParseIndexName(const AIndexName: string): string;
  202. procedure ParseFilter(const AFilter: string);
  203. function GetDbfFieldDefs: TDbfFieldDefs;
  204. function SearchKeyBuffer(Buffer: PChar; SearchType: TSearchKeyType): Boolean;
  205. procedure SetRangeBuffer(LowRange: PChar; HighRange: PChar);
  206. protected
  207. { abstract methods }
  208. function AllocRecordBuffer: PChar; override; {virtual abstract}
  209. procedure ClearCalcFields(Buffer: PChar); override;
  210. procedure FreeRecordBuffer(var Buffer: PChar); override; {virtual abstract}
  211. procedure GetBookmarkData(Buffer: PChar; Data: Pointer); override; {virtual abstract}
  212. function GetBookmarkFlag(Buffer: PChar): TBookmarkFlag; override; {virtual abstract}
  213. function GetRecord(Buffer: PChar; GetMode: TGetMode; DoCheck: Boolean): TGetResult; override; {virtual abstract}
  214. function GetRecordSize: Word; override; {virtual abstract}
  215. procedure InternalAddRecord(Buffer: Pointer; AAppend: Boolean); override; {virtual abstract}
  216. procedure InternalClose; override; {virtual abstract}
  217. procedure InternalDelete; override; {virtual abstract}
  218. procedure InternalFirst; override; {virtual abstract}
  219. procedure InternalGotoBookmark(ABookmark: Pointer); override; {virtual abstract}
  220. procedure InternalHandleException; override; {virtual abstract}
  221. procedure InternalInitFieldDefs; override; {virtual abstract}
  222. procedure InternalInitRecord(Buffer: PChar); override; {virtual abstract}
  223. procedure InternalLast; override; {virtual abstract}
  224. procedure InternalOpen; override; {virtual abstract}
  225. procedure InternalEdit; override; {virtual}
  226. procedure InternalCancel; override; {virtual}
  227. {$ifndef FPC}
  228. {$ifndef DELPHI_3}
  229. procedure InternalInsert; override; {virtual}
  230. {$endif}
  231. {$endif}
  232. procedure InternalPost; override; {virtual abstract}
  233. procedure InternalSetToRecord(Buffer: PChar); override; {virtual abstract}
  234. procedure InitFieldDefs; override;
  235. function IsCursorOpen: Boolean; override; {virtual abstract}
  236. procedure SetBookmarkFlag(Buffer: PChar; Value: TBookmarkFlag); override; {virtual abstract}
  237. procedure SetBookmarkData(Buffer: PChar; Data: Pointer); override; {virtual abstract}
  238. procedure SetFieldData(Field: TField; Buffer: Pointer);
  239. {$ifdef SUPPORT_OVERLOAD} overload; {$endif} override; {virtual abstract}
  240. { virtual methods (mostly optionnal) }
  241. function GetDataSource: TDataSource; {$ifndef VER1_0}override;{$endif}
  242. function GetRecordCount: Integer; override; {virtual}
  243. function GetRecNo: Integer; override; {virtual}
  244. function GetCanModify: Boolean; override; {virtual}
  245. procedure SetRecNo(Value: Integer); override; {virual}
  246. procedure SetFiltered(Value: Boolean); override; {virtual;}
  247. procedure SetFilterText(const Value: String); override; {virtual;}
  248. {$ifdef SUPPORT_DEFCHANGED}
  249. procedure DefChanged(Sender: TObject); override;
  250. {$endif}
  251. function FindRecord(Restart, GoForward: Boolean): Boolean; override;
  252. function GetIndexFieldNames: string; {virtual;}
  253. procedure SetIndexFieldNames(const Value: string); {virtual;}
  254. {$ifdef SUPPORT_VARIANTS}
  255. function LocateRecordLinear(const KeyFields: String; const KeyValues: Variant; Options: TLocateOptions): Boolean;
  256. function LocateRecordIndex(const KeyFields: String; const KeyValues: Variant; Options: TLocateOptions): Boolean;
  257. function LocateRecord(const KeyFields: String; const KeyValues: Variant; Options: TLocateOptions): Boolean;
  258. {$endif}
  259. procedure DoFilterRecord(var Acceptable: Boolean);
  260. public
  261. constructor Create(AOwner: TComponent); override;
  262. destructor Destroy; override;
  263. { abstract methods }
  264. function GetFieldData(Field: TField; Buffer: Pointer): Boolean;
  265. {$ifdef SUPPORT_OVERLOAD} overload; {$endif} override; {virtual abstract}
  266. { virtual methods (mostly optionnal) }
  267. procedure Resync(Mode: TResyncMode); override;
  268. function CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream; override; {virtual}
  269. {$ifdef SUPPORT_NEW_TRANSLATE}
  270. function Translate(Src, Dest: PChar; ToOem: Boolean): Integer; override; {virtual}
  271. {$else}
  272. procedure Translate(Src, Dest: PChar; ToOem: Boolean); override; {virtual}
  273. {$endif}
  274. {$ifdef SUPPORT_OVERLOAD}
  275. function GetFieldData(Field: TField; Buffer: Pointer; NativeFormat: Boolean): Boolean; overload;
  276. {$ifdef SUPPORT_BACKWARD_FIELDDATA} override; {$endif}
  277. procedure SetFieldData(Field: TField; Buffer: Pointer; NativeFormat: Boolean); overload;
  278. {$ifdef SUPPORT_BACKWARD_FIELDDATA} override; {$endif}
  279. {$endif}
  280. function CompareBookmarks(Bookmark1, Bookmark2: TBookmark): Integer; override;
  281. procedure CheckDbfFieldDefs(ADbfFieldDefs: TDbfFieldDefs);
  282. {$ifdef VER1_0}
  283. procedure DataEvent(Event: TDataEvent; Info: Longint); override;
  284. {$endif}
  285. // my own methods and properties
  286. // most look like ttable functions but they are not tdataset related
  287. // I (try to) use the same syntax to facilitate the conversion between bde and TDbf
  288. // index support (use same syntax as ttable but is not related)
  289. {$ifdef SUPPORT_DEFAULT_PARAMS}
  290. procedure AddIndex(const AIndexName, AFields: String; Options: TIndexOptions; const DescFields: String='');
  291. {$else}
  292. procedure AddIndex(const AIndexName, AFields: String; Options: TIndexOptions);
  293. {$endif}
  294. procedure RegenerateIndexes;
  295. procedure CancelRange;
  296. procedure CheckMasterRange;
  297. {$ifdef SUPPORT_VARIANTS}
  298. function SearchKey(Key: Variant; SearchType: TSearchKeyType; KeyIsANSI: boolean
  299. {$ifdef SUPPORT_DEFAULT_PARAMS}= false{$endif}): Boolean;
  300. procedure SetRange(LowRange: Variant; HighRange: Variant; KeyIsANSI: boolean
  301. {$ifdef SUPPORT_DEFAULT_PARAMS}= false{$endif});
  302. {$endif}
  303. function PrepareKey(Buffer: Pointer; BufferType: TExpressionType): PChar;
  304. function SearchKeyPChar(Key: PChar; SearchType: TSearchKeyType; KeyIsANSI: boolean
  305. {$ifdef SUPPORT_DEFAULT_PARAMS}= false{$endif}): Boolean;
  306. procedure SetRangePChar(LowRange: PChar; HighRange: PChar; KeyIsANSI: boolean
  307. {$ifdef SUPPORT_DEFAULT_PARAMS}= false{$endif});
  308. function GetCurrentBuffer: PChar;
  309. procedure ExtractKey(KeyBuffer: PChar);
  310. procedure UpdateIndexDefs; override;
  311. procedure GetFileNames(Strings: TStrings; Files: TDbfFileNames); {$ifdef SUPPORT_DEFAULT_PARAMS} overload; {$endif}
  312. {$ifdef SUPPORT_DEFAULT_PARAMS}
  313. function GetFileNames(Files: TDbfFileNames = [dfDbf] ): string; overload;
  314. {$else}
  315. function GetFileNamesString(Files: TDbfFileNames (* = [dfDbf] *) ): string;
  316. {$endif}
  317. procedure GetIndexNames(Strings: TStrings);
  318. procedure GetAllIndexFiles(Strings: TStrings);
  319. procedure TryExclusive;
  320. procedure EndExclusive;
  321. function LockTable(const Wait: Boolean): Boolean;
  322. procedure UnlockTable;
  323. procedure OpenIndexFile(IndexFile: string);
  324. procedure DeleteIndex(const AIndexName: string);
  325. procedure CloseIndexFile(const AIndexName: string);
  326. procedure RepageIndexFile(const AIndexFile: string);
  327. procedure CompactIndexFile(const AIndexFile: string);
  328. {$ifdef SUPPORT_VARIANTS}
  329. function Lookup(const KeyFields: string; const KeyValues: Variant; const ResultFields: string): Variant; override;
  330. function Locate(const KeyFields: string; const KeyValues: Variant; Options: TLocateOptions): Boolean; override;
  331. {$endif}
  332. function IsDeleted: Boolean;
  333. procedure Undelete;
  334. procedure CreateTable;
  335. procedure CreateTableEx(ADbfFieldDefs: TDbfFieldDefs);
  336. procedure CopyFrom(DataSet: TDataSet; FileName: string; DateTimeAsString: Boolean; Level: Integer);
  337. procedure RestructureTable(ADbfFieldDefs: TDbfFieldDefs; Pack: Boolean);
  338. procedure PackTable;
  339. procedure EmptyTable;
  340. procedure Zap;
  341. {$ifndef SUPPORT_INITDEFSFROMFIELDS}
  342. procedure InitFieldDefsFromFields;
  343. {$endif}
  344. property AbsolutePath: string read FAbsolutePath;
  345. property DbfFieldDefs: TDbfFieldDefs read GetDbfFieldDefs;
  346. property PhysicalRecNo: Integer read GetPhysicalRecNo write SetPhysicalRecNo;
  347. property LanguageID: Byte read FLanguageID write SetLanguageID;
  348. property LanguageStr: String read GetLanguageStr;
  349. property CodePage: Cardinal read GetCodePage;
  350. property ExactRecordCount: Integer read GetExactRecordCount;
  351. property PhysicalRecordCount: Integer read GetPhysicalRecordCount;
  352. property KeySize: Integer read GetKeySize;
  353. property DbfFile: TDbfFile read FDbfFile;
  354. property UserStream: TStream read FUserStream write FUserStream;
  355. property DisableResyncOnPost: Boolean read FDisableResyncOnPost write FDisableResyncOnPost;
  356. published
  357. property DateTimeHandling: TDateTimeHandling
  358. read FDateTimeHandling write FDateTimeHandling default dtBDETimeStamp;
  359. property Exclusive: Boolean read FExclusive write FExclusive default false;
  360. property FilePath: string read FRelativePath write SetFilePath;
  361. property FilePathFull: string read FAbsolutePath write SetFilePath stored false;
  362. property Indexes: TDbfIndexDefs read FIndexDefs write SetDbfIndexDefs stored false;
  363. property IndexDefs: TDbfIndexDefs read FIndexDefs write SetDbfIndexDefs;
  364. property IndexFieldNames: string read GetIndexFieldNames write SetIndexFieldNames stored false;
  365. property IndexName: string read GetIndexName write SetIndexName;
  366. property MasterFields: string read GetMasterFields write SetMasterFields;
  367. property MasterSource: TDataSource read GetDataSource write SetDataSource;
  368. property OpenMode: TDbfOpenMode read FOpenMode write FOpenMode default omNormal;
  369. property ReadOnly: Boolean read FReadOnly write FReadonly default false;
  370. property ShowDeleted: Boolean read FShowDeleted write SetShowDeleted default false;
  371. property Storage: TDbfStorage read FStorage write FStorage default stoFile;
  372. property StoreDefs: Boolean read FStoreDefs write FStoreDefs default False;
  373. property TableName: string read FTableName write SetTableName;
  374. property TableLevel: Integer read FTableLevel write SetTableLevel;
  375. property Version: string read GetVersion write SetVersion stored false;
  376. property BeforeAutoCreate: TBeforeAutoCreateEvent read FBeforeAutoCreate write FBeforeAutoCreate;
  377. property OnCompareRecord: TNotifyEvent read FOnCompareRecord write FOnCompareRecord;
  378. property OnLanguageWarning: TLanguageWarningEvent read FOnLanguageWarning write FOnLanguageWarning;
  379. property OnLocaleError: TDbfLocaleErrorEvent read FOnLocaleError write FOnLocaleError;
  380. property OnIndexMissing: TDbfIndexMissingEvent read FOnIndexMissing write FOnIndexMissing;
  381. property OnCopyDateTimeAsString: TConvertFieldEvent read FOnCopyDateTimeAsString write FOnCopyDateTimeAsString;
  382. property OnTranslate: TTranslateEvent read FOnTranslate write FOnTranslate;
  383. // redeclared data set properties
  384. property Active;
  385. property FieldDefs stored FieldDefsStored;
  386. property Filter;
  387. property Filtered;
  388. property FilterOptions;
  389. property BeforeOpen;
  390. property AfterOpen;
  391. property BeforeClose;
  392. property AfterClose;
  393. property BeforeInsert;
  394. property AfterInsert;
  395. property BeforeEdit;
  396. property AfterEdit;
  397. property BeforePost;
  398. property AfterPost;
  399. property BeforeCancel;
  400. property AfterCancel;
  401. property BeforeDelete;
  402. property AfterDelete;
  403. property BeforeScroll;
  404. property AfterScroll;
  405. property OnCalcFields;
  406. property OnDeleteError;
  407. property OnEditError;
  408. property OnFilterRecord;
  409. property OnNewRecord;
  410. property OnPostError;
  411. end;
  412. TDbf_GetBasePathFunction = function: string;
  413. var
  414. DbfBasePath: TDbf_GetBasePathFunction;
  415. implementation
  416. uses
  417. SysUtils,
  418. {$ifndef FPC}
  419. DBConsts,
  420. {$endif}
  421. {$ifdef WINDOWS}
  422. Windows,
  423. {$else}
  424. {$ifdef KYLIX}
  425. Libc,
  426. {$endif}
  427. Types,
  428. dbf_wtil,
  429. {$endif}
  430. {$ifdef SUPPORT_SEPARATE_VARIANTS_UNIT}
  431. Variants,
  432. {$endif}
  433. dbf_idxcur,
  434. dbf_memo,
  435. dbf_str;
  436. {$ifdef FPC}
  437. const
  438. // TODO: move these to DBConsts
  439. SNotEditing = 'Dataset not in edit or insert mode';
  440. SCircularDataLink = 'Circular datalinks are not allowed';
  441. {$endif}
  442. function TableLevelToDbfVersion(TableLevel: integer): TXBaseVersion;
  443. begin
  444. case TableLevel of
  445. 3: Result := xBaseIII;
  446. 7: Result := xBaseVII;
  447. TDBF_TABLELEVEL_FOXPRO: Result := xFoxPro;
  448. else
  449. {4:} Result := xBaseIV;
  450. end;
  451. end;
  452. //==========================================================
  453. //============ TDbfBlobStream
  454. //==========================================================
  455. constructor TDbfBlobStream.Create(FieldVal: TField);
  456. begin
  457. FBlobField := FieldVal as TBlobField;
  458. FReadSize := 0;
  459. FMemoRecNo := 0;
  460. FRefCount := 1;
  461. FDirty := false;
  462. end;
  463. destructor TDbfBlobStream.Destroy;
  464. begin
  465. // only continue destroy if all references released
  466. if FRefCount = 1 then
  467. begin
  468. // this is the last reference
  469. inherited
  470. end else begin
  471. // fire event when dirty, and the last "user" is freeing it's reference
  472. // tdbf always has the last reference
  473. if FDirty and (FRefCount = 2) then
  474. begin
  475. // a second referer to instance has changed the data, remember modified
  476. // TDbf(FBlobField.DataSet).SetModified(true);
  477. // is following better? seems to provide notification for user (from VCL)
  478. if not (FBlobField.DataSet.State in [dsCalcFields, dsFilter, dsNewValue]) then
  479. TDbf(FBlobField.DataSet).DataEvent(deFieldChange, PtrInt(FBlobField));
  480. end;
  481. end;
  482. Dec(FRefCount);
  483. end;
  484. procedure TDbfBlobStream.FreeInstance;
  485. begin
  486. // only continue freeing if all references released
  487. if FRefCount = 0 then
  488. inherited;
  489. end;
  490. procedure TDbfBlobStream.SetMode(NewMode: TBlobStreamMode);
  491. begin
  492. FMode := NewMode;
  493. FDirty := FDirty or (NewMode = bmWrite) or (NewMode = bmReadWrite);
  494. end;
  495. procedure TDbfBlobStream.Cancel;
  496. begin
  497. FDirty := false;
  498. FMemoRecNo := -1;
  499. end;
  500. procedure TDbfBlobStream.Commit;
  501. var
  502. Dbf: TDbf;
  503. begin
  504. if FDirty then
  505. begin
  506. Size := Position; // Strange but it leave tailing trash bytes if I do not write that.
  507. Dbf := TDbf(FBlobField.DataSet);
  508. Translate(true);
  509. Dbf.FDbfFile.MemoFile.WriteMemo(FMemoRecNo, FReadSize, Self);
  510. Dbf.FDbfFile.SetFieldData(FBlobField.FieldNo-1, ftInteger, @FMemoRecNo,
  511. @pDbfRecord(TDbf(FBlobField.DataSet).ActiveBuffer)^.DeletedFlag, false);
  512. FDirty := false;
  513. end;
  514. end;
  515. function TDbfBlobStream.AddReference: TDbfBlobStream;
  516. begin
  517. Inc(FRefCount);
  518. Result := Self;
  519. end;
  520. function TDbfBlobStream.GetTransliterate: Boolean;
  521. begin
  522. Result := FBlobField.Transliterate;
  523. end;
  524. procedure TDbfBlobStream.Translate(ToOem: Boolean);
  525. var
  526. bytesToDo, numBytes: Integer;
  527. bufPos: PChar;
  528. saveChar: Char;
  529. begin
  530. if (Transliterate) and (Size > 0) then
  531. begin
  532. // get number of bytes to be translated
  533. bytesToDo := Size;
  534. // make space for final null-terminator
  535. Size := Size + 1;
  536. bufPos := Memory;
  537. repeat
  538. // process blocks of 512 bytes
  539. numBytes := bytesToDo;
  540. if numBytes > 512 then
  541. numBytes := 512;
  542. // null-terminate memory
  543. saveChar := bufPos[numBytes];
  544. bufPos[numBytes] := #0;
  545. // translate memory
  546. TDbf(FBlobField.DataSet).Translate(bufPos, bufPos, ToOem);
  547. // restore char
  548. bufPos[numBytes] := saveChar;
  549. // numBytes bytes translated
  550. Dec(bytesToDo, numBytes);
  551. Inc(bufPos, numBytes);
  552. until bytesToDo = 0;
  553. // cut ending null-terminator
  554. Size := Size - 1;
  555. end;
  556. end;
  557. //====================================================================
  558. // TDbf = TDataset Descendant.
  559. //====================================================================
  560. constructor TDbf.Create(AOwner: TComponent); {override;}
  561. begin
  562. inherited;
  563. if DbfGlobals = nil then
  564. DbfGlobals := TDbfGlobals.Create;
  565. BookmarkSize := sizeof(TBookmarkData);
  566. FIndexDefs := TDbfIndexDefs.Create(Self);
  567. FMasterLink := TDbfMasterLink.Create(Self);
  568. FMasterLink.OnMasterChange := MasterChanged;
  569. FMasterLink.OnMasterDisable := MasterDisabled;
  570. FDateTimeHandling := dtBDETimeStamp;
  571. FStorage := stoFile;
  572. FOpenMode := omNormal;
  573. FParser := nil;
  574. FPosting := false;
  575. FReadOnly := false;
  576. FExclusive := false;
  577. FDisableResyncOnPost := false;
  578. FTempExclusive := false;
  579. FCopyDateTimeAsString := false;
  580. FInCopyFrom := false;
  581. FFindRecordFilter := false;
  582. FEditingRecNo := -1;
  583. FTableLevel := 4;
  584. FIndexName := EmptyStr;
  585. FilePath := EmptyStr;
  586. FTempBuffer := nil;
  587. FFilterBuffer := nil;
  588. FIndexFile := nil;
  589. FOnTranslate := nil;
  590. FOnCopyDateTimeAsString := nil;
  591. end;
  592. destructor TDbf.Destroy; {override;}
  593. var
  594. I: Integer;
  595. begin
  596. inherited Destroy;
  597. if FIndexDefs <> nil then
  598. begin
  599. for I := FIndexDefs.Count - 1 downto 0 do
  600. TDbfIndexDef(FIndexDefs.Items[I]).Free;
  601. FIndexDefs.Free;
  602. end;
  603. FMasterLink.Free;
  604. end;
  605. function TDbf.AllocRecordBuffer: PChar; {override virtual abstract from TDataset}
  606. begin
  607. GetMem(Result, SizeOf(TDbfRecordHeader)+FDbfFile.RecordSize+CalcFieldsSize+1);
  608. end;
  609. procedure TDbf.FreeRecordBuffer(var Buffer: PChar); {override virtual abstract from TDataset}
  610. begin
  611. FreeMemAndNil(Pointer(Buffer));
  612. end;
  613. procedure TDbf.GetBookmarkData(Buffer: PChar; Data: Pointer); {override virtual abstract from TDataset}
  614. begin
  615. pBookmarkData(Data)^ := pDbfRecord(Buffer)^.BookmarkData;
  616. end;
  617. function TDbf.GetBookmarkFlag(Buffer: PChar): TBookmarkFlag; {override virtual abstract from TDataset}
  618. begin
  619. Result := pDbfRecord(Buffer)^.BookmarkFlag;
  620. end;
  621. function TDbf.GetCurrentBuffer: PChar;
  622. begin
  623. case State of
  624. dsFilter: Result := FFilterBuffer;
  625. dsCalcFields: Result := CalcBuffer;
  626. // dsSetKey: Result := FKeyBuffer; // TO BE Implemented
  627. else
  628. if IsEmpty then
  629. begin
  630. Result := nil;
  631. end else begin
  632. Result := ActiveBuffer;
  633. end;
  634. end;
  635. if Result <> nil then
  636. Result := @PDbfRecord(Result)^.DeletedFlag;
  637. end;
  638. // we don't want converted data formats, we want native :-)
  639. // it makes coding easier in TDbfFile.GetFieldData
  640. // ftCurrency:
  641. // Delphi 3,4: BCD array
  642. // ftBCD:
  643. // ftDateTime is more difficult though
  644. function TDbf.GetFieldData(Field: TField; Buffer: Pointer): Boolean; {override virtual abstract from TDataset}
  645. {$ifdef SUPPORT_OVERLOAD}
  646. begin
  647. { calling through 'old' delphi 3 interface, use compatible/'native' format }
  648. Result := GetFieldData(Field, Buffer, true);
  649. end;
  650. function TDbf.GetFieldData(Field: TField; Buffer: Pointer; NativeFormat: Boolean): Boolean; {overload; override;}
  651. {$else}
  652. const
  653. { no overload => delphi 3 => use compatible/'native' format }
  654. NativeFormat = true;
  655. {$endif}
  656. var
  657. Src: PChar;
  658. begin
  659. Src := GetCurrentBuffer;
  660. if Src = nil then
  661. begin
  662. Result := false;
  663. exit;
  664. end;
  665. if Field.FieldNo>0 then
  666. begin
  667. Result := FDbfFile.GetFieldData(Field.FieldNo-1, Field.DataType, Src, Buffer, NativeFormat);
  668. end else begin { weird calculated fields voodoo (from dbtables).... }
  669. Inc(PChar(Src), Field.Offset + GetRecordSize);
  670. Result := Boolean(Src[0]);
  671. if Result and (Buffer <> nil) then
  672. Move(Src[1], Buffer^, Field.DataSize);
  673. end;
  674. end;
  675. procedure TDbf.SetFieldData(Field: TField; Buffer: Pointer); {override virtual abstract from TDataset}
  676. {$ifdef SUPPORT_OVERLOAD}
  677. begin
  678. { calling through 'old' delphi 3 interface, use compatible/'native' format }
  679. SetFieldData(Field, Buffer, true);
  680. end;
  681. procedure TDbf.SetFieldData(Field: TField; Buffer: Pointer; NativeFormat: Boolean); {overload; override;}
  682. {$else}
  683. const
  684. { no overload => delphi 3 => use compatible/'native' format }
  685. NativeFormat = true;
  686. {$endif}
  687. var
  688. Dst: PChar;
  689. begin
  690. if (Field.FieldNo >= 0) then
  691. begin
  692. Dst := @PDbfRecord(ActiveBuffer)^.DeletedFlag;
  693. FDbfFile.SetFieldData(Field.FieldNo - 1, Field.DataType, Buffer, Dst, NativeFormat);
  694. end else begin { ***** fkCalculated, fkLookup ***** }
  695. Dst := @PDbfRecord(CalcBuffer)^.DeletedFlag;
  696. Inc(PChar(Dst), RecordSize + Field.Offset);
  697. Boolean(Dst[0]) := Buffer <> nil;
  698. if Buffer <> nil then
  699. Move(Buffer^, Dst[1], Field.DataSize)
  700. end; { end of ***** fkCalculated, fkLookup ***** }
  701. if not (State in [dsCalcFields, dsFilter, dsNewValue]) then begin
  702. DataEvent(deFieldChange, PtrInt(Field));
  703. end;
  704. end;
  705. procedure TDbf.DoFilterRecord(var Acceptable: Boolean);
  706. begin
  707. // check filtertext
  708. if Length(Filter) > 0 then
  709. begin
  710. {$ifndef VER1_0}
  711. Acceptable := Boolean((FParser.ExtractFromBuffer(GetCurrentBuffer))^);
  712. {$else}
  713. // strange problem
  714. // dbf.pas(716,19) Error: Incompatible types: got "CHAR" expected "BOOLEAN"
  715. Acceptable := not ((FParser.ExtractFromBuffer(GetCurrentBuffer))^ = #0);
  716. {$endif}
  717. end;
  718. // check user filter
  719. if Acceptable and Assigned(OnFilterRecord) then
  720. OnFilterRecord(Self, Acceptable);
  721. end;
  722. function TDbf.GetRecord(Buffer: PChar; GetMode: TGetMode; DoCheck: Boolean): TGetResult; {override virtual abstract from TDataset}
  723. var
  724. pRecord: pDBFRecord;
  725. acceptable: Boolean;
  726. SaveState: TDataSetState;
  727. lPhysicalRecNo: Integer;
  728. // s: string;
  729. begin
  730. if FCursor = nil then
  731. begin
  732. Result := grEOF;
  733. exit;
  734. end;
  735. pRecord := pDBFRecord(Buffer);
  736. acceptable := false;
  737. repeat
  738. Result := grOK;
  739. case GetMode of
  740. gmNext :
  741. begin
  742. Acceptable := FCursor.Next;
  743. if Acceptable then begin
  744. Result := grOK;
  745. end else begin
  746. Result := grEOF
  747. end;
  748. end;
  749. gmPrior :
  750. begin
  751. Acceptable := FCursor.Prev;
  752. if Acceptable then begin
  753. Result := grOK;
  754. end else begin
  755. Result := grBOF
  756. end;
  757. end;
  758. end;
  759. if (Result = grOK) then
  760. begin
  761. lPhysicalRecNo := FCursor.PhysicalRecNo;
  762. if (lPhysicalRecNo = 0) or not FDbfFile.IsRecordPresent(lPhysicalRecNo) then
  763. begin
  764. Result := grError;
  765. end else begin
  766. FDbfFile.ReadRecord(lPhysicalRecNo, @pRecord^.DeletedFlag);
  767. acceptable := (FShowDeleted or (pRecord^.DeletedFlag <> '*'))
  768. end;
  769. end;
  770. if (Result = grOK) and acceptable then
  771. begin
  772. pRecord^.BookmarkData.PhysicalRecNo := FCursor.PhysicalRecNo;
  773. pRecord^.BookmarkFlag := bfCurrent;
  774. pRecord^.SequentialRecNo := FCursor.SequentialRecNo;
  775. GetCalcFields(Buffer);
  776. if Filtered or FFindRecordFilter then
  777. begin
  778. FFilterBuffer := Buffer;
  779. SaveState := SetTempState(dsFilter);
  780. DoFilterRecord(acceptable);
  781. RestoreState(SaveState);
  782. end;
  783. end;
  784. if (GetMode = gmCurrent) and not acceptable then
  785. Result := grError;
  786. until (Result <> grOK) or acceptable;
  787. if Result <> grOK then
  788. pRecord^.BookmarkData.PhysicalRecNo := -1;
  789. end;
  790. function TDbf.GetRecordSize: Word; {override virtual abstract from TDataset}
  791. begin
  792. Result := FDbfFile.RecordSize;
  793. end;
  794. procedure TDbf.InternalAddRecord(Buffer: Pointer; AAppend: Boolean); {override virtual abstract from TDataset}
  795. // this function is called from TDataSet.InsertRecord and TDataSet.AppendRecord
  796. // goal: add record with Edit...Set Fields...Post all in one step
  797. var
  798. pRecord: pDbfRecord;
  799. newRecord: integer;
  800. begin
  801. // if InternalAddRecord is called, we know we are active
  802. pRecord := Buffer;
  803. // we can not insert records in DBF files, only append
  804. // ignore Append parameter
  805. newRecord := FDbfFile.Insert(@pRecord^.DeletedFlag);
  806. if newRecord > 0 then
  807. FCursor.PhysicalRecNo := newRecord;
  808. // set flag that TDataSet is about to post...so we can disable resync
  809. FPosting := true;
  810. end;
  811. procedure TDbf.InternalClose; {override virtual abstract from TDataset}
  812. var
  813. lIndex: TDbfIndexDef;
  814. I: Integer;
  815. begin
  816. // clear automatically added MDX index entries
  817. I := 0;
  818. while I < FIndexDefs.Count do
  819. begin
  820. // is this an MDX index?
  821. lIndex := FIndexDefs.Items[I];
  822. if (Length(ExtractFileExt(lIndex.IndexFile)) = 0) and
  823. TDbfIndexDef(FIndexDefs.Items[I]).Temporary then
  824. begin
  825. {$ifdef SUPPORT_DEF_DELETE}
  826. // delete this entry
  827. FIndexDefs.Delete(I);
  828. {$else}
  829. // does this work? I hope so :-)
  830. FIndexDefs.Items[I].Free;
  831. {$endif}
  832. end else begin
  833. // NDX entry -> goto next
  834. Inc(I);
  835. end;
  836. end;
  837. // free blobs
  838. if FBlobStreams <> nil then
  839. begin
  840. for I := 0 to Pred(FieldDefs.Count) do
  841. FBlobStreams^[I].Free;
  842. FreeMemAndNil(Pointer(FBlobStreams));
  843. end;
  844. FreeRecordBuffer(FTempBuffer);
  845. // disconnect field objects
  846. BindFields(false);
  847. // Destroy field object (if not persistent)
  848. if DefaultFields then
  849. DestroyFields;
  850. if FParser <> nil then
  851. FreeAndNil(FParser);
  852. FreeAndNil(FCursor);
  853. if FDbfFile <> nil then
  854. FreeAndNil(FDbfFile);
  855. end;
  856. procedure TDbf.InternalCancel;
  857. var
  858. I: Integer;
  859. begin
  860. // cancel blobs
  861. for I := 0 to Pred(FieldDefs.Count) do
  862. if Assigned(FBlobStreams^[I]) then
  863. FBlobStreams^[I].Cancel;
  864. // if we have locked a record, unlock it
  865. if FEditingRecNo >= 0 then
  866. begin
  867. FDbfFile.UnlockPage(FEditingRecNo);
  868. FEditingRecNo := -1;
  869. end;
  870. end;
  871. procedure TDbf.InternalDelete; {override virtual abstract from TDataset}
  872. var
  873. lRecord: pDbfRecord;
  874. begin
  875. // start editing
  876. InternalEdit;
  877. SetState(dsEdit);
  878. // get record pointer
  879. lRecord := pDbfRecord(ActiveBuffer);
  880. // flag we deleted this record
  881. lRecord^.DeletedFlag := '*';
  882. // notify indexes this record is deleted
  883. FDbfFile.RecordDeleted(FEditingRecNo, @lRecord^.DeletedFlag);
  884. // done!
  885. InternalPost;
  886. end;
  887. procedure TDbf.InternalFirst; {override virtual abstract from TDataset}
  888. begin
  889. FCursor.First;
  890. end;
  891. procedure TDbf.InternalGotoBookmark(ABookmark: Pointer); {override virtual abstract from TDataset}
  892. begin
  893. with PBookmarkData(ABookmark)^ do
  894. begin
  895. if (PhysicalRecNo = 0) then begin
  896. First;
  897. end else
  898. if (PhysicalRecNo = MaxInt) then begin
  899. Last;
  900. end else begin
  901. if FCursor.PhysicalRecNo <> PhysicalRecNo then
  902. FCursor.PhysicalRecNo := PhysicalRecNo;
  903. end;
  904. end;
  905. end;
  906. procedure TDbf.InternalHandleException; {override virtual abstract from TDataset}
  907. begin
  908. SysUtils.ShowException(ExceptObject, ExceptAddr);
  909. end;
  910. procedure TDbf.GetFieldDefsFromDbfFieldDefs;
  911. var
  912. I, N: Integer;
  913. TempFieldDef: TDbfFieldDef;
  914. TempMdxFile: TIndexFile;
  915. BaseName, lIndexName: string;
  916. begin
  917. FieldDefs.Clear;
  918. // get all fields
  919. for I := 0 to FDbfFile.FieldDefs.Count - 1 do
  920. begin
  921. TempFieldDef := FDbfFile.FieldDefs.Items[I];
  922. // handle duplicate field names
  923. N := 1;
  924. BaseName := TempFieldDef.FieldName;
  925. while FieldDefs.IndexOf(TempFieldDef.FieldName)>=0 do
  926. begin
  927. Inc(N);
  928. TempFieldDef.FieldName:=BaseName+IntToStr(N);
  929. end;
  930. // add field
  931. if TempFieldDef.FieldType in [ftString, ftBCD, ftBytes] then
  932. FieldDefs.Add(TempFieldDef.FieldName, TempFieldDef.FieldType, TempFieldDef.Size, false)
  933. else
  934. FieldDefs.Add(TempFieldDef.FieldName, TempFieldDef.FieldType, 0, false);
  935. if TempFieldDef.FieldType = ftFloat then
  936. FieldDefs[I].Precision := TempFieldDef.Precision;
  937. {$ifdef SUPPORT_FIELDDEF_ATTRIBUTES}
  938. // AutoInc fields are readonly
  939. if TempFieldDef.FieldType = ftAutoInc then
  940. FieldDefs[I].Attributes := [Db.faReadOnly];
  941. // if table has dbase lock field, then hide it
  942. if TempFieldDef.IsLockField then
  943. FieldDefs[I].Attributes := [Db.faHiddenCol];
  944. {$endif}
  945. end;
  946. // get all (new) MDX index defs
  947. TempMdxFile := FDbfFile.MdxFile;
  948. for I := 0 to FDbfFile.IndexNames.Count - 1 do
  949. begin
  950. // is this an MDX index?
  951. lIndexName := FDbfFile.IndexNames.Strings[I];
  952. if FDbfFile.IndexNames.Objects[I] = TempMdxFile then
  953. if FIndexDefs.GetIndexByName(lIndexName) = nil then
  954. TempMdxFile.GetIndexInfo(lIndexName, FIndexDefs.Add);
  955. end;
  956. end;
  957. procedure TDbf.InitFieldDefs;
  958. begin
  959. InternalInitFieldDefs;
  960. end;
  961. procedure TDbf.InitDbfFile(FileOpenMode: TPagedFileMode);
  962. const
  963. FileModeToMemMode: array[TPagedFileMode] of TPagedFileMode =
  964. (pfNone, pfMemoryCreate, pfMemoryOpen, pfMemoryCreate, pfMemoryOpen,
  965. pfMemoryCreate, pfMemoryOpen, pfMemoryOpen);
  966. begin
  967. FDbfFile := TDbfFile.Create;
  968. if FStorage = stoMemory then
  969. begin
  970. FDbfFile.Stream := FUserStream;
  971. FDbfFile.Mode := FileModeToMemMode[FileOpenMode];
  972. end else begin
  973. FDbfFile.FileName := FAbsolutePath + FTableName;
  974. FDbfFile.Mode := FileOpenMode;
  975. end;
  976. FDbfFile.AutoCreate := false;
  977. FDbfFile.DateTimeHandling := FDateTimeHandling;
  978. FDbfFile.OnLocaleError := FOnLocaleError;
  979. FDbfFile.OnIndexMissing := FOnIndexMissing;
  980. end;
  981. procedure TDbf.InternalInitFieldDefs; {override virtual abstract from TDataset}
  982. var
  983. MustReleaseDbfFile: Boolean;
  984. begin
  985. MustReleaseDbfFile := false;
  986. with FieldDefs do
  987. begin
  988. if FDbfFile = nil then
  989. begin
  990. // do not AutoCreate file
  991. InitDbfFile(pfReadOnly);
  992. FDbfFile.Open;
  993. MustReleaseDbfFile := true;
  994. end;
  995. GetFieldDefsFromDbfFieldDefs;
  996. if MustReleaseDbfFile then
  997. FreeAndNil(FDbfFile);
  998. end;
  999. end;
  1000. procedure TDbf.InternalInitRecord(Buffer: PChar); {override virtual abstract from TDataset}
  1001. var
  1002. pRecord: pDbfRecord;
  1003. begin
  1004. pRecord := pDbfRecord(Buffer);
  1005. pRecord^.BookmarkData.PhysicalRecNo := 0;
  1006. pRecord^.BookmarkFlag := bfCurrent;
  1007. pRecord^.SequentialRecNo := 0;
  1008. // Init Record with zero and set autoinc field with next value
  1009. FDbfFile.InitRecord(@pRecord^.DeletedFlag);
  1010. end;
  1011. procedure TDbf.InternalLast; {override virtual abstract from TDataset}
  1012. begin
  1013. FCursor.Last;
  1014. end;
  1015. procedure TDbf.DetermineTranslationMode;
  1016. var
  1017. lCodePage: Cardinal;
  1018. begin
  1019. lCodePage := FDbfFile.UseCodePage;
  1020. if lCodePage = GetACP then
  1021. FTranslationMode := tmNoneNeeded
  1022. else
  1023. if lCodePage = GetOEMCP then
  1024. FTranslationMode := tmSimple
  1025. // check if this code page, although non default, is installed
  1026. else
  1027. if DbfGlobals.CodePageInstalled(lCodePage) then
  1028. FTranslationMode := tmAdvanced
  1029. else
  1030. FTranslationMode := tmNoneAvailable;
  1031. end;
  1032. procedure TDbf.InternalOpen; {override virtual abstract from TDataset}
  1033. const
  1034. DbfOpenMode: array[Boolean, Boolean] of TPagedFileMode =
  1035. ((pfReadWriteOpen, pfExclusiveOpen), (pfReadOnly, pfReadOnly));
  1036. var
  1037. lIndex: TDbfIndexDef;
  1038. lIndexName: string;
  1039. LanguageAction: TDbfLanguageAction;
  1040. doCreate: Boolean;
  1041. I: Integer;
  1042. begin
  1043. // close current file
  1044. FreeAndNil(FDbfFile);
  1045. // does file not exist? -> create
  1046. if ((FStorage = stoFile) and
  1047. not FileExists(FAbsolutePath + FTableName) and
  1048. (FOpenMode in [omAutoCreate, omTemporary])) or
  1049. ((FStorage = stoMemory) and (FUserStream = nil)) then
  1050. begin
  1051. doCreate := true;
  1052. if Assigned(FBeforeAutoCreate) then
  1053. FBeforeAutoCreate(Self, doCreate);
  1054. if doCreate then
  1055. CreateTable
  1056. else
  1057. exit;
  1058. end;
  1059. // now we know for sure the file exists
  1060. InitDbfFile(DbfOpenMode[FReadOnly, FExclusive]);
  1061. FDbfFile.Open;
  1062. // fail open?
  1063. {$ifndef FPC}
  1064. if FDbfFile.ForceClose then
  1065. Abort;
  1066. {$endif}
  1067. // determine dbf version
  1068. case FDbfFile.DbfVersion of
  1069. xBaseIII: FTableLevel := 3;
  1070. xBaseIV: FTableLevel := 4;
  1071. xBaseVII: FTableLevel := 7;
  1072. xFoxPro: FTableLevel := TDBF_TABLELEVEL_FOXPRO;
  1073. end;
  1074. FLanguageID := FDbfFile.LanguageID;
  1075. // build VCL fielddef list from native DBF FieldDefs
  1076. (*
  1077. if (FDbfFile.HeaderSize = 0) or (FDbfFile.FieldDefs.Count = 0) then
  1078. begin
  1079. if FieldDefs.Count > 0 then
  1080. begin
  1081. CreateTableFromFieldDefs;
  1082. end else begin
  1083. CreateTableFromFields;
  1084. end;
  1085. end else begin
  1086. *)
  1087. // GetFieldDefsFromDbfFieldDefs;
  1088. // end;
  1089. {$ifdef SUPPORT_FIELDDEFS_UPDATED}
  1090. FieldDefs.Updated := False;
  1091. FieldDefs.Update;
  1092. {$else}
  1093. InternalInitFieldDefs;
  1094. {$endif}
  1095. // create the fields dynamically
  1096. if DefaultFields then
  1097. CreateFields; // Create fields from fielddefs.
  1098. BindFields(true);
  1099. // create array of blobstreams to store memo's in. each field is a possible blob
  1100. FBlobStreams := AllocMem(FieldDefs.Count * SizeOf(TDbfBlobStream));
  1101. // check codepage settings
  1102. DetermineTranslationMode;
  1103. if FTranslationMode = tmNoneAvailable then
  1104. begin
  1105. // no codepage available? ask user
  1106. LanguageAction := laReadOnly;
  1107. if Assigned(FOnLanguageWarning) then
  1108. FOnLanguageWarning(Self, LanguageAction);
  1109. case LanguageAction of
  1110. laReadOnly: FTranslationMode := tmNoneAvailable;
  1111. laForceOEM:
  1112. begin
  1113. FDbfFile.UseCodePage := GetOEMCP;
  1114. FTranslationMode := tmSimple;
  1115. end;
  1116. laForceANSI:
  1117. begin
  1118. FDbfFile.UseCodePage := GetACP;
  1119. FTranslationMode := tmNoneNeeded;
  1120. end;
  1121. laDefault:
  1122. begin
  1123. FDbfFile.UseCodePage := DbfGlobals.DefaultOpenCodePage;
  1124. DetermineTranslationMode;
  1125. end;
  1126. end;
  1127. end;
  1128. // allocate a record buffer for temporary data
  1129. FTempBuffer := AllocRecordBuffer;
  1130. // open indexes
  1131. for I := 0 to FIndexDefs.Count - 1 do
  1132. begin
  1133. lIndex := FIndexDefs.Items[I];
  1134. lIndexName := ParseIndexName(lIndex.IndexFile);
  1135. // if index does not exist -> create, if it does exist -> open only
  1136. FDbfFile.OpenIndex(lIndexName, lIndex.SortField, false, lIndex.Options);
  1137. end;
  1138. // parse filter expression
  1139. try
  1140. ParseFilter(Filter);
  1141. except
  1142. // oops, a problem with parsing, clear filter for now
  1143. on E: EDbfError do Filter := EmptyStr;
  1144. end;
  1145. SetIndexName(FIndexName);
  1146. // SetIndexName will have made the cursor for us if no index selected :-)
  1147. // if FCursor = nil then FCursor := TDbfCursor.Create(FDbfFile);
  1148. InternalFirst;
  1149. // FDbfFile.SetIndex(FIndexName);
  1150. // FDbfFile.FIsCursorOpen := true;
  1151. end;
  1152. function TDbf.GetCodePage: Cardinal;
  1153. begin
  1154. if FDbfFile <> nil then
  1155. Result := FDbfFile.UseCodePage
  1156. else
  1157. Result := 0;
  1158. end;
  1159. function TDbf.GetLanguageStr: String;
  1160. begin
  1161. if FDbfFile <> nil then
  1162. Result := FDbfFile.LanguageStr;
  1163. end;
  1164. function TDbf.LockTable(const Wait: Boolean): Boolean;
  1165. begin
  1166. CheckActive;
  1167. Result := FDbfFile.LockAllPages(Wait);
  1168. end;
  1169. procedure TDbf.UnlockTable;
  1170. begin
  1171. CheckActive;
  1172. FDbfFile.UnlockAllPages;
  1173. end;
  1174. procedure TDbf.InternalEdit;
  1175. var
  1176. I: Integer;
  1177. begin
  1178. // store recno we are editing
  1179. FEditingRecNo := FCursor.PhysicalRecNo;
  1180. // reread blobs, execute cancel -> clears remembered memo pageno,
  1181. // causing it to reread the memo contents
  1182. for I := 0 to Pred(FieldDefs.Count) do
  1183. if Assigned(FBlobStreams^[I]) then
  1184. FBlobStreams^[I].Cancel;
  1185. // try to lock this record
  1186. FDbfFile.LockRecord(FEditingRecNo, @pDbfRecord(ActiveBuffer)^.DeletedFlag);
  1187. // succeeded!
  1188. end;
  1189. {$ifndef FPC}
  1190. {$ifndef DELPHI_3}
  1191. procedure TDbf.InternalInsert; {override virtual from TDataset}
  1192. begin
  1193. CursorPosChanged;
  1194. end;
  1195. {$endif}
  1196. {$endif}
  1197. procedure TDbf.InternalPost; {override virtual abstract from TDataset}
  1198. var
  1199. pRecord: pDbfRecord;
  1200. I, newRecord: Integer;
  1201. begin
  1202. // if internalpost is called, we know we are active
  1203. pRecord := pDbfRecord(ActiveBuffer);
  1204. // commit blobs
  1205. for I := 0 to Pred(FieldDefs.Count) do
  1206. if Assigned(FBlobStreams^[I]) then
  1207. FBlobStreams^[I].Commit;
  1208. if State = dsEdit then
  1209. begin
  1210. // write changes
  1211. FDbfFile.UnlockRecord(FEditingRecNo, @pRecord^.DeletedFlag);
  1212. // not editing anymore
  1213. FEditingRecNo := -1;
  1214. end else begin
  1215. // insert
  1216. newRecord := FDbfFile.Insert(@pRecord^.DeletedFlag);
  1217. if newRecord > 0 then
  1218. FCursor.PhysicalRecNo := newRecord;
  1219. end;
  1220. // set flag that TDataSet is about to post...so we can disable resync
  1221. FPosting := true;
  1222. end;
  1223. procedure TDbf.Resync(Mode: TResyncMode);
  1224. begin
  1225. // try to increase speed
  1226. if not FDisableResyncOnPost or not FPosting then
  1227. inherited;
  1228. // clear post flag
  1229. FPosting := false;
  1230. end;
  1231. {$ifndef SUPPORT_INITDEFSFROMFIELDS}
  1232. procedure TDbf.InitFieldDefsFromFields;
  1233. var
  1234. I: Integer;
  1235. F: TField;
  1236. begin
  1237. { create fielddefs from persistent fields if needed }
  1238. for I := 0 to FieldCount - 1 do
  1239. begin
  1240. F := Fields[I];
  1241. with F do
  1242. if FieldKind = fkData then begin
  1243. FieldDefs.Add(FieldName,DataType,Size,Required);
  1244. end;
  1245. end;
  1246. end;
  1247. {$endif}
  1248. procedure TDbf.CreateTable;
  1249. begin
  1250. CreateTableEx(nil);
  1251. end;
  1252. procedure TDbf.CheckDbfFieldDefs(ADbfFieldDefs: TDbfFieldDefs);
  1253. var
  1254. I: Integer;
  1255. TempDef: TDbfFieldDef;
  1256. function FieldTypeStr(const FieldType: char): string;
  1257. begin
  1258. if FieldType = #0 then
  1259. Result := 'NULL'
  1260. else if FieldType > #127 then
  1261. Result := 'ASCII '+IntToStr(Byte(FieldType))
  1262. else
  1263. Result := ' "'+fieldType+'" ';
  1264. Result := ' ' + Result + '(#'+IntToHex(Byte(FieldType),SizeOf(FieldType))+') '
  1265. end;
  1266. begin
  1267. if ADbfFieldDefs = nil then exit;
  1268. for I := 0 to ADbfFieldDefs.Count - 1 do
  1269. begin
  1270. // check dbffielddefs for errors
  1271. TempDef := ADbfFieldDefs.Items[I];
  1272. if FTableLevel < 7 then
  1273. if not (TempDef.NativeFieldType in ['C', 'F', 'N', 'D', 'L', 'M']) then
  1274. raise EDbfError.CreateFmt(STRING_INVALID_FIELD_TYPE,
  1275. [FieldTypeStr(TempDef.NativeFieldType), TempDef.FieldName]);
  1276. end;
  1277. end;
  1278. procedure TDbf.CreateTableEx(ADbfFieldDefs: TDbfFieldDefs);
  1279. var
  1280. I: Integer;
  1281. lIndex: TDbfIndexDef;
  1282. lIndexName: string;
  1283. tempFieldDefs: Boolean;
  1284. begin
  1285. CheckInactive;
  1286. tempFieldDefs := ADbfFieldDefs = nil;
  1287. try
  1288. try
  1289. if tempFieldDefs then
  1290. begin
  1291. ADbfFieldDefs := TDbfFieldDefs.Create(Self);
  1292. ADbfFieldDefs.DbfVersion := TableLevelToDbfVersion(FTableLevel);
  1293. // get fields -> fielddefs if no fielddefs
  1294. {$ifndef FPC_VERSION}
  1295. if FieldDefs.Count = 0 then
  1296. InitFieldDefsFromFields;
  1297. {$endif}
  1298. // fielddefs -> dbffielddefs
  1299. for I := 0 to FieldDefs.Count - 1 do
  1300. begin
  1301. with ADbfFieldDefs.AddFieldDef do
  1302. begin
  1303. FieldName := FieldDefs.Items[I].Name;
  1304. FieldType := FieldDefs.Items[I].DataType;
  1305. if FieldDefs.Items[I].Size > 0 then
  1306. begin
  1307. Size := FieldDefs.Items[I].Size;
  1308. Precision := FieldDefs.Items[I].Precision;
  1309. end else begin
  1310. SetDefaultSize;
  1311. end;
  1312. end;
  1313. end;
  1314. end;
  1315. InitDbfFile(pfExclusiveCreate);
  1316. FDbfFile.CopyDateTimeAsString := FInCopyFrom and FCopyDateTimeAsString;
  1317. FDbfFile.DbfVersion := TableLevelToDbfVersion(FTableLevel);
  1318. FDbfFile.FileLangID := FLanguageID;
  1319. FDbfFile.Open;
  1320. FDbfFile.FinishCreate(ADbfFieldDefs, 512);
  1321. // if creating memory table, copy stream pointer
  1322. if FStorage = stoMemory then
  1323. FUserStream := FDbfFile.Stream;
  1324. // create all indexes
  1325. for I := 0 to FIndexDefs.Count-1 do
  1326. begin
  1327. lIndex := FIndexDefs.Items[I];
  1328. lIndexName := ParseIndexName(lIndex.IndexFile);
  1329. FDbfFile.OpenIndex(lIndexName, lIndex.SortField, true, lIndex.Options);
  1330. end;
  1331. except
  1332. // dbf file created?
  1333. if (FDbfFile <> nil) and (FStorage = stoFile) then
  1334. begin
  1335. FreeAndNil(FDbfFile);
  1336. SysUtils.DeleteFile(FAbsolutePath+FTableName);
  1337. end;
  1338. raise;
  1339. end;
  1340. finally
  1341. // free temporary fielddefs
  1342. if tempFieldDefs and Assigned(ADbfFieldDefs) then
  1343. ADbfFieldDefs.Free;
  1344. FreeAndNil(FDbfFile);
  1345. end;
  1346. end;
  1347. procedure TDbf.EmptyTable;
  1348. begin
  1349. Zap;
  1350. end;
  1351. procedure TDbf.Zap;
  1352. begin
  1353. // are we active?
  1354. CheckActive;
  1355. FDbfFile.Zap;
  1356. end;
  1357. procedure TDbf.RestructureTable(ADbfFieldDefs: TDbfFieldDefs; Pack: Boolean);
  1358. begin
  1359. CheckInactive;
  1360. // check field defs for errors
  1361. CheckDbfFieldDefs(ADbfFieldDefs);
  1362. // open dbf file
  1363. InitDbfFile(pfExclusiveOpen);
  1364. FDbfFile.Open;
  1365. // do restructure
  1366. try
  1367. FDbfFile.RestructureTable(ADbfFieldDefs, Pack);
  1368. finally
  1369. // close file
  1370. FreeAndNil(FDbfFile);
  1371. end;
  1372. end;
  1373. procedure TDbf.PackTable;
  1374. var
  1375. oldIndexName: string;
  1376. begin
  1377. CheckBrowseMode;
  1378. // deselect any index while packing
  1379. oldIndexName := IndexName;
  1380. IndexName := EmptyStr;
  1381. // pack
  1382. FDbfFile.RestructureTable(nil, true);
  1383. // reselect index
  1384. IndexName := oldIndexName;
  1385. end;
  1386. procedure TDbf.CopyFrom(DataSet: TDataSet; FileName: string; DateTimeAsString: Boolean; Level: Integer);
  1387. var
  1388. lPhysFieldDefs, lFieldDefs: TDbfFieldDefs;
  1389. lSrcField, lDestField: TField;
  1390. I: integer;
  1391. begin
  1392. FInCopyFrom := true;
  1393. lFieldDefs := TDbfFieldDefs.Create(nil);
  1394. lPhysFieldDefs := TDbfFieldDefs.Create(nil);
  1395. try
  1396. if Active then
  1397. Close;
  1398. FilePath := ExtractFilePath(FileName);
  1399. TableName := ExtractFileName(FileName);
  1400. FCopyDateTimeAsString := DateTimeAsString;
  1401. TableLevel := Level;
  1402. if not DataSet.Active then
  1403. DataSet.Open;
  1404. DataSet.FieldDefs.Update;
  1405. // first get a list of physical field defintions
  1406. // we need it for numeric precision in case source is tdbf
  1407. if DataSet is TDbf then
  1408. begin
  1409. lPhysFieldDefs.Assign(TDbf(DataSet).DbfFieldDefs);
  1410. IndexDefs.Assign(TDbf(DataSet).IndexDefs);
  1411. end else begin
  1412. {$ifdef SUPPORT_FIELDDEF_TPERSISTENT}
  1413. lPhysFieldDefs.Assign(DataSet.FieldDefs);
  1414. {$endif}
  1415. IndexDefs.Clear;
  1416. end;
  1417. // convert list of tfields into a list of tdbffielddefs
  1418. // so that our tfields will correspond to the source tfields
  1419. for I := 0 to Pred(DataSet.FieldCount) do
  1420. begin
  1421. lSrcField := DataSet.Fields[I];
  1422. with lFieldDefs.AddFieldDef do
  1423. begin
  1424. if Length(lSrcField.Name) > 0 then
  1425. FieldName := lSrcField.Name
  1426. else
  1427. FieldName := lSrcField.FieldName;
  1428. FieldType := lSrcField.DataType;
  1429. Required := lSrcField.Required;
  1430. if (1 <= lSrcField.FieldNo)
  1431. and (lSrcField.FieldNo <= lPhysFieldDefs.Count) then
  1432. begin
  1433. Size := lPhysFieldDefs.Items[lSrcField.FieldNo-1].Size;
  1434. Precision := lPhysFieldDefs.Items[lSrcField.FieldNo-1].Precision;
  1435. end;
  1436. end;
  1437. end;
  1438. CreateTableEx(lFieldDefs);
  1439. Open;
  1440. DataSet.First;
  1441. {$ifdef USE_CACHE}
  1442. FDbfFile.BufferAhead := true;
  1443. if DataSet is TDbf then
  1444. TDbf(DataSet).DbfFile.BufferAhead := true;
  1445. {$endif}
  1446. while not DataSet.EOF do
  1447. begin
  1448. Append;
  1449. for I := 0 to Pred(FieldCount) do
  1450. begin
  1451. lSrcField := DataSet.Fields[I];
  1452. lDestField := Fields[I];
  1453. if not lSrcField.IsNull then
  1454. begin
  1455. if lSrcField.DataType = ftDateTime then
  1456. begin
  1457. if FCopyDateTimeAsString then
  1458. begin
  1459. lDestField.AsString := lSrcField.AsString;
  1460. if Assigned(FOnCopyDateTimeAsString) then
  1461. FOnCopyDateTimeAsString(Self, lDestField, lSrcField)
  1462. end else
  1463. lDestField.AsDateTime := lSrcField.AsDateTime;
  1464. end else
  1465. lDestField.Assign(lSrcField);
  1466. end;
  1467. end;
  1468. Post;
  1469. DataSet.Next;
  1470. end;
  1471. Close;
  1472. finally
  1473. {$ifdef USE_CACHE}
  1474. if (DataSet is TDbf) and (TDbf(DataSet).DbfFile <> nil) then
  1475. TDbf(DataSet).DbfFile.BufferAhead := false;
  1476. {$endif}
  1477. FInCopyFrom := false;
  1478. lFieldDefs.Free;
  1479. lPhysFieldDefs.Free;
  1480. end;
  1481. end;
  1482. function TDbf.FindRecord(Restart, GoForward: Boolean): Boolean;
  1483. var
  1484. oldRecNo: Integer;
  1485. begin
  1486. CheckBrowseMode;
  1487. DoBeforeScroll;
  1488. Result := false;
  1489. UpdateCursorPos;
  1490. oldRecNo := RecNo;
  1491. try
  1492. FFindRecordFilter := true;
  1493. if GoForward then
  1494. begin
  1495. if Restart then FCursor.First;
  1496. Result := GetRecord(FTempBuffer, gmNext, false) = grOK;
  1497. end else begin
  1498. if Restart then FCursor.Last;
  1499. Result := GetRecord(FTempBuffer, gmPrior, false) = grOK;
  1500. end;
  1501. finally
  1502. FFindRecordFilter := false;
  1503. if not Result then
  1504. begin
  1505. RecNo := oldRecNo;
  1506. end else begin
  1507. CursorPosChanged;
  1508. Resync([]);
  1509. DoAfterScroll;
  1510. end;
  1511. end;
  1512. end;
  1513. {$ifdef SUPPORT_VARIANTS}
  1514. function TDbf.Lookup(const KeyFields: string; const KeyValues: Variant;
  1515. const ResultFields: string): Variant;
  1516. var
  1517. // OldState: TDataSetState;
  1518. saveRecNo: integer;
  1519. saveState: TDataSetState;
  1520. begin
  1521. Result := Null;
  1522. if (FCursor = nil) or VarIsNull(KeyValues) then exit;
  1523. saveRecNo := FCursor.SequentialRecNo;
  1524. try
  1525. if LocateRecord(KeyFields, KeyValues, []) then
  1526. begin
  1527. // FFilterBuffer contains record buffer
  1528. saveState := SetTempState(dsCalcFields);
  1529. try
  1530. CalculateFields(FFilterBuffer);
  1531. if KeyValues = FieldValues[KeyFields] then
  1532. Result := FieldValues[ResultFields];
  1533. finally
  1534. RestoreState(saveState);
  1535. end;
  1536. end;
  1537. finally
  1538. FCursor.SequentialRecNo := saveRecNo;
  1539. end;
  1540. end;
  1541. function TDbf.Locate(const KeyFields: string; const KeyValues: Variant; Options: TLocateOptions): Boolean;
  1542. var
  1543. saveRecNo: integer;
  1544. begin
  1545. if FCursor = nil then
  1546. begin
  1547. Result := false;
  1548. exit;
  1549. end;
  1550. DoBeforeScroll;
  1551. saveRecNo := FCursor.SequentialRecNo;
  1552. FLocateRecNo := -1;
  1553. Result := LocateRecord(KeyFields, KeyValues, Options);
  1554. CursorPosChanged;
  1555. if Result then
  1556. begin
  1557. if FLocateRecNo <> -1 then
  1558. FCursor.PhysicalRecNo := FLocateRecNo;
  1559. Resync([]);
  1560. DoAfterScroll;
  1561. end else
  1562. FCursor.SequentialRecNo := saveRecNo;
  1563. end;
  1564. function TDbf.LocateRecordLinear(const KeyFields: String; const KeyValues: Variant;
  1565. Options: TLocateOptions): Boolean;
  1566. var
  1567. lstKeys : TList;
  1568. iIndex : Integer;
  1569. Field : TField;
  1570. bMatchedData : Boolean;
  1571. bVarIsArray : Boolean;
  1572. varCompare : Variant;
  1573. function CompareValues: Boolean;
  1574. var
  1575. sCompare: String;
  1576. begin
  1577. if (Field.DataType = ftString) then
  1578. begin
  1579. sCompare := VarToStr(varCompare);
  1580. if loCaseInsensitive in Options then
  1581. begin
  1582. Result := AnsiCompareText(Field.AsString,sCompare) = 0;
  1583. if not Result and (iIndex = lstKeys.Count - 1) and (loPartialKey in Options) and
  1584. (Length(sCompare) < Length(Field.AsString)) then
  1585. begin
  1586. if Length(sCompare) = 0 then
  1587. Result := true
  1588. else
  1589. Result := AnsiCompareText (Copy (Field.AsString,1,Length (sCompare)),sCompare) = 0;
  1590. end;
  1591. end else begin
  1592. Result := Field.AsString = sCompare;
  1593. if not Result and (iIndex = lstKeys.Count - 1) and (loPartialKey in Options) and
  1594. (Length (sCompare) < Length (Field.AsString)) then
  1595. begin
  1596. if Length (sCompare) = 0 then
  1597. Result := true
  1598. else
  1599. Result := Copy(Field.AsString, 1, Length(sCompare)) = sCompare;
  1600. end;
  1601. end;
  1602. end
  1603. else
  1604. Result := Field.Value = varCompare;
  1605. end;
  1606. var
  1607. SaveState: TDataSetState;
  1608. lPhysRecNo: integer;
  1609. begin
  1610. Result := false;
  1611. bVarIsArray := false;
  1612. lstKeys := TList.Create;
  1613. FFilterBuffer := TempBuffer;
  1614. SaveState := SetTempState(dsFilter);
  1615. try
  1616. GetFieldList(lstKeys, KeyFields);
  1617. if VarArrayDimCount(KeyValues) = 0 then
  1618. bMatchedData := lstKeys.Count = 1
  1619. else if VarArrayDimCount (KeyValues) = 1 then
  1620. begin
  1621. bMatchedData := VarArrayHighBound (KeyValues,1) + 1 = lstKeys.Count;
  1622. bVarIsArray := true;
  1623. end else
  1624. bMatchedData := false;
  1625. if bMatchedData then
  1626. begin
  1627. FCursor.First;
  1628. while not Result and FCursor.Next do
  1629. begin
  1630. lPhysRecNo := FCursor.PhysicalRecNo;
  1631. if (lPhysRecNo = 0) or not FDbfFile.IsRecordPresent(lPhysRecNo) then
  1632. break;
  1633. FDbfFile.ReadRecord(lPhysRecNo, @PDbfRecord(FFilterBuffer)^.DeletedFlag);
  1634. Result := FShowDeleted or (PDbfRecord(FFilterBuffer)^.DeletedFlag <> '*');
  1635. if Result and Filtered then
  1636. DoFilterRecord(Result);
  1637. iIndex := 0;
  1638. while Result and (iIndex < lstKeys.Count) Do
  1639. begin
  1640. Field := TField (lstKeys [iIndex]);
  1641. if bVarIsArray then
  1642. varCompare := KeyValues [iIndex]
  1643. else
  1644. varCompare := KeyValues;
  1645. Result := CompareValues;
  1646. Inc(iIndex);
  1647. end;
  1648. end;
  1649. end;
  1650. finally
  1651. lstKeys.Free;
  1652. RestoreState(SaveState);
  1653. end;
  1654. end;
  1655. function TDbf.LocateRecordIndex(const KeyFields: String; const KeyValues: Variant;
  1656. Options: TLocateOptions): Boolean;
  1657. var
  1658. searchFlag: TSearchKeyType;
  1659. matchRes: Integer;
  1660. lTempBuffer: array [0..100] of Char;
  1661. begin
  1662. if loPartialKey in Options then
  1663. searchFlag := stGreaterEqual
  1664. else
  1665. searchFlag := stEqual;
  1666. if TIndexCursor(FCursor).VariantToBuffer(KeyValues, @lTempBuffer[0]) = etString then
  1667. Translate(@lTempBuffer[0], @lTempBuffer[0], true);
  1668. Result := FIndexFile.SearchKey(@lTempBuffer[0], searchFlag);
  1669. if Result then
  1670. begin
  1671. Result := GetRecord(TempBuffer, gmCurrent, false) = grOK;
  1672. if not Result then
  1673. begin
  1674. Result := GetRecord(TempBuffer, gmNext, false) = grOK;
  1675. if Result then
  1676. begin
  1677. matchRes := TIndexCursor(FCursor).IndexFile.MatchKey(@lTempBuffer[0]);
  1678. if loPartialKey in Options then
  1679. Result := matchRes <= 0
  1680. else
  1681. Result := matchRes = 0;
  1682. end;
  1683. end;
  1684. FFilterBuffer := TempBuffer;
  1685. end;
  1686. end;
  1687. function TDbf.LocateRecord(const KeyFields: String; const KeyValues: Variant;
  1688. Options: TLocateOptions): Boolean;
  1689. var
  1690. lCursor, lSaveCursor: TVirtualCursor;
  1691. lSaveIndexName, lIndexName: string;
  1692. lIndexDef: TDbfIndexDef;
  1693. lIndexFile, lSaveIndexFile: TIndexFile;
  1694. begin
  1695. lCursor := nil;
  1696. lSaveCursor := nil;
  1697. lIndexFile := nil;
  1698. lSaveIndexFile := FIndexFile;
  1699. if (FCursor is TIndexCursor)
  1700. and (TIndexCursor(FCursor).IndexFile.Expression = KeyFields) then
  1701. begin
  1702. lCursor := FCursor;
  1703. end else begin
  1704. lIndexDef := FIndexDefs.GetIndexByField(KeyFields);
  1705. if lIndexDef <> nil then
  1706. begin
  1707. lIndexName := ParseIndexName(lIndexDef.IndexFile);
  1708. lIndexFile := FDbfFile.GetIndexByName(lIndexName);
  1709. if lIndexFile <> nil then
  1710. begin
  1711. lSaveCursor := FCursor;
  1712. lCursor := TIndexCursor.Create(lIndexFile);
  1713. lSaveIndexName := lIndexFile.IndexName;
  1714. lIndexFile.IndexName := lIndexName;
  1715. FIndexFile := lIndexFile;
  1716. end;
  1717. end;
  1718. end;
  1719. if lCursor <> nil then
  1720. begin
  1721. FCursor := lCursor;
  1722. Result := LocateRecordIndex(KeyFields, KeyValues, Options);
  1723. if lSaveCursor <> nil then
  1724. begin
  1725. FCursor.Free;
  1726. FCursor := lSaveCursor;
  1727. end;
  1728. if lIndexFile <> nil then
  1729. begin
  1730. FLocateRecNo := FIndexFile.PhysicalRecNo;
  1731. lIndexFile.IndexName := lSaveIndexName;
  1732. FIndexFile := lSaveIndexFile;
  1733. end;
  1734. end else
  1735. Result := LocateRecordLinear(KeyFields, KeyValues, Options);
  1736. end;
  1737. {$endif}
  1738. procedure TDbf.TryExclusive;
  1739. begin
  1740. // are we active?
  1741. if Active then
  1742. begin
  1743. // already in exclusive mode?
  1744. FDbfFile.TryExclusive;
  1745. // update file mode
  1746. FExclusive := not FDbfFile.IsSharedAccess;
  1747. FReadOnly := FDbfFile.Mode = pfReadOnly;
  1748. end else begin
  1749. // just set exclusive to true
  1750. FExclusive := true;
  1751. FReadOnly := false;
  1752. end;
  1753. end;
  1754. procedure TDbf.EndExclusive;
  1755. begin
  1756. if Active then
  1757. begin
  1758. // call file handler
  1759. FDbfFile.EndExclusive;
  1760. // update file mode
  1761. FExclusive := not FDbfFile.IsSharedAccess;
  1762. FReadOnly := FDbfFile.Mode = pfReadOnly;
  1763. end else begin
  1764. // just set exclusive to false
  1765. FExclusive := false;
  1766. end;
  1767. end;
  1768. function TDbf.CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream; {override virtual}
  1769. var
  1770. MemoPageNo: Integer;
  1771. MemoFieldNo: Integer;
  1772. lBlob: TDbfBlobStream;
  1773. begin
  1774. // check if in editing mode if user wants to write
  1775. if (Mode = bmWrite) or (Mode = bmReadWrite) then
  1776. if not (State in [dsEdit, dsInsert]) then
  1777. {$ifdef DELPHI_3}
  1778. DatabaseError(SNotEditing);
  1779. {$else}
  1780. DatabaseError(SNotEditing, Self);
  1781. {$endif}
  1782. // already created a `placeholder' blob for this field?
  1783. MemoFieldNo := Field.FieldNo - 1;
  1784. if FBlobStreams^[MemoFieldNo] = nil then
  1785. FBlobStreams^[MemoFieldNo] := TDbfBlobStream.Create(Field);
  1786. lBlob := FBlobStreams^[MemoFieldNo].AddReference;
  1787. // update pageno of blob <-> location where to read/write in memofile
  1788. if FDbfFile.GetFieldData(Field.FieldNo-1, ftInteger, GetCurrentBuffer, @MemoPageNo, false) then
  1789. begin
  1790. // read blob? different blob?
  1791. if (Mode = bmRead) or (Mode = bmReadWrite) then
  1792. begin
  1793. if MemoPageNo <> lBlob.MemoRecNo then
  1794. begin
  1795. FDbfFile.MemoFile.ReadMemo(MemoPageNo, lBlob);
  1796. lBlob.ReadSize := lBlob.Size;
  1797. lBlob.Translate(false);
  1798. end;
  1799. end else begin
  1800. lBlob.Size := 0;
  1801. lBlob.ReadSize := 0;
  1802. end;
  1803. lBlob.MemoRecNo := MemoPageNo;
  1804. end else
  1805. if not lBlob.Dirty or (Mode = bmWrite) then
  1806. begin
  1807. // reading and memo is empty and not written yet, or rewriting
  1808. lBlob.Size := 0;
  1809. lBlob.ReadSize := 0;
  1810. lBlob.MemoRecNo := 0;
  1811. end;
  1812. { this is a hack, we actually need to know per user who's modifying, and who is not }
  1813. { Mode is more like: the mode of the last "creation" }
  1814. { if create/free is nested, then everything will be alright, i think ;-) }
  1815. lBlob.Mode := Mode;
  1816. { this is a hack: we actually need to know per user what it's position is }
  1817. lBlob.Position := 0;
  1818. Result := lBlob;
  1819. end;
  1820. {$ifdef SUPPORT_NEW_TRANSLATE}
  1821. function TDbf.Translate(Src, Dest: PChar; ToOem: Boolean): Integer; {override virtual}
  1822. var
  1823. FromCP, ToCP: Cardinal;
  1824. begin
  1825. if (Src <> nil) and (Dest <> nil) then
  1826. begin
  1827. if Assigned(FOnTranslate) then
  1828. begin
  1829. Result := FOnTranslate(Self, Src, Dest, ToOem);
  1830. if Result = -1 then
  1831. Result := StrLen(Dest);
  1832. end else begin
  1833. if FTranslationMode <> tmNoneNeeded then
  1834. begin
  1835. if ToOem then
  1836. begin
  1837. FromCP := GetACP;
  1838. ToCP := FDbfFile.UseCodePage;
  1839. end else begin
  1840. FromCP := FDbfFile.UseCodePage;
  1841. ToCP := GetACP;
  1842. end;
  1843. end else begin
  1844. FromCP := GetACP;
  1845. ToCP := FromCP;
  1846. end;
  1847. Result := TranslateString(FromCP, ToCP, Src, Dest, -1);
  1848. end;
  1849. end else
  1850. Result := 0;
  1851. end;
  1852. {$else}
  1853. procedure TDbf.Translate(Src, Dest: PChar; ToOem: Boolean); {override virtual}
  1854. var
  1855. FromCP, ToCP: Cardinal;
  1856. begin
  1857. if (Src <> nil) and (Dest <> nil) then
  1858. begin
  1859. if Assigned(FOnTranslate) then
  1860. begin
  1861. FOnTranslate(Self, Src, Dest, ToOem);
  1862. end else begin
  1863. if FTranslationMode <> tmNoneNeeded then
  1864. begin
  1865. if ToOem then
  1866. begin
  1867. FromCP := GetACP;
  1868. ToCP := FDbfFile.UseCodePage;
  1869. end else begin
  1870. FromCP := FDbfFile.UseCodePage;
  1871. ToCP := GetACP;
  1872. end;
  1873. TranslateString(FromCP, ToCP, Src, Dest, -1);
  1874. end;
  1875. end;
  1876. end;
  1877. end;
  1878. {$endif}
  1879. procedure TDbf.ClearCalcFields(Buffer: PChar);
  1880. var
  1881. lRealBuffer, lCalcBuffer: PChar;
  1882. begin
  1883. lRealBuffer := @pDbfRecord(Buffer)^.DeletedFlag;
  1884. lCalcBuffer := lRealBuffer + FDbfFile.RecordSize;
  1885. FillChar(lCalcBuffer^, CalcFieldsSize, 0);
  1886. end;
  1887. procedure TDbf.InternalSetToRecord(Buffer: PChar); {override virtual abstract from TDataset}
  1888. var
  1889. pRecord: pDbfRecord;
  1890. begin
  1891. if Buffer <> nil then
  1892. begin
  1893. pRecord := pDbfRecord(Buffer);
  1894. if pRecord^.BookmarkFlag = bfInserted then
  1895. begin
  1896. // do what ???
  1897. end else begin
  1898. FCursor.SequentialRecNo := pRecord^.SequentialRecNo;
  1899. end;
  1900. end;
  1901. end;
  1902. function TDbf.IsCursorOpen: Boolean; {override virtual abstract from TDataset}
  1903. begin
  1904. Result := FCursor <> nil;
  1905. end;
  1906. function TDbf.FieldDefsStored: Boolean;
  1907. begin
  1908. Result := StoreDefs and (FieldDefs.Count > 0);
  1909. end;
  1910. procedure TDbf.SetBookmarkFlag(Buffer: PChar; Value: TBookmarkFlag); {override virtual abstract from TDataset}
  1911. begin
  1912. pDbfRecord(Buffer)^.BookmarkFlag := Value;
  1913. end;
  1914. procedure TDbf.SetBookmarkData(Buffer: PChar; Data: Pointer); {override virtual abstract from TDataset}
  1915. begin
  1916. pDbfRecord(Buffer)^.BookmarkData := pBookmarkData(Data)^;
  1917. end;
  1918. // this function counts real number of records: skip deleted records, filter, etc.
  1919. // warning: is very slow, compared to GetRecordCount
  1920. function TDbf.GetExactRecordCount: Integer;
  1921. var
  1922. prevRecNo: Integer;
  1923. getRes: TGetResult;
  1924. begin
  1925. // init vars
  1926. Result := 0;
  1927. // check if FCursor open
  1928. if FCursor = nil then
  1929. exit;
  1930. // store current position
  1931. prevRecNo := FCursor.SequentialRecNo;
  1932. FCursor.First;
  1933. repeat
  1934. // repeatedly retrieve next record until eof encountered
  1935. getRes := GetRecord(FTempBuffer, gmNext, true);
  1936. if getRes = grOk then
  1937. inc(Result);
  1938. until getRes <> grOk;
  1939. // restore current position
  1940. FCursor.SequentialRecNo := prevRecNo;
  1941. end;
  1942. // this functions returns the physical number of records present in file
  1943. function TDbf.GetPhysicalRecordCount: Integer;
  1944. begin
  1945. if FDbfFile <> nil then
  1946. Result := FDbfFile.RecordCount
  1947. else
  1948. Result := 0
  1949. end;
  1950. // this function is just for the grid scrollbars
  1951. // it doesn't have to be perfectly accurate, but fast.
  1952. function TDbf.GetRecordCount: Integer; {override virtual}
  1953. begin
  1954. if FCursor <> nil then
  1955. Result := FCursor.SequentialRecordCount
  1956. else
  1957. Result := 0
  1958. end;
  1959. // this function is just for the grid scrollbars
  1960. // it doesn't have to be perfectly accurate, but fast.
  1961. function TDbf.GetRecNo: Integer; {override virtual}
  1962. var
  1963. pBuffer: pointer;
  1964. begin
  1965. if FCursor <> nil then
  1966. begin
  1967. if State = dsCalcFields then
  1968. pBuffer := CalcBuffer
  1969. else
  1970. pBuffer := ActiveBuffer;
  1971. Result := pDbfRecord(pBuffer)^.SequentialRecNo;
  1972. end else
  1973. Result := 0;
  1974. end;
  1975. procedure TDbf.SetRecNo(Value: Integer); {override virtual}
  1976. begin
  1977. CheckBrowseMode;
  1978. if Value = RecNo then
  1979. exit;
  1980. DoBeforeScroll;
  1981. FCursor.SequentialRecNo := Value;
  1982. CursorPosChanged;
  1983. Resync([]);
  1984. DoAfterScroll;
  1985. end;
  1986. function TDbf.GetCanModify: Boolean; {override;}
  1987. begin
  1988. if FReadOnly or (csDesigning in ComponentState) then
  1989. Result := false
  1990. else
  1991. Result := FTranslationMode > tmNoneAvailable;
  1992. end;
  1993. {$ifdef SUPPORT_DEFCHANGED}
  1994. procedure TDbf.DefChanged(Sender: TObject);
  1995. begin
  1996. StoreDefs := true;
  1997. end;
  1998. {$endif}
  1999. procedure TDbf.ParseFilter(const AFilter: string);
  2000. begin
  2001. // parser created?
  2002. if Length(AFilter) > 0 then
  2003. begin
  2004. if (FParser = nil) and (FDbfFile <> nil) then
  2005. begin
  2006. FParser := TDbfParser.Create(FDbfFile);
  2007. // we need truncated, translated (to ANSI) strings
  2008. FParser.RawStringFields := false;
  2009. end;
  2010. // have a parser now?
  2011. if FParser <> nil then
  2012. begin
  2013. // set options
  2014. FParser.PartialMatch := not (foNoPartialCompare in FilterOptions);
  2015. FParser.CaseInsensitive := foCaseInsensitive in FilterOptions;
  2016. // parse expression
  2017. FParser.ParseExpression(AFilter);
  2018. end;
  2019. end;
  2020. end;
  2021. procedure TDbf.SetFilterText(const Value: String);
  2022. begin
  2023. if Value = Filter then
  2024. exit;
  2025. // parse
  2026. ParseFilter(Value);
  2027. // call dataset method
  2028. inherited;
  2029. // refilter dataset if filtered
  2030. if (FDbfFile <> nil) and Filtered then Refresh;
  2031. end;
  2032. procedure TDbf.SetFiltered(Value: Boolean); {override;}
  2033. begin
  2034. if Value = Filtered then
  2035. exit;
  2036. // pass on to ancestor
  2037. inherited;
  2038. // only refresh if active
  2039. if FCursor <> nil then
  2040. Refresh;
  2041. end;
  2042. procedure TDbf.SetFilePath(const Value: string);
  2043. begin
  2044. CheckInactive;
  2045. FRelativePath := Value;
  2046. if Length(FRelativePath) > 0 then
  2047. FRelativePath := IncludeTrailingPathDelimiter(FRelativePath);
  2048. if IsFullFilePath(Value) then
  2049. begin
  2050. FAbsolutePath := IncludeTrailingPathDelimiter(Value);
  2051. end else begin
  2052. FAbsolutePath := GetCompletePath(DbfBasePath(), FRelativePath);
  2053. end;
  2054. end;
  2055. procedure TDbf.SetTableName(const s: string);
  2056. var
  2057. lPath: string;
  2058. begin
  2059. FTableName := ExtractFileName(s);
  2060. lPath := ExtractFilePath(s);
  2061. if (Length(lPath) > 0) then
  2062. FilePath := lPath;
  2063. // force IDE to reread fielddefs when a different file is opened
  2064. {$ifdef SUPPORT_FIELDDEFS_UPDATED}
  2065. FieldDefs.Updated := false;
  2066. {$else}
  2067. // TODO ... ??
  2068. {$endif}
  2069. end;
  2070. procedure TDbf.SetDbfIndexDefs(const Value: TDbfIndexDefs);
  2071. begin
  2072. FIndexDefs.Assign(Value);
  2073. end;
  2074. procedure TDbf.SetLanguageID(NewID: Byte);
  2075. begin
  2076. CheckInactive;
  2077. FLanguageID := NewID;
  2078. end;
  2079. procedure TDbf.SetTableLevel(const NewLevel: Integer);
  2080. begin
  2081. if NewLevel <> FTableLevel then
  2082. begin
  2083. // check validity
  2084. if not ((NewLevel = 3) or (NewLevel = 4) or (NewLevel = 7) or (NewLevel = 25)) then
  2085. exit;
  2086. // can only assign tablelevel if table is closed
  2087. CheckInactive;
  2088. FTableLevel := NewLevel;
  2089. end;
  2090. end;
  2091. function TDbf.GetIndexName: string;
  2092. begin
  2093. Result := FIndexName;
  2094. end;
  2095. function TDbf.CompareBookmarks(Bookmark1, Bookmark2: TBookmark): Integer;
  2096. const
  2097. RetCodes: array[Boolean, Boolean] of ShortInt = ((2,-1),(1,0));
  2098. var
  2099. b1,b2: Integer;
  2100. begin
  2101. // Check for uninitialized bookmarks
  2102. Result := RetCodes[Bookmark1 = nil, Bookmark2 = nil];
  2103. if (Result = 2) then
  2104. begin
  2105. b1 := PInteger(Bookmark1)^;
  2106. b2 := PInteger(Bookmark2)^;
  2107. if b1 < b2 then Result := -1
  2108. else if b1 > b2 then Result := 1
  2109. else Result := 0;
  2110. end;
  2111. end;
  2112. function TDbf.GetVersion: string;
  2113. begin
  2114. Result := Format('%d.%02d', [TDBF_MAJOR_VERSION, TDBF_MINOR_VERSION]);
  2115. end;
  2116. procedure TDbf.SetVersion(const S: string);
  2117. begin
  2118. // What an idea...
  2119. end;
  2120. function TDbf.ParseIndexName(const AIndexName: string): string;
  2121. begin
  2122. // if no ext, then it is a MDX tag, get complete only if it is a filename
  2123. // MDX: get first 10 characters only
  2124. if Length(ExtractFileExt(AIndexName)) > 0 then
  2125. Result := GetCompleteFileName(FAbsolutePath, AIndexName)
  2126. else
  2127. Result := AIndexName;
  2128. end;
  2129. procedure TDbf.RegenerateIndexes;
  2130. begin
  2131. CheckBrowseMode;
  2132. FDbfFile.RegenerateIndexes;
  2133. end;
  2134. {$ifdef SUPPORT_DEFAULT_PARAMS}
  2135. procedure TDbf.AddIndex(const AIndexName, AFields: String; Options: TIndexOptions; const DescFields: String='');
  2136. {$else}
  2137. procedure TDbf.AddIndex(const AIndexName, AFields: String; Options: TIndexOptions);
  2138. {$endif}
  2139. var
  2140. lIndexFileName: string;
  2141. begin
  2142. CheckActive;
  2143. lIndexFileName := ParseIndexName(AIndexName);
  2144. FDbfFile.OpenIndex(lIndexFileName, AFields, true, Options);
  2145. // refresh our indexdefs
  2146. InternalInitFieldDefs;
  2147. end;
  2148. procedure TDbf.SetIndexName(AIndexName: string);
  2149. var
  2150. lRecNo: Integer;
  2151. begin
  2152. FIndexName := AIndexName;
  2153. if FDbfFile = nil then
  2154. exit;
  2155. // get accompanying index file
  2156. AIndexName := ParseIndexName(Trim(AIndexName));
  2157. FIndexFile := FDbfFile.GetIndexByName(AIndexName);
  2158. // store current lRecNo
  2159. if FCursor = nil then
  2160. begin
  2161. lRecNo := 1;
  2162. end else begin
  2163. UpdateCursorPos;
  2164. lRecNo := FCursor.PhysicalRecNo;
  2165. end;
  2166. // select new cursor
  2167. FreeAndNil(FCursor);
  2168. if FIndexFile <> nil then
  2169. begin
  2170. FCursor := TIndexCursor.Create(FIndexFile);
  2171. // select index
  2172. FIndexFile.IndexName := AIndexName;
  2173. // check if can activate master link
  2174. CheckMasterRange;
  2175. end else begin
  2176. FCursor := TDbfCursor.Create(FDbfFile);
  2177. FIndexName := EmptyStr;
  2178. end;
  2179. // reset previous lRecNo
  2180. FCursor.PhysicalRecNo := lRecNo;
  2181. // refresh records
  2182. if State = dsBrowse then
  2183. Resync([]);
  2184. // warn user if selecting non-existing index
  2185. if (FCursor = nil) and (AIndexName <> EmptyStr) then
  2186. raise EDbfError.CreateFmt(STRING_INDEX_NOT_EXIST, [AIndexName]);
  2187. end;
  2188. function TDbf.GetIndexFieldNames: string;
  2189. var
  2190. lIndexDef: TDbfIndexDef;
  2191. begin
  2192. lIndexDef := FIndexDefs.GetIndexByName(IndexName);
  2193. if lIndexDef = nil then
  2194. Result := EmptyStr
  2195. else
  2196. Result := lIndexDef.SortField;
  2197. end;
  2198. procedure TDbf.SetIndexFieldNames(const Value: string);
  2199. var
  2200. lIndexDef: TDbfIndexDef;
  2201. begin
  2202. // Exception if index not found?
  2203. lIndexDef := FIndexDefs.GetIndexByField(Value);
  2204. if lIndexDef = nil then
  2205. IndexName := EmptyStr
  2206. else
  2207. IndexName := lIndexDef.IndexFile;
  2208. end;
  2209. procedure TDbf.DeleteIndex(const AIndexName: string);
  2210. var
  2211. lIndexFileName: string;
  2212. begin
  2213. // extract absolute path if NDX file
  2214. lIndexFileName := ParseIndexName(AIndexName);
  2215. // try to delete index
  2216. FDbfFile.DeleteIndex(lIndexFileName);
  2217. // refresh index defs
  2218. InternalInitFieldDefs;
  2219. end;
  2220. procedure TDbf.OpenIndexFile(IndexFile: string);
  2221. var
  2222. lIndexFileName: string;
  2223. begin
  2224. CheckActive;
  2225. // make absolute path
  2226. lIndexFileName := GetCompleteFileName(FAbsolutePath, IndexFile);
  2227. // open index
  2228. FDbfFile.OpenIndex(lIndexFileName, '', false, []);
  2229. end;
  2230. procedure TDbf.CloseIndexFile(const AIndexName: string);
  2231. var
  2232. lIndexFileName: string;
  2233. begin
  2234. CheckActive;
  2235. // make absolute path
  2236. lIndexFileName := GetCompleteFileName(FAbsolutePath, AIndexName);
  2237. // close this index
  2238. FDbfFile.CloseIndex(lIndexFileName);
  2239. end;
  2240. procedure TDbf.RepageIndexFile(const AIndexFile: string);
  2241. begin
  2242. if FDbfFile <> nil then
  2243. FDbfFile.RepageIndex(ParseIndexName(AIndexFile));
  2244. end;
  2245. procedure TDbf.CompactIndexFile(const AIndexFile: string);
  2246. begin
  2247. if FDbfFile <> nil then
  2248. FDbfFile.CompactIndex(ParseIndexName(AIndexFile));
  2249. end;
  2250. procedure TDbf.GetFileNames(Strings: TStrings; Files: TDbfFileNames);
  2251. var
  2252. I: Integer;
  2253. begin
  2254. Strings.Clear;
  2255. if FDbfFile <> nil then
  2256. begin
  2257. if dfDbf in Files then
  2258. Strings.Add(FDbfFile.FileName);
  2259. if (dfMemo in Files) and (FDbfFile.MemoFile <> nil) then
  2260. Strings.Add(FDbfFile.MemoFile.FileName);
  2261. if dfIndex in Files then
  2262. for I := 0 to Pred(FDbfFile.IndexFiles.Count) do
  2263. Strings.Add(TPagedFile(FDbfFile.IndexFiles.Items[I]).FileName);
  2264. end else
  2265. Strings.Add(IncludeTrailingPathDelimiter(FilePathFull) + TableName);
  2266. end;
  2267. {$ifdef SUPPORT_DEFAULT_PARAMS}
  2268. function TDbf.GetFileNames(Files: TDbfFileNames (* = [dfDbf] *) ): string;
  2269. {$else}
  2270. function TDbf.GetFileNamesString(Files: TDbfFileNames ): string;
  2271. {$endif}
  2272. var
  2273. sl: TStrings;
  2274. begin
  2275. sl := TStringList.Create;
  2276. try
  2277. GetFileNames(sl, Files);
  2278. Result := sl.Text;
  2279. finally
  2280. sl.Free;
  2281. end;
  2282. end;
  2283. procedure TDbf.GetIndexNames(Strings: TStrings);
  2284. begin
  2285. CheckActive;
  2286. Strings.Assign(DbfFile.IndexNames)
  2287. end;
  2288. procedure TDbf.GetAllIndexFiles(Strings: TStrings);
  2289. var
  2290. SR: TSearchRec;
  2291. begin
  2292. CheckActive;
  2293. Strings.Clear;
  2294. if SysUtils.FindFirst(IncludeTrailingPathDelimiter(ExtractFilePath(FDbfFile.FileName))
  2295. + '*.NDX', faAnyFile, SR) = 0 then
  2296. begin
  2297. repeat
  2298. Strings.Add(SR.Name);
  2299. until SysUtils.FindNext(SR)<>0;
  2300. SysUtils.FindClose(SR);
  2301. end;
  2302. end;
  2303. function TDbf.GetPhysicalRecNo: Integer;
  2304. var
  2305. pBuffer: pointer;
  2306. begin
  2307. // check if active, test state: if inserting, then -1
  2308. if (FCursor <> nil) and (State <> dsInsert) then
  2309. begin
  2310. if State = dsCalcFields then
  2311. pBuffer := CalcBuffer
  2312. else
  2313. pBuffer := ActiveBuffer;
  2314. Result := pDbfRecord(pBuffer)^.BookmarkData.PhysicalRecNo;
  2315. end else
  2316. Result := -1;
  2317. end;
  2318. procedure TDbf.SetPhysicalRecNo(const NewRecNo: Integer);
  2319. begin
  2320. // editing?
  2321. CheckBrowseMode;
  2322. DoBeforeScroll;
  2323. FCursor.PhysicalRecNo := NewRecNo;
  2324. CursorPosChanged;
  2325. Resync([]);
  2326. DoAfterScroll;
  2327. end;
  2328. function TDbf.GetDbfFieldDefs: TDbfFieldDefs;
  2329. begin
  2330. if FDbfFile <> nil then
  2331. Result := FDbfFile.FieldDefs
  2332. else
  2333. Result := nil;
  2334. end;
  2335. procedure TDbf.SetShowDeleted(Value: Boolean);
  2336. begin
  2337. // test if changed
  2338. if Value <> FShowDeleted then
  2339. begin
  2340. // store new value
  2341. FShowDeleted := Value;
  2342. // refresh view only if active
  2343. if FCursor <> nil then
  2344. Refresh;
  2345. end;
  2346. end;
  2347. function TDbf.IsDeleted: Boolean;
  2348. var
  2349. src: PChar;
  2350. begin
  2351. src := GetCurrentBuffer;
  2352. IsDeleted := (src=nil) or (src^ = '*')
  2353. end;
  2354. procedure TDbf.Undelete;
  2355. var
  2356. src: PChar;
  2357. begin
  2358. if State <> dsEdit then
  2359. inherited Edit;
  2360. // get active buffer
  2361. src := GetCurrentBuffer;
  2362. if (src <> nil) and (src^ = '*') then
  2363. begin
  2364. // notify indexes record is about to be recalled
  2365. FDbfFile.RecordRecalled(FCursor.PhysicalRecNo, src);
  2366. // recall record
  2367. src^ := ' ';
  2368. FDbfFile.WriteRecord(FCursor.PhysicalRecNo, src);
  2369. end;
  2370. end;
  2371. procedure TDbf.CancelRange;
  2372. begin
  2373. if FIndexFile = nil then
  2374. exit;
  2375. // disable current range if any
  2376. FIndexFile.CancelRange;
  2377. // reretrieve previous and next records
  2378. Refresh;
  2379. end;
  2380. procedure TDbf.SetRangeBuffer(LowRange: PChar; HighRange: PChar);
  2381. begin
  2382. if FIndexFile = nil then
  2383. exit;
  2384. FIndexFile.SetRange(LowRange, HighRange);
  2385. // go to first in this range
  2386. if Active then
  2387. inherited First;
  2388. end;
  2389. {$ifdef SUPPORT_VARIANTS}
  2390. procedure TDbf.SetRange(LowRange: Variant; HighRange: Variant; KeyIsANSI: boolean);
  2391. var
  2392. LowBuf, HighBuf: array[0..100] of Char;
  2393. begin
  2394. if (FIndexFile = nil) or VarIsNull(LowRange) or VarIsNull(HighRange) then
  2395. exit;
  2396. // convert variants to index key type
  2397. if (TIndexCursor(FCursor).VariantToBuffer(LowRange, @LowBuf[0]) = etString) and KeyIsANSI then
  2398. Translate(@LowBuf[0], @LowBuf[0], true);
  2399. if (TIndexCursor(FCursor).VariantToBuffer(HighRange, @HighBuf[0]) = etString) and KeyIsANSI then
  2400. Translate(@HighBuf[0], @HighBuf[0], true);
  2401. SetRangeBuffer(@LowBuf[0], @HighBuf[0]);
  2402. end;
  2403. {$endif}
  2404. procedure TDbf.SetRangePChar(LowRange: PChar; HighRange: PChar; KeyIsANSI: boolean);
  2405. var
  2406. LowBuf, HighBuf: array [0..100] of Char;
  2407. LowPtr, HighPtr: PChar;
  2408. begin
  2409. if FIndexFile = nil then
  2410. exit;
  2411. // convert to pchars
  2412. if KeyIsANSI then
  2413. begin
  2414. Translate(LowRange, @LowBuf[0], true);
  2415. Translate(HighRange, @HighBuf[0], true);
  2416. LowRange := @LowBuf[0];
  2417. HighRange := @HighBuf[0];
  2418. end;
  2419. LowPtr := TIndexCursor(FCursor).CheckUserKey(LowRange, @LowBuf[0]);
  2420. HighPtr := TIndexCursor(FCursor).CheckUserKey(HighRange, @HighBuf[0]);
  2421. SetRangeBuffer(LowPtr, HighPtr);
  2422. end;
  2423. procedure TDbf.ExtractKey(KeyBuffer: PChar);
  2424. begin
  2425. if FIndexFile <> nil then
  2426. StrCopy(FIndexFile.ExtractKeyFromBuffer(GetCurrentBuffer), KeyBuffer)
  2427. else
  2428. KeyBuffer[0] := #0;
  2429. end;
  2430. function TDbf.GetKeySize: Integer;
  2431. begin
  2432. if FCursor is TIndexCursor then
  2433. Result := TIndexCursor(FCursor).IndexFile.KeyLen
  2434. else
  2435. Result := 0;
  2436. end;
  2437. {$ifdef SUPPORT_VARIANTS}
  2438. function TDbf.SearchKey(Key: Variant; SearchType: TSearchKeyType; KeyIsANSI: boolean): Boolean;
  2439. var
  2440. TempBuffer: array [0..100] of Char;
  2441. begin
  2442. if (FIndexFile = nil) or VarIsNull(Key) then
  2443. begin
  2444. Result := false;
  2445. exit;
  2446. end;
  2447. // FIndexFile <> nil -> FCursor as TIndexCursor <> nil
  2448. if (TIndexCursor(FCursor).VariantToBuffer(Key, @TempBuffer[0]) = etString) and KeyIsANSI then
  2449. Translate(@TempBuffer[0], @TempBuffer[0], true);
  2450. Result := SearchKeyBuffer(@TempBuffer[0], SearchType);
  2451. end;
  2452. {$endif}
  2453. function TDbf.PrepareKey(Buffer: Pointer; BufferType: TExpressionType): PChar;
  2454. begin
  2455. if FIndexFile = nil then
  2456. begin
  2457. Result := nil;
  2458. exit;
  2459. end;
  2460. Result := TIndexCursor(FCursor).IndexFile.PrepareKey(Buffer, BufferType);
  2461. end;
  2462. function TDbf.SearchKeyPChar(Key: PChar; SearchType: TSearchKeyType; KeyIsANSI: boolean): Boolean;
  2463. var
  2464. StringBuf: array [0..100] of Char;
  2465. begin
  2466. if FCursor = nil then
  2467. begin
  2468. Result := false;
  2469. exit;
  2470. end;
  2471. if KeyIsANSI then
  2472. begin
  2473. Translate(Key, @StringBuf[0], true);
  2474. Key := @StringBuf[0];
  2475. end;
  2476. Result := SearchKeyBuffer(TIndexCursor(FCursor).CheckUserKey(Key, @StringBuf[0]), SearchType);
  2477. end;
  2478. function TDbf.SearchKeyBuffer(Buffer: PChar; SearchType: TSearchKeyType): Boolean;
  2479. var
  2480. matchRes: Integer;
  2481. begin
  2482. if FIndexFile = nil then
  2483. begin
  2484. Result := false;
  2485. exit;
  2486. end;
  2487. CheckBrowseMode;
  2488. Result := FIndexFile.SearchKey(Buffer, SearchType);
  2489. { if found, then retrieve new current record }
  2490. if Result then
  2491. begin
  2492. CursorPosChanged;
  2493. Resync([]);
  2494. UpdateCursorPos;
  2495. { recno could have been changed due to deleted record, check if still matches }
  2496. matchRes := TIndexCursor(FCursor).IndexFile.MatchKey(Buffer);
  2497. case SearchType of
  2498. stEqual: Result := matchRes = 0;
  2499. stGreater: Result := (not Eof) and (matchRes < 0);
  2500. stGreaterEqual: Result := (not Eof) and (matchRes <= 0);
  2501. end;
  2502. end;
  2503. end;
  2504. procedure TDbf.UpdateIndexDefs;
  2505. begin
  2506. FieldDefs.Update;
  2507. end;
  2508. // A hack to upgrade method visibility, only necessary for FPC 1.0.x
  2509. {$ifdef VER1_0}
  2510. procedure TDbf.DataEvent(Event: TDataEvent; Info: Longint);
  2511. begin
  2512. inherited;
  2513. end;
  2514. {$endif}
  2515. { Master / Detail }
  2516. procedure TDbf.CheckMasterRange;
  2517. begin
  2518. if FMasterLink.Active and FMasterLink.ValidExpression and (FIndexFile <> nil) then
  2519. UpdateRange;
  2520. end;
  2521. procedure TDbf.UpdateRange;
  2522. var
  2523. fieldsVal: PChar;
  2524. tempBuffer: array[0..300] of char;
  2525. begin
  2526. fieldsVal := FMasterLink.FieldsVal;
  2527. if FMasterLink.KeyTranslation then
  2528. begin
  2529. FMasterLink.DataSet.Translate(fieldsVal, @tempBuffer[0], false);
  2530. fieldsVal := @tempBuffer[0];
  2531. Translate(fieldsVal, fieldsVal, true);
  2532. end;
  2533. fieldsVal := TIndexCursor(FCursor).IndexFile.PrepareKey(fieldsVal, FMasterLink.Parser.ResultType);
  2534. SetRangeBuffer(fieldsVal, fieldsVal);
  2535. end;
  2536. procedure TDbf.MasterChanged(Sender: TObject);
  2537. begin
  2538. CheckBrowseMode;
  2539. CheckMasterRange;
  2540. end;
  2541. procedure TDbf.MasterDisabled(Sender: TObject);
  2542. begin
  2543. CancelRange;
  2544. end;
  2545. function TDbf.GetDataSource: TDataSource;
  2546. begin
  2547. Result := FMasterLink.DataSource;
  2548. end;
  2549. procedure TDbf.SetDataSource(Value: TDataSource);
  2550. begin
  2551. {$ifndef FPC}
  2552. if IsLinkedTo(Value) then
  2553. begin
  2554. {$ifdef DELPHI_4}
  2555. DatabaseError(SCircularDataLink, Self);
  2556. {$else}
  2557. DatabaseError(SCircularDataLink);
  2558. {$endif}
  2559. end;
  2560. {$endif}
  2561. FMasterLink.DataSource := Value;
  2562. end;
  2563. function TDbf.GetMasterFields: string;
  2564. begin
  2565. Result := FMasterLink.FieldNames;
  2566. end;
  2567. procedure TDbf.SetMasterFields(const Value: string);
  2568. begin
  2569. FMasterLink.FieldNames := Value;
  2570. end;
  2571. //==========================================================
  2572. //============ TDbfIndexDefs
  2573. //==========================================================
  2574. constructor TDbfIndexDefs.Create(AOwner: TDbf);
  2575. begin
  2576. inherited Create(TDbfIndexDef);
  2577. FOwner := AOwner;
  2578. end;
  2579. function TDbfIndexDefs.Add: TDbfIndexDef;
  2580. begin
  2581. Result := TDbfIndexDef(inherited Add);
  2582. end;
  2583. procedure TDbfIndexDefs.SetItem(N: Integer; Value: TDbfIndexDef);
  2584. begin
  2585. inherited SetItem(N, Value);
  2586. end;
  2587. function TDbfIndexDefs.GetItem(N: Integer): TDbfIndexDef;
  2588. begin
  2589. Result := TDbfIndexDef(inherited GetItem(N));
  2590. end;
  2591. function TDbfIndexDefs.GetOwner: tpersistent;
  2592. begin
  2593. Result := FOwner;
  2594. end;
  2595. function TDbfIndexDefs.GetIndexByName(const Name: string): TDbfIndexDef;
  2596. var
  2597. I: Integer;
  2598. lIndex: TDbfIndexDef;
  2599. begin
  2600. for I := 0 to Count-1 do
  2601. begin
  2602. lIndex := Items[I];
  2603. if lIndex.IndexFile = Name then
  2604. begin
  2605. Result := lIndex;
  2606. exit;
  2607. end
  2608. end;
  2609. Result := nil;
  2610. end;
  2611. function TDbfIndexDefs.GetIndexByField(const Name: string): TDbfIndexDef;
  2612. var
  2613. lIndex: TDbfIndexDef;
  2614. searchStr: string;
  2615. i: integer;
  2616. begin
  2617. searchStr := AnsiUpperCase(Trim(Name));
  2618. Result := nil;
  2619. if searchStr = EmptyStr then
  2620. exit;
  2621. for I := 0 to Count-1 do
  2622. begin
  2623. lIndex := Items[I];
  2624. if AnsiUpperCase(Trim(lIndex.SortField)) = searchStr then
  2625. begin
  2626. Result := lIndex;
  2627. exit;
  2628. end
  2629. end;
  2630. end;
  2631. procedure TDbfIndexDefs.Update;
  2632. begin
  2633. if Assigned(FOwner) then
  2634. FOwner.UpdateIndexDefs;
  2635. end;
  2636. //==========================================================
  2637. //============ TDbfMasterLink
  2638. //==========================================================
  2639. constructor TDbfMasterLink.Create(ADataSet: TDbf);
  2640. begin
  2641. inherited Create;
  2642. FDetailDataSet := ADataSet;
  2643. FParser := TDbfParser.Create(nil);
  2644. FValidExpression := false;
  2645. end;
  2646. destructor TDbfMasterLink.Destroy;
  2647. begin
  2648. FParser.Free;
  2649. inherited;
  2650. end;
  2651. procedure TDbfMasterLink.ActiveChanged;
  2652. begin
  2653. if Active and (FFieldNames <> EmptyStr) then
  2654. begin
  2655. FValidExpression := false;
  2656. FParser.DbfFile := (DataSet as TDbf).DbfFile;
  2657. FParser.ParseExpression(FFieldNames);
  2658. FKeyTranslation := TDbfFile(FParser.DbfFile).UseCodePage <>
  2659. FDetailDataSet.DbfFile.UseCodePage;
  2660. FValidExpression := true;
  2661. end else begin
  2662. FParser.ClearExpressions;
  2663. FValidExpression := false;
  2664. end;
  2665. if FDetailDataSet.Active and not (csDestroying in FDetailDataSet.ComponentState) then
  2666. if Active then
  2667. begin
  2668. if Assigned(FOnMasterChange) then FOnMasterChange(Self);
  2669. end else
  2670. if Assigned(FOnMasterDisable) then FOnMasterDisable(Self);
  2671. end;
  2672. procedure TDbfMasterLink.CheckBrowseMode;
  2673. begin
  2674. if FDetailDataSet.Active then
  2675. FDetailDataSet.CheckBrowseMode;
  2676. end;
  2677. procedure TDbfMasterLink.LayoutChanged;
  2678. begin
  2679. ActiveChanged;
  2680. end;
  2681. procedure TDbfMasterLink.RecordChanged(Field: TField);
  2682. begin
  2683. if (DataSource.State <> dsSetKey) and FDetailDataSet.Active and Assigned(FOnMasterChange) then
  2684. FOnMasterChange(Self);
  2685. end;
  2686. procedure TDbfMasterLink.SetFieldNames(const Value: string);
  2687. begin
  2688. if FFieldNames <> Value then
  2689. begin
  2690. FFieldNames := Value;
  2691. ActiveChanged;
  2692. end;
  2693. end;
  2694. function TDbfMasterLink.GetFieldsVal: PChar;
  2695. begin
  2696. Result := FParser.ExtractFromBuffer(@pDbfRecord(TDbf(DataSet).ActiveBuffer)^.DeletedFlag);
  2697. end;
  2698. ////////////////////////////////////////////////////////////////////////////
  2699. function ApplicationPath: string;
  2700. begin
  2701. Result := ExtractFilePath(ParamStr(0));
  2702. end;
  2703. ////////////////////////////////////////////////////////////////////////////
  2704. initialization
  2705. DbfBasePath := ApplicationPath;
  2706. end.