There are currently two ways to construct SQL expressions with SQLAlchemy without any knowledge of a database connection or implementation.

One is to use the ProxyEngine, which receives a connect() call when you are ready to connect it to something, which occurs only within the scope of the current thread:

        e = ProxyEngine()
        s = Table('mytable', e, Column()...).select()

        e.connect('someengine://')
        s.execute()

        e.connect('someotherengine://')
        s.execute()

This is the way everyone who needs their app to run before a database is defined has been doing it.

Also, if you create a Table without an explicit engine, it uses a "default" ProxyEngine that you can later connect to databases via the "global_connect()" function, so you can also build up your tables without knowing anything about engines at all.

The other way, which is a little more "underground" and is currently used just in some of the unit tests, is to construct using TableClause and ColumnClause objects, which are the "lexical" component to Table and Column objects:

        t = table('mytable', column('col1'), column('col2'))
        s = t.select()

        print s  # will print the select query with a default compiler

        # compile 's' against a SQLite engine
        e = create_engine('sqlite://')
        c = s.compile(e)

        # or compile 's' against SQLite's compiler
        c = sqlalchemy.databases.sqlite.SQLiteCompiler(s)

        # execute
        c.execute()

        # compile 's' against postgres:
        e = create_engine('postgres://')
        c = s.compile(e)
        # execute
        c.execute()

This was a refactoring I came up with after reading the blog post on SQL-API, to cleanly separate the concepts of "lexical" and "schema" constructs. TableClause and ColumnClause, since they arent related to any kind of "schema", dont have any "unique-per-tablename" type behavior, and also dont have the benefit of column types or foreign keys (this might change at some point). I have yet to work out a full set of patterns which can "upgrade" these lexical objects to "schema-aware" objects in a straightforward way.
        



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to