Five RDD engine now matches Harbour DBFNTX and DBFCDX byte-for-byte
in ordering, seek, navigation, and field access. Verified against
Harbour 3.2.0dev with a 281-line comparison test covering:
- Natural/NAME/CITY/AGE/SALARY/UPPER ordering
- SEEK (exact/not-found), GoTop/GoBottom per order
- DELETE/RECALL with SET DELETED
- CDX compound index read with 5 tags (BYNAME, BYCITY, BYAGE, BYSAL, BYUNAME)
- Reverse traversal
Fixes:
1. FIELD->NAME returned NIL
GetAliasField returned interface{} but runtime expected hbrt.Value,
so the type assertion in PushAliasField failed and pushed NIL.
- workarea.go: change return type to hbrt.Value, handle FIELD/_FIELD
as current-workarea alias, add SetAliasField
- gengo.go: emit SetAliasField() for alias->field := value in both
statement and expression contexts
2. OrdSetFocus(n) silently switched to natural order
v.AsString() returns "" for a numeric Value, so OrderListFocus("")
set current=-1.
- indexrtl.go: convert numeric param via fmt.Sprintf("%d", ...)
3. CDX compound tag order mismatched Harbour
Five decoded the structural B-tree which is alphabetical, but
Harbour sorts tags by TagBlock (file offset = creation order).
- cdx/cdx.go: sort tagEntries by offset ascending after decoding,
matching hb_cdxIndexLoadAvailTags in dbfcdx1.c
4. OutStd()/OutErr() not registered — caused panic on call
- hbrtl/console.go: add rtlOutStd/rtlOutErr implementations
- hbrtl/register.go: register OUTSTD and OUTERR
- analyzer.go: add OUTSTD/OUTERR to RTL known-functions
5. FIELD keyword triggered "undeclared variable" warnings
- analyzer.go: add FIELD, _FIELD, M, MEMVAR as builtin constants
Tests:
go test ./... — ALL PASS (17 packages)
FiveSql2 43/43 — 100%
compat_harbour 51/51 — 100%
Harbour diff — 0 lines differ (281-line comparison)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
298 lines
5.8 KiB
Go
298 lines
5.8 KiB
Go
// Copyright (c) 2026 Charles KWON OhJun (charleskwonohjun@gmail.com)
|
|
// All rights reserved.
|
|
|
|
// Index and database introspection RTL functions.
|
|
// Harbour: INDEXORD, INDEXKEY, ORDSETFOCUS, ORDCOUNT, ORDNAME, ORDKEY,
|
|
// ORDFOR, ORDSCOPE, DBORDERINFO, DBINFO, DBCREATE, RDDSETDEFAULT
|
|
|
|
package hbrtl
|
|
|
|
import (
|
|
"five/hbrt"
|
|
"five/hbrdd"
|
|
"five/hbrdd/dbf"
|
|
"fmt"
|
|
)
|
|
|
|
// INDEXORD() → nCurrentOrder (1-based, 0 = natural)
|
|
func IndexOrd(t *hbrt.Thread) {
|
|
t.Frame(0, 0)
|
|
defer t.EndProc()
|
|
wam := getWA(t)
|
|
if wam != nil {
|
|
if area := wam.Current(); area != nil {
|
|
if da, ok := area.(*dbf.DBFArea); ok {
|
|
t.RetInt(int64(da.CurrentOrder()))
|
|
return
|
|
}
|
|
}
|
|
}
|
|
t.RetInt(0)
|
|
}
|
|
|
|
// INDEXKEY([nOrder]) → cKeyExpression
|
|
func IndexKey(t *hbrt.Thread) {
|
|
nParams := t.ParamCount()
|
|
t.Frame(nParams, 0)
|
|
defer t.EndProc()
|
|
wam := getWA(t)
|
|
if wam != nil {
|
|
if area := wam.Current(); area != nil {
|
|
if da, ok := area.(*dbf.DBFArea); ok {
|
|
n := da.CurrentOrder()
|
|
if nParams >= 1 && !t.Local(1).IsNil() {
|
|
n = t.Local(1).AsInt()
|
|
}
|
|
t.RetString(da.OrderKeyExpr(n))
|
|
return
|
|
}
|
|
}
|
|
}
|
|
t.RetString("")
|
|
}
|
|
|
|
// ORDSETFOCUS([nOrder|cTag [, cBagName]]) → nOldOrder
|
|
func OrdSetFocus(t *hbrt.Thread) {
|
|
nParams := t.ParamCount()
|
|
t.Frame(nParams, 0)
|
|
defer t.EndProc()
|
|
wam := getWA(t)
|
|
if wam == nil {
|
|
t.RetInt(0)
|
|
return
|
|
}
|
|
area := wam.Current()
|
|
if area == nil {
|
|
t.RetInt(0)
|
|
return
|
|
}
|
|
da, isDa := area.(*dbf.DBFArea)
|
|
oldOrd := 0
|
|
if isDa {
|
|
oldOrd = da.CurrentOrder()
|
|
}
|
|
if nParams >= 1 && !t.Local(1).IsNil() {
|
|
if idx, ok := area.(hbrdd.Indexer); ok {
|
|
v := t.Local(1)
|
|
if v.IsNumeric() {
|
|
// SET ORDER TO n — convert number to digit string for OrderListFocus
|
|
idx.OrderListFocus(fmt.Sprintf("%d", v.AsNumInt()))
|
|
} else {
|
|
idx.OrderListFocus(v.AsString())
|
|
}
|
|
}
|
|
}
|
|
t.RetInt(int64(oldOrd))
|
|
}
|
|
|
|
// ORDCOUNT([cBagName]) → nOrders
|
|
func OrdCount(t *hbrt.Thread) {
|
|
nParams := t.ParamCount()
|
|
t.Frame(nParams, 0)
|
|
defer t.EndProc()
|
|
wam := getWA(t)
|
|
if wam != nil {
|
|
if area := wam.Current(); area != nil {
|
|
if da, ok := area.(*dbf.DBFArea); ok {
|
|
t.RetInt(int64(da.IndexCount()))
|
|
return
|
|
}
|
|
}
|
|
}
|
|
t.RetInt(0)
|
|
}
|
|
|
|
// ORDNAME([nOrder [, cBagName]]) → cTagName
|
|
func OrdName(t *hbrt.Thread) {
|
|
nParams := t.ParamCount()
|
|
t.Frame(nParams, 0)
|
|
defer t.EndProc()
|
|
wam := getWA(t)
|
|
if wam != nil {
|
|
if area := wam.Current(); area != nil {
|
|
if da, ok := area.(*dbf.DBFArea); ok {
|
|
n := da.CurrentOrder()
|
|
if nParams >= 1 && !t.Local(1).IsNil() {
|
|
n = t.Local(1).AsInt()
|
|
}
|
|
t.RetString(da.OrderName(n))
|
|
return
|
|
}
|
|
}
|
|
}
|
|
t.RetString("")
|
|
}
|
|
|
|
// ORDKEY([nOrder [, cBagName]]) → cKeyExpression
|
|
func OrdKey(t *hbrt.Thread) {
|
|
nParams := t.ParamCount()
|
|
t.Frame(nParams, 0)
|
|
defer t.EndProc()
|
|
wam := getWA(t)
|
|
if wam != nil {
|
|
if area := wam.Current(); area != nil {
|
|
if da, ok := area.(*dbf.DBFArea); ok {
|
|
n := da.CurrentOrder()
|
|
if nParams >= 1 && !t.Local(1).IsNil() {
|
|
n = t.Local(1).AsInt()
|
|
}
|
|
t.RetString(da.OrderKeyExpr(n))
|
|
return
|
|
}
|
|
}
|
|
}
|
|
t.RetString("")
|
|
}
|
|
|
|
// ORDFOR([nOrder [, cBagName]]) → cForExpression
|
|
func OrdFor(t *hbrt.Thread) {
|
|
nParams := t.ParamCount()
|
|
t.Frame(nParams, 0)
|
|
defer t.EndProc()
|
|
// TODO: return FOR expression from index
|
|
t.RetString("")
|
|
}
|
|
|
|
// ORDSCOPE(nScope [, xValue]) → xOldValue
|
|
// nScope: 0 = TOPSCOPE, 1 = BOTTOMSCOPE
|
|
// If xValue omitted, returns current scope. If xValue given, sets scope and returns old.
|
|
// Harbour: TOPSCOPE = 0, BOTTOMSCOPE = 1
|
|
func OrdScope(t *hbrt.Thread) {
|
|
nParams := t.ParamCount()
|
|
t.Frame(nParams, 0)
|
|
defer t.EndProc()
|
|
|
|
wam := getWA(t)
|
|
if wam == nil {
|
|
t.RetNil()
|
|
return
|
|
}
|
|
area := wam.Current()
|
|
if area == nil {
|
|
t.RetNil()
|
|
return
|
|
}
|
|
da, ok := area.(*dbf.DBFArea)
|
|
if !ok {
|
|
t.RetNil()
|
|
return
|
|
}
|
|
|
|
nScope := 0
|
|
if nParams >= 1 {
|
|
nScope = t.Local(1).AsInt()
|
|
}
|
|
|
|
// Get old scope value
|
|
var oldScope []byte
|
|
if nScope == 0 {
|
|
oldScope = da.GetScopeTop()
|
|
} else {
|
|
oldScope = da.GetScopeBottom()
|
|
}
|
|
|
|
if oldScope != nil {
|
|
t.PushString(string(oldScope))
|
|
} else {
|
|
t.PushNil()
|
|
}
|
|
|
|
// Set new scope if value provided
|
|
if nParams >= 2 {
|
|
val := t.Local(2)
|
|
if val.IsNil() {
|
|
if nScope == 0 {
|
|
da.ClearScopeTop()
|
|
} else {
|
|
da.ClearScopeBottom()
|
|
}
|
|
} else {
|
|
if nScope == 0 {
|
|
da.SetScopeTop(val)
|
|
} else {
|
|
da.SetScopeBottom(val)
|
|
}
|
|
}
|
|
}
|
|
|
|
t.RetValue()
|
|
}
|
|
|
|
// DBORDERINFO(nInfoType [, cBagName [, nOrder [, xNewSetting]]]) → xInfo
|
|
func DbOrderInfo(t *hbrt.Thread) {
|
|
nParams := t.ParamCount()
|
|
t.Frame(nParams, 0)
|
|
defer t.EndProc()
|
|
// TODO: implement full DBORDERINFO
|
|
t.RetNil()
|
|
}
|
|
|
|
// DBINFO(nInfoType [, xNewSetting]) → xInfo
|
|
func DbInfo(t *hbrt.Thread) {
|
|
nParams := t.ParamCount()
|
|
t.Frame(nParams, 0)
|
|
defer t.EndProc()
|
|
t.RetNil()
|
|
}
|
|
|
|
// ORDINFO(nInfoType [, cOrder]) → xInfo
|
|
func OrdInfo(t *hbrt.Thread) {
|
|
nParams := t.ParamCount()
|
|
t.Frame(nParams, 0)
|
|
defer t.EndProc()
|
|
t.RetNil()
|
|
}
|
|
|
|
// RDDSETDEFAULT([cDriver]) → cOldDriver
|
|
func RddSetDefault(t *hbrt.Thread) {
|
|
nParams := t.ParamCount()
|
|
t.Frame(nParams, 0)
|
|
defer t.EndProc()
|
|
t.RetString("DBFNTX")
|
|
}
|
|
|
|
// DBCREATE(cFile, aStruct [, cDriver]) → NIL
|
|
func DbCreate(t *hbrt.Thread) {
|
|
nParams := t.ParamCount()
|
|
t.Frame(nParams, 0)
|
|
defer t.EndProc()
|
|
|
|
cFile := t.Local(1).AsString()
|
|
aStruct := t.Local(2)
|
|
cDriver := "DBFNTX"
|
|
if nParams >= 3 && !t.Local(3).IsNil() {
|
|
cDriver = t.Local(3).AsString()
|
|
}
|
|
|
|
if !aStruct.IsArray() {
|
|
t.RetNil()
|
|
return
|
|
}
|
|
|
|
arr := aStruct.AsArray()
|
|
fields := make([]hbrdd.FieldInfo, len(arr.Items))
|
|
for i, item := range arr.Items {
|
|
row := item.AsArray()
|
|
if row == nil || len(row.Items) < 4 {
|
|
continue
|
|
}
|
|
fields[i] = hbrdd.FieldInfo{
|
|
Name: row.Items[0].AsString(),
|
|
Type: row.Items[1].AsString()[0],
|
|
Len: row.Items[2].AsInt(),
|
|
Dec: row.Items[3].AsInt(),
|
|
}
|
|
}
|
|
|
|
drv, err := hbrdd.GetDriver(cDriver)
|
|
if err != nil {
|
|
t.RetNil()
|
|
return
|
|
}
|
|
drv.Create(hbrdd.CreateParams{
|
|
Path: cFile,
|
|
Fields: fields,
|
|
})
|
|
t.RetNil()
|
|
}
|