Namespaces/introspection: collecting sql strings for validation

George Sakkis george.sakkis at gmail.com
Sun Apr 22 18:56:43 EDT 2007


On Apr 21, 4:16 pm, Martin Drautzburg <Martin.Drautzb... at web.de>
wrote:
> I would like to validate sql strings, which are spread all over the
> code, i.e. I run ("prepare") them against a database to see if it happy
> with the statements. Spelling errors in sql have been a major pain for
> me.
>
> The statements will not be assembled from smaller pieces, but they will
> not neccessarily be defined at module level. I could live with class
> level, but method level would be best. And I definitely don't want to
> parse the source file, but I am willing to mark the strings so it is
> easier to tell sql from non-sql.
>
> So the code will look like this
>
> class Foo:(...):
>         def aQuery(...):
>                 stmt = """
>                         -- checkSql
>                         select 1 from dual
>                 """
>                 executeQuery()
>
> at the end of the file I would like to write something like
> if (...):
>         validateSql()
>
> The validateSql() is the problem. It would be imported from elsewhere.
> and has to do some serious magic. It would have to lookup its caller's
> module, find all the classes (and methods, if we go that far) extract
> the constants, check if any of them are an SQL statement and validate
> it.
>
> The problem is the first part: how can I lookup the callers module and
> the classobjs defined in there? Or finding any constant strings in the
> caller's module would also be just fine. Or is there a completely
> different way to do such a thing?

Yes, there is: use an ORM to do the SQL generation for you. Check out
SQLAlchemy, it will buy you much more than what you asked for.

George




More information about the Python-list mailing list