mariadb and transaction

mariadb and transaction

Postby carlos vargas » Fri Jan 24, 2020 10:03 pm

Hello, I currently use a customized version of tmysql which I modify to be able to use transactions in an easy way, currently after using calls to the query and execute methods, it is necessary to validate if there is an error to display a message, I have added a data which calls "lThrowError" which, when set to True, causes that in case of error a RunTime Error is launched, which allows me to use the rollback method, I leave an example of current use.
Code: Select all  Expand view


STATIC PROCEDURE NuevoPrestamo_Grabar()
   LOCAL i, cSqlDetalle := "INSERT INTO prestamosdet (num_pres,cuota_no,fecha_prog,valor_prog,estado) VALUES "
   LOCAL lGrabado := FALSE

   FOR i:=1 TO Len( aTabla )
      cSqlDetalle += "( &1, " + Var2Str( aTabla[ i, TABLA_ABO_NO      ] ) +  "," + ;
                                Var2Str( aTabla[ i, TABLA_ABO_FECHA   ] ) +  "," + ;
                                Var2Str( aTabla[ i, TABLA_ABO_VALPROG ] ) +  "," + "'A'),"
   NEXT

   cSqlDetalle := HB_StrShrink( cSqlDetalle )

   oServer:lThrowError := TRUE

   TRY
      oServer:BeginTransaction()

      IF ( nPresNum := IncCount( "control", "cont_pres" ) ) > 0
         oServer:Insert2( "prestamosmas", { { "num_ruta"      , nRutaCob    }, ;
                                            { "num_clie"      , nClieNum    }, ; /*datos de cliente*/
                                            { "nombre"        , cClieNom    }, ;
                                            { "cedula"        , cClieCed    }, ;
                                            { "ciudad"        , cClieCiu    }, ;
                                            { "direccion"     , cClieDir    }, ;
                                            { "telefonos"     , cClieTel    }, ;
                                            { "num_pres"      , nPresNum    }, ; /*datos del prestamo*/
                                            { "importe"       , nImporte    }, ;
                                            { "interes"       , nInteres    }, ;
                                            { "cuotas"        , nCuotas     }, ;
                                            { "modalidad_pago", nModalidad  }, ;
                                            { "valor_cuota"   , nValorCuota }, ;
                                            { "total"         , nTotal      }, ;
                                            { "fecha_ent"     , dFechaEnt   }, ;
                                            { "fecha_ini"     , dFechaIni   }, ;
                                            { "fecha_fin"     , dFechaFin   }, ;
                                            { "abonado"       , 0           }, ;
                                            { "estado"        , "A"         }, ;
                                            { "nota"          , cNota       } } )

         oServer:Execute( cSqlDetalle, { nPresNum } )
      ENDIF

      oServer:Commit()
      lGrabado := TRUE

   CATCH oError
      ShowError( oError )
      oServer:Rollback()
   END

   oServer:lThrowError := FALSE

   IF lGrabado
      NuevoPrestamo_MostrarNumero()

      IF MsgNoYes( "Desea imprimir contrato del prestamo?" )
         NuevoPrestamo_Imprimir()
      ENDIF

      NuevoPrestamo_Limpiar1( TRUE )

      oDlgE:Update()

      oBtnSearch:SetFocus()
   ENDIF

RETURN lGrabado
 

Code: Select all  Expand view

METHOD Execute( cSqlCmd, aParams, lShow ) CLASS TMySQLServer
   LOCAL nRet, oErr

   DEFAULT lShow TO FALSE

   ::lError := FALSE
   ::cError := ""

   IF hb_IsArray( aParams )
      cSqlCmd := EvalQueryParams( cSqlCmd, aParams, ::pMySql )
   ENDIF

   IF lShow
      MsgInfo( ::cQuery, "SQL" )
   ENDIF

   IF ::lTraceLog
      TraceLog( cSqlCmd )
   ENDIF

   IF hb_isBlock( ::bOnLoadQuery )
      Eval( ::bOnLoadQuery, Self )
   ENDIF

   nRet := mysql_real_query( ::pMySQL, cSqlCmd )

   IF hb_isBlock( ::bOnAfterQuery )
      Eval( ::bOnAfterQuery, Self )
   ENDIF

   IF nRet > 0
      ::lError := TRUE
      ::cError := ::ErrorTxt()
      IF ::lThrowError
         oErr := ErrorNew()
         oErr:Args          := { ::pMySQL, cSqlCmd, aParams }
         oErr:CanDefault    := FALSE
         oErr:CanRetry      := FALSE
         oErr:CanSubstitute := FALSE
         oErr:GenCode       := EG_SYNTAX
         oErr:Severity      := ES_ERROR
         oErr:SubSystem     := "HBMYSQL-(EXECUTE)"
         oErr:SubCode       := ::ErrNo()
         oErr:Description   := ::cError
         oErr:Operation     := cSqlCmd
         Eval( ErrorBlock(), oErr )
      ENDIF
   ENDIF

RETURN !::lError
 

Currently in the native fiad class of mariadb / mysql what I have found is how to show or not show error messages, is there any way to simulate the way I currently work?

since I want to use the native class. Thanks for your attention.

salu2
carlos vargas
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1707
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 43 guests