M
M
Merlin_ML2018-11-26 21:21:47
SQL
Merlin_ML, 2018-11-26 21:21:47

How to set a variable number of arguments for someone else's function?

Please help, there is a lot of boilerplate code when you execute ExecSQLEx, for example:

if not DataModule1.UniConnection1.InTransaction then 
DataModule1.UniConnection1.StartTransaction;          
try          
  //тут выполняемый запрос
  DataModule1.UniConnection1.ExecSQLEx('UPDATE ...','parametr1', parametr1, 'parametr2', parametr2,'parametr3', parametr3 ...);
  DataModule1.UniConnection1.Commit;  
except on E: Exception do
  begin
    DataModule1.UniConnection1.Rollback;
  end;
end;

I would like to put this in a separate function, where you only need to specify the SQL query text and parameters, but I just can’t figure out how to do it correctly. The problem is that there can be a different number of input parameters, and their types can be different. My idea of ​​how it should look like:
...
class function upd<T> (SQL:string; params: array of T): T; static;
...
 function TForm1.upd<T> (SQL:string; params: array of T): T;
 begin
  if not DataModule1.UniConnection1.InTransaction then
  DataModule1.UniConnection1.StartTransaction;
  try

    DataModule1.UniConnection1.ExecSQLEx(SQL, params[i]) //как здесь перечислить в цикле и указать входящие аргументы?
    DataModule1.UniConnection1.Commit;
  except on E: Exception do
    begin
      DataModule1.UniConnection1.Rollback;
      ShowMessage('Ошибка: '+ E.Message)
    end;
  end;
 end;

How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stockholm Syndrome, 2018-11-26
@StockholmSyndrome

probably something like this:

class function upd<T>(SQL: string; params: array of Variant): Variant; static;
...
function TForm1.upd<T>(SQL: string; params: array of Variant): Variant;
begin
  ...
  DataModule1.UniConnection1.ExecSQLEx(SQL, params); 
  DataModule1.UniConnection1.Commit;
  ...
end;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question