************************************************** *-- Class: debase (f:\devel\src\libs\dal.vcx) *-- ParentClass: dataenvironment *-- BaseClass: dataenvironment *-- Time Stamp: 11/07/06 10:17:02 AM * #INCLUDE "f:\devel\src\include\dal.h" * DEFINE CLASS debase AS dataenvironment AutoOpenTables = .F. AutoCloseTables = .F. DataSource = .NULL. *-- The private session object. osession = "" *-- The exact value of the parameter passed into the INIT method. To be used by other objects when creating their data environment. cdataenvironment = "" Name = "debase" *-- Encrypts passwords PROCEDURE encrypt ***************************************************************************** * Name : Encrypt * Description: Encrypts or decrypts a given string * Parameters : tcIn - character string to convert * tlDecrypt - whether this function should decrypt the given string * Returns : Character string converted according to the encrypt/decrypte algorithm ***************************************************************************** LPARAMETER tcIn, tlDecrypt LOCAL lcKey, lnLoop, lcOut, lnPosition, lcChar, lnAdjustment, lnAscii, lnAsciiNew IF PCOUNT() < 1 OR EMPTY(tcIn) RETURN "" ENDIF lcKey = "Any Letter character combination: about 10 characters" lcOut = "" FOR lnLoop = 1 TO LEN(TRIM(tcIn)) lnPosition = MOD(lnLoop-1, LEN(lcKey)) + 1 lnAscii = ASC(SUBSTR(tcIn, lnLoop, 1)) - 32 - 1 lnAdjustment = (ASC(SUBSTR(lcKey, lnPosition, 1)) + lnLoop) * IIF(tlDecrypt,-1,1) lnAsciiNew = lnAscii + lnAdjustment lcChar = CHR(MOD(lnAsciiNew, 94) + 32 + 1) lcOut = lcOut + lcChar ENDFOR RETURN lcOut ENDPROC PROCEDURE Destroy IF VARTYPE(THIS.oSession) = 'O' THIS.oSession.Destroy() ENDIF THIS.oSession = null DODEFAULT() ENDPROC PROCEDURE BeforeOpenTables * Ensure the correct DataSession is active. SET DATASESSION TO THIS.oSession.DataSessionID DODEFAULT() ENDPROC * Session control in the data environment??? YUP!! Single session or multy session depending on BO PROCEDURE Init LPARAMETERS tcDataEnvironment AS String, toSession AS Session *SET CLASSLIB TO util ADDITIVE && this is where the APIInterface is located SET CLASSLIB TO dal ADDITIVE DODEFAULT() IF PCOUNT() >= 2 AND VARTYPE(toSession) = 'O' THIS.oSession = toSession ELSE * Each BO Template will have it's own private DataSession THIS.oSession = NEWOBJECT("Session") ENDIF THIS.cDataEnvironment = tcDataEnvironment ENDPROC ENDDEFINE * *-- EndDefine: debase **************************************************Oracle Example...
************************************************** *-- Class: deoracle (f:\devel\src\libs\dal.vcx) *-- ParentClass: debase (f:\devel\src\libs\dal.vcx) *-- BaseClass: dataenvironment *-- Time Stamp: 11/07/06 10:18:03 AM * #INCLUDE "f:\devel\src\include\dal.h" * DEFINE CLASS deoracle AS debase Tag = "" DataSourceType = "ODBC" *-- The connection string used to connect to the remote data. cconnection = "" Name = "deoracle" *-- Retrieves the Oracle driver from the registry. PROCEDURE getoracledriver LOCAL loRegEdit AS odbcreg OF registry LOCAL loAPIInterface AS APIInterface OF util LOCAL lcDriver, oError, lcINIDir, lcIniFile, lcPreferredDriver STORE "" TO lcDriver, lcINIDir, lcIniFile, lcPreferredDriver TRY loRegEdit = CREATEOBJECT("odbcreg") CATCH TO oError ENDTRY TRY loAPIInterface = CREATEOBJECT('APIInterface') CATCH TO oError ENDTRY *************************************** * preferred driver section * if driver exist, use this driver * specific to the machine so this ini would be in the windows directory * You never know if a user has Oracle 8, Oracle 9 or Oracle 10 drivers installed. * This is considered a bypass. IF VARTYPE(loAPIInterface) = 'O' lcINIDir = loAPIInterface.GetWinDir() IF EMPTY(lcINIDir) lcINIDir = ALLTRIM(FULLPATH(CURDIR())) ENDIF lcIniFile = ADDBS(lcINIDir) + INIFILE IF FILE(lcIniFile) lcPreferredDriver = loAPIInterface.GetPrivStr(lcIniFile, INI_DB_SECTION, DRIVER_KEY) ENDIF ENDIF *************************************** IF EMPTY(lcPreferredDriver) AND VARTYPE(loRegEdit) = "O" LOCAL lnArrayLen DIMENSION a_Drivers(1) a_Drivers[1] = "" * This routine does not always return the correct information loRegEdit.GetOdbcDrvrs(@a_Drivers, .F.) IF !EMPTY(a_Drivers[1,1]) lnArrayLen = ALEN(a_Drivers, 1) * Preferred Driver - by-pass guessing * but make sure it does exist FOR i = 1 TO lnArrayLen IF UPPER(ALLTRIM(a_Drivers[i, 1])) == ALLTRIM(UPPER(lcPreferredDriver)) lcDriver = a_Drivers[i, 1] EXIT ENDIF ENDFOR * Best chance - Oracle driver probably starts with "Oracle" IF EMPTY(lcDriver) FOR i = 1 TO lnArrayLen IF "MICROSOFT"$UPPER(a_Drivers[i, 1]) LOOP ENDIF IF SUBSTR(UPPER(a_Drivers[i, 1]), 1, 6) = "ORACLE" lcDriver = a_Drivers[i, 1] EXIT ENDIF ENDFOR ENDIF * Good Chance - Don't use the Microsoft driver IF EMPTY(lcDriver) FOR i = 1 TO lnArrayLen IF "MICROSOFT"$UPPER(a_Drivers[i, 1]) LOOP ENDIF IF "ORACLE"$UPPER(a_Drivers[i, 1]) lcDriver = a_Drivers[i, 1] EXIT ENDIF ENDFOR ENDIF * No Chance - No choice, probably need to use the Microsoft driver if it exists IF EMPTY(lcDriver) FOR i = 1 TO lnArrayLen IF "ORACLE"$UPPER(a_Drivers[i, 1]) lcDriver = a_Drivers[i, 1] EXIT ENDIF ENDFOR ENDIF ENDIF ELSE * hopefully this is not empty lcDriver = lcPreferredDriver ENDIF RETURN lcDriver ENDPROC PROCEDURE Destroy IF VARTYPE(THIS.DataSource) = 'N' AND THIS.DataSource > 0 * the connection is not automatically destroyed * here we manually destroy it. = SQLDISCONNECT(THIS.DataSource) ENDIF DODEFAULT() ENDPROC PROCEDURE BeforeOpenTables DODEFAULT() *** Select connection code: DO NOT REMOVE ***<DataSource> * only create the connection if it has not been done before. IF VARTYPE(THIS.DataSource) = 'X' OR (VARTYPE(THIS.DATASOURCE) = 'N' AND THIS.DATASOURCE < 1) THIS.DATASOURCE = SQLSTRINGCONNECT(THIS.cConnection) ENDIF ***</DataSource> *** End of Select connection code: DO NOT REMOVE ENDPROC PROCEDURE Init LPARAMETERS tcConnection AS String, toSession AS Session LOCAL loAPIInterface as APIInterface OF util LOCAL lcBuffer, lcIniFile, lcConnection, lcDriver LOCAL oError STORE "" TO lcBuffer, lcIniFile, lcConnection, lcDriver SET CLASSLIB TO registry ADDITIVE && you have this classlib as part of VFP TRY loAPIInterface = CREATEOBJECT('APIInterface') && an object to handle API calls. CATCH TO oError ENDTRY IF PCOUNT() >= 1 AND !ISNULL(tcConnection) AND !EMPTY(tcConnection) lcConnection = LOWER(ALLTRIM(tcConnection)) ELSE lcConnection = PROD_CONNECTION_STRING ENDIF IF VARTYPE(loAPIInterface) = "O" lcIniFile = ADDBS(ALLT(FULLPATH(CURDIR()))) + DBINIFILE IF !FILE(lcIniFile) IF FILE(DBINIFILE) lcIniFile = FULLPATH(DBINIFILE) ELSE lcIniFile = "" ENDIF ENDIF IF !EMPTY(lcIniFile) * Attempt DSN Less connection lcBuffer = loAPIInterface.GetPrivStr(lcIniFile, INI_DB_SECTION, lcConnection) lcDriver = "DRIVER={" + THIS.GetOracleDriver() + "}" IF !EMPTY(lcBuffer) AND !EMPTY(lcDriver) THIS.cConnection = lcDriver + ";DBQ=" + lcBuffer + ";UID=UsErId;PWD=" + THIS.Encrypt(ORACLE_PWD, .T.) ENDIF ENDIF ENDIF IF VARTYPE(loAPIInterface) <> "O" OR EMPTY(THIS.cConnection) OR ISNULL(THIS.cConnection) * Failed: connect via the ODBC setting on the machine THIS.cConnection = "DSN=" + lcConnection + ";UID=UsErId;PWD=" + THIS.Encrypt(ORACLE_PWD, .T.) ENDIF THIS.Tag = THIS.cConnection * Can not modify this parameters value: tcConnection RETURN DODEFAULT(tcConnection, toSession) ENDPROC ENDDEFINE * *-- EndDefine: deoracle **************************************************