Is there a standard approach to generating sql dynamically?

后端 未结 8 2333
滥情空心
滥情空心 2021-02-20 00:17

I want to ask how other programmers are producing Dynamic SQL strings for execution as the CommandText of a SQLCommand object.

I am producing parameterized queries conta

相关标签:
8条回答
  • 2021-02-20 00:45

    Usually it's something like this:

    string query= "SELECT {0} FROM .... WHERE {1}"
    StringBuilder selectclause = new StringBuilder();
    StringBuilder wherecaluse = new StringBuilder();
    
    // .... the logic here will vary greatly depending on what your system looks like
    
    MySqlcommand.CommandText = String.Format(query, selectclause.ToString(), whereclause.ToString());
    

    I'm also just getting started out with ORMs. You might want to take a look at one of those. ActiveRecord / Hibernate are some good keywords to google.

    0 讨论(0)
  • 2021-02-20 00:46

    Gulzar and Ryan Lanciaux make good points in mentioning CodeSmith and ORM. Either of those might reduce or eliminate your current burden when it comes to generating dynamic SQL. Your current approach of using parameterized SQL is wise, simply because it protects well against SQL injection attacks.

    Without an actual code sample to comment on, it's difficult to provide an informed alternative to the loops and switch statements you're currently using. But since you mention that you're setting a CommandText property, I would recommend the use of string.Format in your implementation (if you aren't already using it). I think it may make your code easier to restructure, and therefore improve readability and understanding.

    0 讨论(0)
  • 2021-02-20 00:52

    You could try the approach used by code generation tools like CodeSmith. Create a SQL template with placeholders. At runtime, read the template into a string and substitute the placeholders with actual values. This is only useful if all the SQL code follow a pattern.

    0 讨论(0)
  • 2021-02-20 00:53

    I had the need to do this on one of my recent projects. Here is the scheme that I am using for generating the SQL:

    • Each component of the query is represented by an Object (which in my case is a Linq-to-Sql entity that maps to a table in the DB). So I have the following classes: Query, SelectColumn, Join, WhereCondition, Sort, GroupBy. Each of these classes contains all details relating to that component of the query.
    • The last five classes are all related to a Query object. So the Query object itself has collections of each class.
    • Each class has a method that can generate the SQL for the part of the query that it represents. So creating the overall query ends up calling Query.GenerateQuery() which in turn enumerates through all of the sub-collections and calls their respective GenerateQuery() methods

    It is still a bit complicated, but in the end you know where the SQL generation for each individual part of the query originates (and I don't think that there are any big switch statements). And don't forget to use StringBuilder.

    0 讨论(0)
  • 2021-02-20 00:59

    If you really need to do this from code, then an ORM is probably the way to go to try to keep it clean.

    But I'd like to offer an alternative that works well and could avoid the performance problems that accompany dynamic queries, due to changing SQL that requires new query plans to be created, with different demands on indexes.

    Create a stored procedure that accepts all possible parameters, and then use something like this in the where clause:

    where...
    and (@MyParam5 is null or @MyParam5 = Col5)
    

    then, from code, it's much simpler to set the parameter value to DBNull.Value when it is not applicable, rather than changing the SQL string you generate.

    Your DBAs will be much happier with you, because they will have one place to go for query tuning, the SQL will be easy to read, and they won't have to dig through profiler traces to find the many different queries being generated by your code.

    0 讨论(0)
  • 2021-02-20 01:04

    Out of curiousity, have you considered using an ORM for managing your data access. A lot of the functionality you're trying to implement could already be there. It may be something to look at because its best not to re-invent the wheel.

    0 讨论(0)
提交回复
热议问题