Pinderkent

Pain and glory from the trenches of the IT world.

Why not just write the SQL code?

Posted on Saturday, January 24, 2009 at 2:28 AM.

Lately, there has been a trend towards taking a high-level language, and using it to write SQL-like queries. In the .NET world, the best known example is LINQ. Today I saw a similar, albeit much more simple, approach for Python.

These approaches differ from many ORM systems in that we're still thinking in terms of raw queries, and not as much in terms of objects and the relationships between them, and how the data maps to tables and the relationships map to constraints. Likewise, they differ from SQL embedded within another language, in that we don't actually write much, if any, SQL code.

But after looking at the example Python code, I'm not sure I understand how it is better than just writing the SQL code in the first place. We're still making use of most of the same concepts as in SQL. The general form of the query is the same. We're selecting rows containing certain columns from joined tables or subqueries, subject to certain filters, and sorted in a particular order. There's nothing remarkable happening there.

If anything, we end up writing queries that are nearly identical to the equivalent SQL, but instead using a syntax that is more awkward to work with that SQL is. And we don't gain any extra functionality or abilities in exchange for this loss of developer productivity. We don't benefit in any way by avoiding SQL code, and instead just writing Python code that is virtually identical to that SQL code, but with a more awkward syntax.

Most decent programming languages offer some way to easily interface with a wide variety of database servers, typically also offering easy ways to parameterize the SQL code passed to the database to increase the level of security. So for the vast, vast majority of database queries, we should be able to put the SQL code inline, parameterize it, and pass in the parameter values when we execute the query. We should never have to resort to string concatenation, or anything of that sort. We just write pure SQL, within a string in the host language. It's as simple as that. There is absolutely no need for the SQL-like adaptations of the syntaxes of non-SQL programming languages.

Permalink: http://pinderkent.phumblog.com/post/2009/01/why_not_just_write_the_sql_code
Share:
Feeds
  • RSS 2.0 Feed
  • Atom 2.0 Feed
Tags
Archives