~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to lib/sqlalchemy/orm/query.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski
  • Date: 2013-10-28 22:29:40 UTC
  • mfrom: (1.4.24)
  • Revision ID: package-import@ubuntu.com-20131028222940-wvyqffl4g617caun
Tags: 0.8.3-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
299
299
 
300
300
    @property
301
301
    def _mapper_entities(self):
302
 
        # TODO: this is wrong, its hardcoded to "primary entity" when
303
 
        # for the case of __all_equivs() it should not be
304
 
        # the name of this accessor is wrong too
305
302
        for ent in self._entities:
306
 
            if hasattr(ent, 'primary_entity'):
 
303
            if isinstance(ent, _MapperEntity):
307
304
                yield ent
308
305
 
309
306
    def _joinpoint_zero(self):
575
572
 
576
573
    @property
577
574
    def selectable(self):
 
575
        """Return the :class:`.Select` object emitted by this :class:`.Query`.
 
576
 
 
577
        Used for :func:`.inspect` compatibility, this is equivalent to::
 
578
 
 
579
            query.enable_eagerloads(False).with_labels().statement
 
580
 
 
581
        """
578
582
        return self.__clause_element__()
579
583
 
580
584
    def __clause_element__(self):
714
718
        loading, the full result for all rows is fetched which generally
715
719
        defeats the purpose of :meth:`~sqlalchemy.orm.query.Query.yield_per`.
716
720
 
717
 
        Also note that many DBAPIs do not "stream" results, pre-buffering
718
 
        all rows before making them available, including mysql-python and
719
 
        psycopg2.  :meth:`~sqlalchemy.orm.query.Query.yield_per` will also
720
 
        set the ``stream_results`` execution
721
 
        option to ``True``, which currently is only understood by psycopg2
722
 
        and causes server side cursors to be used.
 
721
        Also note that while :meth:`~sqlalchemy.orm.query.Query.yield_per`
 
722
        will set the ``stream_results`` execution option to True, currently
 
723
        this is only understood by :mod:`~sqlalchemy.dialects.postgresql.psycopg2` dialect
 
724
        which will stream results using server side cursors instead of pre-buffer
 
725
        all rows for this query. Other DBAPIs pre-buffer all rows before
 
726
        making them available.
723
727
 
724
728
        """
725
729
        self._yield_per = count
2488
2492
        .. versionadded:: 0.8.1
2489
2493
 
2490
2494
        """
2491
 
        return sql.exists(self.with_entities('1').statement)
 
2495
        return sql.exists(self.statement.with_only_columns(['1']))
2492
2496
 
2493
2497
    def count(self):
2494
2498
        """Return a count of rows this Query would return.
2559
2563
            The expression evaluator currently doesn't account for differing
2560
2564
            string collations between the database and Python.
2561
2565
 
2562
 
        Returns the number of rows deleted, excluding any cascades.
2563
 
 
2564
 
        The method does *not* offer in-Python cascading of relationships - it
2565
 
        is assumed that ON DELETE CASCADE is configured for any foreign key
2566
 
        references which require it. The Session needs to be expired (occurs
2567
 
        automatically after commit(), or call expire_all()) in order for the
2568
 
        state of dependent objects subject to delete or delete-orphan cascade
2569
 
        to be correctly represented.
2570
 
 
2571
 
        Note that the :meth:`.MapperEvents.before_delete` and
2572
 
        :meth:`.MapperEvents.after_delete`
2573
 
        events are **not** invoked from this method.  It instead
2574
 
        invokes :meth:`.SessionEvents.after_bulk_delete`.
 
2566
        :return: the count of rows matched as returned by the database's
 
2567
          "row count" feature.
 
2568
 
 
2569
        This method has several key caveats:
 
2570
 
 
2571
        * The method does **not** offer in-Python cascading of relationships - it
 
2572
          is assumed that ON DELETE CASCADE/SET NULL/etc. is configured for any foreign key
 
2573
          references which require it, otherwise the database may emit an
 
2574
          integrity violation if foreign key references are being enforced.
 
2575
 
 
2576
          After the DELETE, dependent objects in the :class:`.Session` which
 
2577
          were impacted by an ON DELETE may not contain the current
 
2578
          state, or may have been deleted. This issue is resolved once the
 
2579
          :class:`.Session` is expired,
 
2580
          which normally occurs upon :meth:`.Session.commit` or can be forced
 
2581
          by using :meth:`.Session.expire_all`.  Accessing an expired object
 
2582
          whose row has been deleted will invoke a SELECT to locate the
 
2583
          row; when the row is not found, an :class:`.ObjectDeletedError`
 
2584
          is raised.
 
2585
 
 
2586
        * The :meth:`.MapperEvents.before_delete` and
 
2587
          :meth:`.MapperEvents.after_delete`
 
2588
          events are **not** invoked from this method.  Instead, the
 
2589
          :meth:`.SessionEvents.after_bulk_delete` method is provided to act
 
2590
          upon a mass DELETE of entity rows.
 
2591
 
 
2592
        .. seealso::
 
2593
 
 
2594
            :meth:`.Query.update`
 
2595
 
 
2596
            :ref:`inserts_and_updates` - Core SQL tutorial
2575
2597
 
2576
2598
        """
2577
2599
        #TODO: cascades need handling.
2610
2632
            The expression evaluator currently doesn't account for differing
2611
2633
            string collations between the database and Python.
2612
2634
 
2613
 
        Returns the number of rows matched by the update.
2614
 
 
2615
 
        The method does *not* offer in-Python cascading of relationships - it
2616
 
        is assumed that ON UPDATE CASCADE is configured for any foreign key
2617
 
        references which require it.
2618
 
 
2619
 
        The Session needs to be expired (occurs automatically after commit(),
2620
 
        or call expire_all()) in order for the state of dependent objects
2621
 
        subject foreign key cascade to be correctly represented.
2622
 
 
2623
 
        Note that the :meth:`.MapperEvents.before_update` and
2624
 
        :meth:`.MapperEvents.after_update`
2625
 
        events are **not** invoked from this method.  It instead
2626
 
        invokes :meth:`.SessionEvents.after_bulk_update`.
 
2635
        :return: the count of rows matched as returned by the database's
 
2636
          "row count" feature.
 
2637
 
 
2638
        This method has several key caveats:
 
2639
 
 
2640
        * The method does **not** offer in-Python cascading of relationships - it
 
2641
          is assumed that ON UPDATE CASCADE is configured for any foreign key
 
2642
          references which require it, otherwise the database may emit an
 
2643
          integrity violation if foreign key references are being enforced.
 
2644
 
 
2645
          After the UPDATE, dependent objects in the :class:`.Session` which
 
2646
          were impacted by an ON UPDATE CASCADE may not contain the current
 
2647
          state; this issue is resolved once the :class:`.Session` is expired,
 
2648
          which normally occurs upon :meth:`.Session.commit` or can be forced
 
2649
          by using :meth:`.Session.expire_all`.
 
2650
 
 
2651
        * As of 0.8, this method will support multiple table updates, as detailed
 
2652
          in :ref:`multi_table_updates`, and this behavior does extend to support
 
2653
          updates of joined-inheritance and other multiple table mappings.  However,
 
2654
          the **join condition of an inheritance mapper is currently not
 
2655
          automatically rendered**.
 
2656
          Care must be taken in any multiple-table update to explicitly include
 
2657
          the joining condition between those tables, even in mappings where
 
2658
          this is normally automatic.
 
2659
          E.g. if a class ``Engineer`` subclasses ``Employee``, an UPDATE of the
 
2660
          ``Engineer`` local table using criteria against the ``Employee``
 
2661
          local table might look like::
 
2662
 
 
2663
                session.query(Engineer).\\
 
2664
                    filter(Engineer.id == Employee.id).\\
 
2665
                    filter(Employee.name == 'dilbert').\\
 
2666
                    update({"engineer_type": "programmer"})
 
2667
 
 
2668
        * The :meth:`.MapperEvents.before_update` and
 
2669
          :meth:`.MapperEvents.after_update`
 
2670
          events are **not** invoked from this method.  Instead, the
 
2671
          :meth:`.SessionEvents.after_bulk_update` method is provided to act
 
2672
          upon a mass UPDATE of entity rows.
 
2673
 
 
2674
        .. seealso::
 
2675
 
 
2676
            :meth:`.Query.delete`
 
2677
 
 
2678
            :ref:`inserts_and_updates` - Core SQL tutorial
2627
2679
 
2628
2680
        """
2629
2681