summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext/sqlsoup.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2007-02-25 22:44:52 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2007-02-25 22:44:52 +0000
commit962c22c9eda7d2ab7dc0b41bd1c7a52cf0c9d008 (patch)
treef0ab113c7947c80dfea42d4a1bef52217bf6ed96 /lib/sqlalchemy/ext/sqlsoup.py
parent8fa3becd5fac57bb898a0090bafaac377b60f070 (diff)
downloadsqlalchemy-962c22c9eda7d2ab7dc0b41bd1c7a52cf0c9d008.tar.gz
migrated (most) docstrings to pep-257 format, docstring generator using straight <pre> + trim() func
for now. applies most of [ticket:214], compliemnts of Lele Gaifax
Diffstat (limited to 'lib/sqlalchemy/ext/sqlsoup.py')
-rw-r--r--lib/sqlalchemy/ext/sqlsoup.py214
1 files changed, 135 insertions, 79 deletions
diff --git a/lib/sqlalchemy/ext/sqlsoup.py b/lib/sqlalchemy/ext/sqlsoup.py
index 5fb42df23..b899c043d 100644
--- a/lib/sqlalchemy/ext/sqlsoup.py
+++ b/lib/sqlalchemy/ext/sqlsoup.py
@@ -2,28 +2,30 @@
Introduction
============
-SqlSoup provides a convenient way to access database tables without having
-to declare table or mapper classes ahead of time.
+SqlSoup provides a convenient way to access database tables without
+having to declare table or mapper classes ahead of time.
Suppose we have a database with users, books, and loans tables
-(corresponding to the PyWebOff dataset, if you're curious).
-For testing purposes, we'll create this db as follows:
+(corresponding to the PyWebOff dataset, if you're curious). For
+testing purposes, we'll create this db as follows::
>>> from sqlalchemy import create_engine
>>> e = create_engine('sqlite:///:memory:')
>>> for sql in _testsql: e.execute(sql) #doctest: +ELLIPSIS
<...
-Creating a SqlSoup gateway is just like creating an SqlAlchemy engine:
+Creating a SqlSoup gateway is just like creating an SQLAlchemy
+engine::
>>> from sqlalchemy.ext.sqlsoup import SqlSoup
>>> db = SqlSoup('sqlite:///:memory:')
-or, you can re-use an existing metadata:
+or, you can re-use an existing metadata::
>>> db = SqlSoup(BoundMetaData(e))
-You can optionally specify a schema within the database for your SqlSoup:
+You can optionally specify a schema within the database for your
+SqlSoup::
# >>> db.schema = myschemaname
@@ -31,33 +33,34 @@ You can optionally specify a schema within the database for your SqlSoup:
Loading objects
===============
-Loading objects is as easy as this:
+Loading objects is as easy as this::
>>> users = db.users.select()
>>> users.sort()
>>> users
[MappedUsers(name='Joe Student',email='student@example.edu',password='student',classname=None,admin=0), MappedUsers(name='Bhargan Basepair',email='basepair@example.edu',password='basepair',classname=None,admin=1)]
-Of course, letting the database do the sort is better (".c" is short for ".columns"):
+Of course, letting the database do the sort is better (".c" is short for ".columns")::
>>> db.users.select(order_by=[db.users.c.name])
[MappedUsers(name='Bhargan Basepair',email='basepair@example.edu',password='basepair',classname=None,admin=1), MappedUsers(name='Joe Student',email='student@example.edu',password='student',classname=None,admin=0)]
-Field access is intuitive:
+Field access is intuitive::
>>> users[0].email
u'student@example.edu'
-Of course, you don't want to load all users very often. Let's add a WHERE clause.
-Let's also switch the order_by to DESC while we're at it.
+Of course, you don't want to load all users very often. Let's add a
+WHERE clause. Let's also switch the order_by to DESC while we're at
+it::
>>> from sqlalchemy import or_, and_, desc
>>> where = or_(db.users.c.name=='Bhargan Basepair', db.users.c.email=='student@example.edu')
>>> db.users.select(where, order_by=[desc(db.users.c.name)])
[MappedUsers(name='Joe Student',email='student@example.edu',password='student',classname=None,admin=0), MappedUsers(name='Bhargan Basepair',email='basepair@example.edu',password='basepair',classname=None,admin=1)]
-You can also use the select...by methods if you're querying on a single column.
-This allows using keyword arguments as column names:
+You can also use the select...by methods if you're querying on a
+single column. This allows using keyword arguments as column names::
>>> db.users.selectone_by(name='Bhargan Basepair')
MappedUsers(name='Bhargan Basepair',email='basepair@example.edu',password='basepair',classname=None,admin=1)
@@ -66,36 +69,56 @@ This allows using keyword arguments as column names:
Select variants
---------------
-All the SqlAlchemy Query select variants are available.
-Here's a quick summary of these methods:
+All the SQLAlchemy Query select variants are available. Here's a
+quick summary of these methods::
-- get(PK): load a single object identified by its primary key (either a scalar, or a tuple)
-- select(Clause, \*\*kwargs): perform a select restricted by the Clause argument; returns a list of objects. The most common clause argument takes the form "db.tablename.c.columname == value." The most common optional argument is order_by.
-- select_by(\*\*params): select methods ending with _by allow using bare column names. (columname=value) This feels more natural to most Python programmers; the downside is you can't specify order_by or other select options.
-- selectfirst, selectfirst_by: returns only the first object found; equivalent to select(...)[0] or select_by(...)[0], except None is returned if no rows are selected.
-- selectone, selectone_by: like selectfirst or selectfirst_by, but raises if less or more than one object is selected.
-- count, count_by: returns an integer count of the rows selected.
+- ``get(PK)``: load a single object identified by its primary key
+ (either a scalar, or a tuple)
-See the SqlAlchemy documentation for details:
+- ``select(Clause, **kwargs)``: perform a select restricted by the
+ `Clause` argument; returns a list of objects. The most common clause
+ argument takes the form ``db.tablename.c.columname == value``. The
+ most common optional argument is `order_by`.
-- http://www.sqlalchemy.org/docs/datamapping.myt#datamapping_query for general info and examples,
-- http://www.sqlalchemy.org/docs/sqlconstruction.myt for details on constructing WHERE clauses.
+- ``select_by(**params)``: select methods ending with ``_by`` allow
+ using bare column names (``columname=value``). This feels more
+ natural to most Python programmers; the downside is you can't
+ specify ``order_by`` or other select options.
+
+- ``selectfirst``, ``selectfirst_by``: returns only the first object
+ found; equivalent to ``select(...)[0]`` or ``select_by(...)[0]``,
+ except None is returned if no rows are selected.
+
+- ``selectone``, ``selectone_by``: like ``selectfirst`` or
+ ``selectfirst_by``, but raises if less or more than one object is
+ selected.
+
+- ``count``, ``count_by``: returns an integer count of the rows
+ selected.
+
+See the SQLAlchemy documentation for details, `datamapping query`__
+for general info and examples, `sql construction`__ for details on
+constructing ``WHERE`` clauses.
+
+__ http://www.sqlalchemy.org/docs/datamapping.myt#datamapping_query
+__http://www.sqlalchemy.org/docs/sqlconstruction.myt
Modifying objects
=================
-Modifying objects is intuitive:
+Modifying objects is intuitive::
>>> user = _
>>> user.email = 'basepair+nospam@example.edu'
>>> db.flush()
-(SqlSoup leverages the sophisticated SqlAlchemy unit-of-work code, so
-multiple updates to a single object will be turned into a single UPDATE
-statement when you flush.)
+(SqlSoup leverages the sophisticated SQLAlchemy unit-of-work code, so
+multiple updates to a single object will be turned into a single
+``UPDATE`` statement when you flush.)
-To finish covering the basics, let's insert a new loan, then delete it:
+To finish covering the basics, let's insert a new loan, then delete
+it::
>>> book_id = db.books.selectfirst(db.books.c.title=='Regional Variation in Moss').id
>>> db.loans.insert(book_id=book_id, user_name=user.name)
@@ -106,34 +129,39 @@ To finish covering the basics, let's insert a new loan, then delete it:
>>> db.delete(loan)
>>> db.flush()
-You can also delete rows that have not been loaded as objects. Let's do our
-insert/delete cycle once more, this time using the loans table's delete
-method. (For SQLAlchemy experts: note that no flush() call is required since
-this delete acts at the SQL level, not at the Mapper level.) The same
-where-clause construction rules apply here as to the select methods.
+You can also delete rows that have not been loaded as objects. Let's
+do our insert/delete cycle once more, this time using the loans
+table's delete method. (For SQLAlchemy experts: note that no flush()
+call is required since this delete acts at the SQL level, not at the
+Mapper level.) The same where-clause construction rules apply here as
+to the select methods.
+
+::
>>> db.loans.insert(book_id=book_id, user_name=user.name)
MappedLoans(book_id=2,user_name='Bhargan Basepair',loan_date=None)
>>> db.flush()
>>> db.loans.delete(db.loans.c.book_id==2)
-You can similarly update multiple rows at once. This will change the book_id
-to 1 in all loans whose book_id is 2:
+You can similarly update multiple rows at once. This will change the
+book_id to 1 in all loans whose book_id is 2::
>>> db.loans.update(db.loans.c.book_id==2, book_id=1)
>>> db.loans.select_by(db.loans.c.book_id==1)
[MappedLoans(book_id=1,user_name='Joe Student',loan_date=datetime.datetime(2006, 7, 12, 0, 0))]
-
+
Joins
=====
-Occasionally, you will want to pull out a lot of data from related tables all at
-once. In this situation, it is far
-more efficient to have the database perform the necessary join. (Here
-we do not have "a lot of data," but hopefully the concept is still clear.)
-SQLAlchemy is smart enough to recognize that loans has a foreign key
-to users, and uses that as the join condition automatically.
+Occasionally, you will want to pull out a lot of data from related
+tables all at once. In this situation, it is far more efficient to
+have the database perform the necessary join. (Here we do not have *a
+lot of data* but hopefully the concept is still clear.) SQLAlchemy is
+smart enough to recognize that loans has a foreign key to users, and
+uses that as the join condition automatically.
+
+::
>>> join1 = db.join(db.users, db.loans, isouter=True)
>>> join1.select_by(name='Joe Student')
@@ -142,25 +170,26 @@ to users, and uses that as the join condition automatically.
If you're unfortunate enough to be using MySQL with the default MyISAM
storage engine, you'll have to specify the join condition manually,
since MyISAM does not store foreign keys. Here's the same join again,
-with the join condition explicitly specified:
+with the join condition explicitly specified::
>>> db.join(db.users, db.loans, db.users.c.name==db.loans.c.user_name, isouter=True)
<class 'sqlalchemy.ext.sqlsoup.MappedJoin'>
-You can compose arbitrarily complex joins by combining Join objects with
-tables or other joins. Here we combine our first join with the books table:
+You can compose arbitrarily complex joins by combining Join objects
+with tables or other joins. Here we combine our first join with the
+books table::
>>> join2 = db.join(join1, db.books)
>>> join2.select()
[MappedJoin(name='Joe Student',email='student@example.edu',password='student',classname=None,admin=0,book_id=1,user_name='Joe Student',loan_date=datetime.datetime(2006, 7, 12, 0, 0),id=1,title='Mustards I Have Known',published_year='1989',authors='Jones')]
-If you join tables that have an identical column name, wrap your join with "with_labels",
-to disambiguate columns with their table name:
+If you join tables that have an identical column name, wrap your join
+with `with_labels`, to disambiguate columns with their table name::
>>> db.with_labels(join1).c.keys()
['users_name', 'users_email', 'users_password', 'users_classname', 'users_admin', 'loans_book_id', 'loans_user_name', 'loans_loan_date']
-You can also join directly to a labeled object:
+You can also join directly to a labeled object::
>>> labeled_loans = db.with_labels(db.loans)
>>> db.join(db.users, labeled_loans, isouter=True).c.keys()
@@ -173,27 +202,30 @@ Advanced Use
Accessing the Session
---------------------
-SqlSoup uses a SessionContext to provide thread-local sessions. You can
-get a reference to the current one like this:
+SqlSoup uses a SessionContext to provide thread-local sessions. You
+can get a reference to the current one like this::
>>> from sqlalchemy.ext.sqlsoup import objectstore
>>> session = objectstore.current
-Now you have access to all the standard session-based SA features, such
-as transactions. (SqlSoup's flush() is normally transactionalized, but
-you can perform manual transaction management if you need a transaction
-to span multiple flushes.)
+Now you have access to all the standard session-based SA features,
+such as transactions. (SqlSoup's ``flush()`` is normally
+transactionalized, but you can perform manual transaction management
+if you need a transaction to span multiple flushes.)
Mapping arbitrary Selectables
-----------------------------
-SqlSoup can map any SQLAlchemy Selectable with the map method. Let's map a
-Select object that uses an aggregate function; we'll use the SQLAlchemy Table
-that SqlSoup introspected as the basis. (Since we're not mapping to a simple
-table or join, we need to tell SQLAlchemy how to find the "primary key," which
-just needs to be unique within the select, and not necessarily correspond to a
-"real" PK in the database.)
+SqlSoup can map any SQLAlchemy ``Selectable`` with the map
+method. Let's map a ``Select`` object that uses an aggregate function;
+we'll use the SQLAlchemy ``Table`` that SqlSoup introspected as the
+basis. (Since we're not mapping to a simple table or join, we need to
+tell SQLAlchemy how to find the *primary key* which just needs to be
+unique within the select, and not necessarily correspond to a *real*
+PK in the database.)
+
+::
>>> from sqlalchemy import select, func
>>> b = db.books._table
@@ -202,20 +234,21 @@ just needs to be unique within the select, and not necessarily correspond to a
>>> years_with_count = db.map(s, primary_key=[s.c.published_year])
>>> years_with_count.select_by(published_year='1989')
[MappedBooks(published_year='1989',n=1)]
-
-Obviously if we just wanted to get a list of counts associated with book years
-once, raw SQL is going to be less work. The advantage of mapping a Select is
-reusability, both standalone and in Joins. (And if you go to full SQLAlchemy,
-you can perform mappings like this directly to your object models.)
+
+Obviously if we just wanted to get a list of counts associated with
+book years once, raw SQL is going to be less work. The advantage of
+mapping a Select is reusability, both standalone and in Joins. (And if
+you go to full SQLAlchemy, you can perform mappings like this directly
+to your object models.)
Raw SQL
-------
-You can access the SqlSoup's ``engine`` attribute to compose SQL directly.
-The engine's ``execute`` method corresponds
-to the one of a DBAPI cursor, and returns a ``ResultProxy`` that has ``fetch`` methods
-you would also see on a cursor.
+You can access the SqlSoup's `engine` attribute to compose SQL
+directly. The engine's ``execute`` method corresponds to the one of a
+DBAPI cursor, and returns a ``ResultProxy`` that has ``fetch`` methods
+you would also see on a cursor::
>>> rp = db.engine.execute('select name, email from users order by name')
>>> for name, email in rp.fetchall(): print name, email
@@ -230,14 +263,16 @@ Extra tests
Boring tests here. Nothing of real expository value.
+::
+
>>> db.users.select(db.users.c.classname==None, order_by=[db.users.c.name])
[MappedUsers(name='Bhargan Basepair',email='basepair+nospam@example.edu',password='basepair',classname=None,admin=1), MappedUsers(name='Joe Student',email='student@example.edu',password='student',classname=None,admin=0)]
-
+
>>> db.nopk
Traceback (most recent call last):
...
PKNotFoundError: table 'nopk' does not have a primary key defined
-
+
>>> db.nosuchtable
Traceback (most recent call last):
...
@@ -282,7 +317,7 @@ CREATE TABLE users (
CREATE TABLE loans (
book_id int PRIMARY KEY REFERENCES books(id),
- user_name varchar(32) references users(name)
+ user_name varchar(32) references users(name)
ON DELETE SET NULL ON UPDATE CASCADE,
loan_date datetime DEFAULT current_timestamp
);
@@ -299,7 +334,7 @@ values('Regional Variation in Moss', '1971', 'Flim and Flam');
insert into loans(book_id, user_name, loan_date)
values (
- (select min(id) from books),
+ (select min(id) from books),
(select name from users where name like 'Joe%'),
'2006-07-12 0:0:0')
;
@@ -330,30 +365,37 @@ def _ddl_error(cls):
msg = 'SQLSoup can only modify mapped Tables (found: %s)' \
% cls._table.__class__.__name__
raise InvalidRequestError(msg)
+
class SelectableClassType(type):
def insert(cls, **kwargs):
_ddl_error(cls)
+
def delete(cls, *args, **kwargs):
_ddl_error(cls)
+
def update(cls, whereclause=None, values=None, **kwargs):
_ddl_error(cls)
+
def _selectable(cls):
return cls._table
+
def __getattr__(cls, attr):
if attr == '_query':
# called during mapper init
raise AttributeError()
return getattr(cls._query, attr)
+
class TableClassType(SelectableClassType):
def insert(cls, **kwargs):
o = cls()
o.__dict__.update(kwargs)
return o
+
def delete(cls, *args, **kwargs):
cls._table.delete(*args, **kwargs).execute()
+
def update(cls, whereclause=None, values=None, **kwargs):
cls._table.update(whereclause, values).execute(**kwargs)
-
def _is_outer_join(selectable):
if not isinstance(selectable, sql.Join):
@@ -384,6 +426,7 @@ def class_for_table(selectable, **mapper_kwargs):
klass = TableClassType(mapname, (object,), {})
else:
klass = SelectableClassType(mapname, (object,), {})
+
def __cmp__(self, o):
L = self.__class__.c.keys()
L.sort()
@@ -393,6 +436,7 @@ def class_for_table(selectable, **mapper_kwargs):
except AttributeError:
raise TypeError('unable to compare with %s' % o.__class__)
return cmp(t1, t2)
+
def __repr__(self):
import locale
encoding = locale.getdefaultlocale()[1] or 'ascii'
@@ -403,6 +447,7 @@ def class_for_table(selectable, **mapper_kwargs):
value = value.encode(encoding)
L.append("%s=%r" % (k, value))
return '%s(%s)' % (self.__class__.__name__, ','.join(L))
+
for m in ['__cmp__', '__repr__']:
setattr(klass, m, eval(m))
klass._table = selectable
@@ -416,10 +461,12 @@ def class_for_table(selectable, **mapper_kwargs):
class SqlSoup:
def __init__(self, *args, **kwargs):
+ """Initialize a new ``SqlSoup``.
+
+ `args` may either be an ``SQLEngine`` or a set of arguments
+ suitable for passing to ``create_engine``.
"""
- args may either be an SQLEngine or a set of arguments suitable
- for passing to create_engine
- """
+
# meh, sometimes having method overloading instead of kwargs would be easier
if isinstance(args[0], MetaData):
args = list(args)
@@ -431,15 +478,21 @@ class SqlSoup:
self._metadata = metadata
self._cache = {}
self.schema = None
+
def engine(self):
return self._metadata._engine
+
engine = property(engine)
+
def delete(self, *args, **kwargs):
objectstore.delete(*args, **kwargs)
+
def flush(self):
objectstore.get_session().flush()
+
def clear(self):
objectstore.clear()
+
def map(self, selectable, **kwargs):
try:
t = self._cache[selectable]
@@ -447,12 +500,15 @@ class SqlSoup:
t = class_for_table(selectable, **kwargs)
self._cache[selectable] = t
return t
+
def with_labels(self, item):
# TODO give meaningful aliases
return self.map(item._selectable().select(use_labels=True).alias('foo'))
+
def join(self, *args, **kwargs):
j = join(*args, **kwargs)
return self.map(j)
+
def __getattr__(self, attr):
try:
t = self._cache[attr]