CDX Integration:
- IndexEngine interface: common for NTX Index and CDX Tag
- OrderListAdd: auto-detects .cdx/.ntx extension, opens CDX tags
- decodeCompoundLeaf: proper bit-packed tag directory decoding
(was stub falling through to scanCompoundLeaves with wrong names)
- CDX Tag: added KeyLen(), KeyExpr(), ForExpr(), IsDescending(), Close()
- CDX compound recNo = direct byte offset (not page number)
ORDSCOPE:
- SetScope/ClearScope/SetScopeTop/SetScopeBottom on DBFArea
- GoTopIndexed: seeks to scopeTop, validates within scopeBottom
- GoBottomIndexed: seeks to scopeBottom boundary
- SkipIndexed: stops at scope boundaries (top and bottom)
- OrdScope RTL function registered (nScope: 0=TOP, 1=BOTTOM)
- scopeKeyFromValue: converts Value to padded key bytes
Index Order Management:
- OrderListFocus: handles numeric order ("2" → order 2)
- SET ORDER TO n: gengo emits hbrt.NtoS for int-to-string conversion
- IndexOrd/OrdCount/OrdName/OrdKey: real implementations (were stubs)
- OrderCount/CurrentOrder/OrderName/OrderKeyExpr accessors on DBFArea
- ClearScope on order switch (prevents stale scope)
Cross-read test: Harbour-created CDX → Five reads, 20/20 items match:
NAME/CITY/ID seek, ORDSCOPE count, GoTop/GoBottom all identical
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
297 lines
5.8 KiB
Go
297 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"
|
|
)
|
|
|
|
// 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
|
|
idx.OrderListFocus(v.AsString())
|
|
} 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()
|
|
}
|