views:

37

answers:

3

I am a little bit reluctant when it comes to mixing SQL queries with my code ... I tend to not like it aesthetically. I was wondering whether there's a better way of storing them, without having to mix them with the code. What I've found until now:

  • Use an ORM and you don't have to worry about storing full SQL strings - this is not an option when you're trying to achieve better speed ... ORM suck when it comes to this, so you're better using SQL
  • Place your queries in a totally separate file that stores only queries, from which you can import them for later use
+1  A: 

Either use ORM or put your queries in the code files where they belong.

If you think your queries are very complicated, you could also use Stored Procedures so the query-related "code" is in the database and not your sourcecode.

ThiefMaster
A: 

I'm a big fan of using stored procedures for this type of clean separation. SQL stored as strings is code leads to all sorts of nasty issues since it formats poorly, and after a few years may lead to a bugfarm of sql injection if you have junior resources working on your codebase.

jskaggz
A: 

What I normally do is have the queries definied as const strings at the begining of the source (given that the nature of the query allows such thing) and then just use the member name to reference a query. Like thise there is much less direct mixing between source language and SQL code.

inflagranti