Jump to content

Recommended Posts

Posted

I have a couple functions that I use pretty heavily (one of them isn't the greatest, but its functional, which is all I care about right now).

 

Func _GetRecords($connection,$query)
    $sHeaders=StringReplace($query,"Select ","")
    $sHeaders=StringLeft($sHeaders,StringInStr($sHeaders,"from")-1)
    $aHeaders=StringSplit($sHeaders,",",2)
    For $z=0 to UBound($aHeaders)-1
        $aHeaders[$z]=StringStripWS(StringStripWS($aHeaders[$z],1),2)
        If StringInStr($aHeaders[$z]," as ") <> 0 Then
            $aHeaders[$z]=StringTrimLeft($aHeaders[$z],StringInStr($aHeaders[$z]," as ")+3)
        EndIf
    Next
    _ArrayTranspose($aHeaders)
    $sqlRs = ObjCreate("ADODB.Recordset")
    ConsoleWrite($query&@CRLF)
    $sqlRs.open ($query,$connection)
        If $sqlRs.EOF = True Then
            Return $aHeaders
        Else
            $result = $sqlRs.GetRows
            If UBound($result)>0 Then
                _ArrayInsert($result,0,_ArrayToString($aHeaders))
                Return $result
                $sqlRs.Close
            Else
                SetError(1)
                $sqlRs.Close
            EndIf
        EndIf
EndFunc

Func _InsertRecord($connection,$query)
    $sqlRs = ObjCreate("ADODB.Recordset")
    ConsoleWrite($query&@CRLF)
    $sqlRs.open ($query,$connection)
EndFunc

The problem I face is that if someone leaves their computer and disconnects, it errors out and doesn't perform the query. I have a logger that captures if they get disconnected, but I'm trying to figure out a way to try and redo the query if it fails without changing every reference to my functions. This is my error handler:

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
Func MyErrFunc()
  $HexNumber=hex($oMyError.number,8)
  If StringInStr($oMyError.description,'connection')<>0 then
      DBConnect($mode)
  Else
      MsgBox(0,"","_______------- Error -------_______"       & @CRLF & _
                 "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
                 "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
                 "err.number is: "         & @TAB & $HexNumber              & @CRLF & _
                 "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
                 "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
                 "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
                 "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
                 "err.helpcontext is: "    & @TAB & $oMyError.helpcontext _
                )
      SetError(0)  ; to check for after this function returns
  EndIf
Endfunc

You will notice that if it gets a disconnect, it uses my connection function to reconnect.

 

I'm trying to modify my getrecords and Insert records to make it so if they get disconnected, it tries to reconnect and requery. I was thinking about maybe a Do Until error loop or something, but I'm curious if anyone has any creative ideas for this.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...