//TH-Header //***************************************************************************************** // Copyright (c) 2007 Antwise Solutions // All rights reserved. // // $FileName : \Pkg\cSQLconnect.pkg // $ProjectName : Mcfarland Cascade Website // $Authors : Wil van Antwerpen // $Created : 23.12.2007 22:51 // $Type : LGPL // // Contents: // //***************************************************************************************** //TH-RevisionStart //TH-RevisionEnd Use mertech.inc External_Function Win32_GetPID "_getpid" msvcrt.dll Returns Integer Use cSQLWebAppError.pkg Class cSQLconnect is a cObject Procedure Construct_Object Forward send Construct_Object Property String psServerName "" Property String psUserName "" Property String psPassword "" Property String psDataBase "" Property String psDriverName "" Property Boolean pbTrusted false Property Boolean pbLoggedIn false Property Boolean pbIsConnectionFatal true // for webapps you want driver errors to be fatal so that a page refresh might show the page properly Property Boolean pbLogEvents true // Property Boolean pbLogVerbose false Property Boolean pbLogProcessID true // add the PID to the log so you can see which process it was (useful for troubleshooting some process pooling issues) Property Boolean pbUseDatabaseINT false End_Procedure // Construct_Objecct Procedure End_Construct_Object Forward send End_Construct_Object End_Procedure // End_Construct_Object Procedure doLogEvent Integer iEventType String sEvent Handle hoInetSession Integer iPID Boolean bLogEvents Get pbLogEvents To bLogEvents If (ghInetSession<>0 and sEvent<>"" and bLogEvents) Begin If (pbLogProcessID(Self)) Begin Move (Win32_getPID()) To iPID Move (String(iPID)*sEvent) To sEvent End Send LogEvent To ghInetSession iEventType sEvent End End_Procedure // doLogEvent Procedure doLoginServer String sServerName String sUserName String sPassword String sDatabase String sDriverName Integer hoErrorObj Boolean bTrusted Get psServerName To sServerName Get psUserName To sUserName Get psPassword To sPassword Get psDriverName To sDriverName Get psDataBase To sDatabase Get pbTrusted To bTrusted //Move Error_Object_Id To hoErrorObj //Move Self To Error_Object_Id If (bTrusted) Login sServerName "" "" sDriverName Else Login sServerName sUserName sPassword sDriverName //Move hoErrorObj To Error_Object_Id If (LastErr=25000) Begin If (pbIsConnectionFatal(Self)) Begin Send doLogEvent 5 ("Unable to connect to database server '"+sServerName+"', aborting") Abort End End If (err) Set pbLoggedIn To false Else Begin If (pbLogVerbose(Self)) Send doLogEvent 5 "Successfully logged in" Set pbLoggedIn To true End If (pbUseDatabaseINT(Self)=false) Begin // We are using the database setting in the INT files, do not use // a programmatic setting. SQL_USE_DATABASE sDatabase SET_DATABASE_NAME To sDatabase End End_Procedure // doLoginServer // Returns True if the database connection exists // and false if it doesn't Function ConnectionStatus Returns Boolean String sServerName Integer iStatus Boolean bStatus Move False To bStatus Get psServerName To sServerName GET_CONNECTION_STATUS of sServerName To iStatus If (pbLogVerbose(Self)) Send doLogEvent 5 ("Connections status is "+trim(iStatus)) If (iStatus<>0) Begin Move True To bStatus End Function_Return bStatus End_Function // ConnectionStatus // // Check if the connection with the database server is still open // and abort the application gracefully when the connection // doesn't exist. // Procedure AbortIfDisconnected Boolean bConnected Get ConnectionStatus To bConnected If (bConnected=false) Begin If (pbIsConnectionFatal(Self)) Begin Send doLogEvent 5 "AbortIfDisconnected, no database connection aborting application." Abort // No Database connection, just Kill ourself End End End_Procedure // AbortIfDisconnected // // Reconnect if to the database if the connection was lost due to // whatever reason. // Procedure LoginIfDisconnected Boolean bConnected Get ConnectionStatus To bConnected If (bConnected=false) Begin Send doLogEvent 5 "LoginIfDisconnected, connection with database was lost, trying to reconnect." Send doLoginServer End Else Begin If (pbLogVerbose(Self)) Send doLogEvent 5 "Already logged in - reusing the connection." End End_Procedure // LoginIfDisconnected End_Class