Debian Codegraph @

src/harlequin_sqlite/cli_options.py

raw
1 from __future__ import annotations
2
3 from pathlib import Path
4 from sqlite3 import Connection
5
6 from harlequin.options import (
7 FlagOption,
8 ListOption,
9 PathOption,
10 SelectOption,
11 TextOption,
12 )
13
14 connection_mode = SelectOption(
15 name="mode",
16 short_decls=["-mode", "-m"],
17 description=(
18 "The mode parameter may be set to either 'ro', 'rw', 'rwc', or 'memory'. "
19 "Attempting to set it to any other value is an error. If 'ro' is specified, "
20 "then the database is opened for read-only access. If the mode option is set "
21 "to 'rw', then the database is opened for read-write (but not create) access. "
22 "Value 'rwc' is equivalent to setting both SQLITE_OPEN_READWRITE and "
23 "SQLITE_OPEN_CREATE. If the mode option is set to 'memory' then a pure "
24 "in-memory database that never reads or writes from disk is used. "
25 "It is an error to specify a value for the mode parameter that is less "
26 "restrictive than the separate --read-only option."
27 ),
28 choices=["ro", "rw", "rwc", "memory"],
29 default="rwc",
30 )
31
32
33 def _float_validator(s: str | None) -> tuple[bool, str]:
34 if s is None:
35 return True, ""
36 try:
37 _ = float(s)
38 except ValueError:
39 return False, f"Cannot convert {s} to a float!"
40 else:
41 return True, ""
42
43
44 lock_timeout = TextOption(
45 name="lock-timeout",
46 description=(
47 "How many seconds the connection should wait before raising an "
48 "OperationalError when a table is locked. If another connection opens a "
49 "transaction to modify a table, that table will be locked until the "
50 "transaction is committed. Default five seconds."
51 ),
52 validator=_float_validator,
53 )
54
55
56 def _int_validator(s: str | None) -> tuple[bool, str]:
57 if s is None:
58 return True, ""
59 try:
60 _ = int(s)
61 except ValueError:
62 return False, f"Cannot convert {s} to an int!"
63 else:
64 return True, ""
65
66
67 detect_types = TextOption(
68 name="detect-types",
69 description=(
70 "Control whether and how data types not natively supported by SQLite are "
71 "looked up to be converted to Python types, using the converters registered "
72 "with register_converter(). Set it to any combination (using |, bitwise or) "
73 "of PARSE_DECLTYPES and PARSE_COLNAMES to enable this. Column names takes "
74 "precedence over declared types if both flags are set. Types cannot be "
75 "detected for generated fields (for example max(data)), even when the "
76 "detect_types parameter is set; str will be returned instead. By default (0), "
77 "type detection is disabled."
78 ),
79 validator=_int_validator,
80 )
81
82 isolation_level = SelectOption(
83 name="isolation-level",
84 description=(
85 "Control legacy transaction handling behaviour. See Connection.isolation_level "
86 "and Transaction control via the isolation_level attribute for more "
87 'information. Can be "DEFERRED" (default), "EXCLUSIVE" or "IMMEDIATE".'
88 ),
89 choices=["DEFERRED", "EXCLUSIVE", "IMMEDIATE"],
90 default="DEFERRED",
91 )
92
93
94 cached_statements = TextOption(
95 name="cached-statements",
96 description=(
97 "The number of statements that sqlite3 should internally cache for this "
98 "connection, to avoid parsing overhead. By default, 128 statements."
99 ),
100 validator=_int_validator,
101 )
102
103
104 init = PathOption(
105 name="init-path",
106 description=(
107 "The path to an initialization script. On startup, Harlequin will execute "
108 "the commands in the script against the attached database."
109 ),
110 short_decls=["-i", "-init"],
111 exists=False,
112 file_okay=True,
113 dir_okay=False,
114 resolve_path=True,
115 path_type=Path,
116 )
117
118 no_init = FlagOption(
119 name="no-init",
120 description="Start Harlequin without executing the initialization script.",
121 )
122
123 extensions = ListOption(
124 name="extension",
125 description=(
126 "Load the SQLite extension from the passed path when starting "
127 "Harlequin. To install multiple extensions, repeat this option."
128 ),
129 short_decls=["-e"],
130 )
131
132 SQLITE_OPTIONS = [
133 init,
134 no_init,
135 connection_mode,
136 lock_timeout,
137 detect_types,
138 cached_statements,
139 ]
140
141 if hasattr(Connection, "enable_load_extension"):
142 SQLITE_OPTIONS.append(extensions)
143
144 # Python 3.12 and lower
145 if not hasattr(Connection, "autocommit"):
146 SQLITE_OPTIONS.append(isolation_level)