Re: How to programmatically change the connection in a remote view? by Christian
Christian
Tue Mar 02 02:20:53 CST 2004
Hello,
here it is ..
DEFINE CLASS oDBChanger AS Custom
cDBName = ""
FUNCTION ChangeViewConnection
LPARAMETERS lcViewName, lcNewConnection
LOCAL lcDB, lnTableID, lnRetVal, lcAlias
lnRetVal = 1
IF EMPTY(THIS.cDBName)
lcDB = THIS.cDBName + [.dbc]
ELSE
lcDB = SET('Database') + [.dbc]
ENDIF
lcAlias = SYS(2015)
TRY
USE (lcDB) AGAIN ALIAS (lcAlias) IN 0 SHARED
CATCH
lnRetVal = -1
ENDTRY
IF lnRetVal = -1
RETURN lnRetVal
ENDIF
SELECT (lcAlias)
LOCATE FOR ALLTRIM(Objecttype) == "View" AND ALLTRIM(ObjectName) ==
lcViewName
IF FOUND()
lnViewID = &lcAlias..ObjectID
ELSE
USE IN (lcAlias)
RETURN -1
ENDIF
lnRetVal = THIS.SetViewConnection(lnViewID,lcNewConnection)
USE IN (lcAlias)
RETURN lnRetVal
ENDFUNC
FUNCTION SetViewConnection
LPARAMETERS lnViewID, lcNewConnection
LOCATE FOR ObjectID = lnViewID
IF FOUND()
LOCAL lbDone, lnPos, lcLength, lnLength, lnIDLength, lnIDCode,
lcCurrValue, lnValueLen
lbDone = .F.
lnPos = 1
lnValueLen = LEN(lcNewConnection)
DO WHILE NOT lbDone
* Get the length of this property.
lcLength = SUBSTR(PROPERTY, lnPos, 4)
lnLength = THIS.Hex2Decimal(lcLength)
* Get the length of the property ID.
lcLength = SUBSTR(PROPERTY, lnPos + 4, 2)
lnIDLength = THIS.Hex2Decimal(lcLength)
* Get the property ID.
lcLength = SUBSTR(PROPERTY, lnPos + 6, lnIDLength)
lnIDCode = THIS.Hex2Decimal(lcLength)
IF lnIDCode = 32 && Connection Name
lcCurrValue = SUBSTR(PROPERTY, lnPos + lnIDLength + 6)
lcCurrValue = LEFT(lcCurrValue, AT(CHR(0), lcCurrValue) - 1)
IF LEN(lcCurrValue) <> lnLength - lnIDLength - 6
lnLength = LEN(lcCurrValue) + lnIDLength + 6 + 1 && + 1 for CHr(0) at
the end of string
ENDIF
REPLACE PROPERTY WITH STUFF(PROPERTY,lnPos,lnLength, ;
THIS.Decimal2Hex(lnValueLen + lnIDLength + 6 + 1,4) + ;
THIS.Decimal2Hex(lnIDLength,2) + ;
THIs.Decimal2Hex(32,lnIDLength) + ;
lcNewConnection + CHR(0))
lbDone = .T.
ELSE
* Move the pointer to the next property. If we're out of properties,
we're
* done checking.
lnPos = lnPos + lnLength
IF lnPos > LEN(PROPERTY)
lbDone = .T.
ENDIF
ENDIF
ENDDO
ENDIF
RETURN IIF(lbDone,1,-1)
ENDFUNC
FUNCTION Hex2Decimal
LPARAMETERS lcValue, lbSigned
LOCAL lnDecimal, lnLen, lnI, lnMSB, lnMax
lnDecimal = 0
lnLen = LEN(lcValue)
FOR xj = 1 TO lnLen
lnDecimal = lnDecimal + ASC(SUBSTR(lcValue, xj, 1)) * 256 ^ (xj - 1)
ENDFOR
IF lbSigned
lnMSB = (lnLen * 8) - 1
IF BITTEST(lnDecimal, lnMSB)
lnMax = 2 ^ (lnMSB + 1)
lnDecimal = lnDecimal - lnMax
ENDIF
ENDIF
RETURN lnDecimal
ENDFUNC
FUNCTION Decimal2Hex
LPARAMETERS tnValue, tnPlaces
LOCAL lnPlaces, lcHex, lcOut, lnI
lnPlaces = iif(pcount() = 1, 4, tnPlaces)
lcHex = This.ReverseDecimal2Hex(tnValue, lnPlaces)
lcOut = ''
for lnI = 1 to lnPlaces
lcOut = lcOut + substr(lcHex, lnPlaces - lnI + 1, 1)
next lnI
return lcOut
ENDFUNC
FUNCTION ReverseDecimal2Hex
LPARAMETERS tnValue, tnPlaces
LOCAL lnDecimal, lcHex, lnCurrDecimals, lnPlaces, lnI, lnExponent, lnTemp
lnDecimal = tnValue
lcHex = ''
lnCurrDecimals = set('DECIMALS')
lnPlaces = iif(pcount() = 1, 4, tnPlaces)
set decimals to 17
for lnI = lnPlaces to 1 step -1
lnExponent = 256 ^ (lnI - 1)
lnTemp = int(lnDecimal/lnExponent)
lcHex = lcHex + chr(lnTemp)
lnDecimal = lnDecimal - lnTemp * lnExponent
next lnI
set decimals to lnCurrDecimals
return lcHex
ENDFUNC
ENDDEFINE
most of the code is from Doug Henning's "fixdbc" program ..
but as one can see .. it's not just good for fixing dbc's ..
usage example :
loDB = NEWOBJECT('oDBChanger')
"Jacobus Terhorst" <lanto3000@hotmail.com> schrieb im Newsbeitrag
news:euBVpo9$DHA.3456@TK2MSFTNGP09.phx.gbl...
> Yes, I am interested in the code...
>
> Jacobus Terhorst
>
> "Christian Ehlscheid" <ehlscheid-no-spam@edv-ermtraud.de> wrote in message
> news:c1mnms$rn6$03$1@news.t-online.com...
> > Hello,
> >
> > it can be done but custom code is needed to alter the dbc manually ..
> > maybe you can use one of these two alternatives ..
> >
> > 1. USE viewName CONNSTRING " .. "
> >
> > 2. DROP VIEW viewName
> > and then recreate view with new connection ( the code to create the view
> can
> > be generated with the gendbc utility in the VFP\tools folder)
> >
> > post back if you're interested in the custom code to change the
> > connectionname on the fly
> >
> > Regards
> >
> > Christian
> >
> > "Jacobus Terhorst" <lanto3000@hotmail.com> schrieb im Newsbeitrag
> > news:OJ5p7AL$DHA.3536@TK2MSFTNGP10.phx.gbl...
> > > Does anyone know how to programmatically change the Connection
> > > Name/Datasource of a remote view?
> > >
> > > Jacobus Terhorst
> > >
> > >
> >
> >
>
>