1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>apsw.bestpractice — APSW 3.46.0.1 documentation</title>
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=72bcf2f2" />
<link rel="stylesheet" type="text/css" href="../../_static/css/theme.css?v=19f00094" />
<link rel="stylesheet" type="text/css" href="../../_static/apsw.css?v=3c7e2631" />
<link rel="shortcut icon" href="../../_static/favicon.ico"/>
<!--[if lt IE 9]>
<script src="../../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script src="../../_static/jquery.js?v=5d32c60e"></script>
<script src="../../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
<script src="../../_static/documentation_options.js?v=d98f5d2b"></script>
<script src="../../_static/doctools.js?v=9a2dae69"></script>
<script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../../_static/js/theme.js"></script>
<link rel="author" title="About these documents" href="../../about.html" />
<link rel="index" title="Index" href="../../genindex.html" />
<link rel="search" title="Search" href="../../search.html" />
<link rel="copyright" title="Copyright" href="../../copyright.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../../index.html" class="icon icon-home">
APSW
<img src="../../_static/apswlogo.png" class="logo" alt="Logo"/>
</a>
<div class="version">
3.46.0.1
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../about.html">About</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tips.html">Tips</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../example.html">Example/Tour</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../install.html">Installation and customization</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../extensions.html">Extensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../apsw.html">APSW Module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../connection.html">Connections to a database</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../cursor.html">Cursors (executing SQL)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../blob.html">Blob Input/Output</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../backup.html">Backup</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../vtable.html">Virtual Tables</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../vfs.html">Virtual File System (VFS)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../shell.html">Shell</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../bestpractice.html">Best Practice</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../ext.html">Various interesting and useful bits of functionality</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../exceptions.html">Exceptions and Errors</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../execution.html">Execution and tracing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../dbapi.html">DBAPI notes</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../pysqlite.html">sqlite3 module differences</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../benchmarking.html">Benchmarking</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../copyright.html">Copyright and License</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../changes.html">Change History</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../py-modindex.html">Module Index</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../genindex.html">Index</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../../index.html">APSW</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../../index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item"><a href="../index.html">Module code</a></li>
<li class="breadcrumb-item"><a href="../apsw.html">apsw</a></li>
<li class="breadcrumb-item active">apsw.bestpractice</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<h1>Source code for apsw.bestpractice</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="sd">"""Ensure SQLite usage prevents common mistakes, and get best performance."""</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">annotations</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Callable</span>
<span class="kn">import</span> <span class="nn">apsw</span>
<span class="kn">import</span> <span class="nn">apsw.ext</span>
<div class="viewcode-block" id="connection_wal">
<a class="viewcode-back" href="../../bestpractice.html#apsw.bestpractice.connection_wal">[docs]</a>
<span class="k">def</span> <span class="nf">connection_wal</span><span class="p">(</span><span class="n">connection</span><span class="p">:</span> <span class="n">apsw</span><span class="o">.</span><span class="n">Connection</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""Turns on write ahead logging</span>
<span class="sd"> Reduces contention and improves write performance. WAL is</span>
<span class="sd"> `described here <https://www.sqlite.org/wal.html>`__.</span>
<span class="sd"> """</span>
<span class="n">connection</span><span class="o">.</span><span class="n">pragma</span><span class="p">(</span><span class="s2">"journal_mode"</span><span class="p">,</span> <span class="s2">"wal"</span><span class="p">)</span></div>
<div class="viewcode-block" id="connection_busy_timeout">
<a class="viewcode-back" href="../../bestpractice.html#apsw.bestpractice.connection_busy_timeout">[docs]</a>
<span class="k">def</span> <span class="nf">connection_busy_timeout</span><span class="p">(</span><span class="n">connection</span><span class="p">:</span> <span class="n">apsw</span><span class="o">.</span><span class="n">Connection</span><span class="p">,</span> <span class="n">duration_ms</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">100</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""Sets a short busy timeout</span>
<span class="sd"> :param duration_ms: How many thousandths of a second to wait</span>
<span class="sd"> When another thread or process has locked the database, SQLite</span>
<span class="sd"> immediately raises :exc:`apsw.BusyError`. Changing the `busy</span>
<span class="sd"> timeout <https://www.sqlite.org/c3ref/busy_timeout.html>`__ gives</span>
<span class="sd"> a grace period during which SQLite retries.</span>
<span class="sd"> """</span>
<span class="n">connection</span><span class="o">.</span><span class="n">set_busy_timeout</span><span class="p">(</span><span class="n">duration_ms</span><span class="p">)</span></div>
<div class="viewcode-block" id="connection_enable_foreign_keys">
<a class="viewcode-back" href="../../bestpractice.html#apsw.bestpractice.connection_enable_foreign_keys">[docs]</a>
<span class="k">def</span> <span class="nf">connection_enable_foreign_keys</span><span class="p">(</span><span class="n">connection</span><span class="p">:</span> <span class="n">apsw</span><span class="o">.</span><span class="n">Connection</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""Enables foreign key constraints</span>
<span class="sd"> `Foreign keys <https://www.sqlite.org/foreignkeys.html>`__ need to</span>
<span class="sd"> `be enabled <https://www.sqlite.org/foreignkeys.html#fk_enable>`__</span>
<span class="sd"> to have an effect.</span>
<span class="sd"> """</span>
<span class="n">connection</span><span class="o">.</span><span class="n">pragma</span><span class="p">(</span><span class="s2">"foreign_keys"</span><span class="p">,</span> <span class="s2">"ON"</span><span class="p">)</span></div>
<div class="viewcode-block" id="connection_dqs">
<a class="viewcode-back" href="../../bestpractice.html#apsw.bestpractice.connection_dqs">[docs]</a>
<span class="k">def</span> <span class="nf">connection_dqs</span><span class="p">(</span><span class="n">connection</span><span class="p">:</span> <span class="n">apsw</span><span class="o">.</span><span class="n">Connection</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""Double quotes are for identifiers only, not strings</span>
<span class="sd"> Turns off `allowing double quoted strings</span>
<span class="sd"> <https://www.sqlite.org/quirks.html#dblquote>`__ if they don't</span>
<span class="sd"> match any identifier (column/table names etc), making it an error</span>
<span class="sd"> to use double quotes around strings. SQL strings use single</span>
<span class="sd"> quotes.</span>
<span class="sd"> """</span>
<span class="n">connection</span><span class="o">.</span><span class="n">config</span><span class="p">(</span><span class="n">apsw</span><span class="o">.</span><span class="n">SQLITE_DBCONFIG_DQS_DML</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">connection</span><span class="o">.</span><span class="n">config</span><span class="p">(</span><span class="n">apsw</span><span class="o">.</span><span class="n">SQLITE_DBCONFIG_DQS_DDL</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span></div>
<div class="viewcode-block" id="library_logging">
<a class="viewcode-back" href="../../bestpractice.html#apsw.bestpractice.library_logging">[docs]</a>
<span class="k">def</span> <span class="nf">library_logging</span><span class="p">()</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
<span class="w"> </span><span class="sd">"""Forwards SQLite logging to Python logging module</span>
<span class="sd"> See :meth:`apsw.ext.log_sqlite`</span>
<span class="sd"> """</span>
<span class="n">apsw</span><span class="o">.</span><span class="n">ext</span><span class="o">.</span><span class="n">log_sqlite</span><span class="p">()</span></div>
<span class="n">recommended</span><span class="p">:</span> <span class="nb">tuple</span><span class="p">[</span><span class="n">Callable</span><span class="p">,</span> <span class="o">...</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span>
<span class="n">connection_wal</span><span class="p">,</span>
<span class="n">connection_busy_timeout</span><span class="p">,</span>
<span class="n">connection_enable_foreign_keys</span><span class="p">,</span>
<span class="n">connection_dqs</span><span class="p">,</span>
<span class="n">library_logging</span><span class="p">,</span>
<span class="p">)</span>
<span class="s2">"All of them"</span>
<div class="viewcode-block" id="apply">
<a class="viewcode-back" href="../../bestpractice.html#apsw.bestpractice.apply">[docs]</a>
<span class="k">def</span> <span class="nf">apply</span><span class="p">(</span><span class="n">which</span><span class="p">:</span> <span class="nb">tuple</span><span class="p">[</span><span class="n">Callable</span><span class="p">,</span> <span class="o">...</span><span class="p">])</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
<span class="s2">"Applies library immediately and connection to new connections"</span>
<span class="n">hooks</span> <span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="n">Callable</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">func</span> <span class="ow">in</span> <span class="n">which</span><span class="p">:</span>
<span class="k">if</span> <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">"connection_"</span><span class="p">):</span>
<span class="n">hooks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">func</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">best_practise_connection_apply</span><span class="p">(</span><span class="n">connection</span><span class="p">:</span> <span class="n">apsw</span><span class="o">.</span><span class="n">Connection</span><span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span>
<span class="k">for</span> <span class="n">func</span> <span class="ow">in</span> <span class="n">hooks</span><span class="p">:</span>
<span class="n">func</span><span class="p">(</span><span class="n">connection</span><span class="p">)</span>
<span class="n">apsw</span><span class="o">.</span><span class="n">connection_hooks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">best_practise_connection_apply</span><span class="p">)</span></div>
</pre></div>
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>© <a href="../../copyright.html">Copyright</a> 2004-2024, Roger Binns <rogerb@rogerbinns.com>.
<span class="lastupdated">Last updated on Jun 16, 2024.
</span></p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>
|