Sophie

Sophie

distrib > Mandriva > 2007.0 > i586 > media > contrib-release > by-pkgid > 0993846d17e795d45984b12af4ba9206 > files > 83

kinterbasdb-3.2-2mdv2007.0.i586.rpm

<html>

<head>
<title>KInterbasDB Usage Guide</title>
<link rel="stylesheet" href="global.css" type="text/css">
</head>

<body>

<a href="index.html">Overall Table of Contents</a>

<hr>

<a name="top"><h1 class="compactHeading">KInterbasDB Usage Guide</h1></a>
<p style="font-size: 0.6em; margin-top: 0px; margin-left: 1.4em;">
  (Last updated 2006.08.11 at 15:07 UTC)
</p>

<a name="toc"><h3>Contents</h3></a>
<ul>
    <li><a href="#introduction">Introduction</a>
        <br><br>
    </li>

    <li><a href="#db_api_compliance">Python Database API 2.0 Compliance</a>
        <ul>
            <li><a href="#db_api_incompatibilities">Incompatibilities</a></li>
            <li><a href="#db_api_optional_unsupported">Unsupported Optional Features</a></li>
            <li><a href="#db_api_optional_nominally_supported">Nominally Supported Optional Features</a></li>
            <li><a href="#db_api_extensions_and_caveats">Extensions and Caveats</a></li>
        </ul>
        <br>
    </li>

    <li><a href="#tutorial">Brief Tutorial</a>
        <ul>
            <li><a href="#tutorial_connect">Connecting to a Database</a></li>
            <li><a href="#tutorial_execute_sql">Executing SQL Statements (and Retrieving Results)</a></li>
            <li><a href="#tutorial_stored_procedures">Calling Stored Procedures</a></li>
        </ul>
        <br>
    </li>

    <li><a href="#adv">Native Database Engine Features and Extensions Beyond the Python DB API</a>
        <ul>
            <li><a href="#adv_db_creation_deletion">Programmatic Database Creation and Deletion</a></li>
            <li><a href="#adv_event">Database Event Notification</a> <span class="tocUpdated">(Updated for 3.2)</span></li>
            <li><a href="#adv_ct">Connection Timeouts</a> <span class="tocUpdated">(Updated for 3.2)</span></li>

            <li><a href="#adv_trans_control">Advanced Transaction Control</a>
                <ul>
                    <li><a href="#adv_trans_control_parameters">Transaction Parameters</a></li>
                    <li><a href="#adv_trans_control_retaining">Retaining Operations</a></li>
                    <li><a href="#adv_trans_control_savepoints">Savepoints</a></li>
                    <li><a href="#adv_trans_control_distributed">Distributed Transactions</a></li>
                </ul>
            </li>

            <li><a href="#adv_param_conv">Parameter Conversion</a>
                <ul>
                    <li><a href="#adv_param_conv_implicit_from_string">Implicit Conversion of Input Parameters from Strings</a></li>
                    <li><a href="#adv_param_conv_dynamic_type_translation">Dynamic Type Translation</a> <span class="tocUpdated">(Updated for 3.2)</span></li>
                    <li><a href="#adv_param_conv_database_arrays">Database Arrays</a></li>
                    <li><a href="#adv_param_conv_blobs">Blobs</a> <span class="tocUpdated">(Updated for 3.2)</span></li>
                </ul>
            </li>

            <li><a href="#adv_prepared_statements">Prepared Statements</a> <span class="tocUpdated">(Updated for 3.2)</span></li>

            <li><a href="#adv_named_cursors">Named Cursors</a></li>

            <li><a href="#adv_prog_maint">Programmatic Server, Database, and User Maintenance</a>
                <ul>
                    <li><a href="#adv_prog_maint_servapi">Services API</a>
                        <ul>
                            <li><a href="#adv_prog_maint_servapi_connect">Establishing Services API Connections</a></li>
                            <li><a href="#adv_prog_maint_servapi_server_config">Querying Server Configuration and Activity Levels</a></li>
                            <li><a href="#adv_prog_maint_servapi_database_stats">Querying Database Statistics</a></li>
                            <li><a href="#adv_prog_maint_servapi_backup_restore">Backup and Restoration</a></li>
                            <li><a href="#adv_prog_maint_servapi_database_maint">Controlling Database Operating Modes, Sweeps, and Repair</a></li>
                            <li><a href="#adv_prog_maint_servapi_users">User Maintenance</a></li>
                        </ul>
                    </li>
                    <li><a href="#adv_prog_maint_database_info">The <code>database_info</code> Method</a></li>
                    <li><a href="#adv_prog_maint_db_info">The <code>db_info</code> Method (High-Level Wrapper Around <code>database_info</code>)</a> <span class="tocUpdated">(Updated for 3.2)</span></li>
                </ul>
            </li>
        </ul>
        <br>
    </li>

    <li><a href="#special_issues">Special Issues</a>
      <ul>
          <li><a href="#special_issue_concurrency">Concurrency</a> <span class="tocUpdated">(Updated for 3.2)</span></li>
      </ul>
      <br>
    </li>

    <li><a href="#faq_fep">Frequently Asked Questions and Frequently Encountered Pitfalls</a>
        <ul>
          <li><a href="#faq_fep_is_mxdatetime_required">Does KInterbasDB require <code>mx.DateTime</code>?</a> <span class="tocUpdated">(Updated for 3.2)</span></li>
          <li><a href="#faq_fep_fixed_point_precise">Precise Fixed Point (<code>NUMERIC</code>/<code>DECIMAL</code>) Handling</a> <span class="tocUpdated">(Updated for 3.2)</span></li>
          <li><a href="#faq_fep_result_set_fields_by_name">Refer to Result Row Fields by Name Rather than Index</a></li>
          <li><a href="#faq_fep_unicode">Unicode Fields and KInterbasDB</a></li>
          <li><a href="#faq_fep_embedded_using_with">Using KInterbasDB with Embedded Firebird</a></li>
          <li><a href="#faq_fep_zope_using_with">Using KInterbasDB with Zope</a></li>
          <li><a href="#faq_fep_services_api_classic_embedded">Services API on the Classic and Embedded Server Architectures</a></li>
        </ul>
        <br>
    </li>

    <li><a href="#references">References (external links)</a></li>

    <li><a href="#feedback">Feedback</a><br><br></li>
</ul>

<hr>

<a name="introduction">
  <span style="font-size: 175%; font-weight: bold;">Introduction</span>
  <span style="font-size: 90%; text-decoration: line-through;">(Propaganda)</span>
</a>

<p class="textParagraph" style="margin-top: 1em;">
The <a href="http://firebirdsql.org">Firebird</a> relational database engine
  has a large feature set,
  conforms closely to SQL standards,
  and is flexible enough to operate either as a standalone server or
    as an embedded library on diverse platforms.
In spite of this versatility, the database is easy to
use--almost self-managing.
</p>

<p class="textParagraph" style="margin-bottom: 3px;">
The <a href="http://python.org">Python</a> programming language
  supports numerous paradigms,
  is suitable for constructing both small and large programs,
  and integrates well with native C and C++ libraries.
Despite the versatility of the language, well written Python code achieves an
exceptional lucidity that has led some to call the language
"executable pseudocode".
</p>

<p class="textParagraph">
These two top-flight software tools intersect in a library named KInterbasDB.
KInterbasDB implements Python's standard
<a href="Python-DB-API-2.0.html">Database API 2.0</a>,
but also extends far beyond, to cover almost all of Firebird's extensive
native client API.
KInterbasDB strives to deliver the power of Firebird into the hands of the
Python programmer without compromising the qualities of either tool.
</p>

<p class="textParagraph">
This Usage Guide is not a tutorial on Python, SQL, or Firebird; rather, it
is a topical presentation of KInterbasDB's feature set, with example code
to demonstrate basic usage patterns.
This guide is meant to be consumed in conjunction with the
<a href="Python-DB-API-2.0.html">Python Database API Specification</a>
and the
<a href="#ref_fb_docs_directory">Firebird documentation</a>, especially the
professional, seven-volume
<a href="#ref_ib6_docs">manual</a>
for Firebird's commercial ancestor, Interbase&reg;.
</p>

<p class="textParagraph">
The <a href="#toc">table of contents</a> presents a structural overview
of this document.
</p>


<br>
<hr>
<br>

<a name="db_api_compliance"><h2>Python Database API 2.0 Compliance</h2></a>

<a name="db_api_incompatibilities"><h3>Incompatibilities</h3></a>
<ul>
  <li><code>DATETIME</code> type comparison singleton
        <p class="textParagraph">
        KInterbasDB's deferred loading of dynamic type translators causes this
        singleton to behave in violation of the standard until the
        <code>kinterbasdb.init</code> function has been called (whether
        explicitly or implicitly).
        </p>
        <p class="textParagraph">
        For more information, see
        <a href="#adv_param_conv_dynamic_type_translation_deferred_loading_backcompat">this section</a>.
        </p>
  </li>
</ul>


<a name="db_api_optional_unsupported"><h3>Unsupported Optional Features</h3></a>
<ul>
  <li><code>Cursor</code> class
    <ul>
        <li><code>nextset</code> method<br>
        <p class="textParagraph">
        This method is not implemented because the database engine does
        not support opening multiple result sets simultaneously
        with a single cursor.
        </p>
        <br>
        </li>
    </ul>
  </li>
</ul>

<a name="db_api_optional_nominally_supported"><h3>Nominally Supported Optional Features</h3></a>
<ul>
  <li><code>Cursor</code> class
    <ul>
        <li><code>arraysize</code> attribute<br>
        <p class="textParagraph">
        As required by the spec, the value of this attribute
        is observed with respect to the <code>fetchmany</code>
        method.  However, changing the value of this attribute does
        not make any difference in fetch efficiency because the
        database engine only supports fetching a single row at a time.
        </p>
        <br>
        </li>

        <li><code>setinputsizes</code> method<br>
        <p class="textParagraph">
        Although this method is present, it does nothing, as allowed
        by the spec.
        </p>
        <br>
        </li>
        <li><code>setoutputsize</code> method<br>
        <p class="textParagraph">
        Although this method is present, it does nothing, as allowed
        by the spec.
        </p>
        </li>
    </ul>
  </li>
</ul>

<a name="db_api_extensions_and_caveats"><h3>Extensions and Caveats</h3></a>

<p class="textParagraph">
KInterbasDB offers a large feature set beyond the minimal requirements of
the Python DB API.
Most of these extensions are documented in the section of this document
entitled
<a href="#adv">Native Database Engine Features and Extensions Beyond the Python DB API</a>.
</p>

<p class="textParagraph">
This section attempts to document only those features that overlap with
the DB API, or are too insignificant to warrant their own subsection
elsewhere.
</p>


<ul>
  <li><code>connect</code> function
    <p class="textParagraph">
    This function supports the following optional keyword arguments in
    addition to those required by the spec:
    <ul>
        <li><code>role</code> -
        for connecting to a database with a specific SQL role
        (see page 92 of the
        <a href="#ref_ib6_docs">Interbase&reg; 6 Operations Guide</a>
        for a discussion of Interbase&reg; roles).
        <p class="textParagraph" style="margin-bottom: 0px;">
        Example:
        </p>
<pre style="margin-top: 0px;">
    kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'host:/path/database.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'limited_user'</FONT>,
        password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>, <strong>role</strong>=<FONT COLOR="#8f8c47">'MORE_POWERFUL_ROLE'</FONT>)
</pre>
        </li>
        <li><code>charset</code> -
        for explicitly specifying the character set of the connection.
        See page 221 of the
        <a href="#ref_ib6_docs">Interbase&reg; 6 Data Definition Guide</a>
        for a list of available character sets, and
        <a href="#faq_fep_unicode">this FAQ</a>
        for information on handling extended character sets with KInterbasDB.
        <p class="textParagraph" style="margin-bottom: 0px;">
        Example:
        </p>
<pre style="margin-top: 0px;">
    kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'host:/path/database.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>,
        password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>, <strong>charset</strong><FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'UNICODE_FSS'</FONT>)
</pre>
        </li>
        <li><code>dialect</code> -
        for explicitly specifying the SQL dialect of the connection.
        <p class="textParagraph">
        In KInterbasDB 2.x, the default dialect was <code>1</code>
        (the compatibility dialect for Interbase&reg; 5.5 and earlier).
        In KInterbasDB 3.x, the default dialect is <code>3</code>
        (the most featureful dialect, ideal for Interbase&reg; 6.0+
        and Firebird).
        If you want to connect to Interbase&reg; 5.5 or earlier, you must
        explicitly set this argument's value to <code>1</code>.
        Dialect <code>2</code> is a transitional dialect that is
        normally used only during ports from IB &lt; 6 to IB &gt;= 6 or
        Firebird.
        </p>
        <p class="textParagraph" style="margin-bottom: 0px;">
        Example:
        </p>
<pre style="margin-top: 0px;">
    kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'host:/path/database.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>,
        password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>, <strong>dialect</strong><FONT COLOR="#7d0000">=</FONT><FONT COLOR="#666600">1</FONT>)
</pre>
        </li>
    </ul>

  </li>

  <li style="margin-top: 1em;"><code>Connection</code> class
    <ul>
        <li style="margin-bottom: 1em;"><code>charset</code> attribute <em>(read-only)</em><br>
        <p class="textParagraph">
        The character set of the connection (set via the <code>charset</code>
        parameter of <code>kinterbasdb.connect</code>).
        </p>
        <p class="textParagraph">
        See page 221 of the
        <a href="#ref_ib6_docs">Interbase&reg; 6 Data Definition Guide</a>
        for a list of available character sets, and
        <a href="#faq_fep_unicode">this FAQ</a>
        for information on handling extended character sets with KInterbasDB.
        </p>
        </li>

        <li style="margin-bottom: 1em;"><code>dialect</code> attribute<br>
        <p class="textParagraph">
        This integer attribute indicates which SQL dialect the connection
        is using.
        </p>
        <p class="textParagraph">
        You should not change a connection's dialect; instead, discard the
        connection and establish a new one with the desired dialect.
        </p>
        <p class="textParagraph">
        For more information, see the documentation of the
        <code>dialect</code> argument of the
        <code>connect</code> function.
        </p>
        </li>

        <li><code>server_version</code> attribute <em>(read-only)</em><br>
        <p class="textParagraph">
        The version string of the database server
        to which this connection is connected.
        </p>
        <p class="textParagraph">
        For example, a connection to Firebird 1.0 on Windows has the
        following <code>server_version</code>:<br>
        <code>WI-V6.2.794 Firebird 1.0</code>
        </p>
        <br>
        </li>

        <li><code>execute_immediate</code> method<br>
        <p class="textParagraph">
        Executes a statement without caching its prepared form.  The statement
        must <em>not</em> be of a type that returns a result set.
        </p>
        <p class="textParagraph">
        In most cases
        (especially cases in which the same statement--perhaps a parameterized
        statement--is executed repeatedly), it is better to create a cursor
        using the connection's <code>cursor</code> method, then execute the statement
        using one of the cursor's execute methods.
        </p>
        <p style="margin-bottom: 0px;">
        Arguments:
        </p>
        <ul>
            <li>
            <code>sql</code> -
            string containing the SQL statement to execute.<br>
            </li>
        </ul>
        <br>
        </li>

        <li style="margin-bottom: 1em;">
        <a name="db_api_extensions_and_caveats_precision_mode">
            <code>precision_mode</code> attribute
        </a><br>
        <p class="textParagraph">
        Although this attribute is present in KInterbasDB 3.1+ and works
        in a backward-compatible fashion, it is deprecated in favor of
        the more general
        <a href="#adv_param_conv_dynamic_type_translation">dynamic type translation</a>
        feature.
        </p>
        </li>

        <li><code>commit</code> and <code>rollback</code> methods<br>
        <p class="textParagraph">
        The <code>commit</code> and <code>rollback</code> methods
        accept an optional boolean parameter <code>retaining</code>
        (default <code>False</code>) that indicates whether the transactional
        context of the transaction being resolved should be recycled.
        For details, see the
        <a href="#adv_trans_control_retaining">Advanced Transaction Control:  Retaining Operations</a>
        section of this document.
        </p>
        <p class="textParagraph">
        The <code>rollback</code> method accepts an optional string parameter
        <code>savepoint</code> that causes the transaction to roll back only
        as far as the designated savepoint, rather than rolling back entirely.
        For details, see the
        <a href="#adv_trans_control_savepoints">Advanced Transaction Control:  Savepoints</a>
        section of this document.
        </p>
        </li>
    </ul>
  </li>

    <li style="margin-top: 1em;"><code>Cursor</code> class
      <a name="Cursor_description">
        <ul>
          <li><code>description</code> attribute<br>
            <p class="textParagraph">
            KInterbasDB makes <strong>absolutely no guarantees</strong> about
            <code>description</code> <strong>except</strong>
            those required by the Python Database API Specification 2.0 (that
            is, <code>description</code> is
            either <code>None</code> or a sequence of 7-element sequences).
            Therefore, client programmers should <em>not</em> rely on
            <code>description</code> being an instance of a particular class or
            type.
            <br><br>
            </p>

            <p class="textParagraph">
            KInterbasDB provides several named positional constants to be
            used as indices into a given element of <code>description</code> .
            The contents of all <code>description</code> elements are defined by
            the DB API spec; these constants are provided merely for
            convenience.
            </p>

<pre>DESCRIPTION_NAME
DESCRIPTION_TYPE_CODE
DESCRIPTION_DISPLAY_SIZE
DESCRIPTION_INTERNAL_SIZE
DESCRIPTION_PRECISION
DESCRIPTION_SCALE
DESCRIPTION_NULL_OK</pre>

          <p class="textParagraph">
          Here is an example of accessing the <em>name</em> of the first
          field in the <code>description</code> of cursor <code>cur</code>:
          </p>
<pre class="codeBlock" style="margin-top: 0px;">nameOfFirstField <FONT COLOR="#7d0000">=</FONT> cur.description[<FONT COLOR="#666600">0</FONT>][kinterbasdb.DESCRIPTION_NAME]</pre>

          <p class="textParagraph">
          For more information, see the documentation of Cursor.description in
          the <a href="Python-DB-API-2.0.html">DB API Specification</a>.
          </p>
          <br>
          </a>
        </li>

        <li><code>rowcount</code> attribute<br>
        <p class="textParagraph">
        Although KInterbasDB's <code>Cursor</code>s implement this attribute, the database
        engine's own support for the determination of "rows affected"/"rows
        selected" is quirky.
        </p>
        <p class="textParagraph">
        The database engine only supports the determination of rowcount for
        <code>INSERT</code>, <code>UPDATE</code>, <code>DELETE</code>, and
        <code>SELECT</code> statements.
        When stored procedures become involved, row count figures are usually
        not available to the client.
        </p>
        <p class="textParagraph">
        Determining rowcount for <code>SELECT</code> statements is
        problematic:
        the rowcount is reported as zero until at least one row has been
        fetched from the result set,
        and the rowcount is misreported if the result set is larger than
        1302 rows.  The server apparently marshals result sets internally
        in batches of
        1302, and will misreport the rowcount for result sets larger
        than 1302 rows until the 1303rd row is fetched, result sets larger
        than 2604 rows until the 2605th row is fetched, and so on,
        in increments of 1302.
        </p>
        <p class="textParagraph">
        As required by the Python DB API Spec, the rowcount attribute
        "is -1 in case no executeXX() has been performed on the cursor or
        the rowcount of the last operation is not determinable by the
        interface".
        </p>
        <br>
        </li>

                <li><code>fetch*</code> methods<br>
                <p class="textParagraph">
                KInterbasDB makes <strong>absolutely no guarantees</strong>
                about the return value of the
                <code>fetchone</code> / <code>fetchmany</code> / <code>fetchall</code>
                methods <strong>except</strong> that it is a sequence indexed by
                field position.
                </p>
                <p class="textParagraph">
                KInterbasDB makes <strong>absolutely no guarantees</strong>
                about the return value of the
                <code>fetchonemap</code> / <code>fetchmanymap</code> / <code>fetchallmap</code>
                methods (documented below)
                <strong>except</strong> that it is a mapping of field name to field
                value.
                </p>
                <p class="textParagraph">
                Therefore, client programmers should <em>not</em> rely on the return value being
                an instance of a particular class or type.
                </p>
                <br>
                </li>

                <li>
                    <a name="db_api_extensions_and_caveats_cursor_fetchonemap">
                        <code>fetchonemap</code> method<br>
                    </a>
                <p class="textParagraph">
                This method is just like the standard <code>fetchone</code> method
                of the DB API, except that it returns a mapping of field name to
                field value, rather than a sequence.
                <br><br>
                </p>
                </li>

                <li><code>fetchmanymap</code> method<br>
                <p class="textParagraph">
                This method is just like the standard <code>fetchmany</code> method
                of the DB API, except that it returns a sequence of mappings of
                field name to field value, rather than a sequence of sequences.
                <br><br>
                </p>
                </li>

                <li><code>fetchallmap</code> method<br>
                <p class="textParagraph">
                This method is just like the standard <code>fetchall</code> method
                of the DB API, except that it returns a sequence of mappings
                of field name to field value, rather than a sequence of sequences.
                <br><br>
                </p>
                </li>

                <li><code>iter</code>/<code>itermap</code> methods<br>
                <p class="textParagraph">
                These methods are equivalent to the
                <code>fetchall</code> and <code>fetchallmap</code> methods,
                respectively, except that they return iterators rather than
                materialized sequences.
                </p>
                <p class="textParagraph">
                <code>iter</code> and <code>itermap</code> are exercised in
                <a href="#tutorial_executing_example_2">this example</a>.
                </p>
                <br><br>

                </li>

        </ul>
    </li>
</ul>

<hr>

<a name="tutorial"><h2>Tutorial</h2></a>

<p class="textParagraph">
This brief tutorial aims to get the reader started by
demonstrating elementary usage of KInterbasDB.  It
is not a comprehensive Python Database API tutorial, nor is it
comprehensive in its coverage of anything else.
</p>

<p class="textParagraph">
The numerous advanced features of KInterbasDB are covered in
<a href="#adv">another section</a> of this document, which is not in a
tutorial format, though it is replete with examples.
</p>


<a name="tutorial_connect"><h3>Connecting to a Database</h3></a>

<a name="tutorial_connecting_example_1"><h4>Example 1</h4>

<p class="textParagraph">
A database connection is typically established with code such as this:
</p>
<pre class="codeBlock"><FONT COLOR="#0000ff">import</FONT> kinterbasdb

<FONT COLOR="#32a532"># The server is named 'bison'; the database file is at '/temp/test.db'.</FONT>
con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'bison:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>)

<FONT COLOR="#32a532"># Or, equivalently:</FONT>
con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(
    host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'bison'</FONT>, database<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'/temp/test.db'</FONT>,
    user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>
  )
</PRE>
</pre>

<a name="tutorial_connecting_example_2"><h4>Example 2</h4></a>

<p class="textParagraph">
Suppose we want to connect to an Interbase&reg; 5.5 server, specifying UNICODE_FSS
as the character set of the connection:
<pre class="codeBlock"><FONT COLOR="#0000ff">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(
    dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'bison:/temp/test.db'</FONT>,
    user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>,
    dialect<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#666600">1</FONT>, <FONT COLOR="#32a532"># necessary for Interbase&reg; &lt; 6.0</FONT>
    charset<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'UNICODE_FSS'</FONT> <FONT COLOR="#32a532"># specify a character set for the connection</FONT>
  )
</pre>
</p>

<br>
<a name="tutorial_execute_sql"><h3>Executing SQL Statements</h3></a>

<p class="textParagraph">
For this section, suppose we have a table defined and populated by the
following SQL code:
</p>

<pre class="codeBlock">
<FONT COLOR="#1413be">create</FONT> <FONT COLOR="#1413be">table</FONT> languages
(
  name               <FONT COLOR="#1413be">varchar</FONT>(<FONT COLOR="#666600">20</FONT>),
  year_released      <FONT COLOR="#1413be">integer</FONT>
);

<FONT COLOR="#1413be">insert</FONT> <FONT COLOR="#1413be">into</FONT> languages (name, year_released) <FONT COLOR="#1413be">values</FONT> (<FONT COLOR="#8f8c47">'C'</FONT>,        <FONT COLOR="#666600">1972</FONT>);
<FONT COLOR="#1413be">insert</FONT> <FONT COLOR="#1413be">into</FONT> languages (name, year_released) <FONT COLOR="#1413be">values</FONT> (<FONT COLOR="#8f8c47">'Python'</FONT>,   <FONT COLOR="#666600">1991</FONT>);
</pre>

<a name="tutorial_executing_example_1"><h4>Example 1</h4></a>

<p class="textParagraph">
This example shows the <em>simplest</em> way to
print the entire contents of the <code>languages</code> table:
</p>
<pre class="codeBlock">
<FONT COLOR="#1413be">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#65c265"># Create a Cursor object that operates in the context of Connection con:</FONT>
cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

<FONT COLOR="#65c265"># Execute the SELECT statement:</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from languages order by year_released&quot;</FONT>)

<FONT COLOR="#65c265"># Retrieve all rows as a sequence and print that sequence:</FONT>
<FONT COLOR="#1413be">print</FONT> cur.<FONT COLOR="#000066">fetchall</FONT>()
</pre>

<p class="textParagraph">
Sample output:
</p>
<pre class="programOutputBlock">
[('C', 1972), ('Python', 1991)]
</pre>
<br>

<a name="tutorial_executing_example_2"><h4>Example 2</h4></a>

<p class="textParagraph">
Here's another trivial example that demonstrates various ways of fetching a
single row at a time from a <code>SELECT</code>-cursor:
<pre class="codeBlock">
<FONT COLOR="#1413be">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()
SELECT <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#8f8c47">&quot;select name, year_released from languages order by year_released&quot;</FONT>

<FONT COLOR="#65c265"># 1. Iterate over the rows available from the cursor, unpacking the</FONT>
<FONT COLOR="#65c265"># resulting sequences to yield their elements (name, year_released):</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(SELECT)
<FONT COLOR="#1413be">for</FONT> (name, year_released) <FONT COLOR="#1413be">in</FONT> cur:
    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'%s has been publicly available since %d.'</FONT> <FONT COLOR="#7d0000">%</FONT> (name, year_released)

<FONT COLOR="#65c265"># 2. Equivalently:</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(SELECT)
<FONT COLOR="#1413be">for</FONT> row <FONT COLOR="#1413be">in</FONT> cur:
    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'%s has been publicly available since %d.'</FONT> <FONT COLOR="#7d0000">%</FONT> (row[<FONT COLOR="#666600">0</FONT>], row[<FONT COLOR="#666600">1</FONT>])

<FONT COLOR="#65c265"># 3. Using mapping-iteration rather than sequence-iteration:</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(SELECT)
<FONT COLOR="#1413be">for</FONT> row <FONT COLOR="#1413be">in</FONT> cur.<FONT COLOR="#000066">itermap</FONT>():
    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'%(name)s has been publicly available since %(year_released)d.'</FONT> <FONT COLOR="#7d0000">%</FONT> row
</pre>
</p>

<p class="textParagraph">
Sample output:
</p>
<pre class="programOutputBlock">
C has been publicly available since 1972.
Python has been publicly available since 1991.
C has been publicly available since 1972.
Python has been publicly available since 1991.
C has been publicly available since 1972.
Python has been publicly available since 1991.
</pre>
<br>

<a name="tutorial_executing_example_3"><h4>Example 3</h4></a>

<p class="textParagraph">
The following program is a simplistic table printer
(applied in this example to <code>languages</code>):
</p>
<pre class="codeBlock">
<FONT COLOR="#1413be">import</FONT> kinterbasdb <FONT COLOR="#1413be">as</FONT> k

TABLE_NAME <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#8f8c47">'languages'</FONT>
SELECT <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#8f8c47">'select * from %s order by year_released'</FONT> <FONT COLOR="#7d0000">%</FONT> TABLE_NAME

con <FONT COLOR="#7d0000">=</FONT> k.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()
cur.<FONT COLOR="#000066">execute</FONT>(SELECT)

<FONT COLOR="#65c265"># Print a header.</FONT>
<FONT COLOR="#1413be">for</FONT> fieldDesc <FONT COLOR="#1413be">in</FONT> cur.description:
    <FONT COLOR="#1413be">print</FONT> fieldDesc[k.DESCRIPTION_NAME].<FONT COLOR="#000066">ljust</FONT>(fieldDesc[k.DESCRIPTION_DISPLAY_SIZE]) ,
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#65c265"># Finish the header with a newline.</FONT>
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'-'</FONT> <FONT COLOR="#7d0000">*</FONT> <FONT COLOR="#666600">78</FONT>

<FONT COLOR="#65c265"># For each row, print the value of each field left-justified within</FONT>
<FONT COLOR="#65c265"># the maximum possible width of that field.</FONT>
fieldIndices <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#7d7ca4">range</FONT>(<FONT COLOR="#7d7ca4">len</FONT>(cur.description))
<FONT COLOR="#1413be">for</FONT> row <FONT COLOR="#1413be">in</FONT> cur:
    <FONT COLOR="#1413be">for</FONT> fieldIndex <FONT COLOR="#1413be">in</FONT> fieldIndices:
        fieldValue <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#7d7ca4">str</FONT>(row[fieldIndex])
        fieldMaxWidth <FONT COLOR="#7d0000">=</FONT> cur.description[fieldIndex][k.DESCRIPTION_DISPLAY_SIZE]

        <FONT COLOR="#1413be">print</FONT> fieldValue.<FONT COLOR="#000066">ljust</FONT>(fieldMaxWidth) ,

    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#65c265"># Finish the row with a newline.</FONT>
</pre>

<p class="textParagraph">
Sample output:
</p>
<pre class="programOutputBlock">
NAME                 YEAR_RELEASED
------------------------------------------------------------------------------
C                    1972
Python               1991
</pre>
<br>

<a name="tutorial_executing_example_4"><h4>Example 4</h4></a>

<p class="textParagraph">
Let's insert more languages:
</p>
<pre class="codeBlock">
<FONT COLOR="#1413be">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

newLanguages <FONT COLOR="#7d0000">=</FONT> [
    (<FONT COLOR="#8f8c47">'Lisp'</FONT>,  <FONT COLOR="#666600">1958</FONT>),
    (<FONT COLOR="#8f8c47">'Dylan'</FONT>, <FONT COLOR="#666600">1995</FONT>),
  ]

cur.<FONT COLOR="#000066">executemany</FONT>(<FONT COLOR="#8f8c47">&quot;insert into languages (name, year_released) values (?, ?)&quot;</FONT>,
    newLanguages
  )

<FONT COLOR="#65c265"># The changes will not be saved unless the transaction is committed explicitly:</FONT>
con.<FONT COLOR="#000066">commit</FONT>()
</pre>

<p class="textParagraph">
Note the use of a <em>parameterized</em> SQL statement above.  When dealing
with repetitive statements, this is <strong>much faster and less error-prone</strong>
than assembling each SQL statement manually.
(You can read more about parameterized SQL statements in the section on
 <a href="#adv_prepared_statements">Prepared Statements</a>.)
</p>

<p class="textParagraph" style="margin-top: 30px;">
After running Example 4, the table printer from Example 3
would print:
</p>
<pre class="programOutputBlock">
NAME                 YEAR_RELEASED
------------------------------------------------------------------------------
Lisp                 1958
C                    1972
Python               1991
Dylan                1995
</pre>


<br>
<a name="tutorial_stored_procedures"><h3>Calling Stored Procedures</h3></a>

<p class="textParagraph">
Interbase&reg; and Firebird support stored procedures written in a proprietary
procedural SQL language.
IB/FB stored procedures can have <em>input</em> parameters and/or
<em>output</em> parameters.  Some databases support <em>input/output</em>
parameters, where the same parameter is used for both input and output; IB/FB
does not support this.
</p>

<p class="textParagraph">
It is important to distinguish between procedures that <em>return a result set</em>
and procedures that <em>populate and return their output parameters exactly once</em>.
Conceptually, the latter "return their output parameters" like a Python
function, whereas the former "yield result rows" like a Python generator.
</p>

<p class="textParagraph">
IB/FB's <em>server-side</em> procedural SQL syntax
makes no such distinction, but <em>client-side</em> SQL code (and C API code)
must.
A result set is retrieved from a stored procedure by
<code>SELECT</code>ing from the procedure, whereas output
parameters are retrieved with an <code>EXECUTE PROCEDURE</code>
statement.
<br><br>
</p>

<p class="textParagraphNoBottom">
To <em>retrieve a result set</em> from a stored procedure with KInterbasDB,
use code such as this:
</p>

<pre class="codeBlockNoTop">
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select output1, output2 from the_proc(?, ?)&quot;</FONT>, (input1, input2))

<FONT COLOR="#32a532"># Ordinary fetch code here, such as:</FONT>
<FONT COLOR="#0000ff">for</FONT> row <FONT COLOR="#0000ff">in</FONT> cur:
    ... <FONT COLOR="#32a532"># process row</FONT>

con.<FONT COLOR="#000066">commit</FONT>() <FONT COLOR="#32a532"># If the procedure had any side effects, commit them.</FONT>
</pre>


<p class="textParagraphNoBottom">
To <em>execute</em> a stored procedure and <em>access its output parameters</em>,
use code such as this:
</p>

<pre class="codeBlockNoTop">
cur.<FONT COLOR="#000066">callproc</FONT>(<FONT COLOR="#8f8c47">&quot;the_proc&quot;</FONT>, (input1, input2))

<FONT COLOR="#32a532"># If there are output parameters, retrieve them as though they were the</FONT>
<FONT COLOR="#32a532"># first row of a result set.  For example:</FONT>
outputParams <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">fetchone</FONT>()

con.<FONT COLOR="#000066">commit</FONT>() <FONT COLOR="#32a532"># If the procedure had any side effects, commit them.</FONT>
</pre>

<p class="textParagraph">
This latter is not very elegant; it would be preferable to access the
procedure's output parameters as the return value of
<code>Cursor.callproc</code>.  The Python DB API specification requires the
current behavior, however.
</p>

<br><br>

<hr>

<br>

<a name="adv">
  <h2 style="margin-bottom: 25px;">Native Database Engine Features and Extensions Beyond the Python DB API</h2>
</a>

<hr>

<a name="adv_db_creation_deletion">
  <h2>Programmatic Database Creation and Deletion</h2>
</a>

<p class="textParagraph">
The Firebird engine stores a database in a fairly straightforward manner:
as a single file or, if desired, as a segmented group of files.
</p>

<p class="textParagraph">
The engine supports dynamic database creation via the SQL statement
<code>CREATE DATABASE</code>, which is documented on page 49 of the
<a href="#ref_ib6_docs">Interbase&reg; 6 Language Reference</a>.
</p>

<p class="textParagraph">
The engine also supports dropping (deleting) databases dynamically, but
dropping is a more complicated operation than creating, for several reasons:
an existing database may be in use by users other than the one who requests the
deletion, it may have supporting objects such as temporary sort files, and it may
even have dependent shadow databases.  Although the database engine recognizes a
<code>DROP DATABASE</code> SQL statement, support for that statement is
limited to the <code>isql</code> command-line administration utility.  However,
the engine supports the deletion of databases via an API call, which
KInterbasDB exposes to Python (see below).
</p>

<p class="textParagraph">
KInterbasDB supports dynamic database creation and deletion via the
module-level function <code>create_database</code> and the method
<code>Connection.drop_database</code>.  These are documented below, then
demonstrated by a brief example.
</p>

<table border="1" class="memberDocFrame">
  <tr>
    <td class="memberDocHeader">
        <code class="memberDocName">create_database</code>&nbsp;
        <span class="memberDocNameCaption">(function; member of <code>kinterbasdb</code>)</span>
    </td>
  </tr>
  <tr>
    <td>
        <p>
        Creates a database according to the supplied <code>CREATE DATABASE</code>
        SQL statement.  Returns an open connection to the newly created database.
        </p>

        <p class="argHeader">Arguments:</p>
        <ul class="argList">
        <li>
            <code>sql</code> -
            string containing the <code>CREATE DATABASE</code> statement.<br>
            <p class="textParagraph">
            Note that this statement may need to include a username and password
            (see the <a href="#ref_ib6_docs">IB 6 Language Reference</a> for syntax).
            </p>
        </li>
        <li>
            <code>dialect</code> <span class="memberDocArgOptTag">(optional)</span> -
            the SQL dialect under which to execute the statement
            (defaults to <code>3</code>).
        </li>
        </ul>
    </td>
  </tr>
</table>

<table border="1" class="memberDocFrame">
  <tr>
    <td class="memberDocHeader">
        <code class="memberDocName">drop_database</code>&nbsp;
        <span class="memberDocNameCaption">(method; member of <code>kinterbasdb.Connection</code>)</span>
    </td>
  </tr>
  <tr>
    <td>
        <p>
        Deletes the database to which the connection is attached.
        </p>
        <p>
        This method performs the database deletion in a responsible
        fashion.  Specifically, it:
        <ul class="argList">
            <li>raises an <code>OperationalError</code> instead of deleting
                    the database if there are other active connections to the
                    database</li>
            <li>deletes supporting files and logs in addition to the
                    primary database file(s)</li>
        </ul>
        </p>
        <p>
        This method has no arguments.
        </p>
    </td>
  </tr>
</table>

<p class="textParagraphNoBottom" style="margin-top: 1.5em;">
Example program:
</p>
<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">create_database</FONT>(
    <FONT COLOR="#8f8c47">&quot;create database '/temp/db.db' user 'sysdba' password 'pass'&quot;</FONT>
  )
con.<FONT COLOR="#000066">drop_database</FONT>()
</pre>


<hr>

<a name="adv_event"><h2>Database Event Notification</h2></a>
<ul style="margin-bottom: 2em;">
  <li><a href="#adv_event_what">What are database events?</a></li>
  <li><a href="#adv_event_why">Why use database events?</a></li>
  <li><a href="#adv_event_native_exposure">
        How does the database engine expose events to SQL (in the server process)
        and C (in the client process)?
    </a>
  </li>
  <li><a href="#adv_event_python_exposure">
        How does KInterbasDB expose database events to the Python programmer?
    </a>
  </li>
  <li><a href="#adv_event_example">Example Program</a></li>
  <li><a href="#adv_event_limitations">Pitfalls and Limitations</a></li>
</ul>


<a name="adv_event_what"><h4>What are database events?</h4></a>

<p class="textParagraph">
The database engine features a distributed, interprocess communication
mechanism based on messages called <em>database events</em>.
Chapter 11 of the
<a href="#ref_ib6_docs">Interbase&reg; 6 API Guide</a>
describes database events this way:
</p>
<blockquote>
[A database event is] a message passed from a trigger or stored procedure to an
application to
announce the occurrence of a specified condition or action, usually a database
change such as an insertion, modification, or deletion of a record.
</blockquote>

<blockquote>
The Interbase&reg; [and Firebird] event mechanism enables applications to respond
to actions and database changes made by other, concurrently running
applications without the need for those applications to communicate directly
with one another, and without incurring the expense of CPU time required for
periodic polling to determine if an event has occurred.
</blockquote>

<a name="adv_event_why"><h4>Why use database events?</h4></a>

<p class="textParagraph">
Anything that can be accomplished with database events can also be
implemented using other techniques, so why bother with events?
Since you've chosen to write database-centric programs in Python rather
than assembly language, you probably already know the answer to this
question, but let's illustrate.
</p>

<p class="textParagraph">
A typical application for database events is the handling of administrative
messages.  Suppose you have an administrative message database with a
<code>messages</code> table, into which various applications insert timestamped
status reports.  It may be desirable to react to these messages in diverse ways,
depending on the status they indicate:
to ignore them,
to initiate the update of dependent databases upon their arrival,
to forward them by e-mail to a remote administrator,
or even to set off an alarm so that on-site administrators will know
a problem has occurred.
</p>

<p class="textParagraph">
It is undesirable to tightly couple the program whose status is being reported
(the <em>message producer</em>) to the program that handles the status reports
(the <em>message handler</em>).
There are obvious losses of flexibility in doing so.
For example, the message producer may run on a separate machine from the
administrative message database and may lack access rights to the downstream
reporting facilities (e.g., network access to the SMTP server, in the case of
forwarded e-mail notifications).  Additionally, the actions required to
handle status reports may themselves be time-consuming and
error-prone, as in accessing a remote network to transmit e-mail.
</p>

<p class="textParagraph">
In the absence of database event support, the message handler would probably
be implemented via <em>polling</em>.  Polling is simply the repetition of
a check for a condition at a specified interval.
In this case, the message handler would check in an infinite loop to see
whether the most recent record in the <code>messages</code> table was more
recent than the last message it had handled.  If so, it would handle the
fresh message(s); if not, it would go to sleep for a specified interval,
then loop.
</p>

<p class="textParagraph">
The <em>polling-based</em> implementation of the message handler is
fundamentally flawed.  Polling is a form of
<a href="http://www.catb.org/jargon/html/B/busy-wait.html">busy-wait</a>;
the check for new messages is performed at the specified interval, regardless
of the actual activity level of the message producers.

If the polling interval is lengthy,
messages might not be handled within a reasonable time period after their arrival;
if the polling interval is brief, the message handler program (and there may
be many such programs) will waste a large amount of CPU time on unnecessary
checks.
</p>

<p class="textParagraph">
The database server is necessarily aware of the exact moment
when a new message arrives.  Why not let the message handler program request
that the database server send it a notification when a new message arrives?
The message handler can then efficiently sleep until the moment its services
are needed.  Under this <em>event-based</em>
scheme, the message handler becomes aware of new messages at the instant
they arrive, yet it does not waste CPU time checking in vain for new messages
when there are none available.
</p>

<a name="adv_event_native_exposure">
  <h4>How does the database engine expose events to SQL (in the server process)
    and C (in the client process)?
  </h4>
</a>

<p class="textParagraph">

<ol>

<li style="margin-bottom: 1.5em;">Server Process
  <span style="font-size: 85%;">("An event just occurred!")</span>

<p class="textParagraph">
Recall from Chapter 11 of the
<a href="#ref_ib6_docs">Interbase&reg; 6 API Guide</a>
that
</p>
<blockquote style="margin-top: 0px;">
[A database event is] a message passed from a trigger or stored procedure
to an application to announce the occurrence of a specified condition or
action, usually a database change such as an insertion, modification, or
deletion of a record.
</blockquote>

<p class="textParagraph">
To notify any interested listeners that a specific event has occurred,
issue the
<code>POST_EVENT</code> statement
(see page 176 of the <a href="#ref_ib6_docs">Interbase&reg; 6 Language Reference</a>).
The <code>POST_EVENT</code> statement has one parameter:  the name of the
event to post.
</p>

<p class="textParagraph">
In the preceding example of the administrative message database,
<code>POST_EVENT</code> might be used from an <code>after insert</code>
trigger on the <code>messages</code> table, like this:
</p>
<pre class="codeBlock" style="margin-top: 0px;">create trigger trig_messages_handle_insert
  for messages
    after insert
as
begin
  <span style="color: blue;">POST_EVENT</span> 'new_message';
end
</pre>

<p class="textParagraph">
Note that the physical notification of the client process does not occur until the
transaction in which the <code>POST_EVENT</code> took place is actually
committed.  Therefore, multiple events may <em>conceptually</em> occur
before the client process is <em>physically</em> informed of even one
occurrence.
</p>

<p class="textParagraph">Furthermore, the database engine makes no
guarantee that clients will be informed of events in the same groupings
in which they conceptually occurred.  If, within a single transaction, an event
named <code>event_a</code> is posted once and an event named
<code>event_b</code> is posted once, the client may receive those posts
in separate "batches", despite the fact that they occurred in the same
conceptual unit (a single transaction).  This also applies to multiple
occurrences of <em>the same</em> event within a single conceptual unit:  the
physical notifications may arrive at the client separately.
</p>

</li>

<li>Client Process <span style="font-size: 85%;">("Send me a message when an event occurs.")</span><br>

<p class="textParagraph" style="margin-bottom: 1.5em;">
<strong>Note:</strong>
If you don't care about the gory details of event notification,
skip to the section that describes
<a href="#adv_event_python_exposure">KInterbasDB's Python-level event handling API</a>.
</p>

<p class="textParagraph">
The Interbase&reg;/Firebird C client library offers two forms of event notification.
</p>

<p class="textParagraph">
The first form is <em>synchronous</em> notification, by way of the function
<code>isc_wait_for_event</code>.  This form is admirably simple for a C
programmer to use, but is inappropriate as a basis for KInterbasDB's
event support, chiefly because it's not sophisticated enough to serve as the
basis for a comfortable Python-level API.
</p>

<p class="textParagraph">
The other form of event notification offered by the database client library
is <em>asynchronous</em>, by way of the functions
<code>isc_que_events</code> (note that the name of that function is
misspelled), <code>isc_cancel_events</code>, and others.
</p>

<p class="textParagraph">
The details are as nasty as they are numerous, but the essence of using
asynchronous notification from C is as follows:
</p>

<ol style="margin-left: 3em;">
  <li>Call <code>isc_event_block</code> to create a formatted binary buffer
    that will tell the server which events the client wants to listen for.
  </li>
  <li>Call <code>isc_que_events</code> (passing the buffer created in the
    previous step) to inform the server that the client is ready to receive
    event notifications, and provide a callback that will be asynchronously
    invoked when one or more of the registered events occurs.
  </li>
  <li>[The thread that called <code>isc_que_events</code> to initiate event
    listening must now do something else.]
  </li>
  <li>When the callback is invoked (the database client library starts a thread
    dedicated to this purpose), it can use the <code>isc_event_counts</code>
    function to determine how many times each of the registered events has
    occurred since the last call to <code>isc_event_counts</code> (if any).
  </li>
  <li>[The callback thread should now "do its thing", which may include
    communicating with the thread that called <code>isc_que_events</code>.]
  </li>
  <li>When the callback thread is finished handling an event notification, it
    must call <code>isc_que_events</code> again in order to receive future
    notifications.  Future notifications will invoke the callback again,
    effectively "looping" the callback thread back to Step 4.
  </li>
</ol>

</li>

</ol>


<a name="adv_event_python_exposure">
<h4>How does KInterbasDB expose database events to the Python programmer?</h4>
</a>

<p class="textParagraph">
The KInterbasDB database event API is comprised of the following:
the method <code>Connection.event_conduit</code> and the class
<code>EventConduit</code>.
</p>

<table border="1" class="memberDocFrame">
  <tr>
    <td class="memberDocHeader">
        <code class="memberDocName">event_conduit</code>&nbsp;
        <span class="memberDocNameCaption">(method; member of <code>kinterbasdb.Connection</code>)</span>
    </td>
  </tr>
  <tr>
    <td>
        <p>
        Creates a conduit (an instance of <code>EventConduit</code>) through
        which database event notifications will flow into the Python program.
        </p>

        <p>
        <code>event_conduit</code> is a method of <code>Connection</code>
        rather than a module-level function or a class constructor because
        the database engine deals with events in the context of a particular
        database (after all, <code>POST_EVENT</code> must be issued by a
        stored procedure or a trigger).
        </p>

        <p class="argHeader">Arguments:</p>
        <ul class="argList">
        <li>
            <code>event_names</code> -
            a sequence of string event names<br>
            <p class="textParagraph">
            The <code>EventConduit.wait</code> method will block until the
            occurrence of at least one of the events named by the strings in
            <code>event_names</code>.
            </p>
            <p class="textParagraph">
            KInterbasDB's own event-related code is capable of operating with
            up to 2147483647 events per conduit.
            <span style="color: red; font-weight: bold;">
            However, it has been observed that the Firebird client library
            experiences catastrophic problems (including memory corruption)
            on some platforms with anything beyond about 100 events per
            conduit.
            These limitations are dependent on both the Firebird version and
            the platform.
            </span>
            </p>
        </li>
        </ul>
    </td>
  </tr>
</table>


<p class="compactParagraph" style="font-size: 100%;">
<code>EventConduit:</code>
</p>

<!-- kinterbasdb.EventConduit.__init__ -->
<table border="1" class="memberDocFrame">
  <tr>
    <td class="memberDocHeader">
        <code class="memberDocName">__init__</code>&nbsp;
        <span class="memberDocNameCaption">(method; member of <code>kinterbasdb.EventConduit</code>)</span>
    </td>
  </tr>
  <tr>
    <td>
        <p>
        The <code>EventConduit</code> class is not designed to be instantiated
        directly by the Python programmer.  Instead, use the
        <code>Connection.event_conduit</code> method to create
        <code>EventConduit</code> instances.
        </p>
    </td>
  </tr>
</table>

<!-- kinterbasdb.EventConduit.wait -->
<table border="1" class="memberDocFrame">
  <tr>
    <td class="memberDocHeader">
        <code class="memberDocName">wait</code>&nbsp;
        <span class="memberDocNameCaption">(method; member of <code>kinterbasdb.EventConduit</code>)</span>
    </td>
  </tr>
  <tr>
    <td>
        <p class="compactParagraph">
        Blocks the calling thread until at least one of the events occurs,
        or the specified <code>timeout</code> (if any) expires.
        </p>

        <p class="compactParagraph">
        If one or more event notifications has arrived since the last call
        to <code>wait</code>, this method will retrieve a notification from
        the head of the <code>EventConduit</code>'s internal queue and
        return immediately.
        </p>

        <p class="compactParagraph" style="margin-bottom: 0px;">
        The names of the relevant events were supplied to the
        <code>Connection.event_conduit</code> method during the creation
        of this <code>EventConduit</code>.
        In the code snippet below, the relevant events are named
        <code>event_a</code> and <code>event_b</code>:
        </p>
<pre class="codeBlockInMemberDoc">conduit <FONT COLOR="#7d0000">=</FONT> connection.<FONT COLOR="#000066">event_conduit</FONT>( (<FONT COLOR="#8f8c47">'event_a'</FONT>, <FONT COLOR="#8f8c47">'event_b'</FONT>) )
conduit.<FONT COLOR="#000066">wait</FONT>()
</pre>

        <p class="argHeader">Arguments:</p>
        <ul class="argList">
        <li><code>timeout</code> <em>(optional)</em> -
            number of seconds (use a <code>float</code> to indicate fractions of seconds)<br>
            <p class="textParagraph">
            If not even one of the relevant events has occurred after
            <code>timeout</code> seconds, this method will unblock and return
            <code>None</code>.
            The default <code>timeout</code> is infinite.
            </p>
        </li>
        </ul>

        <p class="retHeader">Returns:</p>
        <p class="retDesc">
        <code>None</code> if the wait timed out, otherwise a dictionary
        that maps <code>event_name -> event_occurrence_count</code>.
        </p>

        <p class="compactParagraph" style="margin-bottom: 0px;">
        In the code snippet above, if <code>event_a</code> occurred once and
        <code>event_b</code> did not occur at all, the return value from
        <code>conduit.wait()</code> would be the following dictionary:
        </p>
<pre class="codeBlockInMemberDoc">{
  <FONT COLOR="#8f8c47">'event_a'</FONT>: <FONT COLOR="#666600">1</FONT>,
  <FONT COLOR="#8f8c47">'event_b'</FONT>: <FONT COLOR="#666600">0</FONT>
}
</pre>
    </td>
  </tr>
</table>

<!-- kinterbasdb.EventConduit.close -->
<table border="1" class="memberDocFrame">
  <tr>
    <td class="memberDocHeader">
        <code class="memberDocName">close</code>&nbsp;
        <span class="memberDocNameCaption">(method; member of <code>kinterbasdb.EventConduit</code>)</span>
    </td>
  </tr>
  <tr>
    <td>
        <p class="compactParagraph">
        Cancels the standing request for this conduit to be notified of events.
        </p>
        <p class="compactParagraph">
        After this method has been called, this <code>EventConduit</code>
        object is useless, and should be discarded.
        (The boolean property <code>closed</code> is <code>True</code> after
        an <code>EventConduit</code> has been closed.)
        </p>
        <p class="compactParagraph">
        This method has no arguments.
        </p>
    </td>
  </tr>
</table>

<!-- kinterbasdb.EventConduit.flush -->
<table border="1" class="memberDocFrame">
  <tr>
    <td class="memberDocHeader">
        <code class="memberDocName">flush</code>&nbsp;
        <span class="memberDocNameCaption">(method; member of <code>kinterbasdb.EventConduit</code>)</span>
    </td>
  </tr>
  <tr>
    <td>
        <p class="compactParagraph">
        This method allows the Python programmer to manually clear any
        event notifications that have accumulated in the conduit's internal
        queue.
        </p>

        <p class="compactParagraph">
        From the moment the conduit is created by the
        <code>Connection.event_conduit</code> method, notifications of any
        events that occur will accumulate asynchronously within the conduit's
        internal queue until the conduit is closed either
        explicitly (via the <code>close</code> method) or
        implicitly (via garbage collection).  There are two ways
        to dispose of the accumulated notifications:  call <code>wait</code>
        to receive them one at a time (<code>wait</code> will block when the
        conduit's internal queue is empty), or call this method to get rid of
        all accumulated notifications.
        </p>

        <p class="compactParagraph">
        This method has no arguments.
        </p>

        <p class="retHeader">Returns:</p>
        <p class="retDesc">
        The number of event notifications that were flushed from the queue.
        The "number of event <em>notifications</em>" is not necessarily the
        same as the "number of event <em>occurrences</em>", since a single
        notification can indicate multiple occurrences of a given event
        (see the return value of the <code>wait</code> method).
        </p>
    </td>
  </tr>
</table>


<br><br>


<a name="adv_event_example"><h4>Example Program</h4></a>

<p class="textParagraph">
The following code (a SQL table definition, a SQL trigger definition, and
two Python programs) demonstrates KInterbasDB-based event notification.
</p>

<p class="textParagraph">
The example is based on a database at
<code>'localhost:/temp/test.db'</code>, which contains
a simple table named <code>test_table</code>.&nbsp;
<code>test_table</code> has an <code>after insert</code>
trigger that posts several events.
Note that the trigger posts <code>test_event_a</code> twice,
<code>test_event_b</code> once, and <code>test_event_c</code> once.
</p>

<p class="textParagraph">
The Python event <em>handler</em> program connects to the database and
establishes an <code>EventConduit</code> in the context of that connection.
As specified by the list of <code>RELEVANT_EVENTS</code> passed to
<code>event_conduit</code>, the event conduit
will concern itself only with events named <code>test_event_a</code>
and <code>test_event_b</code>.
Next, the program calls the conduit's <code>wait</code> method
without a timeout; it will wait infinitely until <em>at least one</em>
of the relevant events is posted in a transaction that is
subsequently committed.
</p>

<p class="textParagraph">
The Python event <em>producer</em> program simply connects to the database,
inserts a row into <code>test_table</code>, and commits the transaction.
Notice that except for the printed comment, no code in the producer
makes any mention of events--the events are posted as an implicit
consequence of the row's insertion into <code>test_table</code>.
</p>

<p class="textParagraph">
The insertion into <code>test_table</code> causes the trigger
to <em>conceptually</em> post events, but those events are not
<em>physically</em> sent to interested listeners until the transaction
is committed.
When the commit occurs, the handler program returns from the <code>wait</code>
call and prints the notification that it received.
</p>


<p style="margin-bottom: 0px;">
SQL table definition:
</p>
<pre class="codeBlock" style="margin-top: 0px;">
<FONT COLOR="#1413be">create</FONT> <FONT COLOR="#1413be">table</FONT> test_table (a <FONT COLOR="#1413be">integer</FONT>)
</pre>

<p style="margin-bottom: 0px;">
SQL trigger definition:
</p>
<pre class="codeBlock" style="margin-top: 0px;">
<FONT COLOR="#1413be">create</FONT> <FONT COLOR="#1413be">trigger</FONT> trig_test_insert_event
  <FONT COLOR="#1413be">for</FONT> test_table
    <FONT COLOR="#1413be">after insert</FONT>
<FONT COLOR="#1413be">as</FONT>
<FONT COLOR="#1413be">begin</FONT>
  post_event <FONT COLOR="#8f8c47">'test_event_a'</FONT>;
  post_event <FONT COLOR="#8f8c47">'test_event_b'</FONT>;
  post_event <FONT COLOR="#8f8c47">'test_event_c'</FONT>;

  post_event <FONT COLOR="#8f8c47">'test_event_a'</FONT>;
<FONT COLOR="#1413be">end</FONT>
</pre>


<p style="margin-bottom: 0px;">
Python event <em>handler</em> program:
</p>
<pre class="codeBlock" style="margin-top: 0px;">
<FONT COLOR="#0000ff">import</FONT> kinterbasdb

RELEVANT_EVENTS <FONT COLOR="#7d0000">=</FONT> [<FONT COLOR="#8f8c47">'test_event_a'</FONT>, <FONT COLOR="#8f8c47">'test_event_b'</FONT>]

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>)
conduit <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">event_conduit</FONT>(RELEVANT_EVENTS)

<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'HANDLER: About to wait for the occurrence of one of %s...\n'</FONT> <FONT COLOR="#7d0000">%</FONT> RELEVANT_EVENTS
result <FONT COLOR="#7d0000">=</FONT> conduit.<FONT COLOR="#000066">wait</FONT>()
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'HANDLER: An event notification has arrived:'</FONT>
<FONT COLOR="#0000ff">print</FONT> result
conduit.<FONT COLOR="#000066">close</FONT>()
</pre>


<p style="margin-bottom: 0px;">
Python event <em>producer</em> program:
</p>
<pre class="codeBlock" style="margin-top: 0px;">
<FONT COLOR="#0000ff">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>)
cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into test_table values (1)&quot;</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'PRODUCER: Committing transaction that will cause event notification to be sent.'</FONT>
con.<FONT COLOR="#000066">commit</FONT>()
</pre>


<p style="margin-bottom: 0px;">
Event producer output:
</p>
<pre class="programOutputBlock" style="margin-top: 0px;">
PRODUCER: Committing transaction that will cause event notification to be sent.
</pre>

<p style="margin-bottom: 0px;">
Event handler output (assuming that the handler was already started and waiting
when the event producer program was executed):
</p>
<pre class="programOutputBlock" style="margin-top: 0px;">
HANDLER: About to wait for the occurrence of one of ['test_event_a', 'test_event_b']...

HANDLER: An event notification has arrived:
{'test_event_a': 2, 'test_event_b': 1}
</pre>

<p class="textParagraph">
Notice that there is no mention of <code>test_event_c</code> in the result
dictionary received by the event handler program.
Although <code>test_event_c</code> was posted by the <code>after insert</code>
trigger, the event conduit in the handler program was created to
listen only for <code>test_event_a</code> and
<code>test_event_b</code> events.
</p>



<br>

<a name="adv_event_limitations">
  <h4>Pitfalls and Limitations</h4>
</a>

<ul>
  <li>
    <p>
    Remember that if an <code>EventConduit</code> is left active (not yet
    <code>close</code>d or garbage collected), notifications for any
    registered events that actually occur
    will continue to accumulate in the <code>EventConduit</code>'s
    internal queue even if the Python programmer doesn't call
    <code>EventConduit.wait</code> to receive the notifications or
    <code>EventConduit.flush</code> to clear the queue.
    The ill-informed may misinterpret this behavior as a memory leak in
    KInterbasDB; it is not.
    </p>
  </li>
  <li>
    <span style="color: red; font-weight: bold;">
        NEVER use LOCAL-protocol connections in a multithreaded program that
        also uses event handling!
    </span><br>
    <p class="textParagraph">
    The database client library implements the local protocol on some
    platforms in such a way that deadlocks may arise in bizarre places if
    you do this.
    <em>This no-LOCAL prohibition is not limited to connections that are
    used as the basis for event conduits; it applies to all connections
    throughout the process.</em>
    </p>

    <p class="textParagraph">
    So why doesn't KInterbasDB protect the Python programmer from this
    mistake?  Because the event handling thread is started by the
    database client library, and it operates beyond the synchronization domain
    of KInterbasDB at times.
    </p>
  </li>
</ul>

<p class="textParagraph" style="margin-top: 1.5em;">
<em>Note:</em>
The restrictions on the number of active <code>EventConduit</code>s in a
    process, and on the number of event names that a single
    <code>EventConduit</code> can listen for, have been removed in
    KInterbasDB 3.2.
</p>

<br>
<hr>
<br>

<a name="adv_ct"><h2>Connection Timeouts</h2></a>

<p class="textParagraph" style="margin-bottom: 0.1em;">
Connection timeouts allow the programmer to request that a connection be
automatically closed after a specified period of inactivity.
The simplest uses of connection timeouts are trivial, as demonstrated by the
following snippet:
</p>
<pre class="codeBlock" style="margin-top: 0em;">
<FONT COLOR="#1413be">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>r<FONT COLOR="#8f8c47">'localhost:D:\temp\test.db'</FONT>,
    user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>,
    timeout<FONT COLOR="#7d0000">=</FONT>{<FONT COLOR="#8f8c47">'period'</FONT>: <FONT COLOR="#666600">120</FONT>.<FONT COLOR="#666600">0</FONT>} <FONT COLOR="#65c265"># time out after 120.0 seconds of inactivity</FONT>
  )

...
</pre>

<p class="textParagraph">
The connection created in the example above is <em>eligible</em> to be
automatically closed by KInterbasDB if it remains idle for at least 120.0
consecutive seconds.
KInterbasDB does not guarantee that the connection will be closed
immediately when the specified period has elapsed.  On a busy system, there
might be a considerable delay between the moment a connection becomes eligible
for timeout and the moment KInterbasDB actually closes it.
However, the thread that performs connection timeouts is programmed in such a
way that on a lightly loaded system, it acts almost instantaneously to take
advantage of a connection's eligibility for timeout.
</p>

<p class="textParagraph" style="margin-bottom: 0.1em;">
After a connection has timed out, KInterbasDB reacts to attempts to reactivate
the severed connection in a manner dependent on the state of the connection
when it timed out.  Consider the following example program:
</p>

<pre class="codeBlock" style="margin-top: 0em;">
<FONT COLOR="#1413be">import</FONT> time
<FONT COLOR="#1413be">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>r<FONT COLOR="#8f8c47">'localhost:D:\temp\test.db'</FONT>,
    user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>,
    timeout<FONT COLOR="#7d0000">=</FONT>{<FONT COLOR="#8f8c47">'period'</FONT>: <FONT COLOR="#666600">3</FONT>.<FONT COLOR="#666600">0</FONT>}
  )
cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;recreate table test (a int, b char(1))&quot;</FONT>)
con.<FONT COLOR="#000066">commit</FONT>()

cur.<FONT COLOR="#000066">executemany</FONT>(<FONT COLOR="#8f8c47">&quot;insert into test (a, b) values (?, ?)&quot;</FONT>,
    [(<FONT COLOR="#666600">1</FONT>, <FONT COLOR="#8f8c47">'A'</FONT>), (<FONT COLOR="#666600">2</FONT>, <FONT COLOR="#8f8c47">'B'</FONT>), (<FONT COLOR="#666600">3</FONT>, <FONT COLOR="#8f8c47">'C'</FONT>)]
  )
con.<FONT COLOR="#000066">commit</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test&quot;</FONT>)
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'BEFORE:'</FONT>, cur.<FONT COLOR="#000066">fetchall</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;update test set b = 'X' where a = 2&quot;</FONT>)

time.<FONT COLOR="#000066">sleep</FONT>(<FONT COLOR="#666600">6</FONT>.<FONT COLOR="#666600">0</FONT>)

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test&quot;</FONT>)
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'AFTER: '</FONT>, cur.<FONT COLOR="#000066">fetchall</FONT>()
</pre>

<p class="textParagraph" style="margin-bottom: 0em;">
So, should the example program print
</p>
<pre class="programOutputBlock" style="margin-top: 0em; margin-bottom: 0em;">
BEFORE: [(1, 'A'), (2, 'B'), (3, 'C')]
AFTER:  [(1, 'A'), (2, 'X'), (3, 'C')]
</pre>
<p class="textParagraph" style="margin-top: 0em; margin-bottom: 0em;">
or
</p>
<pre class="programOutputBlock" style="margin-top: 0em; margin-bottom: 0em;">
BEFORE: [(1, 'A'), (2, 'B'), (3, 'C')]
AFTER:  [(1, 'A'), (2, 'B'), (3, 'C')]
</pre>
<p class="textParagraph" style="margin-top: 0em;">
or should it raise an exception?  The answer is more complex than one might
think.
</p>

<p class="textParagraph">
First of all, we cannot guarantee much about the example
program's behavior because there is a race condition between the obvious thread
that's executing the example code (which we'll call "UserThread" for the rest
of this section) and the KInterbasDB-internal background thread that actually
closes connections that have timed out ("TimeoutThread").
If the operating system were to suspend UserThread just after the
<code>kinterbasdb.connect</code> call for more than the specified timeout
period of 3.0 seconds, the TimeoutThread might close the connection before
UserThread had performed any preparatory operations on the database.  Although
such a scenario is extremely unlikely when more "realistic" timeout periods
such as 1800.0 seconds (30 minutes) are used, it is important to consider.
We'll explore solutions to this race condition
<a href="#adv_ct_callback_example_timeout_authorizer">later</a>.
</p>

<p class="textParagraph">
The <em>likely</em> (but not guaranteed) behavior of the example program is
that UserThread will complete all preparatory database operations including the
<code>cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;update test set b = 'X' where a = 2&quot;</FONT>)</code>
statement in the example program, then
go to sleep for not less than 6.0 seconds.  Not less than 3.0 seconds after
UserThread executes the
<code>cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;update test set b = 'X' where a = 2&quot;</FONT>)</code>
statement, TimeoutThread is likely to close the connection because it has
become eligible for timeout.
</p>

<p class="textParagraph">
The crucial issue is how TimeoutThread should resolve the transaction that
UserThread left open on <code>con</code>, and what should happen when
UserThread reawakens and tries to execute the
<code>cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test&quot;</FONT>)</code>
statement,
since the transaction that UserThread left open will no longer be active.
</p>

<a name="adv_ct_callbacks"><h4>User-Supplied Connection Timeout Callbacks</h4></a>

<p class="textParagraph">
In the context of a particular client program, it is not possible for
KInterbasDB to know the best way for TimeoutThread to react when it encounters
a connection that is eligible for timeout, but has an unresolved transaction.
For this reason, KInterbasDB's connection timeout system offers callbacks that
the client programmer can use to guide the TimeoutThread's actions, or to log
information about connection timeout patterns.
</p>

<a name="adv_ct_callback_before"><h5>The "Before Timeout" Callback</h5></a>

<p class="textParagraph" style="margin-bottom: 0.1em;">
The client programmer can supply a "before timeout" callback that accepts a
single dictionary parameter and returns an integer code to indicate how the
TimeoutThread should proceed when it finds a connection eligible for timeout.
Within the dictionary, KInterbasDB provides the following entries:
</p>
<ul style="margin-top: 0em;">
  <li>
    <code>'dsn'</code>:  The <code>dsn</code> parameter that was passed to
    <code>kinterbasdb.connect</code> when the connection was created.
  </li>
  <li>
    <code>'has_transaction'</code>:  A <code>bool</code>ean that indicates
    whether the connection has an unresolved transaction.
  </li>
  <li>
    <code>'active_secs'</code>:  A <code>float</code> that indicates how
    many seconds elapsed between the point when the connection attached to the
    server and the last client program activity on the connection.
  </li>
  <li>
    <code>'idle_secs'</code>:  A <code>float</code> that indicates how many
    seconds have elapsed since the last client program activity on the
    connection.
    This value will not be less than the specified timeout period, and is
    likely to only a fraction of a second longer.
  </li>
</ul>

<p class="textParagraph" style="margin-bottom: 0.1em;">
Based on those data, the user-supplied callback should return one of the
following codes:
</p>
<ul style="margin-top: 0em;">
  <li><code>kinterbasdb.CT_VETO</code>:
    <p class="textParagraph">
    Directs the TimeoutThread not to close the connection at the current time,
    and not to reconsider timing the connection out until at least another
    timeout period has passed.
    </p>
    <p class="textParagraph">
    For example, if a connection was created with
    a timeout period of 120.0 seconds, and the user-supplied "before callback"
    returns <code>kinterbasdb.CT_VETO</code>, the TimeoutThread will not
    reconsider timing out that particular connection until at least another
    120.0 seconds have elapsed.
    </p>
  </li>
  <li><code>kinterbasdb.CT_NONTRANSPARENT</code> ("nontransparent rollback"):
    <p class="textParagraph">
    Directs the TimeoutThread to roll back the connection's unresolved
    transaction (if any), then close the connection.  Any future attempt to use
    the connection will raise a <code>kinterbasdb.ConnectionTimedOut</code>
    exception.
    </p>
  </li>
  <li><code>kinterbasdb.CT_ROLLBACK</code> ("transparent rollback"):
    <p class="textParagraph">
    Directs the TimeoutThread to roll back the connection's unresolved
    transaction (if any), then close the connection.  Upon any future attempt
    to use the connection, KInterbasDB will <em>attempt</em> to transparently
    reconnect to the database and "resume where it left off" insofar as
    possible.
    </p>
    <p class="textParagraph">
    Of course, network problems and the like could prevent KInterbasDB's
    <em>attempt</em> at transparent resumption from succeeding.  Also, highly
    state-dependent objects such as open result sets,
    <code>BlobReader</code>s, and <code>PreparedStatement</code>s cannot be
    used transparently across a connection timeout.
    </p>
  </li>
  <li><code>kinterbasdb.CT_COMMIT</code> ("transparent commit"):
    <p class="textParagraph">
    Directs the TimeoutThread to commit the connection's unresolved transaction
    (if any), then close the connection.  Upon any future attempt to use
    the connection, KInterbasDB will <em>attempt</em> to transparently
    reconnect to the database and "resume where it left off" insofar as
    possible.
    </p>
  </li>
</ul>

<p class="textParagraph">
If the user does not supply a "before timeout" callback, KInterbasDB considers
the timeout transparent only if the connection does not have an unresolved
transaction.
</p>

<p class="textParagraph">
If the user-supplied "before timeout" callback returns anything other than one
of the codes listed above, or if it raises an exception, the TimeoutThread will
act as though <code>kinterbasdb.CT_NONTRANSPARENT</code> had been returned.
</p>

<p class="textParagraph">
You might have noticed that the input dictionary to the "before timeout"
callback does <em>not</em> include a reference to the
<code>kinterbasdb.Connection</code> object itself.  This is a
<strong>deliberate</strong>
design decision intended to steer the client programmer away from writing
callbacks that take a long time to complete, or that manipulate the
<code>kinterbasdb.Connection</code> instance directly.  See the
<a href="#adv_ct_callback_before_caveats">caveats section</a>
for more information.
</p>

<a name="adv_ct_callback_after"><h5>The "After Timeout" Callback</h5></a>

<p class="textParagraph" style="margin-bottom: 0.1em;">
The client programmer can supply an "after timeout" callback that accepts a
single dictionary parameter.  Within that dictionary, KInterbasDB currently
provides the following entries:
</p>
<ul style="margin-top: 0em;">
  <li><code>'dsn'</code>:  The <code>dsn</code> parameter that was passed to
    <code>kinterbasdb.connect</code> when the connection was created.
  </li>
  <li><code>'active_secs'</code>:  A <code>float</code> that indicates how
    many seconds elapsed between the point when the connection attached to the
    server and the last client program activity on the connection.
  </li>
  <li><code>'idle_secs'</code>:  A <code>float</code> that indicates how many
    seconds elapsed between the last client program activity on the connection
    and the moment the TimeoutThread closed the connection.
  </li>
</ul>

<p class="textParagraph">
KInterbasDB only calls the "after timeout" callback after the connection
has actually been closed by the TimeoutThread.  If the "before timeout"
callback returns <code>kinterbasdb.CT_VETO</code> to cancel the timeout
attempt, the "after timeout" callback will not be called.
</p>

<p class="textParagraph">
KInterbasDB discards the return value of the "after timeout" callback,
and ignores any exceptions.
</p>

<p class="textParagraph">
The same <a href="#adv_ct_callback_before_caveats">caveats</a>
that apply to the "before timeout" callback also apply to the "after timeout"
callback.
</p>

<a name="adv_ct_callback_before_caveats"><h5>User-Supplied Connection Timeout Callback Caveats</h5></a>

<ul>
  <li>The user-supplied callbacks are executed by the TimeoutThread.
    They should be designed to avoid blocking the TimeoutThread any longer than
    absolutely necessary.
  </li>
  <li>
    <p style="color: red;">
    Manipulating the <code>Connection</code> object that is being timed out
    (or any of that connection's subordinate objects such as
    <code>Cursor</code>s, <code>BlobReader</code>s, or
    <code>PreparedStatement</code>s) from the timeout callbacks is strictly
    forbidden.
    </p>
  </li>
</ul>


<a name="adv_ct_callback_example_ct_veto">
  <h5>User-Supplied Connection Timeout Callback Example: <code>CT_VETO</code></h5>
</a>

<p class="textParagraph">
The following program registers a "before timeout" callback that
unconditionally returns <code>kinterbasdb.CT_VETO</code>, which means that the
TimeoutThread never times the connection out.
Although an "after timeout" callback is also registered, it will never be
called.
</p>

<pre class="codeBlock">
<FONT COLOR="#1413be">import</FONT> time
<FONT COLOR="#1413be">import</FONT> kinterbasdb

<FONT COLOR="#1413be">def</FONT> <FONT COLOR="#000066">callback_before</FONT>(info):
    <FONT COLOR="#1413be">print</FONT>
    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'callback_before called; input parameter contained:'</FONT>
    <FONT COLOR="#1413be">for</FONT> key, value <FONT COLOR="#1413be">in</FONT> info.<FONT COLOR="#000066">items</FONT>():
        <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'  %s: %s'</FONT> <FONT COLOR="#7d0000">%</FONT> (<FONT COLOR="#7d7ca4">repr</FONT>(key).<FONT COLOR="#000066">ljust</FONT>(<FONT COLOR="#666600">20</FONT>), <FONT COLOR="#7d7ca4">repr</FONT>(value))
    <FONT COLOR="#1413be">print</FONT>
    <FONT COLOR="#65c265"># Unconditionally veto any timeout attempts:</FONT>
    <FONT COLOR="#1413be">return</FONT> kinterbasdb.CT_VETO

<FONT COLOR="#1413be">def</FONT> <FONT COLOR="#000066">callback_after</FONT>(info):
    <FONT COLOR="#1413be">assert</FONT> <FONT COLOR="#007ea0">False</FONT>, <FONT COLOR="#8f8c47">'This will never be called.'</FONT>

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>r<FONT COLOR="#8f8c47">'localhost:D:\temp\test.db'</FONT>,
    user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>,
    timeout<FONT COLOR="#7d0000">=</FONT>{
        <FONT COLOR="#8f8c47">'period'</FONT>: <FONT COLOR="#666600">3</FONT>.<FONT COLOR="#666600">0</FONT>,
        <FONT COLOR="#8f8c47">'callback_before'</FONT>: callback_before,
        <FONT COLOR="#8f8c47">'callback_after'</FONT>:  callback_after,
      }
  )
cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;recreate table test (a int, b char(1))&quot;</FONT>)
con.<FONT COLOR="#000066">commit</FONT>()

cur.<FONT COLOR="#000066">executemany</FONT>(<FONT COLOR="#8f8c47">&quot;insert into test (a, b) values (?, ?)&quot;</FONT>,
    [(<FONT COLOR="#666600">1</FONT>, <FONT COLOR="#8f8c47">'A'</FONT>), (<FONT COLOR="#666600">2</FONT>, <FONT COLOR="#8f8c47">'B'</FONT>), (<FONT COLOR="#666600">3</FONT>, <FONT COLOR="#8f8c47">'C'</FONT>)]
  )
con.<FONT COLOR="#000066">commit</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test&quot;</FONT>)
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'BEFORE:'</FONT>, cur.<FONT COLOR="#000066">fetchall</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;update test set b = 'X' where a = 2&quot;</FONT>)

time.<FONT COLOR="#000066">sleep</FONT>(<FONT COLOR="#666600">6</FONT>.<FONT COLOR="#666600">0</FONT>)

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test&quot;</FONT>)
rows <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">fetchall</FONT>()
<FONT COLOR="#65c265"># The value of the second column of the second row of the table is still 'X',</FONT>
<FONT COLOR="#65c265"># because the transaction that changed it from 'B' to 'X' remains active.</FONT>
<FONT COLOR="#1413be">assert</FONT> rows[<FONT COLOR="#666600">1</FONT>][<FONT COLOR="#666600">1</FONT>] <FONT COLOR="#7d0000">==</FONT> <FONT COLOR="#8f8c47">'X'</FONT>
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'AFTER: '</FONT>, rows
</pre>

<p class="textParagraph" style="margin-bottom: 0.1em;">
Sample output:
</p>
<pre class="programOutputBlock" style="margin-top: 0em;">
BEFORE: [(1, 'A'), (2, 'B'), (3, 'C')]

callback_before called; input parameter contained:
  'dsn'               : 'localhost:D:\\temp\\test.db'
  'idle_secs'         : 3.0
  'has_transaction'   : True

AFTER:  [(1, 'A'), (2, 'X'), (3, 'C')]
</pre>


<a name="adv_ct_callback_example_timeout_authorizer">
  <h5 style="margin-top: 2em;">
    User-Supplied Connection Timeout Callback Example: Supporting Module
    <code>timeout_authorizer</code>
  </h5>
</a>

<p class="textParagraph">
The example programs for
<a href="#adv_ct_callback_example_ct_nontransparent"><code>CT_NONTRANSPARENT</code></a>,
<a href="#adv_ct_callback_example_ct_rollback"><code>CT_ROLLBACK</code></a>,
and
<a href="#adv_ct_callback_example_ct_commit"><code>CT_COMMIT</code></a>
rely on the <code>TimeoutAuthorizer</code> class from the module below to
guarantee that the TimeoutThread will not time the connection out before the
preparatory code has executed.
</p>

<pre class="codeBlock">
<FONT COLOR="#1413be">import</FONT> threading
<FONT COLOR="#1413be">import</FONT> kinterbasdb

<FONT COLOR="#1413be">class</FONT> <FONT COLOR="#000066">TimeoutAuthorizer</FONT>(<FONT COLOR="#7d7ca4">object</FONT>):
    <FONT COLOR="#1413be">def</FONT> <FONT COLOR="#007ea0">__init__</FONT>(self, opCodeWhenAuthorized):
        self.currentOpCode <FONT COLOR="#7d0000">=</FONT> kinterbasdb.CT_VETO
        self.opCodeWhenAuthorized <FONT COLOR="#7d0000">=</FONT> opCodeWhenAuthorized

        self.lock <FONT COLOR="#7d0000">=</FONT> threading.<FONT COLOR="#000066">Lock</FONT>()

    <FONT COLOR="#1413be">def</FONT> <FONT COLOR="#000066">authorize</FONT>(self):
        self.lock.<FONT COLOR="#000066">acquire</FONT>()
        <FONT COLOR="#1413be">try</FONT>:
            self.currentOpCode <FONT COLOR="#7d0000">=</FONT> self.opCodeWhenAuthorized
        <FONT COLOR="#1413be">finally</FONT>:
            self.lock.<FONT COLOR="#000066">release</FONT>()

    <FONT COLOR="#1413be">def</FONT> <FONT COLOR="#007ea0">__call__</FONT>(self, info):
        self.lock.<FONT COLOR="#000066">acquire</FONT>()
        <FONT COLOR="#1413be">try</FONT>:
            <FONT COLOR="#1413be">return</FONT> self.currentOpCode
        <FONT COLOR="#1413be">finally</FONT>:
            self.lock.<FONT COLOR="#000066">release</FONT>()
</pre>


<a name="adv_ct_callback_example_ct_nontransparent">
  <h5 style="margin-top: 2em;">
    User-Supplied Connection Timeout Callback Example:
    <code>CT_NONTRANSPARENT</code>
  </h5>
</a>

<pre class="codeBlock">
<FONT COLOR="#1413be">import</FONT> threading, time
<FONT COLOR="#1413be">import</FONT> kinterbasdb
<FONT COLOR="#1413be">import</FONT> timeout_authorizer

authorizer <FONT COLOR="#7d0000">=</FONT> timeout_authorizer.<FONT COLOR="#000066">TimeoutAuthorizer</FONT>(kinterbasdb.CT_NONTRANSPARENT)
connectionTimedOut <FONT COLOR="#7d0000">=</FONT> threading.<FONT COLOR="#000066">Event</FONT>()

<FONT COLOR="#1413be">def</FONT> <FONT COLOR="#000066">callback_after</FONT>(info):
    <FONT COLOR="#1413be">print</FONT>
    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'The connection was closed nontransparently.'</FONT>
    <FONT COLOR="#1413be">print</FONT>
    connectionTimedOut.<FONT COLOR="#000066">set</FONT>()

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>r<FONT COLOR="#8f8c47">'localhost:D:\temp\test.db'</FONT>,
    user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>,
    timeout<FONT COLOR="#7d0000">=</FONT>{
        <FONT COLOR="#8f8c47">'period'</FONT>: <FONT COLOR="#666600">3</FONT>.<FONT COLOR="#666600">0</FONT>,
        <FONT COLOR="#8f8c47">'callback_before'</FONT>: authorizer,
        <FONT COLOR="#8f8c47">'callback_after'</FONT>:  callback_after,
      }
  )
cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;recreate table test (a int, b char(1))&quot;</FONT>)
con.<FONT COLOR="#000066">commit</FONT>()

cur.<FONT COLOR="#000066">executemany</FONT>(<FONT COLOR="#8f8c47">&quot;insert into test (a, b) values (?, ?)&quot;</FONT>,
    [(<FONT COLOR="#666600">1</FONT>, <FONT COLOR="#8f8c47">'A'</FONT>), (<FONT COLOR="#666600">2</FONT>, <FONT COLOR="#8f8c47">'B'</FONT>), (<FONT COLOR="#666600">3</FONT>, <FONT COLOR="#8f8c47">'C'</FONT>)]
  )
con.<FONT COLOR="#000066">commit</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test&quot;</FONT>)
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'BEFORE:'</FONT>, cur.<FONT COLOR="#000066">fetchall</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;update test set b = 'X' where a = 2&quot;</FONT>)

authorizer.<FONT COLOR="#000066">authorize</FONT>()
connectionTimedOut.<FONT COLOR="#000066">wait</FONT>()

<FONT COLOR="#65c265"># This will raise a kinterbasdb.ConnectionTimedOut exception because the</FONT>
<FONT COLOR="#65c265"># before callback returned kinterbasdb.CT_NONTRANSPARENT:</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test&quot;</FONT>)
</pre>

<p class="textParagraph" style="margin-bottom: 0.1em;">
Sample output:
</p>
<pre class="programOutputBlock" style="margin-top: 0em;">
BEFORE: [(1, 'A'), (2, 'B'), (3, 'C')]

The connection was closed nontransparently.

Traceback (most recent call last):
  File "connection_timeouts_ct_nontransparent.py", line 42, in ?
    cur.execute("select * from test")
kinterbasdb.ConnectionTimedOut: (0, 'A transaction was still unresolved when
this connection timed out, so it cannot be transparently reactivated.')
</pre>


<a name="adv_ct_callback_example_ct_rollback">
  <h5 style="margin-top: 2em;">
    User-Supplied Connection Timeout Callback Example:
    <code>CT_ROLLBACK</code>
  </h5>
</a>

<pre class="codeBlock">
<FONT COLOR="#1413be">import</FONT> threading, time
<FONT COLOR="#1413be">import</FONT> kinterbasdb
<FONT COLOR="#1413be">import</FONT> timeout_authorizer

authorizer <FONT COLOR="#7d0000">=</FONT> timeout_authorizer.<FONT COLOR="#000066">TimeoutAuthorizer</FONT>(kinterbasdb.CT_ROLLBACK)
connectionTimedOut <FONT COLOR="#7d0000">=</FONT> threading.<FONT COLOR="#000066">Event</FONT>()

<FONT COLOR="#1413be">def</FONT> <FONT COLOR="#000066">callback_after</FONT>(info):
    <FONT COLOR="#1413be">print</FONT>
    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'The unresolved transaction was rolled back; the connection has been'</FONT>
    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">' closed transparently.'</FONT>
    <FONT COLOR="#1413be">print</FONT>
    connectionTimedOut.<FONT COLOR="#000066">set</FONT>()

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>r<FONT COLOR="#8f8c47">'localhost:D:\temp\test.db'</FONT>,
    user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>,
    timeout<FONT COLOR="#7d0000">=</FONT>{
        <FONT COLOR="#8f8c47">'period'</FONT>: <FONT COLOR="#666600">3</FONT>.<FONT COLOR="#666600">0</FONT>,
        <FONT COLOR="#8f8c47">'callback_before'</FONT>: authorizer,
        <FONT COLOR="#8f8c47">'callback_after'</FONT>:  callback_after,
      }
  )
cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;recreate table test (a int, b char(1))&quot;</FONT>)
con.<FONT COLOR="#000066">commit</FONT>()

cur.<FONT COLOR="#000066">executemany</FONT>(<FONT COLOR="#8f8c47">&quot;insert into test (a, b) values (?, ?)&quot;</FONT>,
    [(<FONT COLOR="#666600">1</FONT>, <FONT COLOR="#8f8c47">'A'</FONT>), (<FONT COLOR="#666600">2</FONT>, <FONT COLOR="#8f8c47">'B'</FONT>), (<FONT COLOR="#666600">3</FONT>, <FONT COLOR="#8f8c47">'C'</FONT>)]
  )
con.<FONT COLOR="#000066">commit</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test&quot;</FONT>)
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'BEFORE:'</FONT>, cur.<FONT COLOR="#000066">fetchall</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;update test set b = 'X' where a = 2&quot;</FONT>)

authorizer.<FONT COLOR="#000066">authorize</FONT>()
connectionTimedOut.<FONT COLOR="#000066">wait</FONT>()

<FONT COLOR="#65c265"># The value of the second column of the second row of the table will have</FONT>
<FONT COLOR="#65c265"># reverted to 'B' when the transaction that changed it to 'X' was rolled back.</FONT>
<FONT COLOR="#65c265"># The cur.execute call on the next line will transparently reactivate the</FONT>
<FONT COLOR="#65c265"># connection, which was timed out transparently.</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test&quot;</FONT>)
rows <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">fetchall</FONT>()
<FONT COLOR="#1413be">assert</FONT> rows[<FONT COLOR="#666600">1</FONT>][<FONT COLOR="#666600">1</FONT>] <FONT COLOR="#7d0000">==</FONT> <FONT COLOR="#8f8c47">'B'</FONT>
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'AFTER: '</FONT>, rows
</pre>

<p class="textParagraph" style="margin-bottom: 0.1em;">
Sample output:
</p>
<pre class="programOutputBlock" style="margin-top: 0em;">
BEFORE: [(1, 'A'), (2, 'B'), (3, 'C')]

The unresolved transaction was rolled back; the connection has been
 closed transparently.

AFTER:  [(1, 'A'), (2, 'B'), (3, 'C')]
</pre>


<a name="adv_ct_callback_example_ct_commit">
  <h5 style="margin-top: 2em;">
    User-Supplied Connection Timeout Callback Example:
    <code>CT_COMMIT</code>
  </h5>
</a>

<pre class="codeBlock">
<FONT COLOR="#1413be">import</FONT> threading, time
<FONT COLOR="#1413be">import</FONT> kinterbasdb
<FONT COLOR="#1413be">import</FONT> timeout_authorizer

authorizer <FONT COLOR="#7d0000">=</FONT> timeout_authorizer.<FONT COLOR="#000066">TimeoutAuthorizer</FONT>(kinterbasdb.CT_COMMIT)
connectionTimedOut <FONT COLOR="#7d0000">=</FONT> threading.<FONT COLOR="#000066">Event</FONT>()

<FONT COLOR="#1413be">def</FONT> <FONT COLOR="#000066">callback_after</FONT>(info):
    <FONT COLOR="#1413be">print</FONT>
    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'The unresolved transaction was committed; the connection has been'</FONT>
    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">' closed transparently.'</FONT>
    <FONT COLOR="#1413be">print</FONT>
    connectionTimedOut.<FONT COLOR="#000066">set</FONT>()

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>r<FONT COLOR="#8f8c47">'localhost:D:\temp\test.db'</FONT>,
    user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>,
    timeout<FONT COLOR="#7d0000">=</FONT>{
        <FONT COLOR="#8f8c47">'period'</FONT>: <FONT COLOR="#666600">3</FONT>.<FONT COLOR="#666600">0</FONT>,
        <FONT COLOR="#8f8c47">'callback_before'</FONT>: authorizer,
        <FONT COLOR="#8f8c47">'callback_after'</FONT>:  callback_after,
      }
  )
cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;recreate table test (a int, b char(1))&quot;</FONT>)
con.<FONT COLOR="#000066">commit</FONT>()

cur.<FONT COLOR="#000066">executemany</FONT>(<FONT COLOR="#8f8c47">&quot;insert into test (a, b) values (?, ?)&quot;</FONT>,
    [(<FONT COLOR="#666600">1</FONT>, <FONT COLOR="#8f8c47">'A'</FONT>), (<FONT COLOR="#666600">2</FONT>, <FONT COLOR="#8f8c47">'B'</FONT>), (<FONT COLOR="#666600">3</FONT>, <FONT COLOR="#8f8c47">'C'</FONT>)]
  )
con.<FONT COLOR="#000066">commit</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test&quot;</FONT>)
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'BEFORE:'</FONT>, cur.<FONT COLOR="#000066">fetchall</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;update test set b = 'X' where a = 2&quot;</FONT>)

authorizer.<FONT COLOR="#000066">authorize</FONT>()
connectionTimedOut.<FONT COLOR="#000066">wait</FONT>()

<FONT COLOR="#65c265"># The modification of the value of the second column of the second row of the</FONT>
<FONT COLOR="#65c265"># table from 'B' to 'X' will have persisted, because the TimeoutThread</FONT>
<FONT COLOR="#65c265"># committed the transaction before it timed the connection out.</FONT>
<FONT COLOR="#65c265"># The cur.execute call on the next line will transparently reactivate the</FONT>
<FONT COLOR="#65c265"># connection, which was timed out transparently.</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test&quot;</FONT>)
rows <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">fetchall</FONT>()
<FONT COLOR="#1413be">assert</FONT> rows[<FONT COLOR="#666600">1</FONT>][<FONT COLOR="#666600">1</FONT>] <FONT COLOR="#7d0000">==</FONT> <FONT COLOR="#8f8c47">'X'</FONT>
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'AFTER: '</FONT>, rows
</pre>

<p class="textParagraph" style="margin-bottom: 0.1em;">
Sample output:
</p>
<pre class="programOutputBlock" style="margin-top: 0em;">
BEFORE: [(1, 'A'), (2, 'B'), (3, 'C')]

The unresolved transaction was committed; the connection has been
 closed transparently.

AFTER:  [(1, 'A'), (2, 'X'), (3, 'C')]
</pre>

<br>
<hr>
<br>


<a name="adv_trans_control">
  <h2>Advanced Transaction Control</h2>
</a>

<p class="textParagraph">
For the sake of simplicity, KInterbasDB lets the Python programmer
ignore transaction management to the greatest extent allowed by the
Python Database API Specification 2.0.  The specification says,
"if the database supports an auto-commit feature, this must be
initially off".  At a minimum, therefore, it is necessary to call the
<code>commit</code> method of the connection in order to persist any
changes made to the database.  Transactions left unresolved by the
programmer will be <code>rollback</code>ed when the connection is
garbage collected.
</p>

<p class="textParagraph">
Remember that because of
<a href="http://philip.greenspun.com/panda/databases-choosing#acid">ACID</a>,
every data manipulation operation in the Interbase&reg;/Firebird database engine
takes place in the context of a transaction, including operations that are
conceptually "read-only", such as a typical <code>SELECT</code>.
The client programmer of KInterbasDB establishes a transaction
implicitly by using any SQL execution method, such as
<code>Connection.execute_immediate</code>, <code>Cursor.execute</code>,
or <code>Cursor.callproc</code>.
</p>

<p class="textParagraph">
Although KInterbasDB allows the programmer to pay little attention to
transactions, it also exposes the full complement of the database engine's
advanced transaction control features:
transaction parameters, retaining transactions, savepoints, and
distributed transactions.
</p>

<br>
<a name="adv_trans_control_parameters"><h4>Transaction Parameters</h4></a>

<p class="textParagraph">
The database engine offers the client programmer an optional facility called
<em>transaction parameter buffers</em> (TPBs) for tweaking the operating characteristics
of the transactions he initiates.  These include characteristics such as
"whether the transaction has read and write access to tables,
or read-only access, and whether or not other simultaneously active
transactions can share table access with the transaction"
(<a href="#ref_ib6_docs">IB 6 API Guide</a>, page 62).
</p>

<p class="textParagraph">
In addition to the implicit transaction initiation mentioned in the
<a href="#adv_trans_control">introduction</a> of this section, KInterbasDB
allows the programmer to start transactions explicitly via the
<code>Connection.begin</code> method.

Connections have a <code>default_tpb</code> attribute
that can be changed to set the default TPB for all transactions subsequently
started on the connection.

Alternatively, if the programmer only wants to set the TPB for a single
transaction, he can start a transaction explicitly via the
<code>Connection.begin</code> method and pass a TPB for that single
transaction.
</p>

<p class="textParagraph">
For details about TPB construction, see Chapter 5 of the
<a href="#ref_ib6_docs">Interbase&reg; 6 API Guide</a>.
In particular, page 63 of that document presents a table of possible
TPB elements--single bytes that the C API defines as constants whose names
begin with <code>isc_tpb_</code>.

KInterbasDB makes all of those TPB constants available (under the same names)
as module-level constants in the form of single-character strings.

A transaction parameter <em>buffer</em> is handled in C as a
character array; KInterbasDB requires that TPBs be constructed as Python
strings.  Since the constants in the <code>kinterbasdb.isc_tpb_*</code>
family are single-character Python strings, they can simply be concatenated
to create a TPB.
<br><br>
</p>

<p class="textParagraph">
The following program uses explicit transaction initiation and TPB
construction to establish an unobtrusive transaction for read-only
access to the database:
</p>

<pre class="codeBlock">
<FONT COLOR="#0000ff">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>)

<FONT COLOR="#32a532"># Construct a TPB by concatenating single-character strings (bytes)</FONT>
<FONT COLOR="#32a532"># from the kinterbasdb.isc_tpb_* family.</FONT>
customTPB <FONT COLOR="#7d0000">=</FONT> (
      kinterbasdb.isc_tpb_read
    <FONT COLOR="#7d0000">+</FONT> kinterbasdb.isc_tpb_read_committed
    <FONT COLOR="#7d0000">+</FONT> kinterbasdb.isc_tpb_rec_version
  )

<FONT COLOR="#32a532"># Explicitly start a transaction with the custom TPB:</FONT>
con.<FONT COLOR="#000066">begin</FONT>(tpb<FONT COLOR="#7d0000">=</FONT>customTPB)

<FONT COLOR="#32a532"># Now read some data using cursors:</FONT>
...

<FONT COLOR="#32a532"># Commit the transaction with the custom TPB.  Future transactions</FONT>
<FONT COLOR="#32a532"># opened on con will not use a custom TPB unless it is explicitly</FONT>
<FONT COLOR="#32a532"># passed to con.begin every time, as it was above, or</FONT>
<FONT COLOR="#32a532"># con.default_tpb is changed to the custom TPB, as in:</FONT>
<FONT COLOR="#32a532">#   con.default_tpb = customTPB</FONT>
con.<FONT COLOR="#000066">commit</FONT>()
</pre>


<br>
<a name="adv_trans_control_retaining"><h4>Retaining Operations</h4></a>

<p class="textParagraph">
  The <code>commit</code> and <code>rollback</code> methods of
  <code>kinterbasdb.Connection</code>
  accept an optional boolean parameter <code>retaining</code>
  (default <code>False</code>) to indicate whether to recycle the
  transactional context of the transaction being resolved by the
  method call.
</p>
<p class="textParagraph">
  If <code>retaining</code> is <code>True</code>, the infrastructural
  support for the transaction active
  at the time of the method call will be "retained" (efficiently and
  transparently recycled) after the database server has committed or rolled
  back the conceptual transaction.
</p>

<p class="textParagraph">
  In code that commits or rolls back frequently, "retaining" the
  transaction yields considerably better performance.
  However, retaining transactions must be used cautiously because they can
  interfere with the server's ability to garbage collect old record versions.
  For details about this issue, read the "Garbage" section of
  <a href="http://www.ibphoenix.com/main.nfs?a=ibphoenix&s=1123236035:18161&page=ibp_expert4">this document</a>
  by Ann Harrison.
</p>
<p class="textParagraph">
For more information about retaining transactions, see page 291 of the
<a href="#ref_ib6_docs">Interbase&reg; 6 API Guide</a>.
</p>

<br>

<a name="adv_trans_control_savepoints"><h4>Savepoints</h4></a>

<p class="textParagraph">
Firebird 1.5 introduced support for transaction savepoints.
Savepoints are named, intermediate control points within an open transaction
that can later be rolled back to, without affecting the preceding work.
Multiple savepoints can exist within a single unresolved transaction,
providing "multi-level undo" functionality.
</p>

<p class="textParagraph">
Although Firebird savepoints are fully supported from SQL alone via the
<code>SAVEPOINT 'name'</code> and <code>ROLLBACK TO 'name'</code>
statements, KInterbasDB also exposes savepoints at the Python API level
for the sake of convenience.
The method <code>Connection.savepoint(name)</code> establishes a savepoint
with the specified <code>name</code>.
To roll back to a specific savepoint, call the
<code>Connection.rollback</code> method and provide a value (the name of
the savepoint) for the optional <code>savepoint</code> parameter.
If the <code>savepoint</code> parameter of <code>Connection.rollback</code>
is not specified, the active transaction is cancelled in its entirety,
as required by the Python Database API Specification.
</p>

<p class="textParagraph">
The following program demonstrates savepoint manipulation via
the KInterbasDB API, rather than raw SQL.
</p>

<span style="color: blue;"></span>

<pre class="codeBlock" style="margin-top: 0px;">
<FONT COLOR="#0000ff">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>)
cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;recreate table test_savepoints (a integer)&quot;</FONT>)
con.<FONT COLOR="#000066">commit</FONT>()

<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'Before the first savepoint, the contents of the table are:'</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test_savepoints&quot;</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">' '</FONT>, cur.<FONT COLOR="#000066">fetchall</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into test_savepoints values (?)&quot;</FONT>, [<FONT COLOR="#666600">1</FONT>])
<strong>con.<FONT COLOR="#000066">savepoint</FONT>(<FONT COLOR="#8f8c47">'A'</FONT>)</strong>
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'After savepoint A, the contents of the table are:'</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test_savepoints&quot;</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">' '</FONT>, cur.<FONT COLOR="#000066">fetchall</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into test_savepoints values (?)&quot;</FONT>, [<FONT COLOR="#666600">2</FONT>])
<strong>con.<FONT COLOR="#000066">savepoint</FONT>(<FONT COLOR="#8f8c47">'B'</FONT>)</strong>
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'After savepoint B, the contents of the table are:'</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test_savepoints&quot;</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">' '</FONT>, cur.<FONT COLOR="#000066">fetchall</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into test_savepoints values (?)&quot;</FONT>, [<FONT COLOR="#666600">3</FONT>])
<strong>con.<FONT COLOR="#000066">savepoint</FONT>(<FONT COLOR="#8f8c47">'C'</FONT>)</strong>
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'After savepoint C, the contents of the table are:'</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test_savepoints&quot;</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">' '</FONT>, cur.<FONT COLOR="#000066">fetchall</FONT>()

<strong>con.<FONT COLOR="#000066">rollback</FONT>(savepoint<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'A'</FONT>)</strong>
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'After rolling back to savepoint A, the contents of the table are:'</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test_savepoints&quot;</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">' '</FONT>, cur.<FONT COLOR="#000066">fetchall</FONT>()

<strong>con.<FONT COLOR="#000066">rollback</FONT>()</strong>
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'After rolling back entirely, the contents of the table are:'</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test_savepoints&quot;</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">' '</FONT>, cur.<FONT COLOR="#000066">fetchall</FONT>()
</pre>

<p class="textParagraph">
The output of the example program is shown below.
</p>

<pre class="programOutputBlock" style="margin-top: 0px;">
Before the first savepoint, the contents of the table are:
  []
After savepoint A, the contents of the table are:
  [(1,)]
After savepoint B, the contents of the table are:
  [(1,), (2,)]
After savepoint C, the contents of the table are:
  [(1,), (2,), (3,)]
After rolling back to savepoint A, the contents of the table are:
  [(1,)]
After rolling back entirely, the contents of the table are:
  []
</pre>

<br>
<a name="adv_trans_control_distributed"><h4>Distributed Transactions</h4></a>

<span class="XXX_ADDRESS_THIS">XXX: KInterbasDB's support for distributed
transactions has not yet been thoroughly documented.  In the meantime, read the
source code for the <code>kinterbasdb.ConnectionGroup</code> class
and examine the brief example program below.</span>

<pre class="codeBlock">
<FONT COLOR="#0000ff">import</FONT> kinterbasdb

<FONT COLOR="#32a532"># Establish multiple connections the usual way:</FONT>
con1 <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'weasel:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>)
con2 <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'coyote:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>)

<FONT COLOR="#32a532"># Create a ConnectionGroup to associate multiple connections in such a</FONT>
<FONT COLOR="#32a532"># way that they can participate in a distributed transaction.</FONT>
<FONT COLOR="#32a532"># !!!</FONT>
<FONT COLOR="#32a532"># NO TWO MEMBERS OF A SINGLE CONNECTIONGROUP SHOULD BE ATTACHED TO THE SAME DATABASE!</FONT>
<FONT COLOR="#32a532"># !!!</FONT>
group <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">ConnectionGroup</FONT>( connections<FONT COLOR="#7d0000">=</FONT>(con1,con2) )

<FONT COLOR="#32a532"># Start a distributed transaction involving all of the members of the group</FONT>
<FONT COLOR="#32a532"># (con1 and con2 in this case) with one of the following approaches:</FONT>
<FONT COLOR="#32a532">#   - Call  group.begin()</FONT>
<FONT COLOR="#32a532">#   - Call  con1.begin(); the operation will &quot;bubble upward&quot; and apply to the group.</FONT>
<FONT COLOR="#32a532">#   - Call  con2.begin(); the operation will &quot;bubble upward&quot; and apply to the group.</FONT>
<FONT COLOR="#32a532">#   - Just start executing some SQL statements on either con1 or con2.</FONT>
<FONT COLOR="#32a532">#     A transaction will be started implicitly; it will be a distributed</FONT>
<FONT COLOR="#32a532">#     transaction because con1 and con2 are members of a ConnectionGroup.</FONT>
group.<FONT COLOR="#000066">begin</FONT>()

<FONT COLOR="#32a532"># Perform some database changes the usual way (via cursors on con1 and con2):</FONT>
...

<FONT COLOR="#32a532"># Commit or roll back the distributed transaction by calling the commit</FONT>
<FONT COLOR="#32a532"># or rollback method of the ConnectionGroup itself, or the commit or</FONT>
<FONT COLOR="#32a532"># rollback method of any member connection (con1 or con2 in this case).</FONT>
group.<FONT COLOR="#000066">commit</FONT>()

<FONT COLOR="#32a532"># Unless you want to perform another distributed transaction, disband the</FONT>
<FONT COLOR="#32a532"># group so that member connections can operate independently again.</FONT>
group.<FONT COLOR="#000066">clear</FONT>()
</pre>

<p style="margin-bottom: 0px;"><strong>Notes:</strong></p>

<p class="textParagraph">
While a <code>Connection</code> belongs to a
<code>ConnectionGroup</code>, any calls to the connection's transactional
methods
(<code>begin</code>, <code>prepare</code>, <code>commit</code>, <code>rollback</code>)
will "bubble upward" to apply to the distributed transaction shared by the
group as a whole.
</p>

<p class="textParagraph">
Connections can be dynamically <code>add</code>ed and <code>remove</code>d
from a <code>ConnectionGroup</code> provided that neither the group nor
the connection itself has an unresolved transaction at the time of the
addition/removal.
</p>

<a name="adv_trans_control_distributed_limitations">
  <h4>Pitfalls and Limitations</h4>
</a>

<ul>
  <li>
    <span style="color: red; font-weight: bold;">
    Never add more than one connection to the same database to the same
    <code>ConnectionGroup</code>!
    </span>
  </li>
</ul>


<br>
<hr>
<br>

<a name="adv_param_conv">
  <h2>Parameter Conversion</h2>
</a>

<p class="textParagraph">
KInterbasDB converts bound parameters marked with a <code>?</code>
in SQL code in a standard way.  However, the module also offers several
extensions to standard parameter binding, intended to make client code
more readable and more convenient to write.
</p>

<a name="adv_param_conv_implicit_from_string">
  <h3>Implicit Conversion of Input Parameters from Strings</h3>
</a>

<p class="textParagraph">
The database engine treats most SQL data types in a weakly typed fashion:
the engine may attempt to convert the raw value to a different type,
as appropriate for the current context.
For instance, the SQL expressions <code>123</code> (integer)
and <code>'123'</code> (string) are treated equivalently when the value is
to be inserted into an <code>integer</code> field; the same applies when
<code>'123'</code> and <code>123</code> are to be inserted into a
<code>varchar</code> field.
</p>

<p class="textParagraph">
This weak typing model is quite unlike Python's dynamic yet strong typing.
Although weak typing is regarded with suspicion by most experienced
Python programmers, the database engine is in certain situations so
aggressive about its typing model that KInterbasDB must
<a href="http://sourceforge.net/tracker/index.php?func=detail&aid=531828&group_id=9913&atid=309913">compromise</a>
in order to remain an elegant means of programming the database engine.
</p>

<p class="textParagraph">
An example is the handling of "magic values" for date and time fields.
The database engine interprets certain string values such as
<code>'yesterday'</code> and <code>'now'</code> as having special meaning in
a date/time context.
If KInterbasDB did not accept strings as the values of parameters destined
for storage in date/time fields, the resulting code would be awkward.
Consider the difference between the two Python snippets
below, which insert a row containing an integer and a timestamp into a
table defined with the following DDL statement:
</p>
<pre class="codeBlock" style="margin-top: 0px;">
<FONT COLOR="#0000ff">create</FONT> <FONT COLOR="#0000ff">table</FONT> test_table (i <FONT COLOR="#0000ff">int</FONT>, t <FONT COLOR="#0000ff">timestamp</FONT>)
</pre>

<pre class="codeBlock">
i <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#666600">1</FONT>
t <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#8f8c47">'now'</FONT>
sqlWithMagicValues <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#8f8c47">&quot;insert into test_table (i, t) values (?, '%s')&quot;</FONT> <FONT COLOR="#7d0000">%</FONT> t
cur.<FONT COLOR="#000066">execute</FONT>( sqlWithMagicValues, (i,) )
</pre>

<pre class="codeBlock">
i <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#666600">1</FONT>
t <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#8f8c47">'now'</FONT>
cur.<FONT COLOR="#000066">execute</FONT>( <FONT COLOR="#8f8c47">&quot;insert into test_table (i, t) values (?, ?)&quot;</FONT>, (i, t) )
</pre>

<p class="textParagraph">
If KInterbasDB did not support weak parameter typing, string parameters
that the database engine is to interpret as "magic values" would have to
be rolled into the SQL statement in a separate operation from the
binding of the rest of the parameters, as in the first Python
snippet above.
Implicit conversion of parameter values from strings allows the consistency
evident in the second snippet, which is both more readable and more general.
</p>

<p class="textParagraph">
It should be noted that KInterbasDB does not perform the conversion from
string itself.  Instead, it passes that responsibility to the database
engine by changing the parameter metadata structure dynamically at the
last moment, then restoring the original state of the metadata structure
after the database engine has performed the conversion.
</p>

<p class="textParagraph">
A secondary benefit is that when one uses KInterbasDB to import large
amounts of data from flat files into the database, the incoming values
need not necessarily be converted to their proper Python types before
being passed to the database engine.  Eliminating this
intermediate step may accelerate the import process considerably, although
other factors such as the chosen connection protocol and the deactivation
of indexes during the import are more consequential.
For bulk import tasks, the database engine's external tables also deserve
consideration.  External tables can be used to suck semi-structured data
from flat files directly into the relational database without the
intervention of an ad hoc conversion program.
</p>


<a name="adv_param_conv_dynamic_type_translation">
  <h3>Dynamic Type Translation</h3>
</a>

<p class="textParagraph">
Dynamic type translators are conversion functions registered by the
Python programmer to transparently convert database field values
to and from their internal representation.
</p>

<p class="textParagraph">
  The client programmer can choose to ignore translators altogether, in
which case KInterbasDB will manage them behind the scenes.
  Otherwise, the client programmer can use any of several
<a href="#adv_param_conv_dynamic_type_translation_tbl_ref_trans">standard type translators</a>
included with KInterbasDB, register custom translators, or
set the translators to <code>None</code> to deal directly with the
KInterbasDB-internal representation of the data type.
  When translators have been registered for a specific SQL data
type, Python objects on their way into a database field of that type
will be passed through the input translator before they are presented
to the database engine; values on their way out of the database into
Python will be passed through the corresponding output translator.
Output and input translation for a given type is usually implemented
by two different functions.
</p>

<p class="textParagraph">
</p>


<a name="adv_param_conv_dynamic_type_translation_specifics">
  <h4>Specifics of the Dynamic Type Translation API</h4>
</a>

<p class="textParagraph">
Translators are registered with the <code>[set|get]_type_trans_[in|out]</code>
methods of <code>Connection</code> and <code>Cursor</code>.
The <code>set_type_trans_[in|out]</code> methods accept a single argument:
a mapping of type name to translator.
The <code>get_type_trans[in|out]</code> methods return a copy of the
translation table.  <code>Cursor</code>s inherit their
<code>Connection</code>'s translation settings, but can override
them without affecting the connection or other cursors (much as
subclasses can override the methods of their base classes).
</p>

<p class="textParagraph">
The following code snippet installs an input translator for fixed
point types (<code>NUMERIC</code>/<code>DECIMAL</code> SQL types)
into a connection:
</p>
<pre class="codeBlock" style="margin-top: 0px; margin-bottom: 0px;">
con.set_type_trans_in( {'FIXED': fixed_input_translator_function} )
</pre>

<p class="textParagraph">
The following method call retrieves the type translation table for
<code>con</code>:
</p>
<pre class="codeBlock" style="margin-top: 0px; margin-bottom: 0px;">
con.get_type_trans_in()
</pre>

<p class="textParagraph">
The method call above would return a translation table (dictionary) such as
this:
</p>
<pre class="programOutputBlock" style="margin-top: 0px;">
{
  'DATE': &lt;function date_conv_in at 0x00920648&gt;,
  'TIMESTAMP': &lt;function timestamp_conv_in at 0x0093E090&gt;,
  'FIXED': &lt;function &lt;lambda&gt; at 0x00962DB0&gt;,
  'TIME': &lt;function time_conv_in at 0x009201B0&gt;
}
</pre>

<p class="textParagraph">
Notice that although the sample code registered only one type
translator, there are four listed in the mapping returned by the
<code>get_type_trans_in</code> method.  By default, KInterbasDB uses dynamic
type translation to implement the conversion of
<code>DATE</code>, <code>TIME</code>, <code>TIMESTAMP</code>,
<code>NUMERIC</code>, and <code>DECIMAL</code>
values.
For the source code locations of KInterbasDB's reference translators,
see the
<a href="#adv_param_conv_dynamic_type_translation_tbl_ref_trans">table</a>
in the next section.
</p>

<p class="textParagraph">
In the sample above, a translator is registered under the key
<code>'FIXED'</code>, but Firebird has no SQL data type named
<code>FIXED</code>.  The following table lists the names of the
database engine's SQL data types in the left column, and the
corresponding KInterbasDB-specific key under which client programmers can
register translators in the right column.
</p>


<table style="margin-left: 2em;">
  <tr>
    <td style="font-size: 105%; font-style: italic; font-weight: bold;">
        <a name="adv_param_conv_dynamic_type_translation_tbl_trans_keys">
        Mapping of SQL Data Type Names to Translator Keys
        </a>
    </td>
  </tr>
  <tr>
    <td>
<table border="1">
  <thead style="background-color: #EEEEEE; font-weight: bold;">
    <tr>
      <td style="width: 9em;">SQL Type(s)</td>
      <td>Translator Key</td>
    </tr>
  </thead>

  <tr>
    <td style="vertical-align: top;">
        <code>CHAR</code>/<code>VARCHAR</code>
    </td>
    <td>
        <code>'TEXT'</code>
        <span style="font-size: 80%;">for fields with charsets
        <code>NONE</code>, <code>OCTETS</code>, or <code>ASCII</code></span>
        <br>
        <code>'TEXT_UNICODE'</code>
        <span style="font-size: 80%;">for all other charsets</span>
    </td>
  </tr>
  <tr>
    <td><code>BLOB</code></td>
    <td><code>'BLOB'</code></td>
  </tr>

  <tr>
    <td><code>SMALLINT/INTEGER/BIGINT</code></td>
    <td><code>'INTEGER'</code></td>
  </tr>
  <tr>
    <td><code>FLOAT</code>/<code>DOUBLE PRECISION</code></td>
    <td><code>'FLOATING'</code></td>
  </tr>
  <tr>
    <td><code>NUMERIC</code>/<code>DECIMAL</code></td>
    <td><code>'FIXED'</code></td>
  </tr>

  <tr>
    <td><code>DATE</code></td>
    <td><code>'DATE'</code></td>
  </tr>
  <tr>
    <td><code>TIME</code></td>
    <td><code>'TIME'</code></td>
  </tr>
  <tr>
    <td><code>TIMESTAMP</code></td>
    <td><code>'TIMESTAMP'</code></td>
  </tr>
</table>
    </td>
  </tr>
</table>


<a name="adv_param_conv_dynamic_type_translation_consequences">
  <h4>Consequences of the Availability of Dynamic Type Translation in KInterbasDB</h4>
</a>

<p class="textParagraph">
Dynamic type translation has
<a href="#faq_fep_is_mxdatetime_required">eliminated</a> KInterbasDB's
dependency on <code>mx.DateTime</code>. Although KInterbasDB will continue
to use <code>mx.DateTime</code> as its default date/time representation
for the sake of backward compatibility, dynamic type translation allows
users to conveniently deal with database date/time values in terms of the
new standard library module <code>datetime</code>, or any other representation
they care to write translators for.
</p>

<p class="textParagraph">
Dynamic type translation also allows <code>NUMERIC</code>/<code>DECIMAL</code>
values to be transparently represented as
<code>decimal.Decimal</code>
objects rather than scaled integers, which is much more convenient.
For backward compatibility, <code>NUMERIC</code>/<code>DECIMAL</code>
values are still represented by default as Python <code>float</code>s,
and the older API based on the <code>Connection.precision_mode</code>
attribute is still present.
However, all of these representations are now implemented "under the hood" via
dynamic type translation.
</p>

<p class="textParagraph">
Reference implementations of all of the translators discussed above
are provided with KInterbasDB, in these modules:
</p>

<table style="margin-left: 2em;">
  <tr>
    <td style="font-size: 105%; font-style: italic; font-weight: bold;">
        <a name="adv_param_conv_dynamic_type_translation_tbl_ref_trans">
        Reference Translators Included with KInterbasDB
        </a>
    </td>
  </tr>
  <tr>
    <td>
<table border="1">
  <thead style="background-color: #EEEEEE; font-weight: bold;">
    <tr>
      <td style="width: 11em;">SQL Type(s)</td>
      <td style="width: 16em;">Python Type(s)</td>
      <td style="width: 21em;">Reference Implementation In Module</td>
    </tr>
  </thead>

  <tr>
    <td rowspan="4"><code>NUMERIC</code>/<code>DECIMAL</code></td>
    <td><code>float</code> <em>(imprecise)</em> (default)</td>
    <td><code>kinterbasdb.typeconv_fixed_stdlib</code></td>
  </tr>
  <tr>
    <td>scaled <code>int</code> <em>(precise)</em></td>
    <td><code>kinterbasdb.typeconv_fixed_stdlib</code></td>
  </tr>
  <tr>
    <td><code>fixedpoint.FixedPoint</code> <em>(precise)</em></td>
    <td><code>kinterbasdb.typeconv_fixed_fixedpoint</code></td>
  </tr>
  <tr>
    <td><code>decimal.Decimal</code> <em>(precise)</em></td>
    <td><code>kinterbasdb.typeconv_fixed_decimal</code></td>
  </tr>

  <tr>
    <td rowspan="2"><code>DATE</code>/<code>TIME</code>/<code>TIMESTAMP</code></td>
    <td><code>mx.DateTime</code> (default)</td>
    <td><code>kinterbasdb.typeconv_datetime_mx</code></td>
  </tr>
  <tr>
    <td>Python 2.3+ <code>datetime</code></td>
    <td><code>kinterbasdb.typeconv_datetime_stdlib</code></td>
  </tr>

  <tr>
    <td>
        <code>CHAR/VARCHAR</code>
        <div style="font-size: 80%;">(with any character set except<br> NONE, OCTETS, ASCII)</div>
    </td>
    <td style="vertical-align: top;"><code>unicode</code></td>
    <td style="vertical-align: top;"><code>kinterbasdb.typeconv_text_unicode</code></td>
  </tr>

</table>
    </td>
  </tr>
</table>



<a name="adv_param_conv_dynamic_type_translation_writing_translators">
  <h4>Writing Custom Translators</h4>
</a>

<p class="textParagraph">
Below is a table that specifies the required argument and return value
signatures of input and output converters for the various translator
keys.
Python's native types map perfectly to
<code>'TEXT'</code>,
<code>'TEXT_UNICODE'</code>,
<code>'BLOB'</code>,
<code>'INTEGER'</code>,
and <code>'FLOATING'</code>
types, so in those cases the translator signatures are very simple.
The signatures for
<code>'FIXED'</code>,
<code>'DATE'</code>,
<code>'TIME'</code>,
and <code>'TIMESTAMP'</code>
are not as simple because Python (before 2.4) lacks native types to
represent these values with both precision <em>and</em> convenience.
KInterbasDB handles <code>'FIXED'</code> values internally as scaled
integers; the date and time types as tuples.
</p>

<p class="textParagraph">
KInterbasDB itself uses translators implemented according to the rules in
the table below; the code for these reference translators can be found
in the Python modules named <code>kinterbasdb.typeconv_*</code>
(see the
<a href="#adv_param_conv_dynamic_type_translation_tbl_ref_trans">table</a>
in the previous section for details).
</p>


<table style="margin-left: 1em;">
  <tr>
    <td style="font-size: 105%; font-style: italic; font-weight: bold;">
        <a name="adv_param_conv_dynamic_type_translation_tbl_signatures">
        Signature Specifications for Input and Output Translators
        </a>
    </td>
  </tr>
  <tr>
    <td>
<table border="1">
  <thead style="background-color: #EEEEEE; font-weight: bold;">
    <tr>
      <td style="width: 6em;">Translator Key</td>
      <td>Input Translator Argument/Return Value Signature</td>
      <td>Output Translator Signature</td>
    </tr>
  </thead>

  <tr>
    <td>
        <code>'TEXT'</code><br><br>
        <span style="font-size: 80%">
        (for <code>CHAR</code>/<code>VARCHAR</code> fields
        with character sets <code>NONE</code>, <code>OCTETS</code>,
        or <code>ASCII</code>)
        </span>
    </td>
    <td>Args: a single Python <code>str</code>ing argument
        (or <code>None</code>)
        <br><br>
        Returns: a single Python <code>str</code>ing
    </td>
    <td>Same signature as input translator, except that return value is not constrained.</td>
  </tr>
  <tr>
    <td>
        <code>'TEXT_UNICODE'</code><br><br>
        <span style="font-size: 80%">
        (for <code>CHAR</code>/<code>VARCHAR</code> fields
        with character sets other than <code>NONE</code>, <code>OCTETS</code>,
        or <code>ASCII</code>)
        </span>
    </td>
    <td>Args:
        a single Python 2-tuple argument containing a Python <code>unicode</code>
        or <code>str</code> object (or <code>None</code>) in the first element;
        the database character set code in the second element (the tuple is
        of the form <code>(val, dbCharacterSetCode)</code>).
        <br><br>
        The database character set codes (which are integers) are defined on
        pages 221-225 of the
        <a href="#ref_ib6_docs">Interbase&reg; 6 Data Definition Guide</a>.
        The module <code>kinterbasdb.typeconv_text_unicode</code> contains a
        dictionary named <code>DB_TO_PYTHON_ENCODING_MAP</code> that maps database
        character set codes to Python codec names.  For example, the database
        character set <code>UNICODE_FSS</code> has code <code>3</code>;
        <code>kinterbasdb.typeconv_text_unicode.DB_TO_PYTHON_ENCODING_MAP[3]</code>
        is <code>'utf_8'</code>, the name of a Python codec that can be passed to
        the <code>encode</code>/<code>decode</code> methods of
        <code>unicode</code>/<code>str</code>.
        <br><br>
        Returns: a Python <code>str</code> object containing the
        <span style="font-weight: bold; text-decoration: underline;">en</span>coded
        representation of the incoming value (typically computed
        via <code>val.encode</code>).
    </td>
    <td style="vertical-align: top;">
        Args:
        a single Python 2-tuple argument containing a Python <code>str</code>
        object (or <code>None</code>) in the first element; the database
        character set code in the second element (the tuple is
        of the form <code>(val, dbCharacterSetCode)</code>).
        <code>val</code> contains the encoded representation of the Unicode string.
        <br><br>
        Returns: a Python <code>unicode</code> object containing the
        <span style="font-weight: bold; text-decoration: underline;">de</span>coded
        representation of the outgoing value (typically computed
        via <code>val.decode</code>).
    </td>
  </tr>
  <tr>
    <td><code>'BLOB'</code></td>
    <td>By default, same signature as that of <code>'TEXT'</code>.  A special
      case was introduced in KInterbasDB 3.2 to allow for
      <a href="#adv_param_conv_blobs">streaming blob handling</a>.
    </td>
    <td>Same signature as input translator, except that return value is not constrained.</td>
  </tr>

  <tr>
    <td><code>'INTEGER'</code></td>
    <td>Args: a single Python <code>int</code> argument
        (or <code>None</code>)
        <br><br>
        Returns: a single Python <code>int</code> (or <code>long</code>, if
        the number too large to fit in an <code>int</code>)
    </td>
    <td>Same signature as input translator, except that return value is not constrained.</td>
  </tr>
  <tr>
    <td><code>'FLOATING'</code></td>
    <td>Args: a single Python <code>float</code> argument
        (or <code>None</code>)<br>
        Returns: a single Python <code>float</code>
    </td>
    <td>Same signature as input translator, except that return value is not constrained.</td>
  </tr>
  <tr>
    <td><code>'FIXED'</code></td>
    <td>Args: a single Python 2-tuple argument containing a scaled Python
        integer in the first element and the scale factor in the second
        element (the tuple is of the form <code>(val, scale)</code>).
        <br><br>
        Returns: a single Python integer, scaled appropriately
    </td>
    <td>Same signature as input translator, except that return value is not constrained.</td>
  </tr>

  <tr>
    <td><code>'DATE'</code></td>
    <td>Args:  an instance of the chosen date type (such as Python 2.3+'s
        <code>datetime.date</code>) or <code>None</code>
        <br><br>
        Returns: a single Python 3-tuple of the form
        <code>(year, month, day)</code>
    </td>
    <td>Args: a single Python 3-tuple of the form
        <code>(year, month, day)</code>
        (or <code>None</code> if the database field was <code>NULL</code>)
        <br><br>
        Return value is not constrained.
    </td>
  </tr>
  <tr>
    <td><code>'TIME'</code></td>
    <td>Args: an instance of the chosen time type (such as Python 2.3+'s
        <code>datetime.time</code>) or <code>None</code>
        <br><br>
        Returns: a single Python 3-tuple of the form
        <code>(hour, minute, second)</code>
    </td>
    <td>Args: a single Python 3-tuple of the form
        <code>(hour, minute, second)</code>
        (or <code>None</code> if the database field was <code>NULL</code>).
        <br><br>
        Return value is not constrained.
    </td>
  </tr>
  <tr>
    <td><code>'TIMESTAMP'</code></td>
    <td>Args:  an instance of the chosen time type (such as Python 2.3+'s
        <code>datetime.datetime</code>) or <code>None</code>
        <br><br>
        Returns: a single Python 6-tuple of the form
        <code>(year, month, day, hour, minute, second)</code>
    </td>
    <td>Args: a single Python 3-tuple of the form
        <code>(year, month, day, hour, minute, second)</code>.
        (or <code>None</code> if the database field was <code>NULL</code>).
        <br><br>
        Return value is not constrained.
    </td>
  </tr>
</table>
    </td>
  </tr>
</table>


<br><br>

<a name="adv_param_conv_dynamic_type_translation_example">
  <h4>Example Programs</h4>
</a>

<a name="adv_param_conv_dynamic_type_translation_example_datetime">
<h5>DATE/TIME/TIMESTAMP</h5>
</a>

<pre class="codeBlock">
<FONT COLOR="#0000ff">import</FONT> datetime <FONT COLOR="#32a532"># Python 2.3 standard library module</FONT>

<FONT COLOR="#0000ff">import</FONT> kinterbasdb
<FONT COLOR="#0000ff">import</FONT> kinterbasdb.typeconv_datetime_stdlib <FONT COLOR="#0000ff">as</FONT> tc_dt


<FONT COLOR="#0000ff">def</FONT> <FONT COLOR="#000066">connect</FONT>(<FONT COLOR="#7d0000">*</FONT>args, <FONT COLOR="#7d0000">**</FONT>kwargs):
    <FONT COLOR="#404040">&quot;&quot;&quot;</FONT>
<FONT COLOR="#404040">      This wrapper around kinterbasdb.connect creates connections that use</FONT>
<FONT COLOR="#404040">    the datetime module (which entered the standard library in Python 2.3)</FONT>
<FONT COLOR="#404040">    for both input and output of DATE, TIME, and TIMESTAMP database fields.</FONT>
<FONT COLOR="#404040">      This wrapper simply registers kinterbasdb's official date/time</FONT>
<FONT COLOR="#404040">    translators for the datetime module, which reside in the</FONT>
<FONT COLOR="#404040">    kinterbasdb.typeconv_datetime_stdlib module.</FONT>
<FONT COLOR="#404040">      An equivalent set of translators for mx.DateTime (which kinterbasdb</FONT>
<FONT COLOR="#404040">    uses by default for backward compatibility) resides in the</FONT>
<FONT COLOR="#404040">    kinterbasdb.typeconv_datetime_mx module.</FONT>
<FONT COLOR="#404040">      Note that because cursors inherit their connection's dynamic type</FONT>
<FONT COLOR="#404040">    translation settings, cursors created upon connections returned by this</FONT>
<FONT COLOR="#404040">    function will also use the datetime module.</FONT>
<FONT COLOR="#404040">    &quot;&quot;&quot;</FONT>
    con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(<FONT COLOR="#7d0000">*</FONT>args, <FONT COLOR="#7d0000">**</FONT>kwargs)

    con.<FONT COLOR="#000066">set_type_trans_in</FONT>({
        <FONT COLOR="#8f8c47">'DATE'</FONT>:             tc_dt.date_conv_in,
        <FONT COLOR="#8f8c47">'TIME'</FONT>:             tc_dt.time_conv_in,
        <FONT COLOR="#8f8c47">'TIMESTAMP'</FONT>:        tc_dt.timestamp_conv_in,
        })

    con.<FONT COLOR="#000066">set_type_trans_out</FONT>({
        <FONT COLOR="#8f8c47">'DATE'</FONT>:             tc_dt.date_conv_out,
        <FONT COLOR="#8f8c47">'TIME'</FONT>:             tc_dt.time_conv_out,
        <FONT COLOR="#8f8c47">'TIMESTAMP'</FONT>:        tc_dt.timestamp_conv_out,
        })

    <FONT COLOR="#0000ff">return</FONT> con


<FONT COLOR="#0000ff">def</FONT> <FONT COLOR="#000066">_test</FONT>():
    con <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>)
    cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

    <FONT COLOR="#32a532"># Retrieve the current timestamp of the database server.</FONT>
    cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select current_timestamp from rdb$database&quot;</FONT>)
    curStamp <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">fetchone</FONT>()[<FONT COLOR="#666600">0</FONT>]
    <FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'The type of curStamp is'</FONT>, <FONT COLOR="#4343b8">type</FONT>(curStamp)
    <FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'curStamp is'</FONT>, curStamp

    <FONT COLOR="#32a532"># Create a test table with a single TIMESTAMP column.</FONT>
    con.<FONT COLOR="#000066">execute_immediate</FONT>(<FONT COLOR="#8f8c47">&quot;recreate table test_stamp (a timestamp)&quot;</FONT>)
    con.<FONT COLOR="#000066">commit</FONT>()

    <FONT COLOR="#32a532"># Insert a timestamp into the database, then retrieve it.</FONT>
    py23StandardLibTimestamp <FONT COLOR="#7d0000">=</FONT> datetime.datetime.<FONT COLOR="#000066">now</FONT>()
    cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into test_stamp values (?)&quot;</FONT>, (py23StandardLibTimestamp,))
    cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from test_stamp&quot;</FONT>)
    curStamp <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">fetchone</FONT>()[<FONT COLOR="#666600">0</FONT>]
    <FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'The type of curStamp is'</FONT>, <FONT COLOR="#4343b8">type</FONT>(curStamp)
    <FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'curStamp is'</FONT>, curStamp


<FONT COLOR="#0000ff">if</FONT> <FONT COLOR="#0066a0">__name__</FONT> <FONT COLOR="#7d0000">==</FONT> <FONT COLOR="#8f8c47">'__main__'</FONT>:
    <FONT COLOR="#000066">_test</FONT>()
</pre>

<p class="textParagraph">
Sample output:
</p>
<pre class="programOutputBlock" style="margin-top: 0px; margin-bottom: 1.7em;">
The type of curStamp is &lt;type 'datetime.datetime'&gt;
curStamp is 2003-05-20 03:55:42
The type of stamp is &lt;type 'datetime.datetime'&gt;
stamp is 2003-05-20 03:55:42
</pre>

<br>

<a name="adv_param_conv_dynamic_type_translation_deferred_loading">
  <h4>Deferred Loading of Dynamic Type Translators, and
    <code>kinterbasdb.init</code> Convenience Codes
  </h4>
</a>

<p class="textParagraph">
KInterbasDB has existed since 1998, five years before the
<code>datetime</code> module was available in the Python standard library.
Therefore, KInterbasDB's default representation for date and time
values is the <code>mx.DateTime</code> module.  This representation is
recommended by the Python DB API 2.0 Specification, and was an entirely
sensible choice during the many years before the advent of the standard library
<code>datetime</code> module.
</p>

<p class="textParagraph">
Now that the <code>datetime</code> module is available in the standard
library, many KInterbasDB users prefer it to <code>mx.DateTime</code>.
For the sake of backward-compatibility, it is necessary to continue to use
<code>mx.DateTime</code> by default, but it's both burdensome and wasteful
to import <code>mx.DateTime</code> in programs that don't use it.
To address this situation, KInterbasDB's type translation
initialization code defers the choice of a default set of translators until the
<code>kinterbasdb.init</code> function is called.  A client program can
explicitly call <code>kinterbasdb.init</code> to forestall the import of
<code>mx.DateTime</code>.
</p>


<p class="textParagraph">
The <code>kinterbasdb.init</code> function takes a keyword argument
<code>type_conv</code>, which controls KInterbasDB's initial choice of
type translators.  <code>type_conv</code> can be either an integer or an
object that has all of the attributes named in
<code>kinterbasdb.BASELINE_TYPE_TRANSLATION_FACILITIES</code> (an example
of such an object is the module <code>kinterbasdb.typeconv_backcompat</code>).
If <code>type_conv</code> is an integer, it will cause KInterbasDB to use one
of the following predefined type translator configurations:
</p>

<style type="text/css">
td.num {
  font-size: 110%;
  text-align: right;
  vertical-align: top;
}

p.explanation {
  margin-top: 0em;
  margin-bottom: 0.4em;
}
</style>
<a name="adv_param_conv_dynamic_type_translation_deferred_loading_init_codes">
<table border="1" style="margin-left: 1em;">
<tr style="background-color: #EEEEEE; font-weight: bold; vertical-align: top;">
  <td>
    <a name="adv_param_conv_dynamic_type_translation_tbl_convenience_codes">
        <code>type_conv</code> integer "convenience code"
    </a>
  </td>
  <td>Resulting translator configuration</td>
</tr>
<tr>
  <td class="num">0</td>
  <td>
    <p class="explanation">
    Minimal type translators that represent
    date/time values as <code>tuple</code>s
    and fixed point values as either
        <code>float</code>s
        or scaled <code>int</code>egers,
        depending on the value of the deprecated
        <a href="#db_api_extensions_and_caveats_precision_mode"><code>Connection.precision_mode</code></a>
        attribute.
    </p>
    <p class="explanation">
    Unicode values are not encoded or decoded automatically.
    </p>
    <p class="explanation">
    Implemented by the <code>kinterbasdb.typeconv_naked</code> module.
    </p>
  </td>
</tr>
<tr>
  <td class="num">
    1
    <br><span style="font-size: 85%">(the default)</span>
  </td>
  <td>
    <p class="explanation">
    Backward-compatible type translators that represent
    date/time values via the <code>mx.DateTime</code> module
    and fixed point values as either
        <code>float</code>s
        or scaled <code>int</code>egers,
        depending on the value of the deprecated
        <a href="#db_api_extensions_and_caveats_precision_mode"><code>Connection.precision_mode</code></a>
        attribute.
    </p>
    <p class="explanation">
    Unicode values are not encoded or decoded automatically.
    </p>
    <p class="explanation">
    Implemented by the <code>kinterbasdb.typeconv_backcompat</code> module.
    </p>
    <p class="explanation">
    This configuration, which is the default, perfectly mimics
    the type translation behavior of KInterbasDB 3.0.
    </p>
  </td>
</tr>
<tr>
  <td class="num">100</td>
  <td>
    <p class="explanation">
    This translator configuration, which is intended for use with Python 2.3
    and later, represents
        date/time values via the standard library module <code>datetime</code>
        and fixed point values via the third-party
        <a href="#ref_fixedpoint"><code>fixedpoint</code></a>
        module.
    </p>
    <p class="explanation">
    Unicode values <em>are</em> encoded and decoded automatically
    (see <a href="#faq_fep_unicode">this FAQ</a> for more info).
    </p>
    <p class="explanation">
    Implemented by the <code>kinterbasdb.typeconv_23plus</code> module.
    </p>
  </td>
</tr>
<tr>
  <td class="num">
    200
    <br><span style="font-size: 85%">(the ideal)</span>
  </td>
  <td>
    <p class="explanation">
    This translator configuration represents
        date/time values via the standard library module <code>datetime</code>
        and
        fixed point values via the <code>decimal</code> module.
        The <code>decimal</code> module entered the standard library in
        Python 2.4, but can also be
        <a href="http://www.taniquetil.com.ar/facundo/bdvfiles/get_decimal.html">manually installed in Python 2.3</a>.
    </p>
    <p class="explanation">
    Unicode values <em>are</em> encoded and decoded automatically
    (see <a href="#faq_fep_unicode">this FAQ</a> for more info).
    </p>
    <p class="explanation">
    Implemented by the <code>kinterbasdb.typeconv_24plus</code> module.
    </p>
  </td>
</tr>
<tr>
  <td class="num">199</td>
  <td>
    <p class="explanation">
    This translator configuration is exactly like <code>200</code>, except
    that it represents fixed point values as <code>float</code> objects in
    order to avoid the substantial memory overhead of the <code>decimal</code>
    module.
    </p>
    <p class="explanation">
    <em>It is fundamentally imprecise to represent fixed point values in
    floating point</em>,
    so this convenience code is intended <em>solely</em> for users who
    wish to use <code>datetime</code> instead of <code>mx.DateTime</code>,
    but don't care about fixed point values and don't want to suffer the memory
    overhead of the <code>decimal</code> module.
    </p>
    <p class="explanation">
    Implemented by the <code>kinterbasdb.typeconv_23plus_lowmem</code> module.
    </p>
  </td>
</tr>
</table>
</a>

<p class="textParagraph">
These integer type conversion codes are defined <em>solely</em> for convenience.
The same functionality is available via the object variant of
<code>type_conv</code>, but setting it up is more laborious for typical
translator configurations.
</p>


<a name="adv_param_conv_dynamic_type_translation_deferred_loading_backcompat">
  <h4 style="margin-left: 1em;">Deferred Loading: Backward Compatibility Issues</h4>
</a>

<p class="textParagraph">
The deferred type translator loading scheme introduced in KInterbasDB 3.1
goes to great lengths to maintain backward compatibility.
If the client programmer does not call <code>kinterbasdb.init</code>,
KInterbasDB will implicitly initialize itself in a backward-compatible manner
(<code>type_conv=1</code>) the first time one of its public functions is called
or one of its public classes is instantiated.
</p>

<p class="textParagraph">
The only known backward incompatibility is this:
the DB API type comparison singleton <code>DATETIME</code> will not compare
equal to any type until the <code>kinterbasdb.init</code> function has been
called (whether explicitly or implicitly).
After <code>kinterbasdb.init</code> has been called, <code>DATETIME</code>
will compare equal to the date, time, and timestamp types that were loaded.
</p>

<p class="textParagraph">
This issue should affect hardly any existing KInterbasDB-based programs.
</p>


<a name="adv_param_conv_dynamic_type_translation_deferred_loading_example">
  <h4 style="margin-left: 1em;">Deferred Loading: Example Program That Uses <code>kinterbasdb.init</code></h4>
</a>

<pre class="codeBlock"><FONT COLOR="#1413be">import</FONT> datetime, decimal, os.path, string, sys

<FONT COLOR="#1413be">import</FONT> kinterbasdb
kinterbasdb.<FONT COLOR="#000066">init</FONT>(type_conv<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#666600">200</FONT>)
<FONT COLOR="#65c265"># This program never imports mx.DateTime:</FONT>
<FONT COLOR="#1413be">assert</FONT> <FONT COLOR="#8f8c47">'mx'</FONT> <FONT COLOR="#1413be">not</FONT> <FONT COLOR="#1413be">in</FONT> sys.modules

<FONT COLOR="#1413be">def</FONT> <FONT COLOR="#000066">test</FONT>():
    dbFilename <FONT COLOR="#7d0000">=</FONT> r<FONT COLOR="#8f8c47">'D:\temp\test-deferred.firebird'</FONT>
    <FONT COLOR="#000066">prepareTestDatabase</FONT>(dbFilename)

    <FONT COLOR="#65c265"># Connect with character set UNICODE_FSS, to match the default character</FONT>
    <FONT COLOR="#65c265"># set of the test database.</FONT>
    con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>dbFilename,
        user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>, charset<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'UNICODE_FSS'</FONT>
      )
    cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

    <FONT COLOR="#65c265"># Create a test table.</FONT>
    cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;&quot;&quot;</FONT>
<FONT COLOR="#8f8c47">        create table test (</FONT>
<FONT COLOR="#8f8c47">          a numeric(18,2),</FONT>
<FONT COLOR="#8f8c47">          b date,</FONT>
<FONT COLOR="#8f8c47">          c time,</FONT>
<FONT COLOR="#8f8c47">          d timestamp,</FONT>
<FONT COLOR="#8f8c47">          e varchar(50), /* Defaults to character set UNICODE_FSS. */</FONT>
<FONT COLOR="#8f8c47">          f varchar(50), /* Defaults to character set UNICODE_FSS. */</FONT>
<FONT COLOR="#8f8c47">          g varchar(50) character set ASCII</FONT>
<FONT COLOR="#8f8c47">        )</FONT>
<FONT COLOR="#8f8c47">      &quot;&quot;&quot;</FONT>)
    con.<FONT COLOR="#000066">commit</FONT>()

    <FONT COLOR="#65c265"># Create an input value for each field in the test table.</FONT>
    aIn <FONT COLOR="#7d0000">=</FONT> decimal.<FONT COLOR="#000066">Decimal</FONT>(<FONT COLOR="#8f8c47">'4.53'</FONT>)

    <FONT COLOR="#65c265"># Notice that the DB API date/time constructors in kinterbasdb generate</FONT>
    <FONT COLOR="#65c265"># datetime-based objects instead of mx-based objects because of our earlier</FONT>
    <FONT COLOR="#65c265"># call to kinterbasdb.init(type_conv=200).</FONT>
    bIn <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">Date</FONT>(<FONT COLOR="#666600">2004</FONT>,<FONT COLOR="#666600">1</FONT>,<FONT COLOR="#666600">4</FONT>)
    <FONT COLOR="#1413be">assert</FONT> <FONT COLOR="#7d7ca4">isinstance</FONT>(bIn, datetime.date)
    cIn <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">Time</FONT>(<FONT COLOR="#666600">16</FONT>,<FONT COLOR="#666600">27</FONT>,<FONT COLOR="#666600">59</FONT>)
    <FONT COLOR="#1413be">assert</FONT> <FONT COLOR="#7d7ca4">isinstance</FONT>(cIn, datetime.time)
    dIn <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">Timestamp</FONT>(<FONT COLOR="#666600">2004</FONT>,<FONT COLOR="#666600">1</FONT>,<FONT COLOR="#666600">4</FONT>, <FONT COLOR="#666600">16</FONT>,<FONT COLOR="#666600">27</FONT>,<FONT COLOR="#666600">59</FONT>)
    <FONT COLOR="#1413be">assert</FONT> <FONT COLOR="#7d7ca4">isinstance</FONT>(dIn, datetime.datetime)

    eIn <FONT COLOR="#7d0000">=</FONT> u<FONT COLOR="#8f8c47">'A unicod\u2211 object stored in a Unicode field.'</FONT>
    fIn <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#8f8c47">'A str object stored in a Unicode field.'</FONT>
    gIn <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#8f8c47">'A str object stored in an ASCII field.'</FONT>

    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'-'</FONT> <FONT COLOR="#7d0000">*</FONT> <FONT COLOR="#666600">70</FONT>
    inputValues <FONT COLOR="#7d0000">=</FONT> (aIn, bIn, cIn, dIn, eIn, fIn, gIn)
    <FONT COLOR="#000066">reportValues</FONT>(<FONT COLOR="#8f8c47">'In'</FONT>, inputValues)
    cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into test values (?,?,?,?,?,?,?)&quot;</FONT>, inputValues)
    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'-'</FONT> <FONT COLOR="#7d0000">*</FONT> <FONT COLOR="#666600">70</FONT>
    cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select a,b,c,d,e,f,g from test&quot;</FONT>)
    (aOut, bOut, cOut, dOut, eOut, fOut, gOut) <FONT COLOR="#7d0000">=</FONT> outputValues <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">fetchone</FONT>()
    <FONT COLOR="#000066">reportValues</FONT>(<FONT COLOR="#8f8c47">'Out'</FONT>, outputValues)
    <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'-'</FONT> <FONT COLOR="#7d0000">*</FONT> <FONT COLOR="#666600">70</FONT>

    <FONT COLOR="#65c265"># Notice that all values made the journey to and from the database intact.</FONT>
    <FONT COLOR="#1413be">assert</FONT> inputValues <FONT COLOR="#7d0000">==</FONT> outputValues

<FONT COLOR="#1413be">def</FONT> <FONT COLOR="#000066">reportValues</FONT>(direction, values):
    <FONT COLOR="#1413be">for</FONT> (val, c) <FONT COLOR="#1413be">in</FONT> <FONT COLOR="#7d7ca4">zip</FONT>(values, string.ascii_lowercase[:<FONT COLOR="#7d7ca4">len</FONT>(values)]):
        varName <FONT COLOR="#7d0000">=</FONT> c <FONT COLOR="#7d0000">+</FONT> direction
        <FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'%s has type %s, value\n  %s'</FONT> <FONT COLOR="#7d0000">%</FONT> (varName, <FONT COLOR="#7d7ca4">type</FONT>(val), <FONT COLOR="#7d7ca4">repr</FONT>(val))

<FONT COLOR="#1413be">def</FONT> <FONT COLOR="#000066">prepareTestDatabase</FONT>(dbFilename):
    <FONT COLOR="#65c265"># Delete the test database if an old copy is already present.</FONT>
    <FONT COLOR="#1413be">if</FONT> os.path.<FONT COLOR="#000066">isfile</FONT>(dbFilename):
        conOld <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>dbFilename,
            user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>
          )
        conOld.<FONT COLOR="#000066">drop_database</FONT>()
    <FONT COLOR="#65c265"># Create the test database afresh.</FONT>
    kinterbasdb.<FONT COLOR="#000066">create_database</FONT>(<FONT COLOR="#8f8c47">&quot;&quot;&quot;</FONT>
<FONT COLOR="#8f8c47">          create database '%s'</FONT>
<FONT COLOR="#8f8c47">          user 'sysdba' password 'masterkey'</FONT>
<FONT COLOR="#8f8c47">          default character set UNICODE_FSS</FONT>
<FONT COLOR="#8f8c47">        &quot;&quot;&quot;</FONT> <FONT COLOR="#7d0000">%</FONT> dbFilename
      )

<FONT COLOR="#1413be">if</FONT> <FONT COLOR="#007ea0">__name__</FONT> <FONT COLOR="#7d0000">==</FONT> <FONT COLOR="#8f8c47">'__main__'</FONT>:
    <FONT COLOR="#000066">test</FONT>()
</pre>

<p class="textParagraph">
Program output:
</p>
<pre class="programOutputBlock">
----------------------------------------------------------------------
aIn has type &lt;class 'decimal.Decimal'&gt;, value
  Decimal("4.53")
bIn has type &lt;type 'datetime.date'&gt;, value
  datetime.date(2004, 1, 4)
cIn has type &lt;type 'datetime.time'&gt;, value
  datetime.time(16, 27, 59)
dIn has type &lt;type 'datetime.datetime'&gt;, value
  datetime.datetime(2004, 1, 4, 16, 27, 59)
eIn has type &lt;type 'unicode'&gt;, value
  u'A unicod\u2211 object stored in a Unicode field.'
fIn has type &lt;type 'str'&gt;, value
  'A str object stored in a Unicode field.'
gIn has type &lt;type 'str'&gt;, value
  'A str object stored in an ASCII field.'
----------------------------------------------------------------------
aOut has type &lt;class 'decimal.Decimal'&gt;, value
  Decimal("4.53")
bOut has type &lt;type 'datetime.date'&gt;, value
  datetime.date(2004, 1, 4)
cOut has type &lt;type 'datetime.time'&gt;, value
  datetime.time(16, 27, 59)
dOut has type &lt;type 'datetime.datetime'&gt;, value
  datetime.datetime(2004, 1, 4, 16, 27, 59)
eOut has type &lt;type 'unicode'&gt;, value
  u'A unicod\u2211 object stored in a Unicode field.'
fOut has type &lt;type 'unicode'&gt;, value
  u'A str object stored in a Unicode field.'
gOut has type &lt;type 'unicode'&gt;, value
  u'A str object stored in an ASCII field.'
----------------------------------------------------------------------
</pre>

<p class="textParagraph">
<em>Notes about Unicode handling in the example above:</em>
</p>

<p class="textParagraph">
Upon input, the Python <code>unicode</code> object <code>eIn</code> was
transparently encoded for storage in database field <code>TEST.E</code> (a
<code>VARCHAR</code> field with character set <code>UNICODE_FSS</code> (that
is, UTF-8)).  Upon output, the <code>UNICODE_FSS</code> value in
<code>TEST.E</code> was decoded transparently into the Python
<code>unicode</code> object <code>eOut</code>.
</p>

<p class="textParagraph">
<code>TEST.F</code> accepted a Python <code>str</code> object even though
it's a Unicode field.  The output value <code>fOut</code> is a Python
<code>unicode</code> object rather than a <code>str</code>.
</p>

<p class="textParagraph">
Although <code>TEST.G</code> is an <code>ASCII</code> field, and the input
value <code>gIn</code> is a <code>str</code>, the output value
<code>gOut</code> is a <code>unicode</code> object.
This is because the connection's charset is <code>UNICODE_FSS</code>,
and Firebird tries to convert every retrieved value to match that character
set.
</p>

<br>

<a name="adv_param_conv_dynamic_type_translation_positional">
  <h4>Positional Dymanic Type Translation</h4>
</a>

<p class="textParagraph">
All forms of dynamic type translation discussed so far have used the
<em>type</em> of the database field as the basis for selecting a
translator.
KInterbasDB 3.2 also allows the client programmer to control translator
selection on the basis of a field's <em>position</em> within a
<code>Cursor</code>.
Translator selection based on database field type is called
"<em>typal</em> translation", while selection based on position
is called "<em>positional</em> translation".
</p>

<p class="textParagraph">
Positional translation can be enabled at the <code>Cursor</code>
level by including zero-based integer keys in the dictionary passed to
<code>Cursor.set_type_trans[in|out]</code>.  Consider the following example
program:
</p>

<pre class="codeBlock">
<FONT COLOR="#1413be">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>r<FONT COLOR="#8f8c47">'D:\temp\test-20.firebird'</FONT>,
    user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>
  )
cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;recreate table test(a int, b int, c int, d int, e float)&quot;</FONT>)
con.<FONT COLOR="#000066">commit</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into test values (?,?,?,?,?)&quot;</FONT>, (<FONT COLOR="#666600">1</FONT>, <FONT COLOR="#666600">2</FONT>, <FONT COLOR="#666600">3</FONT>, <FONT COLOR="#666600">4</FONT>, <FONT COLOR="#666600">5</FONT>.<FONT COLOR="#666600">0</FONT>))

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select a,b,c,d,e from test&quot;</FONT>)
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'Before translator modifications, output row is:'</FONT>
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">' '</FONT>, cur.<FONT COLOR="#000066">fetchone</FONT>()

cur.<FONT COLOR="#000066">set_type_trans_out</FONT>({
    <FONT COLOR="#8f8c47">'INTEGER'</FONT>:  <FONT COLOR="#1413be">lambda</FONT> i: i <FONT COLOR="#7d0000">*</FONT> <FONT COLOR="#666600">10</FONT>,
    <FONT COLOR="#666600">1</FONT>:          <FONT COLOR="#1413be">lambda</FONT> i: i <FONT COLOR="#7d0000">*</FONT> <FONT COLOR="#666600">100</FONT>,
    <FONT COLOR="#666600">3</FONT>:          <FONT COLOR="#1413be">lambda</FONT> i: i <FONT COLOR="#7d0000">*</FONT> <FONT COLOR="#666600">1000</FONT>
  })

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select a,b,c,d,e from test&quot;</FONT>)
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'After translator modifications, output row is:'</FONT>
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">' '</FONT>, cur.<FONT COLOR="#000066">fetchone</FONT>()
</pre>

<p class="textParagraphTight">
Program output:
</p>
<pre class="programOutputBlock" style="margin-top: 0em; margin-bottom: 0em;">
Before translator modifications, output row is:
  (1, 2, 3, 4, 5.0)
After translator modifications, output row is:
  (10, 200, 30, 4000, 5.0)
</pre>

<p class="textParagraph">
The <code>cur.set_type_trans_out</code> call in the example program specifies
that integer values retrieved by <code>cur</code> should be multiplied by
<code>10</code>,
then overrides that setting for specific columns:
the value in the second column (position <code>1</code>) is multiplied
by <code>100</code>,
while the value in the fourth column (position <code>3</code>) is multiplied
by <code>1000</code>.
</p>

<p class="textParagraph" style="margin-top: 0.9em;">
KInterbasDB uses a cascading method of translator selection, listed below in
order from highest to lowest precedence:
</p>
<ul style="margin-top: 0em;">
  <li class="liShort">
    <em>Positional</em> translation settings, which can only be activated at
    the <code>Cursor</code> level, take precedence over <em>typal</em>
    translation settings.
  </li>
  <li class="liShort">
    <code>Cursor</code>-level translation settings take precedence over
    <code>Connection</code>-level settings.
  </li>
  <li class="liShort">
    <code>Connection</code>-level translation settings take precedence over
    the module-level defaults.
  </li>
  <li>
    The module-level defaults are established by
    <a href="#adv_param_conv_dynamic_type_translation_deferred_loading">the call to <code>kinterbasdb.init</code></a>.
    If the client programmer does not call <code>kinterbasdb.init</code>
    explicitly, KInterbasDB's internals will do so implicitly.
  </li>
</ul>

<br>


<a name="adv_param_conv_database_arrays">
  <h3>Database Arrays</h3>
</a>

<p class="textParagraph">
KInterbasDB converts database arrays <em>from</em> Python sequences (except
strings) on input; <em>to</em> Python lists on output.
On input, the Python sequence must be nested appropriately if the array
field is multi-dimensional, and the incoming sequence must not
fall short of its maximum possible length (it will not be "padded"
implicitly--see below).
On output, the lists will be nested if the database array has multiple dimensions.
</p>

<p class="textParagraph">
Database arrays have no place in a purely relational data model, which
requires that data values be <em>atomized</em> (that is, every value stored
in the database must be reduced to elementary, non-decomposable parts).
The Interbase&reg;/Firebird implementation of database arrays,
like that of most relational database engines that support this data type,
is fraught with limitations.
</p>

<p class="textParagraph">
First of all, the database engine claims to support up to 16 dimensions, but
actually malfunctions catastrophically above 10 (this bug is fixed in
Firebird 1.5-RC1 and later, thanks to Dmitry Yemanov).
</p>

<p class="textParagraph">
Database arrays are of fixed size, with a predeclared number of dimensions
and number of elements per dimension.  Individual array elements cannot
be set to <code>NULL</code>/<code>None</code>,
so the mapping between Python lists (which have dynamic length and are
therefore <em>not</em> normally "padded" with dummy values) and non-trivial
database arrays is clumsy.
</p>

<p class="textParagraph">
Stored procedures cannot have array parameters.
</p>

<p class="textParagraph">
Finally, many interface libraries, GUIs, and even the isql command line
utility do not support database arrays.
</p>

<p class="textParagraph">
In general, it is preferable to avoid using database arrays unless you
have a compelling reason.
</p>

<a name="adv_param_conv_database_arrays_example">
  <h4>Example Program</h4>
</a>

<p class="textParagraph">
The following program inserts an array (nested Python list) into a
single database field, then retrieves it.
</p>

<pre class="codeBlock" style="margin-top: 0px;">
<FONT COLOR="#0000ff">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>)
con.<FONT COLOR="#000066">execute_immediate</FONT>(<FONT COLOR="#8f8c47">&quot;recreate table array_table (a int[3,4])&quot;</FONT>)
con.<FONT COLOR="#000066">commit</FONT>()

cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

arrayIn <FONT COLOR="#7d0000">=</FONT> [
    [<FONT COLOR="#666600">1</FONT>, <FONT COLOR="#666600">2</FONT>, <FONT COLOR="#666600">3</FONT>, <FONT COLOR="#666600">4</FONT>],
    [<FONT COLOR="#666600">5</FONT>, <FONT COLOR="#666600">6</FONT>, <FONT COLOR="#666600">7</FONT>, <FONT COLOR="#666600">8</FONT>],
    [<FONT COLOR="#666600">9</FONT>,<FONT COLOR="#666600">10</FONT>,<FONT COLOR="#666600">11</FONT>,<FONT COLOR="#666600">12</FONT>]
  ]

<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'arrayIn:  %s'</FONT> <FONT COLOR="#7d0000">%</FONT> arrayIn
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into array_table values (?)&quot;</FONT>, (arrayIn,))

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select a from array_table&quot;</FONT>)
arrayOut <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">fetchone</FONT>()[<FONT COLOR="#666600">0</FONT>]
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'arrayOut: %s'</FONT> <FONT COLOR="#7d0000">%</FONT> arrayOut

con.<FONT COLOR="#000066">commit</FONT>()
</pre>

<p class="textParagraph">
Output:
</p>
<pre class="programOutputBlock" style="margin-top: 0px;">
arrayIn:  [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
arrayOut: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
</pre>
<br>


<a name="adv_param_conv_blobs">
  <h3>Blobs</h3>
</a>

<p class="textParagraph">
KInterbasDB supports the insertion and retrieval of blobs either
wholly in memory ("materialized mode") or in chunks ("streaming mode") to
reduce memory usage when handling large blobs.  The default handling mode is
"materialized"; the "streaming" method is selectable via a special case of
Dynamic Type Translation.
</p>

<p class="textParagraph">
In <em>materialized</em> mode, input and output blobs are represented as Python
<code>str</code> objects,
with the result that the entirety of each blob's contents is loaded into
memory.
Unfortunately, flaws in the database engine's C API
<a href="http://sourceforge.net/forum/forum.php?thread_id=1299756&forum_id=30917">prevent</a>
automatic Unicode conversion from applying to textual blobs in the way it
applies to Unicode <code>CHAR</code> and <code>VARCHAR</code> fields.
</p>

<p class="textParagraph">
In <em>streaming</em> mode, any Python "file-like" object is acceptable as
input for a blob parameter.  Obvious examples of such objects are instances of
<code>file</code> or <code>StringIO</code>.
Each output blob is represented by a <code>kinterbasdb.BlobReader</code>
object.  <code>BlobReader</code> itself is a "file-like" class, so it acts
much like a <code>file</code> instance opened in <code>rb</code> mode.
</p>

<p class="textParagraph">
<code>BlobReader</code> adds one method not found in the "file-like"
interface:  the <code>chunks</code> method.  <code>BlobReader.chunks</code>
takes a single integer parameter that specifies the number of bytes to retrieve
in each chunk (the final chunk may be smaller).
For example, if the size of the blob is <code>50000000</code> bytes,
<code>BlobReader.chunks(2**20)</code> will return <code>47</code>
one-megabyte chunks, and a smaller final chunk of <code>716928</code> bytes.
</p>

<p class="textParagraph">
Due to the combination of CPython's deterministic finalization with careful
programming in KInterbasDB's internals, it is not strictly necessary to close
<code>BlobReader</code> instances explicitly.  A <code>BlobReader</code>
object will be automatically closed by its <code>__del__</code> method when it
goes out of scope, or when its <code>Connection</code> closes, whichever comes
first.  However, it is always a better idea to close resources explicitly
(via <code>try...finally</code>) than to rely on artifacts of the CPython
implementation.  (For the sake of clarity, the example program does not follow
this practice.)
</p>

<a name="adv_param_conv_blobs_example">
  <h4>Example Program</h4>
</a>

<p class="textParagraph">
The following program demonstrates blob storage and retrieval in both
<em>materialized</em> and <em>streaming</em> modes.
</p>

<pre class="codeBlock">
<FONT COLOR="#0000ff">import</FONT> os.path
<FONT COLOR="#0000ff">from</FONT> cStringIO <FONT COLOR="#0000ff">import</FONT> StringIO

<FONT COLOR="#0000ff">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>r<FONT COLOR="#8f8c47">'localhost:D:\temp\test-20.firebird'</FONT>,
    user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>
  )

cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;recreate table blob_test (a blob)&quot;</FONT>)
con.<FONT COLOR="#000066">commit</FONT>()

<FONT COLOR="#32a532"># --- Materialized mode (str objects for both input and output) ---</FONT>
<FONT COLOR="#32a532"># Insertion:</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into blob_test values (?)&quot;</FONT>, (<FONT COLOR="#8f8c47">'abcdef'</FONT>,))
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into blob_test values (?)&quot;</FONT>, (<FONT COLOR="#8f8c47">'ghijklmnop'</FONT>,))
<FONT COLOR="#32a532"># Retrieval:</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from blob_test&quot;</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'Materialized retrieval (as str):'</FONT>
<FONT COLOR="#0000ff">print</FONT> cur.<FONT COLOR="#000066">fetchall</FONT>()

cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;delete from blob_test&quot;</FONT>)

<FONT COLOR="#32a532"># --- Streaming mode (file-like objects for input; kinterbasdb.BlobReader</FONT>
<FONT COLOR="#32a532">#     objects for output) ---</FONT>
cur.set_type_trans_in ({<FONT COLOR="#8f8c47">'BLOB'</FONT>: {<FONT COLOR="#8f8c47">'mode'</FONT>: <FONT COLOR="#8f8c47">'stream'</FONT>}})
cur.<FONT COLOR="#000066">set_type_trans_out</FONT>({<FONT COLOR="#8f8c47">'BLOB'</FONT>: {<FONT COLOR="#8f8c47">'mode'</FONT>: <FONT COLOR="#8f8c47">'stream'</FONT>}})

<FONT COLOR="#32a532"># Insertion:</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into blob_test values (?)&quot;</FONT>, (<FONT COLOR="#000066">StringIO</FONT>(<FONT COLOR="#8f8c47">'abcdef'</FONT>),))
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into blob_test values (?)&quot;</FONT>, (<FONT COLOR="#000066">StringIO</FONT>(<FONT COLOR="#8f8c47">'ghijklmnop'</FONT>),))

f <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#4343b8">file</FONT>(os.path.<FONT COLOR="#000066">abspath</FONT>(<FONT COLOR="#0066a0">__file__</FONT>), <FONT COLOR="#8f8c47">'rb'</FONT>)
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into blob_test values (?)&quot;</FONT>, (f,))
f.<FONT COLOR="#000066">close</FONT>()

<FONT COLOR="#32a532"># Retrieval using the &quot;file-like&quot; methods of BlobReader:</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select * from blob_test&quot;</FONT>)

readerA <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">fetchone</FONT>()[<FONT COLOR="#666600">0</FONT>]

<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'\nStreaming retrieval (via kinterbasdb.BlobReader):'</FONT>

<FONT COLOR="#32a532"># Python &quot;file-like&quot; interface:</FONT>
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'readerA.mode:    &quot;%s&quot;'</FONT> <FONT COLOR="#7d0000">%</FONT> readerA.mode
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'readerA.closed:   %s'</FONT>  <FONT COLOR="#7d0000">%</FONT> readerA.closed
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'readerA.tell():   %d'</FONT>  <FONT COLOR="#7d0000">%</FONT> readerA.<FONT COLOR="#000066">tell</FONT>()
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'readerA.read(2): &quot;%s&quot;'</FONT> <FONT COLOR="#7d0000">%</FONT> readerA.<FONT COLOR="#000066">read</FONT>(<FONT COLOR="#666600">2</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'readerA.tell():   %d'</FONT>  <FONT COLOR="#7d0000">%</FONT> readerA.<FONT COLOR="#000066">tell</FONT>()
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'readerA.read():  &quot;%s&quot;'</FONT> <FONT COLOR="#7d0000">%</FONT> readerA.<FONT COLOR="#000066">read</FONT>()
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'readerA.tell():   %d'</FONT>  <FONT COLOR="#7d0000">%</FONT> readerA.<FONT COLOR="#000066">tell</FONT>()
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'readerA.read():  &quot;%s&quot;'</FONT> <FONT COLOR="#7d0000">%</FONT> readerA.<FONT COLOR="#000066">read</FONT>()
readerA.<FONT COLOR="#000066">close</FONT>()
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'readerA.closed:   %s'</FONT>  <FONT COLOR="#7d0000">%</FONT> readerA.closed

<FONT COLOR="#32a532"># The chunks method (not part of the Python &quot;file-like&quot; interface, but handy):</FONT>
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'\nFor a blob with contents &quot;ghijklmnop&quot;, iterating over'</FONT>
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'BlobReader.chunks(3) produces:'</FONT>
readerB <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">fetchone</FONT>()[<FONT COLOR="#666600">0</FONT>]
<FONT COLOR="#0000ff">for</FONT> chunkNo, chunk <FONT COLOR="#0000ff">in</FONT> <FONT COLOR="#000066">enumerate</FONT>(readerB.<FONT COLOR="#000066">chunks</FONT>(<FONT COLOR="#666600">3</FONT>)):
    <FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'Chunk %d is: &quot;%s&quot;'</FONT> <FONT COLOR="#7d0000">%</FONT> (chunkNo, chunk)
</pre>


<p class="textParagraph">
Output:
</p>
<pre class="programOutputBlock" style="margin-top: 0px;">
Materialized retrieval (as str):
[('abcdef',), ('ghijklmnop',)]

Streaming retrieval (via kinterbasdb.BlobReader):
readerA.mode:    "rb"
readerA.closed:   False
readerA.tell():   0
readerA.read(2): "ab"
readerA.tell():   2
readerA.read():  "cdef"
readerA.tell():   6
readerA.read():  ""
readerA.closed:   True

For a blob with contents "ghijklmnop", iterating over
BlobReader.chunks(3) produces:
Chunk 0 is: "ghi"
Chunk 1 is: "jkl"
Chunk 2 is: "mno"
Chunk 3 is: "p"
</pre>


<br>
<hr>
<br>


<a name="adv_prepared_statements">
  <h2>Prepared Statements</h2>
</a>

<p class="textParagraph">
When you define a Python function, the interpreter initially parses the
textual representation of the function and generates a binary equivalent called
bytecode.  The bytecode representation can then be executed
directly by the Python interpreter any number of times and with a variety of
parameters, but the human-oriented textual definition of the function never
need be parsed again.
</p>

<p class="textParagraph">
Database engines perform a similar series of steps when executing a SQL
statement.  Consider the following series of statements:
</p>
<pre class="codeBlock" style="margin-top: 0em;">
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into the_table (a,b,c) values ('aardvark', 1, 0.1)&quot;</FONT>)
...
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into the_table (a,b,c) values ('zymurgy', 2147483647, 99999.999)&quot;</FONT>)
</pre>

<p class="textParagraph">
If there are many statements in that series, wouldn't it make sense to "define
a function" to insert the provided "parameters" into the predetermined fields
of the predetermined table, instead of forcing the database engine to parse each
statement anew and figure out what database entities the elements of the
statement refer to?  In other words, why not take advantage of the fact that
the form of the statement ("the function") stays the same throughout, and only
the values ("the parameters") vary?  Prepared statements deliver that
performance benefit and other advantages as well.
</p>

<p class="textParagraph">
The following code is semantically equivalent to the series of insert
operations discussed
previously, except that it uses a single SQL statement that contains Firebird's
parameter marker (<code>?</code>) in the slots where values are expected, then
supplies those values as Python tuples instead of constructing a textual
representation of each value and passing it to the database engine for parsing:
</p>
<pre class="codeBlockTight">
insertStatement <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#8f8c47">&quot;insert into the_table (a,b,c) values (?,?,?)&quot;</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(insertStatement, (<FONT COLOR="#8f8c47">'aardvark'</FONT>, <FONT COLOR="#666600">1</FONT>, <FONT COLOR="#666600">0</FONT>.<FONT COLOR="#666600">1</FONT>))
...
cur.<FONT COLOR="#000066">execute</FONT>(insertStatement, (<FONT COLOR="#8f8c47">'zymurgy'</FONT>, <FONT COLOR="#666600">2147483647</FONT>, <FONT COLOR="#666600">99999</FONT>.<FONT COLOR="#666600">999</FONT>))
</pre>

<p class="textParagraphNoTop">
Only the values change as each row is inserted; the statement remains the same.
For many years, KInterbasDB has recognized situations similar to this one and
automatically reused the same prepared statement in each
<code>Cursor.execute</code> call.  In KInterbasDB 3.2, the scheme for
automatically reusing prepared statements has become more sophisticated,
and the API has been extended to offer the client programmer manual control
over prepared statement creation and use.
</p>

<p class="textParagraph">
The entry point for manual statement preparation is the
<code>Cursor.prep</code> method, which takes a single string parameter that
contains the SQL statement to be prepared, and returns a
<code>kinterbasdb.PreparedStatement</code> object.
<code>PreparedStatement</code> has no public methods, but does have the
following public read-only properties:
</p>

<ul>
  <li class="liShort"><code>sql</code>: &nbsp;
    A reference to the string that was passed to
    <code>Cursor.prep</code> to create this <code>PreparedStatement</code>.
  </li>
  <li class="liShort"><code>statement_type</code>: &nbsp;
    An integer code that can be matched against the statement type constants
    in the <code>kinterbasdb.isc_info_sql_stmt_*</code> series.
    The following statement type codes are currently available:
    <code>
    <ul>
      <li>isc_info_sql_stmt_commit</li>
      <li>isc_info_sql_stmt_ddl</li>
      <li>isc_info_sql_stmt_delete</li>
      <li>isc_info_sql_stmt_exec_procedure</li>
      <li>isc_info_sql_stmt_get_segment</li>
      <li>isc_info_sql_stmt_insert</li>
      <li>isc_info_sql_stmt_put_segment</li>
      <li>isc_info_sql_stmt_rollback</li>
      <li>isc_info_sql_stmt_savepoint</li>
      <li>isc_info_sql_stmt_select</li>
      <li>isc_info_sql_stmt_select_for_upd</li>
      <li>isc_info_sql_stmt_set_generator</li>
      <li>isc_info_sql_stmt_start_trans</li>
      <li>isc_info_sql_stmt_update</li>
    </ul>
    </code>
  </li>
  <li class="liShort"><code>n_input_params</code>: &nbsp;
    The number of input parameters the statement requires.
  </li>
  <li class="liShort"><code>n_output_params</code>: &nbsp;
    The number of output fields the statement produces.
  </li>
  <li class="liShort"><code>plan</code>: &nbsp;
    A string representation of the execution plan generated for this statement
    by the database engine's optimizer.  This property can be used, for
    example, to verify that a statement is using the expected index.
  </li>
  <li class="liShort">
    <a name="PreparedStatement_description">
    <code>description</code>: &nbsp;
    A Python DB API 2.0 description sequence (of the same format as
    <a href="#Cursor_description">Cursor.description</a>) that describes the
    statement's output parameters.  Statements without output parameters have
    a <code>description</code> of <code>None</code>.
    <br>
    For more information, see the notes about
    <a href="#Cursor_description">Cursor.description</a> elsewhere in this
    document.
    </a>
  </li>
</ul>

<p class="textParagraph">
In addition to programmatically examining the characteristics of a SQL statement
via the properties of <code>PreparedStatement</code>, the client programmer
can submit a <code>PreparedStatement</code> to
<code>Cursor.execute</code> or <code>Cursor.executemany</code>
for execution.
The code snippet below is semantically equivalent to both of the previous
snippets in this section, but it explicitly prepares the <code>INSERT</code>
statement in advance, then submits it to <code>Cursor.executemany</code> for
execution:
</p>
<pre class="codeBlockTight">
insertStatement <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">prep</FONT>(<FONT COLOR="#8f8c47">&quot;insert into the_table (a,b,c) values (?,?,?)&quot;</FONT>)
inputRows <FONT COLOR="#7d0000">=</FONT> [
    (<FONT COLOR="#8f8c47">'aardvark'</FONT>, <FONT COLOR="#666600">1</FONT>, <FONT COLOR="#666600">0</FONT>.<FONT COLOR="#666600">1</FONT>),
    ...
    (<FONT COLOR="#8f8c47">'zymurgy'</FONT>, <FONT COLOR="#666600">2147483647</FONT>, <FONT COLOR="#666600">99999</FONT>.<FONT COLOR="#666600">999</FONT>)
  ]
cur.<FONT COLOR="#000066">executemany</FONT>(insertStatement, inputRows)
</pre>


<a name="adv_prepared_statements_example">
  <h4>Example Program</h4>
</a>

<p class="textParagraph">
The following program demonstrates the explicit use of
<code>PreparedStatement</code>s.  It also benchmarks explicit
<code>PreparedStatement</code> reuse against KInterbasDB's automatic
<code>PreparedStatement</code> reuse, and against an input strategy that
prevents <code>PreparedStatement</code> reuse.
</p>

<pre class="codeBlock">
<FONT COLOR="#1413be">import</FONT> time

<FONT COLOR="#1413be">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>r<FONT COLOR="#8f8c47">'localhost:D:\temp\test-20.firebird'</FONT>,
    user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>
  )

cur <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

<FONT COLOR="#65c265"># Create supporting database entities:</FONT>
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;recreate table t (a int, b varchar(50))&quot;</FONT>)
con.<FONT COLOR="#000066">commit</FONT>()
cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;create unique index unique_t_a on t(a)&quot;</FONT>)
con.<FONT COLOR="#000066">commit</FONT>()

<FONT COLOR="#65c265"># Explicitly prepare the insert statement:</FONT>
psIns <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">prep</FONT>(<FONT COLOR="#8f8c47">&quot;insert into t (a,b) values (?,?)&quot;</FONT>)
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'psIns.sql: &quot;%s&quot;'</FONT> <FONT COLOR="#7d0000">%</FONT> psIns.sql
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'psIns.statement_type == kinterbasdb.isc_info_sql_stmt_insert:'</FONT>, (
    psIns.statement_type <FONT COLOR="#7d0000">==</FONT> kinterbasdb.isc_info_sql_stmt_insert
  )
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'psIns.n_input_params: %d'</FONT> <FONT COLOR="#7d0000">%</FONT> psIns.n_input_params
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'psIns.n_output_params: %d'</FONT> <FONT COLOR="#7d0000">%</FONT> psIns.n_output_params
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'psIns.plan: %s'</FONT> <FONT COLOR="#7d0000">%</FONT> psIns.plan

<FONT COLOR="#1413be">print</FONT>

N <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#666600">10000</FONT>
iStart <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#666600">0</FONT>

<FONT COLOR="#65c265"># The client programmer uses a PreparedStatement explicitly:</FONT>
startTime <FONT COLOR="#7d0000">=</FONT> time.<FONT COLOR="#000066">time</FONT>()
<FONT COLOR="#1413be">for</FONT> i <FONT COLOR="#1413be">in</FONT> <FONT COLOR="#7d7ca4">xrange</FONT>(iStart, iStart <FONT COLOR="#7d0000">+</FONT> N):
    cur.<FONT COLOR="#000066">execute</FONT>(psIns, (i, <FONT COLOR="#7d7ca4">str</FONT>(i)))
<FONT COLOR="#1413be">print</FONT> (
    <FONT COLOR="#8f8c47">'With explicit prepared statement, performed'</FONT>
    <FONT COLOR="#8f8c47">'\n  %0.2f insertions per second.'</FONT> <FONT COLOR="#7d0000">%</FONT> (N <FONT COLOR="#7d0000">/</FONT> (time.<FONT COLOR="#000066">time</FONT>() <FONT COLOR="#7d0000">-</FONT> startTime))
  )
con.<FONT COLOR="#000066">commit</FONT>()

iStart <FONT COLOR="#7d0000">+=</FONT> N

<FONT COLOR="#65c265"># KInterbasDB automatically uses a PreparedStatement &quot;under the hood&quot;:</FONT>
startTime <FONT COLOR="#7d0000">=</FONT> time.<FONT COLOR="#000066">time</FONT>()
<FONT COLOR="#1413be">for</FONT> i <FONT COLOR="#1413be">in</FONT> <FONT COLOR="#7d7ca4">xrange</FONT>(iStart, iStart <FONT COLOR="#7d0000">+</FONT> N):
    cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into t (a,b) values (?,?)&quot;</FONT>, (i, <FONT COLOR="#7d7ca4">str</FONT>(i)))
<FONT COLOR="#1413be">print</FONT> (
    <FONT COLOR="#8f8c47">'With implicit prepared statement, performed'</FONT>
    <FONT COLOR="#8f8c47">'\n  %0.2f insertions per second.'</FONT> <FONT COLOR="#7d0000">%</FONT> (N <FONT COLOR="#7d0000">/</FONT> (time.<FONT COLOR="#000066">time</FONT>() <FONT COLOR="#7d0000">-</FONT> startTime))
  )
con.<FONT COLOR="#000066">commit</FONT>()

iStart <FONT COLOR="#7d0000">+=</FONT> N

<FONT COLOR="#65c265"># A new SQL string containing the inputs is submitted every time, so</FONT>
<FONT COLOR="#65c265"># KInterbasDB is not able to implicitly reuse a PreparedStatement.  Also, in a</FONT>
<FONT COLOR="#65c265"># more complicated scenario where the end user supplied the string input</FONT>
<FONT COLOR="#65c265"># values, the program would risk SQL injection attacks:</FONT>
startTime <FONT COLOR="#7d0000">=</FONT> time.<FONT COLOR="#000066">time</FONT>()
<FONT COLOR="#1413be">for</FONT> i <FONT COLOR="#1413be">in</FONT> <FONT COLOR="#7d7ca4">xrange</FONT>(iStart, iStart <FONT COLOR="#7d0000">+</FONT> N):
    cur.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;insert into t (a,b) values (%d,'%s')&quot;</FONT> <FONT COLOR="#7d0000">%</FONT> (i, <FONT COLOR="#7d7ca4">str</FONT>(i)))
<FONT COLOR="#1413be">print</FONT> (
    <FONT COLOR="#8f8c47">'When unable to reuse prepared statement, performed'</FONT>
    <FONT COLOR="#8f8c47">'\n  %0.2f insertions per second.'</FONT> <FONT COLOR="#7d0000">%</FONT> (N <FONT COLOR="#7d0000">/</FONT> (time.<FONT COLOR="#000066">time</FONT>() <FONT COLOR="#7d0000">-</FONT> startTime))
  )
con.<FONT COLOR="#000066">commit</FONT>()

<FONT COLOR="#65c265"># Prepare a SELECT statement and examine its properties.  The optimizer's plan</FONT>
<FONT COLOR="#65c265"># should use the unique index that we created at the beginning of this program.</FONT>
<FONT COLOR="#1413be">print</FONT>
psSel <FONT COLOR="#7d0000">=</FONT> cur.<FONT COLOR="#000066">prep</FONT>(<FONT COLOR="#8f8c47">&quot;select * from t where a = ?&quot;</FONT>)
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'psSel.sql: &quot;%s&quot;'</FONT> <FONT COLOR="#7d0000">%</FONT> psSel.sql
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'psSel.statement_type == kinterbasdb.isc_info_sql_stmt_select:'</FONT>, (
    psSel.statement_type <FONT COLOR="#7d0000">==</FONT> kinterbasdb.isc_info_sql_stmt_select
  )
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'psSel.n_input_params: %d'</FONT> <FONT COLOR="#7d0000">%</FONT> psSel.n_input_params
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'psSel.n_output_params: %d'</FONT> <FONT COLOR="#7d0000">%</FONT> psSel.n_output_params
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'psSel.plan: %s'</FONT> <FONT COLOR="#7d0000">%</FONT> psSel.plan

<FONT COLOR="#65c265"># The current implementation does not allow PreparedStatements to be prepared</FONT>
<FONT COLOR="#65c265"># on one Cursor and executed on another:</FONT>
<FONT COLOR="#1413be">print</FONT>
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'Note that PreparedStatements are not transferrable from one cursor to another:'</FONT>
cur2 <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()
cur2.<FONT COLOR="#000066">execute</FONT>(psSel)
</pre>

<p class="textParagraph">
Output:
</p>
<pre class="programOutputBlock" style="margin-top: 0px;">
psIns.sql: "insert into t (a,b) values (?,?)"
psIns.statement_type == kinterbasdb.isc_info_sql_stmt_insert: True
psIns.n_input_params: 2
psIns.n_output_params: 0
psIns.plan: None

With explicit prepared statement, performed
  9551.10 insertions per second.
With implicit prepared statement, performed
  9407.34 insertions per second.
When unable to reuse prepared statement, performed
  1882.53 insertions per second.

psSel.sql: "select * from t where a = ?"
psSel.statement_type == kinterbasdb.isc_info_sql_stmt_select: True
psSel.n_input_params: 1
psSel.n_output_params: 2
psSel.plan: PLAN (T INDEX (UNIQUE_T_A))

Note that PreparedStatements are not transferrable from one cursor to another:
Traceback (most recent call last):
  File "adv_prepared_statements__overall_example.py", line 86, in ?
    cur2.execute(psSel)
kinterbasdb.ProgrammingError: (0, 'A PreparedStatement can only be used with the
 Cursor that originally prepared it.')
</pre>

<p class="textParagraph">
As you can see, the version that prevents the reuse of prepared statements is
about five times slower--<em>for a trivial statement</em>.  In a real
application, SQL statements are likely to be far more complicated, so the
speed advantage of using prepared statements would only increase.
</p>

<p class="textParagraph">
As the timings indicate, KInterbasDB does a good job of reusing prepared
statements even if the client program is written in a style strictly compatible
with the Python DB API 2.0 (which accepts only strings--not
<code>PreparedStatement</code> objects--to the <code>Cursor.execute</code>
method).  The performance loss in this case is less than one percent.
</p>


<br>
<hr>
<br>

<a name="adv_named_cursors">
  <h2>Named Cursors</h2>
</a>

<p class="textParagraph">
The read/write property <code>Cursor.name</code> allows the Python
programmer to perform scrolling <code>UPDATE</code>s or <code>DELETE</code>s
via the "<code>SELECT ... FOR UPDATE</code>" syntax.
If you don't know what this means, refer to the section of the
<a href="#ref_ib6_docs">Interbase&reg; 6 Language Reference</a>
that covers the <code>SELECT</code> statement (page 139).
The <code>Cursor.name</code> property can be ignored entirely if you don't
need to use it.
</p>

<a name="adv_named_cursors_example">
  <h4>Example Program</h4>
</a>

<pre class="codeBlock" style="margin-top: 0px;">
<FONT COLOR="#0000ff">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>)
curScroll <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()
curUpdate <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">cursor</FONT>()

curScroll.<FONT COLOR="#000066">execute</FONT>(<FONT COLOR="#8f8c47">&quot;select city from addresses <strong>for update</strong>&quot;</FONT>)
curScroll.name <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#8f8c47">'city_scroller'</FONT>
update <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#8f8c47">&quot;update addresses set city=? <strong>where current of</strong> &quot;</FONT> <FONT COLOR="#7d0000">+</FONT> curScroll.name

<FONT COLOR="#0000ff">for</FONT> (city,) <FONT COLOR="#0000ff">in</FONT> curScroll:
    city <FONT COLOR="#7d0000">=</FONT> ... <FONT COLOR="#32a532"># make some changes to city</FONT>
    curUpdate.<FONT COLOR="#000066">execute</FONT>( update, (city,) )

con.<FONT COLOR="#000066">commit</FONT>()
</pre>

<br>
<hr>
<br>

<a name="adv_prog_maint">
  <h2>Programmatic Server, Database, and User Maintenance</h2>
</a>

<a name="adv_prog_maint_servapi">
  <h3>Services API</h3>
</a>

<p class="textParagraph">
Database server maintenance tasks such as user management, load monitoring,
and database backup have traditionally been automated by scripting
the command-line tools <code>gbak</code>, <code>gfix</code>, <code>gsec</code>,
and <code>gstat</code>.  These utilities are documented in the
<a href="#ref_ib6_docs">Interbase&reg; 6 Operations Guide</a> (see
"Overview of command-line tools", page 28).
</p>

<p class="textParagraph">
The API presented to the client programmer by these utilities is inelegant
because they are, after all, command-line tools rather than native components
of the client language.  To address this problem, Interbase&reg; 6 introduced
a facility called the Services API, which exposes a uniform interface to the
administrative functionality of the traditional command-line tools.
</p>

<p class="textParagraph">
The native Services API, though consistent, is much lower-level than a
Pythonic API.  If the native version were exposed directly, accomplishing
a given task would probably require more Python code than scripting the
traditional command-line tools.  For this reason, KInterbasDB presents its own
abstraction over the native API via the <code>kinterbasdb.services</code>
module.
</p>

<p class="textParagraph">

</p>


<a name="adv_prog_maint_servapi_connect">
  <h4>Establishing Services API Connections</h4>
</a>

<p class="textParagraph">
All Services API operations are performed in the context of a connection to
a specific database server, represented by the
<code>kinterbasdb.services.Connection</code> class.
<code>Connection</code>s are established by calling the
<code>kinterbasdb.services.connect</code> function, which accepts three keyword
arguments: <code>host</code>, <code>user</code>, and <code>password</code>.
<code>host</code> is the network name of the computer on which the database
server is running; <code>user</code> is the name of the database user under
whose authority the maintenance tasks are to be performed;
<code>password</code> is that
user's password.  Since maintenance operations are most often initiated by an
administrative user on the same computer as the database server,
<code>host</code> defaults to the local computer, and <code>user</code>
defaults to <code>SYSDBA</code>.
</p>

<p class="textParagraph">
The three calls to <code>kinterbasdb.services.connect</code> in the following
program are equivalent:
</p>

<pre class="codeBlock" style="margin-top: 0px;">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services

con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)
</pre>

<p class="textParagraph">
A no-argument <code>close</code> method is available to explicitly terminate
a <code>Connection</code>; if this is not invoked, the underlying connection
will be closed implicitly when the <code>Connection</code> object is garbage
collected.
</p>


<a name="adv_prog_maint_servapi_server_config">
  <h4>Querying Server Configuration and Activity Levels</h4>
</a>


<h5 class="serviceManagerMethodName">Connection.getServiceManagerVersion</h5>
<p class="textParagraphTight">
To help client programs adapt to version changes, the service manager
exposes its version number as an integer:
</p>

<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#0000ff">print</FONT> con.<FONT COLOR="#000066">getServiceManagerVersion</FONT>()
</pre>

<p class="textParagraphTight">
Output (on Firebird 1.5.0):
</p>
<pre class="programOutputBlock" style="margin-top: 0px;">
2
</pre>

<p class="textParagraph">
<code>kinterbasdb.services</code> is a thick wrapper of the Services
API that can shield its users from changes in the underlying C API, so this
method is unlikely to be useful to the typical Python client programmer.
</p>


<h5 class="serviceManagerMethodName">Connection.getServerVersion</h5>
<p class="textParagraphTight">
The <code>getServerVersion</code> method returns the server's version string:
</p>

<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#0000ff">print</FONT> con.<FONT COLOR="#000066">getServerVersion</FONT>()
</pre>
<p class="textParagraphTight">
Output (on Firebird 1.5.0/Win32):
</p>
<pre class="programOutputBlock" style="margin-top: 0px;">
WI-V1.5.0.4290 Firebird 1.5
</pre>

<p class="textParagraph">
At first glance, the <code>kinterbasdb.services.Connection.getServerVersion</code>
method appears to duplicate the functionality of the
<code>kinterbasdb.Connection.server_version</code> property, but when
working with Firebird, there is a difference.
<code>kinterbasdb.Connection.server_version</code> is based
on a C API call (<code>isc_database_info</code>) that existed long
before the introduction of the Services API in Interbase&reg; 6.
Some programs written before the advent of Firebird test the version number in the return value of
<code>isc_database_info</code>, and refuse to work if it indicates that the
server is too old.  Since the first stable version of Firebird
was labeled <code>1.0</code>, this pre-Firebird version testing scheme
incorrectly concludes that (e.g.) Firebird 1.0 is older than
Interbase&reg; 5.0.
</p>
<p class="textParagraph">
Firebird addresses this problem by making
<code>isc_database_info</code> return a "pseudo-Interbase&reg;" version
number, whereas the Services API returns the true Firebird version, as shown:
</p>

<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">import</FONT> kinterbasdb
con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:C:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'Interbase-compatible version string:'</FONT>, con.server_version

<FONT COLOR="#0000ff">import</FONT> kinterbasdb.services
svcCon <FONT COLOR="#7d0000">=</FONT> kinterbasdb.services.<FONT COLOR="#000066">connect</FONT>(host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'Actual Firebird version string:     '</FONT>, svcCon.<FONT COLOR="#000066">getServerVersion</FONT>()
</pre>
<p class="textParagraphTight">
Output (on Firebird 1.5.0/Win32):
</p>
<pre class="programOutputBlock" style="margin-top: 0px;">
Interbase-compatible version string: WI-V6.3.0.4290 Firebird 1.5
Actual Firebird version string:      WI-V1.5.0.4290 Firebird 1.5
</pre>


<h5 class="serviceManagerMethodName">Connection.getArchitecture</h5>
<p class="textParagraphTight">
The <code>getArchitecture</code> method returns platform information for
the server, including hardware architecture and operating system family:
</p>

<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#0000ff">print</FONT> con.<FONT COLOR="#000066">getArchitecture</FONT>()
</pre>

<p class="textParagraphTight">
Output (on Firebird 1.5.0/Windows 2000):
</p>
<pre class="programOutputBlockTight" style="margin-top: 0px;">
Firebird/x86/Windows NT
</pre>

<p class="textParagraphTight">
Unfortunately, the architecture string is almost useless because its format
is irregular and sometimes outright idiotic, as with Firebird 1.5.0 running
on x86 Linux:
</p>
<pre class="programOutputBlockTight" style="margin-top: 0px;">
Firebird/linux Intel
</pre>
<p class="textParagraphTight">
Magically, Linux becomes a hardware architecture, the ASCII store decides
to hold a 31.92% off sale, and Intel grabs an unfilled
niche in the operating system market.
</p>


<h5 class="serviceManagerMethodName">Connection.getHomeDir</h5>
<p class="textParagraphTight">
The <code>getHomeDir</code> method returns the equivalent of the
<code>RootDirectory</code> setting from <code>firebird.conf</code>:
</p>

<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#0000ff">print</FONT> con.<FONT COLOR="#000066">getHomeDir</FONT>()
</pre>

<p class="textParagraphTight">
Output (on a particular Firebird 1.5.0/Windows 2000 installation):
</p>
<pre class="programOutputBlockTight">
C:\dev\db\firebird150\
</pre>
<p class="textParagraphTight">
Output (on a particular Firebird 1.5.0/Linux installation):
</p>
<pre class="programOutputBlockTight">
/opt/firebird/
</pre>


<h5 class="serviceManagerMethodName">Connection.getSecurityDatabasePath</h5>
<p class="textParagraphTight">
The <code>getSecurityDatabasePath</code> method returns the location of
the server's core security database, which contains user definitions and
such.
Interbase&reg; and Firebird 1.0 named this database <code>isc4.gdb</code>,
while in Firebird 1.5 and later it's renamed to <code>security.fdb</code>:
</p>

<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#0000ff">print</FONT> con.<FONT COLOR="#000066">getSecurityDatabasePath</FONT>()
</pre>

<p class="textParagraphTight">
Output (on a particular Firebird 1.5.0/Windows 2000 installation):
</p>
<pre class="programOutputBlockTight">
C:\dev\db\firebird150\security.fdb
</pre>
<p class="textParagraphTight">
Output (on a particular Firebird 1.5.0/Linux installation):
</p>
<pre class="programOutputBlockTight">
/opt/firebird/security.fdb
</pre>


<h5 class="serviceManagerMethodName">Connection.getLockFileDir</h5>
<p class="textParagraphTight">
The database engine
<a href="http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_locking">uses a lock file</a>
to coordinate interprocess communication;
<code>getLockFileDir</code> returns the directory in which that file resides:
</p>

<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#0000ff">print</FONT> con.<FONT COLOR="#000066">getLockFileDir</FONT>()
</pre>

<p class="textParagraphTight">
Output (on a particular Firebird 1.5.0/Windows 2000 installation):
</p>
<pre class="programOutputBlockTight">
C:\dev\db\firebird150\
</pre>
<p class="textParagraphTight">
Output (on a particular Firebird 1.5.0/Linux installation):
</p>
<pre class="programOutputBlockTight">
/opt/firebird/
</pre>


<h5 class="serviceManagerMethodName">Connection.getCapabilityMask</h5>
<p class="textParagraphTight">
The Services API offers "a bitmask representing the capabilities currently
enabled on the server", but the only available
<a href="#ref_ib6_docs">documentation</a>
for this bitmask suggests that it is "reserved for future implementation".
kinterbasdb exposes this bitmask as a Python <code>int</code> returned from
the <code>getCapabilityMask</code> method.
</p>


<h5 class="serviceManagerMethodName">Connection.getMessageFileDir</h5>
<p class="textParagraphTight">
To support internationalized error messages/prompts, the database engine stores
its messages in a file named <code>interbase.msg</code> (Interbase&reg;
and Firebird 1.0) or <code>firebird.msg</code> (Firebird 1.5 and later).
The directory in which this file resides can be determined with the
<code>getMessageFileDir</code> method.
</p>

<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#0000ff">print</FONT> con.<FONT COLOR="#000066">getMessageFileDir</FONT>()
</pre>

<p class="textParagraphTight">
Output (on a particular Firebird 1.5.0/Windows 2000 installation):
</p>
<pre class="programOutputBlockTight">
C:\dev\db\firebird150\
</pre>
<p class="textParagraphTight">
Output (on a particular Firebird 1.5.0/Linux installation):
</p>
<pre class="programOutputBlockTight">
/opt/firebird/
</pre>


<h5 class="serviceManagerMethodName">Connection.getConnectionCount</h5>
<p class="textParagraph">
<code>getConnectionCount</code> returns the number of active connections to
databases managed by the server.
This count only includes <em>database</em>
connections (such as open instances of <code>kinterbasdb.Connection</code>),
not <em>services manager</em> connections
(such as open instances of <code>kinterbasdb.services.Connection</code>).
</p>

<pre class="codeBlock">
<FONT COLOR="#0000ff">import</FONT> kinterbasdb, kinterbasdb.services
svcCon <FONT COLOR="#7d0000">=</FONT> kinterbasdb.services.<FONT COLOR="#000066">connect</FONT>(host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'A:'</FONT>, svcCon.<FONT COLOR="#000066">getConnectionCount</FONT>()

con1 <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:C:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'B:'</FONT>, svcCon.<FONT COLOR="#000066">getConnectionCount</FONT>()

con2 <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:C:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'C:'</FONT>, svcCon.<FONT COLOR="#000066">getConnectionCount</FONT>()

con1.<FONT COLOR="#000066">close</FONT>()
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'D:'</FONT>, svcCon.<FONT COLOR="#000066">getConnectionCount</FONT>()

con2.<FONT COLOR="#000066">close</FONT>()
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'E:'</FONT>, svcCon.<FONT COLOR="#000066">getConnectionCount</FONT>()
</pre>

<p class="textParagraphTight">
On an otherwise inactive server, the example program generates the following
output:
</p>
<pre class="programOutputBlock" style="margin-top: 0px;">
A: 0
B: 1
C: 2
D: 1
E: 0
</pre>


<h5 class="serviceManagerMethodName">Connection.getAttachedDatabaseNames</h5>
<p class="textParagraphTight">
<code>getAttachedDatabaseNames</code> returns a list of the names of all
databases to which the server is maintaining at least one connection.
The database names are not guaranteed to be in any particular order.
</p>

<pre class="codeBlock">
<FONT COLOR="#0000ff">import</FONT> kinterbasdb, kinterbasdb.services
svcCon <FONT COLOR="#7d0000">=</FONT> kinterbasdb.services.<FONT COLOR="#000066">connect</FONT>(host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'A:'</FONT>, svcCon.<FONT COLOR="#000066">getAttachedDatabaseNames</FONT>()

con1 <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:C:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'B:'</FONT>, svcCon.<FONT COLOR="#000066">getAttachedDatabaseNames</FONT>()

con2 <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:C:/temp/test2.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'C:'</FONT>, svcCon.<FONT COLOR="#000066">getAttachedDatabaseNames</FONT>()

con3 <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:C:/temp/test2.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'D:'</FONT>, svcCon.<FONT COLOR="#000066">getAttachedDatabaseNames</FONT>()

con1.<FONT COLOR="#000066">close</FONT>()
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'E:'</FONT>, svcCon.<FONT COLOR="#000066">getAttachedDatabaseNames</FONT>()

con2.<FONT COLOR="#000066">close</FONT>()
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'F:'</FONT>, svcCon.<FONT COLOR="#000066">getAttachedDatabaseNames</FONT>()

con3.<FONT COLOR="#000066">close</FONT>()
<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'G:'</FONT>, svcCon.<FONT COLOR="#000066">getAttachedDatabaseNames</FONT>()
</pre>

<p class="textParagraphTight">
On an otherwise inactive server, the example program generates the following
output:
</p>
<pre class="programOutputBlock" style="margin-top: 0px;">
A: []
B: ['C:\\TEMP\\TEST.DB']
C: ['C:\\TEMP\\TEST2.DB', 'C:\\TEMP\\TEST.DB']
D: ['C:\\TEMP\\TEST2.DB', 'C:\\TEMP\\TEST.DB']
E: ['C:\\TEMP\\TEST2.DB']
F: ['C:\\TEMP\\TEST2.DB']
G: []
</pre>


<h5 class="serviceManagerMethodName">Connection.getLog</h5>
<p class="textParagraphTight">
The <code>getLog</code> method returns the contents of the server's log file
(named <code>interbase.log</code> by Interbase&reg; and Firebird 1.0;
<code>firebird.log</code> by Firebird 1.5 and later):
</p>

<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(host<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#0000ff">print</FONT> con.<FONT COLOR="#000066">getLog</FONT>()
</pre>

<p class="textParagraphTight">
Output (on a particular Firebird 1.5.0/Windows 2000 installation):
</p>
<pre class="programOutputBlock" style="margin-top: 0px;">
WEASEL (Client) Thu Jun 03 12:01:35 2004
  INET/inet_error: send errno = 10054

WEASEL (Client) Sun Jun 06 19:21:17 2004
  INET/inet_error: connect errno = 10061

</pre>



<a name="adv_prog_maint_servapi_database_stats">
  <h4>Querying Database Statistics</h4>
</a>

<h5 class="serviceManagerMethodName">Connection.getStatistics</h5>
<p class="textParagraphTight">
The <code>getStatistics</code> method returns a string containing a printout
in the same format as the output of the <code>gstat</code> command-line
utility.  This method has one required parameter, the location of the database
on which to compute statistics, and five optional boolean parameters for
controlling the domain of the statistics.
</p>

<p class="textParagraph">
The section of the
<a href="#ref_ib6_docs">Interbase&reg; 6 Operations Guide</a>
entitled "gstat command-line tool"  (page 181)
documents <code>gstat</code>'s command-line options.
Rather than attempting to duplicate that documentation here,
we present a table of equivalence:
</p>

<table border="1" cellpadding="5">
  <thead>
    <tr style="background-color: #DEDEDE;">
      <td><code>gstat</code> command-line option</td>
      <td><code>kinterbasdb.services.Connection.getStatistics</code> boolean parameter</td>
    </tr>
  </thead>
  <tr>
    <td><code>-header</code></td>
    <td><code>showOnlyDatabaseHeaderPages</code></td>
  </tr>
  <tr>
    <td><code>-log</code></td>
    <td><code>showOnlyDatabaseLogPages</code></td>
  </tr>
  <tr>
    <td><code>-data</code></td>
    <td><code>showUserDataPages</code></td>
  </tr>
  <tr>
    <td><code>-index</code></td>
    <td><code>showUserIndexPages</code></td>
  </tr>
  <tr>
    <td><code>-system</code></td>
    <td><code>showSystemTablesAndIndexes</code></td>
  </tr>
</table>

<p class="textParagraph">
The following program presents several <code>getStatistics</code>
calls and their <code>gstat</code>-command-line equivalents.  In this
context, output is considered "equivalent" even if their are some whitespace
differences.  When collecting textual output from the Services API,
kinterbasdb terminates lines with <code>\n</code> regardless of the platform's
convention; <code>gstat</code> is platform-sensitive.
</p>

<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#32a532"># Equivalent to 'gstat -u sysdba -p masterkey C:/temp/test.db':</FONT>
<FONT COLOR="#0000ff">print</FONT> con.<FONT COLOR="#000066">getStatistics</FONT>(<FONT COLOR="#8f8c47">'C:/temp/test.db'</FONT>)

<FONT COLOR="#32a532"># Equivalent to 'gstat -u sysdba -p masterkey -header C:/temp/test.db':</FONT>
<FONT COLOR="#0000ff">print</FONT> con.<FONT COLOR="#000066">getStatistics</FONT>(<FONT COLOR="#8f8c47">'C:/temp/test.db'</FONT>, showOnlyDatabaseHeaderPages<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#0066a0">True</FONT>)

<FONT COLOR="#32a532"># Equivalent to 'gstat -u sysdba -p masterkey -log C:/temp/test.db':</FONT>
<FONT COLOR="#0000ff">print</FONT> con.<FONT COLOR="#000066">getStatistics</FONT>(<FONT COLOR="#8f8c47">'C:/temp/test.db'</FONT>, showOnlyDatabaseLogPages<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#0066a0">True</FONT>)

<FONT COLOR="#32a532"># Equivalent to 'gstat -u sysdba -p masterkey -data -index -system C:/temp/test.db':</FONT>
<FONT COLOR="#0000ff">print</FONT> con.<FONT COLOR="#000066">getStatistics</FONT>(<FONT COLOR="#8f8c47">'C:/temp/test.db'</FONT>,
    showUserDataPages<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#0066a0">True</FONT>,
    showUserIndexPages<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#0066a0">True</FONT>,
    showSystemTablesAndIndexes<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#0066a0">True</FONT>
  )
</pre>

<p class="textParagraphTight">
The output of the example program is not shown here because it is quite long.
</p>


<a name="adv_prog_maint_servapi_backup_restore">
  <h4>Backup and Restoration</h4>
</a>

<p class="textParagraph">
KInterbasDB offers convenient programmatic control over database backup and
restoration via the <code>backup</code> and <code>restore</code> methods.
</p>

<p class="textParagraph">
At the time of this writing, released versions of Firebird/Interbase&reg; do
not implement incremental backup, so we can simplistically define
<em>backup</em> as the process of generating and storing an archived replica
of a live database, and <em>restoration</em> as the inverse.
The backup/restoration process exposes numerous parameters, which are properly
documented in
Chapter 7 ("Database Backup and Restore") of the
<a href="#ref_ib6_docs">Interbase&reg; 6 Operations Guide</a>.
The KInterbasDB API to these parameters is presented with minimal
documentation in the sample code below.
</p>

<a name="adv_prog_maint_servapi_backup_restore__backup">
  <h5 class="serviceManagerMethodName">Connection.backup</h5>
</a>

<h5 class="serviceManagerMethod_Form">Simplest Form</h5>

<p class="textParagraphTight">
The simplest form of <code>backup</code> creates a single backup file
that contains everything in the database.  Although the extension
<code>'.gbk'</code> is conventional, it is not required.
</p>

<pre class="codeBlockNoTop" style="margin-bottom: 0px;">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

backupLog <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">backup</FONT>(<FONT COLOR="#8f8c47">'C:/temp/test.db'</FONT>, <FONT COLOR="#8f8c47">'C:/temp/test_backup.gbk'</FONT>)
<FONT COLOR="#0000ff">print</FONT> backupLog
</pre>

<p class="textParagraphTight" style="margin-bottom: 1em;">
In the example, <code>backupLog</code> is a string containing a
<code>gbak</code>-style log of the backup process.  It is too long to
reproduce here.
</p>

<p class="textParagraph">
Although the return value of the <code>backup</code> method is a freeform
log string, <code>backup</code> will raise an exception if there is an error.
For example:
</p>
<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#32a532"># Pass an invalid backup path to the engine:</FONT>
backupLog <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">backup</FONT>(<FONT COLOR="#8f8c47">'C:/temp/test.db'</FONT>, <FONT COLOR="#8f8c47">'BOGUS/PATH/test_backup.gbk'</FONT>)
<FONT COLOR="#0000ff">print</FONT> backupLog
</pre>
<pre class="programOutputBlock" style="margin-top: 0px;">
Traceback (most recent call last):
  File "adv_services_backup_simplest_witherror.py", line 5, in ?
    backupLog = con.backup('C:/temp/test.db', 'BOGUS/PATH/test_backup.gbk')
  File "C:\code\projects\kinterbasdb\Kinterbasdb-3.0\build\lib.win32-2.3\kinterbasdb\services.py", line 269, in backup
    return self._actAndReturnTextualResults(request)
  File "C:\code\projects\kinterbasdb\Kinterbasdb-3.0\build\lib.win32-2.3\kinterbasdb\services.py", line 613, in _actAndReturnTextualResults
    self._act(requestBuffer)
  File "C:\code\projects\kinterbasdb\Kinterbasdb-3.0\build\lib.win32-2.3\kinterbasdb\services.py", line 610, in _act
    return _ksrv.action_thin(self._C_conn, requestBuffer.render())
kinterbasdb.OperationalError: (-902, '_kiservices could not perform the action: cannot open backup file BOGUS/PATH/test_backup.gbk. ')
</pre>


<h5 class="serviceManagerMethod_Form">Multifile Form</h5>

<p class="textParagraphTight">
The database engine has built-in support for splitting the backup into multiple
files, which is useful for circumventing operating system file size limits
or spreading the backup across multiple discs.
</p>

<p class="textParagraph">
KInterbasDB exposes this facility via the <code>Connection.backup</code>
parameters <code>destFilenames</code> and <code>destFileSizes</code>.
<code>destFilenames</code> (the second positional parameter of
<code>Connection.backup</code>) can be either a string (as in the example
above, when creating the backup as a single file) or a sequence of strings
naming each constituent file of the backup.
If <code>destFilenames</code> is a string-sequence with length <code>N</code>,
<code>destFileSizes</code> must be a sequence of integer file sizes
(in bytes) with length <code>N-1</code>.  The database engine will constrain
the size of each backup constituent file named in
<code>destFilenames[:-1]</code> to the corresponding size specified in
<code>destFileSizes</code>; any remaining backup data will be placed in the
file name by <code>destFilenames[-1]</code>.
</p>

<p class="textParagraph">
Unfortunately, the database engine does not appear to expose any convenient
means of calculating the total size of a database backup before its creation.
The page size of the database and the number of pages in the database are
available via
<code>kinterbasdb.Connection.database_info(kinterbasdb.isc_info_page_size, 'i')</code>
and
<code>kinterbasdb.Connection.database_info(kinterbasdb.isc_info_db_size_in_pages, 'i')</code>,
respectively, but the size of the backup file is usually smaller than the size
of the database.
</p>

<p class="textParagraph">
There <em>should</em> be no harm in submitting too many constituent specifications; the
engine will write an empty header record into the excess constituents.
However, at the time of this writing, released versions of the database engine
hang the backup task if more than 11 constituents are specified (that is,
if <code>len(destFilenames) > 11</code>).
KInterbasDB does not prevent the programmer from submitting more than 11
constituents, but it does issue a warning.
</p>

<p class="textParagraph">
The following program directs the engine to split the backup
of the database at <code>'C:/temp/test.db'</code> into
<code>'C:/temp/back01.gbk'</code>, a file 4096 bytes in size,
<code>'C:/temp/back02.gbk'</code>, a file 16384 bytes in size,
and <code>'C:/temp/back03.gbk'</code>, a file containing the remainder
of the backup data.
</p>

<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

con.<FONT COLOR="#000066">backup</FONT>(<FONT COLOR="#8f8c47">'C:/temp/test.db'</FONT>,
    (<FONT COLOR="#8f8c47">'C:/temp/back01.gbk'</FONT>, <FONT COLOR="#8f8c47">'C:/temp/back02.gbk'</FONT>, <FONT COLOR="#8f8c47">'C:/temp/back03.gbk'</FONT>),
    destFileSizes<FONT COLOR="#7d0000">=</FONT>(<FONT COLOR="#666600">4096</FONT>, <FONT COLOR="#666600">16384</FONT>)
  )
</pre>



<h5 class="serviceManagerMethod_Form">Extended Options</h5>

<p class="textParagraphTight">
In addition to the three parameters documented previously
(positional <code>sourceDatabase</code>,
positional <code>destFilenames</code>,
and keyword <code>destFileSizes</code>),
the <code>Connection.backup</code> method accepts six boolean parameters
that control aspects of the backup process and the backup file output format.
These options are well documented beginning on page 149 of the
<a href="#ref_ib6_docs">Interbase&reg; 6 Operations Guide</a>, so in this
document we present only a table of equivalence between the section caption in
the Interbase&reg; 6 Operations Guide and the name of the boolean keyword
parameter:
</p>

<table border="1" style="margin-left: 1em;">
  <thead>
    <tr style="background-color: #DEDEDE;">
      <td>IB6 Op. Guide Caption</td>
      <td><code>Connection.backup</code><br>Parameter Name</td>
      <td><code>Connection.backup</code><br>Parameter Default Value</td>
    </tr>
  </thead>
  <tr>
    <td>Format</td>
    <td><code>transportable</code></td>
    <td><code>True</code></td>
  </tr>
  <tr>
    <td>Metadata Only</td>
    <td><code>metadataOnly</code></td>
    <td><code>False</code></td>
  </tr>
  <tr>
    <td>Garbage Collection</td>
    <td><code>garbageCollect</code></td>
    <td><code>True</code></td>
  </tr>
  <tr>
    <td>Transactions in Limbo&nbsp;&nbsp;</td>
    <td><code>ignoreLimboTransactions</code></td>
    <td><code>False</code></td>
  </tr>
  <tr>
    <td>Checksums</td>
    <td><code>ignoreChecksums</code></td>
    <td><code>False</code></td>
  </tr>
  <tr>
    <td>Convert to Tables</td>
    <td><code>convertExternalTablesToInternalTables&nbsp;</code></td>
    <td><code>True</code></td>
  </tr>
</table>


<a name="adv_prog_maint_servapi_backup_restore__restore">
  <h5 class="serviceManagerMethodName" style="margin-top: 1.5em;">Connection.restore</h5>
</a>

<h5 class="serviceManagerMethod_Form">Simplest Form</h5>

<p class="textParagraphTight">
The simplest form of <code>restore</code> creates a single-file database,
regardless of whether the backup data were split across multiple files.
</p>


<pre class="codeBlockNoTop" style="margin-bottom: 0px;">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

restoreLog <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">restore</FONT>(<FONT COLOR="#8f8c47">'C:/temp/test_backup.gbk'</FONT>, <FONT COLOR="#8f8c47">'C:/temp/test_restored.db'</FONT>)
<FONT COLOR="#0000ff">print</FONT> restoreLog
</pre>

<p class="textParagraphTight" style="margin-bottom: 1em;">
In the example, <code>restoreLog</code> is a string containing a
<code>gbak</code>-style log of the restoration process.  It is too long to
reproduce here.
</p>


<h5 class="serviceManagerMethod_Form">Multifile Form</h5>

<p class="textParagraphTight">
The database engine has built-in support for splitting the restored database
into multiple files, which is useful for circumventing operating system file
size limits or spreading the database across multiple discs.
</p>

<p class="textParagraph">
KInterbasDB exposes this facility via the <code>Connection.restore</code>
parameters <code>destFilenames</code> and <code>destFilePages</code>.
<code>destFilenames</code> (the second positional argument of
<code>Connection.restore</code>) can be either a string (as in the example
above, when restoring to a single database file) or a sequence of strings
naming each constituent file of the restored database.
If <code>destFilenames</code> is a string-sequence with length <code>N</code>,
<code>destFilePages</code> must be a sequence of integers with length
<code>N-1</code>.  The database engine will constrain the size of each
database constituent file named in <code>destFilenames[:-1]</code> to the
corresponding page count specified in <code>destFilePages</code>; any
remaining database pages will be placed in the file name by
<code>destFilenames[-1]</code>.
</p>

<p class="textParagraph">
The following program directs the engine to restore the backup file at
<code>'C:/temp/test_backup.gbk'</code> into a database with three constituent
files:
<code>'C:/temp/test_restored01.db'</code>,
<code>'C:/temp/test_restored02.db'</code>,
and
<code>'C:/temp/test_restored03.db'</code>.

The engine is instructed to place fifty user data pages in the first file,
seventy in the second, and the remainder in the third file.  In practice, the
first database constituent file will be larger than
<code>pageSize*destFilePages[0]</code>, because metadata pages must also be
stored in the first constituent of a multifile database.
</p>

<pre class="codeBlockNoTop">
<FONT COLOR="#0000ff">from</FONT> kinterbasdb <FONT COLOR="#0000ff">import</FONT> services
con <FONT COLOR="#7d0000">=</FONT> services.<FONT COLOR="#000066">connect</FONT>(user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

con.<FONT COLOR="#000066">restore</FONT>(<FONT COLOR="#8f8c47">'C:/temp/test_backup.gbk'</FONT>,
    (<FONT COLOR="#8f8c47">'C:/temp/test_restored01.db'</FONT>, <FONT COLOR="#8f8c47">'C:/temp/test_restored02.db'</FONT>, <FONT COLOR="#8f8c47">'C:/temp/test_restored03.db'</FONT>),
    destFilePages<FONT COLOR="#7d0000">=</FONT>(<FONT COLOR="#666600">50</FONT>, <FONT COLOR="#666600">70</FONT>),
    pageSize<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#666600">1024</FONT>,
    replace<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#0066a0">True</FONT>
  )
</pre>



<h5 class="serviceManagerMethod_Form">Extended Options</h5>

<p class="textParagraphTight">
These options are well documented beginning on page 155 of the
<a href="#ref_ib6_docs">Interbase&reg; 6 Operations Guide</a>, so in this
document we present only a table of equivalence between the section caption in
the Interbase&reg; 6 Operations Guide and the name of the keyword
parameter to <code>Connection.restore</code>:
</p>

<table border="1" style="margin-left: 1em;">
  <thead>
    <tr style="background-color: #DEDEDE;">
      <td>IB6 Op. Guide Caption</td>
      <td><code>Connection.restore</code><br>Parameter Name</td>
      <td><code>Connection.restore</code><br>Parameter Default Value</td>
    </tr>
  </thead>
  <tr>
    <td>Page Size</td>
    <td><code>pageSize</code></td>
    <td><code>[use server default]</code></td>
  </tr>
  <tr>
    <td>Overwrite</td>
    <td><code>replace</code></td>
    <td><code>False</code></td>
  </tr>
  <tr>
    <td>Commit After Each Table</td>
    <td><code>commitAfterEachTable</code></td>
    <td><code>False</code></td>
  </tr>
  <tr>
    <td>Create Shadow Files</td>
    <td><code>doNotRestoreShadows</code></td>
    <td><code>False</code></td>
  </tr>
  <tr>
    <td>Deactivate Indexes</td>
    <td><code>deactivateIndexes</code></td>
    <td><code>False</code></td>
  </tr>
  <tr>
    <td>Validity Conditions</td>
    <td><code>doNotEnforceConstraints</code></td>
    <td><code>False</code></td>
  </tr>
  <tr>
    <td>Use All Space</td>
    <td><code>useAllPageSpace</code></td>
    <td><code>False</code></td>
  </tr>
</table>

<p class="textParagraph">
Two additional boolean parameters are not covered by the table above:
<code>cacheBuffers</code> and <code>accessModeReadOnly</code>.
<code>cacheBuffers</code> specifies the default number of cache pages for
the restored database.  If left unspecified, <code>cacheBuffers</code> uses
the server default.
<code>accessModeReadOnly</code> (default <code>False</code>) specifies whether
the restored database is read-only (<code>True</code>) or
writable (<code>False</code>).
</p>




<a name="adv_prog_maint_servapi_database_maint">
  <h4 style="margin-top: 3em;">Controlling Database Operating Modes, Sweeps, and Repair</h4>
</a>

<p class="textParagraph">
<span class="XXX_ADDRESS_THIS">(XXX: not yet documented)</span>
</p>


<a name="adv_prog_maint_servapi_users">
  <h4>User Maintenance</h4>
</a>

<p class="textParagraph">
<span class="XXX_ADDRESS_THIS">(XXX: not yet documented)</span>
</p>

<br><hr><br>

<a name="adv_prog_maint_database_info">
  <h3>The <code>database_info</code> Method</h3>
</a>

<table border="1" class="memberDocFrame">
  <tr>
    <td class="memberDocHeader">
        <code class="memberDocName">database_info</code>&nbsp;
        <span class="memberDocNameCaption">(method; member of <code>kinterbasdb.Connection</code>)</span>
    </td>
  </tr>
  <tr>
    <td>
        <p>
        Wraps the Interbase&reg; C API function <code>isc_database_info</code> .
        For extensive documentation, see the
        <a href="#ref_ib6_docs">Interbase&reg; 6 API Guide</a>
        section
        entitled "Requesting information about an attachment" (page 51).
        </p>

        <p>
        Note that this method is a <em>very thin</em> wrapper around
        function <code>isc_database_info</code>.
        This method does <em>not</em> attempt to interpret
        its results except with regard to whether they are a string or an
        integer.
        </p>

        <p>
        For example, requesting <code>isc_info_user_names</code> with the
        call<br>
        <pre class="codeBlockInMemberDoc" style="margin-bottom: 0px;">con.<FONT COLOR="#000066">database_info</FONT>(kinterbasdb.isc_info_user_names, <FONT COLOR="#8f8c47">'s'</FONT>)</pre>
        will return a binary string
        containing a <em>raw</em> succession of length-name pairs.  A more
        convenient way to access the same functionality is via the
        <a href="#adv_prog_maint_db_info"><code>Connection.db_info</code></a>
        method.
        </p>

        <p class="argHeader">Arguments:</p>
        <ul class="argList">
        <li>
            <code>request</code> -
            one of the <code>kinterbasdb.isc_info_*</code> constants.<br>
        </li>
        <li>
            <code>result_type</code> -
            must be either
                    <code>'s'</code> if you expect a string result, or
                    <code>'i'</code> if you expect an integer result.
        </li>
        </ul>

    </td>
  </tr>
</table>


<a name="adv_prog_maint_database_info_example">
  <h4>Example Program</h4>
</a>

<pre class="codeBlock"><FONT COLOR="#0000ff">import</FONT> kinterbasdb

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'localhost:/temp/test.db'</FONT>, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>)

<FONT COLOR="#32a532"># Retrieving an integer info item is quite simple.</FONT>
bytesInUse <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">database_info</FONT>(kinterbasdb.isc_info_current_memory, <FONT COLOR="#8f8c47">'i'</FONT>)

<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'The server is currently using %d bytes of memory.'</FONT> <FONT COLOR="#7d0000">%</FONT> bytesInUse

<FONT COLOR="#32a532"># Retrieving a string info item is somewhat more involved, because the</FONT>
<FONT COLOR="#32a532"># information is returned in a raw binary buffer that must be parsed</FONT>
<FONT COLOR="#32a532"># according to the rules defined in the Interbase&reg; 6 API Guide section</FONT>
<FONT COLOR="#32a532"># entitled &quot;Requesting buffer items and result buffer values&quot; (page 51).</FONT>
<FONT COLOR="#32a532">#</FONT>
<FONT COLOR="#32a532"># Often, the buffer contains a succession of length-string pairs</FONT>
<FONT COLOR="#32a532"># (one byte telling the length of s, followed by s itself).</FONT>
<FONT COLOR="#32a532"># Function kinterbasdb.raw_byte_to_int is provided to convert a raw</FONT>
<FONT COLOR="#32a532"># byte to a Python integer (see examples below).</FONT>
buf <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">database_info</FONT>(kinterbasdb.isc_info_db_id, <FONT COLOR="#8f8c47">'s'</FONT>)

<FONT COLOR="#32a532"># Parse the filename from the buffer.</FONT>
beginningOfFilename <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#666600">2</FONT>
<FONT COLOR="#32a532"># The second byte in the buffer contains the size of the database filename</FONT>
<FONT COLOR="#32a532"># in bytes.</FONT>
lengthOfFilename <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">raw_byte_to_int</FONT>(buf[<FONT COLOR="#666600">1</FONT>])
filename <FONT COLOR="#7d0000">=</FONT> buf[beginningOfFilename:beginningOfFilename <FONT COLOR="#7d0000">+</FONT> lengthOfFilename]

<FONT COLOR="#32a532"># Parse the host name from the buffer.</FONT>
beginningOfHostName <FONT COLOR="#7d0000">=</FONT> (beginningOfFilename <FONT COLOR="#7d0000">+</FONT> lengthOfFilename) <FONT COLOR="#7d0000">+</FONT> <FONT COLOR="#666600">1</FONT>
<FONT COLOR="#32a532"># The first byte after the end of the database filename contains the size</FONT>
<FONT COLOR="#32a532"># of the host name in bytes.</FONT>
lengthOfHostName <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">raw_byte_to_int</FONT>(buf[beginningOfHostName <FONT COLOR="#7d0000">-</FONT> <FONT COLOR="#666600">1</FONT>])
host <FONT COLOR="#7d0000">=</FONT> buf[beginningOfHostName:beginningOfHostName <FONT COLOR="#7d0000">+</FONT> lengthOfHostName]

<FONT COLOR="#0000ff">print</FONT> <FONT COLOR="#8f8c47">'We are connected to the database at %s on host %s.'</FONT> <FONT COLOR="#7d0000">%</FONT> (filename, host)
</pre>

<p class="textParagraph">
Sample output:
</p>
<pre class="programOutputBlock">
The server is currently using 8931328 bytes of memory.
We are connected to the database at C:\TEMP\TEST.DB on host WEASEL.
</pre>

<p class="textParagraph">
As you can see, extracting data with the <code>database_info</code> function
is rather clumsy.  In KInterbasDB 3.2, a higher-level means of accessing the
same information is available: the
<a href="#adv_prog_maint_db_info"><code>Connection.db_info</code></a>
method.

Also, the Services API (accessible to Python programmers via the
<a href="#adv_prog_maint_servapi"><code>kinterbasdb.services</code></a>
module) provides high-level support for querying database statistics and
performing maintenance.
</p>



<br><hr><br>

<a name="adv_prog_maint_db_info">
  <h3>The <code>db_info</code> Method</h3>
</a>

<table border="1" class="memberDocFrame">
  <tr>
    <td class="memberDocHeader">
        <code class="memberDocName">db_info</code>&nbsp;
        <span class="memberDocNameCaption">(method; member of <code>kinterbasdb.Connection</code>)</span>
    </td>
  </tr>
  <tr>
    <td>
        <p>
          High-level convenience wrapper around the
        <a href="#adv_prog_maint_database_info"><code>Connection.database_info</code></a>
        method that parses the output of <code>database_info</code> into
        Python-friendly objects instead of returning raw binary buffers in the
        case of complex result types.
        If an unrecognized <code>isc_info_*</code> code is requested,
        this method raises <code>ValueError</code>.
        </p>

        <p>
        For example, requesting <code>isc_info_user_names</code> with the
        call<br>
        <pre class="codeBlockInMemberDoc" style="margin-bottom: 0px;">con.<FONT COLOR="#000066">db_info</FONT>(kinterbasdb.isc_info_user_names)</pre>
        returns a dictionary that maps (username -> number of open
        connections).  If <code>SYSDBA</code> has one open connection to the
        database to which <code>con</code> is connected, and
        <code>TEST_USER_1</code> has three open connections to that same
        database, the return value would be
        <code>{'SYSDBA': 1, 'TEST_USER_1': 3}</code>
        </p>

        <p class="argHeader">Arguments:</p>
        <ul class="argList">
        <li>
            <code>request</code> -
            must be either:
            <ul>
              <li>A single <code>kinterbasdb.isc_info_*</code> info request
                code. In this case, a single result is returned.
              </li>
              <li>A sequence of such codes.  In this case, a mapping of (info
                  request code -> result) is returned.
              </li>
            </ul>
        </li>
        </ul>

    </td>
  </tr>
</table>


<a name="adv_prog_maint_db_info_example">
  <h4>Example Program</h4>
</a>

<pre class="codeBlock"><FONT COLOR="#1413be">import</FONT> os.path

<FONT COLOR="#1413be">import</FONT> kinterbasdb

DB_FILENAME <FONT COLOR="#7d0000">=</FONT> r<FONT COLOR="#8f8c47">'D:\temp\test-20.firebird'</FONT>
DSN <FONT COLOR="#7d0000">=</FONT> <FONT COLOR="#8f8c47">'localhost:'</FONT> <FONT COLOR="#7d0000">+</FONT> DB_FILENAME

<FONT COLOR="#65c265">###############################################################################</FONT>
<FONT COLOR="#65c265"># Querying an isc_info_* item that has a complex result:</FONT>
<FONT COLOR="#65c265">###############################################################################</FONT>
<FONT COLOR="#65c265"># Establish three connections to the test database as TEST_USER_1, and one</FONT>
<FONT COLOR="#65c265"># connection as SYSDBA.  Then use the Connection.db_info method to query the</FONT>
<FONT COLOR="#65c265"># number of attachments by each user to the test database.</FONT>
testUserCons <FONT COLOR="#7d0000">=</FONT> []
<FONT COLOR="#1413be">for</FONT> i <FONT COLOR="#1413be">in</FONT> <FONT COLOR="#7d7ca4">range</FONT>(<FONT COLOR="#666600">3</FONT>):
    tCon <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>DSN, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'test_user_1'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'pass'</FONT>)
    testUserCons.<FONT COLOR="#000066">append</FONT>(tCon)

con <FONT COLOR="#7d0000">=</FONT> kinterbasdb.<FONT COLOR="#000066">connect</FONT>(dsn<FONT COLOR="#7d0000">=</FONT>DSN, user<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'sysdba'</FONT>, password<FONT COLOR="#7d0000">=</FONT><FONT COLOR="#8f8c47">'masterkey'</FONT>)

<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'Open connections to this database:'</FONT>
<FONT COLOR="#1413be">print</FONT> con.<FONT COLOR="#000066">db_info</FONT>(kinterbasdb.isc_info_user_names)

<FONT COLOR="#65c265">###############################################################################</FONT>
<FONT COLOR="#65c265"># Querying multiple isc_info_* items at once:</FONT>
<FONT COLOR="#65c265">###############################################################################</FONT>
<FONT COLOR="#65c265"># Request multiple db_info items at once, specifically the page size of the</FONT>
<FONT COLOR="#65c265"># database and the number of pages currently allocated.  Compare the size</FONT>
<FONT COLOR="#65c265"># computed by that method with the size reported by the file system.</FONT>
<FONT COLOR="#65c265"># The advantages of using db_info instead of the file system to compute</FONT>
<FONT COLOR="#65c265"># database size are:</FONT>
<FONT COLOR="#65c265">#   - db_info works seamlessly on connections to remote databases that reside</FONT>
<FONT COLOR="#65c265">#     in file systems to which the client program lacks access.</FONT>
<FONT COLOR="#65c265">#   - If the database is split across multiple files, db_info includes all of</FONT>
<FONT COLOR="#65c265">#     them.</FONT>
res <FONT COLOR="#7d0000">=</FONT> con.<FONT COLOR="#000066">db_info</FONT>(
    [kinterbasdb.isc_info_page_size, kinterbasdb.isc_info_allocation]
  )
pagesAllocated <FONT COLOR="#7d0000">=</FONT> res[kinterbasdb.isc_info_allocation]
pageSize <FONT COLOR="#7d0000">=</FONT> res[kinterbasdb.isc_info_page_size]
<FONT COLOR="#1413be">print</FONT> <FONT COLOR="#8f8c47">'\ndb_info indicates database size is'</FONT>, pageSize <FONT COLOR="#7d0000">*</FONT> pagesAllocated, <FONT COLOR="#8f8c47">'bytes'</FONT>
<FONT COLOR="#1413be">print</FONT>   <FONT COLOR="#8f8c47">'os.path.getsize indicates size is '</FONT>, os.path.<FONT COLOR="#000066">getsize</FONT>(DB_FILENAME), <FONT COLOR="#8f8c47">'bytes'</FONT>
</pre>

<p class="textParagraph">
Sample output:
</p>
<pre class="programOutputBlockTight">
Open connections to this database:
{'SYSDBA': 1, 'TEST_USER_1': 3}

db_info indicates database size is 20684800 bytes
os.path.getsize indicates size is  20684800 bytes
</pre>


<br><hr><br>

<a name="special_issues"><h2>Special Issues</h2></a>

<a name="special_issue_concurrency"><h3>Concurrency</h3></a>

<h4>Overview</h4>

<p class="textParagraph" style="margin-bottom: 0.9em;">
Note:  This section will not be comprehensible unless you understand the basic
characteristics of the Firebird server architectures.  These are documented
in the "Classic or Superserver?" section of the
<code>doc/Firebird-1.5-QuickStart.pdf</code> file included with the Firebird
distribution.
</p>

<p class="textParagraph">
Versions of KInterbasDB prior to 3.2 imposed a global lock over all database
client library calls.  This lock, referred to as the Global Database API Lock
(GDAL), must be active for multithreaded client programs to work correctly with
versions of the Firebird client library that do not properly support
concurrency.
Many such versions are still in use, so the GDAL remains active by default in
KInterbasDB 3.2.  To determine whether the client library you're using can
correctly handle concurrent database calls, read this
<a href="firebird-client-library-thread-safety-overview.html">Overview of Firebird Client Library Thread-Safety</a>.
</p>

<p class="textParagraph">
Note that a single client library might have different thread-safety properties
depending on which <em>protocol</em> the client program specifies via the
parameters of <code>kinterbasdb.connect</code>.  For example, the Firebird 1.5
client library on Windows is thread-safe if the remote protocol is used, as in
</p>
<pre class="codeBlockNoTop" style="margin-bottom: 0em; margin-left: 1em;">
kinterbasdb.connect(dsn=r'localhost:C:\temp\test.db', ...)
</pre>
<p class="textParagraph" style="text-indent: 0em;">
but is <em>not</em> thread-safe if the local protocol is used, as in
</p>
<pre class="codeBlockNoTop" style="margin-left: 1em;">
kinterbasdb.connect(dsn=r'C:\temp\test.db', ...)
</pre>


<h4>Selecting and Activating a KInterbasDB Concurrency Level</h4>

<p class="textParagraph">
KInterbasDB 3.2 supports three levels of concurrency:
</p>

<ul>
  <li class="liLong">
    <p class="liLabel">
    Level 0: &nbsp;No lock management whatsoever
    </p>

    <p class="textParagraph">
      If the C preprocessor symbol <code>ENABLE_CONCURRENCY</code> is not
      defined when KInterbasDB is compiled, no lock management at all is
      performed at runtime.  In fact, the code to initialize and manage the
      locks is not even compiled in.
    </p>

    <p class="textParagraph">
      Level 0 is intended only for compiling KInterbasDB on non-threaded
      builds of the Python interpreter.  It would not be desirable for a client
      program running on a normal (threaded) build of the Python interpreter
      to use Level 0, so no overhead is invested in making it possible to
      transition to Level 0 at runtime.
    </p>

    <p class="textParagraph">
      Since Level 0 is intended for use in Python interpreters that have no
      Global Interpreter Lock (GIL), the GIL is not manipulated.
    </p>
  </li>

  <li class="liLong">
    <p class="liLabel">
    Level 1: &nbsp;Global Database API Lock (GDAL) is active
    &nbsp;&nbsp;&nbsp;(this is the default level)
    </p>

    <p class="textParagraph">
      At Level 1, a global lock serializes all calls to the database client
      library.  This lock, called the Global Database API Lock (GDAL), is to
      the database client library as the GIL is to the Python interpreter:  a
      mechanism to guarantee that at most one thread is using the database
      client library at any time.
    </p>

    <p class="textParagraph">
      Level 1 exists to support those versions of Firebird in which the
      client library is not thread-safe at the connection level (see the
      <a href="firebird-client-library-thread-safety-overview.html">Overview of Firebird Client Library Thread-Safety</a>
      for details).
      In environments where the author of KInterbasDB creates
      binaries and distributes them to client programmers, there is no way of
      knowing at compile time which Firebird client library configuration the
      KInterbasDB binaries will be used with.
      Level 1 protects client programmers who are not aware of the
      thread-safety properties of their version of the client library.
      For these reasons, Level 1 is the default, but Level 2 can be selected at
      runtime via the <code>kinterbasdb.init</code> function (see next
      section).
    </p>

    <p class="textParagraph">
      At Level 1, the Python GIL is released and reacquired around most
      database client library calls in order to avoid blocking the entire
      Python process for the duration of the call.
    </p>
  </li>

  <li class="liLong">
    <p class="liLabel">
    Level 2: &nbsp;Global Database API Lock (GDAL) is not active, but
    connection and disconnection are serialized via the GCDL
    </p>

    <p class="textParagraph">
      At Level 2, calls to the database client library are not serialized,
      except for calls to the connection attachment and detachment functions,
      which are serialized by a lock called the Global Connection and
      Disconnection Lock (GCDL).  This limited form of serialization is
      necessary because the Firebird client library makes no guarantees about
      the thread-safety of connection and disconnection.
      Since most client programs written with high concurrency in mind use
      a connection pool that minimizes the need to physically connect and
      disconnect, the GCDL is not a serious impediment to concurrency.
    </p>

    <p class="textParagraph">
      Level 2, which can be activated at runtime by calling
      <code>kinterbasdb.init(concurrency_level=2)</code>, is appropriate for
      client programmers who are aware of the thread-safety guarantees provided
      by their version of the Firebird client library, and have written the
      client program accordingly.
      For details about the thread-safety of various Firebird client library
      versions, see the
      <a href="firebird-client-library-thread-safety-overview.html">Overview of Firebird Client Library Thread-Safety</a>.
    </p>

    <p class="textParagraph">
      At Level 2, the Python GIL is released and reacquired around most
      database client library calls, just as it is at Level 1.
    </p>

  </li>

</ul>

<p class="textParagraph">
Level 1 is the default, so if you don't understand these subtleties, or are using
a client library configuration that is not thread-safe, you do not need to take
any action to achieve thread-safety.
</p>

<p class="textParagraph">
Level 2 can greatly increase the throughput of a database-centric,
multithreaded Python application, so you should use it if possible.

Once you've determined that you're using an appropriate connection protocol
with a capable client library, you can activate Level 2 at runtime with the
following call:
</p>
<pre class="codeBlockNoTop" style="margin-bottom: 0em; margin-left: 1em;">
kinterbasdb.init(concurrency_level=2)
</pre>
<p class="textParagraph">
The <code>kinterbasdb.init</code> function can only be called once during the
life of a process.  If it has not been called explicitly, the function will be
called implicitly when the client program tries to perform any database
operation.  Therefore, the recommended place to call
<code>kinterbasdb.init</code> is at the top level of one of the main modules
of your program.  The importation infrastructure of the Python interpreter
serializes all imports, so calling <code>kinterbasdb.init</code> at import
time avoids the potential for multiple simultaneous calls, which could cause
subtle problems.
</p>


<h4>Caveats</h4>

<ul>
  <li class="liLong">
    <p class="liLabel">
      <code>threadsafety</code> versus <code>concurrency_level</code>
    </p>
    <p class="textParagraph">
      Make sure not to confuse KInterbasDB's <code>concurrency_level</code> with its
      <code>threadsafety</code>.  <code>threadsafety</code>, a module-level
      property required by the Python DB API Specification 2.0, represents the
      highest level of granularity at which the DB API implementation remains
      thread-safe.  KInterbasDB is always
      "<a href="firebird-client-library-thread-safety-overview.html#definition">thread-safe at the connection level</a>"
      (DB API <code>threadsafety 1</code>), regardless of which
      <code>concurrency_level</code> is active.
    </p>
    <p class="textParagraph">
      Think of <code>threadsafety</code> as the level of thread-safety that
      KInterbasDB guarantees, and <code>concurrency_level</code> as the degree
      to which KInterbasDB's internals are able to exploit a client program's
      potential for concurrency.
    </p>
  </li>
</ul>



<h4>Tips on Achieving High Concurrency</h4>

<ul>
  <li class="liLong">
    <p class="liLabel">
      Use the Classic server architecture, but the SuperServer client library.
    </p>
    <p class="textParagraph">
      At the time of this writing (December 2005), the thread-centric Vulcan
      had not been released, so the multi-process Classic architecture was the
      only Firebird server architecture that could take advantage of multiple
      CPUs.
      This means that in most scenarios, Classic is far more
      concurrency-friendly than SuperServer.
    </p>
    <p class="textParagraph">
      The Windows version of Firebird--whether Classic or SuperServer--offers a
      single client library, so the following advice is not relevant to
      Windows.
    </p>
    <p class="textParagraph">
      The non-Windows versions of Firebird Classic include two client
      libraries:
    </p>
    <ul>
      <li>
        <p class="liLabel">
          <code>fbclient</code> (<code>libfbclient.so</code>)
          communicates with the server solely via
          the network protocol (possibly over an emulated network such as the
          local loopback).
          <code>fbclient</code>
          <a href="firebird-client-library-thread-safety-overview.html">is thread-safe in recent versions</a>
          of Firebird.
        </p>
      </li>

      <li>
        <p class="liLabel">
          <code>fbembed</code> (<code>libfbembed.so</code>)
          uses an in-process Classic server to manipulate the database file
          directly.
          <code>fbembed</code> is not thread-safe in any version of Firebird; it
      should never be used with KInterbasDB concurrency level 2.
        </p>
      </li>
    </ul>
    <p class="textParagraph" style="margin-top: 1.1em;">
      At present, the best way to achieve a concurrency-friendly
      KInterbasDB/Firebird configuration is to use a version of KInterbasDB
      linked against
      <a href="firebird-client-library-thread-safety-overview.html">a thread-safe <code>fbclient</code></a>,
      running at concurrency level 2, and communicating with a Classic
      server.
    </p>
    <p class="textParagraph">
      On Linux, such a setup can be created by installing the Classic server,
      then compiling KInterbasDB with the <code>database_lib_name</code>
      option in <code>setup.cfg</code> set to <code>fbclient</code>
      (this is the default setting).  A version of KInterbasDB that was linked
      against <code>fbembed</code> (by setting
      <code>database_lib_name=fbembed</code>) will not work in a multithreaded
      program if the concurrency level is higher than 1.
    </p>
    <p class="textParagraph">
      On Windows, use a Classic server in combination with one of the
      standard KInterbasDB Windows binaries for Firebird 1.5 or later, and
      be sure to set KInterbasDB's concurrency level to 2.
    </p>

  </li>
</ul>


<br>
<hr>
<br>

<a name="faq_fep">
  <h2>Frequently Asked Questions and Frequently Encountered Pitfalls</h2>
</a>


<a name="faq_fep_is_mxdatetime_required">
  <h3 style="margin-bottom: 0em;">Does KInterbasDB require <code>mx.DateTime</code>?</h3>
</a>

<p class="textParagraph" style="font-size: 300%; font-weight: bold; margin-top: 0em; margin-bottom: 0em;">
NO!
</p>

<p class="textParagraph">
KInterbasDB uses <code>mx.DateTime</code> by default due to backward
compatibility constraints, but <code>mx.DateTime</code> is definitely not
required.  The standard library <code>datetime</code> module can be used
just as easily.
You can read
<a href="#adv_param_conv_dynamic_type_translation_deferred_loading">this section</a>
for a detailed explanation, or simply replace the line<br>
&nbsp;&nbsp;<code>import kinterbasdb</code><br>
with<br>
&nbsp;&nbsp;<code>import kinterbasdb; kinterbasdb.init(type_conv=200)</code><br>
in your client program (if you're using a version of Python prior to 2.4,
you'll need to
<a href="http://www.taniquetil.com.ar/facundo/bdvfiles/get_decimal.html">install the <code>decimal</code> module manually</a>).

&nbsp;<a href="#adv_param_conv_dynamic_type_translation_deferred_loading_example">Here</a>
is an example program that uses <code>datetime</code> instead of
<code>mx.DateTime</code>.
</p>

<br><br>


<a name="faq_fep_fixed_point_precise">
  <h3>Precise Fixed Point (<code>NUMERIC</code>/<code>DECIMAL</code>) Handling</h3>
</a>

<p class="textParagraph">
KInterbasDB's
<a href="#adv_param_conv_dynamic_type_translation">dynamic type translation</a>
allows database fixed point types to be handled both precisely and
conveniently, when combined with a full-featured fixed point data type
such as that implemented by the
<a href="http://docs.python.org/lib/module-decimal.html"><code>decimal</code></a>
module that entered the standard library in Python 2.4.
</p>

<p class="textParagraph">
An official implementation of dynamic type translators for the
<code>decimal</code> module is distributed with KInterbasDB in the
<code>kinterbasdb.typeconv_fixed_decimal</code> module.
It can be activated conveniently using the features discussed in
<a href="#adv_param_conv_dynamic_type_translation_deferred_loading">this section</a>,
and demonstrated in
<a href="#adv_param_conv_dynamic_type_translation_deferred_loading_example">this example program</a>.
</p>

<br><br>


<a name="faq_fep_result_set_fields_by_name">
  <h3>Refer to Result Row Fields by Name Rather than Index</h3>
</a>

<p class="textParagraph">
Use the
<a href="#db_api_extensions_and_caveats_cursor_fetchonemap"><code>Cursor.fetch*map</code></a>
series of methods for traditional <code>fetch</code>es, or the
<code>Cursor.itermap</code> method to iterate over mappings rather than
sequences.
Example code appears in the Tutorial section entitled
<a href="#tutorial_execute_sql">"Executing SQL Statements"</a>.
</p>

<br><br>


<a name="faq_fep_unicode">
  <h3>Unicode Fields and KInterbasDB</h3>
</a>

<p class="textParagraph">
For the sake of backward-compatibility, KInterbasDB handles Unicode naively by
default, leaving responsibility for encoding and decoding to the client
programmer.
</p>

<p class="textParagraph">
In KInterbasDB 3.1, a dynamic type translation slot named
<code>'TEXT_UNICODE'</code> was introduced.
The <code>'TEXT_UNICODE'</code> translators are invoked for all
<code>CHAR</code> or <code>VARCHAR</code> fields except those with character
sets <code>NONE</code>, <code>OCTETS</code>, or <code>ASCII</code>.
Due to flaws in the database engine's C API, KInterbasDB is not able to
automatically encode or decode Unicode blobs.
</p>

<p class="textParagraph">
The most convenient way to handle Unicode with KInterbasDB is to combine
the <code>'TEXT_UNICODE'</code> slot with the official translator
implementation in the <code>kinterbasdb.typeconv_text_unicode</code> module.
This can be accomplished either manually, via
<code>[Connection|Cursor].set_type_trans_[in|out]</code>,
or by loading a predefined set of translators via
<code>kinterbasdb.init(type_conv=...)</code>.
The <a href="#adv_param_conv_dynamic_type_translation_deferred_loading_init_codes">table of <code>type_conv</code> codes</a>
lists several that enable automatic Unicode handling.
</p>

<p class="textParagraph">
For more information, see the
<a href="#adv_param_conv_dynamic_type_translation_tbl_signatures">translator signature table</a>
and this
<a href="#adv_param_conv_dynamic_type_translation_deferred_loading_example">example program</a>.
</p>

<br><br>

<a name="faq_fep_embedded_using_with">
  <h3>Using KInterbasDB with Embedded Firebird (Windows Only)</h3>
</a>

<p class="textParagraph">
The Firebird 1.5 Release Notes (<code>ReleaseNotes.pdf</code>, included with RC5
and later) describe Embedded Firebird as "a DLL that merges a single client
attachment with a Firebird Superserver for building very quick and efficient
stand-alone and briefcase applications."
Practically speaking, Embedded Firebird allows an application to use the
database engine without managing an external server process.  This is ideal for
applications that will be distributed to end users, or that must be deployed
on operating systems that support background services poorly, such as Win9x.
</p>

<p class="textParagraph">
The KInterbasDB distribution linked against the Firebird 1.5 client library
fbclient.dll
(kinterbasdb-<em>V.V</em>.win32-FB1.5-py<em>V.V</em>)
works fine with Embedded Firebird.  Only local-protocol connections
are supported, of course, and some of the standalone-server-oriented features
of the Services API are not supported.
</p>

<p class="textParagraph">
For generic Embedded Firebird configuration instructions, refer to the section
of the Firebird 1.5 Release Notes entitled
"Installing Embedded server from a zip kit"
(page 51 of the <code>ReleaseNotes.pdf</code> accompanying Firebird 1.5.0).
</p>

<p class="textParagraph">
Below are specific instructions for installing Embedded Firebird 1.5.2 for use
with Python 2.3 and KInterbasDB 3.2.
</p>

<ol>
  <li>
    <p class="textParagraph">
    Extract the file
    <code>kinterbasdb-3.1.3.win32-all_binaries_pack.zip</code>
    to a temporary directory.  Copy the <code>kinterbasdb</code> directory
    from <code>kinterbasdb-V.V-win32-all-binaries-pack\firebird-1.5\lib.win32-2.3</code>
    to a directory on the PYTHONPATH of the <code>python.exe</code> you intend
    to use Embedded Firebird with
    (for example, <code>the-directory-of-python.exe\Lib\site-packages</code>).
    </p>
  </li>
  <li>
    <p class="textParagraph">
    Extract <code>Firebird-1.5.2.4731_embed_win32.zip</code> to a temporary
    directory.  In the directory where KInterbasDB resides
    (<code>the-directory-of-python.exe\Lib\site-packages\kinterbasdb</code>, in
    this example), create a subdirectory named <code>embedded</code>.
    </p>
    <p class="textParagraph">
    Copy the files
        <code>fbembed.dll</code>,
        <code>firebird.msg</code>,
        and <code>ib_util.dll</code>
    to <code>the-directory-of-python.exe\Lib\site-packages\kinterbasdb\embedded</code>.
    If you intend to use character sets other than ASCII, also copy
    the <code>intl</code> subdirectory, which contains <code>fbintl.dll</code>.
    </p>
  </li>
  <li>
    <p class="textParagraph">
    Rename <code>fbembed.dll</code> to <code>fbclient.dll</code>.
    </p>
    <p class="textParagraph">
    You should now
        have the following file structure:
<pre class="codeBlock" style="margin-top: 0px;">
the-directory-of-python.exe\
  [python.exe, and other Python-related files]
  Lib\
    site-packages\
      kinterbasdb\
        embedded\
          fbclient.dll
          firebird.msg
          ib_util.dll
          intl\         &lt;--only necessary if using character sets other than ASCII
            fbintl.dll  &lt;--^
</pre>
    </p>
  </li>
  <li>
    <p class="textParagraph">
    Run your KInterbasDB-based Python application.  The database engine is
    "embedded" within the same process as your application; no external server
    process is necessary, and no code changes are required.
    </p>
  </li>
</ol>

<br><br>

<a name="faq_fep_services_api_classic_embedded">
  <h3>Services API with the Embedded Server Architecture</h3>
</a>

<p class="textParagraph">
As of Firebird 1.5, the embedded engine supports all aspects of the
Services API that it could be expected to, given that the embedded engine
runs in the same process as the client and has no central "server" supervising
all connections.
For example, backup and restore functionality work, but user management (a
server-level feature disabled in the embedded engine) does not.
</p>

<br><br>

<a name="faq_fep_zope_using_with">
  <h3>Using KInterbasDB with Zope</h3>
</a>

<p class="textParagraph">
There exist at least three Zope adapters based on KInterbasDB; see the
<a href="links.html#zope">links page</a>.
</p>

<br><br>


<br>
<hr>
<br>

<a name="references">
  <h2>References (External Links)</h2>
</a>

<ul>
  <li>
    <a name="ref_fb_docs_directory" href="http://www.firebirdsql.org/index.php?op=doc">Firebird Documentation</a>
    (directory of links)
  </li>
  <li>
    <a name="ref_ib6_docs" href="http://www.firebirdsql.org/index.php?op=doc&id=userdoc">Interbase&reg; 6 Documentation</a>
    (including the SQL Language Reference, the Data Definition Guide
    and the API Guide).
    Although this documentation was written for Interbase&reg; 6, it also
    applies to Firebird.
  </li>
  <li><a name="ref_fixedpoint"><a href="http://fixedpoint.sourceforge.net">fixedpoint module</a>
    Precise, convenient representation of fixed point values such as those
    stored in <code>DECIMAL</code>/<code>NUMERIC</code> fields in Firebird.
    (This module has been largely superseded by the standard library
    <code>Decimal</code> module, as of Python 2.4.)
  </li>
  <li>
    <a name="ref_kidb_links" href="links.html">KInterbasDB links page</a>  A directory of links
    to software and documentation useful in conjunction with KInterbasDB.
  </li>
</ul>

<br>
<hr>
<br>

<a name="feedback">
  <h2>Feedback</h2>
</a>

<p class="textParagraph">
Send feedback about this documentation or the KInterbasDB code to the author
of the current versions of both,
<a href="mailto:woodsplitter@rocketmail.com">David S. Rushby</a>.
</p>

<br>
<hr>
<br>


<a href="index.html">Overall Table of Contents</a>
&nbsp;&nbsp;
<a href="#toc">Usage Guide Table of Contents</a>
&nbsp;&nbsp;
<a href="#top">Top of This Page</a>

<br><br>
<div style="margin-bottom: 1.5em; margin-right: 1.5em; text-align: right;">
<a href="http://www.w3.org/Icons/valid-xhtml10"><img border="0" src="w3c.png"></a>
</div>

<div style="margin-top: 15em;"></div>

</body>
</html>