Files
five/hbrtl/missing_fivesql.go
CharlesKWON f4ed42556b checkpoint: season-wide bug fix campaign + infra
Cumulative season's silent-bug hunting (~62 fixes) across the FiveSql2
SQL engine, the Five compiler/runtime, and the hbrdd RDD layer. Saved
as a single checkpoint before refactoring the parser to delegate xBase
command translation to the preprocessor.

Highlights:

FiveSql2 engine (_FiveSql2/src/)
- prefix-glob index attach -> explicit convention (<table>_pk.ntx,
  <table>_uq.ntx, <table>.cdx) — fixes silent multi-row INSERT row-drop
- DROP/CREATE TABLE FErase chain extended (.cdx, .fsc, .fsv, .dbt, .fpt)
- COUNT(DISTINCT col) parsed + aggregated via hSeen hash
- UNION column-count mismatch returns SQL_ERR_GRAMMAR (was silent)
- DISTINCT + ORDER BY hidden-col leak fixed (trim before DISTINCT)
- Derived table FROM (SELECT...) + JOIN right-side derived
- Self-FK CASCADE depth 2+ via SqlGetSingleColPK pre-collect
- LAG/LEAD default arg uses SqlEvalRowExpr (handles -N const exprs)
- DATE literal round-trip validation (Feb 29 non-leap rejected)
- CREATE OR REPLACE VIEW; CREATE VIEW errors on already-exists
- AlterTable type dispatcher comma-wrapped (1-char type "A" no longer
  matches CHARACTER)

Compiler / runtime
- gengo: HB_ -> FV_ prefix on emitted Go function names (Five identity)
- gengo split: emit_block.go, emit_stmt.go, folding.go extracted
- parser/stmtreg.go nudges
- hbrt: debug TUI/CLI restructure (debugcmd, debugkey, termios_*),
  windows debug stubs collapsed
- thread/vm/value/class/pcinterp tightening from panic traces

RDD layer (hbrdd/)
- dbf: null bitmap support (null.go + null_test.go), mmap split
  (mmap_posix.go / mmap_windows.go), byte-level numeric parse
- ntx/cdx: windows mmap parity
- workarea + mem RDD: cross-area state-bleed fixes

RTL (hbrtl/)
- errorlog rewrite with platform-specific FD (errorlog_fd_unix /
  errorlog_fd_other)
- sqlscan, sqlhelpers, indexrtl, datetime extensions

Gates green at checkpoint:
- go test ./...        : PASS
- FiveSql2 SQL:1999    : 43/43
- Harbour compat       : 56/56

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-30 09:26:25 +09:00

125 lines
2.8 KiB
Go

// Copyright (c) 2026 Charles KWON OhJun (charleskwonohjun@gmail.com)
// All rights reserved.
// Missing RTL functions needed by FiveSql2 and other Harbour programs.
package hbrtl
import (
"five/hbrdd"
"five/hbrt"
"os"
"strings"
)
// hb_FileExists(cFile) → lExists
func HbFileExists(t *hbrt.Thread) {
t.Frame(1, 0)
defer t.EndProcFast()
fname := t.Local(1).AsString()
_, err := os.Stat(fname)
t.PushBool(err == nil)
t.RetValue()
}
// hb_Second(dTimestamp) → nSeconds (seconds portion of timestamp)
func HbSecond(t *hbrt.Thread) {
t.Frame(1, 0)
defer t.EndProcFast()
v := t.Local(1)
if v.IsTimestamp() {
ms := v.AsTimeMs()
secs := int64(ms/1000) % 60
t.RetInt(secs)
} else {
t.RetInt(0)
}
}
// hb_ATokens(cString [, cDelim]) → aTokens
// Splits string by delimiter (default: space/tab/newline)
func HbATokens(t *hbrt.Thread) {
nParams := t.ParamCount()
t.Frame(nParams, 0)
defer t.EndProcFast()
s := t.Local(1).AsString()
delim := " "
if nParams >= 2 && !t.Local(2).IsNil() {
delim = t.Local(2).AsString()
}
var parts []string
if delim == " " {
parts = strings.Fields(s)
} else {
parts = strings.Split(s, delim)
}
items := make([]hbrt.Value, len(parts))
for i, p := range parts {
items[i] = hbrt.MakeString(p)
}
t.PushValue(hbrt.MakeArrayFrom(items))
t.RetValue()
}
// hb_cdpSelect([cCodepage]) → cPrevCodepage
// Stub: Five uses UTF-8 internally, codepage selection is a no-op.
func HbCdpSelect(t *hbrt.Thread) {
nParams := t.ParamCount()
t.Frame(nParams, 0)
defer t.EndProcFast()
t.RetString("")
}
// Used() → lUsed — checks if current workarea is in use
func Used(t *hbrt.Thread) {
t.Frame(0, 0)
defer t.EndProcFast()
wam := getWA(t)
if wam == nil {
t.RetBool(false)
return
}
t.RetBool(wam.Current() != nil)
}
// DBSETINDEX — SET INDEX TO <file> (adds index to current workarea).
// Previously a no-op; the generated code path for the SET INDEX TO
// command bypasses this RTL, but SqlAttachTableIndexes (TSqlDDL.prg)
// needs a runtime call so auto-attaching PK / UNIQUE indexes at
// SqlExecOpenTable can happen without parser help. Missing file is
// swallowed — matches Harbour's soft-fail semantics and keeps
// pre-index tables silent.
func rtlDbSetIndex(t *hbrt.Thread) {
nParams := t.ParamCount()
t.Frame(nParams, 0)
defer t.EndProcFast()
if nParams < 1 {
t.RetNil()
return
}
path := t.Local(1).AsString()
if path == "" {
t.RetNil()
return
}
wam, ok := t.WA.(*hbrdd.WorkAreaManager)
if !ok || wam == nil {
t.RetNil()
return
}
area := wam.Current()
if area == nil {
t.RetNil()
return
}
// OrderListAdd lives on the optional Indexer interface — DBFNTX /
// DBFCDX implement it, MEMRDD does not. Type-assert and silently
// no-op on drivers without index support.
if idx, ok := area.(hbrdd.Indexer); ok {
_ = idx.OrderListAdd(path)
}
t.RetNil()
}