A buddy of mine and I are going back and forth on where to store our parameterized SQL queries in our classes. We aren’t using an ORM because they are notorious for creating performance issues and performance is critical in this application.
He prefers the query to be stored where it is executed. For instance:
class SomeClass: def some_function(self, x, y): query = "SELECT a, b, c FROM table WHERE q=$1 AND z=$2" stmt = conn.prepare(query) return stmt.fetch(x, y)
I prefer to define the query as a class property then reference it:
class SomeClass: SELECT_SOMETHING = "SELECT a, b, c FROM table WHERE q=$1 AND z=$2" def some_function(self, x, y): stmt = conn.prepare(SomeClass.SELECT_SOMETHING) return stmt.fetch(x, y)
In a large application with dozens of classes and queries, and several large queries, which style easier to read and maintain?