1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
examples.materialized_paths.materialized_paths
—
SQLAlchemy 0.9 Documentation
</title>
<!-- begin iterate through SQLA + sphinx environment css_files -->
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/docs.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/sphinx_paramlinks.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/changelog.css" type="text/css" />
<!-- end iterate through SQLA + sphinx environment css_files -->
<!-- begin layout.mako headers -->
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../../',
VERSION: '0.9.8',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html'
};
</script>
<!-- begin iterate through sphinx environment script_files -->
<script type="text/javascript" src="../../../_static/jquery.js"></script>
<script type="text/javascript" src="../../../_static/underscore.js"></script>
<script type="text/javascript" src="../../../_static/doctools.js"></script>
<!-- end iterate through sphinx environment script_files -->
<script type="text/javascript" src="../../../_static/detectmobile.js"></script>
<script type="text/javascript" src="../../../_static/init.js"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
<link rel="copyright" title="Copyright" href="../../../copyright.html" />
<link rel="top" title="SQLAlchemy 0.9 Documentation" href="../../../index.html" />
<link rel="up" title="Module code" href="../../index.html" />
<!-- end layout.mako headers -->
</head>
<body>
<div id="docs-container">
<div id="docs-top-navigation-container" class="body-background">
<div id="docs-header">
<div id="docs-version-header">
Release: <span class="version-num">0.9.8</span> | Release Date: October 13, 2014
</div>
<h1>SQLAlchemy 0.9 Documentation</h1>
</div>
</div>
<div id="docs-body-container">
<div id="fixed-sidebar" class="">
<div id="index-nav">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" size="12" /> <input type="submit" value="Search" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p>
<a href="../../../index.html">Contents</a> |
<a href="../../../genindex.html">Index</a>
</p>
</div>
</div>
<div id="docs-body" class="" >
<h1>Source code for examples.materialized_paths.materialized_paths</h1><div class="highlight"><pre>
<span class="sd">"""Illustrates the "materialized paths" pattern.</span>
<span class="sd">Materialized paths is a way to represent a tree structure in SQL with fast</span>
<span class="sd">descendant and ancestor queries at the expense of moving nodes (which require</span>
<span class="sd">O(n) UPDATEs in the worst case, where n is the number of nodes in the tree). It</span>
<span class="sd">is a good balance in terms of performance and simplicity between the nested</span>
<span class="sd">sets model and the adjacency list model.</span>
<span class="sd">It works by storing all nodes in a table with a path column, containing a</span>
<span class="sd">string of delimited IDs. Think file system paths:</span>
<span class="sd"> 1</span>
<span class="sd"> 1.2</span>
<span class="sd"> 1.3</span>
<span class="sd"> 1.3.4</span>
<span class="sd"> 1.3.5</span>
<span class="sd"> 1.3.6</span>
<span class="sd"> 1.7</span>
<span class="sd"> 1.7.8</span>
<span class="sd"> 1.7.9</span>
<span class="sd"> 1.7.9.10</span>
<span class="sd"> 1.7.11</span>
<span class="sd">Descendant queries are simple left-anchored LIKE queries, and ancestors are</span>
<span class="sd">already stored in the path itself. Updates require going through all</span>
<span class="sd">descendants and changing the prefix.</span>
<span class="sd">"""</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">Column</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">String</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">create_engine</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="n">remote</span><span class="p">,</span> <span class="n">foreign</span><span class="p">,</span> <span class="n">relationship</span><span class="p">,</span> <span class="n">Session</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.sql.expression</span> <span class="kn">import</span> <span class="n">cast</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.dialects.postgresql</span> <span class="kn">import</span> <span class="n">ARRAY</span>
<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">()</span>
<span class="k">class</span> <span class="nc">Node</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
<span class="n">__tablename__</span> <span class="o">=</span> <span class="s">"node"</span>
<span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">autoincrement</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">500</span><span class="p">),</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="c"># To find the descendants of this node, we look for nodes whose path</span>
<span class="c"># starts with this node's path.</span>
<span class="n">descendants</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span>
<span class="s">"Node"</span><span class="p">,</span> <span class="n">viewonly</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">order_by</span><span class="o">=</span><span class="n">path</span><span class="p">,</span>
<span class="n">primaryjoin</span><span class="o">=</span><span class="n">remote</span><span class="p">(</span><span class="n">foreign</span><span class="p">(</span><span class="n">path</span><span class="p">))</span><span class="o">.</span><span class="n">like</span><span class="p">(</span><span class="n">path</span><span class="o">.</span><span class="n">concat</span><span class="p">(</span><span class="s">".%"</span><span class="p">)))</span>
<span class="c"># Finding the ancestors is a little bit trickier. We need to create a fake</span>
<span class="c"># secondary table since this behaves like a many-to-many join.</span>
<span class="n">secondary</span> <span class="o">=</span> <span class="n">select</span><span class="p">([</span>
<span class="nb">id</span><span class="o">.</span><span class="n">label</span><span class="p">(</span><span class="s">"id"</span><span class="p">),</span>
<span class="n">func</span><span class="o">.</span><span class="n">unnest</span><span class="p">(</span><span class="n">cast</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="n">string_to_array</span><span class="p">(</span>
<span class="n">func</span><span class="o">.</span><span class="n">regexp_replace</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s">r"\.?\d+$"</span><span class="p">,</span> <span class="s">""</span><span class="p">),</span> <span class="s">"."</span><span class="p">),</span>
<span class="n">ARRAY</span><span class="p">(</span><span class="n">Integer</span><span class="p">)))</span><span class="o">.</span><span class="n">label</span><span class="p">(</span><span class="s">"ancestor_id"</span><span class="p">)</span>
<span class="p">])</span><span class="o">.</span><span class="n">alias</span><span class="p">()</span>
<span class="n">ancestors</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">"Node"</span><span class="p">,</span> <span class="n">viewonly</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">secondary</span><span class="o">=</span><span class="n">secondary</span><span class="p">,</span>
<span class="n">primaryjoin</span><span class="o">=</span><span class="nb">id</span> <span class="o">==</span> <span class="n">secondary</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="p">,</span>
<span class="n">secondaryjoin</span><span class="o">=</span><span class="n">secondary</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">ancestor_id</span> <span class="o">==</span> <span class="nb">id</span><span class="p">,</span>
<span class="n">order_by</span><span class="o">=</span><span class="n">path</span><span class="p">)</span>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">depth</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">"."</span><span class="p">))</span> <span class="o">-</span> <span class="mi">1</span>
<span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s">"Node(id={})"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">root_depth</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">depth</span>
<span class="n">s</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">)]</span>
<span class="n">s</span><span class="o">.</span><span class="n">extend</span><span class="p">(((</span><span class="n">n</span><span class="o">.</span><span class="n">depth</span> <span class="o">-</span> <span class="n">root_depth</span><span class="p">)</span> <span class="o">*</span> <span class="s">" "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">n</span><span class="o">.</span><span class="n">id</span><span class="p">))</span>
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">descendants</span><span class="p">)</span>
<span class="k">return</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">move_to</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new_parent</span><span class="p">):</span>
<span class="n">new_path</span> <span class="o">=</span> <span class="n">new_parent</span><span class="o">.</span><span class="n">path</span> <span class="o">+</span> <span class="s">"."</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">descendants</span><span class="p">:</span>
<span class="n">n</span><span class="o">.</span><span class="n">path</span> <span class="o">=</span> <span class="n">new_path</span> <span class="o">+</span> <span class="n">n</span><span class="o">.</span><span class="n">path</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">path</span><span class="p">):]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">path</span> <span class="o">=</span> <span class="n">new_path</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span>
<span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">"postgresql://scott:tiger@localhost/test"</span><span class="p">,</span> <span class="n">echo</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="o">.</span><span class="n">create_all</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
<span class="n">session</span> <span class="o">=</span> <span class="n">Session</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">"-"</span> <span class="o">*</span> <span class="mi">80</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">"create a tree"</span><span class="p">)</span>
<span class="n">session</span><span class="o">.</span><span class="n">add_all</span><span class="p">([</span>
<span class="n">Node</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"1"</span><span class="p">),</span>
<span class="n">Node</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"1.2"</span><span class="p">),</span>
<span class="n">Node</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"1.3"</span><span class="p">),</span>
<span class="n">Node</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"1.3.4"</span><span class="p">),</span>
<span class="n">Node</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"1.3.5"</span><span class="p">),</span>
<span class="n">Node</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"1.3.6"</span><span class="p">),</span>
<span class="n">Node</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">7</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"1.7"</span><span class="p">),</span>
<span class="n">Node</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"1.7.8"</span><span class="p">),</span>
<span class="n">Node</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">9</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"1.7.9"</span><span class="p">),</span>
<span class="n">Node</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"1.7.9.10"</span><span class="p">),</span>
<span class="n">Node</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">11</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"1.7.11"</span><span class="p">),</span>
<span class="p">])</span>
<span class="n">session</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Node</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="mi">1</span><span class="p">)))</span>
<span class="k">print</span><span class="p">(</span><span class="s">"-"</span> <span class="o">*</span> <span class="mi">80</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">"move 7 under 3"</span><span class="p">)</span>
<span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Node</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span><span class="o">.</span><span class="n">move_to</span><span class="p">(</span><span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Node</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="n">session</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Node</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="mi">1</span><span class="p">)))</span>
<span class="k">print</span><span class="p">(</span><span class="s">"-"</span> <span class="o">*</span> <span class="mi">80</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">"move 3 under 2"</span><span class="p">)</span>
<span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Node</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">move_to</span><span class="p">(</span><span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Node</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
<span class="n">session</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Node</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="mi">1</span><span class="p">)))</span>
<span class="k">print</span><span class="p">(</span><span class="s">"-"</span> <span class="o">*</span> <span class="mi">80</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">"find the ancestors of 10"</span><span class="p">)</span>
<span class="k">print</span><span class="p">([</span><span class="n">n</span><span class="o">.</span><span class="n">id</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Node</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">ancestors</span><span class="p">])</span>
<span class="n">session</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="o">.</span><span class="n">drop_all</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span></pre></div>
</div>
</div>
<div id="docs-bottom-navigation" class="docs-navigation-links">
<div id="docs-copyright">
© <a href="../../../copyright.html">Copyright</a> 2007-2014, the SQLAlchemy authors and contributors.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2b1.
</div>
</div>
</div>
</body>
</html>
|