All above of course derives from a bigger scenario with many tables, scattered into several databases and a lot of SQL queries already working... before doing a big re-design I would like to understand if a "direct port" to XData Aurelius is possible.
Thanks in advance!
There are even more suggestions besides all the valid ones @Mika_Koistinen provided:
Map the Cities table with the db-b prefix. It's indeed for schemas, but could work because all Aurelius does is to add a prefix in SQL: [Table('Cities', 'db-b')].
The "solve it all" solution: use Service Operations instead of Aurelius Crud Entities. Those are the two "operation modes" in XData. The latter is the "super RAD" one. And former is the "normal" and "100% flexible" one. If you are getting stuck in the RAD option, just to to the one you can do whatever you want. Create a service endpoint, you don't even need to use Aurelius if you don't want to, just execute a query in TFDQuery and process the request there as you wish.
Thank you very much both @Mika_Koistinen and @wlandgraf for your suggestions - I will try them all, hoping to work it out.
Wagner, about suggestion 5... I guess I need anyway two separate connections, one to each database and two separate connection pools to optimize the traffic, isn't it.
I've also seen there's a MultiDBconnectionPool component, but I've not found real demos for it.
If you need separate connections, yes. I was assuming that you would access both databases from a single connection.
This is for dynamic pools, in your case you will only two static ones, so I don't think it's the case. But if you want to check, it's used in the demo "Multitenancy".
I tried suggestion 1. with views... simple & straight to the point, perfect for read-only stuff (it's a view); it's only a pity that it's not possible to auto-generate entities from views... since Aurelius is so "super RAD" capable, there might be a reason for that.
Agree about not being the case for dynamic pools - my scenario is way much simpler.
Then I tried suggestion 5 using the schema trick... but so far I'm not able to have it work.
I'm attaching a demo project with DBs and source from D12.1 (fresh update!).
I used the wizard to create the XData server.
Using both Service operations or Aurelius Crud Entities the tables are always searched from db-b - even if I'm using [Table('Persons', 'db-a')] in code - and of course db-b.Persons is not found.
Indeed, MySQL dialect doesn't support schemas (which is the original intention of the second parameter in Table attribute).
You can workaround it this way, create your own MySQL dialect:
uses {...}, Aurelius.Sql.Interfaces, Aurelius.Sql.Register;
TMySQLCustomSQLGenerator = class(TMySQLSQLGenerator)
protected
function GetSupportedFeatures: TDBFeatures; override;
end;
function TMySQLCustomSQLGenerator.GetSupportedFeatures: TDBFeatures;
begin
Result := inherited GetSupportedFeatures;
Result := Result + [TDBFeature.Schemas];
end;
@wlandgraf I added your code in the data module (the one generated by XData Server wizard) and it works great.
I only had to modify db names from db-a and db-b to dba and dbb.
I guess that unless backticks are added, MySQL / MariaDB is not correctly understanding the SQL statement... anyway it's better to choose standard/better db names since the beginning!
In order to understand your solution, I've seen that the current code in Aurelius.Sql.MySQL.pas is
function TMySQLSQLGenerator.GetSupportedFeatures: TDBFeatures;
begin
Result := AllDBFeatures - [TDBFeature.Sequences, TDBFeature.RetrieveIdOnInsert, TDBFeature.Schemas];
end;
so, the overriding restores back the Schema feature... was that just a design choice not to support MySQL schemas because in MySQL databases and schemas are basically the same thing (no distinction)? Other DBMSs refer schemas as collection of tables; and databases as collection of schemas... correct?
There is no concept of schemas in MySQL, that's why it's not present. It's not exactly the same as a different database. It affects other command like CREATE TABLE, as well the retrieval of existing tables (and schemes) in the database.