|
189 | 189 |
|
190 | 190 | After a library is created, it must be loaded before it can be bound using |
191 | 191 | programmer interfaces. The `postgresql.lib.load` interface provides the |
192 | | -primary entry point for loading libraries. ``load()`` accepts `str` objects |
193 | | -and modules. If given a module, it will create a |
194 | | -`postgresql.lib.ModuleLibrary` from the given module, and if a `str` object is |
195 | | -given it will create a `postgresql.lib.ILF` instance. |
| 192 | +primary entry point for loading libraries. |
196 | 193 |
|
197 | 194 | When ``load`` is given a string, it identifies if a directory separator is in |
198 | | -the string, if there is it will treat the string as a path to the ILF to be |
| 195 | +the string, if there is it will treat the string as a *path* to the ILF to be |
199 | 196 | loaded. If no separator is found, it will treat the string as the library |
200 | 197 | name fragment and look for "lib{NAME}.sql" in the directories listed in |
201 | 198 | `postgresql.sys.libpath`. |
|
336 | 333 | ... FROM STDIN``, the iterable must produce COPY lines. |
337 | 334 |
|
338 | 335 |
|
339 | | -Module Libraries |
340 | | -================ |
341 | | -
|
342 | | -Module libraries allow query libraries to be distributed as Python modules. |
343 | | -While not as portable as ILFs, they are convenient for Python projects |
344 | | -as no additional file management is required in order to use them. |
345 | | -
|
346 | | -Module libraries are designed to *not* require special dependencies in order to |
347 | | -annotate symbols. Rather, simple module protocols are used in order to |
348 | | -annotate symbols. |
349 | | -
|
350 | | -For instance, in order to make a preloaded Symbol, the symbol name needs to be |
351 | | -added to the ``__preload__`` collection in the module defining the symbol. |
352 | | -
|
353 | | -libmod.py:: |
354 | | -
|
355 | | - __preload__ = set() |
356 | | - this_symbol = "SELECT $1::text AS useless" |
357 | | - __preload__.add('this_symbol') |
358 | | -
|
359 | | -
|
360 | | -Symbol Annotations |
361 | | ------------------- |
362 | | -
|
363 | | -Module libraries perform symbol annotation by using specially named collection |
364 | | -objects to denote the characteristics of a symbol. The names of the collection |
365 | | -objects in a library module are consistent with statement method names: |
366 | | -
|
367 | | - ``__rows__`` |
368 | | - For each named symbol, use `postgresql.api.PreparedStatement.rows` as the |
369 | | - default execution method. |
370 | | -
|
371 | | - ``__chunks__`` |
372 | | - For each named symbol, use `postgresql.api.PreparedStatement.chunks` as the |
373 | | - default execution method. |
374 | | -
|
375 | | - ``__first__`` |
376 | | - For each named symbol, use `postgresql.api.PreparedStatement.first` as the |
377 | | - default execution method. |
378 | | -
|
379 | | - ``__declare__`` |
380 | | - For each named symbol, use `postgresql.api.PreparedStatement.declare` as the |
381 | | - default execution method. |
382 | | -
|
383 | | - ``__load_rows__`` |
384 | | - For each named symbol, use `postgresql.api.PreparedStatement.load_rows` as the |
385 | | - default execution method. |
386 | | -
|
387 | | - ``__load_chunks__`` |
388 | | - For each named symbol, use `postgresql.api.PreparedStatement.load_chunks` as |
389 | | - the default execution method. |
390 | | -
|
391 | | -The symbol names stored in the above objects must *not* intersect. |
392 | | -
|
393 | | -Additionally, symbols can be constants and preloaded using: |
394 | | -
|
395 | | - ``__preload__`` |
396 | | - Create Bound Symbols for each named symbol when the Library is bound. |
397 | | -
|
398 | | - ``__const__`` |
399 | | - Resolve the results of the query when the Library is bound and return the |
400 | | - results when the symbol is referenced. |
401 | | -
|
402 | | -The objects used to store the symbol names should be Python `set` objects. |
403 | | -
|
404 | | -To use a module library, import the module and give it to |
405 | | -`postgresql.lib.load`:: |
406 | | -
|
407 | | - >>> import libmod |
408 | | - >>> l = pg_lib.load(libmod) |
409 | | -
|
410 | | -
|
411 | 336 | Audience and Motivation |
412 | 337 | ======================= |
413 | 338 |
|
|
0 commit comments