Skip to content

Commit 97e226a

Browse files
committed
Implement INDEXSEEK() function
1 parent 24e7581 commit 97e226a

3 files changed

Lines changed: 119 additions & 7 deletions

File tree

src/Runtime/XSharp.VFP.Tests/CommandTests.prg

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,60 @@ BEGIN NAMESPACE XSharp.VFP.Tests
167167
END TRY
168168
END TRY
169169
END METHOD
170+
171+
[Fact];
172+
METHOD TestIndexSeek() AS VOID
173+
VAR cOldDir := System.IO.Directory.GetCurrentDirectory()
174+
VAR oDir := System.IO.Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), ;
175+
"IndexSeekTest_" + Guid.NewGuid():ToString("N")))
176+
VAR cTempPath := oDir:FullName
177+
TRY
178+
SET DEFAULT TO (cTempPath)
179+
CREATE TABLE SeekTest (Id INT, Name C(10))
180+
INSERT INTO SeekTest VALUES(1, "Alpha")
181+
INSERT INTO SeekTest VALUES(2, "Beta")
182+
INSERT INTO SeekTest VALUES(3, "Gamma")
183+
INDEX ON Name TAG NameIdx
184+
// Found -> pointer must not move.
185+
GO TOP
186+
VAR nRec := RecNo()
187+
Assert.True(IndexSeek("Beta"))
188+
Assert.Equal((INT)nRec, (INT) RecNo())
189+
190+
// Not found -> pointer must not move
191+
nRec := RecNo()
192+
Assert.False(IndexSeek("Zeta"))
193+
Assert.Equal((INT)nRec, (INT) RecNo())
194+
195+
// Found -> pointer must move
196+
GO TOP
197+
Assert.True(IndexSeek("Beta", .T.))
198+
Assert.Equal("Beta", ALLTRIM(SeekTest.Name))
199+
200+
// Not found with lMovePointer = .T.
201+
Assert.False(IndexSeek("Zeta", .T.))
202+
203+
// With alias string -> pointer must not move
204+
GO TOP
205+
nRec := RecNo()
206+
Assert.True(IndexSeek("Gamma", .F., "SeekTest"))
207+
Assert.Equal((INT)nRec, (INT) RecNo())
208+
209+
// With workarea number -> pointer must not move
210+
GO TOP
211+
nRec := RecNo()
212+
Assert.True(IndexSeek("Alpha", .F., Select()))
213+
Assert.Equal((INT)nRec, (INT) RecNo())
214+
215+
// Non-existent area -> must return false
216+
Assert.False(IndexSeek("Alpha", .F., "NonExistent"))
217+
FINALLY
218+
XSharp.CoreDb.CloseAll()
219+
SET DEFAULT TO (cOldDir)
220+
System.IO.Directory.SetCurrentDirectory(cOldDir)
221+
TRY ; System.IO.Directory.Delete(cTempPath, TRUE) ; CATCH ; END TRY
222+
END TRY
223+
END METHOD
170224
END CLASS
171225

172226
END NAMESPACE

src/Runtime/XSharp.VFP/Cursors/DbFunctions.prg

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,68 @@ FUNCTION Target( nRelationshipNumber , uArea ) AS STRING CLIPPER
212212
[FoxProFunction("UNIQUE", FoxFunctionCategory.Database, FoxEngine.WorkArea, FoxFunctionStatus.Full, FoxCriticality.Medium)];
213213
FUNCTION Unique(uArea ) AS LOGIC CLIPPER
214214
RETURN _DoInArea(uArea, { => (LOGIC) DbOrderInfo(DBOI_UNIQUE , NIL, NIL) } , FALSE,__FUNCTION__,1)
215+
216+
/// <include file="VFPDocs.xml" path="Runtimefunctions/indexseek/*" />
217+
[FoxProFunction("INDEXSEEK", FoxFunctionCategory.Database, FoxEngine.WorkArea, FoxFunctionStatus.Full, FoxCriticality.High)];
218+
FUNCTION IndexSeek( eExpression , lMovePointer , uArea, uIndex) AS LOGIC CLIPPER
219+
@@Default(@lMovePointer, FALSE)
220+
221+
LOCAL nArea AS DWORD
222+
IF IsNil(uArea)
223+
nArea := RuntimeState.CurrentWorkarea
224+
ELSEIF IsString(uArea)
225+
nArea := RuntimeState.Workareas.FindAlias((STRING) uArea)
226+
ELSEIF IsNumeric(uArea)
227+
nArea := (DWORD) uArea
228+
ELSE
229+
RETURN FALSE
230+
ENDIF
231+
IF nArea == 0
232+
RETURN FALSE
233+
ENDIF
234+
235+
VAR nOldArea := RuntimeState.CurrentWorkarea
236+
VAR lResult := FALSE
237+
RuntimeState.CurrentWorkarea := nOldArea
238+
TRY
239+
VAR cOldOrder := ""
240+
VAR cOldBag := ""
241+
VAR lChangeOrder := FALSE
242+
243+
IF !IsNil(uIndex)
244+
cOldOrder := OrdName()
245+
cOldBag := ""
246+
OrdSetFocus(uIndex)
247+
lChangeOrder := TRUE
248+
ENDIF
249+
250+
LOCAL nOldRecno := 0 AS DWORD
251+
VAR lWasBof := FALSE
252+
253+
IF !lMovePointer
254+
nOldRecno := RecNo()
255+
lWasBof := Bof()
256+
ENDIF
257+
258+
lResult := DbSeek(eExpression)
259+
260+
IF !lMovePointer
261+
IF lWasBof
262+
DbGoTop()
263+
IF RecCount() > 0
264+
DbSkip(-1)
265+
ENDIF
266+
ELSE
267+
DbGoto(nOldRecno)
268+
ENDIF
269+
ENDIF
270+
271+
IF lChangeOrder
272+
OrdSetFocus(cOldOrder, cOldBag)
273+
ENDIF
274+
FINALLY
275+
RuntimeState.CurrentWorkarea := nOldArea
276+
END TRY
277+
278+
RETURN lResult
279+

src/Runtime/XSharp.VFP/ToDo-HI.prg

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ FUNCTION InDbc( cDatabaseObjectName, cType ) AS LOGIC
1212
THROW NotImplementedException{}
1313
// RETURN FALSE
1414

15-
/// <summary>-- todo --</summary>
16-
/// <include file="VFPDocs.xml" path="Runtimefunctions/indexseek/*" />
17-
[FoxProFunction("INDEXSEEK", FoxFunctionCategory.Database, FoxEngine.WorkArea, FoxFunctionStatus.Stub, FoxCriticality.High)];
18-
FUNCTION IndexSeek( eExpression , lMovePointer , uArea, uIndex) AS LOGIC
19-
THROW NotImplementedException{}
20-
// RETURN FALSE
21-
2215
/// <summary>-- todo --</summary>
2316
/// <include file="VFPDocs.xml" path="Runtimefunctions/inputbox/*" />
2417
[FoxProFunction("INPUTBOX", FoxFunctionCategory.UIAndWindow, FoxEngine.UI, FoxFunctionStatus.Stub, FoxCriticality.High)];

0 commit comments

Comments
 (0)