diff --git a/_FiveSql2/src/TSqlAlias.prg b/_FiveSql2/src/TSqlAlias.prg index b0eb058..aebb585 100644 --- a/_FiveSql2/src/TSqlAlias.prg +++ b/_FiveSql2/src/TSqlAlias.prg @@ -119,11 +119,36 @@ RETURN cAlias /* - * AcquireTemp — create a unique alias for temp/derived tables. + * AcquireTemp — create a stable alias for temp/derived tables. + * + * Returns the purpose (usually the upper-cased table name) itself + * when it's a valid alias and not already in use in this query. + * This lets the WA cache hit on repeated queries of the form + * `FROM table t` — previously every call returned a fresh FA_#### + * that no cache entry could match, so every query re-opened the + * file. Self-joins (`FROM emp e1, emp e2`) still need two distinct + * workareas, so the second acquire for the same purpose falls back + * to the unique counter. */ METHOD AcquireTemp( cPurpose ) CLASS TSqlAlias - LOCAL cAlias + LOCAL cAlias, cUp, i, lTaken + + cUp := Upper( cPurpose ) + + IF ! Empty( cUp ) .AND. IsAlpha( Left( cUp, 1 ) ) + lTaken := .F. + FOR i := 1 TO Len( ::aSlots ) + IF ::aSlots[ i ][ 1 ] == cUp + lTaken := .T. + EXIT + ENDIF + NEXT + IF ! lTaken + AAdd( ::aSlots, { cUp, cPurpose, cPurpose, .F. } ) + RETURN cUp + ENDIF + ENDIF s_nGlobalSeq++ cAlias := "FA_" + StrZero( s_nGlobalSeq, 4 )