Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Base Table Name of View
Message
 
 
To
07/06/2001 14:09:05
Jay Johengen
Altamahaw-Ossipee, North Carolina, United States
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00516515
Message ID:
00516539
Views:
17
This message has been marked as the solution to the initial question of the thread.
>Is there a way to determine in code the name of the table a view is based on? ...and what if a view is based on a view which is based on a table? Can I still get the original base table name? Thanks!
>
>Regards, Renoir

This is an example, how I use Daniel's idea:
********************************************************************
*  Description.......: CloseView - closes view and all underlying tables
*  Calling Samples...: CloseView('WorkConfig')
*  Parameter List....: tcViewAlias
*  Created by........: idea - Daniel Rouleau 
*  Modified by.......: Nadya Nosonovsky 06/07/2001 02:24:37 PM 
********************************************************************
lparameters tcViewAlias
if  vartype(m.tcViewAlias)<>'C' or empty(m.tcViewAlias) or 
     return .f. && Invalid parameter
endif
local lcSQL, lcTablesList
if not used(m.tcViewAlias)
     return .f.
endif
lcSQL=cursorgetprop("SQL",m.tcViewAlias)
lcTablesList=GetSqlTables(m.lcSQL)
if used(m.tcViewAlias)
     use in (m.tcViewAlias)
endif
local array laTables[1], laDBC[1]
local i, lnTables, lcTable, lnPos, k, lcDBCPrev
lnTables=aparser1(@laTables,m.lcTablesList,',')
lcDBCPrev=''
k=0
for i=1 to m.lnTables
     lnPos=at('!',laTables[m.i])
     if m.lnPos>0
          lcTable=substr(laTables[i],m.lnPos+1)
          lcDBC=left(laTables[i],m.lnPos-1) && DBC name
          if m.lcDBCPrev<>m.lcDBC
           k=m.k+1
           dimension laDBC[m.k]
           laDBC[k]=lcDBC
           lcDBCPrev=lcDBC
         endif       
     else
          lcTable=laTables[m.i]
     endif
     use in select(m.lcTable)
next
for i=1 to alen(laDBC)
   if dbused(laDBC[m.i])
     set database to (laDBC[m.i])
     close database
   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(m.tcSQL) + ' '
lcSQL = strtran(m.lcSQL, '(', ' ')
lcSQL = strtran(m.lcSQL, ')', ' ')
lcSQL = strtran(m.lcSQL, ',', ' , ')

lnKeyWordCount = alen(laKeywords)

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

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

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

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

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

               case lcWord = 'FORCE'
*-- Ignore keyword

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

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

               case m.lcWord = 'AS'
*-- Ignore keyword

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

               case m.lcWord = 'JOIN'
                    llDiscard = False

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

                         llDiscard = True
                    endif
               endcase
          enddo
     else
          exit
     endif
enddo
if not empty(m.lcRetVal) Then
     lcRetVal = substr(m.lcRetVal, 2)
endif
set exact &lcExact
return m.lcRetVal
***********************************
function GetNextWord(tcString)
local lcRetVal
local lnCounter
local lnLen

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

for lnCounter = 1 to m.lnLen
     lcChar = substr(m.tcString, m.lnCounter, 1)
     if lcChar == ' ' Then
          tcString = substr(m.tcString, m.lnCounter)
          exit
     else
          lcRetVal = m.lcRetVal + m.lcChar
     endif
endfor
return m.lcRetVal
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform