This file is indexed.

/usr/share/doc/python3-postgresql/html/lib.html is in python3-postgresql 1.1.0-2+b3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<!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>Categories and Libraries &mdash; py-postgresql 1.1.0 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <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>
    <link rel="top" title="py-postgresql 1.1.0 documentation" href="index.html" />
    <link rel="next" title="Client Parameters" href="clientparameters.html" />
    <link rel="prev" title="Cluster Management" href="cluster.html" />

   
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9">

<link rel="stylesheet" href="_static/unsuck.css" type="text/css" />

  </head>
  <body role="document">  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="categories-and-libraries">
<h1>Categories and Libraries<a class="headerlink" href="#categories-and-libraries" title="Permalink to this headline"></a></h1>
<p>This chapter discusses the usage and implementation of connection categories and
libraries.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">First-time users are encouraged to read the <a class="reference internal" href="#audience-and-motivation">Audience and Motivation</a>
section first.</p>
</div>
<p>Libraries are a collection of SQL statements that can be bound to a
connection. Libraries are <em>normally</em> bound directly to the connection object as
an attribute using a name specified by the library.</p>
<p>Libraries provide a common way for SQL statements to be managed outside of the
code that uses them. When using ILFs, this increases the portability of the SQL
by keeping the statements isolated from the Python code in an accessible format
that can be easily used by other languages or systems &#8212; An ILF parser can be
implemented within a few dozen lines using basic text tools.</p>
<p>SQL statements defined by a Library are identified by their Symbol. These
symbols are named and annotated in order to allow the user to define how a
statement is to be used. The user may state the default execution method of
the statement object, or whether the symbol is to be preloaded at bind
time&#8211;these properties are Symbol Annotations.</p>
<p>The purpose of libraries are to provide a means to manage statements on
disk and at runtime. ILFs provide a means to reference a collection
of statements on disk, and, when loaded, the symbol bindings provides means to
reference a statement already prepared for use on a given connection.</p>
<p>The <cite>postgresql.lib</cite> package-module provides fundamental classes for supporting
categories and libraries.</p>
<div class="section" id="writing-libraries">
<h2>Writing Libraries<a class="headerlink" href="#writing-libraries" title="Permalink to this headline"></a></h2>
<p>ILF files are the recommended way to build a library. These files use the
naming convention &#8220;lib{NAME}.sql&#8221;. The prefix and suffix are used describe the
purpose of the file and to provide a hint to editors that SQL highlighting
should be used. The format of an ILF takes the form:</p>
<div class="highlight-python"><div class="highlight"><pre>&lt;Preface&gt;
[name:type:method]
&lt;statement&gt;
...
</pre></div>
</div>
<p>Where multiple symbols may be defined. The Preface that comes before the first
symbol is an arbitrary block of text that should be used to describe the library.
This block is free-form, and should be considered a good place for some
general documentation.</p>
<p>Symbols are named and described using the contents of section markers:
<code class="docutils literal"><span class="pre">('['</span> <span class="pre">...</span> <span class="pre">']')</span></code>. Section markers have three components: the symbol name,
the symbol type and the symbol method. Each of these components are separated
using a single colon, <code class="docutils literal"><span class="pre">:</span></code>. All components are optional except the Symbol name.
For example:</p>
<div class="highlight-python"><div class="highlight"><pre>[get_user_info]
SELECT * FROM user WHERE user_id = $1

[get_user_info_v2::]
SELECT * FROM user WHERE user_id = $1
</pre></div>
</div>
<p>In the above example, <code class="docutils literal"><span class="pre">get_user_info</span></code> and <code class="docutils literal"><span class="pre">get_user_info_v2</span></code> are identical.
Empty components indicate the default effect.</p>
<p>The second component in the section identifier is the symbol type. All Symbol
types are listed in <a class="reference internal" href="#symbol-types">Symbol Types</a>. This can be
used to specify what the section&#8217;s contents are or when to bind the
symbol:</p>
<div class="highlight-python"><div class="highlight"><pre>[get_user_info:preload]
SELECT * FROM user WHERE user_id = $1
</pre></div>
</div>
<p>This provides the Binding with the knowledge that the statement should be
prepared when the Library is bound. Therefore, when this Symbol&#8217;s statement
is used for the first time, it will have already been prepared.</p>
<p>Another type is the <code class="docutils literal"><span class="pre">const</span></code> Symbol type. This defines a data Symbol whose
<em>statement results</em> will be resolved when the Library is bound:</p>
<div class="highlight-python"><div class="highlight"><pre>[user_type_ids:const]
SELECT user_type_id, user_type FROM user_types;
</pre></div>
</div>
<p>Constant Symbols cannot take parameters as they are data properties. The
<em>result</em> of the above query is set to the Bindings&#8217; <code class="docutils literal"><span class="pre">user_type_ids</span></code>
attribute:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">db</span><span class="o">.</span><span class="n">lib</span><span class="o">.</span><span class="n">user_type_ids</span>
<span class="go">&lt;sequence of (user_type_id, user_type)&gt;</span>
</pre></div>
</div>
<p>Where <code class="docutils literal"><span class="pre">lib</span></code> in the above is a Binding of the Library containing the
<code class="docutils literal"><span class="pre">user_type_ids</span></code> Symbol.</p>
<p>Finally, procedures can be bound as symbols using the <code class="docutils literal"><span class="pre">proc</span></code> type:</p>
<div class="highlight-python"><div class="highlight"><pre>[remove_user:proc]
remove_user(bigint)
</pre></div>
</div>
<p>All procedures symbols are loaded when the Library is bound. Procedure symbols
are special because the execution method is effectively specified by the
procedure itself.</p>
<p>The third component is the symbol <code class="docutils literal"><span class="pre">method</span></code>. This defines the execution method
of the statement and ultimately what is returned when the Symbol is called at
runtime. All the execution methods are listed in <a class="reference internal" href="#symbol-execution-methods">Symbol Execution Methods</a>.</p>
<p>The default execution method is the default execution method of
<cite>postgresql.api.PreparedStatement</cite> objects; return the entire result set in a
list object:</p>
<div class="highlight-python"><div class="highlight"><pre>[get_numbers]
SELECT i FROM generate_series(0, 100-1) AS g(i);
</pre></div>
</div>
<p>When bound:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">db</span><span class="o">.</span><span class="n">lib</span><span class="o">.</span><span class="n">get_numbers</span><span class="p">()</span> <span class="o">==</span> <span class="p">[(</span><span class="n">x</span><span class="p">,)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">)]</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The transformation of range in the above is necessary as statements
return a sequence of row objects by default.</p>
<p>For large result-sets, fetching all the rows would be taxing on a system&#8217;s
memory. The <code class="docutils literal"><span class="pre">rows</span></code> and <code class="docutils literal"><span class="pre">chunks</span></code> methods provide an iterator to rows produced
by a statement using a stream:</p>
<div class="highlight-python"><div class="highlight"><pre>[get_some_rows::rows]
SELECT i FROM generate_series(0, 1000) AS g(i);

[get_some_chunks::chunks]
SELECT i FROM generate_series(0, 1000) AS g(i);
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">rows</span></code> means that the Symbol will return an iterator producing individual rows
of the result, and <code class="docutils literal"><span class="pre">chunks</span></code> means that the Symbol will return an iterator
producing sequences of rows of the result.</p>
<p>When bound:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">chain</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">db</span><span class="o">.</span><span class="n">lib</span><span class="o">.</span><span class="n">get_some_rows</span><span class="p">())</span> <span class="o">==</span> <span class="nb">list</span><span class="p">(</span><span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">(</span><span class="n">db</span><span class="o">.</span><span class="n">lib</span><span class="o">.</span><span class="n">get_some_chunks</span><span class="p">()))</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Other methods include <code class="docutils literal"><span class="pre">column</span></code> and <code class="docutils literal"><span class="pre">first</span></code>. The column method provides a
means to designate that the symbol should return an iterator of the values in
the first column instead of an iterator to the rows:</p>
<div class="highlight-python"><div class="highlight"><pre>[another_generate_series_example::column]
SELECT i FROM generate_series(0, $1::int) AS g(i)
</pre></div>
</div>
<p>In use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">db</span><span class="o">.</span><span class="n">lib</span><span class="o">.</span><span class="n">another_generate_series_example</span><span class="p">(</span><span class="mi">100</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span> <span class="o">==</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">db</span><span class="o">.</span><span class="n">lib</span><span class="o">.</span><span class="n">another_generate_series_example</span><span class="p">(</span><span class="mi">10</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span>
<span class="go">[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">first</span></code> method provides direct access to simple results.
Specifically, the first column of the first row when there is only one column.
When there are multiple columns the first row is returned:</p>
<div class="highlight-python"><div class="highlight"><pre>[get_one::first]
SELECT 1

[get_one_twice::first]
SELECT 1, 1
</pre></div>
</div>
<p>In use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">db</span><span class="o">.</span><span class="n">lib</span><span class="o">.</span><span class="n">get_one</span><span class="p">()</span> <span class="o">==</span> <span class="mi">1</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">db</span><span class="o">.</span><span class="n">lib</span><span class="o">.</span><span class="n">get_one_twice</span><span class="p">()</span> <span class="o">==</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><code class="docutils literal"><span class="pre">first</span></code> should be used with care. When the result returns no rows, <cite>None</cite>
will be returned.</p>
</div>
</div>
<div class="section" id="using-libraries">
<h2>Using Libraries<a class="headerlink" href="#using-libraries" title="Permalink to this headline"></a></h2>
<p>After a library is created, it must be loaded before it can be bound using
programmer interfaces. The <cite>postgresql.lib.load</cite> interface provides the
primary entry point for loading libraries.</p>
<p>When <code class="docutils literal"><span class="pre">load</span></code> is given a string, it identifies if a directory separator is in
the string, if there is it will treat the string as a <em>path</em> to the ILF to be
loaded. If no separator is found, it will treat the string as the library
name fragment and look for &#8220;lib{NAME}.sql&#8221; in the directories listed in
<cite>postgresql.sys.libpath</cite>.</p>
<p>Once a <cite>postgresql.lib.Library</cite> instance has been acquired, it can then be
bound to a connection for use. <cite>postgresql.lib.Binding</cite> is used to create an
object that provides and manages the Bound Symbols:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">postgresql.lib</span> <span class="kn">as</span> <span class="nn">pg_lib</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">lib</span> <span class="o">=</span> <span class="n">pg_lib</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">B</span> <span class="o">=</span> <span class="n">pg_lib</span><span class="o">.</span><span class="n">Binding</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">lib</span><span class="p">)</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">B</span></code> object in the above example provides the Library&#8217;s Symbols as
attributes which can be called to in order to execute the Symbol&#8217;s statement:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">B</span><span class="o">.</span><span class="n">symbol</span><span class="p">(</span><span class="n">param</span><span class="p">)</span>
<span class="gp">...</span>
</pre></div>
</div>
<p>While it is sometimes necessary, manual creation of a Binding is discouraged.
Rather, <cite>postgresql.lib.Category</cite> objects should be used to manage the set of
Libraries to be bound to a connection.</p>
<div class="section" id="categories">
<h3>Categories<a class="headerlink" href="#categories" title="Permalink to this headline"></a></h3>
<p>Libraries provide access to a collection of symbols; Bindings provide an
interface to the symbols with respect to a subject database. When a connection
is established, multiple Bindings may need to be created in order to fulfill
the requirements of the programmer. When a Binding is created, it exists in
isolation; this can be an inconvenience when access to both the Binding and
the Connection is necessary. Categories exist to provide a formal method for
defining the interface extensions on a <cite>postgresql.api.Database</cite>
instance(connection).</p>
<p>A Category is essentially a runtime-class for connections. It provides a
formal initialization procedure for connection objects at runtime. However,
the connection resource must be connected prior to category initialization.</p>
<p>Categories are sets of Libraries to be bound to a connection with optional name
substitutions. In order to create one directly, pass the Library instances to
<cite>postgresql.lib.Category</cite>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">postgresql.lib</span> <span class="kn">as</span> <span class="nn">pg_lib</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cat</span> <span class="o">=</span> <span class="n">pg_lib</span><span class="o">.</span><span class="n">Category</span><span class="p">(</span><span class="n">lib1</span><span class="p">,</span> <span class="n">lib2</span><span class="p">,</span> <span class="n">libN</span><span class="p">)</span>
</pre></div>
</div>
<p>Where <code class="docutils literal"><span class="pre">lib1</span></code>, <code class="docutils literal"><span class="pre">lib2</span></code>, <code class="docutils literal"><span class="pre">libN</span></code> are <cite>postgresql.lib.Library</cite> instances;
usually created by <cite>postgresql.lib.load</cite>. Once created, categories can then
used by passing the <code class="docutils literal"><span class="pre">category</span></code> keyword to connection creation interfaces:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">postgresql</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">db</span> <span class="o">=</span> <span class="n">postgresql</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">category</span> <span class="o">=</span> <span class="n">cat</span><span class="p">)</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">db</span></code> object will now have Bindings for <code class="docutils literal"><span class="pre">lib1</span></code>, <code class="docutils literal"><span class="pre">lib2</span></code>, ..., and
<code class="docutils literal"><span class="pre">libN</span></code>.</p>
<p>Categories can alter the access point(attribute name) of Bindings. This is done
by instantiating the Category using keyword parameters:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cat</span> <span class="o">=</span> <span class="n">pg_lib</span><span class="o">.</span><span class="n">Category</span><span class="p">(</span><span class="n">lib1</span><span class="p">,</span> <span class="n">lib2</span><span class="p">,</span> <span class="n">libname</span> <span class="o">=</span> <span class="n">libN</span><span class="p">)</span>
</pre></div>
</div>
<p>At this point, when a connection is established as the category <code class="docutils literal"><span class="pre">cat</span></code>,
<code class="docutils literal"><span class="pre">libN</span></code> will be bound to the connection object on the attribute <code class="docutils literal"><span class="pre">libname</span></code>
instead of the name defined by the library.</p>
<p>And a final illustration of Category usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">db</span> <span class="o">=</span> <span class="n">postgresql</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">category</span> <span class="o">=</span> <span class="n">pg_lib</span><span class="o">.</span><span class="n">Category</span><span class="p">(</span><span class="n">pg_lib</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">db</span><span class="o">.</span><span class="n">name</span>
<span class="go">&lt;Library&gt;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="symbol-types">
<h2>Symbol Types<a class="headerlink" href="#symbol-types" title="Permalink to this headline"></a></h2>
<p>The symbol type determines how a symbol is going to be treated by the Binding.
For instance, <code class="docutils literal"><span class="pre">const</span></code> symbols are resolved when the Library is bound and
the statement object is immediately discarded. Here is a list of symbol types
that can be used in ILF libraries:</p>
<blockquote>
<div><dl class="docutils">
<dt><code class="docutils literal"><span class="pre">&lt;default&gt;</span></code> (Empty component)</dt>
<dd>The symbol&#8217;s statement will never change. This allows the Bound Symbol to
hold onto the <cite>postgresql.api.PreparedStatement</cite> object. When the symbol is
used again, it will refer to the existing prepared statement object.</dd>
<dt><code class="docutils literal"><span class="pre">preload</span></code></dt>
<dd>Like the default type, the Symbol is a simple statement, but it should be
loaded when the library is bound to the connection.</dd>
<dt><code class="docutils literal"><span class="pre">const</span></code></dt>
<dd>The statement takes no parameters and only needs to be executed once. This
will cause the statement to be executed when the library is bound and the
results of the statement will be set to the Binding using the symbol name so
that it may be used as a property by the user.</dd>
<dt><code class="docutils literal"><span class="pre">proc</span></code></dt>
<dd>The contents of the section is a procedure identifier. When this type is used
the symbol method <em>should not</em> be specified as the method annotation will be
automatically resolved based on the procedure&#8217;s signature.</dd>
<dt><code class="docutils literal"><span class="pre">transient</span></code></dt>
<dd>The Symbol is a statement that should <em>not</em> be retained. Specifically, it is
a statement object that will be discarded when the user discard the referenced
Symbol. Used in cases where the statement is used once or very infrequently.</dd>
</dl>
</div></blockquote>
</div>
<div class="section" id="symbol-execution-methods">
<h2>Symbol Execution Methods<a class="headerlink" href="#symbol-execution-methods" title="Permalink to this headline"></a></h2>
<p>The Symbol Execution Method provides a way to specify how a statement is going
to be used. Specifically, which <cite>postgresql.api.PreparedStatement</cite> method
should be executed when a Bound Symbol is called. The following is a list of
the symbol execution methods and the effect it will have when invoked:</p>
<blockquote>
<div><dl class="docutils">
<dt><code class="docutils literal"><span class="pre">&lt;default&gt;</span></code> (Empty component)</dt>
<dd>Returns the entire result set in a single list object. If the statement does
not return rows, a <code class="docutils literal"><span class="pre">(command,</span> <span class="pre">count)</span></code> pair will be returned.</dd>
<dt><code class="docutils literal"><span class="pre">rows</span></code></dt>
<dd>Returns an iterator producing each row in the result set.</dd>
<dt><code class="docutils literal"><span class="pre">chunks</span></code></dt>
<dd>Returns an iterator producing &#8220;chunks&#8221; of rows in the result set.</dd>
<dt><code class="docutils literal"><span class="pre">first</span></code></dt>
<dd>Returns the first column of the first row if there is one column in the result
set. If there are multiple columns in the result set, the first row is
returned. If query is non-RETURNING DML&#8211;insert, update, or delete, the row
count is returned.</dd>
<dt><code class="docutils literal"><span class="pre">column</span></code></dt>
<dd>Returns an iterator to values in the first column. (Equivalent to
executing a statement as <code class="docutils literal"><span class="pre">map(operator.itemgetter(0),</span> <span class="pre">ps.rows())</span></code>.)</dd>
<dt><code class="docutils literal"><span class="pre">declare</span></code></dt>
<dd>Returns a scrollable cursor, <cite>postgresql.api.Cursor</cite>, to the result set.</dd>
<dt><code class="docutils literal"><span class="pre">load_chunks</span></code></dt>
<dd>Takes an iterable row-chunks to be given to the statement. Returns <cite>None</cite>. If
the statement is a <code class="docutils literal"><span class="pre">COPY</span> <span class="pre">...</span> <span class="pre">FROM</span> <span class="pre">STDIN</span></code>, the iterable must produce chunks
of COPY lines.</dd>
<dt><code class="docutils literal"><span class="pre">load_rows</span></code></dt>
<dd>Takes an iterable rows to be given as parameters. If the statement is a <code class="docutils literal"><span class="pre">COPY</span>
<span class="pre">...</span> <span class="pre">FROM</span> <span class="pre">STDIN</span></code>, the iterable must produce COPY lines.</dd>
</dl>
</div></blockquote>
</div>
<div class="section" id="reference-symbols">
<h2>Reference Symbols<a class="headerlink" href="#reference-symbols" title="Permalink to this headline"></a></h2>
<p>Reference Symbols provide a way to construct a Bound Symbol using the Symbol&#8217;s
query. When invoked, A Reference Symbol&#8217;s query is executed in order to produce
an SQL statement to be used as a Bound Symbol. In ILF files, a reference is
identified by its symbol name being prefixed with an ampersand:</p>
<div class="highlight-python"><div class="highlight"><pre>[&amp;refsym::first]
SELECT &#39;SELECT 1::int4&#39;::text
</pre></div>
</div>
<p>Then executed:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c1"># Runs the &#39;refsym&#39; SQL, and creates a Bound Symbol using the results.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sym</span> <span class="o">=</span> <span class="n">lib</span><span class="o">.</span><span class="n">refsym</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">assert</span> <span class="n">sym</span><span class="p">()</span> <span class="o">==</span> <span class="mi">1</span>
</pre></div>
</div>
<p>The Reference Symbol&#8217;s type and execution method are inherited by the created
Bound Symbol. With one exception, <code class="docutils literal"><span class="pre">const</span></code> reference symbols are
special in that they immediately resolved into the target Bound Symbol.</p>
<p>A Reference Symbol&#8217;s source query <em>must</em> produce rows of text columns. Multiple
columns and multiple rows may be produced by the query, but they must be
character types as the results are promptly joined together with whitespace so
that the target statement may be prepared.</p>
<p>Reference Symbols are most likely to be used in dynamic DDL and DML situations,
or, somewhat more specifically, any query whose definition depends on a
generated column list.</p>
</div>
<div class="section" id="distributing-and-usage">
<h2>Distributing and Usage<a class="headerlink" href="#distributing-and-usage" title="Permalink to this headline"></a></h2>
<p>For applications, distribution and management can easily be a custom
process. The application designates the library directory; the entry point
adds the path to the <cite>postgresql.sys.libpath</cite> list; a category is built; and, a
connection is made using the category.</p>
<p>For mere Python extensions, however, <code class="docutils literal"><span class="pre">distutils</span></code> has a feature that can
aid in ILF distribution. The <code class="docutils literal"><span class="pre">package_data</span></code> setup keyword can be used to
include ILF files alongside the Python modules that make up a project. See
<a class="reference external" href="http://docs.python.org/3.1/distutils/setupscript.html#installing-package-data">http://docs.python.org/3.1/distutils/setupscript.html#installing-package-data</a>
for more detailed information on the keyword parameter.</p>
<p>The recommended way to manage libraries for extending projects is to
create a package to contain them. For instance, consider the following layout:</p>
<div class="highlight-python"><div class="highlight"><pre>project/
        setup.py
        pkg/
                __init__.py
                lib/
                        __init__.py
                        libthis.sql
                        libthat.sql
</pre></div>
</div>
<p>The project&#8217;s SQL libraries are organized into a single package directory,
<code class="docutils literal"><span class="pre">lib</span></code>, so <code class="docutils literal"><span class="pre">package_data</span></code> would be configured:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">package_data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;pkg.lib&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;*.sql&#39;</span><span class="p">]}</span>
</pre></div>
</div>
<p>Subsequently, the <code class="docutils literal"><span class="pre">lib</span></code> package initialization script can then be used to
load the libraries, and create any categories(<code class="docutils literal"><span class="pre">project/pkg/lib/__init__.py</span></code>):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os.path</span>
<span class="kn">import</span> <span class="nn">postgresql.lib</span> <span class="kn">as</span> <span class="nn">pg_lib</span>
<span class="kn">import</span> <span class="nn">postgresql.sys</span> <span class="kn">as</span> <span class="nn">pg_sys</span>
<span class="n">libdir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">__file__</span><span class="p">)</span>
<span class="n">pg_sys</span><span class="o">.</span><span class="n">libpath</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">libdir</span><span class="p">)</span>
<span class="n">libthis</span> <span class="o">=</span> <span class="n">pg_lib</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s1">&#39;this&#39;</span><span class="p">)</span>
<span class="n">libthat</span> <span class="o">=</span> <span class="n">pg_lib</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s1">&#39;that&#39;</span><span class="p">)</span>
<span class="n">stdcat</span> <span class="o">=</span> <span class="n">pg_lib</span><span class="o">.</span><span class="n">Category</span><span class="p">(</span><span class="n">libthis</span><span class="p">,</span> <span class="n">libthat</span><span class="p">)</span>
</pre></div>
</div>
<p>However, it can be undesirable to add the package directory to the global
<cite>postgresql.sys.libpath</cite> search paths. Direct path loading can be used in those
cases:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os.path</span>
<span class="kn">import</span> <span class="nn">postgresql.lib</span> <span class="kn">as</span> <span class="nn">pg_lib</span>
<span class="n">libdir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">__file__</span><span class="p">)</span>
<span class="n">libthis</span> <span class="o">=</span> <span class="n">pg_lib</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">libdir</span><span class="p">,</span> <span class="s1">&#39;libthis.sql&#39;</span><span class="p">))</span>
<span class="n">libthat</span> <span class="o">=</span> <span class="n">pg_lib</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">libdir</span><span class="p">,</span> <span class="s1">&#39;libthat.sql&#39;</span><span class="p">))</span>
<span class="n">stdcat</span> <span class="o">=</span> <span class="n">pg_lib</span><span class="o">.</span><span class="n">Category</span><span class="p">(</span><span class="n">libthis</span><span class="p">,</span> <span class="n">libthat</span><span class="p">)</span>
</pre></div>
</div>
<p>Using the established project context, a connection would then be created as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pkg.lib</span> <span class="kn">import</span> <span class="n">stdcat</span>
<span class="kn">import</span> <span class="nn">postgresql</span> <span class="kn">as</span> <span class="nn">pg</span>
<span class="n">db</span> <span class="o">=</span> <span class="n">pg</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">category</span> <span class="o">=</span> <span class="n">stdcat</span><span class="p">)</span>
<span class="c1"># And execute some fictitious symbols.</span>
<span class="n">db</span><span class="o">.</span><span class="n">this</span><span class="o">.</span><span class="n">sym_from_libthis</span><span class="p">()</span>
<span class="n">db</span><span class="o">.</span><span class="n">that</span><span class="o">.</span><span class="n">sym_from_libthat</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="audience-and-motivation">
<h2>Audience and Motivation<a class="headerlink" href="#audience-and-motivation" title="Permalink to this headline"></a></h2>
<p>This chapter covers advanced material. It is <strong>not</strong> recommended that categories
and libraries be used for trivial applications or introductory projects.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Libraries and categories are not likely to be of interest to ORM or DB-API users.</p>
</div>
<p>With exception to ORMs or other similar abstractions, the most common pattern
for managing connections and statements is delegation:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyAppDB</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">connection</span> <span class="o">=</span> <span class="n">connection</span>

        <span class="k">def</span> <span class="nf">my_operation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">op_arg1</span><span class="p">,</span> <span class="n">op_arg2</span><span class="p">):</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">prepare</span><span class="p">(</span>
                        <span class="s2">&quot;SELECT my_operation_proc($1,$2)&quot;</span><span class="p">,</span>
                <span class="p">)(</span><span class="n">op_arg1</span><span class="p">,</span> <span class="n">op_arg2</span><span class="p">)</span>
<span class="o">...</span>
</pre></div>
</div>
<p>The straightforward nature is likeable, but the usage does not take advantage of
prepared statements. In order to do that an extra condition is necessary to see
if the statement has already been prepared:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="o">...</span>

<span class="k">def</span> <span class="nf">my_operation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">op_arg1</span><span class="p">,</span> <span class="n">op_arg2</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;_my_operation&#39;</span><span class="p">):</span>
                <span class="n">ps</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_my_operation</span>
        <span class="k">else</span><span class="p">:</span>
                <span class="n">ps</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_my_operation</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">prepare</span><span class="p">(</span>
                        <span class="s2">&quot;SELECT my_operation_proc($1, $2)&quot;</span><span class="p">,</span>
                <span class="p">)</span>
        <span class="k">return</span> <span class="n">ps</span><span class="p">(</span><span class="n">op_arg1</span><span class="p">,</span> <span class="n">op_arg2</span><span class="p">)</span>
<span class="o">...</span>
</pre></div>
</div>
<p>There are many variations that can implement the above. It works and it&#8217;s
simple, but it will be exhausting if repeated and error prone if the
initialization condition is not factored out. Additionally, if access to statement
metadata is needed, the above example is still lacking as it would require
execution of the statement and further protocol expectations to be established.
This is the province of libraries: direct database interface management.</p>
<p>Categories and Libraries are used to factor out and simplify
the above functionality so re-implementation is unnecessary. For example, an
ILF library containing the symbol:</p>
<div class="highlight-python"><div class="highlight"><pre>[my_operation]
SELECT my_operation_proc($1, $2)

[&lt;other_symbol&gt;]
...
</pre></div>
</div>
<p>Will provide the same functionality as the <code class="docutils literal"><span class="pre">my_operation</span></code> method in the
latter Python implementation.</p>
</div>
<div class="section" id="terminology">
<h2>Terminology<a class="headerlink" href="#terminology" title="Permalink to this headline"></a></h2>
<p>The following terms are used throughout this chapter:</p>
<blockquote>
<div><dl class="docutils">
<dt>Annotations</dt>
<dd>The information of about a Symbol describing what it is and how it should be
used.</dd>
<dt>Binding</dt>
<dd>An interface to the Symbols provided by a Library for use with a given
connection.</dd>
<dt>Bound Symbol</dt>
<dd>An interface to an individual Symbol ready for execution against the subject
database.</dd>
<dt>Bound Reference</dt>
<dd>An interface to an individual Reference Symbol that will produce a Bound
Symbol when executed.</dd>
<dt>ILF</dt>
<dd>INI-style Library Format. &#8220;lib{NAME}.sql&#8221; files.</dd>
<dt>Library</dt>
<dd>A collection of Symbols&#8211;mapping of names to SQL statements.</dd>
<dt>Local Symbol</dt>
<dd>A relative term used to denote a symbol that exists in the same library as
the subject symbol.</dd>
<dt>Preface</dt>
<dd>The block of text that comes before the first symbol in an ILF file.</dd>
<dt>Symbol</dt>
<dd>An named database operation provided by a Library. Usually, an SQL statement
with Annotations.</dd>
<dt>Reference Symbol</dt>
<dd>A Symbol whose SQL statement <em>produces</em> the source for a Bound Symbol.</dd>
<dt>Category</dt>
<dd>An object supporting a classification for connectors that provides database
initialization facilities for produced connections. For libraries,
<cite>postgresql.lib.Category</cite> objects are a set of Libraries,
<cite>postgresql.lib.Library</cite>.</dd>
</dl>
</div></blockquote>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Categories and Libraries</a><ul>
<li><a class="reference internal" href="#writing-libraries">Writing Libraries</a></li>
<li><a class="reference internal" href="#using-libraries">Using Libraries</a><ul>
<li><a class="reference internal" href="#categories">Categories</a></li>
</ul>
</li>
<li><a class="reference internal" href="#symbol-types">Symbol Types</a></li>
<li><a class="reference internal" href="#symbol-execution-methods">Symbol Execution Methods</a></li>
<li><a class="reference internal" href="#reference-symbols">Reference Symbols</a></li>
<li><a class="reference internal" href="#distributing-and-usage">Distributing and Usage</a></li>
<li><a class="reference internal" href="#audience-and-motivation">Audience and Motivation</a></li>
<li><a class="reference internal" href="#terminology">Terminology</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="index.html">Documentation overview</a><ul>
      <li>Previous: <a href="cluster.html" title="previous chapter">Cluster Management</a></li>
      <li>Next: <a href="clientparameters.html" title="next chapter">Client Parameters</a></li>
  </ul></li>
</ul>
</div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/lib.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;Python+Postgres.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.3.5</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.6</a>
      
      |
      <a href="_sources/lib.txt"
          rel="nofollow">Page source</a>
    </div>

    

    
  </body>
</html>