Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
View simple problem
Message
 
 
To
09/11/2000 10:46:14
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00439246
Message ID:
00439698
Views:
22
Thanks, Daniel. I already included your program in our Function Library and tried it on vAllConfig view. Works great.
********************************************************************
*  Description.......: CloseView - closes view and all underlying tables
*  Calling Samples...: CloseView('WorkConfig')
*  Parameter List....: pcViewAlias
*  Created by........: Nadya Nosonovsky 11/08/2000 11:02:50 AM
*  Modified by.......:
********************************************************************
lparameters pcViewAlias
if empty(pcViewAlias) or vartype(pcViewAlias)<>'C'
     return .f. && Invalid parameter
endif
local lcSQL, lcTablesList
if not used(pcViewAlias)
     return .f.
endif
lcSQL=cursorgetprop("SQL",pcViewAlias)
lcTablesList=GetSqlTables(lcSQL)
if used(pcViewAlias)
     use in (pcViewAlias)
endif
local array laTables[1]
local i, lnTables, lcTable, lnPos
lnTables=aparser1(@laTables,lcTablesList,',')
for i=1 to lnTables
     lnPos=at('!',laTables[i])
     if lnPos>0
          lcTable=substr(laTables[i],lnPos+1)
     else
          lcTable=laTables[i]
     endif
     if used(lcTable)
          use in (lcTable)
     endif
next
return .t. && All view tables are closed



********************************************************************
*  Description.......: GetSqlTables - returns list of tables used in SQL (for view)
*  Calling Samples...: GetSqlTables(lcSQL)
*  Parameter List....: tcSQL
*  Created by........: Daniel Rouleau 11/09/2000
*  Modified by.......:
********************************************************************
function GetSqlTables
lparameters tcSQL

#define True .t.
#define False .f.

local lnRetVal
local lnCounter
local llDiscard
local lnEnd
local lcExact
local lnKeyWordCount
local laKeywords[14]
local lnLen
local lnPos
local lcSQL
local lnStart
local lnWhich

lcRetVal = ''

laKeywords[ 1] = ' SELECT '
laKeywords[ 2] = ' INTO '
laKeywords[ 3] = ' TO FILE '
laKeywords[ 4] = ' TO PRINTER '
laKeywords[ 5] = ' TO SCREEN '
laKeywords[ 6] = ' PREFERENCE '
laKeywords[ 7] = ' NOCONSOLE '
laKeywords[ 8] = ' PLAIN '
laKeywords[ 9] = ' NOWAIT '
laKeywords[10] = ' WHERE '
laKeywords[11] = ' GROUP BY '
laKeywords[12] = ' HAVING '
laKeywords[13] = ' UNION '
laKeywords[14] = ' ORDER BY '

lcExact = set('Exact')
set exact on

lcSQL = upper(tcSQL) + ' '
lcSQL = strtran(lcSQL, '(', ' ')
lcSQL = strtran(lcSQL, ')', ' ')
lcSQL = strtran(lcSQL, ',', ' , ')

lnKeyWordCount = alen(laKeywords)

do while True
     lnStart = at(' FROM ', lcSQL)
     if lnStart > 0 Then
          lcSQL = ltrim(substr(lcSQL, lnStart + 5))
          lnLen = len(lcSQL)
          lnEnd = lnLen + 1

          for lnCounter = 1 to lnKeyWordCount
               lnPos = at(laKeywords[lnCounter], lcSQL)
               if lnPos > 0 and lnPos < lnEnd Then
                    lnWhich = lnCounter
                    lnEnd = lnPos
               endif
          endfor

          if lnEnd < lnLen Then
               lcFromClause = left(lcSQL, lnEnd - 1)
          else
               lcFromClause = lcSQL
          endif

          llDiscard = False
          do while True
               lcWord = GetNextWord(@lcFromClause)

               do case
               case empty(lcWord)
*-- Finished
                    exit

               case lcWord = 'FORCE'
*-- Ignore keyword

               case lcWord = ','
*-- Next word will be a table name
                    llDiscard = False

               case lcWord = 'ON'
*-- Next few words represent an expression.
                    llDiscard = True

               case lcWord = 'AS'
*-- Ignore keyword

               case inlist(lcWord, 'LEFT', 'RIGHT', 'FULL', 'INNER', 'OUTER')
*-- Everything up to the JOIN keyword is
*-- to be discarded
                    llDiscard = True

               case lcWord = 'JOIN'
                    llDiscard = False

               otherwise
                    if not llDiscard Then
*-- This word is a tablename; must set discard next word
*-- not a keyword because it will be a local alias
                         if at(',' + lcWord, lcRetVal) = 0 Then
                              lcRetVal = lcRetVal + ',' + lcWord
                         endif

                         llDiscard = True
                    endif
               endcase
          enddo
     else
          exit
     endif
enddo

if not empty(lcRetVal) Then
     lcRetVal = substr(lcRetVal, 2)
endif
set exact &lcExact
return lcRetVal
***********************************
function GetNextWord(tcString)

local lcRetVal
local lnCounter
local lnLen

lcRetVal = ''
tcString = ltrim(tcString) + ' '
lnLen = len(tcString)

for lnCounter = 1 to lnLen
     lcChar = substr(tcString, lnCounter, 1)
     if lcChar == ' ' Then
          tcString = substr(tcString, lnCounter)
          exit
     else
          lcRetVal = lcRetVal + lcChar
     endif
endfor
return ( lcRetVal )
>Nadya:
>
>Modify the Otherwise case to obtain the correct result. I had the right comment but did not code it correctly.
>
>Daniel
>
> Otherwise
> If Not llDiscard Then
> *-- This word is a tablename; must set discard next word
> *-- not a keyword because it will be a local alias
> If At(',' + lcWord, lcRetVal) = 0 Then
> lcRetVal = lcRetVal + ',' + lcWord
> EndIf
>
> llDiscard = True
> EndIf
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform