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

« back to all changes in this revision

Viewing changes to doc/orm/extensions/hybrid.html

  • 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:
20
20
    <script type="text/javascript">
21
21
      var DOCUMENTATION_OPTIONS = {
22
22
          URL_ROOT:    '../../',
23
 
          VERSION:     '0.8.2',
 
23
          VERSION:     '0.8.3',
24
24
          COLLAPSE_MODINDEX: false,
25
25
          FILE_SUFFIX: '.html'
26
26
      };
67
67
    </div>
68
68
 
69
69
    <div id="docs-version-header">
70
 
        Release: <span class="version-num">0.8.2</span> | Release Date: July 3, 2013
 
70
        Release: <span class="version-num">0.8.3</span> | Release Date: October 26, 2013
71
71
 
72
72
 
73
73
    </div>
365
365
            <span class="n">account</span> <span class="o">=</span> <span class="n">Account</span><span class="p">(</span><span class="n">owner</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
366
366
        <span class="k">else</span><span class="p">:</span>
367
367
            <span class="n">account</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
368
 
        <span class="n">account</span><span class="o">.</span><span class="n">balance</span> <span class="o">=</span> <span class="n">balance</span>
 
368
        <span class="n">account</span><span class="o">.</span><span class="n">balance</span> <span class="o">=</span> <span class="n">value</span>
369
369
 
370
370
    <span class="nd">@balance.expression</span>
371
371
    <span class="k">def</span> <span class="nf">balance</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
402
402
<h3>Correlated Subquery Relationship Hybrid<a class="headerlink" href="#correlated-subquery-relationship-hybrid" title="Permalink to this headline">¶</a></h3>
403
403
<p>We can, of course, forego being dependent on the enclosing query&#8217;s usage
404
404
of joins in favor of the correlated subquery, which can portably be packed
405
 
into a single colunn expression. A correlated subquery is more portable, but
 
405
into a single column expression. A correlated subquery is more portable, but
406
406
often performs more poorly at the SQL level. Using the same technique
407
407
illustrated at <a class="reference internal" href="../mapper_config.html#mapper-column-property-sql-expressions"><em>Using column_property</em></a>,
408
408
we can adjust our <tt class="docutils literal"><span class="pre">SavingsAccount</span></tt> example to aggregate the balances for
490
490
<span class="go">WHERE lower(searchword.word) = lower(:lower_1)</span></pre></div>
491
491
</div>
492
492
<p>The <tt class="docutils literal"><span class="pre">CaseInsensitiveComparator</span></tt> above implements part of the
493
 
<a class="reference internal" href="../../core/expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a> interface.   A &#8220;coercion&#8221; operation like
 
493
<a class="reference internal" href="../../core/sqlelement.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a> interface.   A &#8220;coercion&#8221; operation like
494
494
lowercasing can be applied to all comparison operations (i.e. <tt class="docutils literal"><span class="pre">eq</span></tt>,
495
 
<tt class="docutils literal"><span class="pre">lt</span></tt>, <tt class="docutils literal"><span class="pre">gt</span></tt>, etc.) using <a class="reference internal" href="../../core/expression_api.html#sqlalchemy.sql.operators.Operators.operate" title="sqlalchemy.sql.operators.Operators.operate"><tt class="xref py py-meth docutils literal"><span class="pre">Operators.operate()</span></tt></a>:</p>
 
495
<tt class="docutils literal"><span class="pre">lt</span></tt>, <tt class="docutils literal"><span class="pre">gt</span></tt>, etc.) using <a class="reference internal" href="../../core/sqlelement.html#sqlalchemy.sql.operators.Operators.operate" title="sqlalchemy.sql.operators.Operators.operate"><tt class="xref py py-meth docutils literal"><span class="pre">Operators.operate()</span></tt></a>:</p>
496
496
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">CaseInsensitiveComparator</span><span class="p">(</span><span class="n">Comparator</span><span class="p">):</span>
497
497
    <span class="k">def</span> <span class="nf">operate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">op</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
498
498
        <span class="k">return</span> <span class="n">op</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="n">lower</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__clause_element__</span><span class="p">()),</span> <span class="n">func</span><span class="o">.</span><span class="n">lower</span><span class="p">(</span><span class="n">other</span><span class="p">))</span></pre></div>
589
589
<p>The Hybrid Value pattern is very useful for any kind of value that may
590
590
have multiple representations, such as timestamps, time deltas, units
591
591
of measurement, currencies and encrypted passwords.</p>
592
 
<div class="admonition-see-also admonition seealso">
 
592
<div class="admonition seealso">
593
593
<p class="first admonition-title">See also</p>
594
594
<p><a class="reference external" href="http://techspot.zzzeek.org/2011/10/21/hybrids-and-value-agnostic-types/">Hybrids and Value Agnostic Types</a> -
595
595
on the techspot.zzzeek.org blog</p>
666
666
        <span class="k">return</span> <span class="n">GrandparentTransformer</span><span class="p">(</span><span class="n">cls</span><span class="p">)</span></pre></div>
667
667
</div>
668
668
<p>The <tt class="docutils literal"><span class="pre">GrandparentTransformer</span></tt> overrides the core
669
 
<a class="reference internal" href="../../core/expression_api.html#sqlalchemy.sql.operators.Operators.operate" title="sqlalchemy.sql.operators.Operators.operate"><tt class="xref py py-meth docutils literal"><span class="pre">Operators.operate()</span></tt></a> method at the base of the
 
669
<a class="reference internal" href="../../core/sqlelement.html#sqlalchemy.sql.operators.Operators.operate" title="sqlalchemy.sql.operators.Operators.operate"><tt class="xref py py-meth docutils literal"><span class="pre">Operators.operate()</span></tt></a> method at the base of the
670
670
<a class="reference internal" href="#sqlalchemy.ext.hybrid.Comparator" title="sqlalchemy.ext.hybrid.Comparator"><tt class="xref py py-class docutils literal"><span class="pre">Comparator</span></tt></a> hierarchy to return a query-transforming
671
671
callable, which then runs the given comparison operation in a
672
672
particular context. Such as, in the example above, the <tt class="docutils literal"><span class="pre">operate</span></tt>
734
734
<dl class="class">
735
735
<dt id="sqlalchemy.ext.hybrid.hybrid_method">
736
736
<em class="property">class </em><tt class="descclassname">sqlalchemy.ext.hybrid.</tt><tt class="descname">hybrid_method</tt><big>(</big><em>func</em>, <em>expr=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.hybrid.hybrid_method" title="Permalink to this definition">¶</a></dt>
737
 
<dd><p>A decorator which allows definition of a Python object method with both
 
737
<dd><p>Bases: <a class="reference internal" href="../internals.html#sqlalchemy.orm.interfaces._InspectionAttr" title="sqlalchemy.orm.interfaces._InspectionAttr"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.orm.interfaces._InspectionAttr</span></tt></a></p>
 
738
<p>A decorator which allows definition of a Python object method with both
738
739
instance-level and class-level behavior.</p>
739
740
<dl class="method">
740
741
<dt id="sqlalchemy.ext.hybrid.hybrid_method.__init__">
766
767
<dl class="class">
767
768
<dt id="sqlalchemy.ext.hybrid.hybrid_property">
768
769
<em class="property">class </em><tt class="descclassname">sqlalchemy.ext.hybrid.</tt><tt class="descname">hybrid_property</tt><big>(</big><em>fget</em>, <em>fset=None</em>, <em>fdel=None</em>, <em>expr=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.hybrid.hybrid_property" title="Permalink to this definition">¶</a></dt>
769
 
<dd><p>A decorator which allows definition of a Python descriptor with both
 
770
<dd><p>Bases: <a class="reference internal" href="../internals.html#sqlalchemy.orm.interfaces._InspectionAttr" title="sqlalchemy.orm.interfaces._InspectionAttr"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.orm.interfaces._InspectionAttr</span></tt></a></p>
 
771
<p>A decorator which allows definition of a Python descriptor with both
770
772
instance-level and class-level behavior.</p>
771
773
<dl class="method">
772
774
<dt id="sqlalchemy.ext.hybrid.hybrid_property.__init__">
851
853
 
852
854
    <div id="docs-copyright">
853
855
        &copy; <a href="../../copyright.html">Copyright</a> 2007-2013, the SQLAlchemy authors and contributors.
854
 
        Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
 
856
        Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2b1.
855
857
    </div>
856
858
</div>
857
859