@@ -10,7 +10,6 @@ import (
1010
1111 "github.com/cybertec-postgresql/pg_timetable/internal/log"
1212 pgx "github.com/jackc/pgx/v5"
13- "github.com/jackc/pgx/v5/pgconn"
1413)
1514
1615// StartTransaction returns transaction object, virtual transaction id and error
@@ -56,7 +55,7 @@ func (pge *PgEngine) MustRollbackToSavepoint(ctx context.Context, tx pgx.Tx, tas
5655}
5756
5857// ExecuteSQLTask executes SQL task
59- func (pge * PgEngine ) ExecuteSQLTask (ctx context.Context , tx pgx.Tx , task * ChainTask , paramValues []string ) (out string , err error ) {
58+ func (pge * PgEngine ) ExecuteSQLTask (ctx context.Context , tx pgx.Tx , task * ChainTask , paramValues []string ) (err error ) {
6059 switch {
6160 case task .IsRemote ():
6261 return pge .ExecRemoteSQLTask (ctx , task , paramValues )
@@ -68,15 +67,15 @@ func (pge *PgEngine) ExecuteSQLTask(ctx context.Context, tx pgx.Tx, task *ChainT
6867}
6968
7069// ExecLocalSQLTask executes local task in the chain transaction
71- func (pge * PgEngine ) ExecLocalSQLTask (ctx context.Context , tx pgx.Tx , task * ChainTask , paramValues []string ) (out string , err error ) {
70+ func (pge * PgEngine ) ExecLocalSQLTask (ctx context.Context , tx pgx.Tx , task * ChainTask , paramValues []string ) (err error ) {
7271 if err := pge .SetRole (ctx , tx , task .RunAs ); err != nil {
73- return "" , err
72+ return err
7473 }
7574 if task .IgnoreError {
7675 pge .MustSavepoint (ctx , tx , task .TaskID )
7776 }
7877 pge .SetCurrentTaskContext (ctx , tx , task .ChainID , task .TaskID )
79- out , err = pge .ExecuteSQLCommand (ctx , tx , task . Command , paramValues )
78+ err = pge .ExecuteSQLCommand (ctx , tx , task , paramValues )
8079 if err != nil && task .IgnoreError {
8180 pge .MustRollbackToSavepoint (ctx , tx , task .TaskID )
8281 }
@@ -87,55 +86,57 @@ func (pge *PgEngine) ExecLocalSQLTask(ctx context.Context, tx pgx.Tx, task *Chai
8786}
8887
8988// ExecStandaloneTask executes task against the provided connection interface, it can be remote connection or acquired connection from the pool
90- func (pge * PgEngine ) ExecStandaloneTask (ctx context.Context , connf func () (PgxConnIface , error ), task * ChainTask , paramValues []string ) ( string , error ) {
89+ func (pge * PgEngine ) ExecStandaloneTask (ctx context.Context , connf func () (PgxConnIface , error ), task * ChainTask , paramValues []string ) error {
9190 conn , err := connf ()
9291 if err != nil {
93- return "" , err
92+ return err
9493 }
9594 defer pge .FinalizeDBConnection (ctx , conn )
9695 if err := pge .SetRole (ctx , conn , task .RunAs ); err != nil {
97- return "" , err
96+ return err
9897 }
9998 pge .SetCurrentTaskContext (ctx , conn , task .ChainID , task .TaskID )
100- return pge .ExecuteSQLCommand (ctx , conn , task . Command , paramValues )
99+ return pge .ExecuteSQLCommand (ctx , conn , task , paramValues )
101100}
102101
103102// ExecRemoteSQLTask executes task against remote connection
104- func (pge * PgEngine ) ExecRemoteSQLTask (ctx context.Context , task * ChainTask , paramValues []string ) ( string , error ) {
103+ func (pge * PgEngine ) ExecRemoteSQLTask (ctx context.Context , task * ChainTask , paramValues []string ) error {
105104 log .GetLogger (ctx ).Info ("Switching to remote task mode" )
106105 return pge .ExecStandaloneTask (ctx ,
107106 func () (PgxConnIface , error ) { return pge .GetRemoteDBConnection (ctx , task .ConnectString ) },
108107 task , paramValues )
109108}
110109
111110// ExecAutonomousSQLTask executes autonomous task in an acquired connection from pool
112- func (pge * PgEngine ) ExecAutonomousSQLTask (ctx context.Context , task * ChainTask , paramValues []string ) ( string , error ) {
111+ func (pge * PgEngine ) ExecAutonomousSQLTask (ctx context.Context , task * ChainTask , paramValues []string ) error {
113112 log .GetLogger (ctx ).Info ("Switching to autonomous task mode" )
114113 return pge .ExecStandaloneTask (ctx ,
115114 func () (PgxConnIface , error ) { return pge .GetLocalDBConnection (ctx ) },
116115 task , paramValues )
117116}
118117
119118// ExecuteSQLCommand executes chain command with parameters inside transaction
120- func (pge * PgEngine ) ExecuteSQLCommand (ctx context.Context , executor executor , command string , paramValues []string ) (out string , err error ) {
121- var ct pgconn.CommandTag
119+ func (pge * PgEngine ) ExecuteSQLCommand (ctx context.Context , executor executor , task * ChainTask , paramValues []string ) (err error ) {
122120 var params []any
123- if strings .TrimSpace (command ) == "" {
124- return "" , errors .New ("SQL command cannot be empty" )
121+ var errCodes = map [bool ]int {false : 0 , true : - 1 }
122+ if strings .TrimSpace (task .Command ) == "" {
123+ return errors .New ("SQL command cannot be empty" )
125124 }
126125 if len (paramValues ) == 0 { //mimic empty param
127- ct , err = executor .Exec (ctx , command )
128- out = ct .String ()
129- return
126+ ct , e : = executor .Exec (ctx , task . Command )
127+ pge . LogTaskExecution ( context . Background (), task , errCodes [ err != nil ], ct .String (), "" )
128+ return e
130129 }
131130 for _ , val := range paramValues {
132- if val > "" {
133- if err = json .Unmarshal ([]byte (val ), & params ); err != nil {
134- return
135- }
136- ct , err = executor .Exec (ctx , command , params ... )
137- out = out + ct .String () + "\n "
131+ if val == "" {
132+ continue
133+ }
134+ if err = json .Unmarshal ([]byte (val ), & params ); err != nil {
135+ return
138136 }
137+ ct , e := executor .Exec (ctx , task .Command , params ... )
138+ err = errors .Join (err , e )
139+ pge .LogTaskExecution (context .Background (), task , errCodes [e != nil ], ct .String (), val )
139140 }
140141 return
141142}
0 commit comments