MySql Nativa Transacciones. At. Mr. Rao

Re: MySql Nativa Transacciones. At. Mr. Rao

Postby nageswaragunupudi » Fri Sep 17, 2021 6:49 pm

After seeing this post, I am considering introducing two switches:
DATA lRaiseErrorDML INIT .f.
DATA lRaiseErrorALL INIT .f.

I can change the name of the variables to lThrowError, etc. if you all suggest

If programmer sets :lRaiseErrorDML := .t., then the Execute() method will raise a runtime error for failure of DML statements only ( DML: INSERT/UPDATE/DELETE ) and will be silent for errors with other SQL queries.


If programmer sets :lRaiseErrorALL := .t., then the Execute() method will raise a runtime error for failure in execution of any query.

Then we can write:
Code: Select all  Expand view

oCn:BeginTransaction()
oCn:lRaseErrorDML := .t.

TRY
    oCn:Insert(...)
    ....
CATCH
    lSaved := .f.
    oCn:RollBack()
END
oCn:lRaiseErrorDML := .f.
if lSaved
   oCn:CommitTransaction()
endif
 

Welcome any suggestions, before I finish the changes
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10642
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: MySql Nativa Transacciones. At. Mr. Rao

Postby carlos vargas » Sat Sep 18, 2021 4:17 am

DATA lRaiseErrorDML INIT .f.
DATA lRaiseErrorALL INIT .f.

Ok rao.

But, transaction in mysql is only for insert, update and delete statements?

Umm. Alter table.., etc. Is not supported in transaction?
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1720
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: MySql Nativa Transacciones. At. Mr. Rao

Postby Ariel » Sat Sep 18, 2021 10:44 am

Mr. Rao,

A mi modesto parecer deberia funcionar como el :execute()

TRY
oServer:Begintrasaction()
oServer:Execute( "INSERT INTO ... ")
oServer:Execute( "INSERT INTO ... ")
oServer:Execute( "INSERT INTO ... ")
oServer:Execute( "INSERT INTO ... ")
CATCH oErr
oServer:RollBack()
FINALLY
oServer:CommitTransaction()

END

De esta forma SI funciona el break automaticamente por el error.
Saludos.
Ariel
 
Posts: 376
Joined: Wed Nov 29, 2006 1:51 pm
Location: Rosario - Argentina

Re: MySql Nativa Transacciones. At. Mr. Rao

Postby nageswaragunupudi » Sat Sep 18, 2021 10:51 am

But, transaction in mysql is only for insert, update and delete statements?

Yes.
Only for DML statements, i.e., INSERT, UPDATE and DELETE statements

Umm. Alter table.., etc. Is not supported in transaction?

Yes. Not supported.

One more important point to remember is that if at all we use any DDL statement inside a transaction, the transaction gets committed and then this DDL statement is executed. So we need to be careful not to use any statements not compatible inside a transaction.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10642
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: MySql Nativa Transacciones. At. Mr. Rao

Postby Ariel » Sat Sep 18, 2021 11:05 am

Mr. Rao,

a mi modesto entender deberia funcionar como cuando usamos :Execute()

TRY
::oServer:BeginTransaction()
::oServer:Execute( "INSERT INTO ... ")
::oServer:Execute( "INSERT INTO ... ")
::oServer:Execute( "INSERT INTO ... ")
::oServer:Execute( "INSERT INTO ... ")
::oServer:Execute( "INSERT INTO ... ")

CATCH oErr
::oServer:RollBack()

FINALLY
::oServer:CommitTransaction()

END

De esta manera salta solo por el catch
Saludos.
Ariel
 
Posts: 376
Joined: Wed Nov 29, 2006 1:51 pm
Location: Rosario - Argentina

Re: MySql Nativa Transacciones. At. Mr. Rao

Postby carlos vargas » Sat Sep 18, 2021 3:00 pm

from help of xharbour
FINALLY
The finally section is guaranteed to be executed, no matter if an error was handled or not.


in you example
Code: Select all  Expand view

FINALLY
::oServer:CommitTransaction()

END
 


The commit would be executed even when there is an error?
Anyway, I don't know if harbor supports that instruction.


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

Re: MySql Nativa Transacciones. At. Mr. Rao

Postby nageswaragunupudi » Sat Sep 18, 2021 3:10 pm

Anyway, I don't know if harbor supports that instruction.


Harbour does not support "TRY/CATCH" syntax at all. It supports BEGIN SEQUENCE/END SEQUENCE only.
xHarbour supports both.

fivewin.ch translates TRY/CATCH to BEGIN/END SEQUENCE for Harbour users. FINALLY also is translated.

Your observation about FINALLY is right.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10642
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: MySql Nativa Transacciones. At. Mr. Rao

Postby carlos vargas » Sat Sep 18, 2021 3:11 pm

yes, work with xhb.hbc

Code: Select all  Expand view

// The example demonstrates program flow for a TRY..CATCH sequence

#ifdef __HARBOUR__  //from fivewin.ch
   #ifndef __XHARBOUR__
   #xcommand TRY  => BEGIN SEQUENCE WITH {| oErr | Break( oErr ) }
   #xcommand CATCH [<!oErr!>] => RECOVER [USING <oErr>] <-oErr->
   #xcommand FINALLY => ALWAYS
   #xtranslate Throw( <oErr> ) => ( Eval( ErrorBlock(), <oErr> ), Break( <oErr> ) )
   #endif
#endif

   PROCEDURE Main()
      LOCAL oErr

      TRY
        ? "Trying"
        oErr := ErrorNew()
        oErr:Args          := { date(), time(), pi() }
        oErr:CanDefault    := FALSE
        oErr:CanRetry      := FALSE
        oErr:CanSubstitute := FALSE
        oErr:GenCode       := EG_SYNTAX
        oErr:Severity      := ES_ERROR
        oErr:SubSystem     := "MYTEST"
        oErr:SubCode       := 1001
        oErr:Operation     := "Prueba de error"
        oErr:Description   := "Descripcion de la prueba de error"    
        Throw( oErr )
      CATCH oErr
        ? "Caught:", oErr:SubCode, oErr:Operation, oErr:Description
        ? "Throwing to outer, should be deferred"
        //Throw( oErr )
      FINALLY
         ? "Finalized"
      END
     
      ? "Oops, should have Re-Thrown, after the FINALLY."
   RETURN

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

Re: MySql Nativa Transacciones. At. Mr. Rao

Postby nageswaragunupudi » Sat Sep 18, 2021 3:58 pm

Hace años que uso tmysql con estos cambios, y me ha funcionado bien, me gusta mucho la implementación de mysql en fwh pero veo que tengo que hacer muchos cambios :-(


FWH Insert method supports your Insert2 method syntax also.

oCn:Insert( cTable, { { "fld1", "val1" }, ... { "fldN", "valN" } } )
also works.

Soon we are going to implement lThrowError like functionality.

May I know what do you advise to make migration easlier?
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10642
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: MySql Nativa Transacciones. At. Mr. Rao

Postby carlos vargas » Sat Sep 18, 2021 5:10 pm

The only thing about transactions, the other thing is easy, someone else?
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1720
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: MySql Nativa Transacciones. At. Mr. Rao

Postby nageswaragunupudi » Sun Sep 19, 2021 10:22 am

Will be available in the next release.
Applies to execution of DML statements "INSERT,UPDATE,REPLACE,DELETE" directly using oCn:Execute() and to methods:
method Insert(...)
method Upsert(...)
method Update()

Implemented new
DATA lThrowError AS LOGICAL INIT .f.

Recommended usage:
Code: Select all  Expand view
 local lError := .f.
  oCn:BeginTransaction()
  oCn:lThrowError := .t.
  TRY
     oCn:Insert(....)
     oCn:Update(...)
     oCn:Execute( "INSERT ... " )
     ...
   CATCH
     lError := .t.
   END
   oCn:lThrowError := .f.
   if lError
      oCn:RollBack()
   else
      oCn:CommitTransaction()
   endif
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10642
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: MySql Nativa Transacciones. At. Mr. Rao

Postby Marcelo Roggeri » Sun Sep 26, 2021 2:08 pm

Mr Rao, buenísimo muchas gracias es bueno saberlo
Funciona de las dos maneras.
Solo utilizo Harbour
Saludos
FWH - Harbour - BCC7 - PellesC
User avatar
Marcelo Roggeri
 
Posts: 342
Joined: Sat Jul 22, 2006 9:04 pm
Location: Venado Tuerto - Santa Fe -Argentina

Previous

Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 49 guests