This file is indexed.

/usr/share/doc/python-psycopg2-doc/html/sql.html is in python-psycopg2-doc 2.7.4-1.

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
<!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>psycopg2.sql – SQL string composition &#8212; Psycopg 2.7.4 documentation</title>
    <link rel="stylesheet" href="_static/psycopg.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '2.7.4',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </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="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="psycopg2.tz – tzinfo implementations for Psycopg 2" href="tz.html" />
    <link rel="prev" title="psycopg2.extras – Miscellaneous goodies for Psycopg 2" href="extras.html" /> 
  </head>
  <body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="tz.html" title="psycopg2.tz – tzinfo implementations for Psycopg 2"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="extras.html" title="psycopg2.extras – Miscellaneous goodies for Psycopg 2"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Psycopg 2.7.4 documentation</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-psycopg2.sql">
<span id="psycopg2-sql-sql-string-composition"></span><h1><a class="reference internal" href="#module-psycopg2.sql" title="psycopg2.sql"><code class="xref py py-obj docutils literal"><span class="pre">psycopg2.sql</span></code></a> – SQL string composition<a class="headerlink" href="#module-psycopg2.sql" title="Permalink to this headline"></a></h1>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
<p>The module contains objects and functions useful to generate SQL dynamically,
in a convenient and safe way. SQL identifiers (e.g. names of tables and
fields) cannot be passed to the <a class="reference internal" href="cursor.html#cursor.execute" title="cursor.execute"><code class="xref py py-obj docutils literal"><span class="pre">execute()</span></code></a> method like query
arguments:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># This will not work</span>
<span class="n">table_name</span> <span class="o">=</span> <span class="s1">&#39;my_table&#39;</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;insert into </span><span class="si">%s</span><span class="s2"> values (</span><span class="si">%s</span><span class="s2">, </span><span class="si">%s</span><span class="s2">)&quot;</span><span class="p">,</span> <span class="p">[</span><span class="n">table_name</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">])</span>
</pre></div>
</div>
<p>The SQL query should be composed before the arguments are merged, for
instance:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># This works, but it is not optimal</span>
<span class="n">table_name</span> <span class="o">=</span> <span class="s1">&#39;my_table&#39;</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
    <span class="s2">&quot;insert into </span><span class="si">%s</span><span class="s2"> values (</span><span class="si">%%</span><span class="s2">s, </span><span class="si">%%</span><span class="s2">s)&quot;</span> <span class="o">%</span> <span class="n">table_name</span><span class="p">,</span>
    <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">])</span>
</pre></div>
</div>
<p>This sort of works, but it is an accident waiting to happen: the table name
may be an invalid SQL literal and need quoting; even more serious is the
security problem in case the table name comes from an untrusted source. The
name should be escaped using <a class="reference internal" href="extensions.html#psycopg2.extensions.quote_ident" title="psycopg2.extensions.quote_ident"><code class="xref py py-obj docutils literal"><span class="pre">quote_ident()</span></code></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># This works, but it is not optimal</span>
<span class="n">table_name</span> <span class="o">=</span> <span class="s1">&#39;my_table&#39;</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
    <span class="s2">&quot;insert into </span><span class="si">%s</span><span class="s2"> values (</span><span class="si">%%</span><span class="s2">s, </span><span class="si">%%</span><span class="s2">s)&quot;</span> <span class="o">%</span> <span class="n">ext</span><span class="o">.</span><span class="n">quote_ident</span><span class="p">(</span><span class="n">table_name</span><span class="p">),</span>
    <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">])</span>
</pre></div>
</div>
<p>This is now safe, but it somewhat ad-hoc. In case, for some reason, it is
necessary to include a value in the query string (as opposite as in a value)
the merging rule is still different (<a class="reference internal" href="extensions.html#psycopg2.extensions.adapt" title="psycopg2.extensions.adapt"><code class="xref py py-obj docutils literal"><span class="pre">adapt()</span></code></a> should be
used…). It is also still relatively dangerous: if <code class="xref py py-obj docutils literal"><span class="pre">quote_ident()</span></code> is
forgotten somewhere, the program will usually work, but will eventually crash
in the presence of a table or field name with containing characters to escape,
or will present a potentially exploitable weakness.</p>
<p>The objects exposed by the <code class="xref py py-obj docutils literal"><span class="pre">psycopg2.sql</span></code> module allow generating SQL
statements on the fly, separating clearly the variable parts of the statement
from the query parameters:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">psycopg2</span> <span class="kn">import</span> <span class="n">sql</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
    <span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s2">&quot;insert into {} values (</span><span class="si">%s</span><span class="s2">, </span><span class="si">%s</span><span class="s2">)&quot;</span><span class="p">)</span>
        <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s1">&#39;my_table&#39;</span><span class="p">)),</span>
    <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">])</span>
</pre></div>
</div>
<p>The objects exposed by the <code class="xref py py-obj docutils literal"><span class="pre">sql</span></code> module can be used to compose a query as a
Python string (using the <a class="reference internal" href="#psycopg2.sql.Composable.as_string" title="psycopg2.sql.Composable.as_string"><code class="xref py py-obj docutils literal"><span class="pre">as_string()</span></code></a> method) or passed directly
to cursor methods such as <a class="reference internal" href="cursor.html#cursor.execute" title="cursor.execute"><code class="xref py py-obj docutils literal"><span class="pre">execute()</span></code></a>, <a class="reference internal" href="cursor.html#cursor.executemany" title="cursor.executemany"><code class="xref py py-obj docutils literal"><span class="pre">executemany()</span></code></a>,
<a class="reference internal" href="cursor.html#cursor.copy_expert" title="cursor.copy_expert"><code class="xref py py-obj docutils literal"><span class="pre">copy_expert()</span></code></a>.</p>
<dl class="class">
<dt id="psycopg2.sql.Composable">
<em class="property">class </em><code class="descclassname">psycopg2.sql.</code><code class="descname">Composable</code><span class="sig-paren">(</span><em>wrapped</em><span class="sig-paren">)</span><a class="headerlink" href="#psycopg2.sql.Composable" title="Permalink to this definition"></a></dt>
<dd><p>Abstract base class for objects that can be used to compose an SQL string.</p>
<p><code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code> objects can be passed directly to <a class="reference internal" href="cursor.html#cursor.execute" title="cursor.execute"><code class="xref py py-obj docutils literal"><span class="pre">execute()</span></code></a>,
<a class="reference internal" href="cursor.html#cursor.executemany" title="cursor.executemany"><code class="xref py py-obj docutils literal"><span class="pre">executemany()</span></code></a>, <a class="reference internal" href="cursor.html#cursor.copy_expert" title="cursor.copy_expert"><code class="xref py py-obj docutils literal"><span class="pre">copy_expert()</span></code></a> in place of the query
string.</p>
<p><code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code> objects can be joined using the <code class="docutils literal"><span class="pre">+</span></code> operator: the result
will be a <a class="reference internal" href="#psycopg2.sql.Composed" title="psycopg2.sql.Composed"><code class="xref py py-obj docutils literal"><span class="pre">Composed</span></code></a> instance containing the objects joined. The operator
<code class="docutils literal"><span class="pre">*</span></code> is also supported with an integer argument: the result is a
<code class="xref py py-obj docutils literal"><span class="pre">Composed</span></code> instance containing the left argument repeated as many times as
requested.</p>
<dl class="method">
<dt id="psycopg2.sql.Composable.as_string">
<code class="descname">as_string</code><span class="sig-paren">(</span><em>context</em><span class="sig-paren">)</span><a class="headerlink" href="#psycopg2.sql.Composable.as_string" title="Permalink to this definition"></a></dt>
<dd><p>Return the string value of the object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>context</strong> (<a class="reference internal" href="connection.html#connection" title="connection"><code class="xref py py-obj docutils literal"><span class="pre">connection</span></code></a> or <a class="reference internal" href="cursor.html#cursor" title="cursor"><code class="xref py py-obj docutils literal"><span class="pre">cursor</span></code></a>) – the context to evaluate the string into.</td>
</tr>
</tbody>
</table>
<p>The method is automatically invoked by <a class="reference internal" href="cursor.html#cursor.execute" title="cursor.execute"><code class="xref py py-obj docutils literal"><span class="pre">execute()</span></code></a>,
<a class="reference internal" href="cursor.html#cursor.executemany" title="cursor.executemany"><code class="xref py py-obj docutils literal"><span class="pre">executemany()</span></code></a>, <a class="reference internal" href="cursor.html#cursor.copy_expert" title="cursor.copy_expert"><code class="xref py py-obj docutils literal"><span class="pre">copy_expert()</span></code></a> if a <code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code> is
passed instead of the query string.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="psycopg2.sql.SQL">
<em class="property">class </em><code class="descclassname">psycopg2.sql.</code><code class="descname">SQL</code><span class="sig-paren">(</span><em>string</em><span class="sig-paren">)</span><a class="headerlink" href="#psycopg2.sql.SQL" title="Permalink to this definition"></a></dt>
<dd><p>A <a class="reference internal" href="#psycopg2.sql.Composable" title="psycopg2.sql.Composable"><code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code></a> representing a snippet of SQL statement.</p>
<p><code class="xref py py-obj docutils literal"><span class="pre">SQL</span></code> exposes <a class="reference internal" href="#psycopg2.sql.SQL.join" title="psycopg2.sql.SQL.join"><code class="xref py py-obj docutils literal"><span class="pre">join()</span></code></a> and <a class="reference internal" href="#psycopg2.sql.SQL.format" title="psycopg2.sql.SQL.format"><code class="xref py py-obj docutils literal"><span class="pre">format()</span></code></a> methods useful to create a template
where to merge variable parts of a query (for instance field or table
names).</p>
<p>The <em>string</em> doesn’t undergo any form of escaping, so it is not suitable to
represent variable identifiers or values: you should only use it to pass
constant strings representing templates or snippets of SQL statements; use
other objects such as <a class="reference internal" href="#psycopg2.sql.Identifier" title="psycopg2.sql.Identifier"><code class="xref py py-obj docutils literal"><span class="pre">Identifier</span></code></a> or <a class="reference internal" href="#psycopg2.sql.Literal" title="psycopg2.sql.Literal"><code class="xref py py-obj docutils literal"><span class="pre">Literal</span></code></a> to represent variable
parts.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">query</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s2">&quot;select {0} from {1}&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">),</span> <span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s1">&#39;bar&#39;</span><span class="p">)]),</span>
<span class="gp">... </span>   <span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s1">&#39;table&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">query</span><span class="o">.</span><span class="n">as_string</span><span class="p">(</span><span class="n">conn</span><span class="p">))</span>
<span class="go">select &quot;foo&quot;, &quot;bar&quot; from &quot;table&quot;</span>
</pre></div>
</div>
<dl class="attribute">
<dt id="psycopg2.sql.SQL.string">
<code class="descname">string</code><a class="headerlink" href="#psycopg2.sql.SQL.string" title="Permalink to this definition"></a></dt>
<dd><p>The string wrapped by the <code class="xref py py-obj docutils literal"><span class="pre">SQL</span></code> object.</p>
</dd></dl>

<dl class="method">
<dt id="psycopg2.sql.SQL.format">
<code class="descname">format</code><span class="sig-paren">(</span><em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#psycopg2.sql.SQL.format" title="Permalink to this definition"></a></dt>
<dd><p>Merge <a class="reference internal" href="#psycopg2.sql.Composable" title="psycopg2.sql.Composable"><code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code></a> objects into a template.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>args</strong> (<a class="reference internal" href="#psycopg2.sql.Composable" title="psycopg2.sql.Composable"><em>Composable</em></a>) – parameters to replace to numbered
(<code class="docutils literal"><span class="pre">{0}</span></code>, <code class="docutils literal"><span class="pre">{1}</span></code>) or auto-numbered (<code class="docutils literal"><span class="pre">{}</span></code>) placeholders</li>
<li><strong>kwargs</strong> (<a class="reference internal" href="#psycopg2.sql.Composable" title="psycopg2.sql.Composable"><em>Composable</em></a>) – parameters to replace to named (<code class="docutils literal"><span class="pre">{name}</span></code>)
placeholders</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">the union of the <code class="xref py py-obj docutils literal"><span class="pre">SQL</span></code> string with placeholders replaced</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last"><a class="reference internal" href="#psycopg2.sql.Composed" title="psycopg2.sql.Composed"><code class="xref py py-obj docutils literal"><span class="pre">Composed</span></code></a></p>
</td>
</tr>
</tbody>
</table>
<p>The method is similar to the Python <code class="xref py py-obj docutils literal"><span class="pre">str.format()</span></code> method: the string
template supports auto-numbered (<code class="docutils literal"><span class="pre">{}</span></code>, only available from Python
2.7), numbered (<code class="docutils literal"><span class="pre">{0}</span></code>, <code class="docutils literal"><span class="pre">{1}</span></code>…), and named placeholders
(<code class="docutils literal"><span class="pre">{name}</span></code>), with positional arguments replacing the numbered
placeholders and keywords replacing the named ones. However placeholder
modifiers (<code class="docutils literal"><span class="pre">{0!r}</span></code>, <code class="docutils literal"><span class="pre">{0:&lt;10}</span></code>) are not supported. Only
<code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code> objects can be passed to the template.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s2">&quot;select * from {} where {} = </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s1">&#39;people&#39;</span><span class="p">),</span> <span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">))</span>
<span class="gp">... </span>    <span class="o">.</span><span class="n">as_string</span><span class="p">(</span><span class="n">conn</span><span class="p">))</span>
<span class="go">select * from &quot;people&quot; where &quot;id&quot; = %s</span>

<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s2">&quot;select * from {tbl} where {pkey} = </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">tbl</span><span class="o">=</span><span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s1">&#39;people&#39;</span><span class="p">),</span> <span class="n">pkey</span><span class="o">=</span><span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">))</span>
<span class="gp">... </span>    <span class="o">.</span><span class="n">as_string</span><span class="p">(</span><span class="n">conn</span><span class="p">))</span>
<span class="go">select * from &quot;people&quot; where &quot;id&quot; = %s</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="psycopg2.sql.SQL.join">
<code class="descname">join</code><span class="sig-paren">(</span><em>seq</em><span class="sig-paren">)</span><a class="headerlink" href="#psycopg2.sql.SQL.join" title="Permalink to this definition"></a></dt>
<dd><p>Join a sequence of <a class="reference internal" href="#psycopg2.sql.Composable" title="psycopg2.sql.Composable"><code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code></a>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>seq</strong> (iterable of <code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code>) – the elements to join.</td>
</tr>
</tbody>
</table>
<p>Use the <code class="xref py py-obj docutils literal"><span class="pre">SQL</span></code> object’s <em>string</em> to separate the elements in <em>seq</em>.
Note that <a class="reference internal" href="#psycopg2.sql.Composed" title="psycopg2.sql.Composed"><code class="xref py py-obj docutils literal"><span class="pre">Composed</span></code></a> objects are iterable too, so they can be used as
argument for this method.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">snip</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;bar&#39;</span><span class="p">,</span> <span class="s1">&#39;baz&#39;</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">snip</span><span class="o">.</span><span class="n">as_string</span><span class="p">(</span><span class="n">conn</span><span class="p">))</span>
<span class="go">&quot;foo&quot;, &quot;bar&quot;, &quot;baz&quot;</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="psycopg2.sql.Identifier">
<em class="property">class </em><code class="descclassname">psycopg2.sql.</code><code class="descname">Identifier</code><span class="sig-paren">(</span><em>string</em><span class="sig-paren">)</span><a class="headerlink" href="#psycopg2.sql.Identifier" title="Permalink to this definition"></a></dt>
<dd><p>A <a class="reference internal" href="#psycopg2.sql.Composable" title="psycopg2.sql.Composable"><code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code></a> representing an SQL identifer.</p>
<p>Identifiers usually represent names of database objects, such as tables or
fields. PostgreSQL identifiers follow <a class="reference external" href="https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS">different rules</a> than SQL string
literals for escaping (e.g. they use double quotes instead of single).</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">t1</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s2">&quot;foo&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t2</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s2">&quot;ba&#39;r&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t3</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s1">&#39;ba&quot;z&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">t1</span><span class="p">,</span> <span class="n">t2</span><span class="p">,</span> <span class="n">t3</span><span class="p">])</span><span class="o">.</span><span class="n">as_string</span><span class="p">(</span><span class="n">conn</span><span class="p">))</span>
<span class="go">&quot;foo&quot;, &quot;ba&#39;r&quot;, &quot;ba&quot;&quot;z&quot;</span>
</pre></div>
</div>
<dl class="attribute">
<dt id="psycopg2.sql.Identifier.string">
<code class="descname">string</code><a class="headerlink" href="#psycopg2.sql.Identifier.string" title="Permalink to this definition"></a></dt>
<dd><p>The string wrapped by the <a class="reference internal" href="#psycopg2.sql.Identifier" title="psycopg2.sql.Identifier"><code class="xref py py-obj docutils literal"><span class="pre">Identifier</span></code></a>.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="psycopg2.sql.Literal">
<em class="property">class </em><code class="descclassname">psycopg2.sql.</code><code class="descname">Literal</code><span class="sig-paren">(</span><em>wrapped</em><span class="sig-paren">)</span><a class="headerlink" href="#psycopg2.sql.Literal" title="Permalink to this definition"></a></dt>
<dd><p>A <a class="reference internal" href="#psycopg2.sql.Composable" title="psycopg2.sql.Composable"><code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code></a> representing an SQL value to include in a query.</p>
<p>Usually you will want to include placeholders in the query and pass values
as <a class="reference internal" href="cursor.html#cursor.execute" title="cursor.execute"><code class="xref py py-obj docutils literal"><span class="pre">execute()</span></code></a> arguments. If however you really really need to
include a literal value in the query you can use this object.</p>
<p>The string returned by <code class="xref py py-obj docutils literal"><span class="pre">as_string()</span></code> follows the normal <a class="reference internal" href="usage.html#python-types-adaptation"><span class="std std-ref">adaptation
rules</span></a> for Python objects.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s1</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">Literal</span><span class="p">(</span><span class="s2">&quot;foo&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s2</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">Literal</span><span class="p">(</span><span class="s2">&quot;ba&#39;r&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s3</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">Literal</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">s1</span><span class="p">,</span> <span class="n">s2</span><span class="p">,</span> <span class="n">s3</span><span class="p">])</span><span class="o">.</span><span class="n">as_string</span><span class="p">(</span><span class="n">conn</span><span class="p">))</span>
<span class="go">&#39;foo&#39;, &#39;ba&#39;&#39;r&#39;, 42</span>
</pre></div>
</div>
<dl class="attribute">
<dt id="psycopg2.sql.Literal.wrapped">
<code class="descname">wrapped</code><a class="headerlink" href="#psycopg2.sql.Literal.wrapped" title="Permalink to this definition"></a></dt>
<dd><p>The object wrapped by the <code class="xref py py-obj docutils literal"><span class="pre">Literal</span></code>.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="psycopg2.sql.Placeholder">
<em class="property">class </em><code class="descclassname">psycopg2.sql.</code><code class="descname">Placeholder</code><span class="sig-paren">(</span><em>name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#psycopg2.sql.Placeholder" title="Permalink to this definition"></a></dt>
<dd><p>A <a class="reference internal" href="#psycopg2.sql.Composable" title="psycopg2.sql.Composable"><code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code></a> representing a placeholder for query parameters.</p>
<p>If the name is specified, generate a named placeholder (e.g. <code class="docutils literal"><span class="pre">%(name)s</span></code>),
otherwise generate a positional placeholder (e.g. <code class="docutils literal"><span class="pre">%s</span></code>).</p>
<p>The object is useful to generate SQL queries with a variable number of
arguments.</p>
<p>Examples:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;bar&#39;</span><span class="p">,</span> <span class="s1">&#39;baz&#39;</span><span class="p">]</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">q1</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s2">&quot;insert into table ({}) values ({})&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">,</span> <span class="n">names</span><span class="p">)),</span>
<span class="gp">... </span>    <span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">sql</span><span class="o">.</span><span class="n">Placeholder</span><span class="p">()</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">names</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">q1</span><span class="o">.</span><span class="n">as_string</span><span class="p">(</span><span class="n">conn</span><span class="p">))</span>
<span class="go">insert into table (&quot;foo&quot;, &quot;bar&quot;, &quot;baz&quot;) values (%s, %s, %s)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">q2</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s2">&quot;insert into table ({}) values ({})&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">,</span> <span class="n">names</span><span class="p">)),</span>
<span class="gp">... </span>    <span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">sql</span><span class="o">.</span><span class="n">Placeholder</span><span class="p">,</span> <span class="n">names</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">q2</span><span class="o">.</span><span class="n">as_string</span><span class="p">(</span><span class="n">conn</span><span class="p">))</span>
<span class="go">insert into table (&quot;foo&quot;, &quot;bar&quot;, &quot;baz&quot;) values (%(foo)s, %(bar)s, %(baz)s)</span>
</pre></div>
</div>
<dl class="attribute">
<dt id="psycopg2.sql.Placeholder.name">
<code class="descname">name</code><a class="headerlink" href="#psycopg2.sql.Placeholder.name" title="Permalink to this definition"></a></dt>
<dd><p>The name of the <code class="xref py py-obj docutils literal"><span class="pre">Placeholder</span></code>.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="psycopg2.sql.Composed">
<em class="property">class </em><code class="descclassname">psycopg2.sql.</code><code class="descname">Composed</code><span class="sig-paren">(</span><em>seq</em><span class="sig-paren">)</span><a class="headerlink" href="#psycopg2.sql.Composed" title="Permalink to this definition"></a></dt>
<dd><p>A <a class="reference internal" href="#psycopg2.sql.Composable" title="psycopg2.sql.Composable"><code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code></a> object made of a sequence of <code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code>.</p>
<p>The object is usually created using <code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code> operators and methods.
However it is possible to create a <code class="xref py py-obj docutils literal"><span class="pre">Composed</span></code> directly specifying a
sequence of <code class="xref py py-obj docutils literal"><span class="pre">Composable</span></code> as arguments.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">comp</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">Composed</span><span class="p">(</span>
<span class="gp">... </span>    <span class="p">[</span><span class="n">sql</span><span class="o">.</span><span class="n">SQL</span><span class="p">(</span><span class="s2">&quot;insert into &quot;</span><span class="p">),</span> <span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s2">&quot;table&quot;</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">comp</span><span class="o">.</span><span class="n">as_string</span><span class="p">(</span><span class="n">conn</span><span class="p">))</span>
<span class="go">insert into &quot;table&quot;</span>
</pre></div>
</div>
<p><code class="xref py py-obj docutils literal"><span class="pre">Composed</span></code> objects are iterable (so they can be used in <a class="reference internal" href="#psycopg2.sql.SQL.join" title="psycopg2.sql.SQL.join"><code class="xref py py-obj docutils literal"><span class="pre">SQL.join</span></code></a> for
instance).</p>
<dl class="attribute">
<dt id="psycopg2.sql.Composed.seq">
<code class="descname">seq</code><a class="headerlink" href="#psycopg2.sql.Composed.seq" title="Permalink to this definition"></a></dt>
<dd><p>The list of the content of the <code class="xref py py-obj docutils literal"><span class="pre">Composed</span></code>.</p>
</dd></dl>

<dl class="method">
<dt id="psycopg2.sql.Composed.join">
<code class="descname">join</code><span class="sig-paren">(</span><em>joiner</em><span class="sig-paren">)</span><a class="headerlink" href="#psycopg2.sql.Composed.join" title="Permalink to this definition"></a></dt>
<dd><p>Return a new <code class="xref py py-obj docutils literal"><span class="pre">Composed</span></code> interposing the <em>joiner</em> with the <code class="xref py py-obj docutils literal"><span class="pre">Composed</span></code> items.</p>
<p>The <em>joiner</em> must be a <a class="reference internal" href="#psycopg2.sql.SQL" title="psycopg2.sql.SQL"><code class="xref py py-obj docutils literal"><span class="pre">SQL</span></code></a> or a string which will be interpreted as
an <a class="reference internal" href="#psycopg2.sql.SQL" title="psycopg2.sql.SQL"><code class="xref py py-obj docutils literal"><span class="pre">SQL</span></code></a>.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fields</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">sql</span><span class="o">.</span><span class="n">Identifier</span><span class="p">(</span><span class="s1">&#39;bar&#39;</span><span class="p">)</span>  <span class="c1"># a Composed</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">fields</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">as_string</span><span class="p">(</span><span class="n">conn</span><span class="p">))</span>
<span class="go">&quot;foo&quot;, &quot;bar&quot;</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="extras.html"
                        title="previous chapter"><code class="docutils literal"><span class="pre">psycopg2.extras</span></code> – Miscellaneous goodies for Psycopg 2</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="tz.html"
                        title="next chapter"><code class="docutils literal"><span class="pre">psycopg2.tz</span></code><code class="docutils literal"><span class="pre">tzinfo</span></code> implementations for Psycopg 2</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/sql.rst.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">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="tz.html" title="psycopg2.tz – tzinfo implementations for Psycopg 2"
             >next</a> |</li>
        <li class="right" >
          <a href="extras.html" title="psycopg2.extras – Miscellaneous goodies for Psycopg 2"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Psycopg 2.7.4 documentation</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2001-2018, Federico Di Gregorio, Daniele Varrazzo.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.6.
    </div>
  </body>
</html>