| | 38 | |
| | 39 | try: |
| | 40 | from sqlalchemy.orm import ScopedSession |
| | 41 | except ImportError: |
| | 42 | # Not on sqlalchemy version 0.4 |
| | 43 | ScopedSession = type(None) |
| | 44 | |
| | 45 | def _do_mapping(session, cls, *args, **kwargs): |
| | 46 | if session is None: |
| | 47 | return mapper(cls, *args, **kwargs) |
| | 48 | elif isinstance(session, ScopedSession): |
| | 49 | return session.mapper(cls, *args, **kwargs) |
| | 50 | elif isinstance(session, SessionContext): |
| | 51 | extension = kwargs.pop('extension', None) |
| | 52 | if extension is not None: |
| | 53 | if not isinstance(extension, list): |
| | 54 | extension = [extension] |
| | 55 | extension.append(session.mapper_extension) |
| | 56 | else: |
| | 57 | extension = session.mapper_extension |
| | 58 | |
| | 59 | class query(object): |
| | 60 | def __getattr__(s, key): |
| | 61 | return getattr(session.registry().query(cls), key) |
| | 62 | def __call__(s): |
| | 63 | return session.registry().query(cls) |
| | 64 | |
| | 65 | if not 'query' in cls.__dict__: |
| | 66 | cls.query = query() |
| | 67 | |
| | 68 | return mapper(cls, extension=extension, *args, **kwargs) |
| | 677 | # session methods |
| | 678 | def flush(self, *args, **kwargs): |
| | 679 | return object_session(self).flush([self], *args, **kwargs) |
| | 680 | |
| | 681 | def delete(self, *args, **kwargs): |
| | 682 | return object_session(self).delete(self, *args, **kwargs) |
| | 683 | |
| | 684 | def expire(self, *args, **kwargs): |
| | 685 | return object_session(self).expire(self, *args, **kwargs) |
| | 686 | |
| | 687 | def refresh(self, *args, **kwargs): |
| | 688 | return object_session(self).refresh(self, *args, **kwargs) |
| | 689 | |
| | 690 | def expunge(self, *args, **kwargs): |
| | 691 | return object_session(self).expunge(self, *args, **kwargs) |
| | 692 | |
| | 693 | # This bunch of session methods, along with all the query methods below |
| | 694 | # only make sense when using a global/scoped/contextual session. |
| | 695 | def _global_session(self): |
| | 696 | return self._descriptor.session.registry() |
| | 697 | _global_session = property(_global_session) |
| | 698 | |
| | 699 | def merge(self, *args, **kwargs): |
| | 700 | return self._global_session.merge(self, *args, **kwargs) |
| | 701 | |
| | 702 | def save(self, *args, **kwargs): |
| | 703 | return self._global_session.save(self, *args, **kwargs) |
| | 704 | |
| | 705 | def update(self, *args, **kwargs): |
| | 706 | return self._global_session.update(self, *args, **kwargs) |
| | 707 | |
| | 708 | def save_or_update(self, *args, **kwargs): |
| | 709 | return self._global_session.save_or_update(self, *args, **kwargs) |
| | 710 | |
| | 711 | # query methods |
| | 744 | |
| | 745 | def select_by(cls, *args, **kwargs): |
| | 746 | warnings.warn("The select_by method on the class is deprecated." |
| | 747 | "You should use cls.query.filter_by(...).all()", |
| | 748 | DeprecationWarning, stacklevel=2) |
| | 749 | return cls.query.filter_by(*args, **kwargs).all() |
| | 750 | select_by = classmethod(select_by) |
| | 751 | |
| | 752 | def selectfirst(cls, *args, **kwargs): |
| | 753 | warnings.warn("The selectfirst method on the class is deprecated." |
| | 754 | "You should use cls.query.filter(...).first()", |
| | 755 | DeprecationWarning, stacklevel=2) |
| | 756 | return cls.query.filter(*args, **kwargs).first() |
| | 757 | selectfirst = classmethod(selectfirst) |
| | 758 | |
| | 759 | def selectfirst_by(cls, *args, **kwargs): |
| | 760 | warnings.warn("The selectfirst_by method on the class is deprecated." |
| | 761 | "You should use cls.query.filter_by(...).first()", |
| | 762 | DeprecationWarning, stacklevel=2) |
| | 763 | return cls.query.filter_by(*args, **kwargs).first() |
| | 764 | selectfirst_by = classmethod(selectfirst_by) |
| | 765 | |
| | 766 | def selectone(cls, *args, **kwargs): |
| | 767 | warnings.warn("The selectone method on the class is deprecated." |
| | 768 | "You should use cls.query.filter(...).one()", |
| | 769 | DeprecationWarning, stacklevel=2) |
| | 770 | return cls.query.filter(*args, **kwargs).one() |
| | 771 | selectone = classmethod(selectone) |
| | 772 | |
| | 773 | def selectone_by(cls, *args, **kwargs): |
| | 774 | warnings.warn("The selectone_by method on the class is deprecated." |
| | 775 | "You should use cls.query.filter_by(...).one()", |
| | 776 | DeprecationWarning, stacklevel=2) |
| | 777 | return cls.query.filter_by(*args, **kwargs).one() |
| | 778 | selectone_by = classmethod(selectone_by) |
| | 779 | |
| | 780 | def join_to(cls, *args, **kwargs): |
| | 781 | warnings.warn("The join_to method on the class is deprecated." |
| | 782 | "You should use cls.query.join(...)", |
| | 783 | DeprecationWarning, stacklevel=2) |
| | 784 | return cls.query.join_to(*args, **kwargs).all() |
| | 785 | join_to = classmethod(join_to) |
| | 786 | |
| | 787 | def join_via(cls, *args, **kwargs): |
| | 788 | warnings.warn("The join_via method on the class is deprecated." |
| | 789 | "You should use cls.query.join(...)", |
| | 790 | DeprecationWarning, stacklevel=2) |
| | 791 | return cls.query.join_via(*args, **kwargs).all() |
| | 792 | join_via = classmethod(join_via) |
| | 793 | |
| | 794 | def count(cls, *args, **kwargs): |
| | 795 | warnings.warn("The count method on the class is deprecated." |
| | 796 | "You should use cls.query.filter(...).count()", |
| | 797 | DeprecationWarning, stacklevel=2) |
| | 798 | return cls.query.filter(*args, **kwargs).count() |
| | 799 | count = classmethod(count) |
| | 800 | |
| | 801 | def count_by(cls, *args, **kwargs): |
| | 802 | warnings.warn("The count_by method on the class is deprecated." |
| | 803 | "You should use cls.query.filter_by(...).count()", |
| | 804 | DeprecationWarning, stacklevel=2) |
| | 805 | return cls.query.filter_by(*args, **kwargs).count() |
| | 806 | count_by = classmethod(count_by) |
| | 807 | |
| | 808 | def options(cls, *args, **kwargs): |
| | 809 | warnings.warn("The options method on the class is deprecated." |
| | 810 | "You should use cls.query.options(...)", |
| | 811 | DeprecationWarning, stacklevel=2) |
| | 812 | return cls.query.options(*args, **kwargs) |
| | 813 | options = classmethod(options) |
| | 814 | |
| | 815 | def instances(cls, *args, **kwargs): |
| | 816 | warnings.warn("The instances method on the class is deprecated." |
| | 817 | "You should use cls.query.instances(...)", |
| | 818 | DeprecationWarning, stacklevel=2) |
| | 819 | return cls.query.instances(*args, **kwargs) |
| | 820 | instances = classmethod(instances) |
| | 821 | |