In a multitenant project, XData seems to execute SQL on all databases

The way you configure the OnSqlExecuting event is causing this. You are registering a new listener for each database, thus each request will fire multiple ones.

You should just set the event once, in the main form OnCreate event, for example, this way:

procedure TForm1.FormCreate(Sender: TObject);
begin
  FServers := TObjectDictionary<string, TServerByUrlModule>.Create([doOwnsValues]);
  TMappingExplorer.Get(TMappingExplorer.DefaultModelName).Events.OnSqlExecuting.Subscribe(DbSqlLogProc);
end;

The event could be something like this:

procedure TForm1.DbSqlLogProc(Args: TSQLExecutingArgs);
begin
  var Conn := TObjectManager(Args.Manager).Connection;
  if Conn is TDBConnectionWrapper then
    Conn := (Conn as TDBConnectionWrapper).Intf;
  var SqliteConn := Conn as TSQLiteNativeConnectionAdapter;
  var LDbName := TRttiContext.Create.GetType(TSQLiteNativeConnectionAdapter)
    .GetField('FFileName').GetValue(SqliteCOnn).AsString;
  Form1.Memo1.Lines.Add(
    '{' + LDbName + '}' + Args.SQL);
  if Args.Params <> nil then
  begin
    for var MyParam in Args.Params do
      Form1.Memo1.Lines.Add('>>' + MyParam.ParamName
          + '[' + GetEnumName(TypeInfo(TFieldType), Ord(MyParam.ParamType)) + ']='
          + VarToStr(MyParam.ParamValue)
          );
  end;
end;

Note that it's using RTTI to find the file name because TSQLiteNativeConnectionAdapter doesn't make it public. We changed it here so in next version you can simply use:

 var LDbName := SqliteConn.FileName;
1 Like