What is the best way to build dynamic SQL queries in code based off user input? Concatenating a bunch of strings together in 2018 hardly seems like the best way. Or if it is: is there an existing framework that helps out with it? I see https://github.com/ezequieljuliano/SQLBuilder4Delphi but I'm not quite sure that will actually help make code clearer. I also see FireDAC Macros but again not sure that actually helps the situation. http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Preprocessing_Command_Text_(FireDAC)

What is the best way to build dynamic SQL queries in code based off user input? Concatenating a bunch of strings together in 2018 hardly seems like the best way. Or if it is: is there an existing framework that helps out with it? I see https://github.com/ezequieljuliano/SQLBuilder4Delphi but I'm not quite sure that will actually help make code clearer. I also see FireDAC Macros but again not sure that actually helps the situation. http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Preprocessing_Command_Text_(FireDAC)
https://github.com/ezequieljuliano/SQLBuilder4Delphi

Comments

  1. Some years ago I built a simple query builder to suit my needs, here is a basic introduction:
    stackoverflow.com - Delphi & SQL code adding

    No generics nor fluid API, adapted to D2007 ;)

    ReplyDelete
  2. Have you looked at FireDAC's macros? For example, you can have a query like:

    SELECT * FROM &table_name
    &where_clause
    &order_by

    supply &table_name and leave &where_clause and &order_by macros empty and the query will still execute.

    ReplyDelete
  3. In my experience, you should build off the FireDAC macros to ensure cross database compatibility, but other than that - it is almost always dependent on the use case.

    I usually create small mini-DSLs for building queries specific to a context- as having one generic SQL builder inevitably leads to complex and convoluted code and loss of clarity.

    Permission handing, NULL handling, case handling, transaction handling, try catch handling - all of these are far from trivial to wrap in any form of general wrapper code.

    ReplyDelete

Post a Comment