summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/association/dict_of_sets_with_default.py2
-rw-r--r--examples/association/proxied_association.py4
-rw-r--r--examples/custom_attributes/custom_management.py9
-rw-r--r--examples/custom_attributes/listen_for_events.py2
-rw-r--r--examples/dynamic_dict/dynamic_dict.py5
-rw-r--r--examples/large_collection/large_collection.py3
-rw-r--r--examples/nested_sets/nested_sets.py98
-rw-r--r--examples/versioning/test_versioning.py11
-rw-r--r--examples/vertical/dictlike-polymorphic.py3
-rw-r--r--examples/vertical/dictlike.py14
-rw-r--r--lib/sqlalchemy/dialects/oracle/base.py6
-rw-r--r--lib/sqlalchemy/dialects/postgresql/base.py5
-rw-r--r--lib/sqlalchemy/ext/serializer.py2
-rw-r--r--lib/sqlalchemy/types.py4
-rw-r--r--test/profiles.txt14
15 files changed, 101 insertions, 81 deletions
diff --git a/examples/association/dict_of_sets_with_default.py b/examples/association/dict_of_sets_with_default.py
index 63c0f4531..9a43e300c 100644
--- a/examples/association/dict_of_sets_with_default.py
+++ b/examples/association/dict_of_sets_with_default.py
@@ -69,7 +69,7 @@ if __name__ == '__main__':
session.add_all([
A(collections={
- "1":set([1, 2, 3]),
+ "1": set([1, 2, 3]),
})
])
session.commit()
diff --git a/examples/association/proxied_association.py b/examples/association/proxied_association.py
index 96e9e6b07..7f4d611a7 100644
--- a/examples/association/proxied_association.py
+++ b/examples/association/proxied_association.py
@@ -96,6 +96,6 @@ if __name__ == '__main__':
# print customers who bought 'MySQL Crowbar' on sale
orders = session.query(Order).\
join('order_items', 'item').\
- filter(Item.description=='MySQL Crowbar').\
+ filter(Item.description == 'MySQL Crowbar').\
filter(Item.price > OrderItem.price)
- print [order.customer_name for order in orders]
+ print [o.customer_name for o in orders]
diff --git a/examples/custom_attributes/custom_management.py b/examples/custom_attributes/custom_management.py
index 12b745db6..2199e0138 100644
--- a/examples/custom_attributes/custom_management.py
+++ b/examples/custom_attributes/custom_management.py
@@ -64,7 +64,8 @@ class MyClass(object):
if __name__ == '__main__':
- meta = MetaData(create_engine('sqlite://'))
+ engine = create_engine('sqlite://')
+ meta = MetaData()
table1 = Table('table1', meta,
Column('id', Integer, primary_key=True),
@@ -73,7 +74,7 @@ if __name__ == '__main__':
Column('id', Integer, primary_key=True),
Column('name', Text),
Column('t1id', Integer, ForeignKey('table1.id')))
- meta.create_all()
+ meta.create_all(engine)
class A(MyClass):
pass
@@ -82,7 +83,7 @@ if __name__ == '__main__':
pass
mapper(A, table1, properties={
- 'bs':relationship(B)
+ 'bs': relationship(B)
})
mapper(B, table2)
@@ -92,7 +93,7 @@ if __name__ == '__main__':
assert a1.name == 'a1'
assert a1.bs[0].name == 'b1'
- sess = Session()
+ sess = Session(engine)
sess.add(a1)
sess.commit()
diff --git a/examples/custom_attributes/listen_for_events.py b/examples/custom_attributes/listen_for_events.py
index 80a33c0d8..4cdf4b056 100644
--- a/examples/custom_attributes/listen_for_events.py
+++ b/examples/custom_attributes/listen_for_events.py
@@ -4,7 +4,7 @@ and listen for change events.
"""
-from sqlalchemy import event, orm
+from sqlalchemy import event
def configure_listener(class_, key, inst):
def append(instance, value, initiator):
diff --git a/examples/dynamic_dict/dynamic_dict.py b/examples/dynamic_dict/dynamic_dict.py
index 725d65619..ec7c8e918 100644
--- a/examples/dynamic_dict/dynamic_dict.py
+++ b/examples/dynamic_dict/dynamic_dict.py
@@ -32,14 +32,15 @@ from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship
-engine=create_engine('sqlite://', echo=True)
+engine = create_engine('sqlite://', echo=True)
Base = declarative_base(engine)
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
name = Column(String(50))
- _collection = relationship("Child", lazy="dynamic", cascade="all, delete-orphan")
+ _collection = relationship("Child", lazy="dynamic",
+ cascade="all, delete-orphan")
@property
def child_map(self):
diff --git a/examples/large_collection/large_collection.py b/examples/large_collection/large_collection.py
index b3aa5299d..3e386ae64 100644
--- a/examples/large_collection/large_collection.py
+++ b/examples/large_collection/large_collection.py
@@ -14,7 +14,8 @@ org_table = Table('organizations', meta,
member_table = Table('members', meta,
Column('member_id', Integer, primary_key=True),
Column('member_name', String(50), nullable=False, key='name'),
- Column('org_id', Integer, ForeignKey('organizations.org_id', ondelete="CASCADE")),
+ Column('org_id', Integer,
+ ForeignKey('organizations.org_id', ondelete="CASCADE")),
mysql_engine='InnoDB')
diff --git a/examples/nested_sets/nested_sets.py b/examples/nested_sets/nested_sets.py
index e35ea61c3..8225a09f2 100644
--- a/examples/nested_sets/nested_sets.py
+++ b/examples/nested_sets/nested_sets.py
@@ -6,47 +6,17 @@ http://www.intelligententerprise.com/001020/celko.jhtml
from sqlalchemy import (create_engine, Column, Integer, String, select, case,
func)
-from sqlalchemy.orm import sessionmaker, MapperExtension, aliased
+from sqlalchemy.orm import Session, aliased
from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy import event
-engine = create_engine('sqlite://', echo=True)
Base = declarative_base()
-class NestedSetExtension(MapperExtension):
- def before_insert(self, mapper, connection, instance):
- if not instance.parent:
- instance.left = 1
- instance.right = 2
- else:
- personnel = mapper.mapped_table
- right_most_sibling = connection.scalar(
- select([personnel.c.rgt]).where(personnel.c.emp==instance.parent.emp)
- )
-
- connection.execute(
- personnel.update(personnel.c.rgt>=right_most_sibling).values(
- lft = case(
- [(personnel.c.lft>right_most_sibling, personnel.c.lft + 2)],
- else_ = personnel.c.lft
- ),
- rgt = case(
- [(personnel.c.rgt>=right_most_sibling, personnel.c.rgt + 2)],
- else_ = personnel.c.rgt
- )
- )
- )
- instance.left = right_most_sibling
- instance.right = right_most_sibling + 1
-
- # before_update() would be needed to support moving of nodes
- # after_delete() would be needed to support removal of nodes.
- # [ticket:1172] needs to be implemented for deletion to work as well.
-
class Employee(Base):
__tablename__ = 'personnel'
__mapper_args__ = {
- 'extension':NestedSetExtension(),
- 'batch':False # allows extension to fire for each instance before going to the next.
+ 'batch': False # allows extension to fire for each
+ # instance before going to the next.
}
parent = None
@@ -59,9 +29,44 @@ class Employee(Base):
def __repr__(self):
return "Employee(%s, %d, %d)" % (self.emp, self.left, self.right)
+@event.listens_for(Employee, "before_insert")
+def before_insert(mapper, connection, instance):
+ if not instance.parent:
+ instance.left = 1
+ instance.right = 2
+ else:
+ personnel = mapper.mapped_table
+ right_most_sibling = connection.scalar(
+ select([personnel.c.rgt]).
+ where(personnel.c.emp == instance.parent.emp)
+ )
+
+ connection.execute(
+ personnel.update(
+ personnel.c.rgt >= right_most_sibling).values(
+ lft=case(
+ [(personnel.c.lft > right_most_sibling,
+ personnel.c.lft + 2)],
+ else_=personnel.c.lft
+ ),
+ rgt=case(
+ [(personnel.c.rgt >= right_most_sibling,
+ personnel.c.rgt + 2)],
+ else_=personnel.c.rgt
+ )
+ )
+ )
+ instance.left = right_most_sibling
+ instance.right = right_most_sibling + 1
+
+ # before_update() would be needed to support moving of nodes
+ # after_delete() would be needed to support removal of nodes.
+
+engine = create_engine('sqlite://', echo=True)
+
Base.metadata.create_all(engine)
-session = sessionmaker(bind=engine)()
+session = Session(bind=engine)
albert = Employee(emp='Albert')
bert = Employee(emp='Bert')
@@ -81,23 +86,26 @@ fred.parent = chuck
session.add_all([albert, bert, chuck, donna, eddie, fred])
session.commit()
-print session.query(Employee).all()
+print(session.query(Employee).all())
# 1. Find an employee and all his/her supervisors, no matter how deep the tree.
ealias = aliased(Employee)
-print session.query(Employee).\
+print(session.query(Employee).\
filter(ealias.left.between(Employee.left, Employee.right)).\
- filter(ealias.emp=='Eddie').all()
+ filter(ealias.emp == 'Eddie').all())
-#2. Find the employee and all his/her subordinates. (This query has a nice symmetry with the first query.)
-print session.query(Employee).\
+#2. Find the employee and all his/her subordinates.
+# (This query has a nice symmetry with the first query.)
+print(session.query(Employee).\
filter(Employee.left.between(ealias.left, ealias.right)).\
- filter(ealias.emp=='Chuck').all()
+ filter(ealias.emp == 'Chuck').all())
-#3. Find the level of each node, so you can print the tree as an indented listing.
-for indentation, employee in session.query(func.count(Employee.emp).label('indentation') - 1, ealias).\
+#3. Find the level of each node, so you can print the tree
+# as an indented listing.
+for indentation, employee in session.query(
+ func.count(Employee.emp).label('indentation') - 1, ealias).\
filter(ealias.left.between(Employee.left, Employee.right)).\
group_by(ealias.emp).\
- order_by(ealias.left):
- print " " * indentation + str(employee)
+ order_by(ealias.left):
+ print(" " * indentation + str(employee))
diff --git a/examples/versioning/test_versioning.py b/examples/versioning/test_versioning.py
index 9781fdc5d..43e2b0ae1 100644
--- a/examples/versioning/test_versioning.py
+++ b/examples/versioning/test_versioning.py
@@ -5,6 +5,9 @@ from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import clear_mappers, sessionmaker, deferred, relationship
from _lib import ComparableEntity, eq_
+engine = Session = None
+
+
def setup():
global engine
engine = create_engine('sqlite://', echo=True)
@@ -12,16 +15,16 @@ def setup():
class TestVersioning(TestCase):
def setUp(self):
global Base, Session, Versioned
- Base = declarative_base(bind=engine)
- Session = sessionmaker()
+ Base = declarative_base()
+ Session = sessionmaker(engine)
versioned_session(Session)
def tearDown(self):
clear_mappers()
- Base.metadata.drop_all()
+ Base.metadata.drop_all(engine)
def create_tables(self):
- Base.metadata.create_all()
+ Base.metadata.create_all(engine)
def test_plain(self):
class SomeClass(Versioned, Base, ComparableEntity):
diff --git a/examples/vertical/dictlike-polymorphic.py b/examples/vertical/dictlike-polymorphic.py
index c4eb6b5ce..f800eea73 100644
--- a/examples/vertical/dictlike-polymorphic.py
+++ b/examples/vertical/dictlike-polymorphic.py
@@ -254,6 +254,3 @@ if __name__ == '__main__':
q = (session.query(AnimalFact).
filter(with_characteristic(u'cuteness', u'very cute')))
print q.all()
-
- session.close()
- metadata.drop_all(engine)
diff --git a/examples/vertical/dictlike.py b/examples/vertical/dictlike.py
index e288d70ba..71ab77342 100644
--- a/examples/vertical/dictlike.py
+++ b/examples/vertical/dictlike.py
@@ -124,7 +124,7 @@ class VerticalPropertyDictMixin(object):
if __name__ == '__main__':
from sqlalchemy import (MetaData, Table, Column, Integer, Unicode,
- ForeignKey, UnicodeText, and_, not_)
+ ForeignKey, UnicodeText, and_, not_, create_engine)
from sqlalchemy.orm import mapper, relationship, Session
from sqlalchemy.orm.collections import attribute_mapped_collection
@@ -172,10 +172,9 @@ if __name__ == '__main__':
})
mapper(AnimalFact, facts)
-
- metadata.bind = 'sqlite:///'
- metadata.create_all()
- session = Session()
+ engine = create_engine("sqlite://")
+ metadata.create_all(engine)
+ session = Session(bind=engine)
stoat = Animal(u'stoat')
stoat[u'color'] = u'reddish'
@@ -195,9 +194,9 @@ if __name__ == '__main__':
critter[u'cuteness'] = u'very'
print 'changing cuteness:'
- metadata.bind.echo = True
+ engine.echo = True
session.commit()
- metadata.bind.echo = False
+ engine.echo = False
marten = Animal(u'marten')
marten[u'color'] = u'brown'
@@ -245,4 +244,3 @@ if __name__ == '__main__':
print 'just the facts', q.all()
- metadata.drop_all()
diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py
index e0fa10b4f..831ba5f1b 100644
--- a/lib/sqlalchemy/dialects/oracle/base.py
+++ b/lib/sqlalchemy/dialects/oracle/base.py
@@ -429,6 +429,12 @@ class OracleCompiler(compiler.SQLCompiler):
return "CONTAINS (%s, %s)" % (self.process(binary.left),
self.process(binary.right))
+ def visit_true(self, expr, **kw):
+ return '1'
+
+ def visit_false(self, expr, **kw):
+ return '0'
+
def get_select_hint_text(self, byfroms):
return " ".join(
"/*+ %s */" % text for table, text in byfroms.items()
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py
index 5d9368893..1acdb57b9 100644
--- a/lib/sqlalchemy/dialects/postgresql/base.py
+++ b/lib/sqlalchemy/dialects/postgresql/base.py
@@ -673,7 +673,10 @@ class ARRAY(sqltypes.Concatenable, sqltypes.TypeEngine):
if dim is None:
arr = list(arr)
if dim == 1 or dim is None and (
- not arr or not hasattr(arr[0], '__iter__')):
+ # this has to be (list, tuple), or at least
+ # not hasattr('__iter__'), since Py3K strings
+ # etc. have __iter__
+ not arr or not isinstance(arr[0], (list, tuple))):
if itemproc:
return collection(itemproc(x) for x in arr)
else:
diff --git a/lib/sqlalchemy/ext/serializer.py b/lib/sqlalchemy/ext/serializer.py
index b4c67538a..5a3fb5937 100644
--- a/lib/sqlalchemy/ext/serializer.py
+++ b/lib/sqlalchemy/ext/serializer.py
@@ -141,7 +141,7 @@ def Deserializer(file, metadata=None, scoped_session=None, engine=None):
return class_mapper(cls)
elif type_ == "mapperprop":
mapper, keyname = args.split(':')
- cls = pickle.loads(b64decode(args))
+ cls = pickle.loads(b64decode(mapper))
return class_mapper(cls).attrs[keyname]
elif type_ == "table":
return metadata.tables[args]
diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py
index 1824a9b3f..46cf9e2a1 100644
--- a/lib/sqlalchemy/types.py
+++ b/lib/sqlalchemy/types.py
@@ -1184,7 +1184,9 @@ class Text(String):
In SQL, usually corresponds to CLOB or TEXT. Can also take Python
unicode objects and encode to the database's encoding in bind
- params (and the reverse for result sets.)
+ params (and the reverse for result sets.) In general, TEXT objects
+ do not have a length; while some databases will accept a length
+ argument here, it will be rejected by others.
"""
__visit_name__ = 'text'
diff --git a/test/profiles.txt b/test/profiles.txt
index d465fa3be..c9ba04c24 100644
--- a/test/profiles.txt
+++ b/test/profiles.txt
@@ -1,15 +1,15 @@
# /Users/classic/dev/sqlalchemy/test/profiles.txt
# This file is written out on a per-environment basis.
-# For each test in aaa_profiling, the corresponding function and
+# For each test in aaa_profiling, the corresponding function and
# environment is located within this file. If it doesn't exist,
# the test is skipped.
-# If a callcount does exist, it is compared to what we received.
+# If a callcount does exist, it is compared to what we received.
# assertions are raised if the counts do not match.
-#
-# To add a new callcount test, apply the function_call_count
-# decorator and re-run the tests using the --write-profiles
+#
+# To add a new callcount test, apply the function_call_count
+# decorator and re-run the tests using the --write-profiles
# option - this file will be rewritten including the new count.
-#
+#
# TEST: test.aaa_profiling.test_compiler.CompileTest.test_insert
@@ -217,7 +217,7 @@ test.aaa_profiling.test_zoomark.ZooMarkTest.test_profile_3_properties 2.7_postgr
# TEST: test.aaa_profiling.test_zoomark.ZooMarkTest.test_profile_4_expressions
-test.aaa_profiling.test_zoomark.ZooMarkTest.test_profile_4_expressions 2.7_postgresql_psycopg2_cextensions 10366
+test.aaa_profiling.test_zoomark.ZooMarkTest.test_profile_4_expressions 2.7_postgresql_psycopg2_cextensions 10915
test.aaa_profiling.test_zoomark.ZooMarkTest.test_profile_4_expressions 2.7_postgresql_psycopg2_nocextensions 11982
# TEST: test.aaa_profiling.test_zoomark.ZooMarkTest.test_profile_5_aggregates